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