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-2011 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 char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
46 module_param(qlini_mode, charp, S_IRUGO);
47 MODULE_PARM_DESC(qlini_mode,
48 	"Determines when initiator mode will be enabled. Possible values: "
49 	"\"exclusive\" - initiator mode will be enabled on load, "
50 	"disabled on enabling target mode and then on disabling target mode "
51 	"enabled back; "
52 	"\"disabled\" - initiator mode will never be enabled; "
53 	"\"enabled\" (default) - initiator mode will always stay enabled.");
54 
55 static int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
56 
57 /*
58  * From scsi/fc/fc_fcp.h
59  */
60 enum fcp_resp_rsp_codes {
61 	FCP_TMF_CMPL = 0,
62 	FCP_DATA_LEN_INVALID = 1,
63 	FCP_CMND_FIELDS_INVALID = 2,
64 	FCP_DATA_PARAM_MISMATCH = 3,
65 	FCP_TMF_REJECTED = 4,
66 	FCP_TMF_FAILED = 5,
67 	FCP_TMF_INVALID_LUN = 9,
68 };
69 
70 /*
71  * fc_pri_ta from scsi/fc/fc_fcp.h
72  */
73 #define FCP_PTA_SIMPLE      0   /* simple task attribute */
74 #define FCP_PTA_HEADQ       1   /* head of queue task attribute */
75 #define FCP_PTA_ORDERED     2   /* ordered task attribute */
76 #define FCP_PTA_ACA         4   /* auto. contingent allegiance */
77 #define FCP_PTA_MASK        7   /* mask for task attribute field */
78 #define FCP_PRI_SHIFT       3   /* priority field starts in bit 3 */
79 #define FCP_PRI_RESVD_MASK  0x80        /* reserved bits in priority field */
80 
81 /*
82  * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
83  * must be called under HW lock and could unlock/lock it inside.
84  * It isn't an issue, since in the current implementation on the time when
85  * those functions are called:
86  *
87  *   - Either context is IRQ and only IRQ handler can modify HW data,
88  *     including rings related fields,
89  *
90  *   - Or access to target mode variables from struct qla_tgt doesn't
91  *     cross those functions boundaries, except tgt_stop, which
92  *     additionally protected by irq_cmd_count.
93  */
94 /* Predefs for callbacks handed to qla2xxx LLD */
95 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
96 	struct atio_from_isp *pkt);
97 static void qlt_response_pkt(struct scsi_qla_host *ha, response_t *pkt);
98 static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
99 	int fn, void *iocb, int flags);
100 static void qlt_send_term_exchange(struct scsi_qla_host *ha, struct qla_tgt_cmd
101 	*cmd, struct atio_from_isp *atio, int ha_locked);
102 static void qlt_reject_free_srr_imm(struct scsi_qla_host *ha,
103 	struct qla_tgt_srr_imm *imm, int ha_lock);
104 /*
105  * Global Variables
106  */
107 static struct kmem_cache *qla_tgt_cmd_cachep;
108 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
109 static mempool_t *qla_tgt_mgmt_cmd_mempool;
110 static struct workqueue_struct *qla_tgt_wq;
111 static DEFINE_MUTEX(qla_tgt_mutex);
112 static LIST_HEAD(qla_tgt_glist);
113 
114 /* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list) */
115 static struct qla_tgt_sess *qlt_find_sess_by_port_name(
116 	struct qla_tgt *tgt,
117 	const uint8_t *port_name)
118 {
119 	struct qla_tgt_sess *sess;
120 
121 	list_for_each_entry(sess, &tgt->sess_list, sess_list_entry) {
122 		if (!memcmp(sess->port_name, port_name, WWN_SIZE))
123 			return sess;
124 	}
125 
126 	return NULL;
127 }
128 
129 /* Might release hw lock, then reaquire!! */
130 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
131 {
132 	/* Send marker if required */
133 	if (unlikely(vha->marker_needed != 0)) {
134 		int rc = qla2x00_issue_marker(vha, vha_locked);
135 		if (rc != QLA_SUCCESS) {
136 			ql_dbg(ql_dbg_tgt, vha, 0xe03d,
137 			    "qla_target(%d): issue_marker() failed\n",
138 			    vha->vp_idx);
139 		}
140 		return rc;
141 	}
142 	return QLA_SUCCESS;
143 }
144 
145 static inline
146 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
147 	uint8_t *d_id)
148 {
149 	struct qla_hw_data *ha = vha->hw;
150 	uint8_t vp_idx;
151 
152 	if ((vha->d_id.b.area != d_id[1]) || (vha->d_id.b.domain != d_id[0]))
153 		return NULL;
154 
155 	if (vha->d_id.b.al_pa == d_id[2])
156 		return vha;
157 
158 	BUG_ON(ha->tgt.tgt_vp_map == NULL);
159 	vp_idx = ha->tgt.tgt_vp_map[d_id[2]].idx;
160 	if (likely(test_bit(vp_idx, ha->vp_idx_map)))
161 		return ha->tgt.tgt_vp_map[vp_idx].vha;
162 
163 	return NULL;
164 }
165 
166 static inline
167 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
168 	uint16_t vp_idx)
169 {
170 	struct qla_hw_data *ha = vha->hw;
171 
172 	if (vha->vp_idx == vp_idx)
173 		return vha;
174 
175 	BUG_ON(ha->tgt.tgt_vp_map == NULL);
176 	if (likely(test_bit(vp_idx, ha->vp_idx_map)))
177 		return ha->tgt.tgt_vp_map[vp_idx].vha;
178 
179 	return NULL;
180 }
181 
182 void qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
183 	struct atio_from_isp *atio)
184 {
185 	switch (atio->u.raw.entry_type) {
186 	case ATIO_TYPE7:
187 	{
188 		struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
189 		    atio->u.isp24.fcp_hdr.d_id);
190 		if (unlikely(NULL == host)) {
191 			ql_dbg(ql_dbg_tgt, vha, 0xe03e,
192 			    "qla_target(%d): Received ATIO_TYPE7 "
193 			    "with unknown d_id %x:%x:%x\n", vha->vp_idx,
194 			    atio->u.isp24.fcp_hdr.d_id[0],
195 			    atio->u.isp24.fcp_hdr.d_id[1],
196 			    atio->u.isp24.fcp_hdr.d_id[2]);
197 			break;
198 		}
199 		qlt_24xx_atio_pkt(host, atio);
200 		break;
201 	}
202 
203 	case IMMED_NOTIFY_TYPE:
204 	{
205 		struct scsi_qla_host *host = vha;
206 		struct imm_ntfy_from_isp *entry =
207 		    (struct imm_ntfy_from_isp *)atio;
208 
209 		if ((entry->u.isp24.vp_index != 0xFF) &&
210 		    (entry->u.isp24.nport_handle != 0xFFFF)) {
211 			host = qlt_find_host_by_vp_idx(vha,
212 			    entry->u.isp24.vp_index);
213 			if (unlikely(!host)) {
214 				ql_dbg(ql_dbg_tgt, vha, 0xe03f,
215 				    "qla_target(%d): Received "
216 				    "ATIO (IMMED_NOTIFY_TYPE) "
217 				    "with unknown vp_index %d\n",
218 				    vha->vp_idx, entry->u.isp24.vp_index);
219 				break;
220 			}
221 		}
222 		qlt_24xx_atio_pkt(host, atio);
223 		break;
224 	}
225 
226 	default:
227 		ql_dbg(ql_dbg_tgt, vha, 0xe040,
228 		    "qla_target(%d): Received unknown ATIO atio "
229 		    "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
230 		break;
231 	}
232 
233 	return;
234 }
235 
236 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha, response_t *pkt)
237 {
238 	switch (pkt->entry_type) {
239 	case CTIO_TYPE7:
240 	{
241 		struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
242 		struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
243 		    entry->vp_index);
244 		if (unlikely(!host)) {
245 			ql_dbg(ql_dbg_tgt, vha, 0xe041,
246 			    "qla_target(%d): Response pkt (CTIO_TYPE7) "
247 			    "received, with unknown vp_index %d\n",
248 			    vha->vp_idx, entry->vp_index);
249 			break;
250 		}
251 		qlt_response_pkt(host, pkt);
252 		break;
253 	}
254 
255 	case IMMED_NOTIFY_TYPE:
256 	{
257 		struct scsi_qla_host *host = vha;
258 		struct imm_ntfy_from_isp *entry =
259 		    (struct imm_ntfy_from_isp *)pkt;
260 
261 		host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
262 		if (unlikely(!host)) {
263 			ql_dbg(ql_dbg_tgt, vha, 0xe042,
264 			    "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
265 			    "received, with unknown vp_index %d\n",
266 			    vha->vp_idx, entry->u.isp24.vp_index);
267 			break;
268 		}
269 		qlt_response_pkt(host, pkt);
270 		break;
271 	}
272 
273 	case NOTIFY_ACK_TYPE:
274 	{
275 		struct scsi_qla_host *host = vha;
276 		struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
277 
278 		if (0xFF != entry->u.isp24.vp_index) {
279 			host = qlt_find_host_by_vp_idx(vha,
280 			    entry->u.isp24.vp_index);
281 			if (unlikely(!host)) {
282 				ql_dbg(ql_dbg_tgt, vha, 0xe043,
283 				    "qla_target(%d): Response "
284 				    "pkt (NOTIFY_ACK_TYPE) "
285 				    "received, with unknown "
286 				    "vp_index %d\n", vha->vp_idx,
287 				    entry->u.isp24.vp_index);
288 				break;
289 			}
290 		}
291 		qlt_response_pkt(host, pkt);
292 		break;
293 	}
294 
295 	case ABTS_RECV_24XX:
296 	{
297 		struct abts_recv_from_24xx *entry =
298 		    (struct abts_recv_from_24xx *)pkt;
299 		struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
300 		    entry->vp_index);
301 		if (unlikely(!host)) {
302 			ql_dbg(ql_dbg_tgt, vha, 0xe044,
303 			    "qla_target(%d): Response pkt "
304 			    "(ABTS_RECV_24XX) received, with unknown "
305 			    "vp_index %d\n", vha->vp_idx, entry->vp_index);
306 			break;
307 		}
308 		qlt_response_pkt(host, pkt);
309 		break;
310 	}
311 
312 	case ABTS_RESP_24XX:
313 	{
314 		struct abts_resp_to_24xx *entry =
315 		    (struct abts_resp_to_24xx *)pkt;
316 		struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
317 		    entry->vp_index);
318 		if (unlikely(!host)) {
319 			ql_dbg(ql_dbg_tgt, vha, 0xe045,
320 			    "qla_target(%d): Response pkt "
321 			    "(ABTS_RECV_24XX) received, with unknown "
322 			    "vp_index %d\n", vha->vp_idx, entry->vp_index);
323 			break;
324 		}
325 		qlt_response_pkt(host, pkt);
326 		break;
327 	}
328 
329 	default:
330 		qlt_response_pkt(vha, pkt);
331 		break;
332 	}
333 
334 }
335 
336 static void qlt_free_session_done(struct work_struct *work)
337 {
338 	struct qla_tgt_sess *sess = container_of(work, struct qla_tgt_sess,
339 	    free_work);
340 	struct qla_tgt *tgt = sess->tgt;
341 	struct scsi_qla_host *vha = sess->vha;
342 	struct qla_hw_data *ha = vha->hw;
343 
344 	BUG_ON(!tgt);
345 	/*
346 	 * Release the target session for FC Nexus from fabric module code.
347 	 */
348 	if (sess->se_sess != NULL)
349 		ha->tgt.tgt_ops->free_session(sess);
350 
351 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
352 	    "Unregistration of sess %p finished\n", sess);
353 
354 	kfree(sess);
355 	/*
356 	 * We need to protect against race, when tgt is freed before or
357 	 * inside wake_up()
358 	 */
359 	tgt->sess_count--;
360 	if (tgt->sess_count == 0)
361 		wake_up_all(&tgt->waitQ);
362 }
363 
364 /* ha->hardware_lock supposed to be held on entry */
365 void qlt_unreg_sess(struct qla_tgt_sess *sess)
366 {
367 	struct scsi_qla_host *vha = sess->vha;
368 
369 	vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
370 
371 	list_del(&sess->sess_list_entry);
372 	if (sess->deleted)
373 		list_del(&sess->del_list_entry);
374 
375 	INIT_WORK(&sess->free_work, qlt_free_session_done);
376 	schedule_work(&sess->free_work);
377 }
378 EXPORT_SYMBOL(qlt_unreg_sess);
379 
380 /* ha->hardware_lock supposed to be held on entry */
381 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
382 {
383 	struct qla_hw_data *ha = vha->hw;
384 	struct qla_tgt_sess *sess = NULL;
385 	uint32_t unpacked_lun, lun = 0;
386 	uint16_t loop_id;
387 	int res = 0;
388 	struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
389 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
390 
391 	loop_id = le16_to_cpu(n->u.isp24.nport_handle);
392 	if (loop_id == 0xFFFF) {
393 #if 0 /* FIXME: Re-enable Global event handling.. */
394 		/* Global event */
395 		atomic_inc(&ha->tgt.qla_tgt->tgt_global_resets_count);
396 		qlt_clear_tgt_db(ha->tgt.qla_tgt, 1);
397 		if (!list_empty(&ha->tgt.qla_tgt->sess_list)) {
398 			sess = list_entry(ha->tgt.qla_tgt->sess_list.next,
399 			    typeof(*sess), sess_list_entry);
400 			switch (mcmd) {
401 			case QLA_TGT_NEXUS_LOSS_SESS:
402 				mcmd = QLA_TGT_NEXUS_LOSS;
403 				break;
404 			case QLA_TGT_ABORT_ALL_SESS:
405 				mcmd = QLA_TGT_ABORT_ALL;
406 				break;
407 			case QLA_TGT_NEXUS_LOSS:
408 			case QLA_TGT_ABORT_ALL:
409 				break;
410 			default:
411 				ql_dbg(ql_dbg_tgt, vha, 0xe046,
412 				    "qla_target(%d): Not allowed "
413 				    "command %x in %s", vha->vp_idx,
414 				    mcmd, __func__);
415 				sess = NULL;
416 				break;
417 			}
418 		} else
419 			sess = NULL;
420 #endif
421 	} else {
422 		sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
423 	}
424 
425 	ql_dbg(ql_dbg_tgt, vha, 0xe000,
426 	    "Using sess for qla_tgt_reset: %p\n", sess);
427 	if (!sess) {
428 		res = -ESRCH;
429 		return res;
430 	}
431 
432 	ql_dbg(ql_dbg_tgt, vha, 0xe047,
433 	    "scsi(%ld): resetting (session %p from port "
434 	    "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, "
435 	    "mcmd %x, loop_id %d)\n", vha->host_no, sess,
436 	    sess->port_name[0], sess->port_name[1],
437 	    sess->port_name[2], sess->port_name[3],
438 	    sess->port_name[4], sess->port_name[5],
439 	    sess->port_name[6], sess->port_name[7],
440 	    mcmd, loop_id);
441 
442 	lun = a->u.isp24.fcp_cmnd.lun;
443 	unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
444 
445 	return qlt_issue_task_mgmt(sess, unpacked_lun, mcmd,
446 	    iocb, QLA24XX_MGMT_SEND_NACK);
447 }
448 
449 /* ha->hardware_lock supposed to be held on entry */
450 static void qlt_schedule_sess_for_deletion(struct qla_tgt_sess *sess,
451 	bool immediate)
452 {
453 	struct qla_tgt *tgt = sess->tgt;
454 	uint32_t dev_loss_tmo = tgt->ha->port_down_retry_count + 5;
455 
456 	if (sess->deleted)
457 		return;
458 
459 	ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
460 	    "Scheduling sess %p for deletion\n", sess);
461 	list_add_tail(&sess->del_list_entry, &tgt->del_sess_list);
462 	sess->deleted = 1;
463 
464 	if (immediate)
465 		dev_loss_tmo = 0;
466 
467 	sess->expires = jiffies + dev_loss_tmo * HZ;
468 
469 	ql_dbg(ql_dbg_tgt, sess->vha, 0xe048,
470 	    "qla_target(%d): session for port %02x:%02x:%02x:"
471 	    "%02x:%02x:%02x:%02x:%02x (loop ID %d) scheduled for "
472 	    "deletion in %u secs (expires: %lu) immed: %d\n",
473 	    sess->vha->vp_idx,
474 	    sess->port_name[0], sess->port_name[1],
475 	    sess->port_name[2], sess->port_name[3],
476 	    sess->port_name[4], sess->port_name[5],
477 	    sess->port_name[6], sess->port_name[7],
478 	    sess->loop_id, dev_loss_tmo, sess->expires, immediate);
479 
480 	if (immediate)
481 		schedule_delayed_work(&tgt->sess_del_work, 0);
482 	else
483 		schedule_delayed_work(&tgt->sess_del_work,
484 		    jiffies - sess->expires);
485 }
486 
487 /* ha->hardware_lock supposed to be held on entry */
488 static void qlt_clear_tgt_db(struct qla_tgt *tgt, bool local_only)
489 {
490 	struct qla_tgt_sess *sess;
491 
492 	list_for_each_entry(sess, &tgt->sess_list, sess_list_entry)
493 		qlt_schedule_sess_for_deletion(sess, true);
494 
495 	/* At this point tgt could be already dead */
496 }
497 
498 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
499 	uint16_t *loop_id)
500 {
501 	struct qla_hw_data *ha = vha->hw;
502 	dma_addr_t gid_list_dma;
503 	struct gid_list_info *gid_list;
504 	char *id_iter;
505 	int res, rc, i;
506 	uint16_t entries;
507 
508 	gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
509 	    &gid_list_dma, GFP_KERNEL);
510 	if (!gid_list) {
511 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
512 		    "qla_target(%d): DMA Alloc failed of %u\n",
513 		    vha->vp_idx, qla2x00_gid_list_size(ha));
514 		return -ENOMEM;
515 	}
516 
517 	/* Get list of logged in devices */
518 	rc = qla2x00_get_id_list(vha, gid_list, gid_list_dma, &entries);
519 	if (rc != QLA_SUCCESS) {
520 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
521 		    "qla_target(%d): get_id_list() failed: %x\n",
522 		    vha->vp_idx, rc);
523 		res = -1;
524 		goto out_free_id_list;
525 	}
526 
527 	id_iter = (char *)gid_list;
528 	res = -1;
529 	for (i = 0; i < entries; i++) {
530 		struct gid_list_info *gid = (struct gid_list_info *)id_iter;
531 		if ((gid->al_pa == s_id[2]) &&
532 		    (gid->area == s_id[1]) &&
533 		    (gid->domain == s_id[0])) {
534 			*loop_id = le16_to_cpu(gid->loop_id);
535 			res = 0;
536 			break;
537 		}
538 		id_iter += ha->gid_list_info_size;
539 	}
540 
541 out_free_id_list:
542 	dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
543 	    gid_list, gid_list_dma);
544 	return res;
545 }
546 
547 static bool qlt_check_fcport_exist(struct scsi_qla_host *vha,
548 	struct qla_tgt_sess *sess)
549 {
550 	struct qla_hw_data *ha = vha->hw;
551 	struct qla_port_24xx_data *pmap24;
552 	bool res, found = false;
553 	int rc, i;
554 	uint16_t loop_id = 0xFFFF; /* to eliminate compiler's warning */
555 	uint16_t entries;
556 	void *pmap;
557 	int pmap_len;
558 	fc_port_t *fcport;
559 	int global_resets;
560 	unsigned long flags;
561 
562 retry:
563 	global_resets = atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count);
564 
565 	rc = qla2x00_get_node_name_list(vha, &pmap, &pmap_len);
566 	if (rc != QLA_SUCCESS) {
567 		res = false;
568 		goto out;
569 	}
570 
571 	pmap24 = pmap;
572 	entries = pmap_len/sizeof(*pmap24);
573 
574 	for (i = 0; i < entries; ++i) {
575 		if (!memcmp(sess->port_name, pmap24[i].port_name, WWN_SIZE)) {
576 			loop_id = le16_to_cpu(pmap24[i].loop_id);
577 			found = true;
578 			break;
579 		}
580 	}
581 
582 	kfree(pmap);
583 
584 	if (!found) {
585 		res = false;
586 		goto out;
587 	}
588 
589 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf046,
590 	    "qlt_check_fcport_exist(): loop_id %d", loop_id);
591 
592 	fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
593 	if (fcport == NULL) {
594 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf047,
595 		    "qla_target(%d): Allocation of tmp FC port failed",
596 		    vha->vp_idx);
597 		res = false;
598 		goto out;
599 	}
600 
601 	fcport->loop_id = loop_id;
602 
603 	rc = qla2x00_get_port_database(vha, fcport, 0);
604 	if (rc != QLA_SUCCESS) {
605 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf048,
606 		    "qla_target(%d): Failed to retrieve fcport "
607 		    "information -- get_port_database() returned %x "
608 		    "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
609 		res = false;
610 		goto out_free_fcport;
611 	}
612 
613 	if (global_resets !=
614 	    atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count)) {
615 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
616 		    "qla_target(%d): global reset during session discovery"
617 		    " (counter was %d, new %d), retrying",
618 		    vha->vp_idx, global_resets,
619 		    atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count));
620 		goto retry;
621 	}
622 
623 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
624 	    "Updating sess %p s_id %x:%x:%x, loop_id %d) to d_id %x:%x:%x, "
625 	    "loop_id %d", sess, sess->s_id.b.domain, sess->s_id.b.al_pa,
626 	    sess->s_id.b.area, sess->loop_id, fcport->d_id.b.domain,
627 	    fcport->d_id.b.al_pa, fcport->d_id.b.area, fcport->loop_id);
628 
629 	spin_lock_irqsave(&ha->hardware_lock, flags);
630 	ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
631 				(fcport->flags & FCF_CONF_COMP_SUPPORTED));
632 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
633 
634 	res = true;
635 
636 out_free_fcport:
637 	kfree(fcport);
638 
639 out:
640 	return res;
641 }
642 
643 /* ha->hardware_lock supposed to be held on entry */
644 static void qlt_undelete_sess(struct qla_tgt_sess *sess)
645 {
646 	BUG_ON(!sess->deleted);
647 
648 	list_del(&sess->del_list_entry);
649 	sess->deleted = 0;
650 }
651 
652 static void qlt_del_sess_work_fn(struct delayed_work *work)
653 {
654 	struct qla_tgt *tgt = container_of(work, struct qla_tgt,
655 	    sess_del_work);
656 	struct scsi_qla_host *vha = tgt->vha;
657 	struct qla_hw_data *ha = vha->hw;
658 	struct qla_tgt_sess *sess;
659 	unsigned long flags;
660 
661 	spin_lock_irqsave(&ha->hardware_lock, flags);
662 	while (!list_empty(&tgt->del_sess_list)) {
663 		sess = list_entry(tgt->del_sess_list.next, typeof(*sess),
664 		    del_list_entry);
665 		if (time_after_eq(jiffies, sess->expires)) {
666 			bool cancel;
667 
668 			qlt_undelete_sess(sess);
669 
670 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
671 			cancel = qlt_check_fcport_exist(vha, sess);
672 
673 			if (cancel) {
674 				if (sess->deleted) {
675 					/*
676 					 * sess was again deleted while we were
677 					 * discovering it
678 					 */
679 					spin_lock_irqsave(&ha->hardware_lock,
680 					    flags);
681 					continue;
682 				}
683 
684 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf049,
685 				    "qla_target(%d): cancel deletion of "
686 				    "session for port %02x:%02x:%02x:%02x:%02x:"
687 				    "%02x:%02x:%02x (loop ID %d), because "
688 				    " it isn't deleted by firmware",
689 				    vha->vp_idx, sess->port_name[0],
690 				    sess->port_name[1], sess->port_name[2],
691 				    sess->port_name[3], sess->port_name[4],
692 				    sess->port_name[5], sess->port_name[6],
693 				    sess->port_name[7], sess->loop_id);
694 			} else {
695 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
696 				    "Timeout: sess %p about to be deleted\n",
697 				    sess);
698 				ha->tgt.tgt_ops->shutdown_sess(sess);
699 				ha->tgt.tgt_ops->put_sess(sess);
700 			}
701 
702 			spin_lock_irqsave(&ha->hardware_lock, flags);
703 		} else {
704 			schedule_delayed_work(&tgt->sess_del_work,
705 			    jiffies - sess->expires);
706 			break;
707 		}
708 	}
709 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
710 }
711 
712 /*
713  * Adds an extra ref to allow to drop hw lock after adding sess to the list.
714  * Caller must put it.
715  */
716 static struct qla_tgt_sess *qlt_create_sess(
717 	struct scsi_qla_host *vha,
718 	fc_port_t *fcport,
719 	bool local)
720 {
721 	struct qla_hw_data *ha = vha->hw;
722 	struct qla_tgt_sess *sess;
723 	unsigned long flags;
724 	unsigned char be_sid[3];
725 
726 	/* Check to avoid double sessions */
727 	spin_lock_irqsave(&ha->hardware_lock, flags);
728 	list_for_each_entry(sess, &ha->tgt.qla_tgt->sess_list,
729 				sess_list_entry) {
730 		if (!memcmp(sess->port_name, fcport->port_name, WWN_SIZE)) {
731 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
732 			    "Double sess %p found (s_id %x:%x:%x, "
733 			    "loop_id %d), updating to d_id %x:%x:%x, "
734 			    "loop_id %d", sess, sess->s_id.b.domain,
735 			    sess->s_id.b.al_pa, sess->s_id.b.area,
736 			    sess->loop_id, fcport->d_id.b.domain,
737 			    fcport->d_id.b.al_pa, fcport->d_id.b.area,
738 			    fcport->loop_id);
739 
740 			if (sess->deleted)
741 				qlt_undelete_sess(sess);
742 
743 			kref_get(&sess->se_sess->sess_kref);
744 			ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
745 						(fcport->flags & FCF_CONF_COMP_SUPPORTED));
746 
747 			if (sess->local && !local)
748 				sess->local = 0;
749 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
750 
751 			return sess;
752 		}
753 	}
754 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
755 
756 	sess = kzalloc(sizeof(*sess), GFP_KERNEL);
757 	if (!sess) {
758 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04a,
759 		    "qla_target(%u): session allocation failed, "
760 		    "all commands from port %02x:%02x:%02x:%02x:"
761 		    "%02x:%02x:%02x:%02x will be refused", vha->vp_idx,
762 		    fcport->port_name[0], fcport->port_name[1],
763 		    fcport->port_name[2], fcport->port_name[3],
764 		    fcport->port_name[4], fcport->port_name[5],
765 		    fcport->port_name[6], fcport->port_name[7]);
766 
767 		return NULL;
768 	}
769 	sess->tgt = ha->tgt.qla_tgt;
770 	sess->vha = vha;
771 	sess->s_id = fcport->d_id;
772 	sess->loop_id = fcport->loop_id;
773 	sess->local = local;
774 
775 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
776 	    "Adding sess %p to tgt %p via ->check_initiator_node_acl()\n",
777 	    sess, ha->tgt.qla_tgt);
778 
779 	be_sid[0] = sess->s_id.b.domain;
780 	be_sid[1] = sess->s_id.b.area;
781 	be_sid[2] = sess->s_id.b.al_pa;
782 	/*
783 	 * Determine if this fc_port->port_name is allowed to access
784 	 * target mode using explict NodeACLs+MappedLUNs, or using
785 	 * TPG demo mode.  If this is successful a target mode FC nexus
786 	 * is created.
787 	 */
788 	if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
789 	    &fcport->port_name[0], sess, &be_sid[0], fcport->loop_id) < 0) {
790 		kfree(sess);
791 		return NULL;
792 	}
793 	/*
794 	 * Take an extra reference to ->sess_kref here to handle qla_tgt_sess
795 	 * access across ->hardware_lock reaquire.
796 	 */
797 	kref_get(&sess->se_sess->sess_kref);
798 
799 	sess->conf_compl_supported = (fcport->flags & FCF_CONF_COMP_SUPPORTED);
800 	BUILD_BUG_ON(sizeof(sess->port_name) != sizeof(fcport->port_name));
801 	memcpy(sess->port_name, fcport->port_name, sizeof(sess->port_name));
802 
803 	spin_lock_irqsave(&ha->hardware_lock, flags);
804 	list_add_tail(&sess->sess_list_entry, &ha->tgt.qla_tgt->sess_list);
805 	ha->tgt.qla_tgt->sess_count++;
806 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
807 
808 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
809 	    "qla_target(%d): %ssession for wwn %02x:%02x:%02x:%02x:"
810 	    "%02x:%02x:%02x:%02x (loop_id %d, s_id %x:%x:%x, confirmed"
811 	    " completion %ssupported) added\n",
812 	    vha->vp_idx, local ?  "local " : "", fcport->port_name[0],
813 	    fcport->port_name[1], fcport->port_name[2], fcport->port_name[3],
814 	    fcport->port_name[4], fcport->port_name[5], fcport->port_name[6],
815 	    fcport->port_name[7], fcport->loop_id, sess->s_id.b.domain,
816 	    sess->s_id.b.area, sess->s_id.b.al_pa, sess->conf_compl_supported ?
817 	    "" : "not ");
818 
819 	return sess;
820 }
821 
822 /*
823  * Called from drivers/scsi/qla2xxx/qla_init.c:qla2x00_reg_remote_port()
824  */
825 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
826 {
827 	struct qla_hw_data *ha = vha->hw;
828 	struct qla_tgt *tgt = ha->tgt.qla_tgt;
829 	struct qla_tgt_sess *sess;
830 	unsigned long flags;
831 
832 	if (!vha->hw->tgt.tgt_ops)
833 		return;
834 
835 	if (!tgt || (fcport->port_type != FCT_INITIATOR))
836 		return;
837 
838 	spin_lock_irqsave(&ha->hardware_lock, flags);
839 	if (tgt->tgt_stop) {
840 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
841 		return;
842 	}
843 	sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
844 	if (!sess) {
845 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
846 
847 		mutex_lock(&ha->tgt.tgt_mutex);
848 		sess = qlt_create_sess(vha, fcport, false);
849 		mutex_unlock(&ha->tgt.tgt_mutex);
850 
851 		spin_lock_irqsave(&ha->hardware_lock, flags);
852 	} else {
853 		kref_get(&sess->se_sess->sess_kref);
854 
855 		if (sess->deleted) {
856 			qlt_undelete_sess(sess);
857 
858 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
859 			    "qla_target(%u): %ssession for port %02x:"
860 			    "%02x:%02x:%02x:%02x:%02x:%02x:%02x (loop ID %d) "
861 			    "reappeared\n", vha->vp_idx, sess->local ? "local "
862 			    : "", sess->port_name[0], sess->port_name[1],
863 			    sess->port_name[2], sess->port_name[3],
864 			    sess->port_name[4], sess->port_name[5],
865 			    sess->port_name[6], sess->port_name[7],
866 			    sess->loop_id);
867 
868 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
869 			    "Reappeared sess %p\n", sess);
870 		}
871 		ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
872 					(fcport->flags & FCF_CONF_COMP_SUPPORTED));
873 	}
874 
875 	if (sess && sess->local) {
876 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
877 		    "qla_target(%u): local session for "
878 		    "port %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
879 		    "(loop ID %d) became global\n", vha->vp_idx,
880 		    fcport->port_name[0], fcport->port_name[1],
881 		    fcport->port_name[2], fcport->port_name[3],
882 		    fcport->port_name[4], fcport->port_name[5],
883 		    fcport->port_name[6], fcport->port_name[7],
884 		    sess->loop_id);
885 		sess->local = 0;
886 	}
887 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
888 
889 	ha->tgt.tgt_ops->put_sess(sess);
890 }
891 
892 void qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport)
893 {
894 	struct qla_hw_data *ha = vha->hw;
895 	struct qla_tgt *tgt = ha->tgt.qla_tgt;
896 	struct qla_tgt_sess *sess;
897 	unsigned long flags;
898 
899 	if (!vha->hw->tgt.tgt_ops)
900 		return;
901 
902 	if (!tgt || (fcport->port_type != FCT_INITIATOR))
903 		return;
904 
905 	spin_lock_irqsave(&ha->hardware_lock, flags);
906 	if (tgt->tgt_stop) {
907 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
908 		return;
909 	}
910 	sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
911 	if (!sess) {
912 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
913 		return;
914 	}
915 
916 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
917 
918 	sess->local = 1;
919 	qlt_schedule_sess_for_deletion(sess, false);
920 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
921 }
922 
923 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
924 {
925 	struct qla_hw_data *ha = tgt->ha;
926 	unsigned long flags;
927 	int res;
928 	/*
929 	 * We need to protect against race, when tgt is freed before or
930 	 * inside wake_up()
931 	 */
932 	spin_lock_irqsave(&ha->hardware_lock, flags);
933 	ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
934 	    "tgt %p, empty(sess_list)=%d sess_count=%d\n",
935 	    tgt, list_empty(&tgt->sess_list), tgt->sess_count);
936 	res = (tgt->sess_count == 0);
937 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
938 
939 	return res;
940 }
941 
942 /* Called by tcm_qla2xxx configfs code */
943 void qlt_stop_phase1(struct qla_tgt *tgt)
944 {
945 	struct scsi_qla_host *vha = tgt->vha;
946 	struct qla_hw_data *ha = tgt->ha;
947 	unsigned long flags;
948 
949 	if (tgt->tgt_stop || tgt->tgt_stopped) {
950 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
951 		    "Already in tgt->tgt_stop or tgt_stopped state\n");
952 		dump_stack();
953 		return;
954 	}
955 
956 	ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
957 	    vha->host_no, vha);
958 	/*
959 	 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
960 	 * Lock is needed, because we still can get an incoming packet.
961 	 */
962 	mutex_lock(&ha->tgt.tgt_mutex);
963 	spin_lock_irqsave(&ha->hardware_lock, flags);
964 	tgt->tgt_stop = 1;
965 	qlt_clear_tgt_db(tgt, true);
966 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
967 	mutex_unlock(&ha->tgt.tgt_mutex);
968 
969 	flush_delayed_work(&tgt->sess_del_work);
970 
971 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
972 	    "Waiting for sess works (tgt %p)", tgt);
973 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
974 	while (!list_empty(&tgt->sess_works_list)) {
975 		spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
976 		flush_scheduled_work();
977 		spin_lock_irqsave(&tgt->sess_work_lock, flags);
978 	}
979 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
980 
981 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
982 	    "Waiting for tgt %p: list_empty(sess_list)=%d "
983 	    "sess_count=%d\n", tgt, list_empty(&tgt->sess_list),
984 	    tgt->sess_count);
985 
986 	wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
987 
988 	/* Big hammer */
989 	if (!ha->flags.host_shutting_down && qla_tgt_mode_enabled(vha))
990 		qlt_disable_vha(vha);
991 
992 	/* Wait for sessions to clear out (just in case) */
993 	wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
994 }
995 EXPORT_SYMBOL(qlt_stop_phase1);
996 
997 /* Called by tcm_qla2xxx configfs code */
998 void qlt_stop_phase2(struct qla_tgt *tgt)
999 {
1000 	struct qla_hw_data *ha = tgt->ha;
1001 	unsigned long flags;
1002 
1003 	if (tgt->tgt_stopped) {
1004 		ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf04f,
1005 		    "Already in tgt->tgt_stopped state\n");
1006 		dump_stack();
1007 		return;
1008 	}
1009 
1010 	ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00b,
1011 	    "Waiting for %d IRQ commands to complete (tgt %p)",
1012 	    tgt->irq_cmd_count, tgt);
1013 
1014 	mutex_lock(&ha->tgt.tgt_mutex);
1015 	spin_lock_irqsave(&ha->hardware_lock, flags);
1016 	while (tgt->irq_cmd_count != 0) {
1017 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
1018 		udelay(2);
1019 		spin_lock_irqsave(&ha->hardware_lock, flags);
1020 	}
1021 	tgt->tgt_stop = 0;
1022 	tgt->tgt_stopped = 1;
1023 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1024 	mutex_unlock(&ha->tgt.tgt_mutex);
1025 
1026 	ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00c, "Stop of tgt %p finished",
1027 	    tgt);
1028 }
1029 EXPORT_SYMBOL(qlt_stop_phase2);
1030 
1031 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1032 static void qlt_release(struct qla_tgt *tgt)
1033 {
1034 	struct qla_hw_data *ha = tgt->ha;
1035 
1036 	if ((ha->tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
1037 		qlt_stop_phase2(tgt);
1038 
1039 	ha->tgt.qla_tgt = NULL;
1040 
1041 	ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00d,
1042 	    "Release of tgt %p finished\n", tgt);
1043 
1044 	kfree(tgt);
1045 }
1046 
1047 /* ha->hardware_lock supposed to be held on entry */
1048 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1049 	const void *param, unsigned int param_size)
1050 {
1051 	struct qla_tgt_sess_work_param *prm;
1052 	unsigned long flags;
1053 
1054 	prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1055 	if (!prm) {
1056 		ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1057 		    "qla_target(%d): Unable to create session "
1058 		    "work, command will be refused", 0);
1059 		return -ENOMEM;
1060 	}
1061 
1062 	ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1063 	    "Scheduling work (type %d, prm %p)"
1064 	    " to find session for param %p (size %d, tgt %p)\n",
1065 	    type, prm, param, param_size, tgt);
1066 
1067 	prm->type = type;
1068 	memcpy(&prm->tm_iocb, param, param_size);
1069 
1070 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
1071 	list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1072 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1073 
1074 	schedule_work(&tgt->sess_work);
1075 
1076 	return 0;
1077 }
1078 
1079 /*
1080  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1081  */
1082 static void qlt_send_notify_ack(struct scsi_qla_host *vha,
1083 	struct imm_ntfy_from_isp *ntfy,
1084 	uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1085 	uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1086 {
1087 	struct qla_hw_data *ha = vha->hw;
1088 	request_t *pkt;
1089 	struct nack_to_isp *nack;
1090 
1091 	ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1092 
1093 	/* Send marker if required */
1094 	if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1095 		return;
1096 
1097 	pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
1098 	if (!pkt) {
1099 		ql_dbg(ql_dbg_tgt, vha, 0xe049,
1100 		    "qla_target(%d): %s failed: unable to allocate "
1101 		    "request packet\n", vha->vp_idx, __func__);
1102 		return;
1103 	}
1104 
1105 	if (ha->tgt.qla_tgt != NULL)
1106 		ha->tgt.qla_tgt->notify_ack_expected++;
1107 
1108 	pkt->entry_type = NOTIFY_ACK_TYPE;
1109 	pkt->entry_count = 1;
1110 
1111 	nack = (struct nack_to_isp *)pkt;
1112 	nack->ox_id = ntfy->ox_id;
1113 
1114 	nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1115 	if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1116 		nack->u.isp24.flags = ntfy->u.isp24.flags &
1117 			__constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1118 	}
1119 	nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1120 	nack->u.isp24.status = ntfy->u.isp24.status;
1121 	nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1122 	nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1123 	nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1124 	nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1125 	nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1126 	nack->u.isp24.srr_reject_code = srr_reject_code;
1127 	nack->u.isp24.srr_reject_code_expl = srr_explan;
1128 	nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1129 
1130 	ql_dbg(ql_dbg_tgt, vha, 0xe005,
1131 	    "qla_target(%d): Sending 24xx Notify Ack %d\n",
1132 	    vha->vp_idx, nack->u.isp24.status);
1133 
1134 	qla2x00_start_iocbs(vha, vha->req);
1135 }
1136 
1137 /*
1138  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1139  */
1140 static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
1141 	struct abts_recv_from_24xx *abts, uint32_t status,
1142 	bool ids_reversed)
1143 {
1144 	struct qla_hw_data *ha = vha->hw;
1145 	struct abts_resp_to_24xx *resp;
1146 	uint32_t f_ctl;
1147 	uint8_t *p;
1148 
1149 	ql_dbg(ql_dbg_tgt, vha, 0xe006,
1150 	    "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1151 	    ha, abts, status);
1152 
1153 	/* Send marker if required */
1154 	if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1155 		return;
1156 
1157 	resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
1158 	if (!resp) {
1159 		ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1160 		    "qla_target(%d): %s failed: unable to allocate "
1161 		    "request packet", vha->vp_idx, __func__);
1162 		return;
1163 	}
1164 
1165 	resp->entry_type = ABTS_RESP_24XX;
1166 	resp->entry_count = 1;
1167 	resp->nport_handle = abts->nport_handle;
1168 	resp->vp_index = vha->vp_idx;
1169 	resp->sof_type = abts->sof_type;
1170 	resp->exchange_address = abts->exchange_address;
1171 	resp->fcp_hdr_le = abts->fcp_hdr_le;
1172 	f_ctl = __constant_cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1173 	    F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1174 	    F_CTL_SEQ_INITIATIVE);
1175 	p = (uint8_t *)&f_ctl;
1176 	resp->fcp_hdr_le.f_ctl[0] = *p++;
1177 	resp->fcp_hdr_le.f_ctl[1] = *p++;
1178 	resp->fcp_hdr_le.f_ctl[2] = *p;
1179 	if (ids_reversed) {
1180 		resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1181 		resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1182 		resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1183 		resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1184 		resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1185 		resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1186 	} else {
1187 		resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1188 		resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1189 		resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1190 		resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1191 		resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1192 		resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1193 	}
1194 	resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1195 	if (status == FCP_TMF_CMPL) {
1196 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1197 		resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1198 		resp->payload.ba_acct.low_seq_cnt = 0x0000;
1199 		resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1200 		resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1201 		resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1202 	} else {
1203 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1204 		resp->payload.ba_rjt.reason_code =
1205 			BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1206 		/* Other bytes are zero */
1207 	}
1208 
1209 	ha->tgt.qla_tgt->abts_resp_expected++;
1210 
1211 	qla2x00_start_iocbs(vha, vha->req);
1212 }
1213 
1214 /*
1215  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1216  */
1217 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1218 	struct abts_resp_from_24xx_fw *entry)
1219 {
1220 	struct ctio7_to_24xx *ctio;
1221 
1222 	ql_dbg(ql_dbg_tgt, vha, 0xe007,
1223 	    "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1224 	/* Send marker if required */
1225 	if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1226 		return;
1227 
1228 	ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
1229 	if (ctio == NULL) {
1230 		ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1231 		    "qla_target(%d): %s failed: unable to allocate "
1232 		    "request packet\n", vha->vp_idx, __func__);
1233 		return;
1234 	}
1235 
1236 	/*
1237 	 * We've got on entrance firmware's response on by us generated
1238 	 * ABTS response. So, in it ID fields are reversed.
1239 	 */
1240 
1241 	ctio->entry_type = CTIO_TYPE7;
1242 	ctio->entry_count = 1;
1243 	ctio->nport_handle = entry->nport_handle;
1244 	ctio->handle = QLA_TGT_SKIP_HANDLE |	CTIO_COMPLETION_HANDLE_MARK;
1245 	ctio->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1246 	ctio->vp_index = vha->vp_idx;
1247 	ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1248 	ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1249 	ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1250 	ctio->exchange_addr = entry->exchange_addr_to_abort;
1251 	ctio->u.status1.flags =
1252 	    __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1253 		CTIO7_FLAGS_TERMINATE);
1254 	ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1255 
1256 	qla2x00_start_iocbs(vha, vha->req);
1257 
1258 	qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1259 	    FCP_TMF_CMPL, true);
1260 }
1261 
1262 /* ha->hardware_lock supposed to be held on entry */
1263 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1264 	struct abts_recv_from_24xx *abts, struct qla_tgt_sess *sess)
1265 {
1266 	struct qla_hw_data *ha = vha->hw;
1267 	struct se_session *se_sess = sess->se_sess;
1268 	struct qla_tgt_mgmt_cmd *mcmd;
1269 	struct se_cmd *se_cmd;
1270 	u32 lun = 0;
1271 	int rc;
1272 	bool found_lun = false;
1273 
1274 	spin_lock(&se_sess->sess_cmd_lock);
1275 	list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1276 		struct qla_tgt_cmd *cmd =
1277 			container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
1278 		if (cmd->tag == abts->exchange_addr_to_abort) {
1279 			lun = cmd->unpacked_lun;
1280 			found_lun = true;
1281 			break;
1282 		}
1283 	}
1284 	spin_unlock(&se_sess->sess_cmd_lock);
1285 
1286 	if (!found_lun)
1287 		return -ENOENT;
1288 
1289 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1290 	    "qla_target(%d): task abort (tag=%d)\n",
1291 	    vha->vp_idx, abts->exchange_addr_to_abort);
1292 
1293 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1294 	if (mcmd == NULL) {
1295 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1296 		    "qla_target(%d): %s: Allocation of ABORT cmd failed",
1297 		    vha->vp_idx, __func__);
1298 		return -ENOMEM;
1299 	}
1300 	memset(mcmd, 0, sizeof(*mcmd));
1301 
1302 	mcmd->sess = sess;
1303 	memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
1304 
1305 	rc = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, TMR_ABORT_TASK,
1306 	    abts->exchange_addr_to_abort);
1307 	if (rc != 0) {
1308 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1309 		    "qla_target(%d):  tgt_ops->handle_tmr()"
1310 		    " failed: %d", vha->vp_idx, rc);
1311 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1312 		return -EFAULT;
1313 	}
1314 
1315 	return 0;
1316 }
1317 
1318 /*
1319  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1320  */
1321 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1322 	struct abts_recv_from_24xx *abts)
1323 {
1324 	struct qla_hw_data *ha = vha->hw;
1325 	struct qla_tgt_sess *sess;
1326 	uint32_t tag = abts->exchange_addr_to_abort;
1327 	uint8_t s_id[3];
1328 	int rc;
1329 
1330 	if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1331 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1332 		    "qla_target(%d): ABTS: Abort Sequence not "
1333 		    "supported\n", vha->vp_idx);
1334 		qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1335 		return;
1336 	}
1337 
1338 	if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1339 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1340 		    "qla_target(%d): ABTS: Unknown Exchange "
1341 		    "Address received\n", vha->vp_idx);
1342 		qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1343 		return;
1344 	}
1345 
1346 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1347 	    "qla_target(%d): task abort (s_id=%x:%x:%x, "
1348 	    "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1349 	    abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1350 	    le32_to_cpu(abts->fcp_hdr_le.parameter));
1351 
1352 	s_id[0] = abts->fcp_hdr_le.s_id[2];
1353 	s_id[1] = abts->fcp_hdr_le.s_id[1];
1354 	s_id[2] = abts->fcp_hdr_le.s_id[0];
1355 
1356 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1357 	if (!sess) {
1358 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1359 		    "qla_target(%d): task abort for non-existant session\n",
1360 		    vha->vp_idx);
1361 		rc = qlt_sched_sess_work(ha->tgt.qla_tgt,
1362 		    QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
1363 		if (rc != 0) {
1364 			qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1365 			    false);
1366 		}
1367 		return;
1368 	}
1369 
1370 	rc = __qlt_24xx_handle_abts(vha, abts, sess);
1371 	if (rc != 0) {
1372 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1373 		    "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1374 		    vha->vp_idx, rc);
1375 		qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1376 		return;
1377 	}
1378 }
1379 
1380 /*
1381  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1382  */
1383 static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1384 	struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1385 {
1386 	struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1387 	struct ctio7_to_24xx *ctio;
1388 
1389 	ql_dbg(ql_dbg_tgt, ha, 0xe008,
1390 	    "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1391 	    ha, atio, resp_code);
1392 
1393 	/* Send marker if required */
1394 	if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1395 		return;
1396 
1397 	ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
1398 	if (ctio == NULL) {
1399 		ql_dbg(ql_dbg_tgt, ha, 0xe04c,
1400 		    "qla_target(%d): %s failed: unable to allocate "
1401 		    "request packet\n", ha->vp_idx, __func__);
1402 		return;
1403 	}
1404 
1405 	ctio->entry_type = CTIO_TYPE7;
1406 	ctio->entry_count = 1;
1407 	ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1408 	ctio->nport_handle = mcmd->sess->loop_id;
1409 	ctio->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1410 	ctio->vp_index = ha->vp_idx;
1411 	ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1412 	ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1413 	ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1414 	ctio->exchange_addr = atio->u.isp24.exchange_addr;
1415 	ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
1416 	    __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1417 		CTIO7_FLAGS_SEND_STATUS);
1418 	ctio->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
1419 	ctio->u.status1.scsi_status =
1420 	    __constant_cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
1421 	ctio->u.status1.response_len = __constant_cpu_to_le16(8);
1422 	ctio->u.status1.sense_data[0] = resp_code;
1423 
1424 	qla2x00_start_iocbs(ha, ha->req);
1425 }
1426 
1427 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
1428 {
1429 	mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1430 }
1431 EXPORT_SYMBOL(qlt_free_mcmd);
1432 
1433 /* callback from target fabric module code */
1434 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
1435 {
1436 	struct scsi_qla_host *vha = mcmd->sess->vha;
1437 	struct qla_hw_data *ha = vha->hw;
1438 	unsigned long flags;
1439 
1440 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
1441 	    "TM response mcmd (%p) status %#x state %#x",
1442 	    mcmd, mcmd->fc_tm_rsp, mcmd->flags);
1443 
1444 	spin_lock_irqsave(&ha->hardware_lock, flags);
1445 	if (mcmd->flags == QLA24XX_MGMT_SEND_NACK)
1446 		qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
1447 		    0, 0, 0, 0, 0, 0);
1448 	else {
1449 		if (mcmd->se_cmd.se_tmr_req->function == TMR_ABORT_TASK)
1450 			qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
1451 			    mcmd->fc_tm_rsp, false);
1452 		else
1453 			qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
1454 			    mcmd->fc_tm_rsp);
1455 	}
1456 	/*
1457 	 * Make the callback for ->free_mcmd() to queue_work() and invoke
1458 	 * target_put_sess_cmd() to drop cmd_kref to 1.  The final
1459 	 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
1460 	 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
1461 	 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
1462 	 * qlt_xmit_tm_rsp() returns here..
1463 	 */
1464 	ha->tgt.tgt_ops->free_mcmd(mcmd);
1465 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1466 }
1467 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
1468 
1469 /* No locks */
1470 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
1471 {
1472 	struct qla_tgt_cmd *cmd = prm->cmd;
1473 
1474 	BUG_ON(cmd->sg_cnt == 0);
1475 
1476 	prm->sg = (struct scatterlist *)cmd->sg;
1477 	prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
1478 	    cmd->sg_cnt, cmd->dma_data_direction);
1479 	if (unlikely(prm->seg_cnt == 0))
1480 		goto out_err;
1481 
1482 	prm->cmd->sg_mapped = 1;
1483 
1484 	/*
1485 	 * If greater than four sg entries then we need to allocate
1486 	 * the continuation entries
1487 	 */
1488 	if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
1489 		prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
1490 		    prm->tgt->datasegs_per_cmd, prm->tgt->datasegs_per_cont);
1491 
1492 	ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe009, "seg_cnt=%d, req_cnt=%d\n",
1493 	    prm->seg_cnt, prm->req_cnt);
1494 	return 0;
1495 
1496 out_err:
1497 	ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
1498 	    "qla_target(%d): PCI mapping failed: sg_cnt=%d",
1499 	    0, prm->cmd->sg_cnt);
1500 	return -1;
1501 }
1502 
1503 static inline void qlt_unmap_sg(struct scsi_qla_host *vha,
1504 	struct qla_tgt_cmd *cmd)
1505 {
1506 	struct qla_hw_data *ha = vha->hw;
1507 
1508 	BUG_ON(!cmd->sg_mapped);
1509 	pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
1510 	cmd->sg_mapped = 0;
1511 }
1512 
1513 static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
1514 	uint32_t req_cnt)
1515 {
1516 	struct qla_hw_data *ha = vha->hw;
1517 	device_reg_t __iomem *reg = ha->iobase;
1518 	uint32_t cnt;
1519 
1520 	if (vha->req->cnt < (req_cnt + 2)) {
1521 		cnt = (uint16_t)RD_REG_DWORD(&reg->isp24.req_q_out);
1522 
1523 		ql_dbg(ql_dbg_tgt, vha, 0xe00a,
1524 		    "Request ring circled: cnt=%d, vha->->ring_index=%d, "
1525 		    "vha->req->cnt=%d, req_cnt=%d\n", cnt,
1526 		    vha->req->ring_index, vha->req->cnt, req_cnt);
1527 		if  (vha->req->ring_index < cnt)
1528 			vha->req->cnt = cnt - vha->req->ring_index;
1529 		else
1530 			vha->req->cnt = vha->req->length -
1531 			    (vha->req->ring_index - cnt);
1532 	}
1533 
1534 	if (unlikely(vha->req->cnt < (req_cnt + 2))) {
1535 		ql_dbg(ql_dbg_tgt, vha, 0xe00b,
1536 		    "qla_target(%d): There is no room in the "
1537 		    "request ring: vha->req->ring_index=%d, vha->req->cnt=%d, "
1538 		    "req_cnt=%d\n", vha->vp_idx, vha->req->ring_index,
1539 		    vha->req->cnt, req_cnt);
1540 		return -EAGAIN;
1541 	}
1542 	vha->req->cnt -= req_cnt;
1543 
1544 	return 0;
1545 }
1546 
1547 /*
1548  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1549  */
1550 static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
1551 {
1552 	/* Adjust ring index. */
1553 	vha->req->ring_index++;
1554 	if (vha->req->ring_index == vha->req->length) {
1555 		vha->req->ring_index = 0;
1556 		vha->req->ring_ptr = vha->req->ring;
1557 	} else {
1558 		vha->req->ring_ptr++;
1559 	}
1560 	return (cont_entry_t *)vha->req->ring_ptr;
1561 }
1562 
1563 /* ha->hardware_lock supposed to be held on entry */
1564 static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
1565 {
1566 	struct qla_hw_data *ha = vha->hw;
1567 	uint32_t h;
1568 
1569 	h = ha->tgt.current_handle;
1570 	/* always increment cmd handle */
1571 	do {
1572 		++h;
1573 		if (h > MAX_OUTSTANDING_COMMANDS)
1574 			h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
1575 		if (h == ha->tgt.current_handle) {
1576 			ql_dbg(ql_dbg_tgt, vha, 0xe04e,
1577 			    "qla_target(%d): Ran out of "
1578 			    "empty cmd slots in ha %p\n", vha->vp_idx, ha);
1579 			h = QLA_TGT_NULL_HANDLE;
1580 			break;
1581 		}
1582 	} while ((h == QLA_TGT_NULL_HANDLE) ||
1583 	    (h == QLA_TGT_SKIP_HANDLE) ||
1584 	    (ha->tgt.cmds[h-1] != NULL));
1585 
1586 	if (h != QLA_TGT_NULL_HANDLE)
1587 		ha->tgt.current_handle = h;
1588 
1589 	return h;
1590 }
1591 
1592 /* ha->hardware_lock supposed to be held on entry */
1593 static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
1594 	struct scsi_qla_host *vha)
1595 {
1596 	uint32_t h;
1597 	struct ctio7_to_24xx *pkt;
1598 	struct qla_hw_data *ha = vha->hw;
1599 	struct atio_from_isp *atio = &prm->cmd->atio;
1600 
1601 	pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
1602 	prm->pkt = pkt;
1603 	memset(pkt, 0, sizeof(*pkt));
1604 
1605 	pkt->entry_type = CTIO_TYPE7;
1606 	pkt->entry_count = (uint8_t)prm->req_cnt;
1607 	pkt->vp_index = vha->vp_idx;
1608 
1609 	h = qlt_make_handle(vha);
1610 	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1611 		/*
1612 		 * CTIO type 7 from the firmware doesn't provide a way to
1613 		 * know the initiator's LOOP ID, hence we can't find
1614 		 * the session and, so, the command.
1615 		 */
1616 		return -EAGAIN;
1617 	} else
1618 		ha->tgt.cmds[h-1] = prm->cmd;
1619 
1620 	pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
1621 	pkt->nport_handle = prm->cmd->loop_id;
1622 	pkt->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1623 	pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1624 	pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1625 	pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1626 	pkt->exchange_addr = atio->u.isp24.exchange_addr;
1627 	pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
1628 	pkt->u.status0.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
1629 	pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
1630 
1631 	ql_dbg(ql_dbg_tgt, vha, 0xe00c,
1632 	    "qla_target(%d): handle(cmd) -> %08x, timeout %d, ox_id %#x\n",
1633 	    vha->vp_idx, pkt->handle, QLA_TGT_TIMEOUT,
1634 	    le16_to_cpu(pkt->u.status0.ox_id));
1635 	return 0;
1636 }
1637 
1638 /*
1639  * ha->hardware_lock supposed to be held on entry. We have already made sure
1640  * that there is sufficient amount of request entries to not drop it.
1641  */
1642 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
1643 	struct scsi_qla_host *vha)
1644 {
1645 	int cnt;
1646 	uint32_t *dword_ptr;
1647 	int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1648 
1649 	/* Build continuation packets */
1650 	while (prm->seg_cnt > 0) {
1651 		cont_a64_entry_t *cont_pkt64 =
1652 			(cont_a64_entry_t *)qlt_get_req_pkt(vha);
1653 
1654 		/*
1655 		 * Make sure that from cont_pkt64 none of
1656 		 * 64-bit specific fields used for 32-bit
1657 		 * addressing. Cast to (cont_entry_t *) for
1658 		 * that.
1659 		 */
1660 
1661 		memset(cont_pkt64, 0, sizeof(*cont_pkt64));
1662 
1663 		cont_pkt64->entry_count = 1;
1664 		cont_pkt64->sys_define = 0;
1665 
1666 		if (enable_64bit_addressing) {
1667 			cont_pkt64->entry_type = CONTINUE_A64_TYPE;
1668 			dword_ptr =
1669 			    (uint32_t *)&cont_pkt64->dseg_0_address;
1670 		} else {
1671 			cont_pkt64->entry_type = CONTINUE_TYPE;
1672 			dword_ptr =
1673 			    (uint32_t *)&((cont_entry_t *)
1674 				cont_pkt64)->dseg_0_address;
1675 		}
1676 
1677 		/* Load continuation entry data segments */
1678 		for (cnt = 0;
1679 		    cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
1680 		    cnt++, prm->seg_cnt--) {
1681 			*dword_ptr++ =
1682 			    cpu_to_le32(pci_dma_lo32
1683 				(sg_dma_address(prm->sg)));
1684 			if (enable_64bit_addressing) {
1685 				*dword_ptr++ =
1686 				    cpu_to_le32(pci_dma_hi32
1687 					(sg_dma_address
1688 					(prm->sg)));
1689 			}
1690 			*dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
1691 
1692 			ql_dbg(ql_dbg_tgt, vha, 0xe00d,
1693 			    "S/G Segment Cont. phys_addr=%llx:%llx, len=%d\n",
1694 			    (long long unsigned int)
1695 			    pci_dma_hi32(sg_dma_address(prm->sg)),
1696 			    (long long unsigned int)
1697 			    pci_dma_lo32(sg_dma_address(prm->sg)),
1698 			    (int)sg_dma_len(prm->sg));
1699 
1700 			prm->sg = sg_next(prm->sg);
1701 		}
1702 	}
1703 }
1704 
1705 /*
1706  * ha->hardware_lock supposed to be held on entry. We have already made sure
1707  * that there is sufficient amount of request entries to not drop it.
1708  */
1709 static void qlt_load_data_segments(struct qla_tgt_prm *prm,
1710 	struct scsi_qla_host *vha)
1711 {
1712 	int cnt;
1713 	uint32_t *dword_ptr;
1714 	int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1715 	struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
1716 
1717 	ql_dbg(ql_dbg_tgt, vha, 0xe00e,
1718 	    "iocb->scsi_status=%x, iocb->flags=%x\n",
1719 	    le16_to_cpu(pkt24->u.status0.scsi_status),
1720 	    le16_to_cpu(pkt24->u.status0.flags));
1721 
1722 	pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
1723 
1724 	/* Setup packet address segment pointer */
1725 	dword_ptr = pkt24->u.status0.dseg_0_address;
1726 
1727 	/* Set total data segment count */
1728 	if (prm->seg_cnt)
1729 		pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
1730 
1731 	if (prm->seg_cnt == 0) {
1732 		/* No data transfer */
1733 		*dword_ptr++ = 0;
1734 		*dword_ptr = 0;
1735 		return;
1736 	}
1737 
1738 	/* If scatter gather */
1739 	ql_dbg(ql_dbg_tgt, vha, 0xe00f, "%s", "Building S/G data segments...");
1740 
1741 	/* Load command entry data segments */
1742 	for (cnt = 0;
1743 	    (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
1744 	    cnt++, prm->seg_cnt--) {
1745 		*dword_ptr++ =
1746 		    cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
1747 		if (enable_64bit_addressing) {
1748 			*dword_ptr++ =
1749 			    cpu_to_le32(pci_dma_hi32(
1750 				sg_dma_address(prm->sg)));
1751 		}
1752 		*dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
1753 
1754 		ql_dbg(ql_dbg_tgt, vha, 0xe010,
1755 		    "S/G Segment phys_addr=%llx:%llx, len=%d\n",
1756 		    (long long unsigned int)pci_dma_hi32(sg_dma_address(
1757 		    prm->sg)),
1758 		    (long long unsigned int)pci_dma_lo32(sg_dma_address(
1759 		    prm->sg)),
1760 		    (int)sg_dma_len(prm->sg));
1761 
1762 		prm->sg = sg_next(prm->sg);
1763 	}
1764 
1765 	qlt_load_cont_data_segments(prm, vha);
1766 }
1767 
1768 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
1769 {
1770 	return cmd->bufflen > 0;
1771 }
1772 
1773 /*
1774  * Called without ha->hardware_lock held
1775  */
1776 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
1777 	struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
1778 	uint32_t *full_req_cnt)
1779 {
1780 	struct qla_tgt *tgt = cmd->tgt;
1781 	struct scsi_qla_host *vha = tgt->vha;
1782 	struct qla_hw_data *ha = vha->hw;
1783 	struct se_cmd *se_cmd = &cmd->se_cmd;
1784 
1785 	if (unlikely(cmd->aborted)) {
1786 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
1787 		    "qla_target(%d): terminating exchange "
1788 		    "for aborted cmd=%p (se_cmd=%p, tag=%d)", vha->vp_idx, cmd,
1789 		    se_cmd, cmd->tag);
1790 
1791 		cmd->state = QLA_TGT_STATE_ABORTED;
1792 
1793 		qlt_send_term_exchange(vha, cmd, &cmd->atio, 0);
1794 
1795 		/* !! At this point cmd could be already freed !! */
1796 		return QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED;
1797 	}
1798 
1799 	ql_dbg(ql_dbg_tgt, vha, 0xe011, "qla_target(%d): tag=%u\n",
1800 	    vha->vp_idx, cmd->tag);
1801 
1802 	prm->cmd = cmd;
1803 	prm->tgt = tgt;
1804 	prm->rq_result = scsi_status;
1805 	prm->sense_buffer = &cmd->sense_buffer[0];
1806 	prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
1807 	prm->sg = NULL;
1808 	prm->seg_cnt = -1;
1809 	prm->req_cnt = 1;
1810 	prm->add_status_pkt = 0;
1811 
1812 	ql_dbg(ql_dbg_tgt, vha, 0xe012, "rq_result=%x, xmit_type=%x\n",
1813 	    prm->rq_result, xmit_type);
1814 
1815 	/* Send marker if required */
1816 	if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
1817 		return -EFAULT;
1818 
1819 	ql_dbg(ql_dbg_tgt, vha, 0xe013, "CTIO start: vha(%d)\n", vha->vp_idx);
1820 
1821 	if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
1822 		if  (qlt_pci_map_calc_cnt(prm) != 0)
1823 			return -EAGAIN;
1824 	}
1825 
1826 	*full_req_cnt = prm->req_cnt;
1827 
1828 	if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
1829 		prm->residual = se_cmd->residual_count;
1830 		ql_dbg(ql_dbg_tgt, vha, 0xe014,
1831 		    "Residual underflow: %d (tag %d, "
1832 		    "op %x, bufflen %d, rq_result %x)\n", prm->residual,
1833 		    cmd->tag, se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
1834 		    cmd->bufflen, prm->rq_result);
1835 		prm->rq_result |= SS_RESIDUAL_UNDER;
1836 	} else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
1837 		prm->residual = se_cmd->residual_count;
1838 		ql_dbg(ql_dbg_tgt, vha, 0xe015,
1839 		    "Residual overflow: %d (tag %d, "
1840 		    "op %x, bufflen %d, rq_result %x)\n", prm->residual,
1841 		    cmd->tag, se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
1842 		    cmd->bufflen, prm->rq_result);
1843 		prm->rq_result |= SS_RESIDUAL_OVER;
1844 	}
1845 
1846 	if (xmit_type & QLA_TGT_XMIT_STATUS) {
1847 		/*
1848 		 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
1849 		 * ignored in *xmit_response() below
1850 		 */
1851 		if (qlt_has_data(cmd)) {
1852 			if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
1853 			    (IS_FWI2_CAPABLE(ha) &&
1854 			    (prm->rq_result != 0))) {
1855 				prm->add_status_pkt = 1;
1856 				(*full_req_cnt)++;
1857 			}
1858 		}
1859 	}
1860 
1861 	ql_dbg(ql_dbg_tgt, vha, 0xe016,
1862 	    "req_cnt=%d, full_req_cnt=%d, add_status_pkt=%d\n",
1863 	    prm->req_cnt, *full_req_cnt, prm->add_status_pkt);
1864 
1865 	return 0;
1866 }
1867 
1868 static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
1869 	struct qla_tgt_cmd *cmd, int sending_sense)
1870 {
1871 	if (ha->tgt.enable_class_2)
1872 		return 0;
1873 
1874 	if (sending_sense)
1875 		return cmd->conf_compl_supported;
1876 	else
1877 		return ha->tgt.enable_explicit_conf &&
1878 		    cmd->conf_compl_supported;
1879 }
1880 
1881 #ifdef CONFIG_QLA_TGT_DEBUG_SRR
1882 /*
1883  *  Original taken from the XFS code
1884  */
1885 static unsigned long qlt_srr_random(void)
1886 {
1887 	static int Inited;
1888 	static unsigned long RandomValue;
1889 	static DEFINE_SPINLOCK(lock);
1890 	/* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
1891 	register long rv;
1892 	register long lo;
1893 	register long hi;
1894 	unsigned long flags;
1895 
1896 	spin_lock_irqsave(&lock, flags);
1897 	if (!Inited) {
1898 		RandomValue = jiffies;
1899 		Inited = 1;
1900 	}
1901 	rv = RandomValue;
1902 	hi = rv / 127773;
1903 	lo = rv % 127773;
1904 	rv = 16807 * lo - 2836 * hi;
1905 	if (rv <= 0)
1906 		rv += 2147483647;
1907 	RandomValue = rv;
1908 	spin_unlock_irqrestore(&lock, flags);
1909 	return rv;
1910 }
1911 
1912 static void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1913 {
1914 #if 0 /* This is not a real status packets lost, so it won't lead to SRR */
1915 	if ((*xmit_type & QLA_TGT_XMIT_STATUS) && (qlt_srr_random() % 200)
1916 	    == 50) {
1917 		*xmit_type &= ~QLA_TGT_XMIT_STATUS;
1918 		ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf015,
1919 		    "Dropping cmd %p (tag %d) status", cmd, cmd->tag);
1920 	}
1921 #endif
1922 	/*
1923 	 * It's currently not possible to simulate SRRs for FCP_WRITE without
1924 	 * a physical link layer failure, so don't even try here..
1925 	 */
1926 	if (cmd->dma_data_direction != DMA_FROM_DEVICE)
1927 		return;
1928 
1929 	if (qlt_has_data(cmd) && (cmd->sg_cnt > 1) &&
1930 	    ((qlt_srr_random() % 100) == 20)) {
1931 		int i, leave = 0;
1932 		unsigned int tot_len = 0;
1933 
1934 		while (leave == 0)
1935 			leave = qlt_srr_random() % cmd->sg_cnt;
1936 
1937 		for (i = 0; i < leave; i++)
1938 			tot_len += cmd->sg[i].length;
1939 
1940 		ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf016,
1941 		    "Cutting cmd %p (tag %d) buffer"
1942 		    " tail to len %d, sg_cnt %d (cmd->bufflen %d,"
1943 		    " cmd->sg_cnt %d)", cmd, cmd->tag, tot_len, leave,
1944 		    cmd->bufflen, cmd->sg_cnt);
1945 
1946 		cmd->bufflen = tot_len;
1947 		cmd->sg_cnt = leave;
1948 	}
1949 
1950 	if (qlt_has_data(cmd) && ((qlt_srr_random() % 100) == 70)) {
1951 		unsigned int offset = qlt_srr_random() % cmd->bufflen;
1952 
1953 		ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf017,
1954 		    "Cutting cmd %p (tag %d) buffer head "
1955 		    "to offset %d (cmd->bufflen %d)", cmd, cmd->tag, offset,
1956 		    cmd->bufflen);
1957 		if (offset == 0)
1958 			*xmit_type &= ~QLA_TGT_XMIT_DATA;
1959 		else if (qlt_set_data_offset(cmd, offset)) {
1960 			ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf018,
1961 			    "qlt_set_data_offset() failed (tag %d)", cmd->tag);
1962 		}
1963 	}
1964 }
1965 #else
1966 static inline void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1967 {}
1968 #endif
1969 
1970 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
1971 	struct qla_tgt_prm *prm)
1972 {
1973 	prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
1974 	    (uint32_t)sizeof(ctio->u.status1.sense_data));
1975 	ctio->u.status0.flags |=
1976 	    __constant_cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
1977 	if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
1978 		ctio->u.status0.flags |= __constant_cpu_to_le16(
1979 		    CTIO7_FLAGS_EXPLICIT_CONFORM |
1980 		    CTIO7_FLAGS_CONFORM_REQ);
1981 	}
1982 	ctio->u.status0.residual = cpu_to_le32(prm->residual);
1983 	ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
1984 	if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
1985 		int i;
1986 
1987 		if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
1988 			if (prm->cmd->se_cmd.scsi_status != 0) {
1989 				ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
1990 				    "Skipping EXPLICIT_CONFORM and "
1991 				    "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
1992 				    "non GOOD status\n");
1993 				goto skip_explict_conf;
1994 			}
1995 			ctio->u.status1.flags |= __constant_cpu_to_le16(
1996 			    CTIO7_FLAGS_EXPLICIT_CONFORM |
1997 			    CTIO7_FLAGS_CONFORM_REQ);
1998 		}
1999 skip_explict_conf:
2000 		ctio->u.status1.flags &=
2001 		    ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2002 		ctio->u.status1.flags |=
2003 		    __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2004 		ctio->u.status1.scsi_status |=
2005 		    __constant_cpu_to_le16(SS_SENSE_LEN_VALID);
2006 		ctio->u.status1.sense_length =
2007 		    cpu_to_le16(prm->sense_buffer_len);
2008 		for (i = 0; i < prm->sense_buffer_len/4; i++)
2009 			((uint32_t *)ctio->u.status1.sense_data)[i] =
2010 				cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2011 #if 0
2012 		if (unlikely((prm->sense_buffer_len % 4) != 0)) {
2013 			static int q;
2014 			if (q < 10) {
2015 				ql_dbg(ql_dbg_tgt, vha, 0xe04f,
2016 				    "qla_target(%d): %d bytes of sense "
2017 				    "lost", prm->tgt->ha->vp_idx,
2018 				    prm->sense_buffer_len % 4);
2019 				q++;
2020 			}
2021 		}
2022 #endif
2023 	} else {
2024 		ctio->u.status1.flags &=
2025 		    ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2026 		ctio->u.status1.flags |=
2027 		    __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2028 		ctio->u.status1.sense_length = 0;
2029 		memset(ctio->u.status1.sense_data, 0,
2030 		    sizeof(ctio->u.status1.sense_data));
2031 	}
2032 
2033 	/* Sense with len > 24, is it possible ??? */
2034 }
2035 
2036 /*
2037  * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
2038  * QLA_TGT_XMIT_STATUS for >= 24xx silicon
2039  */
2040 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
2041 	uint8_t scsi_status)
2042 {
2043 	struct scsi_qla_host *vha = cmd->vha;
2044 	struct qla_hw_data *ha = vha->hw;
2045 	struct ctio7_to_24xx *pkt;
2046 	struct qla_tgt_prm prm;
2047 	uint32_t full_req_cnt = 0;
2048 	unsigned long flags = 0;
2049 	int res;
2050 
2051 	memset(&prm, 0, sizeof(prm));
2052 	qlt_check_srr_debug(cmd, &xmit_type);
2053 
2054 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
2055 	    "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, "
2056 	    "cmd->dma_data_direction=%d\n", (xmit_type & QLA_TGT_XMIT_STATUS) ?
2057 	    1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction);
2058 
2059 	res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
2060 	    &full_req_cnt);
2061 	if (unlikely(res != 0)) {
2062 		if (res == QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED)
2063 			return 0;
2064 
2065 		return res;
2066 	}
2067 
2068 	spin_lock_irqsave(&ha->hardware_lock, flags);
2069 
2070 	/* Does F/W have an IOCBs for this request */
2071 	res = qlt_check_reserve_free_req(vha, full_req_cnt);
2072 	if (unlikely(res))
2073 		goto out_unmap_unlock;
2074 
2075 	res = qlt_24xx_build_ctio_pkt(&prm, vha);
2076 	if (unlikely(res != 0))
2077 		goto out_unmap_unlock;
2078 
2079 
2080 	pkt = (struct ctio7_to_24xx *)prm.pkt;
2081 
2082 	if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
2083 		pkt->u.status0.flags |=
2084 		    __constant_cpu_to_le16(CTIO7_FLAGS_DATA_IN |
2085 			CTIO7_FLAGS_STATUS_MODE_0);
2086 
2087 		qlt_load_data_segments(&prm, vha);
2088 
2089 		if (prm.add_status_pkt == 0) {
2090 			if (xmit_type & QLA_TGT_XMIT_STATUS) {
2091 				pkt->u.status0.scsi_status =
2092 				    cpu_to_le16(prm.rq_result);
2093 				pkt->u.status0.residual =
2094 				    cpu_to_le32(prm.residual);
2095 				pkt->u.status0.flags |= __constant_cpu_to_le16(
2096 				    CTIO7_FLAGS_SEND_STATUS);
2097 				if (qlt_need_explicit_conf(ha, cmd, 0)) {
2098 					pkt->u.status0.flags |=
2099 					    __constant_cpu_to_le16(
2100 						CTIO7_FLAGS_EXPLICIT_CONFORM |
2101 						CTIO7_FLAGS_CONFORM_REQ);
2102 				}
2103 			}
2104 
2105 		} else {
2106 			/*
2107 			 * We have already made sure that there is sufficient
2108 			 * amount of request entries to not drop HW lock in
2109 			 * req_pkt().
2110 			 */
2111 			struct ctio7_to_24xx *ctio =
2112 				(struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
2113 
2114 			ql_dbg(ql_dbg_tgt, vha, 0xe019,
2115 			    "Building additional status packet\n");
2116 
2117 			memcpy(ctio, pkt, sizeof(*ctio));
2118 			ctio->entry_count = 1;
2119 			ctio->dseg_count = 0;
2120 			ctio->u.status1.flags &= ~__constant_cpu_to_le16(
2121 			    CTIO7_FLAGS_DATA_IN);
2122 
2123 			/* Real finish is ctio_m1's finish */
2124 			pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
2125 			pkt->u.status0.flags |= __constant_cpu_to_le16(
2126 			    CTIO7_FLAGS_DONT_RET_CTIO);
2127 			qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
2128 			    &prm);
2129 			pr_debug("Status CTIO7: %p\n", ctio);
2130 		}
2131 	} else
2132 		qlt_24xx_init_ctio_to_isp(pkt, &prm);
2133 
2134 
2135 	cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
2136 
2137 	ql_dbg(ql_dbg_tgt, vha, 0xe01a,
2138 	    "Xmitting CTIO7 response pkt for 24xx: %p scsi_status: 0x%02x\n",
2139 	    pkt, scsi_status);
2140 
2141 	qla2x00_start_iocbs(vha, vha->req);
2142 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2143 
2144 	return 0;
2145 
2146 out_unmap_unlock:
2147 	if (cmd->sg_mapped)
2148 		qlt_unmap_sg(vha, cmd);
2149 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2150 
2151 	return res;
2152 }
2153 EXPORT_SYMBOL(qlt_xmit_response);
2154 
2155 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
2156 {
2157 	struct ctio7_to_24xx *pkt;
2158 	struct scsi_qla_host *vha = cmd->vha;
2159 	struct qla_hw_data *ha = vha->hw;
2160 	struct qla_tgt *tgt = cmd->tgt;
2161 	struct qla_tgt_prm prm;
2162 	unsigned long flags;
2163 	int res = 0;
2164 
2165 	memset(&prm, 0, sizeof(prm));
2166 	prm.cmd = cmd;
2167 	prm.tgt = tgt;
2168 	prm.sg = NULL;
2169 	prm.req_cnt = 1;
2170 
2171 	/* Send marker if required */
2172 	if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2173 		return -EIO;
2174 
2175 	ql_dbg(ql_dbg_tgt, vha, 0xe01b, "CTIO_start: vha(%d)",
2176 	    (int)vha->vp_idx);
2177 
2178 	/* Calculate number of entries and segments required */
2179 	if (qlt_pci_map_calc_cnt(&prm) != 0)
2180 		return -EAGAIN;
2181 
2182 	spin_lock_irqsave(&ha->hardware_lock, flags);
2183 
2184 	/* Does F/W have an IOCBs for this request */
2185 	res = qlt_check_reserve_free_req(vha, prm.req_cnt);
2186 	if (res != 0)
2187 		goto out_unlock_free_unmap;
2188 
2189 	res = qlt_24xx_build_ctio_pkt(&prm, vha);
2190 	if (unlikely(res != 0))
2191 		goto out_unlock_free_unmap;
2192 	pkt = (struct ctio7_to_24xx *)prm.pkt;
2193 	pkt->u.status0.flags |= __constant_cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
2194 	    CTIO7_FLAGS_STATUS_MODE_0);
2195 	qlt_load_data_segments(&prm, vha);
2196 
2197 	cmd->state = QLA_TGT_STATE_NEED_DATA;
2198 
2199 	qla2x00_start_iocbs(vha, vha->req);
2200 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2201 
2202 	return res;
2203 
2204 out_unlock_free_unmap:
2205 	if (cmd->sg_mapped)
2206 		qlt_unmap_sg(vha, cmd);
2207 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2208 
2209 	return res;
2210 }
2211 EXPORT_SYMBOL(qlt_rdy_to_xfer);
2212 
2213 /* If hardware_lock held on entry, might drop it, then reaquire */
2214 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
2215 static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
2216 	struct qla_tgt_cmd *cmd,
2217 	struct atio_from_isp *atio)
2218 {
2219 	struct ctio7_to_24xx *ctio24;
2220 	struct qla_hw_data *ha = vha->hw;
2221 	request_t *pkt;
2222 	int ret = 0;
2223 
2224 	ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
2225 
2226 	pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
2227 	if (pkt == NULL) {
2228 		ql_dbg(ql_dbg_tgt, vha, 0xe050,
2229 		    "qla_target(%d): %s failed: unable to allocate "
2230 		    "request packet\n", vha->vp_idx, __func__);
2231 		return -ENOMEM;
2232 	}
2233 
2234 	if (cmd != NULL) {
2235 		if (cmd->state < QLA_TGT_STATE_PROCESSED) {
2236 			ql_dbg(ql_dbg_tgt, vha, 0xe051,
2237 			    "qla_target(%d): Terminating cmd %p with "
2238 			    "incorrect state %d\n", vha->vp_idx, cmd,
2239 			    cmd->state);
2240 		} else
2241 			ret = 1;
2242 	}
2243 
2244 	pkt->entry_count = 1;
2245 	pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2246 
2247 	ctio24 = (struct ctio7_to_24xx *)pkt;
2248 	ctio24->entry_type = CTIO_TYPE7;
2249 	ctio24->nport_handle = cmd ? cmd->loop_id : CTIO7_NHANDLE_UNRECOGNIZED;
2250 	ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
2251 	ctio24->vp_index = vha->vp_idx;
2252 	ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2253 	ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2254 	ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2255 	ctio24->exchange_addr = atio->u.isp24.exchange_addr;
2256 	ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
2257 	    __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
2258 		CTIO7_FLAGS_TERMINATE);
2259 	ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
2260 
2261 	/* Most likely, it isn't needed */
2262 	ctio24->u.status1.residual = get_unaligned((uint32_t *)
2263 	    &atio->u.isp24.fcp_cmnd.add_cdb[
2264 	    atio->u.isp24.fcp_cmnd.add_cdb_len]);
2265 	if (ctio24->u.status1.residual != 0)
2266 		ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
2267 
2268 	qla2x00_start_iocbs(vha, vha->req);
2269 	return ret;
2270 }
2271 
2272 static void qlt_send_term_exchange(struct scsi_qla_host *vha,
2273 	struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked)
2274 {
2275 	unsigned long flags;
2276 	int rc;
2277 
2278 	if (qlt_issue_marker(vha, ha_locked) < 0)
2279 		return;
2280 
2281 	if (ha_locked) {
2282 		rc = __qlt_send_term_exchange(vha, cmd, atio);
2283 		goto done;
2284 	}
2285 	spin_lock_irqsave(&vha->hw->hardware_lock, flags);
2286 	rc = __qlt_send_term_exchange(vha, cmd, atio);
2287 	spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
2288 done:
2289 	if (rc == 1) {
2290 		if (!ha_locked && !in_interrupt())
2291 			msleep(250); /* just in case */
2292 
2293 		vha->hw->tgt.tgt_ops->free_cmd(cmd);
2294 	}
2295 }
2296 
2297 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
2298 {
2299 	BUG_ON(cmd->sg_mapped);
2300 
2301 	if (unlikely(cmd->free_sg))
2302 		kfree(cmd->sg);
2303 	kmem_cache_free(qla_tgt_cmd_cachep, cmd);
2304 }
2305 EXPORT_SYMBOL(qlt_free_cmd);
2306 
2307 /* ha->hardware_lock supposed to be held on entry */
2308 static int qlt_prepare_srr_ctio(struct scsi_qla_host *vha,
2309 	struct qla_tgt_cmd *cmd, void *ctio)
2310 {
2311 	struct qla_tgt_srr_ctio *sc;
2312 	struct qla_hw_data *ha = vha->hw;
2313 	struct qla_tgt *tgt = ha->tgt.qla_tgt;
2314 	struct qla_tgt_srr_imm *imm;
2315 
2316 	tgt->ctio_srr_id++;
2317 
2318 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
2319 	    "qla_target(%d): CTIO with SRR status received\n", vha->vp_idx);
2320 
2321 	if (!ctio) {
2322 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf055,
2323 		    "qla_target(%d): SRR CTIO, but ctio is NULL\n",
2324 		    vha->vp_idx);
2325 		return -EINVAL;
2326 	}
2327 
2328 	sc = kzalloc(sizeof(*sc), GFP_ATOMIC);
2329 	if (sc != NULL) {
2330 		sc->cmd = cmd;
2331 		/* IRQ is already OFF */
2332 		spin_lock(&tgt->srr_lock);
2333 		sc->srr_id = tgt->ctio_srr_id;
2334 		list_add_tail(&sc->srr_list_entry,
2335 		    &tgt->srr_ctio_list);
2336 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
2337 		    "CTIO SRR %p added (id %d)\n", sc, sc->srr_id);
2338 		if (tgt->imm_srr_id == tgt->ctio_srr_id) {
2339 			int found = 0;
2340 			list_for_each_entry(imm, &tgt->srr_imm_list,
2341 			    srr_list_entry) {
2342 				if (imm->srr_id == sc->srr_id) {
2343 					found = 1;
2344 					break;
2345 				}
2346 			}
2347 			if (found) {
2348 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01b,
2349 				    "Scheduling srr work\n");
2350 				schedule_work(&tgt->srr_work);
2351 			} else {
2352 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf056,
2353 				    "qla_target(%d): imm_srr_id "
2354 				    "== ctio_srr_id (%d), but there is no "
2355 				    "corresponding SRR IMM, deleting CTIO "
2356 				    "SRR %p\n", vha->vp_idx,
2357 				    tgt->ctio_srr_id, sc);
2358 				list_del(&sc->srr_list_entry);
2359 				spin_unlock(&tgt->srr_lock);
2360 
2361 				kfree(sc);
2362 				return -EINVAL;
2363 			}
2364 		}
2365 		spin_unlock(&tgt->srr_lock);
2366 	} else {
2367 		struct qla_tgt_srr_imm *ti;
2368 
2369 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf057,
2370 		    "qla_target(%d): Unable to allocate SRR CTIO entry\n",
2371 		    vha->vp_idx);
2372 		spin_lock(&tgt->srr_lock);
2373 		list_for_each_entry_safe(imm, ti, &tgt->srr_imm_list,
2374 		    srr_list_entry) {
2375 			if (imm->srr_id == tgt->ctio_srr_id) {
2376 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01c,
2377 				    "IMM SRR %p deleted (id %d)\n",
2378 				    imm, imm->srr_id);
2379 				list_del(&imm->srr_list_entry);
2380 				qlt_reject_free_srr_imm(vha, imm, 1);
2381 			}
2382 		}
2383 		spin_unlock(&tgt->srr_lock);
2384 
2385 		return -ENOMEM;
2386 	}
2387 
2388 	return 0;
2389 }
2390 
2391 /*
2392  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2393  */
2394 static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
2395 	struct qla_tgt_cmd *cmd, uint32_t status)
2396 {
2397 	int term = 0;
2398 
2399 	if (ctio != NULL) {
2400 		struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
2401 		term = !(c->flags &
2402 		    __constant_cpu_to_le16(OF_TERM_EXCH));
2403 	} else
2404 		term = 1;
2405 
2406 	if (term)
2407 		qlt_send_term_exchange(vha, cmd, &cmd->atio, 1);
2408 
2409 	return term;
2410 }
2411 
2412 /* ha->hardware_lock supposed to be held on entry */
2413 static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
2414 	uint32_t handle)
2415 {
2416 	struct qla_hw_data *ha = vha->hw;
2417 
2418 	handle--;
2419 	if (ha->tgt.cmds[handle] != NULL) {
2420 		struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
2421 		ha->tgt.cmds[handle] = NULL;
2422 		return cmd;
2423 	} else
2424 		return NULL;
2425 }
2426 
2427 /* ha->hardware_lock supposed to be held on entry */
2428 static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
2429 	uint32_t handle, void *ctio)
2430 {
2431 	struct qla_tgt_cmd *cmd = NULL;
2432 
2433 	/* Clear out internal marks */
2434 	handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
2435 	    CTIO_INTERMEDIATE_HANDLE_MARK);
2436 
2437 	if (handle != QLA_TGT_NULL_HANDLE) {
2438 		if (unlikely(handle == QLA_TGT_SKIP_HANDLE)) {
2439 			ql_dbg(ql_dbg_tgt, vha, 0xe01d, "%s",
2440 			    "SKIP_HANDLE CTIO\n");
2441 			return NULL;
2442 		}
2443 		/* handle-1 is actually used */
2444 		if (unlikely(handle > MAX_OUTSTANDING_COMMANDS)) {
2445 			ql_dbg(ql_dbg_tgt, vha, 0xe052,
2446 			    "qla_target(%d): Wrong handle %x received\n",
2447 			    vha->vp_idx, handle);
2448 			return NULL;
2449 		}
2450 		cmd = qlt_get_cmd(vha, handle);
2451 		if (unlikely(cmd == NULL)) {
2452 			ql_dbg(ql_dbg_tgt, vha, 0xe053,
2453 			    "qla_target(%d): Suspicious: unable to "
2454 			    "find the command with handle %x\n", vha->vp_idx,
2455 			    handle);
2456 			return NULL;
2457 		}
2458 	} else if (ctio != NULL) {
2459 		/* We can't get loop ID from CTIO7 */
2460 		ql_dbg(ql_dbg_tgt, vha, 0xe054,
2461 		    "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
2462 		    "support NULL handles\n", vha->vp_idx);
2463 		return NULL;
2464 	}
2465 
2466 	return cmd;
2467 }
2468 
2469 /*
2470  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2471  */
2472 static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
2473 	uint32_t status, void *ctio)
2474 {
2475 	struct qla_hw_data *ha = vha->hw;
2476 	struct se_cmd *se_cmd;
2477 	struct target_core_fabric_ops *tfo;
2478 	struct qla_tgt_cmd *cmd;
2479 
2480 	ql_dbg(ql_dbg_tgt, vha, 0xe01e,
2481 	    "qla_target(%d): handle(ctio %p status %#x) <- %08x\n",
2482 	    vha->vp_idx, ctio, status, handle);
2483 
2484 	if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
2485 		/* That could happen only in case of an error/reset/abort */
2486 		if (status != CTIO_SUCCESS) {
2487 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
2488 			    "Intermediate CTIO received"
2489 			    " (status %x)\n", status);
2490 		}
2491 		return;
2492 	}
2493 
2494 	cmd = qlt_ctio_to_cmd(vha, handle, ctio);
2495 	if (cmd == NULL)
2496 		return;
2497 
2498 	se_cmd = &cmd->se_cmd;
2499 	tfo = se_cmd->se_tfo;
2500 
2501 	if (cmd->sg_mapped)
2502 		qlt_unmap_sg(vha, cmd);
2503 
2504 	if (unlikely(status != CTIO_SUCCESS)) {
2505 		switch (status & 0xFFFF) {
2506 		case CTIO_LIP_RESET:
2507 		case CTIO_TARGET_RESET:
2508 		case CTIO_ABORTED:
2509 		case CTIO_TIMEOUT:
2510 		case CTIO_INVALID_RX_ID:
2511 			/* They are OK */
2512 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
2513 			    "qla_target(%d): CTIO with "
2514 			    "status %#x received, state %x, se_cmd %p, "
2515 			    "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
2516 			    "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
2517 			    status, cmd->state, se_cmd);
2518 			break;
2519 
2520 		case CTIO_PORT_LOGGED_OUT:
2521 		case CTIO_PORT_UNAVAILABLE:
2522 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
2523 			    "qla_target(%d): CTIO with PORT LOGGED "
2524 			    "OUT (29) or PORT UNAVAILABLE (28) status %x "
2525 			    "received (state %x, se_cmd %p)\n", vha->vp_idx,
2526 			    status, cmd->state, se_cmd);
2527 			break;
2528 
2529 		case CTIO_SRR_RECEIVED:
2530 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05a,
2531 			    "qla_target(%d): CTIO with SRR_RECEIVED"
2532 			    " status %x received (state %x, se_cmd %p)\n",
2533 			    vha->vp_idx, status, cmd->state, se_cmd);
2534 			if (qlt_prepare_srr_ctio(vha, cmd, ctio) != 0)
2535 				break;
2536 			else
2537 				return;
2538 
2539 		default:
2540 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
2541 			    "qla_target(%d): CTIO with error status "
2542 			    "0x%x received (state %x, se_cmd %p\n",
2543 			    vha->vp_idx, status, cmd->state, se_cmd);
2544 			break;
2545 		}
2546 
2547 		if (cmd->state != QLA_TGT_STATE_NEED_DATA)
2548 			if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
2549 				return;
2550 	}
2551 
2552 	if (cmd->state == QLA_TGT_STATE_PROCESSED) {
2553 		ql_dbg(ql_dbg_tgt, vha, 0xe01f, "Command %p finished\n", cmd);
2554 	} else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
2555 		int rx_status = 0;
2556 
2557 		cmd->state = QLA_TGT_STATE_DATA_IN;
2558 
2559 		if (unlikely(status != CTIO_SUCCESS))
2560 			rx_status = -EIO;
2561 		else
2562 			cmd->write_data_transferred = 1;
2563 
2564 		ql_dbg(ql_dbg_tgt, vha, 0xe020,
2565 		    "Data received, context %x, rx_status %d\n",
2566 		    0x0, rx_status);
2567 
2568 		ha->tgt.tgt_ops->handle_data(cmd);
2569 		return;
2570 	} else if (cmd->state == QLA_TGT_STATE_ABORTED) {
2571 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
2572 		    "Aborted command %p (tag %d) finished\n", cmd, cmd->tag);
2573 	} else {
2574 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
2575 		    "qla_target(%d): A command in state (%d) should "
2576 		    "not return a CTIO complete\n", vha->vp_idx, cmd->state);
2577 	}
2578 
2579 	if (unlikely(status != CTIO_SUCCESS)) {
2580 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
2581 		dump_stack();
2582 	}
2583 
2584 	ha->tgt.tgt_ops->free_cmd(cmd);
2585 }
2586 
2587 /* ha->hardware_lock supposed to be held on entry */
2588 /* called via callback from qla2xxx */
2589 void qlt_ctio_completion(struct scsi_qla_host *vha, uint32_t handle)
2590 {
2591 	struct qla_hw_data *ha = vha->hw;
2592 	struct qla_tgt *tgt = ha->tgt.qla_tgt;
2593 
2594 	if (likely(tgt == NULL)) {
2595 		ql_dbg(ql_dbg_tgt, vha, 0xe021,
2596 		    "CTIO, but target mode not enabled"
2597 		    " (ha %d %p handle %#x)", vha->vp_idx, ha, handle);
2598 		return;
2599 	}
2600 
2601 	tgt->irq_cmd_count++;
2602 	qlt_do_ctio_completion(vha, handle, CTIO_SUCCESS, NULL);
2603 	tgt->irq_cmd_count--;
2604 }
2605 
2606 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
2607 	uint8_t task_codes)
2608 {
2609 	int fcp_task_attr;
2610 
2611 	switch (task_codes) {
2612 	case ATIO_SIMPLE_QUEUE:
2613 		fcp_task_attr = MSG_SIMPLE_TAG;
2614 		break;
2615 	case ATIO_HEAD_OF_QUEUE:
2616 		fcp_task_attr = MSG_HEAD_TAG;
2617 		break;
2618 	case ATIO_ORDERED_QUEUE:
2619 		fcp_task_attr = MSG_ORDERED_TAG;
2620 		break;
2621 	case ATIO_ACA_QUEUE:
2622 		fcp_task_attr = MSG_ACA_TAG;
2623 		break;
2624 	case ATIO_UNTAGGED:
2625 		fcp_task_attr = MSG_SIMPLE_TAG;
2626 		break;
2627 	default:
2628 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
2629 		    "qla_target: unknown task code %x, use ORDERED instead\n",
2630 		    task_codes);
2631 		fcp_task_attr = MSG_ORDERED_TAG;
2632 		break;
2633 	}
2634 
2635 	return fcp_task_attr;
2636 }
2637 
2638 static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *,
2639 					uint8_t *);
2640 /*
2641  * Process context for I/O path into tcm_qla2xxx code
2642  */
2643 static void qlt_do_work(struct work_struct *work)
2644 {
2645 	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
2646 	scsi_qla_host_t *vha = cmd->vha;
2647 	struct qla_hw_data *ha = vha->hw;
2648 	struct qla_tgt *tgt = ha->tgt.qla_tgt;
2649 	struct qla_tgt_sess *sess = NULL;
2650 	struct atio_from_isp *atio = &cmd->atio;
2651 	unsigned char *cdb;
2652 	unsigned long flags;
2653 	uint32_t data_length;
2654 	int ret, fcp_task_attr, data_dir, bidi = 0;
2655 
2656 	if (tgt->tgt_stop)
2657 		goto out_term;
2658 
2659 	spin_lock_irqsave(&ha->hardware_lock, flags);
2660 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
2661 	    atio->u.isp24.fcp_hdr.s_id);
2662 	/* Do kref_get() before dropping qla_hw_data->hardware_lock. */
2663 	if (sess)
2664 		kref_get(&sess->se_sess->sess_kref);
2665 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2666 
2667 	if (unlikely(!sess)) {
2668 		uint8_t *s_id =	atio->u.isp24.fcp_hdr.s_id;
2669 
2670 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
2671 			"qla_target(%d): Unable to find wwn login"
2672 			" (s_id %x:%x:%x), trying to create it manually\n",
2673 			vha->vp_idx, s_id[0], s_id[1], s_id[2]);
2674 
2675 		if (atio->u.raw.entry_count > 1) {
2676 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
2677 				"Dropping multy entry cmd %p\n", cmd);
2678 			goto out_term;
2679 		}
2680 
2681 		mutex_lock(&ha->tgt.tgt_mutex);
2682 		sess = qlt_make_local_sess(vha, s_id);
2683 		/* sess has an extra creation ref. */
2684 		mutex_unlock(&ha->tgt.tgt_mutex);
2685 
2686 		if (!sess)
2687 			goto out_term;
2688 	}
2689 
2690 	cmd->sess = sess;
2691 	cmd->loop_id = sess->loop_id;
2692 	cmd->conf_compl_supported = sess->conf_compl_supported;
2693 
2694 	cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
2695 	cmd->tag = atio->u.isp24.exchange_addr;
2696 	cmd->unpacked_lun = scsilun_to_int(
2697 	    (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
2698 
2699 	if (atio->u.isp24.fcp_cmnd.rddata &&
2700 	    atio->u.isp24.fcp_cmnd.wrdata) {
2701 		bidi = 1;
2702 		data_dir = DMA_TO_DEVICE;
2703 	} else if (atio->u.isp24.fcp_cmnd.rddata)
2704 		data_dir = DMA_FROM_DEVICE;
2705 	else if (atio->u.isp24.fcp_cmnd.wrdata)
2706 		data_dir = DMA_TO_DEVICE;
2707 	else
2708 		data_dir = DMA_NONE;
2709 
2710 	fcp_task_attr = qlt_get_fcp_task_attr(vha,
2711 	    atio->u.isp24.fcp_cmnd.task_attr);
2712 	data_length = be32_to_cpu(get_unaligned((uint32_t *)
2713 	    &atio->u.isp24.fcp_cmnd.add_cdb[
2714 	    atio->u.isp24.fcp_cmnd.add_cdb_len]));
2715 
2716 	ql_dbg(ql_dbg_tgt, vha, 0xe022,
2717 	    "qla_target: START qla command: %p lun: 0x%04x (tag %d)\n",
2718 	    cmd, cmd->unpacked_lun, cmd->tag);
2719 
2720 	ret = vha->hw->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
2721 	    fcp_task_attr, data_dir, bidi);
2722 	if (ret != 0)
2723 		goto out_term;
2724 	/*
2725 	 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
2726 	 */
2727 	ha->tgt.tgt_ops->put_sess(sess);
2728 	return;
2729 
2730 out_term:
2731 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf020, "Terminating work cmd %p", cmd);
2732 	/*
2733 	 * cmd has not sent to target yet, so pass NULL as the second
2734 	 * argument to qlt_send_term_exchange() and free the memory here.
2735 	 */
2736 	spin_lock_irqsave(&ha->hardware_lock, flags);
2737 	qlt_send_term_exchange(vha, NULL, &cmd->atio, 1);
2738 	kmem_cache_free(qla_tgt_cmd_cachep, cmd);
2739 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2740 	if (sess)
2741 		ha->tgt.tgt_ops->put_sess(sess);
2742 }
2743 
2744 /* ha->hardware_lock supposed to be held on entry */
2745 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
2746 	struct atio_from_isp *atio)
2747 {
2748 	struct qla_hw_data *ha = vha->hw;
2749 	struct qla_tgt *tgt = ha->tgt.qla_tgt;
2750 	struct qla_tgt_cmd *cmd;
2751 
2752 	if (unlikely(tgt->tgt_stop)) {
2753 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
2754 		    "New command while device %p is shutting down\n", tgt);
2755 		return -EFAULT;
2756 	}
2757 
2758 	cmd = kmem_cache_zalloc(qla_tgt_cmd_cachep, GFP_ATOMIC);
2759 	if (!cmd) {
2760 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05e,
2761 		    "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
2762 		return -ENOMEM;
2763 	}
2764 
2765 	INIT_LIST_HEAD(&cmd->cmd_list);
2766 
2767 	memcpy(&cmd->atio, atio, sizeof(*atio));
2768 	cmd->state = QLA_TGT_STATE_NEW;
2769 	cmd->tgt = ha->tgt.qla_tgt;
2770 	cmd->vha = vha;
2771 
2772 	INIT_WORK(&cmd->work, qlt_do_work);
2773 	queue_work(qla_tgt_wq, &cmd->work);
2774 	return 0;
2775 
2776 }
2777 
2778 /* ha->hardware_lock supposed to be held on entry */
2779 static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
2780 	int fn, void *iocb, int flags)
2781 {
2782 	struct scsi_qla_host *vha = sess->vha;
2783 	struct qla_hw_data *ha = vha->hw;
2784 	struct qla_tgt_mgmt_cmd *mcmd;
2785 	int res;
2786 	uint8_t tmr_func;
2787 
2788 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2789 	if (!mcmd) {
2790 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
2791 		    "qla_target(%d): Allocation of management "
2792 		    "command failed, some commands and their data could "
2793 		    "leak\n", vha->vp_idx);
2794 		return -ENOMEM;
2795 	}
2796 	memset(mcmd, 0, sizeof(*mcmd));
2797 	mcmd->sess = sess;
2798 
2799 	if (iocb) {
2800 		memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
2801 		    sizeof(mcmd->orig_iocb.imm_ntfy));
2802 	}
2803 	mcmd->tmr_func = fn;
2804 	mcmd->flags = flags;
2805 
2806 	switch (fn) {
2807 	case QLA_TGT_CLEAR_ACA:
2808 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10000,
2809 		    "qla_target(%d): CLEAR_ACA received\n", sess->vha->vp_idx);
2810 		tmr_func = TMR_CLEAR_ACA;
2811 		break;
2812 
2813 	case QLA_TGT_TARGET_RESET:
2814 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10001,
2815 		    "qla_target(%d): TARGET_RESET received\n",
2816 		    sess->vha->vp_idx);
2817 		tmr_func = TMR_TARGET_WARM_RESET;
2818 		break;
2819 
2820 	case QLA_TGT_LUN_RESET:
2821 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10002,
2822 		    "qla_target(%d): LUN_RESET received\n", sess->vha->vp_idx);
2823 		tmr_func = TMR_LUN_RESET;
2824 		break;
2825 
2826 	case QLA_TGT_CLEAR_TS:
2827 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10003,
2828 		    "qla_target(%d): CLEAR_TS received\n", sess->vha->vp_idx);
2829 		tmr_func = TMR_CLEAR_TASK_SET;
2830 		break;
2831 
2832 	case QLA_TGT_ABORT_TS:
2833 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10004,
2834 		    "qla_target(%d): ABORT_TS received\n", sess->vha->vp_idx);
2835 		tmr_func = TMR_ABORT_TASK_SET;
2836 		break;
2837 #if 0
2838 	case QLA_TGT_ABORT_ALL:
2839 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10005,
2840 		    "qla_target(%d): Doing ABORT_ALL_TASKS\n",
2841 		    sess->vha->vp_idx);
2842 		tmr_func = 0;
2843 		break;
2844 
2845 	case QLA_TGT_ABORT_ALL_SESS:
2846 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10006,
2847 		    "qla_target(%d): Doing ABORT_ALL_TASKS_SESS\n",
2848 		    sess->vha->vp_idx);
2849 		tmr_func = 0;
2850 		break;
2851 
2852 	case QLA_TGT_NEXUS_LOSS_SESS:
2853 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10007,
2854 		    "qla_target(%d): Doing NEXUS_LOSS_SESS\n",
2855 		    sess->vha->vp_idx);
2856 		tmr_func = 0;
2857 		break;
2858 
2859 	case QLA_TGT_NEXUS_LOSS:
2860 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10008,
2861 		    "qla_target(%d): Doing NEXUS_LOSS\n", sess->vha->vp_idx);
2862 		tmr_func = 0;
2863 		break;
2864 #endif
2865 	default:
2866 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000a,
2867 		    "qla_target(%d): Unknown task mgmt fn 0x%x\n",
2868 		    sess->vha->vp_idx, fn);
2869 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2870 		return -ENOSYS;
2871 	}
2872 
2873 	res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, tmr_func, 0);
2874 	if (res != 0) {
2875 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
2876 		    "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
2877 		    sess->vha->vp_idx, res);
2878 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2879 		return -EFAULT;
2880 	}
2881 
2882 	return 0;
2883 }
2884 
2885 /* ha->hardware_lock supposed to be held on entry */
2886 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
2887 {
2888 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
2889 	struct qla_hw_data *ha = vha->hw;
2890 	struct qla_tgt *tgt;
2891 	struct qla_tgt_sess *sess;
2892 	uint32_t lun, unpacked_lun;
2893 	int lun_size, fn;
2894 
2895 	tgt = ha->tgt.qla_tgt;
2896 
2897 	lun = a->u.isp24.fcp_cmnd.lun;
2898 	lun_size = sizeof(a->u.isp24.fcp_cmnd.lun);
2899 	fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
2900 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
2901 	    a->u.isp24.fcp_hdr.s_id);
2902 	unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
2903 
2904 	if (!sess) {
2905 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
2906 		    "qla_target(%d): task mgmt fn 0x%x for "
2907 		    "non-existant session\n", vha->vp_idx, fn);
2908 		return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
2909 		    sizeof(struct atio_from_isp));
2910 	}
2911 
2912 	return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
2913 }
2914 
2915 /* ha->hardware_lock supposed to be held on entry */
2916 static int __qlt_abort_task(struct scsi_qla_host *vha,
2917 	struct imm_ntfy_from_isp *iocb, struct qla_tgt_sess *sess)
2918 {
2919 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
2920 	struct qla_hw_data *ha = vha->hw;
2921 	struct qla_tgt_mgmt_cmd *mcmd;
2922 	uint32_t lun, unpacked_lun;
2923 	int rc;
2924 
2925 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2926 	if (mcmd == NULL) {
2927 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
2928 		    "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
2929 		    vha->vp_idx, __func__);
2930 		return -ENOMEM;
2931 	}
2932 	memset(mcmd, 0, sizeof(*mcmd));
2933 
2934 	mcmd->sess = sess;
2935 	memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
2936 	    sizeof(mcmd->orig_iocb.imm_ntfy));
2937 
2938 	lun = a->u.isp24.fcp_cmnd.lun;
2939 	unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
2940 
2941 	rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, TMR_ABORT_TASK,
2942 	    le16_to_cpu(iocb->u.isp2x.seq_id));
2943 	if (rc != 0) {
2944 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
2945 		    "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2946 		    vha->vp_idx, rc);
2947 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2948 		return -EFAULT;
2949 	}
2950 
2951 	return 0;
2952 }
2953 
2954 /* ha->hardware_lock supposed to be held on entry */
2955 static int qlt_abort_task(struct scsi_qla_host *vha,
2956 	struct imm_ntfy_from_isp *iocb)
2957 {
2958 	struct qla_hw_data *ha = vha->hw;
2959 	struct qla_tgt_sess *sess;
2960 	int loop_id;
2961 
2962 	loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
2963 
2964 	sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
2965 	if (sess == NULL) {
2966 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
2967 		    "qla_target(%d): task abort for unexisting "
2968 		    "session\n", vha->vp_idx);
2969 		return qlt_sched_sess_work(ha->tgt.qla_tgt,
2970 		    QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
2971 	}
2972 
2973 	return __qlt_abort_task(vha, iocb, sess);
2974 }
2975 
2976 /*
2977  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2978  */
2979 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
2980 	struct imm_ntfy_from_isp *iocb)
2981 {
2982 	struct qla_hw_data *ha = vha->hw;
2983 	int res = 0;
2984 
2985 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf026,
2986 	    "qla_target(%d): Port ID: 0x%02x:%02x:%02x"
2987 	    " ELS opcode: 0x%02x\n", vha->vp_idx, iocb->u.isp24.port_id[0],
2988 	    iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[2],
2989 	    iocb->u.isp24.status_subcode);
2990 
2991 	switch (iocb->u.isp24.status_subcode) {
2992 	case ELS_PLOGI:
2993 	case ELS_FLOGI:
2994 	case ELS_PRLI:
2995 	case ELS_LOGO:
2996 	case ELS_PRLO:
2997 		res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
2998 		break;
2999 	case ELS_PDISC:
3000 	case ELS_ADISC:
3001 	{
3002 		struct qla_tgt *tgt = ha->tgt.qla_tgt;
3003 		if (tgt->link_reinit_iocb_pending) {
3004 			qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
3005 			    0, 0, 0, 0, 0, 0);
3006 			tgt->link_reinit_iocb_pending = 0;
3007 		}
3008 		res = 1; /* send notify ack */
3009 		break;
3010 	}
3011 
3012 	default:
3013 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
3014 		    "qla_target(%d): Unsupported ELS command %x "
3015 		    "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
3016 		res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
3017 		break;
3018 	}
3019 
3020 	return res;
3021 }
3022 
3023 static int qlt_set_data_offset(struct qla_tgt_cmd *cmd, uint32_t offset)
3024 {
3025 	struct scatterlist *sg, *sgp, *sg_srr, *sg_srr_start = NULL;
3026 	size_t first_offset = 0, rem_offset = offset, tmp = 0;
3027 	int i, sg_srr_cnt, bufflen = 0;
3028 
3029 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe023,
3030 	    "Entering qla_tgt_set_data_offset: cmd: %p, cmd->sg: %p, "
3031 	    "cmd->sg_cnt: %u, direction: %d\n",
3032 	    cmd, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
3033 
3034 	/*
3035 	 * FIXME: Reject non zero SRR relative offset until we can test
3036 	 * this code properly.
3037 	 */
3038 	pr_debug("Rejecting non zero SRR rel_offs: %u\n", offset);
3039 	return -1;
3040 
3041 	if (!cmd->sg || !cmd->sg_cnt) {
3042 		ql_dbg(ql_dbg_tgt, cmd->vha, 0xe055,
3043 		    "Missing cmd->sg or zero cmd->sg_cnt in"
3044 		    " qla_tgt_set_data_offset\n");
3045 		return -EINVAL;
3046 	}
3047 	/*
3048 	 * Walk the current cmd->sg list until we locate the new sg_srr_start
3049 	 */
3050 	for_each_sg(cmd->sg, sg, cmd->sg_cnt, i) {
3051 		ql_dbg(ql_dbg_tgt, cmd->vha, 0xe024,
3052 		    "sg[%d]: %p page: %p, length: %d, offset: %d\n",
3053 		    i, sg, sg_page(sg), sg->length, sg->offset);
3054 
3055 		if ((sg->length + tmp) > offset) {
3056 			first_offset = rem_offset;
3057 			sg_srr_start = sg;
3058 			ql_dbg(ql_dbg_tgt, cmd->vha, 0xe025,
3059 			    "Found matching sg[%d], using %p as sg_srr_start, "
3060 			    "and using first_offset: %zu\n", i, sg,
3061 			    first_offset);
3062 			break;
3063 		}
3064 		tmp += sg->length;
3065 		rem_offset -= sg->length;
3066 	}
3067 
3068 	if (!sg_srr_start) {
3069 		ql_dbg(ql_dbg_tgt, cmd->vha, 0xe056,
3070 		    "Unable to locate sg_srr_start for offset: %u\n", offset);
3071 		return -EINVAL;
3072 	}
3073 	sg_srr_cnt = (cmd->sg_cnt - i);
3074 
3075 	sg_srr = kzalloc(sizeof(struct scatterlist) * sg_srr_cnt, GFP_KERNEL);
3076 	if (!sg_srr) {
3077 		ql_dbg(ql_dbg_tgt, cmd->vha, 0xe057,
3078 		    "Unable to allocate sgp\n");
3079 		return -ENOMEM;
3080 	}
3081 	sg_init_table(sg_srr, sg_srr_cnt);
3082 	sgp = &sg_srr[0];
3083 	/*
3084 	 * Walk the remaining list for sg_srr_start, mapping to the newly
3085 	 * allocated sg_srr taking first_offset into account.
3086 	 */
3087 	for_each_sg(sg_srr_start, sg, sg_srr_cnt, i) {
3088 		if (first_offset) {
3089 			sg_set_page(sgp, sg_page(sg),
3090 			    (sg->length - first_offset), first_offset);
3091 			first_offset = 0;
3092 		} else {
3093 			sg_set_page(sgp, sg_page(sg), sg->length, 0);
3094 		}
3095 		bufflen += sgp->length;
3096 
3097 		sgp = sg_next(sgp);
3098 		if (!sgp)
3099 			break;
3100 	}
3101 
3102 	cmd->sg = sg_srr;
3103 	cmd->sg_cnt = sg_srr_cnt;
3104 	cmd->bufflen = bufflen;
3105 	cmd->offset += offset;
3106 	cmd->free_sg = 1;
3107 
3108 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe026, "New cmd->sg: %p\n", cmd->sg);
3109 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe027, "New cmd->sg_cnt: %u\n",
3110 	    cmd->sg_cnt);
3111 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe028, "New cmd->bufflen: %u\n",
3112 	    cmd->bufflen);
3113 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe029, "New cmd->offset: %u\n",
3114 	    cmd->offset);
3115 
3116 	if (cmd->sg_cnt < 0)
3117 		BUG();
3118 
3119 	if (cmd->bufflen < 0)
3120 		BUG();
3121 
3122 	return 0;
3123 }
3124 
3125 static inline int qlt_srr_adjust_data(struct qla_tgt_cmd *cmd,
3126 	uint32_t srr_rel_offs, int *xmit_type)
3127 {
3128 	int res = 0, rel_offs;
3129 
3130 	rel_offs = srr_rel_offs - cmd->offset;
3131 	ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf027, "srr_rel_offs=%d, rel_offs=%d",
3132 	    srr_rel_offs, rel_offs);
3133 
3134 	*xmit_type = QLA_TGT_XMIT_ALL;
3135 
3136 	if (rel_offs < 0) {
3137 		ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf062,
3138 		    "qla_target(%d): SRR rel_offs (%d) < 0",
3139 		    cmd->vha->vp_idx, rel_offs);
3140 		res = -1;
3141 	} else if (rel_offs == cmd->bufflen)
3142 		*xmit_type = QLA_TGT_XMIT_STATUS;
3143 	else if (rel_offs > 0)
3144 		res = qlt_set_data_offset(cmd, rel_offs);
3145 
3146 	return res;
3147 }
3148 
3149 /* No locks, thread context */
3150 static void qlt_handle_srr(struct scsi_qla_host *vha,
3151 	struct qla_tgt_srr_ctio *sctio, struct qla_tgt_srr_imm *imm)
3152 {
3153 	struct imm_ntfy_from_isp *ntfy =
3154 	    (struct imm_ntfy_from_isp *)&imm->imm_ntfy;
3155 	struct qla_hw_data *ha = vha->hw;
3156 	struct qla_tgt_cmd *cmd = sctio->cmd;
3157 	struct se_cmd *se_cmd = &cmd->se_cmd;
3158 	unsigned long flags;
3159 	int xmit_type = 0, resp = 0;
3160 	uint32_t offset;
3161 	uint16_t srr_ui;
3162 
3163 	offset = le32_to_cpu(ntfy->u.isp24.srr_rel_offs);
3164 	srr_ui = ntfy->u.isp24.srr_ui;
3165 
3166 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf028, "SRR cmd %p, srr_ui %x\n",
3167 	    cmd, srr_ui);
3168 
3169 	switch (srr_ui) {
3170 	case SRR_IU_STATUS:
3171 		spin_lock_irqsave(&ha->hardware_lock, flags);
3172 		qlt_send_notify_ack(vha, ntfy,
3173 		    0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3174 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
3175 		xmit_type = QLA_TGT_XMIT_STATUS;
3176 		resp = 1;
3177 		break;
3178 	case SRR_IU_DATA_IN:
3179 		if (!cmd->sg || !cmd->sg_cnt) {
3180 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf063,
3181 			    "Unable to process SRR_IU_DATA_IN due to"
3182 			    " missing cmd->sg, state: %d\n", cmd->state);
3183 			dump_stack();
3184 			goto out_reject;
3185 		}
3186 		if (se_cmd->scsi_status != 0) {
3187 			ql_dbg(ql_dbg_tgt, vha, 0xe02a,
3188 			    "Rejecting SRR_IU_DATA_IN with non GOOD "
3189 			    "scsi_status\n");
3190 			goto out_reject;
3191 		}
3192 		cmd->bufflen = se_cmd->data_length;
3193 
3194 		if (qlt_has_data(cmd)) {
3195 			if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
3196 				goto out_reject;
3197 			spin_lock_irqsave(&ha->hardware_lock, flags);
3198 			qlt_send_notify_ack(vha, ntfy,
3199 			    0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3200 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
3201 			resp = 1;
3202 		} else {
3203 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf064,
3204 			    "qla_target(%d): SRR for in data for cmd "
3205 			    "without them (tag %d, SCSI status %d), "
3206 			    "reject", vha->vp_idx, cmd->tag,
3207 			    cmd->se_cmd.scsi_status);
3208 			goto out_reject;
3209 		}
3210 		break;
3211 	case SRR_IU_DATA_OUT:
3212 		if (!cmd->sg || !cmd->sg_cnt) {
3213 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf065,
3214 			    "Unable to process SRR_IU_DATA_OUT due to"
3215 			    " missing cmd->sg\n");
3216 			dump_stack();
3217 			goto out_reject;
3218 		}
3219 		if (se_cmd->scsi_status != 0) {
3220 			ql_dbg(ql_dbg_tgt, vha, 0xe02b,
3221 			    "Rejecting SRR_IU_DATA_OUT"
3222 			    " with non GOOD scsi_status\n");
3223 			goto out_reject;
3224 		}
3225 		cmd->bufflen = se_cmd->data_length;
3226 
3227 		if (qlt_has_data(cmd)) {
3228 			if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
3229 				goto out_reject;
3230 			spin_lock_irqsave(&ha->hardware_lock, flags);
3231 			qlt_send_notify_ack(vha, ntfy,
3232 			    0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3233 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
3234 			if (xmit_type & QLA_TGT_XMIT_DATA)
3235 				qlt_rdy_to_xfer(cmd);
3236 		} else {
3237 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf066,
3238 			    "qla_target(%d): SRR for out data for cmd "
3239 			    "without them (tag %d, SCSI status %d), "
3240 			    "reject", vha->vp_idx, cmd->tag,
3241 			    cmd->se_cmd.scsi_status);
3242 			goto out_reject;
3243 		}
3244 		break;
3245 	default:
3246 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf067,
3247 		    "qla_target(%d): Unknown srr_ui value %x",
3248 		    vha->vp_idx, srr_ui);
3249 		goto out_reject;
3250 	}
3251 
3252 	/* Transmit response in case of status and data-in cases */
3253 	if (resp)
3254 		qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
3255 
3256 	return;
3257 
3258 out_reject:
3259 	spin_lock_irqsave(&ha->hardware_lock, flags);
3260 	qlt_send_notify_ack(vha, ntfy, 0, 0, 0,
3261 	    NOTIFY_ACK_SRR_FLAGS_REJECT,
3262 	    NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3263 	    NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3264 	if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3265 		cmd->state = QLA_TGT_STATE_DATA_IN;
3266 		dump_stack();
3267 	} else
3268 		qlt_send_term_exchange(vha, cmd, &cmd->atio, 1);
3269 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3270 }
3271 
3272 static void qlt_reject_free_srr_imm(struct scsi_qla_host *vha,
3273 	struct qla_tgt_srr_imm *imm, int ha_locked)
3274 {
3275 	struct qla_hw_data *ha = vha->hw;
3276 	unsigned long flags = 0;
3277 
3278 	if (!ha_locked)
3279 		spin_lock_irqsave(&ha->hardware_lock, flags);
3280 
3281 	qlt_send_notify_ack(vha, (void *)&imm->imm_ntfy, 0, 0, 0,
3282 	    NOTIFY_ACK_SRR_FLAGS_REJECT,
3283 	    NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3284 	    NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3285 
3286 	if (!ha_locked)
3287 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
3288 
3289 	kfree(imm);
3290 }
3291 
3292 static void qlt_handle_srr_work(struct work_struct *work)
3293 {
3294 	struct qla_tgt *tgt = container_of(work, struct qla_tgt, srr_work);
3295 	struct scsi_qla_host *vha = tgt->vha;
3296 	struct qla_tgt_srr_ctio *sctio;
3297 	unsigned long flags;
3298 
3299 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf029, "Entering SRR work (tgt %p)\n",
3300 	    tgt);
3301 
3302 restart:
3303 	spin_lock_irqsave(&tgt->srr_lock, flags);
3304 	list_for_each_entry(sctio, &tgt->srr_ctio_list, srr_list_entry) {
3305 		struct qla_tgt_srr_imm *imm, *i, *ti;
3306 		struct qla_tgt_cmd *cmd;
3307 		struct se_cmd *se_cmd;
3308 
3309 		imm = NULL;
3310 		list_for_each_entry_safe(i, ti, &tgt->srr_imm_list,
3311 						srr_list_entry) {
3312 			if (i->srr_id == sctio->srr_id) {
3313 				list_del(&i->srr_list_entry);
3314 				if (imm) {
3315 					ql_dbg(ql_dbg_tgt_mgt, vha, 0xf068,
3316 					  "qla_target(%d): There must be "
3317 					  "only one IMM SRR per CTIO SRR "
3318 					  "(IMM SRR %p, id %d, CTIO %p\n",
3319 					  vha->vp_idx, i, i->srr_id, sctio);
3320 					qlt_reject_free_srr_imm(tgt->vha, i, 0);
3321 				} else
3322 					imm = i;
3323 			}
3324 		}
3325 
3326 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02a,
3327 		    "IMM SRR %p, CTIO SRR %p (id %d)\n", imm, sctio,
3328 		    sctio->srr_id);
3329 
3330 		if (imm == NULL) {
3331 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02b,
3332 			    "Not found matching IMM for SRR CTIO (id %d)\n",
3333 			    sctio->srr_id);
3334 			continue;
3335 		} else
3336 			list_del(&sctio->srr_list_entry);
3337 
3338 		spin_unlock_irqrestore(&tgt->srr_lock, flags);
3339 
3340 		cmd = sctio->cmd;
3341 		/*
3342 		 * Reset qla_tgt_cmd SRR values and SGL pointer+count to follow
3343 		 * tcm_qla2xxx_write_pending() and tcm_qla2xxx_queue_data_in()
3344 		 * logic..
3345 		 */
3346 		cmd->offset = 0;
3347 		if (cmd->free_sg) {
3348 			kfree(cmd->sg);
3349 			cmd->sg = NULL;
3350 			cmd->free_sg = 0;
3351 		}
3352 		se_cmd = &cmd->se_cmd;
3353 
3354 		cmd->sg_cnt = se_cmd->t_data_nents;
3355 		cmd->sg = se_cmd->t_data_sg;
3356 
3357 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02c,
3358 		    "SRR cmd %p (se_cmd %p, tag %d, op %x), "
3359 		    "sg_cnt=%d, offset=%d", cmd, &cmd->se_cmd, cmd->tag,
3360 		    se_cmd->t_task_cdb[0], cmd->sg_cnt, cmd->offset);
3361 
3362 		qlt_handle_srr(vha, sctio, imm);
3363 
3364 		kfree(imm);
3365 		kfree(sctio);
3366 		goto restart;
3367 	}
3368 	spin_unlock_irqrestore(&tgt->srr_lock, flags);
3369 }
3370 
3371 /* ha->hardware_lock supposed to be held on entry */
3372 static void qlt_prepare_srr_imm(struct scsi_qla_host *vha,
3373 	struct imm_ntfy_from_isp *iocb)
3374 {
3375 	struct qla_tgt_srr_imm *imm;
3376 	struct qla_hw_data *ha = vha->hw;
3377 	struct qla_tgt *tgt = ha->tgt.qla_tgt;
3378 	struct qla_tgt_srr_ctio *sctio;
3379 
3380 	tgt->imm_srr_id++;
3381 
3382 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02d, "qla_target(%d): SRR received\n",
3383 	    vha->vp_idx);
3384 
3385 	imm = kzalloc(sizeof(*imm), GFP_ATOMIC);
3386 	if (imm != NULL) {
3387 		memcpy(&imm->imm_ntfy, iocb, sizeof(imm->imm_ntfy));
3388 
3389 		/* IRQ is already OFF */
3390 		spin_lock(&tgt->srr_lock);
3391 		imm->srr_id = tgt->imm_srr_id;
3392 		list_add_tail(&imm->srr_list_entry,
3393 		    &tgt->srr_imm_list);
3394 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02e,
3395 		    "IMM NTFY SRR %p added (id %d, ui %x)\n",
3396 		    imm, imm->srr_id, iocb->u.isp24.srr_ui);
3397 		if (tgt->imm_srr_id == tgt->ctio_srr_id) {
3398 			int found = 0;
3399 			list_for_each_entry(sctio, &tgt->srr_ctio_list,
3400 			    srr_list_entry) {
3401 				if (sctio->srr_id == imm->srr_id) {
3402 					found = 1;
3403 					break;
3404 				}
3405 			}
3406 			if (found) {
3407 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02f, "%s",
3408 				    "Scheduling srr work\n");
3409 				schedule_work(&tgt->srr_work);
3410 			} else {
3411 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf030,
3412 				    "qla_target(%d): imm_srr_id "
3413 				    "== ctio_srr_id (%d), but there is no "
3414 				    "corresponding SRR CTIO, deleting IMM "
3415 				    "SRR %p\n", vha->vp_idx, tgt->ctio_srr_id,
3416 				    imm);
3417 				list_del(&imm->srr_list_entry);
3418 
3419 				kfree(imm);
3420 
3421 				spin_unlock(&tgt->srr_lock);
3422 				goto out_reject;
3423 			}
3424 		}
3425 		spin_unlock(&tgt->srr_lock);
3426 	} else {
3427 		struct qla_tgt_srr_ctio *ts;
3428 
3429 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf069,
3430 		    "qla_target(%d): Unable to allocate SRR IMM "
3431 		    "entry, SRR request will be rejected\n", vha->vp_idx);
3432 
3433 		/* IRQ is already OFF */
3434 		spin_lock(&tgt->srr_lock);
3435 		list_for_each_entry_safe(sctio, ts, &tgt->srr_ctio_list,
3436 		    srr_list_entry) {
3437 			if (sctio->srr_id == tgt->imm_srr_id) {
3438 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf031,
3439 				    "CTIO SRR %p deleted (id %d)\n",
3440 				    sctio, sctio->srr_id);
3441 				list_del(&sctio->srr_list_entry);
3442 				qlt_send_term_exchange(vha, sctio->cmd,
3443 				    &sctio->cmd->atio, 1);
3444 				kfree(sctio);
3445 			}
3446 		}
3447 		spin_unlock(&tgt->srr_lock);
3448 		goto out_reject;
3449 	}
3450 
3451 	return;
3452 
3453 out_reject:
3454 	qlt_send_notify_ack(vha, iocb, 0, 0, 0,
3455 	    NOTIFY_ACK_SRR_FLAGS_REJECT,
3456 	    NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3457 	    NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3458 }
3459 
3460 /*
3461  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3462  */
3463 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
3464 	struct imm_ntfy_from_isp *iocb)
3465 {
3466 	struct qla_hw_data *ha = vha->hw;
3467 	uint32_t add_flags = 0;
3468 	int send_notify_ack = 1;
3469 	uint16_t status;
3470 
3471 	status = le16_to_cpu(iocb->u.isp2x.status);
3472 	switch (status) {
3473 	case IMM_NTFY_LIP_RESET:
3474 	{
3475 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
3476 		    "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
3477 		    vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
3478 		    iocb->u.isp24.status_subcode);
3479 
3480 		if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
3481 			send_notify_ack = 0;
3482 		break;
3483 	}
3484 
3485 	case IMM_NTFY_LIP_LINK_REINIT:
3486 	{
3487 		struct qla_tgt *tgt = ha->tgt.qla_tgt;
3488 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
3489 		    "qla_target(%d): LINK REINIT (loop %#x, "
3490 		    "subcode %x)\n", vha->vp_idx,
3491 		    le16_to_cpu(iocb->u.isp24.nport_handle),
3492 		    iocb->u.isp24.status_subcode);
3493 		if (tgt->link_reinit_iocb_pending) {
3494 			qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
3495 			    0, 0, 0, 0, 0, 0);
3496 		}
3497 		memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
3498 		tgt->link_reinit_iocb_pending = 1;
3499 		/*
3500 		 * QLogic requires to wait after LINK REINIT for possible
3501 		 * PDISC or ADISC ELS commands
3502 		 */
3503 		send_notify_ack = 0;
3504 		break;
3505 	}
3506 
3507 	case IMM_NTFY_PORT_LOGOUT:
3508 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
3509 		    "qla_target(%d): Port logout (loop "
3510 		    "%#x, subcode %x)\n", vha->vp_idx,
3511 		    le16_to_cpu(iocb->u.isp24.nport_handle),
3512 		    iocb->u.isp24.status_subcode);
3513 
3514 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
3515 			send_notify_ack = 0;
3516 		/* The sessions will be cleared in the callback, if needed */
3517 		break;
3518 
3519 	case IMM_NTFY_GLBL_TPRLO:
3520 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
3521 		    "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
3522 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
3523 			send_notify_ack = 0;
3524 		/* The sessions will be cleared in the callback, if needed */
3525 		break;
3526 
3527 	case IMM_NTFY_PORT_CONFIG:
3528 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
3529 		    "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
3530 		    status);
3531 		if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
3532 			send_notify_ack = 0;
3533 		/* The sessions will be cleared in the callback, if needed */
3534 		break;
3535 
3536 	case IMM_NTFY_GLBL_LOGO:
3537 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
3538 		    "qla_target(%d): Link failure detected\n",
3539 		    vha->vp_idx);
3540 		/* I_T nexus loss */
3541 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
3542 			send_notify_ack = 0;
3543 		break;
3544 
3545 	case IMM_NTFY_IOCB_OVERFLOW:
3546 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
3547 		    "qla_target(%d): Cannot provide requested "
3548 		    "capability (IOCB overflowed the immediate notify "
3549 		    "resource count)\n", vha->vp_idx);
3550 		break;
3551 
3552 	case IMM_NTFY_ABORT_TASK:
3553 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
3554 		    "qla_target(%d): Abort Task (S %08x I %#x -> "
3555 		    "L %#x)\n", vha->vp_idx,
3556 		    le16_to_cpu(iocb->u.isp2x.seq_id),
3557 		    GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
3558 		    le16_to_cpu(iocb->u.isp2x.lun));
3559 		if (qlt_abort_task(vha, iocb) == 0)
3560 			send_notify_ack = 0;
3561 		break;
3562 
3563 	case IMM_NTFY_RESOURCE:
3564 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
3565 		    "qla_target(%d): Out of resources, host %ld\n",
3566 		    vha->vp_idx, vha->host_no);
3567 		break;
3568 
3569 	case IMM_NTFY_MSG_RX:
3570 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
3571 		    "qla_target(%d): Immediate notify task %x\n",
3572 		    vha->vp_idx, iocb->u.isp2x.task_flags);
3573 		if (qlt_handle_task_mgmt(vha, iocb) == 0)
3574 			send_notify_ack = 0;
3575 		break;
3576 
3577 	case IMM_NTFY_ELS:
3578 		if (qlt_24xx_handle_els(vha, iocb) == 0)
3579 			send_notify_ack = 0;
3580 		break;
3581 
3582 	case IMM_NTFY_SRR:
3583 		qlt_prepare_srr_imm(vha, iocb);
3584 		send_notify_ack = 0;
3585 		break;
3586 
3587 	default:
3588 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
3589 		    "qla_target(%d): Received unknown immediate "
3590 		    "notify status %x\n", vha->vp_idx, status);
3591 		break;
3592 	}
3593 
3594 	if (send_notify_ack)
3595 		qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
3596 }
3597 
3598 /*
3599  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3600  * This function sends busy to ISP 2xxx or 24xx.
3601  */
3602 static void qlt_send_busy(struct scsi_qla_host *vha,
3603 	struct atio_from_isp *atio, uint16_t status)
3604 {
3605 	struct ctio7_to_24xx *ctio24;
3606 	struct qla_hw_data *ha = vha->hw;
3607 	request_t *pkt;
3608 	struct qla_tgt_sess *sess = NULL;
3609 
3610 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
3611 	    atio->u.isp24.fcp_hdr.s_id);
3612 	if (!sess) {
3613 		qlt_send_term_exchange(vha, NULL, atio, 1);
3614 		return;
3615 	}
3616 	/* Sending marker isn't necessary, since we called from ISR */
3617 
3618 	pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3619 	if (!pkt) {
3620 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06e,
3621 		    "qla_target(%d): %s failed: unable to allocate "
3622 		    "request packet", vha->vp_idx, __func__);
3623 		return;
3624 	}
3625 
3626 	pkt->entry_count = 1;
3627 	pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3628 
3629 	ctio24 = (struct ctio7_to_24xx *)pkt;
3630 	ctio24->entry_type = CTIO_TYPE7;
3631 	ctio24->nport_handle = sess->loop_id;
3632 	ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
3633 	ctio24->vp_index = vha->vp_idx;
3634 	ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3635 	ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3636 	ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3637 	ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3638 	ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
3639 	    __constant_cpu_to_le16(
3640 		CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
3641 		CTIO7_FLAGS_DONT_RET_CTIO);
3642 	/*
3643 	 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
3644 	 * if the explicit conformation is used.
3645 	 */
3646 	ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
3647 	ctio24->u.status1.scsi_status = cpu_to_le16(status);
3648 	ctio24->u.status1.residual = get_unaligned((uint32_t *)
3649 	    &atio->u.isp24.fcp_cmnd.add_cdb[
3650 	    atio->u.isp24.fcp_cmnd.add_cdb_len]);
3651 	if (ctio24->u.status1.residual != 0)
3652 		ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3653 
3654 	qla2x00_start_iocbs(vha, vha->req);
3655 }
3656 
3657 /* ha->hardware_lock supposed to be held on entry */
3658 /* called via callback from qla2xxx */
3659 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
3660 	struct atio_from_isp *atio)
3661 {
3662 	struct qla_hw_data *ha = vha->hw;
3663 	struct qla_tgt *tgt = ha->tgt.qla_tgt;
3664 	int rc;
3665 
3666 	if (unlikely(tgt == NULL)) {
3667 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf039,
3668 		    "ATIO pkt, but no tgt (ha %p)", ha);
3669 		return;
3670 	}
3671 	ql_dbg(ql_dbg_tgt, vha, 0xe02c,
3672 	    "qla_target(%d): ATIO pkt %p: type %02x count %02x",
3673 	    vha->vp_idx, atio, atio->u.raw.entry_type,
3674 	    atio->u.raw.entry_count);
3675 	/*
3676 	 * In tgt_stop mode we also should allow all requests to pass.
3677 	 * Otherwise, some commands can stuck.
3678 	 */
3679 
3680 	tgt->irq_cmd_count++;
3681 
3682 	switch (atio->u.raw.entry_type) {
3683 	case ATIO_TYPE7:
3684 		ql_dbg(ql_dbg_tgt, vha, 0xe02d,
3685 		    "ATIO_TYPE7 instance %d, lun %Lx, read/write %d/%d, "
3686 		    "add_cdb_len %d, data_length %04x, s_id %x:%x:%x\n",
3687 		    vha->vp_idx, atio->u.isp24.fcp_cmnd.lun,
3688 		    atio->u.isp24.fcp_cmnd.rddata,
3689 		    atio->u.isp24.fcp_cmnd.wrdata,
3690 		    atio->u.isp24.fcp_cmnd.add_cdb_len,
3691 		    be32_to_cpu(get_unaligned((uint32_t *)
3692 			&atio->u.isp24.fcp_cmnd.add_cdb[
3693 			atio->u.isp24.fcp_cmnd.add_cdb_len])),
3694 		    atio->u.isp24.fcp_hdr.s_id[0],
3695 		    atio->u.isp24.fcp_hdr.s_id[1],
3696 		    atio->u.isp24.fcp_hdr.s_id[2]);
3697 
3698 		if (unlikely(atio->u.isp24.exchange_addr ==
3699 		    ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
3700 			ql_dbg(ql_dbg_tgt, vha, 0xe058,
3701 			    "qla_target(%d): ATIO_TYPE7 "
3702 			    "received with UNKNOWN exchange address, "
3703 			    "sending QUEUE_FULL\n", vha->vp_idx);
3704 			qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
3705 			break;
3706 		}
3707 		if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0))
3708 			rc = qlt_handle_cmd_for_atio(vha, atio);
3709 		else
3710 			rc = qlt_handle_task_mgmt(vha, atio);
3711 		if (unlikely(rc != 0)) {
3712 			if (rc == -ESRCH) {
3713 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
3714 				qlt_send_busy(vha, atio, SAM_STAT_BUSY);
3715 #else
3716 				qlt_send_term_exchange(vha, NULL, atio, 1);
3717 #endif
3718 			} else {
3719 				if (tgt->tgt_stop) {
3720 					ql_dbg(ql_dbg_tgt, vha, 0xe059,
3721 					    "qla_target: Unable to send "
3722 					    "command to target for req, "
3723 					    "ignoring.\n");
3724 				} else {
3725 					ql_dbg(ql_dbg_tgt, vha, 0xe05a,
3726 					    "qla_target(%d): Unable to send "
3727 					    "command to target, sending BUSY "
3728 					    "status.\n", vha->vp_idx);
3729 					qlt_send_busy(vha, atio, SAM_STAT_BUSY);
3730 				}
3731 			}
3732 		}
3733 		break;
3734 
3735 	case IMMED_NOTIFY_TYPE:
3736 	{
3737 		if (unlikely(atio->u.isp2x.entry_status != 0)) {
3738 			ql_dbg(ql_dbg_tgt, vha, 0xe05b,
3739 			    "qla_target(%d): Received ATIO packet %x "
3740 			    "with error status %x\n", vha->vp_idx,
3741 			    atio->u.raw.entry_type,
3742 			    atio->u.isp2x.entry_status);
3743 			break;
3744 		}
3745 		ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
3746 		qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
3747 		break;
3748 	}
3749 
3750 	default:
3751 		ql_dbg(ql_dbg_tgt, vha, 0xe05c,
3752 		    "qla_target(%d): Received unknown ATIO atio "
3753 		    "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
3754 		break;
3755 	}
3756 
3757 	tgt->irq_cmd_count--;
3758 }
3759 
3760 /* ha->hardware_lock supposed to be held on entry */
3761 /* called via callback from qla2xxx */
3762 static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
3763 {
3764 	struct qla_hw_data *ha = vha->hw;
3765 	struct qla_tgt *tgt = ha->tgt.qla_tgt;
3766 
3767 	if (unlikely(tgt == NULL)) {
3768 		ql_dbg(ql_dbg_tgt, vha, 0xe05d,
3769 		    "qla_target(%d): Response pkt %x received, but no "
3770 		    "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
3771 		return;
3772 	}
3773 
3774 	ql_dbg(ql_dbg_tgt, vha, 0xe02f,
3775 	    "qla_target(%d): response pkt %p: T %02x C %02x S %02x "
3776 	    "handle %#x\n", vha->vp_idx, pkt, pkt->entry_type,
3777 	    pkt->entry_count, pkt->entry_status, pkt->handle);
3778 
3779 	/*
3780 	 * In tgt_stop mode we also should allow all requests to pass.
3781 	 * Otherwise, some commands can stuck.
3782 	 */
3783 
3784 	tgt->irq_cmd_count++;
3785 
3786 	switch (pkt->entry_type) {
3787 	case CTIO_TYPE7:
3788 	{
3789 		struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
3790 		ql_dbg(ql_dbg_tgt, vha, 0xe030, "CTIO_TYPE7: instance %d\n",
3791 		    vha->vp_idx);
3792 		qlt_do_ctio_completion(vha, entry->handle,
3793 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3794 		    entry);
3795 		break;
3796 	}
3797 
3798 	case ACCEPT_TGT_IO_TYPE:
3799 	{
3800 		struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
3801 		int rc;
3802 		ql_dbg(ql_dbg_tgt, vha, 0xe031,
3803 		    "ACCEPT_TGT_IO instance %d status %04x "
3804 		    "lun %04x read/write %d data_length %04x "
3805 		    "target_id %02x rx_id %04x\n ", vha->vp_idx,
3806 		    le16_to_cpu(atio->u.isp2x.status),
3807 		    le16_to_cpu(atio->u.isp2x.lun),
3808 		    atio->u.isp2x.execution_codes,
3809 		    le32_to_cpu(atio->u.isp2x.data_length), GET_TARGET_ID(ha,
3810 		    atio), atio->u.isp2x.rx_id);
3811 		if (atio->u.isp2x.status !=
3812 		    __constant_cpu_to_le16(ATIO_CDB_VALID)) {
3813 			ql_dbg(ql_dbg_tgt, vha, 0xe05e,
3814 			    "qla_target(%d): ATIO with error "
3815 			    "status %x received\n", vha->vp_idx,
3816 			    le16_to_cpu(atio->u.isp2x.status));
3817 			break;
3818 		}
3819 		ql_dbg(ql_dbg_tgt, vha, 0xe032,
3820 		    "FCP CDB: 0x%02x, sizeof(cdb): %lu",
3821 		    atio->u.isp2x.cdb[0], (unsigned long
3822 		    int)sizeof(atio->u.isp2x.cdb));
3823 
3824 		rc = qlt_handle_cmd_for_atio(vha, atio);
3825 		if (unlikely(rc != 0)) {
3826 			if (rc == -ESRCH) {
3827 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
3828 				qlt_send_busy(vha, atio, 0);
3829 #else
3830 				qlt_send_term_exchange(vha, NULL, atio, 1);
3831 #endif
3832 			} else {
3833 				if (tgt->tgt_stop) {
3834 					ql_dbg(ql_dbg_tgt, vha, 0xe05f,
3835 					    "qla_target: Unable to send "
3836 					    "command to target, sending TERM "
3837 					    "EXCHANGE for rsp\n");
3838 					qlt_send_term_exchange(vha, NULL,
3839 					    atio, 1);
3840 				} else {
3841 					ql_dbg(ql_dbg_tgt, vha, 0xe060,
3842 					    "qla_target(%d): Unable to send "
3843 					    "command to target, sending BUSY "
3844 					    "status\n", vha->vp_idx);
3845 					qlt_send_busy(vha, atio, 0);
3846 				}
3847 			}
3848 		}
3849 	}
3850 	break;
3851 
3852 	case CONTINUE_TGT_IO_TYPE:
3853 	{
3854 		struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
3855 		ql_dbg(ql_dbg_tgt, vha, 0xe033,
3856 		    "CONTINUE_TGT_IO: instance %d\n", vha->vp_idx);
3857 		qlt_do_ctio_completion(vha, entry->handle,
3858 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3859 		    entry);
3860 		break;
3861 	}
3862 
3863 	case CTIO_A64_TYPE:
3864 	{
3865 		struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
3866 		ql_dbg(ql_dbg_tgt, vha, 0xe034, "CTIO_A64: instance %d\n",
3867 		    vha->vp_idx);
3868 		qlt_do_ctio_completion(vha, entry->handle,
3869 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3870 		    entry);
3871 		break;
3872 	}
3873 
3874 	case IMMED_NOTIFY_TYPE:
3875 		ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
3876 		qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
3877 		break;
3878 
3879 	case NOTIFY_ACK_TYPE:
3880 		if (tgt->notify_ack_expected > 0) {
3881 			struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
3882 			ql_dbg(ql_dbg_tgt, vha, 0xe036,
3883 			    "NOTIFY_ACK seq %08x status %x\n",
3884 			    le16_to_cpu(entry->u.isp2x.seq_id),
3885 			    le16_to_cpu(entry->u.isp2x.status));
3886 			tgt->notify_ack_expected--;
3887 			if (entry->u.isp2x.status !=
3888 			    __constant_cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
3889 				ql_dbg(ql_dbg_tgt, vha, 0xe061,
3890 				    "qla_target(%d): NOTIFY_ACK "
3891 				    "failed %x\n", vha->vp_idx,
3892 				    le16_to_cpu(entry->u.isp2x.status));
3893 			}
3894 		} else {
3895 			ql_dbg(ql_dbg_tgt, vha, 0xe062,
3896 			    "qla_target(%d): Unexpected NOTIFY_ACK received\n",
3897 			    vha->vp_idx);
3898 		}
3899 		break;
3900 
3901 	case ABTS_RECV_24XX:
3902 		ql_dbg(ql_dbg_tgt, vha, 0xe037,
3903 		    "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
3904 		qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
3905 		break;
3906 
3907 	case ABTS_RESP_24XX:
3908 		if (tgt->abts_resp_expected > 0) {
3909 			struct abts_resp_from_24xx_fw *entry =
3910 				(struct abts_resp_from_24xx_fw *)pkt;
3911 			ql_dbg(ql_dbg_tgt, vha, 0xe038,
3912 			    "ABTS_RESP_24XX: compl_status %x\n",
3913 			    entry->compl_status);
3914 			tgt->abts_resp_expected--;
3915 			if (le16_to_cpu(entry->compl_status) !=
3916 			    ABTS_RESP_COMPL_SUCCESS) {
3917 				if ((entry->error_subcode1 == 0x1E) &&
3918 				    (entry->error_subcode2 == 0)) {
3919 					/*
3920 					 * We've got a race here: aborted
3921 					 * exchange not terminated, i.e.
3922 					 * response for the aborted command was
3923 					 * sent between the abort request was
3924 					 * received and processed.
3925 					 * Unfortunately, the firmware has a
3926 					 * silly requirement that all aborted
3927 					 * exchanges must be explicitely
3928 					 * terminated, otherwise it refuses to
3929 					 * send responses for the abort
3930 					 * requests. So, we have to
3931 					 * (re)terminate the exchange and retry
3932 					 * the abort response.
3933 					 */
3934 					qlt_24xx_retry_term_exchange(vha,
3935 					    entry);
3936 				} else
3937 					ql_dbg(ql_dbg_tgt, vha, 0xe063,
3938 					    "qla_target(%d): ABTS_RESP_24XX "
3939 					    "failed %x (subcode %x:%x)",
3940 					    vha->vp_idx, entry->compl_status,
3941 					    entry->error_subcode1,
3942 					    entry->error_subcode2);
3943 			}
3944 		} else {
3945 			ql_dbg(ql_dbg_tgt, vha, 0xe064,
3946 			    "qla_target(%d): Unexpected ABTS_RESP_24XX "
3947 			    "received\n", vha->vp_idx);
3948 		}
3949 		break;
3950 
3951 	default:
3952 		ql_dbg(ql_dbg_tgt, vha, 0xe065,
3953 		    "qla_target(%d): Received unknown response pkt "
3954 		    "type %x\n", vha->vp_idx, pkt->entry_type);
3955 		break;
3956 	}
3957 
3958 	tgt->irq_cmd_count--;
3959 }
3960 
3961 /*
3962  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3963  */
3964 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
3965 	uint16_t *mailbox)
3966 {
3967 	struct qla_hw_data *ha = vha->hw;
3968 	struct qla_tgt *tgt = ha->tgt.qla_tgt;
3969 	int login_code;
3970 
3971 	ql_dbg(ql_dbg_tgt, vha, 0xe039,
3972 	    "scsi(%ld): ha state %d init_done %d oper_mode %d topo %d\n",
3973 	    vha->host_no, atomic_read(&vha->loop_state), vha->flags.init_done,
3974 	    ha->operating_mode, ha->current_topology);
3975 
3976 	if (!ha->tgt.tgt_ops)
3977 		return;
3978 
3979 	if (unlikely(tgt == NULL)) {
3980 		ql_dbg(ql_dbg_tgt, vha, 0xe03a,
3981 		    "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
3982 		return;
3983 	}
3984 
3985 	if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
3986 	    IS_QLA2100(ha))
3987 		return;
3988 	/*
3989 	 * In tgt_stop mode we also should allow all requests to pass.
3990 	 * Otherwise, some commands can stuck.
3991 	 */
3992 
3993 	tgt->irq_cmd_count++;
3994 
3995 	switch (code) {
3996 	case MBA_RESET:			/* Reset */
3997 	case MBA_SYSTEM_ERR:		/* System Error */
3998 	case MBA_REQ_TRANSFER_ERR:	/* Request Transfer Error */
3999 	case MBA_RSP_TRANSFER_ERR:	/* Response Transfer Error */
4000 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
4001 		    "qla_target(%d): System error async event %#x "
4002 		    "occurred", vha->vp_idx, code);
4003 		break;
4004 	case MBA_WAKEUP_THRES:		/* Request Queue Wake-up. */
4005 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4006 		break;
4007 
4008 	case MBA_LOOP_UP:
4009 	{
4010 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
4011 		    "qla_target(%d): Async LOOP_UP occurred "
4012 		    "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
4013 		    le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
4014 		    le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
4015 		if (tgt->link_reinit_iocb_pending) {
4016 			qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
4017 			    0, 0, 0, 0, 0, 0);
4018 			tgt->link_reinit_iocb_pending = 0;
4019 		}
4020 		break;
4021 	}
4022 
4023 	case MBA_LIP_OCCURRED:
4024 	case MBA_LOOP_DOWN:
4025 	case MBA_LIP_RESET:
4026 	case MBA_RSCN_UPDATE:
4027 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
4028 		    "qla_target(%d): Async event %#x occurred "
4029 		    "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
4030 		    le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
4031 		    le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
4032 		break;
4033 
4034 	case MBA_PORT_UPDATE:
4035 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
4036 		    "qla_target(%d): Port update async event %#x "
4037 		    "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
4038 		    "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
4039 		    le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
4040 		    le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
4041 
4042 		login_code = le16_to_cpu(mailbox[2]);
4043 		if (login_code == 0x4)
4044 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
4045 			    "Async MB 2: Got PLOGI Complete\n");
4046 		else if (login_code == 0x7)
4047 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
4048 			    "Async MB 2: Port Logged Out\n");
4049 		break;
4050 
4051 	default:
4052 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf040,
4053 		    "qla_target(%d): Async event %#x occurred: "
4054 		    "ignore (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
4055 		    code, le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
4056 		    le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
4057 		break;
4058 	}
4059 
4060 	tgt->irq_cmd_count--;
4061 }
4062 
4063 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
4064 	uint16_t loop_id)
4065 {
4066 	fc_port_t *fcport;
4067 	int rc;
4068 
4069 	fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
4070 	if (!fcport) {
4071 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
4072 		    "qla_target(%d): Allocation of tmp FC port failed",
4073 		    vha->vp_idx);
4074 		return NULL;
4075 	}
4076 
4077 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf041, "loop_id %d", loop_id);
4078 
4079 	fcport->loop_id = loop_id;
4080 
4081 	rc = qla2x00_get_port_database(vha, fcport, 0);
4082 	if (rc != QLA_SUCCESS) {
4083 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
4084 		    "qla_target(%d): Failed to retrieve fcport "
4085 		    "information -- get_port_database() returned %x "
4086 		    "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
4087 		kfree(fcport);
4088 		return NULL;
4089 	}
4090 
4091 	return fcport;
4092 }
4093 
4094 /* Must be called under tgt_mutex */
4095 static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *vha,
4096 	uint8_t *s_id)
4097 {
4098 	struct qla_hw_data *ha = vha->hw;
4099 	struct qla_tgt_sess *sess = NULL;
4100 	fc_port_t *fcport = NULL;
4101 	int rc, global_resets;
4102 	uint16_t loop_id = 0;
4103 
4104 retry:
4105 	global_resets = atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count);
4106 
4107 	rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
4108 	if (rc != 0) {
4109 		if ((s_id[0] == 0xFF) &&
4110 		    (s_id[1] == 0xFC)) {
4111 			/*
4112 			 * This is Domain Controller, so it should be
4113 			 * OK to drop SCSI commands from it.
4114 			 */
4115 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
4116 			    "Unable to find initiator with S_ID %x:%x:%x",
4117 			    s_id[0], s_id[1], s_id[2]);
4118 		} else
4119 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf071,
4120 			    "qla_target(%d): Unable to find "
4121 			    "initiator with S_ID %x:%x:%x",
4122 			    vha->vp_idx, s_id[0], s_id[1],
4123 			    s_id[2]);
4124 		return NULL;
4125 	}
4126 
4127 	fcport = qlt_get_port_database(vha, loop_id);
4128 	if (!fcport)
4129 		return NULL;
4130 
4131 	if (global_resets !=
4132 	    atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count)) {
4133 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
4134 		    "qla_target(%d): global reset during session discovery "
4135 		    "(counter was %d, new %d), retrying", vha->vp_idx,
4136 		    global_resets,
4137 		    atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count));
4138 		goto retry;
4139 	}
4140 
4141 	sess = qlt_create_sess(vha, fcport, true);
4142 
4143 	kfree(fcport);
4144 	return sess;
4145 }
4146 
4147 static void qlt_abort_work(struct qla_tgt *tgt,
4148 	struct qla_tgt_sess_work_param *prm)
4149 {
4150 	struct scsi_qla_host *vha = tgt->vha;
4151 	struct qla_hw_data *ha = vha->hw;
4152 	struct qla_tgt_sess *sess = NULL;
4153 	unsigned long flags;
4154 	uint32_t be_s_id;
4155 	uint8_t s_id[3];
4156 	int rc;
4157 
4158 	spin_lock_irqsave(&ha->hardware_lock, flags);
4159 
4160 	if (tgt->tgt_stop)
4161 		goto out_term;
4162 
4163 	s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
4164 	s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
4165 	s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
4166 
4167 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4168 	    (unsigned char *)&be_s_id);
4169 	if (!sess) {
4170 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
4171 
4172 		mutex_lock(&ha->tgt.tgt_mutex);
4173 		sess = qlt_make_local_sess(vha, s_id);
4174 		/* sess has got an extra creation ref */
4175 		mutex_unlock(&ha->tgt.tgt_mutex);
4176 
4177 		spin_lock_irqsave(&ha->hardware_lock, flags);
4178 		if (!sess)
4179 			goto out_term;
4180 	} else {
4181 		kref_get(&sess->se_sess->sess_kref);
4182 	}
4183 
4184 	if (tgt->tgt_stop)
4185 		goto out_term;
4186 
4187 	rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
4188 	if (rc != 0)
4189 		goto out_term;
4190 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4191 
4192 	ha->tgt.tgt_ops->put_sess(sess);
4193 	return;
4194 
4195 out_term:
4196 	qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
4197 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4198 	if (sess)
4199 		ha->tgt.tgt_ops->put_sess(sess);
4200 }
4201 
4202 static void qlt_tmr_work(struct qla_tgt *tgt,
4203 	struct qla_tgt_sess_work_param *prm)
4204 {
4205 	struct atio_from_isp *a = &prm->tm_iocb2;
4206 	struct scsi_qla_host *vha = tgt->vha;
4207 	struct qla_hw_data *ha = vha->hw;
4208 	struct qla_tgt_sess *sess = NULL;
4209 	unsigned long flags;
4210 	uint8_t *s_id = NULL; /* to hide compiler warnings */
4211 	int rc;
4212 	uint32_t lun, unpacked_lun;
4213 	int lun_size, fn;
4214 	void *iocb;
4215 
4216 	spin_lock_irqsave(&ha->hardware_lock, flags);
4217 
4218 	if (tgt->tgt_stop)
4219 		goto out_term;
4220 
4221 	s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
4222 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
4223 	if (!sess) {
4224 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
4225 
4226 		mutex_lock(&ha->tgt.tgt_mutex);
4227 		sess = qlt_make_local_sess(vha, s_id);
4228 		/* sess has got an extra creation ref */
4229 		mutex_unlock(&ha->tgt.tgt_mutex);
4230 
4231 		spin_lock_irqsave(&ha->hardware_lock, flags);
4232 		if (!sess)
4233 			goto out_term;
4234 	} else {
4235 		kref_get(&sess->se_sess->sess_kref);
4236 	}
4237 
4238 	iocb = a;
4239 	lun = a->u.isp24.fcp_cmnd.lun;
4240 	lun_size = sizeof(lun);
4241 	fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4242 	unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
4243 
4244 	rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4245 	if (rc != 0)
4246 		goto out_term;
4247 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4248 
4249 	ha->tgt.tgt_ops->put_sess(sess);
4250 	return;
4251 
4252 out_term:
4253 	qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1);
4254 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4255 	if (sess)
4256 		ha->tgt.tgt_ops->put_sess(sess);
4257 }
4258 
4259 static void qlt_sess_work_fn(struct work_struct *work)
4260 {
4261 	struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
4262 	struct scsi_qla_host *vha = tgt->vha;
4263 	unsigned long flags;
4264 
4265 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
4266 
4267 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
4268 	while (!list_empty(&tgt->sess_works_list)) {
4269 		struct qla_tgt_sess_work_param *prm = list_entry(
4270 		    tgt->sess_works_list.next, typeof(*prm),
4271 		    sess_works_list_entry);
4272 
4273 		/*
4274 		 * This work can be scheduled on several CPUs at time, so we
4275 		 * must delete the entry to eliminate double processing
4276 		 */
4277 		list_del(&prm->sess_works_list_entry);
4278 
4279 		spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
4280 
4281 		switch (prm->type) {
4282 		case QLA_TGT_SESS_WORK_ABORT:
4283 			qlt_abort_work(tgt, prm);
4284 			break;
4285 		case QLA_TGT_SESS_WORK_TM:
4286 			qlt_tmr_work(tgt, prm);
4287 			break;
4288 		default:
4289 			BUG_ON(1);
4290 			break;
4291 		}
4292 
4293 		spin_lock_irqsave(&tgt->sess_work_lock, flags);
4294 
4295 		kfree(prm);
4296 	}
4297 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
4298 }
4299 
4300 /* Must be called under tgt_host_action_mutex */
4301 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
4302 {
4303 	struct qla_tgt *tgt;
4304 
4305 	if (!QLA_TGT_MODE_ENABLED())
4306 		return 0;
4307 
4308 	ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
4309 	    "Registering target for host %ld(%p)", base_vha->host_no, ha);
4310 
4311 	BUG_ON((ha->tgt.qla_tgt != NULL) || (ha->tgt.tgt_ops != NULL));
4312 
4313 	tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
4314 	if (!tgt) {
4315 		ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
4316 		    "Unable to allocate struct qla_tgt\n");
4317 		return -ENOMEM;
4318 	}
4319 
4320 	if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
4321 		base_vha->host->hostt->supported_mode |= MODE_TARGET;
4322 
4323 	tgt->ha = ha;
4324 	tgt->vha = base_vha;
4325 	init_waitqueue_head(&tgt->waitQ);
4326 	INIT_LIST_HEAD(&tgt->sess_list);
4327 	INIT_LIST_HEAD(&tgt->del_sess_list);
4328 	INIT_DELAYED_WORK(&tgt->sess_del_work,
4329 		(void (*)(struct work_struct *))qlt_del_sess_work_fn);
4330 	spin_lock_init(&tgt->sess_work_lock);
4331 	INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
4332 	INIT_LIST_HEAD(&tgt->sess_works_list);
4333 	spin_lock_init(&tgt->srr_lock);
4334 	INIT_LIST_HEAD(&tgt->srr_ctio_list);
4335 	INIT_LIST_HEAD(&tgt->srr_imm_list);
4336 	INIT_WORK(&tgt->srr_work, qlt_handle_srr_work);
4337 	atomic_set(&tgt->tgt_global_resets_count, 0);
4338 
4339 	ha->tgt.qla_tgt = tgt;
4340 
4341 	ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
4342 		"qla_target(%d): using 64 Bit PCI addressing",
4343 		base_vha->vp_idx);
4344 	tgt->tgt_enable_64bit_addr = 1;
4345 	/* 3 is reserved */
4346 	tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
4347 	tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
4348 	tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
4349 
4350 	mutex_lock(&qla_tgt_mutex);
4351 	list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
4352 	mutex_unlock(&qla_tgt_mutex);
4353 
4354 	return 0;
4355 }
4356 
4357 /* Must be called under tgt_host_action_mutex */
4358 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
4359 {
4360 	if (!ha->tgt.qla_tgt)
4361 		return 0;
4362 
4363 	mutex_lock(&qla_tgt_mutex);
4364 	list_del(&ha->tgt.qla_tgt->tgt_list_entry);
4365 	mutex_unlock(&qla_tgt_mutex);
4366 
4367 	ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
4368 	    vha->host_no, ha);
4369 	qlt_release(ha->tgt.qla_tgt);
4370 
4371 	return 0;
4372 }
4373 
4374 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
4375 	unsigned char *b)
4376 {
4377 	int i;
4378 
4379 	pr_debug("qla2xxx HW vha->node_name: ");
4380 	for (i = 0; i < WWN_SIZE; i++)
4381 		pr_debug("%02x ", vha->node_name[i]);
4382 	pr_debug("\n");
4383 	pr_debug("qla2xxx HW vha->port_name: ");
4384 	for (i = 0; i < WWN_SIZE; i++)
4385 		pr_debug("%02x ", vha->port_name[i]);
4386 	pr_debug("\n");
4387 
4388 	pr_debug("qla2xxx passed configfs WWPN: ");
4389 	put_unaligned_be64(wwpn, b);
4390 	for (i = 0; i < WWN_SIZE; i++)
4391 		pr_debug("%02x ", b[i]);
4392 	pr_debug("\n");
4393 }
4394 
4395 /**
4396  * qla_tgt_lport_register - register lport with external module
4397  *
4398  * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
4399  * @wwpn: Passwd FC target WWPN
4400  * @callback:  lport initialization callback for tcm_qla2xxx code
4401  * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
4402  */
4403 int qlt_lport_register(struct qla_tgt_func_tmpl *qla_tgt_ops, u64 wwpn,
4404 	int (*callback)(struct scsi_qla_host *), void *target_lport_ptr)
4405 {
4406 	struct qla_tgt *tgt;
4407 	struct scsi_qla_host *vha;
4408 	struct qla_hw_data *ha;
4409 	struct Scsi_Host *host;
4410 	unsigned long flags;
4411 	int rc;
4412 	u8 b[WWN_SIZE];
4413 
4414 	mutex_lock(&qla_tgt_mutex);
4415 	list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
4416 		vha = tgt->vha;
4417 		ha = vha->hw;
4418 
4419 		host = vha->host;
4420 		if (!host)
4421 			continue;
4422 
4423 		if (ha->tgt.tgt_ops != NULL)
4424 			continue;
4425 
4426 		if (!(host->hostt->supported_mode & MODE_TARGET))
4427 			continue;
4428 
4429 		spin_lock_irqsave(&ha->hardware_lock, flags);
4430 		if (host->active_mode & MODE_TARGET) {
4431 			pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
4432 			    host->host_no);
4433 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
4434 			continue;
4435 		}
4436 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
4437 
4438 		if (!scsi_host_get(host)) {
4439 			ql_dbg(ql_dbg_tgt, vha, 0xe068,
4440 			    "Unable to scsi_host_get() for"
4441 			    " qla2xxx scsi_host\n");
4442 			continue;
4443 		}
4444 		qlt_lport_dump(vha, wwpn, b);
4445 
4446 		if (memcmp(vha->port_name, b, WWN_SIZE)) {
4447 			scsi_host_put(host);
4448 			continue;
4449 		}
4450 		/*
4451 		 * Setup passed parameters ahead of invoking callback
4452 		 */
4453 		ha->tgt.tgt_ops = qla_tgt_ops;
4454 		ha->tgt.target_lport_ptr = target_lport_ptr;
4455 		rc = (*callback)(vha);
4456 		if (rc != 0) {
4457 			ha->tgt.tgt_ops = NULL;
4458 			ha->tgt.target_lport_ptr = NULL;
4459 		}
4460 		mutex_unlock(&qla_tgt_mutex);
4461 		return rc;
4462 	}
4463 	mutex_unlock(&qla_tgt_mutex);
4464 
4465 	return -ENODEV;
4466 }
4467 EXPORT_SYMBOL(qlt_lport_register);
4468 
4469 /**
4470  * qla_tgt_lport_deregister - Degister lport
4471  *
4472  * @vha:  Registered scsi_qla_host pointer
4473  */
4474 void qlt_lport_deregister(struct scsi_qla_host *vha)
4475 {
4476 	struct qla_hw_data *ha = vha->hw;
4477 	struct Scsi_Host *sh = vha->host;
4478 	/*
4479 	 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
4480 	 */
4481 	ha->tgt.target_lport_ptr = NULL;
4482 	ha->tgt.tgt_ops = NULL;
4483 	/*
4484 	 * Release the Scsi_Host reference for the underlying qla2xxx host
4485 	 */
4486 	scsi_host_put(sh);
4487 }
4488 EXPORT_SYMBOL(qlt_lport_deregister);
4489 
4490 /* Must be called under HW lock */
4491 void qlt_set_mode(struct scsi_qla_host *vha)
4492 {
4493 	struct qla_hw_data *ha = vha->hw;
4494 
4495 	switch (ql2x_ini_mode) {
4496 	case QLA2XXX_INI_MODE_DISABLED:
4497 	case QLA2XXX_INI_MODE_EXCLUSIVE:
4498 		vha->host->active_mode = MODE_TARGET;
4499 		break;
4500 	case QLA2XXX_INI_MODE_ENABLED:
4501 		vha->host->active_mode |= MODE_TARGET;
4502 		break;
4503 	default:
4504 		break;
4505 	}
4506 
4507 	if (ha->tgt.ini_mode_force_reverse)
4508 		qla_reverse_ini_mode(vha);
4509 }
4510 
4511 /* Must be called under HW lock */
4512 void qlt_clear_mode(struct scsi_qla_host *vha)
4513 {
4514 	struct qla_hw_data *ha = vha->hw;
4515 
4516 	switch (ql2x_ini_mode) {
4517 	case QLA2XXX_INI_MODE_DISABLED:
4518 		vha->host->active_mode = MODE_UNKNOWN;
4519 		break;
4520 	case QLA2XXX_INI_MODE_EXCLUSIVE:
4521 		vha->host->active_mode = MODE_INITIATOR;
4522 		break;
4523 	case QLA2XXX_INI_MODE_ENABLED:
4524 		vha->host->active_mode &= ~MODE_TARGET;
4525 		break;
4526 	default:
4527 		break;
4528 	}
4529 
4530 	if (ha->tgt.ini_mode_force_reverse)
4531 		qla_reverse_ini_mode(vha);
4532 }
4533 
4534 /*
4535  * qla_tgt_enable_vha - NO LOCK HELD
4536  *
4537  * host_reset, bring up w/ Target Mode Enabled
4538  */
4539 void
4540 qlt_enable_vha(struct scsi_qla_host *vha)
4541 {
4542 	struct qla_hw_data *ha = vha->hw;
4543 	struct qla_tgt *tgt = ha->tgt.qla_tgt;
4544 	unsigned long flags;
4545 
4546 	if (!tgt) {
4547 		ql_dbg(ql_dbg_tgt, vha, 0xe069,
4548 		    "Unable to locate qla_tgt pointer from"
4549 		    " struct qla_hw_data\n");
4550 		dump_stack();
4551 		return;
4552 	}
4553 
4554 	spin_lock_irqsave(&ha->hardware_lock, flags);
4555 	tgt->tgt_stopped = 0;
4556 	qlt_set_mode(vha);
4557 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4558 
4559 	set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4560 	qla2xxx_wake_dpc(vha);
4561 	qla2x00_wait_for_hba_online(vha);
4562 }
4563 EXPORT_SYMBOL(qlt_enable_vha);
4564 
4565 /*
4566  * qla_tgt_disable_vha - NO LOCK HELD
4567  *
4568  * Disable Target Mode and reset the adapter
4569  */
4570 void
4571 qlt_disable_vha(struct scsi_qla_host *vha)
4572 {
4573 	struct qla_hw_data *ha = vha->hw;
4574 	struct qla_tgt *tgt = ha->tgt.qla_tgt;
4575 	unsigned long flags;
4576 
4577 	if (!tgt) {
4578 		ql_dbg(ql_dbg_tgt, vha, 0xe06a,
4579 		    "Unable to locate qla_tgt pointer from"
4580 		    " struct qla_hw_data\n");
4581 		dump_stack();
4582 		return;
4583 	}
4584 
4585 	spin_lock_irqsave(&ha->hardware_lock, flags);
4586 	qlt_clear_mode(vha);
4587 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4588 
4589 	set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4590 	qla2xxx_wake_dpc(vha);
4591 	qla2x00_wait_for_hba_online(vha);
4592 }
4593 
4594 /*
4595  * Called from qla_init.c:qla24xx_vport_create() contex to setup
4596  * the target mode specific struct scsi_qla_host and struct qla_hw_data
4597  * members.
4598  */
4599 void
4600 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
4601 {
4602 	if (!qla_tgt_mode_enabled(vha))
4603 		return;
4604 
4605 	mutex_init(&ha->tgt.tgt_mutex);
4606 	mutex_init(&ha->tgt.tgt_host_action_mutex);
4607 
4608 	qlt_clear_mode(vha);
4609 
4610 	/*
4611 	 * NOTE: Currently the value is kept the same for <24xx and
4612 	 * >=24xx ISPs. If it is necessary to change it,
4613 	 * the check should be added for specific ISPs,
4614 	 * assigning the value appropriately.
4615 	 */
4616 	ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
4617 }
4618 
4619 void
4620 qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
4621 {
4622 	/*
4623 	 * FC-4 Feature bit 0 indicates target functionality to the name server.
4624 	 */
4625 	if (qla_tgt_mode_enabled(vha)) {
4626 		if (qla_ini_mode_enabled(vha))
4627 			ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
4628 		else
4629 			ct_req->req.rff_id.fc4_feature = BIT_0;
4630 	} else if (qla_ini_mode_enabled(vha)) {
4631 		ct_req->req.rff_id.fc4_feature = BIT_1;
4632 	}
4633 }
4634 
4635 /*
4636  * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
4637  * @ha: HA context
4638  *
4639  * Beginning of ATIO ring has initialization control block already built
4640  * by nvram config routine.
4641  *
4642  * Returns 0 on success.
4643  */
4644 void
4645 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
4646 {
4647 	struct qla_hw_data *ha = vha->hw;
4648 	uint16_t cnt;
4649 	struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
4650 
4651 	if (!qla_tgt_mode_enabled(vha))
4652 		return;
4653 
4654 	for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
4655 		pkt->u.raw.signature = ATIO_PROCESSED;
4656 		pkt++;
4657 	}
4658 
4659 }
4660 
4661 /*
4662  * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
4663  * @ha: SCSI driver HA context
4664  */
4665 void
4666 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha)
4667 {
4668 	struct qla_hw_data *ha = vha->hw;
4669 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
4670 	struct atio_from_isp *pkt;
4671 	int cnt, i;
4672 
4673 	if (!vha->flags.online)
4674 		return;
4675 
4676 	while (ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) {
4677 		pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
4678 		cnt = pkt->u.raw.entry_count;
4679 
4680 		qlt_24xx_atio_pkt_all_vps(vha, (struct atio_from_isp *)pkt);
4681 
4682 		for (i = 0; i < cnt; i++) {
4683 			ha->tgt.atio_ring_index++;
4684 			if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
4685 				ha->tgt.atio_ring_index = 0;
4686 				ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
4687 			} else
4688 				ha->tgt.atio_ring_ptr++;
4689 
4690 			pkt->u.raw.signature = ATIO_PROCESSED;
4691 			pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
4692 		}
4693 		wmb();
4694 	}
4695 
4696 	/* Adjust ring index */
4697 	WRT_REG_DWORD(&reg->atio_q_out, ha->tgt.atio_ring_index);
4698 }
4699 
4700 void
4701 qlt_24xx_config_rings(struct scsi_qla_host *vha, device_reg_t __iomem *reg)
4702 {
4703 	struct qla_hw_data *ha = vha->hw;
4704 
4705 /* FIXME: atio_q in/out for ha->mqenable=1..? */
4706 	if (ha->mqenable) {
4707 #if 0
4708 		WRT_REG_DWORD(&reg->isp25mq.atio_q_in, 0);
4709 		WRT_REG_DWORD(&reg->isp25mq.atio_q_out, 0);
4710 		RD_REG_DWORD(&reg->isp25mq.atio_q_out);
4711 #endif
4712 	} else {
4713 		/* Setup APTIO registers for target mode */
4714 		WRT_REG_DWORD(&reg->isp24.atio_q_in, 0);
4715 		WRT_REG_DWORD(&reg->isp24.atio_q_out, 0);
4716 		RD_REG_DWORD(&reg->isp24.atio_q_out);
4717 	}
4718 }
4719 
4720 void
4721 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
4722 {
4723 	struct qla_hw_data *ha = vha->hw;
4724 
4725 	if (qla_tgt_mode_enabled(vha)) {
4726 		if (!ha->tgt.saved_set) {
4727 			/* We save only once */
4728 			ha->tgt.saved_exchange_count = nv->exchange_count;
4729 			ha->tgt.saved_firmware_options_1 =
4730 			    nv->firmware_options_1;
4731 			ha->tgt.saved_firmware_options_2 =
4732 			    nv->firmware_options_2;
4733 			ha->tgt.saved_firmware_options_3 =
4734 			    nv->firmware_options_3;
4735 			ha->tgt.saved_set = 1;
4736 		}
4737 
4738 		nv->exchange_count = __constant_cpu_to_le16(0xFFFF);
4739 
4740 		/* Enable target mode */
4741 		nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_4);
4742 
4743 		/* Disable ini mode, if requested */
4744 		if (!qla_ini_mode_enabled(vha))
4745 			nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_5);
4746 
4747 		/* Disable Full Login after LIP */
4748 		nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_13);
4749 		/* Enable initial LIP */
4750 		nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_9);
4751 		/* Enable FC tapes support */
4752 		nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
4753 		/* Disable Full Login after LIP */
4754 		nv->host_p &= __constant_cpu_to_le32(~BIT_10);
4755 		/* Enable target PRLI control */
4756 		nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_14);
4757 	} else {
4758 		if (ha->tgt.saved_set) {
4759 			nv->exchange_count = ha->tgt.saved_exchange_count;
4760 			nv->firmware_options_1 =
4761 			    ha->tgt.saved_firmware_options_1;
4762 			nv->firmware_options_2 =
4763 			    ha->tgt.saved_firmware_options_2;
4764 			nv->firmware_options_3 =
4765 			    ha->tgt.saved_firmware_options_3;
4766 		}
4767 		return;
4768 	}
4769 
4770 	/* out-of-order frames reassembly */
4771 	nv->firmware_options_3 |= BIT_6|BIT_9;
4772 
4773 	if (ha->tgt.enable_class_2) {
4774 		if (vha->flags.init_done)
4775 			fc_host_supported_classes(vha->host) =
4776 				FC_COS_CLASS2 | FC_COS_CLASS3;
4777 
4778 		nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_8);
4779 	} else {
4780 		if (vha->flags.init_done)
4781 			fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
4782 
4783 		nv->firmware_options_2 &= ~__constant_cpu_to_le32(BIT_8);
4784 	}
4785 }
4786 
4787 void
4788 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
4789 	struct init_cb_24xx *icb)
4790 {
4791 	struct qla_hw_data *ha = vha->hw;
4792 
4793 	if (ha->tgt.node_name_set) {
4794 		memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
4795 		icb->firmware_options_1 |= __constant_cpu_to_le32(BIT_14);
4796 	}
4797 }
4798 
4799 int
4800 qlt_24xx_process_response_error(struct scsi_qla_host *vha,
4801 	struct sts_entry_24xx *pkt)
4802 {
4803 	switch (pkt->entry_type) {
4804 	case ABTS_RECV_24XX:
4805 	case ABTS_RESP_24XX:
4806 	case CTIO_TYPE7:
4807 	case NOTIFY_ACK_TYPE:
4808 		return 1;
4809 	default:
4810 		return 0;
4811 	}
4812 }
4813 
4814 void
4815 qlt_modify_vp_config(struct scsi_qla_host *vha,
4816 	struct vp_config_entry_24xx *vpmod)
4817 {
4818 	if (qla_tgt_mode_enabled(vha))
4819 		vpmod->options_idx1 &= ~BIT_5;
4820 	/* Disable ini mode, if requested */
4821 	if (!qla_ini_mode_enabled(vha))
4822 		vpmod->options_idx1 &= ~BIT_4;
4823 }
4824 
4825 void
4826 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
4827 {
4828 	if (!QLA_TGT_MODE_ENABLED())
4829 		return;
4830 
4831 	mutex_init(&ha->tgt.tgt_mutex);
4832 	mutex_init(&ha->tgt.tgt_host_action_mutex);
4833 	qlt_clear_mode(base_vha);
4834 }
4835 
4836 int
4837 qlt_mem_alloc(struct qla_hw_data *ha)
4838 {
4839 	if (!QLA_TGT_MODE_ENABLED())
4840 		return 0;
4841 
4842 	ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
4843 	    MAX_MULTI_ID_FABRIC, GFP_KERNEL);
4844 	if (!ha->tgt.tgt_vp_map)
4845 		return -ENOMEM;
4846 
4847 	ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
4848 	    (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
4849 	    &ha->tgt.atio_dma, GFP_KERNEL);
4850 	if (!ha->tgt.atio_ring) {
4851 		kfree(ha->tgt.tgt_vp_map);
4852 		return -ENOMEM;
4853 	}
4854 	return 0;
4855 }
4856 
4857 void
4858 qlt_mem_free(struct qla_hw_data *ha)
4859 {
4860 	if (!QLA_TGT_MODE_ENABLED())
4861 		return;
4862 
4863 	if (ha->tgt.atio_ring) {
4864 		dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
4865 		    sizeof(struct atio_from_isp), ha->tgt.atio_ring,
4866 		    ha->tgt.atio_dma);
4867 	}
4868 	kfree(ha->tgt.tgt_vp_map);
4869 }
4870 
4871 /* vport_slock to be held by the caller */
4872 void
4873 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
4874 {
4875 	if (!QLA_TGT_MODE_ENABLED())
4876 		return;
4877 
4878 	switch (cmd) {
4879 	case SET_VP_IDX:
4880 		vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
4881 		break;
4882 	case SET_AL_PA:
4883 		vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = vha->vp_idx;
4884 		break;
4885 	case RESET_VP_IDX:
4886 		vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
4887 		break;
4888 	case RESET_AL_PA:
4889 		vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = 0;
4890 		break;
4891 	}
4892 }
4893 
4894 static int __init qlt_parse_ini_mode(void)
4895 {
4896 	if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
4897 		ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
4898 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
4899 		ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
4900 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
4901 		ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
4902 	else
4903 		return false;
4904 
4905 	return true;
4906 }
4907 
4908 int __init qlt_init(void)
4909 {
4910 	int ret;
4911 
4912 	if (!qlt_parse_ini_mode()) {
4913 		ql_log(ql_log_fatal, NULL, 0xe06b,
4914 		    "qlt_parse_ini_mode() failed\n");
4915 		return -EINVAL;
4916 	}
4917 
4918 	if (!QLA_TGT_MODE_ENABLED())
4919 		return 0;
4920 
4921 	qla_tgt_cmd_cachep = kmem_cache_create("qla_tgt_cmd_cachep",
4922 	    sizeof(struct qla_tgt_cmd), __alignof__(struct qla_tgt_cmd), 0,
4923 	    NULL);
4924 	if (!qla_tgt_cmd_cachep) {
4925 		ql_log(ql_log_fatal, NULL, 0xe06c,
4926 		    "kmem_cache_create for qla_tgt_cmd_cachep failed\n");
4927 		return -ENOMEM;
4928 	}
4929 
4930 	qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
4931 	    sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
4932 	    qla_tgt_mgmt_cmd), 0, NULL);
4933 	if (!qla_tgt_mgmt_cmd_cachep) {
4934 		ql_log(ql_log_fatal, NULL, 0xe06d,
4935 		    "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
4936 		ret = -ENOMEM;
4937 		goto out;
4938 	}
4939 
4940 	qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
4941 	    mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
4942 	if (!qla_tgt_mgmt_cmd_mempool) {
4943 		ql_log(ql_log_fatal, NULL, 0xe06e,
4944 		    "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
4945 		ret = -ENOMEM;
4946 		goto out_mgmt_cmd_cachep;
4947 	}
4948 
4949 	qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
4950 	if (!qla_tgt_wq) {
4951 		ql_log(ql_log_fatal, NULL, 0xe06f,
4952 		    "alloc_workqueue for qla_tgt_wq failed\n");
4953 		ret = -ENOMEM;
4954 		goto out_cmd_mempool;
4955 	}
4956 	/*
4957 	 * Return 1 to signal that initiator-mode is being disabled
4958 	 */
4959 	return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
4960 
4961 out_cmd_mempool:
4962 	mempool_destroy(qla_tgt_mgmt_cmd_mempool);
4963 out_mgmt_cmd_cachep:
4964 	kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
4965 out:
4966 	kmem_cache_destroy(qla_tgt_cmd_cachep);
4967 	return ret;
4968 }
4969 
4970 void qlt_exit(void)
4971 {
4972 	if (!QLA_TGT_MODE_ENABLED())
4973 		return;
4974 
4975 	destroy_workqueue(qla_tgt_wq);
4976 	mempool_destroy(qla_tgt_mgmt_cmd_mempool);
4977 	kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
4978 	kmem_cache_destroy(qla_tgt_cmd_cachep);
4979 }
4980