xref: /openbmc/linux/drivers/scsi/libiscsi.c (revision e648f63c)
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/mutex.h>
267996a778SMike Christie #include <linux/kfifo.h>
277996a778SMike Christie #include <linux/delay.h>
287996a778SMike Christie #include <net/tcp.h>
297996a778SMike Christie #include <scsi/scsi_cmnd.h>
307996a778SMike Christie #include <scsi/scsi_device.h>
317996a778SMike Christie #include <scsi/scsi_eh.h>
327996a778SMike Christie #include <scsi/scsi_tcq.h>
337996a778SMike Christie #include <scsi/scsi_host.h>
347996a778SMike Christie #include <scsi/scsi.h>
357996a778SMike Christie #include <scsi/iscsi_proto.h>
367996a778SMike Christie #include <scsi/scsi_transport.h>
377996a778SMike Christie #include <scsi/scsi_transport_iscsi.h>
387996a778SMike Christie #include <scsi/libiscsi.h>
397996a778SMike Christie 
407996a778SMike Christie struct iscsi_session *
417996a778SMike Christie class_to_transport_session(struct iscsi_cls_session *cls_session)
427996a778SMike Christie {
437996a778SMike Christie 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
447996a778SMike Christie 	return iscsi_hostdata(shost->hostdata);
457996a778SMike Christie }
467996a778SMike Christie EXPORT_SYMBOL_GPL(class_to_transport_session);
477996a778SMike Christie 
487996a778SMike Christie #define INVALID_SN_DELTA	0xffff
497996a778SMike Christie 
507996a778SMike Christie int
517996a778SMike Christie iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
527996a778SMike Christie {
537996a778SMike Christie 	uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
547996a778SMike Christie 	uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
557996a778SMike Christie 
567996a778SMike Christie 	if (max_cmdsn < exp_cmdsn -1 &&
577996a778SMike Christie 	    max_cmdsn > exp_cmdsn - INVALID_SN_DELTA)
587996a778SMike Christie 		return ISCSI_ERR_MAX_CMDSN;
597996a778SMike Christie 	if (max_cmdsn > session->max_cmdsn ||
607996a778SMike Christie 	    max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA)
617996a778SMike Christie 		session->max_cmdsn = max_cmdsn;
627996a778SMike Christie 	if (exp_cmdsn > session->exp_cmdsn ||
637996a778SMike Christie 	    exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA)
647996a778SMike Christie 		session->exp_cmdsn = exp_cmdsn;
657996a778SMike Christie 
667996a778SMike Christie 	return 0;
677996a778SMike Christie }
687996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn);
697996a778SMike Christie 
707996a778SMike Christie void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
71ffd0436eSMike Christie 				   struct iscsi_data *hdr)
727996a778SMike Christie {
737996a778SMike Christie 	struct iscsi_conn *conn = ctask->conn;
747996a778SMike Christie 
757996a778SMike Christie 	memset(hdr, 0, sizeof(struct iscsi_data));
767996a778SMike Christie 	hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
777996a778SMike Christie 	hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
787996a778SMike Christie 	ctask->unsol_datasn++;
797996a778SMike Christie 	hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
807996a778SMike Christie 	memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
817996a778SMike Christie 
827996a778SMike Christie 	hdr->itt = ctask->hdr->itt;
837996a778SMike Christie 	hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
84ffd0436eSMike Christie 	hdr->offset = cpu_to_be32(ctask->unsol_offset);
857996a778SMike Christie 
867996a778SMike Christie 	if (ctask->unsol_count > conn->max_xmit_dlength) {
877996a778SMike Christie 		hton24(hdr->dlength, conn->max_xmit_dlength);
887996a778SMike Christie 		ctask->data_count = conn->max_xmit_dlength;
89ffd0436eSMike Christie 		ctask->unsol_offset += ctask->data_count;
907996a778SMike Christie 		hdr->flags = 0;
917996a778SMike Christie 	} else {
927996a778SMike Christie 		hton24(hdr->dlength, ctask->unsol_count);
937996a778SMike Christie 		ctask->data_count = ctask->unsol_count;
947996a778SMike Christie 		hdr->flags = ISCSI_FLAG_CMD_FINAL;
957996a778SMike Christie 	}
967996a778SMike Christie }
977996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
987996a778SMike Christie 
997996a778SMike Christie /**
1007996a778SMike Christie  * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
1017996a778SMike Christie  * @ctask: iscsi cmd task
1027996a778SMike Christie  *
1037996a778SMike Christie  * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
1047996a778SMike Christie  * fields like dlength or final based on how much data it sends
1057996a778SMike Christie  */
1067996a778SMike Christie static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
1077996a778SMike Christie {
1087996a778SMike Christie 	struct iscsi_conn *conn = ctask->conn;
1097996a778SMike Christie 	struct iscsi_session *session = conn->session;
1107996a778SMike Christie 	struct iscsi_cmd *hdr = ctask->hdr;
1117996a778SMike Christie 	struct scsi_cmnd *sc = ctask->sc;
1127996a778SMike Christie 
1137996a778SMike Christie         hdr->opcode = ISCSI_OP_SCSI_CMD;
1147996a778SMike Christie         hdr->flags = ISCSI_ATTR_SIMPLE;
1157996a778SMike Christie         int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
1167996a778SMike Christie         hdr->itt = ctask->itt | (conn->id << ISCSI_CID_SHIFT) |
1177996a778SMike Christie                          (session->age << ISCSI_AGE_SHIFT);
1187996a778SMike Christie         hdr->data_length = cpu_to_be32(sc->request_bufflen);
1197996a778SMike Christie         hdr->cmdsn = cpu_to_be32(session->cmdsn);
1207996a778SMike Christie         session->cmdsn++;
1217996a778SMike Christie         hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
1227996a778SMike Christie         memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
1237996a778SMike Christie         memset(&hdr->cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
1247996a778SMike Christie 
125ffd0436eSMike Christie 	ctask->data_count = 0;
1267996a778SMike Christie 	if (sc->sc_data_direction == DMA_TO_DEVICE) {
1277996a778SMike Christie 		hdr->flags |= ISCSI_FLAG_CMD_WRITE;
1287996a778SMike Christie 		/*
1297996a778SMike Christie 		 * Write counters:
1307996a778SMike Christie 		 *
1317996a778SMike Christie 		 *	imm_count	bytes to be sent right after
1327996a778SMike Christie 		 *			SCSI PDU Header
1337996a778SMike Christie 		 *
1347996a778SMike Christie 		 *	unsol_count	bytes(as Data-Out) to be sent
1357996a778SMike Christie 		 *			without	R2T ack right after
1367996a778SMike Christie 		 *			immediate data
1377996a778SMike Christie 		 *
1387996a778SMike Christie 		 *	r2t_data_count	bytes to be sent via R2T ack's
1397996a778SMike Christie 		 *
1407996a778SMike Christie 		 *      pad_count       bytes to be sent as zero-padding
1417996a778SMike Christie 		 */
1427996a778SMike Christie 		ctask->imm_count = 0;
1437996a778SMike Christie 		ctask->unsol_count = 0;
144ffd0436eSMike Christie 		ctask->unsol_offset = 0;
1457996a778SMike Christie 		ctask->unsol_datasn = 0;
1467996a778SMike Christie 
1477996a778SMike Christie 		if (session->imm_data_en) {
1487996a778SMike Christie 			if (ctask->total_length >= session->first_burst)
1497996a778SMike Christie 				ctask->imm_count = min(session->first_burst,
1507996a778SMike Christie 							conn->max_xmit_dlength);
1517996a778SMike Christie 			else
1527996a778SMike Christie 				ctask->imm_count = min(ctask->total_length,
1537996a778SMike Christie 							conn->max_xmit_dlength);
1547996a778SMike Christie 			hton24(ctask->hdr->dlength, ctask->imm_count);
1557996a778SMike Christie 		} else
1567996a778SMike Christie 			zero_data(ctask->hdr->dlength);
1577996a778SMike Christie 
158ffd0436eSMike Christie 		if (!session->initial_r2t_en) {
1597996a778SMike Christie 			ctask->unsol_count = min(session->first_burst,
1607996a778SMike Christie 				ctask->total_length) - ctask->imm_count;
161ffd0436eSMike Christie 			ctask->unsol_offset = ctask->imm_count;
162ffd0436eSMike Christie 		}
163ffd0436eSMike Christie 
1647996a778SMike Christie 		if (!ctask->unsol_count)
1657996a778SMike Christie 			/* No unsolicit Data-Out's */
1667996a778SMike Christie 			ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1677996a778SMike Christie 	} else {
1687996a778SMike Christie 		ctask->datasn = 0;
1697996a778SMike Christie 		hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1707996a778SMike Christie 		zero_data(hdr->dlength);
1717996a778SMike Christie 
1727996a778SMike Christie 		if (sc->sc_data_direction == DMA_FROM_DEVICE)
1737996a778SMike Christie 			hdr->flags |= ISCSI_FLAG_CMD_READ;
1747996a778SMike Christie 	}
1757996a778SMike Christie 
1767996a778SMike Christie 	conn->scsicmd_pdus_cnt++;
1777996a778SMike Christie }
1787996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu);
1797996a778SMike Christie 
1807996a778SMike Christie /**
1817996a778SMike Christie  * iscsi_complete_command - return command back to scsi-ml
1827996a778SMike Christie  * @ctask: iscsi cmd task
1837996a778SMike Christie  *
1847996a778SMike Christie  * Must be called with session lock.
1857996a778SMike Christie  * This function returns the scsi command to scsi-ml and returns
1867996a778SMike Christie  * the cmd task to the pool of available cmd tasks.
1877996a778SMike Christie  */
18860ecebf5SMike Christie static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
1897996a778SMike Christie {
19060ecebf5SMike Christie 	struct iscsi_session *session = ctask->conn->session;
1917996a778SMike Christie 	struct scsi_cmnd *sc = ctask->sc;
1927996a778SMike Christie 
193b6c395edSMike Christie 	ctask->state = ISCSI_TASK_COMPLETED;
1947996a778SMike Christie 	ctask->sc = NULL;
195f47f2cf5SMike Christie 	/* SCSI eh reuses commands to verify us */
196f47f2cf5SMike Christie 	sc->SCp.ptr = NULL;
1977996a778SMike Christie 	list_del_init(&ctask->running);
1987996a778SMike Christie 	__kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
1997996a778SMike Christie 	sc->scsi_done(sc);
2007996a778SMike Christie }
2017996a778SMike Christie 
20260ecebf5SMike Christie static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
20360ecebf5SMike Christie {
20460ecebf5SMike Christie 	atomic_inc(&ctask->refcount);
20560ecebf5SMike Christie }
20660ecebf5SMike Christie 
20760ecebf5SMike Christie static void iscsi_get_ctask(struct iscsi_cmd_task *ctask)
20860ecebf5SMike Christie {
20960ecebf5SMike Christie 	spin_lock_bh(&ctask->conn->session->lock);
21060ecebf5SMike Christie 	__iscsi_get_ctask(ctask);
21160ecebf5SMike Christie 	spin_unlock_bh(&ctask->conn->session->lock);
21260ecebf5SMike Christie }
21360ecebf5SMike Christie 
21460ecebf5SMike Christie static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
21560ecebf5SMike Christie {
216e648f63cSMike Christie 	if (atomic_dec_and_test(&ctask->refcount))
21760ecebf5SMike Christie 		iscsi_complete_command(ctask);
21860ecebf5SMike Christie }
21960ecebf5SMike Christie 
22060ecebf5SMike Christie static void iscsi_put_ctask(struct iscsi_cmd_task *ctask)
22160ecebf5SMike Christie {
22260ecebf5SMike Christie 	spin_lock_bh(&ctask->conn->session->lock);
22360ecebf5SMike Christie 	__iscsi_put_ctask(ctask);
22460ecebf5SMike Christie 	spin_unlock_bh(&ctask->conn->session->lock);
22560ecebf5SMike Christie }
22660ecebf5SMike Christie 
2277996a778SMike Christie /**
2287996a778SMike Christie  * iscsi_cmd_rsp - SCSI Command Response processing
2297996a778SMike Christie  * @conn: iscsi connection
2307996a778SMike Christie  * @hdr: iscsi header
2317996a778SMike Christie  * @ctask: scsi command task
2327996a778SMike Christie  * @data: cmd data buffer
2337996a778SMike Christie  * @datalen: len of buffer
2347996a778SMike Christie  *
2357996a778SMike Christie  * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
2367996a778SMike Christie  * then completes the command and task.
2377996a778SMike Christie  **/
2387996a778SMike Christie static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
2397996a778SMike Christie 			      struct iscsi_cmd_task *ctask, char *data,
2407996a778SMike Christie 			      int datalen)
2417996a778SMike Christie {
2427996a778SMike Christie 	int rc;
2437996a778SMike Christie 	struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
2447996a778SMike Christie 	struct iscsi_session *session = conn->session;
2457996a778SMike Christie 	struct scsi_cmnd *sc = ctask->sc;
2467996a778SMike Christie 
2477996a778SMike Christie 	rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
2487996a778SMike Christie 	if (rc) {
2497996a778SMike Christie 		sc->result = DID_ERROR << 16;
2507996a778SMike Christie 		goto out;
2517996a778SMike Christie 	}
2527996a778SMike Christie 
2537996a778SMike Christie 	conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
2547996a778SMike Christie 
2557996a778SMike Christie 	sc->result = (DID_OK << 16) | rhdr->cmd_status;
2567996a778SMike Christie 
2577996a778SMike Christie 	if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
2587996a778SMike Christie 		sc->result = DID_ERROR << 16;
2597996a778SMike Christie 		goto out;
2607996a778SMike Christie 	}
2617996a778SMike Christie 
2627996a778SMike Christie 	if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
2637996a778SMike Christie 		int senselen;
2647996a778SMike Christie 
2657996a778SMike Christie 		if (datalen < 2) {
2667996a778SMike Christie invalid_datalen:
267be2df72eSOr Gerlitz 			printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
268be2df72eSOr Gerlitz 			       "invalid data buffer size of %d\n", datalen);
2697996a778SMike Christie 			sc->result = DID_BAD_TARGET << 16;
2707996a778SMike Christie 			goto out;
2717996a778SMike Christie 		}
2727996a778SMike Christie 
2737996a778SMike Christie 		senselen = (data[0] << 8) | data[1];
2747996a778SMike Christie 		if (datalen < senselen)
2757996a778SMike Christie 			goto invalid_datalen;
2767996a778SMike Christie 
2777996a778SMike Christie 		memcpy(sc->sense_buffer, data + 2,
2787996a778SMike Christie 		       min(senselen, SCSI_SENSE_BUFFERSIZE));
2797996a778SMike Christie 		debug_scsi("copied %d bytes of sense\n",
2807996a778SMike Christie 			   min(senselen, SCSI_SENSE_BUFFERSIZE));
2817996a778SMike Christie 	}
2827996a778SMike Christie 
2837996a778SMike Christie 	if (sc->sc_data_direction == DMA_TO_DEVICE)
2847996a778SMike Christie 		goto out;
2857996a778SMike Christie 
2867996a778SMike Christie 	if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
2877996a778SMike Christie 		int res_count = be32_to_cpu(rhdr->residual_count);
2887996a778SMike Christie 
2897996a778SMike Christie 		if (res_count > 0 && res_count <= sc->request_bufflen)
2907996a778SMike Christie 			sc->resid = res_count;
2917996a778SMike Christie 		else
2927996a778SMike Christie 			sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
2937996a778SMike Christie 	} else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
2947996a778SMike Christie 		sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
2957996a778SMike Christie 	else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
2967996a778SMike Christie 		sc->resid = be32_to_cpu(rhdr->residual_count);
2977996a778SMike Christie 
2987996a778SMike Christie out:
2997996a778SMike Christie 	debug_scsi("done [sc %lx res %d itt 0x%x]\n",
3007996a778SMike Christie 		   (long)sc, sc->result, ctask->itt);
3017996a778SMike Christie 	conn->scsirsp_pdus_cnt++;
3027996a778SMike Christie 
30360ecebf5SMike Christie 	__iscsi_put_ctask(ctask);
3047996a778SMike Christie 	return rc;
3057996a778SMike Christie }
3067996a778SMike Christie 
3077ea8b828SMike Christie static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
3087ea8b828SMike Christie {
3097ea8b828SMike Christie 	struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
3107ea8b828SMike Christie 
3117ea8b828SMike Christie 	conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
3127ea8b828SMike Christie 	conn->tmfrsp_pdus_cnt++;
3137ea8b828SMike Christie 
3147ea8b828SMike Christie 	if (conn->tmabort_state != TMABORT_INITIAL)
3157ea8b828SMike Christie 		return;
3167ea8b828SMike Christie 
3177ea8b828SMike Christie 	if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
3187ea8b828SMike Christie 		conn->tmabort_state = TMABORT_SUCCESS;
3197ea8b828SMike Christie 	else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
3207ea8b828SMike Christie 		conn->tmabort_state = TMABORT_NOT_FOUND;
3217ea8b828SMike Christie 	else
3227ea8b828SMike Christie 		conn->tmabort_state = TMABORT_FAILED;
3237ea8b828SMike Christie 	wake_up(&conn->ehwait);
3247ea8b828SMike Christie }
3257ea8b828SMike Christie 
32662f38300SMike Christie static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
32762f38300SMike Christie 			       char *data, int datalen)
32862f38300SMike Christie {
32962f38300SMike Christie 	struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
33062f38300SMike Christie 	struct iscsi_hdr rejected_pdu;
33162f38300SMike Christie 	uint32_t itt;
33262f38300SMike Christie 
33362f38300SMike Christie 	conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
33462f38300SMike Christie 
33562f38300SMike Christie 	if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
33662f38300SMike Christie 		if (ntoh24(reject->dlength) > datalen)
33762f38300SMike Christie 			return ISCSI_ERR_PROTO;
33862f38300SMike Christie 
33962f38300SMike Christie 		if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
34062f38300SMike Christie 			memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
34162f38300SMike Christie 			itt = rejected_pdu.itt & ISCSI_ITT_MASK;
34262f38300SMike Christie 			printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
34362f38300SMike Christie 				"due to DataDigest error.\n", itt,
34462f38300SMike Christie 				rejected_pdu.opcode);
34562f38300SMike Christie 		}
34662f38300SMike Christie 	}
34762f38300SMike Christie 	return 0;
34862f38300SMike Christie }
34962f38300SMike Christie 
3507996a778SMike Christie /**
3517996a778SMike Christie  * __iscsi_complete_pdu - complete pdu
3527996a778SMike Christie  * @conn: iscsi conn
3537996a778SMike Christie  * @hdr: iscsi header
3547996a778SMike Christie  * @data: data buffer
3557996a778SMike Christie  * @datalen: len of data buffer
3567996a778SMike Christie  *
3577996a778SMike Christie  * Completes pdu processing by freeing any resources allocated at
3587996a778SMike Christie  * queuecommand or send generic. session lock must be held and verify
3597996a778SMike Christie  * itt must have been called.
3607996a778SMike Christie  */
3617996a778SMike Christie int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
3627996a778SMike Christie 			 char *data, int datalen)
3637996a778SMike Christie {
3647996a778SMike Christie 	struct iscsi_session *session = conn->session;
3657996a778SMike Christie 	int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
3667996a778SMike Christie 	struct iscsi_cmd_task *ctask;
3677996a778SMike Christie 	struct iscsi_mgmt_task *mtask;
3687996a778SMike Christie 	uint32_t itt;
3697996a778SMike Christie 
3707996a778SMike Christie 	if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
3717996a778SMike Christie 		itt = hdr->itt & ISCSI_ITT_MASK;
3727996a778SMike Christie 	else
3737996a778SMike Christie 		itt = hdr->itt;
3747996a778SMike Christie 
3757996a778SMike Christie 	if (itt < session->cmds_max) {
3767996a778SMike Christie 		ctask = session->cmds[itt];
3777996a778SMike Christie 
3787996a778SMike Christie 		debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
3797996a778SMike Christie 			   opcode, conn->id, ctask->itt, datalen);
3807996a778SMike Christie 
3817996a778SMike Christie 		switch(opcode) {
3827996a778SMike Christie 		case ISCSI_OP_SCSI_CMD_RSP:
3837996a778SMike Christie 			BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
3847996a778SMike Christie 			rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
3857996a778SMike Christie 						datalen);
3867996a778SMike Christie 			break;
3877996a778SMike Christie 		case ISCSI_OP_SCSI_DATA_IN:
3887996a778SMike Christie 			BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
3897996a778SMike Christie 			if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
3907996a778SMike Christie 				conn->scsirsp_pdus_cnt++;
39160ecebf5SMike Christie 				__iscsi_put_ctask(ctask);
3927996a778SMike Christie 			}
3937996a778SMike Christie 			break;
3947996a778SMike Christie 		case ISCSI_OP_R2T:
3957996a778SMike Christie 			/* LLD handles this for now */
3967996a778SMike Christie 			break;
3977996a778SMike Christie 		default:
3987996a778SMike Christie 			rc = ISCSI_ERR_BAD_OPCODE;
3997996a778SMike Christie 			break;
4007996a778SMike Christie 		}
4017996a778SMike Christie 	} else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
4027996a778SMike Christie 		   itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
4037996a778SMike Christie 		mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
4047996a778SMike Christie 
4057996a778SMike Christie 		debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
4067996a778SMike Christie 			   opcode, conn->id, mtask->itt, datalen);
4077996a778SMike Christie 
4087996a778SMike Christie 		rc = iscsi_check_assign_cmdsn(session,
4097996a778SMike Christie 					      (struct iscsi_nopin*)hdr);
4107996a778SMike Christie 		if (rc)
4118d2860b3SMike Christie 			goto done;
4127996a778SMike Christie 
4138d2860b3SMike Christie 		switch(opcode) {
4148d2860b3SMike Christie 		case ISCSI_OP_LOGOUT_RSP:
415c8dc1e52SMike Christie 			if (datalen) {
416c8dc1e52SMike Christie 				rc = ISCSI_ERR_PROTO;
417c8dc1e52SMike Christie 				break;
418c8dc1e52SMike Christie 			}
4198d2860b3SMike Christie 			conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
4208d2860b3SMike Christie 			/* fall through */
4218d2860b3SMike Christie 		case ISCSI_OP_LOGIN_RSP:
4228d2860b3SMike Christie 		case ISCSI_OP_TEXT_RSP:
4238d2860b3SMike Christie 			/*
4248d2860b3SMike Christie 			 * login related PDU's exp_statsn is handled in
4258d2860b3SMike Christie 			 * userspace
4268d2860b3SMike Christie 			 */
42740527afeSMike Christie 			if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
42840527afeSMike Christie 				rc = ISCSI_ERR_CONN_FAILED;
4297996a778SMike Christie 			list_del(&mtask->running);
4307996a778SMike Christie 			if (conn->login_mtask != mtask)
4317996a778SMike Christie 				__kfifo_put(session->mgmtpool.queue,
4327996a778SMike Christie 					    (void*)&mtask, sizeof(void*));
4337996a778SMike Christie 			break;
4347996a778SMike Christie 		case ISCSI_OP_SCSI_TMFUNC_RSP:
4357996a778SMike Christie 			if (datalen) {
4367996a778SMike Christie 				rc = ISCSI_ERR_PROTO;
4377996a778SMike Christie 				break;
4387996a778SMike Christie 			}
4398d2860b3SMike Christie 
4407ea8b828SMike Christie 			iscsi_tmf_rsp(conn, hdr);
4417996a778SMike Christie 			break;
4427996a778SMike Christie 		case ISCSI_OP_NOOP_IN:
443c8dc1e52SMike Christie 			if (hdr->ttt != ISCSI_RESERVED_TAG || datalen) {
4447996a778SMike Christie 				rc = ISCSI_ERR_PROTO;
4457996a778SMike Christie 				break;
4467996a778SMike Christie 			}
4477996a778SMike Christie 			conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
4487996a778SMike Christie 
44940527afeSMike Christie 			if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
45040527afeSMike Christie 				rc = ISCSI_ERR_CONN_FAILED;
4517996a778SMike Christie 			list_del(&mtask->running);
4527996a778SMike Christie 			if (conn->login_mtask != mtask)
4537996a778SMike Christie 				__kfifo_put(session->mgmtpool.queue,
4547996a778SMike Christie 					    (void*)&mtask, sizeof(void*));
4557996a778SMike Christie 			break;
4567996a778SMike Christie 		default:
4577996a778SMike Christie 			rc = ISCSI_ERR_BAD_OPCODE;
4587996a778SMike Christie 			break;
4597996a778SMike Christie 		}
4607996a778SMike Christie 	} else if (itt == ISCSI_RESERVED_TAG) {
46162f38300SMike Christie 		rc = iscsi_check_assign_cmdsn(session,
46262f38300SMike Christie 					     (struct iscsi_nopin*)hdr);
46362f38300SMike Christie 		if (rc)
46462f38300SMike Christie 			goto done;
46562f38300SMike Christie 
4667996a778SMike Christie 		switch(opcode) {
4677996a778SMike Christie 		case ISCSI_OP_NOOP_IN:
46840527afeSMike Christie 			if (datalen) {
46940527afeSMike Christie 				rc = ISCSI_ERR_PROTO;
47040527afeSMike Christie 				break;
47140527afeSMike Christie 			}
47240527afeSMike Christie 
47340527afeSMike Christie 			if (hdr->ttt == ISCSI_RESERVED_TAG)
47440527afeSMike Christie 				break;
47540527afeSMike Christie 
47640527afeSMike Christie 			if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
47740527afeSMike Christie 				rc = ISCSI_ERR_CONN_FAILED;
4787996a778SMike Christie 			break;
4797996a778SMike Christie 		case ISCSI_OP_REJECT:
48062f38300SMike Christie 			rc = iscsi_handle_reject(conn, hdr, data, datalen);
48162f38300SMike Christie 			break;
4827996a778SMike Christie 		case ISCSI_OP_ASYNC_EVENT:
4838d2860b3SMike Christie 			conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
4847996a778SMike Christie 			/* we need sth like iscsi_async_event_rsp() */
4857996a778SMike Christie 			rc = ISCSI_ERR_BAD_OPCODE;
4867996a778SMike Christie 			break;
4877996a778SMike Christie 		default:
4887996a778SMike Christie 			rc = ISCSI_ERR_BAD_OPCODE;
4897996a778SMike Christie 			break;
4907996a778SMike Christie 		}
4917996a778SMike Christie 	} else
4927996a778SMike Christie 		rc = ISCSI_ERR_BAD_ITT;
4937996a778SMike Christie 
4948d2860b3SMike Christie done:
4957996a778SMike Christie 	return rc;
4967996a778SMike Christie }
4977996a778SMike Christie EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
4987996a778SMike Christie 
4997996a778SMike Christie int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
5007996a778SMike Christie 		       char *data, int datalen)
5017996a778SMike Christie {
5027996a778SMike Christie 	int rc;
5037996a778SMike Christie 
5047996a778SMike Christie 	spin_lock(&conn->session->lock);
5057996a778SMike Christie 	rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
5067996a778SMike Christie 	spin_unlock(&conn->session->lock);
5077996a778SMike Christie 	return rc;
5087996a778SMike Christie }
5097996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
5107996a778SMike Christie 
5117996a778SMike Christie /* verify itt (itt encoding: age+cid+itt) */
5127996a778SMike Christie int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
5137996a778SMike Christie 		     uint32_t *ret_itt)
5147996a778SMike Christie {
5157996a778SMike Christie 	struct iscsi_session *session = conn->session;
5167996a778SMike Christie 	struct iscsi_cmd_task *ctask;
5177996a778SMike Christie 	uint32_t itt;
5187996a778SMike Christie 
5197996a778SMike Christie 	if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
5207996a778SMike Christie 		if ((hdr->itt & ISCSI_AGE_MASK) !=
5217996a778SMike Christie 		    (session->age << ISCSI_AGE_SHIFT)) {
522be2df72eSOr Gerlitz 			printk(KERN_ERR "iscsi: received itt %x expected "
5237996a778SMike Christie 				"session age (%x)\n", hdr->itt,
5247996a778SMike Christie 				session->age & ISCSI_AGE_MASK);
5257996a778SMike Christie 			return ISCSI_ERR_BAD_ITT;
5267996a778SMike Christie 		}
5277996a778SMike Christie 
5287996a778SMike Christie 		if ((hdr->itt & ISCSI_CID_MASK) !=
5297996a778SMike Christie 		    (conn->id << ISCSI_CID_SHIFT)) {
530be2df72eSOr Gerlitz 			printk(KERN_ERR "iscsi: received itt %x, expected "
5317996a778SMike Christie 				"CID (%x)\n", hdr->itt, conn->id);
5327996a778SMike Christie 			return ISCSI_ERR_BAD_ITT;
5337996a778SMike Christie 		}
5347996a778SMike Christie 		itt = hdr->itt & ISCSI_ITT_MASK;
5357996a778SMike Christie 	} else
5367996a778SMike Christie 		itt = hdr->itt;
5377996a778SMike Christie 
5387996a778SMike Christie 	if (itt < session->cmds_max) {
5397996a778SMike Christie 		ctask = session->cmds[itt];
5407996a778SMike Christie 
5417996a778SMike Christie 		if (!ctask->sc) {
542be2df72eSOr Gerlitz 			printk(KERN_INFO "iscsi: dropping ctask with "
5437996a778SMike Christie 			       "itt 0x%x\n", ctask->itt);
5447996a778SMike Christie 			/* force drop */
5457996a778SMike Christie 			return ISCSI_ERR_NO_SCSI_CMD;
5467996a778SMike Christie 		}
5477996a778SMike Christie 
5487996a778SMike Christie 		if (ctask->sc->SCp.phase != session->age) {
549be2df72eSOr Gerlitz 			printk(KERN_ERR "iscsi: ctask's session age %d, "
5507996a778SMike Christie 				"expected %d\n", ctask->sc->SCp.phase,
5517996a778SMike Christie 				session->age);
5527996a778SMike Christie 			return ISCSI_ERR_SESSION_FAILED;
5537996a778SMike Christie 		}
5547996a778SMike Christie 	}
5557996a778SMike Christie 
5567996a778SMike Christie 	*ret_itt = itt;
5577996a778SMike Christie 	return 0;
5587996a778SMike Christie }
5597996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_verify_itt);
5607996a778SMike Christie 
5617996a778SMike Christie void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
5627996a778SMike Christie {
5637996a778SMike Christie 	struct iscsi_session *session = conn->session;
5647996a778SMike Christie 	unsigned long flags;
5657996a778SMike Christie 
5667996a778SMike Christie 	spin_lock_irqsave(&session->lock, flags);
567656cffc9SMike Christie 	if (session->state == ISCSI_STATE_FAILED) {
568656cffc9SMike Christie 		spin_unlock_irqrestore(&session->lock, flags);
569656cffc9SMike Christie 		return;
570656cffc9SMike Christie 	}
571656cffc9SMike Christie 
57267a61114SMike Christie 	if (conn->stop_stage == 0)
5737996a778SMike Christie 		session->state = ISCSI_STATE_FAILED;
5747996a778SMike Christie 	spin_unlock_irqrestore(&session->lock, flags);
5757996a778SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
5767996a778SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
5777996a778SMike Christie 	iscsi_conn_error(conn->cls_conn, err);
5787996a778SMike Christie }
5797996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_failure);
5807996a778SMike Christie 
5817996a778SMike Christie /**
5827996a778SMike Christie  * iscsi_data_xmit - xmit any command into the scheduled connection
5837996a778SMike Christie  * @conn: iscsi connection
5847996a778SMike Christie  *
5857996a778SMike Christie  * Notes:
5867996a778SMike Christie  *	The function can return -EAGAIN in which case the caller must
5877996a778SMike Christie  *	re-schedule it again later or recover. '0' return code means
5887996a778SMike Christie  *	successful xmit.
5897996a778SMike Christie  **/
5907996a778SMike Christie static int iscsi_data_xmit(struct iscsi_conn *conn)
5917996a778SMike Christie {
5927996a778SMike Christie 	struct iscsi_transport *tt;
5933219e529SMike Christie 	int rc = 0;
5947996a778SMike Christie 
5957996a778SMike Christie 	if (unlikely(conn->suspend_tx)) {
5967996a778SMike Christie 		debug_scsi("conn %d Tx suspended!\n", conn->id);
5973219e529SMike Christie 		return -ENODATA;
5987996a778SMike Christie 	}
5997996a778SMike Christie 	tt = conn->session->tt;
6007996a778SMike Christie 
6017996a778SMike Christie 	/*
6027996a778SMike Christie 	 * Transmit in the following order:
6037996a778SMike Christie 	 *
6047996a778SMike Christie 	 * 1) un-finished xmit (ctask or mtask)
6057996a778SMike Christie 	 * 2) immediate control PDUs
6067996a778SMike Christie 	 * 3) write data
6077996a778SMike Christie 	 * 4) SCSI commands
6087996a778SMike Christie 	 * 5) non-immediate control PDUs
6097996a778SMike Christie 	 *
6107996a778SMike Christie 	 * No need to lock around __kfifo_get as long as
6117996a778SMike Christie 	 * there's one producer and one consumer.
6127996a778SMike Christie 	 */
6137996a778SMike Christie 
6147996a778SMike Christie 	BUG_ON(conn->ctask && conn->mtask);
6157996a778SMike Christie 
6167996a778SMike Christie 	if (conn->ctask) {
61760ecebf5SMike Christie 		iscsi_get_ctask(conn->ctask);
6183219e529SMike Christie 		rc = tt->xmit_cmd_task(conn, conn->ctask);
61960ecebf5SMike Christie 		iscsi_put_ctask(conn->ctask);
6203219e529SMike Christie 		if (rc)
6217996a778SMike Christie 			goto again;
6227996a778SMike Christie 		/* done with this in-progress ctask */
6237996a778SMike Christie 		conn->ctask = NULL;
6247996a778SMike Christie 	}
6257996a778SMike Christie 	if (conn->mtask) {
6263219e529SMike Christie 		rc = tt->xmit_mgmt_task(conn, conn->mtask);
6273219e529SMike Christie 	        if (rc)
6287996a778SMike Christie 		        goto again;
6297996a778SMike Christie 		/* done with this in-progress mtask */
6307996a778SMike Christie 		conn->mtask = NULL;
6317996a778SMike Christie 	}
6327996a778SMike Christie 
6337996a778SMike Christie 	/* process immediate first */
6347996a778SMike Christie         if (unlikely(__kfifo_len(conn->immqueue))) {
6357996a778SMike Christie 	        while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
6367996a778SMike Christie 			           sizeof(void*))) {
637994442e8SMike Christie 			spin_lock_bh(&conn->session->lock);
6387996a778SMike Christie 			list_add_tail(&conn->mtask->running,
6397996a778SMike Christie 				      &conn->mgmt_run_list);
640994442e8SMike Christie 			spin_unlock_bh(&conn->session->lock);
6413219e529SMike Christie 			rc = tt->xmit_mgmt_task(conn, conn->mtask);
6423219e529SMike Christie 		        if (rc)
6437996a778SMike Christie 			        goto again;
6447996a778SMike Christie 	        }
6457996a778SMike Christie 		/* done with this mtask */
6467996a778SMike Christie 		conn->mtask = NULL;
6477996a778SMike Christie 	}
6487996a778SMike Christie 
6497996a778SMike Christie 	/* process command queue */
650b6c395edSMike Christie 	spin_lock_bh(&conn->session->lock);
651b6c395edSMike Christie 	while (!list_empty(&conn->xmitqueue)) {
6527996a778SMike Christie 		/*
6537996a778SMike Christie 		 * iscsi tcp may readd the task to the xmitqueue to send
6547996a778SMike Christie 		 * write data
6557996a778SMike Christie 		 */
656b6c395edSMike Christie 		conn->ctask = list_entry(conn->xmitqueue.next,
657b6c395edSMike Christie 					 struct iscsi_cmd_task, running);
658b6c395edSMike Christie 		conn->ctask->state = ISCSI_TASK_RUNNING;
659b6c395edSMike Christie 		list_move_tail(conn->xmitqueue.next, &conn->run_list);
66060ecebf5SMike Christie 		__iscsi_get_ctask(conn->ctask);
661994442e8SMike Christie 		spin_unlock_bh(&conn->session->lock);
662b6c395edSMike Christie 
6633219e529SMike Christie 		rc = tt->xmit_cmd_task(conn, conn->ctask);
6643219e529SMike Christie 		if (rc)
6657996a778SMike Christie 			goto again;
66660ecebf5SMike Christie 
667b6c395edSMike Christie 		spin_lock_bh(&conn->session->lock);
66860ecebf5SMike Christie 		__iscsi_put_ctask(conn->ctask);
66960ecebf5SMike Christie 		if (rc) {
67060ecebf5SMike Christie 			spin_unlock_bh(&conn->session->lock);
67160ecebf5SMike Christie 			goto again;
67260ecebf5SMike Christie 		}
6737996a778SMike Christie 	}
674b6c395edSMike Christie 	spin_unlock_bh(&conn->session->lock);
6757996a778SMike Christie 	/* done with this ctask */
6767996a778SMike Christie 	conn->ctask = NULL;
6777996a778SMike Christie 
6787996a778SMike Christie 	/* process the rest control plane PDUs, if any */
6797996a778SMike Christie         if (unlikely(__kfifo_len(conn->mgmtqueue))) {
6807996a778SMike Christie 	        while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
6817996a778SMike Christie 			           sizeof(void*))) {
682994442e8SMike Christie 			spin_lock_bh(&conn->session->lock);
6837996a778SMike Christie 			list_add_tail(&conn->mtask->running,
6847996a778SMike Christie 				      &conn->mgmt_run_list);
685994442e8SMike Christie 			spin_unlock_bh(&conn->session->lock);
6863219e529SMike Christie 		        rc = tt->xmit_mgmt_task(conn, conn->mtask);
6873219e529SMike Christie 			if (rc)
6887996a778SMike Christie 			        goto again;
6897996a778SMike Christie 	        }
6907996a778SMike Christie 		/* done with this mtask */
6917996a778SMike Christie 		conn->mtask = NULL;
6927996a778SMike Christie 	}
6937996a778SMike Christie 
6943219e529SMike Christie 	return -ENODATA;
6957996a778SMike Christie 
6967996a778SMike Christie again:
6977996a778SMike Christie 	if (unlikely(conn->suspend_tx))
6983219e529SMike Christie 		return -ENODATA;
6997996a778SMike Christie 
7003219e529SMike Christie 	return rc;
7017996a778SMike Christie }
7027996a778SMike Christie 
7037996a778SMike Christie static void iscsi_xmitworker(void *data)
7047996a778SMike Christie {
7057996a778SMike Christie 	struct iscsi_conn *conn = data;
7063219e529SMike Christie 	int rc;
7077996a778SMike Christie 	/*
7087996a778SMike Christie 	 * serialize Xmit worker on a per-connection basis.
7097996a778SMike Christie 	 */
7107996a778SMike Christie 	mutex_lock(&conn->xmitmutex);
7113219e529SMike Christie 	do {
7123219e529SMike Christie 		rc = iscsi_data_xmit(conn);
7133219e529SMike Christie 	} while (rc >= 0 || rc == -EAGAIN);
7147996a778SMike Christie 	mutex_unlock(&conn->xmitmutex);
7157996a778SMike Christie }
7167996a778SMike Christie 
7177996a778SMike Christie enum {
7187996a778SMike Christie 	FAILURE_BAD_HOST = 1,
7197996a778SMike Christie 	FAILURE_SESSION_FAILED,
7207996a778SMike Christie 	FAILURE_SESSION_FREED,
7217996a778SMike Christie 	FAILURE_WINDOW_CLOSED,
72260ecebf5SMike Christie 	FAILURE_OOM,
7237996a778SMike Christie 	FAILURE_SESSION_TERMINATE,
724656cffc9SMike Christie 	FAILURE_SESSION_IN_RECOVERY,
7257996a778SMike Christie 	FAILURE_SESSION_RECOVERY_TIMEOUT,
7267996a778SMike Christie };
7277996a778SMike Christie 
7287996a778SMike Christie int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
7297996a778SMike Christie {
7307996a778SMike Christie 	struct Scsi_Host *host;
7317996a778SMike Christie 	int reason = 0;
7327996a778SMike Christie 	struct iscsi_session *session;
7337996a778SMike Christie 	struct iscsi_conn *conn;
7347996a778SMike Christie 	struct iscsi_cmd_task *ctask = NULL;
7357996a778SMike Christie 
7367996a778SMike Christie 	sc->scsi_done = done;
7377996a778SMike Christie 	sc->result = 0;
738f47f2cf5SMike Christie 	sc->SCp.ptr = NULL;
7397996a778SMike Christie 
7407996a778SMike Christie 	host = sc->device->host;
7417996a778SMike Christie 	session = iscsi_hostdata(host->hostdata);
7427996a778SMike Christie 
7437996a778SMike Christie 	spin_lock(&session->lock);
7447996a778SMike Christie 
745656cffc9SMike Christie 	/*
746656cffc9SMike Christie 	 * ISCSI_STATE_FAILED is a temp. state. The recovery
747656cffc9SMike Christie 	 * code will decide what is best to do with command queued
748656cffc9SMike Christie 	 * during this time
749656cffc9SMike Christie 	 */
750656cffc9SMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN &&
751656cffc9SMike Christie 	    session->state != ISCSI_STATE_FAILED) {
752656cffc9SMike Christie 		/*
753656cffc9SMike Christie 		 * to handle the race between when we set the recovery state
754656cffc9SMike Christie 		 * and block the session we requeue here (commands could
755656cffc9SMike Christie 		 * be entering our queuecommand while a block is starting
756656cffc9SMike Christie 		 * up because the block code is not locked)
757656cffc9SMike Christie 		 */
758656cffc9SMike Christie 		if (session->state == ISCSI_STATE_IN_RECOVERY) {
759656cffc9SMike Christie 			reason = FAILURE_SESSION_IN_RECOVERY;
76067a61114SMike Christie 			goto reject;
7617996a778SMike Christie 		}
762656cffc9SMike Christie 
763656cffc9SMike Christie 		if (session->state == ISCSI_STATE_RECOVERY_FAILED)
764656cffc9SMike Christie 			reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
765656cffc9SMike Christie 		else if (session->state == ISCSI_STATE_TERMINATE)
766656cffc9SMike Christie 			reason = FAILURE_SESSION_TERMINATE;
767656cffc9SMike Christie 		else
7687996a778SMike Christie 			reason = FAILURE_SESSION_FREED;
7697996a778SMike Christie 		goto fault;
7707996a778SMike Christie 	}
7717996a778SMike Christie 
7727996a778SMike Christie 	/*
7737996a778SMike Christie 	 * Check for iSCSI window and take care of CmdSN wrap-around
7747996a778SMike Christie 	 */
7757996a778SMike Christie 	if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
7767996a778SMike Christie 		reason = FAILURE_WINDOW_CLOSED;
7777996a778SMike Christie 		goto reject;
7787996a778SMike Christie 	}
7797996a778SMike Christie 
7807996a778SMike Christie 	conn = session->leadconn;
7817996a778SMike Christie 
78260ecebf5SMike Christie 	if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
78360ecebf5SMike Christie 			 sizeof(void*))) {
78460ecebf5SMike Christie 		reason = FAILURE_OOM;
78560ecebf5SMike Christie 		goto reject;
78660ecebf5SMike Christie 	}
7877996a778SMike Christie 	sc->SCp.phase = session->age;
7887996a778SMike Christie 	sc->SCp.ptr = (char *)ctask;
7897996a778SMike Christie 
79060ecebf5SMike Christie 	atomic_set(&ctask->refcount, 1);
791b6c395edSMike Christie 	ctask->state = ISCSI_TASK_PENDING;
7927996a778SMike Christie 	ctask->mtask = NULL;
7937996a778SMike Christie 	ctask->conn = conn;
7947996a778SMike Christie 	ctask->sc = sc;
7957996a778SMike Christie 	INIT_LIST_HEAD(&ctask->running);
7967996a778SMike Christie 	ctask->total_length = sc->request_bufflen;
7977996a778SMike Christie 	iscsi_prep_scsi_cmd_pdu(ctask);
7987996a778SMike Christie 
7997996a778SMike Christie 	session->tt->init_cmd_task(ctask);
8007996a778SMike Christie 
801b6c395edSMike Christie 	list_add_tail(&ctask->running, &conn->xmitqueue);
8027996a778SMike Christie 	debug_scsi(
803f47f2cf5SMike Christie 	       "ctask enq [%s cid %d sc %p cdb 0x%x itt 0x%x len %d cmdsn %d "
804f47f2cf5SMike Christie 		"win %d]\n",
8057996a778SMike Christie 		sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
806f47f2cf5SMike Christie 		conn->id, sc, sc->cmnd[0], ctask->itt, sc->request_bufflen,
8077996a778SMike Christie 		session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
8087996a778SMike Christie 	spin_unlock(&session->lock);
8097996a778SMike Christie 
8107996a778SMike Christie 	scsi_queue_work(host, &conn->xmitwork);
8117996a778SMike Christie 	return 0;
8127996a778SMike Christie 
8137996a778SMike Christie reject:
8147996a778SMike Christie 	spin_unlock(&session->lock);
8157996a778SMike Christie 	debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
8167996a778SMike Christie 	return SCSI_MLQUEUE_HOST_BUSY;
8177996a778SMike Christie 
8187996a778SMike Christie fault:
8197996a778SMike Christie 	spin_unlock(&session->lock);
820be2df72eSOr Gerlitz 	printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
8217996a778SMike Christie 	       sc->cmnd[0], reason);
8227996a778SMike Christie 	sc->result = (DID_NO_CONNECT << 16);
8237996a778SMike Christie 	sc->resid = sc->request_bufflen;
8247996a778SMike Christie 	sc->scsi_done(sc);
8257996a778SMike Christie 	return 0;
8267996a778SMike Christie }
8277996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_queuecommand);
8287996a778SMike Christie 
8297996a778SMike Christie int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
8307996a778SMike Christie {
8317996a778SMike Christie 	if (depth > ISCSI_MAX_CMD_PER_LUN)
8327996a778SMike Christie 		depth = ISCSI_MAX_CMD_PER_LUN;
8337996a778SMike Christie 	scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
8347996a778SMike Christie 	return sdev->queue_depth;
8357996a778SMike Christie }
8367996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
8377996a778SMike Christie 
8387996a778SMike Christie static int
8397996a778SMike Christie iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
8407996a778SMike Christie 			char *data, uint32_t data_size)
8417996a778SMike Christie {
8427996a778SMike Christie 	struct iscsi_session *session = conn->session;
8437996a778SMike Christie 	struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
8447996a778SMike Christie 	struct iscsi_mgmt_task *mtask;
8457996a778SMike Christie 
8467996a778SMike Christie 	spin_lock_bh(&session->lock);
8477996a778SMike Christie 	if (session->state == ISCSI_STATE_TERMINATE) {
8487996a778SMike Christie 		spin_unlock_bh(&session->lock);
8497996a778SMike Christie 		return -EPERM;
8507996a778SMike Christie 	}
8517996a778SMike Christie 	if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
8527996a778SMike Christie 	    hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
8537996a778SMike Christie 		/*
8547996a778SMike Christie 		 * Login and Text are sent serially, in
8557996a778SMike Christie 		 * request-followed-by-response sequence.
8567996a778SMike Christie 		 * Same mtask can be used. Same ITT must be used.
8577996a778SMike Christie 		 * Note that login_mtask is preallocated at conn_create().
8587996a778SMike Christie 		 */
8597996a778SMike Christie 		mtask = conn->login_mtask;
8607996a778SMike Christie 	else {
8617996a778SMike Christie 		BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
8627996a778SMike Christie 		BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
8637996a778SMike Christie 
8648d2860b3SMike Christie 		nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
8657996a778SMike Christie 		if (!__kfifo_get(session->mgmtpool.queue,
8667996a778SMike Christie 				 (void*)&mtask, sizeof(void*))) {
8677996a778SMike Christie 			spin_unlock_bh(&session->lock);
8687996a778SMike Christie 			return -ENOSPC;
8697996a778SMike Christie 		}
8707996a778SMike Christie 	}
8717996a778SMike Christie 
8727996a778SMike Christie 	/*
8738d2860b3SMike Christie 	 * pre-format CmdSN for outgoing PDU.
8747996a778SMike Christie 	 */
8757996a778SMike Christie 	if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
8767996a778SMike Christie 		hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
8777996a778SMike Christie 			   (session->age << ISCSI_AGE_SHIFT);
8787996a778SMike Christie 		nop->cmdsn = cpu_to_be32(session->cmdsn);
8797996a778SMike Christie 		if (conn->c_stage == ISCSI_CONN_STARTED &&
8807996a778SMike Christie 		    !(hdr->opcode & ISCSI_OP_IMMEDIATE))
8817996a778SMike Christie 			session->cmdsn++;
8827996a778SMike Christie 	} else
8837996a778SMike Christie 		/* do not advance CmdSN */
8847996a778SMike Christie 		nop->cmdsn = cpu_to_be32(session->cmdsn);
8857996a778SMike Christie 
8867996a778SMike Christie 	if (data_size) {
8877996a778SMike Christie 		memcpy(mtask->data, data, data_size);
8887996a778SMike Christie 		mtask->data_count = data_size;
8897996a778SMike Christie 	} else
8907996a778SMike Christie 		mtask->data_count = 0;
8917996a778SMike Christie 
8927996a778SMike Christie 	INIT_LIST_HEAD(&mtask->running);
8937996a778SMike Christie 	memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
8947996a778SMike Christie 	if (session->tt->init_mgmt_task)
8957996a778SMike Christie 		session->tt->init_mgmt_task(conn, mtask, data, data_size);
8967996a778SMike Christie 	spin_unlock_bh(&session->lock);
8977996a778SMike Christie 
8987996a778SMike Christie 	debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
8997996a778SMike Christie 		   hdr->opcode, hdr->itt, data_size);
9007996a778SMike Christie 
9017996a778SMike Christie 	/*
9027996a778SMike Christie 	 * since send_pdu() could be called at least from two contexts,
9037996a778SMike Christie 	 * we need to serialize __kfifo_put, so we don't have to take
9047996a778SMike Christie 	 * additional lock on fast data-path
9057996a778SMike Christie 	 */
9067996a778SMike Christie         if (hdr->opcode & ISCSI_OP_IMMEDIATE)
9077996a778SMike Christie 	        __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
9087996a778SMike Christie 	else
9097996a778SMike Christie 	        __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
9107996a778SMike Christie 
9117996a778SMike Christie 	scsi_queue_work(session->host, &conn->xmitwork);
9127996a778SMike Christie 	return 0;
9137996a778SMike Christie }
9147996a778SMike Christie 
9157996a778SMike Christie int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
9167996a778SMike Christie 			char *data, uint32_t data_size)
9177996a778SMike Christie {
9187996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
9197996a778SMike Christie 	int rc;
9207996a778SMike Christie 
9217996a778SMike Christie 	mutex_lock(&conn->xmitmutex);
9227996a778SMike Christie 	rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
9237996a778SMike Christie 	mutex_unlock(&conn->xmitmutex);
9247996a778SMike Christie 
9257996a778SMike Christie 	return rc;
9267996a778SMike Christie }
9277996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
9287996a778SMike Christie 
9297996a778SMike Christie void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
9307996a778SMike Christie {
9317996a778SMike Christie 	struct iscsi_session *session = class_to_transport_session(cls_session);
9327996a778SMike Christie 	struct iscsi_conn *conn = session->leadconn;
9337996a778SMike Christie 
9347996a778SMike Christie 	spin_lock_bh(&session->lock);
9357996a778SMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN) {
936656cffc9SMike Christie 		session->state = ISCSI_STATE_RECOVERY_FAILED;
9377996a778SMike Christie 		if (conn)
9387996a778SMike Christie 			wake_up(&conn->ehwait);
9397996a778SMike Christie 	}
9407996a778SMike Christie 	spin_unlock_bh(&session->lock);
9417996a778SMike Christie }
9427996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
9437996a778SMike Christie 
9447996a778SMike Christie int iscsi_eh_host_reset(struct scsi_cmnd *sc)
9457996a778SMike Christie {
9467996a778SMike Christie 	struct Scsi_Host *host = sc->device->host;
9477996a778SMike Christie 	struct iscsi_session *session = iscsi_hostdata(host->hostdata);
9487996a778SMike Christie 	struct iscsi_conn *conn = session->leadconn;
9497996a778SMike Christie 	int fail_session = 0;
9507996a778SMike Christie 
9517996a778SMike Christie 	spin_lock_bh(&session->lock);
9527996a778SMike Christie 	if (session->state == ISCSI_STATE_TERMINATE) {
9537996a778SMike Christie failed:
9547996a778SMike Christie 		debug_scsi("failing host reset: session terminated "
9557996a778SMike Christie 			   "[CID %d age %d]", conn->id, session->age);
9567996a778SMike Christie 		spin_unlock_bh(&session->lock);
9577996a778SMike Christie 		return FAILED;
9587996a778SMike Christie 	}
9597996a778SMike Christie 
9607996a778SMike Christie 	if (sc->SCp.phase == session->age) {
9617996a778SMike Christie 		debug_scsi("failing connection CID %d due to SCSI host reset",
9627996a778SMike Christie 			   conn->id);
9637996a778SMike Christie 		fail_session = 1;
9647996a778SMike Christie 	}
9657996a778SMike Christie 	spin_unlock_bh(&session->lock);
9667996a778SMike Christie 
9677996a778SMike Christie 	/*
9687996a778SMike Christie 	 * we drop the lock here but the leadconn cannot be destoyed while
9697996a778SMike Christie 	 * we are in the scsi eh
9707996a778SMike Christie 	 */
971656cffc9SMike Christie 	if (fail_session)
9727996a778SMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
9737996a778SMike Christie 
9747996a778SMike Christie 	debug_scsi("iscsi_eh_host_reset wait for relogin\n");
9757996a778SMike Christie 	wait_event_interruptible(conn->ehwait,
9767996a778SMike Christie 				 session->state == ISCSI_STATE_TERMINATE ||
9777996a778SMike Christie 				 session->state == ISCSI_STATE_LOGGED_IN ||
978656cffc9SMike Christie 				 session->state == ISCSI_STATE_RECOVERY_FAILED);
9797996a778SMike Christie 	if (signal_pending(current))
9807996a778SMike Christie 		flush_signals(current);
9817996a778SMike Christie 
9827996a778SMike Christie 	spin_lock_bh(&session->lock);
9837996a778SMike Christie 	if (session->state == ISCSI_STATE_LOGGED_IN)
984be2df72eSOr Gerlitz 		printk(KERN_INFO "iscsi: host reset succeeded\n");
9857996a778SMike Christie 	else
9867996a778SMike Christie 		goto failed;
9877996a778SMike Christie 	spin_unlock_bh(&session->lock);
9887996a778SMike Christie 
9897996a778SMike Christie 	return SUCCESS;
9907996a778SMike Christie }
9917996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
9927996a778SMike Christie 
9937996a778SMike Christie static void iscsi_tmabort_timedout(unsigned long data)
9947996a778SMike Christie {
9957996a778SMike Christie 	struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
9967996a778SMike Christie 	struct iscsi_conn *conn = ctask->conn;
9977996a778SMike Christie 	struct iscsi_session *session = conn->session;
9987996a778SMike Christie 
9997996a778SMike Christie 	spin_lock(&session->lock);
10007996a778SMike Christie 	if (conn->tmabort_state == TMABORT_INITIAL) {
10017996a778SMike Christie 		conn->tmabort_state = TMABORT_TIMEDOUT;
10027996a778SMike Christie 		debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
10037996a778SMike Christie 			ctask->sc, ctask->itt);
10047996a778SMike Christie 		/* unblock eh_abort() */
10057996a778SMike Christie 		wake_up(&conn->ehwait);
10067996a778SMike Christie 	}
10077996a778SMike Christie 	spin_unlock(&session->lock);
10087996a778SMike Christie }
10097996a778SMike Christie 
10107996a778SMike Christie /* must be called with the mutex lock */
10117996a778SMike Christie static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
10127996a778SMike Christie 				 struct iscsi_cmd_task *ctask)
10137996a778SMike Christie {
10147996a778SMike Christie 	struct iscsi_conn *conn = ctask->conn;
10157996a778SMike Christie 	struct iscsi_session *session = conn->session;
10167996a778SMike Christie 	struct iscsi_tm *hdr = &conn->tmhdr;
10177996a778SMike Christie 	int rc;
10187996a778SMike Christie 
10197996a778SMike Christie 	/*
10207996a778SMike Christie 	 * ctask timed out but session is OK requests must be serialized.
10217996a778SMike Christie 	 */
10227996a778SMike Christie 	memset(hdr, 0, sizeof(struct iscsi_tm));
10237996a778SMike Christie 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
10247996a778SMike Christie 	hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
10257996a778SMike Christie 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
10267996a778SMike Christie 	memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
10277996a778SMike Christie 	hdr->rtt = ctask->hdr->itt;
10287996a778SMike Christie 	hdr->refcmdsn = ctask->hdr->cmdsn;
10297996a778SMike Christie 
10307996a778SMike Christie 	rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
10317996a778SMike Christie 				     NULL, 0);
10327996a778SMike Christie 	if (rc) {
10337996a778SMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
10347996a778SMike Christie 		debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc);
10357996a778SMike Christie 		return rc;
10367996a778SMike Christie 	}
10377996a778SMike Christie 
10387996a778SMike Christie 	debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
10397996a778SMike Christie 
10407996a778SMike Christie 	spin_lock_bh(&session->lock);
10417996a778SMike Christie 	ctask->mtask = (struct iscsi_mgmt_task *)
10427996a778SMike Christie 			session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
10437996a778SMike Christie 					ISCSI_MGMT_ITT_OFFSET];
10447996a778SMike Christie 
10457996a778SMike Christie 	if (conn->tmabort_state == TMABORT_INITIAL) {
10467996a778SMike Christie 		conn->tmfcmd_pdus_cnt++;
10477996a778SMike Christie 		conn->tmabort_timer.expires = 10*HZ + jiffies;
10487996a778SMike Christie 		conn->tmabort_timer.function = iscsi_tmabort_timedout;
10497996a778SMike Christie 		conn->tmabort_timer.data = (unsigned long)ctask;
10507996a778SMike Christie 		add_timer(&conn->tmabort_timer);
10517996a778SMike Christie 		debug_scsi("abort set timeout [itt 0x%x]", ctask->itt);
10527996a778SMike Christie 	}
10537996a778SMike Christie 	spin_unlock_bh(&session->lock);
10547996a778SMike Christie 	mutex_unlock(&conn->xmitmutex);
10557996a778SMike Christie 
10567996a778SMike Christie 	/*
10577996a778SMike Christie 	 * block eh thread until:
10587996a778SMike Christie 	 *
10597996a778SMike Christie 	 * 1) abort response
10607996a778SMike Christie 	 * 2) abort timeout
10617996a778SMike Christie 	 * 3) session is terminated or restarted or userspace has
10627996a778SMike Christie 	 * given up on recovery
10637996a778SMike Christie 	 */
10647996a778SMike Christie 	wait_event_interruptible(conn->ehwait,
10657996a778SMike Christie 				 sc->SCp.phase != session->age ||
10667996a778SMike Christie 				 session->state != ISCSI_STATE_LOGGED_IN ||
1067656cffc9SMike Christie 				 conn->tmabort_state != TMABORT_INITIAL);
10687996a778SMike Christie 	if (signal_pending(current))
10697996a778SMike Christie 		flush_signals(current);
10707996a778SMike Christie 	del_timer_sync(&conn->tmabort_timer);
10717996a778SMike Christie 
10727996a778SMike Christie 	mutex_lock(&conn->xmitmutex);
10737996a778SMike Christie 	return 0;
10747996a778SMike Christie }
10757996a778SMike Christie 
10767996a778SMike Christie /*
10777996a778SMike Christie  * xmit mutex and session lock must be held
10787996a778SMike Christie  */
1079b6c395edSMike Christie static struct iscsi_mgmt_task *
1080b6c395edSMike Christie iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1081b6c395edSMike Christie {
1082b6c395edSMike Christie 	int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1083b6c395edSMike Christie 	struct iscsi_mgmt_task *task;
1084b6c395edSMike Christie 
1085b6c395edSMike Christie 	debug_scsi("searching %d tasks\n", nr_tasks);
1086b6c395edSMike Christie 
1087b6c395edSMike Christie 	for (i = 0; i < nr_tasks; i++) {
1088b6c395edSMike Christie 		__kfifo_get(fifo, (void*)&task, sizeof(void*));
1089b6c395edSMike Christie 		debug_scsi("check task %u\n", task->itt);
1090b6c395edSMike Christie 
1091b6c395edSMike Christie 		if (task->itt == itt) {
1092b6c395edSMike Christie 			debug_scsi("matched task\n");
1093b6c395edSMike Christie 			return task;
10947996a778SMike Christie 		}
10957996a778SMike Christie 
1096b6c395edSMike Christie 		__kfifo_put(fifo, (void*)&task, sizeof(void*));
1097b6c395edSMike Christie 	}
1098b6c395edSMike Christie 	return NULL;
1099b6c395edSMike Christie }
11007996a778SMike Christie 
11017996a778SMike Christie static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
11027996a778SMike Christie {
11037996a778SMike Christie 	struct iscsi_conn *conn = ctask->conn;
11047996a778SMike Christie 	struct iscsi_session *session = conn->session;
11057996a778SMike Christie 
11067996a778SMike Christie 	if (!ctask->mtask)
11077996a778SMike Christie 		return -EINVAL;
11087996a778SMike Christie 
11097996a778SMike Christie 	if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
11107996a778SMike Christie 		list_del(&ctask->mtask->running);
11117996a778SMike Christie 	__kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
11127996a778SMike Christie 		    sizeof(void*));
11137996a778SMike Christie 	ctask->mtask = NULL;
11147996a778SMike Christie 	return 0;
11157996a778SMike Christie }
11167996a778SMike Christie 
11177996a778SMike Christie /*
11187996a778SMike Christie  * session lock and xmitmutex must be held
11197996a778SMike Christie  */
11207996a778SMike Christie static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
11217996a778SMike Christie 			 int err)
11227996a778SMike Christie {
11237996a778SMike Christie 	struct scsi_cmnd *sc;
11247996a778SMike Christie 
11257996a778SMike Christie 	sc = ctask->sc;
11267996a778SMike Christie 	if (!sc)
11277996a778SMike Christie 		return;
1128e648f63cSMike Christie 
1129e648f63cSMike Christie 	conn->session->tt->cleanup_cmd_task(conn, ctask);
11307ea8b828SMike Christie 	iscsi_ctask_mtask_cleanup(ctask);
11317ea8b828SMike Christie 
11327996a778SMike Christie 	sc->result = err;
11337996a778SMike Christie 	sc->resid = sc->request_bufflen;
1134e648f63cSMike Christie 	/* release ref from queuecommand */
113560ecebf5SMike Christie 	__iscsi_put_ctask(ctask);
11367996a778SMike Christie }
11377996a778SMike Christie 
11387996a778SMike Christie int iscsi_eh_abort(struct scsi_cmnd *sc)
11397996a778SMike Christie {
1140f47f2cf5SMike Christie 	struct iscsi_cmd_task *ctask;
1141f47f2cf5SMike Christie 	struct iscsi_conn *conn;
1142f47f2cf5SMike Christie 	struct iscsi_session *session;
11437996a778SMike Christie 	int rc;
11447996a778SMike Christie 
1145f47f2cf5SMike Christie 	/*
1146f47f2cf5SMike Christie 	 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1147f47f2cf5SMike Christie 	 * got the command.
1148f47f2cf5SMike Christie 	 */
1149f47f2cf5SMike Christie 	if (!sc->SCp.ptr) {
1150f47f2cf5SMike Christie 		debug_scsi("sc never reached iscsi layer or it completed.\n");
1151f47f2cf5SMike Christie 		return SUCCESS;
1152f47f2cf5SMike Christie 	}
1153f47f2cf5SMike Christie 
1154f47f2cf5SMike Christie 	ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1155f47f2cf5SMike Christie 	conn = ctask->conn;
1156f47f2cf5SMike Christie 	session = conn->session;
1157f47f2cf5SMike Christie 
11587996a778SMike Christie 	conn->eh_abort_cnt++;
11597996a778SMike Christie 	debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
11607996a778SMike Christie 
11617996a778SMike Christie 	mutex_lock(&conn->xmitmutex);
11627996a778SMike Christie 	spin_lock_bh(&session->lock);
11637996a778SMike Christie 
11647996a778SMike Christie 	/*
11657996a778SMike Christie 	 * If we are not logged in or we have started a new session
11667996a778SMike Christie 	 * then let the host reset code handle this
11677996a778SMike Christie 	 */
11687996a778SMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN ||
11697996a778SMike Christie 	    sc->SCp.phase != session->age)
11707996a778SMike Christie 		goto failed;
11717996a778SMike Christie 
11727996a778SMike Christie 	/* ctask completed before time out */
11737ea8b828SMike Christie 	if (!ctask->sc) {
11747ea8b828SMike Christie 		spin_unlock_bh(&session->lock);
11757ea8b828SMike Christie 		debug_scsi("sc completed while abort in progress\n");
11767ea8b828SMike Christie 		goto success_rel_mutex;
11777ea8b828SMike Christie 	}
11787996a778SMike Christie 
11797996a778SMike Christie 	/* what should we do here ? */
11807996a778SMike Christie 	if (conn->ctask == ctask) {
1181be2df72eSOr Gerlitz 		printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1182be2df72eSOr Gerlitz 		       "Failing abort\n", sc, ctask->itt);
11837996a778SMike Christie 		goto failed;
11847996a778SMike Christie 	}
11857996a778SMike Christie 
1186b6c395edSMike Christie 	if (ctask->state == ISCSI_TASK_PENDING)
11877ea8b828SMike Christie 		goto success_cleanup;
11887996a778SMike Christie 
11897996a778SMike Christie 	conn->tmabort_state = TMABORT_INITIAL;
11907996a778SMike Christie 
11917996a778SMike Christie 	spin_unlock_bh(&session->lock);
11927996a778SMike Christie 	rc = iscsi_exec_abort_task(sc, ctask);
11937996a778SMike Christie 	spin_lock_bh(&session->lock);
11947996a778SMike Christie 
11957996a778SMike Christie 	if (rc || sc->SCp.phase != session->age ||
11967996a778SMike Christie 	    session->state != ISCSI_STATE_LOGGED_IN)
11977996a778SMike Christie 		goto failed;
11987ea8b828SMike Christie 	iscsi_ctask_mtask_cleanup(ctask);
11997996a778SMike Christie 
12007ea8b828SMike Christie 	switch (conn->tmabort_state) {
12017ea8b828SMike Christie 	case TMABORT_SUCCESS:
12027ea8b828SMike Christie 		goto success_cleanup;
12037ea8b828SMike Christie 	case TMABORT_NOT_FOUND:
12047996a778SMike Christie 		if (!ctask->sc) {
12057ea8b828SMike Christie 			/* ctask completed before tmf abort response */
12067ea8b828SMike Christie 			spin_unlock_bh(&session->lock);
12077996a778SMike Christie 			debug_scsi("sc completed while abort in progress\n");
12087ea8b828SMike Christie 			goto success_rel_mutex;
12097996a778SMike Christie 		}
12107ea8b828SMike Christie 		/* fall through */
12117ea8b828SMike Christie 	default:
12127ea8b828SMike Christie 		/* timedout or failed */
12137996a778SMike Christie 		spin_unlock_bh(&session->lock);
12147996a778SMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
12157996a778SMike Christie 		spin_lock_bh(&session->lock);
12167996a778SMike Christie 		goto failed;
12177996a778SMike Christie 	}
12187996a778SMike Christie 
12197ea8b828SMike Christie success_cleanup:
12207996a778SMike Christie 	debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
12217996a778SMike Christie 	spin_unlock_bh(&session->lock);
12227996a778SMike Christie 
12237996a778SMike Christie 	/*
12247996a778SMike Christie 	 * clean up task if aborted. we have the xmitmutex so grab
12257996a778SMike Christie 	 * the recv lock as a writer
12267996a778SMike Christie 	 */
12277996a778SMike Christie 	write_lock_bh(conn->recv_lock);
12287996a778SMike Christie 	spin_lock(&session->lock);
12297996a778SMike Christie 	fail_command(conn, ctask, DID_ABORT << 16);
12307996a778SMike Christie 	spin_unlock(&session->lock);
12317996a778SMike Christie 	write_unlock_bh(conn->recv_lock);
12327996a778SMike Christie 
12337ea8b828SMike Christie success_rel_mutex:
12347996a778SMike Christie 	mutex_unlock(&conn->xmitmutex);
12357996a778SMike Christie 	return SUCCESS;
12367996a778SMike Christie 
12377996a778SMike Christie failed:
12387996a778SMike Christie 	spin_unlock_bh(&session->lock);
12397996a778SMike Christie 	mutex_unlock(&conn->xmitmutex);
12407996a778SMike Christie 
12417996a778SMike Christie 	debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
12427996a778SMike Christie 	return FAILED;
12437996a778SMike Christie }
12447996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_abort);
12457996a778SMike Christie 
12467996a778SMike Christie int
12477996a778SMike Christie iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
12487996a778SMike Christie {
12497996a778SMike Christie 	int i;
12507996a778SMike Christie 
12517996a778SMike Christie 	*items = kmalloc(max * sizeof(void*), GFP_KERNEL);
12527996a778SMike Christie 	if (*items == NULL)
12537996a778SMike Christie 		return -ENOMEM;
12547996a778SMike Christie 
12557996a778SMike Christie 	q->max = max;
12567996a778SMike Christie 	q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
12577996a778SMike Christie 	if (q->pool == NULL) {
12587996a778SMike Christie 		kfree(*items);
12597996a778SMike Christie 		return -ENOMEM;
12607996a778SMike Christie 	}
12617996a778SMike Christie 
12627996a778SMike Christie 	q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
12637996a778SMike Christie 			      GFP_KERNEL, NULL);
12647996a778SMike Christie 	if (q->queue == ERR_PTR(-ENOMEM)) {
12657996a778SMike Christie 		kfree(q->pool);
12667996a778SMike Christie 		kfree(*items);
12677996a778SMike Christie 		return -ENOMEM;
12687996a778SMike Christie 	}
12697996a778SMike Christie 
12707996a778SMike Christie 	for (i = 0; i < max; i++) {
12717996a778SMike Christie 		q->pool[i] = kmalloc(item_size, GFP_KERNEL);
12727996a778SMike Christie 		if (q->pool[i] == NULL) {
12737996a778SMike Christie 			int j;
12747996a778SMike Christie 
12757996a778SMike Christie 			for (j = 0; j < i; j++)
12767996a778SMike Christie 				kfree(q->pool[j]);
12777996a778SMike Christie 
12787996a778SMike Christie 			kfifo_free(q->queue);
12797996a778SMike Christie 			kfree(q->pool);
12807996a778SMike Christie 			kfree(*items);
12817996a778SMike Christie 			return -ENOMEM;
12827996a778SMike Christie 		}
12837996a778SMike Christie 		memset(q->pool[i], 0, item_size);
12847996a778SMike Christie 		(*items)[i] = q->pool[i];
12857996a778SMike Christie 		__kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
12867996a778SMike Christie 	}
12877996a778SMike Christie 	return 0;
12887996a778SMike Christie }
12897996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_init);
12907996a778SMike Christie 
12917996a778SMike Christie void iscsi_pool_free(struct iscsi_queue *q, void **items)
12927996a778SMike Christie {
12937996a778SMike Christie 	int i;
12947996a778SMike Christie 
12957996a778SMike Christie 	for (i = 0; i < q->max; i++)
12967996a778SMike Christie 		kfree(items[i]);
12977996a778SMike Christie 	kfree(q->pool);
12987996a778SMike Christie 	kfree(items);
12997996a778SMike Christie }
13007996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_free);
13017996a778SMike Christie 
13027996a778SMike Christie /*
13037996a778SMike Christie  * iSCSI Session's hostdata organization:
13047996a778SMike Christie  *
13057996a778SMike Christie  *    *------------------* <== hostdata_session(host->hostdata)
13067996a778SMike Christie  *    | ptr to class sess|
13077996a778SMike Christie  *    |------------------| <== iscsi_hostdata(host->hostdata)
13087996a778SMike Christie  *    | iscsi_session    |
13097996a778SMike Christie  *    *------------------*
13107996a778SMike Christie  */
13117996a778SMike Christie 
13127996a778SMike Christie #define hostdata_privsize(_sz)	(sizeof(unsigned long) + _sz + \
13137996a778SMike Christie 				 _sz % sizeof(unsigned long))
13147996a778SMike Christie 
13157996a778SMike Christie #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
13167996a778SMike Christie 
13177996a778SMike Christie /**
13187996a778SMike Christie  * iscsi_session_setup - create iscsi cls session and host and session
13197996a778SMike Christie  * @scsit: scsi transport template
13207996a778SMike Christie  * @iscsit: iscsi transport template
13217996a778SMike Christie  * @initial_cmdsn: initial CmdSN
13227996a778SMike Christie  * @hostno: host no allocated
13237996a778SMike Christie  *
13247996a778SMike Christie  * This can be used by software iscsi_transports that allocate
13257996a778SMike Christie  * a session per scsi host.
13267996a778SMike Christie  **/
13277996a778SMike Christie struct iscsi_cls_session *
13287996a778SMike Christie iscsi_session_setup(struct iscsi_transport *iscsit,
13297996a778SMike Christie 		    struct scsi_transport_template *scsit,
13307996a778SMike Christie 		    int cmd_task_size, int mgmt_task_size,
13317996a778SMike Christie 		    uint32_t initial_cmdsn, uint32_t *hostno)
13327996a778SMike Christie {
13337996a778SMike Christie 	struct Scsi_Host *shost;
13347996a778SMike Christie 	struct iscsi_session *session;
13357996a778SMike Christie 	struct iscsi_cls_session *cls_session;
13367996a778SMike Christie 	int cmd_i;
13377996a778SMike Christie 
13387996a778SMike Christie 	shost = scsi_host_alloc(iscsit->host_template,
13397996a778SMike Christie 				hostdata_privsize(sizeof(*session)));
13407996a778SMike Christie 	if (!shost)
13417996a778SMike Christie 		return NULL;
13427996a778SMike Christie 
13437996a778SMike Christie 	shost->max_id = 1;
13447996a778SMike Christie 	shost->max_channel = 0;
13457996a778SMike Christie 	shost->max_lun = iscsit->max_lun;
13467996a778SMike Christie 	shost->max_cmd_len = iscsit->max_cmd_len;
13477996a778SMike Christie 	shost->transportt = scsit;
13487996a778SMike Christie 	shost->transportt->create_work_queue = 1;
13497996a778SMike Christie 	*hostno = shost->host_no;
13507996a778SMike Christie 
13517996a778SMike Christie 	session = iscsi_hostdata(shost->hostdata);
13527996a778SMike Christie 	memset(session, 0, sizeof(struct iscsi_session));
13537996a778SMike Christie 	session->host = shost;
13547996a778SMike Christie 	session->state = ISCSI_STATE_FREE;
13557996a778SMike Christie 	session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
13567996a778SMike Christie 	session->cmds_max = ISCSI_XMIT_CMDS_MAX;
13577996a778SMike Christie 	session->cmdsn = initial_cmdsn;
13587996a778SMike Christie 	session->exp_cmdsn = initial_cmdsn + 1;
13597996a778SMike Christie 	session->max_cmdsn = initial_cmdsn + 1;
13607996a778SMike Christie 	session->max_r2t = 1;
13617996a778SMike Christie 	session->tt = iscsit;
13627996a778SMike Christie 
13637996a778SMike Christie 	/* initialize SCSI PDU commands pool */
13647996a778SMike Christie 	if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
13657996a778SMike Christie 			    (void***)&session->cmds,
13667996a778SMike Christie 			    cmd_task_size + sizeof(struct iscsi_cmd_task)))
13677996a778SMike Christie 		goto cmdpool_alloc_fail;
13687996a778SMike Christie 
13697996a778SMike Christie 	/* pre-format cmds pool with ITT */
13707996a778SMike Christie 	for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
13717996a778SMike Christie 		struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
13727996a778SMike Christie 
13737996a778SMike Christie 		if (cmd_task_size)
13747996a778SMike Christie 			ctask->dd_data = &ctask[1];
13757996a778SMike Christie 		ctask->itt = cmd_i;
1376b6c395edSMike Christie 		INIT_LIST_HEAD(&ctask->running);
13777996a778SMike Christie 	}
13787996a778SMike Christie 
13797996a778SMike Christie 	spin_lock_init(&session->lock);
13807996a778SMike Christie 	INIT_LIST_HEAD(&session->connections);
13817996a778SMike Christie 
13827996a778SMike Christie 	/* initialize immediate command pool */
13837996a778SMike Christie 	if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
13847996a778SMike Christie 			   (void***)&session->mgmt_cmds,
13857996a778SMike Christie 			   mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
13867996a778SMike Christie 		goto mgmtpool_alloc_fail;
13877996a778SMike Christie 
13887996a778SMike Christie 
13897996a778SMike Christie 	/* pre-format immediate cmds pool with ITT */
13907996a778SMike Christie 	for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
13917996a778SMike Christie 		struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
13927996a778SMike Christie 
13937996a778SMike Christie 		if (mgmt_task_size)
13947996a778SMike Christie 			mtask->dd_data = &mtask[1];
13957996a778SMike Christie 		mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
1396b6c395edSMike Christie 		INIT_LIST_HEAD(&mtask->running);
13977996a778SMike Christie 	}
13987996a778SMike Christie 
13997996a778SMike Christie 	if (scsi_add_host(shost, NULL))
14007996a778SMike Christie 		goto add_host_fail;
14017996a778SMike Christie 
1402f53a88daSMike Christie 	if (!try_module_get(iscsit->owner))
1403f53a88daSMike Christie 		goto cls_session_fail;
1404f53a88daSMike Christie 
14056a8a0d36SMike Christie 	cls_session = iscsi_create_session(shost, iscsit, 0);
14067996a778SMike Christie 	if (!cls_session)
1407f53a88daSMike Christie 		goto module_put;
14087996a778SMike Christie 	*(unsigned long*)shost->hostdata = (unsigned long)cls_session;
14097996a778SMike Christie 
14107996a778SMike Christie 	return cls_session;
14117996a778SMike Christie 
1412f53a88daSMike Christie module_put:
1413f53a88daSMike Christie 	module_put(iscsit->owner);
14147996a778SMike Christie cls_session_fail:
14157996a778SMike Christie 	scsi_remove_host(shost);
14167996a778SMike Christie add_host_fail:
14177996a778SMike Christie 	iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
14187996a778SMike Christie mgmtpool_alloc_fail:
14197996a778SMike Christie 	iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
14207996a778SMike Christie cmdpool_alloc_fail:
14217996a778SMike Christie 	scsi_host_put(shost);
14227996a778SMike Christie 	return NULL;
14237996a778SMike Christie }
14247996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_setup);
14257996a778SMike Christie 
14267996a778SMike Christie /**
14277996a778SMike Christie  * iscsi_session_teardown - destroy session, host, and cls_session
14287996a778SMike Christie  * shost: scsi host
14297996a778SMike Christie  *
14307996a778SMike Christie  * This can be used by software iscsi_transports that allocate
14317996a778SMike Christie  * a session per scsi host.
14327996a778SMike Christie  **/
14337996a778SMike Christie void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
14347996a778SMike Christie {
14357996a778SMike Christie 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
14367996a778SMike Christie 	struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
143763f75cc8SMike Christie 	struct module *owner = cls_session->transport->owner;
14387996a778SMike Christie 
14397996a778SMike Christie 	scsi_remove_host(shost);
14407996a778SMike Christie 
14417996a778SMike Christie 	iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
14427996a778SMike Christie 	iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
14437996a778SMike Christie 
1444f3ff0c36SMike Christie 	kfree(session->targetname);
1445f3ff0c36SMike Christie 
14467996a778SMike Christie 	iscsi_destroy_session(cls_session);
14477996a778SMike Christie 	scsi_host_put(shost);
144863f75cc8SMike Christie 	module_put(owner);
14497996a778SMike Christie }
14507996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_teardown);
14517996a778SMike Christie 
14527996a778SMike Christie /**
14537996a778SMike Christie  * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
14547996a778SMike Christie  * @cls_session: iscsi_cls_session
14557996a778SMike Christie  * @conn_idx: cid
14567996a778SMike Christie  **/
14577996a778SMike Christie struct iscsi_cls_conn *
14587996a778SMike Christie iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
14597996a778SMike Christie {
14607996a778SMike Christie 	struct iscsi_session *session = class_to_transport_session(cls_session);
14617996a778SMike Christie 	struct iscsi_conn *conn;
14627996a778SMike Christie 	struct iscsi_cls_conn *cls_conn;
1463d36ab6f3SMike Christie 	char *data;
14647996a778SMike Christie 
14657996a778SMike Christie 	cls_conn = iscsi_create_conn(cls_session, conn_idx);
14667996a778SMike Christie 	if (!cls_conn)
14677996a778SMike Christie 		return NULL;
14687996a778SMike Christie 	conn = cls_conn->dd_data;
14697996a778SMike Christie 	memset(conn, 0, sizeof(*conn));
14707996a778SMike Christie 
14717996a778SMike Christie 	conn->session = session;
14727996a778SMike Christie 	conn->cls_conn = cls_conn;
14737996a778SMike Christie 	conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
14747996a778SMike Christie 	conn->id = conn_idx;
14757996a778SMike Christie 	conn->exp_statsn = 0;
14767996a778SMike Christie 	conn->tmabort_state = TMABORT_INITIAL;
14777996a778SMike Christie 	INIT_LIST_HEAD(&conn->run_list);
14787996a778SMike Christie 	INIT_LIST_HEAD(&conn->mgmt_run_list);
1479b6c395edSMike Christie 	INIT_LIST_HEAD(&conn->xmitqueue);
14807996a778SMike Christie 
14817996a778SMike Christie 	/* initialize general immediate & non-immediate PDU commands queue */
14827996a778SMike Christie 	conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
14837996a778SMike Christie 			                GFP_KERNEL, NULL);
14847996a778SMike Christie 	if (conn->immqueue == ERR_PTR(-ENOMEM))
14857996a778SMike Christie 		goto immqueue_alloc_fail;
14867996a778SMike Christie 
14877996a778SMike Christie 	conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
14887996a778SMike Christie 			                GFP_KERNEL, NULL);
14897996a778SMike Christie 	if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
14907996a778SMike Christie 		goto mgmtqueue_alloc_fail;
14917996a778SMike Christie 
14927996a778SMike Christie 	INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
14937996a778SMike Christie 
14947996a778SMike Christie 	/* allocate login_mtask used for the login/text sequences */
14957996a778SMike Christie 	spin_lock_bh(&session->lock);
14967996a778SMike Christie 	if (!__kfifo_get(session->mgmtpool.queue,
14977996a778SMike Christie                          (void*)&conn->login_mtask,
14987996a778SMike Christie 			 sizeof(void*))) {
14997996a778SMike Christie 		spin_unlock_bh(&session->lock);
15007996a778SMike Christie 		goto login_mtask_alloc_fail;
15017996a778SMike Christie 	}
15027996a778SMike Christie 	spin_unlock_bh(&session->lock);
15037996a778SMike Christie 
1504d36ab6f3SMike Christie 	data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
1505d36ab6f3SMike Christie 	if (!data)
1506d36ab6f3SMike Christie 		goto login_mtask_data_alloc_fail;
1507c8dc1e52SMike Christie 	conn->login_mtask->data = conn->data = data;
1508d36ab6f3SMike Christie 
15097996a778SMike Christie 	init_timer(&conn->tmabort_timer);
15107996a778SMike Christie 	mutex_init(&conn->xmitmutex);
15117996a778SMike Christie 	init_waitqueue_head(&conn->ehwait);
15127996a778SMike Christie 
15137996a778SMike Christie 	return cls_conn;
15147996a778SMike Christie 
1515d36ab6f3SMike Christie login_mtask_data_alloc_fail:
1516d36ab6f3SMike Christie 	__kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1517d36ab6f3SMike Christie 		    sizeof(void*));
15187996a778SMike Christie login_mtask_alloc_fail:
15197996a778SMike Christie 	kfifo_free(conn->mgmtqueue);
15207996a778SMike Christie mgmtqueue_alloc_fail:
15217996a778SMike Christie 	kfifo_free(conn->immqueue);
15227996a778SMike Christie immqueue_alloc_fail:
15237996a778SMike Christie 	iscsi_destroy_conn(cls_conn);
15247996a778SMike Christie 	return NULL;
15257996a778SMike Christie }
15267996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_setup);
15277996a778SMike Christie 
15287996a778SMike Christie /**
15297996a778SMike Christie  * iscsi_conn_teardown - teardown iscsi connection
15307996a778SMike Christie  * cls_conn: iscsi class connection
15317996a778SMike Christie  *
15327996a778SMike Christie  * TODO: we may need to make this into a two step process
15337996a778SMike Christie  * like scsi-mls remove + put host
15347996a778SMike Christie  */
15357996a778SMike Christie void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
15367996a778SMike Christie {
15377996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
15387996a778SMike Christie 	struct iscsi_session *session = conn->session;
15397996a778SMike Christie 	unsigned long flags;
15407996a778SMike Christie 
15417996a778SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
154267a61114SMike Christie 	mutex_lock(&conn->xmitmutex);
15437996a778SMike Christie 
15447996a778SMike Christie 	spin_lock_bh(&session->lock);
15457996a778SMike Christie 	conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
15467996a778SMike Christie 	if (session->leadconn == conn) {
15477996a778SMike Christie 		/*
15487996a778SMike Christie 		 * leading connection? then give up on recovery.
15497996a778SMike Christie 		 */
15507996a778SMike Christie 		session->state = ISCSI_STATE_TERMINATE;
15517996a778SMike Christie 		wake_up(&conn->ehwait);
15527996a778SMike Christie 	}
15537996a778SMike Christie 	spin_unlock_bh(&session->lock);
15547996a778SMike Christie 
15557996a778SMike Christie 	mutex_unlock(&conn->xmitmutex);
15567996a778SMike Christie 
15577996a778SMike Christie 	/*
15587996a778SMike Christie 	 * Block until all in-progress commands for this connection
15597996a778SMike Christie 	 * time out or fail.
15607996a778SMike Christie 	 */
15617996a778SMike Christie 	for (;;) {
15627996a778SMike Christie 		spin_lock_irqsave(session->host->host_lock, flags);
15637996a778SMike Christie 		if (!session->host->host_busy) { /* OK for ERL == 0 */
15647996a778SMike Christie 			spin_unlock_irqrestore(session->host->host_lock, flags);
15657996a778SMike Christie 			break;
15667996a778SMike Christie 		}
15677996a778SMike Christie 		spin_unlock_irqrestore(session->host->host_lock, flags);
15687996a778SMike Christie 		msleep_interruptible(500);
1569be2df72eSOr Gerlitz 		printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1570be2df72eSOr Gerlitz 		       "host_failed %d\n", session->host->host_busy,
1571be2df72eSOr Gerlitz 		       session->host->host_failed);
15727996a778SMike Christie 		/*
15737996a778SMike Christie 		 * force eh_abort() to unblock
15747996a778SMike Christie 		 */
15757996a778SMike Christie 		wake_up(&conn->ehwait);
15767996a778SMike Christie 	}
15777996a778SMike Christie 
15787996a778SMike Christie 	spin_lock_bh(&session->lock);
1579c8dc1e52SMike Christie 	kfree(conn->data);
1580f3ff0c36SMike Christie 	kfree(conn->persistent_address);
15817996a778SMike Christie 	__kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
15827996a778SMike Christie 		    sizeof(void*));
15837996a778SMike Christie 	list_del(&conn->item);
15847996a778SMike Christie 	if (list_empty(&session->connections))
15857996a778SMike Christie 		session->leadconn = NULL;
15867996a778SMike Christie 	if (session->leadconn && session->leadconn == conn)
15877996a778SMike Christie 		session->leadconn = container_of(session->connections.next,
15887996a778SMike Christie 			struct iscsi_conn, item);
15897996a778SMike Christie 
15907996a778SMike Christie 	if (session->leadconn == NULL)
15917996a778SMike Christie 		/* no connections exits.. reset sequencing */
15927996a778SMike Christie 		session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
15937996a778SMike Christie 	spin_unlock_bh(&session->lock);
15947996a778SMike Christie 
15957996a778SMike Christie 	kfifo_free(conn->immqueue);
15967996a778SMike Christie 	kfifo_free(conn->mgmtqueue);
15977996a778SMike Christie 
15987996a778SMike Christie 	iscsi_destroy_conn(cls_conn);
15997996a778SMike Christie }
16007996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
16017996a778SMike Christie 
16027996a778SMike Christie int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
16037996a778SMike Christie {
16047996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
16057996a778SMike Christie 	struct iscsi_session *session = conn->session;
16067996a778SMike Christie 
1607ffd0436eSMike Christie 	if (!session) {
16087996a778SMike Christie 		printk(KERN_ERR "iscsi: can't start unbound connection\n");
16097996a778SMike Christie 		return -EPERM;
16107996a778SMike Christie 	}
16117996a778SMike Christie 
1612db98ccdeSMike Christie 	if ((session->imm_data_en || !session->initial_r2t_en) &&
1613db98ccdeSMike Christie 	     session->first_burst > session->max_burst) {
1614ffd0436eSMike Christie 		printk("iscsi: invalid burst lengths: "
1615ffd0436eSMike Christie 		       "first_burst %d max_burst %d\n",
1616ffd0436eSMike Christie 		       session->first_burst, session->max_burst);
1617ffd0436eSMike Christie 		return -EINVAL;
1618ffd0436eSMike Christie 	}
1619ffd0436eSMike Christie 
16207996a778SMike Christie 	spin_lock_bh(&session->lock);
16217996a778SMike Christie 	conn->c_stage = ISCSI_CONN_STARTED;
16227996a778SMike Christie 	session->state = ISCSI_STATE_LOGGED_IN;
16237996a778SMike Christie 
16247996a778SMike Christie 	switch(conn->stop_stage) {
16257996a778SMike Christie 	case STOP_CONN_RECOVER:
16267996a778SMike Christie 		/*
16277996a778SMike Christie 		 * unblock eh_abort() if it is blocked. re-try all
16287996a778SMike Christie 		 * commands after successful recovery
16297996a778SMike Christie 		 */
16307996a778SMike Christie 		conn->stop_stage = 0;
16317996a778SMike Christie 		conn->tmabort_state = TMABORT_INITIAL;
16327996a778SMike Christie 		session->age++;
16337996a778SMike Christie 		spin_unlock_bh(&session->lock);
16347996a778SMike Christie 
16357996a778SMike Christie 		iscsi_unblock_session(session_to_cls(session));
16367996a778SMike Christie 		wake_up(&conn->ehwait);
16377996a778SMike Christie 		return 0;
16387996a778SMike Christie 	case STOP_CONN_TERM:
16397996a778SMike Christie 		conn->stop_stage = 0;
16407996a778SMike Christie 		break;
16417996a778SMike Christie 	default:
16427996a778SMike Christie 		break;
16437996a778SMike Christie 	}
16447996a778SMike Christie 	spin_unlock_bh(&session->lock);
16457996a778SMike Christie 
16467996a778SMike Christie 	return 0;
16477996a778SMike Christie }
16487996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_start);
16497996a778SMike Christie 
16507996a778SMike Christie static void
16517996a778SMike Christie flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
16527996a778SMike Christie {
16537996a778SMike Christie 	struct iscsi_mgmt_task *mtask, *tmp;
16547996a778SMike Christie 
16557996a778SMike Christie 	/* handle pending */
16567996a778SMike Christie 	while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
16577996a778SMike Christie 	       __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
16587996a778SMike Christie 		if (mtask == conn->login_mtask)
16597996a778SMike Christie 			continue;
16607996a778SMike Christie 		debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
16617996a778SMike Christie 		__kfifo_put(session->mgmtpool.queue, (void*)&mtask,
16627996a778SMike Christie 			    sizeof(void*));
16637996a778SMike Christie 	}
16647996a778SMike Christie 
16657996a778SMike Christie 	/* handle running */
16667996a778SMike Christie 	list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
16677996a778SMike Christie 		debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
1668ed2abc7fSMike Christie 		list_del(&mtask->running);
1669ed2abc7fSMike Christie 
16707996a778SMike Christie 		if (mtask == conn->login_mtask)
16717996a778SMike Christie 			continue;
1672ed2abc7fSMike Christie 		__kfifo_put(session->mgmtpool.queue, (void*)&mtask,
16737996a778SMike Christie 			   sizeof(void*));
16747996a778SMike Christie 	}
16757996a778SMike Christie 
16767996a778SMike Christie 	conn->mtask = NULL;
16777996a778SMike Christie }
16787996a778SMike Christie 
16797996a778SMike Christie /* Fail commands. Mutex and session lock held and recv side suspended */
16807996a778SMike Christie static void fail_all_commands(struct iscsi_conn *conn)
16817996a778SMike Christie {
16827996a778SMike Christie 	struct iscsi_cmd_task *ctask, *tmp;
16837996a778SMike Christie 
16847996a778SMike Christie 	/* flush pending */
1685b6c395edSMike Christie 	list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
16867996a778SMike Christie 		debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
16877996a778SMike Christie 			   ctask->itt);
16887996a778SMike Christie 		fail_command(conn, ctask, DID_BUS_BUSY << 16);
16897996a778SMike Christie 	}
16907996a778SMike Christie 
16917996a778SMike Christie 	/* fail all other running */
16927996a778SMike Christie 	list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
16937996a778SMike Christie 		debug_scsi("failing in progress sc %p itt 0x%x\n",
16947996a778SMike Christie 			   ctask->sc, ctask->itt);
16957996a778SMike Christie 		fail_command(conn, ctask, DID_BUS_BUSY << 16);
16967996a778SMike Christie 	}
16977996a778SMike Christie 
16987996a778SMike Christie 	conn->ctask = NULL;
16997996a778SMike Christie }
17007996a778SMike Christie 
1701656cffc9SMike Christie static void iscsi_start_session_recovery(struct iscsi_session *session,
17027996a778SMike Christie 					 struct iscsi_conn *conn, int flag)
17037996a778SMike Christie {
1704ed2abc7fSMike Christie 	int old_stop_stage;
1705ed2abc7fSMike Christie 
17067996a778SMike Christie 	spin_lock_bh(&session->lock);
1707ed2abc7fSMike Christie 	if (conn->stop_stage == STOP_CONN_TERM) {
17087996a778SMike Christie 		spin_unlock_bh(&session->lock);
17097996a778SMike Christie 		return;
17107996a778SMike Christie 	}
1711ed2abc7fSMike Christie 
1712ed2abc7fSMike Christie 	/*
1713ed2abc7fSMike Christie 	 * When this is called for the in_login state, we only want to clean
171467a61114SMike Christie 	 * up the login task and connection. We do not need to block and set
171567a61114SMike Christie 	 * the recovery state again
1716ed2abc7fSMike Christie 	 */
171767a61114SMike Christie 	if (flag == STOP_CONN_TERM)
171867a61114SMike Christie 		session->state = ISCSI_STATE_TERMINATE;
171967a61114SMike Christie 	else if (conn->stop_stage != STOP_CONN_RECOVER)
172067a61114SMike Christie 		session->state = ISCSI_STATE_IN_RECOVERY;
1721ed2abc7fSMike Christie 
1722ed2abc7fSMike Christie 	old_stop_stage = conn->stop_stage;
17237996a778SMike Christie 	conn->stop_stage = flag;
172467a61114SMike Christie 	conn->c_stage = ISCSI_CONN_STOPPED;
172567a61114SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
17267996a778SMike Christie 	spin_unlock_bh(&session->lock);
17277996a778SMike Christie 
17281c83469dSMike Christie 	write_lock_bh(conn->recv_lock);
17291c83469dSMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
17301c83469dSMike Christie 	write_unlock_bh(conn->recv_lock);
17317996a778SMike Christie 
17327996a778SMike Christie 	mutex_lock(&conn->xmitmutex);
17337996a778SMike Christie 	/*
17347996a778SMike Christie 	 * for connection level recovery we should not calculate
17357996a778SMike Christie 	 * header digest. conn->hdr_size used for optimization
17367996a778SMike Christie 	 * in hdr_extract() and will be re-negotiated at
17377996a778SMike Christie 	 * set_param() time.
17387996a778SMike Christie 	 */
17397996a778SMike Christie 	if (flag == STOP_CONN_RECOVER) {
17407996a778SMike Christie 		conn->hdrdgst_en = 0;
17417996a778SMike Christie 		conn->datadgst_en = 0;
1742656cffc9SMike Christie 		if (session->state == ISCSI_STATE_IN_RECOVERY &&
174367a61114SMike Christie 		    old_stop_stage != STOP_CONN_RECOVER) {
174467a61114SMike Christie 			debug_scsi("blocking session\n");
17457996a778SMike Christie 			iscsi_block_session(session_to_cls(session));
17467996a778SMike Christie 		}
174767a61114SMike Christie 	}
1748656cffc9SMike Christie 
1749656cffc9SMike Christie 	/*
1750656cffc9SMike Christie 	 * flush queues.
1751656cffc9SMike Christie 	 */
1752656cffc9SMike Christie 	spin_lock_bh(&session->lock);
1753656cffc9SMike Christie 	fail_all_commands(conn);
1754656cffc9SMike Christie 	flush_control_queues(session, conn);
1755656cffc9SMike Christie 	spin_unlock_bh(&session->lock);
1756656cffc9SMike Christie 
17577996a778SMike Christie 	mutex_unlock(&conn->xmitmutex);
17587996a778SMike Christie }
17597996a778SMike Christie 
17607996a778SMike Christie void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
17617996a778SMike Christie {
17627996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
17637996a778SMike Christie 	struct iscsi_session *session = conn->session;
17647996a778SMike Christie 
17657996a778SMike Christie 	switch (flag) {
17667996a778SMike Christie 	case STOP_CONN_RECOVER:
17677996a778SMike Christie 	case STOP_CONN_TERM:
17687996a778SMike Christie 		iscsi_start_session_recovery(session, conn, flag);
17698d2860b3SMike Christie 		break;
17707996a778SMike Christie 	default:
1771be2df72eSOr Gerlitz 		printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
17727996a778SMike Christie 	}
17737996a778SMike Christie }
17747996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_stop);
17757996a778SMike Christie 
17767996a778SMike Christie int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
17777996a778SMike Christie 		    struct iscsi_cls_conn *cls_conn, int is_leading)
17787996a778SMike Christie {
17797996a778SMike Christie 	struct iscsi_session *session = class_to_transport_session(cls_session);
17807996a778SMike Christie 	struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data;
17817996a778SMike Christie 
17827996a778SMike Christie 	/* lookup for existing connection */
17837996a778SMike Christie 	spin_lock_bh(&session->lock);
17847996a778SMike Christie 	list_for_each_entry(tmp, &session->connections, item) {
17857996a778SMike Christie 		if (tmp == conn) {
17867996a778SMike Christie 			if (conn->c_stage != ISCSI_CONN_STOPPED ||
17877996a778SMike Christie 			    conn->stop_stage == STOP_CONN_TERM) {
1788be2df72eSOr Gerlitz 				printk(KERN_ERR "iscsi: can't bind "
17897996a778SMike Christie 				       "non-stopped connection (%d:%d)\n",
17907996a778SMike Christie 				       conn->c_stage, conn->stop_stage);
17917996a778SMike Christie 				spin_unlock_bh(&session->lock);
17927996a778SMike Christie 				return -EIO;
17937996a778SMike Christie 			}
17947996a778SMike Christie 			break;
17957996a778SMike Christie 		}
17967996a778SMike Christie 	}
17977996a778SMike Christie 	if (tmp != conn) {
17987996a778SMike Christie 		/* bind new iSCSI connection to session */
17997996a778SMike Christie 		conn->session = session;
18007996a778SMike Christie 		list_add(&conn->item, &session->connections);
18017996a778SMike Christie 	}
18027996a778SMike Christie 	spin_unlock_bh(&session->lock);
18037996a778SMike Christie 
18047996a778SMike Christie 	if (is_leading)
18057996a778SMike Christie 		session->leadconn = conn;
18067996a778SMike Christie 
18077996a778SMike Christie 	/*
18087996a778SMike Christie 	 * Unblock xmitworker(), Login Phase will pass through.
18097996a778SMike Christie 	 */
18107996a778SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
18117996a778SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
18127996a778SMike Christie 	return 0;
18137996a778SMike Christie }
18147996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_bind);
18157996a778SMike Christie 
1816a54a52caSMike Christie 
1817a54a52caSMike Christie int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1818a54a52caSMike Christie 		    enum iscsi_param param, char *buf, int buflen)
1819a54a52caSMike Christie {
1820a54a52caSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
1821a54a52caSMike Christie 	struct iscsi_session *session = conn->session;
1822a54a52caSMike Christie 	uint32_t value;
1823a54a52caSMike Christie 
1824a54a52caSMike Christie 	switch(param) {
1825a54a52caSMike Christie 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
1826a54a52caSMike Christie 		sscanf(buf, "%d", &conn->max_recv_dlength);
1827a54a52caSMike Christie 		break;
1828a54a52caSMike Christie 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1829a54a52caSMike Christie 		sscanf(buf, "%d", &conn->max_xmit_dlength);
1830a54a52caSMike Christie 		break;
1831a54a52caSMike Christie 	case ISCSI_PARAM_HDRDGST_EN:
1832a54a52caSMike Christie 		sscanf(buf, "%d", &conn->hdrdgst_en);
1833a54a52caSMike Christie 		break;
1834a54a52caSMike Christie 	case ISCSI_PARAM_DATADGST_EN:
1835a54a52caSMike Christie 		sscanf(buf, "%d", &conn->datadgst_en);
1836a54a52caSMike Christie 		break;
1837a54a52caSMike Christie 	case ISCSI_PARAM_INITIAL_R2T_EN:
1838a54a52caSMike Christie 		sscanf(buf, "%d", &session->initial_r2t_en);
1839a54a52caSMike Christie 		break;
1840a54a52caSMike Christie 	case ISCSI_PARAM_MAX_R2T:
1841a54a52caSMike Christie 		sscanf(buf, "%d", &session->max_r2t);
1842a54a52caSMike Christie 		break;
1843a54a52caSMike Christie 	case ISCSI_PARAM_IMM_DATA_EN:
1844a54a52caSMike Christie 		sscanf(buf, "%d", &session->imm_data_en);
1845a54a52caSMike Christie 		break;
1846a54a52caSMike Christie 	case ISCSI_PARAM_FIRST_BURST:
1847a54a52caSMike Christie 		sscanf(buf, "%d", &session->first_burst);
1848a54a52caSMike Christie 		break;
1849a54a52caSMike Christie 	case ISCSI_PARAM_MAX_BURST:
1850a54a52caSMike Christie 		sscanf(buf, "%d", &session->max_burst);
1851a54a52caSMike Christie 		break;
1852a54a52caSMike Christie 	case ISCSI_PARAM_PDU_INORDER_EN:
1853a54a52caSMike Christie 		sscanf(buf, "%d", &session->pdu_inorder_en);
1854a54a52caSMike Christie 		break;
1855a54a52caSMike Christie 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
1856a54a52caSMike Christie 		sscanf(buf, "%d", &session->dataseq_inorder_en);
1857a54a52caSMike Christie 		break;
1858a54a52caSMike Christie 	case ISCSI_PARAM_ERL:
1859a54a52caSMike Christie 		sscanf(buf, "%d", &session->erl);
1860a54a52caSMike Christie 		break;
1861a54a52caSMike Christie 	case ISCSI_PARAM_IFMARKER_EN:
1862a54a52caSMike Christie 		sscanf(buf, "%d", &value);
1863a54a52caSMike Christie 		BUG_ON(value);
1864a54a52caSMike Christie 		break;
1865a54a52caSMike Christie 	case ISCSI_PARAM_OFMARKER_EN:
1866a54a52caSMike Christie 		sscanf(buf, "%d", &value);
1867a54a52caSMike Christie 		BUG_ON(value);
1868a54a52caSMike Christie 		break;
1869a54a52caSMike Christie 	case ISCSI_PARAM_EXP_STATSN:
1870a54a52caSMike Christie 		sscanf(buf, "%u", &conn->exp_statsn);
1871a54a52caSMike Christie 		break;
1872a54a52caSMike Christie 	case ISCSI_PARAM_TARGET_NAME:
1873a54a52caSMike Christie 		/* this should not change between logins */
1874a54a52caSMike Christie 		if (session->targetname)
1875a54a52caSMike Christie 			break;
1876a54a52caSMike Christie 
1877a54a52caSMike Christie 		session->targetname = kstrdup(buf, GFP_KERNEL);
1878a54a52caSMike Christie 		if (!session->targetname)
1879a54a52caSMike Christie 			return -ENOMEM;
1880a54a52caSMike Christie 		break;
1881a54a52caSMike Christie 	case ISCSI_PARAM_TPGT:
1882a54a52caSMike Christie 		sscanf(buf, "%d", &session->tpgt);
1883a54a52caSMike Christie 		break;
1884a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_PORT:
1885a54a52caSMike Christie 		sscanf(buf, "%d", &conn->persistent_port);
1886a54a52caSMike Christie 		break;
1887a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
1888a54a52caSMike Christie 		/*
1889a54a52caSMike Christie 		 * this is the address returned in discovery so it should
1890a54a52caSMike Christie 		 * not change between logins.
1891a54a52caSMike Christie 		 */
1892a54a52caSMike Christie 		if (conn->persistent_address)
1893a54a52caSMike Christie 			break;
1894a54a52caSMike Christie 
1895a54a52caSMike Christie 		conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1896a54a52caSMike Christie 		if (!conn->persistent_address)
1897a54a52caSMike Christie 			return -ENOMEM;
1898a54a52caSMike Christie 		break;
1899a54a52caSMike Christie 	default:
1900a54a52caSMike Christie 		return -ENOSYS;
1901a54a52caSMike Christie 	}
1902a54a52caSMike Christie 
1903a54a52caSMike Christie 	return 0;
1904a54a52caSMike Christie }
1905a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_set_param);
1906a54a52caSMike Christie 
1907a54a52caSMike Christie int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1908a54a52caSMike Christie 			    enum iscsi_param param, char *buf)
1909a54a52caSMike Christie {
1910a54a52caSMike Christie 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1911a54a52caSMike Christie 	struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1912a54a52caSMike Christie 	int len;
1913a54a52caSMike Christie 
1914a54a52caSMike Christie 	switch(param) {
1915a54a52caSMike Christie 	case ISCSI_PARAM_INITIAL_R2T_EN:
1916a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->initial_r2t_en);
1917a54a52caSMike Christie 		break;
1918a54a52caSMike Christie 	case ISCSI_PARAM_MAX_R2T:
1919a54a52caSMike Christie 		len = sprintf(buf, "%hu\n", session->max_r2t);
1920a54a52caSMike Christie 		break;
1921a54a52caSMike Christie 	case ISCSI_PARAM_IMM_DATA_EN:
1922a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->imm_data_en);
1923a54a52caSMike Christie 		break;
1924a54a52caSMike Christie 	case ISCSI_PARAM_FIRST_BURST:
1925a54a52caSMike Christie 		len = sprintf(buf, "%u\n", session->first_burst);
1926a54a52caSMike Christie 		break;
1927a54a52caSMike Christie 	case ISCSI_PARAM_MAX_BURST:
1928a54a52caSMike Christie 		len = sprintf(buf, "%u\n", session->max_burst);
1929a54a52caSMike Christie 		break;
1930a54a52caSMike Christie 	case ISCSI_PARAM_PDU_INORDER_EN:
1931a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->pdu_inorder_en);
1932a54a52caSMike Christie 		break;
1933a54a52caSMike Christie 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
1934a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
1935a54a52caSMike Christie 		break;
1936a54a52caSMike Christie 	case ISCSI_PARAM_ERL:
1937a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->erl);
1938a54a52caSMike Christie 		break;
1939a54a52caSMike Christie 	case ISCSI_PARAM_TARGET_NAME:
1940a54a52caSMike Christie 		len = sprintf(buf, "%s\n", session->targetname);
1941a54a52caSMike Christie 		break;
1942a54a52caSMike Christie 	case ISCSI_PARAM_TPGT:
1943a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->tpgt);
1944a54a52caSMike Christie 		break;
1945a54a52caSMike Christie 	default:
1946a54a52caSMike Christie 		return -ENOSYS;
1947a54a52caSMike Christie 	}
1948a54a52caSMike Christie 
1949a54a52caSMike Christie 	return len;
1950a54a52caSMike Christie }
1951a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_get_param);
1952a54a52caSMike Christie 
1953a54a52caSMike Christie int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
1954a54a52caSMike Christie 			 enum iscsi_param param, char *buf)
1955a54a52caSMike Christie {
1956a54a52caSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
1957a54a52caSMike Christie 	int len;
1958a54a52caSMike Christie 
1959a54a52caSMike Christie 	switch(param) {
1960a54a52caSMike Christie 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
1961a54a52caSMike Christie 		len = sprintf(buf, "%u\n", conn->max_recv_dlength);
1962a54a52caSMike Christie 		break;
1963a54a52caSMike Christie 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1964a54a52caSMike Christie 		len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
1965a54a52caSMike Christie 		break;
1966a54a52caSMike Christie 	case ISCSI_PARAM_HDRDGST_EN:
1967a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->hdrdgst_en);
1968a54a52caSMike Christie 		break;
1969a54a52caSMike Christie 	case ISCSI_PARAM_DATADGST_EN:
1970a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->datadgst_en);
1971a54a52caSMike Christie 		break;
1972a54a52caSMike Christie 	case ISCSI_PARAM_IFMARKER_EN:
1973a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->ifmarker_en);
1974a54a52caSMike Christie 		break;
1975a54a52caSMike Christie 	case ISCSI_PARAM_OFMARKER_EN:
1976a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->ofmarker_en);
1977a54a52caSMike Christie 		break;
1978a54a52caSMike Christie 	case ISCSI_PARAM_EXP_STATSN:
1979a54a52caSMike Christie 		len = sprintf(buf, "%u\n", conn->exp_statsn);
1980a54a52caSMike Christie 		break;
1981a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_PORT:
1982a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->persistent_port);
1983a54a52caSMike Christie 		break;
1984a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
1985a54a52caSMike Christie 		len = sprintf(buf, "%s\n", conn->persistent_address);
1986a54a52caSMike Christie 		break;
1987a54a52caSMike Christie 	default:
1988a54a52caSMike Christie 		return -ENOSYS;
1989a54a52caSMike Christie 	}
1990a54a52caSMike Christie 
1991a54a52caSMike Christie 	return len;
1992a54a52caSMike Christie }
1993a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
1994a54a52caSMike Christie 
19957996a778SMike Christie MODULE_AUTHOR("Mike Christie");
19967996a778SMike Christie MODULE_DESCRIPTION("iSCSI library functions");
19977996a778SMike Christie MODULE_LICENSE("GPL");
1998