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