xref: /linux/drivers/scsi/libiscsi.c (revision 6f8830f5bbab16e54f261de187f3df4644a5b977)
17996a778SMike Christie /*
27996a778SMike Christie  * iSCSI lib functions
37996a778SMike Christie  *
47996a778SMike Christie  * Copyright (C) 2006 Red Hat, Inc.  All rights reserved.
57996a778SMike Christie  * Copyright (C) 2004 - 2006 Mike Christie
67996a778SMike Christie  * Copyright (C) 2004 - 2005 Dmitry Yusupov
77996a778SMike Christie  * Copyright (C) 2004 - 2005 Alex Aizman
87996a778SMike Christie  * maintained by open-iscsi@googlegroups.com
97996a778SMike Christie  *
107996a778SMike Christie  * This program is free software; you can redistribute it and/or modify
117996a778SMike Christie  * it under the terms of the GNU General Public License as published by
127996a778SMike Christie  * the Free Software Foundation; either version 2 of the License, or
137996a778SMike Christie  * (at your option) any later version.
147996a778SMike Christie  *
157996a778SMike Christie  * This program is distributed in the hope that it will be useful,
167996a778SMike Christie  * but WITHOUT ANY WARRANTY; without even the implied warranty of
177996a778SMike Christie  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
187996a778SMike Christie  * GNU General Public License for more details.
197996a778SMike Christie  *
207996a778SMike Christie  * You should have received a copy of the GNU General Public License
217996a778SMike Christie  * along with this program; if not, write to the Free Software
227996a778SMike Christie  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
237996a778SMike Christie  */
247996a778SMike Christie #include <linux/types.h>
257996a778SMike Christie #include <linux/kfifo.h>
267996a778SMike Christie #include <linux/delay.h>
2711836572Svignesh babu #include <linux/log2.h>
285a0e3ad6STejun Heo #include <linux/slab.h>
29acf3368fSPaul Gortmaker #include <linux/module.h>
308eb00539SMike Christie #include <asm/unaligned.h>
317996a778SMike Christie #include <net/tcp.h>
327996a778SMike Christie #include <scsi/scsi_cmnd.h>
337996a778SMike Christie #include <scsi/scsi_device.h>
347996a778SMike Christie #include <scsi/scsi_eh.h>
357996a778SMike Christie #include <scsi/scsi_tcq.h>
367996a778SMike Christie #include <scsi/scsi_host.h>
377996a778SMike Christie #include <scsi/scsi.h>
387996a778SMike Christie #include <scsi/iscsi_proto.h>
397996a778SMike Christie #include <scsi/scsi_transport.h>
407996a778SMike Christie #include <scsi/scsi_transport_iscsi.h>
417996a778SMike Christie #include <scsi/libiscsi.h>
427996a778SMike Christie 
43bd2199d4SErez Zilber static int iscsi_dbg_lib_conn;
44bd2199d4SErez Zilber module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
45bd2199d4SErez Zilber 		   S_IRUGO | S_IWUSR);
46bd2199d4SErez Zilber MODULE_PARM_DESC(debug_libiscsi_conn,
47bd2199d4SErez Zilber 		 "Turn on debugging for connections in libiscsi module. "
48bd2199d4SErez Zilber 		 "Set to 1 to turn on, and zero to turn off. Default is off.");
49bd2199d4SErez Zilber 
50bd2199d4SErez Zilber static int iscsi_dbg_lib_session;
51bd2199d4SErez Zilber module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
52bd2199d4SErez Zilber 		   S_IRUGO | S_IWUSR);
53bd2199d4SErez Zilber MODULE_PARM_DESC(debug_libiscsi_session,
54bd2199d4SErez Zilber 		 "Turn on debugging for sessions in libiscsi module. "
55bd2199d4SErez Zilber 		 "Set to 1 to turn on, and zero to turn off. Default is off.");
56bd2199d4SErez Zilber 
57bd2199d4SErez Zilber static int iscsi_dbg_lib_eh;
58bd2199d4SErez Zilber module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
59bd2199d4SErez Zilber 		   S_IRUGO | S_IWUSR);
60bd2199d4SErez Zilber MODULE_PARM_DESC(debug_libiscsi_eh,
61bd2199d4SErez Zilber 		 "Turn on debugging for error handling in libiscsi module. "
62bd2199d4SErez Zilber 		 "Set to 1 to turn on, and zero to turn off. Default is off.");
631b2c7af8SMike Christie 
641b2c7af8SMike Christie #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...)			\
651b2c7af8SMike Christie 	do {							\
66bd2199d4SErez Zilber 		if (iscsi_dbg_lib_conn)				\
671b2c7af8SMike Christie 			iscsi_conn_printk(KERN_INFO, _conn,	\
681b2c7af8SMike Christie 					     "%s " dbg_fmt,	\
691b2c7af8SMike Christie 					     __func__, ##arg);	\
701b2c7af8SMike Christie 	} while (0);
711b2c7af8SMike Christie 
721b2c7af8SMike Christie #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...)			\
731b2c7af8SMike Christie 	do {								\
74bd2199d4SErez Zilber 		if (iscsi_dbg_lib_session)				\
75bd2199d4SErez Zilber 			iscsi_session_printk(KERN_INFO, _session,	\
76bd2199d4SErez Zilber 					     "%s " dbg_fmt,		\
77bd2199d4SErez Zilber 					     __func__, ##arg);		\
78bd2199d4SErez Zilber 	} while (0);
79bd2199d4SErez Zilber 
80bd2199d4SErez Zilber #define ISCSI_DBG_EH(_session, dbg_fmt, arg...)				\
81bd2199d4SErez Zilber 	do {								\
82bd2199d4SErez Zilber 		if (iscsi_dbg_lib_eh)					\
831b2c7af8SMike Christie 			iscsi_session_printk(KERN_INFO, _session,	\
841b2c7af8SMike Christie 					     "%s " dbg_fmt,		\
851b2c7af8SMike Christie 					     __func__, ##arg);		\
861b2c7af8SMike Christie 	} while (0);
871b2c7af8SMike Christie 
8832ae763eSMike Christie inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
8932ae763eSMike Christie {
9032ae763eSMike Christie 	struct Scsi_Host *shost = conn->session->host;
9132ae763eSMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
9232ae763eSMike Christie 
931336aed1SMike Christie 	if (ihost->workq)
9432ae763eSMike Christie 		queue_work(ihost->workq, &conn->xmitwork);
9532ae763eSMike Christie }
9632ae763eSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
9732ae763eSMike Christie 
984c0ba5d2SMike Christie static void __iscsi_update_cmdsn(struct iscsi_session *session,
994c0ba5d2SMike Christie 				 uint32_t exp_cmdsn, uint32_t max_cmdsn)
1007996a778SMike Christie {
10177a23c21SMike Christie 	/*
10277a23c21SMike Christie 	 * standard specifies this check for when to update expected and
10377a23c21SMike Christie 	 * max sequence numbers
10477a23c21SMike Christie 	 */
10577a23c21SMike Christie 	if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
10677a23c21SMike Christie 		return;
10777a23c21SMike Christie 
10877a23c21SMike Christie 	if (exp_cmdsn != session->exp_cmdsn &&
10977a23c21SMike Christie 	    !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
1107996a778SMike Christie 		session->exp_cmdsn = exp_cmdsn;
1117996a778SMike Christie 
11277a23c21SMike Christie 	if (max_cmdsn != session->max_cmdsn &&
11346a84c65SMike Christie 	    !iscsi_sna_lt(max_cmdsn, session->max_cmdsn))
11477a23c21SMike Christie 		session->max_cmdsn = max_cmdsn;
11577a23c21SMike Christie }
1164c0ba5d2SMike Christie 
1174c0ba5d2SMike Christie void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
1184c0ba5d2SMike Christie {
1194c0ba5d2SMike Christie 	__iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
1204c0ba5d2SMike Christie 			     be32_to_cpu(hdr->max_cmdsn));
1214c0ba5d2SMike Christie }
12277a23c21SMike Christie EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
1237996a778SMike Christie 
124577577daSMike Christie /**
125577577daSMike Christie  * iscsi_prep_data_out_pdu - initialize Data-Out
126577577daSMike Christie  * @task: scsi command task
127577577daSMike Christie  * @r2t: R2T info
128577577daSMike Christie  * @hdr: iscsi data in pdu
129577577daSMike Christie  *
130577577daSMike Christie  * Notes:
131577577daSMike Christie  *	Initialize Data-Out within this R2T sequence and finds
132577577daSMike Christie  *	proper data_offset within this SCSI command.
133577577daSMike Christie  *
134577577daSMike Christie  *	This function is called with connection lock taken.
135577577daSMike Christie  **/
136577577daSMike Christie void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
137ffd0436eSMike Christie 			   struct iscsi_data *hdr)
1387996a778SMike Christie {
1399c19a7d0SMike Christie 	struct iscsi_conn *conn = task->conn;
140577577daSMike Christie 	unsigned int left = r2t->data_length - r2t->sent;
141577577daSMike Christie 
142577577daSMike Christie 	task->hdr_len = sizeof(struct iscsi_data);
1437996a778SMike Christie 
1447996a778SMike Christie 	memset(hdr, 0, sizeof(struct iscsi_data));
145577577daSMike Christie 	hdr->ttt = r2t->ttt;
146577577daSMike Christie 	hdr->datasn = cpu_to_be32(r2t->datasn);
147577577daSMike Christie 	r2t->datasn++;
1487996a778SMike Christie 	hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
14955bdabdfSAndy Grover 	hdr->lun = task->lun;
150577577daSMike Christie 	hdr->itt = task->hdr_itt;
151577577daSMike Christie 	hdr->exp_statsn = r2t->exp_statsn;
152577577daSMike Christie 	hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
153577577daSMike Christie 	if (left > conn->max_xmit_dlength) {
1547996a778SMike Christie 		hton24(hdr->dlength, conn->max_xmit_dlength);
155577577daSMike Christie 		r2t->data_count = conn->max_xmit_dlength;
1567996a778SMike Christie 		hdr->flags = 0;
1577996a778SMike Christie 	} else {
158577577daSMike Christie 		hton24(hdr->dlength, left);
159577577daSMike Christie 		r2t->data_count = left;
1607996a778SMike Christie 		hdr->flags = ISCSI_FLAG_CMD_FINAL;
1617996a778SMike Christie 	}
162577577daSMike Christie 	conn->dataout_pdus_cnt++;
1637996a778SMike Christie }
164577577daSMike Christie EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
1657996a778SMike Christie 
1669c19a7d0SMike Christie static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
167004d6530SBoaz Harrosh {
1689c19a7d0SMike Christie 	unsigned exp_len = task->hdr_len + len;
169004d6530SBoaz Harrosh 
1709c19a7d0SMike Christie 	if (exp_len > task->hdr_max) {
171004d6530SBoaz Harrosh 		WARN_ON(1);
172004d6530SBoaz Harrosh 		return -EINVAL;
173004d6530SBoaz Harrosh 	}
174004d6530SBoaz Harrosh 
175004d6530SBoaz Harrosh 	WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
1769c19a7d0SMike Christie 	task->hdr_len = exp_len;
177004d6530SBoaz Harrosh 	return 0;
178004d6530SBoaz Harrosh }
179004d6530SBoaz Harrosh 
18038d1c069SBoaz Harrosh /*
18138d1c069SBoaz Harrosh  * make an extended cdb AHS
18238d1c069SBoaz Harrosh  */
1839c19a7d0SMike Christie static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
18438d1c069SBoaz Harrosh {
1859c19a7d0SMike Christie 	struct scsi_cmnd *cmd = task->sc;
18638d1c069SBoaz Harrosh 	unsigned rlen, pad_len;
18738d1c069SBoaz Harrosh 	unsigned short ahslength;
18838d1c069SBoaz Harrosh 	struct iscsi_ecdb_ahdr *ecdb_ahdr;
18938d1c069SBoaz Harrosh 	int rc;
19038d1c069SBoaz Harrosh 
1919c19a7d0SMike Christie 	ecdb_ahdr = iscsi_next_hdr(task);
19238d1c069SBoaz Harrosh 	rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
19338d1c069SBoaz Harrosh 
19438d1c069SBoaz Harrosh 	BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
19538d1c069SBoaz Harrosh 	ahslength = rlen + sizeof(ecdb_ahdr->reserved);
19638d1c069SBoaz Harrosh 
19738d1c069SBoaz Harrosh 	pad_len = iscsi_padding(rlen);
19838d1c069SBoaz Harrosh 
1999c19a7d0SMike Christie 	rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
20038d1c069SBoaz Harrosh 	                   sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
20138d1c069SBoaz Harrosh 	if (rc)
20238d1c069SBoaz Harrosh 		return rc;
20338d1c069SBoaz Harrosh 
20438d1c069SBoaz Harrosh 	if (pad_len)
20538d1c069SBoaz Harrosh 		memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
20638d1c069SBoaz Harrosh 
20738d1c069SBoaz Harrosh 	ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
20838d1c069SBoaz Harrosh 	ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
20938d1c069SBoaz Harrosh 	ecdb_ahdr->reserved = 0;
21038d1c069SBoaz Harrosh 	memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
21138d1c069SBoaz Harrosh 
2121b2c7af8SMike Christie 	ISCSI_DBG_SESSION(task->conn->session,
2131b2c7af8SMike Christie 			  "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
2141b2c7af8SMike Christie 		          "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
2151b2c7af8SMike Christie 		          "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
2161b2c7af8SMike Christie 		          task->hdr_len);
21738d1c069SBoaz Harrosh 	return 0;
21838d1c069SBoaz Harrosh }
21938d1c069SBoaz Harrosh 
2209c19a7d0SMike Christie static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
221c07d4444SBoaz Harrosh {
2229c19a7d0SMike Christie 	struct scsi_cmnd *sc = task->sc;
223c07d4444SBoaz Harrosh 	struct iscsi_rlength_ahdr *rlen_ahdr;
224c07d4444SBoaz Harrosh 	int rc;
225c07d4444SBoaz Harrosh 
2269c19a7d0SMike Christie 	rlen_ahdr = iscsi_next_hdr(task);
2279c19a7d0SMike Christie 	rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
228c07d4444SBoaz Harrosh 	if (rc)
229c07d4444SBoaz Harrosh 		return rc;
230c07d4444SBoaz Harrosh 
231c07d4444SBoaz Harrosh 	rlen_ahdr->ahslength =
232c07d4444SBoaz Harrosh 		cpu_to_be16(sizeof(rlen_ahdr->read_length) +
233c07d4444SBoaz Harrosh 						  sizeof(rlen_ahdr->reserved));
234c07d4444SBoaz Harrosh 	rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
235c07d4444SBoaz Harrosh 	rlen_ahdr->reserved = 0;
236c07d4444SBoaz Harrosh 	rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
237c07d4444SBoaz Harrosh 
2381b2c7af8SMike Christie 	ISCSI_DBG_SESSION(task->conn->session,
2391b2c7af8SMike Christie 			  "bidi-in rlen_ahdr->read_length(%d) "
240c07d4444SBoaz Harrosh 		          "rlen_ahdr->ahslength(%d)\n",
241c07d4444SBoaz Harrosh 		          be32_to_cpu(rlen_ahdr->read_length),
242c07d4444SBoaz Harrosh 		          be16_to_cpu(rlen_ahdr->ahslength));
243c07d4444SBoaz Harrosh 	return 0;
244c07d4444SBoaz Harrosh }
245c07d4444SBoaz Harrosh 
2467996a778SMike Christie /**
2475d12c05eSMike Christie  * iscsi_check_tmf_restrictions - check if a task is affected by TMF
2485d12c05eSMike Christie  * @task: iscsi task
2495d12c05eSMike Christie  * @opcode: opcode to check for
2505d12c05eSMike Christie  *
2515d12c05eSMike Christie  * During TMF a task has to be checked if it's affected.
2525d12c05eSMike Christie  * All unrelated I/O can be passed through, but I/O to the
2535d12c05eSMike Christie  * affected LUN should be restricted.
2545d12c05eSMike Christie  * If 'fast_abort' is set we won't be sending any I/O to the
2555d12c05eSMike Christie  * affected LUN.
2565d12c05eSMike Christie  * Otherwise the target is waiting for all TTTs to be completed,
2575d12c05eSMike Christie  * so we have to send all outstanding Data-Out PDUs to the target.
2585d12c05eSMike Christie  */
2595d12c05eSMike Christie static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
2605d12c05eSMike Christie {
2615d12c05eSMike Christie 	struct iscsi_conn *conn = task->conn;
2625d12c05eSMike Christie 	struct iscsi_tm *tmf = &conn->tmhdr;
2639cb78c16SHannes Reinecke 	u64 hdr_lun;
2645d12c05eSMike Christie 
2655d12c05eSMike Christie 	if (conn->tmf_state == TMF_INITIAL)
2665d12c05eSMike Christie 		return 0;
2675d12c05eSMike Christie 
2685d12c05eSMike Christie 	if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
2695d12c05eSMike Christie 		return 0;
2705d12c05eSMike Christie 
2715d12c05eSMike Christie 	switch (ISCSI_TM_FUNC_VALUE(tmf)) {
2725d12c05eSMike Christie 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2735d12c05eSMike Christie 		/*
2745d12c05eSMike Christie 		 * Allow PDUs for unrelated LUNs
2755d12c05eSMike Christie 		 */
27655bdabdfSAndy Grover 		hdr_lun = scsilun_to_int(&tmf->lun);
2775d12c05eSMike Christie 		if (hdr_lun != task->sc->device->lun)
2785d12c05eSMike Christie 			return 0;
2793fe5ae8bSMike Christie 		/* fall through */
2803fe5ae8bSMike Christie 	case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2815d12c05eSMike Christie 		/*
2825d12c05eSMike Christie 		 * Fail all SCSI cmd PDUs
2835d12c05eSMike Christie 		 */
2845d12c05eSMike Christie 		if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
2855d12c05eSMike Christie 			iscsi_conn_printk(KERN_INFO, conn,
2865d12c05eSMike Christie 					  "task [op %x/%x itt "
2873fe5ae8bSMike Christie 					  "0x%x/0x%x] "
2885d12c05eSMike Christie 					  "rejected.\n",
2895d12c05eSMike Christie 					  task->hdr->opcode, opcode,
2903fe5ae8bSMike Christie 					  task->itt, task->hdr_itt);
2915d12c05eSMike Christie 			return -EACCES;
2925d12c05eSMike Christie 		}
2935d12c05eSMike Christie 		/*
2945d12c05eSMike Christie 		 * And also all data-out PDUs in response to R2T
2955d12c05eSMike Christie 		 * if fast_abort is set.
2965d12c05eSMike Christie 		 */
2975d12c05eSMike Christie 		if (conn->session->fast_abort) {
2985d12c05eSMike Christie 			iscsi_conn_printk(KERN_INFO, conn,
2995d12c05eSMike Christie 					  "task [op %x/%x itt "
3003fe5ae8bSMike Christie 					  "0x%x/0x%x] fast abort.\n",
3015d12c05eSMike Christie 					  task->hdr->opcode, opcode,
3023fe5ae8bSMike Christie 					  task->itt, task->hdr_itt);
3035d12c05eSMike Christie 			return -EACCES;
3045d12c05eSMike Christie 		}
3055d12c05eSMike Christie 		break;
3065d12c05eSMike Christie 	case ISCSI_TM_FUNC_ABORT_TASK:
3075d12c05eSMike Christie 		/*
3085d12c05eSMike Christie 		 * the caller has already checked if the task
3095d12c05eSMike Christie 		 * they want to abort was in the pending queue so if
3105d12c05eSMike Christie 		 * we are here the cmd pdu has gone out already, and
3115d12c05eSMike Christie 		 * we will only hit this for data-outs
3125d12c05eSMike Christie 		 */
3135d12c05eSMike Christie 		if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
3145d12c05eSMike Christie 		    task->hdr_itt == tmf->rtt) {
3155d12c05eSMike Christie 			ISCSI_DBG_SESSION(conn->session,
3165d12c05eSMike Christie 					  "Preventing task %x/%x from sending "
3175d12c05eSMike Christie 					  "data-out due to abort task in "
3185d12c05eSMike Christie 					  "progress\n", task->itt,
3195d12c05eSMike Christie 					  task->hdr_itt);
3205d12c05eSMike Christie 			return -EACCES;
3215d12c05eSMike Christie 		}
3225d12c05eSMike Christie 		break;
3235d12c05eSMike Christie 	}
3245d12c05eSMike Christie 
3255d12c05eSMike Christie 	return 0;
3265d12c05eSMike Christie }
3275d12c05eSMike Christie 
3285d12c05eSMike Christie /**
3297996a778SMike Christie  * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
3309c19a7d0SMike Christie  * @task: iscsi task
3317996a778SMike Christie  *
3327996a778SMike Christie  * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
3337996a778SMike Christie  * fields like dlength or final based on how much data it sends
3347996a778SMike Christie  */
3359c19a7d0SMike Christie static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
3367996a778SMike Christie {
3379c19a7d0SMike Christie 	struct iscsi_conn *conn = task->conn;
3387996a778SMike Christie 	struct iscsi_session *session = conn->session;
3399c19a7d0SMike Christie 	struct scsi_cmnd *sc = task->sc;
34012352183SNicholas Bellinger 	struct iscsi_scsi_req *hdr;
341d77e6535SSagi Grimberg 	unsigned hdrlength, cmd_len, transfer_length;
342262ef636SMike Christie 	itt_t itt;
343004d6530SBoaz Harrosh 	int rc;
3447996a778SMike Christie 
3455d12c05eSMike Christie 	rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
3465d12c05eSMike Christie 	if (rc)
3475d12c05eSMike Christie 		return rc;
3485d12c05eSMike Christie 
349184b57c6SMike Christie 	if (conn->session->tt->alloc_pdu) {
3502ff79d52SMike Christie 		rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
351577577daSMike Christie 		if (rc)
352577577daSMike Christie 			return rc;
353184b57c6SMike Christie 	}
35412352183SNicholas Bellinger 	hdr = (struct iscsi_scsi_req *)task->hdr;
355262ef636SMike Christie 	itt = hdr->itt;
356577577daSMike Christie 	memset(hdr, 0, sizeof(*hdr));
357577577daSMike Christie 
3582ff79d52SMike Christie 	if (session->tt->parse_pdu_itt)
3592ff79d52SMike Christie 		hdr->itt = task->hdr_itt = itt;
3602ff79d52SMike Christie 	else
3612ff79d52SMike Christie 		hdr->itt = task->hdr_itt = build_itt(task->itt,
3622ff79d52SMike Christie 						     task->conn->session->age);
3639c19a7d0SMike Christie 	task->hdr_len = 0;
3649c19a7d0SMike Christie 	rc = iscsi_add_hdr(task, sizeof(*hdr));
365004d6530SBoaz Harrosh 	if (rc)
366004d6530SBoaz Harrosh 		return rc;
3677996a778SMike Christie 	hdr->opcode = ISCSI_OP_SCSI_CMD;
3687996a778SMike Christie 	hdr->flags = ISCSI_ATTR_SIMPLE;
36955bdabdfSAndy Grover 	int_to_scsilun(sc->device->lun, &hdr->lun);
37055bdabdfSAndy Grover 	task->lun = hdr->lun;
3717996a778SMike Christie 	hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
37238d1c069SBoaz Harrosh 	cmd_len = sc->cmd_len;
37338d1c069SBoaz Harrosh 	if (cmd_len < ISCSI_CDB_SIZE)
37438d1c069SBoaz Harrosh 		memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
37538d1c069SBoaz Harrosh 	else if (cmd_len > ISCSI_CDB_SIZE) {
3769c19a7d0SMike Christie 		rc = iscsi_prep_ecdb_ahs(task);
37738d1c069SBoaz Harrosh 		if (rc)
37838d1c069SBoaz Harrosh 			return rc;
37938d1c069SBoaz Harrosh 		cmd_len = ISCSI_CDB_SIZE;
38038d1c069SBoaz Harrosh 	}
38138d1c069SBoaz Harrosh 	memcpy(hdr->cdb, sc->cmnd, cmd_len);
3827996a778SMike Christie 
3839c19a7d0SMike Christie 	task->imm_count = 0;
384c07d4444SBoaz Harrosh 	if (scsi_bidi_cmnd(sc)) {
385c07d4444SBoaz Harrosh 		hdr->flags |= ISCSI_FLAG_CMD_READ;
3869c19a7d0SMike Christie 		rc = iscsi_prep_bidi_ahs(task);
387c07d4444SBoaz Harrosh 		if (rc)
388c07d4444SBoaz Harrosh 			return rc;
389c07d4444SBoaz Harrosh 	}
39055e51edaSSagi Grimberg 
39155e51edaSSagi Grimberg 	if (scsi_get_prot_op(sc) != SCSI_PROT_NORMAL)
39255e51edaSSagi Grimberg 		task->protected = true;
39355e51edaSSagi Grimberg 
394d77e6535SSagi Grimberg 	transfer_length = scsi_transfer_length(sc);
395d77e6535SSagi Grimberg 	hdr->data_length = cpu_to_be32(transfer_length);
3967996a778SMike Christie 	if (sc->sc_data_direction == DMA_TO_DEVICE) {
397577577daSMike Christie 		struct iscsi_r2t_info *r2t = &task->unsol_r2t;
398577577daSMike Christie 
3997996a778SMike Christie 		hdr->flags |= ISCSI_FLAG_CMD_WRITE;
4007996a778SMike Christie 		/*
4017996a778SMike Christie 		 * Write counters:
4027996a778SMike Christie 		 *
4037996a778SMike Christie 		 *	imm_count	bytes to be sent right after
4047996a778SMike Christie 		 *			SCSI PDU Header
4057996a778SMike Christie 		 *
4067996a778SMike Christie 		 *	unsol_count	bytes(as Data-Out) to be sent
4077996a778SMike Christie 		 *			without	R2T ack right after
4087996a778SMike Christie 		 *			immediate data
4097996a778SMike Christie 		 *
410577577daSMike Christie 		 *	r2t data_length bytes to be sent via R2T ack's
4117996a778SMike Christie 		 *
4127996a778SMike Christie 		 *      pad_count       bytes to be sent as zero-padding
4137996a778SMike Christie 		 */
414577577daSMike Christie 		memset(r2t, 0, sizeof(*r2t));
4157996a778SMike Christie 
4167996a778SMike Christie 		if (session->imm_data_en) {
417d77e6535SSagi Grimberg 			if (transfer_length >= session->first_burst)
4189c19a7d0SMike Christie 				task->imm_count = min(session->first_burst,
4197996a778SMike Christie 							conn->max_xmit_dlength);
4207996a778SMike Christie 			else
421d77e6535SSagi Grimberg 				task->imm_count = min(transfer_length,
4227996a778SMike Christie 						      conn->max_xmit_dlength);
4239c19a7d0SMike Christie 			hton24(hdr->dlength, task->imm_count);
4247996a778SMike Christie 		} else
425a8ac6311SOlaf Kirch 			zero_data(hdr->dlength);
4267996a778SMike Christie 
427ffd0436eSMike Christie 		if (!session->initial_r2t_en) {
428d77e6535SSagi Grimberg 			r2t->data_length = min(session->first_burst,
429d77e6535SSagi Grimberg 					       transfer_length) -
430577577daSMike Christie 					       task->imm_count;
431577577daSMike Christie 			r2t->data_offset = task->imm_count;
432577577daSMike Christie 			r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
433577577daSMike Christie 			r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
434ffd0436eSMike Christie 		}
435ffd0436eSMike Christie 
436577577daSMike Christie 		if (!task->unsol_r2t.data_length)
4377996a778SMike Christie 			/* No unsolicit Data-Out's */
438a8ac6311SOlaf Kirch 			hdr->flags |= ISCSI_FLAG_CMD_FINAL;
4397996a778SMike Christie 	} else {
4407996a778SMike Christie 		hdr->flags |= ISCSI_FLAG_CMD_FINAL;
4417996a778SMike Christie 		zero_data(hdr->dlength);
4427996a778SMike Christie 
4437996a778SMike Christie 		if (sc->sc_data_direction == DMA_FROM_DEVICE)
4447996a778SMike Christie 			hdr->flags |= ISCSI_FLAG_CMD_READ;
4457996a778SMike Christie 	}
4467996a778SMike Christie 
447004d6530SBoaz Harrosh 	/* calculate size of additional header segments (AHSs) */
4489c19a7d0SMike Christie 	hdrlength = task->hdr_len - sizeof(*hdr);
449004d6530SBoaz Harrosh 
450004d6530SBoaz Harrosh 	WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
451004d6530SBoaz Harrosh 	hdrlength /= ISCSI_PAD_LEN;
452004d6530SBoaz Harrosh 
453004d6530SBoaz Harrosh 	WARN_ON(hdrlength >= 256);
454004d6530SBoaz Harrosh 	hdr->hlength = hdrlength & 0xFF;
45596b1f96dSMike Christie 	hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
456004d6530SBoaz Harrosh 
457577577daSMike Christie 	if (session->tt->init_task && session->tt->init_task(task))
458052d0144SMike Christie 		return -EIO;
459052d0144SMike Christie 
4609c19a7d0SMike Christie 	task->state = ISCSI_TASK_RUNNING;
461d3305f34SMike Christie 	session->cmdsn++;
46277a23c21SMike Christie 
463a8ac6311SOlaf Kirch 	conn->scsicmd_pdus_cnt++;
4641b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
4651b2c7af8SMike Christie 			  "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
4661b2c7af8SMike Christie 			  scsi_bidi_cmnd(sc) ? "bidirectional" :
4671b2c7af8SMike Christie 			  sc->sc_data_direction == DMA_TO_DEVICE ?
4681b2c7af8SMike Christie 			  "write" : "read", conn->id, sc, sc->cmnd[0],
469d77e6535SSagi Grimberg 			  task->itt, transfer_length,
4703e5c28adSMike Christie 			  scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
4711b2c7af8SMike Christie 			  session->cmdsn,
4721b2c7af8SMike Christie 			  session->max_cmdsn - session->exp_cmdsn + 1);
473004d6530SBoaz Harrosh 	return 0;
4747996a778SMike Christie }
4757996a778SMike Christie 
4767996a778SMike Christie /**
4773bbaaad9SMike Christie  * iscsi_free_task - free a task
4789c19a7d0SMike Christie  * @task: iscsi cmd task
4797996a778SMike Christie  *
480659743b0SShlomo Pongratz  * Must be called with session back_lock.
4813e5c28adSMike Christie  * This function returns the scsi command to scsi-ml or cleans
4823e5c28adSMike Christie  * up mgmt tasks then returns the task to the pool.
4837996a778SMike Christie  */
4843bbaaad9SMike Christie static void iscsi_free_task(struct iscsi_task *task)
4857996a778SMike Christie {
4869c19a7d0SMike Christie 	struct iscsi_conn *conn = task->conn;
487c1635cb7SMike Christie 	struct iscsi_session *session = conn->session;
4889c19a7d0SMike Christie 	struct scsi_cmnd *sc = task->sc;
489f41d4721SMike Christie 	int oldstate = task->state;
4907996a778SMike Christie 
4914421c9ebSMike Christie 	ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
4924421c9ebSMike Christie 			  task->itt, task->state, task->sc);
4934421c9ebSMike Christie 
494577577daSMike Christie 	session->tt->cleanup_task(task);
4953bbaaad9SMike Christie 	task->state = ISCSI_TASK_FREE;
4969c19a7d0SMike Christie 	task->sc = NULL;
4973e5c28adSMike Christie 	/*
4989c19a7d0SMike Christie 	 * login task is preallocated so do not free
4993e5c28adSMike Christie 	 */
5009c19a7d0SMike Christie 	if (conn->login_task == task)
5013e5c28adSMike Christie 		return;
5023e5c28adSMike Christie 
5037acd72ebSStefani Seibold 	kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
5043e5c28adSMike Christie 
5053e5c28adSMike Christie 	if (sc) {
506f47f2cf5SMike Christie 		/* SCSI eh reuses commands to verify us */
507f47f2cf5SMike Christie 		sc->SCp.ptr = NULL;
5083e5c28adSMike Christie 		/*
509f41d4721SMike Christie 		 * queue command may call this to free the task, so
510f41d4721SMike Christie 		 * it will decide how to return sc to scsi-ml.
5113e5c28adSMike Christie 		 */
512f41d4721SMike Christie 		if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
5137996a778SMike Christie 			sc->scsi_done(sc);
5147996a778SMike Christie 	}
5153e5c28adSMike Christie }
5167996a778SMike Christie 
517913e5bf4SMike Christie void __iscsi_get_task(struct iscsi_task *task)
51860ecebf5SMike Christie {
5199c19a7d0SMike Christie 	atomic_inc(&task->refcount);
52060ecebf5SMike Christie }
521913e5bf4SMike Christie EXPORT_SYMBOL_GPL(__iscsi_get_task);
52260ecebf5SMike Christie 
5238eea2f55SEddie Wai void __iscsi_put_task(struct iscsi_task *task)
52460ecebf5SMike Christie {
5259c19a7d0SMike Christie 	if (atomic_dec_and_test(&task->refcount))
5263bbaaad9SMike Christie 		iscsi_free_task(task);
52760ecebf5SMike Christie }
5288eea2f55SEddie Wai EXPORT_SYMBOL_GPL(__iscsi_put_task);
52960ecebf5SMike Christie 
5309c19a7d0SMike Christie void iscsi_put_task(struct iscsi_task *task)
5313e5c28adSMike Christie {
5329c19a7d0SMike Christie 	struct iscsi_session *session = task->conn->session;
5333e5c28adSMike Christie 
534659743b0SShlomo Pongratz 	/* regular RX path uses back_lock */
535659743b0SShlomo Pongratz 	spin_lock_bh(&session->back_lock);
5369c19a7d0SMike Christie 	__iscsi_put_task(task);
537659743b0SShlomo Pongratz 	spin_unlock_bh(&session->back_lock);
5383e5c28adSMike Christie }
5399c19a7d0SMike Christie EXPORT_SYMBOL_GPL(iscsi_put_task);
5403e5c28adSMike Christie 
5413bbaaad9SMike Christie /**
5423bbaaad9SMike Christie  * iscsi_complete_task - finish a task
5433bbaaad9SMike Christie  * @task: iscsi cmd task
544b3cd5050SMike Christie  * @state: state to complete task with
5453bbaaad9SMike Christie  *
546659743b0SShlomo Pongratz  * Must be called with session back_lock.
547b3a7ea8dSMike Christie  */
548b3cd5050SMike Christie static void iscsi_complete_task(struct iscsi_task *task, int state)
549b3a7ea8dSMike Christie {
5503bbaaad9SMike Christie 	struct iscsi_conn *conn = task->conn;
5513bbaaad9SMike Christie 
5524421c9ebSMike Christie 	ISCSI_DBG_SESSION(conn->session,
5534421c9ebSMike Christie 			  "complete task itt 0x%x state %d sc %p\n",
5544421c9ebSMike Christie 			  task->itt, task->state, task->sc);
555b3cd5050SMike Christie 	if (task->state == ISCSI_TASK_COMPLETED ||
556b3cd5050SMike Christie 	    task->state == ISCSI_TASK_ABRT_TMF ||
557f41d4721SMike Christie 	    task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
558f41d4721SMike Christie 	    task->state == ISCSI_TASK_REQUEUE_SCSIQ)
5593bbaaad9SMike Christie 		return;
5603bbaaad9SMike Christie 	WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
561b3cd5050SMike Christie 	task->state = state;
5623bbaaad9SMike Christie 
563*6f8830f5SChris Leech 	spin_lock_bh(&conn->taskqueuelock);
564*6f8830f5SChris Leech 	if (!list_empty(&task->running)) {
565*6f8830f5SChris Leech 		pr_debug_once("%s while task on list", __func__);
5663bbaaad9SMike Christie 		list_del_init(&task->running);
567*6f8830f5SChris Leech 	}
568*6f8830f5SChris Leech 	spin_unlock_bh(&conn->taskqueuelock);
5693bbaaad9SMike Christie 
5703bbaaad9SMike Christie 	if (conn->task == task)
5713bbaaad9SMike Christie 		conn->task = NULL;
5723bbaaad9SMike Christie 
5733bbaaad9SMike Christie 	if (conn->ping_task == task)
5743bbaaad9SMike Christie 		conn->ping_task = NULL;
5753bbaaad9SMike Christie 
5763bbaaad9SMike Christie 	/* release get from queueing */
5773bbaaad9SMike Christie 	__iscsi_put_task(task);
5783bbaaad9SMike Christie }
5793bbaaad9SMike Christie 
5804c0ba5d2SMike Christie /**
5814c0ba5d2SMike Christie  * iscsi_complete_scsi_task - finish scsi task normally
5824c0ba5d2SMike Christie  * @task: iscsi task for scsi cmd
5834c0ba5d2SMike Christie  * @exp_cmdsn: expected cmd sn in cpu format
5844c0ba5d2SMike Christie  * @max_cmdsn: max cmd sn in cpu format
5854c0ba5d2SMike Christie  *
5864c0ba5d2SMike Christie  * This is used when drivers do not need or cannot perform
5874c0ba5d2SMike Christie  * lower level pdu processing.
5884c0ba5d2SMike Christie  *
589659743b0SShlomo Pongratz  * Called with session back_lock
5904c0ba5d2SMike Christie  */
5914c0ba5d2SMike Christie void iscsi_complete_scsi_task(struct iscsi_task *task,
5924c0ba5d2SMike Christie 			      uint32_t exp_cmdsn, uint32_t max_cmdsn)
5934c0ba5d2SMike Christie {
5944c0ba5d2SMike Christie 	struct iscsi_conn *conn = task->conn;
5954c0ba5d2SMike Christie 
5964c0ba5d2SMike Christie 	ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
5974c0ba5d2SMike Christie 
5984c0ba5d2SMike Christie 	conn->last_recv = jiffies;
5994c0ba5d2SMike Christie 	__iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
6004c0ba5d2SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
6014c0ba5d2SMike Christie }
6024c0ba5d2SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
6034c0ba5d2SMike Christie 
6044c0ba5d2SMike Christie 
6053bbaaad9SMike Christie /*
606659743b0SShlomo Pongratz  * session back_lock must be held and if not called for a task that is
6073bbaaad9SMike Christie  * still pending or from the xmit thread, then xmit thread must
6083bbaaad9SMike Christie  * be suspended.
6093bbaaad9SMike Christie  */
6103bbaaad9SMike Christie static void fail_scsi_task(struct iscsi_task *task, int err)
6113bbaaad9SMike Christie {
6123bbaaad9SMike Christie 	struct iscsi_conn *conn = task->conn;
613b3a7ea8dSMike Christie 	struct scsi_cmnd *sc;
614b3cd5050SMike Christie 	int state;
615b3a7ea8dSMike Christie 
6163bbaaad9SMike Christie 	/*
6173bbaaad9SMike Christie 	 * if a command completes and we get a successful tmf response
6183bbaaad9SMike Christie 	 * we will hit this because the scsi eh abort code does not take
6193bbaaad9SMike Christie 	 * a ref to the task.
6203bbaaad9SMike Christie 	 */
6219c19a7d0SMike Christie 	sc = task->sc;
622b3a7ea8dSMike Christie 	if (!sc)
623b3a7ea8dSMike Christie 		return;
624b3a7ea8dSMike Christie 
625b3cd5050SMike Christie 	if (task->state == ISCSI_TASK_PENDING) {
626b3a7ea8dSMike Christie 		/*
627b3a7ea8dSMike Christie 		 * cmd never made it to the xmit thread, so we should not count
628b3a7ea8dSMike Christie 		 * the cmd in the sequencing
629b3a7ea8dSMike Christie 		 */
630b3a7ea8dSMike Christie 		conn->session->queued_cmdsn--;
631b3cd5050SMike Christie 		/* it was never sent so just complete like normal */
632b3cd5050SMike Christie 		state = ISCSI_TASK_COMPLETED;
633b3cd5050SMike Christie 	} else if (err == DID_TRANSPORT_DISRUPTED)
634b3cd5050SMike Christie 		state = ISCSI_TASK_ABRT_SESS_RECOV;
635b3cd5050SMike Christie 	else
636b3cd5050SMike Christie 		state = ISCSI_TASK_ABRT_TMF;
637b3a7ea8dSMike Christie 
638b3cd5050SMike Christie 	sc->result = err << 16;
639c07d4444SBoaz Harrosh 	if (!scsi_bidi_cmnd(sc))
640b3a7ea8dSMike Christie 		scsi_set_resid(sc, scsi_bufflen(sc));
641c07d4444SBoaz Harrosh 	else {
642c07d4444SBoaz Harrosh 		scsi_out(sc)->resid = scsi_out(sc)->length;
643c07d4444SBoaz Harrosh 		scsi_in(sc)->resid = scsi_in(sc)->length;
644c07d4444SBoaz Harrosh 	}
6453e5c28adSMike Christie 
646659743b0SShlomo Pongratz 	/* regular RX path uses back_lock */
647659743b0SShlomo Pongratz 	spin_lock_bh(&conn->session->back_lock);
648b3cd5050SMike Christie 	iscsi_complete_task(task, state);
649659743b0SShlomo Pongratz 	spin_unlock_bh(&conn->session->back_lock);
650b3a7ea8dSMike Christie }
651b3a7ea8dSMike Christie 
6523e5c28adSMike Christie static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
6539c19a7d0SMike Christie 				struct iscsi_task *task)
654052d0144SMike Christie {
655052d0144SMike Christie 	struct iscsi_session *session = conn->session;
656577577daSMike Christie 	struct iscsi_hdr *hdr = task->hdr;
657052d0144SMike Christie 	struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
6584f704dc0SMike Christie 	uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
659052d0144SMike Christie 
660052d0144SMike Christie 	if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
661052d0144SMike Christie 		return -ENOTCONN;
662052d0144SMike Christie 
6634f704dc0SMike Christie 	if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
664052d0144SMike Christie 		nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
665052d0144SMike Christie 	/*
666052d0144SMike Christie 	 * pre-format CmdSN for outgoing PDU.
667052d0144SMike Christie 	 */
668052d0144SMike Christie 	nop->cmdsn = cpu_to_be32(session->cmdsn);
669052d0144SMike Christie 	if (hdr->itt != RESERVED_ITT) {
670052d0144SMike Christie 		/*
6714f704dc0SMike Christie 		 * TODO: We always use immediate for normal session pdus.
672052d0144SMike Christie 		 * If we start to send tmfs or nops as non-immediate then
673052d0144SMike Christie 		 * we should start checking the cmdsn numbers for mgmt tasks.
6744f704dc0SMike Christie 		 *
6754f704dc0SMike Christie 		 * During discovery sessions iscsid sends TEXT as non immediate,
6764f704dc0SMike Christie 		 * but we always only send one PDU at a time.
677052d0144SMike Christie 		 */
678052d0144SMike Christie 		if (conn->c_stage == ISCSI_CONN_STARTED &&
679052d0144SMike Christie 		    !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
680052d0144SMike Christie 			session->queued_cmdsn++;
681052d0144SMike Christie 			session->cmdsn++;
682052d0144SMike Christie 		}
683052d0144SMike Christie 	}
684052d0144SMike Christie 
685ae15f801SMike Christie 	if (session->tt->init_task && session->tt->init_task(task))
686ae15f801SMike Christie 		return -EIO;
687052d0144SMike Christie 
688052d0144SMike Christie 	if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
689052d0144SMike Christie 		session->state = ISCSI_STATE_LOGGING_OUT;
690052d0144SMike Christie 
691577577daSMike Christie 	task->state = ISCSI_TASK_RUNNING;
6921b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
6931b2c7af8SMike Christie 			  "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
6941b2c7af8SMike Christie 			  hdr->itt, task->data_count);
695052d0144SMike Christie 	return 0;
696052d0144SMike Christie }
697052d0144SMike Christie 
6989c19a7d0SMike Christie static struct iscsi_task *
699f6d5180cSMike Christie __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
700f6d5180cSMike Christie 		      char *data, uint32_t data_size)
701f6d5180cSMike Christie {
702f6d5180cSMike Christie 	struct iscsi_session *session = conn->session;
7031336aed1SMike Christie 	struct iscsi_host *ihost = shost_priv(session->host);
7044f704dc0SMike Christie 	uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
7059c19a7d0SMike Christie 	struct iscsi_task *task;
706262ef636SMike Christie 	itt_t itt;
707f6d5180cSMike Christie 
708f6d5180cSMike Christie 	if (session->state == ISCSI_STATE_TERMINATE)
709f6d5180cSMike Christie 		return NULL;
710f6d5180cSMike Christie 
7114f704dc0SMike Christie 	if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
712f6d5180cSMike Christie 		/*
713f6d5180cSMike Christie 		 * Login and Text are sent serially, in
714f6d5180cSMike Christie 		 * request-followed-by-response sequence.
7153e5c28adSMike Christie 		 * Same task can be used. Same ITT must be used.
7163e5c28adSMike Christie 		 * Note that login_task is preallocated at conn_create().
717f6d5180cSMike Christie 		 */
7184f704dc0SMike Christie 		if (conn->login_task->state != ISCSI_TASK_FREE) {
7194f704dc0SMike Christie 			iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
7204f704dc0SMike Christie 					  "progress. Cannot start new task.\n");
7214f704dc0SMike Christie 			return NULL;
7224f704dc0SMike Christie 		}
7234f704dc0SMike Christie 
724cbaa4221SMike Christie 		if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) {
725cbaa4221SMike 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);
726cbaa4221SMike Christie 			return NULL;
727cbaa4221SMike Christie 		}
728cbaa4221SMike Christie 
7299c19a7d0SMike Christie 		task = conn->login_task;
7304f704dc0SMike Christie 	} else {
73126013ad4SMike Christie 		if (session->state != ISCSI_STATE_LOGGED_IN)
73226013ad4SMike Christie 			return NULL;
73326013ad4SMike Christie 
734cbaa4221SMike Christie 		if (data_size != 0) {
735cbaa4221SMike Christie 			iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode);
736cbaa4221SMike Christie 			return NULL;
737cbaa4221SMike Christie 		}
738cbaa4221SMike Christie 
739f6d5180cSMike Christie 		BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
740f6d5180cSMike Christie 		BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
741f6d5180cSMike Christie 
7427acd72ebSStefani Seibold 		if (!kfifo_out(&session->cmdpool.queue,
7439c19a7d0SMike Christie 				 (void*)&task, sizeof(void*)))
744f6d5180cSMike Christie 			return NULL;
745f6d5180cSMike Christie 	}
7463e5c28adSMike Christie 	/*
7473e5c28adSMike Christie 	 * released in complete pdu for task we expect a response for, and
7483e5c28adSMike Christie 	 * released by the lld when it has transmitted the task for
7493e5c28adSMike Christie 	 * pdus we do not expect a response for.
7503e5c28adSMike Christie 	 */
7519c19a7d0SMike Christie 	atomic_set(&task->refcount, 1);
7529c19a7d0SMike Christie 	task->conn = conn;
7539c19a7d0SMike Christie 	task->sc = NULL;
7543bbaaad9SMike Christie 	INIT_LIST_HEAD(&task->running);
7553bbaaad9SMike Christie 	task->state = ISCSI_TASK_PENDING;
756f6d5180cSMike Christie 
757f6d5180cSMike Christie 	if (data_size) {
7589c19a7d0SMike Christie 		memcpy(task->data, data, data_size);
7599c19a7d0SMike Christie 		task->data_count = data_size;
760f6d5180cSMike Christie 	} else
7619c19a7d0SMike Christie 		task->data_count = 0;
762f6d5180cSMike Christie 
763184b57c6SMike Christie 	if (conn->session->tt->alloc_pdu) {
7642ff79d52SMike Christie 		if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
765577577daSMike Christie 			iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
766577577daSMike Christie 					 "pdu for mgmt task.\n");
7673bbaaad9SMike Christie 			goto free_task;
768577577daSMike Christie 		}
769184b57c6SMike Christie 	}
770184b57c6SMike Christie 
771262ef636SMike Christie 	itt = task->hdr->itt;
772577577daSMike Christie 	task->hdr_len = sizeof(struct iscsi_hdr);
7739c19a7d0SMike Christie 	memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
774262ef636SMike Christie 
775262ef636SMike Christie 	if (hdr->itt != RESERVED_ITT) {
776262ef636SMike Christie 		if (session->tt->parse_pdu_itt)
777262ef636SMike Christie 			task->hdr->itt = itt;
778262ef636SMike Christie 		else
779262ef636SMike Christie 			task->hdr->itt = build_itt(task->itt,
780262ef636SMike Christie 						   task->conn->session->age);
781262ef636SMike Christie 	}
782262ef636SMike Christie 
7831336aed1SMike Christie 	if (!ihost->workq) {
784577577daSMike Christie 		if (iscsi_prep_mgmt_task(conn, task))
785577577daSMike Christie 			goto free_task;
786052d0144SMike Christie 
7879c19a7d0SMike Christie 		if (session->tt->xmit_task(task))
788577577daSMike Christie 			goto free_task;
7893bbaaad9SMike Christie 	} else {
790*6f8830f5SChris Leech 		spin_lock_bh(&conn->taskqueuelock);
7913bbaaad9SMike Christie 		list_add_tail(&task->running, &conn->mgmtqueue);
792*6f8830f5SChris Leech 		spin_unlock_bh(&conn->taskqueuelock);
79332ae763eSMike Christie 		iscsi_conn_queue_work(conn);
7943bbaaad9SMike Christie 	}
795052d0144SMike Christie 
7969c19a7d0SMike Christie 	return task;
797577577daSMike Christie 
798577577daSMike Christie free_task:
799659743b0SShlomo Pongratz 	/* regular RX path uses back_lock */
8004fa50799SJitendra Bhivare 	spin_lock(&session->back_lock);
801577577daSMike Christie 	__iscsi_put_task(task);
8024fa50799SJitendra Bhivare 	spin_unlock(&session->back_lock);
803577577daSMike Christie 	return NULL;
804f6d5180cSMike Christie }
805f6d5180cSMike Christie 
806f6d5180cSMike Christie int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
807f6d5180cSMike Christie 			char *data, uint32_t data_size)
808f6d5180cSMike Christie {
809f6d5180cSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
810f6d5180cSMike Christie 	struct iscsi_session *session = conn->session;
811f6d5180cSMike Christie 	int err = 0;
812f6d5180cSMike Christie 
813659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
814f6d5180cSMike Christie 	if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
815f6d5180cSMike Christie 		err = -EPERM;
816659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
817f6d5180cSMike Christie 	return err;
818f6d5180cSMike Christie }
819f6d5180cSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
820f6d5180cSMike Christie 
8217996a778SMike Christie /**
8227996a778SMike Christie  * iscsi_cmd_rsp - SCSI Command Response processing
8237996a778SMike Christie  * @conn: iscsi connection
8247996a778SMike Christie  * @hdr: iscsi header
8259c19a7d0SMike Christie  * @task: scsi command task
8267996a778SMike Christie  * @data: cmd data buffer
8277996a778SMike Christie  * @datalen: len of buffer
8287996a778SMike Christie  *
8297996a778SMike Christie  * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
8309c19a7d0SMike Christie  * then completes the command and task.
8317996a778SMike Christie  **/
83277a23c21SMike Christie static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
8339c19a7d0SMike Christie 			       struct iscsi_task *task, char *data,
8347996a778SMike Christie 			       int datalen)
8357996a778SMike Christie {
83612352183SNicholas Bellinger 	struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
8377996a778SMike Christie 	struct iscsi_session *session = conn->session;
8389c19a7d0SMike Christie 	struct scsi_cmnd *sc = task->sc;
8397996a778SMike Christie 
84077a23c21SMike Christie 	iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
8417996a778SMike Christie 	conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
8427996a778SMike Christie 
8437996a778SMike Christie 	sc->result = (DID_OK << 16) | rhdr->cmd_status;
8447996a778SMike Christie 
84555e51edaSSagi Grimberg 	if (task->protected) {
84655e51edaSSagi Grimberg 		sector_t sector;
84755e51edaSSagi Grimberg 		u8 ascq;
84855e51edaSSagi Grimberg 
84955e51edaSSagi Grimberg 		/**
85055e51edaSSagi Grimberg 		 * Transports that didn't implement check_protection
85155e51edaSSagi Grimberg 		 * callback but still published T10-PI support to scsi-mid
85255e51edaSSagi Grimberg 		 * deserve this BUG_ON.
85355e51edaSSagi Grimberg 		 **/
85455e51edaSSagi Grimberg 		BUG_ON(!session->tt->check_protection);
85555e51edaSSagi Grimberg 
85655e51edaSSagi Grimberg 		ascq = session->tt->check_protection(task, &sector);
85755e51edaSSagi Grimberg 		if (ascq) {
85855e51edaSSagi Grimberg 			sc->result = DRIVER_SENSE << 24 |
85955e51edaSSagi Grimberg 				     SAM_STAT_CHECK_CONDITION;
86055e51edaSSagi Grimberg 			scsi_build_sense_buffer(1, sc->sense_buffer,
86155e51edaSSagi Grimberg 						ILLEGAL_REQUEST, 0x10, ascq);
862a73c2a2fSSagi Grimberg 			scsi_set_sense_information(sc->sense_buffer,
863a73c2a2fSSagi Grimberg 						   SCSI_SENSE_BUFFERSIZE,
864a73c2a2fSSagi Grimberg 						   sector);
86555e51edaSSagi Grimberg 			goto out;
86655e51edaSSagi Grimberg 		}
86755e51edaSSagi Grimberg 	}
86855e51edaSSagi Grimberg 
8697996a778SMike Christie 	if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
8707996a778SMike Christie 		sc->result = DID_ERROR << 16;
8717996a778SMike Christie 		goto out;
8727996a778SMike Christie 	}
8737996a778SMike Christie 
8747996a778SMike Christie 	if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
8759b80cb4bSMike Christie 		uint16_t senselen;
8767996a778SMike Christie 
8777996a778SMike Christie 		if (datalen < 2) {
8787996a778SMike Christie invalid_datalen:
879322d739dSMike Christie 			iscsi_conn_printk(KERN_ERR,  conn,
880322d739dSMike Christie 					 "Got CHECK_CONDITION but invalid data "
881322d739dSMike Christie 					 "buffer size of %d\n", datalen);
8827996a778SMike Christie 			sc->result = DID_BAD_TARGET << 16;
8837996a778SMike Christie 			goto out;
8847996a778SMike Christie 		}
8857996a778SMike Christie 
8868f333991SHarvey Harrison 		senselen = get_unaligned_be16(data);
8877996a778SMike Christie 		if (datalen < senselen)
8887996a778SMike Christie 			goto invalid_datalen;
8897996a778SMike Christie 
8907996a778SMike Christie 		memcpy(sc->sense_buffer, data + 2,
8919b80cb4bSMike Christie 		       min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
8921b2c7af8SMike Christie 		ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
8931b2c7af8SMike Christie 				  min_t(uint16_t, senselen,
8941b2c7af8SMike Christie 				  SCSI_SENSE_BUFFERSIZE));
8957996a778SMike Christie 	}
8967996a778SMike Christie 
897c07d4444SBoaz Harrosh 	if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
898c07d4444SBoaz Harrosh 			   ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
899c07d4444SBoaz Harrosh 		int res_count = be32_to_cpu(rhdr->bi_residual_count);
900c07d4444SBoaz Harrosh 
901c07d4444SBoaz Harrosh 		if (scsi_bidi_cmnd(sc) && res_count > 0 &&
902c07d4444SBoaz Harrosh 				(rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
903c07d4444SBoaz Harrosh 				 res_count <= scsi_in(sc)->length))
904c07d4444SBoaz Harrosh 			scsi_in(sc)->resid = res_count;
905c07d4444SBoaz Harrosh 		else
906c07d4444SBoaz Harrosh 			sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
907c07d4444SBoaz Harrosh 	}
908c07d4444SBoaz Harrosh 
9097207fea4SBoaz Harrosh 	if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
9107207fea4SBoaz Harrosh 	                   ISCSI_FLAG_CMD_OVERFLOW)) {
9117996a778SMike Christie 		int res_count = be32_to_cpu(rhdr->residual_count);
9127996a778SMike Christie 
9137207fea4SBoaz Harrosh 		if (res_count > 0 &&
9147207fea4SBoaz Harrosh 		    (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
9157207fea4SBoaz Harrosh 		     res_count <= scsi_bufflen(sc)))
916c07d4444SBoaz Harrosh 			/* write side for bidi or uni-io set_resid */
9171c138991SFUJITA Tomonori 			scsi_set_resid(sc, res_count);
9187996a778SMike Christie 		else
9197996a778SMike Christie 			sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
920c07d4444SBoaz Harrosh 	}
9217996a778SMike Christie out:
9223bbaaad9SMike Christie 	ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
9231b2c7af8SMike Christie 			  sc, sc->result, task->itt);
9247996a778SMike Christie 	conn->scsirsp_pdus_cnt++;
925b3cd5050SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
9267996a778SMike Christie }
9277996a778SMike Christie 
9281d9edf02SMike Christie /**
9291d9edf02SMike Christie  * iscsi_data_in_rsp - SCSI Data-In Response processing
9301d9edf02SMike Christie  * @conn: iscsi connection
9311d9edf02SMike Christie  * @hdr:  iscsi pdu
9321d9edf02SMike Christie  * @task: scsi command task
9331d9edf02SMike Christie  **/
9341d9edf02SMike Christie static void
9351d9edf02SMike Christie iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
9361d9edf02SMike Christie 		  struct iscsi_task *task)
9371d9edf02SMike Christie {
9381d9edf02SMike Christie 	struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
9391d9edf02SMike Christie 	struct scsi_cmnd *sc = task->sc;
9401d9edf02SMike Christie 
9411d9edf02SMike Christie 	if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
9421d9edf02SMike Christie 		return;
9431d9edf02SMike Christie 
944edbc9aa0SMike Christie 	iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
9451d9edf02SMike Christie 	sc->result = (DID_OK << 16) | rhdr->cmd_status;
9461d9edf02SMike Christie 	conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
9471d9edf02SMike Christie 	if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
9481d9edf02SMike Christie 	                   ISCSI_FLAG_DATA_OVERFLOW)) {
9491d9edf02SMike Christie 		int res_count = be32_to_cpu(rhdr->residual_count);
9501d9edf02SMike Christie 
9511d9edf02SMike Christie 		if (res_count > 0 &&
9521d9edf02SMike Christie 		    (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
9531d9edf02SMike Christie 		     res_count <= scsi_in(sc)->length))
9541d9edf02SMike Christie 			scsi_in(sc)->resid = res_count;
9551d9edf02SMike Christie 		else
9561d9edf02SMike Christie 			sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
9571d9edf02SMike Christie 	}
9581d9edf02SMike Christie 
9593bbaaad9SMike Christie 	ISCSI_DBG_SESSION(conn->session, "data in with status done "
9603bbaaad9SMike Christie 			  "[sc %p res %d itt 0x%x]\n",
9613bbaaad9SMike Christie 			  sc, sc->result, task->itt);
9621d9edf02SMike Christie 	conn->scsirsp_pdus_cnt++;
963b3cd5050SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
9641d9edf02SMike Christie }
9651d9edf02SMike Christie 
9667ea8b828SMike Christie static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
9677ea8b828SMike Christie {
9687ea8b828SMike Christie 	struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
9697ea8b828SMike Christie 
9707ea8b828SMike Christie 	conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
9717ea8b828SMike Christie 	conn->tmfrsp_pdus_cnt++;
9727ea8b828SMike Christie 
973843c0a8aSMike Christie 	if (conn->tmf_state != TMF_QUEUED)
9747ea8b828SMike Christie 		return;
9757ea8b828SMike Christie 
9767ea8b828SMike Christie 	if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
977843c0a8aSMike Christie 		conn->tmf_state = TMF_SUCCESS;
9787ea8b828SMike Christie 	else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
979843c0a8aSMike Christie 		conn->tmf_state = TMF_NOT_FOUND;
9807ea8b828SMike Christie 	else
981843c0a8aSMike Christie 		conn->tmf_state = TMF_FAILED;
9827ea8b828SMike Christie 	wake_up(&conn->ehwait);
9837ea8b828SMike Christie }
9847ea8b828SMike Christie 
98552f5664aSAriel Nahum static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
986f6d5180cSMike Christie {
987f6d5180cSMike Christie         struct iscsi_nopout hdr;
9889c19a7d0SMike Christie 	struct iscsi_task *task;
989f6d5180cSMike Christie 
9909c19a7d0SMike Christie 	if (!rhdr && conn->ping_task)
99152f5664aSAriel Nahum 		return -EINVAL;
992f6d5180cSMike Christie 
993f6d5180cSMike Christie 	memset(&hdr, 0, sizeof(struct iscsi_nopout));
994f6d5180cSMike Christie 	hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
995f6d5180cSMike Christie 	hdr.flags = ISCSI_FLAG_CMD_FINAL;
996f6d5180cSMike Christie 
997f6d5180cSMike Christie 	if (rhdr) {
99855bdabdfSAndy Grover 		hdr.lun = rhdr->lun;
999f6d5180cSMike Christie 		hdr.ttt = rhdr->ttt;
1000f6d5180cSMike Christie 		hdr.itt = RESERVED_ITT;
1001f6d5180cSMike Christie 	} else
1002f6d5180cSMike Christie 		hdr.ttt = RESERVED_ITT;
1003f6d5180cSMike Christie 
10049c19a7d0SMike Christie 	task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
100552f5664aSAriel Nahum 	if (!task) {
1006322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
100752f5664aSAriel Nahum 		return -EIO;
100852f5664aSAriel Nahum 	} else if (!rhdr) {
1009d3acf022SMike Christie 		/* only track our nops */
1010d3acf022SMike Christie 		conn->ping_task = task;
1011d3acf022SMike Christie 		conn->last_ping = jiffies;
1012d3acf022SMike Christie 	}
101352f5664aSAriel Nahum 
101452f5664aSAriel Nahum 	return 0;
1015f6d5180cSMike Christie }
1016f6d5180cSMike Christie 
10178afa1439SMike Christie static int iscsi_nop_out_rsp(struct iscsi_task *task,
10188afa1439SMike Christie 			     struct iscsi_nopin *nop, char *data, int datalen)
10198afa1439SMike Christie {
10208afa1439SMike Christie 	struct iscsi_conn *conn = task->conn;
10218afa1439SMike Christie 	int rc = 0;
10228afa1439SMike Christie 
10238afa1439SMike Christie 	if (conn->ping_task != task) {
10248afa1439SMike Christie 		/*
10258afa1439SMike Christie 		 * If this is not in response to one of our
10268afa1439SMike Christie 		 * nops then it must be from userspace.
10278afa1439SMike Christie 		 */
10288afa1439SMike Christie 		if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
10298afa1439SMike Christie 				   data, datalen))
10308afa1439SMike Christie 			rc = ISCSI_ERR_CONN_FAILED;
10318afa1439SMike Christie 	} else
10328afa1439SMike Christie 		mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
10338afa1439SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
10348afa1439SMike Christie 	return rc;
10358afa1439SMike Christie }
10368afa1439SMike Christie 
103762f38300SMike Christie static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
103862f38300SMike Christie 			       char *data, int datalen)
103962f38300SMike Christie {
104062f38300SMike Christie 	struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
104162f38300SMike Christie 	struct iscsi_hdr rejected_pdu;
10428afa1439SMike Christie 	int opcode, rc = 0;
104362f38300SMike Christie 
104462f38300SMike Christie 	conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
104562f38300SMike Christie 
10468afa1439SMike Christie 	if (ntoh24(reject->dlength) > datalen ||
10478afa1439SMike Christie 	    ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
10488afa1439SMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
10498afa1439SMike Christie 				  "pdu. Invalid data length (pdu dlength "
10508afa1439SMike Christie 				  "%u, datalen %d\n", ntoh24(reject->dlength),
10518afa1439SMike Christie 				  datalen);
105262f38300SMike Christie 		return ISCSI_ERR_PROTO;
10538afa1439SMike Christie 	}
105462f38300SMike Christie 	memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
10558afa1439SMike Christie 	opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
10568afa1439SMike Christie 
10578afa1439SMike Christie 	switch (reject->reason) {
10588afa1439SMike Christie 	case ISCSI_REASON_DATA_DIGEST_ERROR:
1059322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
10608afa1439SMike Christie 				  "pdu (op 0x%x itt 0x%x) rejected "
1061262ef636SMike Christie 				  "due to DataDigest error.\n",
1062e5dbbe27SVaughan Cao 				  opcode, rejected_pdu.itt);
10638afa1439SMike Christie 		break;
10648afa1439SMike Christie 	case ISCSI_REASON_IMM_CMD_REJECT:
10658afa1439SMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
10668afa1439SMike Christie 				  "pdu (op 0x%x itt 0x%x) rejected. Too many "
10678afa1439SMike Christie 				  "immediate commands.\n",
1068e5dbbe27SVaughan Cao 				  opcode, rejected_pdu.itt);
10698afa1439SMike Christie 		/*
10708afa1439SMike Christie 		 * We only send one TMF at a time so if the target could not
10718afa1439SMike Christie 		 * handle it, then it should get fixed (RFC mandates that
10728afa1439SMike Christie 		 * a target can handle one immediate TMF per conn).
10738afa1439SMike Christie 		 *
10748afa1439SMike Christie 		 * For nops-outs, we could have sent more than one if
10758afa1439SMike Christie 		 * the target is sending us lots of nop-ins
10768afa1439SMike Christie 		 */
10778afa1439SMike Christie 		if (opcode != ISCSI_OP_NOOP_OUT)
107862f38300SMike Christie 			return 0;
10798afa1439SMike Christie 
1080659743b0SShlomo Pongratz 		 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
10818afa1439SMike Christie 			/*
10828afa1439SMike Christie 			 * nop-out in response to target's nop-out rejected.
10838afa1439SMike Christie 			 * Just resend.
10848afa1439SMike Christie 			 */
1085659743b0SShlomo Pongratz 			/* In RX path we are under back lock */
1086659743b0SShlomo Pongratz 			spin_unlock(&conn->session->back_lock);
1087659743b0SShlomo Pongratz 			spin_lock(&conn->session->frwd_lock);
10888afa1439SMike Christie 			iscsi_send_nopout(conn,
10898afa1439SMike Christie 					  (struct iscsi_nopin*)&rejected_pdu);
1090659743b0SShlomo Pongratz 			spin_unlock(&conn->session->frwd_lock);
1091659743b0SShlomo Pongratz 			spin_lock(&conn->session->back_lock);
1092659743b0SShlomo Pongratz 		} else {
10938afa1439SMike Christie 			struct iscsi_task *task;
10948afa1439SMike Christie 			/*
10958afa1439SMike Christie 			 * Our nop as ping got dropped. We know the target
10968afa1439SMike Christie 			 * and transport are ok so just clean up
10978afa1439SMike Christie 			 */
10988afa1439SMike Christie 			task = iscsi_itt_to_task(conn, rejected_pdu.itt);
10998afa1439SMike Christie 			if (!task) {
11008afa1439SMike Christie 				iscsi_conn_printk(KERN_ERR, conn,
11018afa1439SMike Christie 						 "Invalid pdu reject. Could "
11028afa1439SMike Christie 						 "not lookup rejected task.\n");
11038afa1439SMike Christie 				rc = ISCSI_ERR_BAD_ITT;
11048afa1439SMike Christie 			} else
11058afa1439SMike Christie 				rc = iscsi_nop_out_rsp(task,
11068afa1439SMike Christie 					(struct iscsi_nopin*)&rejected_pdu,
11078afa1439SMike Christie 					NULL, 0);
11088afa1439SMike Christie 		}
11098afa1439SMike Christie 		break;
11108afa1439SMike Christie 	default:
11118afa1439SMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
11128afa1439SMike Christie 				  "pdu (op 0x%x itt 0x%x) rejected. Reason "
1113e5dbbe27SVaughan Cao 				  "code 0x%x\n", rejected_pdu.opcode,
1114e5dbbe27SVaughan Cao 				  rejected_pdu.itt, reject->reason);
11158afa1439SMike Christie 		break;
11168afa1439SMike Christie 	}
11178afa1439SMike Christie 	return rc;
111862f38300SMike Christie }
111962f38300SMike Christie 
11207996a778SMike Christie /**
1121913e5bf4SMike Christie  * iscsi_itt_to_task - look up task by itt
1122913e5bf4SMike Christie  * @conn: iscsi connection
1123913e5bf4SMike Christie  * @itt: itt
1124913e5bf4SMike Christie  *
1125913e5bf4SMike Christie  * This should be used for mgmt tasks like login and nops, or if
1126913e5bf4SMike Christie  * the LDD's itt space does not include the session age.
1127913e5bf4SMike Christie  *
1128659743b0SShlomo Pongratz  * The session back_lock must be held.
1129913e5bf4SMike Christie  */
11308f9256ceSMike Christie struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
1131913e5bf4SMike Christie {
1132913e5bf4SMike Christie 	struct iscsi_session *session = conn->session;
1133262ef636SMike Christie 	int i;
1134913e5bf4SMike Christie 
1135913e5bf4SMike Christie 	if (itt == RESERVED_ITT)
1136913e5bf4SMike Christie 		return NULL;
1137913e5bf4SMike Christie 
1138262ef636SMike Christie 	if (session->tt->parse_pdu_itt)
1139262ef636SMike Christie 		session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1140262ef636SMike Christie 	else
1141913e5bf4SMike Christie 		i = get_itt(itt);
1142913e5bf4SMike Christie 	if (i >= session->cmds_max)
1143913e5bf4SMike Christie 		return NULL;
1144913e5bf4SMike Christie 
1145913e5bf4SMike Christie 	return session->cmds[i];
1146913e5bf4SMike Christie }
11478f9256ceSMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
1148913e5bf4SMike Christie 
1149913e5bf4SMike Christie /**
11507996a778SMike Christie  * __iscsi_complete_pdu - complete pdu
11517996a778SMike Christie  * @conn: iscsi conn
11527996a778SMike Christie  * @hdr: iscsi header
11537996a778SMike Christie  * @data: data buffer
11547996a778SMike Christie  * @datalen: len of data buffer
11557996a778SMike Christie  *
11567996a778SMike Christie  * Completes pdu processing by freeing any resources allocated at
1157659743b0SShlomo Pongratz  * queuecommand or send generic. session back_lock must be held and verify
11587996a778SMike Christie  * itt must have been called.
11597996a778SMike Christie  */
1160913e5bf4SMike Christie int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
11617996a778SMike Christie 			 char *data, int datalen)
11627996a778SMike Christie {
11637996a778SMike Christie 	struct iscsi_session *session = conn->session;
11647996a778SMike Christie 	int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
11659c19a7d0SMike Christie 	struct iscsi_task *task;
11667996a778SMike Christie 	uint32_t itt;
11677996a778SMike Christie 
1168f6d5180cSMike Christie 	conn->last_recv = jiffies;
11690af967f5SMike Christie 	rc = iscsi_verify_itt(conn, hdr->itt);
11700af967f5SMike Christie 	if (rc)
11710af967f5SMike Christie 		return rc;
11720af967f5SMike Christie 
1173b4377356SAl Viro 	if (hdr->itt != RESERVED_ITT)
1174b4377356SAl Viro 		itt = get_itt(hdr->itt);
11757996a778SMike Christie 	else
1176b4377356SAl Viro 		itt = ~0U;
11777996a778SMike Christie 
11781b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
11793e5c28adSMike Christie 			  opcode, conn->id, itt, datalen);
11807996a778SMike Christie 
11813e5c28adSMike Christie 	if (itt == ~0U) {
118277a23c21SMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
118362f38300SMike Christie 
11847996a778SMike Christie 		switch(opcode) {
11857996a778SMike Christie 		case ISCSI_OP_NOOP_IN:
118640527afeSMike Christie 			if (datalen) {
118740527afeSMike Christie 				rc = ISCSI_ERR_PROTO;
118840527afeSMike Christie 				break;
118940527afeSMike Christie 			}
119040527afeSMike Christie 
1191b4377356SAl Viro 			if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
119240527afeSMike Christie 				break;
119340527afeSMike Christie 
1194659743b0SShlomo Pongratz 			/* In RX path we are under back lock */
1195659743b0SShlomo Pongratz 			spin_unlock(&session->back_lock);
1196659743b0SShlomo Pongratz 			spin_lock(&session->frwd_lock);
1197f6d5180cSMike Christie 			iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
1198659743b0SShlomo Pongratz 			spin_unlock(&session->frwd_lock);
1199659743b0SShlomo Pongratz 			spin_lock(&session->back_lock);
12007996a778SMike Christie 			break;
12017996a778SMike Christie 		case ISCSI_OP_REJECT:
120262f38300SMike Christie 			rc = iscsi_handle_reject(conn, hdr, data, datalen);
120362f38300SMike Christie 			break;
12047996a778SMike Christie 		case ISCSI_OP_ASYNC_EVENT:
12058d2860b3SMike Christie 			conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
12065831c737SMike Christie 			if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
12075831c737SMike Christie 				rc = ISCSI_ERR_CONN_FAILED;
12087996a778SMike Christie 			break;
12097996a778SMike Christie 		default:
12107996a778SMike Christie 			rc = ISCSI_ERR_BAD_OPCODE;
12117996a778SMike Christie 			break;
12127996a778SMike Christie 		}
12133e5c28adSMike Christie 		goto out;
12143e5c28adSMike Christie 	}
12157996a778SMike Christie 
12163e5c28adSMike Christie 	switch(opcode) {
12173e5c28adSMike Christie 	case ISCSI_OP_SCSI_CMD_RSP:
1218913e5bf4SMike Christie 	case ISCSI_OP_SCSI_DATA_IN:
1219913e5bf4SMike Christie 		task = iscsi_itt_to_ctask(conn, hdr->itt);
1220913e5bf4SMike Christie 		if (!task)
1221913e5bf4SMike Christie 			return ISCSI_ERR_BAD_ITT;
1222d355e57dSMike Christie 		task->last_xfer = jiffies;
12233e5c28adSMike Christie 		break;
1224913e5bf4SMike Christie 	case ISCSI_OP_R2T:
1225913e5bf4SMike Christie 		/*
1226913e5bf4SMike Christie 		 * LLD handles R2Ts if they need to.
1227913e5bf4SMike Christie 		 */
1228913e5bf4SMike Christie 		return 0;
1229913e5bf4SMike Christie 	case ISCSI_OP_LOGOUT_RSP:
1230913e5bf4SMike Christie 	case ISCSI_OP_LOGIN_RSP:
1231913e5bf4SMike Christie 	case ISCSI_OP_TEXT_RSP:
1232913e5bf4SMike Christie 	case ISCSI_OP_SCSI_TMFUNC_RSP:
1233913e5bf4SMike Christie 	case ISCSI_OP_NOOP_IN:
1234913e5bf4SMike Christie 		task = iscsi_itt_to_task(conn, hdr->itt);
1235913e5bf4SMike Christie 		if (!task)
1236913e5bf4SMike Christie 			return ISCSI_ERR_BAD_ITT;
1237913e5bf4SMike Christie 		break;
1238913e5bf4SMike Christie 	default:
1239913e5bf4SMike Christie 		return ISCSI_ERR_BAD_OPCODE;
12403e5c28adSMike Christie 	}
1241913e5bf4SMike Christie 
1242913e5bf4SMike Christie 	switch(opcode) {
1243913e5bf4SMike Christie 	case ISCSI_OP_SCSI_CMD_RSP:
12449c19a7d0SMike Christie 		iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
12453e5c28adSMike Christie 		break;
12463e5c28adSMike Christie 	case ISCSI_OP_SCSI_DATA_IN:
12471d9edf02SMike Christie 		iscsi_data_in_rsp(conn, hdr, task);
12483e5c28adSMike Christie 		break;
12493e5c28adSMike Christie 	case ISCSI_OP_LOGOUT_RSP:
12503e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12513e5c28adSMike Christie 		if (datalen) {
12523e5c28adSMike Christie 			rc = ISCSI_ERR_PROTO;
12533e5c28adSMike Christie 			break;
12543e5c28adSMike Christie 		}
12553e5c28adSMike Christie 		conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
12563e5c28adSMike Christie 		goto recv_pdu;
12573e5c28adSMike Christie 	case ISCSI_OP_LOGIN_RSP:
12583e5c28adSMike Christie 	case ISCSI_OP_TEXT_RSP:
12593e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12603e5c28adSMike Christie 		/*
12613e5c28adSMike Christie 		 * login related PDU's exp_statsn is handled in
12623e5c28adSMike Christie 		 * userspace
12633e5c28adSMike Christie 		 */
12643e5c28adSMike Christie 		goto recv_pdu;
12653e5c28adSMike Christie 	case ISCSI_OP_SCSI_TMFUNC_RSP:
12663e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12673e5c28adSMike Christie 		if (datalen) {
12683e5c28adSMike Christie 			rc = ISCSI_ERR_PROTO;
12693e5c28adSMike Christie 			break;
12703e5c28adSMike Christie 		}
12713e5c28adSMike Christie 
12723e5c28adSMike Christie 		iscsi_tmf_rsp(conn, hdr);
1273b3cd5050SMike Christie 		iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
12743e5c28adSMike Christie 		break;
12753e5c28adSMike Christie 	case ISCSI_OP_NOOP_IN:
12763e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12773e5c28adSMike Christie 		if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
12783e5c28adSMike Christie 			rc = ISCSI_ERR_PROTO;
12793e5c28adSMike Christie 			break;
12803e5c28adSMike Christie 		}
12813e5c28adSMike Christie 		conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
12823e5c28adSMike Christie 
12838afa1439SMike Christie 		rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
12848afa1439SMike Christie 				       data, datalen);
12853e5c28adSMike Christie 		break;
12863e5c28adSMike Christie 	default:
12873e5c28adSMike Christie 		rc = ISCSI_ERR_BAD_OPCODE;
12883e5c28adSMike Christie 		break;
12893e5c28adSMike Christie 	}
12903e5c28adSMike Christie 
12913e5c28adSMike Christie out:
12923e5c28adSMike Christie 	return rc;
12933e5c28adSMike Christie recv_pdu:
12943e5c28adSMike Christie 	if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
12953e5c28adSMike Christie 		rc = ISCSI_ERR_CONN_FAILED;
1296b3cd5050SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
12977996a778SMike Christie 	return rc;
12987996a778SMike Christie }
1299913e5bf4SMike Christie EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
13007996a778SMike Christie 
13017996a778SMike Christie int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
13027996a778SMike Christie 		       char *data, int datalen)
13037996a778SMike Christie {
13047996a778SMike Christie 	int rc;
13057996a778SMike Christie 
1306659743b0SShlomo Pongratz 	spin_lock(&conn->session->back_lock);
13077996a778SMike Christie 	rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1308659743b0SShlomo Pongratz 	spin_unlock(&conn->session->back_lock);
13097996a778SMike Christie 	return rc;
13107996a778SMike Christie }
13117996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
13127996a778SMike Christie 
13130af967f5SMike Christie int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
13147996a778SMike Christie {
13157996a778SMike Christie 	struct iscsi_session *session = conn->session;
1316262ef636SMike Christie 	int age = 0, i = 0;
13177996a778SMike Christie 
13180af967f5SMike Christie 	if (itt == RESERVED_ITT)
13190af967f5SMike Christie 		return 0;
13200af967f5SMike Christie 
1321262ef636SMike Christie 	if (session->tt->parse_pdu_itt)
1322262ef636SMike Christie 		session->tt->parse_pdu_itt(conn, itt, &i, &age);
1323262ef636SMike Christie 	else {
1324262ef636SMike Christie 		i = get_itt(itt);
1325262ef636SMike Christie 		age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1326262ef636SMike Christie 	}
1327262ef636SMike Christie 
1328262ef636SMike Christie 	if (age != session->age) {
1329322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
13300af967f5SMike Christie 				  "received itt %x expected session age (%x)\n",
1331913e5bf4SMike Christie 				  (__force u32)itt, session->age);
13327996a778SMike Christie 		return ISCSI_ERR_BAD_ITT;
13337996a778SMike Christie 	}
13347996a778SMike Christie 
13353e5c28adSMike Christie 	if (i >= session->cmds_max) {
13363e5c28adSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
13373e5c28adSMike Christie 				  "received invalid itt index %u (max cmds "
13383e5c28adSMike Christie 				   "%u.\n", i, session->cmds_max);
13393e5c28adSMike Christie 		return ISCSI_ERR_BAD_ITT;
13407996a778SMike Christie 	}
13417996a778SMike Christie 	return 0;
13427996a778SMike Christie }
13437996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_verify_itt);
13447996a778SMike Christie 
1345913e5bf4SMike Christie /**
1346913e5bf4SMike Christie  * iscsi_itt_to_ctask - look up ctask by itt
1347913e5bf4SMike Christie  * @conn: iscsi connection
1348913e5bf4SMike Christie  * @itt: itt
1349913e5bf4SMike Christie  *
1350913e5bf4SMike Christie  * This should be used for cmd tasks.
1351913e5bf4SMike Christie  *
1352659743b0SShlomo Pongratz  * The session back_lock must be held.
1353913e5bf4SMike Christie  */
1354913e5bf4SMike Christie struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
13550af967f5SMike Christie {
13569c19a7d0SMike Christie 	struct iscsi_task *task;
13570af967f5SMike Christie 
13580af967f5SMike Christie 	if (iscsi_verify_itt(conn, itt))
13590af967f5SMike Christie 		return NULL;
13600af967f5SMike Christie 
1361913e5bf4SMike Christie 	task = iscsi_itt_to_task(conn, itt);
1362913e5bf4SMike Christie 	if (!task || !task->sc)
13630af967f5SMike Christie 		return NULL;
13640af967f5SMike Christie 
1365913e5bf4SMike Christie 	if (task->sc->SCp.phase != conn->session->age) {
1366913e5bf4SMike Christie 		iscsi_session_printk(KERN_ERR, conn->session,
1367913e5bf4SMike Christie 				  "task's session age %d, expected %d\n",
1368913e5bf4SMike Christie 				  task->sc->SCp.phase, conn->session->age);
13690af967f5SMike Christie 		return NULL;
1370913e5bf4SMike Christie 	}
13710af967f5SMike Christie 
13729c19a7d0SMike Christie 	return task;
13730af967f5SMike Christie }
13740af967f5SMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
13750af967f5SMike Christie 
137640a06e75SMike Christie void iscsi_session_failure(struct iscsi_session *session,
1377e5bd7b54SMike Christie 			   enum iscsi_err err)
1378e5bd7b54SMike Christie {
1379e5bd7b54SMike Christie 	struct iscsi_conn *conn;
1380e5bd7b54SMike Christie 	struct device *dev;
1381e5bd7b54SMike Christie 
1382659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
1383e5bd7b54SMike Christie 	conn = session->leadconn;
1384e5bd7b54SMike Christie 	if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1385659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
1386e5bd7b54SMike Christie 		return;
1387e5bd7b54SMike Christie 	}
1388e5bd7b54SMike Christie 
1389e5bd7b54SMike Christie 	dev = get_device(&conn->cls_conn->dev);
1390659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
1391e5bd7b54SMike Christie 	if (!dev)
1392e5bd7b54SMike Christie 	        return;
1393e5bd7b54SMike Christie 	/*
1394e5bd7b54SMike Christie 	 * if the host is being removed bypass the connection
1395e5bd7b54SMike Christie 	 * recovery initialization because we are going to kill
1396e5bd7b54SMike Christie 	 * the session.
1397e5bd7b54SMike Christie 	 */
1398e5bd7b54SMike Christie 	if (err == ISCSI_ERR_INVALID_HOST)
1399e5bd7b54SMike Christie 		iscsi_conn_error_event(conn->cls_conn, err);
1400e5bd7b54SMike Christie 	else
1401e5bd7b54SMike Christie 		iscsi_conn_failure(conn, err);
1402e5bd7b54SMike Christie 	put_device(dev);
1403e5bd7b54SMike Christie }
1404e5bd7b54SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_failure);
1405e5bd7b54SMike Christie 
14067996a778SMike Christie void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
14077996a778SMike Christie {
14087996a778SMike Christie 	struct iscsi_session *session = conn->session;
14097996a778SMike Christie 
1410659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
1411656cffc9SMike Christie 	if (session->state == ISCSI_STATE_FAILED) {
1412659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
1413656cffc9SMike Christie 		return;
1414656cffc9SMike Christie 	}
1415656cffc9SMike Christie 
141667a61114SMike Christie 	if (conn->stop_stage == 0)
14177996a778SMike Christie 		session->state = ISCSI_STATE_FAILED;
1418659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
1419e5bd7b54SMike Christie 
14207996a778SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
14217996a778SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1422e5bd7b54SMike Christie 	iscsi_conn_error_event(conn->cls_conn, err);
14237996a778SMike Christie }
14247996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_failure);
14257996a778SMike Christie 
142677a23c21SMike Christie static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
142777a23c21SMike Christie {
142877a23c21SMike Christie 	struct iscsi_session *session = conn->session;
142977a23c21SMike Christie 
143077a23c21SMike Christie 	/*
143177a23c21SMike Christie 	 * Check for iSCSI window and take care of CmdSN wrap-around
143277a23c21SMike Christie 	 */
1433e0726407SMike Christie 	if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
14341b2c7af8SMike Christie 		ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
14351b2c7af8SMike Christie 				  "%u MaxCmdSN %u CmdSN %u/%u\n",
14361b2c7af8SMike Christie 				  session->exp_cmdsn, session->max_cmdsn,
14371b2c7af8SMike Christie 				  session->cmdsn, session->queued_cmdsn);
143877a23c21SMike Christie 		return -ENOSPC;
143977a23c21SMike Christie 	}
144077a23c21SMike Christie 	return 0;
144177a23c21SMike Christie }
144277a23c21SMike Christie 
14439c19a7d0SMike Christie static int iscsi_xmit_task(struct iscsi_conn *conn)
144477a23c21SMike Christie {
14459c19a7d0SMike Christie 	struct iscsi_task *task = conn->task;
1446843c0a8aSMike Christie 	int rc;
144777a23c21SMike Christie 
144870b31c15SMike Christie 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
144970b31c15SMike Christie 		return -ENODATA;
145070b31c15SMike Christie 
14519c19a7d0SMike Christie 	__iscsi_get_task(task);
1452659743b0SShlomo Pongratz 	spin_unlock_bh(&conn->session->frwd_lock);
14539c19a7d0SMike Christie 	rc = conn->session->tt->xmit_task(task);
1454659743b0SShlomo Pongratz 	spin_lock_bh(&conn->session->frwd_lock);
1455d355e57dSMike Christie 	if (!rc) {
14569c19a7d0SMike Christie 		/* done with this task */
1457d355e57dSMike Christie 		task->last_xfer = jiffies;
14589c19a7d0SMike Christie 		conn->task = NULL;
1459d355e57dSMike Christie 	}
1460659743b0SShlomo Pongratz 	/* regular RX path uses back_lock */
146172b97402SShlomo Pongratz 	spin_lock(&conn->session->back_lock);
1462d355e57dSMike Christie 	__iscsi_put_task(task);
146372b97402SShlomo Pongratz 	spin_unlock(&conn->session->back_lock);
146477a23c21SMike Christie 	return rc;
146577a23c21SMike Christie }
146677a23c21SMike Christie 
14677996a778SMike Christie /**
14689c19a7d0SMike Christie  * iscsi_requeue_task - requeue task to run from session workqueue
14699c19a7d0SMike Christie  * @task: task to requeue
1470843c0a8aSMike Christie  *
14719c19a7d0SMike Christie  * LLDs that need to run a task from the session workqueue should call
1472659743b0SShlomo Pongratz  * this. The session frwd_lock must be held. This should only be called
1473052d0144SMike Christie  * by software drivers.
1474843c0a8aSMike Christie  */
14759c19a7d0SMike Christie void iscsi_requeue_task(struct iscsi_task *task)
1476843c0a8aSMike Christie {
14779c19a7d0SMike Christie 	struct iscsi_conn *conn = task->conn;
1478843c0a8aSMike Christie 
14793bbaaad9SMike Christie 	/*
14803bbaaad9SMike Christie 	 * this may be on the requeue list already if the xmit_task callout
14813bbaaad9SMike Christie 	 * is handling the r2ts while we are adding new ones
14823bbaaad9SMike Christie 	 */
1483*6f8830f5SChris Leech 	spin_lock_bh(&conn->taskqueuelock);
14843bbaaad9SMike Christie 	if (list_empty(&task->running))
14853bbaaad9SMike Christie 		list_add_tail(&task->running, &conn->requeue);
1486*6f8830f5SChris Leech 	spin_unlock_bh(&conn->taskqueuelock);
148732ae763eSMike Christie 	iscsi_conn_queue_work(conn);
1488843c0a8aSMike Christie }
14899c19a7d0SMike Christie EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1490843c0a8aSMike Christie 
1491843c0a8aSMike Christie /**
14927996a778SMike Christie  * iscsi_data_xmit - xmit any command into the scheduled connection
14937996a778SMike Christie  * @conn: iscsi connection
14947996a778SMike Christie  *
14957996a778SMike Christie  * Notes:
14967996a778SMike Christie  *	The function can return -EAGAIN in which case the caller must
14977996a778SMike Christie  *	re-schedule it again later or recover. '0' return code means
14987996a778SMike Christie  *	successful xmit.
14997996a778SMike Christie  **/
15007996a778SMike Christie static int iscsi_data_xmit(struct iscsi_conn *conn)
15017996a778SMike Christie {
15025d12c05eSMike Christie 	struct iscsi_task *task;
15033219e529SMike Christie 	int rc = 0;
15047996a778SMike Christie 
1505659743b0SShlomo Pongratz 	spin_lock_bh(&conn->session->frwd_lock);
150670b31c15SMike Christie 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
15071b2c7af8SMike Christie 		ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1508659743b0SShlomo Pongratz 		spin_unlock_bh(&conn->session->frwd_lock);
15093219e529SMike Christie 		return -ENODATA;
15107996a778SMike Christie 	}
15117996a778SMike Christie 
15129c19a7d0SMike Christie 	if (conn->task) {
15139c19a7d0SMike Christie 		rc = iscsi_xmit_task(conn);
15143219e529SMike Christie 	        if (rc)
151570b31c15SMike Christie 		        goto done;
15167996a778SMike Christie 	}
151777a23c21SMike Christie 
151877a23c21SMike Christie 	/*
151977a23c21SMike Christie 	 * process mgmt pdus like nops before commands since we should
152077a23c21SMike Christie 	 * only have one nop-out as a ping from us and targets should not
152177a23c21SMike Christie 	 * overflow us with nop-ins
152277a23c21SMike Christie 	 */
1523*6f8830f5SChris Leech 	spin_lock_bh(&conn->taskqueuelock);
152477a23c21SMike Christie check_mgmt:
1525843c0a8aSMike Christie 	while (!list_empty(&conn->mgmtqueue)) {
15269c19a7d0SMike Christie 		conn->task = list_entry(conn->mgmtqueue.next,
15279c19a7d0SMike Christie 					 struct iscsi_task, running);
15283bbaaad9SMike Christie 		list_del_init(&conn->task->running);
1529*6f8830f5SChris Leech 		spin_unlock_bh(&conn->taskqueuelock);
15309c19a7d0SMike Christie 		if (iscsi_prep_mgmt_task(conn, conn->task)) {
1531659743b0SShlomo Pongratz 			/* regular RX path uses back_lock */
1532659743b0SShlomo Pongratz 			spin_lock_bh(&conn->session->back_lock);
15339c19a7d0SMike Christie 			__iscsi_put_task(conn->task);
1534659743b0SShlomo Pongratz 			spin_unlock_bh(&conn->session->back_lock);
15359c19a7d0SMike Christie 			conn->task = NULL;
1536*6f8830f5SChris Leech 			spin_lock_bh(&conn->taskqueuelock);
1537b3a7ea8dSMike Christie 			continue;
1538b3a7ea8dSMike Christie 		}
15399c19a7d0SMike Christie 		rc = iscsi_xmit_task(conn);
15403219e529SMike Christie 		if (rc)
154170b31c15SMike Christie 			goto done;
1542*6f8830f5SChris Leech 		spin_lock_bh(&conn->taskqueuelock);
15437996a778SMike Christie 	}
15447996a778SMike Christie 
1545843c0a8aSMike Christie 	/* process pending command queue */
15463bbaaad9SMike Christie 	while (!list_empty(&conn->cmdqueue)) {
15475d12c05eSMike Christie 		conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
15485d12c05eSMike Christie 					running);
15493bbaaad9SMike Christie 		list_del_init(&conn->task->running);
1550*6f8830f5SChris Leech 		spin_unlock_bh(&conn->taskqueuelock);
1551b3a7ea8dSMike Christie 		if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1552b3cd5050SMike Christie 			fail_scsi_task(conn->task, DID_IMM_RETRY);
1553*6f8830f5SChris Leech 			spin_lock_bh(&conn->taskqueuelock);
1554b3a7ea8dSMike Christie 			continue;
1555b3a7ea8dSMike Christie 		}
1556577577daSMike Christie 		rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1557577577daSMike Christie 		if (rc) {
15585d12c05eSMike Christie 			if (rc == -ENOMEM || rc == -EACCES) {
1559*6f8830f5SChris Leech 				spin_lock_bh(&conn->taskqueuelock);
15603bbaaad9SMike Christie 				list_add_tail(&conn->task->running,
15613bbaaad9SMike Christie 					      &conn->cmdqueue);
1562577577daSMike Christie 				conn->task = NULL;
1563*6f8830f5SChris Leech 				spin_unlock_bh(&conn->taskqueuelock);
156470b31c15SMike Christie 				goto done;
1565577577daSMike Christie 			} else
1566b3cd5050SMike Christie 				fail_scsi_task(conn->task, DID_ABORT);
1567*6f8830f5SChris Leech 			spin_lock_bh(&conn->taskqueuelock);
1568004d6530SBoaz Harrosh 			continue;
1569004d6530SBoaz Harrosh 		}
15709c19a7d0SMike Christie 		rc = iscsi_xmit_task(conn);
15713219e529SMike Christie 		if (rc)
157270b31c15SMike Christie 			goto done;
157377a23c21SMike Christie 		/*
15749c19a7d0SMike Christie 		 * we could continuously get new task requests so
157577a23c21SMike Christie 		 * we need to check the mgmt queue for nops that need to
157677a23c21SMike Christie 		 * be sent to aviod starvation
157777a23c21SMike Christie 		 */
1578*6f8830f5SChris Leech 		spin_lock_bh(&conn->taskqueuelock);
1579843c0a8aSMike Christie 		if (!list_empty(&conn->mgmtqueue))
1580843c0a8aSMike Christie 			goto check_mgmt;
1581843c0a8aSMike Christie 	}
1582843c0a8aSMike Christie 
1583843c0a8aSMike Christie 	while (!list_empty(&conn->requeue)) {
1584b3a7ea8dSMike Christie 		/*
1585b3a7ea8dSMike Christie 		 * we always do fastlogout - conn stop code will clean up.
1586b3a7ea8dSMike Christie 		 */
1587b3a7ea8dSMike Christie 		if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1588b3a7ea8dSMike Christie 			break;
1589b3a7ea8dSMike Christie 
15905d12c05eSMike Christie 		task = list_entry(conn->requeue.next, struct iscsi_task,
15915d12c05eSMike Christie 				  running);
15925d12c05eSMike Christie 		if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
15935d12c05eSMike Christie 			break;
15945d12c05eSMike Christie 
15955d12c05eSMike Christie 		conn->task = task;
15963bbaaad9SMike Christie 		list_del_init(&conn->task->running);
15979c19a7d0SMike Christie 		conn->task->state = ISCSI_TASK_RUNNING;
1598*6f8830f5SChris Leech 		spin_unlock_bh(&conn->taskqueuelock);
15999c19a7d0SMike Christie 		rc = iscsi_xmit_task(conn);
1600843c0a8aSMike Christie 		if (rc)
160170b31c15SMike Christie 			goto done;
1602*6f8830f5SChris Leech 		spin_lock_bh(&conn->taskqueuelock);
1603843c0a8aSMike Christie 		if (!list_empty(&conn->mgmtqueue))
160477a23c21SMike Christie 			goto check_mgmt;
16057996a778SMike Christie 	}
1606*6f8830f5SChris Leech 	spin_unlock_bh(&conn->taskqueuelock);
1607659743b0SShlomo Pongratz 	spin_unlock_bh(&conn->session->frwd_lock);
16083219e529SMike Christie 	return -ENODATA;
16097996a778SMike Christie 
161070b31c15SMike Christie done:
1611659743b0SShlomo Pongratz 	spin_unlock_bh(&conn->session->frwd_lock);
16123219e529SMike Christie 	return rc;
16137996a778SMike Christie }
16147996a778SMike Christie 
1615c4028958SDavid Howells static void iscsi_xmitworker(struct work_struct *work)
16167996a778SMike Christie {
1617c4028958SDavid Howells 	struct iscsi_conn *conn =
1618c4028958SDavid Howells 		container_of(work, struct iscsi_conn, xmitwork);
16193219e529SMike Christie 	int rc;
16207996a778SMike Christie 	/*
16217996a778SMike Christie 	 * serialize Xmit worker on a per-connection basis.
16227996a778SMike Christie 	 */
16233219e529SMike Christie 	do {
16243219e529SMike Christie 		rc = iscsi_data_xmit(conn);
16253219e529SMike Christie 	} while (rc >= 0 || rc == -EAGAIN);
16267996a778SMike Christie }
16277996a778SMike Christie 
1628577577daSMike Christie static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1629577577daSMike Christie 						  struct scsi_cmnd *sc)
1630577577daSMike Christie {
1631577577daSMike Christie 	struct iscsi_task *task;
1632577577daSMike Christie 
16337acd72ebSStefani Seibold 	if (!kfifo_out(&conn->session->cmdpool.queue,
1634577577daSMike Christie 			 (void *) &task, sizeof(void *)))
1635577577daSMike Christie 		return NULL;
1636577577daSMike Christie 
1637577577daSMike Christie 	sc->SCp.phase = conn->session->age;
1638577577daSMike Christie 	sc->SCp.ptr = (char *) task;
1639577577daSMike Christie 
1640577577daSMike Christie 	atomic_set(&task->refcount, 1);
1641577577daSMike Christie 	task->state = ISCSI_TASK_PENDING;
1642577577daSMike Christie 	task->conn = conn;
1643577577daSMike Christie 	task->sc = sc;
1644d355e57dSMike Christie 	task->have_checked_conn = false;
1645d355e57dSMike Christie 	task->last_timeout = jiffies;
1646d355e57dSMike Christie 	task->last_xfer = jiffies;
164755e51edaSSagi Grimberg 	task->protected = false;
1648577577daSMike Christie 	INIT_LIST_HEAD(&task->running);
1649577577daSMike Christie 	return task;
1650577577daSMike Christie }
1651577577daSMike Christie 
16527996a778SMike Christie enum {
16537996a778SMike Christie 	FAILURE_BAD_HOST = 1,
16547996a778SMike Christie 	FAILURE_SESSION_FAILED,
16557996a778SMike Christie 	FAILURE_SESSION_FREED,
16567996a778SMike Christie 	FAILURE_WINDOW_CLOSED,
165760ecebf5SMike Christie 	FAILURE_OOM,
16587996a778SMike Christie 	FAILURE_SESSION_TERMINATE,
1659656cffc9SMike Christie 	FAILURE_SESSION_IN_RECOVERY,
16607996a778SMike Christie 	FAILURE_SESSION_RECOVERY_TIMEOUT,
1661b3a7ea8dSMike Christie 	FAILURE_SESSION_LOGGING_OUT,
16626eabafbeSMike Christie 	FAILURE_SESSION_NOT_READY,
16637996a778SMike Christie };
16647996a778SMike Christie 
1665f41d4721SMike Christie int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
16667996a778SMike Christie {
166775613521SMike Christie 	struct iscsi_cls_session *cls_session;
16681336aed1SMike Christie 	struct iscsi_host *ihost;
16697996a778SMike Christie 	int reason = 0;
16707996a778SMike Christie 	struct iscsi_session *session;
16717996a778SMike Christie 	struct iscsi_conn *conn;
16729c19a7d0SMike Christie 	struct iscsi_task *task = NULL;
16737996a778SMike Christie 
16747996a778SMike Christie 	sc->result = 0;
1675f47f2cf5SMike Christie 	sc->SCp.ptr = NULL;
16767996a778SMike Christie 
16771336aed1SMike Christie 	ihost = shost_priv(host);
16787996a778SMike Christie 
167975613521SMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
168075613521SMike Christie 	session = cls_session->dd_data;
1681659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
16827996a778SMike Christie 
168375613521SMike Christie 	reason = iscsi_session_chkready(cls_session);
16846eabafbeSMike Christie 	if (reason) {
16856eabafbeSMike Christie 		sc->result = reason;
16866eabafbeSMike Christie 		goto fault;
16876eabafbeSMike Christie 	}
16886eabafbeSMike Christie 
1689301e0f7eSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN) {
1690656cffc9SMike Christie 		/*
1691656cffc9SMike Christie 		 * to handle the race between when we set the recovery state
1692656cffc9SMike Christie 		 * and block the session we requeue here (commands could
1693656cffc9SMike Christie 		 * be entering our queuecommand while a block is starting
1694656cffc9SMike Christie 		 * up because the block code is not locked)
1695656cffc9SMike Christie 		 */
16969000bcd6SMike Christie 		switch (session->state) {
1697301e0f7eSMike Christie 		case ISCSI_STATE_FAILED:
16989000bcd6SMike Christie 		case ISCSI_STATE_IN_RECOVERY:
1699656cffc9SMike Christie 			reason = FAILURE_SESSION_IN_RECOVERY;
1700301e0f7eSMike Christie 			sc->result = DID_IMM_RETRY << 16;
1701301e0f7eSMike Christie 			break;
17029000bcd6SMike Christie 		case ISCSI_STATE_LOGGING_OUT:
17039000bcd6SMike Christie 			reason = FAILURE_SESSION_LOGGING_OUT;
1704301e0f7eSMike Christie 			sc->result = DID_IMM_RETRY << 16;
1705301e0f7eSMike Christie 			break;
1706b3a7ea8dSMike Christie 		case ISCSI_STATE_RECOVERY_FAILED:
1707656cffc9SMike Christie 			reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
170856d7fcfaSMike Christie 			sc->result = DID_TRANSPORT_FAILFAST << 16;
1709b3a7ea8dSMike Christie 			break;
1710b3a7ea8dSMike Christie 		case ISCSI_STATE_TERMINATE:
1711656cffc9SMike Christie 			reason = FAILURE_SESSION_TERMINATE;
17126eabafbeSMike Christie 			sc->result = DID_NO_CONNECT << 16;
1713b3a7ea8dSMike Christie 			break;
1714b3a7ea8dSMike Christie 		default:
17157996a778SMike Christie 			reason = FAILURE_SESSION_FREED;
17166eabafbeSMike Christie 			sc->result = DID_NO_CONNECT << 16;
1717b3a7ea8dSMike Christie 		}
17187996a778SMike Christie 		goto fault;
17197996a778SMike Christie 	}
17207996a778SMike Christie 
17217996a778SMike Christie 	conn = session->leadconn;
172298644047SMike Christie 	if (!conn) {
172398644047SMike Christie 		reason = FAILURE_SESSION_FREED;
17246eabafbeSMike Christie 		sc->result = DID_NO_CONNECT << 16;
172598644047SMike Christie 		goto fault;
172698644047SMike Christie 	}
17277996a778SMike Christie 
1728661134adSMike Christie 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1729661134adSMike Christie 		reason = FAILURE_SESSION_IN_RECOVERY;
1730661134adSMike Christie 		sc->result = DID_REQUEUE;
1731661134adSMike Christie 		goto fault;
1732661134adSMike Christie 	}
1733661134adSMike Christie 
173477a23c21SMike Christie 	if (iscsi_check_cmdsn_window_closed(conn)) {
173577a23c21SMike Christie 		reason = FAILURE_WINDOW_CLOSED;
173677a23c21SMike Christie 		goto reject;
173777a23c21SMike Christie 	}
173877a23c21SMike Christie 
1739577577daSMike Christie 	task = iscsi_alloc_task(conn, sc);
1740577577daSMike Christie 	if (!task) {
174160ecebf5SMike Christie 		reason = FAILURE_OOM;
174260ecebf5SMike Christie 		goto reject;
174360ecebf5SMike Christie 	}
17447996a778SMike Christie 
17451336aed1SMike Christie 	if (!ihost->workq) {
1746577577daSMike Christie 		reason = iscsi_prep_scsi_cmd_pdu(task);
1747577577daSMike Christie 		if (reason) {
17485d12c05eSMike Christie 			if (reason == -ENOMEM ||  reason == -EACCES) {
1749577577daSMike Christie 				reason = FAILURE_OOM;
1750577577daSMike Christie 				goto prepd_reject;
1751577577daSMike Christie 			} else {
1752052d0144SMike Christie 				sc->result = DID_ABORT << 16;
1753577577daSMike Christie 				goto prepd_fault;
1754577577daSMike Christie 			}
1755052d0144SMike Christie 		}
17569c19a7d0SMike Christie 		if (session->tt->xmit_task(task)) {
1757d3305f34SMike Christie 			session->cmdsn--;
1758052d0144SMike Christie 			reason = FAILURE_SESSION_NOT_READY;
1759577577daSMike Christie 			goto prepd_reject;
1760052d0144SMike Christie 		}
17613bbaaad9SMike Christie 	} else {
1762*6f8830f5SChris Leech 		spin_lock_bh(&conn->taskqueuelock);
17633bbaaad9SMike Christie 		list_add_tail(&task->running, &conn->cmdqueue);
1764*6f8830f5SChris Leech 		spin_unlock_bh(&conn->taskqueuelock);
176532ae763eSMike Christie 		iscsi_conn_queue_work(conn);
17663bbaaad9SMike Christie 	}
1767052d0144SMike Christie 
1768052d0144SMike Christie 	session->queued_cmdsn++;
1769659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
17707996a778SMike Christie 	return 0;
17717996a778SMike Christie 
1772577577daSMike Christie prepd_reject:
1773f41d4721SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
17747996a778SMike Christie reject:
1775659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
17761b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
17771b2c7af8SMike Christie 			  sc->cmnd[0], reason);
1778d6d13ee1SMike Christie 	return SCSI_MLQUEUE_TARGET_BUSY;
17797996a778SMike Christie 
1780577577daSMike Christie prepd_fault:
1781f41d4721SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
17827996a778SMike Christie fault:
1783659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
17841b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
17851b2c7af8SMike Christie 			  sc->cmnd[0], reason);
1786c07d4444SBoaz Harrosh 	if (!scsi_bidi_cmnd(sc))
17871c138991SFUJITA Tomonori 		scsi_set_resid(sc, scsi_bufflen(sc));
1788c07d4444SBoaz Harrosh 	else {
1789c07d4444SBoaz Harrosh 		scsi_out(sc)->resid = scsi_out(sc)->length;
1790c07d4444SBoaz Harrosh 		scsi_in(sc)->resid = scsi_in(sc)->length;
1791c07d4444SBoaz Harrosh 	}
1792f41d4721SMike Christie 	sc->scsi_done(sc);
17937996a778SMike Christie 	return 0;
17947996a778SMike Christie }
17957996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_queuecommand);
17967996a778SMike Christie 
17976b5d6c44SMike Christie int iscsi_target_alloc(struct scsi_target *starget)
17986b5d6c44SMike Christie {
17996b5d6c44SMike Christie 	struct iscsi_cls_session *cls_session = starget_to_session(starget);
18006b5d6c44SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
18016b5d6c44SMike Christie 
18026b5d6c44SMike Christie 	starget->can_queue = session->scsi_cmds_max;
18036b5d6c44SMike Christie 	return 0;
18046b5d6c44SMike Christie }
18056b5d6c44SMike Christie EXPORT_SYMBOL_GPL(iscsi_target_alloc);
18066b5d6c44SMike Christie 
1807843c0a8aSMike Christie static void iscsi_tmf_timedout(unsigned long data)
18087996a778SMike Christie {
1809843c0a8aSMike Christie 	struct iscsi_conn *conn = (struct iscsi_conn *)data;
18107996a778SMike Christie 	struct iscsi_session *session = conn->session;
18117996a778SMike Christie 
1812659743b0SShlomo Pongratz 	spin_lock(&session->frwd_lock);
1813843c0a8aSMike Christie 	if (conn->tmf_state == TMF_QUEUED) {
1814843c0a8aSMike Christie 		conn->tmf_state = TMF_TIMEDOUT;
1815bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "tmf timedout\n");
18167996a778SMike Christie 		/* unblock eh_abort() */
18177996a778SMike Christie 		wake_up(&conn->ehwait);
18187996a778SMike Christie 	}
1819659743b0SShlomo Pongratz 	spin_unlock(&session->frwd_lock);
18207996a778SMike Christie }
18217996a778SMike Christie 
18229c19a7d0SMike Christie static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1823f6d5180cSMike Christie 				   struct iscsi_tm *hdr, int age,
1824f6d5180cSMike Christie 				   int timeout)
18257996a778SMike Christie {
18267996a778SMike Christie 	struct iscsi_session *session = conn->session;
18279c19a7d0SMike Christie 	struct iscsi_task *task;
18287996a778SMike Christie 
18299c19a7d0SMike Christie 	task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
18307996a778SMike Christie 				      NULL, 0);
18319c19a7d0SMike Christie 	if (!task) {
1832659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
1833df4da5cdSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
18347996a778SMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1835659743b0SShlomo Pongratz 		spin_lock_bh(&session->frwd_lock);
183677a23c21SMike Christie 		return -EPERM;
18377996a778SMike Christie 	}
18387996a778SMike Christie 	conn->tmfcmd_pdus_cnt++;
1839f6d5180cSMike Christie 	conn->tmf_timer.expires = timeout * HZ + jiffies;
1840843c0a8aSMike Christie 	conn->tmf_timer.function = iscsi_tmf_timedout;
1841843c0a8aSMike Christie 	conn->tmf_timer.data = (unsigned long)conn;
1842843c0a8aSMike Christie 	add_timer(&conn->tmf_timer);
1843bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "tmf set timeout\n");
1844843c0a8aSMike Christie 
1845659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
18466724add1SMike Christie 	mutex_unlock(&session->eh_mutex);
18477996a778SMike Christie 
18487996a778SMike Christie 	/*
18497996a778SMike Christie 	 * block eh thread until:
18507996a778SMike Christie 	 *
1851843c0a8aSMike Christie 	 * 1) tmf response
1852843c0a8aSMike Christie 	 * 2) tmf timeout
18537996a778SMike Christie 	 * 3) session is terminated or restarted or userspace has
18547996a778SMike Christie 	 * given up on recovery
18557996a778SMike Christie 	 */
1856843c0a8aSMike Christie 	wait_event_interruptible(conn->ehwait, age != session->age ||
18577996a778SMike Christie 				 session->state != ISCSI_STATE_LOGGED_IN ||
1858843c0a8aSMike Christie 				 conn->tmf_state != TMF_QUEUED);
18597996a778SMike Christie 	if (signal_pending(current))
18607996a778SMike Christie 		flush_signals(current);
1861843c0a8aSMike Christie 	del_timer_sync(&conn->tmf_timer);
1862843c0a8aSMike Christie 
18636724add1SMike Christie 	mutex_lock(&session->eh_mutex);
1864659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
18659c19a7d0SMike Christie 	/* if the session drops it will clean up the task */
1866843c0a8aSMike Christie 	if (age != session->age ||
1867843c0a8aSMike Christie 	    session->state != ISCSI_STATE_LOGGED_IN)
1868843c0a8aSMike Christie 		return -ENOTCONN;
18697996a778SMike Christie 	return 0;
18707996a778SMike Christie }
18717996a778SMike Christie 
18727996a778SMike Christie /*
1873843c0a8aSMike Christie  * Fail commands. session lock held and recv side suspended and xmit
1874843c0a8aSMike Christie  * thread flushed
1875843c0a8aSMike Christie  */
18769cb78c16SHannes Reinecke static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
1877843c0a8aSMike Christie {
18783bbaaad9SMike Christie 	struct iscsi_task *task;
18793bbaaad9SMike Christie 	int i;
1880843c0a8aSMike Christie 
18813bbaaad9SMike Christie 	for (i = 0; i < conn->session->cmds_max; i++) {
18823bbaaad9SMike Christie 		task = conn->session->cmds[i];
18833bbaaad9SMike Christie 		if (!task->sc || task->state == ISCSI_TASK_FREE)
18843bbaaad9SMike Christie 			continue;
1885843c0a8aSMike Christie 
18863bbaaad9SMike Christie 		if (lun != -1 && lun != task->sc->device->lun)
18873bbaaad9SMike Christie 			continue;
18883bbaaad9SMike Christie 
18891b2c7af8SMike Christie 		ISCSI_DBG_SESSION(conn->session,
18903bbaaad9SMike Christie 				  "failing sc %p itt 0x%x state %d\n",
18913bbaaad9SMike Christie 				  task->sc, task->itt, task->state);
1892b3cd5050SMike Christie 		fail_scsi_task(task, error);
1893843c0a8aSMike Christie 	}
1894843c0a8aSMike Christie }
1895843c0a8aSMike Christie 
1896661134adSMike Christie /**
1897661134adSMike Christie  * iscsi_suspend_queue - suspend iscsi_queuecommand
1898661134adSMike Christie  * @conn: iscsi conn to stop queueing IO on
1899661134adSMike Christie  *
1900659743b0SShlomo Pongratz  * This grabs the session frwd_lock to make sure no one is in
1901661134adSMike Christie  * xmit_task/queuecommand, and then sets suspend to prevent
1902661134adSMike Christie  * new commands from being queued. This only needs to be called
1903661134adSMike Christie  * by offload drivers that need to sync a path like ep disconnect
1904661134adSMike Christie  * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1905661134adSMike Christie  * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1906661134adSMike Christie  */
1907661134adSMike Christie void iscsi_suspend_queue(struct iscsi_conn *conn)
1908661134adSMike Christie {
1909659743b0SShlomo Pongratz 	spin_lock_bh(&conn->session->frwd_lock);
1910661134adSMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1911659743b0SShlomo Pongratz 	spin_unlock_bh(&conn->session->frwd_lock);
1912661134adSMike Christie }
1913661134adSMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1914661134adSMike Christie 
1915661134adSMike Christie /**
1916661134adSMike Christie  * iscsi_suspend_tx - suspend iscsi_data_xmit
1917661134adSMike Christie  * @conn: iscsi conn tp stop processing IO on.
1918661134adSMike Christie  *
1919661134adSMike Christie  * This function sets the suspend bit to prevent iscsi_data_xmit
1920661134adSMike Christie  * from sending new IO, and if work is queued on the xmit thread
1921661134adSMike Christie  * it will wait for it to be completed.
1922661134adSMike Christie  */
1923b40977d9SMike Christie void iscsi_suspend_tx(struct iscsi_conn *conn)
19246724add1SMike Christie {
192532ae763eSMike Christie 	struct Scsi_Host *shost = conn->session->host;
192632ae763eSMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
192732ae763eSMike Christie 
19286724add1SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
19291336aed1SMike Christie 	if (ihost->workq)
193032ae763eSMike Christie 		flush_workqueue(ihost->workq);
19316724add1SMike Christie }
1932b40977d9SMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
19336724add1SMike Christie 
19346724add1SMike Christie static void iscsi_start_tx(struct iscsi_conn *conn)
19356724add1SMike Christie {
19366724add1SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
193732ae763eSMike Christie 	iscsi_conn_queue_work(conn);
19386724add1SMike Christie }
19396724add1SMike Christie 
19404c48a829SMike Christie /*
19414c48a829SMike Christie  * We want to make sure a ping is in flight. It has timed out.
19424c48a829SMike Christie  * And we are not busy processing a pdu that is making
19434c48a829SMike Christie  * progress but got started before the ping and is taking a while
19444c48a829SMike Christie  * to complete so the ping is just stuck behind it in a queue.
19454c48a829SMike Christie  */
19464c48a829SMike Christie static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
19474c48a829SMike Christie {
19484c48a829SMike Christie 	if (conn->ping_task &&
19494c48a829SMike Christie 	    time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
19504c48a829SMike Christie 			   (conn->ping_timeout * HZ), jiffies))
19514c48a829SMike Christie 		return 1;
19524c48a829SMike Christie 	else
19534c48a829SMike Christie 		return 0;
19544c48a829SMike Christie }
19554c48a829SMike Christie 
1956b6a05c82SChristoph Hellwig enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1957f6d5180cSMike Christie {
1958d355e57dSMike Christie 	enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
195992ed4d69SMike Christie 	struct iscsi_task *task = NULL, *running_task;
1960f6d5180cSMike Christie 	struct iscsi_cls_session *cls_session;
1961f6d5180cSMike Christie 	struct iscsi_session *session;
1962f6d5180cSMike Christie 	struct iscsi_conn *conn;
196392ed4d69SMike Christie 	int i;
1964f6d5180cSMike Christie 
1965d355e57dSMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
196675613521SMike Christie 	session = cls_session->dd_data;
1967f6d5180cSMike Christie 
1968bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1969f6d5180cSMike Christie 
1970659743b0SShlomo Pongratz 	spin_lock(&session->frwd_lock);
1971e3d338a5SMike Christie 	task = (struct iscsi_task *)sc->SCp.ptr;
1972e3d338a5SMike Christie 	if (!task) {
1973e3d338a5SMike Christie 		/*
1974e3d338a5SMike Christie 		 * Raced with completion. Blk layer has taken ownership
1975e3d338a5SMike Christie 		 * so let timeout code complete it now.
1976e3d338a5SMike Christie 		 */
1977e3d338a5SMike Christie 		rc = BLK_EH_HANDLED;
1978e3d338a5SMike Christie 		goto done;
1979e3d338a5SMike Christie 	}
1980e3d338a5SMike Christie 
1981f6d5180cSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN) {
1982f6d5180cSMike Christie 		/*
1983f6d5180cSMike Christie 		 * We are probably in the middle of iscsi recovery so let
1984f6d5180cSMike Christie 		 * that complete and handle the error.
1985f6d5180cSMike Christie 		 */
1986242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
1987f6d5180cSMike Christie 		goto done;
1988f6d5180cSMike Christie 	}
1989f6d5180cSMike Christie 
1990f6d5180cSMike Christie 	conn = session->leadconn;
1991f6d5180cSMike Christie 	if (!conn) {
1992f6d5180cSMike Christie 		/* In the middle of shuting down */
1993242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
1994f6d5180cSMike Christie 		goto done;
1995f6d5180cSMike Christie 	}
1996f6d5180cSMike Christie 
1997d355e57dSMike Christie 	/*
1998d355e57dSMike Christie 	 * If we have sent (at least queued to the network layer) a pdu or
1999d355e57dSMike Christie 	 * recvd one for the task since the last timeout ask for
2000d355e57dSMike Christie 	 * more time. If on the next timeout we have not made progress
2001d355e57dSMike Christie 	 * we can check if it is the task or connection when we send the
2002d355e57dSMike Christie 	 * nop as a ping.
2003d355e57dSMike Christie 	 */
200492ed4d69SMike Christie 	if (time_after(task->last_xfer, task->last_timeout)) {
2005bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "Command making progress. Asking "
2006d355e57dSMike Christie 			     "scsi-ml for more time to complete. "
200792ed4d69SMike Christie 			     "Last data xfer at %lu. Last timeout was at "
2008d355e57dSMike Christie 			     "%lu\n.", task->last_xfer, task->last_timeout);
2009d355e57dSMike Christie 		task->have_checked_conn = false;
2010d355e57dSMike Christie 		rc = BLK_EH_RESET_TIMER;
2011d355e57dSMike Christie 		goto done;
2012d355e57dSMike Christie 	}
2013d355e57dSMike Christie 
2014f6d5180cSMike Christie 	if (!conn->recv_timeout && !conn->ping_timeout)
2015f6d5180cSMike Christie 		goto done;
2016f6d5180cSMike Christie 	/*
2017f6d5180cSMike Christie 	 * if the ping timedout then we are in the middle of cleaning up
2018f6d5180cSMike Christie 	 * and can let the iscsi eh handle it
2019f6d5180cSMike Christie 	 */
20204c48a829SMike Christie 	if (iscsi_has_ping_timed_out(conn)) {
2021242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
20224c48a829SMike Christie 		goto done;
20234c48a829SMike Christie 	}
2024d355e57dSMike Christie 
202592ed4d69SMike Christie 	for (i = 0; i < conn->session->cmds_max; i++) {
202692ed4d69SMike Christie 		running_task = conn->session->cmds[i];
202792ed4d69SMike Christie 		if (!running_task->sc || running_task == task ||
202892ed4d69SMike Christie 		     running_task->state != ISCSI_TASK_RUNNING)
202992ed4d69SMike Christie 			continue;
203092ed4d69SMike Christie 
203192ed4d69SMike Christie 		/*
203292ed4d69SMike Christie 		 * Only check if cmds started before this one have made
203392ed4d69SMike Christie 		 * progress, or this could never fail
203492ed4d69SMike Christie 		 */
203592ed4d69SMike Christie 		if (time_after(running_task->sc->jiffies_at_alloc,
203692ed4d69SMike Christie 			       task->sc->jiffies_at_alloc))
203792ed4d69SMike Christie 			continue;
203892ed4d69SMike Christie 
203992ed4d69SMike Christie 		if (time_after(running_task->last_xfer, task->last_timeout)) {
204092ed4d69SMike Christie 			/*
204192ed4d69SMike Christie 			 * This task has not made progress, but a task
204292ed4d69SMike Christie 			 * started before us has transferred data since
204392ed4d69SMike Christie 			 * we started/last-checked. We could be queueing
204492ed4d69SMike Christie 			 * too many tasks or the LU is bad.
204592ed4d69SMike Christie 			 *
204692ed4d69SMike Christie 			 * If the device is bad the cmds ahead of us on
204792ed4d69SMike Christie 			 * other devs will complete, and this loop will
204892ed4d69SMike Christie 			 * eventually fail starting the scsi eh.
204992ed4d69SMike Christie 			 */
205092ed4d69SMike Christie 			ISCSI_DBG_EH(session, "Command has not made progress "
205192ed4d69SMike Christie 				     "but commands ahead of it have. "
205292ed4d69SMike Christie 				     "Asking scsi-ml for more time to "
205392ed4d69SMike Christie 				     "complete. Our last xfer vs running task "
205492ed4d69SMike Christie 				     "last xfer %lu/%lu. Last check %lu.\n",
205592ed4d69SMike Christie 				     task->last_xfer, running_task->last_xfer,
205692ed4d69SMike Christie 				     task->last_timeout);
205792ed4d69SMike Christie 			rc = BLK_EH_RESET_TIMER;
205892ed4d69SMike Christie 			goto done;
205992ed4d69SMike Christie 		}
206092ed4d69SMike Christie 	}
206192ed4d69SMike Christie 
2062d355e57dSMike Christie 	/* Assumes nop timeout is shorter than scsi cmd timeout */
2063d355e57dSMike Christie 	if (task->have_checked_conn)
2064d355e57dSMike Christie 		goto done;
2065d355e57dSMike Christie 
2066f6d5180cSMike Christie 	/*
2067d355e57dSMike Christie 	 * Checking the transport already or nop from a cmd timeout still
2068d355e57dSMike Christie 	 * running
2069f6d5180cSMike Christie 	 */
2070d355e57dSMike Christie 	if (conn->ping_task) {
2071d355e57dSMike Christie 		task->have_checked_conn = true;
2072242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
20734c48a829SMike Christie 		goto done;
20744c48a829SMike Christie 	}
20754c48a829SMike Christie 
2076d355e57dSMike Christie 	/* Make sure there is a transport check done */
2077d355e57dSMike Christie 	iscsi_send_nopout(conn, NULL);
2078d355e57dSMike Christie 	task->have_checked_conn = true;
2079242f9dcbSJens Axboe 	rc = BLK_EH_RESET_TIMER;
2080d355e57dSMike Christie 
2081f6d5180cSMike Christie done:
2082d355e57dSMike Christie 	if (task)
2083d355e57dSMike Christie 		task->last_timeout = jiffies;
2084659743b0SShlomo Pongratz 	spin_unlock(&session->frwd_lock);
2085bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2086242f9dcbSJens Axboe 		     "timer reset" : "nh");
2087f6d5180cSMike Christie 	return rc;
2088f6d5180cSMike Christie }
2089b6a05c82SChristoph Hellwig EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out);
2090f6d5180cSMike Christie 
2091f6d5180cSMike Christie static void iscsi_check_transport_timeouts(unsigned long data)
2092f6d5180cSMike Christie {
2093f6d5180cSMike Christie 	struct iscsi_conn *conn = (struct iscsi_conn *)data;
2094f6d5180cSMike Christie 	struct iscsi_session *session = conn->session;
20954cf10435SMike Christie 	unsigned long recv_timeout, next_timeout = 0, last_recv;
2096f6d5180cSMike Christie 
2097659743b0SShlomo Pongratz 	spin_lock(&session->frwd_lock);
2098f6d5180cSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN)
2099f6d5180cSMike Christie 		goto done;
2100f6d5180cSMike Christie 
21014cf10435SMike Christie 	recv_timeout = conn->recv_timeout;
21024cf10435SMike Christie 	if (!recv_timeout)
2103f6d5180cSMike Christie 		goto done;
2104f6d5180cSMike Christie 
21054cf10435SMike Christie 	recv_timeout *= HZ;
2106f6d5180cSMike Christie 	last_recv = conn->last_recv;
21074c48a829SMike Christie 
21084c48a829SMike Christie 	if (iscsi_has_ping_timed_out(conn)) {
2109322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
21104c48a829SMike Christie 				  "expired, recv timeout %d, last rx %lu, "
21114c48a829SMike Christie 				  "last ping %lu, now %lu\n",
21124c48a829SMike Christie 				  conn->ping_timeout, conn->recv_timeout,
21134c48a829SMike Christie 				  last_recv, conn->last_ping, jiffies);
2114659743b0SShlomo Pongratz 		spin_unlock(&session->frwd_lock);
211509ff742cSMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
2116f6d5180cSMike Christie 		return;
2117f6d5180cSMike Christie 	}
2118f6d5180cSMike Christie 
21194cf10435SMike Christie 	if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2120f6d5180cSMike Christie 		/* send a ping to try to provoke some traffic */
21211b2c7af8SMike Christie 		ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
212252f5664aSAriel Nahum 		if (iscsi_send_nopout(conn, NULL))
212352f5664aSAriel Nahum 			next_timeout = jiffies + (1 * HZ);
212452f5664aSAriel Nahum 		else
21254cf10435SMike Christie 			next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2126ad294e9cSMike Christie 	} else
21274cf10435SMike Christie 		next_timeout = last_recv + recv_timeout;
2128f6d5180cSMike Christie 
21291b2c7af8SMike Christie 	ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2130f6d5180cSMike Christie 	mod_timer(&conn->transport_timer, next_timeout);
2131f6d5180cSMike Christie done:
2132659743b0SShlomo Pongratz 	spin_unlock(&session->frwd_lock);
2133f6d5180cSMike Christie }
2134f6d5180cSMike Christie 
21359c19a7d0SMike Christie static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2136843c0a8aSMike Christie 				      struct iscsi_tm *hdr)
2137843c0a8aSMike Christie {
2138843c0a8aSMike Christie 	memset(hdr, 0, sizeof(*hdr));
2139843c0a8aSMike Christie 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2140843c0a8aSMike Christie 	hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2141843c0a8aSMike Christie 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
214255bdabdfSAndy Grover 	hdr->lun = task->lun;
2143577577daSMike Christie 	hdr->rtt = task->hdr_itt;
2144577577daSMike Christie 	hdr->refcmdsn = task->cmdsn;
2145843c0a8aSMike Christie }
2146843c0a8aSMike Christie 
21477996a778SMike Christie int iscsi_eh_abort(struct scsi_cmnd *sc)
21487996a778SMike Christie {
214975613521SMike Christie 	struct iscsi_cls_session *cls_session;
215075613521SMike Christie 	struct iscsi_session *session;
2151f47f2cf5SMike Christie 	struct iscsi_conn *conn;
21529c19a7d0SMike Christie 	struct iscsi_task *task;
2153843c0a8aSMike Christie 	struct iscsi_tm *hdr;
2154e9e410e8SBart Van Assche 	int age;
21557996a778SMike Christie 
215675613521SMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
215775613521SMike Christie 	session = cls_session->dd_data;
215875613521SMike Christie 
2159bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
21604421c9ebSMike Christie 
21616724add1SMike Christie 	mutex_lock(&session->eh_mutex);
2162659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
2163f47f2cf5SMike Christie 	/*
2164f47f2cf5SMike Christie 	 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2165f47f2cf5SMike Christie 	 * got the command.
2166f47f2cf5SMike Christie 	 */
2167f47f2cf5SMike Christie 	if (!sc->SCp.ptr) {
2168bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
21691b2c7af8SMike Christie 				      "it completed.\n");
2170659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
21716724add1SMike Christie 		mutex_unlock(&session->eh_mutex);
2172f47f2cf5SMike Christie 		return SUCCESS;
2173f47f2cf5SMike Christie 	}
2174f47f2cf5SMike Christie 
21757996a778SMike Christie 	/*
21767996a778SMike Christie 	 * If we are not logged in or we have started a new session
21777996a778SMike Christie 	 * then let the host reset code handle this
21787996a778SMike Christie 	 */
2179843c0a8aSMike Christie 	if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2180843c0a8aSMike Christie 	    sc->SCp.phase != session->age) {
2181659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
2182843c0a8aSMike Christie 		mutex_unlock(&session->eh_mutex);
2183bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "failing abort due to dropped "
21844421c9ebSMike Christie 				  "session.\n");
2185843c0a8aSMike Christie 		return FAILED;
2186843c0a8aSMike Christie 	}
2187843c0a8aSMike Christie 
2188843c0a8aSMike Christie 	conn = session->leadconn;
2189843c0a8aSMike Christie 	conn->eh_abort_cnt++;
2190843c0a8aSMike Christie 	age = session->age;
2191843c0a8aSMike Christie 
21929c19a7d0SMike Christie 	task = (struct iscsi_task *)sc->SCp.ptr;
2193bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
21941b2c7af8SMike Christie 		     sc, task->itt);
21957996a778SMike Christie 
21969c19a7d0SMike Christie 	/* task completed before time out */
21979c19a7d0SMike Christie 	if (!task->sc) {
2198bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
219977a23c21SMike Christie 		goto success;
22007ea8b828SMike Christie 	}
22017996a778SMike Christie 
22029c19a7d0SMike Christie 	if (task->state == ISCSI_TASK_PENDING) {
2203b3cd5050SMike Christie 		fail_scsi_task(task, DID_ABORT);
220477a23c21SMike Christie 		goto success;
220577a23c21SMike Christie 	}
22067996a778SMike Christie 
2207843c0a8aSMike Christie 	/* only have one tmf outstanding at a time */
2208843c0a8aSMike Christie 	if (conn->tmf_state != TMF_INITIAL)
22097996a778SMike Christie 		goto failed;
2210843c0a8aSMike Christie 	conn->tmf_state = TMF_QUEUED;
22117996a778SMike Christie 
2212843c0a8aSMike Christie 	hdr = &conn->tmhdr;
22139c19a7d0SMike Christie 	iscsi_prep_abort_task_pdu(task, hdr);
2214843c0a8aSMike Christie 
2215e9e410e8SBart Van Assche 	if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout))
2216843c0a8aSMike Christie 		goto failed;
2217843c0a8aSMike Christie 
2218843c0a8aSMike Christie 	switch (conn->tmf_state) {
2219843c0a8aSMike Christie 	case TMF_SUCCESS:
2220659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
2221913e5bf4SMike Christie 		/*
2222913e5bf4SMike Christie 		 * stop tx side incase the target had sent a abort rsp but
2223913e5bf4SMike Christie 		 * the initiator was still writing out data.
2224913e5bf4SMike Christie 		 */
22256724add1SMike Christie 		iscsi_suspend_tx(conn);
22267996a778SMike Christie 		/*
2227913e5bf4SMike Christie 		 * we do not stop the recv side because targets have been
2228913e5bf4SMike Christie 		 * good and have never sent us a successful tmf response
2229913e5bf4SMike Christie 		 * then sent more data for the cmd.
22307996a778SMike Christie 		 */
2231659743b0SShlomo Pongratz 		spin_lock_bh(&session->frwd_lock);
2232b3cd5050SMike Christie 		fail_scsi_task(task, DID_ABORT);
2233843c0a8aSMike Christie 		conn->tmf_state = TMF_INITIAL;
22345d12c05eSMike Christie 		memset(hdr, 0, sizeof(*hdr));
2235659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
22366724add1SMike Christie 		iscsi_start_tx(conn);
223777a23c21SMike Christie 		goto success_unlocked;
2238843c0a8aSMike Christie 	case TMF_TIMEDOUT:
2239659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
2240df4da5cdSMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2241843c0a8aSMike Christie 		goto failed_unlocked;
2242843c0a8aSMike Christie 	case TMF_NOT_FOUND:
2243843c0a8aSMike Christie 		if (!sc->SCp.ptr) {
2244843c0a8aSMike Christie 			conn->tmf_state = TMF_INITIAL;
22455d12c05eSMike Christie 			memset(hdr, 0, sizeof(*hdr));
22469c19a7d0SMike Christie 			/* task completed before tmf abort response */
2247bd2199d4SErez Zilber 			ISCSI_DBG_EH(session, "sc completed while abort	in "
2248bd2199d4SErez Zilber 					      "progress\n");
224977a23c21SMike Christie 			goto success;
225077a23c21SMike Christie 		}
225177a23c21SMike Christie 		/* fall through */
225277a23c21SMike Christie 	default:
2253843c0a8aSMike Christie 		conn->tmf_state = TMF_INITIAL;
2254843c0a8aSMike Christie 		goto failed;
225577a23c21SMike Christie 	}
22567996a778SMike Christie 
225777a23c21SMike Christie success:
2258659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
225977a23c21SMike Christie success_unlocked:
2260bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
22611b2c7af8SMike Christie 		     sc, task->itt);
22626724add1SMike Christie 	mutex_unlock(&session->eh_mutex);
22637996a778SMike Christie 	return SUCCESS;
22647996a778SMike Christie 
22657996a778SMike Christie failed:
2266659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
226777a23c21SMike Christie failed_unlocked:
2268bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
22699c19a7d0SMike Christie 		     task ? task->itt : 0);
22706724add1SMike Christie 	mutex_unlock(&session->eh_mutex);
22717996a778SMike Christie 	return FAILED;
22727996a778SMike Christie }
22737996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_abort);
22747996a778SMike Christie 
2275843c0a8aSMike Christie static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2276843c0a8aSMike Christie {
2277843c0a8aSMike Christie 	memset(hdr, 0, sizeof(*hdr));
2278843c0a8aSMike Christie 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2279843c0a8aSMike Christie 	hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2280843c0a8aSMike Christie 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
228155bdabdfSAndy Grover 	int_to_scsilun(sc->device->lun, &hdr->lun);
2282f6d5180cSMike Christie 	hdr->rtt = RESERVED_ITT;
2283843c0a8aSMike Christie }
2284843c0a8aSMike Christie 
2285843c0a8aSMike Christie int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2286843c0a8aSMike Christie {
228775613521SMike Christie 	struct iscsi_cls_session *cls_session;
228875613521SMike Christie 	struct iscsi_session *session;
2289843c0a8aSMike Christie 	struct iscsi_conn *conn;
2290843c0a8aSMike Christie 	struct iscsi_tm *hdr;
2291843c0a8aSMike Christie 	int rc = FAILED;
2292843c0a8aSMike Christie 
229375613521SMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
229475613521SMike Christie 	session = cls_session->dd_data;
229575613521SMike Christie 
22969cb78c16SHannes Reinecke 	ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc,
22979cb78c16SHannes Reinecke 		     sc->device->lun);
2298843c0a8aSMike Christie 
2299843c0a8aSMike Christie 	mutex_lock(&session->eh_mutex);
2300659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
2301843c0a8aSMike Christie 	/*
2302843c0a8aSMike Christie 	 * Just check if we are not logged in. We cannot check for
2303843c0a8aSMike Christie 	 * the phase because the reset could come from a ioctl.
2304843c0a8aSMike Christie 	 */
2305843c0a8aSMike Christie 	if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2306843c0a8aSMike Christie 		goto unlock;
2307843c0a8aSMike Christie 	conn = session->leadconn;
2308843c0a8aSMike Christie 
2309843c0a8aSMike Christie 	/* only have one tmf outstanding at a time */
2310843c0a8aSMike Christie 	if (conn->tmf_state != TMF_INITIAL)
2311843c0a8aSMike Christie 		goto unlock;
2312843c0a8aSMike Christie 	conn->tmf_state = TMF_QUEUED;
2313843c0a8aSMike Christie 
2314843c0a8aSMike Christie 	hdr = &conn->tmhdr;
2315843c0a8aSMike Christie 	iscsi_prep_lun_reset_pdu(sc, hdr);
2316843c0a8aSMike Christie 
23179c19a7d0SMike Christie 	if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2318f6d5180cSMike Christie 				    session->lu_reset_timeout)) {
2319843c0a8aSMike Christie 		rc = FAILED;
2320843c0a8aSMike Christie 		goto unlock;
2321843c0a8aSMike Christie 	}
2322843c0a8aSMike Christie 
2323843c0a8aSMike Christie 	switch (conn->tmf_state) {
2324843c0a8aSMike Christie 	case TMF_SUCCESS:
2325843c0a8aSMike Christie 		break;
2326843c0a8aSMike Christie 	case TMF_TIMEDOUT:
2327659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
2328df4da5cdSMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2329843c0a8aSMike Christie 		goto done;
2330843c0a8aSMike Christie 	default:
2331843c0a8aSMike Christie 		conn->tmf_state = TMF_INITIAL;
2332843c0a8aSMike Christie 		goto unlock;
2333843c0a8aSMike Christie 	}
2334843c0a8aSMike Christie 
2335843c0a8aSMike Christie 	rc = SUCCESS;
2336659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
2337843c0a8aSMike Christie 
2338843c0a8aSMike Christie 	iscsi_suspend_tx(conn);
2339913e5bf4SMike Christie 
2340659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
23415d12c05eSMike Christie 	memset(hdr, 0, sizeof(*hdr));
23423bbaaad9SMike Christie 	fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2343843c0a8aSMike Christie 	conn->tmf_state = TMF_INITIAL;
2344659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
2345843c0a8aSMike Christie 
2346843c0a8aSMike Christie 	iscsi_start_tx(conn);
2347843c0a8aSMike Christie 	goto done;
2348843c0a8aSMike Christie 
2349843c0a8aSMike Christie unlock:
2350659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
2351843c0a8aSMike Christie done:
2352bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "dev reset result = %s\n",
2353843c0a8aSMike Christie 		     rc == SUCCESS ? "SUCCESS" : "FAILED");
2354843c0a8aSMike Christie 	mutex_unlock(&session->eh_mutex);
2355843c0a8aSMike Christie 	return rc;
2356843c0a8aSMike Christie }
2357843c0a8aSMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2358843c0a8aSMike Christie 
23593fe5ae8bSMike Christie void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
23603fe5ae8bSMike Christie {
23613fe5ae8bSMike Christie 	struct iscsi_session *session = cls_session->dd_data;
23623fe5ae8bSMike Christie 
2363659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
23643fe5ae8bSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN) {
23653fe5ae8bSMike Christie 		session->state = ISCSI_STATE_RECOVERY_FAILED;
23663fe5ae8bSMike Christie 		if (session->leadconn)
23673fe5ae8bSMike Christie 			wake_up(&session->leadconn->ehwait);
23683fe5ae8bSMike Christie 	}
2369659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
23703fe5ae8bSMike Christie }
23713fe5ae8bSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
23723fe5ae8bSMike Christie 
23733fe5ae8bSMike Christie /**
23743fe5ae8bSMike Christie  * iscsi_eh_session_reset - drop session and attempt relogin
23753fe5ae8bSMike Christie  * @sc: scsi command
23763fe5ae8bSMike Christie  *
23773fe5ae8bSMike Christie  * This function will wait for a relogin, session termination from
23783fe5ae8bSMike Christie  * userspace, or a recovery/replacement timeout.
23793fe5ae8bSMike Christie  */
2380309ce156SJayamohan Kallickal int iscsi_eh_session_reset(struct scsi_cmnd *sc)
23813fe5ae8bSMike Christie {
23823fe5ae8bSMike Christie 	struct iscsi_cls_session *cls_session;
23833fe5ae8bSMike Christie 	struct iscsi_session *session;
23843fe5ae8bSMike Christie 	struct iscsi_conn *conn;
23853fe5ae8bSMike Christie 
23863fe5ae8bSMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
23873fe5ae8bSMike Christie 	session = cls_session->dd_data;
23883fe5ae8bSMike Christie 	conn = session->leadconn;
23893fe5ae8bSMike Christie 
23903fe5ae8bSMike Christie 	mutex_lock(&session->eh_mutex);
2391659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
23923fe5ae8bSMike Christie 	if (session->state == ISCSI_STATE_TERMINATE) {
23933fe5ae8bSMike Christie failed:
23943fe5ae8bSMike Christie 		ISCSI_DBG_EH(session,
23953fe5ae8bSMike Christie 			     "failing session reset: Could not log back into "
23963fe5ae8bSMike Christie 			     "%s, %s [age %d]\n", session->targetname,
23973fe5ae8bSMike Christie 			     conn->persistent_address, session->age);
2398659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
23993fe5ae8bSMike Christie 		mutex_unlock(&session->eh_mutex);
24003fe5ae8bSMike Christie 		return FAILED;
24013fe5ae8bSMike Christie 	}
24023fe5ae8bSMike Christie 
2403659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
24043fe5ae8bSMike Christie 	mutex_unlock(&session->eh_mutex);
24053fe5ae8bSMike Christie 	/*
24063fe5ae8bSMike Christie 	 * we drop the lock here but the leadconn cannot be destoyed while
24073fe5ae8bSMike Christie 	 * we are in the scsi eh
24083fe5ae8bSMike Christie 	 */
2409df4da5cdSMike Christie 	iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
24103fe5ae8bSMike Christie 
24113fe5ae8bSMike Christie 	ISCSI_DBG_EH(session, "wait for relogin\n");
24123fe5ae8bSMike Christie 	wait_event_interruptible(conn->ehwait,
24133fe5ae8bSMike Christie 				 session->state == ISCSI_STATE_TERMINATE ||
24143fe5ae8bSMike Christie 				 session->state == ISCSI_STATE_LOGGED_IN ||
24153fe5ae8bSMike Christie 				 session->state == ISCSI_STATE_RECOVERY_FAILED);
24163fe5ae8bSMike Christie 	if (signal_pending(current))
24173fe5ae8bSMike Christie 		flush_signals(current);
24183fe5ae8bSMike Christie 
24193fe5ae8bSMike Christie 	mutex_lock(&session->eh_mutex);
2420659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
24213fe5ae8bSMike Christie 	if (session->state == ISCSI_STATE_LOGGED_IN) {
24223fe5ae8bSMike Christie 		ISCSI_DBG_EH(session,
24233fe5ae8bSMike Christie 			     "session reset succeeded for %s,%s\n",
24243fe5ae8bSMike Christie 			     session->targetname, conn->persistent_address);
24253fe5ae8bSMike Christie 	} else
24263fe5ae8bSMike Christie 		goto failed;
2427659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
24283fe5ae8bSMike Christie 	mutex_unlock(&session->eh_mutex);
24293fe5ae8bSMike Christie 	return SUCCESS;
24303fe5ae8bSMike Christie }
2431309ce156SJayamohan Kallickal EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
24323fe5ae8bSMike Christie 
24333fe5ae8bSMike Christie static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
24343fe5ae8bSMike Christie {
24353fe5ae8bSMike Christie 	memset(hdr, 0, sizeof(*hdr));
24363fe5ae8bSMike Christie 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
24373fe5ae8bSMike Christie 	hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
24383fe5ae8bSMike Christie 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
24393fe5ae8bSMike Christie 	hdr->rtt = RESERVED_ITT;
24403fe5ae8bSMike Christie }
24413fe5ae8bSMike Christie 
24423fe5ae8bSMike Christie /**
24433fe5ae8bSMike Christie  * iscsi_eh_target_reset - reset target
24443fe5ae8bSMike Christie  * @sc: scsi command
24453fe5ae8bSMike Christie  *
2446309ce156SJayamohan Kallickal  * This will attempt to send a warm target reset.
24473fe5ae8bSMike Christie  */
24483907adf6SBart Van Assche static int iscsi_eh_target_reset(struct scsi_cmnd *sc)
24493fe5ae8bSMike Christie {
24503fe5ae8bSMike Christie 	struct iscsi_cls_session *cls_session;
24513fe5ae8bSMike Christie 	struct iscsi_session *session;
24523fe5ae8bSMike Christie 	struct iscsi_conn *conn;
24533fe5ae8bSMike Christie 	struct iscsi_tm *hdr;
24543fe5ae8bSMike Christie 	int rc = FAILED;
24553fe5ae8bSMike Christie 
24563fe5ae8bSMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
24573fe5ae8bSMike Christie 	session = cls_session->dd_data;
24583fe5ae8bSMike Christie 
24593fe5ae8bSMike Christie 	ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
24603fe5ae8bSMike Christie 		     session->targetname);
24613fe5ae8bSMike Christie 
24623fe5ae8bSMike Christie 	mutex_lock(&session->eh_mutex);
2463659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
24643fe5ae8bSMike Christie 	/*
24653fe5ae8bSMike Christie 	 * Just check if we are not logged in. We cannot check for
24663fe5ae8bSMike Christie 	 * the phase because the reset could come from a ioctl.
24673fe5ae8bSMike Christie 	 */
24683fe5ae8bSMike Christie 	if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
24693fe5ae8bSMike Christie 		goto unlock;
24703fe5ae8bSMike Christie 	conn = session->leadconn;
24713fe5ae8bSMike Christie 
24723fe5ae8bSMike Christie 	/* only have one tmf outstanding at a time */
24733fe5ae8bSMike Christie 	if (conn->tmf_state != TMF_INITIAL)
24743fe5ae8bSMike Christie 		goto unlock;
24753fe5ae8bSMike Christie 	conn->tmf_state = TMF_QUEUED;
24763fe5ae8bSMike Christie 
24773fe5ae8bSMike Christie 	hdr = &conn->tmhdr;
24783fe5ae8bSMike Christie 	iscsi_prep_tgt_reset_pdu(sc, hdr);
24793fe5ae8bSMike Christie 
24803fe5ae8bSMike Christie 	if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
24813fe5ae8bSMike Christie 				    session->tgt_reset_timeout)) {
24823fe5ae8bSMike Christie 		rc = FAILED;
24833fe5ae8bSMike Christie 		goto unlock;
24843fe5ae8bSMike Christie 	}
24853fe5ae8bSMike Christie 
24863fe5ae8bSMike Christie 	switch (conn->tmf_state) {
24873fe5ae8bSMike Christie 	case TMF_SUCCESS:
24883fe5ae8bSMike Christie 		break;
24893fe5ae8bSMike Christie 	case TMF_TIMEDOUT:
2490659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
2491df4da5cdSMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
24923fe5ae8bSMike Christie 		goto done;
24933fe5ae8bSMike Christie 	default:
24943fe5ae8bSMike Christie 		conn->tmf_state = TMF_INITIAL;
24953fe5ae8bSMike Christie 		goto unlock;
24963fe5ae8bSMike Christie 	}
24973fe5ae8bSMike Christie 
24983fe5ae8bSMike Christie 	rc = SUCCESS;
2499659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
25003fe5ae8bSMike Christie 
25013fe5ae8bSMike Christie 	iscsi_suspend_tx(conn);
25023fe5ae8bSMike Christie 
2503659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
25043fe5ae8bSMike Christie 	memset(hdr, 0, sizeof(*hdr));
25053fe5ae8bSMike Christie 	fail_scsi_tasks(conn, -1, DID_ERROR);
25063fe5ae8bSMike Christie 	conn->tmf_state = TMF_INITIAL;
2507659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
25083fe5ae8bSMike Christie 
25093fe5ae8bSMike Christie 	iscsi_start_tx(conn);
25103fe5ae8bSMike Christie 	goto done;
25113fe5ae8bSMike Christie 
25123fe5ae8bSMike Christie unlock:
2513659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
25143fe5ae8bSMike Christie done:
25153fe5ae8bSMike Christie 	ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
25163fe5ae8bSMike Christie 		     rc == SUCCESS ? "SUCCESS" : "FAILED");
25173fe5ae8bSMike Christie 	mutex_unlock(&session->eh_mutex);
2518309ce156SJayamohan Kallickal 	return rc;
2519309ce156SJayamohan Kallickal }
25203fe5ae8bSMike Christie 
2521309ce156SJayamohan Kallickal /**
2522309ce156SJayamohan Kallickal  * iscsi_eh_recover_target - reset target and possibly the session
2523309ce156SJayamohan Kallickal  * @sc: scsi command
2524309ce156SJayamohan Kallickal  *
2525309ce156SJayamohan Kallickal  * This will attempt to send a warm target reset. If that fails,
2526309ce156SJayamohan Kallickal  * we will escalate to ERL0 session recovery.
2527309ce156SJayamohan Kallickal  */
2528309ce156SJayamohan Kallickal int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2529309ce156SJayamohan Kallickal {
2530309ce156SJayamohan Kallickal 	int rc;
2531309ce156SJayamohan Kallickal 
2532309ce156SJayamohan Kallickal 	rc = iscsi_eh_target_reset(sc);
25333fe5ae8bSMike Christie 	if (rc == FAILED)
25343fe5ae8bSMike Christie 		rc = iscsi_eh_session_reset(sc);
25353fe5ae8bSMike Christie 	return rc;
25363fe5ae8bSMike Christie }
2537309ce156SJayamohan Kallickal EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
25383fe5ae8bSMike Christie 
25396320377fSOlaf Kirch /*
25406320377fSOlaf Kirch  * Pre-allocate a pool of @max items of @item_size. By default, the pool
25416320377fSOlaf Kirch  * should be accessed via kfifo_{get,put} on q->queue.
25426320377fSOlaf Kirch  * Optionally, the caller can obtain the array of object pointers
25436320377fSOlaf Kirch  * by passing in a non-NULL @items pointer
25446320377fSOlaf Kirch  */
25457996a778SMike Christie int
25466320377fSOlaf Kirch iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
25477996a778SMike Christie {
25486320377fSOlaf Kirch 	int i, num_arrays = 1;
25497996a778SMike Christie 
25506320377fSOlaf Kirch 	memset(q, 0, sizeof(*q));
25517996a778SMike Christie 
25527996a778SMike Christie 	q->max = max;
25536320377fSOlaf Kirch 
25546320377fSOlaf Kirch 	/* If the user passed an items pointer, he wants a copy of
25556320377fSOlaf Kirch 	 * the array. */
25566320377fSOlaf Kirch 	if (items)
25576320377fSOlaf Kirch 		num_arrays++;
25586320377fSOlaf Kirch 	q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
25596320377fSOlaf Kirch 	if (q->pool == NULL)
2560f474a37bSJean Delvare 		return -ENOMEM;
25617996a778SMike Christie 
2562c1e13f25SStefani Seibold 	kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
25637996a778SMike Christie 
25647996a778SMike Christie 	for (i = 0; i < max; i++) {
25656320377fSOlaf Kirch 		q->pool[i] = kzalloc(item_size, GFP_KERNEL);
25667996a778SMike Christie 		if (q->pool[i] == NULL) {
25676320377fSOlaf Kirch 			q->max = i;
25686320377fSOlaf Kirch 			goto enomem;
25697996a778SMike Christie 		}
25707acd72ebSStefani Seibold 		kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
25717996a778SMike Christie 	}
25726320377fSOlaf Kirch 
25736320377fSOlaf Kirch 	if (items) {
25746320377fSOlaf Kirch 		*items = q->pool + max;
25756320377fSOlaf Kirch 		memcpy(*items, q->pool, max * sizeof(void *));
25766320377fSOlaf Kirch 	}
25776320377fSOlaf Kirch 
25787996a778SMike Christie 	return 0;
25796320377fSOlaf Kirch 
25806320377fSOlaf Kirch enomem:
25816320377fSOlaf Kirch 	iscsi_pool_free(q);
25826320377fSOlaf Kirch 	return -ENOMEM;
25837996a778SMike Christie }
25847996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_init);
25857996a778SMike Christie 
25866320377fSOlaf Kirch void iscsi_pool_free(struct iscsi_pool *q)
25877996a778SMike Christie {
25887996a778SMike Christie 	int i;
25897996a778SMike Christie 
25907996a778SMike Christie 	for (i = 0; i < q->max; i++)
25916320377fSOlaf Kirch 		kfree(q->pool[i]);
25927996a778SMike Christie 	kfree(q->pool);
25937996a778SMike Christie }
25947996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_free);
25957996a778SMike Christie 
2596a4804cd6SMike Christie /**
2597a4804cd6SMike Christie  * iscsi_host_add - add host to system
2598a4804cd6SMike Christie  * @shost: scsi host
2599a4804cd6SMike Christie  * @pdev: parent device
2600a4804cd6SMike Christie  *
2601a4804cd6SMike Christie  * This should be called by partial offload and software iscsi drivers
2602a4804cd6SMike Christie  * to add a host to the system.
2603a4804cd6SMike Christie  */
2604a4804cd6SMike Christie int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
26057996a778SMike Christie {
26068e9a20ceSMike Christie 	if (!shost->can_queue)
26078e9a20ceSMike Christie 		shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
26088e9a20ceSMike Christie 
26094d108350SMike Christie 	if (!shost->cmd_per_lun)
26104d108350SMike Christie 		shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
26114d108350SMike Christie 
2612a4804cd6SMike Christie 	return scsi_add_host(shost, pdev);
2613a4804cd6SMike Christie }
2614a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_add);
2615a4804cd6SMike Christie 
2616a4804cd6SMike Christie /**
2617a4804cd6SMike Christie  * iscsi_host_alloc - allocate a host and driver data
2618a4804cd6SMike Christie  * @sht: scsi host template
2619a4804cd6SMike Christie  * @dd_data_size: driver host data size
262032ae763eSMike Christie  * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2621a4804cd6SMike Christie  *
2622a4804cd6SMike Christie  * This should be called by partial offload and software iscsi drivers.
2623a4804cd6SMike Christie  * To access the driver specific memory use the iscsi_host_priv() macro.
2624a4804cd6SMike Christie  */
2625a4804cd6SMike Christie struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
26264d108350SMike Christie 				   int dd_data_size, bool xmit_can_sleep)
2627a4804cd6SMike Christie {
2628a4804cd6SMike Christie 	struct Scsi_Host *shost;
2629e5bd7b54SMike Christie 	struct iscsi_host *ihost;
2630a4804cd6SMike Christie 
2631a4804cd6SMike Christie 	shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2632a4804cd6SMike Christie 	if (!shost)
2633a4804cd6SMike Christie 		return NULL;
2634e5bd7b54SMike Christie 	ihost = shost_priv(shost);
263532ae763eSMike Christie 
263632ae763eSMike Christie 	if (xmit_can_sleep) {
263732ae763eSMike Christie 		snprintf(ihost->workq_name, sizeof(ihost->workq_name),
263832ae763eSMike Christie 			"iscsi_q_%d", shost->host_no);
263932ae763eSMike Christie 		ihost->workq = create_singlethread_workqueue(ihost->workq_name);
264032ae763eSMike Christie 		if (!ihost->workq)
264132ae763eSMike Christie 			goto free_host;
264232ae763eSMike Christie 	}
264332ae763eSMike Christie 
2644e5bd7b54SMike Christie 	spin_lock_init(&ihost->lock);
2645e5bd7b54SMike Christie 	ihost->state = ISCSI_HOST_SETUP;
2646e5bd7b54SMike Christie 	ihost->num_sessions = 0;
2647e5bd7b54SMike Christie 	init_waitqueue_head(&ihost->session_removal_wq);
2648a4804cd6SMike Christie 	return shost;
264932ae763eSMike Christie 
265032ae763eSMike Christie free_host:
265132ae763eSMike Christie 	scsi_host_put(shost);
265232ae763eSMike Christie 	return NULL;
265375613521SMike Christie }
2654a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_alloc);
265575613521SMike Christie 
2656e5bd7b54SMike Christie static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2657e5bd7b54SMike Christie {
265840a06e75SMike Christie 	iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2659e5bd7b54SMike Christie }
2660e5bd7b54SMike Christie 
2661a4804cd6SMike Christie /**
2662a4804cd6SMike Christie  * iscsi_host_remove - remove host and sessions
2663a4804cd6SMike Christie  * @shost: scsi host
2664a4804cd6SMike Christie  *
2665e5bd7b54SMike Christie  * If there are any sessions left, this will initiate the removal and wait
2666e5bd7b54SMike Christie  * for the completion.
2667a4804cd6SMike Christie  */
2668a4804cd6SMike Christie void iscsi_host_remove(struct Scsi_Host *shost)
2669a4804cd6SMike Christie {
2670e5bd7b54SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
2671e5bd7b54SMike Christie 	unsigned long flags;
2672e5bd7b54SMike Christie 
2673e5bd7b54SMike Christie 	spin_lock_irqsave(&ihost->lock, flags);
2674e5bd7b54SMike Christie 	ihost->state = ISCSI_HOST_REMOVED;
2675e5bd7b54SMike Christie 	spin_unlock_irqrestore(&ihost->lock, flags);
2676e5bd7b54SMike Christie 
2677e5bd7b54SMike Christie 	iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2678e5bd7b54SMike Christie 	wait_event_interruptible(ihost->session_removal_wq,
2679e5bd7b54SMike Christie 				 ihost->num_sessions == 0);
2680e5bd7b54SMike Christie 	if (signal_pending(current))
2681e5bd7b54SMike Christie 		flush_signals(current);
2682e5bd7b54SMike Christie 
2683a4804cd6SMike Christie 	scsi_remove_host(shost);
268432ae763eSMike Christie 	if (ihost->workq)
268532ae763eSMike Christie 		destroy_workqueue(ihost->workq);
2686a4804cd6SMike Christie }
2687a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_remove);
2688a4804cd6SMike Christie 
2689a4804cd6SMike Christie void iscsi_host_free(struct Scsi_Host *shost)
269075613521SMike Christie {
269175613521SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
269275613521SMike Christie 
269375613521SMike Christie 	kfree(ihost->netdev);
269475613521SMike Christie 	kfree(ihost->hwaddress);
269575613521SMike Christie 	kfree(ihost->initiatorname);
2696a4804cd6SMike Christie 	scsi_host_put(shost);
269775613521SMike Christie }
2698a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_free);
269975613521SMike Christie 
2700e5bd7b54SMike Christie static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2701e5bd7b54SMike Christie {
2702e5bd7b54SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
2703e5bd7b54SMike Christie 	unsigned long flags;
2704e5bd7b54SMike Christie 
2705e5bd7b54SMike Christie 	shost = scsi_host_get(shost);
2706e5bd7b54SMike Christie 	if (!shost) {
2707e5bd7b54SMike Christie 		printk(KERN_ERR "Invalid state. Cannot notify host removal "
2708e5bd7b54SMike Christie 		      "of session teardown event because host already "
2709e5bd7b54SMike Christie 		      "removed.\n");
2710e5bd7b54SMike Christie 		return;
2711e5bd7b54SMike Christie 	}
2712e5bd7b54SMike Christie 
2713e5bd7b54SMike Christie 	spin_lock_irqsave(&ihost->lock, flags);
2714e5bd7b54SMike Christie 	ihost->num_sessions--;
2715e5bd7b54SMike Christie 	if (ihost->num_sessions == 0)
2716e5bd7b54SMike Christie 		wake_up(&ihost->session_removal_wq);
2717e5bd7b54SMike Christie 	spin_unlock_irqrestore(&ihost->lock, flags);
2718e5bd7b54SMike Christie 	scsi_host_put(shost);
2719e5bd7b54SMike Christie }
2720e5bd7b54SMike Christie 
272175613521SMike Christie /**
272275613521SMike Christie  * iscsi_session_setup - create iscsi cls session and host and session
272375613521SMike Christie  * @iscsit: iscsi transport template
272475613521SMike Christie  * @shost: scsi host
272575613521SMike Christie  * @cmds_max: session can queue
27269c19a7d0SMike Christie  * @cmd_task_size: LLD task private data size
272775613521SMike Christie  * @initial_cmdsn: initial CmdSN
272875613521SMike Christie  *
272975613521SMike Christie  * This can be used by software iscsi_transports that allocate
273075613521SMike Christie  * a session per scsi host.
27313cf7b233SMike Christie  *
27323cf7b233SMike Christie  * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
27333cf7b233SMike Christie  * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
27343cf7b233SMike Christie  * for nop handling and login/logout requests.
273575613521SMike Christie  */
273675613521SMike Christie struct iscsi_cls_session *
273775613521SMike Christie iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2738b8b9e1b8SJayamohan Kallickal 		    uint16_t cmds_max, int dd_size, int cmd_task_size,
27397970634bSMike Christie 		    uint32_t initial_cmdsn, unsigned int id)
274075613521SMike Christie {
2741e5bd7b54SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
274275613521SMike Christie 	struct iscsi_session *session;
274375613521SMike Christie 	struct iscsi_cls_session *cls_session;
27443cf7b233SMike Christie 	int cmd_i, scsi_cmds, total_cmds = cmds_max;
2745e5bd7b54SMike Christie 	unsigned long flags;
2746e5bd7b54SMike Christie 
2747e5bd7b54SMike Christie 	spin_lock_irqsave(&ihost->lock, flags);
2748e5bd7b54SMike Christie 	if (ihost->state == ISCSI_HOST_REMOVED) {
2749e5bd7b54SMike Christie 		spin_unlock_irqrestore(&ihost->lock, flags);
2750e5bd7b54SMike Christie 		return NULL;
2751e5bd7b54SMike Christie 	}
2752e5bd7b54SMike Christie 	ihost->num_sessions++;
2753e5bd7b54SMike Christie 	spin_unlock_irqrestore(&ihost->lock, flags);
27548e9a20ceSMike Christie 
27558e9a20ceSMike Christie 	if (!total_cmds)
27568e9a20ceSMike Christie 		total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
27573e5c28adSMike Christie 	/*
27583cf7b233SMike Christie 	 * The iscsi layer needs some tasks for nop handling and tmfs,
27593cf7b233SMike Christie 	 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
27603cf7b233SMike Christie 	 * + 1 command for scsi IO.
27613e5c28adSMike Christie 	 */
27623cf7b233SMike Christie 	if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
27633cf7b233SMike Christie 		printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
27643cf7b233SMike Christie 		       "must be a power of two that is at least %d.\n",
27653cf7b233SMike Christie 		       total_cmds, ISCSI_TOTAL_CMDS_MIN);
2766e5bd7b54SMike Christie 		goto dec_session_count;
27671548271eSMike Christie 	}
27683cf7b233SMike Christie 
27693cf7b233SMike Christie 	if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
27703cf7b233SMike Christie 		printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
27713cf7b233SMike Christie 		       "must be a power of 2 less than or equal to %d.\n",
27723cf7b233SMike Christie 		       cmds_max, ISCSI_TOTAL_CMDS_MAX);
27733cf7b233SMike Christie 		total_cmds = ISCSI_TOTAL_CMDS_MAX;
27743cf7b233SMike Christie 	}
27753cf7b233SMike Christie 
27763cf7b233SMike Christie 	if (!is_power_of_2(total_cmds)) {
27773cf7b233SMike Christie 		printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
27783cf7b233SMike Christie 		       "must be a power of 2.\n", total_cmds);
27793cf7b233SMike Christie 		total_cmds = rounddown_pow_of_two(total_cmds);
27803cf7b233SMike Christie 		if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
27813cf7b233SMike Christie 			return NULL;
27823cf7b233SMike Christie 		printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
27833cf7b233SMike Christie 		       total_cmds);
27843cf7b233SMike Christie 	}
27853cf7b233SMike Christie 	scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
27861548271eSMike Christie 
27875d91e209SMike Christie 	cls_session = iscsi_alloc_session(shost, iscsit,
2788b8b9e1b8SJayamohan Kallickal 					  sizeof(struct iscsi_session) +
2789b8b9e1b8SJayamohan Kallickal 					  dd_size);
279075613521SMike Christie 	if (!cls_session)
2791e5bd7b54SMike Christie 		goto dec_session_count;
279275613521SMike Christie 	session = cls_session->dd_data;
279375613521SMike Christie 	session->cls_session = cls_session;
27947996a778SMike Christie 	session->host = shost;
27957996a778SMike Christie 	session->state = ISCSI_STATE_FREE;
2796f6d5180cSMike Christie 	session->fast_abort = 1;
27973fe5ae8bSMike Christie 	session->tgt_reset_timeout = 30;
27984cd49ea1SMike Christie 	session->lu_reset_timeout = 15;
27994cd49ea1SMike Christie 	session->abort_timeout = 10;
28003cf7b233SMike Christie 	session->scsi_cmds_max = scsi_cmds;
28013cf7b233SMike Christie 	session->cmds_max = total_cmds;
2802e0726407SMike Christie 	session->queued_cmdsn = session->cmdsn = initial_cmdsn;
28037996a778SMike Christie 	session->exp_cmdsn = initial_cmdsn + 1;
28047996a778SMike Christie 	session->max_cmdsn = initial_cmdsn + 1;
28057996a778SMike Christie 	session->max_r2t = 1;
28067996a778SMike Christie 	session->tt = iscsit;
2807b8b9e1b8SJayamohan Kallickal 	session->dd_data = cls_session->dd_data + sizeof(*session);
2808659743b0SShlomo Pongratz 
28096724add1SMike Christie 	mutex_init(&session->eh_mutex);
2810659743b0SShlomo Pongratz 	spin_lock_init(&session->frwd_lock);
2811659743b0SShlomo Pongratz 	spin_lock_init(&session->back_lock);
28127996a778SMike Christie 
28137996a778SMike Christie 	/* initialize SCSI PDU commands pool */
28147996a778SMike Christie 	if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
28157996a778SMike Christie 			    (void***)&session->cmds,
28169c19a7d0SMike Christie 			    cmd_task_size + sizeof(struct iscsi_task)))
28177996a778SMike Christie 		goto cmdpool_alloc_fail;
28187996a778SMike Christie 
28197996a778SMike Christie 	/* pre-format cmds pool with ITT */
28207996a778SMike Christie 	for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
28219c19a7d0SMike Christie 		struct iscsi_task *task = session->cmds[cmd_i];
28227996a778SMike Christie 
28239c19a7d0SMike Christie 		if (cmd_task_size)
28249c19a7d0SMike Christie 			task->dd_data = &task[1];
28259c19a7d0SMike Christie 		task->itt = cmd_i;
28263bbaaad9SMike Christie 		task->state = ISCSI_TASK_FREE;
28279c19a7d0SMike Christie 		INIT_LIST_HEAD(&task->running);
28287996a778SMike Christie 	}
28297996a778SMike Christie 
2830f53a88daSMike Christie 	if (!try_module_get(iscsit->owner))
283175613521SMike Christie 		goto module_get_fail;
283275613521SMike Christie 
28337970634bSMike Christie 	if (iscsi_add_session(cls_session, id))
2834f53a88daSMike Christie 		goto cls_session_fail;
2835e5bd7b54SMike Christie 
28367996a778SMike Christie 	return cls_session;
28377996a778SMike Christie 
28387996a778SMike Christie cls_session_fail:
283975613521SMike Christie 	module_put(iscsit->owner);
284075613521SMike Christie module_get_fail:
28416320377fSOlaf Kirch 	iscsi_pool_free(&session->cmdpool);
28427996a778SMike Christie cmdpool_alloc_fail:
284375613521SMike Christie 	iscsi_free_session(cls_session);
2844e5bd7b54SMike Christie dec_session_count:
2845e5bd7b54SMike Christie 	iscsi_host_dec_session_cnt(shost);
28467996a778SMike Christie 	return NULL;
28477996a778SMike Christie }
28487996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_setup);
28497996a778SMike Christie 
28507996a778SMike Christie /**
28517996a778SMike Christie  * iscsi_session_teardown - destroy session, host, and cls_session
285275613521SMike Christie  * @cls_session: iscsi session
28537996a778SMike Christie  *
285475613521SMike Christie  * The driver must have called iscsi_remove_session before
285575613521SMike Christie  * calling this.
285675613521SMike Christie  */
28577996a778SMike Christie void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
28587996a778SMike Christie {
285975613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
286063f75cc8SMike Christie 	struct module *owner = cls_session->transport->owner;
2861e5bd7b54SMike Christie 	struct Scsi_Host *shost = session->host;
28627996a778SMike Christie 
28636320377fSOlaf Kirch 	iscsi_pool_free(&session->cmdpool);
28647996a778SMike Christie 
2865b2c64167SMike Christie 	kfree(session->password);
2866b2c64167SMike Christie 	kfree(session->password_in);
2867b2c64167SMike Christie 	kfree(session->username);
2868b2c64167SMike Christie 	kfree(session->username_in);
2869f3ff0c36SMike Christie 	kfree(session->targetname);
28703c5c4801SVikas Chaudhary 	kfree(session->targetalias);
287188dfd340SMike Christie 	kfree(session->initiatorname);
28723b9373e9SEddie Wai 	kfree(session->boot_root);
28733b9373e9SEddie Wai 	kfree(session->boot_nic);
28743b9373e9SEddie Wai 	kfree(session->boot_target);
287588dfd340SMike Christie 	kfree(session->ifacename);
2876f8525eb4SAdheer Chandravanshi 	kfree(session->portal_type);
2877f8525eb4SAdheer Chandravanshi 	kfree(session->discovery_parent_type);
2878f3ff0c36SMike Christie 
287975613521SMike Christie 	iscsi_destroy_session(cls_session);
2880e5bd7b54SMike Christie 	iscsi_host_dec_session_cnt(shost);
288163f75cc8SMike Christie 	module_put(owner);
28827996a778SMike Christie }
28837996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_teardown);
28847996a778SMike Christie 
28857996a778SMike Christie /**
28867996a778SMike Christie  * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
28877996a778SMike Christie  * @cls_session: iscsi_cls_session
28885d91e209SMike Christie  * @dd_size: private driver data size
28897996a778SMike Christie  * @conn_idx: cid
28905d91e209SMike Christie  */
28917996a778SMike Christie struct iscsi_cls_conn *
28925d91e209SMike Christie iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
28935d91e209SMike Christie 		 uint32_t conn_idx)
28947996a778SMike Christie {
289575613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
28967996a778SMike Christie 	struct iscsi_conn *conn;
28977996a778SMike Christie 	struct iscsi_cls_conn *cls_conn;
2898d36ab6f3SMike Christie 	char *data;
28997996a778SMike Christie 
29005d91e209SMike Christie 	cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
29015d91e209SMike Christie 				     conn_idx);
29027996a778SMike Christie 	if (!cls_conn)
29037996a778SMike Christie 		return NULL;
29047996a778SMike Christie 	conn = cls_conn->dd_data;
29055d91e209SMike Christie 	memset(conn, 0, sizeof(*conn) + dd_size);
29067996a778SMike Christie 
29075d91e209SMike Christie 	conn->dd_data = cls_conn->dd_data + sizeof(*conn);
29087996a778SMike Christie 	conn->session = session;
29097996a778SMike Christie 	conn->cls_conn = cls_conn;
29107996a778SMike Christie 	conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
29117996a778SMike Christie 	conn->id = conn_idx;
29127996a778SMike Christie 	conn->exp_statsn = 0;
2913843c0a8aSMike Christie 	conn->tmf_state = TMF_INITIAL;
2914f6d5180cSMike Christie 
2915f6d5180cSMike Christie 	init_timer(&conn->transport_timer);
2916f6d5180cSMike Christie 	conn->transport_timer.data = (unsigned long)conn;
2917f6d5180cSMike Christie 	conn->transport_timer.function = iscsi_check_transport_timeouts;
2918f6d5180cSMike Christie 
2919843c0a8aSMike Christie 	INIT_LIST_HEAD(&conn->mgmtqueue);
29203bbaaad9SMike Christie 	INIT_LIST_HEAD(&conn->cmdqueue);
2921843c0a8aSMike Christie 	INIT_LIST_HEAD(&conn->requeue);
2922*6f8830f5SChris Leech 	spin_lock_init(&conn->taskqueuelock);
2923c4028958SDavid Howells 	INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
29247996a778SMike Christie 
29259c19a7d0SMike Christie 	/* allocate login_task used for the login/text sequences */
2926659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
29277acd72ebSStefani Seibold 	if (!kfifo_out(&session->cmdpool.queue,
29289c19a7d0SMike Christie                          (void*)&conn->login_task,
29297996a778SMike Christie 			 sizeof(void*))) {
2930659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
29319c19a7d0SMike Christie 		goto login_task_alloc_fail;
29327996a778SMike Christie 	}
2933659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
29347996a778SMike Christie 
2935cfeb2cf9SMike Christie 	data = (char *) __get_free_pages(GFP_KERNEL,
2936cfeb2cf9SMike Christie 					 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2937d36ab6f3SMike Christie 	if (!data)
29389c19a7d0SMike Christie 		goto login_task_data_alloc_fail;
29399c19a7d0SMike Christie 	conn->login_task->data = conn->data = data;
2940d36ab6f3SMike Christie 
2941843c0a8aSMike Christie 	init_timer(&conn->tmf_timer);
29427996a778SMike Christie 	init_waitqueue_head(&conn->ehwait);
29437996a778SMike Christie 
29447996a778SMike Christie 	return cls_conn;
29457996a778SMike Christie 
29469c19a7d0SMike Christie login_task_data_alloc_fail:
29477acd72ebSStefani Seibold 	kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2948d36ab6f3SMike Christie 		    sizeof(void*));
29499c19a7d0SMike Christie login_task_alloc_fail:
29507996a778SMike Christie 	iscsi_destroy_conn(cls_conn);
29517996a778SMike Christie 	return NULL;
29527996a778SMike Christie }
29537996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_setup);
29547996a778SMike Christie 
29557996a778SMike Christie /**
29567996a778SMike Christie  * iscsi_conn_teardown - teardown iscsi connection
29577996a778SMike Christie  * cls_conn: iscsi class connection
29587996a778SMike Christie  *
29597996a778SMike Christie  * TODO: we may need to make this into a two step process
29607996a778SMike Christie  * like scsi-mls remove + put host
29617996a778SMike Christie  */
29627996a778SMike Christie void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
29637996a778SMike Christie {
29647996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
29657996a778SMike Christie 	struct iscsi_session *session = conn->session;
29667996a778SMike Christie 
2967f6d5180cSMike Christie 	del_timer_sync(&conn->transport_timer);
2968f6d5180cSMike Christie 
2969660d0831SJohn Soni Jose 	mutex_lock(&session->eh_mutex);
2970659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
29717996a778SMike Christie 	conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
29727996a778SMike Christie 	if (session->leadconn == conn) {
29737996a778SMike Christie 		/*
29747996a778SMike Christie 		 * leading connection? then give up on recovery.
29757996a778SMike Christie 		 */
29767996a778SMike Christie 		session->state = ISCSI_STATE_TERMINATE;
29777996a778SMike Christie 		wake_up(&conn->ehwait);
29787996a778SMike Christie 	}
2979659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
29807996a778SMike Christie 
2981779ea120SMike Christie 	/* flush queued up work because we free the connection below */
2982843c0a8aSMike Christie 	iscsi_suspend_tx(conn);
2983779ea120SMike Christie 
2984659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
2985cfeb2cf9SMike Christie 	free_pages((unsigned long) conn->data,
2986cfeb2cf9SMike Christie 		   get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2987f3ff0c36SMike Christie 	kfree(conn->persistent_address);
2988ae56ff40SAdheer Chandravanshi 	kfree(conn->local_ipaddr);
2989659743b0SShlomo Pongratz 	/* regular RX path uses back_lock */
2990659743b0SShlomo Pongratz 	spin_lock_bh(&session->back_lock);
29917acd72ebSStefani Seibold 	kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
29927996a778SMike Christie 		    sizeof(void*));
2993659743b0SShlomo Pongratz 	spin_unlock_bh(&session->back_lock);
2994e0726407SMike Christie 	if (session->leadconn == conn)
29957996a778SMike Christie 		session->leadconn = NULL;
2996659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
2997660d0831SJohn Soni Jose 	mutex_unlock(&session->eh_mutex);
29987996a778SMike Christie 
29997996a778SMike Christie 	iscsi_destroy_conn(cls_conn);
30007996a778SMike Christie }
30017996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
30027996a778SMike Christie 
30037996a778SMike Christie int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
30047996a778SMike Christie {
30057996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
30067996a778SMike Christie 	struct iscsi_session *session = conn->session;
30077996a778SMike Christie 
3008ffd0436eSMike Christie 	if (!session) {
3009322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
3010322d739dSMike Christie 				  "can't start unbound connection\n");
30117996a778SMike Christie 		return -EPERM;
30127996a778SMike Christie 	}
30137996a778SMike Christie 
3014db98ccdeSMike Christie 	if ((session->imm_data_en || !session->initial_r2t_en) &&
3015db98ccdeSMike Christie 	     session->first_burst > session->max_burst) {
3016322d739dSMike Christie 		iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
3017ffd0436eSMike Christie 				  "first_burst %d max_burst %d\n",
3018ffd0436eSMike Christie 				  session->first_burst, session->max_burst);
3019ffd0436eSMike Christie 		return -EINVAL;
3020ffd0436eSMike Christie 	}
3021ffd0436eSMike Christie 
3022f6d5180cSMike Christie 	if (conn->ping_timeout && !conn->recv_timeout) {
3023322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
3024322d739dSMike Christie 				  "zero. Using 5 seconds\n.");
3025f6d5180cSMike Christie 		conn->recv_timeout = 5;
3026f6d5180cSMike Christie 	}
3027f6d5180cSMike Christie 
3028f6d5180cSMike Christie 	if (conn->recv_timeout && !conn->ping_timeout) {
3029322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3030322d739dSMike Christie 				  "zero. Using 5 seconds.\n");
3031f6d5180cSMike Christie 		conn->ping_timeout = 5;
3032f6d5180cSMike Christie 	}
3033f6d5180cSMike Christie 
3034659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
30357996a778SMike Christie 	conn->c_stage = ISCSI_CONN_STARTED;
30367996a778SMike Christie 	session->state = ISCSI_STATE_LOGGED_IN;
3037e0726407SMike Christie 	session->queued_cmdsn = session->cmdsn;
30387996a778SMike Christie 
3039f6d5180cSMike Christie 	conn->last_recv = jiffies;
3040f6d5180cSMike Christie 	conn->last_ping = jiffies;
3041f6d5180cSMike Christie 	if (conn->recv_timeout && conn->ping_timeout)
3042f6d5180cSMike Christie 		mod_timer(&conn->transport_timer,
3043f6d5180cSMike Christie 			  jiffies + (conn->recv_timeout * HZ));
3044f6d5180cSMike Christie 
30457996a778SMike Christie 	switch(conn->stop_stage) {
30467996a778SMike Christie 	case STOP_CONN_RECOVER:
30477996a778SMike Christie 		/*
30487996a778SMike Christie 		 * unblock eh_abort() if it is blocked. re-try all
30497996a778SMike Christie 		 * commands after successful recovery
30507996a778SMike Christie 		 */
30517996a778SMike Christie 		conn->stop_stage = 0;
3052843c0a8aSMike Christie 		conn->tmf_state = TMF_INITIAL;
30537996a778SMike Christie 		session->age++;
30548b1d0343SMike Christie 		if (session->age == 16)
30558b1d0343SMike Christie 			session->age = 0;
30566eabafbeSMike Christie 		break;
30577996a778SMike Christie 	case STOP_CONN_TERM:
30587996a778SMike Christie 		conn->stop_stage = 0;
30597996a778SMike Christie 		break;
30607996a778SMike Christie 	default:
30617996a778SMike Christie 		break;
30627996a778SMike Christie 	}
3063659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
30647996a778SMike Christie 
306575613521SMike Christie 	iscsi_unblock_session(session->cls_session);
30666eabafbeSMike Christie 	wake_up(&conn->ehwait);
30677996a778SMike Christie 	return 0;
30687996a778SMike Christie }
30697996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_start);
30707996a778SMike Christie 
30717996a778SMike Christie static void
30723bbaaad9SMike Christie fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
30737996a778SMike Christie {
30743bbaaad9SMike Christie 	struct iscsi_task *task;
3075b3cd5050SMike Christie 	int i, state;
30767996a778SMike Christie 
30773bbaaad9SMike Christie 	for (i = 0; i < conn->session->cmds_max; i++) {
30783bbaaad9SMike Christie 		task = conn->session->cmds[i];
30793bbaaad9SMike Christie 		if (task->sc)
30803bbaaad9SMike Christie 			continue;
30813bbaaad9SMike Christie 
30823bbaaad9SMike Christie 		if (task->state == ISCSI_TASK_FREE)
30833bbaaad9SMike Christie 			continue;
30843bbaaad9SMike Christie 
30853bbaaad9SMike Christie 		ISCSI_DBG_SESSION(conn->session,
30863bbaaad9SMike Christie 				  "failing mgmt itt 0x%x state %d\n",
30873bbaaad9SMike Christie 				  task->itt, task->state);
3088b3cd5050SMike Christie 		state = ISCSI_TASK_ABRT_SESS_RECOV;
3089b3cd5050SMike Christie 		if (task->state == ISCSI_TASK_PENDING)
3090b3cd5050SMike Christie 			state = ISCSI_TASK_COMPLETED;
3091b3cd5050SMike Christie 		iscsi_complete_task(task, state);
3092b3cd5050SMike Christie 
30937996a778SMike Christie 	}
30947996a778SMike Christie }
30957996a778SMike Christie 
3096656cffc9SMike Christie static void iscsi_start_session_recovery(struct iscsi_session *session,
30977996a778SMike Christie 					 struct iscsi_conn *conn, int flag)
30987996a778SMike Christie {
3099ed2abc7fSMike Christie 	int old_stop_stage;
3100ed2abc7fSMike Christie 
31016724add1SMike Christie 	mutex_lock(&session->eh_mutex);
3102659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
3103ed2abc7fSMike Christie 	if (conn->stop_stage == STOP_CONN_TERM) {
3104659743b0SShlomo Pongratz 		spin_unlock_bh(&session->frwd_lock);
31056724add1SMike Christie 		mutex_unlock(&session->eh_mutex);
31066724add1SMike Christie 		return;
31076724add1SMike Christie 	}
31086724add1SMike Christie 
31096724add1SMike Christie 	/*
3110ed2abc7fSMike Christie 	 * When this is called for the in_login state, we only want to clean
31119c19a7d0SMike Christie 	 * up the login task and connection. We do not need to block and set
311267a61114SMike Christie 	 * the recovery state again
3113ed2abc7fSMike Christie 	 */
311467a61114SMike Christie 	if (flag == STOP_CONN_TERM)
311567a61114SMike Christie 		session->state = ISCSI_STATE_TERMINATE;
311667a61114SMike Christie 	else if (conn->stop_stage != STOP_CONN_RECOVER)
311767a61114SMike Christie 		session->state = ISCSI_STATE_IN_RECOVERY;
31184ae0a6c1SMike Christie 
31194ae0a6c1SMike Christie 	old_stop_stage = conn->stop_stage;
31204ae0a6c1SMike Christie 	conn->stop_stage = flag;
3121659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
3122ed2abc7fSMike Christie 
312326013ad4SMike Christie 	del_timer_sync(&conn->transport_timer);
312426013ad4SMike Christie 	iscsi_suspend_tx(conn);
312526013ad4SMike Christie 
3126659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
312767a61114SMike Christie 	conn->c_stage = ISCSI_CONN_STOPPED;
3128659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
31296724add1SMike Christie 
31307996a778SMike Christie 	/*
31317996a778SMike Christie 	 * for connection level recovery we should not calculate
31327996a778SMike Christie 	 * header digest. conn->hdr_size used for optimization
31337996a778SMike Christie 	 * in hdr_extract() and will be re-negotiated at
31347996a778SMike Christie 	 * set_param() time.
31357996a778SMike Christie 	 */
31367996a778SMike Christie 	if (flag == STOP_CONN_RECOVER) {
31377996a778SMike Christie 		conn->hdrdgst_en = 0;
31387996a778SMike Christie 		conn->datadgst_en = 0;
3139656cffc9SMike Christie 		if (session->state == ISCSI_STATE_IN_RECOVERY &&
314067a61114SMike Christie 		    old_stop_stage != STOP_CONN_RECOVER) {
31411b2c7af8SMike Christie 			ISCSI_DBG_SESSION(session, "blocking session\n");
314275613521SMike Christie 			iscsi_block_session(session->cls_session);
31437996a778SMike Christie 		}
314467a61114SMike Christie 	}
3145656cffc9SMike Christie 
3146656cffc9SMike Christie 	/*
3147656cffc9SMike Christie 	 * flush queues.
3148656cffc9SMike Christie 	 */
3149659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
31503bbaaad9SMike Christie 	fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
31513bbaaad9SMike Christie 	fail_mgmt_tasks(session, conn);
31525d12c05eSMike Christie 	memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
3153659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
31546724add1SMike Christie 	mutex_unlock(&session->eh_mutex);
31557996a778SMike Christie }
31567996a778SMike Christie 
31577996a778SMike Christie void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
31587996a778SMike Christie {
31597996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
31607996a778SMike Christie 	struct iscsi_session *session = conn->session;
31617996a778SMike Christie 
31627996a778SMike Christie 	switch (flag) {
31637996a778SMike Christie 	case STOP_CONN_RECOVER:
31647996a778SMike Christie 	case STOP_CONN_TERM:
31657996a778SMike Christie 		iscsi_start_session_recovery(session, conn, flag);
31668d2860b3SMike Christie 		break;
31677996a778SMike Christie 	default:
3168322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
3169322d739dSMike Christie 				  "invalid stop flag %d\n", flag);
31707996a778SMike Christie 	}
31717996a778SMike Christie }
31727996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_stop);
31737996a778SMike Christie 
31747996a778SMike Christie int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
31757996a778SMike Christie 		    struct iscsi_cls_conn *cls_conn, int is_leading)
31767996a778SMike Christie {
317775613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
317898644047SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
31797996a778SMike Christie 
3180659743b0SShlomo Pongratz 	spin_lock_bh(&session->frwd_lock);
31817996a778SMike Christie 	if (is_leading)
31827996a778SMike Christie 		session->leadconn = conn;
3183659743b0SShlomo Pongratz 	spin_unlock_bh(&session->frwd_lock);
31847996a778SMike Christie 
31857996a778SMike Christie 	/*
31867996a778SMike Christie 	 * Unblock xmitworker(), Login Phase will pass through.
31877996a778SMike Christie 	 */
31887996a778SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
31897996a778SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
31907996a778SMike Christie 	return 0;
31917996a778SMike Christie }
31927996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_bind);
31937996a778SMike Christie 
3194adaf6990SAdheer Chandravanshi int iscsi_switch_str_param(char **param, char *new_val_buf)
31955700b1afSMike Christie {
31965700b1afSMike Christie 	char *new_val;
31975700b1afSMike Christie 
31985700b1afSMike Christie 	if (*param) {
31995700b1afSMike Christie 		if (!strcmp(*param, new_val_buf))
32005700b1afSMike Christie 			return 0;
32015700b1afSMike Christie 	}
32025700b1afSMike Christie 
32035700b1afSMike Christie 	new_val = kstrdup(new_val_buf, GFP_NOIO);
32045700b1afSMike Christie 	if (!new_val)
32055700b1afSMike Christie 		return -ENOMEM;
32065700b1afSMike Christie 
32075700b1afSMike Christie 	kfree(*param);
32085700b1afSMike Christie 	*param = new_val;
32095700b1afSMike Christie 	return 0;
32105700b1afSMike Christie }
3211adaf6990SAdheer Chandravanshi EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
3212a54a52caSMike Christie 
3213a54a52caSMike Christie int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3214a54a52caSMike Christie 		    enum iscsi_param param, char *buf, int buflen)
3215a54a52caSMike Christie {
3216a54a52caSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
3217a54a52caSMike Christie 	struct iscsi_session *session = conn->session;
32186a06a4b8SOr Gerlitz 	int val;
3219a54a52caSMike Christie 
3220a54a52caSMike Christie 	switch(param) {
3221843c0a8aSMike Christie 	case ISCSI_PARAM_FAST_ABORT:
3222843c0a8aSMike Christie 		sscanf(buf, "%d", &session->fast_abort);
3223843c0a8aSMike Christie 		break;
3224f6d5180cSMike Christie 	case ISCSI_PARAM_ABORT_TMO:
3225f6d5180cSMike Christie 		sscanf(buf, "%d", &session->abort_timeout);
3226f6d5180cSMike Christie 		break;
3227f6d5180cSMike Christie 	case ISCSI_PARAM_LU_RESET_TMO:
3228f6d5180cSMike Christie 		sscanf(buf, "%d", &session->lu_reset_timeout);
3229f6d5180cSMike Christie 		break;
32303fe5ae8bSMike Christie 	case ISCSI_PARAM_TGT_RESET_TMO:
32313fe5ae8bSMike Christie 		sscanf(buf, "%d", &session->tgt_reset_timeout);
32323fe5ae8bSMike Christie 		break;
3233f6d5180cSMike Christie 	case ISCSI_PARAM_PING_TMO:
3234f6d5180cSMike Christie 		sscanf(buf, "%d", &conn->ping_timeout);
3235f6d5180cSMike Christie 		break;
3236f6d5180cSMike Christie 	case ISCSI_PARAM_RECV_TMO:
3237f6d5180cSMike Christie 		sscanf(buf, "%d", &conn->recv_timeout);
3238f6d5180cSMike Christie 		break;
3239a54a52caSMike Christie 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
3240a54a52caSMike Christie 		sscanf(buf, "%d", &conn->max_recv_dlength);
3241a54a52caSMike Christie 		break;
3242a54a52caSMike Christie 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3243a54a52caSMike Christie 		sscanf(buf, "%d", &conn->max_xmit_dlength);
3244a54a52caSMike Christie 		break;
3245a54a52caSMike Christie 	case ISCSI_PARAM_HDRDGST_EN:
3246a54a52caSMike Christie 		sscanf(buf, "%d", &conn->hdrdgst_en);
3247a54a52caSMike Christie 		break;
3248a54a52caSMike Christie 	case ISCSI_PARAM_DATADGST_EN:
3249a54a52caSMike Christie 		sscanf(buf, "%d", &conn->datadgst_en);
3250a54a52caSMike Christie 		break;
3251a54a52caSMike Christie 	case ISCSI_PARAM_INITIAL_R2T_EN:
3252a54a52caSMike Christie 		sscanf(buf, "%d", &session->initial_r2t_en);
3253a54a52caSMike Christie 		break;
3254a54a52caSMike Christie 	case ISCSI_PARAM_MAX_R2T:
32551304be5fSMike Christie 		sscanf(buf, "%hu", &session->max_r2t);
3256a54a52caSMike Christie 		break;
3257a54a52caSMike Christie 	case ISCSI_PARAM_IMM_DATA_EN:
3258a54a52caSMike Christie 		sscanf(buf, "%d", &session->imm_data_en);
3259a54a52caSMike Christie 		break;
3260a54a52caSMike Christie 	case ISCSI_PARAM_FIRST_BURST:
3261a54a52caSMike Christie 		sscanf(buf, "%d", &session->first_burst);
3262a54a52caSMike Christie 		break;
3263a54a52caSMike Christie 	case ISCSI_PARAM_MAX_BURST:
3264a54a52caSMike Christie 		sscanf(buf, "%d", &session->max_burst);
3265a54a52caSMike Christie 		break;
3266a54a52caSMike Christie 	case ISCSI_PARAM_PDU_INORDER_EN:
3267a54a52caSMike Christie 		sscanf(buf, "%d", &session->pdu_inorder_en);
3268a54a52caSMike Christie 		break;
3269a54a52caSMike Christie 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
3270a54a52caSMike Christie 		sscanf(buf, "%d", &session->dataseq_inorder_en);
3271a54a52caSMike Christie 		break;
3272a54a52caSMike Christie 	case ISCSI_PARAM_ERL:
3273a54a52caSMike Christie 		sscanf(buf, "%d", &session->erl);
3274a54a52caSMike Christie 		break;
3275a54a52caSMike Christie 	case ISCSI_PARAM_EXP_STATSN:
3276a54a52caSMike Christie 		sscanf(buf, "%u", &conn->exp_statsn);
3277a54a52caSMike Christie 		break;
3278b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME:
32795700b1afSMike Christie 		return iscsi_switch_str_param(&session->username, buf);
3280b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME_IN:
32815700b1afSMike Christie 		return iscsi_switch_str_param(&session->username_in, buf);
3282b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD:
32835700b1afSMike Christie 		return iscsi_switch_str_param(&session->password, buf);
3284b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD_IN:
32855700b1afSMike Christie 		return iscsi_switch_str_param(&session->password_in, buf);
3286a54a52caSMike Christie 	case ISCSI_PARAM_TARGET_NAME:
32875700b1afSMike Christie 		return iscsi_switch_str_param(&session->targetname, buf);
32883c5c4801SVikas Chaudhary 	case ISCSI_PARAM_TARGET_ALIAS:
32893c5c4801SVikas Chaudhary 		return iscsi_switch_str_param(&session->targetalias, buf);
3290a54a52caSMike Christie 	case ISCSI_PARAM_TPGT:
3291a54a52caSMike Christie 		sscanf(buf, "%d", &session->tpgt);
3292a54a52caSMike Christie 		break;
3293a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_PORT:
3294a54a52caSMike Christie 		sscanf(buf, "%d", &conn->persistent_port);
3295a54a52caSMike Christie 		break;
3296a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
32975700b1afSMike Christie 		return iscsi_switch_str_param(&conn->persistent_address, buf);
329888dfd340SMike Christie 	case ISCSI_PARAM_IFACE_NAME:
32995700b1afSMike Christie 		return iscsi_switch_str_param(&session->ifacename, buf);
330088dfd340SMike Christie 	case ISCSI_PARAM_INITIATOR_NAME:
33015700b1afSMike Christie 		return iscsi_switch_str_param(&session->initiatorname, buf);
33023b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_ROOT:
33033b9373e9SEddie Wai 		return iscsi_switch_str_param(&session->boot_root, buf);
33043b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_NIC:
33053b9373e9SEddie Wai 		return iscsi_switch_str_param(&session->boot_nic, buf);
33063b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_TARGET:
33073b9373e9SEddie Wai 		return iscsi_switch_str_param(&session->boot_target, buf);
3308f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_PORTAL_TYPE:
3309f8525eb4SAdheer Chandravanshi 		return iscsi_switch_str_param(&session->portal_type, buf);
3310f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3311f8525eb4SAdheer Chandravanshi 		return iscsi_switch_str_param(&session->discovery_parent_type,
3312f8525eb4SAdheer Chandravanshi 					      buf);
33136a06a4b8SOr Gerlitz 	case ISCSI_PARAM_DISCOVERY_SESS:
33146a06a4b8SOr Gerlitz 		sscanf(buf, "%d", &val);
33156a06a4b8SOr Gerlitz 		session->discovery_sess = !!val;
33166a06a4b8SOr Gerlitz 		break;
3317ae56ff40SAdheer Chandravanshi 	case ISCSI_PARAM_LOCAL_IPADDR:
3318ae56ff40SAdheer Chandravanshi 		return iscsi_switch_str_param(&conn->local_ipaddr, buf);
3319a54a52caSMike Christie 	default:
3320a54a52caSMike Christie 		return -ENOSYS;
3321a54a52caSMike Christie 	}
3322a54a52caSMike Christie 
3323a54a52caSMike Christie 	return 0;
3324a54a52caSMike Christie }
3325a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_set_param);
3326a54a52caSMike Christie 
3327a54a52caSMike Christie int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3328a54a52caSMike Christie 			    enum iscsi_param param, char *buf)
3329a54a52caSMike Christie {
333075613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
3331a54a52caSMike Christie 	int len;
3332a54a52caSMike Christie 
3333a54a52caSMike Christie 	switch(param) {
3334843c0a8aSMike Christie 	case ISCSI_PARAM_FAST_ABORT:
3335843c0a8aSMike Christie 		len = sprintf(buf, "%d\n", session->fast_abort);
3336843c0a8aSMike Christie 		break;
3337f6d5180cSMike Christie 	case ISCSI_PARAM_ABORT_TMO:
3338f6d5180cSMike Christie 		len = sprintf(buf, "%d\n", session->abort_timeout);
3339f6d5180cSMike Christie 		break;
3340f6d5180cSMike Christie 	case ISCSI_PARAM_LU_RESET_TMO:
3341f6d5180cSMike Christie 		len = sprintf(buf, "%d\n", session->lu_reset_timeout);
3342f6d5180cSMike Christie 		break;
33433fe5ae8bSMike Christie 	case ISCSI_PARAM_TGT_RESET_TMO:
33443fe5ae8bSMike Christie 		len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
33453fe5ae8bSMike Christie 		break;
3346a54a52caSMike Christie 	case ISCSI_PARAM_INITIAL_R2T_EN:
3347a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->initial_r2t_en);
3348a54a52caSMike Christie 		break;
3349a54a52caSMike Christie 	case ISCSI_PARAM_MAX_R2T:
3350a54a52caSMike Christie 		len = sprintf(buf, "%hu\n", session->max_r2t);
3351a54a52caSMike Christie 		break;
3352a54a52caSMike Christie 	case ISCSI_PARAM_IMM_DATA_EN:
3353a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->imm_data_en);
3354a54a52caSMike Christie 		break;
3355a54a52caSMike Christie 	case ISCSI_PARAM_FIRST_BURST:
3356a54a52caSMike Christie 		len = sprintf(buf, "%u\n", session->first_burst);
3357a54a52caSMike Christie 		break;
3358a54a52caSMike Christie 	case ISCSI_PARAM_MAX_BURST:
3359a54a52caSMike Christie 		len = sprintf(buf, "%u\n", session->max_burst);
3360a54a52caSMike Christie 		break;
3361a54a52caSMike Christie 	case ISCSI_PARAM_PDU_INORDER_EN:
3362a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->pdu_inorder_en);
3363a54a52caSMike Christie 		break;
3364a54a52caSMike Christie 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
3365a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
3366a54a52caSMike Christie 		break;
33675f09e1f3SAdheer Chandravanshi 	case ISCSI_PARAM_DEF_TASKMGMT_TMO:
33685f09e1f3SAdheer Chandravanshi 		len = sprintf(buf, "%d\n", session->def_taskmgmt_tmo);
33695f09e1f3SAdheer Chandravanshi 		break;
3370a54a52caSMike Christie 	case ISCSI_PARAM_ERL:
3371a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->erl);
3372a54a52caSMike Christie 		break;
3373a54a52caSMike Christie 	case ISCSI_PARAM_TARGET_NAME:
3374a54a52caSMike Christie 		len = sprintf(buf, "%s\n", session->targetname);
3375a54a52caSMike Christie 		break;
33763c5c4801SVikas Chaudhary 	case ISCSI_PARAM_TARGET_ALIAS:
33773c5c4801SVikas Chaudhary 		len = sprintf(buf, "%s\n", session->targetalias);
33783c5c4801SVikas Chaudhary 		break;
3379a54a52caSMike Christie 	case ISCSI_PARAM_TPGT:
3380a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->tpgt);
3381a54a52caSMike Christie 		break;
3382b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME:
3383b2c64167SMike Christie 		len = sprintf(buf, "%s\n", session->username);
3384b2c64167SMike Christie 		break;
3385b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME_IN:
3386b2c64167SMike Christie 		len = sprintf(buf, "%s\n", session->username_in);
3387b2c64167SMike Christie 		break;
3388b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD:
3389b2c64167SMike Christie 		len = sprintf(buf, "%s\n", session->password);
3390b2c64167SMike Christie 		break;
3391b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD_IN:
3392b2c64167SMike Christie 		len = sprintf(buf, "%s\n", session->password_in);
3393b2c64167SMike Christie 		break;
339488dfd340SMike Christie 	case ISCSI_PARAM_IFACE_NAME:
339588dfd340SMike Christie 		len = sprintf(buf, "%s\n", session->ifacename);
339688dfd340SMike Christie 		break;
339788dfd340SMike Christie 	case ISCSI_PARAM_INITIATOR_NAME:
339888dfd340SMike Christie 		len = sprintf(buf, "%s\n", session->initiatorname);
339988dfd340SMike Christie 		break;
34003b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_ROOT:
34013b9373e9SEddie Wai 		len = sprintf(buf, "%s\n", session->boot_root);
34023b9373e9SEddie Wai 		break;
34033b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_NIC:
34043b9373e9SEddie Wai 		len = sprintf(buf, "%s\n", session->boot_nic);
34053b9373e9SEddie Wai 		break;
34063b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_TARGET:
34073b9373e9SEddie Wai 		len = sprintf(buf, "%s\n", session->boot_target);
34089a2307b1SAdheer Chandravanshi 		break;
3409f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
3410f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->auto_snd_tgt_disable);
3411f8525eb4SAdheer Chandravanshi 		break;
3412f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_SESS:
3413f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->discovery_sess);
3414f8525eb4SAdheer Chandravanshi 		break;
3415f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_PORTAL_TYPE:
3416f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%s\n", session->portal_type);
3417f8525eb4SAdheer Chandravanshi 		break;
3418f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_CHAP_AUTH_EN:
3419f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->chap_auth_en);
3420f8525eb4SAdheer Chandravanshi 		break;
3421f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
3422f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->discovery_logout_en);
3423f8525eb4SAdheer Chandravanshi 		break;
3424f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_BIDI_CHAP_EN:
3425f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->bidi_chap_en);
3426f8525eb4SAdheer Chandravanshi 		break;
3427f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
3428f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->discovery_auth_optional);
3429f8525eb4SAdheer Chandravanshi 		break;
3430f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DEF_TIME2WAIT:
3431f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%d\n", session->time2wait);
3432f8525eb4SAdheer Chandravanshi 		break;
3433f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DEF_TIME2RETAIN:
3434f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%d\n", session->time2retain);
3435f8525eb4SAdheer Chandravanshi 		break;
3436f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TSID:
3437f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->tsid);
3438f8525eb4SAdheer Chandravanshi 		break;
3439f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_ISID:
3440f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%02x%02x%02x%02x%02x%02x\n",
3441f8525eb4SAdheer Chandravanshi 			      session->isid[0], session->isid[1],
3442f8525eb4SAdheer Chandravanshi 			      session->isid[2], session->isid[3],
3443f8525eb4SAdheer Chandravanshi 			      session->isid[4], session->isid[5]);
3444f8525eb4SAdheer Chandravanshi 		break;
3445f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
3446f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->discovery_parent_idx);
3447f8525eb4SAdheer Chandravanshi 		break;
3448f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3449f8525eb4SAdheer Chandravanshi 		if (session->discovery_parent_type)
3450f8525eb4SAdheer Chandravanshi 			len = sprintf(buf, "%s\n",
3451f8525eb4SAdheer Chandravanshi 				      session->discovery_parent_type);
3452f8525eb4SAdheer Chandravanshi 		else
3453f8525eb4SAdheer Chandravanshi 			len = sprintf(buf, "\n");
34543b9373e9SEddie Wai 		break;
3455a54a52caSMike Christie 	default:
3456a54a52caSMike Christie 		return -ENOSYS;
3457a54a52caSMike Christie 	}
3458a54a52caSMike Christie 
3459a54a52caSMike Christie 	return len;
3460a54a52caSMike Christie }
3461a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3462a54a52caSMike Christie 
346300f3708eSMike Christie int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
346400f3708eSMike Christie 			      enum iscsi_param param, char *buf)
346500f3708eSMike Christie {
346600f3708eSMike Christie 	struct sockaddr_in6 *sin6 = NULL;
346700f3708eSMike Christie 	struct sockaddr_in *sin = NULL;
346800f3708eSMike Christie 	int len;
346900f3708eSMike Christie 
347000f3708eSMike Christie 	switch (addr->ss_family) {
347100f3708eSMike Christie 	case AF_INET:
347200f3708eSMike Christie 		sin = (struct sockaddr_in *)addr;
347300f3708eSMike Christie 		break;
347400f3708eSMike Christie 	case AF_INET6:
347500f3708eSMike Christie 		sin6 = (struct sockaddr_in6 *)addr;
347600f3708eSMike Christie 		break;
347700f3708eSMike Christie 	default:
347800f3708eSMike Christie 		return -EINVAL;
347900f3708eSMike Christie 	}
348000f3708eSMike Christie 
348100f3708eSMike Christie 	switch (param) {
348200f3708eSMike Christie 	case ISCSI_PARAM_CONN_ADDRESS:
348300f3708eSMike Christie 	case ISCSI_HOST_PARAM_IPADDRESS:
348400f3708eSMike Christie 		if (sin)
348500f3708eSMike Christie 			len = sprintf(buf, "%pI4\n", &sin->sin_addr.s_addr);
348600f3708eSMike Christie 		else
348700f3708eSMike Christie 			len = sprintf(buf, "%pI6\n", &sin6->sin6_addr);
348800f3708eSMike Christie 		break;
348900f3708eSMike Christie 	case ISCSI_PARAM_CONN_PORT:
34904bfb8ebfSMike Christie 	case ISCSI_PARAM_LOCAL_PORT:
349100f3708eSMike Christie 		if (sin)
349200f3708eSMike Christie 			len = sprintf(buf, "%hu\n", be16_to_cpu(sin->sin_port));
349300f3708eSMike Christie 		else
349400f3708eSMike Christie 			len = sprintf(buf, "%hu\n",
349500f3708eSMike Christie 				      be16_to_cpu(sin6->sin6_port));
349600f3708eSMike Christie 		break;
349700f3708eSMike Christie 	default:
349800f3708eSMike Christie 		return -EINVAL;
349900f3708eSMike Christie 	}
350000f3708eSMike Christie 
350100f3708eSMike Christie 	return len;
350200f3708eSMike Christie }
350300f3708eSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
350400f3708eSMike Christie 
3505a54a52caSMike Christie int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3506a54a52caSMike Christie 			 enum iscsi_param param, char *buf)
3507a54a52caSMike Christie {
3508a54a52caSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
3509a54a52caSMike Christie 	int len;
3510a54a52caSMike Christie 
3511a54a52caSMike Christie 	switch(param) {
3512f6d5180cSMike Christie 	case ISCSI_PARAM_PING_TMO:
3513f6d5180cSMike Christie 		len = sprintf(buf, "%u\n", conn->ping_timeout);
3514f6d5180cSMike Christie 		break;
3515f6d5180cSMike Christie 	case ISCSI_PARAM_RECV_TMO:
3516f6d5180cSMike Christie 		len = sprintf(buf, "%u\n", conn->recv_timeout);
3517f6d5180cSMike Christie 		break;
3518a54a52caSMike Christie 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
3519a54a52caSMike Christie 		len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3520a54a52caSMike Christie 		break;
3521a54a52caSMike Christie 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3522a54a52caSMike Christie 		len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3523a54a52caSMike Christie 		break;
3524a54a52caSMike Christie 	case ISCSI_PARAM_HDRDGST_EN:
3525a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3526a54a52caSMike Christie 		break;
3527a54a52caSMike Christie 	case ISCSI_PARAM_DATADGST_EN:
3528a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->datadgst_en);
3529a54a52caSMike Christie 		break;
3530a54a52caSMike Christie 	case ISCSI_PARAM_IFMARKER_EN:
3531a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->ifmarker_en);
3532a54a52caSMike Christie 		break;
3533a54a52caSMike Christie 	case ISCSI_PARAM_OFMARKER_EN:
3534a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->ofmarker_en);
3535a54a52caSMike Christie 		break;
3536a54a52caSMike Christie 	case ISCSI_PARAM_EXP_STATSN:
3537a54a52caSMike Christie 		len = sprintf(buf, "%u\n", conn->exp_statsn);
3538a54a52caSMike Christie 		break;
3539a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_PORT:
3540a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->persistent_port);
3541a54a52caSMike Christie 		break;
3542a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
3543a54a52caSMike Christie 		len = sprintf(buf, "%s\n", conn->persistent_address);
3544a54a52caSMike Christie 		break;
3545f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_STATSN:
3546f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->statsn);
3547f8525eb4SAdheer Chandravanshi 		break;
3548f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_MAX_SEGMENT_SIZE:
3549f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->max_segment_size);
3550f8525eb4SAdheer Chandravanshi 		break;
3551f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_KEEPALIVE_TMO:
3552f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->keepalive_tmo);
3553f8525eb4SAdheer Chandravanshi 		break;
3554f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_LOCAL_PORT:
3555f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->local_port);
3556f8525eb4SAdheer Chandravanshi 		break;
3557f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
3558f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->tcp_timestamp_stat);
3559f8525eb4SAdheer Chandravanshi 		break;
3560f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_NAGLE_DISABLE:
3561f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->tcp_nagle_disable);
3562f8525eb4SAdheer Chandravanshi 		break;
3563f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_WSF_DISABLE:
3564f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->tcp_wsf_disable);
3565f8525eb4SAdheer Chandravanshi 		break;
3566f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_TIMER_SCALE:
3567f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->tcp_timer_scale);
3568f8525eb4SAdheer Chandravanshi 		break;
3569f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_TIMESTAMP_EN:
3570f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->tcp_timestamp_en);
3571f8525eb4SAdheer Chandravanshi 		break;
3572f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
3573f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->fragment_disable);
3574f8525eb4SAdheer Chandravanshi 		break;
3575f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IPV4_TOS:
3576f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->ipv4_tos);
3577f8525eb4SAdheer Chandravanshi 		break;
3578f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IPV6_TC:
3579f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->ipv6_traffic_class);
3580f8525eb4SAdheer Chandravanshi 		break;
35815f09e1f3SAdheer Chandravanshi 	case ISCSI_PARAM_IPV6_FLOW_LABEL:
35825f09e1f3SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->ipv6_flow_label);
35835f09e1f3SAdheer Chandravanshi 		break;
3584f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
3585f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->is_fw_assigned_ipv6);
3586f8525eb4SAdheer Chandravanshi 		break;
3587f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_XMIT_WSF:
3588f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->tcp_xmit_wsf);
3589f8525eb4SAdheer Chandravanshi 		break;
3590f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_RECV_WSF:
3591f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->tcp_recv_wsf);
3592f8525eb4SAdheer Chandravanshi 		break;
3593ae56ff40SAdheer Chandravanshi 	case ISCSI_PARAM_LOCAL_IPADDR:
3594ae56ff40SAdheer Chandravanshi 		len = sprintf(buf, "%s\n", conn->local_ipaddr);
3595ae56ff40SAdheer Chandravanshi 		break;
3596a54a52caSMike Christie 	default:
3597a54a52caSMike Christie 		return -ENOSYS;
3598a54a52caSMike Christie 	}
3599a54a52caSMike Christie 
3600a54a52caSMike Christie 	return len;
3601a54a52caSMike Christie }
3602a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3603a54a52caSMike Christie 
36040801c242SMike Christie int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
36050801c242SMike Christie 			 char *buf)
36060801c242SMike Christie {
360775613521SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
36080801c242SMike Christie 	int len;
36090801c242SMike Christie 
36100801c242SMike Christie 	switch (param) {
3611d8196ed2SMike Christie 	case ISCSI_HOST_PARAM_NETDEV_NAME:
361275613521SMike Christie 		len = sprintf(buf, "%s\n", ihost->netdev);
3613d8196ed2SMike Christie 		break;
36140801c242SMike Christie 	case ISCSI_HOST_PARAM_HWADDRESS:
361575613521SMike Christie 		len = sprintf(buf, "%s\n", ihost->hwaddress);
36160801c242SMike Christie 		break;
36178ad5781aSMike Christie 	case ISCSI_HOST_PARAM_INITIATOR_NAME:
361875613521SMike Christie 		len = sprintf(buf, "%s\n", ihost->initiatorname);
36198ad5781aSMike Christie 		break;
36200801c242SMike Christie 	default:
36210801c242SMike Christie 		return -ENOSYS;
36220801c242SMike Christie 	}
36230801c242SMike Christie 
36240801c242SMike Christie 	return len;
36250801c242SMike Christie }
36260801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_get_param);
36270801c242SMike Christie 
36280801c242SMike Christie int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
36290801c242SMike Christie 			 char *buf, int buflen)
36300801c242SMike Christie {
363175613521SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
36320801c242SMike Christie 
36330801c242SMike Christie 	switch (param) {
3634d8196ed2SMike Christie 	case ISCSI_HOST_PARAM_NETDEV_NAME:
36355700b1afSMike Christie 		return iscsi_switch_str_param(&ihost->netdev, buf);
36360801c242SMike Christie 	case ISCSI_HOST_PARAM_HWADDRESS:
36375700b1afSMike Christie 		return iscsi_switch_str_param(&ihost->hwaddress, buf);
36388ad5781aSMike Christie 	case ISCSI_HOST_PARAM_INITIATOR_NAME:
36395700b1afSMike Christie 		return iscsi_switch_str_param(&ihost->initiatorname, buf);
36400801c242SMike Christie 	default:
36410801c242SMike Christie 		return -ENOSYS;
36420801c242SMike Christie 	}
36430801c242SMike Christie 
36440801c242SMike Christie 	return 0;
36450801c242SMike Christie }
36460801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_set_param);
36470801c242SMike Christie 
36487996a778SMike Christie MODULE_AUTHOR("Mike Christie");
36497996a778SMike Christie MODULE_DESCRIPTION("iSCSI library functions");
36507996a778SMike Christie MODULE_LICENSE("GPL");
3651