xref: /openbmc/linux/drivers/scsi/libiscsi.c (revision c8dc1e52)
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,
717996a778SMike Christie 				   struct iscsi_data *hdr,
727996a778SMike Christie 				   int transport_data_cnt)
737996a778SMike Christie {
747996a778SMike Christie 	struct iscsi_conn *conn = ctask->conn;
757996a778SMike Christie 
767996a778SMike Christie 	memset(hdr, 0, sizeof(struct iscsi_data));
777996a778SMike Christie 	hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
787996a778SMike Christie 	hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
797996a778SMike Christie 	ctask->unsol_datasn++;
807996a778SMike Christie 	hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
817996a778SMike Christie 	memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
827996a778SMike Christie 
837996a778SMike Christie 	hdr->itt = ctask->hdr->itt;
847996a778SMike Christie 	hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
857996a778SMike Christie 
867996a778SMike Christie 	hdr->offset = cpu_to_be32(ctask->total_length -
877996a778SMike Christie 				  transport_data_cnt -
887996a778SMike Christie 				  ctask->unsol_count);
897996a778SMike Christie 
907996a778SMike Christie 	if (ctask->unsol_count > conn->max_xmit_dlength) {
917996a778SMike Christie 		hton24(hdr->dlength, conn->max_xmit_dlength);
927996a778SMike Christie 		ctask->data_count = conn->max_xmit_dlength;
937996a778SMike Christie 		hdr->flags = 0;
947996a778SMike Christie 	} else {
957996a778SMike Christie 		hton24(hdr->dlength, ctask->unsol_count);
967996a778SMike Christie 		ctask->data_count = ctask->unsol_count;
977996a778SMike Christie 		hdr->flags = ISCSI_FLAG_CMD_FINAL;
987996a778SMike Christie 	}
997996a778SMike Christie }
1007996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
1017996a778SMike Christie 
1027996a778SMike Christie /**
1037996a778SMike Christie  * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
1047996a778SMike Christie  * @ctask: iscsi cmd task
1057996a778SMike Christie  *
1067996a778SMike Christie  * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
1077996a778SMike Christie  * fields like dlength or final based on how much data it sends
1087996a778SMike Christie  */
1097996a778SMike Christie static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
1107996a778SMike Christie {
1117996a778SMike Christie 	struct iscsi_conn *conn = ctask->conn;
1127996a778SMike Christie 	struct iscsi_session *session = conn->session;
1137996a778SMike Christie 	struct iscsi_cmd *hdr = ctask->hdr;
1147996a778SMike Christie 	struct scsi_cmnd *sc = ctask->sc;
1157996a778SMike Christie 
1167996a778SMike Christie         hdr->opcode = ISCSI_OP_SCSI_CMD;
1177996a778SMike Christie         hdr->flags = ISCSI_ATTR_SIMPLE;
1187996a778SMike Christie         int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
1197996a778SMike Christie         hdr->itt = ctask->itt | (conn->id << ISCSI_CID_SHIFT) |
1207996a778SMike Christie                          (session->age << ISCSI_AGE_SHIFT);
1217996a778SMike Christie         hdr->data_length = cpu_to_be32(sc->request_bufflen);
1227996a778SMike Christie         hdr->cmdsn = cpu_to_be32(session->cmdsn);
1237996a778SMike Christie         session->cmdsn++;
1247996a778SMike Christie         hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
1257996a778SMike Christie         memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
1267996a778SMike Christie         memset(&hdr->cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
1277996a778SMike Christie 
1287996a778SMike Christie 	if (sc->sc_data_direction == DMA_TO_DEVICE) {
1297996a778SMike Christie 		hdr->flags |= ISCSI_FLAG_CMD_WRITE;
1307996a778SMike Christie 		/*
1317996a778SMike Christie 		 * Write counters:
1327996a778SMike Christie 		 *
1337996a778SMike Christie 		 *	imm_count	bytes to be sent right after
1347996a778SMike Christie 		 *			SCSI PDU Header
1357996a778SMike Christie 		 *
1367996a778SMike Christie 		 *	unsol_count	bytes(as Data-Out) to be sent
1377996a778SMike Christie 		 *			without	R2T ack right after
1387996a778SMike Christie 		 *			immediate data
1397996a778SMike Christie 		 *
1407996a778SMike Christie 		 *	r2t_data_count	bytes to be sent via R2T ack's
1417996a778SMike Christie 		 *
1427996a778SMike Christie 		 *      pad_count       bytes to be sent as zero-padding
1437996a778SMike Christie 		 */
1447996a778SMike Christie 		ctask->imm_count = 0;
1457996a778SMike Christie 		ctask->unsol_count = 0;
1467996a778SMike Christie 		ctask->unsol_datasn = 0;
1477996a778SMike Christie 
1487996a778SMike Christie 		if (session->imm_data_en) {
1497996a778SMike Christie 			if (ctask->total_length >= session->first_burst)
1507996a778SMike Christie 				ctask->imm_count = min(session->first_burst,
1517996a778SMike Christie 							conn->max_xmit_dlength);
1527996a778SMike Christie 			else
1537996a778SMike Christie 				ctask->imm_count = min(ctask->total_length,
1547996a778SMike Christie 							conn->max_xmit_dlength);
1557996a778SMike Christie 			hton24(ctask->hdr->dlength, ctask->imm_count);
1567996a778SMike Christie 		} else
1577996a778SMike Christie 			zero_data(ctask->hdr->dlength);
1587996a778SMike Christie 
1597996a778SMike Christie 		if (!session->initial_r2t_en)
1607996a778SMike Christie 			ctask->unsol_count = min(session->first_burst,
1617996a778SMike Christie 				ctask->total_length) - ctask->imm_count;
1627996a778SMike Christie 		if (!ctask->unsol_count)
1637996a778SMike Christie 			/* No unsolicit Data-Out's */
1647996a778SMike Christie 			ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1657996a778SMike Christie 	} else {
1667996a778SMike Christie 		ctask->datasn = 0;
1677996a778SMike Christie 		hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1687996a778SMike Christie 		zero_data(hdr->dlength);
1697996a778SMike Christie 
1707996a778SMike Christie 		if (sc->sc_data_direction == DMA_FROM_DEVICE)
1717996a778SMike Christie 			hdr->flags |= ISCSI_FLAG_CMD_READ;
1727996a778SMike Christie 	}
1737996a778SMike Christie 
1747996a778SMike Christie 	conn->scsicmd_pdus_cnt++;
1757996a778SMike Christie }
1767996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu);
1777996a778SMike Christie 
1787996a778SMike Christie /**
1797996a778SMike Christie  * iscsi_complete_command - return command back to scsi-ml
1807996a778SMike Christie  * @session: iscsi session
1817996a778SMike Christie  * @ctask: iscsi cmd task
1827996a778SMike Christie  *
1837996a778SMike Christie  * Must be called with session lock.
1847996a778SMike Christie  * This function returns the scsi command to scsi-ml and returns
1857996a778SMike Christie  * the cmd task to the pool of available cmd tasks.
1867996a778SMike Christie  */
1877996a778SMike Christie static void iscsi_complete_command(struct iscsi_session *session,
1887996a778SMike Christie 				   struct iscsi_cmd_task *ctask)
1897996a778SMike Christie {
1907996a778SMike Christie 	struct scsi_cmnd *sc = ctask->sc;
1917996a778SMike Christie 
192b6c395edSMike Christie 	ctask->state = ISCSI_TASK_COMPLETED;
1937996a778SMike Christie 	ctask->sc = NULL;
1947996a778SMike Christie 	list_del_init(&ctask->running);
1957996a778SMike Christie 	__kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
1967996a778SMike Christie 	sc->scsi_done(sc);
1977996a778SMike Christie }
1987996a778SMike Christie 
1997996a778SMike Christie /**
2007996a778SMike Christie  * iscsi_cmd_rsp - SCSI Command Response processing
2017996a778SMike Christie  * @conn: iscsi connection
2027996a778SMike Christie  * @hdr: iscsi header
2037996a778SMike Christie  * @ctask: scsi command task
2047996a778SMike Christie  * @data: cmd data buffer
2057996a778SMike Christie  * @datalen: len of buffer
2067996a778SMike Christie  *
2077996a778SMike Christie  * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
2087996a778SMike Christie  * then completes the command and task.
2097996a778SMike Christie  **/
2107996a778SMike Christie static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
2117996a778SMike Christie 			      struct iscsi_cmd_task *ctask, char *data,
2127996a778SMike Christie 			      int datalen)
2137996a778SMike Christie {
2147996a778SMike Christie 	int rc;
2157996a778SMike Christie 	struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
2167996a778SMike Christie 	struct iscsi_session *session = conn->session;
2177996a778SMike Christie 	struct scsi_cmnd *sc = ctask->sc;
2187996a778SMike Christie 
2197996a778SMike Christie 	rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
2207996a778SMike Christie 	if (rc) {
2217996a778SMike Christie 		sc->result = DID_ERROR << 16;
2227996a778SMike Christie 		goto out;
2237996a778SMike Christie 	}
2247996a778SMike Christie 
2257996a778SMike Christie 	conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
2267996a778SMike Christie 
2277996a778SMike Christie 	sc->result = (DID_OK << 16) | rhdr->cmd_status;
2287996a778SMike Christie 
2297996a778SMike Christie 	if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
2307996a778SMike Christie 		sc->result = DID_ERROR << 16;
2317996a778SMike Christie 		goto out;
2327996a778SMike Christie 	}
2337996a778SMike Christie 
2347996a778SMike Christie 	if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
2357996a778SMike Christie 		int senselen;
2367996a778SMike Christie 
2377996a778SMike Christie 		if (datalen < 2) {
2387996a778SMike Christie invalid_datalen:
239be2df72eSOr Gerlitz 			printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
240be2df72eSOr Gerlitz 			       "invalid data buffer size of %d\n", datalen);
2417996a778SMike Christie 			sc->result = DID_BAD_TARGET << 16;
2427996a778SMike Christie 			goto out;
2437996a778SMike Christie 		}
2447996a778SMike Christie 
2457996a778SMike Christie 		senselen = (data[0] << 8) | data[1];
2467996a778SMike Christie 		if (datalen < senselen)
2477996a778SMike Christie 			goto invalid_datalen;
2487996a778SMike Christie 
2497996a778SMike Christie 		memcpy(sc->sense_buffer, data + 2,
2507996a778SMike Christie 		       min(senselen, SCSI_SENSE_BUFFERSIZE));
2517996a778SMike Christie 		debug_scsi("copied %d bytes of sense\n",
2527996a778SMike Christie 			   min(senselen, SCSI_SENSE_BUFFERSIZE));
2537996a778SMike Christie 	}
2547996a778SMike Christie 
2557996a778SMike Christie 	if (sc->sc_data_direction == DMA_TO_DEVICE)
2567996a778SMike Christie 		goto out;
2577996a778SMike Christie 
2587996a778SMike Christie 	if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
2597996a778SMike Christie 		int res_count = be32_to_cpu(rhdr->residual_count);
2607996a778SMike Christie 
2617996a778SMike Christie 		if (res_count > 0 && res_count <= sc->request_bufflen)
2627996a778SMike Christie 			sc->resid = res_count;
2637996a778SMike Christie 		else
2647996a778SMike Christie 			sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
2657996a778SMike Christie 	} else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
2667996a778SMike Christie 		sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
2677996a778SMike Christie 	else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
2687996a778SMike Christie 		sc->resid = be32_to_cpu(rhdr->residual_count);
2697996a778SMike Christie 
2707996a778SMike Christie out:
2717996a778SMike Christie 	debug_scsi("done [sc %lx res %d itt 0x%x]\n",
2727996a778SMike Christie 		   (long)sc, sc->result, ctask->itt);
2737996a778SMike Christie 	conn->scsirsp_pdus_cnt++;
2747996a778SMike Christie 
2757996a778SMike Christie 	iscsi_complete_command(conn->session, ctask);
2767996a778SMike Christie 	return rc;
2777996a778SMike Christie }
2787996a778SMike Christie 
2797ea8b828SMike Christie static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
2807ea8b828SMike Christie {
2817ea8b828SMike Christie 	struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
2827ea8b828SMike Christie 
2837ea8b828SMike Christie 	conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
2847ea8b828SMike Christie 	conn->tmfrsp_pdus_cnt++;
2857ea8b828SMike Christie 
2867ea8b828SMike Christie 	if (conn->tmabort_state != TMABORT_INITIAL)
2877ea8b828SMike Christie 		return;
2887ea8b828SMike Christie 
2897ea8b828SMike Christie 	if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
2907ea8b828SMike Christie 		conn->tmabort_state = TMABORT_SUCCESS;
2917ea8b828SMike Christie 	else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
2927ea8b828SMike Christie 		conn->tmabort_state = TMABORT_NOT_FOUND;
2937ea8b828SMike Christie 	else
2947ea8b828SMike Christie 		conn->tmabort_state = TMABORT_FAILED;
2957ea8b828SMike Christie 	wake_up(&conn->ehwait);
2967ea8b828SMike Christie }
2977ea8b828SMike Christie 
2987996a778SMike Christie /**
2997996a778SMike Christie  * __iscsi_complete_pdu - complete pdu
3007996a778SMike Christie  * @conn: iscsi conn
3017996a778SMike Christie  * @hdr: iscsi header
3027996a778SMike Christie  * @data: data buffer
3037996a778SMike Christie  * @datalen: len of data buffer
3047996a778SMike Christie  *
3057996a778SMike Christie  * Completes pdu processing by freeing any resources allocated at
3067996a778SMike Christie  * queuecommand or send generic. session lock must be held and verify
3077996a778SMike Christie  * itt must have been called.
3087996a778SMike Christie  */
3097996a778SMike Christie int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
3107996a778SMike Christie 			 char *data, int datalen)
3117996a778SMike Christie {
3127996a778SMike Christie 	struct iscsi_session *session = conn->session;
3137996a778SMike Christie 	int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
3147996a778SMike Christie 	struct iscsi_cmd_task *ctask;
3157996a778SMike Christie 	struct iscsi_mgmt_task *mtask;
3167996a778SMike Christie 	uint32_t itt;
3177996a778SMike Christie 
3187996a778SMike Christie 	if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
3197996a778SMike Christie 		itt = hdr->itt & ISCSI_ITT_MASK;
3207996a778SMike Christie 	else
3217996a778SMike Christie 		itt = hdr->itt;
3227996a778SMike Christie 
3237996a778SMike Christie 	if (itt < session->cmds_max) {
3247996a778SMike Christie 		ctask = session->cmds[itt];
3257996a778SMike Christie 
3267996a778SMike Christie 		debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
3277996a778SMike Christie 			   opcode, conn->id, ctask->itt, datalen);
3287996a778SMike Christie 
3297996a778SMike Christie 		switch(opcode) {
3307996a778SMike Christie 		case ISCSI_OP_SCSI_CMD_RSP:
3317996a778SMike Christie 			BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
3327996a778SMike Christie 			rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
3337996a778SMike Christie 						datalen);
3347996a778SMike Christie 			break;
3357996a778SMike Christie 		case ISCSI_OP_SCSI_DATA_IN:
3367996a778SMike Christie 			BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
3377996a778SMike Christie 			if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
3387996a778SMike Christie 				conn->scsirsp_pdus_cnt++;
3397996a778SMike Christie 				iscsi_complete_command(session, ctask);
3407996a778SMike Christie 			}
3417996a778SMike Christie 			break;
3427996a778SMike Christie 		case ISCSI_OP_R2T:
3437996a778SMike Christie 			/* LLD handles this for now */
3447996a778SMike Christie 			break;
3457996a778SMike Christie 		default:
3467996a778SMike Christie 			rc = ISCSI_ERR_BAD_OPCODE;
3477996a778SMike Christie 			break;
3487996a778SMike Christie 		}
3497996a778SMike Christie 	} else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
3507996a778SMike Christie 		   itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
3517996a778SMike Christie 		mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
3527996a778SMike Christie 
3537996a778SMike Christie 		debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
3547996a778SMike Christie 			   opcode, conn->id, mtask->itt, datalen);
3557996a778SMike Christie 
3567996a778SMike Christie 		rc = iscsi_check_assign_cmdsn(session,
3577996a778SMike Christie 					      (struct iscsi_nopin*)hdr);
3587996a778SMike Christie 		if (rc)
3598d2860b3SMike Christie 			goto done;
3607996a778SMike Christie 
3618d2860b3SMike Christie 		switch(opcode) {
3628d2860b3SMike Christie 		case ISCSI_OP_LOGOUT_RSP:
363c8dc1e52SMike Christie 			if (datalen) {
364c8dc1e52SMike Christie 				rc = ISCSI_ERR_PROTO;
365c8dc1e52SMike Christie 				break;
366c8dc1e52SMike Christie 			}
3678d2860b3SMike Christie 			conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
3688d2860b3SMike Christie 			/* fall through */
3698d2860b3SMike Christie 		case ISCSI_OP_LOGIN_RSP:
3708d2860b3SMike Christie 		case ISCSI_OP_TEXT_RSP:
3718d2860b3SMike Christie 			/*
3728d2860b3SMike Christie 			 * login related PDU's exp_statsn is handled in
3738d2860b3SMike Christie 			 * userspace
3748d2860b3SMike Christie 			 */
3757996a778SMike Christie 			rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen);
3767996a778SMike Christie 			list_del(&mtask->running);
3777996a778SMike Christie 			if (conn->login_mtask != mtask)
3787996a778SMike Christie 				__kfifo_put(session->mgmtpool.queue,
3797996a778SMike Christie 					    (void*)&mtask, sizeof(void*));
3807996a778SMike Christie 			break;
3817996a778SMike Christie 		case ISCSI_OP_SCSI_TMFUNC_RSP:
3827996a778SMike Christie 			if (datalen) {
3837996a778SMike Christie 				rc = ISCSI_ERR_PROTO;
3847996a778SMike Christie 				break;
3857996a778SMike Christie 			}
3868d2860b3SMike Christie 
3877ea8b828SMike Christie 			iscsi_tmf_rsp(conn, hdr);
3887996a778SMike Christie 			break;
3897996a778SMike Christie 		case ISCSI_OP_NOOP_IN:
390c8dc1e52SMike Christie 			if (hdr->ttt != ISCSI_RESERVED_TAG || datalen) {
3917996a778SMike Christie 				rc = ISCSI_ERR_PROTO;
3927996a778SMike Christie 				break;
3937996a778SMike Christie 			}
3947996a778SMike Christie 			conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
3957996a778SMike Christie 
3967996a778SMike Christie 			rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen);
3977996a778SMike Christie 			list_del(&mtask->running);
3987996a778SMike Christie 			if (conn->login_mtask != mtask)
3997996a778SMike Christie 				__kfifo_put(session->mgmtpool.queue,
4007996a778SMike Christie 					    (void*)&mtask, sizeof(void*));
4017996a778SMike Christie 			break;
4027996a778SMike Christie 		default:
4037996a778SMike Christie 			rc = ISCSI_ERR_BAD_OPCODE;
4047996a778SMike Christie 			break;
4057996a778SMike Christie 		}
4067996a778SMike Christie 	} else if (itt == ISCSI_RESERVED_TAG) {
4077996a778SMike Christie 		switch(opcode) {
4087996a778SMike Christie 		case ISCSI_OP_NOOP_IN:
4097996a778SMike Christie 			if (!datalen) {
4107996a778SMike Christie 				rc = iscsi_check_assign_cmdsn(session,
4117996a778SMike Christie 						 (struct iscsi_nopin*)hdr);
4127996a778SMike Christie 				if (!rc && hdr->ttt != ISCSI_RESERVED_TAG)
4137996a778SMike Christie 					rc = iscsi_recv_pdu(conn->cls_conn,
4147996a778SMike Christie 							    hdr, NULL, 0);
4157996a778SMike Christie 			} else
4167996a778SMike Christie 				rc = ISCSI_ERR_PROTO;
4177996a778SMike Christie 			break;
4187996a778SMike Christie 		case ISCSI_OP_REJECT:
4197996a778SMike Christie 			/* we need sth like iscsi_reject_rsp()*/
4207996a778SMike Christie 		case ISCSI_OP_ASYNC_EVENT:
4218d2860b3SMike Christie 			conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
4227996a778SMike Christie 			/* we need sth like iscsi_async_event_rsp() */
4237996a778SMike Christie 			rc = ISCSI_ERR_BAD_OPCODE;
4247996a778SMike Christie 			break;
4257996a778SMike Christie 		default:
4267996a778SMike Christie 			rc = ISCSI_ERR_BAD_OPCODE;
4277996a778SMike Christie 			break;
4287996a778SMike Christie 		}
4297996a778SMike Christie 	} else
4307996a778SMike Christie 		rc = ISCSI_ERR_BAD_ITT;
4317996a778SMike Christie 
4328d2860b3SMike Christie done:
4337996a778SMike Christie 	return rc;
4347996a778SMike Christie }
4357996a778SMike Christie EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
4367996a778SMike Christie 
4377996a778SMike Christie int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
4387996a778SMike Christie 		       char *data, int datalen)
4397996a778SMike Christie {
4407996a778SMike Christie 	int rc;
4417996a778SMike Christie 
4427996a778SMike Christie 	spin_lock(&conn->session->lock);
4437996a778SMike Christie 	rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
4447996a778SMike Christie 	spin_unlock(&conn->session->lock);
4457996a778SMike Christie 	return rc;
4467996a778SMike Christie }
4477996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
4487996a778SMike Christie 
4497996a778SMike Christie /* verify itt (itt encoding: age+cid+itt) */
4507996a778SMike Christie int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
4517996a778SMike Christie 		     uint32_t *ret_itt)
4527996a778SMike Christie {
4537996a778SMike Christie 	struct iscsi_session *session = conn->session;
4547996a778SMike Christie 	struct iscsi_cmd_task *ctask;
4557996a778SMike Christie 	uint32_t itt;
4567996a778SMike Christie 
4577996a778SMike Christie 	if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
4587996a778SMike Christie 		if ((hdr->itt & ISCSI_AGE_MASK) !=
4597996a778SMike Christie 		    (session->age << ISCSI_AGE_SHIFT)) {
460be2df72eSOr Gerlitz 			printk(KERN_ERR "iscsi: received itt %x expected "
4617996a778SMike Christie 				"session age (%x)\n", hdr->itt,
4627996a778SMike Christie 				session->age & ISCSI_AGE_MASK);
4637996a778SMike Christie 			return ISCSI_ERR_BAD_ITT;
4647996a778SMike Christie 		}
4657996a778SMike Christie 
4667996a778SMike Christie 		if ((hdr->itt & ISCSI_CID_MASK) !=
4677996a778SMike Christie 		    (conn->id << ISCSI_CID_SHIFT)) {
468be2df72eSOr Gerlitz 			printk(KERN_ERR "iscsi: received itt %x, expected "
4697996a778SMike Christie 				"CID (%x)\n", hdr->itt, conn->id);
4707996a778SMike Christie 			return ISCSI_ERR_BAD_ITT;
4717996a778SMike Christie 		}
4727996a778SMike Christie 		itt = hdr->itt & ISCSI_ITT_MASK;
4737996a778SMike Christie 	} else
4747996a778SMike Christie 		itt = hdr->itt;
4757996a778SMike Christie 
4767996a778SMike Christie 	if (itt < session->cmds_max) {
4777996a778SMike Christie 		ctask = session->cmds[itt];
4787996a778SMike Christie 
4797996a778SMike Christie 		if (!ctask->sc) {
480be2df72eSOr Gerlitz 			printk(KERN_INFO "iscsi: dropping ctask with "
4817996a778SMike Christie 			       "itt 0x%x\n", ctask->itt);
4827996a778SMike Christie 			/* force drop */
4837996a778SMike Christie 			return ISCSI_ERR_NO_SCSI_CMD;
4847996a778SMike Christie 		}
4857996a778SMike Christie 
4867996a778SMike Christie 		if (ctask->sc->SCp.phase != session->age) {
487be2df72eSOr Gerlitz 			printk(KERN_ERR "iscsi: ctask's session age %d, "
4887996a778SMike Christie 				"expected %d\n", ctask->sc->SCp.phase,
4897996a778SMike Christie 				session->age);
4907996a778SMike Christie 			return ISCSI_ERR_SESSION_FAILED;
4917996a778SMike Christie 		}
4927996a778SMike Christie 	}
4937996a778SMike Christie 
4947996a778SMike Christie 	*ret_itt = itt;
4957996a778SMike Christie 	return 0;
4967996a778SMike Christie }
4977996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_verify_itt);
4987996a778SMike Christie 
4997996a778SMike Christie void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
5007996a778SMike Christie {
5017996a778SMike Christie 	struct iscsi_session *session = conn->session;
5027996a778SMike Christie 	unsigned long flags;
5037996a778SMike Christie 
5047996a778SMike Christie 	spin_lock_irqsave(&session->lock, flags);
505656cffc9SMike Christie 	if (session->state == ISCSI_STATE_FAILED) {
506656cffc9SMike Christie 		spin_unlock_irqrestore(&session->lock, flags);
507656cffc9SMike Christie 		return;
508656cffc9SMike Christie 	}
509656cffc9SMike Christie 
51067a61114SMike Christie 	if (conn->stop_stage == 0)
5117996a778SMike Christie 		session->state = ISCSI_STATE_FAILED;
5127996a778SMike Christie 	spin_unlock_irqrestore(&session->lock, flags);
5137996a778SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
5147996a778SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
5157996a778SMike Christie 	iscsi_conn_error(conn->cls_conn, err);
5167996a778SMike Christie }
5177996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_failure);
5187996a778SMike Christie 
5197996a778SMike Christie /**
5207996a778SMike Christie  * iscsi_data_xmit - xmit any command into the scheduled connection
5217996a778SMike Christie  * @conn: iscsi connection
5227996a778SMike Christie  *
5237996a778SMike Christie  * Notes:
5247996a778SMike Christie  *	The function can return -EAGAIN in which case the caller must
5257996a778SMike Christie  *	re-schedule it again later or recover. '0' return code means
5267996a778SMike Christie  *	successful xmit.
5277996a778SMike Christie  **/
5287996a778SMike Christie static int iscsi_data_xmit(struct iscsi_conn *conn)
5297996a778SMike Christie {
5307996a778SMike Christie 	struct iscsi_transport *tt;
5313219e529SMike Christie 	int rc = 0;
5327996a778SMike Christie 
5337996a778SMike Christie 	if (unlikely(conn->suspend_tx)) {
5347996a778SMike Christie 		debug_scsi("conn %d Tx suspended!\n", conn->id);
5353219e529SMike Christie 		return -ENODATA;
5367996a778SMike Christie 	}
5377996a778SMike Christie 	tt = conn->session->tt;
5387996a778SMike Christie 
5397996a778SMike Christie 	/*
5407996a778SMike Christie 	 * Transmit in the following order:
5417996a778SMike Christie 	 *
5427996a778SMike Christie 	 * 1) un-finished xmit (ctask or mtask)
5437996a778SMike Christie 	 * 2) immediate control PDUs
5447996a778SMike Christie 	 * 3) write data
5457996a778SMike Christie 	 * 4) SCSI commands
5467996a778SMike Christie 	 * 5) non-immediate control PDUs
5477996a778SMike Christie 	 *
5487996a778SMike Christie 	 * No need to lock around __kfifo_get as long as
5497996a778SMike Christie 	 * there's one producer and one consumer.
5507996a778SMike Christie 	 */
5517996a778SMike Christie 
5527996a778SMike Christie 	BUG_ON(conn->ctask && conn->mtask);
5537996a778SMike Christie 
5547996a778SMike Christie 	if (conn->ctask) {
5553219e529SMike Christie 		rc = tt->xmit_cmd_task(conn, conn->ctask);
5563219e529SMike Christie 		if (rc)
5577996a778SMike Christie 			goto again;
5587996a778SMike Christie 		/* done with this in-progress ctask */
5597996a778SMike Christie 		conn->ctask = NULL;
5607996a778SMike Christie 	}
5617996a778SMike Christie 	if (conn->mtask) {
5623219e529SMike Christie 		rc = tt->xmit_mgmt_task(conn, conn->mtask);
5633219e529SMike Christie 	        if (rc)
5647996a778SMike Christie 		        goto again;
5657996a778SMike Christie 		/* done with this in-progress mtask */
5667996a778SMike Christie 		conn->mtask = NULL;
5677996a778SMike Christie 	}
5687996a778SMike Christie 
5697996a778SMike Christie 	/* process immediate first */
5707996a778SMike Christie         if (unlikely(__kfifo_len(conn->immqueue))) {
5717996a778SMike Christie 	        while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
5727996a778SMike Christie 			           sizeof(void*))) {
573994442e8SMike Christie 			spin_lock_bh(&conn->session->lock);
5747996a778SMike Christie 			list_add_tail(&conn->mtask->running,
5757996a778SMike Christie 				      &conn->mgmt_run_list);
576994442e8SMike Christie 			spin_unlock_bh(&conn->session->lock);
5773219e529SMike Christie 			rc = tt->xmit_mgmt_task(conn, conn->mtask);
5783219e529SMike Christie 		        if (rc)
5797996a778SMike Christie 			        goto again;
5807996a778SMike Christie 	        }
5817996a778SMike Christie 		/* done with this mtask */
5827996a778SMike Christie 		conn->mtask = NULL;
5837996a778SMike Christie 	}
5847996a778SMike Christie 
5857996a778SMike Christie 	/* process command queue */
586b6c395edSMike Christie 	spin_lock_bh(&conn->session->lock);
587b6c395edSMike Christie 	while (!list_empty(&conn->xmitqueue)) {
5887996a778SMike Christie 		/*
5897996a778SMike Christie 		 * iscsi tcp may readd the task to the xmitqueue to send
5907996a778SMike Christie 		 * write data
5917996a778SMike Christie 		 */
592b6c395edSMike Christie 		conn->ctask = list_entry(conn->xmitqueue.next,
593b6c395edSMike Christie 					 struct iscsi_cmd_task, running);
594b6c395edSMike Christie 		conn->ctask->state = ISCSI_TASK_RUNNING;
595b6c395edSMike Christie 		list_move_tail(conn->xmitqueue.next, &conn->run_list);
596994442e8SMike Christie 		spin_unlock_bh(&conn->session->lock);
597b6c395edSMike Christie 
5983219e529SMike Christie 		rc = tt->xmit_cmd_task(conn, conn->ctask);
5993219e529SMike Christie 		if (rc)
6007996a778SMike Christie 			goto again;
601b6c395edSMike Christie 		spin_lock_bh(&conn->session->lock);
6027996a778SMike Christie 	}
603b6c395edSMike Christie 	spin_unlock_bh(&conn->session->lock);
6047996a778SMike Christie 	/* done with this ctask */
6057996a778SMike Christie 	conn->ctask = NULL;
6067996a778SMike Christie 
6077996a778SMike Christie 	/* process the rest control plane PDUs, if any */
6087996a778SMike Christie         if (unlikely(__kfifo_len(conn->mgmtqueue))) {
6097996a778SMike Christie 	        while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
6107996a778SMike Christie 			           sizeof(void*))) {
611994442e8SMike Christie 			spin_lock_bh(&conn->session->lock);
6127996a778SMike Christie 			list_add_tail(&conn->mtask->running,
6137996a778SMike Christie 				      &conn->mgmt_run_list);
614994442e8SMike Christie 			spin_unlock_bh(&conn->session->lock);
6153219e529SMike Christie 		        rc = tt->xmit_mgmt_task(conn, conn->mtask);
6163219e529SMike Christie 			if (rc)
6177996a778SMike Christie 			        goto again;
6187996a778SMike Christie 	        }
6197996a778SMike Christie 		/* done with this mtask */
6207996a778SMike Christie 		conn->mtask = NULL;
6217996a778SMike Christie 	}
6227996a778SMike Christie 
6233219e529SMike Christie 	return -ENODATA;
6247996a778SMike Christie 
6257996a778SMike Christie again:
6267996a778SMike Christie 	if (unlikely(conn->suspend_tx))
6273219e529SMike Christie 		return -ENODATA;
6287996a778SMike Christie 
6293219e529SMike Christie 	return rc;
6307996a778SMike Christie }
6317996a778SMike Christie 
6327996a778SMike Christie static void iscsi_xmitworker(void *data)
6337996a778SMike Christie {
6347996a778SMike Christie 	struct iscsi_conn *conn = data;
6353219e529SMike Christie 	int rc;
6367996a778SMike Christie 	/*
6377996a778SMike Christie 	 * serialize Xmit worker on a per-connection basis.
6387996a778SMike Christie 	 */
6397996a778SMike Christie 	mutex_lock(&conn->xmitmutex);
6403219e529SMike Christie 	do {
6413219e529SMike Christie 		rc = iscsi_data_xmit(conn);
6423219e529SMike Christie 	} while (rc >= 0 || rc == -EAGAIN);
6437996a778SMike Christie 	mutex_unlock(&conn->xmitmutex);
6447996a778SMike Christie }
6457996a778SMike Christie 
6467996a778SMike Christie enum {
6477996a778SMike Christie 	FAILURE_BAD_HOST = 1,
6487996a778SMike Christie 	FAILURE_SESSION_FAILED,
6497996a778SMike Christie 	FAILURE_SESSION_FREED,
6507996a778SMike Christie 	FAILURE_WINDOW_CLOSED,
6517996a778SMike Christie 	FAILURE_SESSION_TERMINATE,
652656cffc9SMike Christie 	FAILURE_SESSION_IN_RECOVERY,
6537996a778SMike Christie 	FAILURE_SESSION_RECOVERY_TIMEOUT,
6547996a778SMike Christie };
6557996a778SMike Christie 
6567996a778SMike Christie int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
6577996a778SMike Christie {
6587996a778SMike Christie 	struct Scsi_Host *host;
6597996a778SMike Christie 	int reason = 0;
6607996a778SMike Christie 	struct iscsi_session *session;
6617996a778SMike Christie 	struct iscsi_conn *conn;
6627996a778SMike Christie 	struct iscsi_cmd_task *ctask = NULL;
6637996a778SMike Christie 
6647996a778SMike Christie 	sc->scsi_done = done;
6657996a778SMike Christie 	sc->result = 0;
6667996a778SMike Christie 
6677996a778SMike Christie 	host = sc->device->host;
6687996a778SMike Christie 	session = iscsi_hostdata(host->hostdata);
6697996a778SMike Christie 
6707996a778SMike Christie 	spin_lock(&session->lock);
6717996a778SMike Christie 
672656cffc9SMike Christie 	/*
673656cffc9SMike Christie 	 * ISCSI_STATE_FAILED is a temp. state. The recovery
674656cffc9SMike Christie 	 * code will decide what is best to do with command queued
675656cffc9SMike Christie 	 * during this time
676656cffc9SMike Christie 	 */
677656cffc9SMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN &&
678656cffc9SMike Christie 	    session->state != ISCSI_STATE_FAILED) {
679656cffc9SMike Christie 		/*
680656cffc9SMike Christie 		 * to handle the race between when we set the recovery state
681656cffc9SMike Christie 		 * and block the session we requeue here (commands could
682656cffc9SMike Christie 		 * be entering our queuecommand while a block is starting
683656cffc9SMike Christie 		 * up because the block code is not locked)
684656cffc9SMike Christie 		 */
685656cffc9SMike Christie 		if (session->state == ISCSI_STATE_IN_RECOVERY) {
686656cffc9SMike Christie 			reason = FAILURE_SESSION_IN_RECOVERY;
68767a61114SMike Christie 			goto reject;
6887996a778SMike Christie 		}
689656cffc9SMike Christie 
690656cffc9SMike Christie 		if (session->state == ISCSI_STATE_RECOVERY_FAILED)
691656cffc9SMike Christie 			reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
692656cffc9SMike Christie 		else if (session->state == ISCSI_STATE_TERMINATE)
693656cffc9SMike Christie 			reason = FAILURE_SESSION_TERMINATE;
694656cffc9SMike Christie 		else
6957996a778SMike Christie 			reason = FAILURE_SESSION_FREED;
6967996a778SMike Christie 		goto fault;
6977996a778SMike Christie 	}
6987996a778SMike Christie 
6997996a778SMike Christie 	/*
7007996a778SMike Christie 	 * Check for iSCSI window and take care of CmdSN wrap-around
7017996a778SMike Christie 	 */
7027996a778SMike Christie 	if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
7037996a778SMike Christie 		reason = FAILURE_WINDOW_CLOSED;
7047996a778SMike Christie 		goto reject;
7057996a778SMike Christie 	}
7067996a778SMike Christie 
7077996a778SMike Christie 	conn = session->leadconn;
7087996a778SMike Christie 
7097996a778SMike Christie 	__kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
7107996a778SMike Christie 	sc->SCp.phase = session->age;
7117996a778SMike Christie 	sc->SCp.ptr = (char *)ctask;
7127996a778SMike Christie 
713b6c395edSMike Christie 	ctask->state = ISCSI_TASK_PENDING;
7147996a778SMike Christie 	ctask->mtask = NULL;
7157996a778SMike Christie 	ctask->conn = conn;
7167996a778SMike Christie 	ctask->sc = sc;
7177996a778SMike Christie 	INIT_LIST_HEAD(&ctask->running);
7187996a778SMike Christie 	ctask->total_length = sc->request_bufflen;
7197996a778SMike Christie 	iscsi_prep_scsi_cmd_pdu(ctask);
7207996a778SMike Christie 
7217996a778SMike Christie 	session->tt->init_cmd_task(ctask);
7227996a778SMike Christie 
723b6c395edSMike Christie 	list_add_tail(&ctask->running, &conn->xmitqueue);
7247996a778SMike Christie 	debug_scsi(
7257996a778SMike Christie 	       "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
7267996a778SMike Christie 		sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
7277996a778SMike Christie 		conn->id, (long)sc, ctask->itt, sc->request_bufflen,
7287996a778SMike Christie 		session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
7297996a778SMike Christie 	spin_unlock(&session->lock);
7307996a778SMike Christie 
7317996a778SMike Christie 	scsi_queue_work(host, &conn->xmitwork);
7327996a778SMike Christie 	return 0;
7337996a778SMike Christie 
7347996a778SMike Christie reject:
7357996a778SMike Christie 	spin_unlock(&session->lock);
7367996a778SMike Christie 	debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
7377996a778SMike Christie 	return SCSI_MLQUEUE_HOST_BUSY;
7387996a778SMike Christie 
7397996a778SMike Christie fault:
7407996a778SMike Christie 	spin_unlock(&session->lock);
741be2df72eSOr Gerlitz 	printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
7427996a778SMike Christie 	       sc->cmnd[0], reason);
7437996a778SMike Christie 	sc->result = (DID_NO_CONNECT << 16);
7447996a778SMike Christie 	sc->resid = sc->request_bufflen;
7457996a778SMike Christie 	sc->scsi_done(sc);
7467996a778SMike Christie 	return 0;
7477996a778SMike Christie }
7487996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_queuecommand);
7497996a778SMike Christie 
7507996a778SMike Christie int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
7517996a778SMike Christie {
7527996a778SMike Christie 	if (depth > ISCSI_MAX_CMD_PER_LUN)
7537996a778SMike Christie 		depth = ISCSI_MAX_CMD_PER_LUN;
7547996a778SMike Christie 	scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
7557996a778SMike Christie 	return sdev->queue_depth;
7567996a778SMike Christie }
7577996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
7587996a778SMike Christie 
7597996a778SMike Christie static int
7607996a778SMike Christie iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
7617996a778SMike Christie 			char *data, uint32_t data_size)
7627996a778SMike Christie {
7637996a778SMike Christie 	struct iscsi_session *session = conn->session;
7647996a778SMike Christie 	struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
7657996a778SMike Christie 	struct iscsi_mgmt_task *mtask;
7667996a778SMike Christie 
7677996a778SMike Christie 	spin_lock_bh(&session->lock);
7687996a778SMike Christie 	if (session->state == ISCSI_STATE_TERMINATE) {
7697996a778SMike Christie 		spin_unlock_bh(&session->lock);
7707996a778SMike Christie 		return -EPERM;
7717996a778SMike Christie 	}
7727996a778SMike Christie 	if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
7737996a778SMike Christie 	    hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
7747996a778SMike Christie 		/*
7757996a778SMike Christie 		 * Login and Text are sent serially, in
7767996a778SMike Christie 		 * request-followed-by-response sequence.
7777996a778SMike Christie 		 * Same mtask can be used. Same ITT must be used.
7787996a778SMike Christie 		 * Note that login_mtask is preallocated at conn_create().
7797996a778SMike Christie 		 */
7807996a778SMike Christie 		mtask = conn->login_mtask;
7817996a778SMike Christie 	else {
7827996a778SMike Christie 		BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
7837996a778SMike Christie 		BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
7847996a778SMike Christie 
7858d2860b3SMike Christie 		nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
7867996a778SMike Christie 		if (!__kfifo_get(session->mgmtpool.queue,
7877996a778SMike Christie 				 (void*)&mtask, sizeof(void*))) {
7887996a778SMike Christie 			spin_unlock_bh(&session->lock);
7897996a778SMike Christie 			return -ENOSPC;
7907996a778SMike Christie 		}
7917996a778SMike Christie 	}
7927996a778SMike Christie 
7937996a778SMike Christie 	/*
7948d2860b3SMike Christie 	 * pre-format CmdSN for outgoing PDU.
7957996a778SMike Christie 	 */
7967996a778SMike Christie 	if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
7977996a778SMike Christie 		hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
7987996a778SMike Christie 			   (session->age << ISCSI_AGE_SHIFT);
7997996a778SMike Christie 		nop->cmdsn = cpu_to_be32(session->cmdsn);
8007996a778SMike Christie 		if (conn->c_stage == ISCSI_CONN_STARTED &&
8017996a778SMike Christie 		    !(hdr->opcode & ISCSI_OP_IMMEDIATE))
8027996a778SMike Christie 			session->cmdsn++;
8037996a778SMike Christie 	} else
8047996a778SMike Christie 		/* do not advance CmdSN */
8057996a778SMike Christie 		nop->cmdsn = cpu_to_be32(session->cmdsn);
8067996a778SMike Christie 
8077996a778SMike Christie 	if (data_size) {
8087996a778SMike Christie 		memcpy(mtask->data, data, data_size);
8097996a778SMike Christie 		mtask->data_count = data_size;
8107996a778SMike Christie 	} else
8117996a778SMike Christie 		mtask->data_count = 0;
8127996a778SMike Christie 
8137996a778SMike Christie 	INIT_LIST_HEAD(&mtask->running);
8147996a778SMike Christie 	memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
8157996a778SMike Christie 	if (session->tt->init_mgmt_task)
8167996a778SMike Christie 		session->tt->init_mgmt_task(conn, mtask, data, data_size);
8177996a778SMike Christie 	spin_unlock_bh(&session->lock);
8187996a778SMike Christie 
8197996a778SMike Christie 	debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
8207996a778SMike Christie 		   hdr->opcode, hdr->itt, data_size);
8217996a778SMike Christie 
8227996a778SMike Christie 	/*
8237996a778SMike Christie 	 * since send_pdu() could be called at least from two contexts,
8247996a778SMike Christie 	 * we need to serialize __kfifo_put, so we don't have to take
8257996a778SMike Christie 	 * additional lock on fast data-path
8267996a778SMike Christie 	 */
8277996a778SMike Christie         if (hdr->opcode & ISCSI_OP_IMMEDIATE)
8287996a778SMike Christie 	        __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
8297996a778SMike Christie 	else
8307996a778SMike Christie 	        __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
8317996a778SMike Christie 
8327996a778SMike Christie 	scsi_queue_work(session->host, &conn->xmitwork);
8337996a778SMike Christie 	return 0;
8347996a778SMike Christie }
8357996a778SMike Christie 
8367996a778SMike Christie int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
8377996a778SMike Christie 			char *data, uint32_t data_size)
8387996a778SMike Christie {
8397996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
8407996a778SMike Christie 	int rc;
8417996a778SMike Christie 
8427996a778SMike Christie 	mutex_lock(&conn->xmitmutex);
8437996a778SMike Christie 	rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
8447996a778SMike Christie 	mutex_unlock(&conn->xmitmutex);
8457996a778SMike Christie 
8467996a778SMike Christie 	return rc;
8477996a778SMike Christie }
8487996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
8497996a778SMike Christie 
8507996a778SMike Christie void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
8517996a778SMike Christie {
8527996a778SMike Christie 	struct iscsi_session *session = class_to_transport_session(cls_session);
8537996a778SMike Christie 	struct iscsi_conn *conn = session->leadconn;
8547996a778SMike Christie 
8557996a778SMike Christie 	spin_lock_bh(&session->lock);
8567996a778SMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN) {
857656cffc9SMike Christie 		session->state = ISCSI_STATE_RECOVERY_FAILED;
8587996a778SMike Christie 		if (conn)
8597996a778SMike Christie 			wake_up(&conn->ehwait);
8607996a778SMike Christie 	}
8617996a778SMike Christie 	spin_unlock_bh(&session->lock);
8627996a778SMike Christie }
8637996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
8647996a778SMike Christie 
8657996a778SMike Christie int iscsi_eh_host_reset(struct scsi_cmnd *sc)
8667996a778SMike Christie {
8677996a778SMike Christie 	struct Scsi_Host *host = sc->device->host;
8687996a778SMike Christie 	struct iscsi_session *session = iscsi_hostdata(host->hostdata);
8697996a778SMike Christie 	struct iscsi_conn *conn = session->leadconn;
8707996a778SMike Christie 	int fail_session = 0;
8717996a778SMike Christie 
8727996a778SMike Christie 	spin_lock_bh(&session->lock);
8737996a778SMike Christie 	if (session->state == ISCSI_STATE_TERMINATE) {
8747996a778SMike Christie failed:
8757996a778SMike Christie 		debug_scsi("failing host reset: session terminated "
8767996a778SMike Christie 			   "[CID %d age %d]", conn->id, session->age);
8777996a778SMike Christie 		spin_unlock_bh(&session->lock);
8787996a778SMike Christie 		return FAILED;
8797996a778SMike Christie 	}
8807996a778SMike Christie 
8817996a778SMike Christie 	if (sc->SCp.phase == session->age) {
8827996a778SMike Christie 		debug_scsi("failing connection CID %d due to SCSI host reset",
8837996a778SMike Christie 			   conn->id);
8847996a778SMike Christie 		fail_session = 1;
8857996a778SMike Christie 	}
8867996a778SMike Christie 	spin_unlock_bh(&session->lock);
8877996a778SMike Christie 
8887996a778SMike Christie 	/*
8897996a778SMike Christie 	 * we drop the lock here but the leadconn cannot be destoyed while
8907996a778SMike Christie 	 * we are in the scsi eh
8917996a778SMike Christie 	 */
892656cffc9SMike Christie 	if (fail_session)
8937996a778SMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
8947996a778SMike Christie 
8957996a778SMike Christie 	debug_scsi("iscsi_eh_host_reset wait for relogin\n");
8967996a778SMike Christie 	wait_event_interruptible(conn->ehwait,
8977996a778SMike Christie 				 session->state == ISCSI_STATE_TERMINATE ||
8987996a778SMike Christie 				 session->state == ISCSI_STATE_LOGGED_IN ||
899656cffc9SMike Christie 				 session->state == ISCSI_STATE_RECOVERY_FAILED);
9007996a778SMike Christie 	if (signal_pending(current))
9017996a778SMike Christie 		flush_signals(current);
9027996a778SMike Christie 
9037996a778SMike Christie 	spin_lock_bh(&session->lock);
9047996a778SMike Christie 	if (session->state == ISCSI_STATE_LOGGED_IN)
905be2df72eSOr Gerlitz 		printk(KERN_INFO "iscsi: host reset succeeded\n");
9067996a778SMike Christie 	else
9077996a778SMike Christie 		goto failed;
9087996a778SMike Christie 	spin_unlock_bh(&session->lock);
9097996a778SMike Christie 
9107996a778SMike Christie 	return SUCCESS;
9117996a778SMike Christie }
9127996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
9137996a778SMike Christie 
9147996a778SMike Christie static void iscsi_tmabort_timedout(unsigned long data)
9157996a778SMike Christie {
9167996a778SMike Christie 	struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
9177996a778SMike Christie 	struct iscsi_conn *conn = ctask->conn;
9187996a778SMike Christie 	struct iscsi_session *session = conn->session;
9197996a778SMike Christie 
9207996a778SMike Christie 	spin_lock(&session->lock);
9217996a778SMike Christie 	if (conn->tmabort_state == TMABORT_INITIAL) {
9227996a778SMike Christie 		conn->tmabort_state = TMABORT_TIMEDOUT;
9237996a778SMike Christie 		debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
9247996a778SMike Christie 			ctask->sc, ctask->itt);
9257996a778SMike Christie 		/* unblock eh_abort() */
9267996a778SMike Christie 		wake_up(&conn->ehwait);
9277996a778SMike Christie 	}
9287996a778SMike Christie 	spin_unlock(&session->lock);
9297996a778SMike Christie }
9307996a778SMike Christie 
9317996a778SMike Christie /* must be called with the mutex lock */
9327996a778SMike Christie static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
9337996a778SMike Christie 				 struct iscsi_cmd_task *ctask)
9347996a778SMike Christie {
9357996a778SMike Christie 	struct iscsi_conn *conn = ctask->conn;
9367996a778SMike Christie 	struct iscsi_session *session = conn->session;
9377996a778SMike Christie 	struct iscsi_tm *hdr = &conn->tmhdr;
9387996a778SMike Christie 	int rc;
9397996a778SMike Christie 
9407996a778SMike Christie 	/*
9417996a778SMike Christie 	 * ctask timed out but session is OK requests must be serialized.
9427996a778SMike Christie 	 */
9437996a778SMike Christie 	memset(hdr, 0, sizeof(struct iscsi_tm));
9447996a778SMike Christie 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
9457996a778SMike Christie 	hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
9467996a778SMike Christie 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
9477996a778SMike Christie 	memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
9487996a778SMike Christie 	hdr->rtt = ctask->hdr->itt;
9497996a778SMike Christie 	hdr->refcmdsn = ctask->hdr->cmdsn;
9507996a778SMike Christie 
9517996a778SMike Christie 	rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
9527996a778SMike Christie 				     NULL, 0);
9537996a778SMike Christie 	if (rc) {
9547996a778SMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
9557996a778SMike Christie 		debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc);
9567996a778SMike Christie 		return rc;
9577996a778SMike Christie 	}
9587996a778SMike Christie 
9597996a778SMike Christie 	debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
9607996a778SMike Christie 
9617996a778SMike Christie 	spin_lock_bh(&session->lock);
9627996a778SMike Christie 	ctask->mtask = (struct iscsi_mgmt_task *)
9637996a778SMike Christie 			session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
9647996a778SMike Christie 					ISCSI_MGMT_ITT_OFFSET];
9657996a778SMike Christie 
9667996a778SMike Christie 	if (conn->tmabort_state == TMABORT_INITIAL) {
9677996a778SMike Christie 		conn->tmfcmd_pdus_cnt++;
9687996a778SMike Christie 		conn->tmabort_timer.expires = 10*HZ + jiffies;
9697996a778SMike Christie 		conn->tmabort_timer.function = iscsi_tmabort_timedout;
9707996a778SMike Christie 		conn->tmabort_timer.data = (unsigned long)ctask;
9717996a778SMike Christie 		add_timer(&conn->tmabort_timer);
9727996a778SMike Christie 		debug_scsi("abort set timeout [itt 0x%x]", ctask->itt);
9737996a778SMike Christie 	}
9747996a778SMike Christie 	spin_unlock_bh(&session->lock);
9757996a778SMike Christie 	mutex_unlock(&conn->xmitmutex);
9767996a778SMike Christie 
9777996a778SMike Christie 	/*
9787996a778SMike Christie 	 * block eh thread until:
9797996a778SMike Christie 	 *
9807996a778SMike Christie 	 * 1) abort response
9817996a778SMike Christie 	 * 2) abort timeout
9827996a778SMike Christie 	 * 3) session is terminated or restarted or userspace has
9837996a778SMike Christie 	 * given up on recovery
9847996a778SMike Christie 	 */
9857996a778SMike Christie 	wait_event_interruptible(conn->ehwait,
9867996a778SMike Christie 				 sc->SCp.phase != session->age ||
9877996a778SMike Christie 				 session->state != ISCSI_STATE_LOGGED_IN ||
988656cffc9SMike Christie 				 conn->tmabort_state != TMABORT_INITIAL);
9897996a778SMike Christie 	if (signal_pending(current))
9907996a778SMike Christie 		flush_signals(current);
9917996a778SMike Christie 	del_timer_sync(&conn->tmabort_timer);
9927996a778SMike Christie 
9937996a778SMike Christie 	mutex_lock(&conn->xmitmutex);
9947996a778SMike Christie 	return 0;
9957996a778SMike Christie }
9967996a778SMike Christie 
9977996a778SMike Christie /*
9987996a778SMike Christie  * xmit mutex and session lock must be held
9997996a778SMike Christie  */
1000b6c395edSMike Christie static struct iscsi_mgmt_task *
1001b6c395edSMike Christie iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1002b6c395edSMike Christie {
1003b6c395edSMike Christie 	int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1004b6c395edSMike Christie 	struct iscsi_mgmt_task *task;
1005b6c395edSMike Christie 
1006b6c395edSMike Christie 	debug_scsi("searching %d tasks\n", nr_tasks);
1007b6c395edSMike Christie 
1008b6c395edSMike Christie 	for (i = 0; i < nr_tasks; i++) {
1009b6c395edSMike Christie 		__kfifo_get(fifo, (void*)&task, sizeof(void*));
1010b6c395edSMike Christie 		debug_scsi("check task %u\n", task->itt);
1011b6c395edSMike Christie 
1012b6c395edSMike Christie 		if (task->itt == itt) {
1013b6c395edSMike Christie 			debug_scsi("matched task\n");
1014b6c395edSMike Christie 			return task;
10157996a778SMike Christie 		}
10167996a778SMike Christie 
1017b6c395edSMike Christie 		__kfifo_put(fifo, (void*)&task, sizeof(void*));
1018b6c395edSMike Christie 	}
1019b6c395edSMike Christie 	return NULL;
1020b6c395edSMike Christie }
10217996a778SMike Christie 
10227996a778SMike Christie static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
10237996a778SMike Christie {
10247996a778SMike Christie 	struct iscsi_conn *conn = ctask->conn;
10257996a778SMike Christie 	struct iscsi_session *session = conn->session;
10267996a778SMike Christie 
10277996a778SMike Christie 	if (!ctask->mtask)
10287996a778SMike Christie 		return -EINVAL;
10297996a778SMike Christie 
10307996a778SMike Christie 	if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
10317996a778SMike Christie 		list_del(&ctask->mtask->running);
10327996a778SMike Christie 	__kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
10337996a778SMike Christie 		    sizeof(void*));
10347996a778SMike Christie 	ctask->mtask = NULL;
10357996a778SMike Christie 	return 0;
10367996a778SMike Christie }
10377996a778SMike Christie 
10387996a778SMike Christie /*
10397996a778SMike Christie  * session lock and xmitmutex must be held
10407996a778SMike Christie  */
10417996a778SMike Christie static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
10427996a778SMike Christie 			 int err)
10437996a778SMike Christie {
10447996a778SMike Christie 	struct scsi_cmnd *sc;
10457996a778SMike Christie 
10467996a778SMike Christie 	sc = ctask->sc;
10477996a778SMike Christie 	if (!sc)
10487996a778SMike Christie 		return;
10497ea8b828SMike Christie 
10507ea8b828SMike Christie 	conn->session->tt->cleanup_cmd_task(conn, ctask);
10517ea8b828SMike Christie 	iscsi_ctask_mtask_cleanup(ctask);
10527ea8b828SMike Christie 
10537996a778SMike Christie 	sc->result = err;
10547996a778SMike Christie 	sc->resid = sc->request_bufflen;
10557996a778SMike Christie 	iscsi_complete_command(conn->session, ctask);
10567996a778SMike Christie }
10577996a778SMike Christie 
10587996a778SMike Christie int iscsi_eh_abort(struct scsi_cmnd *sc)
10597996a778SMike Christie {
10607996a778SMike Christie 	struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
10617996a778SMike Christie 	struct iscsi_conn *conn = ctask->conn;
10627996a778SMike Christie 	struct iscsi_session *session = conn->session;
10637996a778SMike Christie 	int rc;
10647996a778SMike Christie 
10657996a778SMike Christie 	conn->eh_abort_cnt++;
10667996a778SMike Christie 	debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
10677996a778SMike Christie 
10687996a778SMike Christie 	mutex_lock(&conn->xmitmutex);
10697996a778SMike Christie 	spin_lock_bh(&session->lock);
10707996a778SMike Christie 
10717996a778SMike Christie 	/*
10727996a778SMike Christie 	 * If we are not logged in or we have started a new session
10737996a778SMike Christie 	 * then let the host reset code handle this
10747996a778SMike Christie 	 */
10757996a778SMike Christie 	if (session->state != ISCSI_STATE_LOGGED_IN ||
10767996a778SMike Christie 	    sc->SCp.phase != session->age)
10777996a778SMike Christie 		goto failed;
10787996a778SMike Christie 
10797996a778SMike Christie 	/* ctask completed before time out */
10807ea8b828SMike Christie 	if (!ctask->sc) {
10817ea8b828SMike Christie 		spin_unlock_bh(&session->lock);
10827ea8b828SMike Christie 		debug_scsi("sc completed while abort in progress\n");
10837ea8b828SMike Christie 		goto success_rel_mutex;
10847ea8b828SMike Christie 	}
10857996a778SMike Christie 
10867996a778SMike Christie 	/* what should we do here ? */
10877996a778SMike Christie 	if (conn->ctask == ctask) {
1088be2df72eSOr Gerlitz 		printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1089be2df72eSOr Gerlitz 		       "Failing abort\n", sc, ctask->itt);
10907996a778SMike Christie 		goto failed;
10917996a778SMike Christie 	}
10927996a778SMike Christie 
1093b6c395edSMike Christie 	if (ctask->state == ISCSI_TASK_PENDING)
10947ea8b828SMike Christie 		goto success_cleanup;
10957996a778SMike Christie 
10967996a778SMike Christie 	conn->tmabort_state = TMABORT_INITIAL;
10977996a778SMike Christie 
10987996a778SMike Christie 	spin_unlock_bh(&session->lock);
10997996a778SMike Christie 	rc = iscsi_exec_abort_task(sc, ctask);
11007996a778SMike Christie 	spin_lock_bh(&session->lock);
11017996a778SMike Christie 
11027996a778SMike Christie 	if (rc || sc->SCp.phase != session->age ||
11037996a778SMike Christie 	    session->state != ISCSI_STATE_LOGGED_IN)
11047996a778SMike Christie 		goto failed;
11057ea8b828SMike Christie 	iscsi_ctask_mtask_cleanup(ctask);
11067996a778SMike Christie 
11077ea8b828SMike Christie 	switch (conn->tmabort_state) {
11087ea8b828SMike Christie 	case TMABORT_SUCCESS:
11097ea8b828SMike Christie 		goto success_cleanup;
11107ea8b828SMike Christie 	case TMABORT_NOT_FOUND:
11117996a778SMike Christie 		if (!ctask->sc) {
11127ea8b828SMike Christie 			/* ctask completed before tmf abort response */
11137ea8b828SMike Christie 			spin_unlock_bh(&session->lock);
11147996a778SMike Christie 			debug_scsi("sc completed while abort in progress\n");
11157ea8b828SMike Christie 			goto success_rel_mutex;
11167996a778SMike Christie 		}
11177ea8b828SMike Christie 		/* fall through */
11187ea8b828SMike Christie 	default:
11197ea8b828SMike Christie 		/* timedout or failed */
11207996a778SMike Christie 		spin_unlock_bh(&session->lock);
11217996a778SMike Christie 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
11227996a778SMike Christie 		spin_lock_bh(&session->lock);
11237996a778SMike Christie 		goto failed;
11247996a778SMike Christie 	}
11257996a778SMike Christie 
11267ea8b828SMike Christie success_cleanup:
11277996a778SMike Christie 	debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
11287996a778SMike Christie 	spin_unlock_bh(&session->lock);
11297996a778SMike Christie 
11307996a778SMike Christie 	/*
11317996a778SMike Christie 	 * clean up task if aborted. we have the xmitmutex so grab
11327996a778SMike Christie 	 * the recv lock as a writer
11337996a778SMike Christie 	 */
11347996a778SMike Christie 	write_lock_bh(conn->recv_lock);
11357996a778SMike Christie 	spin_lock(&session->lock);
11367996a778SMike Christie 	fail_command(conn, ctask, DID_ABORT << 16);
11377996a778SMike Christie 	spin_unlock(&session->lock);
11387996a778SMike Christie 	write_unlock_bh(conn->recv_lock);
11397996a778SMike Christie 
11407ea8b828SMike Christie success_rel_mutex:
11417996a778SMike Christie 	mutex_unlock(&conn->xmitmutex);
11427996a778SMike Christie 	return SUCCESS;
11437996a778SMike Christie 
11447996a778SMike Christie failed:
11457996a778SMike Christie 	spin_unlock_bh(&session->lock);
11467996a778SMike Christie 	mutex_unlock(&conn->xmitmutex);
11477996a778SMike Christie 
11487996a778SMike Christie 	debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
11497996a778SMike Christie 	return FAILED;
11507996a778SMike Christie }
11517996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_abort);
11527996a778SMike Christie 
11537996a778SMike Christie int
11547996a778SMike Christie iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
11557996a778SMike Christie {
11567996a778SMike Christie 	int i;
11577996a778SMike Christie 
11587996a778SMike Christie 	*items = kmalloc(max * sizeof(void*), GFP_KERNEL);
11597996a778SMike Christie 	if (*items == NULL)
11607996a778SMike Christie 		return -ENOMEM;
11617996a778SMike Christie 
11627996a778SMike Christie 	q->max = max;
11637996a778SMike Christie 	q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
11647996a778SMike Christie 	if (q->pool == NULL) {
11657996a778SMike Christie 		kfree(*items);
11667996a778SMike Christie 		return -ENOMEM;
11677996a778SMike Christie 	}
11687996a778SMike Christie 
11697996a778SMike Christie 	q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
11707996a778SMike Christie 			      GFP_KERNEL, NULL);
11717996a778SMike Christie 	if (q->queue == ERR_PTR(-ENOMEM)) {
11727996a778SMike Christie 		kfree(q->pool);
11737996a778SMike Christie 		kfree(*items);
11747996a778SMike Christie 		return -ENOMEM;
11757996a778SMike Christie 	}
11767996a778SMike Christie 
11777996a778SMike Christie 	for (i = 0; i < max; i++) {
11787996a778SMike Christie 		q->pool[i] = kmalloc(item_size, GFP_KERNEL);
11797996a778SMike Christie 		if (q->pool[i] == NULL) {
11807996a778SMike Christie 			int j;
11817996a778SMike Christie 
11827996a778SMike Christie 			for (j = 0; j < i; j++)
11837996a778SMike Christie 				kfree(q->pool[j]);
11847996a778SMike Christie 
11857996a778SMike Christie 			kfifo_free(q->queue);
11867996a778SMike Christie 			kfree(q->pool);
11877996a778SMike Christie 			kfree(*items);
11887996a778SMike Christie 			return -ENOMEM;
11897996a778SMike Christie 		}
11907996a778SMike Christie 		memset(q->pool[i], 0, item_size);
11917996a778SMike Christie 		(*items)[i] = q->pool[i];
11927996a778SMike Christie 		__kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
11937996a778SMike Christie 	}
11947996a778SMike Christie 	return 0;
11957996a778SMike Christie }
11967996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_init);
11977996a778SMike Christie 
11987996a778SMike Christie void iscsi_pool_free(struct iscsi_queue *q, void **items)
11997996a778SMike Christie {
12007996a778SMike Christie 	int i;
12017996a778SMike Christie 
12027996a778SMike Christie 	for (i = 0; i < q->max; i++)
12037996a778SMike Christie 		kfree(items[i]);
12047996a778SMike Christie 	kfree(q->pool);
12057996a778SMike Christie 	kfree(items);
12067996a778SMike Christie }
12077996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_free);
12087996a778SMike Christie 
12097996a778SMike Christie /*
12107996a778SMike Christie  * iSCSI Session's hostdata organization:
12117996a778SMike Christie  *
12127996a778SMike Christie  *    *------------------* <== hostdata_session(host->hostdata)
12137996a778SMike Christie  *    | ptr to class sess|
12147996a778SMike Christie  *    |------------------| <== iscsi_hostdata(host->hostdata)
12157996a778SMike Christie  *    | iscsi_session    |
12167996a778SMike Christie  *    *------------------*
12177996a778SMike Christie  */
12187996a778SMike Christie 
12197996a778SMike Christie #define hostdata_privsize(_sz)	(sizeof(unsigned long) + _sz + \
12207996a778SMike Christie 				 _sz % sizeof(unsigned long))
12217996a778SMike Christie 
12227996a778SMike Christie #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
12237996a778SMike Christie 
12247996a778SMike Christie /**
12257996a778SMike Christie  * iscsi_session_setup - create iscsi cls session and host and session
12267996a778SMike Christie  * @scsit: scsi transport template
12277996a778SMike Christie  * @iscsit: iscsi transport template
12287996a778SMike Christie  * @initial_cmdsn: initial CmdSN
12297996a778SMike Christie  * @hostno: host no allocated
12307996a778SMike Christie  *
12317996a778SMike Christie  * This can be used by software iscsi_transports that allocate
12327996a778SMike Christie  * a session per scsi host.
12337996a778SMike Christie  **/
12347996a778SMike Christie struct iscsi_cls_session *
12357996a778SMike Christie iscsi_session_setup(struct iscsi_transport *iscsit,
12367996a778SMike Christie 		    struct scsi_transport_template *scsit,
12377996a778SMike Christie 		    int cmd_task_size, int mgmt_task_size,
12387996a778SMike Christie 		    uint32_t initial_cmdsn, uint32_t *hostno)
12397996a778SMike Christie {
12407996a778SMike Christie 	struct Scsi_Host *shost;
12417996a778SMike Christie 	struct iscsi_session *session;
12427996a778SMike Christie 	struct iscsi_cls_session *cls_session;
12437996a778SMike Christie 	int cmd_i;
12447996a778SMike Christie 
12457996a778SMike Christie 	shost = scsi_host_alloc(iscsit->host_template,
12467996a778SMike Christie 				hostdata_privsize(sizeof(*session)));
12477996a778SMike Christie 	if (!shost)
12487996a778SMike Christie 		return NULL;
12497996a778SMike Christie 
12507996a778SMike Christie 	shost->max_id = 1;
12517996a778SMike Christie 	shost->max_channel = 0;
12527996a778SMike Christie 	shost->max_lun = iscsit->max_lun;
12537996a778SMike Christie 	shost->max_cmd_len = iscsit->max_cmd_len;
12547996a778SMike Christie 	shost->transportt = scsit;
12557996a778SMike Christie 	shost->transportt->create_work_queue = 1;
12567996a778SMike Christie 	*hostno = shost->host_no;
12577996a778SMike Christie 
12587996a778SMike Christie 	session = iscsi_hostdata(shost->hostdata);
12597996a778SMike Christie 	memset(session, 0, sizeof(struct iscsi_session));
12607996a778SMike Christie 	session->host = shost;
12617996a778SMike Christie 	session->state = ISCSI_STATE_FREE;
12627996a778SMike Christie 	session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
12637996a778SMike Christie 	session->cmds_max = ISCSI_XMIT_CMDS_MAX;
12647996a778SMike Christie 	session->cmdsn = initial_cmdsn;
12657996a778SMike Christie 	session->exp_cmdsn = initial_cmdsn + 1;
12667996a778SMike Christie 	session->max_cmdsn = initial_cmdsn + 1;
12677996a778SMike Christie 	session->max_r2t = 1;
12687996a778SMike Christie 	session->tt = iscsit;
12697996a778SMike Christie 
12707996a778SMike Christie 	/* initialize SCSI PDU commands pool */
12717996a778SMike Christie 	if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
12727996a778SMike Christie 			    (void***)&session->cmds,
12737996a778SMike Christie 			    cmd_task_size + sizeof(struct iscsi_cmd_task)))
12747996a778SMike Christie 		goto cmdpool_alloc_fail;
12757996a778SMike Christie 
12767996a778SMike Christie 	/* pre-format cmds pool with ITT */
12777996a778SMike Christie 	for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
12787996a778SMike Christie 		struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
12797996a778SMike Christie 
12807996a778SMike Christie 		if (cmd_task_size)
12817996a778SMike Christie 			ctask->dd_data = &ctask[1];
12827996a778SMike Christie 		ctask->itt = cmd_i;
1283b6c395edSMike Christie 		INIT_LIST_HEAD(&ctask->running);
12847996a778SMike Christie 	}
12857996a778SMike Christie 
12867996a778SMike Christie 	spin_lock_init(&session->lock);
12877996a778SMike Christie 	INIT_LIST_HEAD(&session->connections);
12887996a778SMike Christie 
12897996a778SMike Christie 	/* initialize immediate command pool */
12907996a778SMike Christie 	if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
12917996a778SMike Christie 			   (void***)&session->mgmt_cmds,
12927996a778SMike Christie 			   mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
12937996a778SMike Christie 		goto mgmtpool_alloc_fail;
12947996a778SMike Christie 
12957996a778SMike Christie 
12967996a778SMike Christie 	/* pre-format immediate cmds pool with ITT */
12977996a778SMike Christie 	for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
12987996a778SMike Christie 		struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
12997996a778SMike Christie 
13007996a778SMike Christie 		if (mgmt_task_size)
13017996a778SMike Christie 			mtask->dd_data = &mtask[1];
13027996a778SMike Christie 		mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
1303b6c395edSMike Christie 		INIT_LIST_HEAD(&mtask->running);
13047996a778SMike Christie 	}
13057996a778SMike Christie 
13067996a778SMike Christie 	if (scsi_add_host(shost, NULL))
13077996a778SMike Christie 		goto add_host_fail;
13087996a778SMike Christie 
1309f53a88daSMike Christie 	if (!try_module_get(iscsit->owner))
1310f53a88daSMike Christie 		goto cls_session_fail;
1311f53a88daSMike Christie 
13126a8a0d36SMike Christie 	cls_session = iscsi_create_session(shost, iscsit, 0);
13137996a778SMike Christie 	if (!cls_session)
1314f53a88daSMike Christie 		goto module_put;
13157996a778SMike Christie 	*(unsigned long*)shost->hostdata = (unsigned long)cls_session;
13167996a778SMike Christie 
13177996a778SMike Christie 	return cls_session;
13187996a778SMike Christie 
1319f53a88daSMike Christie module_put:
1320f53a88daSMike Christie 	module_put(iscsit->owner);
13217996a778SMike Christie cls_session_fail:
13227996a778SMike Christie 	scsi_remove_host(shost);
13237996a778SMike Christie add_host_fail:
13247996a778SMike Christie 	iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
13257996a778SMike Christie mgmtpool_alloc_fail:
13267996a778SMike Christie 	iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
13277996a778SMike Christie cmdpool_alloc_fail:
13287996a778SMike Christie 	scsi_host_put(shost);
13297996a778SMike Christie 	return NULL;
13307996a778SMike Christie }
13317996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_setup);
13327996a778SMike Christie 
13337996a778SMike Christie /**
13347996a778SMike Christie  * iscsi_session_teardown - destroy session, host, and cls_session
13357996a778SMike Christie  * shost: scsi host
13367996a778SMike Christie  *
13377996a778SMike Christie  * This can be used by software iscsi_transports that allocate
13387996a778SMike Christie  * a session per scsi host.
13397996a778SMike Christie  **/
13407996a778SMike Christie void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
13417996a778SMike Christie {
13427996a778SMike Christie 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
13437996a778SMike Christie 	struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
134463f75cc8SMike Christie 	struct module *owner = cls_session->transport->owner;
13457996a778SMike Christie 
13467996a778SMike Christie 	scsi_remove_host(shost);
13477996a778SMike Christie 
13487996a778SMike Christie 	iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
13497996a778SMike Christie 	iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
13507996a778SMike Christie 
13517996a778SMike Christie 	iscsi_destroy_session(cls_session);
13527996a778SMike Christie 	scsi_host_put(shost);
135363f75cc8SMike Christie 	module_put(owner);
13547996a778SMike Christie }
13557996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_teardown);
13567996a778SMike Christie 
13577996a778SMike Christie /**
13587996a778SMike Christie  * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
13597996a778SMike Christie  * @cls_session: iscsi_cls_session
13607996a778SMike Christie  * @conn_idx: cid
13617996a778SMike Christie  **/
13627996a778SMike Christie struct iscsi_cls_conn *
13637996a778SMike Christie iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
13647996a778SMike Christie {
13657996a778SMike Christie 	struct iscsi_session *session = class_to_transport_session(cls_session);
13667996a778SMike Christie 	struct iscsi_conn *conn;
13677996a778SMike Christie 	struct iscsi_cls_conn *cls_conn;
1368d36ab6f3SMike Christie 	char *data;
13697996a778SMike Christie 
13707996a778SMike Christie 	cls_conn = iscsi_create_conn(cls_session, conn_idx);
13717996a778SMike Christie 	if (!cls_conn)
13727996a778SMike Christie 		return NULL;
13737996a778SMike Christie 	conn = cls_conn->dd_data;
13747996a778SMike Christie 	memset(conn, 0, sizeof(*conn));
13757996a778SMike Christie 
13767996a778SMike Christie 	conn->session = session;
13777996a778SMike Christie 	conn->cls_conn = cls_conn;
13787996a778SMike Christie 	conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
13797996a778SMike Christie 	conn->id = conn_idx;
13807996a778SMike Christie 	conn->exp_statsn = 0;
13817996a778SMike Christie 	conn->tmabort_state = TMABORT_INITIAL;
13827996a778SMike Christie 	INIT_LIST_HEAD(&conn->run_list);
13837996a778SMike Christie 	INIT_LIST_HEAD(&conn->mgmt_run_list);
1384b6c395edSMike Christie 	INIT_LIST_HEAD(&conn->xmitqueue);
13857996a778SMike Christie 
13867996a778SMike Christie 	/* initialize general immediate & non-immediate PDU commands queue */
13877996a778SMike Christie 	conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
13887996a778SMike Christie 			                GFP_KERNEL, NULL);
13897996a778SMike Christie 	if (conn->immqueue == ERR_PTR(-ENOMEM))
13907996a778SMike Christie 		goto immqueue_alloc_fail;
13917996a778SMike Christie 
13927996a778SMike Christie 	conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
13937996a778SMike Christie 			                GFP_KERNEL, NULL);
13947996a778SMike Christie 	if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
13957996a778SMike Christie 		goto mgmtqueue_alloc_fail;
13967996a778SMike Christie 
13977996a778SMike Christie 	INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
13987996a778SMike Christie 
13997996a778SMike Christie 	/* allocate login_mtask used for the login/text sequences */
14007996a778SMike Christie 	spin_lock_bh(&session->lock);
14017996a778SMike Christie 	if (!__kfifo_get(session->mgmtpool.queue,
14027996a778SMike Christie                          (void*)&conn->login_mtask,
14037996a778SMike Christie 			 sizeof(void*))) {
14047996a778SMike Christie 		spin_unlock_bh(&session->lock);
14057996a778SMike Christie 		goto login_mtask_alloc_fail;
14067996a778SMike Christie 	}
14077996a778SMike Christie 	spin_unlock_bh(&session->lock);
14087996a778SMike Christie 
1409d36ab6f3SMike Christie 	data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
1410d36ab6f3SMike Christie 	if (!data)
1411d36ab6f3SMike Christie 		goto login_mtask_data_alloc_fail;
1412c8dc1e52SMike Christie 	conn->login_mtask->data = conn->data = data;
1413d36ab6f3SMike Christie 
14147996a778SMike Christie 	init_timer(&conn->tmabort_timer);
14157996a778SMike Christie 	mutex_init(&conn->xmitmutex);
14167996a778SMike Christie 	init_waitqueue_head(&conn->ehwait);
14177996a778SMike Christie 
14187996a778SMike Christie 	return cls_conn;
14197996a778SMike Christie 
1420d36ab6f3SMike Christie login_mtask_data_alloc_fail:
1421d36ab6f3SMike Christie 	__kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1422d36ab6f3SMike Christie 		    sizeof(void*));
14237996a778SMike Christie login_mtask_alloc_fail:
14247996a778SMike Christie 	kfifo_free(conn->mgmtqueue);
14257996a778SMike Christie mgmtqueue_alloc_fail:
14267996a778SMike Christie 	kfifo_free(conn->immqueue);
14277996a778SMike Christie immqueue_alloc_fail:
14287996a778SMike Christie 	iscsi_destroy_conn(cls_conn);
14297996a778SMike Christie 	return NULL;
14307996a778SMike Christie }
14317996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_setup);
14327996a778SMike Christie 
14337996a778SMike Christie /**
14347996a778SMike Christie  * iscsi_conn_teardown - teardown iscsi connection
14357996a778SMike Christie  * cls_conn: iscsi class connection
14367996a778SMike Christie  *
14377996a778SMike Christie  * TODO: we may need to make this into a two step process
14387996a778SMike Christie  * like scsi-mls remove + put host
14397996a778SMike Christie  */
14407996a778SMike Christie void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
14417996a778SMike Christie {
14427996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
14437996a778SMike Christie 	struct iscsi_session *session = conn->session;
14447996a778SMike Christie 	unsigned long flags;
14457996a778SMike Christie 
14467996a778SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
144767a61114SMike Christie 	mutex_lock(&conn->xmitmutex);
14487996a778SMike Christie 
14497996a778SMike Christie 	spin_lock_bh(&session->lock);
14507996a778SMike Christie 	conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
14517996a778SMike Christie 	if (session->leadconn == conn) {
14527996a778SMike Christie 		/*
14537996a778SMike Christie 		 * leading connection? then give up on recovery.
14547996a778SMike Christie 		 */
14557996a778SMike Christie 		session->state = ISCSI_STATE_TERMINATE;
14567996a778SMike Christie 		wake_up(&conn->ehwait);
14577996a778SMike Christie 	}
14587996a778SMike Christie 	spin_unlock_bh(&session->lock);
14597996a778SMike Christie 
14607996a778SMike Christie 	mutex_unlock(&conn->xmitmutex);
14617996a778SMike Christie 
14627996a778SMike Christie 	/*
14637996a778SMike Christie 	 * Block until all in-progress commands for this connection
14647996a778SMike Christie 	 * time out or fail.
14657996a778SMike Christie 	 */
14667996a778SMike Christie 	for (;;) {
14677996a778SMike Christie 		spin_lock_irqsave(session->host->host_lock, flags);
14687996a778SMike Christie 		if (!session->host->host_busy) { /* OK for ERL == 0 */
14697996a778SMike Christie 			spin_unlock_irqrestore(session->host->host_lock, flags);
14707996a778SMike Christie 			break;
14717996a778SMike Christie 		}
14727996a778SMike Christie 		spin_unlock_irqrestore(session->host->host_lock, flags);
14737996a778SMike Christie 		msleep_interruptible(500);
1474be2df72eSOr Gerlitz 		printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1475be2df72eSOr Gerlitz 		       "host_failed %d\n", session->host->host_busy,
1476be2df72eSOr Gerlitz 		       session->host->host_failed);
14777996a778SMike Christie 		/*
14787996a778SMike Christie 		 * force eh_abort() to unblock
14797996a778SMike Christie 		 */
14807996a778SMike Christie 		wake_up(&conn->ehwait);
14817996a778SMike Christie 	}
14827996a778SMike Christie 
14837996a778SMike Christie 	spin_lock_bh(&session->lock);
1484c8dc1e52SMike Christie 	kfree(conn->data);
14857996a778SMike Christie 	__kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
14867996a778SMike Christie 		    sizeof(void*));
14877996a778SMike Christie 	list_del(&conn->item);
14887996a778SMike Christie 	if (list_empty(&session->connections))
14897996a778SMike Christie 		session->leadconn = NULL;
14907996a778SMike Christie 	if (session->leadconn && session->leadconn == conn)
14917996a778SMike Christie 		session->leadconn = container_of(session->connections.next,
14927996a778SMike Christie 			struct iscsi_conn, item);
14937996a778SMike Christie 
14947996a778SMike Christie 	if (session->leadconn == NULL)
14957996a778SMike Christie 		/* no connections exits.. reset sequencing */
14967996a778SMike Christie 		session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
14977996a778SMike Christie 	spin_unlock_bh(&session->lock);
14987996a778SMike Christie 
14997996a778SMike Christie 	kfifo_free(conn->immqueue);
15007996a778SMike Christie 	kfifo_free(conn->mgmtqueue);
15017996a778SMike Christie 
15027996a778SMike Christie 	iscsi_destroy_conn(cls_conn);
15037996a778SMike Christie }
15047996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
15057996a778SMike Christie 
15067996a778SMike Christie int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
15077996a778SMike Christie {
15087996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
15097996a778SMike Christie 	struct iscsi_session *session = conn->session;
15107996a778SMike Christie 
15117996a778SMike Christie 	if (session == NULL) {
15127996a778SMike Christie 		printk(KERN_ERR "iscsi: can't start unbound connection\n");
15137996a778SMike Christie 		return -EPERM;
15147996a778SMike Christie 	}
15157996a778SMike Christie 
15167996a778SMike Christie 	spin_lock_bh(&session->lock);
15177996a778SMike Christie 	conn->c_stage = ISCSI_CONN_STARTED;
15187996a778SMike Christie 	session->state = ISCSI_STATE_LOGGED_IN;
15197996a778SMike Christie 
15207996a778SMike Christie 	switch(conn->stop_stage) {
15217996a778SMike Christie 	case STOP_CONN_RECOVER:
15227996a778SMike Christie 		/*
15237996a778SMike Christie 		 * unblock eh_abort() if it is blocked. re-try all
15247996a778SMike Christie 		 * commands after successful recovery
15257996a778SMike Christie 		 */
15267996a778SMike Christie 		conn->stop_stage = 0;
15277996a778SMike Christie 		conn->tmabort_state = TMABORT_INITIAL;
15287996a778SMike Christie 		session->age++;
15297996a778SMike Christie 		spin_unlock_bh(&session->lock);
15307996a778SMike Christie 
15317996a778SMike Christie 		iscsi_unblock_session(session_to_cls(session));
15327996a778SMike Christie 		wake_up(&conn->ehwait);
15337996a778SMike Christie 		return 0;
15347996a778SMike Christie 	case STOP_CONN_TERM:
15357996a778SMike Christie 		conn->stop_stage = 0;
15367996a778SMike Christie 		break;
15377996a778SMike Christie 	default:
15387996a778SMike Christie 		break;
15397996a778SMike Christie 	}
15407996a778SMike Christie 	spin_unlock_bh(&session->lock);
15417996a778SMike Christie 
15427996a778SMike Christie 	return 0;
15437996a778SMike Christie }
15447996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_start);
15457996a778SMike Christie 
15467996a778SMike Christie static void
15477996a778SMike Christie flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
15487996a778SMike Christie {
15497996a778SMike Christie 	struct iscsi_mgmt_task *mtask, *tmp;
15507996a778SMike Christie 
15517996a778SMike Christie 	/* handle pending */
15527996a778SMike Christie 	while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
15537996a778SMike Christie 	       __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
15547996a778SMike Christie 		if (mtask == conn->login_mtask)
15557996a778SMike Christie 			continue;
15567996a778SMike Christie 		debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
15577996a778SMike Christie 		__kfifo_put(session->mgmtpool.queue, (void*)&mtask,
15587996a778SMike Christie 			    sizeof(void*));
15597996a778SMike Christie 	}
15607996a778SMike Christie 
15617996a778SMike Christie 	/* handle running */
15627996a778SMike Christie 	list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
15637996a778SMike Christie 		debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
1564ed2abc7fSMike Christie 		list_del(&mtask->running);
1565ed2abc7fSMike Christie 
15667996a778SMike Christie 		if (mtask == conn->login_mtask)
15677996a778SMike Christie 			continue;
1568ed2abc7fSMike Christie 		__kfifo_put(session->mgmtpool.queue, (void*)&mtask,
15697996a778SMike Christie 			   sizeof(void*));
15707996a778SMike Christie 	}
15717996a778SMike Christie 
15727996a778SMike Christie 	conn->mtask = NULL;
15737996a778SMike Christie }
15747996a778SMike Christie 
15757996a778SMike Christie /* Fail commands. Mutex and session lock held and recv side suspended */
15767996a778SMike Christie static void fail_all_commands(struct iscsi_conn *conn)
15777996a778SMike Christie {
15787996a778SMike Christie 	struct iscsi_cmd_task *ctask, *tmp;
15797996a778SMike Christie 
15807996a778SMike Christie 	/* flush pending */
1581b6c395edSMike Christie 	list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
15827996a778SMike Christie 		debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
15837996a778SMike Christie 			   ctask->itt);
15847996a778SMike Christie 		fail_command(conn, ctask, DID_BUS_BUSY << 16);
15857996a778SMike Christie 	}
15867996a778SMike Christie 
15877996a778SMike Christie 	/* fail all other running */
15887996a778SMike Christie 	list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
15897996a778SMike Christie 		debug_scsi("failing in progress sc %p itt 0x%x\n",
15907996a778SMike Christie 			   ctask->sc, ctask->itt);
15917996a778SMike Christie 		fail_command(conn, ctask, DID_BUS_BUSY << 16);
15927996a778SMike Christie 	}
15937996a778SMike Christie 
15947996a778SMike Christie 	conn->ctask = NULL;
15957996a778SMike Christie }
15967996a778SMike Christie 
1597656cffc9SMike Christie static void iscsi_start_session_recovery(struct iscsi_session *session,
15987996a778SMike Christie 					 struct iscsi_conn *conn, int flag)
15997996a778SMike Christie {
1600ed2abc7fSMike Christie 	int old_stop_stage;
1601ed2abc7fSMike Christie 
16027996a778SMike Christie 	spin_lock_bh(&session->lock);
1603ed2abc7fSMike Christie 	if (conn->stop_stage == STOP_CONN_TERM) {
16047996a778SMike Christie 		spin_unlock_bh(&session->lock);
16057996a778SMike Christie 		return;
16067996a778SMike Christie 	}
1607ed2abc7fSMike Christie 
1608ed2abc7fSMike Christie 	/*
1609ed2abc7fSMike Christie 	 * When this is called for the in_login state, we only want to clean
161067a61114SMike Christie 	 * up the login task and connection. We do not need to block and set
161167a61114SMike Christie 	 * the recovery state again
1612ed2abc7fSMike Christie 	 */
161367a61114SMike Christie 	if (flag == STOP_CONN_TERM)
161467a61114SMike Christie 		session->state = ISCSI_STATE_TERMINATE;
161567a61114SMike Christie 	else if (conn->stop_stage != STOP_CONN_RECOVER)
161667a61114SMike Christie 		session->state = ISCSI_STATE_IN_RECOVERY;
1617ed2abc7fSMike Christie 
1618ed2abc7fSMike Christie 	old_stop_stage = conn->stop_stage;
16197996a778SMike Christie 	conn->stop_stage = flag;
162067a61114SMike Christie 	conn->c_stage = ISCSI_CONN_STOPPED;
162167a61114SMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
16227996a778SMike Christie 	spin_unlock_bh(&session->lock);
16237996a778SMike Christie 
16241c83469dSMike Christie 	write_lock_bh(conn->recv_lock);
16251c83469dSMike Christie 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
16261c83469dSMike Christie 	write_unlock_bh(conn->recv_lock);
16277996a778SMike Christie 
16287996a778SMike Christie 	mutex_lock(&conn->xmitmutex);
16297996a778SMike Christie 	/*
16307996a778SMike Christie 	 * for connection level recovery we should not calculate
16317996a778SMike Christie 	 * header digest. conn->hdr_size used for optimization
16327996a778SMike Christie 	 * in hdr_extract() and will be re-negotiated at
16337996a778SMike Christie 	 * set_param() time.
16347996a778SMike Christie 	 */
16357996a778SMike Christie 	if (flag == STOP_CONN_RECOVER) {
16367996a778SMike Christie 		conn->hdrdgst_en = 0;
16377996a778SMike Christie 		conn->datadgst_en = 0;
1638656cffc9SMike Christie 		if (session->state == ISCSI_STATE_IN_RECOVERY &&
163967a61114SMike Christie 		    old_stop_stage != STOP_CONN_RECOVER) {
164067a61114SMike Christie 			debug_scsi("blocking session\n");
16417996a778SMike Christie 			iscsi_block_session(session_to_cls(session));
16427996a778SMike Christie 		}
164367a61114SMike Christie 	}
1644656cffc9SMike Christie 
1645656cffc9SMike Christie 	/*
1646656cffc9SMike Christie 	 * flush queues.
1647656cffc9SMike Christie 	 */
1648656cffc9SMike Christie 	spin_lock_bh(&session->lock);
1649656cffc9SMike Christie 	fail_all_commands(conn);
1650656cffc9SMike Christie 	flush_control_queues(session, conn);
1651656cffc9SMike Christie 	spin_unlock_bh(&session->lock);
1652656cffc9SMike Christie 
16537996a778SMike Christie 	mutex_unlock(&conn->xmitmutex);
16547996a778SMike Christie }
16557996a778SMike Christie 
16567996a778SMike Christie void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
16577996a778SMike Christie {
16587996a778SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
16597996a778SMike Christie 	struct iscsi_session *session = conn->session;
16607996a778SMike Christie 
16617996a778SMike Christie 	switch (flag) {
16627996a778SMike Christie 	case STOP_CONN_RECOVER:
16637996a778SMike Christie 	case STOP_CONN_TERM:
16647996a778SMike Christie 		iscsi_start_session_recovery(session, conn, flag);
16658d2860b3SMike Christie 		break;
16667996a778SMike Christie 	default:
1667be2df72eSOr Gerlitz 		printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
16687996a778SMike Christie 	}
16697996a778SMike Christie }
16707996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_stop);
16717996a778SMike Christie 
16727996a778SMike Christie int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
16737996a778SMike Christie 		    struct iscsi_cls_conn *cls_conn, int is_leading)
16747996a778SMike Christie {
16757996a778SMike Christie 	struct iscsi_session *session = class_to_transport_session(cls_session);
16767996a778SMike Christie 	struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data;
16777996a778SMike Christie 
16787996a778SMike Christie 	/* lookup for existing connection */
16797996a778SMike Christie 	spin_lock_bh(&session->lock);
16807996a778SMike Christie 	list_for_each_entry(tmp, &session->connections, item) {
16817996a778SMike Christie 		if (tmp == conn) {
16827996a778SMike Christie 			if (conn->c_stage != ISCSI_CONN_STOPPED ||
16837996a778SMike Christie 			    conn->stop_stage == STOP_CONN_TERM) {
1684be2df72eSOr Gerlitz 				printk(KERN_ERR "iscsi: can't bind "
16857996a778SMike Christie 				       "non-stopped connection (%d:%d)\n",
16867996a778SMike Christie 				       conn->c_stage, conn->stop_stage);
16877996a778SMike Christie 				spin_unlock_bh(&session->lock);
16887996a778SMike Christie 				return -EIO;
16897996a778SMike Christie 			}
16907996a778SMike Christie 			break;
16917996a778SMike Christie 		}
16927996a778SMike Christie 	}
16937996a778SMike Christie 	if (tmp != conn) {
16947996a778SMike Christie 		/* bind new iSCSI connection to session */
16957996a778SMike Christie 		conn->session = session;
16967996a778SMike Christie 		list_add(&conn->item, &session->connections);
16977996a778SMike Christie 	}
16987996a778SMike Christie 	spin_unlock_bh(&session->lock);
16997996a778SMike Christie 
17007996a778SMike Christie 	if (is_leading)
17017996a778SMike Christie 		session->leadconn = conn;
17027996a778SMike Christie 
17037996a778SMike Christie 	/*
17047996a778SMike Christie 	 * Unblock xmitworker(), Login Phase will pass through.
17057996a778SMike Christie 	 */
17067996a778SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
17077996a778SMike Christie 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
17087996a778SMike Christie 	return 0;
17097996a778SMike Christie }
17107996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_bind);
17117996a778SMike Christie 
1712a54a52caSMike Christie 
1713a54a52caSMike Christie int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1714a54a52caSMike Christie 		    enum iscsi_param param, char *buf, int buflen)
1715a54a52caSMike Christie {
1716a54a52caSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
1717a54a52caSMike Christie 	struct iscsi_session *session = conn->session;
1718a54a52caSMike Christie 	uint32_t value;
1719a54a52caSMike Christie 
1720a54a52caSMike Christie 	switch(param) {
1721a54a52caSMike Christie 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
1722a54a52caSMike Christie 		sscanf(buf, "%d", &conn->max_recv_dlength);
1723a54a52caSMike Christie 		break;
1724a54a52caSMike Christie 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1725a54a52caSMike Christie 		sscanf(buf, "%d", &conn->max_xmit_dlength);
1726a54a52caSMike Christie 		break;
1727a54a52caSMike Christie 	case ISCSI_PARAM_HDRDGST_EN:
1728a54a52caSMike Christie 		sscanf(buf, "%d", &conn->hdrdgst_en);
1729a54a52caSMike Christie 		break;
1730a54a52caSMike Christie 	case ISCSI_PARAM_DATADGST_EN:
1731a54a52caSMike Christie 		sscanf(buf, "%d", &conn->datadgst_en);
1732a54a52caSMike Christie 		break;
1733a54a52caSMike Christie 	case ISCSI_PARAM_INITIAL_R2T_EN:
1734a54a52caSMike Christie 		sscanf(buf, "%d", &session->initial_r2t_en);
1735a54a52caSMike Christie 		break;
1736a54a52caSMike Christie 	case ISCSI_PARAM_MAX_R2T:
1737a54a52caSMike Christie 		sscanf(buf, "%d", &session->max_r2t);
1738a54a52caSMike Christie 		break;
1739a54a52caSMike Christie 	case ISCSI_PARAM_IMM_DATA_EN:
1740a54a52caSMike Christie 		sscanf(buf, "%d", &session->imm_data_en);
1741a54a52caSMike Christie 		break;
1742a54a52caSMike Christie 	case ISCSI_PARAM_FIRST_BURST:
1743a54a52caSMike Christie 		sscanf(buf, "%d", &session->first_burst);
1744a54a52caSMike Christie 		break;
1745a54a52caSMike Christie 	case ISCSI_PARAM_MAX_BURST:
1746a54a52caSMike Christie 		sscanf(buf, "%d", &session->max_burst);
1747a54a52caSMike Christie 		break;
1748a54a52caSMike Christie 	case ISCSI_PARAM_PDU_INORDER_EN:
1749a54a52caSMike Christie 		sscanf(buf, "%d", &session->pdu_inorder_en);
1750a54a52caSMike Christie 		break;
1751a54a52caSMike Christie 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
1752a54a52caSMike Christie 		sscanf(buf, "%d", &session->dataseq_inorder_en);
1753a54a52caSMike Christie 		break;
1754a54a52caSMike Christie 	case ISCSI_PARAM_ERL:
1755a54a52caSMike Christie 		sscanf(buf, "%d", &session->erl);
1756a54a52caSMike Christie 		break;
1757a54a52caSMike Christie 	case ISCSI_PARAM_IFMARKER_EN:
1758a54a52caSMike Christie 		sscanf(buf, "%d", &value);
1759a54a52caSMike Christie 		BUG_ON(value);
1760a54a52caSMike Christie 		break;
1761a54a52caSMike Christie 	case ISCSI_PARAM_OFMARKER_EN:
1762a54a52caSMike Christie 		sscanf(buf, "%d", &value);
1763a54a52caSMike Christie 		BUG_ON(value);
1764a54a52caSMike Christie 		break;
1765a54a52caSMike Christie 	case ISCSI_PARAM_EXP_STATSN:
1766a54a52caSMike Christie 		sscanf(buf, "%u", &conn->exp_statsn);
1767a54a52caSMike Christie 		break;
1768a54a52caSMike Christie 	case ISCSI_PARAM_TARGET_NAME:
1769a54a52caSMike Christie 		/* this should not change between logins */
1770a54a52caSMike Christie 		if (session->targetname)
1771a54a52caSMike Christie 			break;
1772a54a52caSMike Christie 
1773a54a52caSMike Christie 		session->targetname = kstrdup(buf, GFP_KERNEL);
1774a54a52caSMike Christie 		if (!session->targetname)
1775a54a52caSMike Christie 			return -ENOMEM;
1776a54a52caSMike Christie 		break;
1777a54a52caSMike Christie 	case ISCSI_PARAM_TPGT:
1778a54a52caSMike Christie 		sscanf(buf, "%d", &session->tpgt);
1779a54a52caSMike Christie 		break;
1780a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_PORT:
1781a54a52caSMike Christie 		sscanf(buf, "%d", &conn->persistent_port);
1782a54a52caSMike Christie 		break;
1783a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
1784a54a52caSMike Christie 		/*
1785a54a52caSMike Christie 		 * this is the address returned in discovery so it should
1786a54a52caSMike Christie 		 * not change between logins.
1787a54a52caSMike Christie 		 */
1788a54a52caSMike Christie 		if (conn->persistent_address)
1789a54a52caSMike Christie 			break;
1790a54a52caSMike Christie 
1791a54a52caSMike Christie 		conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1792a54a52caSMike Christie 		if (!conn->persistent_address)
1793a54a52caSMike Christie 			return -ENOMEM;
1794a54a52caSMike Christie 		break;
1795a54a52caSMike Christie 	default:
1796a54a52caSMike Christie 		return -ENOSYS;
1797a54a52caSMike Christie 	}
1798a54a52caSMike Christie 
1799a54a52caSMike Christie 	return 0;
1800a54a52caSMike Christie }
1801a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_set_param);
1802a54a52caSMike Christie 
1803a54a52caSMike Christie int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1804a54a52caSMike Christie 			    enum iscsi_param param, char *buf)
1805a54a52caSMike Christie {
1806a54a52caSMike Christie 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1807a54a52caSMike Christie 	struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1808a54a52caSMike Christie 	int len;
1809a54a52caSMike Christie 
1810a54a52caSMike Christie 	switch(param) {
1811a54a52caSMike Christie 	case ISCSI_PARAM_INITIAL_R2T_EN:
1812a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->initial_r2t_en);
1813a54a52caSMike Christie 		break;
1814a54a52caSMike Christie 	case ISCSI_PARAM_MAX_R2T:
1815a54a52caSMike Christie 		len = sprintf(buf, "%hu\n", session->max_r2t);
1816a54a52caSMike Christie 		break;
1817a54a52caSMike Christie 	case ISCSI_PARAM_IMM_DATA_EN:
1818a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->imm_data_en);
1819a54a52caSMike Christie 		break;
1820a54a52caSMike Christie 	case ISCSI_PARAM_FIRST_BURST:
1821a54a52caSMike Christie 		len = sprintf(buf, "%u\n", session->first_burst);
1822a54a52caSMike Christie 		break;
1823a54a52caSMike Christie 	case ISCSI_PARAM_MAX_BURST:
1824a54a52caSMike Christie 		len = sprintf(buf, "%u\n", session->max_burst);
1825a54a52caSMike Christie 		break;
1826a54a52caSMike Christie 	case ISCSI_PARAM_PDU_INORDER_EN:
1827a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->pdu_inorder_en);
1828a54a52caSMike Christie 		break;
1829a54a52caSMike Christie 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
1830a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
1831a54a52caSMike Christie 		break;
1832a54a52caSMike Christie 	case ISCSI_PARAM_ERL:
1833a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->erl);
1834a54a52caSMike Christie 		break;
1835a54a52caSMike Christie 	case ISCSI_PARAM_TARGET_NAME:
1836a54a52caSMike Christie 		len = sprintf(buf, "%s\n", session->targetname);
1837a54a52caSMike Christie 		break;
1838a54a52caSMike Christie 	case ISCSI_PARAM_TPGT:
1839a54a52caSMike Christie 		len = sprintf(buf, "%d\n", session->tpgt);
1840a54a52caSMike Christie 		break;
1841a54a52caSMike Christie 	default:
1842a54a52caSMike Christie 		return -ENOSYS;
1843a54a52caSMike Christie 	}
1844a54a52caSMike Christie 
1845a54a52caSMike Christie 	return len;
1846a54a52caSMike Christie }
1847a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_get_param);
1848a54a52caSMike Christie 
1849a54a52caSMike Christie int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
1850a54a52caSMike Christie 			 enum iscsi_param param, char *buf)
1851a54a52caSMike Christie {
1852a54a52caSMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
1853a54a52caSMike Christie 	int len;
1854a54a52caSMike Christie 
1855a54a52caSMike Christie 	switch(param) {
1856a54a52caSMike Christie 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
1857a54a52caSMike Christie 		len = sprintf(buf, "%u\n", conn->max_recv_dlength);
1858a54a52caSMike Christie 		break;
1859a54a52caSMike Christie 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1860a54a52caSMike Christie 		len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
1861a54a52caSMike Christie 		break;
1862a54a52caSMike Christie 	case ISCSI_PARAM_HDRDGST_EN:
1863a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->hdrdgst_en);
1864a54a52caSMike Christie 		break;
1865a54a52caSMike Christie 	case ISCSI_PARAM_DATADGST_EN:
1866a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->datadgst_en);
1867a54a52caSMike Christie 		break;
1868a54a52caSMike Christie 	case ISCSI_PARAM_IFMARKER_EN:
1869a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->ifmarker_en);
1870a54a52caSMike Christie 		break;
1871a54a52caSMike Christie 	case ISCSI_PARAM_OFMARKER_EN:
1872a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->ofmarker_en);
1873a54a52caSMike Christie 		break;
1874a54a52caSMike Christie 	case ISCSI_PARAM_EXP_STATSN:
1875a54a52caSMike Christie 		len = sprintf(buf, "%u\n", conn->exp_statsn);
1876a54a52caSMike Christie 		break;
1877a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_PORT:
1878a54a52caSMike Christie 		len = sprintf(buf, "%d\n", conn->persistent_port);
1879a54a52caSMike Christie 		break;
1880a54a52caSMike Christie 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
1881a54a52caSMike Christie 		len = sprintf(buf, "%s\n", conn->persistent_address);
1882a54a52caSMike Christie 		break;
1883a54a52caSMike Christie 	default:
1884a54a52caSMike Christie 		return -ENOSYS;
1885a54a52caSMike Christie 	}
1886a54a52caSMike Christie 
1887a54a52caSMike Christie 	return len;
1888a54a52caSMike Christie }
1889a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
1890a54a52caSMike Christie 
18917996a778SMike Christie MODULE_AUTHOR("Mike Christie");
18927996a778SMike Christie MODULE_DESCRIPTION("iSCSI library functions");
18937996a778SMike Christie MODULE_LICENSE("GPL");
1894