xref: /linux/drivers/scsi/libiscsi.c (revision db22de3eb0352d2f8e7cda08f3fa65690e3fd64d)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
27996a778SMike Christie /*
37996a778SMike Christie  * iSCSI lib functions
47996a778SMike Christie  *
57996a778SMike Christie  * Copyright (C) 2006 Red Hat, Inc.  All rights reserved.
67996a778SMike Christie  * Copyright (C) 2004 - 2006 Mike Christie
77996a778SMike Christie  * Copyright (C) 2004 - 2005 Dmitry Yusupov
87996a778SMike Christie  * Copyright (C) 2004 - 2005 Alex Aizman
97996a778SMike Christie  * maintained by open-iscsi@googlegroups.com
107996a778SMike Christie  */
117996a778SMike Christie #include <linux/types.h>
127996a778SMike Christie #include <linux/kfifo.h>
137996a778SMike Christie #include <linux/delay.h>
1411836572Svignesh babu #include <linux/log2.h>
155a0e3ad6STejun Heo #include <linux/slab.h>
163f07c014SIngo Molnar #include <linux/sched/signal.h>
17acf3368fSPaul Gortmaker #include <linux/module.h>
188eb00539SMike Christie #include <asm/unaligned.h>
197996a778SMike Christie #include <net/tcp.h>
207996a778SMike Christie #include <scsi/scsi_cmnd.h>
217996a778SMike Christie #include <scsi/scsi_device.h>
227996a778SMike Christie #include <scsi/scsi_eh.h>
237996a778SMike Christie #include <scsi/scsi_tcq.h>
247996a778SMike Christie #include <scsi/scsi_host.h>
257996a778SMike Christie #include <scsi/scsi.h>
267996a778SMike Christie #include <scsi/iscsi_proto.h>
277996a778SMike Christie #include <scsi/scsi_transport.h>
287996a778SMike Christie #include <scsi/scsi_transport_iscsi.h>
297996a778SMike Christie #include <scsi/libiscsi.h>
30c2332b00SFred Herard #include <trace/events/iscsi.h>
317996a778SMike Christie 
32bd2199d4SErez Zilber static int iscsi_dbg_lib_conn;
33bd2199d4SErez Zilber module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
34bd2199d4SErez Zilber 		   S_IRUGO | S_IWUSR);
35bd2199d4SErez Zilber MODULE_PARM_DESC(debug_libiscsi_conn,
36bd2199d4SErez Zilber 		 "Turn on debugging for connections in libiscsi module. "
37bd2199d4SErez Zilber 		 "Set to 1 to turn on, and zero to turn off. Default is off.");
38bd2199d4SErez Zilber 
39bd2199d4SErez Zilber static int iscsi_dbg_lib_session;
40bd2199d4SErez Zilber module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
41bd2199d4SErez Zilber 		   S_IRUGO | S_IWUSR);
42bd2199d4SErez Zilber MODULE_PARM_DESC(debug_libiscsi_session,
43bd2199d4SErez Zilber 		 "Turn on debugging for sessions in libiscsi module. "
44bd2199d4SErez Zilber 		 "Set to 1 to turn on, and zero to turn off. Default is off.");
45bd2199d4SErez Zilber 
46bd2199d4SErez Zilber static int iscsi_dbg_lib_eh;
47bd2199d4SErez Zilber module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
48bd2199d4SErez Zilber 		   S_IRUGO | S_IWUSR);
49bd2199d4SErez Zilber MODULE_PARM_DESC(debug_libiscsi_eh,
50bd2199d4SErez Zilber 		 "Turn on debugging for error handling in libiscsi module. "
51bd2199d4SErez Zilber 		 "Set to 1 to turn on, and zero to turn off. Default is off.");
521b2c7af8SMike Christie 
531b2c7af8SMike Christie #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...)			\
541b2c7af8SMike Christie 	do {							\
55bd2199d4SErez Zilber 		if (iscsi_dbg_lib_conn)				\
561b2c7af8SMike Christie 			iscsi_conn_printk(KERN_INFO, _conn,	\
571b2c7af8SMike Christie 					     "%s " dbg_fmt,	\
581b2c7af8SMike Christie 					     __func__, ##arg);	\
59c2332b00SFred Herard 		iscsi_dbg_trace(trace_iscsi_dbg_conn,		\
60c2332b00SFred Herard 				&(_conn)->cls_conn->dev,	\
61c2332b00SFred Herard 				"%s " dbg_fmt, __func__, ##arg);\
621b2c7af8SMike Christie 	} while (0);
631b2c7af8SMike Christie 
641b2c7af8SMike Christie #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...)			\
651b2c7af8SMike Christie 	do {								\
66bd2199d4SErez Zilber 		if (iscsi_dbg_lib_session)				\
67bd2199d4SErez Zilber 			iscsi_session_printk(KERN_INFO, _session,	\
68bd2199d4SErez Zilber 					     "%s " dbg_fmt,		\
69bd2199d4SErez Zilber 					     __func__, ##arg);		\
70c2332b00SFred Herard 		iscsi_dbg_trace(trace_iscsi_dbg_session, 		\
71c2332b00SFred Herard 				&(_session)->cls_session->dev,		\
72c2332b00SFred Herard 				"%s " dbg_fmt, __func__, ##arg);	\
73bd2199d4SErez Zilber 	} while (0);
74bd2199d4SErez Zilber 
75bd2199d4SErez Zilber #define ISCSI_DBG_EH(_session, dbg_fmt, arg...)				\
76bd2199d4SErez Zilber 	do {								\
77bd2199d4SErez Zilber 		if (iscsi_dbg_lib_eh)					\
781b2c7af8SMike Christie 			iscsi_session_printk(KERN_INFO, _session,	\
791b2c7af8SMike Christie 					     "%s " dbg_fmt,		\
801b2c7af8SMike Christie 					     __func__, ##arg);		\
81c2332b00SFred Herard 		iscsi_dbg_trace(trace_iscsi_dbg_eh,			\
82c2332b00SFred Herard 				&(_session)->cls_session->dev,		\
83c2332b00SFred Herard 				"%s " dbg_fmt, __func__, ##arg);	\
841b2c7af8SMike Christie 	} while (0);
851b2c7af8SMike Christie 
8632ae763eSMike Christie inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
8732ae763eSMike Christie {
8832ae763eSMike Christie 	struct Scsi_Host *shost = conn->session->host;
8932ae763eSMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
9032ae763eSMike Christie 
911336aed1SMike Christie 	if (ihost->workq)
9232ae763eSMike Christie 		queue_work(ihost->workq, &conn->xmitwork);
9332ae763eSMike Christie }
9432ae763eSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
9532ae763eSMike Christie 
964c0ba5d2SMike Christie static void __iscsi_update_cmdsn(struct iscsi_session *session,
974c0ba5d2SMike Christie 				 uint32_t exp_cmdsn, uint32_t max_cmdsn)
987996a778SMike Christie {
9977a23c21SMike Christie 	/*
10077a23c21SMike Christie 	 * standard specifies this check for when to update expected and
10177a23c21SMike Christie 	 * max sequence numbers
10277a23c21SMike Christie 	 */
10377a23c21SMike Christie 	if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
10477a23c21SMike Christie 		return;
10577a23c21SMike Christie 
10677a23c21SMike Christie 	if (exp_cmdsn != session->exp_cmdsn &&
10777a23c21SMike Christie 	    !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
1087996a778SMike Christie 		session->exp_cmdsn = exp_cmdsn;
1097996a778SMike Christie 
11077a23c21SMike Christie 	if (max_cmdsn != session->max_cmdsn &&
11146a84c65SMike Christie 	    !iscsi_sna_lt(max_cmdsn, session->max_cmdsn))
11277a23c21SMike Christie 		session->max_cmdsn = max_cmdsn;
11377a23c21SMike Christie }
1144c0ba5d2SMike Christie 
1154c0ba5d2SMike Christie void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
1164c0ba5d2SMike Christie {
1174c0ba5d2SMike Christie 	__iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
1184c0ba5d2SMike Christie 			     be32_to_cpu(hdr->max_cmdsn));
1194c0ba5d2SMike Christie }
12077a23c21SMike Christie EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
1217996a778SMike Christie 
122577577daSMike Christie /**
123577577daSMike Christie  * iscsi_prep_data_out_pdu - initialize Data-Out
124577577daSMike Christie  * @task: scsi command task
125577577daSMike Christie  * @r2t: R2T info
126577577daSMike Christie  * @hdr: iscsi data in pdu
127577577daSMike Christie  *
128577577daSMike Christie  * Notes:
129577577daSMike Christie  *	Initialize Data-Out within this R2T sequence and finds
130577577daSMike Christie  *	proper data_offset within this SCSI command.
131577577daSMike Christie  *
132577577daSMike Christie  *	This function is called with connection lock taken.
133577577daSMike Christie  **/
134577577daSMike Christie void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
135ffd0436eSMike Christie 			   struct iscsi_data *hdr)
1367996a778SMike Christie {
1379c19a7d0SMike Christie 	struct iscsi_conn *conn = task->conn;
138577577daSMike Christie 	unsigned int left = r2t->data_length - r2t->sent;
139577577daSMike Christie 
140577577daSMike Christie 	task->hdr_len = sizeof(struct iscsi_data);
1417996a778SMike Christie 
1427996a778SMike Christie 	memset(hdr, 0, sizeof(struct iscsi_data));
143577577daSMike Christie 	hdr->ttt = r2t->ttt;
144577577daSMike Christie 	hdr->datasn = cpu_to_be32(r2t->datasn);
145577577daSMike Christie 	r2t->datasn++;
1467996a778SMike Christie 	hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
14755bdabdfSAndy Grover 	hdr->lun = task->lun;
148577577daSMike Christie 	hdr->itt = task->hdr_itt;
149577577daSMike Christie 	hdr->exp_statsn = r2t->exp_statsn;
150577577daSMike Christie 	hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
151577577daSMike Christie 	if (left > conn->max_xmit_dlength) {
1527996a778SMike Christie 		hton24(hdr->dlength, conn->max_xmit_dlength);
153577577daSMike Christie 		r2t->data_count = conn->max_xmit_dlength;
1547996a778SMike Christie 		hdr->flags = 0;
1557996a778SMike Christie 	} else {
156577577daSMike Christie 		hton24(hdr->dlength, left);
157577577daSMike Christie 		r2t->data_count = left;
1587996a778SMike Christie 		hdr->flags = ISCSI_FLAG_CMD_FINAL;
1597996a778SMike Christie 	}
160577577daSMike Christie 	conn->dataout_pdus_cnt++;
1617996a778SMike Christie }
162577577daSMike Christie EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
1637996a778SMike Christie 
1649c19a7d0SMike Christie static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
165004d6530SBoaz Harrosh {
1669c19a7d0SMike Christie 	unsigned exp_len = task->hdr_len + len;
167004d6530SBoaz Harrosh 
1689c19a7d0SMike Christie 	if (exp_len > task->hdr_max) {
169004d6530SBoaz Harrosh 		WARN_ON(1);
170004d6530SBoaz Harrosh 		return -EINVAL;
171004d6530SBoaz Harrosh 	}
172004d6530SBoaz Harrosh 
173004d6530SBoaz Harrosh 	WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
1749c19a7d0SMike Christie 	task->hdr_len = exp_len;
175004d6530SBoaz Harrosh 	return 0;
176004d6530SBoaz Harrosh }
177004d6530SBoaz Harrosh 
17838d1c069SBoaz Harrosh /*
17938d1c069SBoaz Harrosh  * make an extended cdb AHS
18038d1c069SBoaz Harrosh  */
1819c19a7d0SMike Christie static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
18238d1c069SBoaz Harrosh {
1839c19a7d0SMike Christie 	struct scsi_cmnd *cmd = task->sc;
18438d1c069SBoaz Harrosh 	unsigned rlen, pad_len;
18538d1c069SBoaz Harrosh 	unsigned short ahslength;
18638d1c069SBoaz Harrosh 	struct iscsi_ecdb_ahdr *ecdb_ahdr;
18738d1c069SBoaz Harrosh 	int rc;
18838d1c069SBoaz Harrosh 
1899c19a7d0SMike Christie 	ecdb_ahdr = iscsi_next_hdr(task);
19038d1c069SBoaz Harrosh 	rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
19138d1c069SBoaz Harrosh 
19238d1c069SBoaz Harrosh 	BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
19338d1c069SBoaz Harrosh 	ahslength = rlen + sizeof(ecdb_ahdr->reserved);
19438d1c069SBoaz Harrosh 
19538d1c069SBoaz Harrosh 	pad_len = iscsi_padding(rlen);
19638d1c069SBoaz Harrosh 
1979c19a7d0SMike Christie 	rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
19838d1c069SBoaz Harrosh 	                   sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
19938d1c069SBoaz Harrosh 	if (rc)
20038d1c069SBoaz Harrosh 		return rc;
20138d1c069SBoaz Harrosh 
20238d1c069SBoaz Harrosh 	if (pad_len)
20338d1c069SBoaz Harrosh 		memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
20438d1c069SBoaz Harrosh 
20538d1c069SBoaz Harrosh 	ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
20638d1c069SBoaz Harrosh 	ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
20738d1c069SBoaz Harrosh 	ecdb_ahdr->reserved = 0;
20838d1c069SBoaz Harrosh 	memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
20938d1c069SBoaz Harrosh 
2101b2c7af8SMike Christie 	ISCSI_DBG_SESSION(task->conn->session,
2111b2c7af8SMike Christie 			  "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
2121b2c7af8SMike Christie 		          "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
2131b2c7af8SMike Christie 		          "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
2141b2c7af8SMike Christie 		          task->hdr_len);
21538d1c069SBoaz Harrosh 	return 0;
21638d1c069SBoaz Harrosh }
21738d1c069SBoaz Harrosh 
2187996a778SMike Christie /**
2195d12c05eSMike Christie  * iscsi_check_tmf_restrictions - check if a task is affected by TMF
2205d12c05eSMike Christie  * @task: iscsi task
2215d12c05eSMike Christie  * @opcode: opcode to check for
2225d12c05eSMike Christie  *
2235d12c05eSMike Christie  * During TMF a task has to be checked if it's affected.
2245d12c05eSMike Christie  * All unrelated I/O can be passed through, but I/O to the
2255d12c05eSMike Christie  * affected LUN should be restricted.
2265d12c05eSMike Christie  * If 'fast_abort' is set we won't be sending any I/O to the
2275d12c05eSMike Christie  * affected LUN.
2285d12c05eSMike Christie  * Otherwise the target is waiting for all TTTs to be completed,
2295d12c05eSMike Christie  * so we have to send all outstanding Data-Out PDUs to the target.
2305d12c05eSMike Christie  */
2315d12c05eSMike Christie static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
2325d12c05eSMike Christie {
233ec29d0acSMike Christie 	struct iscsi_session *session = task->conn->session;
234ec29d0acSMike Christie 	struct iscsi_tm *tmf = &session->tmhdr;
2359cb78c16SHannes Reinecke 	u64 hdr_lun;
2365d12c05eSMike Christie 
237ec29d0acSMike Christie 	if (session->tmf_state == TMF_INITIAL)
2385d12c05eSMike Christie 		return 0;
2395d12c05eSMike Christie 
2405d12c05eSMike Christie 	if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
2415d12c05eSMike Christie 		return 0;
2425d12c05eSMike Christie 
2435d12c05eSMike Christie 	switch (ISCSI_TM_FUNC_VALUE(tmf)) {
2445d12c05eSMike Christie 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2455d12c05eSMike Christie 		/*
2465d12c05eSMike Christie 		 * Allow PDUs for unrelated LUNs
2475d12c05eSMike Christie 		 */
24855bdabdfSAndy Grover 		hdr_lun = scsilun_to_int(&tmf->lun);
2495d12c05eSMike Christie 		if (hdr_lun != task->sc->device->lun)
2505d12c05eSMike Christie 			return 0;
251df561f66SGustavo A. R. Silva 		fallthrough;
2523fe5ae8bSMike Christie 	case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2535d12c05eSMike Christie 		/*
2545d12c05eSMike Christie 		 * Fail all SCSI cmd PDUs
2555d12c05eSMike Christie 		 */
2565d12c05eSMike Christie 		if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
257ec29d0acSMike Christie 			iscsi_session_printk(KERN_INFO, session,
258ec29d0acSMike Christie 					     "task [op %x itt 0x%x/0x%x] rejected.\n",
259ec29d0acSMike Christie 					     opcode, task->itt, task->hdr_itt);
2605d12c05eSMike Christie 			return -EACCES;
2615d12c05eSMike Christie 		}
2625d12c05eSMike Christie 		/*
2635d12c05eSMike Christie 		 * And also all data-out PDUs in response to R2T
2645d12c05eSMike Christie 		 * if fast_abort is set.
2655d12c05eSMike Christie 		 */
266ec29d0acSMike Christie 		if (session->fast_abort) {
267ec29d0acSMike Christie 			iscsi_session_printk(KERN_INFO, session,
268ec29d0acSMike Christie 					     "task [op %x itt 0x%x/0x%x] fast abort.\n",
269ec29d0acSMike Christie 					     opcode, task->itt, task->hdr_itt);
2705d12c05eSMike Christie 			return -EACCES;
2715d12c05eSMike Christie 		}
2725d12c05eSMike Christie 		break;
2735d12c05eSMike Christie 	case ISCSI_TM_FUNC_ABORT_TASK:
2745d12c05eSMike Christie 		/*
2755d12c05eSMike Christie 		 * the caller has already checked if the task
2765d12c05eSMike Christie 		 * they want to abort was in the pending queue so if
2775d12c05eSMike Christie 		 * we are here the cmd pdu has gone out already, and
2785d12c05eSMike Christie 		 * we will only hit this for data-outs
2795d12c05eSMike Christie 		 */
2805d12c05eSMike Christie 		if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
2815d12c05eSMike Christie 		    task->hdr_itt == tmf->rtt) {
282ec29d0acSMike Christie 			ISCSI_DBG_SESSION(session,
2835d12c05eSMike Christie 					  "Preventing task %x/%x from sending "
2845d12c05eSMike Christie 					  "data-out due to abort task in "
2855d12c05eSMike Christie 					  "progress\n", task->itt,
2865d12c05eSMike Christie 					  task->hdr_itt);
2875d12c05eSMike Christie 			return -EACCES;
2885d12c05eSMike Christie 		}
2895d12c05eSMike Christie 		break;
2905d12c05eSMike Christie 	}
2915d12c05eSMike Christie 
2925d12c05eSMike Christie 	return 0;
2935d12c05eSMike Christie }
2945d12c05eSMike Christie 
2955d12c05eSMike Christie /**
2967996a778SMike Christie  * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
2979c19a7d0SMike Christie  * @task: iscsi task
2987996a778SMike Christie  *
2997996a778SMike Christie  * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
3007996a778SMike Christie  * fields like dlength or final based on how much data it sends
3017996a778SMike Christie  */
3029c19a7d0SMike Christie static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
3037996a778SMike Christie {
3049c19a7d0SMike Christie 	struct iscsi_conn *conn = task->conn;
3057996a778SMike Christie 	struct iscsi_session *session = conn->session;
3069c19a7d0SMike Christie 	struct scsi_cmnd *sc = task->sc;
30712352183SNicholas Bellinger 	struct iscsi_scsi_req *hdr;
308d77e6535SSagi Grimberg 	unsigned hdrlength, cmd_len, transfer_length;
309262ef636SMike Christie 	itt_t itt;
310004d6530SBoaz Harrosh 	int rc;
3117996a778SMike Christie 
3125d12c05eSMike Christie 	rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
3135d12c05eSMike Christie 	if (rc)
3145d12c05eSMike Christie 		return rc;
3155d12c05eSMike Christie 
316184b57c6SMike Christie 	if (conn->session->tt->alloc_pdu) {
3172ff79d52SMike Christie 		rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
318577577daSMike Christie 		if (rc)
319577577daSMike Christie 			return rc;
320184b57c6SMike Christie 	}
32112352183SNicholas Bellinger 	hdr = (struct iscsi_scsi_req *)task->hdr;
322262ef636SMike Christie 	itt = hdr->itt;
323577577daSMike Christie 	memset(hdr, 0, sizeof(*hdr));
324577577daSMike Christie 
3252ff79d52SMike Christie 	if (session->tt->parse_pdu_itt)
3262ff79d52SMike Christie 		hdr->itt = task->hdr_itt = itt;
3272ff79d52SMike Christie 	else
3282ff79d52SMike Christie 		hdr->itt = task->hdr_itt = build_itt(task->itt,
3292ff79d52SMike Christie 						     task->conn->session->age);
3309c19a7d0SMike Christie 	task->hdr_len = 0;
3319c19a7d0SMike Christie 	rc = iscsi_add_hdr(task, sizeof(*hdr));
332004d6530SBoaz Harrosh 	if (rc)
333004d6530SBoaz Harrosh 		return rc;
3347996a778SMike Christie 	hdr->opcode = ISCSI_OP_SCSI_CMD;
3357996a778SMike Christie 	hdr->flags = ISCSI_ATTR_SIMPLE;
33655bdabdfSAndy Grover 	int_to_scsilun(sc->device->lun, &hdr->lun);
33755bdabdfSAndy Grover 	task->lun = hdr->lun;
3387996a778SMike Christie 	hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
33938d1c069SBoaz Harrosh 	cmd_len = sc->cmd_len;
34038d1c069SBoaz Harrosh 	if (cmd_len < ISCSI_CDB_SIZE)
34138d1c069SBoaz Harrosh 		memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
34238d1c069SBoaz Harrosh 	else if (cmd_len > ISCSI_CDB_SIZE) {
3439c19a7d0SMike Christie 		rc = iscsi_prep_ecdb_ahs(task);
34438d1c069SBoaz Harrosh 		if (rc)
34538d1c069SBoaz Harrosh 			return rc;
34638d1c069SBoaz Harrosh 		cmd_len = ISCSI_CDB_SIZE;
34738d1c069SBoaz Harrosh 	}
34838d1c069SBoaz Harrosh 	memcpy(hdr->cdb, sc->cmnd, cmd_len);
3497996a778SMike Christie 
3509c19a7d0SMike Christie 	task->imm_count = 0;
35155e51edaSSagi Grimberg 	if (scsi_get_prot_op(sc) != SCSI_PROT_NORMAL)
35255e51edaSSagi Grimberg 		task->protected = true;
35355e51edaSSagi Grimberg 
354d77e6535SSagi Grimberg 	transfer_length = scsi_transfer_length(sc);
355d77e6535SSagi Grimberg 	hdr->data_length = cpu_to_be32(transfer_length);
3567996a778SMike Christie 	if (sc->sc_data_direction == DMA_TO_DEVICE) {
357577577daSMike Christie 		struct iscsi_r2t_info *r2t = &task->unsol_r2t;
358577577daSMike Christie 
3597996a778SMike Christie 		hdr->flags |= ISCSI_FLAG_CMD_WRITE;
3607996a778SMike Christie 		/*
3617996a778SMike Christie 		 * Write counters:
3627996a778SMike Christie 		 *
3637996a778SMike Christie 		 *	imm_count	bytes to be sent right after
3647996a778SMike Christie 		 *			SCSI PDU Header
3657996a778SMike Christie 		 *
3667996a778SMike Christie 		 *	unsol_count	bytes(as Data-Out) to be sent
3677996a778SMike Christie 		 *			without	R2T ack right after
3687996a778SMike Christie 		 *			immediate data
3697996a778SMike Christie 		 *
370577577daSMike Christie 		 *	r2t data_length bytes to be sent via R2T ack's
3717996a778SMike Christie 		 *
3727996a778SMike Christie 		 *      pad_count       bytes to be sent as zero-padding
3737996a778SMike Christie 		 */
374577577daSMike Christie 		memset(r2t, 0, sizeof(*r2t));
3757996a778SMike Christie 
3767996a778SMike Christie 		if (session->imm_data_en) {
377d77e6535SSagi Grimberg 			if (transfer_length >= session->first_burst)
3789c19a7d0SMike Christie 				task->imm_count = min(session->first_burst,
3797996a778SMike Christie 							conn->max_xmit_dlength);
3807996a778SMike Christie 			else
381d77e6535SSagi Grimberg 				task->imm_count = min(transfer_length,
3827996a778SMike Christie 						      conn->max_xmit_dlength);
3839c19a7d0SMike Christie 			hton24(hdr->dlength, task->imm_count);
3847996a778SMike Christie 		} else
385a8ac6311SOlaf Kirch 			zero_data(hdr->dlength);
3867996a778SMike Christie 
387ffd0436eSMike Christie 		if (!session->initial_r2t_en) {
388d77e6535SSagi Grimberg 			r2t->data_length = min(session->first_burst,
389d77e6535SSagi Grimberg 					       transfer_length) -
390577577daSMike Christie 					       task->imm_count;
391577577daSMike Christie 			r2t->data_offset = task->imm_count;
392577577daSMike Christie 			r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
393577577daSMike Christie 			r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
394ffd0436eSMike Christie 		}
395ffd0436eSMike Christie 
396577577daSMike Christie 		if (!task->unsol_r2t.data_length)
3977996a778SMike Christie 			/* No unsolicit Data-Out's */
398a8ac6311SOlaf Kirch 			hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3997996a778SMike Christie 	} else {
4007996a778SMike Christie 		hdr->flags |= ISCSI_FLAG_CMD_FINAL;
4017996a778SMike Christie 		zero_data(hdr->dlength);
4027996a778SMike Christie 
4037996a778SMike Christie 		if (sc->sc_data_direction == DMA_FROM_DEVICE)
4047996a778SMike Christie 			hdr->flags |= ISCSI_FLAG_CMD_READ;
4057996a778SMike Christie 	}
4067996a778SMike Christie 
407004d6530SBoaz Harrosh 	/* calculate size of additional header segments (AHSs) */
4089c19a7d0SMike Christie 	hdrlength = task->hdr_len - sizeof(*hdr);
409004d6530SBoaz Harrosh 
410004d6530SBoaz Harrosh 	WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
411004d6530SBoaz Harrosh 	hdrlength /= ISCSI_PAD_LEN;
412004d6530SBoaz Harrosh 
413004d6530SBoaz Harrosh 	WARN_ON(hdrlength >= 256);
414004d6530SBoaz Harrosh 	hdr->hlength = hdrlength & 0xFF;
41596b1f96dSMike Christie 	hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
416004d6530SBoaz Harrosh 
417577577daSMike Christie 	if (session->tt->init_task && session->tt->init_task(task))
418052d0144SMike Christie 		return -EIO;
419052d0144SMike Christie 
4209c19a7d0SMike Christie 	task->state = ISCSI_TASK_RUNNING;
421d3305f34SMike Christie 	session->cmdsn++;
42277a23c21SMike Christie 
423a8ac6311SOlaf Kirch 	conn->scsicmd_pdus_cnt++;
4241b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
425ae3d56d8SChristoph Hellwig 			  "itt 0x%x len %d cmdsn %d win %d]\n",
4261b2c7af8SMike Christie 			  sc->sc_data_direction == DMA_TO_DEVICE ?
4271b2c7af8SMike Christie 			  "write" : "read", conn->id, sc, sc->cmnd[0],
428d77e6535SSagi Grimberg 			  task->itt, transfer_length,
4291b2c7af8SMike Christie 			  session->cmdsn,
4301b2c7af8SMike Christie 			  session->max_cmdsn - session->exp_cmdsn + 1);
431004d6530SBoaz Harrosh 	return 0;
4327996a778SMike Christie }
4337996a778SMike Christie 
4347996a778SMike Christie /**
4353bbaaad9SMike Christie  * iscsi_free_task - free a task
4369c19a7d0SMike Christie  * @task: iscsi cmd task
4377996a778SMike Christie  *
438659743b0SShlomo Pongratz  * Must be called with session back_lock.
4393e5c28adSMike Christie  * This function returns the scsi command to scsi-ml or cleans
4403e5c28adSMike Christie  * up mgmt tasks then returns the task to the pool.
4417996a778SMike Christie  */
4423bbaaad9SMike Christie static void iscsi_free_task(struct iscsi_task *task)
4437996a778SMike Christie {
4449c19a7d0SMike Christie 	struct iscsi_conn *conn = task->conn;
445c1635cb7SMike Christie 	struct iscsi_session *session = conn->session;
4469c19a7d0SMike Christie 	struct scsi_cmnd *sc = task->sc;
447f41d4721SMike Christie 	int oldstate = task->state;
4487996a778SMike Christie 
4494421c9ebSMike Christie 	ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
4504421c9ebSMike Christie 			  task->itt, task->state, task->sc);
4514421c9ebSMike Christie 
452577577daSMike Christie 	session->tt->cleanup_task(task);
4533bbaaad9SMike Christie 	task->state = ISCSI_TASK_FREE;
4549c19a7d0SMike Christie 	task->sc = NULL;
4553e5c28adSMike Christie 	/*
4569c19a7d0SMike Christie 	 * login task is preallocated so do not free
4573e5c28adSMike Christie 	 */
4589c19a7d0SMike Christie 	if (conn->login_task == task)
4593e5c28adSMike Christie 		return;
4603e5c28adSMike Christie 
4617acd72ebSStefani Seibold 	kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
4623e5c28adSMike Christie 
4633e5c28adSMike Christie 	if (sc) {
464f47f2cf5SMike Christie 		/* SCSI eh reuses commands to verify us */
465*db22de3eSBart Van Assche 		iscsi_cmd(sc)->task = NULL;
4663e5c28adSMike Christie 		/*
467f41d4721SMike Christie 		 * queue command may call this to free the task, so
468f41d4721SMike Christie 		 * it will decide how to return sc to scsi-ml.
4693e5c28adSMike Christie 		 */
470f41d4721SMike Christie 		if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
471b4b84edcSBart Van Assche 			scsi_done(sc);
4727996a778SMike Christie 	}
4733e5c28adSMike Christie }
4747996a778SMike Christie 
475913e5bf4SMike Christie void __iscsi_get_task(struct iscsi_task *task)
47660ecebf5SMike Christie {
4776dc618cdSElena Reshetova 	refcount_inc(&task->refcount);
47860ecebf5SMike Christie }
479913e5bf4SMike Christie EXPORT_SYMBOL_GPL(__iscsi_get_task);
48060ecebf5SMike Christie 
4818eea2f55SEddie Wai void __iscsi_put_task(struct iscsi_task *task)
48260ecebf5SMike Christie {
4836dc618cdSElena Reshetova 	if (refcount_dec_and_test(&task->refcount))
4843bbaaad9SMike Christie 		iscsi_free_task(task);
48560ecebf5SMike Christie }
4868eea2f55SEddie Wai EXPORT_SYMBOL_GPL(__iscsi_put_task);
48760ecebf5SMike Christie 
4889c19a7d0SMike Christie void iscsi_put_task(struct iscsi_task *task)
4893e5c28adSMike Christie {
4909c19a7d0SMike Christie 	struct iscsi_session *session = task->conn->session;
4913e5c28adSMike Christie 
492659743b0SShlomo Pongratz 	/* regular RX path uses back_lock */
493659743b0SShlomo Pongratz 	spin_lock_bh(&session->back_lock);
4949c19a7d0SMike Christie 	__iscsi_put_task(task);
495659743b0SShlomo Pongratz 	spin_unlock_bh(&session->back_lock);
4963e5c28adSMike Christie }
4979c19a7d0SMike Christie EXPORT_SYMBOL_GPL(iscsi_put_task);
4983e5c28adSMike Christie 
4993bbaaad9SMike Christie /**
5003bbaaad9SMike Christie  * iscsi_complete_task - finish a task
5013bbaaad9SMike Christie  * @task: iscsi cmd task
502b3cd5050SMike Christie  * @state: state to complete task with
5033bbaaad9SMike Christie  *
504659743b0SShlomo Pongratz  * Must be called with session back_lock.
505b3a7ea8dSMike Christie  */
506b3cd5050SMike Christie static void iscsi_complete_task(struct iscsi_task *task, int state)
507b3a7ea8dSMike Christie {
5083bbaaad9SMike Christie 	struct iscsi_conn *conn = task->conn;
5093bbaaad9SMike Christie 
5104421c9ebSMike Christie 	ISCSI_DBG_SESSION(conn->session,
5114421c9ebSMike Christie 			  "complete task itt 0x%x state %d sc %p\n",
5124421c9ebSMike Christie 			  task->itt, task->state, task->sc);
513b3cd5050SMike Christie 	if (task->state == ISCSI_TASK_COMPLETED ||
514b3cd5050SMike Christie 	    task->state == ISCSI_TASK_ABRT_TMF ||
515f41d4721SMike Christie 	    task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
516f41d4721SMike Christie 	    task->state == ISCSI_TASK_REQUEUE_SCSIQ)
5173bbaaad9SMike Christie 		return;
5183bbaaad9SMike Christie 	WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
519b3cd5050SMike Christie 	task->state = state;
5203bbaaad9SMike Christie 
521fe0a8a95SLee Duncan 	if (READ_ONCE(conn->ping_task) == task)
522fe0a8a95SLee Duncan 		WRITE_ONCE(conn->ping_task, NULL);
5233bbaaad9SMike Christie 
5243bbaaad9SMike Christie 	/* release get from queueing */
5253bbaaad9SMike Christie 	__iscsi_put_task(task);
5263bbaaad9SMike Christie }
5273bbaaad9SMike Christie 
5284c0ba5d2SMike Christie /**
5294c0ba5d2SMike Christie  * iscsi_complete_scsi_task - finish scsi task normally
5304c0ba5d2SMike Christie  * @task: iscsi task for scsi cmd
5314c0ba5d2SMike Christie  * @exp_cmdsn: expected cmd sn in cpu format
5324c0ba5d2SMike Christie  * @max_cmdsn: max cmd sn in cpu format
5334c0ba5d2SMike Christie  *
5344c0ba5d2SMike Christie  * This is used when drivers do not need or cannot perform
5354c0ba5d2SMike Christie  * lower level pdu processing.
5364c0ba5d2SMike Christie  *
537659743b0SShlomo Pongratz  * Called with session back_lock
5384c0ba5d2SMike Christie  */
5394c0ba5d2SMike Christie void iscsi_complete_scsi_task(struct iscsi_task *task,
5404c0ba5d2SMike Christie 			      uint32_t exp_cmdsn, uint32_t max_cmdsn)
5414c0ba5d2SMike Christie {
5424c0ba5d2SMike Christie 	struct iscsi_conn *conn = task->conn;
5434c0ba5d2SMike Christie 
5444c0ba5d2SMike Christie 	ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
5454c0ba5d2SMike Christie 
5464c0ba5d2SMike Christie 	conn->last_recv = jiffies;
5474c0ba5d2SMike Christie 	__iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
5484c0ba5d2SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
5494c0ba5d2SMike Christie }
5504c0ba5d2SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
5514c0ba5d2SMike Christie 
5525923d64bSMike Christie /*
5535923d64bSMike Christie  * Must be called with back and frwd lock
5545923d64bSMike Christie  */
5555923d64bSMike Christie static bool cleanup_queued_task(struct iscsi_task *task)
5565923d64bSMike Christie {
5575923d64bSMike Christie 	struct iscsi_conn *conn = task->conn;
5585923d64bSMike Christie 	bool early_complete = false;
5595923d64bSMike Christie 
5605923d64bSMike Christie 	/* Bad target might have completed task while it was still running */
5615923d64bSMike Christie 	if (task->state == ISCSI_TASK_COMPLETED)
5625923d64bSMike Christie 		early_complete = true;
5635923d64bSMike Christie 
5645923d64bSMike Christie 	if (!list_empty(&task->running)) {
5655923d64bSMike Christie 		list_del_init(&task->running);
5665923d64bSMike Christie 		/*
5675923d64bSMike Christie 		 * If it's on a list but still running, this could be from
5685923d64bSMike Christie 		 * a bad target sending a rsp early, cleanup from a TMF, or
5695923d64bSMike Christie 		 * session recovery.
5705923d64bSMike Christie 		 */
5715923d64bSMike Christie 		if (task->state == ISCSI_TASK_RUNNING ||
5725923d64bSMike Christie 		    task->state == ISCSI_TASK_COMPLETED)
5735923d64bSMike Christie 			__iscsi_put_task(task);
5745923d64bSMike Christie 	}
5755923d64bSMike Christie 
57699b06033SMike Christie 	if (conn->session->running_aborted_task == task) {
57799b06033SMike Christie 		conn->session->running_aborted_task = NULL;
57899b06033SMike Christie 		__iscsi_put_task(task);
57999b06033SMike Christie 	}
58099b06033SMike Christie 
5815923d64bSMike Christie 	if (conn->task == task) {
5825923d64bSMike Christie 		conn->task = NULL;
5835923d64bSMike Christie 		__iscsi_put_task(task);
5845923d64bSMike Christie 	}
5855923d64bSMike Christie 
5865923d64bSMike Christie 	return early_complete;
5875923d64bSMike Christie }
5884c0ba5d2SMike Christie 
5893bbaaad9SMike Christie /*
59014936b1eSMike Christie  * session frwd lock must be held and if not called for a task that is still
59114936b1eSMike Christie  * pending or from the xmit thread, then xmit thread must be suspended
5923bbaaad9SMike Christie  */
5933bbaaad9SMike Christie static void fail_scsi_task(struct iscsi_task *task, int err)
5943bbaaad9SMike Christie {
5953bbaaad9SMike Christie 	struct iscsi_conn *conn = task->conn;
596b3a7ea8dSMike Christie 	struct scsi_cmnd *sc;
597b3cd5050SMike Christie 	int state;
598b3a7ea8dSMike Christie 
5995923d64bSMike Christie 	spin_lock_bh(&conn->session->back_lock);
6005923d64bSMike Christie 	if (cleanup_queued_task(task)) {
6015923d64bSMike Christie 		spin_unlock_bh(&conn->session->back_lock);
6025923d64bSMike Christie 		return;
6035923d64bSMike Christie 	}
6045923d64bSMike Christie 
605b3cd5050SMike Christie 	if (task->state == ISCSI_TASK_PENDING) {
606b3a7ea8dSMike Christie 		/*
607b3a7ea8dSMike Christie 		 * cmd never made it to the xmit thread, so we should not count
608b3a7ea8dSMike Christie 		 * the cmd in the sequencing
609b3a7ea8dSMike Christie 		 */
610b3a7ea8dSMike Christie 		conn->session->queued_cmdsn--;
611b3cd5050SMike Christie 		/* it was never sent so just complete like normal */
612b3cd5050SMike Christie 		state = ISCSI_TASK_COMPLETED;
613b3cd5050SMike Christie 	} else if (err == DID_TRANSPORT_DISRUPTED)
614b3cd5050SMike Christie 		state = ISCSI_TASK_ABRT_SESS_RECOV;
615b3cd5050SMike Christie 	else
616b3cd5050SMike Christie 		state = ISCSI_TASK_ABRT_TMF;
617b3a7ea8dSMike Christie 
61814936b1eSMike Christie 	sc = task->sc;
619b3cd5050SMike Christie 	sc->result = err << 16;
620b3a7ea8dSMike Christie 	scsi_set_resid(sc, scsi_bufflen(sc));
621b3cd5050SMike Christie 	iscsi_complete_task(task, state);
622659743b0SShlomo Pongratz 	spin_unlock_bh(&conn->session->back_lock);
623b3a7ea8dSMike Christie }
624b3a7ea8dSMike Christie 
6253e5c28adSMike Christie static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
6269c19a7d0SMike Christie 				struct iscsi_task *task)
627052d0144SMike Christie {
628052d0144SMike Christie 	struct iscsi_session *session = conn->session;
629577577daSMike Christie 	struct iscsi_hdr *hdr = task->hdr;
630052d0144SMike Christie 	struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
6314f704dc0SMike Christie 	uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
632052d0144SMike Christie 
633052d0144SMike Christie 	if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
634052d0144SMike Christie 		return -ENOTCONN;
635052d0144SMike Christie 
6364f704dc0SMike Christie 	if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
637052d0144SMike Christie 		nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
638052d0144SMike Christie 	/*
639052d0144SMike Christie 	 * pre-format CmdSN for outgoing PDU.
640052d0144SMike Christie 	 */
641052d0144SMike Christie 	nop->cmdsn = cpu_to_be32(session->cmdsn);
642052d0144SMike Christie 	if (hdr->itt != RESERVED_ITT) {
643052d0144SMike Christie 		/*
6444f704dc0SMike Christie 		 * TODO: We always use immediate for normal session pdus.
645052d0144SMike Christie 		 * If we start to send tmfs or nops as non-immediate then
646052d0144SMike Christie 		 * we should start checking the cmdsn numbers for mgmt tasks.
6474f704dc0SMike Christie 		 *
6484f704dc0SMike Christie 		 * During discovery sessions iscsid sends TEXT as non immediate,
6494f704dc0SMike Christie 		 * but we always only send one PDU at a time.
650052d0144SMike Christie 		 */
651052d0144SMike Christie 		if (conn->c_stage == ISCSI_CONN_STARTED &&
652052d0144SMike Christie 		    !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
653052d0144SMike Christie 			session->queued_cmdsn++;
654052d0144SMike Christie 			session->cmdsn++;
655052d0144SMike Christie 		}
656052d0144SMike Christie 	}
657052d0144SMike Christie 
658ae15f801SMike Christie 	if (session->tt->init_task && session->tt->init_task(task))
659ae15f801SMike Christie 		return -EIO;
660052d0144SMike Christie 
661052d0144SMike Christie 	if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
662052d0144SMike Christie 		session->state = ISCSI_STATE_LOGGING_OUT;
663052d0144SMike Christie 
664577577daSMike Christie 	task->state = ISCSI_TASK_RUNNING;
6651b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
6661b2c7af8SMike Christie 			  "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
6671b2c7af8SMike Christie 			  hdr->itt, task->data_count);
668052d0144SMike Christie 	return 0;
669052d0144SMike Christie }
670052d0144SMike Christie 
6719c19a7d0SMike Christie static struct iscsi_task *
672f6d5180cSMike Christie __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
673f6d5180cSMike Christie 		      char *data, uint32_t data_size)
674f6d5180cSMike Christie {
675f6d5180cSMike Christie 	struct iscsi_session *session = conn->session;
6761336aed1SMike Christie 	struct iscsi_host *ihost = shost_priv(session->host);
6774f704dc0SMike Christie 	uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
6789c19a7d0SMike Christie 	struct iscsi_task *task;
679262ef636SMike Christie 	itt_t itt;
680f6d5180cSMike Christie 
681f6d5180cSMike Christie 	if (session->state == ISCSI_STATE_TERMINATE)
682f6d5180cSMike Christie 		return NULL;
683f6d5180cSMike Christie 
6844f704dc0SMike Christie 	if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
685f6d5180cSMike Christie 		/*
686f6d5180cSMike Christie 		 * Login and Text are sent serially, in
687f6d5180cSMike Christie 		 * request-followed-by-response sequence.
6883e5c28adSMike Christie 		 * Same task can be used. Same ITT must be used.
6893e5c28adSMike Christie 		 * Note that login_task is preallocated at conn_create().
690f6d5180cSMike Christie 		 */
6914f704dc0SMike Christie 		if (conn->login_task->state != ISCSI_TASK_FREE) {
6924f704dc0SMike Christie 			iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
6934f704dc0SMike Christie 					  "progress. Cannot start new task.\n");
6944f704dc0SMike Christie 			return NULL;
6954f704dc0SMike Christie 		}
6964f704dc0SMike Christie 
697cbaa4221SMike Christie 		if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) {
698cbaa4221SMike Christie 			iscsi_conn_printk(KERN_ERR, conn, "Invalid buffer len of %u for login task. Max len is %u\n", data_size, ISCSI_DEF_MAX_RECV_SEG_LEN);
699cbaa4221SMike Christie 			return NULL;
700cbaa4221SMike Christie 		}
701cbaa4221SMike Christie 
7029c19a7d0SMike Christie 		task = conn->login_task;
7034f704dc0SMike Christie 	} else {
70426013ad4SMike Christie 		if (session->state != ISCSI_STATE_LOGGED_IN)
70526013ad4SMike Christie 			return NULL;
70626013ad4SMike Christie 
707cbaa4221SMike Christie 		if (data_size != 0) {
708cbaa4221SMike Christie 			iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode);
709cbaa4221SMike Christie 			return NULL;
710cbaa4221SMike Christie 		}
711cbaa4221SMike Christie 
712f6d5180cSMike Christie 		BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
713f6d5180cSMike Christie 		BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
714f6d5180cSMike Christie 
7157acd72ebSStefani Seibold 		if (!kfifo_out(&session->cmdpool.queue,
7169c19a7d0SMike Christie 				 (void*)&task, sizeof(void*)))
717f6d5180cSMike Christie 			return NULL;
718f6d5180cSMike Christie 	}
7193e5c28adSMike Christie 	/*
7203e5c28adSMike Christie 	 * released in complete pdu for task we expect a response for, and
7213e5c28adSMike Christie 	 * released by the lld when it has transmitted the task for
7223e5c28adSMike Christie 	 * pdus we do not expect a response for.
7233e5c28adSMike Christie 	 */
7246dc618cdSElena Reshetova 	refcount_set(&task->refcount, 1);
7259c19a7d0SMike Christie 	task->conn = conn;
7269c19a7d0SMike Christie 	task->sc = NULL;
7273bbaaad9SMike Christie 	INIT_LIST_HEAD(&task->running);
7283bbaaad9SMike Christie 	task->state = ISCSI_TASK_PENDING;
729f6d5180cSMike Christie 
730f6d5180cSMike Christie 	if (data_size) {
7319c19a7d0SMike Christie 		memcpy(task->data, data, data_size);
7329c19a7d0SMike Christie 		task->data_count = data_size;
733f6d5180cSMike Christie 	} else
7349c19a7d0SMike Christie 		task->data_count = 0;
735f6d5180cSMike Christie 
736184b57c6SMike Christie 	if (conn->session->tt->alloc_pdu) {
7372ff79d52SMike Christie 		if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
738577577daSMike Christie 			iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
739577577daSMike Christie 					 "pdu for mgmt task.\n");
7403bbaaad9SMike Christie 			goto free_task;
741577577daSMike Christie 		}
742184b57c6SMike Christie 	}
743184b57c6SMike Christie 
744262ef636SMike Christie 	itt = task->hdr->itt;
745577577daSMike Christie 	task->hdr_len = sizeof(struct iscsi_hdr);
7469c19a7d0SMike Christie 	memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
747262ef636SMike Christie 
748262ef636SMike Christie 	if (hdr->itt != RESERVED_ITT) {
749262ef636SMike Christie 		if (session->tt->parse_pdu_itt)
750262ef636SMike Christie 			task->hdr->itt = itt;
751262ef636SMike Christie 		else
752262ef636SMike Christie 			task->hdr->itt = build_itt(task->itt,
753262ef636SMike Christie 						   task->conn->session->age);
754262ef636SMike Christie 	}
755262ef636SMike Christie 
756fe0a8a95SLee Duncan 	if (unlikely(READ_ONCE(conn->ping_task) == INVALID_SCSI_TASK))
757fe0a8a95SLee Duncan 		WRITE_ONCE(conn->ping_task, task);
758fe0a8a95SLee Duncan 
7591336aed1SMike Christie 	if (!ihost->workq) {
760577577daSMike Christie 		if (iscsi_prep_mgmt_task(conn, task))
761577577daSMike Christie 			goto free_task;
762052d0144SMike Christie 
7639c19a7d0SMike Christie 		if (session->tt->xmit_task(task))
764577577daSMike Christie 			goto free_task;
7653bbaaad9SMike Christie 	} else {
7663bbaaad9SMike Christie 		list_add_tail(&task->running, &conn->mgmtqueue);
76732ae763eSMike Christie 		iscsi_conn_queue_work(conn);
7683bbaaad9SMike Christie 	}
769052d0144SMike Christie 
7709c19a7d0SMike Christie 	return task;
771577577daSMike Christie 
772577577daSMike Christie free_task:
773659743b0SShlomo Pongratz 	/* regular RX path uses back_lock */
7744fa50799SJitendra Bhivare 	spin_lock(&session->back_lock);
775577577daSMike Christie 	__iscsi_put_task(task);
7764fa50799SJitendra Bhivare 	spin_unlock(&session->back_lock);
777577577daSMike Christie 	return NULL;
778f6d5180cSMike Christie }
779f6d5180cSMike Christie 
780f6d5180cSMike Christie int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
781f6d5180cSMike Christie 			char *data, uint32_t data_size)
782f6d5180cSMike Christie {
783f6d5180cSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
784f6d5180cSMike Christie 	struct iscsi_session *session = conn->session;
785f6d5180cSMike Christie 	int err = 0;
786f6d5180cSMike Christie 
787659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
788f6d5180cSMike Christie 	if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
789f6d5180cSMike Christie 		err = -EPERM;
790659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
791f6d5180cSMike Christie 	return err;
792f6d5180cSMike Christie }
793f6d5180cSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
794f6d5180cSMike Christie 
7957996a778SMike Christie /**
796ae6b4e69SMauro Carvalho Chehab  * iscsi_scsi_cmd_rsp - SCSI Command Response processing
7977996a778SMike Christie  * @conn: iscsi connection
7987996a778SMike Christie  * @hdr: iscsi header
7999c19a7d0SMike Christie  * @task: scsi command task
8007996a778SMike Christie  * @data: cmd data buffer
8017996a778SMike Christie  * @datalen: len of buffer
8027996a778SMike Christie  *
8037996a778SMike Christie  * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
804a656183eSLee Duncan  * then completes the command and task. called under back_lock
8057996a778SMike Christie  **/
80677a23c21SMike Christie static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
8079c19a7d0SMike Christie 			       struct iscsi_task *task, char *data,
8087996a778SMike Christie 			       int datalen)
8097996a778SMike Christie {
81012352183SNicholas Bellinger 	struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
8117996a778SMike Christie 	struct iscsi_session *session = conn->session;
8129c19a7d0SMike Christie 	struct scsi_cmnd *sc = task->sc;
8137996a778SMike Christie 
81477a23c21SMike Christie 	iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
8157996a778SMike Christie 	conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
8167996a778SMike Christie 
8177996a778SMike Christie 	sc->result = (DID_OK << 16) | rhdr->cmd_status;
8187996a778SMike Christie 
81955e51edaSSagi Grimberg 	if (task->protected) {
82055e51edaSSagi Grimberg 		sector_t sector;
82155e51edaSSagi Grimberg 		u8 ascq;
82255e51edaSSagi Grimberg 
82355e51edaSSagi Grimberg 		/**
82455e51edaSSagi Grimberg 		 * Transports that didn't implement check_protection
82555e51edaSSagi Grimberg 		 * callback but still published T10-PI support to scsi-mid
82655e51edaSSagi Grimberg 		 * deserve this BUG_ON.
82755e51edaSSagi Grimberg 		 **/
82855e51edaSSagi Grimberg 		BUG_ON(!session->tt->check_protection);
82955e51edaSSagi Grimberg 
83055e51edaSSagi Grimberg 		ascq = session->tt->check_protection(task, &sector);
83155e51edaSSagi Grimberg 		if (ascq) {
832f2b1e9c6SHannes Reinecke 			scsi_build_sense(sc, 1, ILLEGAL_REQUEST, 0x10, ascq);
833a73c2a2fSSagi Grimberg 			scsi_set_sense_information(sc->sense_buffer,
834a73c2a2fSSagi Grimberg 						   SCSI_SENSE_BUFFERSIZE,
835a73c2a2fSSagi Grimberg 						   sector);
83655e51edaSSagi Grimberg 			goto out;
83755e51edaSSagi Grimberg 		}
83855e51edaSSagi Grimberg 	}
83955e51edaSSagi Grimberg 
8407996a778SMike Christie 	if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
8417996a778SMike Christie 		sc->result = DID_ERROR << 16;
8427996a778SMike Christie 		goto out;
8437996a778SMike Christie 	}
8447996a778SMike Christie 
8457996a778SMike Christie 	if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
8469b80cb4bSMike Christie 		uint16_t senselen;
8477996a778SMike Christie 
8487996a778SMike Christie 		if (datalen < 2) {
8497996a778SMike Christie invalid_datalen:
850322d739dSMike Christie 			iscsi_conn_printk(KERN_ERR,  conn,
851322d739dSMike Christie 					 "Got CHECK_CONDITION but invalid data "
852322d739dSMike Christie 					 "buffer size of %d\n", datalen);
8537996a778SMike Christie 			sc->result = DID_BAD_TARGET << 16;
8547996a778SMike Christie 			goto out;
8557996a778SMike Christie 		}
8567996a778SMike Christie 
8578f333991SHarvey Harrison 		senselen = get_unaligned_be16(data);
8587996a778SMike Christie 		if (datalen < senselen)
8597996a778SMike Christie 			goto invalid_datalen;
8607996a778SMike Christie 
8617996a778SMike Christie 		memcpy(sc->sense_buffer, data + 2,
8629b80cb4bSMike Christie 		       min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
8631b2c7af8SMike Christie 		ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
8641b2c7af8SMike Christie 				  min_t(uint16_t, senselen,
8651b2c7af8SMike Christie 				  SCSI_SENSE_BUFFERSIZE));
8667996a778SMike Christie 	}
8677996a778SMike Christie 
868c07d4444SBoaz Harrosh 	if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
869c07d4444SBoaz Harrosh 			   ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
870c07d4444SBoaz Harrosh 		sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
871c07d4444SBoaz Harrosh 	}
872c07d4444SBoaz Harrosh 
8737207fea4SBoaz Harrosh 	if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
8747207fea4SBoaz Harrosh 	                   ISCSI_FLAG_CMD_OVERFLOW)) {
8757996a778SMike Christie 		int res_count = be32_to_cpu(rhdr->residual_count);
8767996a778SMike Christie 
8777207fea4SBoaz Harrosh 		if (res_count > 0 &&
8787207fea4SBoaz Harrosh 		    (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
8797207fea4SBoaz Harrosh 		     res_count <= scsi_bufflen(sc)))
880c07d4444SBoaz Harrosh 			/* write side for bidi or uni-io set_resid */
8811c138991SFUJITA Tomonori 			scsi_set_resid(sc, res_count);
8827996a778SMike Christie 		else
8837996a778SMike Christie 			sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
884c07d4444SBoaz Harrosh 	}
8857996a778SMike Christie out:
8863bbaaad9SMike Christie 	ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
8871b2c7af8SMike Christie 			  sc, sc->result, task->itt);
8887996a778SMike Christie 	conn->scsirsp_pdus_cnt++;
889b3cd5050SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
8907996a778SMike Christie }
8917996a778SMike Christie 
8921d9edf02SMike Christie /**
8931d9edf02SMike Christie  * iscsi_data_in_rsp - SCSI Data-In Response processing
8941d9edf02SMike Christie  * @conn: iscsi connection
8951d9edf02SMike Christie  * @hdr:  iscsi pdu
8961d9edf02SMike Christie  * @task: scsi command task
897a656183eSLee Duncan  *
898a656183eSLee Duncan  * iscsi_data_in_rsp sets up the scsi_cmnd fields based on the data received
899a656183eSLee Duncan  * then completes the command and task. called under back_lock
9001d9edf02SMike Christie  **/
9011d9edf02SMike Christie static void
9021d9edf02SMike Christie iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
9031d9edf02SMike Christie 		  struct iscsi_task *task)
9041d9edf02SMike Christie {
9051d9edf02SMike Christie 	struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
9061d9edf02SMike Christie 	struct scsi_cmnd *sc = task->sc;
9071d9edf02SMike Christie 
9081d9edf02SMike Christie 	if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
9091d9edf02SMike Christie 		return;
9101d9edf02SMike Christie 
911edbc9aa0SMike Christie 	iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
9121d9edf02SMike Christie 	sc->result = (DID_OK << 16) | rhdr->cmd_status;
9131d9edf02SMike Christie 	conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
9141d9edf02SMike Christie 	if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
9151d9edf02SMike Christie 	                   ISCSI_FLAG_DATA_OVERFLOW)) {
9161d9edf02SMike Christie 		int res_count = be32_to_cpu(rhdr->residual_count);
9171d9edf02SMike Christie 
9181d9edf02SMike Christie 		if (res_count > 0 &&
9191d9edf02SMike Christie 		    (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
920ae3d56d8SChristoph Hellwig 		     res_count <= sc->sdb.length))
921960bf87aSBart Van Assche 			scsi_set_resid(sc, res_count);
9221d9edf02SMike Christie 		else
9231d9edf02SMike Christie 			sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
9241d9edf02SMike Christie 	}
9251d9edf02SMike Christie 
9263bbaaad9SMike Christie 	ISCSI_DBG_SESSION(conn->session, "data in with status done "
9273bbaaad9SMike Christie 			  "[sc %p res %d itt 0x%x]\n",
9283bbaaad9SMike Christie 			  sc, sc->result, task->itt);
9291d9edf02SMike Christie 	conn->scsirsp_pdus_cnt++;
930b3cd5050SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
9311d9edf02SMike Christie }
9321d9edf02SMike Christie 
9337ea8b828SMike Christie static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
9347ea8b828SMike Christie {
9357ea8b828SMike Christie 	struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
936ec29d0acSMike Christie 	struct iscsi_session *session = conn->session;
9377ea8b828SMike Christie 
9387ea8b828SMike Christie 	conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
9397ea8b828SMike Christie 	conn->tmfrsp_pdus_cnt++;
9407ea8b828SMike Christie 
941ec29d0acSMike Christie 	if (session->tmf_state != TMF_QUEUED)
9427ea8b828SMike Christie 		return;
9437ea8b828SMike Christie 
9447ea8b828SMike Christie 	if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
945ec29d0acSMike Christie 		session->tmf_state = TMF_SUCCESS;
9467ea8b828SMike Christie 	else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
947ec29d0acSMike Christie 		session->tmf_state = TMF_NOT_FOUND;
9487ea8b828SMike Christie 	else
949ec29d0acSMike Christie 		session->tmf_state = TMF_FAILED;
950ec29d0acSMike Christie 	wake_up(&session->ehwait);
9517ea8b828SMike Christie }
9527ea8b828SMike Christie 
95352f5664aSAriel Nahum static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
954f6d5180cSMike Christie {
955f6d5180cSMike Christie         struct iscsi_nopout hdr;
9569c19a7d0SMike Christie 	struct iscsi_task *task;
957f6d5180cSMike Christie 
958fe0a8a95SLee Duncan 	if (!rhdr) {
959fe0a8a95SLee Duncan 		if (READ_ONCE(conn->ping_task))
96052f5664aSAriel Nahum 			return -EINVAL;
961fe0a8a95SLee Duncan 		WRITE_ONCE(conn->ping_task, INVALID_SCSI_TASK);
962fe0a8a95SLee Duncan 	}
963f6d5180cSMike Christie 
964f6d5180cSMike Christie 	memset(&hdr, 0, sizeof(struct iscsi_nopout));
965f6d5180cSMike Christie 	hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
966f6d5180cSMike Christie 	hdr.flags = ISCSI_FLAG_CMD_FINAL;
967f6d5180cSMike Christie 
968f6d5180cSMike Christie 	if (rhdr) {
96955bdabdfSAndy Grover 		hdr.lun = rhdr->lun;
970f6d5180cSMike Christie 		hdr.ttt = rhdr->ttt;
971f6d5180cSMike Christie 		hdr.itt = RESERVED_ITT;
972f6d5180cSMike Christie 	} else
973f6d5180cSMike Christie 		hdr.ttt = RESERVED_ITT;
974f6d5180cSMike Christie 
9759c19a7d0SMike Christie 	task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
97652f5664aSAriel Nahum 	if (!task) {
977fe0a8a95SLee Duncan 		if (!rhdr)
978fe0a8a95SLee Duncan 			WRITE_ONCE(conn->ping_task, NULL);
979322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
98052f5664aSAriel Nahum 		return -EIO;
98152f5664aSAriel Nahum 	} else if (!rhdr) {
982d3acf022SMike Christie 		/* only track our nops */
983d3acf022SMike Christie 		conn->last_ping = jiffies;
984d3acf022SMike Christie 	}
98552f5664aSAriel Nahum 
98652f5664aSAriel Nahum 	return 0;
987f6d5180cSMike Christie }
988f6d5180cSMike Christie 
989a656183eSLee Duncan /**
990a656183eSLee Duncan  * iscsi_nop_out_rsp - SCSI NOP Response processing
991a656183eSLee Duncan  * @task: scsi command task
992a656183eSLee Duncan  * @nop: the nop structure
993a656183eSLee Duncan  * @data: where to put the data
994a656183eSLee Duncan  * @datalen: length of data
995a656183eSLee Duncan  *
996a656183eSLee Duncan  * iscsi_nop_out_rsp handles nop response from use or
997a656183eSLee Duncan  * from user space. called under back_lock
998a656183eSLee Duncan  **/
9998afa1439SMike Christie static int iscsi_nop_out_rsp(struct iscsi_task *task,
10008afa1439SMike Christie 			     struct iscsi_nopin *nop, char *data, int datalen)
10018afa1439SMike Christie {
10028afa1439SMike Christie 	struct iscsi_conn *conn = task->conn;
10038afa1439SMike Christie 	int rc = 0;
10048afa1439SMike Christie 
1005fe0a8a95SLee Duncan 	if (READ_ONCE(conn->ping_task) != task) {
10068afa1439SMike Christie 		/*
10078afa1439SMike Christie 		 * If this is not in response to one of our
10088afa1439SMike Christie 		 * nops then it must be from userspace.
10098afa1439SMike Christie 		 */
10108afa1439SMike Christie 		if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
10118afa1439SMike Christie 				   data, datalen))
10128afa1439SMike Christie 			rc = ISCSI_ERR_CONN_FAILED;
10138afa1439SMike Christie 	} else
10148afa1439SMike Christie 		mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
10158afa1439SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
10168afa1439SMike Christie 	return rc;
10178afa1439SMike Christie }
10188afa1439SMike Christie 
101962f38300SMike Christie static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
102062f38300SMike Christie 			       char *data, int datalen)
102162f38300SMike Christie {
102262f38300SMike Christie 	struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
102362f38300SMike Christie 	struct iscsi_hdr rejected_pdu;
10248afa1439SMike Christie 	int opcode, rc = 0;
102562f38300SMike Christie 
102662f38300SMike Christie 	conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
102762f38300SMike Christie 
10288afa1439SMike Christie 	if (ntoh24(reject->dlength) > datalen ||
10298afa1439SMike Christie 	    ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
10308afa1439SMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
10318afa1439SMike Christie 				  "pdu. Invalid data length (pdu dlength "
10328afa1439SMike Christie 				  "%u, datalen %d\n", ntoh24(reject->dlength),
10338afa1439SMike Christie 				  datalen);
103462f38300SMike Christie 		return ISCSI_ERR_PROTO;
10358afa1439SMike Christie 	}
103662f38300SMike Christie 	memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
10378afa1439SMike Christie 	opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
10388afa1439SMike Christie 
10398afa1439SMike Christie 	switch (reject->reason) {
10408afa1439SMike Christie 	case ISCSI_REASON_DATA_DIGEST_ERROR:
1041322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
10428afa1439SMike Christie 				  "pdu (op 0x%x itt 0x%x) rejected "
1043262ef636SMike Christie 				  "due to DataDigest error.\n",
1044e5dbbe27SVaughan Cao 				  opcode, rejected_pdu.itt);
10458afa1439SMike Christie 		break;
10468afa1439SMike Christie 	case ISCSI_REASON_IMM_CMD_REJECT:
10478afa1439SMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
10488afa1439SMike Christie 				  "pdu (op 0x%x itt 0x%x) rejected. Too many "
10498afa1439SMike Christie 				  "immediate commands.\n",
1050e5dbbe27SVaughan Cao 				  opcode, rejected_pdu.itt);
10518afa1439SMike Christie 		/*
10528afa1439SMike Christie 		 * We only send one TMF at a time so if the target could not
10538afa1439SMike Christie 		 * handle it, then it should get fixed (RFC mandates that
10548afa1439SMike Christie 		 * a target can handle one immediate TMF per conn).
10558afa1439SMike Christie 		 *
10568afa1439SMike Christie 		 * For nops-outs, we could have sent more than one if
10578afa1439SMike Christie 		 * the target is sending us lots of nop-ins
10588afa1439SMike Christie 		 */
10598afa1439SMike Christie 		if (opcode != ISCSI_OP_NOOP_OUT)
106062f38300SMike Christie 			return 0;
10618afa1439SMike Christie 
1062659743b0SShlomo Pongratz 		if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
10638afa1439SMike Christie 			/*
10648afa1439SMike Christie 			 * nop-out in response to target's nop-out rejected.
10658afa1439SMike Christie 			 * Just resend.
10668afa1439SMike Christie 			 */
1067659743b0SShlomo Pongratz 			/* In RX path we are under back lock */
1068659743b0SShlomo Pongratz 			spin_unlock(&conn->session->back_lock);
1069659743b0SShlomo Pongratz 			spin_lock(&conn->session->frwd_lock);
10708afa1439SMike Christie 			iscsi_send_nopout(conn,
10718afa1439SMike Christie 					  (struct iscsi_nopin*)&rejected_pdu);
1072659743b0SShlomo Pongratz 			spin_unlock(&conn->session->frwd_lock);
1073659743b0SShlomo Pongratz 			spin_lock(&conn->session->back_lock);
1074659743b0SShlomo Pongratz 		} else {
10758afa1439SMike Christie 			struct iscsi_task *task;
10768afa1439SMike Christie 			/*
10778afa1439SMike Christie 			 * Our nop as ping got dropped. We know the target
10788afa1439SMike Christie 			 * and transport are ok so just clean up
10798afa1439SMike Christie 			 */
10808afa1439SMike Christie 			task = iscsi_itt_to_task(conn, rejected_pdu.itt);
10818afa1439SMike Christie 			if (!task) {
10828afa1439SMike Christie 				iscsi_conn_printk(KERN_ERR, conn,
10838afa1439SMike Christie 						 "Invalid pdu reject. Could "
10848afa1439SMike Christie 						 "not lookup rejected task.\n");
10858afa1439SMike Christie 				rc = ISCSI_ERR_BAD_ITT;
10868afa1439SMike Christie 			} else
10878afa1439SMike Christie 				rc = iscsi_nop_out_rsp(task,
10888afa1439SMike Christie 					(struct iscsi_nopin*)&rejected_pdu,
10898afa1439SMike Christie 					NULL, 0);
10908afa1439SMike Christie 		}
10918afa1439SMike Christie 		break;
10928afa1439SMike Christie 	default:
10938afa1439SMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
10948afa1439SMike Christie 				  "pdu (op 0x%x itt 0x%x) rejected. Reason "
1095e5dbbe27SVaughan Cao 				  "code 0x%x\n", rejected_pdu.opcode,
1096e5dbbe27SVaughan Cao 				  rejected_pdu.itt, reject->reason);
10978afa1439SMike Christie 		break;
10988afa1439SMike Christie 	}
10998afa1439SMike Christie 	return rc;
110062f38300SMike Christie }
110162f38300SMike Christie 
11027996a778SMike Christie /**
1103913e5bf4SMike Christie  * iscsi_itt_to_task - look up task by itt
1104913e5bf4SMike Christie  * @conn: iscsi connection
1105913e5bf4SMike Christie  * @itt: itt
1106913e5bf4SMike Christie  *
1107913e5bf4SMike Christie  * This should be used for mgmt tasks like login and nops, or if
1108913e5bf4SMike Christie  * the LDD's itt space does not include the session age.
1109913e5bf4SMike Christie  *
1110659743b0SShlomo Pongratz  * The session back_lock must be held.
1111913e5bf4SMike Christie  */
11128f9256ceSMike Christie struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
1113913e5bf4SMike Christie {
1114913e5bf4SMike Christie 	struct iscsi_session *session = conn->session;
1115262ef636SMike Christie 	int i;
1116913e5bf4SMike Christie 
1117913e5bf4SMike Christie 	if (itt == RESERVED_ITT)
1118913e5bf4SMike Christie 		return NULL;
1119913e5bf4SMike Christie 
1120262ef636SMike Christie 	if (session->tt->parse_pdu_itt)
1121262ef636SMike Christie 		session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1122262ef636SMike Christie 	else
1123913e5bf4SMike Christie 		i = get_itt(itt);
1124913e5bf4SMike Christie 	if (i >= session->cmds_max)
1125913e5bf4SMike Christie 		return NULL;
1126913e5bf4SMike Christie 
1127913e5bf4SMike Christie 	return session->cmds[i];
1128913e5bf4SMike Christie }
11298f9256ceSMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
1130913e5bf4SMike Christie 
1131913e5bf4SMike Christie /**
11327996a778SMike Christie  * __iscsi_complete_pdu - complete pdu
11337996a778SMike Christie  * @conn: iscsi conn
11347996a778SMike Christie  * @hdr: iscsi header
11357996a778SMike Christie  * @data: data buffer
11367996a778SMike Christie  * @datalen: len of data buffer
11377996a778SMike Christie  *
11387996a778SMike Christie  * Completes pdu processing by freeing any resources allocated at
1139659743b0SShlomo Pongratz  * queuecommand or send generic. session back_lock must be held and verify
11407996a778SMike Christie  * itt must have been called.
11417996a778SMike Christie  */
1142913e5bf4SMike Christie int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
11437996a778SMike Christie 			 char *data, int datalen)
11447996a778SMike Christie {
11457996a778SMike Christie 	struct iscsi_session *session = conn->session;
11467996a778SMike Christie 	int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
11479c19a7d0SMike Christie 	struct iscsi_task *task;
11487996a778SMike Christie 	uint32_t itt;
11497996a778SMike Christie 
1150f6d5180cSMike Christie 	conn->last_recv = jiffies;
11510af967f5SMike Christie 	rc = iscsi_verify_itt(conn, hdr->itt);
11520af967f5SMike Christie 	if (rc)
11530af967f5SMike Christie 		return rc;
11540af967f5SMike Christie 
1155b4377356SAl Viro 	if (hdr->itt != RESERVED_ITT)
1156b4377356SAl Viro 		itt = get_itt(hdr->itt);
11577996a778SMike Christie 	else
1158b4377356SAl Viro 		itt = ~0U;
11597996a778SMike Christie 
11601b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
11613e5c28adSMike Christie 			  opcode, conn->id, itt, datalen);
11627996a778SMike Christie 
11633e5c28adSMike Christie 	if (itt == ~0U) {
116477a23c21SMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
116562f38300SMike Christie 
11667996a778SMike Christie 		switch(opcode) {
11677996a778SMike Christie 		case ISCSI_OP_NOOP_IN:
116840527afeSMike Christie 			if (datalen) {
116940527afeSMike Christie 				rc = ISCSI_ERR_PROTO;
117040527afeSMike Christie 				break;
117140527afeSMike Christie 			}
117240527afeSMike Christie 
1173b4377356SAl Viro 			if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
117440527afeSMike Christie 				break;
117540527afeSMike Christie 
1176659743b0SShlomo Pongratz 			/* In RX path we are under back lock */
1177659743b0SShlomo Pongratz 			spin_unlock(&session->back_lock);
1178659743b0SShlomo Pongratz 			spin_lock(&session->frwd_lock);
1179f6d5180cSMike Christie 			iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
1180659743b0SShlomo Pongratz 			spin_unlock(&session->frwd_lock);
1181659743b0SShlomo Pongratz 			spin_lock(&session->back_lock);
11827996a778SMike Christie 			break;
11837996a778SMike Christie 		case ISCSI_OP_REJECT:
118462f38300SMike Christie 			rc = iscsi_handle_reject(conn, hdr, data, datalen);
118562f38300SMike Christie 			break;
11867996a778SMike Christie 		case ISCSI_OP_ASYNC_EVENT:
11878d2860b3SMike Christie 			conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
11885831c737SMike Christie 			if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
11895831c737SMike Christie 				rc = ISCSI_ERR_CONN_FAILED;
11907996a778SMike Christie 			break;
11917996a778SMike Christie 		default:
11927996a778SMike Christie 			rc = ISCSI_ERR_BAD_OPCODE;
11937996a778SMike Christie 			break;
11947996a778SMike Christie 		}
11953e5c28adSMike Christie 		goto out;
11963e5c28adSMike Christie 	}
11977996a778SMike Christie 
11983e5c28adSMike Christie 	switch(opcode) {
11993e5c28adSMike Christie 	case ISCSI_OP_SCSI_CMD_RSP:
1200913e5bf4SMike Christie 	case ISCSI_OP_SCSI_DATA_IN:
1201913e5bf4SMike Christie 		task = iscsi_itt_to_ctask(conn, hdr->itt);
1202913e5bf4SMike Christie 		if (!task)
1203913e5bf4SMike Christie 			return ISCSI_ERR_BAD_ITT;
1204d355e57dSMike Christie 		task->last_xfer = jiffies;
12053e5c28adSMike Christie 		break;
1206913e5bf4SMike Christie 	case ISCSI_OP_R2T:
1207913e5bf4SMike Christie 		/*
1208913e5bf4SMike Christie 		 * LLD handles R2Ts if they need to.
1209913e5bf4SMike Christie 		 */
1210913e5bf4SMike Christie 		return 0;
1211913e5bf4SMike Christie 	case ISCSI_OP_LOGOUT_RSP:
1212913e5bf4SMike Christie 	case ISCSI_OP_LOGIN_RSP:
1213913e5bf4SMike Christie 	case ISCSI_OP_TEXT_RSP:
1214913e5bf4SMike Christie 	case ISCSI_OP_SCSI_TMFUNC_RSP:
1215913e5bf4SMike Christie 	case ISCSI_OP_NOOP_IN:
1216913e5bf4SMike Christie 		task = iscsi_itt_to_task(conn, hdr->itt);
1217913e5bf4SMike Christie 		if (!task)
1218913e5bf4SMike Christie 			return ISCSI_ERR_BAD_ITT;
1219913e5bf4SMike Christie 		break;
1220913e5bf4SMike Christie 	default:
1221913e5bf4SMike Christie 		return ISCSI_ERR_BAD_OPCODE;
12223e5c28adSMike Christie 	}
1223913e5bf4SMike Christie 
1224913e5bf4SMike Christie 	switch(opcode) {
1225913e5bf4SMike Christie 	case ISCSI_OP_SCSI_CMD_RSP:
12269c19a7d0SMike Christie 		iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
12273e5c28adSMike Christie 		break;
12283e5c28adSMike Christie 	case ISCSI_OP_SCSI_DATA_IN:
12291d9edf02SMike Christie 		iscsi_data_in_rsp(conn, hdr, task);
12303e5c28adSMike Christie 		break;
12313e5c28adSMike Christie 	case ISCSI_OP_LOGOUT_RSP:
12323e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12333e5c28adSMike Christie 		if (datalen) {
12343e5c28adSMike Christie 			rc = ISCSI_ERR_PROTO;
12353e5c28adSMike Christie 			break;
12363e5c28adSMike Christie 		}
12373e5c28adSMike Christie 		conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
12383e5c28adSMike Christie 		goto recv_pdu;
12393e5c28adSMike Christie 	case ISCSI_OP_LOGIN_RSP:
12403e5c28adSMike Christie 	case ISCSI_OP_TEXT_RSP:
12413e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12423e5c28adSMike Christie 		/*
12433e5c28adSMike Christie 		 * login related PDU's exp_statsn is handled in
12443e5c28adSMike Christie 		 * userspace
12453e5c28adSMike Christie 		 */
12463e5c28adSMike Christie 		goto recv_pdu;
12473e5c28adSMike Christie 	case ISCSI_OP_SCSI_TMFUNC_RSP:
12483e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12493e5c28adSMike Christie 		if (datalen) {
12503e5c28adSMike Christie 			rc = ISCSI_ERR_PROTO;
12513e5c28adSMike Christie 			break;
12523e5c28adSMike Christie 		}
12533e5c28adSMike Christie 
12543e5c28adSMike Christie 		iscsi_tmf_rsp(conn, hdr);
1255b3cd5050SMike Christie 		iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
12563e5c28adSMike Christie 		break;
12573e5c28adSMike Christie 	case ISCSI_OP_NOOP_IN:
12583e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12593e5c28adSMike Christie 		if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
12603e5c28adSMike Christie 			rc = ISCSI_ERR_PROTO;
12613e5c28adSMike Christie 			break;
12623e5c28adSMike Christie 		}
12633e5c28adSMike Christie 		conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
12643e5c28adSMike Christie 
12658afa1439SMike Christie 		rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
12668afa1439SMike Christie 				       data, datalen);
12673e5c28adSMike Christie 		break;
12683e5c28adSMike Christie 	default:
12693e5c28adSMike Christie 		rc = ISCSI_ERR_BAD_OPCODE;
12703e5c28adSMike Christie 		break;
12713e5c28adSMike Christie 	}
12723e5c28adSMike Christie 
12733e5c28adSMike Christie out:
12743e5c28adSMike Christie 	return rc;
12753e5c28adSMike Christie recv_pdu:
12763e5c28adSMike Christie 	if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
12773e5c28adSMike Christie 		rc = ISCSI_ERR_CONN_FAILED;
1278b3cd5050SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
12797996a778SMike Christie 	return rc;
12807996a778SMike Christie }
1281913e5bf4SMike Christie EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
12827996a778SMike Christie 
12837996a778SMike Christie int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
12847996a778SMike Christie 		       char *data, int datalen)
12857996a778SMike Christie {
12867996a778SMike Christie 	int rc;
12877996a778SMike Christie 
1288659743b0SShlomo Pongratz 	spin_lock(&conn->session->back_lock);
12897996a778SMike Christie 	rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1290659743b0SShlomo Pongratz 	spin_unlock(&conn->session->back_lock);
12917996a778SMike Christie 	return rc;
12927996a778SMike Christie }
12937996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
12947996a778SMike Christie 
12950af967f5SMike Christie int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
12967996a778SMike Christie {
12977996a778SMike Christie 	struct iscsi_session *session = conn->session;
1298262ef636SMike Christie 	int age = 0, i = 0;
12997996a778SMike Christie 
13000af967f5SMike Christie 	if (itt == RESERVED_ITT)
13010af967f5SMike Christie 		return 0;
13020af967f5SMike Christie 
1303262ef636SMike Christie 	if (session->tt->parse_pdu_itt)
1304262ef636SMike Christie 		session->tt->parse_pdu_itt(conn, itt, &i, &age);
1305262ef636SMike Christie 	else {
1306262ef636SMike Christie 		i = get_itt(itt);
1307262ef636SMike Christie 		age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1308262ef636SMike Christie 	}
1309262ef636SMike Christie 
1310262ef636SMike Christie 	if (age != session->age) {
1311322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
13120af967f5SMike Christie 				  "received itt %x expected session age (%x)\n",
1313913e5bf4SMike Christie 				  (__force u32)itt, session->age);
13147996a778SMike Christie 		return ISCSI_ERR_BAD_ITT;
13157996a778SMike Christie 	}
13167996a778SMike Christie 
13173e5c28adSMike Christie 	if (i >= session->cmds_max) {
13183e5c28adSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
13193e5c28adSMike Christie 				  "received invalid itt index %u (max cmds "
13203e5c28adSMike Christie 				   "%u.\n", i, session->cmds_max);
13213e5c28adSMike Christie 		return ISCSI_ERR_BAD_ITT;
13227996a778SMike Christie 	}
13237996a778SMike Christie 	return 0;
13247996a778SMike Christie }
13257996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_verify_itt);
13267996a778SMike Christie 
1327913e5bf4SMike Christie /**
1328913e5bf4SMike Christie  * iscsi_itt_to_ctask - look up ctask by itt
1329913e5bf4SMike Christie  * @conn: iscsi connection
1330913e5bf4SMike Christie  * @itt: itt
1331913e5bf4SMike Christie  *
1332913e5bf4SMike Christie  * This should be used for cmd tasks.
1333913e5bf4SMike Christie  *
1334659743b0SShlomo Pongratz  * The session back_lock must be held.
1335913e5bf4SMike Christie  */
1336913e5bf4SMike Christie struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
13370af967f5SMike Christie {
13389c19a7d0SMike Christie 	struct iscsi_task *task;
13390af967f5SMike Christie 
13400af967f5SMike Christie 	if (iscsi_verify_itt(conn, itt))
13410af967f5SMike Christie 		return NULL;
13420af967f5SMike Christie 
1343913e5bf4SMike Christie 	task = iscsi_itt_to_task(conn, itt);
1344913e5bf4SMike Christie 	if (!task || !task->sc)
13450af967f5SMike Christie 		return NULL;
13460af967f5SMike Christie 
1347*db22de3eSBart Van Assche 	if (iscsi_cmd(task->sc)->age != conn->session->age) {
1348913e5bf4SMike Christie 		iscsi_session_printk(KERN_ERR, conn->session,
1349913e5bf4SMike Christie 				  "task's session age %d, expected %d\n",
1350*db22de3eSBart Van Assche 				  iscsi_cmd(task->sc)->age, conn->session->age);
13510af967f5SMike Christie 		return NULL;
1352913e5bf4SMike Christie 	}
13530af967f5SMike Christie 
13549c19a7d0SMike Christie 	return task;
13550af967f5SMike Christie }
13560af967f5SMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
13570af967f5SMike Christie 
135840a06e75SMike Christie void iscsi_session_failure(struct iscsi_session *session,
1359e5bd7b54SMike Christie 			   enum iscsi_err err)
1360e5bd7b54SMike Christie {
1361e5bd7b54SMike Christie 	struct iscsi_conn *conn;
1362e5bd7b54SMike Christie 
1363659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
1364e5bd7b54SMike Christie 	conn = session->leadconn;
1365e5bd7b54SMike Christie 	if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1366659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
1367e5bd7b54SMike Christie 		return;
1368e5bd7b54SMike Christie 	}
1369e5bd7b54SMike Christie 
1370b1d19e8cSMike Christie 	iscsi_get_conn(conn->cls_conn);
1371659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
1372e5bd7b54SMike Christie 	/*
1373e5bd7b54SMike Christie 	 * if the host is being removed bypass the connection
1374e5bd7b54SMike Christie 	 * recovery initialization because we are going to kill
1375e5bd7b54SMike Christie 	 * the session.
1376e5bd7b54SMike Christie 	 */
1377e5bd7b54SMike Christie 	if (err == ISCSI_ERR_INVALID_HOST)
1378e5bd7b54SMike Christie 		iscsi_conn_error_event(conn->cls_conn, err);
1379e5bd7b54SMike Christie 	else
1380e5bd7b54SMike Christie 		iscsi_conn_failure(conn, err);
1381b1d19e8cSMike Christie 	iscsi_put_conn(conn->cls_conn);
1382e5bd7b54SMike Christie }
1383e5bd7b54SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_failure);
1384e5bd7b54SMike Christie 
1385891e2639SMike Christie static bool iscsi_set_conn_failed(struct iscsi_conn *conn)
13867996a778SMike Christie {
13877996a778SMike Christie 	struct iscsi_session *session = conn->session;
13887996a778SMike Christie 
1389891e2639SMike Christie 	if (session->state == ISCSI_STATE_FAILED)
1390891e2639SMike Christie 		return false;
1391656cffc9SMike Christie 
139267a61114SMike Christie 	if (conn->stop_stage == 0)
13937996a778SMike Christie 		session->state = ISCSI_STATE_FAILED;
1394e5bd7b54SMike Christie 
13957996a778SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
13967996a778SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1397891e2639SMike Christie 	return true;
1398891e2639SMike Christie }
1399891e2639SMike Christie 
1400891e2639SMike Christie void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1401891e2639SMike Christie {
1402891e2639SMike Christie 	struct iscsi_session *session = conn->session;
1403891e2639SMike Christie 	bool needs_evt;
1404891e2639SMike Christie 
1405891e2639SMike Christie 	spin_lock_bh(&session->frwd_lock);
1406891e2639SMike Christie 	needs_evt = iscsi_set_conn_failed(conn);
1407891e2639SMike Christie 	spin_unlock_bh(&session->frwd_lock);
1408891e2639SMike Christie 
1409891e2639SMike Christie 	if (needs_evt)
1410e5bd7b54SMike Christie 		iscsi_conn_error_event(conn->cls_conn, err);
14117996a778SMike Christie }
14127996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_failure);
14137996a778SMike Christie 
141477a23c21SMike Christie static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
141577a23c21SMike Christie {
141677a23c21SMike Christie 	struct iscsi_session *session = conn->session;
141777a23c21SMike Christie 
141877a23c21SMike Christie 	/*
141977a23c21SMike Christie 	 * Check for iSCSI window and take care of CmdSN wrap-around
142077a23c21SMike Christie 	 */
1421e0726407SMike Christie 	if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
14221b2c7af8SMike Christie 		ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
14231b2c7af8SMike Christie 				  "%u MaxCmdSN %u CmdSN %u/%u\n",
14241b2c7af8SMike Christie 				  session->exp_cmdsn, session->max_cmdsn,
14251b2c7af8SMike Christie 				  session->cmdsn, session->queued_cmdsn);
142677a23c21SMike Christie 		return -ENOSPC;
142777a23c21SMike Christie 	}
142877a23c21SMike Christie 	return 0;
142977a23c21SMike Christie }
143077a23c21SMike Christie 
14315923d64bSMike Christie static int iscsi_xmit_task(struct iscsi_conn *conn, struct iscsi_task *task,
14325923d64bSMike Christie 			   bool was_requeue)
143377a23c21SMike Christie {
1434843c0a8aSMike Christie 	int rc;
143577a23c21SMike Christie 
143679edd00dSAnoob Soman 	spin_lock_bh(&conn->session->back_lock);
14375923d64bSMike Christie 
14385923d64bSMike Christie 	if (!conn->task) {
14395923d64bSMike Christie 		/* Take a ref so we can access it after xmit_task() */
14405923d64bSMike Christie 		__iscsi_get_task(task);
14415923d64bSMike Christie 	} else {
14425923d64bSMike Christie 		/* Already have a ref from when we failed to send it last call */
14435923d64bSMike Christie 		conn->task = NULL;
14445923d64bSMike Christie 	}
14455923d64bSMike Christie 
14465923d64bSMike Christie 	/*
14475923d64bSMike Christie 	 * If this was a requeue for a R2T we have an extra ref on the task in
14485923d64bSMike Christie 	 * case a bad target sends a cmd rsp before we have handled the task.
14495923d64bSMike Christie 	 */
14505923d64bSMike Christie 	if (was_requeue)
14515923d64bSMike Christie 		__iscsi_put_task(task);
14525923d64bSMike Christie 
14535923d64bSMike Christie 	/*
14545923d64bSMike Christie 	 * Do this after dropping the extra ref because if this was a requeue
14555923d64bSMike Christie 	 * it's removed from that list and cleanup_queued_task would miss it.
14565923d64bSMike Christie 	 */
14575923d64bSMike Christie 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
14585923d64bSMike Christie 		/*
14595923d64bSMike Christie 		 * Save the task and ref in case we weren't cleaning up this
14605923d64bSMike Christie 		 * task and get woken up again.
14615923d64bSMike Christie 		 */
14625923d64bSMike Christie 		conn->task = task;
146379edd00dSAnoob Soman 		spin_unlock_bh(&conn->session->back_lock);
146479edd00dSAnoob Soman 		return -ENODATA;
146579edd00dSAnoob Soman 	}
146679edd00dSAnoob Soman 	spin_unlock_bh(&conn->session->back_lock);
14675923d64bSMike Christie 
1468659743b0SShlomo Pongratz 	spin_unlock_bh(&conn->session->frwd_lock);
14699c19a7d0SMike Christie 	rc = conn->session->tt->xmit_task(task);
1470659743b0SShlomo Pongratz 	spin_lock_bh(&conn->session->frwd_lock);
1471d355e57dSMike Christie 	if (!rc) {
14729c19a7d0SMike Christie 		/* done with this task */
1473d355e57dSMike Christie 		task->last_xfer = jiffies;
1474d355e57dSMike Christie 	}
1475659743b0SShlomo Pongratz 	/* regular RX path uses back_lock */
147672b97402SShlomo Pongratz 	spin_lock(&conn->session->back_lock);
14775923d64bSMike Christie 	if (rc && task->state == ISCSI_TASK_RUNNING) {
14785923d64bSMike Christie 		/*
14795923d64bSMike Christie 		 * get an extra ref that is released next time we access it
14805923d64bSMike Christie 		 * as conn->task above.
14815923d64bSMike Christie 		 */
14825923d64bSMike Christie 		__iscsi_get_task(task);
14835923d64bSMike Christie 		conn->task = task;
14845923d64bSMike Christie 	}
14855923d64bSMike Christie 
1486d355e57dSMike Christie 	__iscsi_put_task(task);
148772b97402SShlomo Pongratz 	spin_unlock(&conn->session->back_lock);
148877a23c21SMike Christie 	return rc;
148977a23c21SMike Christie }
149077a23c21SMike Christie 
14917996a778SMike Christie /**
14929c19a7d0SMike Christie  * iscsi_requeue_task - requeue task to run from session workqueue
14939c19a7d0SMike Christie  * @task: task to requeue
1494843c0a8aSMike Christie  *
14955923d64bSMike Christie  * Callers must have taken a ref to the task that is going to be requeued.
1496843c0a8aSMike Christie  */
14979c19a7d0SMike Christie void iscsi_requeue_task(struct iscsi_task *task)
1498843c0a8aSMike Christie {
14999c19a7d0SMike Christie 	struct iscsi_conn *conn = task->conn;
1500843c0a8aSMike Christie 
15013bbaaad9SMike Christie 	/*
15023bbaaad9SMike Christie 	 * this may be on the requeue list already if the xmit_task callout
15033bbaaad9SMike Christie 	 * is handling the r2ts while we are adding new ones
15043bbaaad9SMike Christie 	 */
15055923d64bSMike Christie 	spin_lock_bh(&conn->session->frwd_lock);
15065923d64bSMike Christie 	if (list_empty(&task->running)) {
15073bbaaad9SMike Christie 		list_add_tail(&task->running, &conn->requeue);
15085923d64bSMike Christie 	} else {
15095923d64bSMike Christie 		/*
15105923d64bSMike Christie 		 * Don't need the extra ref since it's already requeued and
15115923d64bSMike Christie 		 * has a ref.
15125923d64bSMike Christie 		 */
15135923d64bSMike Christie 		iscsi_put_task(task);
15145923d64bSMike Christie 	}
151532ae763eSMike Christie 	iscsi_conn_queue_work(conn);
15165923d64bSMike Christie 	spin_unlock_bh(&conn->session->frwd_lock);
1517843c0a8aSMike Christie }
15189c19a7d0SMike Christie EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1519843c0a8aSMike Christie 
1520843c0a8aSMike Christie /**
15217996a778SMike Christie  * iscsi_data_xmit - xmit any command into the scheduled connection
15227996a778SMike Christie  * @conn: iscsi connection
15237996a778SMike Christie  *
15247996a778SMike Christie  * Notes:
15257996a778SMike Christie  *	The function can return -EAGAIN in which case the caller must
15267996a778SMike Christie  *	re-schedule it again later or recover. '0' return code means
15277996a778SMike Christie  *	successful xmit.
15287996a778SMike Christie  **/
15297996a778SMike Christie static int iscsi_data_xmit(struct iscsi_conn *conn)
15307996a778SMike Christie {
15315d12c05eSMike Christie 	struct iscsi_task *task;
15323219e529SMike Christie 	int rc = 0;
15337996a778SMike Christie 
1534659743b0SShlomo Pongratz 	spin_lock_bh(&conn->session->frwd_lock);
153570b31c15SMike Christie 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
15361b2c7af8SMike Christie 		ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1537659743b0SShlomo Pongratz 		spin_unlock_bh(&conn->session->frwd_lock);
15383219e529SMike Christie 		return -ENODATA;
15397996a778SMike Christie 	}
15407996a778SMike Christie 
15419c19a7d0SMike Christie 	if (conn->task) {
15425923d64bSMike Christie 		rc = iscsi_xmit_task(conn, conn->task, false);
15433219e529SMike Christie 	        if (rc)
154470b31c15SMike Christie 		        goto done;
15457996a778SMike Christie 	}
154677a23c21SMike Christie 
154777a23c21SMike Christie 	/*
154877a23c21SMike Christie 	 * process mgmt pdus like nops before commands since we should
154977a23c21SMike Christie 	 * only have one nop-out as a ping from us and targets should not
155077a23c21SMike Christie 	 * overflow us with nop-ins
155177a23c21SMike Christie 	 */
155277a23c21SMike Christie check_mgmt:
1553843c0a8aSMike Christie 	while (!list_empty(&conn->mgmtqueue)) {
15545923d64bSMike Christie 		task = list_entry(conn->mgmtqueue.next, struct iscsi_task,
15555923d64bSMike Christie 				  running);
15565923d64bSMike Christie 		list_del_init(&task->running);
15575923d64bSMike Christie 		if (iscsi_prep_mgmt_task(conn, task)) {
1558659743b0SShlomo Pongratz 			/* regular RX path uses back_lock */
1559659743b0SShlomo Pongratz 			spin_lock_bh(&conn->session->back_lock);
15605923d64bSMike Christie 			__iscsi_put_task(task);
1561659743b0SShlomo Pongratz 			spin_unlock_bh(&conn->session->back_lock);
1562b3a7ea8dSMike Christie 			continue;
1563b3a7ea8dSMike Christie 		}
15645923d64bSMike Christie 		rc = iscsi_xmit_task(conn, task, false);
15653219e529SMike Christie 		if (rc)
156670b31c15SMike Christie 			goto done;
15677996a778SMike Christie 	}
15687996a778SMike Christie 
1569843c0a8aSMike Christie 	/* process pending command queue */
15703bbaaad9SMike Christie 	while (!list_empty(&conn->cmdqueue)) {
15715923d64bSMike Christie 		task = list_entry(conn->cmdqueue.next, struct iscsi_task,
15725d12c05eSMike Christie 				  running);
15735923d64bSMike Christie 		list_del_init(&task->running);
1574b3a7ea8dSMike Christie 		if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
15755923d64bSMike Christie 			fail_scsi_task(task, DID_IMM_RETRY);
1576b3a7ea8dSMike Christie 			continue;
1577b3a7ea8dSMike Christie 		}
15785923d64bSMike Christie 		rc = iscsi_prep_scsi_cmd_pdu(task);
1579577577daSMike Christie 		if (rc) {
1580d28d48c6SMike Christie 			if (rc == -ENOMEM || rc == -EACCES)
15815923d64bSMike Christie 				fail_scsi_task(task, DID_IMM_RETRY);
1582d28d48c6SMike Christie 			else
15835923d64bSMike Christie 				fail_scsi_task(task, DID_ABORT);
1584004d6530SBoaz Harrosh 			continue;
1585004d6530SBoaz Harrosh 		}
15865923d64bSMike Christie 		rc = iscsi_xmit_task(conn, task, false);
15873219e529SMike Christie 		if (rc)
158870b31c15SMike Christie 			goto done;
158977a23c21SMike Christie 		/*
15909c19a7d0SMike Christie 		 * we could continuously get new task requests so
159177a23c21SMike Christie 		 * we need to check the mgmt queue for nops that need to
159277a23c21SMike Christie 		 * be sent to aviod starvation
159377a23c21SMike Christie 		 */
1594843c0a8aSMike Christie 		if (!list_empty(&conn->mgmtqueue))
1595843c0a8aSMike Christie 			goto check_mgmt;
1596843c0a8aSMike Christie 	}
1597843c0a8aSMike Christie 
1598843c0a8aSMike Christie 	while (!list_empty(&conn->requeue)) {
1599b3a7ea8dSMike Christie 		/*
1600b3a7ea8dSMike Christie 		 * we always do fastlogout - conn stop code will clean up.
1601b3a7ea8dSMike Christie 		 */
1602b3a7ea8dSMike Christie 		if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1603b3a7ea8dSMike Christie 			break;
1604b3a7ea8dSMike Christie 
16055d12c05eSMike Christie 		task = list_entry(conn->requeue.next, struct iscsi_task,
16065d12c05eSMike Christie 				  running);
16075923d64bSMike Christie 
16085d12c05eSMike Christie 		if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
16095d12c05eSMike Christie 			break;
16105d12c05eSMike Christie 
16115923d64bSMike Christie 		list_del_init(&task->running);
16125923d64bSMike Christie 		rc = iscsi_xmit_task(conn, task, true);
1613843c0a8aSMike Christie 		if (rc)
161470b31c15SMike Christie 			goto done;
1615843c0a8aSMike Christie 		if (!list_empty(&conn->mgmtqueue))
161677a23c21SMike Christie 			goto check_mgmt;
16177996a778SMike Christie 	}
1618659743b0SShlomo Pongratz 	spin_unlock_bh(&conn->session->frwd_lock);
16193219e529SMike Christie 	return -ENODATA;
16207996a778SMike Christie 
162170b31c15SMike Christie done:
1622659743b0SShlomo Pongratz 	spin_unlock_bh(&conn->session->frwd_lock);
16233219e529SMike Christie 	return rc;
16247996a778SMike Christie }
16257996a778SMike Christie 
1626c4028958SDavid Howells static void iscsi_xmitworker(struct work_struct *work)
16277996a778SMike Christie {
1628c4028958SDavid Howells 	struct iscsi_conn *conn =
1629c4028958SDavid Howells 		container_of(work, struct iscsi_conn, xmitwork);
16303219e529SMike Christie 	int rc;
16317996a778SMike Christie 	/*
16327996a778SMike Christie 	 * serialize Xmit worker on a per-connection basis.
16337996a778SMike Christie 	 */
16343219e529SMike Christie 	do {
16353219e529SMike Christie 		rc = iscsi_data_xmit(conn);
16363219e529SMike Christie 	} while (rc >= 0 || rc == -EAGAIN);
16377996a778SMike Christie }
16387996a778SMike Christie 
1639577577daSMike Christie static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1640577577daSMike Christie 						  struct scsi_cmnd *sc)
1641577577daSMike Christie {
1642577577daSMike Christie 	struct iscsi_task *task;
1643577577daSMike Christie 
16447acd72ebSStefani Seibold 	if (!kfifo_out(&conn->session->cmdpool.queue,
1645577577daSMike Christie 			 (void *) &task, sizeof(void *)))
1646577577daSMike Christie 		return NULL;
1647577577daSMike Christie 
1648*db22de3eSBart Van Assche 	iscsi_cmd(sc)->age = conn->session->age;
1649*db22de3eSBart Van Assche 	iscsi_cmd(sc)->task = task;
1650577577daSMike Christie 
16516dc618cdSElena Reshetova 	refcount_set(&task->refcount, 1);
1652577577daSMike Christie 	task->state = ISCSI_TASK_PENDING;
1653577577daSMike Christie 	task->conn = conn;
1654577577daSMike Christie 	task->sc = sc;
1655d355e57dSMike Christie 	task->have_checked_conn = false;
1656d355e57dSMike Christie 	task->last_timeout = jiffies;
1657d355e57dSMike Christie 	task->last_xfer = jiffies;
165855e51edaSSagi Grimberg 	task->protected = false;
1659577577daSMike Christie 	INIT_LIST_HEAD(&task->running);
1660577577daSMike Christie 	return task;
1661577577daSMike Christie }
1662577577daSMike Christie 
16637996a778SMike Christie enum {
16647996a778SMike Christie 	FAILURE_BAD_HOST = 1,
16657996a778SMike Christie 	FAILURE_SESSION_FAILED,
16667996a778SMike Christie 	FAILURE_SESSION_FREED,
16677996a778SMike Christie 	FAILURE_WINDOW_CLOSED,
166860ecebf5SMike Christie 	FAILURE_OOM,
16697996a778SMike Christie 	FAILURE_SESSION_TERMINATE,
1670656cffc9SMike Christie 	FAILURE_SESSION_IN_RECOVERY,
16717996a778SMike Christie 	FAILURE_SESSION_RECOVERY_TIMEOUT,
1672b3a7ea8dSMike Christie 	FAILURE_SESSION_LOGGING_OUT,
16736eabafbeSMike Christie 	FAILURE_SESSION_NOT_READY,
16747996a778SMike Christie };
16757996a778SMike Christie 
1676f41d4721SMike Christie int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
16777996a778SMike Christie {
167875613521SMike Christie 	struct iscsi_cls_session *cls_session;
16791336aed1SMike Christie 	struct iscsi_host *ihost;
16807996a778SMike Christie 	int reason = 0;
16817996a778SMike Christie 	struct iscsi_session *session;
16827996a778SMike Christie 	struct iscsi_conn *conn;
16839c19a7d0SMike Christie 	struct iscsi_task *task = NULL;
16847996a778SMike Christie 
16857996a778SMike Christie 	sc->result = 0;
1686*db22de3eSBart Van Assche 	iscsi_cmd(sc)->task = NULL;
16877996a778SMike Christie 
16881336aed1SMike Christie 	ihost = shost_priv(host);
16897996a778SMike Christie 
169075613521SMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
169175613521SMike Christie 	session = cls_session->dd_data;
1692659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
16937996a778SMike Christie 
169475613521SMike Christie 	reason = iscsi_session_chkready(cls_session);
16956eabafbeSMike Christie 	if (reason) {
16966eabafbeSMike Christie 		sc->result = reason;
16976eabafbeSMike Christie 		goto fault;
16986eabafbeSMike Christie 	}
16996eabafbeSMike Christie 
1700301e0f7eSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN) {
1701656cffc9SMike Christie 		/*
1702656cffc9SMike Christie 		 * to handle the race between when we set the recovery state
1703656cffc9SMike Christie 		 * and block the session we requeue here (commands could
1704656cffc9SMike Christie 		 * be entering our queuecommand while a block is starting
1705656cffc9SMike Christie 		 * up because the block code is not locked)
1706656cffc9SMike Christie 		 */
17079000bcd6SMike Christie 		switch (session->state) {
1708301e0f7eSMike Christie 		case ISCSI_STATE_FAILED:
1709d7549412SRafael David Tinoco 			/*
1710d7549412SRafael David Tinoco 			 * cmds should fail during shutdown, if the session
1711d7549412SRafael David Tinoco 			 * state is bad, allowing completion to happen
1712d7549412SRafael David Tinoco 			 */
1713d7549412SRafael David Tinoco 			if (unlikely(system_state != SYSTEM_RUNNING)) {
1714d7549412SRafael David Tinoco 				reason = FAILURE_SESSION_FAILED;
1715d7549412SRafael David Tinoco 				sc->result = DID_NO_CONNECT << 16;
1716d7549412SRafael David Tinoco 				break;
1717d7549412SRafael David Tinoco 			}
1718df561f66SGustavo A. R. Silva 			fallthrough;
17199000bcd6SMike Christie 		case ISCSI_STATE_IN_RECOVERY:
1720656cffc9SMike Christie 			reason = FAILURE_SESSION_IN_RECOVERY;
1721301e0f7eSMike Christie 			sc->result = DID_IMM_RETRY << 16;
1722301e0f7eSMike Christie 			break;
17239000bcd6SMike Christie 		case ISCSI_STATE_LOGGING_OUT:
17249000bcd6SMike Christie 			reason = FAILURE_SESSION_LOGGING_OUT;
1725301e0f7eSMike Christie 			sc->result = DID_IMM_RETRY << 16;
1726301e0f7eSMike Christie 			break;
1727b3a7ea8dSMike Christie 		case ISCSI_STATE_RECOVERY_FAILED:
1728656cffc9SMike Christie 			reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
172956d7fcfaSMike Christie 			sc->result = DID_TRANSPORT_FAILFAST << 16;
1730b3a7ea8dSMike Christie 			break;
1731b3a7ea8dSMike Christie 		case ISCSI_STATE_TERMINATE:
1732656cffc9SMike Christie 			reason = FAILURE_SESSION_TERMINATE;
17336eabafbeSMike Christie 			sc->result = DID_NO_CONNECT << 16;
1734b3a7ea8dSMike Christie 			break;
1735b3a7ea8dSMike Christie 		default:
17367996a778SMike Christie 			reason = FAILURE_SESSION_FREED;
17376eabafbeSMike Christie 			sc->result = DID_NO_CONNECT << 16;
1738b3a7ea8dSMike Christie 		}
17397996a778SMike Christie 		goto fault;
17407996a778SMike Christie 	}
17417996a778SMike Christie 
17427996a778SMike Christie 	conn = session->leadconn;
174398644047SMike Christie 	if (!conn) {
174498644047SMike Christie 		reason = FAILURE_SESSION_FREED;
17456eabafbeSMike Christie 		sc->result = DID_NO_CONNECT << 16;
174698644047SMike Christie 		goto fault;
174798644047SMike Christie 	}
17487996a778SMike Christie 
1749661134adSMike Christie 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1750661134adSMike Christie 		reason = FAILURE_SESSION_IN_RECOVERY;
1751eef9ffdfSJohannes Thumshirn 		sc->result = DID_REQUEUE << 16;
1752661134adSMike Christie 		goto fault;
1753661134adSMike Christie 	}
1754661134adSMike Christie 
175577a23c21SMike Christie 	if (iscsi_check_cmdsn_window_closed(conn)) {
175677a23c21SMike Christie 		reason = FAILURE_WINDOW_CLOSED;
175777a23c21SMike Christie 		goto reject;
175877a23c21SMike Christie 	}
175977a23c21SMike Christie 
1760577577daSMike Christie 	task = iscsi_alloc_task(conn, sc);
1761577577daSMike Christie 	if (!task) {
176260ecebf5SMike Christie 		reason = FAILURE_OOM;
176360ecebf5SMike Christie 		goto reject;
176460ecebf5SMike Christie 	}
17657996a778SMike Christie 
17661336aed1SMike Christie 	if (!ihost->workq) {
1767577577daSMike Christie 		reason = iscsi_prep_scsi_cmd_pdu(task);
1768577577daSMike Christie 		if (reason) {
17695d12c05eSMike Christie 			if (reason == -ENOMEM ||  reason == -EACCES) {
1770577577daSMike Christie 				reason = FAILURE_OOM;
1771577577daSMike Christie 				goto prepd_reject;
1772577577daSMike Christie 			} else {
1773052d0144SMike Christie 				sc->result = DID_ABORT << 16;
1774577577daSMike Christie 				goto prepd_fault;
1775577577daSMike Christie 			}
1776052d0144SMike Christie 		}
17779c19a7d0SMike Christie 		if (session->tt->xmit_task(task)) {
1778d3305f34SMike Christie 			session->cmdsn--;
1779052d0144SMike Christie 			reason = FAILURE_SESSION_NOT_READY;
1780577577daSMike Christie 			goto prepd_reject;
1781052d0144SMike Christie 		}
17823bbaaad9SMike Christie 	} else {
17833bbaaad9SMike Christie 		list_add_tail(&task->running, &conn->cmdqueue);
178432ae763eSMike Christie 		iscsi_conn_queue_work(conn);
17853bbaaad9SMike Christie 	}
1786052d0144SMike Christie 
1787052d0144SMike Christie 	session->queued_cmdsn++;
1788659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
17897996a778SMike Christie 	return 0;
17907996a778SMike Christie 
1791577577daSMike Christie prepd_reject:
1792a656183eSLee Duncan 	spin_lock_bh(&session->back_lock);
1793f41d4721SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1794a656183eSLee Duncan 	spin_unlock_bh(&session->back_lock);
17957996a778SMike Christie reject:
1796659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
17971b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
17981b2c7af8SMike Christie 			  sc->cmnd[0], reason);
1799d6d13ee1SMike Christie 	return SCSI_MLQUEUE_TARGET_BUSY;
18007996a778SMike Christie 
1801577577daSMike Christie prepd_fault:
1802a656183eSLee Duncan 	spin_lock_bh(&session->back_lock);
1803f41d4721SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1804a656183eSLee Duncan 	spin_unlock_bh(&session->back_lock);
18057996a778SMike Christie fault:
1806659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
18071b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
18081b2c7af8SMike Christie 			  sc->cmnd[0], reason);
18091c138991SFUJITA Tomonori 	scsi_set_resid(sc, scsi_bufflen(sc));
1810b4b84edcSBart Van Assche 	scsi_done(sc);
18117996a778SMike Christie 	return 0;
18127996a778SMike Christie }
18137996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_queuecommand);
18147996a778SMike Christie 
18156b5d6c44SMike Christie int iscsi_target_alloc(struct scsi_target *starget)
18166b5d6c44SMike Christie {
18176b5d6c44SMike Christie 	struct iscsi_cls_session *cls_session = starget_to_session(starget);
18186b5d6c44SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
18196b5d6c44SMike Christie 
18206b5d6c44SMike Christie 	starget->can_queue = session->scsi_cmds_max;
18216b5d6c44SMike Christie 	return 0;
18226b5d6c44SMike Christie }
18236b5d6c44SMike Christie EXPORT_SYMBOL_GPL(iscsi_target_alloc);
18246b5d6c44SMike Christie 
18252c4b9637SKees Cook static void iscsi_tmf_timedout(struct timer_list *t)
18267996a778SMike Christie {
1827ec29d0acSMike Christie 	struct iscsi_session *session = from_timer(session, t, tmf_timer);
18287996a778SMike Christie 
1829659743b0SShlomo Pongratz 	spin_lock(&session->frwd_lock);
1830ec29d0acSMike Christie 	if (session->tmf_state == TMF_QUEUED) {
1831ec29d0acSMike Christie 		session->tmf_state = TMF_TIMEDOUT;
1832bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "tmf timedout\n");
18337996a778SMike Christie 		/* unblock eh_abort() */
1834ec29d0acSMike Christie 		wake_up(&session->ehwait);
18357996a778SMike Christie 	}
1836659743b0SShlomo Pongratz 	spin_unlock(&session->frwd_lock);
18377996a778SMike Christie }
18387996a778SMike Christie 
18399c19a7d0SMike Christie static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1840f6d5180cSMike Christie 				   struct iscsi_tm *hdr, int age,
1841f6d5180cSMike Christie 				   int timeout)
18421360c58aSBart Van Assche 	__must_hold(&session->frwd_lock)
18437996a778SMike Christie {
18447996a778SMike Christie 	struct iscsi_session *session = conn->session;
18459c19a7d0SMike Christie 	struct iscsi_task *task;
18467996a778SMike Christie 
18479c19a7d0SMike Christie 	task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
18487996a778SMike Christie 				      NULL, 0);
18499c19a7d0SMike Christie 	if (!task) {
1850659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
1851df4da5cdSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
18527996a778SMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1853659743b0SShlomo Pongratz 		spin_lock_bh(&session->frwd_lock);
185477a23c21SMike Christie 		return -EPERM;
18557996a778SMike Christie 	}
18567996a778SMike Christie 	conn->tmfcmd_pdus_cnt++;
1857ec29d0acSMike Christie 	session->tmf_timer.expires = timeout * HZ + jiffies;
1858ec29d0acSMike Christie 	add_timer(&session->tmf_timer);
1859bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "tmf set timeout\n");
1860843c0a8aSMike Christie 
1861659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
18626724add1SMike Christie 	mutex_unlock(&session->eh_mutex);
18637996a778SMike Christie 
18647996a778SMike Christie 	/*
18657996a778SMike Christie 	 * block eh thread until:
18667996a778SMike Christie 	 *
1867843c0a8aSMike Christie 	 * 1) tmf response
1868843c0a8aSMike Christie 	 * 2) tmf timeout
18697996a778SMike Christie 	 * 3) session is terminated or restarted or userspace has
18707996a778SMike Christie 	 * given up on recovery
18717996a778SMike Christie 	 */
1872ec29d0acSMike Christie 	wait_event_interruptible(session->ehwait, age != session->age ||
18737996a778SMike Christie 				 session->state != ISCSI_STATE_LOGGED_IN ||
1874ec29d0acSMike Christie 				 session->tmf_state != TMF_QUEUED);
18757996a778SMike Christie 	if (signal_pending(current))
18767996a778SMike Christie 		flush_signals(current);
1877ec29d0acSMike Christie 	del_timer_sync(&session->tmf_timer);
1878843c0a8aSMike Christie 
18796724add1SMike Christie 	mutex_lock(&session->eh_mutex);
1880659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
18819c19a7d0SMike Christie 	/* if the session drops it will clean up the task */
1882843c0a8aSMike Christie 	if (age != session->age ||
1883843c0a8aSMike Christie 	    session->state != ISCSI_STATE_LOGGED_IN)
1884843c0a8aSMike Christie 		return -ENOTCONN;
18857996a778SMike Christie 	return 0;
18867996a778SMike Christie }
18877996a778SMike Christie 
18887996a778SMike Christie /*
188914936b1eSMike Christie  * Fail commands. session frwd lock held and xmit thread flushed.
1890843c0a8aSMike Christie  */
18919cb78c16SHannes Reinecke static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
1892843c0a8aSMike Christie {
189314936b1eSMike Christie 	struct iscsi_session *session = conn->session;
18943bbaaad9SMike Christie 	struct iscsi_task *task;
18953bbaaad9SMike Christie 	int i;
1896843c0a8aSMike Christie 
189714936b1eSMike Christie 	spin_lock_bh(&session->back_lock);
189814936b1eSMike Christie 	for (i = 0; i < session->cmds_max; i++) {
189914936b1eSMike Christie 		task = session->cmds[i];
19003bbaaad9SMike Christie 		if (!task->sc || task->state == ISCSI_TASK_FREE)
19013bbaaad9SMike Christie 			continue;
1902843c0a8aSMike Christie 
19033bbaaad9SMike Christie 		if (lun != -1 && lun != task->sc->device->lun)
19043bbaaad9SMike Christie 			continue;
19053bbaaad9SMike Christie 
190614936b1eSMike Christie 		__iscsi_get_task(task);
190714936b1eSMike Christie 		spin_unlock_bh(&session->back_lock);
190814936b1eSMike Christie 
190914936b1eSMike Christie 		ISCSI_DBG_SESSION(session,
19103bbaaad9SMike Christie 				  "failing sc %p itt 0x%x state %d\n",
19113bbaaad9SMike Christie 				  task->sc, task->itt, task->state);
1912b3cd5050SMike Christie 		fail_scsi_task(task, error);
191314936b1eSMike Christie 
191414936b1eSMike Christie 		spin_unlock_bh(&session->frwd_lock);
191514936b1eSMike Christie 		iscsi_put_task(task);
191614936b1eSMike Christie 		spin_lock_bh(&session->frwd_lock);
191714936b1eSMike Christie 
191814936b1eSMike Christie 		spin_lock_bh(&session->back_lock);
1919843c0a8aSMike Christie 	}
192014936b1eSMike Christie 
192114936b1eSMike Christie 	spin_unlock_bh(&session->back_lock);
1922843c0a8aSMike Christie }
1923843c0a8aSMike Christie 
1924661134adSMike Christie /**
1925661134adSMike Christie  * iscsi_suspend_queue - suspend iscsi_queuecommand
1926661134adSMike Christie  * @conn: iscsi conn to stop queueing IO on
1927661134adSMike Christie  *
1928659743b0SShlomo Pongratz  * This grabs the session frwd_lock to make sure no one is in
1929661134adSMike Christie  * xmit_task/queuecommand, and then sets suspend to prevent
1930661134adSMike Christie  * new commands from being queued. This only needs to be called
1931661134adSMike Christie  * by offload drivers that need to sync a path like ep disconnect
1932661134adSMike Christie  * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1933661134adSMike Christie  * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1934661134adSMike Christie  */
1935661134adSMike Christie void iscsi_suspend_queue(struct iscsi_conn *conn)
1936661134adSMike Christie {
1937659743b0SShlomo Pongratz 	spin_lock_bh(&conn->session->frwd_lock);
1938661134adSMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1939659743b0SShlomo Pongratz 	spin_unlock_bh(&conn->session->frwd_lock);
1940661134adSMike Christie }
1941661134adSMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1942661134adSMike Christie 
1943661134adSMike Christie /**
1944661134adSMike Christie  * iscsi_suspend_tx - suspend iscsi_data_xmit
1945661134adSMike Christie  * @conn: iscsi conn tp stop processing IO on.
1946661134adSMike Christie  *
1947661134adSMike Christie  * This function sets the suspend bit to prevent iscsi_data_xmit
1948661134adSMike Christie  * from sending new IO, and if work is queued on the xmit thread
1949661134adSMike Christie  * it will wait for it to be completed.
1950661134adSMike Christie  */
1951b40977d9SMike Christie void iscsi_suspend_tx(struct iscsi_conn *conn)
19526724add1SMike Christie {
195332ae763eSMike Christie 	struct Scsi_Host *shost = conn->session->host;
195432ae763eSMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
195532ae763eSMike Christie 
19566724add1SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
19571336aed1SMike Christie 	if (ihost->workq)
195832ae763eSMike Christie 		flush_workqueue(ihost->workq);
19596724add1SMike Christie }
1960b40977d9SMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
19616724add1SMike Christie 
19626724add1SMike Christie static void iscsi_start_tx(struct iscsi_conn *conn)
19636724add1SMike Christie {
19646724add1SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
196532ae763eSMike Christie 	iscsi_conn_queue_work(conn);
19666724add1SMike Christie }
19676724add1SMike Christie 
19684c48a829SMike Christie /*
19694c48a829SMike Christie  * We want to make sure a ping is in flight. It has timed out.
19704c48a829SMike Christie  * And we are not busy processing a pdu that is making
19714c48a829SMike Christie  * progress but got started before the ping and is taking a while
19724c48a829SMike Christie  * to complete so the ping is just stuck behind it in a queue.
19734c48a829SMike Christie  */
19744c48a829SMike Christie static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
19754c48a829SMike Christie {
1976fe0a8a95SLee Duncan 	if (READ_ONCE(conn->ping_task) &&
19774c48a829SMike Christie 	    time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
19784c48a829SMike Christie 			   (conn->ping_timeout * HZ), jiffies))
19794c48a829SMike Christie 		return 1;
19804c48a829SMike Christie 	else
19814c48a829SMike Christie 		return 0;
19824c48a829SMike Christie }
19834c48a829SMike Christie 
1984b6a05c82SChristoph Hellwig enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1985f6d5180cSMike Christie {
19866600593cSChristoph Hellwig 	enum blk_eh_timer_return rc = BLK_EH_DONE;
198792ed4d69SMike Christie 	struct iscsi_task *task = NULL, *running_task;
1988f6d5180cSMike Christie 	struct iscsi_cls_session *cls_session;
1989f6d5180cSMike Christie 	struct iscsi_session *session;
1990f6d5180cSMike Christie 	struct iscsi_conn *conn;
199192ed4d69SMike Christie 	int i;
1992f6d5180cSMike Christie 
1993d355e57dSMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
199475613521SMike Christie 	session = cls_session->dd_data;
1995f6d5180cSMike Christie 
1996bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1997f6d5180cSMike Christie 
19985480e299SBart Van Assche 	spin_lock_bh(&session->frwd_lock);
199914936b1eSMike Christie 	spin_lock(&session->back_lock);
2000*db22de3eSBart Van Assche 	task = iscsi_cmd(sc)->task;
2001e3d338a5SMike Christie 	if (!task) {
2002e3d338a5SMike Christie 		/*
2003e3d338a5SMike Christie 		 * Raced with completion. Blk layer has taken ownership
2004e3d338a5SMike Christie 		 * so let timeout code complete it now.
2005e3d338a5SMike Christie 		 */
2006adb2b769SChristoph Hellwig 		rc = BLK_EH_DONE;
200714936b1eSMike Christie 		spin_unlock(&session->back_lock);
2008e3d338a5SMike Christie 		goto done;
2009e3d338a5SMike Christie 	}
201014936b1eSMike Christie 	__iscsi_get_task(task);
201114936b1eSMike Christie 	spin_unlock(&session->back_lock);
2012e3d338a5SMike Christie 
2013f6d5180cSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN) {
2014f6d5180cSMike Christie 		/*
2015d7549412SRafael David Tinoco 		 * During shutdown, if session is prematurely disconnected,
2016d7549412SRafael David Tinoco 		 * recovery won't happen and there will be hung cmds. Not
2017d7549412SRafael David Tinoco 		 * handling cmds would trigger EH, also bad in this case.
2018d7549412SRafael David Tinoco 		 * Instead, handle cmd, allow completion to happen and let
2019d7549412SRafael David Tinoco 		 * upper layer to deal with the result.
2020d7549412SRafael David Tinoco 		 */
2021d7549412SRafael David Tinoco 		if (unlikely(system_state != SYSTEM_RUNNING)) {
2022d7549412SRafael David Tinoco 			sc->result = DID_NO_CONNECT << 16;
2023d7549412SRafael David Tinoco 			ISCSI_DBG_EH(session, "sc on shutdown, handled\n");
2024adb2b769SChristoph Hellwig 			rc = BLK_EH_DONE;
2025d7549412SRafael David Tinoco 			goto done;
2026d7549412SRafael David Tinoco 		}
2027d7549412SRafael David Tinoco 		/*
2028f6d5180cSMike Christie 		 * We are probably in the middle of iscsi recovery so let
2029f6d5180cSMike Christie 		 * that complete and handle the error.
2030f6d5180cSMike Christie 		 */
2031242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
2032f6d5180cSMike Christie 		goto done;
2033f6d5180cSMike Christie 	}
2034f6d5180cSMike Christie 
2035f6d5180cSMike Christie 	conn = session->leadconn;
2036f6d5180cSMike Christie 	if (!conn) {
2037f6d5180cSMike Christie 		/* In the middle of shuting down */
2038242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
2039f6d5180cSMike Christie 		goto done;
2040f6d5180cSMike Christie 	}
2041f6d5180cSMike Christie 
2042d355e57dSMike Christie 	/*
2043d355e57dSMike Christie 	 * If we have sent (at least queued to the network layer) a pdu or
2044d355e57dSMike Christie 	 * recvd one for the task since the last timeout ask for
2045d355e57dSMike Christie 	 * more time. If on the next timeout we have not made progress
2046d355e57dSMike Christie 	 * we can check if it is the task or connection when we send the
2047d355e57dSMike Christie 	 * nop as a ping.
2048d355e57dSMike Christie 	 */
204992ed4d69SMike Christie 	if (time_after(task->last_xfer, task->last_timeout)) {
2050bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "Command making progress. Asking "
2051d355e57dSMike Christie 			     "scsi-ml for more time to complete. "
205292ed4d69SMike Christie 			     "Last data xfer at %lu. Last timeout was at "
2053d355e57dSMike Christie 			     "%lu\n.", task->last_xfer, task->last_timeout);
2054d355e57dSMike Christie 		task->have_checked_conn = false;
2055d355e57dSMike Christie 		rc = BLK_EH_RESET_TIMER;
2056d355e57dSMike Christie 		goto done;
2057d355e57dSMike Christie 	}
2058d355e57dSMike Christie 
2059f6d5180cSMike Christie 	if (!conn->recv_timeout && !conn->ping_timeout)
2060f6d5180cSMike Christie 		goto done;
2061f6d5180cSMike Christie 	/*
2062f6d5180cSMike Christie 	 * if the ping timedout then we are in the middle of cleaning up
2063f6d5180cSMike Christie 	 * and can let the iscsi eh handle it
2064f6d5180cSMike Christie 	 */
20654c48a829SMike Christie 	if (iscsi_has_ping_timed_out(conn)) {
2066242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
20674c48a829SMike Christie 		goto done;
20684c48a829SMike Christie 	}
2069d355e57dSMike Christie 
207014936b1eSMike Christie 	spin_lock(&session->back_lock);
207192ed4d69SMike Christie 	for (i = 0; i < conn->session->cmds_max; i++) {
207292ed4d69SMike Christie 		running_task = conn->session->cmds[i];
207392ed4d69SMike Christie 		if (!running_task->sc || running_task == task ||
207492ed4d69SMike Christie 		     running_task->state != ISCSI_TASK_RUNNING)
207592ed4d69SMike Christie 			continue;
207692ed4d69SMike Christie 
207792ed4d69SMike Christie 		/*
207892ed4d69SMike Christie 		 * Only check if cmds started before this one have made
207992ed4d69SMike Christie 		 * progress, or this could never fail
208092ed4d69SMike Christie 		 */
208192ed4d69SMike Christie 		if (time_after(running_task->sc->jiffies_at_alloc,
208292ed4d69SMike Christie 			       task->sc->jiffies_at_alloc))
208392ed4d69SMike Christie 			continue;
208492ed4d69SMike Christie 
208592ed4d69SMike Christie 		if (time_after(running_task->last_xfer, task->last_timeout)) {
208692ed4d69SMike Christie 			/*
208792ed4d69SMike Christie 			 * This task has not made progress, but a task
208892ed4d69SMike Christie 			 * started before us has transferred data since
208992ed4d69SMike Christie 			 * we started/last-checked. We could be queueing
209092ed4d69SMike Christie 			 * too many tasks or the LU is bad.
209192ed4d69SMike Christie 			 *
209292ed4d69SMike Christie 			 * If the device is bad the cmds ahead of us on
209392ed4d69SMike Christie 			 * other devs will complete, and this loop will
209492ed4d69SMike Christie 			 * eventually fail starting the scsi eh.
209592ed4d69SMike Christie 			 */
209692ed4d69SMike Christie 			ISCSI_DBG_EH(session, "Command has not made progress "
209792ed4d69SMike Christie 				     "but commands ahead of it have. "
209892ed4d69SMike Christie 				     "Asking scsi-ml for more time to "
209992ed4d69SMike Christie 				     "complete. Our last xfer vs running task "
210092ed4d69SMike Christie 				     "last xfer %lu/%lu. Last check %lu.\n",
210192ed4d69SMike Christie 				     task->last_xfer, running_task->last_xfer,
210292ed4d69SMike Christie 				     task->last_timeout);
210314936b1eSMike Christie 			spin_unlock(&session->back_lock);
210492ed4d69SMike Christie 			rc = BLK_EH_RESET_TIMER;
210592ed4d69SMike Christie 			goto done;
210692ed4d69SMike Christie 		}
210792ed4d69SMike Christie 	}
210814936b1eSMike Christie 	spin_unlock(&session->back_lock);
210992ed4d69SMike Christie 
2110d355e57dSMike Christie 	/* Assumes nop timeout is shorter than scsi cmd timeout */
2111d355e57dSMike Christie 	if (task->have_checked_conn)
2112d355e57dSMike Christie 		goto done;
2113d355e57dSMike Christie 
2114f6d5180cSMike Christie 	/*
2115d355e57dSMike Christie 	 * Checking the transport already or nop from a cmd timeout still
2116d355e57dSMike Christie 	 * running
2117f6d5180cSMike Christie 	 */
2118fe0a8a95SLee Duncan 	if (READ_ONCE(conn->ping_task)) {
2119d355e57dSMike Christie 		task->have_checked_conn = true;
2120242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
21214c48a829SMike Christie 		goto done;
21224c48a829SMike Christie 	}
21234c48a829SMike Christie 
2124d355e57dSMike Christie 	/* Make sure there is a transport check done */
2125d355e57dSMike Christie 	iscsi_send_nopout(conn, NULL);
2126d355e57dSMike Christie 	task->have_checked_conn = true;
2127242f9dcbSJens Axboe 	rc = BLK_EH_RESET_TIMER;
2128d355e57dSMike Christie 
2129f6d5180cSMike Christie done:
21305480e299SBart Van Assche 	spin_unlock_bh(&session->frwd_lock);
213114936b1eSMike Christie 
213214936b1eSMike Christie 	if (task) {
213314936b1eSMike Christie 		task->last_timeout = jiffies;
213414936b1eSMike Christie 		iscsi_put_task(task);
213514936b1eSMike Christie 	}
2136bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2137d7549412SRafael David Tinoco 		     "timer reset" : "shutdown or nh");
2138f6d5180cSMike Christie 	return rc;
2139f6d5180cSMike Christie }
2140b6a05c82SChristoph Hellwig EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out);
2141f6d5180cSMike Christie 
21422c4b9637SKees Cook static void iscsi_check_transport_timeouts(struct timer_list *t)
2143f6d5180cSMike Christie {
21442c4b9637SKees Cook 	struct iscsi_conn *conn = from_timer(conn, t, transport_timer);
2145f6d5180cSMike Christie 	struct iscsi_session *session = conn->session;
21464cf10435SMike Christie 	unsigned long recv_timeout, next_timeout = 0, last_recv;
2147f6d5180cSMike Christie 
2148659743b0SShlomo Pongratz 	spin_lock(&session->frwd_lock);
2149f6d5180cSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN)
2150f6d5180cSMike Christie 		goto done;
2151f6d5180cSMike Christie 
21524cf10435SMike Christie 	recv_timeout = conn->recv_timeout;
21534cf10435SMike Christie 	if (!recv_timeout)
2154f6d5180cSMike Christie 		goto done;
2155f6d5180cSMike Christie 
21564cf10435SMike Christie 	recv_timeout *= HZ;
2157f6d5180cSMike Christie 	last_recv = conn->last_recv;
21584c48a829SMike Christie 
21594c48a829SMike Christie 	if (iscsi_has_ping_timed_out(conn)) {
2160322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
21614c48a829SMike Christie 				  "expired, recv timeout %d, last rx %lu, "
21624c48a829SMike Christie 				  "last ping %lu, now %lu\n",
21634c48a829SMike Christie 				  conn->ping_timeout, conn->recv_timeout,
21644c48a829SMike Christie 				  last_recv, conn->last_ping, jiffies);
2165659743b0SShlomo Pongratz 		spin_unlock(&session->frwd_lock);
216609ff742cSMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
2167f6d5180cSMike Christie 		return;
2168f6d5180cSMike Christie 	}
2169f6d5180cSMike Christie 
21704cf10435SMike Christie 	if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2171f6d5180cSMike Christie 		/* send a ping to try to provoke some traffic */
21721b2c7af8SMike Christie 		ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
217352f5664aSAriel Nahum 		if (iscsi_send_nopout(conn, NULL))
217452f5664aSAriel Nahum 			next_timeout = jiffies + (1 * HZ);
217552f5664aSAriel Nahum 		else
21764cf10435SMike Christie 			next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2177ad294e9cSMike Christie 	} else
21784cf10435SMike Christie 		next_timeout = last_recv + recv_timeout;
2179f6d5180cSMike Christie 
21801b2c7af8SMike Christie 	ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2181f6d5180cSMike Christie 	mod_timer(&conn->transport_timer, next_timeout);
2182f6d5180cSMike Christie done:
2183659743b0SShlomo Pongratz 	spin_unlock(&session->frwd_lock);
2184f6d5180cSMike Christie }
2185f6d5180cSMike Christie 
2186891e2639SMike Christie /**
2187891e2639SMike Christie  * iscsi_conn_unbind - prevent queueing to conn.
2188891e2639SMike Christie  * @cls_conn: iscsi conn ep is bound to.
2189891e2639SMike Christie  * @is_active: is the conn in use for boot or is this for EH/termination
2190891e2639SMike Christie  *
2191891e2639SMike Christie  * This must be called by drivers implementing the ep_disconnect callout.
2192891e2639SMike Christie  * It disables queueing to the connection from libiscsi in preparation for
2193891e2639SMike Christie  * an ep_disconnect call.
2194891e2639SMike Christie  */
2195891e2639SMike Christie void iscsi_conn_unbind(struct iscsi_cls_conn *cls_conn, bool is_active)
2196891e2639SMike Christie {
2197891e2639SMike Christie 	struct iscsi_session *session;
2198891e2639SMike Christie 	struct iscsi_conn *conn;
2199891e2639SMike Christie 
2200891e2639SMike Christie 	if (!cls_conn)
2201891e2639SMike Christie 		return;
2202891e2639SMike Christie 
2203891e2639SMike Christie 	conn = cls_conn->dd_data;
2204891e2639SMike Christie 	session = conn->session;
2205891e2639SMike Christie 	/*
2206891e2639SMike Christie 	 * Wait for iscsi_eh calls to exit. We don't wait for the tmf to
2207891e2639SMike Christie 	 * complete or timeout. The caller just wants to know what's running
2208891e2639SMike Christie 	 * is everything that needs to be cleaned up, and no cmds will be
2209891e2639SMike Christie 	 * queued.
2210891e2639SMike Christie 	 */
2211891e2639SMike Christie 	mutex_lock(&session->eh_mutex);
2212891e2639SMike Christie 
2213891e2639SMike Christie 	iscsi_suspend_queue(conn);
2214891e2639SMike Christie 	iscsi_suspend_tx(conn);
2215891e2639SMike Christie 
2216891e2639SMike Christie 	spin_lock_bh(&session->frwd_lock);
2217891e2639SMike Christie 	if (!is_active) {
2218891e2639SMike Christie 		/*
2219891e2639SMike Christie 		 * if logout timed out before userspace could even send a PDU
2220891e2639SMike Christie 		 * the state might still be in ISCSI_STATE_LOGGED_IN and
2221891e2639SMike Christie 		 * allowing new cmds and TMFs.
2222891e2639SMike Christie 		 */
2223891e2639SMike Christie 		if (session->state == ISCSI_STATE_LOGGED_IN)
2224891e2639SMike Christie 			iscsi_set_conn_failed(conn);
2225891e2639SMike Christie 	}
2226891e2639SMike Christie 	spin_unlock_bh(&session->frwd_lock);
2227891e2639SMike Christie 	mutex_unlock(&session->eh_mutex);
2228891e2639SMike Christie }
2229891e2639SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_unbind);
2230891e2639SMike Christie 
22319c19a7d0SMike Christie static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2232843c0a8aSMike Christie 				      struct iscsi_tm *hdr)
2233843c0a8aSMike Christie {
2234843c0a8aSMike Christie 	memset(hdr, 0, sizeof(*hdr));
2235843c0a8aSMike Christie 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2236843c0a8aSMike Christie 	hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2237843c0a8aSMike Christie 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
223855bdabdfSAndy Grover 	hdr->lun = task->lun;
2239577577daSMike Christie 	hdr->rtt = task->hdr_itt;
2240577577daSMike Christie 	hdr->refcmdsn = task->cmdsn;
2241843c0a8aSMike Christie }
2242843c0a8aSMike Christie 
22437996a778SMike Christie int iscsi_eh_abort(struct scsi_cmnd *sc)
22447996a778SMike Christie {
224575613521SMike Christie 	struct iscsi_cls_session *cls_session;
224675613521SMike Christie 	struct iscsi_session *session;
2247f47f2cf5SMike Christie 	struct iscsi_conn *conn;
22489c19a7d0SMike Christie 	struct iscsi_task *task;
2249843c0a8aSMike Christie 	struct iscsi_tm *hdr;
2250e9e410e8SBart Van Assche 	int age;
22517996a778SMike Christie 
225275613521SMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
225375613521SMike Christie 	session = cls_session->dd_data;
225475613521SMike Christie 
2255bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
22564421c9ebSMike Christie 
22576724add1SMike Christie 	mutex_lock(&session->eh_mutex);
2258659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
2259f47f2cf5SMike Christie 	/*
2260f47f2cf5SMike Christie 	 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2261f47f2cf5SMike Christie 	 * got the command.
2262f47f2cf5SMike Christie 	 */
2263*db22de3eSBart Van Assche 	if (!iscsi_cmd(sc)->task) {
2264bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
22651b2c7af8SMike Christie 				      "it completed.\n");
2266659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
22676724add1SMike Christie 		mutex_unlock(&session->eh_mutex);
2268f47f2cf5SMike Christie 		return SUCCESS;
2269f47f2cf5SMike Christie 	}
2270f47f2cf5SMike Christie 
22717996a778SMike Christie 	/*
22727996a778SMike Christie 	 * If we are not logged in or we have started a new session
22737996a778SMike Christie 	 * then let the host reset code handle this
22747996a778SMike Christie 	 */
2275843c0a8aSMike Christie 	if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2276*db22de3eSBart Van Assche 	    iscsi_cmd(sc)->age != session->age) {
2277659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
2278843c0a8aSMike Christie 		mutex_unlock(&session->eh_mutex);
2279bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "failing abort due to dropped "
22804421c9ebSMike Christie 				  "session.\n");
2281843c0a8aSMike Christie 		return FAILED;
2282843c0a8aSMike Christie 	}
2283843c0a8aSMike Christie 
228414936b1eSMike Christie 	spin_lock(&session->back_lock);
2285*db22de3eSBart Van Assche 	task = iscsi_cmd(sc)->task;
228614936b1eSMike Christie 	if (!task || !task->sc) {
22879c19a7d0SMike Christie 		/* task completed before time out */
2288bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
228914936b1eSMike Christie 
229014936b1eSMike Christie 		spin_unlock(&session->back_lock);
2291258aad75SMike Christie 		spin_unlock_bh(&session->frwd_lock);
2292258aad75SMike Christie 		mutex_unlock(&session->eh_mutex);
2293258aad75SMike Christie 		return SUCCESS;
22947ea8b828SMike Christie 	}
2295258aad75SMike Christie 
2296258aad75SMike Christie 	conn = session->leadconn;
2297258aad75SMike Christie 	iscsi_get_conn(conn->cls_conn);
2298258aad75SMike Christie 	conn->eh_abort_cnt++;
2299258aad75SMike Christie 	age = session->age;
2300258aad75SMike Christie 
230114936b1eSMike Christie 	ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n", sc, task->itt);
230214936b1eSMike Christie 	__iscsi_get_task(task);
230314936b1eSMike Christie 	spin_unlock(&session->back_lock);
23047996a778SMike Christie 
23059c19a7d0SMike Christie 	if (task->state == ISCSI_TASK_PENDING) {
2306b3cd5050SMike Christie 		fail_scsi_task(task, DID_ABORT);
230777a23c21SMike Christie 		goto success;
230877a23c21SMike Christie 	}
23097996a778SMike Christie 
2310843c0a8aSMike Christie 	/* only have one tmf outstanding at a time */
2311ec29d0acSMike Christie 	if (session->tmf_state != TMF_INITIAL)
23127996a778SMike Christie 		goto failed;
2313ec29d0acSMike Christie 	session->tmf_state = TMF_QUEUED;
23147996a778SMike Christie 
2315ec29d0acSMike Christie 	hdr = &session->tmhdr;
23169c19a7d0SMike Christie 	iscsi_prep_abort_task_pdu(task, hdr);
2317843c0a8aSMike Christie 
2318e9e410e8SBart Van Assche 	if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout))
2319843c0a8aSMike Christie 		goto failed;
2320843c0a8aSMike Christie 
2321ec29d0acSMike Christie 	switch (session->tmf_state) {
2322843c0a8aSMike Christie 	case TMF_SUCCESS:
2323659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
2324913e5bf4SMike Christie 		/*
2325913e5bf4SMike Christie 		 * stop tx side incase the target had sent a abort rsp but
2326913e5bf4SMike Christie 		 * the initiator was still writing out data.
2327913e5bf4SMike Christie 		 */
23286724add1SMike Christie 		iscsi_suspend_tx(conn);
23297996a778SMike Christie 		/*
2330913e5bf4SMike Christie 		 * we do not stop the recv side because targets have been
2331913e5bf4SMike Christie 		 * good and have never sent us a successful tmf response
2332913e5bf4SMike Christie 		 * then sent more data for the cmd.
23337996a778SMike Christie 		 */
2334659743b0SShlomo Pongratz 		spin_lock_bh(&session->frwd_lock);
2335b3cd5050SMike Christie 		fail_scsi_task(task, DID_ABORT);
2336ec29d0acSMike Christie 		session->tmf_state = TMF_INITIAL;
23375d12c05eSMike Christie 		memset(hdr, 0, sizeof(*hdr));
2338659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
23396724add1SMike Christie 		iscsi_start_tx(conn);
234077a23c21SMike Christie 		goto success_unlocked;
2341843c0a8aSMike Christie 	case TMF_TIMEDOUT:
234299b06033SMike Christie 		session->running_aborted_task = task;
2343659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
2344df4da5cdSMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2345843c0a8aSMike Christie 		goto failed_unlocked;
2346843c0a8aSMike Christie 	case TMF_NOT_FOUND:
2347f6f96457SMike Christie 		if (iscsi_task_is_completed(task)) {
2348ec29d0acSMike Christie 			session->tmf_state = TMF_INITIAL;
23495d12c05eSMike Christie 			memset(hdr, 0, sizeof(*hdr));
23509c19a7d0SMike Christie 			/* task completed before tmf abort response */
2351bd2199d4SErez Zilber 			ISCSI_DBG_EH(session, "sc completed while abort	in "
2352bd2199d4SErez Zilber 					      "progress\n");
235377a23c21SMike Christie 			goto success;
235477a23c21SMike Christie 		}
2355df561f66SGustavo A. R. Silva 		fallthrough;
235677a23c21SMike Christie 	default:
2357ec29d0acSMike Christie 		session->tmf_state = TMF_INITIAL;
2358843c0a8aSMike Christie 		goto failed;
235977a23c21SMike Christie 	}
23607996a778SMike Christie 
236177a23c21SMike Christie success:
2362659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
236377a23c21SMike Christie success_unlocked:
2364bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
23651b2c7af8SMike Christie 		     sc, task->itt);
236614936b1eSMike Christie 	iscsi_put_task(task);
2367d39df158SMike Christie 	iscsi_put_conn(conn->cls_conn);
23686724add1SMike Christie 	mutex_unlock(&session->eh_mutex);
23697996a778SMike Christie 	return SUCCESS;
23707996a778SMike Christie 
23717996a778SMike Christie failed:
2372659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
237377a23c21SMike Christie failed_unlocked:
2374bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
23759c19a7d0SMike Christie 		     task ? task->itt : 0);
237699b06033SMike Christie 	/*
237799b06033SMike Christie 	 * The driver might be accessing the task so hold the ref. The conn
237899b06033SMike Christie 	 * stop cleanup will drop the ref after ep_disconnect so we know the
237999b06033SMike Christie 	 * driver's no longer touching the task.
238099b06033SMike Christie 	 */
238199b06033SMike Christie 	if (!session->running_aborted_task)
238214936b1eSMike Christie 		iscsi_put_task(task);
238399b06033SMike Christie 
2384d39df158SMike Christie 	iscsi_put_conn(conn->cls_conn);
23856724add1SMike Christie 	mutex_unlock(&session->eh_mutex);
23867996a778SMike Christie 	return FAILED;
23877996a778SMike Christie }
23887996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_abort);
23897996a778SMike Christie 
2390843c0a8aSMike Christie static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2391843c0a8aSMike Christie {
2392843c0a8aSMike Christie 	memset(hdr, 0, sizeof(*hdr));
2393843c0a8aSMike Christie 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2394843c0a8aSMike Christie 	hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2395843c0a8aSMike Christie 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
239655bdabdfSAndy Grover 	int_to_scsilun(sc->device->lun, &hdr->lun);
2397f6d5180cSMike Christie 	hdr->rtt = RESERVED_ITT;
2398843c0a8aSMike Christie }
2399843c0a8aSMike Christie 
2400843c0a8aSMike Christie int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2401843c0a8aSMike Christie {
240275613521SMike Christie 	struct iscsi_cls_session *cls_session;
240375613521SMike Christie 	struct iscsi_session *session;
2404843c0a8aSMike Christie 	struct iscsi_conn *conn;
2405843c0a8aSMike Christie 	struct iscsi_tm *hdr;
2406843c0a8aSMike Christie 	int rc = FAILED;
2407843c0a8aSMike Christie 
240875613521SMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
240975613521SMike Christie 	session = cls_session->dd_data;
241075613521SMike Christie 
24119cb78c16SHannes Reinecke 	ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc,
24129cb78c16SHannes Reinecke 		     sc->device->lun);
2413843c0a8aSMike Christie 
2414843c0a8aSMike Christie 	mutex_lock(&session->eh_mutex);
2415659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
2416843c0a8aSMike Christie 	/*
2417843c0a8aSMike Christie 	 * Just check if we are not logged in. We cannot check for
2418843c0a8aSMike Christie 	 * the phase because the reset could come from a ioctl.
2419843c0a8aSMike Christie 	 */
2420843c0a8aSMike Christie 	if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2421843c0a8aSMike Christie 		goto unlock;
2422843c0a8aSMike Christie 	conn = session->leadconn;
2423843c0a8aSMike Christie 
2424843c0a8aSMike Christie 	/* only have one tmf outstanding at a time */
2425ec29d0acSMike Christie 	if (session->tmf_state != TMF_INITIAL)
2426843c0a8aSMike Christie 		goto unlock;
2427ec29d0acSMike Christie 	session->tmf_state = TMF_QUEUED;
2428843c0a8aSMike Christie 
2429ec29d0acSMike Christie 	hdr = &session->tmhdr;
2430843c0a8aSMike Christie 	iscsi_prep_lun_reset_pdu(sc, hdr);
2431843c0a8aSMike Christie 
24329c19a7d0SMike Christie 	if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2433f6d5180cSMike Christie 				    session->lu_reset_timeout)) {
2434843c0a8aSMike Christie 		rc = FAILED;
2435843c0a8aSMike Christie 		goto unlock;
2436843c0a8aSMike Christie 	}
2437843c0a8aSMike Christie 
2438ec29d0acSMike Christie 	switch (session->tmf_state) {
2439843c0a8aSMike Christie 	case TMF_SUCCESS:
2440843c0a8aSMike Christie 		break;
2441843c0a8aSMike Christie 	case TMF_TIMEDOUT:
2442659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
2443df4da5cdSMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2444843c0a8aSMike Christie 		goto done;
2445843c0a8aSMike Christie 	default:
2446ec29d0acSMike Christie 		session->tmf_state = TMF_INITIAL;
2447843c0a8aSMike Christie 		goto unlock;
2448843c0a8aSMike Christie 	}
2449843c0a8aSMike Christie 
2450843c0a8aSMike Christie 	rc = SUCCESS;
2451659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
2452843c0a8aSMike Christie 
2453843c0a8aSMike Christie 	iscsi_suspend_tx(conn);
2454913e5bf4SMike Christie 
2455659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
24565d12c05eSMike Christie 	memset(hdr, 0, sizeof(*hdr));
24573bbaaad9SMike Christie 	fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2458ec29d0acSMike Christie 	session->tmf_state = TMF_INITIAL;
2459659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
2460843c0a8aSMike Christie 
2461843c0a8aSMike Christie 	iscsi_start_tx(conn);
2462843c0a8aSMike Christie 	goto done;
2463843c0a8aSMike Christie 
2464843c0a8aSMike Christie unlock:
2465659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
2466843c0a8aSMike Christie done:
2467bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "dev reset result = %s\n",
2468843c0a8aSMike Christie 		     rc == SUCCESS ? "SUCCESS" : "FAILED");
2469843c0a8aSMike Christie 	mutex_unlock(&session->eh_mutex);
2470843c0a8aSMike Christie 	return rc;
2471843c0a8aSMike Christie }
2472843c0a8aSMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2473843c0a8aSMike Christie 
24743fe5ae8bSMike Christie void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
24753fe5ae8bSMike Christie {
24763fe5ae8bSMike Christie 	struct iscsi_session *session = cls_session->dd_data;
24773fe5ae8bSMike Christie 
2478659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
24793fe5ae8bSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN) {
24803fe5ae8bSMike Christie 		session->state = ISCSI_STATE_RECOVERY_FAILED;
2481ec29d0acSMike Christie 		wake_up(&session->ehwait);
24823fe5ae8bSMike Christie 	}
2483659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
24843fe5ae8bSMike Christie }
24853fe5ae8bSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
24863fe5ae8bSMike Christie 
24873fe5ae8bSMike Christie /**
24883fe5ae8bSMike Christie  * iscsi_eh_session_reset - drop session and attempt relogin
24893fe5ae8bSMike Christie  * @sc: scsi command
24903fe5ae8bSMike Christie  *
24913fe5ae8bSMike Christie  * This function will wait for a relogin, session termination from
24923fe5ae8bSMike Christie  * userspace, or a recovery/replacement timeout.
24933fe5ae8bSMike Christie  */
2494309ce156SJayamohan Kallickal int iscsi_eh_session_reset(struct scsi_cmnd *sc)
24953fe5ae8bSMike Christie {
24963fe5ae8bSMike Christie 	struct iscsi_cls_session *cls_session;
24973fe5ae8bSMike Christie 	struct iscsi_session *session;
24983fe5ae8bSMike Christie 	struct iscsi_conn *conn;
24993fe5ae8bSMike Christie 
25003fe5ae8bSMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
25013fe5ae8bSMike Christie 	session = cls_session->dd_data;
25023fe5ae8bSMike Christie 
25033fe5ae8bSMike Christie 	mutex_lock(&session->eh_mutex);
2504659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
25053fe5ae8bSMike Christie 	if (session->state == ISCSI_STATE_TERMINATE) {
25063fe5ae8bSMike Christie failed:
25073fe5ae8bSMike Christie 		ISCSI_DBG_EH(session,
25083fe5ae8bSMike Christie 			     "failing session reset: Could not log back into "
25095db6dd14SFred Herard 			     "%s [age %d]\n", session->targetname,
25105db6dd14SFred Herard 			     session->age);
2511659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
25123fe5ae8bSMike Christie 		mutex_unlock(&session->eh_mutex);
25133fe5ae8bSMike Christie 		return FAILED;
25143fe5ae8bSMike Christie 	}
25153fe5ae8bSMike Christie 
2516fda290c5SMike Christie 	conn = session->leadconn;
2517fda290c5SMike Christie 	iscsi_get_conn(conn->cls_conn);
2518fda290c5SMike Christie 
2519659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
25203fe5ae8bSMike Christie 	mutex_unlock(&session->eh_mutex);
2521fda290c5SMike Christie 
2522df4da5cdSMike Christie 	iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2523fda290c5SMike Christie 	iscsi_put_conn(conn->cls_conn);
25243fe5ae8bSMike Christie 
25253fe5ae8bSMike Christie 	ISCSI_DBG_EH(session, "wait for relogin\n");
2526ec29d0acSMike Christie 	wait_event_interruptible(session->ehwait,
25273fe5ae8bSMike Christie 				 session->state == ISCSI_STATE_TERMINATE ||
25283fe5ae8bSMike Christie 				 session->state == ISCSI_STATE_LOGGED_IN ||
25293fe5ae8bSMike Christie 				 session->state == ISCSI_STATE_RECOVERY_FAILED);
25303fe5ae8bSMike Christie 	if (signal_pending(current))
25313fe5ae8bSMike Christie 		flush_signals(current);
25323fe5ae8bSMike Christie 
25333fe5ae8bSMike Christie 	mutex_lock(&session->eh_mutex);
2534659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
25353fe5ae8bSMike Christie 	if (session->state == ISCSI_STATE_LOGGED_IN) {
25363fe5ae8bSMike Christie 		ISCSI_DBG_EH(session,
25373fe5ae8bSMike Christie 			     "session reset succeeded for %s,%s\n",
25383fe5ae8bSMike Christie 			     session->targetname, conn->persistent_address);
25393fe5ae8bSMike Christie 	} else
25403fe5ae8bSMike Christie 		goto failed;
2541659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
25423fe5ae8bSMike Christie 	mutex_unlock(&session->eh_mutex);
25433fe5ae8bSMike Christie 	return SUCCESS;
25443fe5ae8bSMike Christie }
2545309ce156SJayamohan Kallickal EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
25463fe5ae8bSMike Christie 
25473fe5ae8bSMike Christie static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
25483fe5ae8bSMike Christie {
25493fe5ae8bSMike Christie 	memset(hdr, 0, sizeof(*hdr));
25503fe5ae8bSMike Christie 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
25513fe5ae8bSMike Christie 	hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
25523fe5ae8bSMike Christie 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
25533fe5ae8bSMike Christie 	hdr->rtt = RESERVED_ITT;
25543fe5ae8bSMike Christie }
25553fe5ae8bSMike Christie 
25563fe5ae8bSMike Christie /**
25573fe5ae8bSMike Christie  * iscsi_eh_target_reset - reset target
25583fe5ae8bSMike Christie  * @sc: scsi command
25593fe5ae8bSMike Christie  *
2560309ce156SJayamohan Kallickal  * This will attempt to send a warm target reset.
25613fe5ae8bSMike Christie  */
25623907adf6SBart Van Assche static int iscsi_eh_target_reset(struct scsi_cmnd *sc)
25633fe5ae8bSMike Christie {
25643fe5ae8bSMike Christie 	struct iscsi_cls_session *cls_session;
25653fe5ae8bSMike Christie 	struct iscsi_session *session;
25663fe5ae8bSMike Christie 	struct iscsi_conn *conn;
25673fe5ae8bSMike Christie 	struct iscsi_tm *hdr;
25683fe5ae8bSMike Christie 	int rc = FAILED;
25693fe5ae8bSMike Christie 
25703fe5ae8bSMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
25713fe5ae8bSMike Christie 	session = cls_session->dd_data;
25723fe5ae8bSMike Christie 
25733fe5ae8bSMike Christie 	ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
25743fe5ae8bSMike Christie 		     session->targetname);
25753fe5ae8bSMike Christie 
25763fe5ae8bSMike Christie 	mutex_lock(&session->eh_mutex);
2577659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
25783fe5ae8bSMike Christie 	/*
25793fe5ae8bSMike Christie 	 * Just check if we are not logged in. We cannot check for
25803fe5ae8bSMike Christie 	 * the phase because the reset could come from a ioctl.
25813fe5ae8bSMike Christie 	 */
25823fe5ae8bSMike Christie 	if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
25833fe5ae8bSMike Christie 		goto unlock;
25843fe5ae8bSMike Christie 	conn = session->leadconn;
25853fe5ae8bSMike Christie 
25863fe5ae8bSMike Christie 	/* only have one tmf outstanding at a time */
2587ec29d0acSMike Christie 	if (session->tmf_state != TMF_INITIAL)
25883fe5ae8bSMike Christie 		goto unlock;
2589ec29d0acSMike Christie 	session->tmf_state = TMF_QUEUED;
25903fe5ae8bSMike Christie 
2591ec29d0acSMike Christie 	hdr = &session->tmhdr;
25923fe5ae8bSMike Christie 	iscsi_prep_tgt_reset_pdu(sc, hdr);
25933fe5ae8bSMike Christie 
25943fe5ae8bSMike Christie 	if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
25953fe5ae8bSMike Christie 				    session->tgt_reset_timeout)) {
25963fe5ae8bSMike Christie 		rc = FAILED;
25973fe5ae8bSMike Christie 		goto unlock;
25983fe5ae8bSMike Christie 	}
25993fe5ae8bSMike Christie 
2600ec29d0acSMike Christie 	switch (session->tmf_state) {
26013fe5ae8bSMike Christie 	case TMF_SUCCESS:
26023fe5ae8bSMike Christie 		break;
26033fe5ae8bSMike Christie 	case TMF_TIMEDOUT:
2604659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
2605df4da5cdSMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
26063fe5ae8bSMike Christie 		goto done;
26073fe5ae8bSMike Christie 	default:
2608ec29d0acSMike Christie 		session->tmf_state = TMF_INITIAL;
26093fe5ae8bSMike Christie 		goto unlock;
26103fe5ae8bSMike Christie 	}
26113fe5ae8bSMike Christie 
26123fe5ae8bSMike Christie 	rc = SUCCESS;
2613659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
26143fe5ae8bSMike Christie 
26153fe5ae8bSMike Christie 	iscsi_suspend_tx(conn);
26163fe5ae8bSMike Christie 
2617659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
26183fe5ae8bSMike Christie 	memset(hdr, 0, sizeof(*hdr));
26193fe5ae8bSMike Christie 	fail_scsi_tasks(conn, -1, DID_ERROR);
2620ec29d0acSMike Christie 	session->tmf_state = TMF_INITIAL;
2621659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
26223fe5ae8bSMike Christie 
26233fe5ae8bSMike Christie 	iscsi_start_tx(conn);
26243fe5ae8bSMike Christie 	goto done;
26253fe5ae8bSMike Christie 
26263fe5ae8bSMike Christie unlock:
2627659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
26283fe5ae8bSMike Christie done:
26293fe5ae8bSMike Christie 	ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
26303fe5ae8bSMike Christie 		     rc == SUCCESS ? "SUCCESS" : "FAILED");
26313fe5ae8bSMike Christie 	mutex_unlock(&session->eh_mutex);
2632309ce156SJayamohan Kallickal 	return rc;
2633309ce156SJayamohan Kallickal }
26343fe5ae8bSMike Christie 
2635309ce156SJayamohan Kallickal /**
2636309ce156SJayamohan Kallickal  * iscsi_eh_recover_target - reset target and possibly the session
2637309ce156SJayamohan Kallickal  * @sc: scsi command
2638309ce156SJayamohan Kallickal  *
2639309ce156SJayamohan Kallickal  * This will attempt to send a warm target reset. If that fails,
2640309ce156SJayamohan Kallickal  * we will escalate to ERL0 session recovery.
2641309ce156SJayamohan Kallickal  */
2642309ce156SJayamohan Kallickal int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2643309ce156SJayamohan Kallickal {
2644309ce156SJayamohan Kallickal 	int rc;
2645309ce156SJayamohan Kallickal 
2646309ce156SJayamohan Kallickal 	rc = iscsi_eh_target_reset(sc);
26473fe5ae8bSMike Christie 	if (rc == FAILED)
26483fe5ae8bSMike Christie 		rc = iscsi_eh_session_reset(sc);
26493fe5ae8bSMike Christie 	return rc;
26503fe5ae8bSMike Christie }
2651309ce156SJayamohan Kallickal EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
26523fe5ae8bSMike Christie 
26536320377fSOlaf Kirch /*
26546320377fSOlaf Kirch  * Pre-allocate a pool of @max items of @item_size. By default, the pool
26556320377fSOlaf Kirch  * should be accessed via kfifo_{get,put} on q->queue.
26566320377fSOlaf Kirch  * Optionally, the caller can obtain the array of object pointers
26576320377fSOlaf Kirch  * by passing in a non-NULL @items pointer
26586320377fSOlaf Kirch  */
26597996a778SMike Christie int
26606320377fSOlaf Kirch iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
26617996a778SMike Christie {
26626320377fSOlaf Kirch 	int i, num_arrays = 1;
26637996a778SMike Christie 
26646320377fSOlaf Kirch 	memset(q, 0, sizeof(*q));
26657996a778SMike Christie 
26667996a778SMike Christie 	q->max = max;
26676320377fSOlaf Kirch 
26686320377fSOlaf Kirch 	/* If the user passed an items pointer, he wants a copy of
26696320377fSOlaf Kirch 	 * the array. */
26706320377fSOlaf Kirch 	if (items)
26716320377fSOlaf Kirch 		num_arrays++;
2672778e1cddSKees Cook 	q->pool = kvcalloc(num_arrays * max, sizeof(void *), GFP_KERNEL);
26736320377fSOlaf Kirch 	if (q->pool == NULL)
2674f474a37bSJean Delvare 		return -ENOMEM;
26757996a778SMike Christie 
2676c1e13f25SStefani Seibold 	kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
26777996a778SMike Christie 
26787996a778SMike Christie 	for (i = 0; i < max; i++) {
26796320377fSOlaf Kirch 		q->pool[i] = kzalloc(item_size, GFP_KERNEL);
26807996a778SMike Christie 		if (q->pool[i] == NULL) {
26816320377fSOlaf Kirch 			q->max = i;
26826320377fSOlaf Kirch 			goto enomem;
26837996a778SMike Christie 		}
26847acd72ebSStefani Seibold 		kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
26857996a778SMike Christie 	}
26866320377fSOlaf Kirch 
26876320377fSOlaf Kirch 	if (items) {
26886320377fSOlaf Kirch 		*items = q->pool + max;
26896320377fSOlaf Kirch 		memcpy(*items, q->pool, max * sizeof(void *));
26906320377fSOlaf Kirch 	}
26916320377fSOlaf Kirch 
26927996a778SMike Christie 	return 0;
26936320377fSOlaf Kirch 
26946320377fSOlaf Kirch enomem:
26956320377fSOlaf Kirch 	iscsi_pool_free(q);
26966320377fSOlaf Kirch 	return -ENOMEM;
26977996a778SMike Christie }
26987996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_init);
26997996a778SMike Christie 
27006320377fSOlaf Kirch void iscsi_pool_free(struct iscsi_pool *q)
27017996a778SMike Christie {
27027996a778SMike Christie 	int i;
27037996a778SMike Christie 
27047996a778SMike Christie 	for (i = 0; i < q->max; i++)
27056320377fSOlaf Kirch 		kfree(q->pool[i]);
2706bfcc62edSKyle Fortin 	kvfree(q->pool);
27077996a778SMike Christie }
27087996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_free);
27097996a778SMike Christie 
2710b4046922SMike Christie int iscsi_host_get_max_scsi_cmds(struct Scsi_Host *shost,
2711b4046922SMike Christie 				 uint16_t requested_cmds_max)
2712b4046922SMike Christie {
2713b4046922SMike Christie 	int scsi_cmds, total_cmds = requested_cmds_max;
2714b4046922SMike Christie 
2715b4046922SMike Christie check:
2716b4046922SMike Christie 	if (!total_cmds)
2717b4046922SMike Christie 		total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2718b4046922SMike Christie 	/*
2719b4046922SMike Christie 	 * The iscsi layer needs some tasks for nop handling and tmfs,
2720b4046922SMike Christie 	 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2721b4046922SMike Christie 	 * + 1 command for scsi IO.
2722b4046922SMike Christie 	 */
2723b4046922SMike Christie 	if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2724b4046922SMike Christie 		printk(KERN_ERR "iscsi: invalid max cmds of %d. Must be a power of two that is at least %d.\n",
2725b4046922SMike Christie 		       total_cmds, ISCSI_TOTAL_CMDS_MIN);
2726b4046922SMike Christie 		return -EINVAL;
2727b4046922SMike Christie 	}
2728b4046922SMike Christie 
2729b4046922SMike Christie 	if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2730b4046922SMike Christie 		printk(KERN_INFO "iscsi: invalid max cmds of %d. Must be a power of 2 less than or equal to %d. Using %d.\n",
2731b4046922SMike Christie 		       requested_cmds_max, ISCSI_TOTAL_CMDS_MAX,
2732b4046922SMike Christie 		       ISCSI_TOTAL_CMDS_MAX);
2733b4046922SMike Christie 		total_cmds = ISCSI_TOTAL_CMDS_MAX;
2734b4046922SMike Christie 	}
2735b4046922SMike Christie 
2736b4046922SMike Christie 	if (!is_power_of_2(total_cmds)) {
2737b4046922SMike Christie 		total_cmds = rounddown_pow_of_two(total_cmds);
2738b4046922SMike Christie 		if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2739b4046922SMike Christie 			printk(KERN_ERR "iscsi: invalid max cmds of %d. Must be a power of 2 greater than %d.\n", requested_cmds_max, ISCSI_TOTAL_CMDS_MIN);
2740b4046922SMike Christie 			return -EINVAL;
2741b4046922SMike Christie 		}
2742b4046922SMike Christie 
2743b4046922SMike Christie 		printk(KERN_INFO "iscsi: invalid max cmds %d. Must be a power of 2. Rounding max cmds down to %d.\n",
2744b4046922SMike Christie 		       requested_cmds_max, total_cmds);
2745b4046922SMike Christie 	}
2746b4046922SMike Christie 
2747b4046922SMike Christie 	scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2748b4046922SMike Christie 	if (shost->can_queue && scsi_cmds > shost->can_queue) {
2749b4046922SMike Christie 		total_cmds = shost->can_queue;
2750b4046922SMike Christie 
2751b4046922SMike Christie 		printk(KERN_INFO "iscsi: requested max cmds %u is higher than driver limit. Using driver limit %u\n",
2752b4046922SMike Christie 		       requested_cmds_max, shost->can_queue);
2753b4046922SMike Christie 		goto check;
2754b4046922SMike Christie 	}
2755b4046922SMike Christie 
2756b4046922SMike Christie 	return scsi_cmds;
2757b4046922SMike Christie }
2758b4046922SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_get_max_scsi_cmds);
2759b4046922SMike Christie 
2760a4804cd6SMike Christie /**
2761a4804cd6SMike Christie  * iscsi_host_add - add host to system
2762a4804cd6SMike Christie  * @shost: scsi host
2763a4804cd6SMike Christie  * @pdev: parent device
2764a4804cd6SMike Christie  *
2765a4804cd6SMike Christie  * This should be called by partial offload and software iscsi drivers
2766a4804cd6SMike Christie  * to add a host to the system.
2767a4804cd6SMike Christie  */
2768a4804cd6SMike Christie int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
27697996a778SMike Christie {
27708e9a20ceSMike Christie 	if (!shost->can_queue)
27718e9a20ceSMike Christie 		shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
27728e9a20ceSMike Christie 
27734d108350SMike Christie 	if (!shost->cmd_per_lun)
27744d108350SMike Christie 		shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
27754d108350SMike Christie 
2776a4804cd6SMike Christie 	return scsi_add_host(shost, pdev);
2777a4804cd6SMike Christie }
2778a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_add);
2779a4804cd6SMike Christie 
2780a4804cd6SMike Christie /**
2781a4804cd6SMike Christie  * iscsi_host_alloc - allocate a host and driver data
2782a4804cd6SMike Christie  * @sht: scsi host template
2783a4804cd6SMike Christie  * @dd_data_size: driver host data size
278432ae763eSMike Christie  * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2785a4804cd6SMike Christie  *
2786a4804cd6SMike Christie  * This should be called by partial offload and software iscsi drivers.
2787a4804cd6SMike Christie  * To access the driver specific memory use the iscsi_host_priv() macro.
2788a4804cd6SMike Christie  */
2789a4804cd6SMike Christie struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
27904d108350SMike Christie 				   int dd_data_size, bool xmit_can_sleep)
2791a4804cd6SMike Christie {
2792a4804cd6SMike Christie 	struct Scsi_Host *shost;
2793e5bd7b54SMike Christie 	struct iscsi_host *ihost;
2794a4804cd6SMike Christie 
2795a4804cd6SMike Christie 	shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2796a4804cd6SMike Christie 	if (!shost)
2797a4804cd6SMike Christie 		return NULL;
2798e5bd7b54SMike Christie 	ihost = shost_priv(shost);
279932ae763eSMike Christie 
280032ae763eSMike Christie 	if (xmit_can_sleep) {
280132ae763eSMike Christie 		snprintf(ihost->workq_name, sizeof(ihost->workq_name),
280232ae763eSMike Christie 			"iscsi_q_%d", shost->host_no);
28033ce41966SBob Liu 		ihost->workq = alloc_workqueue("%s",
28043ce41966SBob Liu 			WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND,
28051a982620SBob Liu 			1, ihost->workq_name);
280632ae763eSMike Christie 		if (!ihost->workq)
280732ae763eSMike Christie 			goto free_host;
280832ae763eSMike Christie 	}
280932ae763eSMike Christie 
2810e5bd7b54SMike Christie 	spin_lock_init(&ihost->lock);
2811e5bd7b54SMike Christie 	ihost->state = ISCSI_HOST_SETUP;
2812e5bd7b54SMike Christie 	ihost->num_sessions = 0;
2813e5bd7b54SMike Christie 	init_waitqueue_head(&ihost->session_removal_wq);
2814a4804cd6SMike Christie 	return shost;
281532ae763eSMike Christie 
281632ae763eSMike Christie free_host:
281732ae763eSMike Christie 	scsi_host_put(shost);
281832ae763eSMike Christie 	return NULL;
281975613521SMike Christie }
2820a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_alloc);
282175613521SMike Christie 
2822e5bd7b54SMike Christie static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2823e5bd7b54SMike Christie {
282440a06e75SMike Christie 	iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2825e5bd7b54SMike Christie }
2826e5bd7b54SMike Christie 
2827a4804cd6SMike Christie /**
2828a4804cd6SMike Christie  * iscsi_host_remove - remove host and sessions
2829a4804cd6SMike Christie  * @shost: scsi host
2830a4804cd6SMike Christie  *
2831e5bd7b54SMike Christie  * If there are any sessions left, this will initiate the removal and wait
2832e5bd7b54SMike Christie  * for the completion.
2833a4804cd6SMike Christie  */
2834a4804cd6SMike Christie void iscsi_host_remove(struct Scsi_Host *shost)
2835a4804cd6SMike Christie {
2836e5bd7b54SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
2837e5bd7b54SMike Christie 	unsigned long flags;
2838e5bd7b54SMike Christie 
2839e5bd7b54SMike Christie 	spin_lock_irqsave(&ihost->lock, flags);
2840e5bd7b54SMike Christie 	ihost->state = ISCSI_HOST_REMOVED;
2841e5bd7b54SMike Christie 	spin_unlock_irqrestore(&ihost->lock, flags);
2842e5bd7b54SMike Christie 
2843e5bd7b54SMike Christie 	iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2844e5bd7b54SMike Christie 	wait_event_interruptible(ihost->session_removal_wq,
2845e5bd7b54SMike Christie 				 ihost->num_sessions == 0);
2846e5bd7b54SMike Christie 	if (signal_pending(current))
2847e5bd7b54SMike Christie 		flush_signals(current);
2848e5bd7b54SMike Christie 
2849a4804cd6SMike Christie 	scsi_remove_host(shost);
2850a4804cd6SMike Christie }
2851a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_remove);
2852a4804cd6SMike Christie 
2853a4804cd6SMike Christie void iscsi_host_free(struct Scsi_Host *shost)
285475613521SMike Christie {
285575613521SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
285675613521SMike Christie 
2857c435f0a9SMike Christie 	if (ihost->workq)
2858c435f0a9SMike Christie 		destroy_workqueue(ihost->workq);
2859c435f0a9SMike Christie 
286075613521SMike Christie 	kfree(ihost->netdev);
286175613521SMike Christie 	kfree(ihost->hwaddress);
286275613521SMike Christie 	kfree(ihost->initiatorname);
2863a4804cd6SMike Christie 	scsi_host_put(shost);
286475613521SMike Christie }
2865a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_free);
286675613521SMike Christie 
2867e5bd7b54SMike Christie static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2868e5bd7b54SMike Christie {
2869e5bd7b54SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
2870e5bd7b54SMike Christie 	unsigned long flags;
2871e5bd7b54SMike Christie 
2872e5bd7b54SMike Christie 	shost = scsi_host_get(shost);
2873e5bd7b54SMike Christie 	if (!shost) {
2874e5bd7b54SMike Christie 		printk(KERN_ERR "Invalid state. Cannot notify host removal "
2875e5bd7b54SMike Christie 		      "of session teardown event because host already "
2876e5bd7b54SMike Christie 		      "removed.\n");
2877e5bd7b54SMike Christie 		return;
2878e5bd7b54SMike Christie 	}
2879e5bd7b54SMike Christie 
2880e5bd7b54SMike Christie 	spin_lock_irqsave(&ihost->lock, flags);
2881e5bd7b54SMike Christie 	ihost->num_sessions--;
2882e5bd7b54SMike Christie 	if (ihost->num_sessions == 0)
2883e5bd7b54SMike Christie 		wake_up(&ihost->session_removal_wq);
2884e5bd7b54SMike Christie 	spin_unlock_irqrestore(&ihost->lock, flags);
2885e5bd7b54SMike Christie 	scsi_host_put(shost);
2886e5bd7b54SMike Christie }
2887e5bd7b54SMike Christie 
288875613521SMike Christie /**
288975613521SMike Christie  * iscsi_session_setup - create iscsi cls session and host and session
289075613521SMike Christie  * @iscsit: iscsi transport template
289175613521SMike Christie  * @shost: scsi host
289275613521SMike Christie  * @cmds_max: session can queue
2893ccd4a430SRandy Dunlap  * @dd_size: private driver data size, added to session allocation size
28949c19a7d0SMike Christie  * @cmd_task_size: LLD task private data size
289575613521SMike Christie  * @initial_cmdsn: initial CmdSN
2896ccd4a430SRandy Dunlap  * @id: target ID to add to this session
289775613521SMike Christie  *
289875613521SMike Christie  * This can be used by software iscsi_transports that allocate
289975613521SMike Christie  * a session per scsi host.
29003cf7b233SMike Christie  *
29013cf7b233SMike Christie  * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
29023cf7b233SMike Christie  * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
29033cf7b233SMike Christie  * for nop handling and login/logout requests.
290475613521SMike Christie  */
290575613521SMike Christie struct iscsi_cls_session *
290675613521SMike Christie iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2907b8b9e1b8SJayamohan Kallickal 		    uint16_t cmds_max, int dd_size, int cmd_task_size,
29087970634bSMike Christie 		    uint32_t initial_cmdsn, unsigned int id)
290975613521SMike Christie {
2910e5bd7b54SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
291175613521SMike Christie 	struct iscsi_session *session;
291275613521SMike Christie 	struct iscsi_cls_session *cls_session;
2913b4046922SMike Christie 	int cmd_i, scsi_cmds;
2914e5bd7b54SMike Christie 	unsigned long flags;
2915e5bd7b54SMike Christie 
2916e5bd7b54SMike Christie 	spin_lock_irqsave(&ihost->lock, flags);
2917e5bd7b54SMike Christie 	if (ihost->state == ISCSI_HOST_REMOVED) {
2918e5bd7b54SMike Christie 		spin_unlock_irqrestore(&ihost->lock, flags);
2919e5bd7b54SMike Christie 		return NULL;
2920e5bd7b54SMike Christie 	}
2921e5bd7b54SMike Christie 	ihost->num_sessions++;
2922e5bd7b54SMike Christie 	spin_unlock_irqrestore(&ihost->lock, flags);
29238e9a20ceSMike Christie 
2924b4046922SMike Christie 	scsi_cmds = iscsi_host_get_max_scsi_cmds(shost, cmds_max);
2925b4046922SMike Christie 	if (scsi_cmds < 0)
2926e5bd7b54SMike Christie 		goto dec_session_count;
29271548271eSMike Christie 
29285d91e209SMike Christie 	cls_session = iscsi_alloc_session(shost, iscsit,
2929b8b9e1b8SJayamohan Kallickal 					  sizeof(struct iscsi_session) +
2930b8b9e1b8SJayamohan Kallickal 					  dd_size);
293175613521SMike Christie 	if (!cls_session)
2932e5bd7b54SMike Christie 		goto dec_session_count;
293375613521SMike Christie 	session = cls_session->dd_data;
293475613521SMike Christie 	session->cls_session = cls_session;
29357996a778SMike Christie 	session->host = shost;
29367996a778SMike Christie 	session->state = ISCSI_STATE_FREE;
2937f6d5180cSMike Christie 	session->fast_abort = 1;
29383fe5ae8bSMike Christie 	session->tgt_reset_timeout = 30;
29394cd49ea1SMike Christie 	session->lu_reset_timeout = 15;
29404cd49ea1SMike Christie 	session->abort_timeout = 10;
29413cf7b233SMike Christie 	session->scsi_cmds_max = scsi_cmds;
2942b4046922SMike Christie 	session->cmds_max = scsi_cmds + ISCSI_MGMT_CMDS_MAX;
2943e0726407SMike Christie 	session->queued_cmdsn = session->cmdsn = initial_cmdsn;
29447996a778SMike Christie 	session->exp_cmdsn = initial_cmdsn + 1;
29457996a778SMike Christie 	session->max_cmdsn = initial_cmdsn + 1;
29467996a778SMike Christie 	session->max_r2t = 1;
29477996a778SMike Christie 	session->tt = iscsit;
2948b8b9e1b8SJayamohan Kallickal 	session->dd_data = cls_session->dd_data + sizeof(*session);
2949659743b0SShlomo Pongratz 
2950ec29d0acSMike Christie 	session->tmf_state = TMF_INITIAL;
2951ec29d0acSMike Christie 	timer_setup(&session->tmf_timer, iscsi_tmf_timedout, 0);
29526724add1SMike Christie 	mutex_init(&session->eh_mutex);
2953e018f03dSDing Hui 	init_waitqueue_head(&session->ehwait);
2954ec29d0acSMike Christie 
2955659743b0SShlomo Pongratz 	spin_lock_init(&session->frwd_lock);
2956659743b0SShlomo Pongratz 	spin_lock_init(&session->back_lock);
29577996a778SMike Christie 
29587996a778SMike Christie 	/* initialize SCSI PDU commands pool */
29597996a778SMike Christie 	if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
29607996a778SMike Christie 			    (void***)&session->cmds,
29619c19a7d0SMike Christie 			    cmd_task_size + sizeof(struct iscsi_task)))
29627996a778SMike Christie 		goto cmdpool_alloc_fail;
29637996a778SMike Christie 
29647996a778SMike Christie 	/* pre-format cmds pool with ITT */
29657996a778SMike Christie 	for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
29669c19a7d0SMike Christie 		struct iscsi_task *task = session->cmds[cmd_i];
29677996a778SMike Christie 
29689c19a7d0SMike Christie 		if (cmd_task_size)
29699c19a7d0SMike Christie 			task->dd_data = &task[1];
29709c19a7d0SMike Christie 		task->itt = cmd_i;
29713bbaaad9SMike Christie 		task->state = ISCSI_TASK_FREE;
29729c19a7d0SMike Christie 		INIT_LIST_HEAD(&task->running);
29737996a778SMike Christie 	}
29747996a778SMike Christie 
2975f53a88daSMike Christie 	if (!try_module_get(iscsit->owner))
297675613521SMike Christie 		goto module_get_fail;
297775613521SMike Christie 
29787970634bSMike Christie 	if (iscsi_add_session(cls_session, id))
2979f53a88daSMike Christie 		goto cls_session_fail;
2980e5bd7b54SMike Christie 
29817996a778SMike Christie 	return cls_session;
29827996a778SMike Christie 
29837996a778SMike Christie cls_session_fail:
298475613521SMike Christie 	module_put(iscsit->owner);
298575613521SMike Christie module_get_fail:
29866320377fSOlaf Kirch 	iscsi_pool_free(&session->cmdpool);
29877996a778SMike Christie cmdpool_alloc_fail:
298875613521SMike Christie 	iscsi_free_session(cls_session);
2989e5bd7b54SMike Christie dec_session_count:
2990e5bd7b54SMike Christie 	iscsi_host_dec_session_cnt(shost);
29917996a778SMike Christie 	return NULL;
29927996a778SMike Christie }
29937996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_setup);
29947996a778SMike Christie 
29957996a778SMike Christie /**
29967996a778SMike Christie  * iscsi_session_teardown - destroy session, host, and cls_session
299775613521SMike Christie  * @cls_session: iscsi session
299875613521SMike Christie  */
29997996a778SMike Christie void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
30007996a778SMike Christie {
300175613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
300263f75cc8SMike Christie 	struct module *owner = cls_session->transport->owner;
3003e5bd7b54SMike Christie 	struct Scsi_Host *shost = session->host;
30047996a778SMike Christie 
30059e10b512SKhazhismel Kumykov 	iscsi_remove_session(cls_session);
30069e10b512SKhazhismel Kumykov 
3007a1f3486bSMike Christie 	iscsi_pool_free(&session->cmdpool);
3008b2c64167SMike Christie 	kfree(session->password);
3009b2c64167SMike Christie 	kfree(session->password_in);
3010b2c64167SMike Christie 	kfree(session->username);
3011b2c64167SMike Christie 	kfree(session->username_in);
3012f3ff0c36SMike Christie 	kfree(session->targetname);
30133c5c4801SVikas Chaudhary 	kfree(session->targetalias);
301488dfd340SMike Christie 	kfree(session->initiatorname);
30153b9373e9SEddie Wai 	kfree(session->boot_root);
30163b9373e9SEddie Wai 	kfree(session->boot_nic);
30173b9373e9SEddie Wai 	kfree(session->boot_target);
301888dfd340SMike Christie 	kfree(session->ifacename);
3019f8525eb4SAdheer Chandravanshi 	kfree(session->portal_type);
3020f8525eb4SAdheer Chandravanshi 	kfree(session->discovery_parent_type);
3021f3ff0c36SMike Christie 
30229e10b512SKhazhismel Kumykov 	iscsi_free_session(cls_session);
30239e10b512SKhazhismel Kumykov 
3024e5bd7b54SMike Christie 	iscsi_host_dec_session_cnt(shost);
302563f75cc8SMike Christie 	module_put(owner);
30267996a778SMike Christie }
30277996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_teardown);
30287996a778SMike Christie 
30297996a778SMike Christie /**
30307996a778SMike Christie  * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
30317996a778SMike Christie  * @cls_session: iscsi_cls_session
30325d91e209SMike Christie  * @dd_size: private driver data size
30337996a778SMike Christie  * @conn_idx: cid
30345d91e209SMike Christie  */
30357996a778SMike Christie struct iscsi_cls_conn *
30365d91e209SMike Christie iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
30375d91e209SMike Christie 		 uint32_t conn_idx)
30387996a778SMike Christie {
303975613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
30407996a778SMike Christie 	struct iscsi_conn *conn;
30417996a778SMike Christie 	struct iscsi_cls_conn *cls_conn;
3042d36ab6f3SMike Christie 	char *data;
30437996a778SMike Christie 
30445d91e209SMike Christie 	cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
30455d91e209SMike Christie 				     conn_idx);
30467996a778SMike Christie 	if (!cls_conn)
30477996a778SMike Christie 		return NULL;
30487996a778SMike Christie 	conn = cls_conn->dd_data;
30495d91e209SMike Christie 	memset(conn, 0, sizeof(*conn) + dd_size);
30507996a778SMike Christie 
30515d91e209SMike Christie 	conn->dd_data = cls_conn->dd_data + sizeof(*conn);
30527996a778SMike Christie 	conn->session = session;
30537996a778SMike Christie 	conn->cls_conn = cls_conn;
30547996a778SMike Christie 	conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
30557996a778SMike Christie 	conn->id = conn_idx;
30567996a778SMike Christie 	conn->exp_statsn = 0;
3057f6d5180cSMike Christie 
30582c4b9637SKees Cook 	timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0);
3059f6d5180cSMike Christie 
3060843c0a8aSMike Christie 	INIT_LIST_HEAD(&conn->mgmtqueue);
30613bbaaad9SMike Christie 	INIT_LIST_HEAD(&conn->cmdqueue);
3062843c0a8aSMike Christie 	INIT_LIST_HEAD(&conn->requeue);
3063c4028958SDavid Howells 	INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
30647996a778SMike Christie 
30659c19a7d0SMike Christie 	/* allocate login_task used for the login/text sequences */
3066659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
30677acd72ebSStefani Seibold 	if (!kfifo_out(&session->cmdpool.queue,
30689c19a7d0SMike Christie                          (void*)&conn->login_task,
30697996a778SMike Christie 			 sizeof(void*))) {
3070659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
30719c19a7d0SMike Christie 		goto login_task_alloc_fail;
30727996a778SMike Christie 	}
3073659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
30747996a778SMike Christie 
3075cfeb2cf9SMike Christie 	data = (char *) __get_free_pages(GFP_KERNEL,
3076cfeb2cf9SMike Christie 					 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
3077d36ab6f3SMike Christie 	if (!data)
30789c19a7d0SMike Christie 		goto login_task_data_alloc_fail;
30799c19a7d0SMike Christie 	conn->login_task->data = conn->data = data;
3080d36ab6f3SMike Christie 
30817996a778SMike Christie 	return cls_conn;
30827996a778SMike Christie 
30839c19a7d0SMike Christie login_task_data_alloc_fail:
30847acd72ebSStefani Seibold 	kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
3085d36ab6f3SMike Christie 		    sizeof(void*));
30869c19a7d0SMike Christie login_task_alloc_fail:
30877996a778SMike Christie 	iscsi_destroy_conn(cls_conn);
30887996a778SMike Christie 	return NULL;
30897996a778SMike Christie }
30907996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_setup);
30917996a778SMike Christie 
30927996a778SMike Christie /**
30937996a778SMike Christie  * iscsi_conn_teardown - teardown iscsi connection
3094ccd4a430SRandy Dunlap  * @cls_conn: iscsi class connection
30957996a778SMike Christie  *
30967996a778SMike Christie  * TODO: we may need to make this into a two step process
30977996a778SMike Christie  * like scsi-mls remove + put host
30987996a778SMike Christie  */
30997996a778SMike Christie void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
31007996a778SMike Christie {
31017996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
31027996a778SMike Christie 	struct iscsi_session *session = conn->session;
31031b8d0300SLixiaokeng 	char *tmp_persistent_address = conn->persistent_address;
31041b8d0300SLixiaokeng 	char *tmp_local_ipaddr = conn->local_ipaddr;
31057996a778SMike Christie 
3106f6d5180cSMike Christie 	del_timer_sync(&conn->transport_timer);
3107f6d5180cSMike Christie 
3108660d0831SJohn Soni Jose 	mutex_lock(&session->eh_mutex);
3109659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
31107996a778SMike Christie 	conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
31117996a778SMike Christie 	if (session->leadconn == conn) {
31127996a778SMike Christie 		/*
31137996a778SMike Christie 		 * leading connection? then give up on recovery.
31147996a778SMike Christie 		 */
31157996a778SMike Christie 		session->state = ISCSI_STATE_TERMINATE;
3116ec29d0acSMike Christie 		wake_up(&session->ehwait);
31177996a778SMike Christie 	}
3118659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
31197996a778SMike Christie 
3120779ea120SMike Christie 	/* flush queued up work because we free the connection below */
3121843c0a8aSMike Christie 	iscsi_suspend_tx(conn);
3122779ea120SMike Christie 
3123659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
3124cfeb2cf9SMike Christie 	free_pages((unsigned long) conn->data,
3125cfeb2cf9SMike Christie 		   get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
3126659743b0SShlomo Pongratz 	/* regular RX path uses back_lock */
3127659743b0SShlomo Pongratz 	spin_lock_bh(&session->back_lock);
31287acd72ebSStefani Seibold 	kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
31297996a778SMike Christie 		    sizeof(void*));
3130659743b0SShlomo Pongratz 	spin_unlock_bh(&session->back_lock);
3131e0726407SMike Christie 	if (session->leadconn == conn)
31327996a778SMike Christie 		session->leadconn = NULL;
3133659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
3134660d0831SJohn Soni Jose 	mutex_unlock(&session->eh_mutex);
31357996a778SMike Christie 
31367996a778SMike Christie 	iscsi_destroy_conn(cls_conn);
31371b8d0300SLixiaokeng 	kfree(tmp_persistent_address);
31381b8d0300SLixiaokeng 	kfree(tmp_local_ipaddr);
31397996a778SMike Christie }
31407996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
31417996a778SMike Christie 
31427996a778SMike Christie int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
31437996a778SMike Christie {
31447996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
31457996a778SMike Christie 	struct iscsi_session *session = conn->session;
31467996a778SMike Christie 
3147ffd0436eSMike Christie 	if (!session) {
3148322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
3149322d739dSMike Christie 				  "can't start unbound connection\n");
31507996a778SMike Christie 		return -EPERM;
31517996a778SMike Christie 	}
31527996a778SMike Christie 
3153db98ccdeSMike Christie 	if ((session->imm_data_en || !session->initial_r2t_en) &&
3154db98ccdeSMike Christie 	     session->first_burst > session->max_burst) {
3155322d739dSMike Christie 		iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
3156ffd0436eSMike Christie 				  "first_burst %d max_burst %d\n",
3157ffd0436eSMike Christie 				  session->first_burst, session->max_burst);
3158ffd0436eSMike Christie 		return -EINVAL;
3159ffd0436eSMike Christie 	}
3160ffd0436eSMike Christie 
3161f6d5180cSMike Christie 	if (conn->ping_timeout && !conn->recv_timeout) {
3162322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
3163322d739dSMike Christie 				  "zero. Using 5 seconds\n.");
3164f6d5180cSMike Christie 		conn->recv_timeout = 5;
3165f6d5180cSMike Christie 	}
3166f6d5180cSMike Christie 
3167f6d5180cSMike Christie 	if (conn->recv_timeout && !conn->ping_timeout) {
3168322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3169322d739dSMike Christie 				  "zero. Using 5 seconds.\n");
3170f6d5180cSMike Christie 		conn->ping_timeout = 5;
3171f6d5180cSMike Christie 	}
3172f6d5180cSMike Christie 
3173659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
31747996a778SMike Christie 	conn->c_stage = ISCSI_CONN_STARTED;
31757996a778SMike Christie 	session->state = ISCSI_STATE_LOGGED_IN;
3176e0726407SMike Christie 	session->queued_cmdsn = session->cmdsn;
31777996a778SMike Christie 
3178f6d5180cSMike Christie 	conn->last_recv = jiffies;
3179f6d5180cSMike Christie 	conn->last_ping = jiffies;
3180f6d5180cSMike Christie 	if (conn->recv_timeout && conn->ping_timeout)
3181f6d5180cSMike Christie 		mod_timer(&conn->transport_timer,
3182f6d5180cSMike Christie 			  jiffies + (conn->recv_timeout * HZ));
3183f6d5180cSMike Christie 
31847996a778SMike Christie 	switch(conn->stop_stage) {
31857996a778SMike Christie 	case STOP_CONN_RECOVER:
31867996a778SMike Christie 		/*
31877996a778SMike Christie 		 * unblock eh_abort() if it is blocked. re-try all
31887996a778SMike Christie 		 * commands after successful recovery
31897996a778SMike Christie 		 */
31907996a778SMike Christie 		conn->stop_stage = 0;
3191ec29d0acSMike Christie 		session->tmf_state = TMF_INITIAL;
31927996a778SMike Christie 		session->age++;
31938b1d0343SMike Christie 		if (session->age == 16)
31948b1d0343SMike Christie 			session->age = 0;
31956eabafbeSMike Christie 		break;
31967996a778SMike Christie 	case STOP_CONN_TERM:
31977996a778SMike Christie 		conn->stop_stage = 0;
31987996a778SMike Christie 		break;
31997996a778SMike Christie 	default:
32007996a778SMike Christie 		break;
32017996a778SMike Christie 	}
3202659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
32037996a778SMike Christie 
320475613521SMike Christie 	iscsi_unblock_session(session->cls_session);
3205ec29d0acSMike Christie 	wake_up(&session->ehwait);
32067996a778SMike Christie 	return 0;
32077996a778SMike Christie }
32087996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_start);
32097996a778SMike Christie 
32107996a778SMike Christie static void
32113bbaaad9SMike Christie fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
32127996a778SMike Christie {
32133bbaaad9SMike Christie 	struct iscsi_task *task;
3214b3cd5050SMike Christie 	int i, state;
32157996a778SMike Christie 
32163bbaaad9SMike Christie 	for (i = 0; i < conn->session->cmds_max; i++) {
32173bbaaad9SMike Christie 		task = conn->session->cmds[i];
32183bbaaad9SMike Christie 		if (task->sc)
32193bbaaad9SMike Christie 			continue;
32203bbaaad9SMike Christie 
32213bbaaad9SMike Christie 		if (task->state == ISCSI_TASK_FREE)
32223bbaaad9SMike Christie 			continue;
32233bbaaad9SMike Christie 
32243bbaaad9SMike Christie 		ISCSI_DBG_SESSION(conn->session,
32253bbaaad9SMike Christie 				  "failing mgmt itt 0x%x state %d\n",
32263bbaaad9SMike Christie 				  task->itt, task->state);
32275923d64bSMike Christie 
32285923d64bSMike Christie 		spin_lock_bh(&session->back_lock);
32295923d64bSMike Christie 		if (cleanup_queued_task(task)) {
32305923d64bSMike Christie 			spin_unlock_bh(&session->back_lock);
32315923d64bSMike Christie 			continue;
32325923d64bSMike Christie 		}
32335923d64bSMike Christie 
3234b3cd5050SMike Christie 		state = ISCSI_TASK_ABRT_SESS_RECOV;
3235b3cd5050SMike Christie 		if (task->state == ISCSI_TASK_PENDING)
3236b3cd5050SMike Christie 			state = ISCSI_TASK_COMPLETED;
3237b3cd5050SMike Christie 		iscsi_complete_task(task, state);
3238a656183eSLee Duncan 		spin_unlock_bh(&session->back_lock);
32397996a778SMike Christie 	}
32407996a778SMike Christie }
32417996a778SMike Christie 
32420dcf8febSMike Christie void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
32437996a778SMike Christie {
32440dcf8febSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
32450dcf8febSMike Christie 	struct iscsi_session *session = conn->session;
3246ed2abc7fSMike Christie 	int old_stop_stage;
3247ed2abc7fSMike Christie 
32486724add1SMike Christie 	mutex_lock(&session->eh_mutex);
3249659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
3250ed2abc7fSMike Christie 	if (conn->stop_stage == STOP_CONN_TERM) {
3251659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
32526724add1SMike Christie 		mutex_unlock(&session->eh_mutex);
32536724add1SMike Christie 		return;
32546724add1SMike Christie 	}
32556724add1SMike Christie 
32566724add1SMike Christie 	/*
3257ed2abc7fSMike Christie 	 * When this is called for the in_login state, we only want to clean
32589c19a7d0SMike Christie 	 * up the login task and connection. We do not need to block and set
325967a61114SMike Christie 	 * the recovery state again
3260ed2abc7fSMike Christie 	 */
326167a61114SMike Christie 	if (flag == STOP_CONN_TERM)
326267a61114SMike Christie 		session->state = ISCSI_STATE_TERMINATE;
326367a61114SMike Christie 	else if (conn->stop_stage != STOP_CONN_RECOVER)
326467a61114SMike Christie 		session->state = ISCSI_STATE_IN_RECOVERY;
32654ae0a6c1SMike Christie 
32664ae0a6c1SMike Christie 	old_stop_stage = conn->stop_stage;
32674ae0a6c1SMike Christie 	conn->stop_stage = flag;
3268659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
3269ed2abc7fSMike Christie 
327026013ad4SMike Christie 	del_timer_sync(&conn->transport_timer);
327126013ad4SMike Christie 	iscsi_suspend_tx(conn);
327226013ad4SMike Christie 
3273659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
327467a61114SMike Christie 	conn->c_stage = ISCSI_CONN_STOPPED;
3275659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
32766724add1SMike Christie 
32777996a778SMike Christie 	/*
32787996a778SMike Christie 	 * for connection level recovery we should not calculate
32797996a778SMike Christie 	 * header digest. conn->hdr_size used for optimization
32807996a778SMike Christie 	 * in hdr_extract() and will be re-negotiated at
32817996a778SMike Christie 	 * set_param() time.
32827996a778SMike Christie 	 */
32837996a778SMike Christie 	if (flag == STOP_CONN_RECOVER) {
32847996a778SMike Christie 		conn->hdrdgst_en = 0;
32857996a778SMike Christie 		conn->datadgst_en = 0;
3286656cffc9SMike Christie 		if (session->state == ISCSI_STATE_IN_RECOVERY &&
328767a61114SMike Christie 		    old_stop_stage != STOP_CONN_RECOVER) {
32881b2c7af8SMike Christie 			ISCSI_DBG_SESSION(session, "blocking session\n");
328975613521SMike Christie 			iscsi_block_session(session->cls_session);
32907996a778SMike Christie 		}
329167a61114SMike Christie 	}
3292656cffc9SMike Christie 
3293656cffc9SMike Christie 	/*
3294656cffc9SMike Christie 	 * flush queues.
3295656cffc9SMike Christie 	 */
3296659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
32973bbaaad9SMike Christie 	fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
32983bbaaad9SMike Christie 	fail_mgmt_tasks(session, conn);
3299ec29d0acSMike Christie 	memset(&session->tmhdr, 0, sizeof(session->tmhdr));
3300659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
33016724add1SMike Christie 	mutex_unlock(&session->eh_mutex);
33027996a778SMike Christie }
33037996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_stop);
33047996a778SMike Christie 
33057996a778SMike Christie int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
33067996a778SMike Christie 		    struct iscsi_cls_conn *cls_conn, int is_leading)
33077996a778SMike Christie {
330875613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
330998644047SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
33107996a778SMike Christie 
3311659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
33127996a778SMike Christie 	if (is_leading)
33137996a778SMike Christie 		session->leadconn = conn;
3314659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
33157996a778SMike Christie 
33167996a778SMike Christie 	/*
3317c8447e4cSMike Christie 	 * The target could have reduced it's window size between logins, so
3318c8447e4cSMike Christie 	 * we have to reset max/exp cmdsn so we can see the new values.
3319c8447e4cSMike Christie 	 */
3320c8447e4cSMike Christie 	spin_lock_bh(&session->back_lock);
3321c8447e4cSMike Christie 	session->max_cmdsn = session->exp_cmdsn = session->cmdsn + 1;
3322c8447e4cSMike Christie 	spin_unlock_bh(&session->back_lock);
3323c8447e4cSMike Christie 	/*
33247996a778SMike Christie 	 * Unblock xmitworker(), Login Phase will pass through.
33257996a778SMike Christie 	 */
33267996a778SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
33277996a778SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
33287996a778SMike Christie 	return 0;
33297996a778SMike Christie }
33307996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_bind);
33317996a778SMike Christie 
3332adaf6990SAdheer Chandravanshi int iscsi_switch_str_param(char **param, char *new_val_buf)
33335700b1afSMike Christie {
33345700b1afSMike Christie 	char *new_val;
33355700b1afSMike Christie 
33365700b1afSMike Christie 	if (*param) {
33375700b1afSMike Christie 		if (!strcmp(*param, new_val_buf))
33385700b1afSMike Christie 			return 0;
33395700b1afSMike Christie 	}
33405700b1afSMike Christie 
33415700b1afSMike Christie 	new_val = kstrdup(new_val_buf, GFP_NOIO);
33425700b1afSMike Christie 	if (!new_val)
33435700b1afSMike Christie 		return -ENOMEM;
33445700b1afSMike Christie 
33455700b1afSMike Christie 	kfree(*param);
33465700b1afSMike Christie 	*param = new_val;
33475700b1afSMike Christie 	return 0;
33485700b1afSMike Christie }
3349adaf6990SAdheer Chandravanshi EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
3350a54a52caSMike Christie 
3351a54a52caSMike Christie int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3352a54a52caSMike Christie 		    enum iscsi_param param, char *buf, int buflen)
3353a54a52caSMike Christie {
3354a54a52caSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
3355a54a52caSMike Christie 	struct iscsi_session *session = conn->session;
33566a06a4b8SOr Gerlitz 	int val;
3357a54a52caSMike Christie 
3358a54a52caSMike Christie 	switch(param) {
3359843c0a8aSMike Christie 	case ISCSI_PARAM_FAST_ABORT:
3360843c0a8aSMike Christie 		sscanf(buf, "%d", &session->fast_abort);
3361843c0a8aSMike Christie 		break;
3362f6d5180cSMike Christie 	case ISCSI_PARAM_ABORT_TMO:
3363f6d5180cSMike Christie 		sscanf(buf, "%d", &session->abort_timeout);
3364f6d5180cSMike Christie 		break;
3365f6d5180cSMike Christie 	case ISCSI_PARAM_LU_RESET_TMO:
3366f6d5180cSMike Christie 		sscanf(buf, "%d", &session->lu_reset_timeout);
3367f6d5180cSMike Christie 		break;
33683fe5ae8bSMike Christie 	case ISCSI_PARAM_TGT_RESET_TMO:
33693fe5ae8bSMike Christie 		sscanf(buf, "%d", &session->tgt_reset_timeout);
33703fe5ae8bSMike Christie 		break;
3371f6d5180cSMike Christie 	case ISCSI_PARAM_PING_TMO:
3372f6d5180cSMike Christie 		sscanf(buf, "%d", &conn->ping_timeout);
3373f6d5180cSMike Christie 		break;
3374f6d5180cSMike Christie 	case ISCSI_PARAM_RECV_TMO:
3375f6d5180cSMike Christie 		sscanf(buf, "%d", &conn->recv_timeout);
3376f6d5180cSMike Christie 		break;
3377a54a52caSMike Christie 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
3378a54a52caSMike Christie 		sscanf(buf, "%d", &conn->max_recv_dlength);
3379a54a52caSMike Christie 		break;
3380a54a52caSMike Christie 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3381a54a52caSMike Christie 		sscanf(buf, "%d", &conn->max_xmit_dlength);
3382a54a52caSMike Christie 		break;
3383a54a52caSMike Christie 	case ISCSI_PARAM_HDRDGST_EN:
3384a54a52caSMike Christie 		sscanf(buf, "%d", &conn->hdrdgst_en);
3385a54a52caSMike Christie 		break;
3386a54a52caSMike Christie 	case ISCSI_PARAM_DATADGST_EN:
3387a54a52caSMike Christie 		sscanf(buf, "%d", &conn->datadgst_en);
3388a54a52caSMike Christie 		break;
3389a54a52caSMike Christie 	case ISCSI_PARAM_INITIAL_R2T_EN:
3390a54a52caSMike Christie 		sscanf(buf, "%d", &session->initial_r2t_en);
3391a54a52caSMike Christie 		break;
3392a54a52caSMike Christie 	case ISCSI_PARAM_MAX_R2T:
33931304be5fSMike Christie 		sscanf(buf, "%hu", &session->max_r2t);
3394a54a52caSMike Christie 		break;
3395a54a52caSMike Christie 	case ISCSI_PARAM_IMM_DATA_EN:
3396a54a52caSMike Christie 		sscanf(buf, "%d", &session->imm_data_en);
3397a54a52caSMike Christie 		break;
3398a54a52caSMike Christie 	case ISCSI_PARAM_FIRST_BURST:
3399a54a52caSMike Christie 		sscanf(buf, "%d", &session->first_burst);
3400a54a52caSMike Christie 		break;
3401a54a52caSMike Christie 	case ISCSI_PARAM_MAX_BURST:
3402a54a52caSMike Christie 		sscanf(buf, "%d", &session->max_burst);
3403a54a52caSMike Christie 		break;
3404a54a52caSMike Christie 	case ISCSI_PARAM_PDU_INORDER_EN:
3405a54a52caSMike Christie 		sscanf(buf, "%d", &session->pdu_inorder_en);
3406a54a52caSMike Christie 		break;
3407a54a52caSMike Christie 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
3408a54a52caSMike Christie 		sscanf(buf, "%d", &session->dataseq_inorder_en);
3409a54a52caSMike Christie 		break;
3410a54a52caSMike Christie 	case ISCSI_PARAM_ERL:
3411a54a52caSMike Christie 		sscanf(buf, "%d", &session->erl);
3412a54a52caSMike Christie 		break;
3413a54a52caSMike Christie 	case ISCSI_PARAM_EXP_STATSN:
3414a54a52caSMike Christie 		sscanf(buf, "%u", &conn->exp_statsn);
3415a54a52caSMike Christie 		break;
3416b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME:
34175700b1afSMike Christie 		return iscsi_switch_str_param(&session->username, buf);
3418b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME_IN:
34195700b1afSMike Christie 		return iscsi_switch_str_param(&session->username_in, buf);
3420b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD:
34215700b1afSMike Christie 		return iscsi_switch_str_param(&session->password, buf);
3422b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD_IN:
34235700b1afSMike Christie 		return iscsi_switch_str_param(&session->password_in, buf);
3424a54a52caSMike Christie 	case ISCSI_PARAM_TARGET_NAME:
34255700b1afSMike Christie 		return iscsi_switch_str_param(&session->targetname, buf);
34263c5c4801SVikas Chaudhary 	case ISCSI_PARAM_TARGET_ALIAS:
34273c5c4801SVikas Chaudhary 		return iscsi_switch_str_param(&session->targetalias, buf);
3428a54a52caSMike Christie 	case ISCSI_PARAM_TPGT:
3429a54a52caSMike Christie 		sscanf(buf, "%d", &session->tpgt);
3430a54a52caSMike Christie 		break;
3431a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_PORT:
3432a54a52caSMike Christie 		sscanf(buf, "%d", &conn->persistent_port);
3433a54a52caSMike Christie 		break;
3434a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
34355700b1afSMike Christie 		return iscsi_switch_str_param(&conn->persistent_address, buf);
343688dfd340SMike Christie 	case ISCSI_PARAM_IFACE_NAME:
34375700b1afSMike Christie 		return iscsi_switch_str_param(&session->ifacename, buf);
343888dfd340SMike Christie 	case ISCSI_PARAM_INITIATOR_NAME:
34395700b1afSMike Christie 		return iscsi_switch_str_param(&session->initiatorname, buf);
34403b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_ROOT:
34413b9373e9SEddie Wai 		return iscsi_switch_str_param(&session->boot_root, buf);
34423b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_NIC:
34433b9373e9SEddie Wai 		return iscsi_switch_str_param(&session->boot_nic, buf);
34443b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_TARGET:
34453b9373e9SEddie Wai 		return iscsi_switch_str_param(&session->boot_target, buf);
3446f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_PORTAL_TYPE:
3447f8525eb4SAdheer Chandravanshi 		return iscsi_switch_str_param(&session->portal_type, buf);
3448f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3449f8525eb4SAdheer Chandravanshi 		return iscsi_switch_str_param(&session->discovery_parent_type,
3450f8525eb4SAdheer Chandravanshi 					      buf);
34516a06a4b8SOr Gerlitz 	case ISCSI_PARAM_DISCOVERY_SESS:
34526a06a4b8SOr Gerlitz 		sscanf(buf, "%d", &val);
34536a06a4b8SOr Gerlitz 		session->discovery_sess = !!val;
34546a06a4b8SOr Gerlitz 		break;
3455ae56ff40SAdheer Chandravanshi 	case ISCSI_PARAM_LOCAL_IPADDR:
3456ae56ff40SAdheer Chandravanshi 		return iscsi_switch_str_param(&conn->local_ipaddr, buf);
3457a54a52caSMike Christie 	default:
3458a54a52caSMike Christie 		return -ENOSYS;
3459a54a52caSMike Christie 	}
3460a54a52caSMike Christie 
3461a54a52caSMike Christie 	return 0;
3462a54a52caSMike Christie }
3463a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_set_param);
3464a54a52caSMike Christie 
3465a54a52caSMike Christie int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3466a54a52caSMike Christie 			    enum iscsi_param param, char *buf)
3467a54a52caSMike Christie {
346875613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
3469a54a52caSMike Christie 	int len;
3470a54a52caSMike Christie 
3471a54a52caSMike Christie 	switch(param) {
3472843c0a8aSMike Christie 	case ISCSI_PARAM_FAST_ABORT:
3473ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->fast_abort);
3474843c0a8aSMike Christie 		break;
3475f6d5180cSMike Christie 	case ISCSI_PARAM_ABORT_TMO:
3476ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->abort_timeout);
3477f6d5180cSMike Christie 		break;
3478f6d5180cSMike Christie 	case ISCSI_PARAM_LU_RESET_TMO:
3479ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->lu_reset_timeout);
3480f6d5180cSMike Christie 		break;
34813fe5ae8bSMike Christie 	case ISCSI_PARAM_TGT_RESET_TMO:
3482ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->tgt_reset_timeout);
34833fe5ae8bSMike Christie 		break;
3484a54a52caSMike Christie 	case ISCSI_PARAM_INITIAL_R2T_EN:
3485ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->initial_r2t_en);
3486a54a52caSMike Christie 		break;
3487a54a52caSMike Christie 	case ISCSI_PARAM_MAX_R2T:
3488ec98ea70SChris Leech 		len = sysfs_emit(buf, "%hu\n", session->max_r2t);
3489a54a52caSMike Christie 		break;
3490a54a52caSMike Christie 	case ISCSI_PARAM_IMM_DATA_EN:
3491ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->imm_data_en);
3492a54a52caSMike Christie 		break;
3493a54a52caSMike Christie 	case ISCSI_PARAM_FIRST_BURST:
3494ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->first_burst);
3495a54a52caSMike Christie 		break;
3496a54a52caSMike Christie 	case ISCSI_PARAM_MAX_BURST:
3497ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->max_burst);
3498a54a52caSMike Christie 		break;
3499a54a52caSMike Christie 	case ISCSI_PARAM_PDU_INORDER_EN:
3500ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->pdu_inorder_en);
3501a54a52caSMike Christie 		break;
3502a54a52caSMike Christie 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
3503ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->dataseq_inorder_en);
3504a54a52caSMike Christie 		break;
35055f09e1f3SAdheer Chandravanshi 	case ISCSI_PARAM_DEF_TASKMGMT_TMO:
3506ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->def_taskmgmt_tmo);
35075f09e1f3SAdheer Chandravanshi 		break;
3508a54a52caSMike Christie 	case ISCSI_PARAM_ERL:
3509ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->erl);
3510a54a52caSMike Christie 		break;
3511a54a52caSMike Christie 	case ISCSI_PARAM_TARGET_NAME:
3512ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->targetname);
3513a54a52caSMike Christie 		break;
35143c5c4801SVikas Chaudhary 	case ISCSI_PARAM_TARGET_ALIAS:
3515ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->targetalias);
35163c5c4801SVikas Chaudhary 		break;
3517a54a52caSMike Christie 	case ISCSI_PARAM_TPGT:
3518ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->tpgt);
3519a54a52caSMike Christie 		break;
3520b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME:
3521ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->username);
3522b2c64167SMike Christie 		break;
3523b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME_IN:
3524ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->username_in);
3525b2c64167SMike Christie 		break;
3526b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD:
3527ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->password);
3528b2c64167SMike Christie 		break;
3529b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD_IN:
3530ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->password_in);
3531b2c64167SMike Christie 		break;
353288dfd340SMike Christie 	case ISCSI_PARAM_IFACE_NAME:
3533ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->ifacename);
353488dfd340SMike Christie 		break;
353588dfd340SMike Christie 	case ISCSI_PARAM_INITIATOR_NAME:
3536ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->initiatorname);
353788dfd340SMike Christie 		break;
35383b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_ROOT:
3539ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->boot_root);
35403b9373e9SEddie Wai 		break;
35413b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_NIC:
3542ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->boot_nic);
35433b9373e9SEddie Wai 		break;
35443b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_TARGET:
3545ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->boot_target);
35469a2307b1SAdheer Chandravanshi 		break;
3547f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
3548ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->auto_snd_tgt_disable);
3549f8525eb4SAdheer Chandravanshi 		break;
3550f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_SESS:
3551ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->discovery_sess);
3552f8525eb4SAdheer Chandravanshi 		break;
3553f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_PORTAL_TYPE:
3554ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->portal_type);
3555f8525eb4SAdheer Chandravanshi 		break;
3556f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_CHAP_AUTH_EN:
3557ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->chap_auth_en);
3558f8525eb4SAdheer Chandravanshi 		break;
3559f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
3560ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->discovery_logout_en);
3561f8525eb4SAdheer Chandravanshi 		break;
3562f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_BIDI_CHAP_EN:
3563ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->bidi_chap_en);
3564f8525eb4SAdheer Chandravanshi 		break;
3565f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
3566ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->discovery_auth_optional);
3567f8525eb4SAdheer Chandravanshi 		break;
3568f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DEF_TIME2WAIT:
3569ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->time2wait);
3570f8525eb4SAdheer Chandravanshi 		break;
3571f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DEF_TIME2RETAIN:
3572ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->time2retain);
3573f8525eb4SAdheer Chandravanshi 		break;
3574f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TSID:
3575ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->tsid);
3576f8525eb4SAdheer Chandravanshi 		break;
3577f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_ISID:
3578ec98ea70SChris Leech 		len = sysfs_emit(buf, "%02x%02x%02x%02x%02x%02x\n",
3579f8525eb4SAdheer Chandravanshi 			      session->isid[0], session->isid[1],
3580f8525eb4SAdheer Chandravanshi 			      session->isid[2], session->isid[3],
3581f8525eb4SAdheer Chandravanshi 			      session->isid[4], session->isid[5]);
3582f8525eb4SAdheer Chandravanshi 		break;
3583f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
3584ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->discovery_parent_idx);
3585f8525eb4SAdheer Chandravanshi 		break;
3586f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3587f8525eb4SAdheer Chandravanshi 		if (session->discovery_parent_type)
3588ec98ea70SChris Leech 			len = sysfs_emit(buf, "%s\n",
3589f8525eb4SAdheer Chandravanshi 				      session->discovery_parent_type);
3590f8525eb4SAdheer Chandravanshi 		else
3591ec98ea70SChris Leech 			len = sysfs_emit(buf, "\n");
35923b9373e9SEddie Wai 		break;
3593a54a52caSMike Christie 	default:
3594a54a52caSMike Christie 		return -ENOSYS;
3595a54a52caSMike Christie 	}
3596a54a52caSMike Christie 
3597a54a52caSMike Christie 	return len;
3598a54a52caSMike Christie }
3599a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3600a54a52caSMike Christie 
360100f3708eSMike Christie int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
360200f3708eSMike Christie 			      enum iscsi_param param, char *buf)
360300f3708eSMike Christie {
360400f3708eSMike Christie 	struct sockaddr_in6 *sin6 = NULL;
360500f3708eSMike Christie 	struct sockaddr_in *sin = NULL;
360600f3708eSMike Christie 	int len;
360700f3708eSMike Christie 
360800f3708eSMike Christie 	switch (addr->ss_family) {
360900f3708eSMike Christie 	case AF_INET:
361000f3708eSMike Christie 		sin = (struct sockaddr_in *)addr;
361100f3708eSMike Christie 		break;
361200f3708eSMike Christie 	case AF_INET6:
361300f3708eSMike Christie 		sin6 = (struct sockaddr_in6 *)addr;
361400f3708eSMike Christie 		break;
361500f3708eSMike Christie 	default:
361600f3708eSMike Christie 		return -EINVAL;
361700f3708eSMike Christie 	}
361800f3708eSMike Christie 
361900f3708eSMike Christie 	switch (param) {
362000f3708eSMike Christie 	case ISCSI_PARAM_CONN_ADDRESS:
362100f3708eSMike Christie 	case ISCSI_HOST_PARAM_IPADDRESS:
362200f3708eSMike Christie 		if (sin)
3623ec98ea70SChris Leech 			len = sysfs_emit(buf, "%pI4\n", &sin->sin_addr.s_addr);
362400f3708eSMike Christie 		else
3625ec98ea70SChris Leech 			len = sysfs_emit(buf, "%pI6\n", &sin6->sin6_addr);
362600f3708eSMike Christie 		break;
362700f3708eSMike Christie 	case ISCSI_PARAM_CONN_PORT:
36284bfb8ebfSMike Christie 	case ISCSI_PARAM_LOCAL_PORT:
362900f3708eSMike Christie 		if (sin)
3630ec98ea70SChris Leech 			len = sysfs_emit(buf, "%hu\n", be16_to_cpu(sin->sin_port));
363100f3708eSMike Christie 		else
3632ec98ea70SChris Leech 			len = sysfs_emit(buf, "%hu\n",
363300f3708eSMike Christie 				      be16_to_cpu(sin6->sin6_port));
363400f3708eSMike Christie 		break;
363500f3708eSMike Christie 	default:
363600f3708eSMike Christie 		return -EINVAL;
363700f3708eSMike Christie 	}
363800f3708eSMike Christie 
363900f3708eSMike Christie 	return len;
364000f3708eSMike Christie }
364100f3708eSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
364200f3708eSMike Christie 
3643a54a52caSMike Christie int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3644a54a52caSMike Christie 			 enum iscsi_param param, char *buf)
3645a54a52caSMike Christie {
3646a54a52caSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
3647a54a52caSMike Christie 	int len;
3648a54a52caSMike Christie 
3649a54a52caSMike Christie 	switch(param) {
3650f6d5180cSMike Christie 	case ISCSI_PARAM_PING_TMO:
3651ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->ping_timeout);
3652f6d5180cSMike Christie 		break;
3653f6d5180cSMike Christie 	case ISCSI_PARAM_RECV_TMO:
3654ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->recv_timeout);
3655f6d5180cSMike Christie 		break;
3656a54a52caSMike Christie 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
3657ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->max_recv_dlength);
3658a54a52caSMike Christie 		break;
3659a54a52caSMike Christie 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3660ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->max_xmit_dlength);
3661a54a52caSMike Christie 		break;
3662a54a52caSMike Christie 	case ISCSI_PARAM_HDRDGST_EN:
3663ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", conn->hdrdgst_en);
3664a54a52caSMike Christie 		break;
3665a54a52caSMike Christie 	case ISCSI_PARAM_DATADGST_EN:
3666ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", conn->datadgst_en);
3667a54a52caSMike Christie 		break;
3668a54a52caSMike Christie 	case ISCSI_PARAM_IFMARKER_EN:
3669ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", conn->ifmarker_en);
3670a54a52caSMike Christie 		break;
3671a54a52caSMike Christie 	case ISCSI_PARAM_OFMARKER_EN:
3672ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", conn->ofmarker_en);
3673a54a52caSMike Christie 		break;
3674a54a52caSMike Christie 	case ISCSI_PARAM_EXP_STATSN:
3675ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->exp_statsn);
3676a54a52caSMike Christie 		break;
3677a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_PORT:
3678ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", conn->persistent_port);
3679a54a52caSMike Christie 		break;
3680a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
3681ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", conn->persistent_address);
3682a54a52caSMike Christie 		break;
3683f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_STATSN:
3684ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->statsn);
3685f8525eb4SAdheer Chandravanshi 		break;
3686f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_MAX_SEGMENT_SIZE:
3687ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->max_segment_size);
3688f8525eb4SAdheer Chandravanshi 		break;
3689f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_KEEPALIVE_TMO:
3690ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->keepalive_tmo);
3691f8525eb4SAdheer Chandravanshi 		break;
3692f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_LOCAL_PORT:
3693ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->local_port);
3694f8525eb4SAdheer Chandravanshi 		break;
3695f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
3696ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_stat);
3697f8525eb4SAdheer Chandravanshi 		break;
3698f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_NAGLE_DISABLE:
3699ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->tcp_nagle_disable);
3700f8525eb4SAdheer Chandravanshi 		break;
3701f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_WSF_DISABLE:
3702ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->tcp_wsf_disable);
3703f8525eb4SAdheer Chandravanshi 		break;
3704f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_TIMER_SCALE:
3705ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->tcp_timer_scale);
3706f8525eb4SAdheer Chandravanshi 		break;
3707f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_TIMESTAMP_EN:
3708ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_en);
3709f8525eb4SAdheer Chandravanshi 		break;
3710f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
3711ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->fragment_disable);
3712f8525eb4SAdheer Chandravanshi 		break;
3713f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IPV4_TOS:
3714ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->ipv4_tos);
3715f8525eb4SAdheer Chandravanshi 		break;
3716f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IPV6_TC:
3717ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->ipv6_traffic_class);
3718f8525eb4SAdheer Chandravanshi 		break;
37195f09e1f3SAdheer Chandravanshi 	case ISCSI_PARAM_IPV6_FLOW_LABEL:
3720ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->ipv6_flow_label);
37215f09e1f3SAdheer Chandravanshi 		break;
3722f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
3723ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->is_fw_assigned_ipv6);
3724f8525eb4SAdheer Chandravanshi 		break;
3725f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_XMIT_WSF:
3726ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->tcp_xmit_wsf);
3727f8525eb4SAdheer Chandravanshi 		break;
3728f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_RECV_WSF:
3729ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->tcp_recv_wsf);
3730f8525eb4SAdheer Chandravanshi 		break;
3731ae56ff40SAdheer Chandravanshi 	case ISCSI_PARAM_LOCAL_IPADDR:
3732ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", conn->local_ipaddr);
3733ae56ff40SAdheer Chandravanshi 		break;
3734a54a52caSMike Christie 	default:
3735a54a52caSMike Christie 		return -ENOSYS;
3736a54a52caSMike Christie 	}
3737a54a52caSMike Christie 
3738a54a52caSMike Christie 	return len;
3739a54a52caSMike Christie }
3740a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3741a54a52caSMike Christie 
37420801c242SMike Christie int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
37430801c242SMike Christie 			 char *buf)
37440801c242SMike Christie {
374575613521SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
37460801c242SMike Christie 	int len;
37470801c242SMike Christie 
37480801c242SMike Christie 	switch (param) {
3749d8196ed2SMike Christie 	case ISCSI_HOST_PARAM_NETDEV_NAME:
3750ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", ihost->netdev);
3751d8196ed2SMike Christie 		break;
37520801c242SMike Christie 	case ISCSI_HOST_PARAM_HWADDRESS:
3753ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", ihost->hwaddress);
37540801c242SMike Christie 		break;
37558ad5781aSMike Christie 	case ISCSI_HOST_PARAM_INITIATOR_NAME:
3756ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", ihost->initiatorname);
37578ad5781aSMike Christie 		break;
37580801c242SMike Christie 	default:
37590801c242SMike Christie 		return -ENOSYS;
37600801c242SMike Christie 	}
37610801c242SMike Christie 
37620801c242SMike Christie 	return len;
37630801c242SMike Christie }
37640801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_get_param);
37650801c242SMike Christie 
37660801c242SMike Christie int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
37670801c242SMike Christie 			 char *buf, int buflen)
37680801c242SMike Christie {
376975613521SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
37700801c242SMike Christie 
37710801c242SMike Christie 	switch (param) {
3772d8196ed2SMike Christie 	case ISCSI_HOST_PARAM_NETDEV_NAME:
37735700b1afSMike Christie 		return iscsi_switch_str_param(&ihost->netdev, buf);
37740801c242SMike Christie 	case ISCSI_HOST_PARAM_HWADDRESS:
37755700b1afSMike Christie 		return iscsi_switch_str_param(&ihost->hwaddress, buf);
37768ad5781aSMike Christie 	case ISCSI_HOST_PARAM_INITIATOR_NAME:
37775700b1afSMike Christie 		return iscsi_switch_str_param(&ihost->initiatorname, buf);
37780801c242SMike Christie 	default:
37790801c242SMike Christie 		return -ENOSYS;
37800801c242SMike Christie 	}
37810801c242SMike Christie 
37820801c242SMike Christie 	return 0;
37830801c242SMike Christie }
37840801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_set_param);
37850801c242SMike Christie 
37867996a778SMike Christie MODULE_AUTHOR("Mike Christie");
37877996a778SMike Christie MODULE_DESCRIPTION("iSCSI library functions");
37887996a778SMike Christie MODULE_LICENSE("GPL");
3789