xref: /linux/drivers/scsi/libiscsi.c (revision a1f3486b3b095ed2259d7a1fc021a8b6e72a5365)
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 */
465f47f2cf5SMike Christie 		sc->SCp.ptr = 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)
4717996a778SMike Christie 			sc->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) {
83255e51edaSSagi Grimberg 			sc->result = DRIVER_SENSE << 24 |
83355e51edaSSagi Grimberg 				     SAM_STAT_CHECK_CONDITION;
83455e51edaSSagi Grimberg 			scsi_build_sense_buffer(1, sc->sense_buffer,
83555e51edaSSagi Grimberg 						ILLEGAL_REQUEST, 0x10, ascq);
836a73c2a2fSSagi Grimberg 			scsi_set_sense_information(sc->sense_buffer,
837a73c2a2fSSagi Grimberg 						   SCSI_SENSE_BUFFERSIZE,
838a73c2a2fSSagi Grimberg 						   sector);
83955e51edaSSagi Grimberg 			goto out;
84055e51edaSSagi Grimberg 		}
84155e51edaSSagi Grimberg 	}
84255e51edaSSagi Grimberg 
8437996a778SMike Christie 	if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
8447996a778SMike Christie 		sc->result = DID_ERROR << 16;
8457996a778SMike Christie 		goto out;
8467996a778SMike Christie 	}
8477996a778SMike Christie 
8487996a778SMike Christie 	if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
8499b80cb4bSMike Christie 		uint16_t senselen;
8507996a778SMike Christie 
8517996a778SMike Christie 		if (datalen < 2) {
8527996a778SMike Christie invalid_datalen:
853322d739dSMike Christie 			iscsi_conn_printk(KERN_ERR,  conn,
854322d739dSMike Christie 					 "Got CHECK_CONDITION but invalid data "
855322d739dSMike Christie 					 "buffer size of %d\n", datalen);
8567996a778SMike Christie 			sc->result = DID_BAD_TARGET << 16;
8577996a778SMike Christie 			goto out;
8587996a778SMike Christie 		}
8597996a778SMike Christie 
8608f333991SHarvey Harrison 		senselen = get_unaligned_be16(data);
8617996a778SMike Christie 		if (datalen < senselen)
8627996a778SMike Christie 			goto invalid_datalen;
8637996a778SMike Christie 
8647996a778SMike Christie 		memcpy(sc->sense_buffer, data + 2,
8659b80cb4bSMike Christie 		       min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
8661b2c7af8SMike Christie 		ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
8671b2c7af8SMike Christie 				  min_t(uint16_t, senselen,
8681b2c7af8SMike Christie 				  SCSI_SENSE_BUFFERSIZE));
8697996a778SMike Christie 	}
8707996a778SMike Christie 
871c07d4444SBoaz Harrosh 	if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
872c07d4444SBoaz Harrosh 			   ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
873c07d4444SBoaz Harrosh 		sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
874c07d4444SBoaz Harrosh 	}
875c07d4444SBoaz Harrosh 
8767207fea4SBoaz Harrosh 	if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
8777207fea4SBoaz Harrosh 	                   ISCSI_FLAG_CMD_OVERFLOW)) {
8787996a778SMike Christie 		int res_count = be32_to_cpu(rhdr->residual_count);
8797996a778SMike Christie 
8807207fea4SBoaz Harrosh 		if (res_count > 0 &&
8817207fea4SBoaz Harrosh 		    (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
8827207fea4SBoaz Harrosh 		     res_count <= scsi_bufflen(sc)))
883c07d4444SBoaz Harrosh 			/* write side for bidi or uni-io set_resid */
8841c138991SFUJITA Tomonori 			scsi_set_resid(sc, res_count);
8857996a778SMike Christie 		else
8867996a778SMike Christie 			sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
887c07d4444SBoaz Harrosh 	}
8887996a778SMike Christie out:
8893bbaaad9SMike Christie 	ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
8901b2c7af8SMike Christie 			  sc, sc->result, task->itt);
8917996a778SMike Christie 	conn->scsirsp_pdus_cnt++;
892b3cd5050SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
8937996a778SMike Christie }
8947996a778SMike Christie 
8951d9edf02SMike Christie /**
8961d9edf02SMike Christie  * iscsi_data_in_rsp - SCSI Data-In Response processing
8971d9edf02SMike Christie  * @conn: iscsi connection
8981d9edf02SMike Christie  * @hdr:  iscsi pdu
8991d9edf02SMike Christie  * @task: scsi command task
900a656183eSLee Duncan  *
901a656183eSLee Duncan  * iscsi_data_in_rsp sets up the scsi_cmnd fields based on the data received
902a656183eSLee Duncan  * then completes the command and task. called under back_lock
9031d9edf02SMike Christie  **/
9041d9edf02SMike Christie static void
9051d9edf02SMike Christie iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
9061d9edf02SMike Christie 		  struct iscsi_task *task)
9071d9edf02SMike Christie {
9081d9edf02SMike Christie 	struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
9091d9edf02SMike Christie 	struct scsi_cmnd *sc = task->sc;
9101d9edf02SMike Christie 
9111d9edf02SMike Christie 	if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
9121d9edf02SMike Christie 		return;
9131d9edf02SMike Christie 
914edbc9aa0SMike Christie 	iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
9151d9edf02SMike Christie 	sc->result = (DID_OK << 16) | rhdr->cmd_status;
9161d9edf02SMike Christie 	conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
9171d9edf02SMike Christie 	if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
9181d9edf02SMike Christie 	                   ISCSI_FLAG_DATA_OVERFLOW)) {
9191d9edf02SMike Christie 		int res_count = be32_to_cpu(rhdr->residual_count);
9201d9edf02SMike Christie 
9211d9edf02SMike Christie 		if (res_count > 0 &&
9221d9edf02SMike Christie 		    (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
923ae3d56d8SChristoph Hellwig 		     res_count <= sc->sdb.length))
924960bf87aSBart Van Assche 			scsi_set_resid(sc, res_count);
9251d9edf02SMike Christie 		else
9261d9edf02SMike Christie 			sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
9271d9edf02SMike Christie 	}
9281d9edf02SMike Christie 
9293bbaaad9SMike Christie 	ISCSI_DBG_SESSION(conn->session, "data in with status done "
9303bbaaad9SMike Christie 			  "[sc %p res %d itt 0x%x]\n",
9313bbaaad9SMike Christie 			  sc, sc->result, task->itt);
9321d9edf02SMike Christie 	conn->scsirsp_pdus_cnt++;
933b3cd5050SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
9341d9edf02SMike Christie }
9351d9edf02SMike Christie 
9367ea8b828SMike Christie static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
9377ea8b828SMike Christie {
9387ea8b828SMike Christie 	struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
939ec29d0acSMike Christie 	struct iscsi_session *session = conn->session;
9407ea8b828SMike Christie 
9417ea8b828SMike Christie 	conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
9427ea8b828SMike Christie 	conn->tmfrsp_pdus_cnt++;
9437ea8b828SMike Christie 
944ec29d0acSMike Christie 	if (session->tmf_state != TMF_QUEUED)
9457ea8b828SMike Christie 		return;
9467ea8b828SMike Christie 
9477ea8b828SMike Christie 	if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
948ec29d0acSMike Christie 		session->tmf_state = TMF_SUCCESS;
9497ea8b828SMike Christie 	else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
950ec29d0acSMike Christie 		session->tmf_state = TMF_NOT_FOUND;
9517ea8b828SMike Christie 	else
952ec29d0acSMike Christie 		session->tmf_state = TMF_FAILED;
953ec29d0acSMike Christie 	wake_up(&session->ehwait);
9547ea8b828SMike Christie }
9557ea8b828SMike Christie 
95652f5664aSAriel Nahum static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
957f6d5180cSMike Christie {
958f6d5180cSMike Christie         struct iscsi_nopout hdr;
9599c19a7d0SMike Christie 	struct iscsi_task *task;
960f6d5180cSMike Christie 
961fe0a8a95SLee Duncan 	if (!rhdr) {
962fe0a8a95SLee Duncan 		if (READ_ONCE(conn->ping_task))
96352f5664aSAriel Nahum 			return -EINVAL;
964fe0a8a95SLee Duncan 		WRITE_ONCE(conn->ping_task, INVALID_SCSI_TASK);
965fe0a8a95SLee Duncan 	}
966f6d5180cSMike Christie 
967f6d5180cSMike Christie 	memset(&hdr, 0, sizeof(struct iscsi_nopout));
968f6d5180cSMike Christie 	hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
969f6d5180cSMike Christie 	hdr.flags = ISCSI_FLAG_CMD_FINAL;
970f6d5180cSMike Christie 
971f6d5180cSMike Christie 	if (rhdr) {
97255bdabdfSAndy Grover 		hdr.lun = rhdr->lun;
973f6d5180cSMike Christie 		hdr.ttt = rhdr->ttt;
974f6d5180cSMike Christie 		hdr.itt = RESERVED_ITT;
975f6d5180cSMike Christie 	} else
976f6d5180cSMike Christie 		hdr.ttt = RESERVED_ITT;
977f6d5180cSMike Christie 
9789c19a7d0SMike Christie 	task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
97952f5664aSAriel Nahum 	if (!task) {
980fe0a8a95SLee Duncan 		if (!rhdr)
981fe0a8a95SLee Duncan 			WRITE_ONCE(conn->ping_task, NULL);
982322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
98352f5664aSAriel Nahum 		return -EIO;
98452f5664aSAriel Nahum 	} else if (!rhdr) {
985d3acf022SMike Christie 		/* only track our nops */
986d3acf022SMike Christie 		conn->last_ping = jiffies;
987d3acf022SMike Christie 	}
98852f5664aSAriel Nahum 
98952f5664aSAriel Nahum 	return 0;
990f6d5180cSMike Christie }
991f6d5180cSMike Christie 
992a656183eSLee Duncan /**
993a656183eSLee Duncan  * iscsi_nop_out_rsp - SCSI NOP Response processing
994a656183eSLee Duncan  * @task: scsi command task
995a656183eSLee Duncan  * @nop: the nop structure
996a656183eSLee Duncan  * @data: where to put the data
997a656183eSLee Duncan  * @datalen: length of data
998a656183eSLee Duncan  *
999a656183eSLee Duncan  * iscsi_nop_out_rsp handles nop response from use or
1000a656183eSLee Duncan  * from user space. called under back_lock
1001a656183eSLee Duncan  **/
10028afa1439SMike Christie static int iscsi_nop_out_rsp(struct iscsi_task *task,
10038afa1439SMike Christie 			     struct iscsi_nopin *nop, char *data, int datalen)
10048afa1439SMike Christie {
10058afa1439SMike Christie 	struct iscsi_conn *conn = task->conn;
10068afa1439SMike Christie 	int rc = 0;
10078afa1439SMike Christie 
1008fe0a8a95SLee Duncan 	if (READ_ONCE(conn->ping_task) != task) {
10098afa1439SMike Christie 		/*
10108afa1439SMike Christie 		 * If this is not in response to one of our
10118afa1439SMike Christie 		 * nops then it must be from userspace.
10128afa1439SMike Christie 		 */
10138afa1439SMike Christie 		if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
10148afa1439SMike Christie 				   data, datalen))
10158afa1439SMike Christie 			rc = ISCSI_ERR_CONN_FAILED;
10168afa1439SMike Christie 	} else
10178afa1439SMike Christie 		mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
10188afa1439SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
10198afa1439SMike Christie 	return rc;
10208afa1439SMike Christie }
10218afa1439SMike Christie 
102262f38300SMike Christie static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
102362f38300SMike Christie 			       char *data, int datalen)
102462f38300SMike Christie {
102562f38300SMike Christie 	struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
102662f38300SMike Christie 	struct iscsi_hdr rejected_pdu;
10278afa1439SMike Christie 	int opcode, rc = 0;
102862f38300SMike Christie 
102962f38300SMike Christie 	conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
103062f38300SMike Christie 
10318afa1439SMike Christie 	if (ntoh24(reject->dlength) > datalen ||
10328afa1439SMike Christie 	    ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
10338afa1439SMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
10348afa1439SMike Christie 				  "pdu. Invalid data length (pdu dlength "
10358afa1439SMike Christie 				  "%u, datalen %d\n", ntoh24(reject->dlength),
10368afa1439SMike Christie 				  datalen);
103762f38300SMike Christie 		return ISCSI_ERR_PROTO;
10388afa1439SMike Christie 	}
103962f38300SMike Christie 	memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
10408afa1439SMike Christie 	opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
10418afa1439SMike Christie 
10428afa1439SMike Christie 	switch (reject->reason) {
10438afa1439SMike Christie 	case ISCSI_REASON_DATA_DIGEST_ERROR:
1044322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
10458afa1439SMike Christie 				  "pdu (op 0x%x itt 0x%x) rejected "
1046262ef636SMike Christie 				  "due to DataDigest error.\n",
1047e5dbbe27SVaughan Cao 				  opcode, rejected_pdu.itt);
10488afa1439SMike Christie 		break;
10498afa1439SMike Christie 	case ISCSI_REASON_IMM_CMD_REJECT:
10508afa1439SMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
10518afa1439SMike Christie 				  "pdu (op 0x%x itt 0x%x) rejected. Too many "
10528afa1439SMike Christie 				  "immediate commands.\n",
1053e5dbbe27SVaughan Cao 				  opcode, rejected_pdu.itt);
10548afa1439SMike Christie 		/*
10558afa1439SMike Christie 		 * We only send one TMF at a time so if the target could not
10568afa1439SMike Christie 		 * handle it, then it should get fixed (RFC mandates that
10578afa1439SMike Christie 		 * a target can handle one immediate TMF per conn).
10588afa1439SMike Christie 		 *
10598afa1439SMike Christie 		 * For nops-outs, we could have sent more than one if
10608afa1439SMike Christie 		 * the target is sending us lots of nop-ins
10618afa1439SMike Christie 		 */
10628afa1439SMike Christie 		if (opcode != ISCSI_OP_NOOP_OUT)
106362f38300SMike Christie 			return 0;
10648afa1439SMike Christie 
1065659743b0SShlomo Pongratz 		if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
10668afa1439SMike Christie 			/*
10678afa1439SMike Christie 			 * nop-out in response to target's nop-out rejected.
10688afa1439SMike Christie 			 * Just resend.
10698afa1439SMike Christie 			 */
1070659743b0SShlomo Pongratz 			/* In RX path we are under back lock */
1071659743b0SShlomo Pongratz 			spin_unlock(&conn->session->back_lock);
1072659743b0SShlomo Pongratz 			spin_lock(&conn->session->frwd_lock);
10738afa1439SMike Christie 			iscsi_send_nopout(conn,
10748afa1439SMike Christie 					  (struct iscsi_nopin*)&rejected_pdu);
1075659743b0SShlomo Pongratz 			spin_unlock(&conn->session->frwd_lock);
1076659743b0SShlomo Pongratz 			spin_lock(&conn->session->back_lock);
1077659743b0SShlomo Pongratz 		} else {
10788afa1439SMike Christie 			struct iscsi_task *task;
10798afa1439SMike Christie 			/*
10808afa1439SMike Christie 			 * Our nop as ping got dropped. We know the target
10818afa1439SMike Christie 			 * and transport are ok so just clean up
10828afa1439SMike Christie 			 */
10838afa1439SMike Christie 			task = iscsi_itt_to_task(conn, rejected_pdu.itt);
10848afa1439SMike Christie 			if (!task) {
10858afa1439SMike Christie 				iscsi_conn_printk(KERN_ERR, conn,
10868afa1439SMike Christie 						 "Invalid pdu reject. Could "
10878afa1439SMike Christie 						 "not lookup rejected task.\n");
10888afa1439SMike Christie 				rc = ISCSI_ERR_BAD_ITT;
10898afa1439SMike Christie 			} else
10908afa1439SMike Christie 				rc = iscsi_nop_out_rsp(task,
10918afa1439SMike Christie 					(struct iscsi_nopin*)&rejected_pdu,
10928afa1439SMike Christie 					NULL, 0);
10938afa1439SMike Christie 		}
10948afa1439SMike Christie 		break;
10958afa1439SMike Christie 	default:
10968afa1439SMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
10978afa1439SMike Christie 				  "pdu (op 0x%x itt 0x%x) rejected. Reason "
1098e5dbbe27SVaughan Cao 				  "code 0x%x\n", rejected_pdu.opcode,
1099e5dbbe27SVaughan Cao 				  rejected_pdu.itt, reject->reason);
11008afa1439SMike Christie 		break;
11018afa1439SMike Christie 	}
11028afa1439SMike Christie 	return rc;
110362f38300SMike Christie }
110462f38300SMike Christie 
11057996a778SMike Christie /**
1106913e5bf4SMike Christie  * iscsi_itt_to_task - look up task by itt
1107913e5bf4SMike Christie  * @conn: iscsi connection
1108913e5bf4SMike Christie  * @itt: itt
1109913e5bf4SMike Christie  *
1110913e5bf4SMike Christie  * This should be used for mgmt tasks like login and nops, or if
1111913e5bf4SMike Christie  * the LDD's itt space does not include the session age.
1112913e5bf4SMike Christie  *
1113659743b0SShlomo Pongratz  * The session back_lock must be held.
1114913e5bf4SMike Christie  */
11158f9256ceSMike Christie struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
1116913e5bf4SMike Christie {
1117913e5bf4SMike Christie 	struct iscsi_session *session = conn->session;
1118262ef636SMike Christie 	int i;
1119913e5bf4SMike Christie 
1120913e5bf4SMike Christie 	if (itt == RESERVED_ITT)
1121913e5bf4SMike Christie 		return NULL;
1122913e5bf4SMike Christie 
1123262ef636SMike Christie 	if (session->tt->parse_pdu_itt)
1124262ef636SMike Christie 		session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1125262ef636SMike Christie 	else
1126913e5bf4SMike Christie 		i = get_itt(itt);
1127913e5bf4SMike Christie 	if (i >= session->cmds_max)
1128913e5bf4SMike Christie 		return NULL;
1129913e5bf4SMike Christie 
1130913e5bf4SMike Christie 	return session->cmds[i];
1131913e5bf4SMike Christie }
11328f9256ceSMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
1133913e5bf4SMike Christie 
1134913e5bf4SMike Christie /**
11357996a778SMike Christie  * __iscsi_complete_pdu - complete pdu
11367996a778SMike Christie  * @conn: iscsi conn
11377996a778SMike Christie  * @hdr: iscsi header
11387996a778SMike Christie  * @data: data buffer
11397996a778SMike Christie  * @datalen: len of data buffer
11407996a778SMike Christie  *
11417996a778SMike Christie  * Completes pdu processing by freeing any resources allocated at
1142659743b0SShlomo Pongratz  * queuecommand or send generic. session back_lock must be held and verify
11437996a778SMike Christie  * itt must have been called.
11447996a778SMike Christie  */
1145913e5bf4SMike Christie int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
11467996a778SMike Christie 			 char *data, int datalen)
11477996a778SMike Christie {
11487996a778SMike Christie 	struct iscsi_session *session = conn->session;
11497996a778SMike Christie 	int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
11509c19a7d0SMike Christie 	struct iscsi_task *task;
11517996a778SMike Christie 	uint32_t itt;
11527996a778SMike Christie 
1153f6d5180cSMike Christie 	conn->last_recv = jiffies;
11540af967f5SMike Christie 	rc = iscsi_verify_itt(conn, hdr->itt);
11550af967f5SMike Christie 	if (rc)
11560af967f5SMike Christie 		return rc;
11570af967f5SMike Christie 
1158b4377356SAl Viro 	if (hdr->itt != RESERVED_ITT)
1159b4377356SAl Viro 		itt = get_itt(hdr->itt);
11607996a778SMike Christie 	else
1161b4377356SAl Viro 		itt = ~0U;
11627996a778SMike Christie 
11631b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
11643e5c28adSMike Christie 			  opcode, conn->id, itt, datalen);
11657996a778SMike Christie 
11663e5c28adSMike Christie 	if (itt == ~0U) {
116777a23c21SMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
116862f38300SMike Christie 
11697996a778SMike Christie 		switch(opcode) {
11707996a778SMike Christie 		case ISCSI_OP_NOOP_IN:
117140527afeSMike Christie 			if (datalen) {
117240527afeSMike Christie 				rc = ISCSI_ERR_PROTO;
117340527afeSMike Christie 				break;
117440527afeSMike Christie 			}
117540527afeSMike Christie 
1176b4377356SAl Viro 			if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
117740527afeSMike Christie 				break;
117840527afeSMike Christie 
1179659743b0SShlomo Pongratz 			/* In RX path we are under back lock */
1180659743b0SShlomo Pongratz 			spin_unlock(&session->back_lock);
1181659743b0SShlomo Pongratz 			spin_lock(&session->frwd_lock);
1182f6d5180cSMike Christie 			iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
1183659743b0SShlomo Pongratz 			spin_unlock(&session->frwd_lock);
1184659743b0SShlomo Pongratz 			spin_lock(&session->back_lock);
11857996a778SMike Christie 			break;
11867996a778SMike Christie 		case ISCSI_OP_REJECT:
118762f38300SMike Christie 			rc = iscsi_handle_reject(conn, hdr, data, datalen);
118862f38300SMike Christie 			break;
11897996a778SMike Christie 		case ISCSI_OP_ASYNC_EVENT:
11908d2860b3SMike Christie 			conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
11915831c737SMike Christie 			if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
11925831c737SMike Christie 				rc = ISCSI_ERR_CONN_FAILED;
11937996a778SMike Christie 			break;
11947996a778SMike Christie 		default:
11957996a778SMike Christie 			rc = ISCSI_ERR_BAD_OPCODE;
11967996a778SMike Christie 			break;
11977996a778SMike Christie 		}
11983e5c28adSMike Christie 		goto out;
11993e5c28adSMike Christie 	}
12007996a778SMike Christie 
12013e5c28adSMike Christie 	switch(opcode) {
12023e5c28adSMike Christie 	case ISCSI_OP_SCSI_CMD_RSP:
1203913e5bf4SMike Christie 	case ISCSI_OP_SCSI_DATA_IN:
1204913e5bf4SMike Christie 		task = iscsi_itt_to_ctask(conn, hdr->itt);
1205913e5bf4SMike Christie 		if (!task)
1206913e5bf4SMike Christie 			return ISCSI_ERR_BAD_ITT;
1207d355e57dSMike Christie 		task->last_xfer = jiffies;
12083e5c28adSMike Christie 		break;
1209913e5bf4SMike Christie 	case ISCSI_OP_R2T:
1210913e5bf4SMike Christie 		/*
1211913e5bf4SMike Christie 		 * LLD handles R2Ts if they need to.
1212913e5bf4SMike Christie 		 */
1213913e5bf4SMike Christie 		return 0;
1214913e5bf4SMike Christie 	case ISCSI_OP_LOGOUT_RSP:
1215913e5bf4SMike Christie 	case ISCSI_OP_LOGIN_RSP:
1216913e5bf4SMike Christie 	case ISCSI_OP_TEXT_RSP:
1217913e5bf4SMike Christie 	case ISCSI_OP_SCSI_TMFUNC_RSP:
1218913e5bf4SMike Christie 	case ISCSI_OP_NOOP_IN:
1219913e5bf4SMike Christie 		task = iscsi_itt_to_task(conn, hdr->itt);
1220913e5bf4SMike Christie 		if (!task)
1221913e5bf4SMike Christie 			return ISCSI_ERR_BAD_ITT;
1222913e5bf4SMike Christie 		break;
1223913e5bf4SMike Christie 	default:
1224913e5bf4SMike Christie 		return ISCSI_ERR_BAD_OPCODE;
12253e5c28adSMike Christie 	}
1226913e5bf4SMike Christie 
1227913e5bf4SMike Christie 	switch(opcode) {
1228913e5bf4SMike Christie 	case ISCSI_OP_SCSI_CMD_RSP:
12299c19a7d0SMike Christie 		iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
12303e5c28adSMike Christie 		break;
12313e5c28adSMike Christie 	case ISCSI_OP_SCSI_DATA_IN:
12321d9edf02SMike Christie 		iscsi_data_in_rsp(conn, hdr, task);
12333e5c28adSMike Christie 		break;
12343e5c28adSMike Christie 	case ISCSI_OP_LOGOUT_RSP:
12353e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12363e5c28adSMike Christie 		if (datalen) {
12373e5c28adSMike Christie 			rc = ISCSI_ERR_PROTO;
12383e5c28adSMike Christie 			break;
12393e5c28adSMike Christie 		}
12403e5c28adSMike Christie 		conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
12413e5c28adSMike Christie 		goto recv_pdu;
12423e5c28adSMike Christie 	case ISCSI_OP_LOGIN_RSP:
12433e5c28adSMike Christie 	case ISCSI_OP_TEXT_RSP:
12443e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12453e5c28adSMike Christie 		/*
12463e5c28adSMike Christie 		 * login related PDU's exp_statsn is handled in
12473e5c28adSMike Christie 		 * userspace
12483e5c28adSMike Christie 		 */
12493e5c28adSMike Christie 		goto recv_pdu;
12503e5c28adSMike Christie 	case ISCSI_OP_SCSI_TMFUNC_RSP:
12513e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12523e5c28adSMike Christie 		if (datalen) {
12533e5c28adSMike Christie 			rc = ISCSI_ERR_PROTO;
12543e5c28adSMike Christie 			break;
12553e5c28adSMike Christie 		}
12563e5c28adSMike Christie 
12573e5c28adSMike Christie 		iscsi_tmf_rsp(conn, hdr);
1258b3cd5050SMike Christie 		iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
12593e5c28adSMike Christie 		break;
12603e5c28adSMike Christie 	case ISCSI_OP_NOOP_IN:
12613e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12623e5c28adSMike Christie 		if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
12633e5c28adSMike Christie 			rc = ISCSI_ERR_PROTO;
12643e5c28adSMike Christie 			break;
12653e5c28adSMike Christie 		}
12663e5c28adSMike Christie 		conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
12673e5c28adSMike Christie 
12688afa1439SMike Christie 		rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
12698afa1439SMike Christie 				       data, datalen);
12703e5c28adSMike Christie 		break;
12713e5c28adSMike Christie 	default:
12723e5c28adSMike Christie 		rc = ISCSI_ERR_BAD_OPCODE;
12733e5c28adSMike Christie 		break;
12743e5c28adSMike Christie 	}
12753e5c28adSMike Christie 
12763e5c28adSMike Christie out:
12773e5c28adSMike Christie 	return rc;
12783e5c28adSMike Christie recv_pdu:
12793e5c28adSMike Christie 	if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
12803e5c28adSMike Christie 		rc = ISCSI_ERR_CONN_FAILED;
1281b3cd5050SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
12827996a778SMike Christie 	return rc;
12837996a778SMike Christie }
1284913e5bf4SMike Christie EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
12857996a778SMike Christie 
12867996a778SMike Christie int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
12877996a778SMike Christie 		       char *data, int datalen)
12887996a778SMike Christie {
12897996a778SMike Christie 	int rc;
12907996a778SMike Christie 
1291659743b0SShlomo Pongratz 	spin_lock(&conn->session->back_lock);
12927996a778SMike Christie 	rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1293659743b0SShlomo Pongratz 	spin_unlock(&conn->session->back_lock);
12947996a778SMike Christie 	return rc;
12957996a778SMike Christie }
12967996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
12977996a778SMike Christie 
12980af967f5SMike Christie int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
12997996a778SMike Christie {
13007996a778SMike Christie 	struct iscsi_session *session = conn->session;
1301262ef636SMike Christie 	int age = 0, i = 0;
13027996a778SMike Christie 
13030af967f5SMike Christie 	if (itt == RESERVED_ITT)
13040af967f5SMike Christie 		return 0;
13050af967f5SMike Christie 
1306262ef636SMike Christie 	if (session->tt->parse_pdu_itt)
1307262ef636SMike Christie 		session->tt->parse_pdu_itt(conn, itt, &i, &age);
1308262ef636SMike Christie 	else {
1309262ef636SMike Christie 		i = get_itt(itt);
1310262ef636SMike Christie 		age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1311262ef636SMike Christie 	}
1312262ef636SMike Christie 
1313262ef636SMike Christie 	if (age != session->age) {
1314322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
13150af967f5SMike Christie 				  "received itt %x expected session age (%x)\n",
1316913e5bf4SMike Christie 				  (__force u32)itt, session->age);
13177996a778SMike Christie 		return ISCSI_ERR_BAD_ITT;
13187996a778SMike Christie 	}
13197996a778SMike Christie 
13203e5c28adSMike Christie 	if (i >= session->cmds_max) {
13213e5c28adSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
13223e5c28adSMike Christie 				  "received invalid itt index %u (max cmds "
13233e5c28adSMike Christie 				   "%u.\n", i, session->cmds_max);
13243e5c28adSMike Christie 		return ISCSI_ERR_BAD_ITT;
13257996a778SMike Christie 	}
13267996a778SMike Christie 	return 0;
13277996a778SMike Christie }
13287996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_verify_itt);
13297996a778SMike Christie 
1330913e5bf4SMike Christie /**
1331913e5bf4SMike Christie  * iscsi_itt_to_ctask - look up ctask by itt
1332913e5bf4SMike Christie  * @conn: iscsi connection
1333913e5bf4SMike Christie  * @itt: itt
1334913e5bf4SMike Christie  *
1335913e5bf4SMike Christie  * This should be used for cmd tasks.
1336913e5bf4SMike Christie  *
1337659743b0SShlomo Pongratz  * The session back_lock must be held.
1338913e5bf4SMike Christie  */
1339913e5bf4SMike Christie struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
13400af967f5SMike Christie {
13419c19a7d0SMike Christie 	struct iscsi_task *task;
13420af967f5SMike Christie 
13430af967f5SMike Christie 	if (iscsi_verify_itt(conn, itt))
13440af967f5SMike Christie 		return NULL;
13450af967f5SMike Christie 
1346913e5bf4SMike Christie 	task = iscsi_itt_to_task(conn, itt);
1347913e5bf4SMike Christie 	if (!task || !task->sc)
13480af967f5SMike Christie 		return NULL;
13490af967f5SMike Christie 
1350913e5bf4SMike Christie 	if (task->sc->SCp.phase != conn->session->age) {
1351913e5bf4SMike Christie 		iscsi_session_printk(KERN_ERR, conn->session,
1352913e5bf4SMike Christie 				  "task's session age %d, expected %d\n",
1353913e5bf4SMike Christie 				  task->sc->SCp.phase, conn->session->age);
13540af967f5SMike Christie 		return NULL;
1355913e5bf4SMike Christie 	}
13560af967f5SMike Christie 
13579c19a7d0SMike Christie 	return task;
13580af967f5SMike Christie }
13590af967f5SMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
13600af967f5SMike Christie 
136140a06e75SMike Christie void iscsi_session_failure(struct iscsi_session *session,
1362e5bd7b54SMike Christie 			   enum iscsi_err err)
1363e5bd7b54SMike Christie {
1364e5bd7b54SMike Christie 	struct iscsi_conn *conn;
1365e5bd7b54SMike Christie 
1366659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
1367e5bd7b54SMike Christie 	conn = session->leadconn;
1368e5bd7b54SMike Christie 	if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1369659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
1370e5bd7b54SMike Christie 		return;
1371e5bd7b54SMike Christie 	}
1372e5bd7b54SMike Christie 
1373b1d19e8cSMike Christie 	iscsi_get_conn(conn->cls_conn);
1374659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
1375e5bd7b54SMike Christie 	/*
1376e5bd7b54SMike Christie 	 * if the host is being removed bypass the connection
1377e5bd7b54SMike Christie 	 * recovery initialization because we are going to kill
1378e5bd7b54SMike Christie 	 * the session.
1379e5bd7b54SMike Christie 	 */
1380e5bd7b54SMike Christie 	if (err == ISCSI_ERR_INVALID_HOST)
1381e5bd7b54SMike Christie 		iscsi_conn_error_event(conn->cls_conn, err);
1382e5bd7b54SMike Christie 	else
1383e5bd7b54SMike Christie 		iscsi_conn_failure(conn, err);
1384b1d19e8cSMike Christie 	iscsi_put_conn(conn->cls_conn);
1385e5bd7b54SMike Christie }
1386e5bd7b54SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_failure);
1387e5bd7b54SMike Christie 
1388891e2639SMike Christie static bool iscsi_set_conn_failed(struct iscsi_conn *conn)
13897996a778SMike Christie {
13907996a778SMike Christie 	struct iscsi_session *session = conn->session;
13917996a778SMike Christie 
1392891e2639SMike Christie 	if (session->state == ISCSI_STATE_FAILED)
1393891e2639SMike Christie 		return false;
1394656cffc9SMike Christie 
139567a61114SMike Christie 	if (conn->stop_stage == 0)
13967996a778SMike Christie 		session->state = ISCSI_STATE_FAILED;
1397e5bd7b54SMike Christie 
13987996a778SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
13997996a778SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1400891e2639SMike Christie 	return true;
1401891e2639SMike Christie }
1402891e2639SMike Christie 
1403891e2639SMike Christie void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1404891e2639SMike Christie {
1405891e2639SMike Christie 	struct iscsi_session *session = conn->session;
1406891e2639SMike Christie 	bool needs_evt;
1407891e2639SMike Christie 
1408891e2639SMike Christie 	spin_lock_bh(&session->frwd_lock);
1409891e2639SMike Christie 	needs_evt = iscsi_set_conn_failed(conn);
1410891e2639SMike Christie 	spin_unlock_bh(&session->frwd_lock);
1411891e2639SMike Christie 
1412891e2639SMike Christie 	if (needs_evt)
1413e5bd7b54SMike Christie 		iscsi_conn_error_event(conn->cls_conn, err);
14147996a778SMike Christie }
14157996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_failure);
14167996a778SMike Christie 
141777a23c21SMike Christie static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
141877a23c21SMike Christie {
141977a23c21SMike Christie 	struct iscsi_session *session = conn->session;
142077a23c21SMike Christie 
142177a23c21SMike Christie 	/*
142277a23c21SMike Christie 	 * Check for iSCSI window and take care of CmdSN wrap-around
142377a23c21SMike Christie 	 */
1424e0726407SMike Christie 	if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
14251b2c7af8SMike Christie 		ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
14261b2c7af8SMike Christie 				  "%u MaxCmdSN %u CmdSN %u/%u\n",
14271b2c7af8SMike Christie 				  session->exp_cmdsn, session->max_cmdsn,
14281b2c7af8SMike Christie 				  session->cmdsn, session->queued_cmdsn);
142977a23c21SMike Christie 		return -ENOSPC;
143077a23c21SMike Christie 	}
143177a23c21SMike Christie 	return 0;
143277a23c21SMike Christie }
143377a23c21SMike Christie 
14345923d64bSMike Christie static int iscsi_xmit_task(struct iscsi_conn *conn, struct iscsi_task *task,
14355923d64bSMike Christie 			   bool was_requeue)
143677a23c21SMike Christie {
1437843c0a8aSMike Christie 	int rc;
143877a23c21SMike Christie 
143979edd00dSAnoob Soman 	spin_lock_bh(&conn->session->back_lock);
14405923d64bSMike Christie 
14415923d64bSMike Christie 	if (!conn->task) {
14425923d64bSMike Christie 		/* Take a ref so we can access it after xmit_task() */
14435923d64bSMike Christie 		__iscsi_get_task(task);
14445923d64bSMike Christie 	} else {
14455923d64bSMike Christie 		/* Already have a ref from when we failed to send it last call */
14465923d64bSMike Christie 		conn->task = NULL;
14475923d64bSMike Christie 	}
14485923d64bSMike Christie 
14495923d64bSMike Christie 	/*
14505923d64bSMike Christie 	 * If this was a requeue for a R2T we have an extra ref on the task in
14515923d64bSMike Christie 	 * case a bad target sends a cmd rsp before we have handled the task.
14525923d64bSMike Christie 	 */
14535923d64bSMike Christie 	if (was_requeue)
14545923d64bSMike Christie 		__iscsi_put_task(task);
14555923d64bSMike Christie 
14565923d64bSMike Christie 	/*
14575923d64bSMike Christie 	 * Do this after dropping the extra ref because if this was a requeue
14585923d64bSMike Christie 	 * it's removed from that list and cleanup_queued_task would miss it.
14595923d64bSMike Christie 	 */
14605923d64bSMike Christie 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
14615923d64bSMike Christie 		/*
14625923d64bSMike Christie 		 * Save the task and ref in case we weren't cleaning up this
14635923d64bSMike Christie 		 * task and get woken up again.
14645923d64bSMike Christie 		 */
14655923d64bSMike Christie 		conn->task = task;
146679edd00dSAnoob Soman 		spin_unlock_bh(&conn->session->back_lock);
146779edd00dSAnoob Soman 		return -ENODATA;
146879edd00dSAnoob Soman 	}
146979edd00dSAnoob Soman 	spin_unlock_bh(&conn->session->back_lock);
14705923d64bSMike Christie 
1471659743b0SShlomo Pongratz 	spin_unlock_bh(&conn->session->frwd_lock);
14729c19a7d0SMike Christie 	rc = conn->session->tt->xmit_task(task);
1473659743b0SShlomo Pongratz 	spin_lock_bh(&conn->session->frwd_lock);
1474d355e57dSMike Christie 	if (!rc) {
14759c19a7d0SMike Christie 		/* done with this task */
1476d355e57dSMike Christie 		task->last_xfer = jiffies;
1477d355e57dSMike Christie 	}
1478659743b0SShlomo Pongratz 	/* regular RX path uses back_lock */
147972b97402SShlomo Pongratz 	spin_lock(&conn->session->back_lock);
14805923d64bSMike Christie 	if (rc && task->state == ISCSI_TASK_RUNNING) {
14815923d64bSMike Christie 		/*
14825923d64bSMike Christie 		 * get an extra ref that is released next time we access it
14835923d64bSMike Christie 		 * as conn->task above.
14845923d64bSMike Christie 		 */
14855923d64bSMike Christie 		__iscsi_get_task(task);
14865923d64bSMike Christie 		conn->task = task;
14875923d64bSMike Christie 	}
14885923d64bSMike Christie 
1489d355e57dSMike Christie 	__iscsi_put_task(task);
149072b97402SShlomo Pongratz 	spin_unlock(&conn->session->back_lock);
149177a23c21SMike Christie 	return rc;
149277a23c21SMike Christie }
149377a23c21SMike Christie 
14947996a778SMike Christie /**
14959c19a7d0SMike Christie  * iscsi_requeue_task - requeue task to run from session workqueue
14969c19a7d0SMike Christie  * @task: task to requeue
1497843c0a8aSMike Christie  *
14985923d64bSMike Christie  * Callers must have taken a ref to the task that is going to be requeued.
1499843c0a8aSMike Christie  */
15009c19a7d0SMike Christie void iscsi_requeue_task(struct iscsi_task *task)
1501843c0a8aSMike Christie {
15029c19a7d0SMike Christie 	struct iscsi_conn *conn = task->conn;
1503843c0a8aSMike Christie 
15043bbaaad9SMike Christie 	/*
15053bbaaad9SMike Christie 	 * this may be on the requeue list already if the xmit_task callout
15063bbaaad9SMike Christie 	 * is handling the r2ts while we are adding new ones
15073bbaaad9SMike Christie 	 */
15085923d64bSMike Christie 	spin_lock_bh(&conn->session->frwd_lock);
15095923d64bSMike Christie 	if (list_empty(&task->running)) {
15103bbaaad9SMike Christie 		list_add_tail(&task->running, &conn->requeue);
15115923d64bSMike Christie 	} else {
15125923d64bSMike Christie 		/*
15135923d64bSMike Christie 		 * Don't need the extra ref since it's already requeued and
15145923d64bSMike Christie 		 * has a ref.
15155923d64bSMike Christie 		 */
15165923d64bSMike Christie 		iscsi_put_task(task);
15175923d64bSMike Christie 	}
151832ae763eSMike Christie 	iscsi_conn_queue_work(conn);
15195923d64bSMike Christie 	spin_unlock_bh(&conn->session->frwd_lock);
1520843c0a8aSMike Christie }
15219c19a7d0SMike Christie EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1522843c0a8aSMike Christie 
1523843c0a8aSMike Christie /**
15247996a778SMike Christie  * iscsi_data_xmit - xmit any command into the scheduled connection
15257996a778SMike Christie  * @conn: iscsi connection
15267996a778SMike Christie  *
15277996a778SMike Christie  * Notes:
15287996a778SMike Christie  *	The function can return -EAGAIN in which case the caller must
15297996a778SMike Christie  *	re-schedule it again later or recover. '0' return code means
15307996a778SMike Christie  *	successful xmit.
15317996a778SMike Christie  **/
15327996a778SMike Christie static int iscsi_data_xmit(struct iscsi_conn *conn)
15337996a778SMike Christie {
15345d12c05eSMike Christie 	struct iscsi_task *task;
15353219e529SMike Christie 	int rc = 0;
15367996a778SMike Christie 
1537659743b0SShlomo Pongratz 	spin_lock_bh(&conn->session->frwd_lock);
153870b31c15SMike Christie 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
15391b2c7af8SMike Christie 		ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1540659743b0SShlomo Pongratz 		spin_unlock_bh(&conn->session->frwd_lock);
15413219e529SMike Christie 		return -ENODATA;
15427996a778SMike Christie 	}
15437996a778SMike Christie 
15449c19a7d0SMike Christie 	if (conn->task) {
15455923d64bSMike Christie 		rc = iscsi_xmit_task(conn, conn->task, false);
15463219e529SMike Christie 	        if (rc)
154770b31c15SMike Christie 		        goto done;
15487996a778SMike Christie 	}
154977a23c21SMike Christie 
155077a23c21SMike Christie 	/*
155177a23c21SMike Christie 	 * process mgmt pdus like nops before commands since we should
155277a23c21SMike Christie 	 * only have one nop-out as a ping from us and targets should not
155377a23c21SMike Christie 	 * overflow us with nop-ins
155477a23c21SMike Christie 	 */
155577a23c21SMike Christie check_mgmt:
1556843c0a8aSMike Christie 	while (!list_empty(&conn->mgmtqueue)) {
15575923d64bSMike Christie 		task = list_entry(conn->mgmtqueue.next, struct iscsi_task,
15585923d64bSMike Christie 				  running);
15595923d64bSMike Christie 		list_del_init(&task->running);
15605923d64bSMike Christie 		if (iscsi_prep_mgmt_task(conn, task)) {
1561659743b0SShlomo Pongratz 			/* regular RX path uses back_lock */
1562659743b0SShlomo Pongratz 			spin_lock_bh(&conn->session->back_lock);
15635923d64bSMike Christie 			__iscsi_put_task(task);
1564659743b0SShlomo Pongratz 			spin_unlock_bh(&conn->session->back_lock);
1565b3a7ea8dSMike Christie 			continue;
1566b3a7ea8dSMike Christie 		}
15675923d64bSMike Christie 		rc = iscsi_xmit_task(conn, task, false);
15683219e529SMike Christie 		if (rc)
156970b31c15SMike Christie 			goto done;
15707996a778SMike Christie 	}
15717996a778SMike Christie 
1572843c0a8aSMike Christie 	/* process pending command queue */
15733bbaaad9SMike Christie 	while (!list_empty(&conn->cmdqueue)) {
15745923d64bSMike Christie 		task = list_entry(conn->cmdqueue.next, struct iscsi_task,
15755d12c05eSMike Christie 				  running);
15765923d64bSMike Christie 		list_del_init(&task->running);
1577b3a7ea8dSMike Christie 		if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
15785923d64bSMike Christie 			fail_scsi_task(task, DID_IMM_RETRY);
1579b3a7ea8dSMike Christie 			continue;
1580b3a7ea8dSMike Christie 		}
15815923d64bSMike Christie 		rc = iscsi_prep_scsi_cmd_pdu(task);
1582577577daSMike Christie 		if (rc) {
1583d28d48c6SMike Christie 			if (rc == -ENOMEM || rc == -EACCES)
15845923d64bSMike Christie 				fail_scsi_task(task, DID_IMM_RETRY);
1585d28d48c6SMike Christie 			else
15865923d64bSMike Christie 				fail_scsi_task(task, DID_ABORT);
1587004d6530SBoaz Harrosh 			continue;
1588004d6530SBoaz Harrosh 		}
15895923d64bSMike Christie 		rc = iscsi_xmit_task(conn, task, false);
15903219e529SMike Christie 		if (rc)
159170b31c15SMike Christie 			goto done;
159277a23c21SMike Christie 		/*
15939c19a7d0SMike Christie 		 * we could continuously get new task requests so
159477a23c21SMike Christie 		 * we need to check the mgmt queue for nops that need to
159577a23c21SMike Christie 		 * be sent to aviod starvation
159677a23c21SMike Christie 		 */
1597843c0a8aSMike Christie 		if (!list_empty(&conn->mgmtqueue))
1598843c0a8aSMike Christie 			goto check_mgmt;
1599843c0a8aSMike Christie 	}
1600843c0a8aSMike Christie 
1601843c0a8aSMike Christie 	while (!list_empty(&conn->requeue)) {
1602b3a7ea8dSMike Christie 		/*
1603b3a7ea8dSMike Christie 		 * we always do fastlogout - conn stop code will clean up.
1604b3a7ea8dSMike Christie 		 */
1605b3a7ea8dSMike Christie 		if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1606b3a7ea8dSMike Christie 			break;
1607b3a7ea8dSMike Christie 
16085d12c05eSMike Christie 		task = list_entry(conn->requeue.next, struct iscsi_task,
16095d12c05eSMike Christie 				  running);
16105923d64bSMike Christie 
16115d12c05eSMike Christie 		if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
16125d12c05eSMike Christie 			break;
16135d12c05eSMike Christie 
16145923d64bSMike Christie 		list_del_init(&task->running);
16155923d64bSMike Christie 		rc = iscsi_xmit_task(conn, task, true);
1616843c0a8aSMike Christie 		if (rc)
161770b31c15SMike Christie 			goto done;
1618843c0a8aSMike Christie 		if (!list_empty(&conn->mgmtqueue))
161977a23c21SMike Christie 			goto check_mgmt;
16207996a778SMike Christie 	}
1621659743b0SShlomo Pongratz 	spin_unlock_bh(&conn->session->frwd_lock);
16223219e529SMike Christie 	return -ENODATA;
16237996a778SMike Christie 
162470b31c15SMike Christie done:
1625659743b0SShlomo Pongratz 	spin_unlock_bh(&conn->session->frwd_lock);
16263219e529SMike Christie 	return rc;
16277996a778SMike Christie }
16287996a778SMike Christie 
1629c4028958SDavid Howells static void iscsi_xmitworker(struct work_struct *work)
16307996a778SMike Christie {
1631c4028958SDavid Howells 	struct iscsi_conn *conn =
1632c4028958SDavid Howells 		container_of(work, struct iscsi_conn, xmitwork);
16333219e529SMike Christie 	int rc;
16347996a778SMike Christie 	/*
16357996a778SMike Christie 	 * serialize Xmit worker on a per-connection basis.
16367996a778SMike Christie 	 */
16373219e529SMike Christie 	do {
16383219e529SMike Christie 		rc = iscsi_data_xmit(conn);
16393219e529SMike Christie 	} while (rc >= 0 || rc == -EAGAIN);
16407996a778SMike Christie }
16417996a778SMike Christie 
1642577577daSMike Christie static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1643577577daSMike Christie 						  struct scsi_cmnd *sc)
1644577577daSMike Christie {
1645577577daSMike Christie 	struct iscsi_task *task;
1646577577daSMike Christie 
16477acd72ebSStefani Seibold 	if (!kfifo_out(&conn->session->cmdpool.queue,
1648577577daSMike Christie 			 (void *) &task, sizeof(void *)))
1649577577daSMike Christie 		return NULL;
1650577577daSMike Christie 
1651577577daSMike Christie 	sc->SCp.phase = conn->session->age;
1652577577daSMike Christie 	sc->SCp.ptr = (char *) task;
1653577577daSMike Christie 
16546dc618cdSElena Reshetova 	refcount_set(&task->refcount, 1);
1655577577daSMike Christie 	task->state = ISCSI_TASK_PENDING;
1656577577daSMike Christie 	task->conn = conn;
1657577577daSMike Christie 	task->sc = sc;
1658d355e57dSMike Christie 	task->have_checked_conn = false;
1659d355e57dSMike Christie 	task->last_timeout = jiffies;
1660d355e57dSMike Christie 	task->last_xfer = jiffies;
166155e51edaSSagi Grimberg 	task->protected = false;
1662577577daSMike Christie 	INIT_LIST_HEAD(&task->running);
1663577577daSMike Christie 	return task;
1664577577daSMike Christie }
1665577577daSMike Christie 
16667996a778SMike Christie enum {
16677996a778SMike Christie 	FAILURE_BAD_HOST = 1,
16687996a778SMike Christie 	FAILURE_SESSION_FAILED,
16697996a778SMike Christie 	FAILURE_SESSION_FREED,
16707996a778SMike Christie 	FAILURE_WINDOW_CLOSED,
167160ecebf5SMike Christie 	FAILURE_OOM,
16727996a778SMike Christie 	FAILURE_SESSION_TERMINATE,
1673656cffc9SMike Christie 	FAILURE_SESSION_IN_RECOVERY,
16747996a778SMike Christie 	FAILURE_SESSION_RECOVERY_TIMEOUT,
1675b3a7ea8dSMike Christie 	FAILURE_SESSION_LOGGING_OUT,
16766eabafbeSMike Christie 	FAILURE_SESSION_NOT_READY,
16777996a778SMike Christie };
16787996a778SMike Christie 
1679f41d4721SMike Christie int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
16807996a778SMike Christie {
168175613521SMike Christie 	struct iscsi_cls_session *cls_session;
16821336aed1SMike Christie 	struct iscsi_host *ihost;
16837996a778SMike Christie 	int reason = 0;
16847996a778SMike Christie 	struct iscsi_session *session;
16857996a778SMike Christie 	struct iscsi_conn *conn;
16869c19a7d0SMike Christie 	struct iscsi_task *task = NULL;
16877996a778SMike Christie 
16887996a778SMike Christie 	sc->result = 0;
1689f47f2cf5SMike Christie 	sc->SCp.ptr = NULL;
16907996a778SMike Christie 
16911336aed1SMike Christie 	ihost = shost_priv(host);
16927996a778SMike Christie 
169375613521SMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
169475613521SMike Christie 	session = cls_session->dd_data;
1695659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
16967996a778SMike Christie 
169775613521SMike Christie 	reason = iscsi_session_chkready(cls_session);
16986eabafbeSMike Christie 	if (reason) {
16996eabafbeSMike Christie 		sc->result = reason;
17006eabafbeSMike Christie 		goto fault;
17016eabafbeSMike Christie 	}
17026eabafbeSMike Christie 
1703301e0f7eSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN) {
1704656cffc9SMike Christie 		/*
1705656cffc9SMike Christie 		 * to handle the race between when we set the recovery state
1706656cffc9SMike Christie 		 * and block the session we requeue here (commands could
1707656cffc9SMike Christie 		 * be entering our queuecommand while a block is starting
1708656cffc9SMike Christie 		 * up because the block code is not locked)
1709656cffc9SMike Christie 		 */
17109000bcd6SMike Christie 		switch (session->state) {
1711301e0f7eSMike Christie 		case ISCSI_STATE_FAILED:
1712d7549412SRafael David Tinoco 			/*
1713d7549412SRafael David Tinoco 			 * cmds should fail during shutdown, if the session
1714d7549412SRafael David Tinoco 			 * state is bad, allowing completion to happen
1715d7549412SRafael David Tinoco 			 */
1716d7549412SRafael David Tinoco 			if (unlikely(system_state != SYSTEM_RUNNING)) {
1717d7549412SRafael David Tinoco 				reason = FAILURE_SESSION_FAILED;
1718d7549412SRafael David Tinoco 				sc->result = DID_NO_CONNECT << 16;
1719d7549412SRafael David Tinoco 				break;
1720d7549412SRafael David Tinoco 			}
1721df561f66SGustavo A. R. Silva 			fallthrough;
17229000bcd6SMike Christie 		case ISCSI_STATE_IN_RECOVERY:
1723656cffc9SMike Christie 			reason = FAILURE_SESSION_IN_RECOVERY;
1724301e0f7eSMike Christie 			sc->result = DID_IMM_RETRY << 16;
1725301e0f7eSMike Christie 			break;
17269000bcd6SMike Christie 		case ISCSI_STATE_LOGGING_OUT:
17279000bcd6SMike Christie 			reason = FAILURE_SESSION_LOGGING_OUT;
1728301e0f7eSMike Christie 			sc->result = DID_IMM_RETRY << 16;
1729301e0f7eSMike Christie 			break;
1730b3a7ea8dSMike Christie 		case ISCSI_STATE_RECOVERY_FAILED:
1731656cffc9SMike Christie 			reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
173256d7fcfaSMike Christie 			sc->result = DID_TRANSPORT_FAILFAST << 16;
1733b3a7ea8dSMike Christie 			break;
1734b3a7ea8dSMike Christie 		case ISCSI_STATE_TERMINATE:
1735656cffc9SMike Christie 			reason = FAILURE_SESSION_TERMINATE;
17366eabafbeSMike Christie 			sc->result = DID_NO_CONNECT << 16;
1737b3a7ea8dSMike Christie 			break;
1738b3a7ea8dSMike Christie 		default:
17397996a778SMike Christie 			reason = FAILURE_SESSION_FREED;
17406eabafbeSMike Christie 			sc->result = DID_NO_CONNECT << 16;
1741b3a7ea8dSMike Christie 		}
17427996a778SMike Christie 		goto fault;
17437996a778SMike Christie 	}
17447996a778SMike Christie 
17457996a778SMike Christie 	conn = session->leadconn;
174698644047SMike Christie 	if (!conn) {
174798644047SMike Christie 		reason = FAILURE_SESSION_FREED;
17486eabafbeSMike Christie 		sc->result = DID_NO_CONNECT << 16;
174998644047SMike Christie 		goto fault;
175098644047SMike Christie 	}
17517996a778SMike Christie 
1752661134adSMike Christie 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1753661134adSMike Christie 		reason = FAILURE_SESSION_IN_RECOVERY;
1754eef9ffdfSJohannes Thumshirn 		sc->result = DID_REQUEUE << 16;
1755661134adSMike Christie 		goto fault;
1756661134adSMike Christie 	}
1757661134adSMike Christie 
175877a23c21SMike Christie 	if (iscsi_check_cmdsn_window_closed(conn)) {
175977a23c21SMike Christie 		reason = FAILURE_WINDOW_CLOSED;
176077a23c21SMike Christie 		goto reject;
176177a23c21SMike Christie 	}
176277a23c21SMike Christie 
1763577577daSMike Christie 	task = iscsi_alloc_task(conn, sc);
1764577577daSMike Christie 	if (!task) {
176560ecebf5SMike Christie 		reason = FAILURE_OOM;
176660ecebf5SMike Christie 		goto reject;
176760ecebf5SMike Christie 	}
17687996a778SMike Christie 
17691336aed1SMike Christie 	if (!ihost->workq) {
1770577577daSMike Christie 		reason = iscsi_prep_scsi_cmd_pdu(task);
1771577577daSMike Christie 		if (reason) {
17725d12c05eSMike Christie 			if (reason == -ENOMEM ||  reason == -EACCES) {
1773577577daSMike Christie 				reason = FAILURE_OOM;
1774577577daSMike Christie 				goto prepd_reject;
1775577577daSMike Christie 			} else {
1776052d0144SMike Christie 				sc->result = DID_ABORT << 16;
1777577577daSMike Christie 				goto prepd_fault;
1778577577daSMike Christie 			}
1779052d0144SMike Christie 		}
17809c19a7d0SMike Christie 		if (session->tt->xmit_task(task)) {
1781d3305f34SMike Christie 			session->cmdsn--;
1782052d0144SMike Christie 			reason = FAILURE_SESSION_NOT_READY;
1783577577daSMike Christie 			goto prepd_reject;
1784052d0144SMike Christie 		}
17853bbaaad9SMike Christie 	} else {
17863bbaaad9SMike Christie 		list_add_tail(&task->running, &conn->cmdqueue);
178732ae763eSMike Christie 		iscsi_conn_queue_work(conn);
17883bbaaad9SMike Christie 	}
1789052d0144SMike Christie 
1790052d0144SMike Christie 	session->queued_cmdsn++;
1791659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
17927996a778SMike Christie 	return 0;
17937996a778SMike Christie 
1794577577daSMike Christie prepd_reject:
1795a656183eSLee Duncan 	spin_lock_bh(&session->back_lock);
1796f41d4721SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1797a656183eSLee Duncan 	spin_unlock_bh(&session->back_lock);
17987996a778SMike Christie reject:
1799659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
18001b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
18011b2c7af8SMike Christie 			  sc->cmnd[0], reason);
1802d6d13ee1SMike Christie 	return SCSI_MLQUEUE_TARGET_BUSY;
18037996a778SMike Christie 
1804577577daSMike Christie prepd_fault:
1805a656183eSLee Duncan 	spin_lock_bh(&session->back_lock);
1806f41d4721SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1807a656183eSLee Duncan 	spin_unlock_bh(&session->back_lock);
18087996a778SMike Christie fault:
1809659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
18101b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
18111b2c7af8SMike Christie 			  sc->cmnd[0], reason);
18121c138991SFUJITA Tomonori 	scsi_set_resid(sc, scsi_bufflen(sc));
1813f41d4721SMike Christie 	sc->scsi_done(sc);
18147996a778SMike Christie 	return 0;
18157996a778SMike Christie }
18167996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_queuecommand);
18177996a778SMike Christie 
18186b5d6c44SMike Christie int iscsi_target_alloc(struct scsi_target *starget)
18196b5d6c44SMike Christie {
18206b5d6c44SMike Christie 	struct iscsi_cls_session *cls_session = starget_to_session(starget);
18216b5d6c44SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
18226b5d6c44SMike Christie 
18236b5d6c44SMike Christie 	starget->can_queue = session->scsi_cmds_max;
18246b5d6c44SMike Christie 	return 0;
18256b5d6c44SMike Christie }
18266b5d6c44SMike Christie EXPORT_SYMBOL_GPL(iscsi_target_alloc);
18276b5d6c44SMike Christie 
18282c4b9637SKees Cook static void iscsi_tmf_timedout(struct timer_list *t)
18297996a778SMike Christie {
1830ec29d0acSMike Christie 	struct iscsi_session *session = from_timer(session, t, tmf_timer);
18317996a778SMike Christie 
1832659743b0SShlomo Pongratz 	spin_lock(&session->frwd_lock);
1833ec29d0acSMike Christie 	if (session->tmf_state == TMF_QUEUED) {
1834ec29d0acSMike Christie 		session->tmf_state = TMF_TIMEDOUT;
1835bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "tmf timedout\n");
18367996a778SMike Christie 		/* unblock eh_abort() */
1837ec29d0acSMike Christie 		wake_up(&session->ehwait);
18387996a778SMike Christie 	}
1839659743b0SShlomo Pongratz 	spin_unlock(&session->frwd_lock);
18407996a778SMike Christie }
18417996a778SMike Christie 
18429c19a7d0SMike Christie static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1843f6d5180cSMike Christie 				   struct iscsi_tm *hdr, int age,
1844f6d5180cSMike Christie 				   int timeout)
18451360c58aSBart Van Assche 	__must_hold(&session->frwd_lock)
18467996a778SMike Christie {
18477996a778SMike Christie 	struct iscsi_session *session = conn->session;
18489c19a7d0SMike Christie 	struct iscsi_task *task;
18497996a778SMike Christie 
18509c19a7d0SMike Christie 	task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
18517996a778SMike Christie 				      NULL, 0);
18529c19a7d0SMike Christie 	if (!task) {
1853659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
1854df4da5cdSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
18557996a778SMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1856659743b0SShlomo Pongratz 		spin_lock_bh(&session->frwd_lock);
185777a23c21SMike Christie 		return -EPERM;
18587996a778SMike Christie 	}
18597996a778SMike Christie 	conn->tmfcmd_pdus_cnt++;
1860ec29d0acSMike Christie 	session->tmf_timer.expires = timeout * HZ + jiffies;
1861ec29d0acSMike Christie 	add_timer(&session->tmf_timer);
1862bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "tmf set timeout\n");
1863843c0a8aSMike Christie 
1864659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
18656724add1SMike Christie 	mutex_unlock(&session->eh_mutex);
18667996a778SMike Christie 
18677996a778SMike Christie 	/*
18687996a778SMike Christie 	 * block eh thread until:
18697996a778SMike Christie 	 *
1870843c0a8aSMike Christie 	 * 1) tmf response
1871843c0a8aSMike Christie 	 * 2) tmf timeout
18727996a778SMike Christie 	 * 3) session is terminated or restarted or userspace has
18737996a778SMike Christie 	 * given up on recovery
18747996a778SMike Christie 	 */
1875ec29d0acSMike Christie 	wait_event_interruptible(session->ehwait, age != session->age ||
18767996a778SMike Christie 				 session->state != ISCSI_STATE_LOGGED_IN ||
1877ec29d0acSMike Christie 				 session->tmf_state != TMF_QUEUED);
18787996a778SMike Christie 	if (signal_pending(current))
18797996a778SMike Christie 		flush_signals(current);
1880ec29d0acSMike Christie 	del_timer_sync(&session->tmf_timer);
1881843c0a8aSMike Christie 
18826724add1SMike Christie 	mutex_lock(&session->eh_mutex);
1883659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
18849c19a7d0SMike Christie 	/* if the session drops it will clean up the task */
1885843c0a8aSMike Christie 	if (age != session->age ||
1886843c0a8aSMike Christie 	    session->state != ISCSI_STATE_LOGGED_IN)
1887843c0a8aSMike Christie 		return -ENOTCONN;
18887996a778SMike Christie 	return 0;
18897996a778SMike Christie }
18907996a778SMike Christie 
18917996a778SMike Christie /*
189214936b1eSMike Christie  * Fail commands. session frwd lock held and xmit thread flushed.
1893843c0a8aSMike Christie  */
18949cb78c16SHannes Reinecke static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
1895843c0a8aSMike Christie {
189614936b1eSMike Christie 	struct iscsi_session *session = conn->session;
18973bbaaad9SMike Christie 	struct iscsi_task *task;
18983bbaaad9SMike Christie 	int i;
1899843c0a8aSMike Christie 
190014936b1eSMike Christie 	spin_lock_bh(&session->back_lock);
190114936b1eSMike Christie 	for (i = 0; i < session->cmds_max; i++) {
190214936b1eSMike Christie 		task = session->cmds[i];
19033bbaaad9SMike Christie 		if (!task->sc || task->state == ISCSI_TASK_FREE)
19043bbaaad9SMike Christie 			continue;
1905843c0a8aSMike Christie 
19063bbaaad9SMike Christie 		if (lun != -1 && lun != task->sc->device->lun)
19073bbaaad9SMike Christie 			continue;
19083bbaaad9SMike Christie 
190914936b1eSMike Christie 		__iscsi_get_task(task);
191014936b1eSMike Christie 		spin_unlock_bh(&session->back_lock);
191114936b1eSMike Christie 
191214936b1eSMike Christie 		ISCSI_DBG_SESSION(session,
19133bbaaad9SMike Christie 				  "failing sc %p itt 0x%x state %d\n",
19143bbaaad9SMike Christie 				  task->sc, task->itt, task->state);
1915b3cd5050SMike Christie 		fail_scsi_task(task, error);
191614936b1eSMike Christie 
191714936b1eSMike Christie 		spin_unlock_bh(&session->frwd_lock);
191814936b1eSMike Christie 		iscsi_put_task(task);
191914936b1eSMike Christie 		spin_lock_bh(&session->frwd_lock);
192014936b1eSMike Christie 
192114936b1eSMike Christie 		spin_lock_bh(&session->back_lock);
1922843c0a8aSMike Christie 	}
192314936b1eSMike Christie 
192414936b1eSMike Christie 	spin_unlock_bh(&session->back_lock);
1925843c0a8aSMike Christie }
1926843c0a8aSMike Christie 
1927661134adSMike Christie /**
1928661134adSMike Christie  * iscsi_suspend_queue - suspend iscsi_queuecommand
1929661134adSMike Christie  * @conn: iscsi conn to stop queueing IO on
1930661134adSMike Christie  *
1931659743b0SShlomo Pongratz  * This grabs the session frwd_lock to make sure no one is in
1932661134adSMike Christie  * xmit_task/queuecommand, and then sets suspend to prevent
1933661134adSMike Christie  * new commands from being queued. This only needs to be called
1934661134adSMike Christie  * by offload drivers that need to sync a path like ep disconnect
1935661134adSMike Christie  * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1936661134adSMike Christie  * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1937661134adSMike Christie  */
1938661134adSMike Christie void iscsi_suspend_queue(struct iscsi_conn *conn)
1939661134adSMike Christie {
1940659743b0SShlomo Pongratz 	spin_lock_bh(&conn->session->frwd_lock);
1941661134adSMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1942659743b0SShlomo Pongratz 	spin_unlock_bh(&conn->session->frwd_lock);
1943661134adSMike Christie }
1944661134adSMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1945661134adSMike Christie 
1946661134adSMike Christie /**
1947661134adSMike Christie  * iscsi_suspend_tx - suspend iscsi_data_xmit
1948661134adSMike Christie  * @conn: iscsi conn tp stop processing IO on.
1949661134adSMike Christie  *
1950661134adSMike Christie  * This function sets the suspend bit to prevent iscsi_data_xmit
1951661134adSMike Christie  * from sending new IO, and if work is queued on the xmit thread
1952661134adSMike Christie  * it will wait for it to be completed.
1953661134adSMike Christie  */
1954b40977d9SMike Christie void iscsi_suspend_tx(struct iscsi_conn *conn)
19556724add1SMike Christie {
195632ae763eSMike Christie 	struct Scsi_Host *shost = conn->session->host;
195732ae763eSMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
195832ae763eSMike Christie 
19596724add1SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
19601336aed1SMike Christie 	if (ihost->workq)
196132ae763eSMike Christie 		flush_workqueue(ihost->workq);
19626724add1SMike Christie }
1963b40977d9SMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
19646724add1SMike Christie 
19656724add1SMike Christie static void iscsi_start_tx(struct iscsi_conn *conn)
19666724add1SMike Christie {
19676724add1SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
196832ae763eSMike Christie 	iscsi_conn_queue_work(conn);
19696724add1SMike Christie }
19706724add1SMike Christie 
19714c48a829SMike Christie /*
19724c48a829SMike Christie  * We want to make sure a ping is in flight. It has timed out.
19734c48a829SMike Christie  * And we are not busy processing a pdu that is making
19744c48a829SMike Christie  * progress but got started before the ping and is taking a while
19754c48a829SMike Christie  * to complete so the ping is just stuck behind it in a queue.
19764c48a829SMike Christie  */
19774c48a829SMike Christie static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
19784c48a829SMike Christie {
1979fe0a8a95SLee Duncan 	if (READ_ONCE(conn->ping_task) &&
19804c48a829SMike Christie 	    time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
19814c48a829SMike Christie 			   (conn->ping_timeout * HZ), jiffies))
19824c48a829SMike Christie 		return 1;
19834c48a829SMike Christie 	else
19844c48a829SMike Christie 		return 0;
19854c48a829SMike Christie }
19864c48a829SMike Christie 
1987b6a05c82SChristoph Hellwig enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1988f6d5180cSMike Christie {
19896600593cSChristoph Hellwig 	enum blk_eh_timer_return rc = BLK_EH_DONE;
199092ed4d69SMike Christie 	struct iscsi_task *task = NULL, *running_task;
1991f6d5180cSMike Christie 	struct iscsi_cls_session *cls_session;
1992f6d5180cSMike Christie 	struct iscsi_session *session;
1993f6d5180cSMike Christie 	struct iscsi_conn *conn;
199492ed4d69SMike Christie 	int i;
1995f6d5180cSMike Christie 
1996d355e57dSMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
199775613521SMike Christie 	session = cls_session->dd_data;
1998f6d5180cSMike Christie 
1999bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
2000f6d5180cSMike Christie 
20015480e299SBart Van Assche 	spin_lock_bh(&session->frwd_lock);
200214936b1eSMike Christie 	spin_lock(&session->back_lock);
2003e3d338a5SMike Christie 	task = (struct iscsi_task *)sc->SCp.ptr;
2004e3d338a5SMike Christie 	if (!task) {
2005e3d338a5SMike Christie 		/*
2006e3d338a5SMike Christie 		 * Raced with completion. Blk layer has taken ownership
2007e3d338a5SMike Christie 		 * so let timeout code complete it now.
2008e3d338a5SMike Christie 		 */
2009adb2b769SChristoph Hellwig 		rc = BLK_EH_DONE;
201014936b1eSMike Christie 		spin_unlock(&session->back_lock);
2011e3d338a5SMike Christie 		goto done;
2012e3d338a5SMike Christie 	}
201314936b1eSMike Christie 	__iscsi_get_task(task);
201414936b1eSMike Christie 	spin_unlock(&session->back_lock);
2015e3d338a5SMike Christie 
2016f6d5180cSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN) {
2017f6d5180cSMike Christie 		/*
2018d7549412SRafael David Tinoco 		 * During shutdown, if session is prematurely disconnected,
2019d7549412SRafael David Tinoco 		 * recovery won't happen and there will be hung cmds. Not
2020d7549412SRafael David Tinoco 		 * handling cmds would trigger EH, also bad in this case.
2021d7549412SRafael David Tinoco 		 * Instead, handle cmd, allow completion to happen and let
2022d7549412SRafael David Tinoco 		 * upper layer to deal with the result.
2023d7549412SRafael David Tinoco 		 */
2024d7549412SRafael David Tinoco 		if (unlikely(system_state != SYSTEM_RUNNING)) {
2025d7549412SRafael David Tinoco 			sc->result = DID_NO_CONNECT << 16;
2026d7549412SRafael David Tinoco 			ISCSI_DBG_EH(session, "sc on shutdown, handled\n");
2027adb2b769SChristoph Hellwig 			rc = BLK_EH_DONE;
2028d7549412SRafael David Tinoco 			goto done;
2029d7549412SRafael David Tinoco 		}
2030d7549412SRafael David Tinoco 		/*
2031f6d5180cSMike Christie 		 * We are probably in the middle of iscsi recovery so let
2032f6d5180cSMike Christie 		 * that complete and handle the error.
2033f6d5180cSMike Christie 		 */
2034242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
2035f6d5180cSMike Christie 		goto done;
2036f6d5180cSMike Christie 	}
2037f6d5180cSMike Christie 
2038f6d5180cSMike Christie 	conn = session->leadconn;
2039f6d5180cSMike Christie 	if (!conn) {
2040f6d5180cSMike Christie 		/* In the middle of shuting down */
2041242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
2042f6d5180cSMike Christie 		goto done;
2043f6d5180cSMike Christie 	}
2044f6d5180cSMike Christie 
2045d355e57dSMike Christie 	/*
2046d355e57dSMike Christie 	 * If we have sent (at least queued to the network layer) a pdu or
2047d355e57dSMike Christie 	 * recvd one for the task since the last timeout ask for
2048d355e57dSMike Christie 	 * more time. If on the next timeout we have not made progress
2049d355e57dSMike Christie 	 * we can check if it is the task or connection when we send the
2050d355e57dSMike Christie 	 * nop as a ping.
2051d355e57dSMike Christie 	 */
205292ed4d69SMike Christie 	if (time_after(task->last_xfer, task->last_timeout)) {
2053bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "Command making progress. Asking "
2054d355e57dSMike Christie 			     "scsi-ml for more time to complete. "
205592ed4d69SMike Christie 			     "Last data xfer at %lu. Last timeout was at "
2056d355e57dSMike Christie 			     "%lu\n.", task->last_xfer, task->last_timeout);
2057d355e57dSMike Christie 		task->have_checked_conn = false;
2058d355e57dSMike Christie 		rc = BLK_EH_RESET_TIMER;
2059d355e57dSMike Christie 		goto done;
2060d355e57dSMike Christie 	}
2061d355e57dSMike Christie 
2062f6d5180cSMike Christie 	if (!conn->recv_timeout && !conn->ping_timeout)
2063f6d5180cSMike Christie 		goto done;
2064f6d5180cSMike Christie 	/*
2065f6d5180cSMike Christie 	 * if the ping timedout then we are in the middle of cleaning up
2066f6d5180cSMike Christie 	 * and can let the iscsi eh handle it
2067f6d5180cSMike Christie 	 */
20684c48a829SMike Christie 	if (iscsi_has_ping_timed_out(conn)) {
2069242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
20704c48a829SMike Christie 		goto done;
20714c48a829SMike Christie 	}
2072d355e57dSMike Christie 
207314936b1eSMike Christie 	spin_lock(&session->back_lock);
207492ed4d69SMike Christie 	for (i = 0; i < conn->session->cmds_max; i++) {
207592ed4d69SMike Christie 		running_task = conn->session->cmds[i];
207692ed4d69SMike Christie 		if (!running_task->sc || running_task == task ||
207792ed4d69SMike Christie 		     running_task->state != ISCSI_TASK_RUNNING)
207892ed4d69SMike Christie 			continue;
207992ed4d69SMike Christie 
208092ed4d69SMike Christie 		/*
208192ed4d69SMike Christie 		 * Only check if cmds started before this one have made
208292ed4d69SMike Christie 		 * progress, or this could never fail
208392ed4d69SMike Christie 		 */
208492ed4d69SMike Christie 		if (time_after(running_task->sc->jiffies_at_alloc,
208592ed4d69SMike Christie 			       task->sc->jiffies_at_alloc))
208692ed4d69SMike Christie 			continue;
208792ed4d69SMike Christie 
208892ed4d69SMike Christie 		if (time_after(running_task->last_xfer, task->last_timeout)) {
208992ed4d69SMike Christie 			/*
209092ed4d69SMike Christie 			 * This task has not made progress, but a task
209192ed4d69SMike Christie 			 * started before us has transferred data since
209292ed4d69SMike Christie 			 * we started/last-checked. We could be queueing
209392ed4d69SMike Christie 			 * too many tasks or the LU is bad.
209492ed4d69SMike Christie 			 *
209592ed4d69SMike Christie 			 * If the device is bad the cmds ahead of us on
209692ed4d69SMike Christie 			 * other devs will complete, and this loop will
209792ed4d69SMike Christie 			 * eventually fail starting the scsi eh.
209892ed4d69SMike Christie 			 */
209992ed4d69SMike Christie 			ISCSI_DBG_EH(session, "Command has not made progress "
210092ed4d69SMike Christie 				     "but commands ahead of it have. "
210192ed4d69SMike Christie 				     "Asking scsi-ml for more time to "
210292ed4d69SMike Christie 				     "complete. Our last xfer vs running task "
210392ed4d69SMike Christie 				     "last xfer %lu/%lu. Last check %lu.\n",
210492ed4d69SMike Christie 				     task->last_xfer, running_task->last_xfer,
210592ed4d69SMike Christie 				     task->last_timeout);
210614936b1eSMike Christie 			spin_unlock(&session->back_lock);
210792ed4d69SMike Christie 			rc = BLK_EH_RESET_TIMER;
210892ed4d69SMike Christie 			goto done;
210992ed4d69SMike Christie 		}
211092ed4d69SMike Christie 	}
211114936b1eSMike Christie 	spin_unlock(&session->back_lock);
211292ed4d69SMike Christie 
2113d355e57dSMike Christie 	/* Assumes nop timeout is shorter than scsi cmd timeout */
2114d355e57dSMike Christie 	if (task->have_checked_conn)
2115d355e57dSMike Christie 		goto done;
2116d355e57dSMike Christie 
2117f6d5180cSMike Christie 	/*
2118d355e57dSMike Christie 	 * Checking the transport already or nop from a cmd timeout still
2119d355e57dSMike Christie 	 * running
2120f6d5180cSMike Christie 	 */
2121fe0a8a95SLee Duncan 	if (READ_ONCE(conn->ping_task)) {
2122d355e57dSMike Christie 		task->have_checked_conn = true;
2123242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
21244c48a829SMike Christie 		goto done;
21254c48a829SMike Christie 	}
21264c48a829SMike Christie 
2127d355e57dSMike Christie 	/* Make sure there is a transport check done */
2128d355e57dSMike Christie 	iscsi_send_nopout(conn, NULL);
2129d355e57dSMike Christie 	task->have_checked_conn = true;
2130242f9dcbSJens Axboe 	rc = BLK_EH_RESET_TIMER;
2131d355e57dSMike Christie 
2132f6d5180cSMike Christie done:
21335480e299SBart Van Assche 	spin_unlock_bh(&session->frwd_lock);
213414936b1eSMike Christie 
213514936b1eSMike Christie 	if (task) {
213614936b1eSMike Christie 		task->last_timeout = jiffies;
213714936b1eSMike Christie 		iscsi_put_task(task);
213814936b1eSMike Christie 	}
2139bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2140d7549412SRafael David Tinoco 		     "timer reset" : "shutdown or nh");
2141f6d5180cSMike Christie 	return rc;
2142f6d5180cSMike Christie }
2143b6a05c82SChristoph Hellwig EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out);
2144f6d5180cSMike Christie 
21452c4b9637SKees Cook static void iscsi_check_transport_timeouts(struct timer_list *t)
2146f6d5180cSMike Christie {
21472c4b9637SKees Cook 	struct iscsi_conn *conn = from_timer(conn, t, transport_timer);
2148f6d5180cSMike Christie 	struct iscsi_session *session = conn->session;
21494cf10435SMike Christie 	unsigned long recv_timeout, next_timeout = 0, last_recv;
2150f6d5180cSMike Christie 
2151659743b0SShlomo Pongratz 	spin_lock(&session->frwd_lock);
2152f6d5180cSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN)
2153f6d5180cSMike Christie 		goto done;
2154f6d5180cSMike Christie 
21554cf10435SMike Christie 	recv_timeout = conn->recv_timeout;
21564cf10435SMike Christie 	if (!recv_timeout)
2157f6d5180cSMike Christie 		goto done;
2158f6d5180cSMike Christie 
21594cf10435SMike Christie 	recv_timeout *= HZ;
2160f6d5180cSMike Christie 	last_recv = conn->last_recv;
21614c48a829SMike Christie 
21624c48a829SMike Christie 	if (iscsi_has_ping_timed_out(conn)) {
2163322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
21644c48a829SMike Christie 				  "expired, recv timeout %d, last rx %lu, "
21654c48a829SMike Christie 				  "last ping %lu, now %lu\n",
21664c48a829SMike Christie 				  conn->ping_timeout, conn->recv_timeout,
21674c48a829SMike Christie 				  last_recv, conn->last_ping, jiffies);
2168659743b0SShlomo Pongratz 		spin_unlock(&session->frwd_lock);
216909ff742cSMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
2170f6d5180cSMike Christie 		return;
2171f6d5180cSMike Christie 	}
2172f6d5180cSMike Christie 
21734cf10435SMike Christie 	if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2174f6d5180cSMike Christie 		/* send a ping to try to provoke some traffic */
21751b2c7af8SMike Christie 		ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
217652f5664aSAriel Nahum 		if (iscsi_send_nopout(conn, NULL))
217752f5664aSAriel Nahum 			next_timeout = jiffies + (1 * HZ);
217852f5664aSAriel Nahum 		else
21794cf10435SMike Christie 			next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2180ad294e9cSMike Christie 	} else
21814cf10435SMike Christie 		next_timeout = last_recv + recv_timeout;
2182f6d5180cSMike Christie 
21831b2c7af8SMike Christie 	ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2184f6d5180cSMike Christie 	mod_timer(&conn->transport_timer, next_timeout);
2185f6d5180cSMike Christie done:
2186659743b0SShlomo Pongratz 	spin_unlock(&session->frwd_lock);
2187f6d5180cSMike Christie }
2188f6d5180cSMike Christie 
2189891e2639SMike Christie /**
2190891e2639SMike Christie  * iscsi_conn_unbind - prevent queueing to conn.
2191891e2639SMike Christie  * @cls_conn: iscsi conn ep is bound to.
2192891e2639SMike Christie  * @is_active: is the conn in use for boot or is this for EH/termination
2193891e2639SMike Christie  *
2194891e2639SMike Christie  * This must be called by drivers implementing the ep_disconnect callout.
2195891e2639SMike Christie  * It disables queueing to the connection from libiscsi in preparation for
2196891e2639SMike Christie  * an ep_disconnect call.
2197891e2639SMike Christie  */
2198891e2639SMike Christie void iscsi_conn_unbind(struct iscsi_cls_conn *cls_conn, bool is_active)
2199891e2639SMike Christie {
2200891e2639SMike Christie 	struct iscsi_session *session;
2201891e2639SMike Christie 	struct iscsi_conn *conn;
2202891e2639SMike Christie 
2203891e2639SMike Christie 	if (!cls_conn)
2204891e2639SMike Christie 		return;
2205891e2639SMike Christie 
2206891e2639SMike Christie 	conn = cls_conn->dd_data;
2207891e2639SMike Christie 	session = conn->session;
2208891e2639SMike Christie 	/*
2209891e2639SMike Christie 	 * Wait for iscsi_eh calls to exit. We don't wait for the tmf to
2210891e2639SMike Christie 	 * complete or timeout. The caller just wants to know what's running
2211891e2639SMike Christie 	 * is everything that needs to be cleaned up, and no cmds will be
2212891e2639SMike Christie 	 * queued.
2213891e2639SMike Christie 	 */
2214891e2639SMike Christie 	mutex_lock(&session->eh_mutex);
2215891e2639SMike Christie 
2216891e2639SMike Christie 	iscsi_suspend_queue(conn);
2217891e2639SMike Christie 	iscsi_suspend_tx(conn);
2218891e2639SMike Christie 
2219891e2639SMike Christie 	spin_lock_bh(&session->frwd_lock);
2220891e2639SMike Christie 	if (!is_active) {
2221891e2639SMike Christie 		/*
2222891e2639SMike Christie 		 * if logout timed out before userspace could even send a PDU
2223891e2639SMike Christie 		 * the state might still be in ISCSI_STATE_LOGGED_IN and
2224891e2639SMike Christie 		 * allowing new cmds and TMFs.
2225891e2639SMike Christie 		 */
2226891e2639SMike Christie 		if (session->state == ISCSI_STATE_LOGGED_IN)
2227891e2639SMike Christie 			iscsi_set_conn_failed(conn);
2228891e2639SMike Christie 	}
2229891e2639SMike Christie 	spin_unlock_bh(&session->frwd_lock);
2230891e2639SMike Christie 	mutex_unlock(&session->eh_mutex);
2231891e2639SMike Christie }
2232891e2639SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_unbind);
2233891e2639SMike Christie 
22349c19a7d0SMike Christie static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2235843c0a8aSMike Christie 				      struct iscsi_tm *hdr)
2236843c0a8aSMike Christie {
2237843c0a8aSMike Christie 	memset(hdr, 0, sizeof(*hdr));
2238843c0a8aSMike Christie 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2239843c0a8aSMike Christie 	hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2240843c0a8aSMike Christie 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
224155bdabdfSAndy Grover 	hdr->lun = task->lun;
2242577577daSMike Christie 	hdr->rtt = task->hdr_itt;
2243577577daSMike Christie 	hdr->refcmdsn = task->cmdsn;
2244843c0a8aSMike Christie }
2245843c0a8aSMike Christie 
22467996a778SMike Christie int iscsi_eh_abort(struct scsi_cmnd *sc)
22477996a778SMike Christie {
224875613521SMike Christie 	struct iscsi_cls_session *cls_session;
224975613521SMike Christie 	struct iscsi_session *session;
2250f47f2cf5SMike Christie 	struct iscsi_conn *conn;
22519c19a7d0SMike Christie 	struct iscsi_task *task;
2252843c0a8aSMike Christie 	struct iscsi_tm *hdr;
2253e9e410e8SBart Van Assche 	int age;
22547996a778SMike Christie 
225575613521SMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
225675613521SMike Christie 	session = cls_session->dd_data;
225775613521SMike Christie 
2258bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
22594421c9ebSMike Christie 
22606724add1SMike Christie 	mutex_lock(&session->eh_mutex);
2261659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
2262f47f2cf5SMike Christie 	/*
2263f47f2cf5SMike Christie 	 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2264f47f2cf5SMike Christie 	 * got the command.
2265f47f2cf5SMike Christie 	 */
2266f47f2cf5SMike Christie 	if (!sc->SCp.ptr) {
2267bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
22681b2c7af8SMike Christie 				      "it completed.\n");
2269659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
22706724add1SMike Christie 		mutex_unlock(&session->eh_mutex);
2271f47f2cf5SMike Christie 		return SUCCESS;
2272f47f2cf5SMike Christie 	}
2273f47f2cf5SMike Christie 
22747996a778SMike Christie 	/*
22757996a778SMike Christie 	 * If we are not logged in or we have started a new session
22767996a778SMike Christie 	 * then let the host reset code handle this
22777996a778SMike Christie 	 */
2278843c0a8aSMike Christie 	if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2279843c0a8aSMike Christie 	    sc->SCp.phase != session->age) {
2280659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
2281843c0a8aSMike Christie 		mutex_unlock(&session->eh_mutex);
2282bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "failing abort due to dropped "
22834421c9ebSMike Christie 				  "session.\n");
2284843c0a8aSMike Christie 		return FAILED;
2285843c0a8aSMike Christie 	}
2286843c0a8aSMike Christie 
2287843c0a8aSMike Christie 	conn = session->leadconn;
2288d39df158SMike Christie 	iscsi_get_conn(conn->cls_conn);
2289843c0a8aSMike Christie 	conn->eh_abort_cnt++;
2290843c0a8aSMike Christie 	age = session->age;
2291843c0a8aSMike Christie 
229214936b1eSMike Christie 	spin_lock(&session->back_lock);
22939c19a7d0SMike Christie 	task = (struct iscsi_task *)sc->SCp.ptr;
229414936b1eSMike Christie 	if (!task || !task->sc) {
22959c19a7d0SMike Christie 		/* task completed before time out */
2296bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
229714936b1eSMike Christie 
229814936b1eSMike Christie 		spin_unlock(&session->back_lock);
2299d39df158SMike Christie 		goto success;
23007ea8b828SMike 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);
2953ec29d0acSMike Christie 
2954659743b0SShlomo Pongratz 	spin_lock_init(&session->frwd_lock);
2955659743b0SShlomo Pongratz 	spin_lock_init(&session->back_lock);
29567996a778SMike Christie 
29577996a778SMike Christie 	/* initialize SCSI PDU commands pool */
29587996a778SMike Christie 	if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
29597996a778SMike Christie 			    (void***)&session->cmds,
29609c19a7d0SMike Christie 			    cmd_task_size + sizeof(struct iscsi_task)))
29617996a778SMike Christie 		goto cmdpool_alloc_fail;
29627996a778SMike Christie 
29637996a778SMike Christie 	/* pre-format cmds pool with ITT */
29647996a778SMike Christie 	for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
29659c19a7d0SMike Christie 		struct iscsi_task *task = session->cmds[cmd_i];
29667996a778SMike Christie 
29679c19a7d0SMike Christie 		if (cmd_task_size)
29689c19a7d0SMike Christie 			task->dd_data = &task[1];
29699c19a7d0SMike Christie 		task->itt = cmd_i;
29703bbaaad9SMike Christie 		task->state = ISCSI_TASK_FREE;
29719c19a7d0SMike Christie 		INIT_LIST_HEAD(&task->running);
29727996a778SMike Christie 	}
29737996a778SMike Christie 
2974f53a88daSMike Christie 	if (!try_module_get(iscsit->owner))
297575613521SMike Christie 		goto module_get_fail;
297675613521SMike Christie 
29777970634bSMike Christie 	if (iscsi_add_session(cls_session, id))
2978f53a88daSMike Christie 		goto cls_session_fail;
2979e5bd7b54SMike Christie 
29807996a778SMike Christie 	return cls_session;
29817996a778SMike Christie 
29827996a778SMike Christie cls_session_fail:
298375613521SMike Christie 	module_put(iscsit->owner);
298475613521SMike Christie module_get_fail:
29856320377fSOlaf Kirch 	iscsi_pool_free(&session->cmdpool);
29867996a778SMike Christie cmdpool_alloc_fail:
298775613521SMike Christie 	iscsi_free_session(cls_session);
2988e5bd7b54SMike Christie dec_session_count:
2989e5bd7b54SMike Christie 	iscsi_host_dec_session_cnt(shost);
29907996a778SMike Christie 	return NULL;
29917996a778SMike Christie }
29927996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_setup);
29937996a778SMike Christie 
29947996a778SMike Christie /**
29957996a778SMike Christie  * iscsi_session_teardown - destroy session, host, and cls_session
299675613521SMike Christie  * @cls_session: iscsi session
299775613521SMike Christie  */
29987996a778SMike Christie void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
29997996a778SMike Christie {
300075613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
300163f75cc8SMike Christie 	struct module *owner = cls_session->transport->owner;
3002e5bd7b54SMike Christie 	struct Scsi_Host *shost = session->host;
30037996a778SMike Christie 
30049e10b512SKhazhismel Kumykov 	iscsi_remove_session(cls_session);
30059e10b512SKhazhismel Kumykov 
3006*a1f3486bSMike Christie 	iscsi_pool_free(&session->cmdpool);
3007b2c64167SMike Christie 	kfree(session->password);
3008b2c64167SMike Christie 	kfree(session->password_in);
3009b2c64167SMike Christie 	kfree(session->username);
3010b2c64167SMike Christie 	kfree(session->username_in);
3011f3ff0c36SMike Christie 	kfree(session->targetname);
30123c5c4801SVikas Chaudhary 	kfree(session->targetalias);
301388dfd340SMike Christie 	kfree(session->initiatorname);
30143b9373e9SEddie Wai 	kfree(session->boot_root);
30153b9373e9SEddie Wai 	kfree(session->boot_nic);
30163b9373e9SEddie Wai 	kfree(session->boot_target);
301788dfd340SMike Christie 	kfree(session->ifacename);
3018f8525eb4SAdheer Chandravanshi 	kfree(session->portal_type);
3019f8525eb4SAdheer Chandravanshi 	kfree(session->discovery_parent_type);
3020f3ff0c36SMike Christie 
30219e10b512SKhazhismel Kumykov 	iscsi_free_session(cls_session);
30229e10b512SKhazhismel Kumykov 
3023e5bd7b54SMike Christie 	iscsi_host_dec_session_cnt(shost);
302463f75cc8SMike Christie 	module_put(owner);
30257996a778SMike Christie }
30267996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_teardown);
30277996a778SMike Christie 
30287996a778SMike Christie /**
30297996a778SMike Christie  * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
30307996a778SMike Christie  * @cls_session: iscsi_cls_session
30315d91e209SMike Christie  * @dd_size: private driver data size
30327996a778SMike Christie  * @conn_idx: cid
30335d91e209SMike Christie  */
30347996a778SMike Christie struct iscsi_cls_conn *
30355d91e209SMike Christie iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
30365d91e209SMike Christie 		 uint32_t conn_idx)
30377996a778SMike Christie {
303875613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
30397996a778SMike Christie 	struct iscsi_conn *conn;
30407996a778SMike Christie 	struct iscsi_cls_conn *cls_conn;
3041d36ab6f3SMike Christie 	char *data;
30427996a778SMike Christie 
30435d91e209SMike Christie 	cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
30445d91e209SMike Christie 				     conn_idx);
30457996a778SMike Christie 	if (!cls_conn)
30467996a778SMike Christie 		return NULL;
30477996a778SMike Christie 	conn = cls_conn->dd_data;
30485d91e209SMike Christie 	memset(conn, 0, sizeof(*conn) + dd_size);
30497996a778SMike Christie 
30505d91e209SMike Christie 	conn->dd_data = cls_conn->dd_data + sizeof(*conn);
30517996a778SMike Christie 	conn->session = session;
30527996a778SMike Christie 	conn->cls_conn = cls_conn;
30537996a778SMike Christie 	conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
30547996a778SMike Christie 	conn->id = conn_idx;
30557996a778SMike Christie 	conn->exp_statsn = 0;
3056f6d5180cSMike Christie 
30572c4b9637SKees Cook 	timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0);
3058f6d5180cSMike Christie 
3059843c0a8aSMike Christie 	INIT_LIST_HEAD(&conn->mgmtqueue);
30603bbaaad9SMike Christie 	INIT_LIST_HEAD(&conn->cmdqueue);
3061843c0a8aSMike Christie 	INIT_LIST_HEAD(&conn->requeue);
3062c4028958SDavid Howells 	INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
30637996a778SMike Christie 
30649c19a7d0SMike Christie 	/* allocate login_task used for the login/text sequences */
3065659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
30667acd72ebSStefani Seibold 	if (!kfifo_out(&session->cmdpool.queue,
30679c19a7d0SMike Christie                          (void*)&conn->login_task,
30687996a778SMike Christie 			 sizeof(void*))) {
3069659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
30709c19a7d0SMike Christie 		goto login_task_alloc_fail;
30717996a778SMike Christie 	}
3072659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
30737996a778SMike Christie 
3074cfeb2cf9SMike Christie 	data = (char *) __get_free_pages(GFP_KERNEL,
3075cfeb2cf9SMike Christie 					 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
3076d36ab6f3SMike Christie 	if (!data)
30779c19a7d0SMike Christie 		goto login_task_data_alloc_fail;
30789c19a7d0SMike Christie 	conn->login_task->data = conn->data = data;
3079d36ab6f3SMike Christie 
3080ec29d0acSMike Christie 	init_waitqueue_head(&session->ehwait);
30817996a778SMike Christie 
30827996a778SMike Christie 	return cls_conn;
30837996a778SMike Christie 
30849c19a7d0SMike Christie login_task_data_alloc_fail:
30857acd72ebSStefani Seibold 	kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
3086d36ab6f3SMike Christie 		    sizeof(void*));
30879c19a7d0SMike Christie login_task_alloc_fail:
30887996a778SMike Christie 	iscsi_destroy_conn(cls_conn);
30897996a778SMike Christie 	return NULL;
30907996a778SMike Christie }
30917996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_setup);
30927996a778SMike Christie 
30937996a778SMike Christie /**
30947996a778SMike Christie  * iscsi_conn_teardown - teardown iscsi connection
3095ccd4a430SRandy Dunlap  * @cls_conn: iscsi class connection
30967996a778SMike Christie  *
30977996a778SMike Christie  * TODO: we may need to make this into a two step process
30987996a778SMike Christie  * like scsi-mls remove + put host
30997996a778SMike Christie  */
31007996a778SMike Christie void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
31017996a778SMike Christie {
31027996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
31037996a778SMike Christie 	struct iscsi_session *session = conn->session;
31047996a778SMike Christie 
3105f6d5180cSMike Christie 	del_timer_sync(&conn->transport_timer);
3106f6d5180cSMike Christie 
3107660d0831SJohn Soni Jose 	mutex_lock(&session->eh_mutex);
3108659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
31097996a778SMike Christie 	conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
31107996a778SMike Christie 	if (session->leadconn == conn) {
31117996a778SMike Christie 		/*
31127996a778SMike Christie 		 * leading connection? then give up on recovery.
31137996a778SMike Christie 		 */
31147996a778SMike Christie 		session->state = ISCSI_STATE_TERMINATE;
3115ec29d0acSMike Christie 		wake_up(&session->ehwait);
31167996a778SMike Christie 	}
3117659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
31187996a778SMike Christie 
3119779ea120SMike Christie 	/* flush queued up work because we free the connection below */
3120843c0a8aSMike Christie 	iscsi_suspend_tx(conn);
3121779ea120SMike Christie 
3122659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
3123cfeb2cf9SMike Christie 	free_pages((unsigned long) conn->data,
3124cfeb2cf9SMike Christie 		   get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
3125f3ff0c36SMike Christie 	kfree(conn->persistent_address);
3126ae56ff40SAdheer Chandravanshi 	kfree(conn->local_ipaddr);
3127659743b0SShlomo Pongratz 	/* regular RX path uses back_lock */
3128659743b0SShlomo Pongratz 	spin_lock_bh(&session->back_lock);
31297acd72ebSStefani Seibold 	kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
31307996a778SMike Christie 		    sizeof(void*));
3131659743b0SShlomo Pongratz 	spin_unlock_bh(&session->back_lock);
3132e0726407SMike Christie 	if (session->leadconn == conn)
31337996a778SMike Christie 		session->leadconn = NULL;
3134659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
3135660d0831SJohn Soni Jose 	mutex_unlock(&session->eh_mutex);
31367996a778SMike Christie 
31377996a778SMike Christie 	iscsi_destroy_conn(cls_conn);
31387996a778SMike Christie }
31397996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
31407996a778SMike Christie 
31417996a778SMike Christie int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
31427996a778SMike Christie {
31437996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
31447996a778SMike Christie 	struct iscsi_session *session = conn->session;
31457996a778SMike Christie 
3146ffd0436eSMike Christie 	if (!session) {
3147322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
3148322d739dSMike Christie 				  "can't start unbound connection\n");
31497996a778SMike Christie 		return -EPERM;
31507996a778SMike Christie 	}
31517996a778SMike Christie 
3152db98ccdeSMike Christie 	if ((session->imm_data_en || !session->initial_r2t_en) &&
3153db98ccdeSMike Christie 	     session->first_burst > session->max_burst) {
3154322d739dSMike Christie 		iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
3155ffd0436eSMike Christie 				  "first_burst %d max_burst %d\n",
3156ffd0436eSMike Christie 				  session->first_burst, session->max_burst);
3157ffd0436eSMike Christie 		return -EINVAL;
3158ffd0436eSMike Christie 	}
3159ffd0436eSMike Christie 
3160f6d5180cSMike Christie 	if (conn->ping_timeout && !conn->recv_timeout) {
3161322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
3162322d739dSMike Christie 				  "zero. Using 5 seconds\n.");
3163f6d5180cSMike Christie 		conn->recv_timeout = 5;
3164f6d5180cSMike Christie 	}
3165f6d5180cSMike Christie 
3166f6d5180cSMike Christie 	if (conn->recv_timeout && !conn->ping_timeout) {
3167322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3168322d739dSMike Christie 				  "zero. Using 5 seconds.\n");
3169f6d5180cSMike Christie 		conn->ping_timeout = 5;
3170f6d5180cSMike Christie 	}
3171f6d5180cSMike Christie 
3172659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
31737996a778SMike Christie 	conn->c_stage = ISCSI_CONN_STARTED;
31747996a778SMike Christie 	session->state = ISCSI_STATE_LOGGED_IN;
3175e0726407SMike Christie 	session->queued_cmdsn = session->cmdsn;
31767996a778SMike Christie 
3177f6d5180cSMike Christie 	conn->last_recv = jiffies;
3178f6d5180cSMike Christie 	conn->last_ping = jiffies;
3179f6d5180cSMike Christie 	if (conn->recv_timeout && conn->ping_timeout)
3180f6d5180cSMike Christie 		mod_timer(&conn->transport_timer,
3181f6d5180cSMike Christie 			  jiffies + (conn->recv_timeout * HZ));
3182f6d5180cSMike Christie 
31837996a778SMike Christie 	switch(conn->stop_stage) {
31847996a778SMike Christie 	case STOP_CONN_RECOVER:
31857996a778SMike Christie 		/*
31867996a778SMike Christie 		 * unblock eh_abort() if it is blocked. re-try all
31877996a778SMike Christie 		 * commands after successful recovery
31887996a778SMike Christie 		 */
31897996a778SMike Christie 		conn->stop_stage = 0;
3190ec29d0acSMike Christie 		session->tmf_state = TMF_INITIAL;
31917996a778SMike Christie 		session->age++;
31928b1d0343SMike Christie 		if (session->age == 16)
31938b1d0343SMike Christie 			session->age = 0;
31946eabafbeSMike Christie 		break;
31957996a778SMike Christie 	case STOP_CONN_TERM:
31967996a778SMike Christie 		conn->stop_stage = 0;
31977996a778SMike Christie 		break;
31987996a778SMike Christie 	default:
31997996a778SMike Christie 		break;
32007996a778SMike Christie 	}
3201659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
32027996a778SMike Christie 
320375613521SMike Christie 	iscsi_unblock_session(session->cls_session);
3204ec29d0acSMike Christie 	wake_up(&session->ehwait);
32057996a778SMike Christie 	return 0;
32067996a778SMike Christie }
32077996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_start);
32087996a778SMike Christie 
32097996a778SMike Christie static void
32103bbaaad9SMike Christie fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
32117996a778SMike Christie {
32123bbaaad9SMike Christie 	struct iscsi_task *task;
3213b3cd5050SMike Christie 	int i, state;
32147996a778SMike Christie 
32153bbaaad9SMike Christie 	for (i = 0; i < conn->session->cmds_max; i++) {
32163bbaaad9SMike Christie 		task = conn->session->cmds[i];
32173bbaaad9SMike Christie 		if (task->sc)
32183bbaaad9SMike Christie 			continue;
32193bbaaad9SMike Christie 
32203bbaaad9SMike Christie 		if (task->state == ISCSI_TASK_FREE)
32213bbaaad9SMike Christie 			continue;
32223bbaaad9SMike Christie 
32233bbaaad9SMike Christie 		ISCSI_DBG_SESSION(conn->session,
32243bbaaad9SMike Christie 				  "failing mgmt itt 0x%x state %d\n",
32253bbaaad9SMike Christie 				  task->itt, task->state);
32265923d64bSMike Christie 
32275923d64bSMike Christie 		spin_lock_bh(&session->back_lock);
32285923d64bSMike Christie 		if (cleanup_queued_task(task)) {
32295923d64bSMike Christie 			spin_unlock_bh(&session->back_lock);
32305923d64bSMike Christie 			continue;
32315923d64bSMike Christie 		}
32325923d64bSMike Christie 
3233b3cd5050SMike Christie 		state = ISCSI_TASK_ABRT_SESS_RECOV;
3234b3cd5050SMike Christie 		if (task->state == ISCSI_TASK_PENDING)
3235b3cd5050SMike Christie 			state = ISCSI_TASK_COMPLETED;
3236b3cd5050SMike Christie 		iscsi_complete_task(task, state);
3237a656183eSLee Duncan 		spin_unlock_bh(&session->back_lock);
32387996a778SMike Christie 	}
32397996a778SMike Christie }
32407996a778SMike Christie 
32410dcf8febSMike Christie void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
32427996a778SMike Christie {
32430dcf8febSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
32440dcf8febSMike Christie 	struct iscsi_session *session = conn->session;
3245ed2abc7fSMike Christie 	int old_stop_stage;
3246ed2abc7fSMike Christie 
32476724add1SMike Christie 	mutex_lock(&session->eh_mutex);
3248659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
3249ed2abc7fSMike Christie 	if (conn->stop_stage == STOP_CONN_TERM) {
3250659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
32516724add1SMike Christie 		mutex_unlock(&session->eh_mutex);
32526724add1SMike Christie 		return;
32536724add1SMike Christie 	}
32546724add1SMike Christie 
32556724add1SMike Christie 	/*
3256ed2abc7fSMike Christie 	 * When this is called for the in_login state, we only want to clean
32579c19a7d0SMike Christie 	 * up the login task and connection. We do not need to block and set
325867a61114SMike Christie 	 * the recovery state again
3259ed2abc7fSMike Christie 	 */
326067a61114SMike Christie 	if (flag == STOP_CONN_TERM)
326167a61114SMike Christie 		session->state = ISCSI_STATE_TERMINATE;
326267a61114SMike Christie 	else if (conn->stop_stage != STOP_CONN_RECOVER)
326367a61114SMike Christie 		session->state = ISCSI_STATE_IN_RECOVERY;
32644ae0a6c1SMike Christie 
32654ae0a6c1SMike Christie 	old_stop_stage = conn->stop_stage;
32664ae0a6c1SMike Christie 	conn->stop_stage = flag;
3267659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
3268ed2abc7fSMike Christie 
326926013ad4SMike Christie 	del_timer_sync(&conn->transport_timer);
327026013ad4SMike Christie 	iscsi_suspend_tx(conn);
327126013ad4SMike Christie 
3272659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
327367a61114SMike Christie 	conn->c_stage = ISCSI_CONN_STOPPED;
3274659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
32756724add1SMike Christie 
32767996a778SMike Christie 	/*
32777996a778SMike Christie 	 * for connection level recovery we should not calculate
32787996a778SMike Christie 	 * header digest. conn->hdr_size used for optimization
32797996a778SMike Christie 	 * in hdr_extract() and will be re-negotiated at
32807996a778SMike Christie 	 * set_param() time.
32817996a778SMike Christie 	 */
32827996a778SMike Christie 	if (flag == STOP_CONN_RECOVER) {
32837996a778SMike Christie 		conn->hdrdgst_en = 0;
32847996a778SMike Christie 		conn->datadgst_en = 0;
3285656cffc9SMike Christie 		if (session->state == ISCSI_STATE_IN_RECOVERY &&
328667a61114SMike Christie 		    old_stop_stage != STOP_CONN_RECOVER) {
32871b2c7af8SMike Christie 			ISCSI_DBG_SESSION(session, "blocking session\n");
328875613521SMike Christie 			iscsi_block_session(session->cls_session);
32897996a778SMike Christie 		}
329067a61114SMike Christie 	}
3291656cffc9SMike Christie 
3292656cffc9SMike Christie 	/*
3293656cffc9SMike Christie 	 * flush queues.
3294656cffc9SMike Christie 	 */
3295659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
32963bbaaad9SMike Christie 	fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
32973bbaaad9SMike Christie 	fail_mgmt_tasks(session, conn);
3298ec29d0acSMike Christie 	memset(&session->tmhdr, 0, sizeof(session->tmhdr));
3299659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
33006724add1SMike Christie 	mutex_unlock(&session->eh_mutex);
33017996a778SMike Christie }
33027996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_stop);
33037996a778SMike Christie 
33047996a778SMike Christie int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
33057996a778SMike Christie 		    struct iscsi_cls_conn *cls_conn, int is_leading)
33067996a778SMike Christie {
330775613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
330898644047SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
33097996a778SMike Christie 
3310659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
33117996a778SMike Christie 	if (is_leading)
33127996a778SMike Christie 		session->leadconn = conn;
3313659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
33147996a778SMike Christie 
33157996a778SMike Christie 	/*
3316c8447e4cSMike Christie 	 * The target could have reduced it's window size between logins, so
3317c8447e4cSMike Christie 	 * we have to reset max/exp cmdsn so we can see the new values.
3318c8447e4cSMike Christie 	 */
3319c8447e4cSMike Christie 	spin_lock_bh(&session->back_lock);
3320c8447e4cSMike Christie 	session->max_cmdsn = session->exp_cmdsn = session->cmdsn + 1;
3321c8447e4cSMike Christie 	spin_unlock_bh(&session->back_lock);
3322c8447e4cSMike Christie 	/*
33237996a778SMike Christie 	 * Unblock xmitworker(), Login Phase will pass through.
33247996a778SMike Christie 	 */
33257996a778SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
33267996a778SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
33277996a778SMike Christie 	return 0;
33287996a778SMike Christie }
33297996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_bind);
33307996a778SMike Christie 
3331adaf6990SAdheer Chandravanshi int iscsi_switch_str_param(char **param, char *new_val_buf)
33325700b1afSMike Christie {
33335700b1afSMike Christie 	char *new_val;
33345700b1afSMike Christie 
33355700b1afSMike Christie 	if (*param) {
33365700b1afSMike Christie 		if (!strcmp(*param, new_val_buf))
33375700b1afSMike Christie 			return 0;
33385700b1afSMike Christie 	}
33395700b1afSMike Christie 
33405700b1afSMike Christie 	new_val = kstrdup(new_val_buf, GFP_NOIO);
33415700b1afSMike Christie 	if (!new_val)
33425700b1afSMike Christie 		return -ENOMEM;
33435700b1afSMike Christie 
33445700b1afSMike Christie 	kfree(*param);
33455700b1afSMike Christie 	*param = new_val;
33465700b1afSMike Christie 	return 0;
33475700b1afSMike Christie }
3348adaf6990SAdheer Chandravanshi EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
3349a54a52caSMike Christie 
3350a54a52caSMike Christie int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3351a54a52caSMike Christie 		    enum iscsi_param param, char *buf, int buflen)
3352a54a52caSMike Christie {
3353a54a52caSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
3354a54a52caSMike Christie 	struct iscsi_session *session = conn->session;
33556a06a4b8SOr Gerlitz 	int val;
3356a54a52caSMike Christie 
3357a54a52caSMike Christie 	switch(param) {
3358843c0a8aSMike Christie 	case ISCSI_PARAM_FAST_ABORT:
3359843c0a8aSMike Christie 		sscanf(buf, "%d", &session->fast_abort);
3360843c0a8aSMike Christie 		break;
3361f6d5180cSMike Christie 	case ISCSI_PARAM_ABORT_TMO:
3362f6d5180cSMike Christie 		sscanf(buf, "%d", &session->abort_timeout);
3363f6d5180cSMike Christie 		break;
3364f6d5180cSMike Christie 	case ISCSI_PARAM_LU_RESET_TMO:
3365f6d5180cSMike Christie 		sscanf(buf, "%d", &session->lu_reset_timeout);
3366f6d5180cSMike Christie 		break;
33673fe5ae8bSMike Christie 	case ISCSI_PARAM_TGT_RESET_TMO:
33683fe5ae8bSMike Christie 		sscanf(buf, "%d", &session->tgt_reset_timeout);
33693fe5ae8bSMike Christie 		break;
3370f6d5180cSMike Christie 	case ISCSI_PARAM_PING_TMO:
3371f6d5180cSMike Christie 		sscanf(buf, "%d", &conn->ping_timeout);
3372f6d5180cSMike Christie 		break;
3373f6d5180cSMike Christie 	case ISCSI_PARAM_RECV_TMO:
3374f6d5180cSMike Christie 		sscanf(buf, "%d", &conn->recv_timeout);
3375f6d5180cSMike Christie 		break;
3376a54a52caSMike Christie 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
3377a54a52caSMike Christie 		sscanf(buf, "%d", &conn->max_recv_dlength);
3378a54a52caSMike Christie 		break;
3379a54a52caSMike Christie 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3380a54a52caSMike Christie 		sscanf(buf, "%d", &conn->max_xmit_dlength);
3381a54a52caSMike Christie 		break;
3382a54a52caSMike Christie 	case ISCSI_PARAM_HDRDGST_EN:
3383a54a52caSMike Christie 		sscanf(buf, "%d", &conn->hdrdgst_en);
3384a54a52caSMike Christie 		break;
3385a54a52caSMike Christie 	case ISCSI_PARAM_DATADGST_EN:
3386a54a52caSMike Christie 		sscanf(buf, "%d", &conn->datadgst_en);
3387a54a52caSMike Christie 		break;
3388a54a52caSMike Christie 	case ISCSI_PARAM_INITIAL_R2T_EN:
3389a54a52caSMike Christie 		sscanf(buf, "%d", &session->initial_r2t_en);
3390a54a52caSMike Christie 		break;
3391a54a52caSMike Christie 	case ISCSI_PARAM_MAX_R2T:
33921304be5fSMike Christie 		sscanf(buf, "%hu", &session->max_r2t);
3393a54a52caSMike Christie 		break;
3394a54a52caSMike Christie 	case ISCSI_PARAM_IMM_DATA_EN:
3395a54a52caSMike Christie 		sscanf(buf, "%d", &session->imm_data_en);
3396a54a52caSMike Christie 		break;
3397a54a52caSMike Christie 	case ISCSI_PARAM_FIRST_BURST:
3398a54a52caSMike Christie 		sscanf(buf, "%d", &session->first_burst);
3399a54a52caSMike Christie 		break;
3400a54a52caSMike Christie 	case ISCSI_PARAM_MAX_BURST:
3401a54a52caSMike Christie 		sscanf(buf, "%d", &session->max_burst);
3402a54a52caSMike Christie 		break;
3403a54a52caSMike Christie 	case ISCSI_PARAM_PDU_INORDER_EN:
3404a54a52caSMike Christie 		sscanf(buf, "%d", &session->pdu_inorder_en);
3405a54a52caSMike Christie 		break;
3406a54a52caSMike Christie 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
3407a54a52caSMike Christie 		sscanf(buf, "%d", &session->dataseq_inorder_en);
3408a54a52caSMike Christie 		break;
3409a54a52caSMike Christie 	case ISCSI_PARAM_ERL:
3410a54a52caSMike Christie 		sscanf(buf, "%d", &session->erl);
3411a54a52caSMike Christie 		break;
3412a54a52caSMike Christie 	case ISCSI_PARAM_EXP_STATSN:
3413a54a52caSMike Christie 		sscanf(buf, "%u", &conn->exp_statsn);
3414a54a52caSMike Christie 		break;
3415b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME:
34165700b1afSMike Christie 		return iscsi_switch_str_param(&session->username, buf);
3417b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME_IN:
34185700b1afSMike Christie 		return iscsi_switch_str_param(&session->username_in, buf);
3419b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD:
34205700b1afSMike Christie 		return iscsi_switch_str_param(&session->password, buf);
3421b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD_IN:
34225700b1afSMike Christie 		return iscsi_switch_str_param(&session->password_in, buf);
3423a54a52caSMike Christie 	case ISCSI_PARAM_TARGET_NAME:
34245700b1afSMike Christie 		return iscsi_switch_str_param(&session->targetname, buf);
34253c5c4801SVikas Chaudhary 	case ISCSI_PARAM_TARGET_ALIAS:
34263c5c4801SVikas Chaudhary 		return iscsi_switch_str_param(&session->targetalias, buf);
3427a54a52caSMike Christie 	case ISCSI_PARAM_TPGT:
3428a54a52caSMike Christie 		sscanf(buf, "%d", &session->tpgt);
3429a54a52caSMike Christie 		break;
3430a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_PORT:
3431a54a52caSMike Christie 		sscanf(buf, "%d", &conn->persistent_port);
3432a54a52caSMike Christie 		break;
3433a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
34345700b1afSMike Christie 		return iscsi_switch_str_param(&conn->persistent_address, buf);
343588dfd340SMike Christie 	case ISCSI_PARAM_IFACE_NAME:
34365700b1afSMike Christie 		return iscsi_switch_str_param(&session->ifacename, buf);
343788dfd340SMike Christie 	case ISCSI_PARAM_INITIATOR_NAME:
34385700b1afSMike Christie 		return iscsi_switch_str_param(&session->initiatorname, buf);
34393b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_ROOT:
34403b9373e9SEddie Wai 		return iscsi_switch_str_param(&session->boot_root, buf);
34413b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_NIC:
34423b9373e9SEddie Wai 		return iscsi_switch_str_param(&session->boot_nic, buf);
34433b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_TARGET:
34443b9373e9SEddie Wai 		return iscsi_switch_str_param(&session->boot_target, buf);
3445f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_PORTAL_TYPE:
3446f8525eb4SAdheer Chandravanshi 		return iscsi_switch_str_param(&session->portal_type, buf);
3447f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3448f8525eb4SAdheer Chandravanshi 		return iscsi_switch_str_param(&session->discovery_parent_type,
3449f8525eb4SAdheer Chandravanshi 					      buf);
34506a06a4b8SOr Gerlitz 	case ISCSI_PARAM_DISCOVERY_SESS:
34516a06a4b8SOr Gerlitz 		sscanf(buf, "%d", &val);
34526a06a4b8SOr Gerlitz 		session->discovery_sess = !!val;
34536a06a4b8SOr Gerlitz 		break;
3454ae56ff40SAdheer Chandravanshi 	case ISCSI_PARAM_LOCAL_IPADDR:
3455ae56ff40SAdheer Chandravanshi 		return iscsi_switch_str_param(&conn->local_ipaddr, buf);
3456a54a52caSMike Christie 	default:
3457a54a52caSMike Christie 		return -ENOSYS;
3458a54a52caSMike Christie 	}
3459a54a52caSMike Christie 
3460a54a52caSMike Christie 	return 0;
3461a54a52caSMike Christie }
3462a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_set_param);
3463a54a52caSMike Christie 
3464a54a52caSMike Christie int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3465a54a52caSMike Christie 			    enum iscsi_param param, char *buf)
3466a54a52caSMike Christie {
346775613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
3468a54a52caSMike Christie 	int len;
3469a54a52caSMike Christie 
3470a54a52caSMike Christie 	switch(param) {
3471843c0a8aSMike Christie 	case ISCSI_PARAM_FAST_ABORT:
3472ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->fast_abort);
3473843c0a8aSMike Christie 		break;
3474f6d5180cSMike Christie 	case ISCSI_PARAM_ABORT_TMO:
3475ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->abort_timeout);
3476f6d5180cSMike Christie 		break;
3477f6d5180cSMike Christie 	case ISCSI_PARAM_LU_RESET_TMO:
3478ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->lu_reset_timeout);
3479f6d5180cSMike Christie 		break;
34803fe5ae8bSMike Christie 	case ISCSI_PARAM_TGT_RESET_TMO:
3481ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->tgt_reset_timeout);
34823fe5ae8bSMike Christie 		break;
3483a54a52caSMike Christie 	case ISCSI_PARAM_INITIAL_R2T_EN:
3484ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->initial_r2t_en);
3485a54a52caSMike Christie 		break;
3486a54a52caSMike Christie 	case ISCSI_PARAM_MAX_R2T:
3487ec98ea70SChris Leech 		len = sysfs_emit(buf, "%hu\n", session->max_r2t);
3488a54a52caSMike Christie 		break;
3489a54a52caSMike Christie 	case ISCSI_PARAM_IMM_DATA_EN:
3490ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->imm_data_en);
3491a54a52caSMike Christie 		break;
3492a54a52caSMike Christie 	case ISCSI_PARAM_FIRST_BURST:
3493ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->first_burst);
3494a54a52caSMike Christie 		break;
3495a54a52caSMike Christie 	case ISCSI_PARAM_MAX_BURST:
3496ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->max_burst);
3497a54a52caSMike Christie 		break;
3498a54a52caSMike Christie 	case ISCSI_PARAM_PDU_INORDER_EN:
3499ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->pdu_inorder_en);
3500a54a52caSMike Christie 		break;
3501a54a52caSMike Christie 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
3502ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->dataseq_inorder_en);
3503a54a52caSMike Christie 		break;
35045f09e1f3SAdheer Chandravanshi 	case ISCSI_PARAM_DEF_TASKMGMT_TMO:
3505ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->def_taskmgmt_tmo);
35065f09e1f3SAdheer Chandravanshi 		break;
3507a54a52caSMike Christie 	case ISCSI_PARAM_ERL:
3508ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->erl);
3509a54a52caSMike Christie 		break;
3510a54a52caSMike Christie 	case ISCSI_PARAM_TARGET_NAME:
3511ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->targetname);
3512a54a52caSMike Christie 		break;
35133c5c4801SVikas Chaudhary 	case ISCSI_PARAM_TARGET_ALIAS:
3514ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->targetalias);
35153c5c4801SVikas Chaudhary 		break;
3516a54a52caSMike Christie 	case ISCSI_PARAM_TPGT:
3517ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->tpgt);
3518a54a52caSMike Christie 		break;
3519b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME:
3520ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->username);
3521b2c64167SMike Christie 		break;
3522b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME_IN:
3523ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->username_in);
3524b2c64167SMike Christie 		break;
3525b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD:
3526ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->password);
3527b2c64167SMike Christie 		break;
3528b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD_IN:
3529ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->password_in);
3530b2c64167SMike Christie 		break;
353188dfd340SMike Christie 	case ISCSI_PARAM_IFACE_NAME:
3532ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->ifacename);
353388dfd340SMike Christie 		break;
353488dfd340SMike Christie 	case ISCSI_PARAM_INITIATOR_NAME:
3535ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->initiatorname);
353688dfd340SMike Christie 		break;
35373b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_ROOT:
3538ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->boot_root);
35393b9373e9SEddie Wai 		break;
35403b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_NIC:
3541ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->boot_nic);
35423b9373e9SEddie Wai 		break;
35433b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_TARGET:
3544ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->boot_target);
35459a2307b1SAdheer Chandravanshi 		break;
3546f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
3547ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->auto_snd_tgt_disable);
3548f8525eb4SAdheer Chandravanshi 		break;
3549f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_SESS:
3550ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->discovery_sess);
3551f8525eb4SAdheer Chandravanshi 		break;
3552f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_PORTAL_TYPE:
3553ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", session->portal_type);
3554f8525eb4SAdheer Chandravanshi 		break;
3555f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_CHAP_AUTH_EN:
3556ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->chap_auth_en);
3557f8525eb4SAdheer Chandravanshi 		break;
3558f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
3559ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->discovery_logout_en);
3560f8525eb4SAdheer Chandravanshi 		break;
3561f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_BIDI_CHAP_EN:
3562ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->bidi_chap_en);
3563f8525eb4SAdheer Chandravanshi 		break;
3564f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
3565ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->discovery_auth_optional);
3566f8525eb4SAdheer Chandravanshi 		break;
3567f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DEF_TIME2WAIT:
3568ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->time2wait);
3569f8525eb4SAdheer Chandravanshi 		break;
3570f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DEF_TIME2RETAIN:
3571ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", session->time2retain);
3572f8525eb4SAdheer Chandravanshi 		break;
3573f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TSID:
3574ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->tsid);
3575f8525eb4SAdheer Chandravanshi 		break;
3576f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_ISID:
3577ec98ea70SChris Leech 		len = sysfs_emit(buf, "%02x%02x%02x%02x%02x%02x\n",
3578f8525eb4SAdheer Chandravanshi 			      session->isid[0], session->isid[1],
3579f8525eb4SAdheer Chandravanshi 			      session->isid[2], session->isid[3],
3580f8525eb4SAdheer Chandravanshi 			      session->isid[4], session->isid[5]);
3581f8525eb4SAdheer Chandravanshi 		break;
3582f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
3583ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", session->discovery_parent_idx);
3584f8525eb4SAdheer Chandravanshi 		break;
3585f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3586f8525eb4SAdheer Chandravanshi 		if (session->discovery_parent_type)
3587ec98ea70SChris Leech 			len = sysfs_emit(buf, "%s\n",
3588f8525eb4SAdheer Chandravanshi 				      session->discovery_parent_type);
3589f8525eb4SAdheer Chandravanshi 		else
3590ec98ea70SChris Leech 			len = sysfs_emit(buf, "\n");
35913b9373e9SEddie Wai 		break;
3592a54a52caSMike Christie 	default:
3593a54a52caSMike Christie 		return -ENOSYS;
3594a54a52caSMike Christie 	}
3595a54a52caSMike Christie 
3596a54a52caSMike Christie 	return len;
3597a54a52caSMike Christie }
3598a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3599a54a52caSMike Christie 
360000f3708eSMike Christie int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
360100f3708eSMike Christie 			      enum iscsi_param param, char *buf)
360200f3708eSMike Christie {
360300f3708eSMike Christie 	struct sockaddr_in6 *sin6 = NULL;
360400f3708eSMike Christie 	struct sockaddr_in *sin = NULL;
360500f3708eSMike Christie 	int len;
360600f3708eSMike Christie 
360700f3708eSMike Christie 	switch (addr->ss_family) {
360800f3708eSMike Christie 	case AF_INET:
360900f3708eSMike Christie 		sin = (struct sockaddr_in *)addr;
361000f3708eSMike Christie 		break;
361100f3708eSMike Christie 	case AF_INET6:
361200f3708eSMike Christie 		sin6 = (struct sockaddr_in6 *)addr;
361300f3708eSMike Christie 		break;
361400f3708eSMike Christie 	default:
361500f3708eSMike Christie 		return -EINVAL;
361600f3708eSMike Christie 	}
361700f3708eSMike Christie 
361800f3708eSMike Christie 	switch (param) {
361900f3708eSMike Christie 	case ISCSI_PARAM_CONN_ADDRESS:
362000f3708eSMike Christie 	case ISCSI_HOST_PARAM_IPADDRESS:
362100f3708eSMike Christie 		if (sin)
3622ec98ea70SChris Leech 			len = sysfs_emit(buf, "%pI4\n", &sin->sin_addr.s_addr);
362300f3708eSMike Christie 		else
3624ec98ea70SChris Leech 			len = sysfs_emit(buf, "%pI6\n", &sin6->sin6_addr);
362500f3708eSMike Christie 		break;
362600f3708eSMike Christie 	case ISCSI_PARAM_CONN_PORT:
36274bfb8ebfSMike Christie 	case ISCSI_PARAM_LOCAL_PORT:
362800f3708eSMike Christie 		if (sin)
3629ec98ea70SChris Leech 			len = sysfs_emit(buf, "%hu\n", be16_to_cpu(sin->sin_port));
363000f3708eSMike Christie 		else
3631ec98ea70SChris Leech 			len = sysfs_emit(buf, "%hu\n",
363200f3708eSMike Christie 				      be16_to_cpu(sin6->sin6_port));
363300f3708eSMike Christie 		break;
363400f3708eSMike Christie 	default:
363500f3708eSMike Christie 		return -EINVAL;
363600f3708eSMike Christie 	}
363700f3708eSMike Christie 
363800f3708eSMike Christie 	return len;
363900f3708eSMike Christie }
364000f3708eSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
364100f3708eSMike Christie 
3642a54a52caSMike Christie int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3643a54a52caSMike Christie 			 enum iscsi_param param, char *buf)
3644a54a52caSMike Christie {
3645a54a52caSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
3646a54a52caSMike Christie 	int len;
3647a54a52caSMike Christie 
3648a54a52caSMike Christie 	switch(param) {
3649f6d5180cSMike Christie 	case ISCSI_PARAM_PING_TMO:
3650ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->ping_timeout);
3651f6d5180cSMike Christie 		break;
3652f6d5180cSMike Christie 	case ISCSI_PARAM_RECV_TMO:
3653ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->recv_timeout);
3654f6d5180cSMike Christie 		break;
3655a54a52caSMike Christie 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
3656ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->max_recv_dlength);
3657a54a52caSMike Christie 		break;
3658a54a52caSMike Christie 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3659ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->max_xmit_dlength);
3660a54a52caSMike Christie 		break;
3661a54a52caSMike Christie 	case ISCSI_PARAM_HDRDGST_EN:
3662ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", conn->hdrdgst_en);
3663a54a52caSMike Christie 		break;
3664a54a52caSMike Christie 	case ISCSI_PARAM_DATADGST_EN:
3665ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", conn->datadgst_en);
3666a54a52caSMike Christie 		break;
3667a54a52caSMike Christie 	case ISCSI_PARAM_IFMARKER_EN:
3668ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", conn->ifmarker_en);
3669a54a52caSMike Christie 		break;
3670a54a52caSMike Christie 	case ISCSI_PARAM_OFMARKER_EN:
3671ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", conn->ofmarker_en);
3672a54a52caSMike Christie 		break;
3673a54a52caSMike Christie 	case ISCSI_PARAM_EXP_STATSN:
3674ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->exp_statsn);
3675a54a52caSMike Christie 		break;
3676a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_PORT:
3677ec98ea70SChris Leech 		len = sysfs_emit(buf, "%d\n", conn->persistent_port);
3678a54a52caSMike Christie 		break;
3679a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
3680ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", conn->persistent_address);
3681a54a52caSMike Christie 		break;
3682f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_STATSN:
3683ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->statsn);
3684f8525eb4SAdheer Chandravanshi 		break;
3685f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_MAX_SEGMENT_SIZE:
3686ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->max_segment_size);
3687f8525eb4SAdheer Chandravanshi 		break;
3688f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_KEEPALIVE_TMO:
3689ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->keepalive_tmo);
3690f8525eb4SAdheer Chandravanshi 		break;
3691f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_LOCAL_PORT:
3692ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->local_port);
3693f8525eb4SAdheer Chandravanshi 		break;
3694f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
3695ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_stat);
3696f8525eb4SAdheer Chandravanshi 		break;
3697f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_NAGLE_DISABLE:
3698ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->tcp_nagle_disable);
3699f8525eb4SAdheer Chandravanshi 		break;
3700f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_WSF_DISABLE:
3701ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->tcp_wsf_disable);
3702f8525eb4SAdheer Chandravanshi 		break;
3703f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_TIMER_SCALE:
3704ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->tcp_timer_scale);
3705f8525eb4SAdheer Chandravanshi 		break;
3706f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_TIMESTAMP_EN:
3707ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_en);
3708f8525eb4SAdheer Chandravanshi 		break;
3709f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
3710ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->fragment_disable);
3711f8525eb4SAdheer Chandravanshi 		break;
3712f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IPV4_TOS:
3713ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->ipv4_tos);
3714f8525eb4SAdheer Chandravanshi 		break;
3715f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IPV6_TC:
3716ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->ipv6_traffic_class);
3717f8525eb4SAdheer Chandravanshi 		break;
37185f09e1f3SAdheer Chandravanshi 	case ISCSI_PARAM_IPV6_FLOW_LABEL:
3719ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->ipv6_flow_label);
37205f09e1f3SAdheer Chandravanshi 		break;
3721f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
3722ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->is_fw_assigned_ipv6);
3723f8525eb4SAdheer Chandravanshi 		break;
3724f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_XMIT_WSF:
3725ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->tcp_xmit_wsf);
3726f8525eb4SAdheer Chandravanshi 		break;
3727f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_RECV_WSF:
3728ec98ea70SChris Leech 		len = sysfs_emit(buf, "%u\n", conn->tcp_recv_wsf);
3729f8525eb4SAdheer Chandravanshi 		break;
3730ae56ff40SAdheer Chandravanshi 	case ISCSI_PARAM_LOCAL_IPADDR:
3731ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", conn->local_ipaddr);
3732ae56ff40SAdheer Chandravanshi 		break;
3733a54a52caSMike Christie 	default:
3734a54a52caSMike Christie 		return -ENOSYS;
3735a54a52caSMike Christie 	}
3736a54a52caSMike Christie 
3737a54a52caSMike Christie 	return len;
3738a54a52caSMike Christie }
3739a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3740a54a52caSMike Christie 
37410801c242SMike Christie int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
37420801c242SMike Christie 			 char *buf)
37430801c242SMike Christie {
374475613521SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
37450801c242SMike Christie 	int len;
37460801c242SMike Christie 
37470801c242SMike Christie 	switch (param) {
3748d8196ed2SMike Christie 	case ISCSI_HOST_PARAM_NETDEV_NAME:
3749ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", ihost->netdev);
3750d8196ed2SMike Christie 		break;
37510801c242SMike Christie 	case ISCSI_HOST_PARAM_HWADDRESS:
3752ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", ihost->hwaddress);
37530801c242SMike Christie 		break;
37548ad5781aSMike Christie 	case ISCSI_HOST_PARAM_INITIATOR_NAME:
3755ec98ea70SChris Leech 		len = sysfs_emit(buf, "%s\n", ihost->initiatorname);
37568ad5781aSMike Christie 		break;
37570801c242SMike Christie 	default:
37580801c242SMike Christie 		return -ENOSYS;
37590801c242SMike Christie 	}
37600801c242SMike Christie 
37610801c242SMike Christie 	return len;
37620801c242SMike Christie }
37630801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_get_param);
37640801c242SMike Christie 
37650801c242SMike Christie int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
37660801c242SMike Christie 			 char *buf, int buflen)
37670801c242SMike Christie {
376875613521SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
37690801c242SMike Christie 
37700801c242SMike Christie 	switch (param) {
3771d8196ed2SMike Christie 	case ISCSI_HOST_PARAM_NETDEV_NAME:
37725700b1afSMike Christie 		return iscsi_switch_str_param(&ihost->netdev, buf);
37730801c242SMike Christie 	case ISCSI_HOST_PARAM_HWADDRESS:
37745700b1afSMike Christie 		return iscsi_switch_str_param(&ihost->hwaddress, buf);
37758ad5781aSMike Christie 	case ISCSI_HOST_PARAM_INITIATOR_NAME:
37765700b1afSMike Christie 		return iscsi_switch_str_param(&ihost->initiatorname, buf);
37770801c242SMike Christie 	default:
37780801c242SMike Christie 		return -ENOSYS;
37790801c242SMike Christie 	}
37800801c242SMike Christie 
37810801c242SMike Christie 	return 0;
37820801c242SMike Christie }
37830801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_set_param);
37840801c242SMike Christie 
37857996a778SMike Christie MODULE_AUTHOR("Mike Christie");
37867996a778SMike Christie MODULE_DESCRIPTION("iSCSI library functions");
37877996a778SMike Christie MODULE_LICENSE("GPL");
3788