xref: /linux/drivers/scsi/libiscsi.c (revision e5dbbe273f29e3de7f954abeba95ce986fafc532)
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 &&
11377a23c21SMike Christie 	    !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
11477a23c21SMike Christie 		session->max_cmdsn = max_cmdsn;
11577a23c21SMike Christie 		/*
11677a23c21SMike Christie 		 * if the window closed with IO queued, then kick the
11777a23c21SMike Christie 		 * xmit thread
11877a23c21SMike Christie 		 */
1193bbaaad9SMike Christie 		if (!list_empty(&session->leadconn->cmdqueue) ||
1201336aed1SMike Christie 		    !list_empty(&session->leadconn->mgmtqueue))
12132ae763eSMike Christie 			iscsi_conn_queue_work(session->leadconn);
1227996a778SMike Christie 	}
12377a23c21SMike Christie }
1244c0ba5d2SMike Christie 
1254c0ba5d2SMike Christie void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
1264c0ba5d2SMike Christie {
1274c0ba5d2SMike Christie 	__iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
1284c0ba5d2SMike Christie 			     be32_to_cpu(hdr->max_cmdsn));
1294c0ba5d2SMike Christie }
13077a23c21SMike Christie EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
1317996a778SMike Christie 
132577577daSMike Christie /**
133577577daSMike Christie  * iscsi_prep_data_out_pdu - initialize Data-Out
134577577daSMike Christie  * @task: scsi command task
135577577daSMike Christie  * @r2t: R2T info
136577577daSMike Christie  * @hdr: iscsi data in pdu
137577577daSMike Christie  *
138577577daSMike Christie  * Notes:
139577577daSMike Christie  *	Initialize Data-Out within this R2T sequence and finds
140577577daSMike Christie  *	proper data_offset within this SCSI command.
141577577daSMike Christie  *
142577577daSMike Christie  *	This function is called with connection lock taken.
143577577daSMike Christie  **/
144577577daSMike Christie void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
145ffd0436eSMike Christie 			   struct iscsi_data *hdr)
1467996a778SMike Christie {
1479c19a7d0SMike Christie 	struct iscsi_conn *conn = task->conn;
148577577daSMike Christie 	unsigned int left = r2t->data_length - r2t->sent;
149577577daSMike Christie 
150577577daSMike Christie 	task->hdr_len = sizeof(struct iscsi_data);
1517996a778SMike Christie 
1527996a778SMike Christie 	memset(hdr, 0, sizeof(struct iscsi_data));
153577577daSMike Christie 	hdr->ttt = r2t->ttt;
154577577daSMike Christie 	hdr->datasn = cpu_to_be32(r2t->datasn);
155577577daSMike Christie 	r2t->datasn++;
1567996a778SMike Christie 	hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
15755bdabdfSAndy Grover 	hdr->lun = task->lun;
158577577daSMike Christie 	hdr->itt = task->hdr_itt;
159577577daSMike Christie 	hdr->exp_statsn = r2t->exp_statsn;
160577577daSMike Christie 	hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
161577577daSMike Christie 	if (left > conn->max_xmit_dlength) {
1627996a778SMike Christie 		hton24(hdr->dlength, conn->max_xmit_dlength);
163577577daSMike Christie 		r2t->data_count = conn->max_xmit_dlength;
1647996a778SMike Christie 		hdr->flags = 0;
1657996a778SMike Christie 	} else {
166577577daSMike Christie 		hton24(hdr->dlength, left);
167577577daSMike Christie 		r2t->data_count = left;
1687996a778SMike Christie 		hdr->flags = ISCSI_FLAG_CMD_FINAL;
1697996a778SMike Christie 	}
170577577daSMike Christie 	conn->dataout_pdus_cnt++;
1717996a778SMike Christie }
172577577daSMike Christie EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
1737996a778SMike Christie 
1749c19a7d0SMike Christie static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
175004d6530SBoaz Harrosh {
1769c19a7d0SMike Christie 	unsigned exp_len = task->hdr_len + len;
177004d6530SBoaz Harrosh 
1789c19a7d0SMike Christie 	if (exp_len > task->hdr_max) {
179004d6530SBoaz Harrosh 		WARN_ON(1);
180004d6530SBoaz Harrosh 		return -EINVAL;
181004d6530SBoaz Harrosh 	}
182004d6530SBoaz Harrosh 
183004d6530SBoaz Harrosh 	WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
1849c19a7d0SMike Christie 	task->hdr_len = exp_len;
185004d6530SBoaz Harrosh 	return 0;
186004d6530SBoaz Harrosh }
187004d6530SBoaz Harrosh 
18838d1c069SBoaz Harrosh /*
18938d1c069SBoaz Harrosh  * make an extended cdb AHS
19038d1c069SBoaz Harrosh  */
1919c19a7d0SMike Christie static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
19238d1c069SBoaz Harrosh {
1939c19a7d0SMike Christie 	struct scsi_cmnd *cmd = task->sc;
19438d1c069SBoaz Harrosh 	unsigned rlen, pad_len;
19538d1c069SBoaz Harrosh 	unsigned short ahslength;
19638d1c069SBoaz Harrosh 	struct iscsi_ecdb_ahdr *ecdb_ahdr;
19738d1c069SBoaz Harrosh 	int rc;
19838d1c069SBoaz Harrosh 
1999c19a7d0SMike Christie 	ecdb_ahdr = iscsi_next_hdr(task);
20038d1c069SBoaz Harrosh 	rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
20138d1c069SBoaz Harrosh 
20238d1c069SBoaz Harrosh 	BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
20338d1c069SBoaz Harrosh 	ahslength = rlen + sizeof(ecdb_ahdr->reserved);
20438d1c069SBoaz Harrosh 
20538d1c069SBoaz Harrosh 	pad_len = iscsi_padding(rlen);
20638d1c069SBoaz Harrosh 
2079c19a7d0SMike Christie 	rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
20838d1c069SBoaz Harrosh 	                   sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
20938d1c069SBoaz Harrosh 	if (rc)
21038d1c069SBoaz Harrosh 		return rc;
21138d1c069SBoaz Harrosh 
21238d1c069SBoaz Harrosh 	if (pad_len)
21338d1c069SBoaz Harrosh 		memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
21438d1c069SBoaz Harrosh 
21538d1c069SBoaz Harrosh 	ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
21638d1c069SBoaz Harrosh 	ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
21738d1c069SBoaz Harrosh 	ecdb_ahdr->reserved = 0;
21838d1c069SBoaz Harrosh 	memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
21938d1c069SBoaz Harrosh 
2201b2c7af8SMike Christie 	ISCSI_DBG_SESSION(task->conn->session,
2211b2c7af8SMike Christie 			  "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
2221b2c7af8SMike Christie 		          "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
2231b2c7af8SMike Christie 		          "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
2241b2c7af8SMike Christie 		          task->hdr_len);
22538d1c069SBoaz Harrosh 	return 0;
22638d1c069SBoaz Harrosh }
22738d1c069SBoaz Harrosh 
2289c19a7d0SMike Christie static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
229c07d4444SBoaz Harrosh {
2309c19a7d0SMike Christie 	struct scsi_cmnd *sc = task->sc;
231c07d4444SBoaz Harrosh 	struct iscsi_rlength_ahdr *rlen_ahdr;
232c07d4444SBoaz Harrosh 	int rc;
233c07d4444SBoaz Harrosh 
2349c19a7d0SMike Christie 	rlen_ahdr = iscsi_next_hdr(task);
2359c19a7d0SMike Christie 	rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
236c07d4444SBoaz Harrosh 	if (rc)
237c07d4444SBoaz Harrosh 		return rc;
238c07d4444SBoaz Harrosh 
239c07d4444SBoaz Harrosh 	rlen_ahdr->ahslength =
240c07d4444SBoaz Harrosh 		cpu_to_be16(sizeof(rlen_ahdr->read_length) +
241c07d4444SBoaz Harrosh 						  sizeof(rlen_ahdr->reserved));
242c07d4444SBoaz Harrosh 	rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
243c07d4444SBoaz Harrosh 	rlen_ahdr->reserved = 0;
244c07d4444SBoaz Harrosh 	rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
245c07d4444SBoaz Harrosh 
2461b2c7af8SMike Christie 	ISCSI_DBG_SESSION(task->conn->session,
2471b2c7af8SMike Christie 			  "bidi-in rlen_ahdr->read_length(%d) "
248c07d4444SBoaz Harrosh 		          "rlen_ahdr->ahslength(%d)\n",
249c07d4444SBoaz Harrosh 		          be32_to_cpu(rlen_ahdr->read_length),
250c07d4444SBoaz Harrosh 		          be16_to_cpu(rlen_ahdr->ahslength));
251c07d4444SBoaz Harrosh 	return 0;
252c07d4444SBoaz Harrosh }
253c07d4444SBoaz Harrosh 
2547996a778SMike Christie /**
2555d12c05eSMike Christie  * iscsi_check_tmf_restrictions - check if a task is affected by TMF
2565d12c05eSMike Christie  * @task: iscsi task
2575d12c05eSMike Christie  * @opcode: opcode to check for
2585d12c05eSMike Christie  *
2595d12c05eSMike Christie  * During TMF a task has to be checked if it's affected.
2605d12c05eSMike Christie  * All unrelated I/O can be passed through, but I/O to the
2615d12c05eSMike Christie  * affected LUN should be restricted.
2625d12c05eSMike Christie  * If 'fast_abort' is set we won't be sending any I/O to the
2635d12c05eSMike Christie  * affected LUN.
2645d12c05eSMike Christie  * Otherwise the target is waiting for all TTTs to be completed,
2655d12c05eSMike Christie  * so we have to send all outstanding Data-Out PDUs to the target.
2665d12c05eSMike Christie  */
2675d12c05eSMike Christie static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
2685d12c05eSMike Christie {
2695d12c05eSMike Christie 	struct iscsi_conn *conn = task->conn;
2705d12c05eSMike Christie 	struct iscsi_tm *tmf = &conn->tmhdr;
2715d12c05eSMike Christie 	unsigned int hdr_lun;
2725d12c05eSMike Christie 
2735d12c05eSMike Christie 	if (conn->tmf_state == TMF_INITIAL)
2745d12c05eSMike Christie 		return 0;
2755d12c05eSMike Christie 
2765d12c05eSMike Christie 	if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
2775d12c05eSMike Christie 		return 0;
2785d12c05eSMike Christie 
2795d12c05eSMike Christie 	switch (ISCSI_TM_FUNC_VALUE(tmf)) {
2805d12c05eSMike Christie 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2815d12c05eSMike Christie 		/*
2825d12c05eSMike Christie 		 * Allow PDUs for unrelated LUNs
2835d12c05eSMike Christie 		 */
28455bdabdfSAndy Grover 		hdr_lun = scsilun_to_int(&tmf->lun);
2855d12c05eSMike Christie 		if (hdr_lun != task->sc->device->lun)
2865d12c05eSMike Christie 			return 0;
2873fe5ae8bSMike Christie 		/* fall through */
2883fe5ae8bSMike Christie 	case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2895d12c05eSMike Christie 		/*
2905d12c05eSMike Christie 		 * Fail all SCSI cmd PDUs
2915d12c05eSMike Christie 		 */
2925d12c05eSMike Christie 		if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
2935d12c05eSMike Christie 			iscsi_conn_printk(KERN_INFO, conn,
2945d12c05eSMike Christie 					  "task [op %x/%x itt "
2953fe5ae8bSMike Christie 					  "0x%x/0x%x] "
2965d12c05eSMike Christie 					  "rejected.\n",
2975d12c05eSMike Christie 					  task->hdr->opcode, opcode,
2983fe5ae8bSMike Christie 					  task->itt, task->hdr_itt);
2995d12c05eSMike Christie 			return -EACCES;
3005d12c05eSMike Christie 		}
3015d12c05eSMike Christie 		/*
3025d12c05eSMike Christie 		 * And also all data-out PDUs in response to R2T
3035d12c05eSMike Christie 		 * if fast_abort is set.
3045d12c05eSMike Christie 		 */
3055d12c05eSMike Christie 		if (conn->session->fast_abort) {
3065d12c05eSMike Christie 			iscsi_conn_printk(KERN_INFO, conn,
3075d12c05eSMike Christie 					  "task [op %x/%x itt "
3083fe5ae8bSMike Christie 					  "0x%x/0x%x] fast abort.\n",
3095d12c05eSMike Christie 					  task->hdr->opcode, opcode,
3103fe5ae8bSMike Christie 					  task->itt, task->hdr_itt);
3115d12c05eSMike Christie 			return -EACCES;
3125d12c05eSMike Christie 		}
3135d12c05eSMike Christie 		break;
3145d12c05eSMike Christie 	case ISCSI_TM_FUNC_ABORT_TASK:
3155d12c05eSMike Christie 		/*
3165d12c05eSMike Christie 		 * the caller has already checked if the task
3175d12c05eSMike Christie 		 * they want to abort was in the pending queue so if
3185d12c05eSMike Christie 		 * we are here the cmd pdu has gone out already, and
3195d12c05eSMike Christie 		 * we will only hit this for data-outs
3205d12c05eSMike Christie 		 */
3215d12c05eSMike Christie 		if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
3225d12c05eSMike Christie 		    task->hdr_itt == tmf->rtt) {
3235d12c05eSMike Christie 			ISCSI_DBG_SESSION(conn->session,
3245d12c05eSMike Christie 					  "Preventing task %x/%x from sending "
3255d12c05eSMike Christie 					  "data-out due to abort task in "
3265d12c05eSMike Christie 					  "progress\n", task->itt,
3275d12c05eSMike Christie 					  task->hdr_itt);
3285d12c05eSMike Christie 			return -EACCES;
3295d12c05eSMike Christie 		}
3305d12c05eSMike Christie 		break;
3315d12c05eSMike Christie 	}
3325d12c05eSMike Christie 
3335d12c05eSMike Christie 	return 0;
3345d12c05eSMike Christie }
3355d12c05eSMike Christie 
3365d12c05eSMike Christie /**
3377996a778SMike Christie  * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
3389c19a7d0SMike Christie  * @task: iscsi task
3397996a778SMike Christie  *
3407996a778SMike Christie  * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
3417996a778SMike Christie  * fields like dlength or final based on how much data it sends
3427996a778SMike Christie  */
3439c19a7d0SMike Christie static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
3447996a778SMike Christie {
3459c19a7d0SMike Christie 	struct iscsi_conn *conn = task->conn;
3467996a778SMike Christie 	struct iscsi_session *session = conn->session;
3479c19a7d0SMike Christie 	struct scsi_cmnd *sc = task->sc;
34812352183SNicholas Bellinger 	struct iscsi_scsi_req *hdr;
34938d1c069SBoaz Harrosh 	unsigned hdrlength, cmd_len;
350262ef636SMike Christie 	itt_t itt;
351004d6530SBoaz Harrosh 	int rc;
3527996a778SMike Christie 
3535d12c05eSMike Christie 	rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
3545d12c05eSMike Christie 	if (rc)
3555d12c05eSMike Christie 		return rc;
3565d12c05eSMike Christie 
357184b57c6SMike Christie 	if (conn->session->tt->alloc_pdu) {
3582ff79d52SMike Christie 		rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
359577577daSMike Christie 		if (rc)
360577577daSMike Christie 			return rc;
361184b57c6SMike Christie 	}
36212352183SNicholas Bellinger 	hdr = (struct iscsi_scsi_req *)task->hdr;
363262ef636SMike Christie 	itt = hdr->itt;
364577577daSMike Christie 	memset(hdr, 0, sizeof(*hdr));
365577577daSMike Christie 
3662ff79d52SMike Christie 	if (session->tt->parse_pdu_itt)
3672ff79d52SMike Christie 		hdr->itt = task->hdr_itt = itt;
3682ff79d52SMike Christie 	else
3692ff79d52SMike Christie 		hdr->itt = task->hdr_itt = build_itt(task->itt,
3702ff79d52SMike Christie 						     task->conn->session->age);
3719c19a7d0SMike Christie 	task->hdr_len = 0;
3729c19a7d0SMike Christie 	rc = iscsi_add_hdr(task, sizeof(*hdr));
373004d6530SBoaz Harrosh 	if (rc)
374004d6530SBoaz Harrosh 		return rc;
3757996a778SMike Christie 	hdr->opcode = ISCSI_OP_SCSI_CMD;
3767996a778SMike Christie 	hdr->flags = ISCSI_ATTR_SIMPLE;
37755bdabdfSAndy Grover 	int_to_scsilun(sc->device->lun, &hdr->lun);
37855bdabdfSAndy Grover 	task->lun = hdr->lun;
3797996a778SMike Christie 	hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
38038d1c069SBoaz Harrosh 	cmd_len = sc->cmd_len;
38138d1c069SBoaz Harrosh 	if (cmd_len < ISCSI_CDB_SIZE)
38238d1c069SBoaz Harrosh 		memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
38338d1c069SBoaz Harrosh 	else if (cmd_len > ISCSI_CDB_SIZE) {
3849c19a7d0SMike Christie 		rc = iscsi_prep_ecdb_ahs(task);
38538d1c069SBoaz Harrosh 		if (rc)
38638d1c069SBoaz Harrosh 			return rc;
38738d1c069SBoaz Harrosh 		cmd_len = ISCSI_CDB_SIZE;
38838d1c069SBoaz Harrosh 	}
38938d1c069SBoaz Harrosh 	memcpy(hdr->cdb, sc->cmnd, cmd_len);
3907996a778SMike Christie 
3919c19a7d0SMike Christie 	task->imm_count = 0;
392c07d4444SBoaz Harrosh 	if (scsi_bidi_cmnd(sc)) {
393c07d4444SBoaz Harrosh 		hdr->flags |= ISCSI_FLAG_CMD_READ;
3949c19a7d0SMike Christie 		rc = iscsi_prep_bidi_ahs(task);
395c07d4444SBoaz Harrosh 		if (rc)
396c07d4444SBoaz Harrosh 			return rc;
397c07d4444SBoaz Harrosh 	}
3987996a778SMike Christie 	if (sc->sc_data_direction == DMA_TO_DEVICE) {
399c07d4444SBoaz Harrosh 		unsigned out_len = scsi_out(sc)->length;
400577577daSMike Christie 		struct iscsi_r2t_info *r2t = &task->unsol_r2t;
401577577daSMike Christie 
402c07d4444SBoaz Harrosh 		hdr->data_length = cpu_to_be32(out_len);
4037996a778SMike Christie 		hdr->flags |= ISCSI_FLAG_CMD_WRITE;
4047996a778SMike Christie 		/*
4057996a778SMike Christie 		 * Write counters:
4067996a778SMike Christie 		 *
4077996a778SMike Christie 		 *	imm_count	bytes to be sent right after
4087996a778SMike Christie 		 *			SCSI PDU Header
4097996a778SMike Christie 		 *
4107996a778SMike Christie 		 *	unsol_count	bytes(as Data-Out) to be sent
4117996a778SMike Christie 		 *			without	R2T ack right after
4127996a778SMike Christie 		 *			immediate data
4137996a778SMike Christie 		 *
414577577daSMike Christie 		 *	r2t data_length bytes to be sent via R2T ack's
4157996a778SMike Christie 		 *
4167996a778SMike Christie 		 *      pad_count       bytes to be sent as zero-padding
4177996a778SMike Christie 		 */
418577577daSMike Christie 		memset(r2t, 0, sizeof(*r2t));
4197996a778SMike Christie 
4207996a778SMike Christie 		if (session->imm_data_en) {
421c07d4444SBoaz Harrosh 			if (out_len >= session->first_burst)
4229c19a7d0SMike Christie 				task->imm_count = min(session->first_burst,
4237996a778SMike Christie 							conn->max_xmit_dlength);
4247996a778SMike Christie 			else
4259c19a7d0SMike Christie 				task->imm_count = min(out_len,
4267996a778SMike Christie 							conn->max_xmit_dlength);
4279c19a7d0SMike Christie 			hton24(hdr->dlength, task->imm_count);
4287996a778SMike Christie 		} else
429a8ac6311SOlaf Kirch 			zero_data(hdr->dlength);
4307996a778SMike Christie 
431ffd0436eSMike Christie 		if (!session->initial_r2t_en) {
432577577daSMike Christie 			r2t->data_length = min(session->first_burst, out_len) -
433577577daSMike Christie 					       task->imm_count;
434577577daSMike Christie 			r2t->data_offset = task->imm_count;
435577577daSMike Christie 			r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
436577577daSMike Christie 			r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
437ffd0436eSMike Christie 		}
438ffd0436eSMike Christie 
439577577daSMike Christie 		if (!task->unsol_r2t.data_length)
4407996a778SMike Christie 			/* No unsolicit Data-Out's */
441a8ac6311SOlaf Kirch 			hdr->flags |= ISCSI_FLAG_CMD_FINAL;
4427996a778SMike Christie 	} else {
4437996a778SMike Christie 		hdr->flags |= ISCSI_FLAG_CMD_FINAL;
4447996a778SMike Christie 		zero_data(hdr->dlength);
445c07d4444SBoaz Harrosh 		hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
4467996a778SMike Christie 
4477996a778SMike Christie 		if (sc->sc_data_direction == DMA_FROM_DEVICE)
4487996a778SMike Christie 			hdr->flags |= ISCSI_FLAG_CMD_READ;
4497996a778SMike Christie 	}
4507996a778SMike Christie 
451004d6530SBoaz Harrosh 	/* calculate size of additional header segments (AHSs) */
4529c19a7d0SMike Christie 	hdrlength = task->hdr_len - sizeof(*hdr);
453004d6530SBoaz Harrosh 
454004d6530SBoaz Harrosh 	WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
455004d6530SBoaz Harrosh 	hdrlength /= ISCSI_PAD_LEN;
456004d6530SBoaz Harrosh 
457004d6530SBoaz Harrosh 	WARN_ON(hdrlength >= 256);
458004d6530SBoaz Harrosh 	hdr->hlength = hdrlength & 0xFF;
45996b1f96dSMike Christie 	hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
460004d6530SBoaz Harrosh 
461577577daSMike Christie 	if (session->tt->init_task && session->tt->init_task(task))
462052d0144SMike Christie 		return -EIO;
463052d0144SMike Christie 
4649c19a7d0SMike Christie 	task->state = ISCSI_TASK_RUNNING;
465d3305f34SMike Christie 	session->cmdsn++;
46677a23c21SMike Christie 
467a8ac6311SOlaf Kirch 	conn->scsicmd_pdus_cnt++;
4681b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
4691b2c7af8SMike Christie 			  "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
4701b2c7af8SMike Christie 			  scsi_bidi_cmnd(sc) ? "bidirectional" :
4711b2c7af8SMike Christie 			  sc->sc_data_direction == DMA_TO_DEVICE ?
4721b2c7af8SMike Christie 			  "write" : "read", conn->id, sc, sc->cmnd[0],
4731b2c7af8SMike Christie 			  task->itt, scsi_bufflen(sc),
4743e5c28adSMike Christie 			  scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
4751b2c7af8SMike Christie 			  session->cmdsn,
4761b2c7af8SMike Christie 			  session->max_cmdsn - session->exp_cmdsn + 1);
477004d6530SBoaz Harrosh 	return 0;
4787996a778SMike Christie }
4797996a778SMike Christie 
4807996a778SMike Christie /**
4813bbaaad9SMike Christie  * iscsi_free_task - free a task
4829c19a7d0SMike Christie  * @task: iscsi cmd task
4837996a778SMike Christie  *
4847996a778SMike Christie  * Must be called with session lock.
4853e5c28adSMike Christie  * This function returns the scsi command to scsi-ml or cleans
4863e5c28adSMike Christie  * up mgmt tasks then returns the task to the pool.
4877996a778SMike Christie  */
4883bbaaad9SMike Christie static void iscsi_free_task(struct iscsi_task *task)
4897996a778SMike Christie {
4909c19a7d0SMike Christie 	struct iscsi_conn *conn = task->conn;
491c1635cb7SMike Christie 	struct iscsi_session *session = conn->session;
4929c19a7d0SMike Christie 	struct scsi_cmnd *sc = task->sc;
493f41d4721SMike Christie 	int oldstate = task->state;
4947996a778SMike Christie 
4954421c9ebSMike Christie 	ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
4964421c9ebSMike Christie 			  task->itt, task->state, task->sc);
4974421c9ebSMike Christie 
498577577daSMike Christie 	session->tt->cleanup_task(task);
4993bbaaad9SMike Christie 	task->state = ISCSI_TASK_FREE;
5009c19a7d0SMike Christie 	task->sc = NULL;
5013e5c28adSMike Christie 	/*
5029c19a7d0SMike Christie 	 * login task is preallocated so do not free
5033e5c28adSMike Christie 	 */
5049c19a7d0SMike Christie 	if (conn->login_task == task)
5053e5c28adSMike Christie 		return;
5063e5c28adSMike Christie 
5077acd72ebSStefani Seibold 	kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
5083e5c28adSMike Christie 
5093e5c28adSMike Christie 	if (sc) {
510f47f2cf5SMike Christie 		/* SCSI eh reuses commands to verify us */
511f47f2cf5SMike Christie 		sc->SCp.ptr = NULL;
5123e5c28adSMike Christie 		/*
513f41d4721SMike Christie 		 * queue command may call this to free the task, so
514f41d4721SMike Christie 		 * it will decide how to return sc to scsi-ml.
5153e5c28adSMike Christie 		 */
516f41d4721SMike Christie 		if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
5177996a778SMike Christie 			sc->scsi_done(sc);
5187996a778SMike Christie 	}
5193e5c28adSMike Christie }
5207996a778SMike Christie 
521913e5bf4SMike Christie void __iscsi_get_task(struct iscsi_task *task)
52260ecebf5SMike Christie {
5239c19a7d0SMike Christie 	atomic_inc(&task->refcount);
52460ecebf5SMike Christie }
525913e5bf4SMike Christie EXPORT_SYMBOL_GPL(__iscsi_get_task);
52660ecebf5SMike Christie 
5278eea2f55SEddie Wai void __iscsi_put_task(struct iscsi_task *task)
52860ecebf5SMike Christie {
5299c19a7d0SMike Christie 	if (atomic_dec_and_test(&task->refcount))
5303bbaaad9SMike Christie 		iscsi_free_task(task);
53160ecebf5SMike Christie }
5328eea2f55SEddie Wai EXPORT_SYMBOL_GPL(__iscsi_put_task);
53360ecebf5SMike Christie 
5349c19a7d0SMike Christie void iscsi_put_task(struct iscsi_task *task)
5353e5c28adSMike Christie {
5369c19a7d0SMike Christie 	struct iscsi_session *session = task->conn->session;
5373e5c28adSMike Christie 
5383e5c28adSMike Christie 	spin_lock_bh(&session->lock);
5399c19a7d0SMike Christie 	__iscsi_put_task(task);
5403e5c28adSMike Christie 	spin_unlock_bh(&session->lock);
5413e5c28adSMike Christie }
5429c19a7d0SMike Christie EXPORT_SYMBOL_GPL(iscsi_put_task);
5433e5c28adSMike Christie 
5443bbaaad9SMike Christie /**
5453bbaaad9SMike Christie  * iscsi_complete_task - finish a task
5463bbaaad9SMike Christie  * @task: iscsi cmd task
547b3cd5050SMike Christie  * @state: state to complete task with
5483bbaaad9SMike Christie  *
5493bbaaad9SMike Christie  * Must be called with session lock.
550b3a7ea8dSMike Christie  */
551b3cd5050SMike Christie static void iscsi_complete_task(struct iscsi_task *task, int state)
552b3a7ea8dSMike Christie {
5533bbaaad9SMike Christie 	struct iscsi_conn *conn = task->conn;
5543bbaaad9SMike Christie 
5554421c9ebSMike Christie 	ISCSI_DBG_SESSION(conn->session,
5564421c9ebSMike Christie 			  "complete task itt 0x%x state %d sc %p\n",
5574421c9ebSMike Christie 			  task->itt, task->state, task->sc);
558b3cd5050SMike Christie 	if (task->state == ISCSI_TASK_COMPLETED ||
559b3cd5050SMike Christie 	    task->state == ISCSI_TASK_ABRT_TMF ||
560f41d4721SMike Christie 	    task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
561f41d4721SMike Christie 	    task->state == ISCSI_TASK_REQUEUE_SCSIQ)
5623bbaaad9SMike Christie 		return;
5633bbaaad9SMike Christie 	WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
564b3cd5050SMike Christie 	task->state = state;
5653bbaaad9SMike Christie 
5663bbaaad9SMike Christie 	if (!list_empty(&task->running))
5673bbaaad9SMike Christie 		list_del_init(&task->running);
5683bbaaad9SMike Christie 
5693bbaaad9SMike Christie 	if (conn->task == task)
5703bbaaad9SMike Christie 		conn->task = NULL;
5713bbaaad9SMike Christie 
5723bbaaad9SMike Christie 	if (conn->ping_task == task)
5733bbaaad9SMike Christie 		conn->ping_task = NULL;
5743bbaaad9SMike Christie 
5753bbaaad9SMike Christie 	/* release get from queueing */
5763bbaaad9SMike Christie 	__iscsi_put_task(task);
5773bbaaad9SMike Christie }
5783bbaaad9SMike Christie 
5794c0ba5d2SMike Christie /**
5804c0ba5d2SMike Christie  * iscsi_complete_scsi_task - finish scsi task normally
5814c0ba5d2SMike Christie  * @task: iscsi task for scsi cmd
5824c0ba5d2SMike Christie  * @exp_cmdsn: expected cmd sn in cpu format
5834c0ba5d2SMike Christie  * @max_cmdsn: max cmd sn in cpu format
5844c0ba5d2SMike Christie  *
5854c0ba5d2SMike Christie  * This is used when drivers do not need or cannot perform
5864c0ba5d2SMike Christie  * lower level pdu processing.
5874c0ba5d2SMike Christie  *
5884c0ba5d2SMike Christie  * Called with session lock
5894c0ba5d2SMike Christie  */
5904c0ba5d2SMike Christie void iscsi_complete_scsi_task(struct iscsi_task *task,
5914c0ba5d2SMike Christie 			      uint32_t exp_cmdsn, uint32_t max_cmdsn)
5924c0ba5d2SMike Christie {
5934c0ba5d2SMike Christie 	struct iscsi_conn *conn = task->conn;
5944c0ba5d2SMike Christie 
5954c0ba5d2SMike Christie 	ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
5964c0ba5d2SMike Christie 
5974c0ba5d2SMike Christie 	conn->last_recv = jiffies;
5984c0ba5d2SMike Christie 	__iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
5994c0ba5d2SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
6004c0ba5d2SMike Christie }
6014c0ba5d2SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
6024c0ba5d2SMike Christie 
6034c0ba5d2SMike Christie 
6043bbaaad9SMike Christie /*
6053bbaaad9SMike Christie  * session lock must be held and if not called for a task that is
6063bbaaad9SMike Christie  * still pending or from the xmit thread, then xmit thread must
6073bbaaad9SMike Christie  * be suspended.
6083bbaaad9SMike Christie  */
6093bbaaad9SMike Christie static void fail_scsi_task(struct iscsi_task *task, int err)
6103bbaaad9SMike Christie {
6113bbaaad9SMike Christie 	struct iscsi_conn *conn = task->conn;
612b3a7ea8dSMike Christie 	struct scsi_cmnd *sc;
613b3cd5050SMike Christie 	int state;
614b3a7ea8dSMike Christie 
6153bbaaad9SMike Christie 	/*
6163bbaaad9SMike Christie 	 * if a command completes and we get a successful tmf response
6173bbaaad9SMike Christie 	 * we will hit this because the scsi eh abort code does not take
6183bbaaad9SMike Christie 	 * a ref to the task.
6193bbaaad9SMike Christie 	 */
6209c19a7d0SMike Christie 	sc = task->sc;
621b3a7ea8dSMike Christie 	if (!sc)
622b3a7ea8dSMike Christie 		return;
623b3a7ea8dSMike Christie 
624b3cd5050SMike Christie 	if (task->state == ISCSI_TASK_PENDING) {
625b3a7ea8dSMike Christie 		/*
626b3a7ea8dSMike Christie 		 * cmd never made it to the xmit thread, so we should not count
627b3a7ea8dSMike Christie 		 * the cmd in the sequencing
628b3a7ea8dSMike Christie 		 */
629b3a7ea8dSMike Christie 		conn->session->queued_cmdsn--;
630b3cd5050SMike Christie 		/* it was never sent so just complete like normal */
631b3cd5050SMike Christie 		state = ISCSI_TASK_COMPLETED;
632b3cd5050SMike Christie 	} else if (err == DID_TRANSPORT_DISRUPTED)
633b3cd5050SMike Christie 		state = ISCSI_TASK_ABRT_SESS_RECOV;
634b3cd5050SMike Christie 	else
635b3cd5050SMike Christie 		state = ISCSI_TASK_ABRT_TMF;
636b3a7ea8dSMike Christie 
637b3cd5050SMike Christie 	sc->result = err << 16;
638c07d4444SBoaz Harrosh 	if (!scsi_bidi_cmnd(sc))
639b3a7ea8dSMike Christie 		scsi_set_resid(sc, scsi_bufflen(sc));
640c07d4444SBoaz Harrosh 	else {
641c07d4444SBoaz Harrosh 		scsi_out(sc)->resid = scsi_out(sc)->length;
642c07d4444SBoaz Harrosh 		scsi_in(sc)->resid = scsi_in(sc)->length;
643c07d4444SBoaz Harrosh 	}
6443e5c28adSMike Christie 
645b3cd5050SMike Christie 	iscsi_complete_task(task, state);
646b3a7ea8dSMike Christie }
647b3a7ea8dSMike Christie 
6483e5c28adSMike Christie static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
6499c19a7d0SMike Christie 				struct iscsi_task *task)
650052d0144SMike Christie {
651052d0144SMike Christie 	struct iscsi_session *session = conn->session;
652577577daSMike Christie 	struct iscsi_hdr *hdr = task->hdr;
653052d0144SMike Christie 	struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
6544f704dc0SMike Christie 	uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
655052d0144SMike Christie 
656052d0144SMike Christie 	if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
657052d0144SMike Christie 		return -ENOTCONN;
658052d0144SMike Christie 
6594f704dc0SMike Christie 	if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
660052d0144SMike Christie 		nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
661052d0144SMike Christie 	/*
662052d0144SMike Christie 	 * pre-format CmdSN for outgoing PDU.
663052d0144SMike Christie 	 */
664052d0144SMike Christie 	nop->cmdsn = cpu_to_be32(session->cmdsn);
665052d0144SMike Christie 	if (hdr->itt != RESERVED_ITT) {
666052d0144SMike Christie 		/*
6674f704dc0SMike Christie 		 * TODO: We always use immediate for normal session pdus.
668052d0144SMike Christie 		 * If we start to send tmfs or nops as non-immediate then
669052d0144SMike Christie 		 * we should start checking the cmdsn numbers for mgmt tasks.
6704f704dc0SMike Christie 		 *
6714f704dc0SMike Christie 		 * During discovery sessions iscsid sends TEXT as non immediate,
6724f704dc0SMike Christie 		 * but we always only send one PDU at a time.
673052d0144SMike Christie 		 */
674052d0144SMike Christie 		if (conn->c_stage == ISCSI_CONN_STARTED &&
675052d0144SMike Christie 		    !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
676052d0144SMike Christie 			session->queued_cmdsn++;
677052d0144SMike Christie 			session->cmdsn++;
678052d0144SMike Christie 		}
679052d0144SMike Christie 	}
680052d0144SMike Christie 
681ae15f801SMike Christie 	if (session->tt->init_task && session->tt->init_task(task))
682ae15f801SMike Christie 		return -EIO;
683052d0144SMike Christie 
684052d0144SMike Christie 	if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
685052d0144SMike Christie 		session->state = ISCSI_STATE_LOGGING_OUT;
686052d0144SMike Christie 
687577577daSMike Christie 	task->state = ISCSI_TASK_RUNNING;
6881b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
6891b2c7af8SMike Christie 			  "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
6901b2c7af8SMike Christie 			  hdr->itt, task->data_count);
691052d0144SMike Christie 	return 0;
692052d0144SMike Christie }
693052d0144SMike Christie 
6949c19a7d0SMike Christie static struct iscsi_task *
695f6d5180cSMike Christie __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
696f6d5180cSMike Christie 		      char *data, uint32_t data_size)
697f6d5180cSMike Christie {
698f6d5180cSMike Christie 	struct iscsi_session *session = conn->session;
6991336aed1SMike Christie 	struct iscsi_host *ihost = shost_priv(session->host);
7004f704dc0SMike Christie 	uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
7019c19a7d0SMike Christie 	struct iscsi_task *task;
702262ef636SMike Christie 	itt_t itt;
703f6d5180cSMike Christie 
704f6d5180cSMike Christie 	if (session->state == ISCSI_STATE_TERMINATE)
705f6d5180cSMike Christie 		return NULL;
706f6d5180cSMike Christie 
7074f704dc0SMike Christie 	if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
708f6d5180cSMike Christie 		/*
709f6d5180cSMike Christie 		 * Login and Text are sent serially, in
710f6d5180cSMike Christie 		 * request-followed-by-response sequence.
7113e5c28adSMike Christie 		 * Same task can be used. Same ITT must be used.
7123e5c28adSMike Christie 		 * Note that login_task is preallocated at conn_create().
713f6d5180cSMike Christie 		 */
7144f704dc0SMike Christie 		if (conn->login_task->state != ISCSI_TASK_FREE) {
7154f704dc0SMike Christie 			iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
7164f704dc0SMike Christie 					  "progress. Cannot start new task.\n");
7174f704dc0SMike Christie 			return NULL;
7184f704dc0SMike Christie 		}
7194f704dc0SMike Christie 
7209c19a7d0SMike Christie 		task = conn->login_task;
7214f704dc0SMike Christie 	} else {
72226013ad4SMike Christie 		if (session->state != ISCSI_STATE_LOGGED_IN)
72326013ad4SMike Christie 			return NULL;
72426013ad4SMike Christie 
725f6d5180cSMike Christie 		BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
726f6d5180cSMike Christie 		BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
727f6d5180cSMike Christie 
7287acd72ebSStefani Seibold 		if (!kfifo_out(&session->cmdpool.queue,
7299c19a7d0SMike Christie 				 (void*)&task, sizeof(void*)))
730f6d5180cSMike Christie 			return NULL;
731f6d5180cSMike Christie 	}
7323e5c28adSMike Christie 	/*
7333e5c28adSMike Christie 	 * released in complete pdu for task we expect a response for, and
7343e5c28adSMike Christie 	 * released by the lld when it has transmitted the task for
7353e5c28adSMike Christie 	 * pdus we do not expect a response for.
7363e5c28adSMike Christie 	 */
7379c19a7d0SMike Christie 	atomic_set(&task->refcount, 1);
7389c19a7d0SMike Christie 	task->conn = conn;
7399c19a7d0SMike Christie 	task->sc = NULL;
7403bbaaad9SMike Christie 	INIT_LIST_HEAD(&task->running);
7413bbaaad9SMike Christie 	task->state = ISCSI_TASK_PENDING;
742f6d5180cSMike Christie 
743f6d5180cSMike Christie 	if (data_size) {
7449c19a7d0SMike Christie 		memcpy(task->data, data, data_size);
7459c19a7d0SMike Christie 		task->data_count = data_size;
746f6d5180cSMike Christie 	} else
7479c19a7d0SMike Christie 		task->data_count = 0;
748f6d5180cSMike Christie 
749184b57c6SMike Christie 	if (conn->session->tt->alloc_pdu) {
7502ff79d52SMike Christie 		if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
751577577daSMike Christie 			iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
752577577daSMike Christie 					 "pdu for mgmt task.\n");
7533bbaaad9SMike Christie 			goto free_task;
754577577daSMike Christie 		}
755184b57c6SMike Christie 	}
756184b57c6SMike Christie 
757262ef636SMike Christie 	itt = task->hdr->itt;
758577577daSMike Christie 	task->hdr_len = sizeof(struct iscsi_hdr);
7599c19a7d0SMike Christie 	memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
760262ef636SMike Christie 
761262ef636SMike Christie 	if (hdr->itt != RESERVED_ITT) {
762262ef636SMike Christie 		if (session->tt->parse_pdu_itt)
763262ef636SMike Christie 			task->hdr->itt = itt;
764262ef636SMike Christie 		else
765262ef636SMike Christie 			task->hdr->itt = build_itt(task->itt,
766262ef636SMike Christie 						   task->conn->session->age);
767262ef636SMike Christie 	}
768262ef636SMike Christie 
7691336aed1SMike Christie 	if (!ihost->workq) {
770577577daSMike Christie 		if (iscsi_prep_mgmt_task(conn, task))
771577577daSMike Christie 			goto free_task;
772052d0144SMike Christie 
7739c19a7d0SMike Christie 		if (session->tt->xmit_task(task))
774577577daSMike Christie 			goto free_task;
7753bbaaad9SMike Christie 	} else {
7763bbaaad9SMike Christie 		list_add_tail(&task->running, &conn->mgmtqueue);
77732ae763eSMike Christie 		iscsi_conn_queue_work(conn);
7783bbaaad9SMike Christie 	}
779052d0144SMike Christie 
7809c19a7d0SMike Christie 	return task;
781577577daSMike Christie 
782577577daSMike Christie free_task:
783577577daSMike Christie 	__iscsi_put_task(task);
784577577daSMike Christie 	return NULL;
785f6d5180cSMike Christie }
786f6d5180cSMike Christie 
787f6d5180cSMike Christie int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
788f6d5180cSMike Christie 			char *data, uint32_t data_size)
789f6d5180cSMike Christie {
790f6d5180cSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
791f6d5180cSMike Christie 	struct iscsi_session *session = conn->session;
792f6d5180cSMike Christie 	int err = 0;
793f6d5180cSMike Christie 
794f6d5180cSMike Christie 	spin_lock_bh(&session->lock);
795f6d5180cSMike Christie 	if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
796f6d5180cSMike Christie 		err = -EPERM;
797f6d5180cSMike Christie 	spin_unlock_bh(&session->lock);
798f6d5180cSMike Christie 	return err;
799f6d5180cSMike Christie }
800f6d5180cSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
801f6d5180cSMike Christie 
8027996a778SMike Christie /**
8037996a778SMike Christie  * iscsi_cmd_rsp - SCSI Command Response processing
8047996a778SMike Christie  * @conn: iscsi connection
8057996a778SMike Christie  * @hdr: iscsi header
8069c19a7d0SMike Christie  * @task: scsi command task
8077996a778SMike Christie  * @data: cmd data buffer
8087996a778SMike Christie  * @datalen: len of buffer
8097996a778SMike Christie  *
8107996a778SMike Christie  * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
8119c19a7d0SMike Christie  * then completes the command and task.
8127996a778SMike Christie  **/
81377a23c21SMike Christie static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
8149c19a7d0SMike Christie 			       struct iscsi_task *task, char *data,
8157996a778SMike Christie 			       int datalen)
8167996a778SMike Christie {
81712352183SNicholas Bellinger 	struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
8187996a778SMike Christie 	struct iscsi_session *session = conn->session;
8199c19a7d0SMike Christie 	struct scsi_cmnd *sc = task->sc;
8207996a778SMike Christie 
82177a23c21SMike Christie 	iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
8227996a778SMike Christie 	conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
8237996a778SMike Christie 
8247996a778SMike Christie 	sc->result = (DID_OK << 16) | rhdr->cmd_status;
8257996a778SMike Christie 
8267996a778SMike Christie 	if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
8277996a778SMike Christie 		sc->result = DID_ERROR << 16;
8287996a778SMike Christie 		goto out;
8297996a778SMike Christie 	}
8307996a778SMike Christie 
8317996a778SMike Christie 	if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
8329b80cb4bSMike Christie 		uint16_t senselen;
8337996a778SMike Christie 
8347996a778SMike Christie 		if (datalen < 2) {
8357996a778SMike Christie invalid_datalen:
836322d739dSMike Christie 			iscsi_conn_printk(KERN_ERR,  conn,
837322d739dSMike Christie 					 "Got CHECK_CONDITION but invalid data "
838322d739dSMike Christie 					 "buffer size of %d\n", datalen);
8397996a778SMike Christie 			sc->result = DID_BAD_TARGET << 16;
8407996a778SMike Christie 			goto out;
8417996a778SMike Christie 		}
8427996a778SMike Christie 
8438f333991SHarvey Harrison 		senselen = get_unaligned_be16(data);
8447996a778SMike Christie 		if (datalen < senselen)
8457996a778SMike Christie 			goto invalid_datalen;
8467996a778SMike Christie 
8477996a778SMike Christie 		memcpy(sc->sense_buffer, data + 2,
8489b80cb4bSMike Christie 		       min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
8491b2c7af8SMike Christie 		ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
8501b2c7af8SMike Christie 				  min_t(uint16_t, senselen,
8511b2c7af8SMike Christie 				  SCSI_SENSE_BUFFERSIZE));
8527996a778SMike Christie 	}
8537996a778SMike Christie 
854c07d4444SBoaz Harrosh 	if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
855c07d4444SBoaz Harrosh 			   ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
856c07d4444SBoaz Harrosh 		int res_count = be32_to_cpu(rhdr->bi_residual_count);
857c07d4444SBoaz Harrosh 
858c07d4444SBoaz Harrosh 		if (scsi_bidi_cmnd(sc) && res_count > 0 &&
859c07d4444SBoaz Harrosh 				(rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
860c07d4444SBoaz Harrosh 				 res_count <= scsi_in(sc)->length))
861c07d4444SBoaz Harrosh 			scsi_in(sc)->resid = res_count;
862c07d4444SBoaz Harrosh 		else
863c07d4444SBoaz Harrosh 			sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
864c07d4444SBoaz Harrosh 	}
865c07d4444SBoaz Harrosh 
8667207fea4SBoaz Harrosh 	if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
8677207fea4SBoaz Harrosh 	                   ISCSI_FLAG_CMD_OVERFLOW)) {
8687996a778SMike Christie 		int res_count = be32_to_cpu(rhdr->residual_count);
8697996a778SMike Christie 
8707207fea4SBoaz Harrosh 		if (res_count > 0 &&
8717207fea4SBoaz Harrosh 		    (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
8727207fea4SBoaz Harrosh 		     res_count <= scsi_bufflen(sc)))
873c07d4444SBoaz Harrosh 			/* write side for bidi or uni-io set_resid */
8741c138991SFUJITA Tomonori 			scsi_set_resid(sc, res_count);
8757996a778SMike Christie 		else
8767996a778SMike Christie 			sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
877c07d4444SBoaz Harrosh 	}
8787996a778SMike Christie out:
8793bbaaad9SMike Christie 	ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
8801b2c7af8SMike Christie 			  sc, sc->result, task->itt);
8817996a778SMike Christie 	conn->scsirsp_pdus_cnt++;
882b3cd5050SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
8837996a778SMike Christie }
8847996a778SMike Christie 
8851d9edf02SMike Christie /**
8861d9edf02SMike Christie  * iscsi_data_in_rsp - SCSI Data-In Response processing
8871d9edf02SMike Christie  * @conn: iscsi connection
8881d9edf02SMike Christie  * @hdr:  iscsi pdu
8891d9edf02SMike Christie  * @task: scsi command task
8901d9edf02SMike Christie  **/
8911d9edf02SMike Christie static void
8921d9edf02SMike Christie iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
8931d9edf02SMike Christie 		  struct iscsi_task *task)
8941d9edf02SMike Christie {
8951d9edf02SMike Christie 	struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
8961d9edf02SMike Christie 	struct scsi_cmnd *sc = task->sc;
8971d9edf02SMike Christie 
8981d9edf02SMike Christie 	if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
8991d9edf02SMike Christie 		return;
9001d9edf02SMike Christie 
901edbc9aa0SMike Christie 	iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
9021d9edf02SMike Christie 	sc->result = (DID_OK << 16) | rhdr->cmd_status;
9031d9edf02SMike Christie 	conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
9041d9edf02SMike Christie 	if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
9051d9edf02SMike Christie 	                   ISCSI_FLAG_DATA_OVERFLOW)) {
9061d9edf02SMike Christie 		int res_count = be32_to_cpu(rhdr->residual_count);
9071d9edf02SMike Christie 
9081d9edf02SMike Christie 		if (res_count > 0 &&
9091d9edf02SMike Christie 		    (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
9101d9edf02SMike Christie 		     res_count <= scsi_in(sc)->length))
9111d9edf02SMike Christie 			scsi_in(sc)->resid = res_count;
9121d9edf02SMike Christie 		else
9131d9edf02SMike Christie 			sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
9141d9edf02SMike Christie 	}
9151d9edf02SMike Christie 
9163bbaaad9SMike Christie 	ISCSI_DBG_SESSION(conn->session, "data in with status done "
9173bbaaad9SMike Christie 			  "[sc %p res %d itt 0x%x]\n",
9183bbaaad9SMike Christie 			  sc, sc->result, task->itt);
9191d9edf02SMike Christie 	conn->scsirsp_pdus_cnt++;
920b3cd5050SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
9211d9edf02SMike Christie }
9221d9edf02SMike Christie 
9237ea8b828SMike Christie static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
9247ea8b828SMike Christie {
9257ea8b828SMike Christie 	struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
9267ea8b828SMike Christie 
9277ea8b828SMike Christie 	conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
9287ea8b828SMike Christie 	conn->tmfrsp_pdus_cnt++;
9297ea8b828SMike Christie 
930843c0a8aSMike Christie 	if (conn->tmf_state != TMF_QUEUED)
9317ea8b828SMike Christie 		return;
9327ea8b828SMike Christie 
9337ea8b828SMike Christie 	if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
934843c0a8aSMike Christie 		conn->tmf_state = TMF_SUCCESS;
9357ea8b828SMike Christie 	else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
936843c0a8aSMike Christie 		conn->tmf_state = TMF_NOT_FOUND;
9377ea8b828SMike Christie 	else
938843c0a8aSMike Christie 		conn->tmf_state = TMF_FAILED;
9397ea8b828SMike Christie 	wake_up(&conn->ehwait);
9407ea8b828SMike Christie }
9417ea8b828SMike Christie 
942f6d5180cSMike Christie static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
943f6d5180cSMike Christie {
944f6d5180cSMike Christie         struct iscsi_nopout hdr;
9459c19a7d0SMike Christie 	struct iscsi_task *task;
946f6d5180cSMike Christie 
9479c19a7d0SMike Christie 	if (!rhdr && conn->ping_task)
948f6d5180cSMike Christie 		return;
949f6d5180cSMike Christie 
950f6d5180cSMike Christie 	memset(&hdr, 0, sizeof(struct iscsi_nopout));
951f6d5180cSMike Christie 	hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
952f6d5180cSMike Christie 	hdr.flags = ISCSI_FLAG_CMD_FINAL;
953f6d5180cSMike Christie 
954f6d5180cSMike Christie 	if (rhdr) {
95555bdabdfSAndy Grover 		hdr.lun = rhdr->lun;
956f6d5180cSMike Christie 		hdr.ttt = rhdr->ttt;
957f6d5180cSMike Christie 		hdr.itt = RESERVED_ITT;
958f6d5180cSMike Christie 	} else
959f6d5180cSMike Christie 		hdr.ttt = RESERVED_ITT;
960f6d5180cSMike Christie 
9619c19a7d0SMike Christie 	task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
9629c19a7d0SMike Christie 	if (!task)
963322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
964d3acf022SMike Christie 	else if (!rhdr) {
965d3acf022SMike Christie 		/* only track our nops */
966d3acf022SMike Christie 		conn->ping_task = task;
967d3acf022SMike Christie 		conn->last_ping = jiffies;
968d3acf022SMike Christie 	}
969f6d5180cSMike Christie }
970f6d5180cSMike Christie 
9718afa1439SMike Christie static int iscsi_nop_out_rsp(struct iscsi_task *task,
9728afa1439SMike Christie 			     struct iscsi_nopin *nop, char *data, int datalen)
9738afa1439SMike Christie {
9748afa1439SMike Christie 	struct iscsi_conn *conn = task->conn;
9758afa1439SMike Christie 	int rc = 0;
9768afa1439SMike Christie 
9778afa1439SMike Christie 	if (conn->ping_task != task) {
9788afa1439SMike Christie 		/*
9798afa1439SMike Christie 		 * If this is not in response to one of our
9808afa1439SMike Christie 		 * nops then it must be from userspace.
9818afa1439SMike Christie 		 */
9828afa1439SMike Christie 		if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
9838afa1439SMike Christie 				   data, datalen))
9848afa1439SMike Christie 			rc = ISCSI_ERR_CONN_FAILED;
9858afa1439SMike Christie 	} else
9868afa1439SMike Christie 		mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
9878afa1439SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
9888afa1439SMike Christie 	return rc;
9898afa1439SMike Christie }
9908afa1439SMike Christie 
99162f38300SMike Christie static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
99262f38300SMike Christie 			       char *data, int datalen)
99362f38300SMike Christie {
99462f38300SMike Christie 	struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
99562f38300SMike Christie 	struct iscsi_hdr rejected_pdu;
9968afa1439SMike Christie 	int opcode, rc = 0;
99762f38300SMike Christie 
99862f38300SMike Christie 	conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
99962f38300SMike Christie 
10008afa1439SMike Christie 	if (ntoh24(reject->dlength) > datalen ||
10018afa1439SMike Christie 	    ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
10028afa1439SMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
10038afa1439SMike Christie 				  "pdu. Invalid data length (pdu dlength "
10048afa1439SMike Christie 				  "%u, datalen %d\n", ntoh24(reject->dlength),
10058afa1439SMike Christie 				  datalen);
100662f38300SMike Christie 		return ISCSI_ERR_PROTO;
10078afa1439SMike Christie 	}
100862f38300SMike Christie 	memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
10098afa1439SMike Christie 	opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
10108afa1439SMike Christie 
10118afa1439SMike Christie 	switch (reject->reason) {
10128afa1439SMike Christie 	case ISCSI_REASON_DATA_DIGEST_ERROR:
1013322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
10148afa1439SMike Christie 				  "pdu (op 0x%x itt 0x%x) rejected "
1015262ef636SMike Christie 				  "due to DataDigest error.\n",
1016*e5dbbe27SVaughan Cao 				  opcode, rejected_pdu.itt);
10178afa1439SMike Christie 		break;
10188afa1439SMike Christie 	case ISCSI_REASON_IMM_CMD_REJECT:
10198afa1439SMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
10208afa1439SMike Christie 				  "pdu (op 0x%x itt 0x%x) rejected. Too many "
10218afa1439SMike Christie 				  "immediate commands.\n",
1022*e5dbbe27SVaughan Cao 				  opcode, rejected_pdu.itt);
10238afa1439SMike Christie 		/*
10248afa1439SMike Christie 		 * We only send one TMF at a time so if the target could not
10258afa1439SMike Christie 		 * handle it, then it should get fixed (RFC mandates that
10268afa1439SMike Christie 		 * a target can handle one immediate TMF per conn).
10278afa1439SMike Christie 		 *
10288afa1439SMike Christie 		 * For nops-outs, we could have sent more than one if
10298afa1439SMike Christie 		 * the target is sending us lots of nop-ins
10308afa1439SMike Christie 		 */
10318afa1439SMike Christie 		if (opcode != ISCSI_OP_NOOP_OUT)
103262f38300SMike Christie 			return 0;
10338afa1439SMike Christie 
10348afa1439SMike Christie 		 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG))
10358afa1439SMike Christie 			/*
10368afa1439SMike Christie 			 * nop-out in response to target's nop-out rejected.
10378afa1439SMike Christie 			 * Just resend.
10388afa1439SMike Christie 			 */
10398afa1439SMike Christie 			iscsi_send_nopout(conn,
10408afa1439SMike Christie 					  (struct iscsi_nopin*)&rejected_pdu);
10418afa1439SMike Christie 		else {
10428afa1439SMike Christie 			struct iscsi_task *task;
10438afa1439SMike Christie 			/*
10448afa1439SMike Christie 			 * Our nop as ping got dropped. We know the target
10458afa1439SMike Christie 			 * and transport are ok so just clean up
10468afa1439SMike Christie 			 */
10478afa1439SMike Christie 			task = iscsi_itt_to_task(conn, rejected_pdu.itt);
10488afa1439SMike Christie 			if (!task) {
10498afa1439SMike Christie 				iscsi_conn_printk(KERN_ERR, conn,
10508afa1439SMike Christie 						 "Invalid pdu reject. Could "
10518afa1439SMike Christie 						 "not lookup rejected task.\n");
10528afa1439SMike Christie 				rc = ISCSI_ERR_BAD_ITT;
10538afa1439SMike Christie 			} else
10548afa1439SMike Christie 				rc = iscsi_nop_out_rsp(task,
10558afa1439SMike Christie 					(struct iscsi_nopin*)&rejected_pdu,
10568afa1439SMike Christie 					NULL, 0);
10578afa1439SMike Christie 		}
10588afa1439SMike Christie 		break;
10598afa1439SMike Christie 	default:
10608afa1439SMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
10618afa1439SMike Christie 				  "pdu (op 0x%x itt 0x%x) rejected. Reason "
1062*e5dbbe27SVaughan Cao 				  "code 0x%x\n", rejected_pdu.opcode,
1063*e5dbbe27SVaughan Cao 				  rejected_pdu.itt, reject->reason);
10648afa1439SMike Christie 		break;
10658afa1439SMike Christie 	}
10668afa1439SMike Christie 	return rc;
106762f38300SMike Christie }
106862f38300SMike Christie 
10697996a778SMike Christie /**
1070913e5bf4SMike Christie  * iscsi_itt_to_task - look up task by itt
1071913e5bf4SMike Christie  * @conn: iscsi connection
1072913e5bf4SMike Christie  * @itt: itt
1073913e5bf4SMike Christie  *
1074913e5bf4SMike Christie  * This should be used for mgmt tasks like login and nops, or if
1075913e5bf4SMike Christie  * the LDD's itt space does not include the session age.
1076913e5bf4SMike Christie  *
1077913e5bf4SMike Christie  * The session lock must be held.
1078913e5bf4SMike Christie  */
10798f9256ceSMike Christie struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
1080913e5bf4SMike Christie {
1081913e5bf4SMike Christie 	struct iscsi_session *session = conn->session;
1082262ef636SMike Christie 	int i;
1083913e5bf4SMike Christie 
1084913e5bf4SMike Christie 	if (itt == RESERVED_ITT)
1085913e5bf4SMike Christie 		return NULL;
1086913e5bf4SMike Christie 
1087262ef636SMike Christie 	if (session->tt->parse_pdu_itt)
1088262ef636SMike Christie 		session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1089262ef636SMike Christie 	else
1090913e5bf4SMike Christie 		i = get_itt(itt);
1091913e5bf4SMike Christie 	if (i >= session->cmds_max)
1092913e5bf4SMike Christie 		return NULL;
1093913e5bf4SMike Christie 
1094913e5bf4SMike Christie 	return session->cmds[i];
1095913e5bf4SMike Christie }
10968f9256ceSMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
1097913e5bf4SMike Christie 
1098913e5bf4SMike Christie /**
10997996a778SMike Christie  * __iscsi_complete_pdu - complete pdu
11007996a778SMike Christie  * @conn: iscsi conn
11017996a778SMike Christie  * @hdr: iscsi header
11027996a778SMike Christie  * @data: data buffer
11037996a778SMike Christie  * @datalen: len of data buffer
11047996a778SMike Christie  *
11057996a778SMike Christie  * Completes pdu processing by freeing any resources allocated at
11067996a778SMike Christie  * queuecommand or send generic. session lock must be held and verify
11077996a778SMike Christie  * itt must have been called.
11087996a778SMike Christie  */
1109913e5bf4SMike Christie int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
11107996a778SMike Christie 			 char *data, int datalen)
11117996a778SMike Christie {
11127996a778SMike Christie 	struct iscsi_session *session = conn->session;
11137996a778SMike Christie 	int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
11149c19a7d0SMike Christie 	struct iscsi_task *task;
11157996a778SMike Christie 	uint32_t itt;
11167996a778SMike Christie 
1117f6d5180cSMike Christie 	conn->last_recv = jiffies;
11180af967f5SMike Christie 	rc = iscsi_verify_itt(conn, hdr->itt);
11190af967f5SMike Christie 	if (rc)
11200af967f5SMike Christie 		return rc;
11210af967f5SMike Christie 
1122b4377356SAl Viro 	if (hdr->itt != RESERVED_ITT)
1123b4377356SAl Viro 		itt = get_itt(hdr->itt);
11247996a778SMike Christie 	else
1125b4377356SAl Viro 		itt = ~0U;
11267996a778SMike Christie 
11271b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
11283e5c28adSMike Christie 			  opcode, conn->id, itt, datalen);
11297996a778SMike Christie 
11303e5c28adSMike Christie 	if (itt == ~0U) {
113177a23c21SMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
113262f38300SMike Christie 
11337996a778SMike Christie 		switch(opcode) {
11347996a778SMike Christie 		case ISCSI_OP_NOOP_IN:
113540527afeSMike Christie 			if (datalen) {
113640527afeSMike Christie 				rc = ISCSI_ERR_PROTO;
113740527afeSMike Christie 				break;
113840527afeSMike Christie 			}
113940527afeSMike Christie 
1140b4377356SAl Viro 			if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
114140527afeSMike Christie 				break;
114240527afeSMike Christie 
1143f6d5180cSMike Christie 			iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
11447996a778SMike Christie 			break;
11457996a778SMike Christie 		case ISCSI_OP_REJECT:
114662f38300SMike Christie 			rc = iscsi_handle_reject(conn, hdr, data, datalen);
114762f38300SMike Christie 			break;
11487996a778SMike Christie 		case ISCSI_OP_ASYNC_EVENT:
11498d2860b3SMike Christie 			conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
11505831c737SMike Christie 			if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
11515831c737SMike Christie 				rc = ISCSI_ERR_CONN_FAILED;
11527996a778SMike Christie 			break;
11537996a778SMike Christie 		default:
11547996a778SMike Christie 			rc = ISCSI_ERR_BAD_OPCODE;
11557996a778SMike Christie 			break;
11567996a778SMike Christie 		}
11573e5c28adSMike Christie 		goto out;
11583e5c28adSMike Christie 	}
11597996a778SMike Christie 
11603e5c28adSMike Christie 	switch(opcode) {
11613e5c28adSMike Christie 	case ISCSI_OP_SCSI_CMD_RSP:
1162913e5bf4SMike Christie 	case ISCSI_OP_SCSI_DATA_IN:
1163913e5bf4SMike Christie 		task = iscsi_itt_to_ctask(conn, hdr->itt);
1164913e5bf4SMike Christie 		if (!task)
1165913e5bf4SMike Christie 			return ISCSI_ERR_BAD_ITT;
1166d355e57dSMike Christie 		task->last_xfer = jiffies;
11673e5c28adSMike Christie 		break;
1168913e5bf4SMike Christie 	case ISCSI_OP_R2T:
1169913e5bf4SMike Christie 		/*
1170913e5bf4SMike Christie 		 * LLD handles R2Ts if they need to.
1171913e5bf4SMike Christie 		 */
1172913e5bf4SMike Christie 		return 0;
1173913e5bf4SMike Christie 	case ISCSI_OP_LOGOUT_RSP:
1174913e5bf4SMike Christie 	case ISCSI_OP_LOGIN_RSP:
1175913e5bf4SMike Christie 	case ISCSI_OP_TEXT_RSP:
1176913e5bf4SMike Christie 	case ISCSI_OP_SCSI_TMFUNC_RSP:
1177913e5bf4SMike Christie 	case ISCSI_OP_NOOP_IN:
1178913e5bf4SMike Christie 		task = iscsi_itt_to_task(conn, hdr->itt);
1179913e5bf4SMike Christie 		if (!task)
1180913e5bf4SMike Christie 			return ISCSI_ERR_BAD_ITT;
1181913e5bf4SMike Christie 		break;
1182913e5bf4SMike Christie 	default:
1183913e5bf4SMike Christie 		return ISCSI_ERR_BAD_OPCODE;
11843e5c28adSMike Christie 	}
1185913e5bf4SMike Christie 
1186913e5bf4SMike Christie 	switch(opcode) {
1187913e5bf4SMike Christie 	case ISCSI_OP_SCSI_CMD_RSP:
11889c19a7d0SMike Christie 		iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
11893e5c28adSMike Christie 		break;
11903e5c28adSMike Christie 	case ISCSI_OP_SCSI_DATA_IN:
11911d9edf02SMike Christie 		iscsi_data_in_rsp(conn, hdr, task);
11923e5c28adSMike Christie 		break;
11933e5c28adSMike Christie 	case ISCSI_OP_LOGOUT_RSP:
11943e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
11953e5c28adSMike Christie 		if (datalen) {
11963e5c28adSMike Christie 			rc = ISCSI_ERR_PROTO;
11973e5c28adSMike Christie 			break;
11983e5c28adSMike Christie 		}
11993e5c28adSMike Christie 		conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
12003e5c28adSMike Christie 		goto recv_pdu;
12013e5c28adSMike Christie 	case ISCSI_OP_LOGIN_RSP:
12023e5c28adSMike Christie 	case ISCSI_OP_TEXT_RSP:
12033e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12043e5c28adSMike Christie 		/*
12053e5c28adSMike Christie 		 * login related PDU's exp_statsn is handled in
12063e5c28adSMike Christie 		 * userspace
12073e5c28adSMike Christie 		 */
12083e5c28adSMike Christie 		goto recv_pdu;
12093e5c28adSMike Christie 	case ISCSI_OP_SCSI_TMFUNC_RSP:
12103e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12113e5c28adSMike Christie 		if (datalen) {
12123e5c28adSMike Christie 			rc = ISCSI_ERR_PROTO;
12133e5c28adSMike Christie 			break;
12143e5c28adSMike Christie 		}
12153e5c28adSMike Christie 
12163e5c28adSMike Christie 		iscsi_tmf_rsp(conn, hdr);
1217b3cd5050SMike Christie 		iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
12183e5c28adSMike Christie 		break;
12193e5c28adSMike Christie 	case ISCSI_OP_NOOP_IN:
12203e5c28adSMike Christie 		iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
12213e5c28adSMike Christie 		if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
12223e5c28adSMike Christie 			rc = ISCSI_ERR_PROTO;
12233e5c28adSMike Christie 			break;
12243e5c28adSMike Christie 		}
12253e5c28adSMike Christie 		conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
12263e5c28adSMike Christie 
12278afa1439SMike Christie 		rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
12288afa1439SMike Christie 				       data, datalen);
12293e5c28adSMike Christie 		break;
12303e5c28adSMike Christie 	default:
12313e5c28adSMike Christie 		rc = ISCSI_ERR_BAD_OPCODE;
12323e5c28adSMike Christie 		break;
12333e5c28adSMike Christie 	}
12343e5c28adSMike Christie 
12353e5c28adSMike Christie out:
12363e5c28adSMike Christie 	return rc;
12373e5c28adSMike Christie recv_pdu:
12383e5c28adSMike Christie 	if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
12393e5c28adSMike Christie 		rc = ISCSI_ERR_CONN_FAILED;
1240b3cd5050SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
12417996a778SMike Christie 	return rc;
12427996a778SMike Christie }
1243913e5bf4SMike Christie EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
12447996a778SMike Christie 
12457996a778SMike Christie int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
12467996a778SMike Christie 		       char *data, int datalen)
12477996a778SMike Christie {
12487996a778SMike Christie 	int rc;
12497996a778SMike Christie 
12507996a778SMike Christie 	spin_lock(&conn->session->lock);
12517996a778SMike Christie 	rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
12527996a778SMike Christie 	spin_unlock(&conn->session->lock);
12537996a778SMike Christie 	return rc;
12547996a778SMike Christie }
12557996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
12567996a778SMike Christie 
12570af967f5SMike Christie int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
12587996a778SMike Christie {
12597996a778SMike Christie 	struct iscsi_session *session = conn->session;
1260262ef636SMike Christie 	int age = 0, i = 0;
12617996a778SMike Christie 
12620af967f5SMike Christie 	if (itt == RESERVED_ITT)
12630af967f5SMike Christie 		return 0;
12640af967f5SMike Christie 
1265262ef636SMike Christie 	if (session->tt->parse_pdu_itt)
1266262ef636SMike Christie 		session->tt->parse_pdu_itt(conn, itt, &i, &age);
1267262ef636SMike Christie 	else {
1268262ef636SMike Christie 		i = get_itt(itt);
1269262ef636SMike Christie 		age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1270262ef636SMike Christie 	}
1271262ef636SMike Christie 
1272262ef636SMike Christie 	if (age != session->age) {
1273322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
12740af967f5SMike Christie 				  "received itt %x expected session age (%x)\n",
1275913e5bf4SMike Christie 				  (__force u32)itt, session->age);
12767996a778SMike Christie 		return ISCSI_ERR_BAD_ITT;
12777996a778SMike Christie 	}
12787996a778SMike Christie 
12793e5c28adSMike Christie 	if (i >= session->cmds_max) {
12803e5c28adSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
12813e5c28adSMike Christie 				  "received invalid itt index %u (max cmds "
12823e5c28adSMike Christie 				   "%u.\n", i, session->cmds_max);
12833e5c28adSMike Christie 		return ISCSI_ERR_BAD_ITT;
12847996a778SMike Christie 	}
12857996a778SMike Christie 	return 0;
12867996a778SMike Christie }
12877996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_verify_itt);
12887996a778SMike Christie 
1289913e5bf4SMike Christie /**
1290913e5bf4SMike Christie  * iscsi_itt_to_ctask - look up ctask by itt
1291913e5bf4SMike Christie  * @conn: iscsi connection
1292913e5bf4SMike Christie  * @itt: itt
1293913e5bf4SMike Christie  *
1294913e5bf4SMike Christie  * This should be used for cmd tasks.
1295913e5bf4SMike Christie  *
1296913e5bf4SMike Christie  * The session lock must be held.
1297913e5bf4SMike Christie  */
1298913e5bf4SMike Christie struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
12990af967f5SMike Christie {
13009c19a7d0SMike Christie 	struct iscsi_task *task;
13010af967f5SMike Christie 
13020af967f5SMike Christie 	if (iscsi_verify_itt(conn, itt))
13030af967f5SMike Christie 		return NULL;
13040af967f5SMike Christie 
1305913e5bf4SMike Christie 	task = iscsi_itt_to_task(conn, itt);
1306913e5bf4SMike Christie 	if (!task || !task->sc)
13070af967f5SMike Christie 		return NULL;
13080af967f5SMike Christie 
1309913e5bf4SMike Christie 	if (task->sc->SCp.phase != conn->session->age) {
1310913e5bf4SMike Christie 		iscsi_session_printk(KERN_ERR, conn->session,
1311913e5bf4SMike Christie 				  "task's session age %d, expected %d\n",
1312913e5bf4SMike Christie 				  task->sc->SCp.phase, conn->session->age);
13130af967f5SMike Christie 		return NULL;
1314913e5bf4SMike Christie 	}
13150af967f5SMike Christie 
13169c19a7d0SMike Christie 	return task;
13170af967f5SMike Christie }
13180af967f5SMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
13190af967f5SMike Christie 
132040a06e75SMike Christie void iscsi_session_failure(struct iscsi_session *session,
1321e5bd7b54SMike Christie 			   enum iscsi_err err)
1322e5bd7b54SMike Christie {
1323e5bd7b54SMike Christie 	struct iscsi_conn *conn;
1324e5bd7b54SMike Christie 	struct device *dev;
1325e5bd7b54SMike Christie 
1326bfcf72e4SMike Christie 	spin_lock_bh(&session->lock);
1327e5bd7b54SMike Christie 	conn = session->leadconn;
1328e5bd7b54SMike Christie 	if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1329bfcf72e4SMike Christie 		spin_unlock_bh(&session->lock);
1330e5bd7b54SMike Christie 		return;
1331e5bd7b54SMike Christie 	}
1332e5bd7b54SMike Christie 
1333e5bd7b54SMike Christie 	dev = get_device(&conn->cls_conn->dev);
1334bfcf72e4SMike Christie 	spin_unlock_bh(&session->lock);
1335e5bd7b54SMike Christie 	if (!dev)
1336e5bd7b54SMike Christie 	        return;
1337e5bd7b54SMike Christie 	/*
1338e5bd7b54SMike Christie 	 * if the host is being removed bypass the connection
1339e5bd7b54SMike Christie 	 * recovery initialization because we are going to kill
1340e5bd7b54SMike Christie 	 * the session.
1341e5bd7b54SMike Christie 	 */
1342e5bd7b54SMike Christie 	if (err == ISCSI_ERR_INVALID_HOST)
1343e5bd7b54SMike Christie 		iscsi_conn_error_event(conn->cls_conn, err);
1344e5bd7b54SMike Christie 	else
1345e5bd7b54SMike Christie 		iscsi_conn_failure(conn, err);
1346e5bd7b54SMike Christie 	put_device(dev);
1347e5bd7b54SMike Christie }
1348e5bd7b54SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_failure);
1349e5bd7b54SMike Christie 
13507996a778SMike Christie void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
13517996a778SMike Christie {
13527996a778SMike Christie 	struct iscsi_session *session = conn->session;
13537996a778SMike Christie 
1354bfcf72e4SMike Christie 	spin_lock_bh(&session->lock);
1355656cffc9SMike Christie 	if (session->state == ISCSI_STATE_FAILED) {
1356bfcf72e4SMike Christie 		spin_unlock_bh(&session->lock);
1357656cffc9SMike Christie 		return;
1358656cffc9SMike Christie 	}
1359656cffc9SMike Christie 
136067a61114SMike Christie 	if (conn->stop_stage == 0)
13617996a778SMike Christie 		session->state = ISCSI_STATE_FAILED;
1362bfcf72e4SMike Christie 	spin_unlock_bh(&session->lock);
1363e5bd7b54SMike Christie 
13647996a778SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
13657996a778SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1366e5bd7b54SMike Christie 	iscsi_conn_error_event(conn->cls_conn, err);
13677996a778SMike Christie }
13687996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_failure);
13697996a778SMike Christie 
137077a23c21SMike Christie static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
137177a23c21SMike Christie {
137277a23c21SMike Christie 	struct iscsi_session *session = conn->session;
137377a23c21SMike Christie 
137477a23c21SMike Christie 	/*
137577a23c21SMike Christie 	 * Check for iSCSI window and take care of CmdSN wrap-around
137677a23c21SMike Christie 	 */
1377e0726407SMike Christie 	if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
13781b2c7af8SMike Christie 		ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
13791b2c7af8SMike Christie 				  "%u MaxCmdSN %u CmdSN %u/%u\n",
13801b2c7af8SMike Christie 				  session->exp_cmdsn, session->max_cmdsn,
13811b2c7af8SMike Christie 				  session->cmdsn, session->queued_cmdsn);
138277a23c21SMike Christie 		return -ENOSPC;
138377a23c21SMike Christie 	}
138477a23c21SMike Christie 	return 0;
138577a23c21SMike Christie }
138677a23c21SMike Christie 
13879c19a7d0SMike Christie static int iscsi_xmit_task(struct iscsi_conn *conn)
138877a23c21SMike Christie {
13899c19a7d0SMike Christie 	struct iscsi_task *task = conn->task;
1390843c0a8aSMike Christie 	int rc;
139177a23c21SMike Christie 
139270b31c15SMike Christie 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
139370b31c15SMike Christie 		return -ENODATA;
139470b31c15SMike Christie 
13959c19a7d0SMike Christie 	__iscsi_get_task(task);
139677a23c21SMike Christie 	spin_unlock_bh(&conn->session->lock);
13979c19a7d0SMike Christie 	rc = conn->session->tt->xmit_task(task);
139877a23c21SMike Christie 	spin_lock_bh(&conn->session->lock);
1399d355e57dSMike Christie 	if (!rc) {
14009c19a7d0SMike Christie 		/* done with this task */
1401d355e57dSMike Christie 		task->last_xfer = jiffies;
14029c19a7d0SMike Christie 		conn->task = NULL;
1403d355e57dSMike Christie 	}
1404d355e57dSMike Christie 	__iscsi_put_task(task);
140577a23c21SMike Christie 	return rc;
140677a23c21SMike Christie }
140777a23c21SMike Christie 
14087996a778SMike Christie /**
14099c19a7d0SMike Christie  * iscsi_requeue_task - requeue task to run from session workqueue
14109c19a7d0SMike Christie  * @task: task to requeue
1411843c0a8aSMike Christie  *
14129c19a7d0SMike Christie  * LLDs that need to run a task from the session workqueue should call
1413052d0144SMike Christie  * this. The session lock must be held. This should only be called
1414052d0144SMike Christie  * by software drivers.
1415843c0a8aSMike Christie  */
14169c19a7d0SMike Christie void iscsi_requeue_task(struct iscsi_task *task)
1417843c0a8aSMike Christie {
14189c19a7d0SMike Christie 	struct iscsi_conn *conn = task->conn;
1419843c0a8aSMike Christie 
14203bbaaad9SMike Christie 	/*
14213bbaaad9SMike Christie 	 * this may be on the requeue list already if the xmit_task callout
14223bbaaad9SMike Christie 	 * is handling the r2ts while we are adding new ones
14233bbaaad9SMike Christie 	 */
14243bbaaad9SMike Christie 	if (list_empty(&task->running))
14253bbaaad9SMike Christie 		list_add_tail(&task->running, &conn->requeue);
142632ae763eSMike Christie 	iscsi_conn_queue_work(conn);
1427843c0a8aSMike Christie }
14289c19a7d0SMike Christie EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1429843c0a8aSMike Christie 
1430843c0a8aSMike Christie /**
14317996a778SMike Christie  * iscsi_data_xmit - xmit any command into the scheduled connection
14327996a778SMike Christie  * @conn: iscsi connection
14337996a778SMike Christie  *
14347996a778SMike Christie  * Notes:
14357996a778SMike Christie  *	The function can return -EAGAIN in which case the caller must
14367996a778SMike Christie  *	re-schedule it again later or recover. '0' return code means
14377996a778SMike Christie  *	successful xmit.
14387996a778SMike Christie  **/
14397996a778SMike Christie static int iscsi_data_xmit(struct iscsi_conn *conn)
14407996a778SMike Christie {
14415d12c05eSMike Christie 	struct iscsi_task *task;
14423219e529SMike Christie 	int rc = 0;
14437996a778SMike Christie 
144477a23c21SMike Christie 	spin_lock_bh(&conn->session->lock);
144570b31c15SMike Christie 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
14461b2c7af8SMike Christie 		ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
144777a23c21SMike Christie 		spin_unlock_bh(&conn->session->lock);
14483219e529SMike Christie 		return -ENODATA;
14497996a778SMike Christie 	}
14507996a778SMike Christie 
14519c19a7d0SMike Christie 	if (conn->task) {
14529c19a7d0SMike Christie 		rc = iscsi_xmit_task(conn);
14533219e529SMike Christie 	        if (rc)
145470b31c15SMike Christie 		        goto done;
14557996a778SMike Christie 	}
145677a23c21SMike Christie 
145777a23c21SMike Christie 	/*
145877a23c21SMike Christie 	 * process mgmt pdus like nops before commands since we should
145977a23c21SMike Christie 	 * only have one nop-out as a ping from us and targets should not
146077a23c21SMike Christie 	 * overflow us with nop-ins
146177a23c21SMike Christie 	 */
146277a23c21SMike Christie check_mgmt:
1463843c0a8aSMike Christie 	while (!list_empty(&conn->mgmtqueue)) {
14649c19a7d0SMike Christie 		conn->task = list_entry(conn->mgmtqueue.next,
14659c19a7d0SMike Christie 					 struct iscsi_task, running);
14663bbaaad9SMike Christie 		list_del_init(&conn->task->running);
14679c19a7d0SMike Christie 		if (iscsi_prep_mgmt_task(conn, conn->task)) {
14689c19a7d0SMike Christie 			__iscsi_put_task(conn->task);
14699c19a7d0SMike Christie 			conn->task = NULL;
1470b3a7ea8dSMike Christie 			continue;
1471b3a7ea8dSMike Christie 		}
14729c19a7d0SMike Christie 		rc = iscsi_xmit_task(conn);
14733219e529SMike Christie 		if (rc)
147470b31c15SMike Christie 			goto done;
14757996a778SMike Christie 	}
14767996a778SMike Christie 
1477843c0a8aSMike Christie 	/* process pending command queue */
14783bbaaad9SMike Christie 	while (!list_empty(&conn->cmdqueue)) {
14795d12c05eSMike Christie 		conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
14805d12c05eSMike Christie 					running);
14813bbaaad9SMike Christie 		list_del_init(&conn->task->running);
1482b3a7ea8dSMike Christie 		if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1483b3cd5050SMike Christie 			fail_scsi_task(conn->task, DID_IMM_RETRY);
1484b3a7ea8dSMike Christie 			continue;
1485b3a7ea8dSMike Christie 		}
1486577577daSMike Christie 		rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1487577577daSMike Christie 		if (rc) {
14885d12c05eSMike Christie 			if (rc == -ENOMEM || rc == -EACCES) {
14893bbaaad9SMike Christie 				list_add_tail(&conn->task->running,
14903bbaaad9SMike Christie 					      &conn->cmdqueue);
1491577577daSMike Christie 				conn->task = NULL;
149270b31c15SMike Christie 				goto done;
1493577577daSMike Christie 			} else
1494b3cd5050SMike Christie 				fail_scsi_task(conn->task, DID_ABORT);
1495004d6530SBoaz Harrosh 			continue;
1496004d6530SBoaz Harrosh 		}
14979c19a7d0SMike Christie 		rc = iscsi_xmit_task(conn);
14983219e529SMike Christie 		if (rc)
149970b31c15SMike Christie 			goto done;
150077a23c21SMike Christie 		/*
15019c19a7d0SMike Christie 		 * we could continuously get new task requests so
150277a23c21SMike Christie 		 * we need to check the mgmt queue for nops that need to
150377a23c21SMike Christie 		 * be sent to aviod starvation
150477a23c21SMike Christie 		 */
1505843c0a8aSMike Christie 		if (!list_empty(&conn->mgmtqueue))
1506843c0a8aSMike Christie 			goto check_mgmt;
1507843c0a8aSMike Christie 	}
1508843c0a8aSMike Christie 
1509843c0a8aSMike Christie 	while (!list_empty(&conn->requeue)) {
1510b3a7ea8dSMike Christie 		/*
1511b3a7ea8dSMike Christie 		 * we always do fastlogout - conn stop code will clean up.
1512b3a7ea8dSMike Christie 		 */
1513b3a7ea8dSMike Christie 		if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1514b3a7ea8dSMike Christie 			break;
1515b3a7ea8dSMike Christie 
15165d12c05eSMike Christie 		task = list_entry(conn->requeue.next, struct iscsi_task,
15175d12c05eSMike Christie 				  running);
15185d12c05eSMike Christie 		if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
15195d12c05eSMike Christie 			break;
15205d12c05eSMike Christie 
15215d12c05eSMike Christie 		conn->task = task;
15223bbaaad9SMike Christie 		list_del_init(&conn->task->running);
15239c19a7d0SMike Christie 		conn->task->state = ISCSI_TASK_RUNNING;
15249c19a7d0SMike Christie 		rc = iscsi_xmit_task(conn);
1525843c0a8aSMike Christie 		if (rc)
152670b31c15SMike Christie 			goto done;
1527843c0a8aSMike Christie 		if (!list_empty(&conn->mgmtqueue))
152877a23c21SMike Christie 			goto check_mgmt;
15297996a778SMike Christie 	}
153077a23c21SMike Christie 	spin_unlock_bh(&conn->session->lock);
15313219e529SMike Christie 	return -ENODATA;
15327996a778SMike Christie 
153370b31c15SMike Christie done:
153477a23c21SMike Christie 	spin_unlock_bh(&conn->session->lock);
15353219e529SMike Christie 	return rc;
15367996a778SMike Christie }
15377996a778SMike Christie 
1538c4028958SDavid Howells static void iscsi_xmitworker(struct work_struct *work)
15397996a778SMike Christie {
1540c4028958SDavid Howells 	struct iscsi_conn *conn =
1541c4028958SDavid Howells 		container_of(work, struct iscsi_conn, xmitwork);
15423219e529SMike Christie 	int rc;
15437996a778SMike Christie 	/*
15447996a778SMike Christie 	 * serialize Xmit worker on a per-connection basis.
15457996a778SMike Christie 	 */
15463219e529SMike Christie 	do {
15473219e529SMike Christie 		rc = iscsi_data_xmit(conn);
15483219e529SMike Christie 	} while (rc >= 0 || rc == -EAGAIN);
15497996a778SMike Christie }
15507996a778SMike Christie 
1551577577daSMike Christie static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1552577577daSMike Christie 						  struct scsi_cmnd *sc)
1553577577daSMike Christie {
1554577577daSMike Christie 	struct iscsi_task *task;
1555577577daSMike Christie 
15567acd72ebSStefani Seibold 	if (!kfifo_out(&conn->session->cmdpool.queue,
1557577577daSMike Christie 			 (void *) &task, sizeof(void *)))
1558577577daSMike Christie 		return NULL;
1559577577daSMike Christie 
1560577577daSMike Christie 	sc->SCp.phase = conn->session->age;
1561577577daSMike Christie 	sc->SCp.ptr = (char *) task;
1562577577daSMike Christie 
1563577577daSMike Christie 	atomic_set(&task->refcount, 1);
1564577577daSMike Christie 	task->state = ISCSI_TASK_PENDING;
1565577577daSMike Christie 	task->conn = conn;
1566577577daSMike Christie 	task->sc = sc;
1567d355e57dSMike Christie 	task->have_checked_conn = false;
1568d355e57dSMike Christie 	task->last_timeout = jiffies;
1569d355e57dSMike Christie 	task->last_xfer = jiffies;
1570577577daSMike Christie 	INIT_LIST_HEAD(&task->running);
1571577577daSMike Christie 	return task;
1572577577daSMike Christie }
1573577577daSMike Christie 
15747996a778SMike Christie enum {
15757996a778SMike Christie 	FAILURE_BAD_HOST = 1,
15767996a778SMike Christie 	FAILURE_SESSION_FAILED,
15777996a778SMike Christie 	FAILURE_SESSION_FREED,
15787996a778SMike Christie 	FAILURE_WINDOW_CLOSED,
157960ecebf5SMike Christie 	FAILURE_OOM,
15807996a778SMike Christie 	FAILURE_SESSION_TERMINATE,
1581656cffc9SMike Christie 	FAILURE_SESSION_IN_RECOVERY,
15827996a778SMike Christie 	FAILURE_SESSION_RECOVERY_TIMEOUT,
1583b3a7ea8dSMike Christie 	FAILURE_SESSION_LOGGING_OUT,
15846eabafbeSMike Christie 	FAILURE_SESSION_NOT_READY,
15857996a778SMike Christie };
15867996a778SMike Christie 
1587f41d4721SMike Christie int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
15887996a778SMike Christie {
158975613521SMike Christie 	struct iscsi_cls_session *cls_session;
15901336aed1SMike Christie 	struct iscsi_host *ihost;
15917996a778SMike Christie 	int reason = 0;
15927996a778SMike Christie 	struct iscsi_session *session;
15937996a778SMike Christie 	struct iscsi_conn *conn;
15949c19a7d0SMike Christie 	struct iscsi_task *task = NULL;
15957996a778SMike Christie 
15967996a778SMike Christie 	sc->result = 0;
1597f47f2cf5SMike Christie 	sc->SCp.ptr = NULL;
15987996a778SMike Christie 
15991336aed1SMike Christie 	ihost = shost_priv(host);
16007996a778SMike Christie 
160175613521SMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
160275613521SMike Christie 	session = cls_session->dd_data;
1603f41d4721SMike Christie 	spin_lock_bh(&session->lock);
16047996a778SMike Christie 
160575613521SMike Christie 	reason = iscsi_session_chkready(cls_session);
16066eabafbeSMike Christie 	if (reason) {
16076eabafbeSMike Christie 		sc->result = reason;
16086eabafbeSMike Christie 		goto fault;
16096eabafbeSMike Christie 	}
16106eabafbeSMike Christie 
1611301e0f7eSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN) {
1612656cffc9SMike Christie 		/*
1613656cffc9SMike Christie 		 * to handle the race between when we set the recovery state
1614656cffc9SMike Christie 		 * and block the session we requeue here (commands could
1615656cffc9SMike Christie 		 * be entering our queuecommand while a block is starting
1616656cffc9SMike Christie 		 * up because the block code is not locked)
1617656cffc9SMike Christie 		 */
16189000bcd6SMike Christie 		switch (session->state) {
1619301e0f7eSMike Christie 		case ISCSI_STATE_FAILED:
16209000bcd6SMike Christie 		case ISCSI_STATE_IN_RECOVERY:
1621656cffc9SMike Christie 			reason = FAILURE_SESSION_IN_RECOVERY;
1622301e0f7eSMike Christie 			sc->result = DID_IMM_RETRY << 16;
1623301e0f7eSMike Christie 			break;
16249000bcd6SMike Christie 		case ISCSI_STATE_LOGGING_OUT:
16259000bcd6SMike Christie 			reason = FAILURE_SESSION_LOGGING_OUT;
1626301e0f7eSMike Christie 			sc->result = DID_IMM_RETRY << 16;
1627301e0f7eSMike Christie 			break;
1628b3a7ea8dSMike Christie 		case ISCSI_STATE_RECOVERY_FAILED:
1629656cffc9SMike Christie 			reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
163056d7fcfaSMike Christie 			sc->result = DID_TRANSPORT_FAILFAST << 16;
1631b3a7ea8dSMike Christie 			break;
1632b3a7ea8dSMike Christie 		case ISCSI_STATE_TERMINATE:
1633656cffc9SMike Christie 			reason = FAILURE_SESSION_TERMINATE;
16346eabafbeSMike Christie 			sc->result = DID_NO_CONNECT << 16;
1635b3a7ea8dSMike Christie 			break;
1636b3a7ea8dSMike Christie 		default:
16377996a778SMike Christie 			reason = FAILURE_SESSION_FREED;
16386eabafbeSMike Christie 			sc->result = DID_NO_CONNECT << 16;
1639b3a7ea8dSMike Christie 		}
16407996a778SMike Christie 		goto fault;
16417996a778SMike Christie 	}
16427996a778SMike Christie 
16437996a778SMike Christie 	conn = session->leadconn;
164498644047SMike Christie 	if (!conn) {
164598644047SMike Christie 		reason = FAILURE_SESSION_FREED;
16466eabafbeSMike Christie 		sc->result = DID_NO_CONNECT << 16;
164798644047SMike Christie 		goto fault;
164898644047SMike Christie 	}
16497996a778SMike Christie 
1650661134adSMike Christie 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1651661134adSMike Christie 		reason = FAILURE_SESSION_IN_RECOVERY;
1652661134adSMike Christie 		sc->result = DID_REQUEUE;
1653661134adSMike Christie 		goto fault;
1654661134adSMike Christie 	}
1655661134adSMike Christie 
165677a23c21SMike Christie 	if (iscsi_check_cmdsn_window_closed(conn)) {
165777a23c21SMike Christie 		reason = FAILURE_WINDOW_CLOSED;
165877a23c21SMike Christie 		goto reject;
165977a23c21SMike Christie 	}
166077a23c21SMike Christie 
1661577577daSMike Christie 	task = iscsi_alloc_task(conn, sc);
1662577577daSMike Christie 	if (!task) {
166360ecebf5SMike Christie 		reason = FAILURE_OOM;
166460ecebf5SMike Christie 		goto reject;
166560ecebf5SMike Christie 	}
16667996a778SMike Christie 
16671336aed1SMike Christie 	if (!ihost->workq) {
1668577577daSMike Christie 		reason = iscsi_prep_scsi_cmd_pdu(task);
1669577577daSMike Christie 		if (reason) {
16705d12c05eSMike Christie 			if (reason == -ENOMEM ||  reason == -EACCES) {
1671577577daSMike Christie 				reason = FAILURE_OOM;
1672577577daSMike Christie 				goto prepd_reject;
1673577577daSMike Christie 			} else {
1674052d0144SMike Christie 				sc->result = DID_ABORT << 16;
1675577577daSMike Christie 				goto prepd_fault;
1676577577daSMike Christie 			}
1677052d0144SMike Christie 		}
16789c19a7d0SMike Christie 		if (session->tt->xmit_task(task)) {
1679d3305f34SMike Christie 			session->cmdsn--;
1680052d0144SMike Christie 			reason = FAILURE_SESSION_NOT_READY;
1681577577daSMike Christie 			goto prepd_reject;
1682052d0144SMike Christie 		}
16833bbaaad9SMike Christie 	} else {
16843bbaaad9SMike Christie 		list_add_tail(&task->running, &conn->cmdqueue);
168532ae763eSMike Christie 		iscsi_conn_queue_work(conn);
16863bbaaad9SMike Christie 	}
1687052d0144SMike Christie 
1688052d0144SMike Christie 	session->queued_cmdsn++;
1689f41d4721SMike Christie 	spin_unlock_bh(&session->lock);
16907996a778SMike Christie 	return 0;
16917996a778SMike Christie 
1692577577daSMike Christie prepd_reject:
1693f41d4721SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
16947996a778SMike Christie reject:
1695f41d4721SMike Christie 	spin_unlock_bh(&session->lock);
16961b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
16971b2c7af8SMike Christie 			  sc->cmnd[0], reason);
1698d6d13ee1SMike Christie 	return SCSI_MLQUEUE_TARGET_BUSY;
16997996a778SMike Christie 
1700577577daSMike Christie prepd_fault:
1701f41d4721SMike Christie 	iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
17027996a778SMike Christie fault:
1703f41d4721SMike Christie 	spin_unlock_bh(&session->lock);
17041b2c7af8SMike Christie 	ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
17051b2c7af8SMike Christie 			  sc->cmnd[0], reason);
1706c07d4444SBoaz Harrosh 	if (!scsi_bidi_cmnd(sc))
17071c138991SFUJITA Tomonori 		scsi_set_resid(sc, scsi_bufflen(sc));
1708c07d4444SBoaz Harrosh 	else {
1709c07d4444SBoaz Harrosh 		scsi_out(sc)->resid = scsi_out(sc)->length;
1710c07d4444SBoaz Harrosh 		scsi_in(sc)->resid = scsi_in(sc)->length;
1711c07d4444SBoaz Harrosh 	}
1712f41d4721SMike Christie 	sc->scsi_done(sc);
17137996a778SMike Christie 	return 0;
17147996a778SMike Christie }
17157996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_queuecommand);
17167996a778SMike Christie 
1717e881a172SMike Christie int iscsi_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
17187996a778SMike Christie {
17191796e722SMike Christie 	switch (reason) {
17201796e722SMike Christie 	case SCSI_QDEPTH_DEFAULT:
17217996a778SMike Christie 		scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
17221796e722SMike Christie 		break;
17231796e722SMike Christie 	case SCSI_QDEPTH_QFULL:
17241796e722SMike Christie 		scsi_track_queue_full(sdev, depth);
17251796e722SMike Christie 		break;
17261796e722SMike Christie 	case SCSI_QDEPTH_RAMP_UP:
17271796e722SMike Christie 		scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
17281796e722SMike Christie 		break;
17291796e722SMike Christie 	default:
17301796e722SMike Christie 		return -EOPNOTSUPP;
17311796e722SMike Christie 	}
17327996a778SMike Christie 	return sdev->queue_depth;
17337996a778SMike Christie }
17347996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
17357996a778SMike Christie 
17366b5d6c44SMike Christie int iscsi_target_alloc(struct scsi_target *starget)
17376b5d6c44SMike Christie {
17386b5d6c44SMike Christie 	struct iscsi_cls_session *cls_session = starget_to_session(starget);
17396b5d6c44SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
17406b5d6c44SMike Christie 
17416b5d6c44SMike Christie 	starget->can_queue = session->scsi_cmds_max;
17426b5d6c44SMike Christie 	return 0;
17436b5d6c44SMike Christie }
17446b5d6c44SMike Christie EXPORT_SYMBOL_GPL(iscsi_target_alloc);
17456b5d6c44SMike Christie 
1746843c0a8aSMike Christie static void iscsi_tmf_timedout(unsigned long data)
17477996a778SMike Christie {
1748843c0a8aSMike Christie 	struct iscsi_conn *conn = (struct iscsi_conn *)data;
17497996a778SMike Christie 	struct iscsi_session *session = conn->session;
17507996a778SMike Christie 
17517996a778SMike Christie 	spin_lock(&session->lock);
1752843c0a8aSMike Christie 	if (conn->tmf_state == TMF_QUEUED) {
1753843c0a8aSMike Christie 		conn->tmf_state = TMF_TIMEDOUT;
1754bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "tmf timedout\n");
17557996a778SMike Christie 		/* unblock eh_abort() */
17567996a778SMike Christie 		wake_up(&conn->ehwait);
17577996a778SMike Christie 	}
17587996a778SMike Christie 	spin_unlock(&session->lock);
17597996a778SMike Christie }
17607996a778SMike Christie 
17619c19a7d0SMike Christie static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1762f6d5180cSMike Christie 				   struct iscsi_tm *hdr, int age,
1763f6d5180cSMike Christie 				   int timeout)
17647996a778SMike Christie {
17657996a778SMike Christie 	struct iscsi_session *session = conn->session;
17669c19a7d0SMike Christie 	struct iscsi_task *task;
17677996a778SMike Christie 
17689c19a7d0SMike Christie 	task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
17697996a778SMike Christie 				      NULL, 0);
17709c19a7d0SMike Christie 	if (!task) {
17716724add1SMike Christie 		spin_unlock_bh(&session->lock);
1772df4da5cdSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
17737996a778SMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1774843c0a8aSMike Christie 		spin_lock_bh(&session->lock);
177577a23c21SMike Christie 		return -EPERM;
17767996a778SMike Christie 	}
17777996a778SMike Christie 	conn->tmfcmd_pdus_cnt++;
1778f6d5180cSMike Christie 	conn->tmf_timer.expires = timeout * HZ + jiffies;
1779843c0a8aSMike Christie 	conn->tmf_timer.function = iscsi_tmf_timedout;
1780843c0a8aSMike Christie 	conn->tmf_timer.data = (unsigned long)conn;
1781843c0a8aSMike Christie 	add_timer(&conn->tmf_timer);
1782bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "tmf set timeout\n");
1783843c0a8aSMike Christie 
17847996a778SMike Christie 	spin_unlock_bh(&session->lock);
17856724add1SMike Christie 	mutex_unlock(&session->eh_mutex);
17867996a778SMike Christie 
17877996a778SMike Christie 	/*
17887996a778SMike Christie 	 * block eh thread until:
17897996a778SMike Christie 	 *
1790843c0a8aSMike Christie 	 * 1) tmf response
1791843c0a8aSMike Christie 	 * 2) tmf timeout
17927996a778SMike Christie 	 * 3) session is terminated or restarted or userspace has
17937996a778SMike Christie 	 * given up on recovery
17947996a778SMike Christie 	 */
1795843c0a8aSMike Christie 	wait_event_interruptible(conn->ehwait, age != session->age ||
17967996a778SMike Christie 				 session->state != ISCSI_STATE_LOGGED_IN ||
1797843c0a8aSMike Christie 				 conn->tmf_state != TMF_QUEUED);
17987996a778SMike Christie 	if (signal_pending(current))
17997996a778SMike Christie 		flush_signals(current);
1800843c0a8aSMike Christie 	del_timer_sync(&conn->tmf_timer);
1801843c0a8aSMike Christie 
18026724add1SMike Christie 	mutex_lock(&session->eh_mutex);
180377a23c21SMike Christie 	spin_lock_bh(&session->lock);
18049c19a7d0SMike Christie 	/* if the session drops it will clean up the task */
1805843c0a8aSMike Christie 	if (age != session->age ||
1806843c0a8aSMike Christie 	    session->state != ISCSI_STATE_LOGGED_IN)
1807843c0a8aSMike Christie 		return -ENOTCONN;
18087996a778SMike Christie 	return 0;
18097996a778SMike Christie }
18107996a778SMike Christie 
18117996a778SMike Christie /*
1812843c0a8aSMike Christie  * Fail commands. session lock held and recv side suspended and xmit
1813843c0a8aSMike Christie  * thread flushed
1814843c0a8aSMike Christie  */
18153bbaaad9SMike Christie static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
18166eabafbeSMike Christie 			    int error)
1817843c0a8aSMike Christie {
18183bbaaad9SMike Christie 	struct iscsi_task *task;
18193bbaaad9SMike Christie 	int i;
1820843c0a8aSMike Christie 
18213bbaaad9SMike Christie 	for (i = 0; i < conn->session->cmds_max; i++) {
18223bbaaad9SMike Christie 		task = conn->session->cmds[i];
18233bbaaad9SMike Christie 		if (!task->sc || task->state == ISCSI_TASK_FREE)
18243bbaaad9SMike Christie 			continue;
1825843c0a8aSMike Christie 
18263bbaaad9SMike Christie 		if (lun != -1 && lun != task->sc->device->lun)
18273bbaaad9SMike Christie 			continue;
18283bbaaad9SMike Christie 
18291b2c7af8SMike Christie 		ISCSI_DBG_SESSION(conn->session,
18303bbaaad9SMike Christie 				  "failing sc %p itt 0x%x state %d\n",
18313bbaaad9SMike Christie 				  task->sc, task->itt, task->state);
1832b3cd5050SMike Christie 		fail_scsi_task(task, error);
1833843c0a8aSMike Christie 	}
1834843c0a8aSMike Christie }
1835843c0a8aSMike Christie 
1836661134adSMike Christie /**
1837661134adSMike Christie  * iscsi_suspend_queue - suspend iscsi_queuecommand
1838661134adSMike Christie  * @conn: iscsi conn to stop queueing IO on
1839661134adSMike Christie  *
1840661134adSMike Christie  * This grabs the session lock to make sure no one is in
1841661134adSMike Christie  * xmit_task/queuecommand, and then sets suspend to prevent
1842661134adSMike Christie  * new commands from being queued. This only needs to be called
1843661134adSMike Christie  * by offload drivers that need to sync a path like ep disconnect
1844661134adSMike Christie  * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1845661134adSMike Christie  * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1846661134adSMike Christie  */
1847661134adSMike Christie void iscsi_suspend_queue(struct iscsi_conn *conn)
1848661134adSMike Christie {
1849661134adSMike Christie 	spin_lock_bh(&conn->session->lock);
1850661134adSMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1851661134adSMike Christie 	spin_unlock_bh(&conn->session->lock);
1852661134adSMike Christie }
1853661134adSMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1854661134adSMike Christie 
1855661134adSMike Christie /**
1856661134adSMike Christie  * iscsi_suspend_tx - suspend iscsi_data_xmit
1857661134adSMike Christie  * @conn: iscsi conn tp stop processing IO on.
1858661134adSMike Christie  *
1859661134adSMike Christie  * This function sets the suspend bit to prevent iscsi_data_xmit
1860661134adSMike Christie  * from sending new IO, and if work is queued on the xmit thread
1861661134adSMike Christie  * it will wait for it to be completed.
1862661134adSMike Christie  */
1863b40977d9SMike Christie void iscsi_suspend_tx(struct iscsi_conn *conn)
18646724add1SMike Christie {
186532ae763eSMike Christie 	struct Scsi_Host *shost = conn->session->host;
186632ae763eSMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
186732ae763eSMike Christie 
18686724add1SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
18691336aed1SMike Christie 	if (ihost->workq)
187032ae763eSMike Christie 		flush_workqueue(ihost->workq);
18716724add1SMike Christie }
1872b40977d9SMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
18736724add1SMike Christie 
18746724add1SMike Christie static void iscsi_start_tx(struct iscsi_conn *conn)
18756724add1SMike Christie {
18766724add1SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
187732ae763eSMike Christie 	iscsi_conn_queue_work(conn);
18786724add1SMike Christie }
18796724add1SMike Christie 
18804c48a829SMike Christie /*
18814c48a829SMike Christie  * We want to make sure a ping is in flight. It has timed out.
18824c48a829SMike Christie  * And we are not busy processing a pdu that is making
18834c48a829SMike Christie  * progress but got started before the ping and is taking a while
18844c48a829SMike Christie  * to complete so the ping is just stuck behind it in a queue.
18854c48a829SMike Christie  */
18864c48a829SMike Christie static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
18874c48a829SMike Christie {
18884c48a829SMike Christie 	if (conn->ping_task &&
18894c48a829SMike Christie 	    time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
18904c48a829SMike Christie 			   (conn->ping_timeout * HZ), jiffies))
18914c48a829SMike Christie 		return 1;
18924c48a829SMike Christie 	else
18934c48a829SMike Christie 		return 0;
18944c48a829SMike Christie }
18954c48a829SMike Christie 
1896d355e57dSMike Christie static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1897f6d5180cSMike Christie {
1898d355e57dSMike Christie 	enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
189992ed4d69SMike Christie 	struct iscsi_task *task = NULL, *running_task;
1900f6d5180cSMike Christie 	struct iscsi_cls_session *cls_session;
1901f6d5180cSMike Christie 	struct iscsi_session *session;
1902f6d5180cSMike Christie 	struct iscsi_conn *conn;
190392ed4d69SMike Christie 	int i;
1904f6d5180cSMike Christie 
1905d355e57dSMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
190675613521SMike Christie 	session = cls_session->dd_data;
1907f6d5180cSMike Christie 
1908bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1909f6d5180cSMike Christie 
1910f6d5180cSMike Christie 	spin_lock(&session->lock);
1911e3d338a5SMike Christie 	task = (struct iscsi_task *)sc->SCp.ptr;
1912e3d338a5SMike Christie 	if (!task) {
1913e3d338a5SMike Christie 		/*
1914e3d338a5SMike Christie 		 * Raced with completion. Blk layer has taken ownership
1915e3d338a5SMike Christie 		 * so let timeout code complete it now.
1916e3d338a5SMike Christie 		 */
1917e3d338a5SMike Christie 		rc = BLK_EH_HANDLED;
1918e3d338a5SMike Christie 		goto done;
1919e3d338a5SMike Christie 	}
1920e3d338a5SMike Christie 
1921f6d5180cSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN) {
1922f6d5180cSMike Christie 		/*
1923f6d5180cSMike Christie 		 * We are probably in the middle of iscsi recovery so let
1924f6d5180cSMike Christie 		 * that complete and handle the error.
1925f6d5180cSMike Christie 		 */
1926242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
1927f6d5180cSMike Christie 		goto done;
1928f6d5180cSMike Christie 	}
1929f6d5180cSMike Christie 
1930f6d5180cSMike Christie 	conn = session->leadconn;
1931f6d5180cSMike Christie 	if (!conn) {
1932f6d5180cSMike Christie 		/* In the middle of shuting down */
1933242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
1934f6d5180cSMike Christie 		goto done;
1935f6d5180cSMike Christie 	}
1936f6d5180cSMike Christie 
1937d355e57dSMike Christie 	/*
1938d355e57dSMike Christie 	 * If we have sent (at least queued to the network layer) a pdu or
1939d355e57dSMike Christie 	 * recvd one for the task since the last timeout ask for
1940d355e57dSMike Christie 	 * more time. If on the next timeout we have not made progress
1941d355e57dSMike Christie 	 * we can check if it is the task or connection when we send the
1942d355e57dSMike Christie 	 * nop as a ping.
1943d355e57dSMike Christie 	 */
194492ed4d69SMike Christie 	if (time_after(task->last_xfer, task->last_timeout)) {
1945bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "Command making progress. Asking "
1946d355e57dSMike Christie 			     "scsi-ml for more time to complete. "
194792ed4d69SMike Christie 			     "Last data xfer at %lu. Last timeout was at "
1948d355e57dSMike Christie 			     "%lu\n.", task->last_xfer, task->last_timeout);
1949d355e57dSMike Christie 		task->have_checked_conn = false;
1950d355e57dSMike Christie 		rc = BLK_EH_RESET_TIMER;
1951d355e57dSMike Christie 		goto done;
1952d355e57dSMike Christie 	}
1953d355e57dSMike Christie 
1954f6d5180cSMike Christie 	if (!conn->recv_timeout && !conn->ping_timeout)
1955f6d5180cSMike Christie 		goto done;
1956f6d5180cSMike Christie 	/*
1957f6d5180cSMike Christie 	 * if the ping timedout then we are in the middle of cleaning up
1958f6d5180cSMike Christie 	 * and can let the iscsi eh handle it
1959f6d5180cSMike Christie 	 */
19604c48a829SMike Christie 	if (iscsi_has_ping_timed_out(conn)) {
1961242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
19624c48a829SMike Christie 		goto done;
19634c48a829SMike Christie 	}
1964d355e57dSMike Christie 
196592ed4d69SMike Christie 	for (i = 0; i < conn->session->cmds_max; i++) {
196692ed4d69SMike Christie 		running_task = conn->session->cmds[i];
196792ed4d69SMike Christie 		if (!running_task->sc || running_task == task ||
196892ed4d69SMike Christie 		     running_task->state != ISCSI_TASK_RUNNING)
196992ed4d69SMike Christie 			continue;
197092ed4d69SMike Christie 
197192ed4d69SMike Christie 		/*
197292ed4d69SMike Christie 		 * Only check if cmds started before this one have made
197392ed4d69SMike Christie 		 * progress, or this could never fail
197492ed4d69SMike Christie 		 */
197592ed4d69SMike Christie 		if (time_after(running_task->sc->jiffies_at_alloc,
197692ed4d69SMike Christie 			       task->sc->jiffies_at_alloc))
197792ed4d69SMike Christie 			continue;
197892ed4d69SMike Christie 
197992ed4d69SMike Christie 		if (time_after(running_task->last_xfer, task->last_timeout)) {
198092ed4d69SMike Christie 			/*
198192ed4d69SMike Christie 			 * This task has not made progress, but a task
198292ed4d69SMike Christie 			 * started before us has transferred data since
198392ed4d69SMike Christie 			 * we started/last-checked. We could be queueing
198492ed4d69SMike Christie 			 * too many tasks or the LU is bad.
198592ed4d69SMike Christie 			 *
198692ed4d69SMike Christie 			 * If the device is bad the cmds ahead of us on
198792ed4d69SMike Christie 			 * other devs will complete, and this loop will
198892ed4d69SMike Christie 			 * eventually fail starting the scsi eh.
198992ed4d69SMike Christie 			 */
199092ed4d69SMike Christie 			ISCSI_DBG_EH(session, "Command has not made progress "
199192ed4d69SMike Christie 				     "but commands ahead of it have. "
199292ed4d69SMike Christie 				     "Asking scsi-ml for more time to "
199392ed4d69SMike Christie 				     "complete. Our last xfer vs running task "
199492ed4d69SMike Christie 				     "last xfer %lu/%lu. Last check %lu.\n",
199592ed4d69SMike Christie 				     task->last_xfer, running_task->last_xfer,
199692ed4d69SMike Christie 				     task->last_timeout);
199792ed4d69SMike Christie 			rc = BLK_EH_RESET_TIMER;
199892ed4d69SMike Christie 			goto done;
199992ed4d69SMike Christie 		}
200092ed4d69SMike Christie 	}
200192ed4d69SMike Christie 
2002d355e57dSMike Christie 	/* Assumes nop timeout is shorter than scsi cmd timeout */
2003d355e57dSMike Christie 	if (task->have_checked_conn)
2004d355e57dSMike Christie 		goto done;
2005d355e57dSMike Christie 
2006f6d5180cSMike Christie 	/*
2007d355e57dSMike Christie 	 * Checking the transport already or nop from a cmd timeout still
2008d355e57dSMike Christie 	 * running
2009f6d5180cSMike Christie 	 */
2010d355e57dSMike Christie 	if (conn->ping_task) {
2011d355e57dSMike Christie 		task->have_checked_conn = true;
2012242f9dcbSJens Axboe 		rc = BLK_EH_RESET_TIMER;
20134c48a829SMike Christie 		goto done;
20144c48a829SMike Christie 	}
20154c48a829SMike Christie 
2016d355e57dSMike Christie 	/* Make sure there is a transport check done */
2017d355e57dSMike Christie 	iscsi_send_nopout(conn, NULL);
2018d355e57dSMike Christie 	task->have_checked_conn = true;
2019242f9dcbSJens Axboe 	rc = BLK_EH_RESET_TIMER;
2020d355e57dSMike Christie 
2021f6d5180cSMike Christie done:
2022d355e57dSMike Christie 	if (task)
2023d355e57dSMike Christie 		task->last_timeout = jiffies;
2024f6d5180cSMike Christie 	spin_unlock(&session->lock);
2025bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2026242f9dcbSJens Axboe 		     "timer reset" : "nh");
2027f6d5180cSMike Christie 	return rc;
2028f6d5180cSMike Christie }
2029f6d5180cSMike Christie 
2030f6d5180cSMike Christie static void iscsi_check_transport_timeouts(unsigned long data)
2031f6d5180cSMike Christie {
2032f6d5180cSMike Christie 	struct iscsi_conn *conn = (struct iscsi_conn *)data;
2033f6d5180cSMike Christie 	struct iscsi_session *session = conn->session;
20344cf10435SMike Christie 	unsigned long recv_timeout, next_timeout = 0, last_recv;
2035f6d5180cSMike Christie 
2036f6d5180cSMike Christie 	spin_lock(&session->lock);
2037f6d5180cSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN)
2038f6d5180cSMike Christie 		goto done;
2039f6d5180cSMike Christie 
20404cf10435SMike Christie 	recv_timeout = conn->recv_timeout;
20414cf10435SMike Christie 	if (!recv_timeout)
2042f6d5180cSMike Christie 		goto done;
2043f6d5180cSMike Christie 
20444cf10435SMike Christie 	recv_timeout *= HZ;
2045f6d5180cSMike Christie 	last_recv = conn->last_recv;
20464c48a829SMike Christie 
20474c48a829SMike Christie 	if (iscsi_has_ping_timed_out(conn)) {
2048322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
20494c48a829SMike Christie 				  "expired, recv timeout %d, last rx %lu, "
20504c48a829SMike Christie 				  "last ping %lu, now %lu\n",
20514c48a829SMike Christie 				  conn->ping_timeout, conn->recv_timeout,
20524c48a829SMike Christie 				  last_recv, conn->last_ping, jiffies);
2053f6d5180cSMike Christie 		spin_unlock(&session->lock);
2054f6d5180cSMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2055f6d5180cSMike Christie 		return;
2056f6d5180cSMike Christie 	}
2057f6d5180cSMike Christie 
20584cf10435SMike Christie 	if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2059f6d5180cSMike Christie 		/* send a ping to try to provoke some traffic */
20601b2c7af8SMike Christie 		ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
2061f6d5180cSMike Christie 		iscsi_send_nopout(conn, NULL);
20624cf10435SMike Christie 		next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2063ad294e9cSMike Christie 	} else
20644cf10435SMike Christie 		next_timeout = last_recv + recv_timeout;
2065f6d5180cSMike Christie 
20661b2c7af8SMike Christie 	ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2067f6d5180cSMike Christie 	mod_timer(&conn->transport_timer, next_timeout);
2068f6d5180cSMike Christie done:
2069f6d5180cSMike Christie 	spin_unlock(&session->lock);
2070f6d5180cSMike Christie }
2071f6d5180cSMike Christie 
20729c19a7d0SMike Christie static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2073843c0a8aSMike Christie 				      struct iscsi_tm *hdr)
2074843c0a8aSMike Christie {
2075843c0a8aSMike Christie 	memset(hdr, 0, sizeof(*hdr));
2076843c0a8aSMike Christie 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2077843c0a8aSMike Christie 	hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2078843c0a8aSMike Christie 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
207955bdabdfSAndy Grover 	hdr->lun = task->lun;
2080577577daSMike Christie 	hdr->rtt = task->hdr_itt;
2081577577daSMike Christie 	hdr->refcmdsn = task->cmdsn;
2082843c0a8aSMike Christie }
2083843c0a8aSMike Christie 
20847996a778SMike Christie int iscsi_eh_abort(struct scsi_cmnd *sc)
20857996a778SMike Christie {
208675613521SMike Christie 	struct iscsi_cls_session *cls_session;
208775613521SMike Christie 	struct iscsi_session *session;
2088f47f2cf5SMike Christie 	struct iscsi_conn *conn;
20899c19a7d0SMike Christie 	struct iscsi_task *task;
2090843c0a8aSMike Christie 	struct iscsi_tm *hdr;
2091843c0a8aSMike Christie 	int rc, age;
20927996a778SMike Christie 
209375613521SMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
209475613521SMike Christie 	session = cls_session->dd_data;
209575613521SMike Christie 
2096bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
20974421c9ebSMike Christie 
20986724add1SMike Christie 	mutex_lock(&session->eh_mutex);
20996724add1SMike Christie 	spin_lock_bh(&session->lock);
2100f47f2cf5SMike Christie 	/*
2101f47f2cf5SMike Christie 	 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2102f47f2cf5SMike Christie 	 * got the command.
2103f47f2cf5SMike Christie 	 */
2104f47f2cf5SMike Christie 	if (!sc->SCp.ptr) {
2105bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
21061b2c7af8SMike Christie 				      "it completed.\n");
21076724add1SMike Christie 		spin_unlock_bh(&session->lock);
21086724add1SMike Christie 		mutex_unlock(&session->eh_mutex);
2109f47f2cf5SMike Christie 		return SUCCESS;
2110f47f2cf5SMike Christie 	}
2111f47f2cf5SMike Christie 
21127996a778SMike Christie 	/*
21137996a778SMike Christie 	 * If we are not logged in or we have started a new session
21147996a778SMike Christie 	 * then let the host reset code handle this
21157996a778SMike Christie 	 */
2116843c0a8aSMike Christie 	if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2117843c0a8aSMike Christie 	    sc->SCp.phase != session->age) {
2118843c0a8aSMike Christie 		spin_unlock_bh(&session->lock);
2119843c0a8aSMike Christie 		mutex_unlock(&session->eh_mutex);
2120bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "failing abort due to dropped "
21214421c9ebSMike Christie 				  "session.\n");
2122843c0a8aSMike Christie 		return FAILED;
2123843c0a8aSMike Christie 	}
2124843c0a8aSMike Christie 
2125843c0a8aSMike Christie 	conn = session->leadconn;
2126843c0a8aSMike Christie 	conn->eh_abort_cnt++;
2127843c0a8aSMike Christie 	age = session->age;
2128843c0a8aSMike Christie 
21299c19a7d0SMike Christie 	task = (struct iscsi_task *)sc->SCp.ptr;
2130bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
21311b2c7af8SMike Christie 		     sc, task->itt);
21327996a778SMike Christie 
21339c19a7d0SMike Christie 	/* task completed before time out */
21349c19a7d0SMike Christie 	if (!task->sc) {
2135bd2199d4SErez Zilber 		ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
213677a23c21SMike Christie 		goto success;
21377ea8b828SMike Christie 	}
21387996a778SMike Christie 
21399c19a7d0SMike Christie 	if (task->state == ISCSI_TASK_PENDING) {
2140b3cd5050SMike Christie 		fail_scsi_task(task, DID_ABORT);
214177a23c21SMike Christie 		goto success;
214277a23c21SMike Christie 	}
21437996a778SMike Christie 
2144843c0a8aSMike Christie 	/* only have one tmf outstanding at a time */
2145843c0a8aSMike Christie 	if (conn->tmf_state != TMF_INITIAL)
21467996a778SMike Christie 		goto failed;
2147843c0a8aSMike Christie 	conn->tmf_state = TMF_QUEUED;
21487996a778SMike Christie 
2149843c0a8aSMike Christie 	hdr = &conn->tmhdr;
21509c19a7d0SMike Christie 	iscsi_prep_abort_task_pdu(task, hdr);
2151843c0a8aSMike Christie 
21529c19a7d0SMike Christie 	if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
2153843c0a8aSMike Christie 		rc = FAILED;
2154843c0a8aSMike Christie 		goto failed;
2155843c0a8aSMike Christie 	}
2156843c0a8aSMike Christie 
2157843c0a8aSMike Christie 	switch (conn->tmf_state) {
2158843c0a8aSMike Christie 	case TMF_SUCCESS:
21597ea8b828SMike Christie 		spin_unlock_bh(&session->lock);
2160913e5bf4SMike Christie 		/*
2161913e5bf4SMike Christie 		 * stop tx side incase the target had sent a abort rsp but
2162913e5bf4SMike Christie 		 * the initiator was still writing out data.
2163913e5bf4SMike Christie 		 */
21646724add1SMike Christie 		iscsi_suspend_tx(conn);
21657996a778SMike Christie 		/*
2166913e5bf4SMike Christie 		 * we do not stop the recv side because targets have been
2167913e5bf4SMike Christie 		 * good and have never sent us a successful tmf response
2168913e5bf4SMike Christie 		 * then sent more data for the cmd.
21697996a778SMike Christie 		 */
21706187c242SMike Christie 		spin_lock_bh(&session->lock);
2171b3cd5050SMike Christie 		fail_scsi_task(task, DID_ABORT);
2172843c0a8aSMike Christie 		conn->tmf_state = TMF_INITIAL;
21735d12c05eSMike Christie 		memset(hdr, 0, sizeof(*hdr));
21746187c242SMike Christie 		spin_unlock_bh(&session->lock);
21756724add1SMike Christie 		iscsi_start_tx(conn);
217677a23c21SMike Christie 		goto success_unlocked;
2177843c0a8aSMike Christie 	case TMF_TIMEDOUT:
2178843c0a8aSMike Christie 		spin_unlock_bh(&session->lock);
2179df4da5cdSMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2180843c0a8aSMike Christie 		goto failed_unlocked;
2181843c0a8aSMike Christie 	case TMF_NOT_FOUND:
2182843c0a8aSMike Christie 		if (!sc->SCp.ptr) {
2183843c0a8aSMike Christie 			conn->tmf_state = TMF_INITIAL;
21845d12c05eSMike Christie 			memset(hdr, 0, sizeof(*hdr));
21859c19a7d0SMike Christie 			/* task completed before tmf abort response */
2186bd2199d4SErez Zilber 			ISCSI_DBG_EH(session, "sc completed while abort	in "
2187bd2199d4SErez Zilber 					      "progress\n");
218877a23c21SMike Christie 			goto success;
218977a23c21SMike Christie 		}
219077a23c21SMike Christie 		/* fall through */
219177a23c21SMike Christie 	default:
2192843c0a8aSMike Christie 		conn->tmf_state = TMF_INITIAL;
2193843c0a8aSMike Christie 		goto failed;
219477a23c21SMike Christie 	}
21957996a778SMike Christie 
219677a23c21SMike Christie success:
219777a23c21SMike Christie 	spin_unlock_bh(&session->lock);
219877a23c21SMike Christie success_unlocked:
2199bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
22001b2c7af8SMike Christie 		     sc, task->itt);
22016724add1SMike Christie 	mutex_unlock(&session->eh_mutex);
22027996a778SMike Christie 	return SUCCESS;
22037996a778SMike Christie 
22047996a778SMike Christie failed:
22057996a778SMike Christie 	spin_unlock_bh(&session->lock);
220677a23c21SMike Christie failed_unlocked:
2207bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
22089c19a7d0SMike Christie 		     task ? task->itt : 0);
22096724add1SMike Christie 	mutex_unlock(&session->eh_mutex);
22107996a778SMike Christie 	return FAILED;
22117996a778SMike Christie }
22127996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_abort);
22137996a778SMike Christie 
2214843c0a8aSMike Christie static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2215843c0a8aSMike Christie {
2216843c0a8aSMike Christie 	memset(hdr, 0, sizeof(*hdr));
2217843c0a8aSMike Christie 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2218843c0a8aSMike Christie 	hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2219843c0a8aSMike Christie 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
222055bdabdfSAndy Grover 	int_to_scsilun(sc->device->lun, &hdr->lun);
2221f6d5180cSMike Christie 	hdr->rtt = RESERVED_ITT;
2222843c0a8aSMike Christie }
2223843c0a8aSMike Christie 
2224843c0a8aSMike Christie int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2225843c0a8aSMike Christie {
222675613521SMike Christie 	struct iscsi_cls_session *cls_session;
222775613521SMike Christie 	struct iscsi_session *session;
2228843c0a8aSMike Christie 	struct iscsi_conn *conn;
2229843c0a8aSMike Christie 	struct iscsi_tm *hdr;
2230843c0a8aSMike Christie 	int rc = FAILED;
2231843c0a8aSMike Christie 
223275613521SMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
223375613521SMike Christie 	session = cls_session->dd_data;
223475613521SMike Christie 
2235bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
2236843c0a8aSMike Christie 
2237843c0a8aSMike Christie 	mutex_lock(&session->eh_mutex);
2238843c0a8aSMike Christie 	spin_lock_bh(&session->lock);
2239843c0a8aSMike Christie 	/*
2240843c0a8aSMike Christie 	 * Just check if we are not logged in. We cannot check for
2241843c0a8aSMike Christie 	 * the phase because the reset could come from a ioctl.
2242843c0a8aSMike Christie 	 */
2243843c0a8aSMike Christie 	if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2244843c0a8aSMike Christie 		goto unlock;
2245843c0a8aSMike Christie 	conn = session->leadconn;
2246843c0a8aSMike Christie 
2247843c0a8aSMike Christie 	/* only have one tmf outstanding at a time */
2248843c0a8aSMike Christie 	if (conn->tmf_state != TMF_INITIAL)
2249843c0a8aSMike Christie 		goto unlock;
2250843c0a8aSMike Christie 	conn->tmf_state = TMF_QUEUED;
2251843c0a8aSMike Christie 
2252843c0a8aSMike Christie 	hdr = &conn->tmhdr;
2253843c0a8aSMike Christie 	iscsi_prep_lun_reset_pdu(sc, hdr);
2254843c0a8aSMike Christie 
22559c19a7d0SMike Christie 	if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2256f6d5180cSMike Christie 				    session->lu_reset_timeout)) {
2257843c0a8aSMike Christie 		rc = FAILED;
2258843c0a8aSMike Christie 		goto unlock;
2259843c0a8aSMike Christie 	}
2260843c0a8aSMike Christie 
2261843c0a8aSMike Christie 	switch (conn->tmf_state) {
2262843c0a8aSMike Christie 	case TMF_SUCCESS:
2263843c0a8aSMike Christie 		break;
2264843c0a8aSMike Christie 	case TMF_TIMEDOUT:
2265843c0a8aSMike Christie 		spin_unlock_bh(&session->lock);
2266df4da5cdSMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2267843c0a8aSMike Christie 		goto done;
2268843c0a8aSMike Christie 	default:
2269843c0a8aSMike Christie 		conn->tmf_state = TMF_INITIAL;
2270843c0a8aSMike Christie 		goto unlock;
2271843c0a8aSMike Christie 	}
2272843c0a8aSMike Christie 
2273843c0a8aSMike Christie 	rc = SUCCESS;
2274843c0a8aSMike Christie 	spin_unlock_bh(&session->lock);
2275843c0a8aSMike Christie 
2276843c0a8aSMike Christie 	iscsi_suspend_tx(conn);
2277913e5bf4SMike Christie 
2278a3439148SMike Christie 	spin_lock_bh(&session->lock);
22795d12c05eSMike Christie 	memset(hdr, 0, sizeof(*hdr));
22803bbaaad9SMike Christie 	fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2281843c0a8aSMike Christie 	conn->tmf_state = TMF_INITIAL;
2282a3439148SMike Christie 	spin_unlock_bh(&session->lock);
2283843c0a8aSMike Christie 
2284843c0a8aSMike Christie 	iscsi_start_tx(conn);
2285843c0a8aSMike Christie 	goto done;
2286843c0a8aSMike Christie 
2287843c0a8aSMike Christie unlock:
2288843c0a8aSMike Christie 	spin_unlock_bh(&session->lock);
2289843c0a8aSMike Christie done:
2290bd2199d4SErez Zilber 	ISCSI_DBG_EH(session, "dev reset result = %s\n",
2291843c0a8aSMike Christie 		     rc == SUCCESS ? "SUCCESS" : "FAILED");
2292843c0a8aSMike Christie 	mutex_unlock(&session->eh_mutex);
2293843c0a8aSMike Christie 	return rc;
2294843c0a8aSMike Christie }
2295843c0a8aSMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2296843c0a8aSMike Christie 
22973fe5ae8bSMike Christie void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
22983fe5ae8bSMike Christie {
22993fe5ae8bSMike Christie 	struct iscsi_session *session = cls_session->dd_data;
23003fe5ae8bSMike Christie 
23013fe5ae8bSMike Christie 	spin_lock_bh(&session->lock);
23023fe5ae8bSMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN) {
23033fe5ae8bSMike Christie 		session->state = ISCSI_STATE_RECOVERY_FAILED;
23043fe5ae8bSMike Christie 		if (session->leadconn)
23053fe5ae8bSMike Christie 			wake_up(&session->leadconn->ehwait);
23063fe5ae8bSMike Christie 	}
23073fe5ae8bSMike Christie 	spin_unlock_bh(&session->lock);
23083fe5ae8bSMike Christie }
23093fe5ae8bSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
23103fe5ae8bSMike Christie 
23113fe5ae8bSMike Christie /**
23123fe5ae8bSMike Christie  * iscsi_eh_session_reset - drop session and attempt relogin
23133fe5ae8bSMike Christie  * @sc: scsi command
23143fe5ae8bSMike Christie  *
23153fe5ae8bSMike Christie  * This function will wait for a relogin, session termination from
23163fe5ae8bSMike Christie  * userspace, or a recovery/replacement timeout.
23173fe5ae8bSMike Christie  */
2318309ce156SJayamohan Kallickal int iscsi_eh_session_reset(struct scsi_cmnd *sc)
23193fe5ae8bSMike Christie {
23203fe5ae8bSMike Christie 	struct iscsi_cls_session *cls_session;
23213fe5ae8bSMike Christie 	struct iscsi_session *session;
23223fe5ae8bSMike Christie 	struct iscsi_conn *conn;
23233fe5ae8bSMike Christie 
23243fe5ae8bSMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
23253fe5ae8bSMike Christie 	session = cls_session->dd_data;
23263fe5ae8bSMike Christie 	conn = session->leadconn;
23273fe5ae8bSMike Christie 
23283fe5ae8bSMike Christie 	mutex_lock(&session->eh_mutex);
23293fe5ae8bSMike Christie 	spin_lock_bh(&session->lock);
23303fe5ae8bSMike Christie 	if (session->state == ISCSI_STATE_TERMINATE) {
23313fe5ae8bSMike Christie failed:
23323fe5ae8bSMike Christie 		ISCSI_DBG_EH(session,
23333fe5ae8bSMike Christie 			     "failing session reset: Could not log back into "
23343fe5ae8bSMike Christie 			     "%s, %s [age %d]\n", session->targetname,
23353fe5ae8bSMike Christie 			     conn->persistent_address, session->age);
23363fe5ae8bSMike Christie 		spin_unlock_bh(&session->lock);
23373fe5ae8bSMike Christie 		mutex_unlock(&session->eh_mutex);
23383fe5ae8bSMike Christie 		return FAILED;
23393fe5ae8bSMike Christie 	}
23403fe5ae8bSMike Christie 
23413fe5ae8bSMike Christie 	spin_unlock_bh(&session->lock);
23423fe5ae8bSMike Christie 	mutex_unlock(&session->eh_mutex);
23433fe5ae8bSMike Christie 	/*
23443fe5ae8bSMike Christie 	 * we drop the lock here but the leadconn cannot be destoyed while
23453fe5ae8bSMike Christie 	 * we are in the scsi eh
23463fe5ae8bSMike Christie 	 */
2347df4da5cdSMike Christie 	iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
23483fe5ae8bSMike Christie 
23493fe5ae8bSMike Christie 	ISCSI_DBG_EH(session, "wait for relogin\n");
23503fe5ae8bSMike Christie 	wait_event_interruptible(conn->ehwait,
23513fe5ae8bSMike Christie 				 session->state == ISCSI_STATE_TERMINATE ||
23523fe5ae8bSMike Christie 				 session->state == ISCSI_STATE_LOGGED_IN ||
23533fe5ae8bSMike Christie 				 session->state == ISCSI_STATE_RECOVERY_FAILED);
23543fe5ae8bSMike Christie 	if (signal_pending(current))
23553fe5ae8bSMike Christie 		flush_signals(current);
23563fe5ae8bSMike Christie 
23573fe5ae8bSMike Christie 	mutex_lock(&session->eh_mutex);
23583fe5ae8bSMike Christie 	spin_lock_bh(&session->lock);
23593fe5ae8bSMike Christie 	if (session->state == ISCSI_STATE_LOGGED_IN) {
23603fe5ae8bSMike Christie 		ISCSI_DBG_EH(session,
23613fe5ae8bSMike Christie 			     "session reset succeeded for %s,%s\n",
23623fe5ae8bSMike Christie 			     session->targetname, conn->persistent_address);
23633fe5ae8bSMike Christie 	} else
23643fe5ae8bSMike Christie 		goto failed;
23653fe5ae8bSMike Christie 	spin_unlock_bh(&session->lock);
23663fe5ae8bSMike Christie 	mutex_unlock(&session->eh_mutex);
23673fe5ae8bSMike Christie 	return SUCCESS;
23683fe5ae8bSMike Christie }
2369309ce156SJayamohan Kallickal EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
23703fe5ae8bSMike Christie 
23713fe5ae8bSMike Christie static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
23723fe5ae8bSMike Christie {
23733fe5ae8bSMike Christie 	memset(hdr, 0, sizeof(*hdr));
23743fe5ae8bSMike Christie 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
23753fe5ae8bSMike Christie 	hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
23763fe5ae8bSMike Christie 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
23773fe5ae8bSMike Christie 	hdr->rtt = RESERVED_ITT;
23783fe5ae8bSMike Christie }
23793fe5ae8bSMike Christie 
23803fe5ae8bSMike Christie /**
23813fe5ae8bSMike Christie  * iscsi_eh_target_reset - reset target
23823fe5ae8bSMike Christie  * @sc: scsi command
23833fe5ae8bSMike Christie  *
2384309ce156SJayamohan Kallickal  * This will attempt to send a warm target reset.
23853fe5ae8bSMike Christie  */
23863fe5ae8bSMike Christie int iscsi_eh_target_reset(struct scsi_cmnd *sc)
23873fe5ae8bSMike Christie {
23883fe5ae8bSMike Christie 	struct iscsi_cls_session *cls_session;
23893fe5ae8bSMike Christie 	struct iscsi_session *session;
23903fe5ae8bSMike Christie 	struct iscsi_conn *conn;
23913fe5ae8bSMike Christie 	struct iscsi_tm *hdr;
23923fe5ae8bSMike Christie 	int rc = FAILED;
23933fe5ae8bSMike Christie 
23943fe5ae8bSMike Christie 	cls_session = starget_to_session(scsi_target(sc->device));
23953fe5ae8bSMike Christie 	session = cls_session->dd_data;
23963fe5ae8bSMike Christie 
23973fe5ae8bSMike Christie 	ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
23983fe5ae8bSMike Christie 		     session->targetname);
23993fe5ae8bSMike Christie 
24003fe5ae8bSMike Christie 	mutex_lock(&session->eh_mutex);
24013fe5ae8bSMike Christie 	spin_lock_bh(&session->lock);
24023fe5ae8bSMike Christie 	/*
24033fe5ae8bSMike Christie 	 * Just check if we are not logged in. We cannot check for
24043fe5ae8bSMike Christie 	 * the phase because the reset could come from a ioctl.
24053fe5ae8bSMike Christie 	 */
24063fe5ae8bSMike Christie 	if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
24073fe5ae8bSMike Christie 		goto unlock;
24083fe5ae8bSMike Christie 	conn = session->leadconn;
24093fe5ae8bSMike Christie 
24103fe5ae8bSMike Christie 	/* only have one tmf outstanding at a time */
24113fe5ae8bSMike Christie 	if (conn->tmf_state != TMF_INITIAL)
24123fe5ae8bSMike Christie 		goto unlock;
24133fe5ae8bSMike Christie 	conn->tmf_state = TMF_QUEUED;
24143fe5ae8bSMike Christie 
24153fe5ae8bSMike Christie 	hdr = &conn->tmhdr;
24163fe5ae8bSMike Christie 	iscsi_prep_tgt_reset_pdu(sc, hdr);
24173fe5ae8bSMike Christie 
24183fe5ae8bSMike Christie 	if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
24193fe5ae8bSMike Christie 				    session->tgt_reset_timeout)) {
24203fe5ae8bSMike Christie 		rc = FAILED;
24213fe5ae8bSMike Christie 		goto unlock;
24223fe5ae8bSMike Christie 	}
24233fe5ae8bSMike Christie 
24243fe5ae8bSMike Christie 	switch (conn->tmf_state) {
24253fe5ae8bSMike Christie 	case TMF_SUCCESS:
24263fe5ae8bSMike Christie 		break;
24273fe5ae8bSMike Christie 	case TMF_TIMEDOUT:
24283fe5ae8bSMike Christie 		spin_unlock_bh(&session->lock);
2429df4da5cdSMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
24303fe5ae8bSMike Christie 		goto done;
24313fe5ae8bSMike Christie 	default:
24323fe5ae8bSMike Christie 		conn->tmf_state = TMF_INITIAL;
24333fe5ae8bSMike Christie 		goto unlock;
24343fe5ae8bSMike Christie 	}
24353fe5ae8bSMike Christie 
24363fe5ae8bSMike Christie 	rc = SUCCESS;
24373fe5ae8bSMike Christie 	spin_unlock_bh(&session->lock);
24383fe5ae8bSMike Christie 
24393fe5ae8bSMike Christie 	iscsi_suspend_tx(conn);
24403fe5ae8bSMike Christie 
24413fe5ae8bSMike Christie 	spin_lock_bh(&session->lock);
24423fe5ae8bSMike Christie 	memset(hdr, 0, sizeof(*hdr));
24433fe5ae8bSMike Christie 	fail_scsi_tasks(conn, -1, DID_ERROR);
24443fe5ae8bSMike Christie 	conn->tmf_state = TMF_INITIAL;
24453fe5ae8bSMike Christie 	spin_unlock_bh(&session->lock);
24463fe5ae8bSMike Christie 
24473fe5ae8bSMike Christie 	iscsi_start_tx(conn);
24483fe5ae8bSMike Christie 	goto done;
24493fe5ae8bSMike Christie 
24503fe5ae8bSMike Christie unlock:
24513fe5ae8bSMike Christie 	spin_unlock_bh(&session->lock);
24523fe5ae8bSMike Christie done:
24533fe5ae8bSMike Christie 	ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
24543fe5ae8bSMike Christie 		     rc == SUCCESS ? "SUCCESS" : "FAILED");
24553fe5ae8bSMike Christie 	mutex_unlock(&session->eh_mutex);
2456309ce156SJayamohan Kallickal 	return rc;
2457309ce156SJayamohan Kallickal }
2458309ce156SJayamohan Kallickal EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
24593fe5ae8bSMike Christie 
2460309ce156SJayamohan Kallickal /**
2461309ce156SJayamohan Kallickal  * iscsi_eh_recover_target - reset target and possibly the session
2462309ce156SJayamohan Kallickal  * @sc: scsi command
2463309ce156SJayamohan Kallickal  *
2464309ce156SJayamohan Kallickal  * This will attempt to send a warm target reset. If that fails,
2465309ce156SJayamohan Kallickal  * we will escalate to ERL0 session recovery.
2466309ce156SJayamohan Kallickal  */
2467309ce156SJayamohan Kallickal int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2468309ce156SJayamohan Kallickal {
2469309ce156SJayamohan Kallickal 	int rc;
2470309ce156SJayamohan Kallickal 
2471309ce156SJayamohan Kallickal 	rc = iscsi_eh_target_reset(sc);
24723fe5ae8bSMike Christie 	if (rc == FAILED)
24733fe5ae8bSMike Christie 		rc = iscsi_eh_session_reset(sc);
24743fe5ae8bSMike Christie 	return rc;
24753fe5ae8bSMike Christie }
2476309ce156SJayamohan Kallickal EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
24773fe5ae8bSMike Christie 
24786320377fSOlaf Kirch /*
24796320377fSOlaf Kirch  * Pre-allocate a pool of @max items of @item_size. By default, the pool
24806320377fSOlaf Kirch  * should be accessed via kfifo_{get,put} on q->queue.
24816320377fSOlaf Kirch  * Optionally, the caller can obtain the array of object pointers
24826320377fSOlaf Kirch  * by passing in a non-NULL @items pointer
24836320377fSOlaf Kirch  */
24847996a778SMike Christie int
24856320377fSOlaf Kirch iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
24867996a778SMike Christie {
24876320377fSOlaf Kirch 	int i, num_arrays = 1;
24887996a778SMike Christie 
24896320377fSOlaf Kirch 	memset(q, 0, sizeof(*q));
24907996a778SMike Christie 
24917996a778SMike Christie 	q->max = max;
24926320377fSOlaf Kirch 
24936320377fSOlaf Kirch 	/* If the user passed an items pointer, he wants a copy of
24946320377fSOlaf Kirch 	 * the array. */
24956320377fSOlaf Kirch 	if (items)
24966320377fSOlaf Kirch 		num_arrays++;
24976320377fSOlaf Kirch 	q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
24986320377fSOlaf Kirch 	if (q->pool == NULL)
2499f474a37bSJean Delvare 		return -ENOMEM;
25007996a778SMike Christie 
2501c1e13f25SStefani Seibold 	kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
25027996a778SMike Christie 
25037996a778SMike Christie 	for (i = 0; i < max; i++) {
25046320377fSOlaf Kirch 		q->pool[i] = kzalloc(item_size, GFP_KERNEL);
25057996a778SMike Christie 		if (q->pool[i] == NULL) {
25066320377fSOlaf Kirch 			q->max = i;
25076320377fSOlaf Kirch 			goto enomem;
25087996a778SMike Christie 		}
25097acd72ebSStefani Seibold 		kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
25107996a778SMike Christie 	}
25116320377fSOlaf Kirch 
25126320377fSOlaf Kirch 	if (items) {
25136320377fSOlaf Kirch 		*items = q->pool + max;
25146320377fSOlaf Kirch 		memcpy(*items, q->pool, max * sizeof(void *));
25156320377fSOlaf Kirch 	}
25166320377fSOlaf Kirch 
25177996a778SMike Christie 	return 0;
25186320377fSOlaf Kirch 
25196320377fSOlaf Kirch enomem:
25206320377fSOlaf Kirch 	iscsi_pool_free(q);
25216320377fSOlaf Kirch 	return -ENOMEM;
25227996a778SMike Christie }
25237996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_init);
25247996a778SMike Christie 
25256320377fSOlaf Kirch void iscsi_pool_free(struct iscsi_pool *q)
25267996a778SMike Christie {
25277996a778SMike Christie 	int i;
25287996a778SMike Christie 
25297996a778SMike Christie 	for (i = 0; i < q->max; i++)
25306320377fSOlaf Kirch 		kfree(q->pool[i]);
25317996a778SMike Christie 	kfree(q->pool);
25327996a778SMike Christie }
25337996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_free);
25347996a778SMike Christie 
2535a4804cd6SMike Christie /**
2536a4804cd6SMike Christie  * iscsi_host_add - add host to system
2537a4804cd6SMike Christie  * @shost: scsi host
2538a4804cd6SMike Christie  * @pdev: parent device
2539a4804cd6SMike Christie  *
2540a4804cd6SMike Christie  * This should be called by partial offload and software iscsi drivers
2541a4804cd6SMike Christie  * to add a host to the system.
2542a4804cd6SMike Christie  */
2543a4804cd6SMike Christie int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
25447996a778SMike Christie {
25458e9a20ceSMike Christie 	if (!shost->can_queue)
25468e9a20ceSMike Christie 		shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
25478e9a20ceSMike Christie 
25484d108350SMike Christie 	if (!shost->cmd_per_lun)
25494d108350SMike Christie 		shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
25504d108350SMike Christie 
2551308cec14SMike Christie 	if (!shost->transportt->eh_timed_out)
2552308cec14SMike Christie 		shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
2553a4804cd6SMike Christie 	return scsi_add_host(shost, pdev);
2554a4804cd6SMike Christie }
2555a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_add);
2556a4804cd6SMike Christie 
2557a4804cd6SMike Christie /**
2558a4804cd6SMike Christie  * iscsi_host_alloc - allocate a host and driver data
2559a4804cd6SMike Christie  * @sht: scsi host template
2560a4804cd6SMike Christie  * @dd_data_size: driver host data size
256132ae763eSMike Christie  * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2562a4804cd6SMike Christie  *
2563a4804cd6SMike Christie  * This should be called by partial offload and software iscsi drivers.
2564a4804cd6SMike Christie  * To access the driver specific memory use the iscsi_host_priv() macro.
2565a4804cd6SMike Christie  */
2566a4804cd6SMike Christie struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
25674d108350SMike Christie 				   int dd_data_size, bool xmit_can_sleep)
2568a4804cd6SMike Christie {
2569a4804cd6SMike Christie 	struct Scsi_Host *shost;
2570e5bd7b54SMike Christie 	struct iscsi_host *ihost;
2571a4804cd6SMike Christie 
2572a4804cd6SMike Christie 	shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2573a4804cd6SMike Christie 	if (!shost)
2574a4804cd6SMike Christie 		return NULL;
2575e5bd7b54SMike Christie 	ihost = shost_priv(shost);
257632ae763eSMike Christie 
257732ae763eSMike Christie 	if (xmit_can_sleep) {
257832ae763eSMike Christie 		snprintf(ihost->workq_name, sizeof(ihost->workq_name),
257932ae763eSMike Christie 			"iscsi_q_%d", shost->host_no);
258032ae763eSMike Christie 		ihost->workq = create_singlethread_workqueue(ihost->workq_name);
258132ae763eSMike Christie 		if (!ihost->workq)
258232ae763eSMike Christie 			goto free_host;
258332ae763eSMike Christie 	}
258432ae763eSMike Christie 
2585e5bd7b54SMike Christie 	spin_lock_init(&ihost->lock);
2586e5bd7b54SMike Christie 	ihost->state = ISCSI_HOST_SETUP;
2587e5bd7b54SMike Christie 	ihost->num_sessions = 0;
2588e5bd7b54SMike Christie 	init_waitqueue_head(&ihost->session_removal_wq);
2589a4804cd6SMike Christie 	return shost;
259032ae763eSMike Christie 
259132ae763eSMike Christie free_host:
259232ae763eSMike Christie 	scsi_host_put(shost);
259332ae763eSMike Christie 	return NULL;
259475613521SMike Christie }
2595a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_alloc);
259675613521SMike Christie 
2597e5bd7b54SMike Christie static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2598e5bd7b54SMike Christie {
259940a06e75SMike Christie 	iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2600e5bd7b54SMike Christie }
2601e5bd7b54SMike Christie 
2602a4804cd6SMike Christie /**
2603a4804cd6SMike Christie  * iscsi_host_remove - remove host and sessions
2604a4804cd6SMike Christie  * @shost: scsi host
2605a4804cd6SMike Christie  *
2606e5bd7b54SMike Christie  * If there are any sessions left, this will initiate the removal and wait
2607e5bd7b54SMike Christie  * for the completion.
2608a4804cd6SMike Christie  */
2609a4804cd6SMike Christie void iscsi_host_remove(struct Scsi_Host *shost)
2610a4804cd6SMike Christie {
2611e5bd7b54SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
2612e5bd7b54SMike Christie 	unsigned long flags;
2613e5bd7b54SMike Christie 
2614e5bd7b54SMike Christie 	spin_lock_irqsave(&ihost->lock, flags);
2615e5bd7b54SMike Christie 	ihost->state = ISCSI_HOST_REMOVED;
2616e5bd7b54SMike Christie 	spin_unlock_irqrestore(&ihost->lock, flags);
2617e5bd7b54SMike Christie 
2618e5bd7b54SMike Christie 	iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2619e5bd7b54SMike Christie 	wait_event_interruptible(ihost->session_removal_wq,
2620e5bd7b54SMike Christie 				 ihost->num_sessions == 0);
2621e5bd7b54SMike Christie 	if (signal_pending(current))
2622e5bd7b54SMike Christie 		flush_signals(current);
2623e5bd7b54SMike Christie 
2624a4804cd6SMike Christie 	scsi_remove_host(shost);
262532ae763eSMike Christie 	if (ihost->workq)
262632ae763eSMike Christie 		destroy_workqueue(ihost->workq);
2627a4804cd6SMike Christie }
2628a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_remove);
2629a4804cd6SMike Christie 
2630a4804cd6SMike Christie void iscsi_host_free(struct Scsi_Host *shost)
263175613521SMike Christie {
263275613521SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
263375613521SMike Christie 
263475613521SMike Christie 	kfree(ihost->netdev);
263575613521SMike Christie 	kfree(ihost->hwaddress);
263675613521SMike Christie 	kfree(ihost->initiatorname);
2637a4804cd6SMike Christie 	scsi_host_put(shost);
263875613521SMike Christie }
2639a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_free);
264075613521SMike Christie 
2641e5bd7b54SMike Christie static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2642e5bd7b54SMike Christie {
2643e5bd7b54SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
2644e5bd7b54SMike Christie 	unsigned long flags;
2645e5bd7b54SMike Christie 
2646e5bd7b54SMike Christie 	shost = scsi_host_get(shost);
2647e5bd7b54SMike Christie 	if (!shost) {
2648e5bd7b54SMike Christie 		printk(KERN_ERR "Invalid state. Cannot notify host removal "
2649e5bd7b54SMike Christie 		      "of session teardown event because host already "
2650e5bd7b54SMike Christie 		      "removed.\n");
2651e5bd7b54SMike Christie 		return;
2652e5bd7b54SMike Christie 	}
2653e5bd7b54SMike Christie 
2654e5bd7b54SMike Christie 	spin_lock_irqsave(&ihost->lock, flags);
2655e5bd7b54SMike Christie 	ihost->num_sessions--;
2656e5bd7b54SMike Christie 	if (ihost->num_sessions == 0)
2657e5bd7b54SMike Christie 		wake_up(&ihost->session_removal_wq);
2658e5bd7b54SMike Christie 	spin_unlock_irqrestore(&ihost->lock, flags);
2659e5bd7b54SMike Christie 	scsi_host_put(shost);
2660e5bd7b54SMike Christie }
2661e5bd7b54SMike Christie 
266275613521SMike Christie /**
266375613521SMike Christie  * iscsi_session_setup - create iscsi cls session and host and session
266475613521SMike Christie  * @iscsit: iscsi transport template
266575613521SMike Christie  * @shost: scsi host
266675613521SMike Christie  * @cmds_max: session can queue
26679c19a7d0SMike Christie  * @cmd_task_size: LLD task private data size
266875613521SMike Christie  * @initial_cmdsn: initial CmdSN
266975613521SMike Christie  *
267075613521SMike Christie  * This can be used by software iscsi_transports that allocate
267175613521SMike Christie  * a session per scsi host.
26723cf7b233SMike Christie  *
26733cf7b233SMike Christie  * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
26743cf7b233SMike Christie  * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
26753cf7b233SMike Christie  * for nop handling and login/logout requests.
267675613521SMike Christie  */
267775613521SMike Christie struct iscsi_cls_session *
267875613521SMike Christie iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2679b8b9e1b8SJayamohan Kallickal 		    uint16_t cmds_max, int dd_size, int cmd_task_size,
26807970634bSMike Christie 		    uint32_t initial_cmdsn, unsigned int id)
268175613521SMike Christie {
2682e5bd7b54SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
268375613521SMike Christie 	struct iscsi_session *session;
268475613521SMike Christie 	struct iscsi_cls_session *cls_session;
26853cf7b233SMike Christie 	int cmd_i, scsi_cmds, total_cmds = cmds_max;
2686e5bd7b54SMike Christie 	unsigned long flags;
2687e5bd7b54SMike Christie 
2688e5bd7b54SMike Christie 	spin_lock_irqsave(&ihost->lock, flags);
2689e5bd7b54SMike Christie 	if (ihost->state == ISCSI_HOST_REMOVED) {
2690e5bd7b54SMike Christie 		spin_unlock_irqrestore(&ihost->lock, flags);
2691e5bd7b54SMike Christie 		return NULL;
2692e5bd7b54SMike Christie 	}
2693e5bd7b54SMike Christie 	ihost->num_sessions++;
2694e5bd7b54SMike Christie 	spin_unlock_irqrestore(&ihost->lock, flags);
26958e9a20ceSMike Christie 
26968e9a20ceSMike Christie 	if (!total_cmds)
26978e9a20ceSMike Christie 		total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
26983e5c28adSMike Christie 	/*
26993cf7b233SMike Christie 	 * The iscsi layer needs some tasks for nop handling and tmfs,
27003cf7b233SMike Christie 	 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
27013cf7b233SMike Christie 	 * + 1 command for scsi IO.
27023e5c28adSMike Christie 	 */
27033cf7b233SMike Christie 	if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
27043cf7b233SMike Christie 		printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
27053cf7b233SMike Christie 		       "must be a power of two that is at least %d.\n",
27063cf7b233SMike Christie 		       total_cmds, ISCSI_TOTAL_CMDS_MIN);
2707e5bd7b54SMike Christie 		goto dec_session_count;
27081548271eSMike Christie 	}
27093cf7b233SMike Christie 
27103cf7b233SMike Christie 	if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
27113cf7b233SMike Christie 		printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
27123cf7b233SMike Christie 		       "must be a power of 2 less than or equal to %d.\n",
27133cf7b233SMike Christie 		       cmds_max, ISCSI_TOTAL_CMDS_MAX);
27143cf7b233SMike Christie 		total_cmds = ISCSI_TOTAL_CMDS_MAX;
27153cf7b233SMike Christie 	}
27163cf7b233SMike Christie 
27173cf7b233SMike Christie 	if (!is_power_of_2(total_cmds)) {
27183cf7b233SMike Christie 		printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
27193cf7b233SMike Christie 		       "must be a power of 2.\n", total_cmds);
27203cf7b233SMike Christie 		total_cmds = rounddown_pow_of_two(total_cmds);
27213cf7b233SMike Christie 		if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
27223cf7b233SMike Christie 			return NULL;
27233cf7b233SMike Christie 		printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
27243cf7b233SMike Christie 		       total_cmds);
27253cf7b233SMike Christie 	}
27263cf7b233SMike Christie 	scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
27271548271eSMike Christie 
27285d91e209SMike Christie 	cls_session = iscsi_alloc_session(shost, iscsit,
2729b8b9e1b8SJayamohan Kallickal 					  sizeof(struct iscsi_session) +
2730b8b9e1b8SJayamohan Kallickal 					  dd_size);
273175613521SMike Christie 	if (!cls_session)
2732e5bd7b54SMike Christie 		goto dec_session_count;
273375613521SMike Christie 	session = cls_session->dd_data;
273475613521SMike Christie 	session->cls_session = cls_session;
27357996a778SMike Christie 	session->host = shost;
27367996a778SMike Christie 	session->state = ISCSI_STATE_FREE;
2737f6d5180cSMike Christie 	session->fast_abort = 1;
27383fe5ae8bSMike Christie 	session->tgt_reset_timeout = 30;
27394cd49ea1SMike Christie 	session->lu_reset_timeout = 15;
27404cd49ea1SMike Christie 	session->abort_timeout = 10;
27413cf7b233SMike Christie 	session->scsi_cmds_max = scsi_cmds;
27423cf7b233SMike Christie 	session->cmds_max = total_cmds;
2743e0726407SMike Christie 	session->queued_cmdsn = session->cmdsn = initial_cmdsn;
27447996a778SMike Christie 	session->exp_cmdsn = initial_cmdsn + 1;
27457996a778SMike Christie 	session->max_cmdsn = initial_cmdsn + 1;
27467996a778SMike Christie 	session->max_r2t = 1;
27477996a778SMike Christie 	session->tt = iscsit;
2748b8b9e1b8SJayamohan Kallickal 	session->dd_data = cls_session->dd_data + sizeof(*session);
27496724add1SMike Christie 	mutex_init(&session->eh_mutex);
275075613521SMike Christie 	spin_lock_init(&session->lock);
27517996a778SMike Christie 
27527996a778SMike Christie 	/* initialize SCSI PDU commands pool */
27537996a778SMike Christie 	if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
27547996a778SMike Christie 			    (void***)&session->cmds,
27559c19a7d0SMike Christie 			    cmd_task_size + sizeof(struct iscsi_task)))
27567996a778SMike Christie 		goto cmdpool_alloc_fail;
27577996a778SMike Christie 
27587996a778SMike Christie 	/* pre-format cmds pool with ITT */
27597996a778SMike Christie 	for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
27609c19a7d0SMike Christie 		struct iscsi_task *task = session->cmds[cmd_i];
27617996a778SMike Christie 
27629c19a7d0SMike Christie 		if (cmd_task_size)
27639c19a7d0SMike Christie 			task->dd_data = &task[1];
27649c19a7d0SMike Christie 		task->itt = cmd_i;
27653bbaaad9SMike Christie 		task->state = ISCSI_TASK_FREE;
27669c19a7d0SMike Christie 		INIT_LIST_HEAD(&task->running);
27677996a778SMike Christie 	}
27687996a778SMike Christie 
2769f53a88daSMike Christie 	if (!try_module_get(iscsit->owner))
277075613521SMike Christie 		goto module_get_fail;
277175613521SMike Christie 
27727970634bSMike Christie 	if (iscsi_add_session(cls_session, id))
2773f53a88daSMike Christie 		goto cls_session_fail;
2774e5bd7b54SMike Christie 
27757996a778SMike Christie 	return cls_session;
27767996a778SMike Christie 
27777996a778SMike Christie cls_session_fail:
277875613521SMike Christie 	module_put(iscsit->owner);
277975613521SMike Christie module_get_fail:
27806320377fSOlaf Kirch 	iscsi_pool_free(&session->cmdpool);
27817996a778SMike Christie cmdpool_alloc_fail:
278275613521SMike Christie 	iscsi_free_session(cls_session);
2783e5bd7b54SMike Christie dec_session_count:
2784e5bd7b54SMike Christie 	iscsi_host_dec_session_cnt(shost);
27857996a778SMike Christie 	return NULL;
27867996a778SMike Christie }
27877996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_setup);
27887996a778SMike Christie 
27897996a778SMike Christie /**
27907996a778SMike Christie  * iscsi_session_teardown - destroy session, host, and cls_session
279175613521SMike Christie  * @cls_session: iscsi session
27927996a778SMike Christie  *
279375613521SMike Christie  * The driver must have called iscsi_remove_session before
279475613521SMike Christie  * calling this.
279575613521SMike Christie  */
27967996a778SMike Christie void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
27977996a778SMike Christie {
279875613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
279963f75cc8SMike Christie 	struct module *owner = cls_session->transport->owner;
2800e5bd7b54SMike Christie 	struct Scsi_Host *shost = session->host;
28017996a778SMike Christie 
28026320377fSOlaf Kirch 	iscsi_pool_free(&session->cmdpool);
28037996a778SMike Christie 
2804b2c64167SMike Christie 	kfree(session->password);
2805b2c64167SMike Christie 	kfree(session->password_in);
2806b2c64167SMike Christie 	kfree(session->username);
2807b2c64167SMike Christie 	kfree(session->username_in);
2808f3ff0c36SMike Christie 	kfree(session->targetname);
28093c5c4801SVikas Chaudhary 	kfree(session->targetalias);
281088dfd340SMike Christie 	kfree(session->initiatorname);
28113b9373e9SEddie Wai 	kfree(session->boot_root);
28123b9373e9SEddie Wai 	kfree(session->boot_nic);
28133b9373e9SEddie Wai 	kfree(session->boot_target);
281488dfd340SMike Christie 	kfree(session->ifacename);
2815f8525eb4SAdheer Chandravanshi 	kfree(session->portal_type);
2816f8525eb4SAdheer Chandravanshi 	kfree(session->discovery_parent_type);
2817f3ff0c36SMike Christie 
281875613521SMike Christie 	iscsi_destroy_session(cls_session);
2819e5bd7b54SMike Christie 	iscsi_host_dec_session_cnt(shost);
282063f75cc8SMike Christie 	module_put(owner);
28217996a778SMike Christie }
28227996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_teardown);
28237996a778SMike Christie 
28247996a778SMike Christie /**
28257996a778SMike Christie  * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
28267996a778SMike Christie  * @cls_session: iscsi_cls_session
28275d91e209SMike Christie  * @dd_size: private driver data size
28287996a778SMike Christie  * @conn_idx: cid
28295d91e209SMike Christie  */
28307996a778SMike Christie struct iscsi_cls_conn *
28315d91e209SMike Christie iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
28325d91e209SMike Christie 		 uint32_t conn_idx)
28337996a778SMike Christie {
283475613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
28357996a778SMike Christie 	struct iscsi_conn *conn;
28367996a778SMike Christie 	struct iscsi_cls_conn *cls_conn;
2837d36ab6f3SMike Christie 	char *data;
28387996a778SMike Christie 
28395d91e209SMike Christie 	cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
28405d91e209SMike Christie 				     conn_idx);
28417996a778SMike Christie 	if (!cls_conn)
28427996a778SMike Christie 		return NULL;
28437996a778SMike Christie 	conn = cls_conn->dd_data;
28445d91e209SMike Christie 	memset(conn, 0, sizeof(*conn) + dd_size);
28457996a778SMike Christie 
28465d91e209SMike Christie 	conn->dd_data = cls_conn->dd_data + sizeof(*conn);
28477996a778SMike Christie 	conn->session = session;
28487996a778SMike Christie 	conn->cls_conn = cls_conn;
28497996a778SMike Christie 	conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
28507996a778SMike Christie 	conn->id = conn_idx;
28517996a778SMike Christie 	conn->exp_statsn = 0;
2852843c0a8aSMike Christie 	conn->tmf_state = TMF_INITIAL;
2853f6d5180cSMike Christie 
2854f6d5180cSMike Christie 	init_timer(&conn->transport_timer);
2855f6d5180cSMike Christie 	conn->transport_timer.data = (unsigned long)conn;
2856f6d5180cSMike Christie 	conn->transport_timer.function = iscsi_check_transport_timeouts;
2857f6d5180cSMike Christie 
2858843c0a8aSMike Christie 	INIT_LIST_HEAD(&conn->mgmtqueue);
28593bbaaad9SMike Christie 	INIT_LIST_HEAD(&conn->cmdqueue);
2860843c0a8aSMike Christie 	INIT_LIST_HEAD(&conn->requeue);
2861c4028958SDavid Howells 	INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
28627996a778SMike Christie 
28639c19a7d0SMike Christie 	/* allocate login_task used for the login/text sequences */
28647996a778SMike Christie 	spin_lock_bh(&session->lock);
28657acd72ebSStefani Seibold 	if (!kfifo_out(&session->cmdpool.queue,
28669c19a7d0SMike Christie                          (void*)&conn->login_task,
28677996a778SMike Christie 			 sizeof(void*))) {
28687996a778SMike Christie 		spin_unlock_bh(&session->lock);
28699c19a7d0SMike Christie 		goto login_task_alloc_fail;
28707996a778SMike Christie 	}
28717996a778SMike Christie 	spin_unlock_bh(&session->lock);
28727996a778SMike Christie 
2873cfeb2cf9SMike Christie 	data = (char *) __get_free_pages(GFP_KERNEL,
2874cfeb2cf9SMike Christie 					 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2875d36ab6f3SMike Christie 	if (!data)
28769c19a7d0SMike Christie 		goto login_task_data_alloc_fail;
28779c19a7d0SMike Christie 	conn->login_task->data = conn->data = data;
2878d36ab6f3SMike Christie 
2879843c0a8aSMike Christie 	init_timer(&conn->tmf_timer);
28807996a778SMike Christie 	init_waitqueue_head(&conn->ehwait);
28817996a778SMike Christie 
28827996a778SMike Christie 	return cls_conn;
28837996a778SMike Christie 
28849c19a7d0SMike Christie login_task_data_alloc_fail:
28857acd72ebSStefani Seibold 	kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2886d36ab6f3SMike Christie 		    sizeof(void*));
28879c19a7d0SMike Christie login_task_alloc_fail:
28887996a778SMike Christie 	iscsi_destroy_conn(cls_conn);
28897996a778SMike Christie 	return NULL;
28907996a778SMike Christie }
28917996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_setup);
28927996a778SMike Christie 
28937996a778SMike Christie /**
28947996a778SMike Christie  * iscsi_conn_teardown - teardown iscsi connection
28957996a778SMike Christie  * cls_conn: iscsi class connection
28967996a778SMike Christie  *
28977996a778SMike Christie  * TODO: we may need to make this into a two step process
28987996a778SMike Christie  * like scsi-mls remove + put host
28997996a778SMike Christie  */
29007996a778SMike Christie void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
29017996a778SMike Christie {
29027996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
29037996a778SMike Christie 	struct iscsi_session *session = conn->session;
29047996a778SMike Christie 	unsigned long flags;
29057996a778SMike Christie 
2906f6d5180cSMike Christie 	del_timer_sync(&conn->transport_timer);
2907f6d5180cSMike Christie 
29087996a778SMike Christie 	spin_lock_bh(&session->lock);
29097996a778SMike Christie 	conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
29107996a778SMike Christie 	if (session->leadconn == conn) {
29117996a778SMike Christie 		/*
29127996a778SMike Christie 		 * leading connection? then give up on recovery.
29137996a778SMike Christie 		 */
29147996a778SMike Christie 		session->state = ISCSI_STATE_TERMINATE;
29157996a778SMike Christie 		wake_up(&conn->ehwait);
29167996a778SMike Christie 	}
29177996a778SMike Christie 	spin_unlock_bh(&session->lock);
29187996a778SMike Christie 
29197996a778SMike Christie 	/*
29207996a778SMike Christie 	 * Block until all in-progress commands for this connection
29217996a778SMike Christie 	 * time out or fail.
29227996a778SMike Christie 	 */
29237996a778SMike Christie 	for (;;) {
29247996a778SMike Christie 		spin_lock_irqsave(session->host->host_lock, flags);
29257996a778SMike Christie 		if (!session->host->host_busy) { /* OK for ERL == 0 */
29267996a778SMike Christie 			spin_unlock_irqrestore(session->host->host_lock, flags);
29277996a778SMike Christie 			break;
29287996a778SMike Christie 		}
29297996a778SMike Christie 		spin_unlock_irqrestore(session->host->host_lock, flags);
29307996a778SMike Christie 		msleep_interruptible(500);
2931322d739dSMike Christie 		iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2932322d739dSMike Christie 				  "host_busy %d host_failed %d\n",
2933322d739dSMike Christie 				  session->host->host_busy,
2934be2df72eSOr Gerlitz 				  session->host->host_failed);
29357996a778SMike Christie 		/*
29367996a778SMike Christie 		 * force eh_abort() to unblock
29377996a778SMike Christie 		 */
29387996a778SMike Christie 		wake_up(&conn->ehwait);
29397996a778SMike Christie 	}
29407996a778SMike Christie 
2941779ea120SMike Christie 	/* flush queued up work because we free the connection below */
2942843c0a8aSMike Christie 	iscsi_suspend_tx(conn);
2943779ea120SMike Christie 
29447996a778SMike Christie 	spin_lock_bh(&session->lock);
2945cfeb2cf9SMike Christie 	free_pages((unsigned long) conn->data,
2946cfeb2cf9SMike Christie 		   get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2947f3ff0c36SMike Christie 	kfree(conn->persistent_address);
2948ae56ff40SAdheer Chandravanshi 	kfree(conn->local_ipaddr);
29497acd72ebSStefani Seibold 	kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
29507996a778SMike Christie 		    sizeof(void*));
2951e0726407SMike Christie 	if (session->leadconn == conn)
29527996a778SMike Christie 		session->leadconn = NULL;
29537996a778SMike Christie 	spin_unlock_bh(&session->lock);
29547996a778SMike Christie 
29557996a778SMike Christie 	iscsi_destroy_conn(cls_conn);
29567996a778SMike Christie }
29577996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
29587996a778SMike Christie 
29597996a778SMike Christie int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
29607996a778SMike Christie {
29617996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
29627996a778SMike Christie 	struct iscsi_session *session = conn->session;
29637996a778SMike Christie 
2964ffd0436eSMike Christie 	if (!session) {
2965322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
2966322d739dSMike Christie 				  "can't start unbound connection\n");
29677996a778SMike Christie 		return -EPERM;
29687996a778SMike Christie 	}
29697996a778SMike Christie 
2970db98ccdeSMike Christie 	if ((session->imm_data_en || !session->initial_r2t_en) &&
2971db98ccdeSMike Christie 	     session->first_burst > session->max_burst) {
2972322d739dSMike Christie 		iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2973ffd0436eSMike Christie 				  "first_burst %d max_burst %d\n",
2974ffd0436eSMike Christie 				  session->first_burst, session->max_burst);
2975ffd0436eSMike Christie 		return -EINVAL;
2976ffd0436eSMike Christie 	}
2977ffd0436eSMike Christie 
2978f6d5180cSMike Christie 	if (conn->ping_timeout && !conn->recv_timeout) {
2979322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2980322d739dSMike Christie 				  "zero. Using 5 seconds\n.");
2981f6d5180cSMike Christie 		conn->recv_timeout = 5;
2982f6d5180cSMike Christie 	}
2983f6d5180cSMike Christie 
2984f6d5180cSMike Christie 	if (conn->recv_timeout && !conn->ping_timeout) {
2985322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2986322d739dSMike Christie 				  "zero. Using 5 seconds.\n");
2987f6d5180cSMike Christie 		conn->ping_timeout = 5;
2988f6d5180cSMike Christie 	}
2989f6d5180cSMike Christie 
29907996a778SMike Christie 	spin_lock_bh(&session->lock);
29917996a778SMike Christie 	conn->c_stage = ISCSI_CONN_STARTED;
29927996a778SMike Christie 	session->state = ISCSI_STATE_LOGGED_IN;
2993e0726407SMike Christie 	session->queued_cmdsn = session->cmdsn;
29947996a778SMike Christie 
2995f6d5180cSMike Christie 	conn->last_recv = jiffies;
2996f6d5180cSMike Christie 	conn->last_ping = jiffies;
2997f6d5180cSMike Christie 	if (conn->recv_timeout && conn->ping_timeout)
2998f6d5180cSMike Christie 		mod_timer(&conn->transport_timer,
2999f6d5180cSMike Christie 			  jiffies + (conn->recv_timeout * HZ));
3000f6d5180cSMike Christie 
30017996a778SMike Christie 	switch(conn->stop_stage) {
30027996a778SMike Christie 	case STOP_CONN_RECOVER:
30037996a778SMike Christie 		/*
30047996a778SMike Christie 		 * unblock eh_abort() if it is blocked. re-try all
30057996a778SMike Christie 		 * commands after successful recovery
30067996a778SMike Christie 		 */
30077996a778SMike Christie 		conn->stop_stage = 0;
3008843c0a8aSMike Christie 		conn->tmf_state = TMF_INITIAL;
30097996a778SMike Christie 		session->age++;
30108b1d0343SMike Christie 		if (session->age == 16)
30118b1d0343SMike Christie 			session->age = 0;
30126eabafbeSMike Christie 		break;
30137996a778SMike Christie 	case STOP_CONN_TERM:
30147996a778SMike Christie 		conn->stop_stage = 0;
30157996a778SMike Christie 		break;
30167996a778SMike Christie 	default:
30177996a778SMike Christie 		break;
30187996a778SMike Christie 	}
30197996a778SMike Christie 	spin_unlock_bh(&session->lock);
30207996a778SMike Christie 
302175613521SMike Christie 	iscsi_unblock_session(session->cls_session);
30226eabafbeSMike Christie 	wake_up(&conn->ehwait);
30237996a778SMike Christie 	return 0;
30247996a778SMike Christie }
30257996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_start);
30267996a778SMike Christie 
30277996a778SMike Christie static void
30283bbaaad9SMike Christie fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
30297996a778SMike Christie {
30303bbaaad9SMike Christie 	struct iscsi_task *task;
3031b3cd5050SMike Christie 	int i, state;
30327996a778SMike Christie 
30333bbaaad9SMike Christie 	for (i = 0; i < conn->session->cmds_max; i++) {
30343bbaaad9SMike Christie 		task = conn->session->cmds[i];
30353bbaaad9SMike Christie 		if (task->sc)
30363bbaaad9SMike Christie 			continue;
30373bbaaad9SMike Christie 
30383bbaaad9SMike Christie 		if (task->state == ISCSI_TASK_FREE)
30393bbaaad9SMike Christie 			continue;
30403bbaaad9SMike Christie 
30413bbaaad9SMike Christie 		ISCSI_DBG_SESSION(conn->session,
30423bbaaad9SMike Christie 				  "failing mgmt itt 0x%x state %d\n",
30433bbaaad9SMike Christie 				  task->itt, task->state);
3044b3cd5050SMike Christie 		state = ISCSI_TASK_ABRT_SESS_RECOV;
3045b3cd5050SMike Christie 		if (task->state == ISCSI_TASK_PENDING)
3046b3cd5050SMike Christie 			state = ISCSI_TASK_COMPLETED;
3047b3cd5050SMike Christie 		iscsi_complete_task(task, state);
3048b3cd5050SMike Christie 
30497996a778SMike Christie 	}
30507996a778SMike Christie }
30517996a778SMike Christie 
3052656cffc9SMike Christie static void iscsi_start_session_recovery(struct iscsi_session *session,
30537996a778SMike Christie 					 struct iscsi_conn *conn, int flag)
30547996a778SMike Christie {
3055ed2abc7fSMike Christie 	int old_stop_stage;
3056ed2abc7fSMike Christie 
30576724add1SMike Christie 	mutex_lock(&session->eh_mutex);
30587996a778SMike Christie 	spin_lock_bh(&session->lock);
3059ed2abc7fSMike Christie 	if (conn->stop_stage == STOP_CONN_TERM) {
30607996a778SMike Christie 		spin_unlock_bh(&session->lock);
30616724add1SMike Christie 		mutex_unlock(&session->eh_mutex);
30626724add1SMike Christie 		return;
30636724add1SMike Christie 	}
30646724add1SMike Christie 
30656724add1SMike Christie 	/*
3066ed2abc7fSMike Christie 	 * When this is called for the in_login state, we only want to clean
30679c19a7d0SMike Christie 	 * up the login task and connection. We do not need to block and set
306867a61114SMike Christie 	 * the recovery state again
3069ed2abc7fSMike Christie 	 */
307067a61114SMike Christie 	if (flag == STOP_CONN_TERM)
307167a61114SMike Christie 		session->state = ISCSI_STATE_TERMINATE;
307267a61114SMike Christie 	else if (conn->stop_stage != STOP_CONN_RECOVER)
307367a61114SMike Christie 		session->state = ISCSI_STATE_IN_RECOVERY;
30744ae0a6c1SMike Christie 
30754ae0a6c1SMike Christie 	old_stop_stage = conn->stop_stage;
30764ae0a6c1SMike Christie 	conn->stop_stage = flag;
307726013ad4SMike Christie 	spin_unlock_bh(&session->lock);
3078ed2abc7fSMike Christie 
307926013ad4SMike Christie 	del_timer_sync(&conn->transport_timer);
308026013ad4SMike Christie 	iscsi_suspend_tx(conn);
308126013ad4SMike Christie 
308226013ad4SMike Christie 	spin_lock_bh(&session->lock);
308367a61114SMike Christie 	conn->c_stage = ISCSI_CONN_STOPPED;
30847996a778SMike Christie 	spin_unlock_bh(&session->lock);
30856724add1SMike Christie 
30867996a778SMike Christie 	/*
30877996a778SMike Christie 	 * for connection level recovery we should not calculate
30887996a778SMike Christie 	 * header digest. conn->hdr_size used for optimization
30897996a778SMike Christie 	 * in hdr_extract() and will be re-negotiated at
30907996a778SMike Christie 	 * set_param() time.
30917996a778SMike Christie 	 */
30927996a778SMike Christie 	if (flag == STOP_CONN_RECOVER) {
30937996a778SMike Christie 		conn->hdrdgst_en = 0;
30947996a778SMike Christie 		conn->datadgst_en = 0;
3095656cffc9SMike Christie 		if (session->state == ISCSI_STATE_IN_RECOVERY &&
309667a61114SMike Christie 		    old_stop_stage != STOP_CONN_RECOVER) {
30971b2c7af8SMike Christie 			ISCSI_DBG_SESSION(session, "blocking session\n");
309875613521SMike Christie 			iscsi_block_session(session->cls_session);
30997996a778SMike Christie 		}
310067a61114SMike Christie 	}
3101656cffc9SMike Christie 
3102656cffc9SMike Christie 	/*
3103656cffc9SMike Christie 	 * flush queues.
3104656cffc9SMike Christie 	 */
3105656cffc9SMike Christie 	spin_lock_bh(&session->lock);
31063bbaaad9SMike Christie 	fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
31073bbaaad9SMike Christie 	fail_mgmt_tasks(session, conn);
31085d12c05eSMike Christie 	memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
3109656cffc9SMike Christie 	spin_unlock_bh(&session->lock);
31106724add1SMike Christie 	mutex_unlock(&session->eh_mutex);
31117996a778SMike Christie }
31127996a778SMike Christie 
31137996a778SMike Christie void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
31147996a778SMike Christie {
31157996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
31167996a778SMike Christie 	struct iscsi_session *session = conn->session;
31177996a778SMike Christie 
31187996a778SMike Christie 	switch (flag) {
31197996a778SMike Christie 	case STOP_CONN_RECOVER:
31207996a778SMike Christie 	case STOP_CONN_TERM:
31217996a778SMike Christie 		iscsi_start_session_recovery(session, conn, flag);
31228d2860b3SMike Christie 		break;
31237996a778SMike Christie 	default:
3124322d739dSMike Christie 		iscsi_conn_printk(KERN_ERR, conn,
3125322d739dSMike Christie 				  "invalid stop flag %d\n", flag);
31267996a778SMike Christie 	}
31277996a778SMike Christie }
31287996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_stop);
31297996a778SMike Christie 
31307996a778SMike Christie int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
31317996a778SMike Christie 		    struct iscsi_cls_conn *cls_conn, int is_leading)
31327996a778SMike Christie {
313375613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
313498644047SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
31357996a778SMike Christie 
31367996a778SMike Christie 	spin_lock_bh(&session->lock);
31377996a778SMike Christie 	if (is_leading)
31387996a778SMike Christie 		session->leadconn = conn;
313998644047SMike Christie 	spin_unlock_bh(&session->lock);
31407996a778SMike Christie 
31417996a778SMike Christie 	/*
31427996a778SMike Christie 	 * Unblock xmitworker(), Login Phase will pass through.
31437996a778SMike Christie 	 */
31447996a778SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
31457996a778SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
31467996a778SMike Christie 	return 0;
31477996a778SMike Christie }
31487996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_bind);
31497996a778SMike Christie 
3150adaf6990SAdheer Chandravanshi int iscsi_switch_str_param(char **param, char *new_val_buf)
31515700b1afSMike Christie {
31525700b1afSMike Christie 	char *new_val;
31535700b1afSMike Christie 
31545700b1afSMike Christie 	if (*param) {
31555700b1afSMike Christie 		if (!strcmp(*param, new_val_buf))
31565700b1afSMike Christie 			return 0;
31575700b1afSMike Christie 	}
31585700b1afSMike Christie 
31595700b1afSMike Christie 	new_val = kstrdup(new_val_buf, GFP_NOIO);
31605700b1afSMike Christie 	if (!new_val)
31615700b1afSMike Christie 		return -ENOMEM;
31625700b1afSMike Christie 
31635700b1afSMike Christie 	kfree(*param);
31645700b1afSMike Christie 	*param = new_val;
31655700b1afSMike Christie 	return 0;
31665700b1afSMike Christie }
3167adaf6990SAdheer Chandravanshi EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
3168a54a52caSMike Christie 
3169a54a52caSMike Christie int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3170a54a52caSMike Christie 		    enum iscsi_param param, char *buf, int buflen)
3171a54a52caSMike Christie {
3172a54a52caSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
3173a54a52caSMike Christie 	struct iscsi_session *session = conn->session;
31746a06a4b8SOr Gerlitz 	int val;
3175a54a52caSMike Christie 
3176a54a52caSMike Christie 	switch(param) {
3177843c0a8aSMike Christie 	case ISCSI_PARAM_FAST_ABORT:
3178843c0a8aSMike Christie 		sscanf(buf, "%d", &session->fast_abort);
3179843c0a8aSMike Christie 		break;
3180f6d5180cSMike Christie 	case ISCSI_PARAM_ABORT_TMO:
3181f6d5180cSMike Christie 		sscanf(buf, "%d", &session->abort_timeout);
3182f6d5180cSMike Christie 		break;
3183f6d5180cSMike Christie 	case ISCSI_PARAM_LU_RESET_TMO:
3184f6d5180cSMike Christie 		sscanf(buf, "%d", &session->lu_reset_timeout);
3185f6d5180cSMike Christie 		break;
31863fe5ae8bSMike Christie 	case ISCSI_PARAM_TGT_RESET_TMO:
31873fe5ae8bSMike Christie 		sscanf(buf, "%d", &session->tgt_reset_timeout);
31883fe5ae8bSMike Christie 		break;
3189f6d5180cSMike Christie 	case ISCSI_PARAM_PING_TMO:
3190f6d5180cSMike Christie 		sscanf(buf, "%d", &conn->ping_timeout);
3191f6d5180cSMike Christie 		break;
3192f6d5180cSMike Christie 	case ISCSI_PARAM_RECV_TMO:
3193f6d5180cSMike Christie 		sscanf(buf, "%d", &conn->recv_timeout);
3194f6d5180cSMike Christie 		break;
3195a54a52caSMike Christie 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
3196a54a52caSMike Christie 		sscanf(buf, "%d", &conn->max_recv_dlength);
3197a54a52caSMike Christie 		break;
3198a54a52caSMike Christie 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3199a54a52caSMike Christie 		sscanf(buf, "%d", &conn->max_xmit_dlength);
3200a54a52caSMike Christie 		break;
3201a54a52caSMike Christie 	case ISCSI_PARAM_HDRDGST_EN:
3202a54a52caSMike Christie 		sscanf(buf, "%d", &conn->hdrdgst_en);
3203a54a52caSMike Christie 		break;
3204a54a52caSMike Christie 	case ISCSI_PARAM_DATADGST_EN:
3205a54a52caSMike Christie 		sscanf(buf, "%d", &conn->datadgst_en);
3206a54a52caSMike Christie 		break;
3207a54a52caSMike Christie 	case ISCSI_PARAM_INITIAL_R2T_EN:
3208a54a52caSMike Christie 		sscanf(buf, "%d", &session->initial_r2t_en);
3209a54a52caSMike Christie 		break;
3210a54a52caSMike Christie 	case ISCSI_PARAM_MAX_R2T:
32111304be5fSMike Christie 		sscanf(buf, "%hu", &session->max_r2t);
3212a54a52caSMike Christie 		break;
3213a54a52caSMike Christie 	case ISCSI_PARAM_IMM_DATA_EN:
3214a54a52caSMike Christie 		sscanf(buf, "%d", &session->imm_data_en);
3215a54a52caSMike Christie 		break;
3216a54a52caSMike Christie 	case ISCSI_PARAM_FIRST_BURST:
3217a54a52caSMike Christie 		sscanf(buf, "%d", &session->first_burst);
3218a54a52caSMike Christie 		break;
3219a54a52caSMike Christie 	case ISCSI_PARAM_MAX_BURST:
3220a54a52caSMike Christie 		sscanf(buf, "%d", &session->max_burst);
3221a54a52caSMike Christie 		break;
3222a54a52caSMike Christie 	case ISCSI_PARAM_PDU_INORDER_EN:
3223a54a52caSMike Christie 		sscanf(buf, "%d", &session->pdu_inorder_en);
3224a54a52caSMike Christie 		break;
3225a54a52caSMike Christie 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
3226a54a52caSMike Christie 		sscanf(buf, "%d", &session->dataseq_inorder_en);
3227a54a52caSMike Christie 		break;
3228a54a52caSMike Christie 	case ISCSI_PARAM_ERL:
3229a54a52caSMike Christie 		sscanf(buf, "%d", &session->erl);
3230a54a52caSMike Christie 		break;
3231a54a52caSMike Christie 	case ISCSI_PARAM_EXP_STATSN:
3232a54a52caSMike Christie 		sscanf(buf, "%u", &conn->exp_statsn);
3233a54a52caSMike Christie 		break;
3234b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME:
32355700b1afSMike Christie 		return iscsi_switch_str_param(&session->username, buf);
3236b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME_IN:
32375700b1afSMike Christie 		return iscsi_switch_str_param(&session->username_in, buf);
3238b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD:
32395700b1afSMike Christie 		return iscsi_switch_str_param(&session->password, buf);
3240b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD_IN:
32415700b1afSMike Christie 		return iscsi_switch_str_param(&session->password_in, buf);
3242a54a52caSMike Christie 	case ISCSI_PARAM_TARGET_NAME:
32435700b1afSMike Christie 		return iscsi_switch_str_param(&session->targetname, buf);
32443c5c4801SVikas Chaudhary 	case ISCSI_PARAM_TARGET_ALIAS:
32453c5c4801SVikas Chaudhary 		return iscsi_switch_str_param(&session->targetalias, buf);
3246a54a52caSMike Christie 	case ISCSI_PARAM_TPGT:
3247a54a52caSMike Christie 		sscanf(buf, "%d", &session->tpgt);
3248a54a52caSMike Christie 		break;
3249a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_PORT:
3250a54a52caSMike Christie 		sscanf(buf, "%d", &conn->persistent_port);
3251a54a52caSMike Christie 		break;
3252a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
32535700b1afSMike Christie 		return iscsi_switch_str_param(&conn->persistent_address, buf);
325488dfd340SMike Christie 	case ISCSI_PARAM_IFACE_NAME:
32555700b1afSMike Christie 		return iscsi_switch_str_param(&session->ifacename, buf);
325688dfd340SMike Christie 	case ISCSI_PARAM_INITIATOR_NAME:
32575700b1afSMike Christie 		return iscsi_switch_str_param(&session->initiatorname, buf);
32583b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_ROOT:
32593b9373e9SEddie Wai 		return iscsi_switch_str_param(&session->boot_root, buf);
32603b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_NIC:
32613b9373e9SEddie Wai 		return iscsi_switch_str_param(&session->boot_nic, buf);
32623b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_TARGET:
32633b9373e9SEddie Wai 		return iscsi_switch_str_param(&session->boot_target, buf);
3264f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_PORTAL_TYPE:
3265f8525eb4SAdheer Chandravanshi 		return iscsi_switch_str_param(&session->portal_type, buf);
3266f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3267f8525eb4SAdheer Chandravanshi 		return iscsi_switch_str_param(&session->discovery_parent_type,
3268f8525eb4SAdheer Chandravanshi 					      buf);
32696a06a4b8SOr Gerlitz 	case ISCSI_PARAM_DISCOVERY_SESS:
32706a06a4b8SOr Gerlitz 		sscanf(buf, "%d", &val);
32716a06a4b8SOr Gerlitz 		session->discovery_sess = !!val;
32726a06a4b8SOr Gerlitz 		break;
3273ae56ff40SAdheer Chandravanshi 	case ISCSI_PARAM_LOCAL_IPADDR:
3274ae56ff40SAdheer Chandravanshi 		return iscsi_switch_str_param(&conn->local_ipaddr, buf);
3275a54a52caSMike Christie 	default:
3276a54a52caSMike Christie 		return -ENOSYS;
3277a54a52caSMike Christie 	}
3278a54a52caSMike Christie 
3279a54a52caSMike Christie 	return 0;
3280a54a52caSMike Christie }
3281a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_set_param);
3282a54a52caSMike Christie 
3283a54a52caSMike Christie int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3284a54a52caSMike Christie 			    enum iscsi_param param, char *buf)
3285a54a52caSMike Christie {
328675613521SMike Christie 	struct iscsi_session *session = cls_session->dd_data;
3287a54a52caSMike Christie 	int len;
3288a54a52caSMike Christie 
3289a54a52caSMike Christie 	switch(param) {
3290843c0a8aSMike Christie 	case ISCSI_PARAM_FAST_ABORT:
3291843c0a8aSMike Christie 		len = sprintf(buf, "%d\n", session->fast_abort);
3292843c0a8aSMike Christie 		break;
3293f6d5180cSMike Christie 	case ISCSI_PARAM_ABORT_TMO:
3294f6d5180cSMike Christie 		len = sprintf(buf, "%d\n", session->abort_timeout);
3295f6d5180cSMike Christie 		break;
3296f6d5180cSMike Christie 	case ISCSI_PARAM_LU_RESET_TMO:
3297f6d5180cSMike Christie 		len = sprintf(buf, "%d\n", session->lu_reset_timeout);
3298f6d5180cSMike Christie 		break;
32993fe5ae8bSMike Christie 	case ISCSI_PARAM_TGT_RESET_TMO:
33003fe5ae8bSMike Christie 		len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
33013fe5ae8bSMike Christie 		break;
3302a54a52caSMike Christie 	case ISCSI_PARAM_INITIAL_R2T_EN:
3303a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->initial_r2t_en);
3304a54a52caSMike Christie 		break;
3305a54a52caSMike Christie 	case ISCSI_PARAM_MAX_R2T:
3306a54a52caSMike Christie 		len = sprintf(buf, "%hu\n", session->max_r2t);
3307a54a52caSMike Christie 		break;
3308a54a52caSMike Christie 	case ISCSI_PARAM_IMM_DATA_EN:
3309a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->imm_data_en);
3310a54a52caSMike Christie 		break;
3311a54a52caSMike Christie 	case ISCSI_PARAM_FIRST_BURST:
3312a54a52caSMike Christie 		len = sprintf(buf, "%u\n", session->first_burst);
3313a54a52caSMike Christie 		break;
3314a54a52caSMike Christie 	case ISCSI_PARAM_MAX_BURST:
3315a54a52caSMike Christie 		len = sprintf(buf, "%u\n", session->max_burst);
3316a54a52caSMike Christie 		break;
3317a54a52caSMike Christie 	case ISCSI_PARAM_PDU_INORDER_EN:
3318a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->pdu_inorder_en);
3319a54a52caSMike Christie 		break;
3320a54a52caSMike Christie 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
3321a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
3322a54a52caSMike Christie 		break;
33235f09e1f3SAdheer Chandravanshi 	case ISCSI_PARAM_DEF_TASKMGMT_TMO:
33245f09e1f3SAdheer Chandravanshi 		len = sprintf(buf, "%d\n", session->def_taskmgmt_tmo);
33255f09e1f3SAdheer Chandravanshi 		break;
3326a54a52caSMike Christie 	case ISCSI_PARAM_ERL:
3327a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->erl);
3328a54a52caSMike Christie 		break;
3329a54a52caSMike Christie 	case ISCSI_PARAM_TARGET_NAME:
3330a54a52caSMike Christie 		len = sprintf(buf, "%s\n", session->targetname);
3331a54a52caSMike Christie 		break;
33323c5c4801SVikas Chaudhary 	case ISCSI_PARAM_TARGET_ALIAS:
33333c5c4801SVikas Chaudhary 		len = sprintf(buf, "%s\n", session->targetalias);
33343c5c4801SVikas Chaudhary 		break;
3335a54a52caSMike Christie 	case ISCSI_PARAM_TPGT:
3336a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->tpgt);
3337a54a52caSMike Christie 		break;
3338b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME:
3339b2c64167SMike Christie 		len = sprintf(buf, "%s\n", session->username);
3340b2c64167SMike Christie 		break;
3341b2c64167SMike Christie 	case ISCSI_PARAM_USERNAME_IN:
3342b2c64167SMike Christie 		len = sprintf(buf, "%s\n", session->username_in);
3343b2c64167SMike Christie 		break;
3344b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD:
3345b2c64167SMike Christie 		len = sprintf(buf, "%s\n", session->password);
3346b2c64167SMike Christie 		break;
3347b2c64167SMike Christie 	case ISCSI_PARAM_PASSWORD_IN:
3348b2c64167SMike Christie 		len = sprintf(buf, "%s\n", session->password_in);
3349b2c64167SMike Christie 		break;
335088dfd340SMike Christie 	case ISCSI_PARAM_IFACE_NAME:
335188dfd340SMike Christie 		len = sprintf(buf, "%s\n", session->ifacename);
335288dfd340SMike Christie 		break;
335388dfd340SMike Christie 	case ISCSI_PARAM_INITIATOR_NAME:
335488dfd340SMike Christie 		len = sprintf(buf, "%s\n", session->initiatorname);
335588dfd340SMike Christie 		break;
33563b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_ROOT:
33573b9373e9SEddie Wai 		len = sprintf(buf, "%s\n", session->boot_root);
33583b9373e9SEddie Wai 		break;
33593b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_NIC:
33603b9373e9SEddie Wai 		len = sprintf(buf, "%s\n", session->boot_nic);
33613b9373e9SEddie Wai 		break;
33623b9373e9SEddie Wai 	case ISCSI_PARAM_BOOT_TARGET:
33633b9373e9SEddie Wai 		len = sprintf(buf, "%s\n", session->boot_target);
33649a2307b1SAdheer Chandravanshi 		break;
3365f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
3366f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->auto_snd_tgt_disable);
3367f8525eb4SAdheer Chandravanshi 		break;
3368f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_SESS:
3369f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->discovery_sess);
3370f8525eb4SAdheer Chandravanshi 		break;
3371f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_PORTAL_TYPE:
3372f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%s\n", session->portal_type);
3373f8525eb4SAdheer Chandravanshi 		break;
3374f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_CHAP_AUTH_EN:
3375f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->chap_auth_en);
3376f8525eb4SAdheer Chandravanshi 		break;
3377f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
3378f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->discovery_logout_en);
3379f8525eb4SAdheer Chandravanshi 		break;
3380f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_BIDI_CHAP_EN:
3381f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->bidi_chap_en);
3382f8525eb4SAdheer Chandravanshi 		break;
3383f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
3384f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->discovery_auth_optional);
3385f8525eb4SAdheer Chandravanshi 		break;
3386f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DEF_TIME2WAIT:
3387f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%d\n", session->time2wait);
3388f8525eb4SAdheer Chandravanshi 		break;
3389f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DEF_TIME2RETAIN:
3390f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%d\n", session->time2retain);
3391f8525eb4SAdheer Chandravanshi 		break;
3392f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TSID:
3393f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->tsid);
3394f8525eb4SAdheer Chandravanshi 		break;
3395f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_ISID:
3396f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%02x%02x%02x%02x%02x%02x\n",
3397f8525eb4SAdheer Chandravanshi 			      session->isid[0], session->isid[1],
3398f8525eb4SAdheer Chandravanshi 			      session->isid[2], session->isid[3],
3399f8525eb4SAdheer Chandravanshi 			      session->isid[4], session->isid[5]);
3400f8525eb4SAdheer Chandravanshi 		break;
3401f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
3402f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", session->discovery_parent_idx);
3403f8525eb4SAdheer Chandravanshi 		break;
3404f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3405f8525eb4SAdheer Chandravanshi 		if (session->discovery_parent_type)
3406f8525eb4SAdheer Chandravanshi 			len = sprintf(buf, "%s\n",
3407f8525eb4SAdheer Chandravanshi 				      session->discovery_parent_type);
3408f8525eb4SAdheer Chandravanshi 		else
3409f8525eb4SAdheer Chandravanshi 			len = sprintf(buf, "\n");
34103b9373e9SEddie Wai 		break;
3411a54a52caSMike Christie 	default:
3412a54a52caSMike Christie 		return -ENOSYS;
3413a54a52caSMike Christie 	}
3414a54a52caSMike Christie 
3415a54a52caSMike Christie 	return len;
3416a54a52caSMike Christie }
3417a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3418a54a52caSMike Christie 
341900f3708eSMike Christie int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
342000f3708eSMike Christie 			      enum iscsi_param param, char *buf)
342100f3708eSMike Christie {
342200f3708eSMike Christie 	struct sockaddr_in6 *sin6 = NULL;
342300f3708eSMike Christie 	struct sockaddr_in *sin = NULL;
342400f3708eSMike Christie 	int len;
342500f3708eSMike Christie 
342600f3708eSMike Christie 	switch (addr->ss_family) {
342700f3708eSMike Christie 	case AF_INET:
342800f3708eSMike Christie 		sin = (struct sockaddr_in *)addr;
342900f3708eSMike Christie 		break;
343000f3708eSMike Christie 	case AF_INET6:
343100f3708eSMike Christie 		sin6 = (struct sockaddr_in6 *)addr;
343200f3708eSMike Christie 		break;
343300f3708eSMike Christie 	default:
343400f3708eSMike Christie 		return -EINVAL;
343500f3708eSMike Christie 	}
343600f3708eSMike Christie 
343700f3708eSMike Christie 	switch (param) {
343800f3708eSMike Christie 	case ISCSI_PARAM_CONN_ADDRESS:
343900f3708eSMike Christie 	case ISCSI_HOST_PARAM_IPADDRESS:
344000f3708eSMike Christie 		if (sin)
344100f3708eSMike Christie 			len = sprintf(buf, "%pI4\n", &sin->sin_addr.s_addr);
344200f3708eSMike Christie 		else
344300f3708eSMike Christie 			len = sprintf(buf, "%pI6\n", &sin6->sin6_addr);
344400f3708eSMike Christie 		break;
344500f3708eSMike Christie 	case ISCSI_PARAM_CONN_PORT:
344600f3708eSMike Christie 		if (sin)
344700f3708eSMike Christie 			len = sprintf(buf, "%hu\n", be16_to_cpu(sin->sin_port));
344800f3708eSMike Christie 		else
344900f3708eSMike Christie 			len = sprintf(buf, "%hu\n",
345000f3708eSMike Christie 				      be16_to_cpu(sin6->sin6_port));
345100f3708eSMike Christie 		break;
345200f3708eSMike Christie 	default:
345300f3708eSMike Christie 		return -EINVAL;
345400f3708eSMike Christie 	}
345500f3708eSMike Christie 
345600f3708eSMike Christie 	return len;
345700f3708eSMike Christie }
345800f3708eSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
345900f3708eSMike Christie 
3460a54a52caSMike Christie int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3461a54a52caSMike Christie 			 enum iscsi_param param, char *buf)
3462a54a52caSMike Christie {
3463a54a52caSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
3464a54a52caSMike Christie 	int len;
3465a54a52caSMike Christie 
3466a54a52caSMike Christie 	switch(param) {
3467f6d5180cSMike Christie 	case ISCSI_PARAM_PING_TMO:
3468f6d5180cSMike Christie 		len = sprintf(buf, "%u\n", conn->ping_timeout);
3469f6d5180cSMike Christie 		break;
3470f6d5180cSMike Christie 	case ISCSI_PARAM_RECV_TMO:
3471f6d5180cSMike Christie 		len = sprintf(buf, "%u\n", conn->recv_timeout);
3472f6d5180cSMike Christie 		break;
3473a54a52caSMike Christie 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
3474a54a52caSMike Christie 		len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3475a54a52caSMike Christie 		break;
3476a54a52caSMike Christie 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3477a54a52caSMike Christie 		len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3478a54a52caSMike Christie 		break;
3479a54a52caSMike Christie 	case ISCSI_PARAM_HDRDGST_EN:
3480a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3481a54a52caSMike Christie 		break;
3482a54a52caSMike Christie 	case ISCSI_PARAM_DATADGST_EN:
3483a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->datadgst_en);
3484a54a52caSMike Christie 		break;
3485a54a52caSMike Christie 	case ISCSI_PARAM_IFMARKER_EN:
3486a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->ifmarker_en);
3487a54a52caSMike Christie 		break;
3488a54a52caSMike Christie 	case ISCSI_PARAM_OFMARKER_EN:
3489a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->ofmarker_en);
3490a54a52caSMike Christie 		break;
3491a54a52caSMike Christie 	case ISCSI_PARAM_EXP_STATSN:
3492a54a52caSMike Christie 		len = sprintf(buf, "%u\n", conn->exp_statsn);
3493a54a52caSMike Christie 		break;
3494a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_PORT:
3495a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->persistent_port);
3496a54a52caSMike Christie 		break;
3497a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
3498a54a52caSMike Christie 		len = sprintf(buf, "%s\n", conn->persistent_address);
3499a54a52caSMike Christie 		break;
3500f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_STATSN:
3501f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->statsn);
3502f8525eb4SAdheer Chandravanshi 		break;
3503f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_MAX_SEGMENT_SIZE:
3504f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->max_segment_size);
3505f8525eb4SAdheer Chandravanshi 		break;
3506f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_KEEPALIVE_TMO:
3507f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->keepalive_tmo);
3508f8525eb4SAdheer Chandravanshi 		break;
3509f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_LOCAL_PORT:
3510f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->local_port);
3511f8525eb4SAdheer Chandravanshi 		break;
3512f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
3513f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->tcp_timestamp_stat);
3514f8525eb4SAdheer Chandravanshi 		break;
3515f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_NAGLE_DISABLE:
3516f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->tcp_nagle_disable);
3517f8525eb4SAdheer Chandravanshi 		break;
3518f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_WSF_DISABLE:
3519f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->tcp_wsf_disable);
3520f8525eb4SAdheer Chandravanshi 		break;
3521f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_TIMER_SCALE:
3522f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->tcp_timer_scale);
3523f8525eb4SAdheer Chandravanshi 		break;
3524f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_TIMESTAMP_EN:
3525f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->tcp_timestamp_en);
3526f8525eb4SAdheer Chandravanshi 		break;
3527f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
3528f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->fragment_disable);
3529f8525eb4SAdheer Chandravanshi 		break;
3530f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IPV4_TOS:
3531f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->ipv4_tos);
3532f8525eb4SAdheer Chandravanshi 		break;
3533f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IPV6_TC:
3534f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->ipv6_traffic_class);
3535f8525eb4SAdheer Chandravanshi 		break;
35365f09e1f3SAdheer Chandravanshi 	case ISCSI_PARAM_IPV6_FLOW_LABEL:
35375f09e1f3SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->ipv6_flow_label);
35385f09e1f3SAdheer Chandravanshi 		break;
3539f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
3540f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->is_fw_assigned_ipv6);
3541f8525eb4SAdheer Chandravanshi 		break;
3542f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_XMIT_WSF:
3543f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->tcp_xmit_wsf);
3544f8525eb4SAdheer Chandravanshi 		break;
3545f8525eb4SAdheer Chandravanshi 	case ISCSI_PARAM_TCP_RECV_WSF:
3546f8525eb4SAdheer Chandravanshi 		len = sprintf(buf, "%u\n", conn->tcp_recv_wsf);
3547f8525eb4SAdheer Chandravanshi 		break;
3548ae56ff40SAdheer Chandravanshi 	case ISCSI_PARAM_LOCAL_IPADDR:
3549ae56ff40SAdheer Chandravanshi 		len = sprintf(buf, "%s\n", conn->local_ipaddr);
3550ae56ff40SAdheer Chandravanshi 		break;
3551a54a52caSMike Christie 	default:
3552a54a52caSMike Christie 		return -ENOSYS;
3553a54a52caSMike Christie 	}
3554a54a52caSMike Christie 
3555a54a52caSMike Christie 	return len;
3556a54a52caSMike Christie }
3557a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3558a54a52caSMike Christie 
35590801c242SMike Christie int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
35600801c242SMike Christie 			 char *buf)
35610801c242SMike Christie {
356275613521SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
35630801c242SMike Christie 	int len;
35640801c242SMike Christie 
35650801c242SMike Christie 	switch (param) {
3566d8196ed2SMike Christie 	case ISCSI_HOST_PARAM_NETDEV_NAME:
356775613521SMike Christie 		len = sprintf(buf, "%s\n", ihost->netdev);
3568d8196ed2SMike Christie 		break;
35690801c242SMike Christie 	case ISCSI_HOST_PARAM_HWADDRESS:
357075613521SMike Christie 		len = sprintf(buf, "%s\n", ihost->hwaddress);
35710801c242SMike Christie 		break;
35728ad5781aSMike Christie 	case ISCSI_HOST_PARAM_INITIATOR_NAME:
357375613521SMike Christie 		len = sprintf(buf, "%s\n", ihost->initiatorname);
35748ad5781aSMike Christie 		break;
35750801c242SMike Christie 	default:
35760801c242SMike Christie 		return -ENOSYS;
35770801c242SMike Christie 	}
35780801c242SMike Christie 
35790801c242SMike Christie 	return len;
35800801c242SMike Christie }
35810801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_get_param);
35820801c242SMike Christie 
35830801c242SMike Christie int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
35840801c242SMike Christie 			 char *buf, int buflen)
35850801c242SMike Christie {
358675613521SMike Christie 	struct iscsi_host *ihost = shost_priv(shost);
35870801c242SMike Christie 
35880801c242SMike Christie 	switch (param) {
3589d8196ed2SMike Christie 	case ISCSI_HOST_PARAM_NETDEV_NAME:
35905700b1afSMike Christie 		return iscsi_switch_str_param(&ihost->netdev, buf);
35910801c242SMike Christie 	case ISCSI_HOST_PARAM_HWADDRESS:
35925700b1afSMike Christie 		return iscsi_switch_str_param(&ihost->hwaddress, buf);
35938ad5781aSMike Christie 	case ISCSI_HOST_PARAM_INITIATOR_NAME:
35945700b1afSMike Christie 		return iscsi_switch_str_param(&ihost->initiatorname, buf);
35950801c242SMike Christie 	default:
35960801c242SMike Christie 		return -ENOSYS;
35970801c242SMike Christie 	}
35980801c242SMike Christie 
35990801c242SMike Christie 	return 0;
36000801c242SMike Christie }
36010801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_set_param);
36020801c242SMike Christie 
36037996a778SMike Christie MODULE_AUTHOR("Mike Christie");
36047996a778SMike Christie MODULE_DESCRIPTION("iSCSI library functions");
36057996a778SMike Christie MODULE_LICENSE("GPL");
3606