17996a778SMike Christie /* 27996a778SMike Christie * iSCSI lib functions 37996a778SMike Christie * 47996a778SMike Christie * Copyright (C) 2006 Red Hat, Inc. All rights reserved. 57996a778SMike Christie * Copyright (C) 2004 - 2006 Mike Christie 67996a778SMike Christie * Copyright (C) 2004 - 2005 Dmitry Yusupov 77996a778SMike Christie * Copyright (C) 2004 - 2005 Alex Aizman 87996a778SMike Christie * maintained by open-iscsi@googlegroups.com 97996a778SMike Christie * 107996a778SMike Christie * This program is free software; you can redistribute it and/or modify 117996a778SMike Christie * it under the terms of the GNU General Public License as published by 127996a778SMike Christie * the Free Software Foundation; either version 2 of the License, or 137996a778SMike Christie * (at your option) any later version. 147996a778SMike Christie * 157996a778SMike Christie * This program is distributed in the hope that it will be useful, 167996a778SMike Christie * but WITHOUT ANY WARRANTY; without even the implied warranty of 177996a778SMike Christie * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 187996a778SMike Christie * GNU General Public License for more details. 197996a778SMike Christie * 207996a778SMike Christie * You should have received a copy of the GNU General Public License 217996a778SMike Christie * along with this program; if not, write to the Free Software 227996a778SMike Christie * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 237996a778SMike Christie */ 247996a778SMike Christie #include <linux/types.h> 257996a778SMike Christie #include <linux/kfifo.h> 267996a778SMike Christie #include <linux/delay.h> 2711836572Svignesh babu #include <linux/log2.h> 288eb00539SMike Christie #include <asm/unaligned.h> 297996a778SMike Christie #include <net/tcp.h> 307996a778SMike Christie #include <scsi/scsi_cmnd.h> 317996a778SMike Christie #include <scsi/scsi_device.h> 327996a778SMike Christie #include <scsi/scsi_eh.h> 337996a778SMike Christie #include <scsi/scsi_tcq.h> 347996a778SMike Christie #include <scsi/scsi_host.h> 357996a778SMike Christie #include <scsi/scsi.h> 367996a778SMike Christie #include <scsi/iscsi_proto.h> 377996a778SMike Christie #include <scsi/scsi_transport.h> 387996a778SMike Christie #include <scsi/scsi_transport_iscsi.h> 397996a778SMike Christie #include <scsi/libiscsi.h> 407996a778SMike Christie 41bd2199d4SErez Zilber static int iscsi_dbg_lib_conn; 42bd2199d4SErez Zilber module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int, 43bd2199d4SErez Zilber S_IRUGO | S_IWUSR); 44bd2199d4SErez Zilber MODULE_PARM_DESC(debug_libiscsi_conn, 45bd2199d4SErez Zilber "Turn on debugging for connections in libiscsi module. " 46bd2199d4SErez Zilber "Set to 1 to turn on, and zero to turn off. Default is off."); 47bd2199d4SErez Zilber 48bd2199d4SErez Zilber static int iscsi_dbg_lib_session; 49bd2199d4SErez Zilber module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int, 50bd2199d4SErez Zilber S_IRUGO | S_IWUSR); 51bd2199d4SErez Zilber MODULE_PARM_DESC(debug_libiscsi_session, 52bd2199d4SErez Zilber "Turn on debugging for sessions in libiscsi module. " 53bd2199d4SErez Zilber "Set to 1 to turn on, and zero to turn off. Default is off."); 54bd2199d4SErez Zilber 55bd2199d4SErez Zilber static int iscsi_dbg_lib_eh; 56bd2199d4SErez Zilber module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int, 57bd2199d4SErez Zilber S_IRUGO | S_IWUSR); 58bd2199d4SErez Zilber MODULE_PARM_DESC(debug_libiscsi_eh, 59bd2199d4SErez Zilber "Turn on debugging for error handling in libiscsi module. " 60bd2199d4SErez Zilber "Set to 1 to turn on, and zero to turn off. Default is off."); 611b2c7af8SMike Christie 621b2c7af8SMike Christie #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \ 631b2c7af8SMike Christie do { \ 64bd2199d4SErez Zilber if (iscsi_dbg_lib_conn) \ 651b2c7af8SMike Christie iscsi_conn_printk(KERN_INFO, _conn, \ 661b2c7af8SMike Christie "%s " dbg_fmt, \ 671b2c7af8SMike Christie __func__, ##arg); \ 681b2c7af8SMike Christie } while (0); 691b2c7af8SMike Christie 701b2c7af8SMike Christie #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \ 711b2c7af8SMike Christie do { \ 72bd2199d4SErez Zilber if (iscsi_dbg_lib_session) \ 73bd2199d4SErez Zilber iscsi_session_printk(KERN_INFO, _session, \ 74bd2199d4SErez Zilber "%s " dbg_fmt, \ 75bd2199d4SErez Zilber __func__, ##arg); \ 76bd2199d4SErez Zilber } while (0); 77bd2199d4SErez Zilber 78bd2199d4SErez Zilber #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \ 79bd2199d4SErez Zilber do { \ 80bd2199d4SErez Zilber if (iscsi_dbg_lib_eh) \ 811b2c7af8SMike Christie iscsi_session_printk(KERN_INFO, _session, \ 821b2c7af8SMike Christie "%s " dbg_fmt, \ 831b2c7af8SMike Christie __func__, ##arg); \ 841b2c7af8SMike Christie } while (0); 851b2c7af8SMike Christie 8677a23c21SMike Christie /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */ 8777a23c21SMike Christie #define SNA32_CHECK 2147483648UL 887996a778SMike Christie 8977a23c21SMike Christie static int iscsi_sna_lt(u32 n1, u32 n2) 9077a23c21SMike Christie { 9177a23c21SMike Christie return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) || 9277a23c21SMike Christie (n1 > n2 && (n2 - n1 < SNA32_CHECK))); 9377a23c21SMike Christie } 9477a23c21SMike Christie 9577a23c21SMike Christie /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */ 9677a23c21SMike Christie static int iscsi_sna_lte(u32 n1, u32 n2) 9777a23c21SMike Christie { 9877a23c21SMike Christie return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) || 9977a23c21SMike Christie (n1 > n2 && (n2 - n1 < SNA32_CHECK))); 10077a23c21SMike Christie } 10177a23c21SMike Christie 10232ae763eSMike Christie inline void iscsi_conn_queue_work(struct iscsi_conn *conn) 10332ae763eSMike Christie { 10432ae763eSMike Christie struct Scsi_Host *shost = conn->session->host; 10532ae763eSMike Christie struct iscsi_host *ihost = shost_priv(shost); 10632ae763eSMike Christie 1071336aed1SMike Christie if (ihost->workq) 10832ae763eSMike Christie queue_work(ihost->workq, &conn->xmitwork); 10932ae763eSMike Christie } 11032ae763eSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_queue_work); 11132ae763eSMike Christie 1124c0ba5d2SMike Christie static void __iscsi_update_cmdsn(struct iscsi_session *session, 1134c0ba5d2SMike Christie uint32_t exp_cmdsn, uint32_t max_cmdsn) 1147996a778SMike Christie { 11577a23c21SMike Christie /* 11677a23c21SMike Christie * standard specifies this check for when to update expected and 11777a23c21SMike Christie * max sequence numbers 11877a23c21SMike Christie */ 11977a23c21SMike Christie if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1)) 12077a23c21SMike Christie return; 12177a23c21SMike Christie 12277a23c21SMike Christie if (exp_cmdsn != session->exp_cmdsn && 12377a23c21SMike Christie !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn)) 1247996a778SMike Christie session->exp_cmdsn = exp_cmdsn; 1257996a778SMike Christie 12677a23c21SMike Christie if (max_cmdsn != session->max_cmdsn && 12777a23c21SMike Christie !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) { 12877a23c21SMike Christie session->max_cmdsn = max_cmdsn; 12977a23c21SMike Christie /* 13077a23c21SMike Christie * if the window closed with IO queued, then kick the 13177a23c21SMike Christie * xmit thread 13277a23c21SMike Christie */ 1333bbaaad9SMike Christie if (!list_empty(&session->leadconn->cmdqueue) || 1341336aed1SMike Christie !list_empty(&session->leadconn->mgmtqueue)) 13532ae763eSMike Christie iscsi_conn_queue_work(session->leadconn); 1367996a778SMike Christie } 13777a23c21SMike Christie } 1384c0ba5d2SMike Christie 1394c0ba5d2SMike Christie void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr) 1404c0ba5d2SMike Christie { 1414c0ba5d2SMike Christie __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn), 1424c0ba5d2SMike Christie be32_to_cpu(hdr->max_cmdsn)); 1434c0ba5d2SMike Christie } 14477a23c21SMike Christie EXPORT_SYMBOL_GPL(iscsi_update_cmdsn); 1457996a778SMike Christie 146577577daSMike Christie /** 147577577daSMike Christie * iscsi_prep_data_out_pdu - initialize Data-Out 148577577daSMike Christie * @task: scsi command task 149577577daSMike Christie * @r2t: R2T info 150577577daSMike Christie * @hdr: iscsi data in pdu 151577577daSMike Christie * 152577577daSMike Christie * Notes: 153577577daSMike Christie * Initialize Data-Out within this R2T sequence and finds 154577577daSMike Christie * proper data_offset within this SCSI command. 155577577daSMike Christie * 156577577daSMike Christie * This function is called with connection lock taken. 157577577daSMike Christie **/ 158577577daSMike Christie void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t, 159ffd0436eSMike Christie struct iscsi_data *hdr) 1607996a778SMike Christie { 1619c19a7d0SMike Christie struct iscsi_conn *conn = task->conn; 162577577daSMike Christie unsigned int left = r2t->data_length - r2t->sent; 163577577daSMike Christie 164577577daSMike Christie task->hdr_len = sizeof(struct iscsi_data); 1657996a778SMike Christie 1667996a778SMike Christie memset(hdr, 0, sizeof(struct iscsi_data)); 167577577daSMike Christie hdr->ttt = r2t->ttt; 168577577daSMike Christie hdr->datasn = cpu_to_be32(r2t->datasn); 169577577daSMike Christie r2t->datasn++; 1707996a778SMike Christie hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; 171577577daSMike Christie memcpy(hdr->lun, task->lun, sizeof(hdr->lun)); 172577577daSMike Christie hdr->itt = task->hdr_itt; 173577577daSMike Christie hdr->exp_statsn = r2t->exp_statsn; 174577577daSMike Christie hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent); 175577577daSMike Christie if (left > conn->max_xmit_dlength) { 1767996a778SMike Christie hton24(hdr->dlength, conn->max_xmit_dlength); 177577577daSMike Christie r2t->data_count = conn->max_xmit_dlength; 1787996a778SMike Christie hdr->flags = 0; 1797996a778SMike Christie } else { 180577577daSMike Christie hton24(hdr->dlength, left); 181577577daSMike Christie r2t->data_count = left; 1827996a778SMike Christie hdr->flags = ISCSI_FLAG_CMD_FINAL; 1837996a778SMike Christie } 184577577daSMike Christie conn->dataout_pdus_cnt++; 1857996a778SMike Christie } 186577577daSMike Christie EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu); 1877996a778SMike Christie 1889c19a7d0SMike Christie static int iscsi_add_hdr(struct iscsi_task *task, unsigned len) 189004d6530SBoaz Harrosh { 1909c19a7d0SMike Christie unsigned exp_len = task->hdr_len + len; 191004d6530SBoaz Harrosh 1929c19a7d0SMike Christie if (exp_len > task->hdr_max) { 193004d6530SBoaz Harrosh WARN_ON(1); 194004d6530SBoaz Harrosh return -EINVAL; 195004d6530SBoaz Harrosh } 196004d6530SBoaz Harrosh 197004d6530SBoaz Harrosh WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */ 1989c19a7d0SMike Christie task->hdr_len = exp_len; 199004d6530SBoaz Harrosh return 0; 200004d6530SBoaz Harrosh } 201004d6530SBoaz Harrosh 20238d1c069SBoaz Harrosh /* 20338d1c069SBoaz Harrosh * make an extended cdb AHS 20438d1c069SBoaz Harrosh */ 2059c19a7d0SMike Christie static int iscsi_prep_ecdb_ahs(struct iscsi_task *task) 20638d1c069SBoaz Harrosh { 2079c19a7d0SMike Christie struct scsi_cmnd *cmd = task->sc; 20838d1c069SBoaz Harrosh unsigned rlen, pad_len; 20938d1c069SBoaz Harrosh unsigned short ahslength; 21038d1c069SBoaz Harrosh struct iscsi_ecdb_ahdr *ecdb_ahdr; 21138d1c069SBoaz Harrosh int rc; 21238d1c069SBoaz Harrosh 2139c19a7d0SMike Christie ecdb_ahdr = iscsi_next_hdr(task); 21438d1c069SBoaz Harrosh rlen = cmd->cmd_len - ISCSI_CDB_SIZE; 21538d1c069SBoaz Harrosh 21638d1c069SBoaz Harrosh BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb)); 21738d1c069SBoaz Harrosh ahslength = rlen + sizeof(ecdb_ahdr->reserved); 21838d1c069SBoaz Harrosh 21938d1c069SBoaz Harrosh pad_len = iscsi_padding(rlen); 22038d1c069SBoaz Harrosh 2219c19a7d0SMike Christie rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) + 22238d1c069SBoaz Harrosh sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len); 22338d1c069SBoaz Harrosh if (rc) 22438d1c069SBoaz Harrosh return rc; 22538d1c069SBoaz Harrosh 22638d1c069SBoaz Harrosh if (pad_len) 22738d1c069SBoaz Harrosh memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len); 22838d1c069SBoaz Harrosh 22938d1c069SBoaz Harrosh ecdb_ahdr->ahslength = cpu_to_be16(ahslength); 23038d1c069SBoaz Harrosh ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB; 23138d1c069SBoaz Harrosh ecdb_ahdr->reserved = 0; 23238d1c069SBoaz Harrosh memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen); 23338d1c069SBoaz Harrosh 2341b2c7af8SMike Christie ISCSI_DBG_SESSION(task->conn->session, 2351b2c7af8SMike Christie "iscsi_prep_ecdb_ahs: varlen_cdb_len %d " 2361b2c7af8SMike Christie "rlen %d pad_len %d ahs_length %d iscsi_headers_size " 2371b2c7af8SMike Christie "%u\n", cmd->cmd_len, rlen, pad_len, ahslength, 2381b2c7af8SMike Christie task->hdr_len); 23938d1c069SBoaz Harrosh return 0; 24038d1c069SBoaz Harrosh } 24138d1c069SBoaz Harrosh 2429c19a7d0SMike Christie static int iscsi_prep_bidi_ahs(struct iscsi_task *task) 243c07d4444SBoaz Harrosh { 2449c19a7d0SMike Christie struct scsi_cmnd *sc = task->sc; 245c07d4444SBoaz Harrosh struct iscsi_rlength_ahdr *rlen_ahdr; 246c07d4444SBoaz Harrosh int rc; 247c07d4444SBoaz Harrosh 2489c19a7d0SMike Christie rlen_ahdr = iscsi_next_hdr(task); 2499c19a7d0SMike Christie rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr)); 250c07d4444SBoaz Harrosh if (rc) 251c07d4444SBoaz Harrosh return rc; 252c07d4444SBoaz Harrosh 253c07d4444SBoaz Harrosh rlen_ahdr->ahslength = 254c07d4444SBoaz Harrosh cpu_to_be16(sizeof(rlen_ahdr->read_length) + 255c07d4444SBoaz Harrosh sizeof(rlen_ahdr->reserved)); 256c07d4444SBoaz Harrosh rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH; 257c07d4444SBoaz Harrosh rlen_ahdr->reserved = 0; 258c07d4444SBoaz Harrosh rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length); 259c07d4444SBoaz Harrosh 2601b2c7af8SMike Christie ISCSI_DBG_SESSION(task->conn->session, 2611b2c7af8SMike Christie "bidi-in rlen_ahdr->read_length(%d) " 262c07d4444SBoaz Harrosh "rlen_ahdr->ahslength(%d)\n", 263c07d4444SBoaz Harrosh be32_to_cpu(rlen_ahdr->read_length), 264c07d4444SBoaz Harrosh be16_to_cpu(rlen_ahdr->ahslength)); 265c07d4444SBoaz Harrosh return 0; 266c07d4444SBoaz Harrosh } 267c07d4444SBoaz Harrosh 2687996a778SMike Christie /** 2697996a778SMike Christie * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu 2709c19a7d0SMike Christie * @task: iscsi task 2717996a778SMike Christie * 2727996a778SMike Christie * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set 2737996a778SMike Christie * fields like dlength or final based on how much data it sends 2747996a778SMike Christie */ 2759c19a7d0SMike Christie static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task) 2767996a778SMike Christie { 2779c19a7d0SMike Christie struct iscsi_conn *conn = task->conn; 2787996a778SMike Christie struct iscsi_session *session = conn->session; 2799c19a7d0SMike Christie struct scsi_cmnd *sc = task->sc; 280577577daSMike Christie struct iscsi_cmd *hdr; 28138d1c069SBoaz Harrosh unsigned hdrlength, cmd_len; 282262ef636SMike Christie itt_t itt; 283004d6530SBoaz Harrosh int rc; 2847996a778SMike Christie 285184b57c6SMike Christie if (conn->session->tt->alloc_pdu) { 2862ff79d52SMike Christie rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD); 287577577daSMike Christie if (rc) 288577577daSMike Christie return rc; 289184b57c6SMike Christie } 290577577daSMike Christie hdr = (struct iscsi_cmd *) task->hdr; 291262ef636SMike Christie itt = hdr->itt; 292577577daSMike Christie memset(hdr, 0, sizeof(*hdr)); 293577577daSMike Christie 2942ff79d52SMike Christie if (session->tt->parse_pdu_itt) 2952ff79d52SMike Christie hdr->itt = task->hdr_itt = itt; 2962ff79d52SMike Christie else 2972ff79d52SMike Christie hdr->itt = task->hdr_itt = build_itt(task->itt, 2982ff79d52SMike Christie task->conn->session->age); 2999c19a7d0SMike Christie task->hdr_len = 0; 3009c19a7d0SMike Christie rc = iscsi_add_hdr(task, sizeof(*hdr)); 301004d6530SBoaz Harrosh if (rc) 302004d6530SBoaz Harrosh return rc; 3037996a778SMike Christie hdr->opcode = ISCSI_OP_SCSI_CMD; 3047996a778SMike Christie hdr->flags = ISCSI_ATTR_SIMPLE; 3057996a778SMike Christie int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun); 306577577daSMike Christie memcpy(task->lun, hdr->lun, sizeof(task->lun)); 3077996a778SMike Christie hdr->exp_statsn = cpu_to_be32(conn->exp_statsn); 30838d1c069SBoaz Harrosh cmd_len = sc->cmd_len; 30938d1c069SBoaz Harrosh if (cmd_len < ISCSI_CDB_SIZE) 31038d1c069SBoaz Harrosh memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len); 31138d1c069SBoaz Harrosh else if (cmd_len > ISCSI_CDB_SIZE) { 3129c19a7d0SMike Christie rc = iscsi_prep_ecdb_ahs(task); 31338d1c069SBoaz Harrosh if (rc) 31438d1c069SBoaz Harrosh return rc; 31538d1c069SBoaz Harrosh cmd_len = ISCSI_CDB_SIZE; 31638d1c069SBoaz Harrosh } 31738d1c069SBoaz Harrosh memcpy(hdr->cdb, sc->cmnd, cmd_len); 3187996a778SMike Christie 3199c19a7d0SMike Christie task->imm_count = 0; 320c07d4444SBoaz Harrosh if (scsi_bidi_cmnd(sc)) { 321c07d4444SBoaz Harrosh hdr->flags |= ISCSI_FLAG_CMD_READ; 3229c19a7d0SMike Christie rc = iscsi_prep_bidi_ahs(task); 323c07d4444SBoaz Harrosh if (rc) 324c07d4444SBoaz Harrosh return rc; 325c07d4444SBoaz Harrosh } 3267996a778SMike Christie if (sc->sc_data_direction == DMA_TO_DEVICE) { 327c07d4444SBoaz Harrosh unsigned out_len = scsi_out(sc)->length; 328577577daSMike Christie struct iscsi_r2t_info *r2t = &task->unsol_r2t; 329577577daSMike Christie 330c07d4444SBoaz Harrosh hdr->data_length = cpu_to_be32(out_len); 3317996a778SMike Christie hdr->flags |= ISCSI_FLAG_CMD_WRITE; 3327996a778SMike Christie /* 3337996a778SMike Christie * Write counters: 3347996a778SMike Christie * 3357996a778SMike Christie * imm_count bytes to be sent right after 3367996a778SMike Christie * SCSI PDU Header 3377996a778SMike Christie * 3387996a778SMike Christie * unsol_count bytes(as Data-Out) to be sent 3397996a778SMike Christie * without R2T ack right after 3407996a778SMike Christie * immediate data 3417996a778SMike Christie * 342577577daSMike Christie * r2t data_length bytes to be sent via R2T ack's 3437996a778SMike Christie * 3447996a778SMike Christie * pad_count bytes to be sent as zero-padding 3457996a778SMike Christie */ 346577577daSMike Christie memset(r2t, 0, sizeof(*r2t)); 3477996a778SMike Christie 3487996a778SMike Christie if (session->imm_data_en) { 349c07d4444SBoaz Harrosh if (out_len >= session->first_burst) 3509c19a7d0SMike Christie task->imm_count = min(session->first_burst, 3517996a778SMike Christie conn->max_xmit_dlength); 3527996a778SMike Christie else 3539c19a7d0SMike Christie task->imm_count = min(out_len, 3547996a778SMike Christie conn->max_xmit_dlength); 3559c19a7d0SMike Christie hton24(hdr->dlength, task->imm_count); 3567996a778SMike Christie } else 357a8ac6311SOlaf Kirch zero_data(hdr->dlength); 3587996a778SMike Christie 359ffd0436eSMike Christie if (!session->initial_r2t_en) { 360577577daSMike Christie r2t->data_length = min(session->first_burst, out_len) - 361577577daSMike Christie task->imm_count; 362577577daSMike Christie r2t->data_offset = task->imm_count; 363577577daSMike Christie r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG); 364577577daSMike Christie r2t->exp_statsn = cpu_to_be32(conn->exp_statsn); 365ffd0436eSMike Christie } 366ffd0436eSMike Christie 367577577daSMike Christie if (!task->unsol_r2t.data_length) 3687996a778SMike Christie /* No unsolicit Data-Out's */ 369a8ac6311SOlaf Kirch hdr->flags |= ISCSI_FLAG_CMD_FINAL; 3707996a778SMike Christie } else { 3717996a778SMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 3727996a778SMike Christie zero_data(hdr->dlength); 373c07d4444SBoaz Harrosh hdr->data_length = cpu_to_be32(scsi_in(sc)->length); 3747996a778SMike Christie 3757996a778SMike Christie if (sc->sc_data_direction == DMA_FROM_DEVICE) 3767996a778SMike Christie hdr->flags |= ISCSI_FLAG_CMD_READ; 3777996a778SMike Christie } 3787996a778SMike Christie 379004d6530SBoaz Harrosh /* calculate size of additional header segments (AHSs) */ 3809c19a7d0SMike Christie hdrlength = task->hdr_len - sizeof(*hdr); 381004d6530SBoaz Harrosh 382004d6530SBoaz Harrosh WARN_ON(hdrlength & (ISCSI_PAD_LEN-1)); 383004d6530SBoaz Harrosh hdrlength /= ISCSI_PAD_LEN; 384004d6530SBoaz Harrosh 385004d6530SBoaz Harrosh WARN_ON(hdrlength >= 256); 386004d6530SBoaz Harrosh hdr->hlength = hdrlength & 0xFF; 387004d6530SBoaz Harrosh 388577577daSMike Christie if (session->tt->init_task && session->tt->init_task(task)) 389052d0144SMike Christie return -EIO; 390052d0144SMike Christie 3919c19a7d0SMike Christie task->state = ISCSI_TASK_RUNNING; 392d3305f34SMike Christie hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn); 393d3305f34SMike Christie session->cmdsn++; 39477a23c21SMike Christie 395a8ac6311SOlaf Kirch conn->scsicmd_pdus_cnt++; 3961b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x " 3971b2c7af8SMike Christie "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n", 3981b2c7af8SMike Christie scsi_bidi_cmnd(sc) ? "bidirectional" : 3991b2c7af8SMike Christie sc->sc_data_direction == DMA_TO_DEVICE ? 4001b2c7af8SMike Christie "write" : "read", conn->id, sc, sc->cmnd[0], 4011b2c7af8SMike Christie task->itt, scsi_bufflen(sc), 4023e5c28adSMike Christie scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0, 4031b2c7af8SMike Christie session->cmdsn, 4041b2c7af8SMike Christie session->max_cmdsn - session->exp_cmdsn + 1); 405004d6530SBoaz Harrosh return 0; 4067996a778SMike Christie } 4077996a778SMike Christie 4087996a778SMike Christie /** 4093bbaaad9SMike Christie * iscsi_free_task - free a task 4109c19a7d0SMike Christie * @task: iscsi cmd task 4117996a778SMike Christie * 4127996a778SMike Christie * Must be called with session lock. 4133e5c28adSMike Christie * This function returns the scsi command to scsi-ml or cleans 4143e5c28adSMike Christie * up mgmt tasks then returns the task to the pool. 4157996a778SMike Christie */ 4163bbaaad9SMike Christie static void iscsi_free_task(struct iscsi_task *task) 4177996a778SMike Christie { 4189c19a7d0SMike Christie struct iscsi_conn *conn = task->conn; 419c1635cb7SMike Christie struct iscsi_session *session = conn->session; 4209c19a7d0SMike Christie struct scsi_cmnd *sc = task->sc; 4217996a778SMike Christie 4224421c9ebSMike Christie ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n", 4234421c9ebSMike Christie task->itt, task->state, task->sc); 4244421c9ebSMike Christie 425577577daSMike Christie session->tt->cleanup_task(task); 4263bbaaad9SMike Christie task->state = ISCSI_TASK_FREE; 4279c19a7d0SMike Christie task->sc = NULL; 4283e5c28adSMike Christie /* 4299c19a7d0SMike Christie * login task is preallocated so do not free 4303e5c28adSMike Christie */ 4319c19a7d0SMike Christie if (conn->login_task == task) 4323e5c28adSMike Christie return; 4333e5c28adSMike Christie 4349c19a7d0SMike Christie __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*)); 4353e5c28adSMike Christie 4363e5c28adSMike Christie if (sc) { 4379c19a7d0SMike Christie task->sc = NULL; 438f47f2cf5SMike Christie /* SCSI eh reuses commands to verify us */ 439f47f2cf5SMike Christie sc->SCp.ptr = NULL; 4403e5c28adSMike Christie /* 4413e5c28adSMike Christie * queue command may call this to free the task, but 4423e5c28adSMike Christie * not have setup the sc callback 4433e5c28adSMike Christie */ 444052d0144SMike Christie if (sc->scsi_done) 4457996a778SMike Christie sc->scsi_done(sc); 4467996a778SMike Christie } 4473e5c28adSMike Christie } 4487996a778SMike Christie 449913e5bf4SMike Christie void __iscsi_get_task(struct iscsi_task *task) 45060ecebf5SMike Christie { 4519c19a7d0SMike Christie atomic_inc(&task->refcount); 45260ecebf5SMike Christie } 453913e5bf4SMike Christie EXPORT_SYMBOL_GPL(__iscsi_get_task); 45460ecebf5SMike Christie 4559c19a7d0SMike Christie static void __iscsi_put_task(struct iscsi_task *task) 45660ecebf5SMike Christie { 4579c19a7d0SMike Christie if (atomic_dec_and_test(&task->refcount)) 4583bbaaad9SMike Christie iscsi_free_task(task); 45960ecebf5SMike Christie } 46060ecebf5SMike Christie 4619c19a7d0SMike Christie void iscsi_put_task(struct iscsi_task *task) 4623e5c28adSMike Christie { 4639c19a7d0SMike Christie struct iscsi_session *session = task->conn->session; 4643e5c28adSMike Christie 4653e5c28adSMike Christie spin_lock_bh(&session->lock); 4669c19a7d0SMike Christie __iscsi_put_task(task); 4673e5c28adSMike Christie spin_unlock_bh(&session->lock); 4683e5c28adSMike Christie } 4699c19a7d0SMike Christie EXPORT_SYMBOL_GPL(iscsi_put_task); 4703e5c28adSMike Christie 4713bbaaad9SMike Christie /** 4723bbaaad9SMike Christie * iscsi_complete_task - finish a task 4733bbaaad9SMike Christie * @task: iscsi cmd task 474b3cd5050SMike Christie * @state: state to complete task with 4753bbaaad9SMike Christie * 4763bbaaad9SMike Christie * Must be called with session lock. 477b3a7ea8dSMike Christie */ 478b3cd5050SMike Christie static void iscsi_complete_task(struct iscsi_task *task, int state) 479b3a7ea8dSMike Christie { 4803bbaaad9SMike Christie struct iscsi_conn *conn = task->conn; 4813bbaaad9SMike Christie 4824421c9ebSMike Christie ISCSI_DBG_SESSION(conn->session, 4834421c9ebSMike Christie "complete task itt 0x%x state %d sc %p\n", 4844421c9ebSMike Christie task->itt, task->state, task->sc); 485b3cd5050SMike Christie if (task->state == ISCSI_TASK_COMPLETED || 486b3cd5050SMike Christie task->state == ISCSI_TASK_ABRT_TMF || 487b3cd5050SMike Christie task->state == ISCSI_TASK_ABRT_SESS_RECOV) 4883bbaaad9SMike Christie return; 4893bbaaad9SMike Christie WARN_ON_ONCE(task->state == ISCSI_TASK_FREE); 490b3cd5050SMike Christie task->state = state; 4913bbaaad9SMike Christie 4923bbaaad9SMike Christie if (!list_empty(&task->running)) 4933bbaaad9SMike Christie list_del_init(&task->running); 4943bbaaad9SMike Christie 4953bbaaad9SMike Christie if (conn->task == task) 4963bbaaad9SMike Christie conn->task = NULL; 4973bbaaad9SMike Christie 4983bbaaad9SMike Christie if (conn->ping_task == task) 4993bbaaad9SMike Christie conn->ping_task = NULL; 5003bbaaad9SMike Christie 5013bbaaad9SMike Christie /* release get from queueing */ 5023bbaaad9SMike Christie __iscsi_put_task(task); 5033bbaaad9SMike Christie } 5043bbaaad9SMike Christie 5054c0ba5d2SMike Christie /** 5064c0ba5d2SMike Christie * iscsi_complete_scsi_task - finish scsi task normally 5074c0ba5d2SMike Christie * @task: iscsi task for scsi cmd 5084c0ba5d2SMike Christie * @exp_cmdsn: expected cmd sn in cpu format 5094c0ba5d2SMike Christie * @max_cmdsn: max cmd sn in cpu format 5104c0ba5d2SMike Christie * 5114c0ba5d2SMike Christie * This is used when drivers do not need or cannot perform 5124c0ba5d2SMike Christie * lower level pdu processing. 5134c0ba5d2SMike Christie * 5144c0ba5d2SMike Christie * Called with session lock 5154c0ba5d2SMike Christie */ 5164c0ba5d2SMike Christie void iscsi_complete_scsi_task(struct iscsi_task *task, 5174c0ba5d2SMike Christie uint32_t exp_cmdsn, uint32_t max_cmdsn) 5184c0ba5d2SMike Christie { 5194c0ba5d2SMike Christie struct iscsi_conn *conn = task->conn; 5204c0ba5d2SMike Christie 5214c0ba5d2SMike Christie ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt); 5224c0ba5d2SMike Christie 5234c0ba5d2SMike Christie conn->last_recv = jiffies; 5244c0ba5d2SMike Christie __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn); 5254c0ba5d2SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 5264c0ba5d2SMike Christie } 5274c0ba5d2SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task); 5284c0ba5d2SMike Christie 5294c0ba5d2SMike Christie 5303bbaaad9SMike Christie /* 5313bbaaad9SMike Christie * session lock must be held and if not called for a task that is 5323bbaaad9SMike Christie * still pending or from the xmit thread, then xmit thread must 5333bbaaad9SMike Christie * be suspended. 5343bbaaad9SMike Christie */ 5353bbaaad9SMike Christie static void fail_scsi_task(struct iscsi_task *task, int err) 5363bbaaad9SMike Christie { 5373bbaaad9SMike Christie struct iscsi_conn *conn = task->conn; 538b3a7ea8dSMike Christie struct scsi_cmnd *sc; 539b3cd5050SMike Christie int state; 540b3a7ea8dSMike Christie 5413bbaaad9SMike Christie /* 5423bbaaad9SMike Christie * if a command completes and we get a successful tmf response 5433bbaaad9SMike Christie * we will hit this because the scsi eh abort code does not take 5443bbaaad9SMike Christie * a ref to the task. 5453bbaaad9SMike Christie */ 5469c19a7d0SMike Christie sc = task->sc; 547b3a7ea8dSMike Christie if (!sc) 548b3a7ea8dSMike Christie return; 549b3a7ea8dSMike Christie 550b3cd5050SMike Christie if (task->state == ISCSI_TASK_PENDING) { 551b3a7ea8dSMike Christie /* 552b3a7ea8dSMike Christie * cmd never made it to the xmit thread, so we should not count 553b3a7ea8dSMike Christie * the cmd in the sequencing 554b3a7ea8dSMike Christie */ 555b3a7ea8dSMike Christie conn->session->queued_cmdsn--; 556b3cd5050SMike Christie /* it was never sent so just complete like normal */ 557b3cd5050SMike Christie state = ISCSI_TASK_COMPLETED; 558b3cd5050SMike Christie } else if (err == DID_TRANSPORT_DISRUPTED) 559b3cd5050SMike Christie state = ISCSI_TASK_ABRT_SESS_RECOV; 560b3cd5050SMike Christie else 561b3cd5050SMike Christie state = ISCSI_TASK_ABRT_TMF; 562b3a7ea8dSMike Christie 563b3cd5050SMike Christie sc->result = err << 16; 564c07d4444SBoaz Harrosh if (!scsi_bidi_cmnd(sc)) 565b3a7ea8dSMike Christie scsi_set_resid(sc, scsi_bufflen(sc)); 566c07d4444SBoaz Harrosh else { 567c07d4444SBoaz Harrosh scsi_out(sc)->resid = scsi_out(sc)->length; 568c07d4444SBoaz Harrosh scsi_in(sc)->resid = scsi_in(sc)->length; 569c07d4444SBoaz Harrosh } 5703e5c28adSMike Christie 571b3cd5050SMike Christie iscsi_complete_task(task, state); 572b3a7ea8dSMike Christie } 573b3a7ea8dSMike Christie 5743e5c28adSMike Christie static int iscsi_prep_mgmt_task(struct iscsi_conn *conn, 5759c19a7d0SMike Christie struct iscsi_task *task) 576052d0144SMike Christie { 577052d0144SMike Christie struct iscsi_session *session = conn->session; 578577577daSMike Christie struct iscsi_hdr *hdr = task->hdr; 579052d0144SMike Christie struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr; 580*4f704dc0SMike Christie uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK; 581052d0144SMike Christie 582052d0144SMike Christie if (conn->session->state == ISCSI_STATE_LOGGING_OUT) 583052d0144SMike Christie return -ENOTCONN; 584052d0144SMike Christie 585*4f704dc0SMike Christie if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT) 586052d0144SMike Christie nop->exp_statsn = cpu_to_be32(conn->exp_statsn); 587052d0144SMike Christie /* 588052d0144SMike Christie * pre-format CmdSN for outgoing PDU. 589052d0144SMike Christie */ 590052d0144SMike Christie nop->cmdsn = cpu_to_be32(session->cmdsn); 591052d0144SMike Christie if (hdr->itt != RESERVED_ITT) { 592052d0144SMike Christie /* 593*4f704dc0SMike Christie * TODO: We always use immediate for normal session pdus. 594052d0144SMike Christie * If we start to send tmfs or nops as non-immediate then 595052d0144SMike Christie * we should start checking the cmdsn numbers for mgmt tasks. 596*4f704dc0SMike Christie * 597*4f704dc0SMike Christie * During discovery sessions iscsid sends TEXT as non immediate, 598*4f704dc0SMike Christie * but we always only send one PDU at a time. 599052d0144SMike Christie */ 600052d0144SMike Christie if (conn->c_stage == ISCSI_CONN_STARTED && 601052d0144SMike Christie !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { 602052d0144SMike Christie session->queued_cmdsn++; 603052d0144SMike Christie session->cmdsn++; 604052d0144SMike Christie } 605052d0144SMike Christie } 606052d0144SMike Christie 607ae15f801SMike Christie if (session->tt->init_task && session->tt->init_task(task)) 608ae15f801SMike Christie return -EIO; 609052d0144SMike Christie 610052d0144SMike Christie if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) 611052d0144SMike Christie session->state = ISCSI_STATE_LOGGING_OUT; 612052d0144SMike Christie 613577577daSMike Christie task->state = ISCSI_TASK_RUNNING; 6141b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x " 6151b2c7af8SMike Christie "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK, 6161b2c7af8SMike Christie hdr->itt, task->data_count); 617052d0144SMike Christie return 0; 618052d0144SMike Christie } 619052d0144SMike Christie 6209c19a7d0SMike Christie static struct iscsi_task * 621f6d5180cSMike Christie __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 622f6d5180cSMike Christie char *data, uint32_t data_size) 623f6d5180cSMike Christie { 624f6d5180cSMike Christie struct iscsi_session *session = conn->session; 6251336aed1SMike Christie struct iscsi_host *ihost = shost_priv(session->host); 626*4f704dc0SMike Christie uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK; 6279c19a7d0SMike Christie struct iscsi_task *task; 628262ef636SMike Christie itt_t itt; 629f6d5180cSMike Christie 630f6d5180cSMike Christie if (session->state == ISCSI_STATE_TERMINATE) 631f6d5180cSMike Christie return NULL; 632f6d5180cSMike Christie 633*4f704dc0SMike Christie if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) { 634f6d5180cSMike Christie /* 635f6d5180cSMike Christie * Login and Text are sent serially, in 636f6d5180cSMike Christie * request-followed-by-response sequence. 6373e5c28adSMike Christie * Same task can be used. Same ITT must be used. 6383e5c28adSMike Christie * Note that login_task is preallocated at conn_create(). 639f6d5180cSMike Christie */ 640*4f704dc0SMike Christie if (conn->login_task->state != ISCSI_TASK_FREE) { 641*4f704dc0SMike Christie iscsi_conn_printk(KERN_ERR, conn, "Login/Text in " 642*4f704dc0SMike Christie "progress. Cannot start new task.\n"); 643*4f704dc0SMike Christie return NULL; 644*4f704dc0SMike Christie } 645*4f704dc0SMike Christie 6469c19a7d0SMike Christie task = conn->login_task; 647*4f704dc0SMike Christie } else { 64826013ad4SMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) 64926013ad4SMike Christie return NULL; 65026013ad4SMike Christie 651f6d5180cSMike Christie BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); 652f6d5180cSMike Christie BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); 653f6d5180cSMike Christie 6543e5c28adSMike Christie if (!__kfifo_get(session->cmdpool.queue, 6559c19a7d0SMike Christie (void*)&task, sizeof(void*))) 656f6d5180cSMike Christie return NULL; 657f6d5180cSMike Christie } 6583e5c28adSMike Christie /* 6593e5c28adSMike Christie * released in complete pdu for task we expect a response for, and 6603e5c28adSMike Christie * released by the lld when it has transmitted the task for 6613e5c28adSMike Christie * pdus we do not expect a response for. 6623e5c28adSMike Christie */ 6639c19a7d0SMike Christie atomic_set(&task->refcount, 1); 6649c19a7d0SMike Christie task->conn = conn; 6659c19a7d0SMike Christie task->sc = NULL; 6663bbaaad9SMike Christie INIT_LIST_HEAD(&task->running); 6673bbaaad9SMike Christie task->state = ISCSI_TASK_PENDING; 668f6d5180cSMike Christie 669f6d5180cSMike Christie if (data_size) { 6709c19a7d0SMike Christie memcpy(task->data, data, data_size); 6719c19a7d0SMike Christie task->data_count = data_size; 672f6d5180cSMike Christie } else 6739c19a7d0SMike Christie task->data_count = 0; 674f6d5180cSMike Christie 675184b57c6SMike Christie if (conn->session->tt->alloc_pdu) { 6762ff79d52SMike Christie if (conn->session->tt->alloc_pdu(task, hdr->opcode)) { 677577577daSMike Christie iscsi_conn_printk(KERN_ERR, conn, "Could not allocate " 678577577daSMike Christie "pdu for mgmt task.\n"); 6793bbaaad9SMike Christie goto free_task; 680577577daSMike Christie } 681184b57c6SMike Christie } 682184b57c6SMike Christie 683262ef636SMike Christie itt = task->hdr->itt; 684577577daSMike Christie task->hdr_len = sizeof(struct iscsi_hdr); 6859c19a7d0SMike Christie memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr)); 686262ef636SMike Christie 687262ef636SMike Christie if (hdr->itt != RESERVED_ITT) { 688262ef636SMike Christie if (session->tt->parse_pdu_itt) 689262ef636SMike Christie task->hdr->itt = itt; 690262ef636SMike Christie else 691262ef636SMike Christie task->hdr->itt = build_itt(task->itt, 692262ef636SMike Christie task->conn->session->age); 693262ef636SMike Christie } 694262ef636SMike Christie 6951336aed1SMike Christie if (!ihost->workq) { 696577577daSMike Christie if (iscsi_prep_mgmt_task(conn, task)) 697577577daSMike Christie goto free_task; 698052d0144SMike Christie 6999c19a7d0SMike Christie if (session->tt->xmit_task(task)) 700577577daSMike Christie goto free_task; 7013bbaaad9SMike Christie } else { 7023bbaaad9SMike Christie list_add_tail(&task->running, &conn->mgmtqueue); 70332ae763eSMike Christie iscsi_conn_queue_work(conn); 7043bbaaad9SMike Christie } 705052d0144SMike Christie 7069c19a7d0SMike Christie return task; 707577577daSMike Christie 708577577daSMike Christie free_task: 709577577daSMike Christie __iscsi_put_task(task); 710577577daSMike Christie return NULL; 711f6d5180cSMike Christie } 712f6d5180cSMike Christie 713f6d5180cSMike Christie int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr, 714f6d5180cSMike Christie char *data, uint32_t data_size) 715f6d5180cSMike Christie { 716f6d5180cSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 717f6d5180cSMike Christie struct iscsi_session *session = conn->session; 718f6d5180cSMike Christie int err = 0; 719f6d5180cSMike Christie 720f6d5180cSMike Christie spin_lock_bh(&session->lock); 721f6d5180cSMike Christie if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size)) 722f6d5180cSMike Christie err = -EPERM; 723f6d5180cSMike Christie spin_unlock_bh(&session->lock); 724f6d5180cSMike Christie return err; 725f6d5180cSMike Christie } 726f6d5180cSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu); 727f6d5180cSMike Christie 7287996a778SMike Christie /** 7297996a778SMike Christie * iscsi_cmd_rsp - SCSI Command Response processing 7307996a778SMike Christie * @conn: iscsi connection 7317996a778SMike Christie * @hdr: iscsi header 7329c19a7d0SMike Christie * @task: scsi command task 7337996a778SMike Christie * @data: cmd data buffer 7347996a778SMike Christie * @datalen: len of buffer 7357996a778SMike Christie * 7367996a778SMike Christie * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and 7379c19a7d0SMike Christie * then completes the command and task. 7387996a778SMike Christie **/ 73977a23c21SMike Christie static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 7409c19a7d0SMike Christie struct iscsi_task *task, char *data, 7417996a778SMike Christie int datalen) 7427996a778SMike Christie { 7437996a778SMike Christie struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr; 7447996a778SMike Christie struct iscsi_session *session = conn->session; 7459c19a7d0SMike Christie struct scsi_cmnd *sc = task->sc; 7467996a778SMike Christie 74777a23c21SMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); 7487996a778SMike Christie conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; 7497996a778SMike Christie 7507996a778SMike Christie sc->result = (DID_OK << 16) | rhdr->cmd_status; 7517996a778SMike Christie 7527996a778SMike Christie if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) { 7537996a778SMike Christie sc->result = DID_ERROR << 16; 7547996a778SMike Christie goto out; 7557996a778SMike Christie } 7567996a778SMike Christie 7577996a778SMike Christie if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) { 7589b80cb4bSMike Christie uint16_t senselen; 7597996a778SMike Christie 7607996a778SMike Christie if (datalen < 2) { 7617996a778SMike Christie invalid_datalen: 762322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 763322d739dSMike Christie "Got CHECK_CONDITION but invalid data " 764322d739dSMike Christie "buffer size of %d\n", datalen); 7657996a778SMike Christie sc->result = DID_BAD_TARGET << 16; 7667996a778SMike Christie goto out; 7677996a778SMike Christie } 7687996a778SMike Christie 7698f333991SHarvey Harrison senselen = get_unaligned_be16(data); 7707996a778SMike Christie if (datalen < senselen) 7717996a778SMike Christie goto invalid_datalen; 7727996a778SMike Christie 7737996a778SMike Christie memcpy(sc->sense_buffer, data + 2, 7749b80cb4bSMike Christie min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE)); 7751b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n", 7761b2c7af8SMike Christie min_t(uint16_t, senselen, 7771b2c7af8SMike Christie SCSI_SENSE_BUFFERSIZE)); 7787996a778SMike Christie } 7797996a778SMike Christie 780c07d4444SBoaz Harrosh if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW | 781c07d4444SBoaz Harrosh ISCSI_FLAG_CMD_BIDI_OVERFLOW)) { 782c07d4444SBoaz Harrosh int res_count = be32_to_cpu(rhdr->bi_residual_count); 783c07d4444SBoaz Harrosh 784c07d4444SBoaz Harrosh if (scsi_bidi_cmnd(sc) && res_count > 0 && 785c07d4444SBoaz Harrosh (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW || 786c07d4444SBoaz Harrosh res_count <= scsi_in(sc)->length)) 787c07d4444SBoaz Harrosh scsi_in(sc)->resid = res_count; 788c07d4444SBoaz Harrosh else 789c07d4444SBoaz Harrosh sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 790c07d4444SBoaz Harrosh } 791c07d4444SBoaz Harrosh 7927207fea4SBoaz Harrosh if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW | 7937207fea4SBoaz Harrosh ISCSI_FLAG_CMD_OVERFLOW)) { 7947996a778SMike Christie int res_count = be32_to_cpu(rhdr->residual_count); 7957996a778SMike Christie 7967207fea4SBoaz Harrosh if (res_count > 0 && 7977207fea4SBoaz Harrosh (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || 7987207fea4SBoaz Harrosh res_count <= scsi_bufflen(sc))) 799c07d4444SBoaz Harrosh /* write side for bidi or uni-io set_resid */ 8001c138991SFUJITA Tomonori scsi_set_resid(sc, res_count); 8017996a778SMike Christie else 8027996a778SMike Christie sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 803c07d4444SBoaz Harrosh } 8047996a778SMike Christie out: 8053bbaaad9SMike Christie ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n", 8061b2c7af8SMike Christie sc, sc->result, task->itt); 8077996a778SMike Christie conn->scsirsp_pdus_cnt++; 808b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 8097996a778SMike Christie } 8107996a778SMike Christie 8111d9edf02SMike Christie /** 8121d9edf02SMike Christie * iscsi_data_in_rsp - SCSI Data-In Response processing 8131d9edf02SMike Christie * @conn: iscsi connection 8141d9edf02SMike Christie * @hdr: iscsi pdu 8151d9edf02SMike Christie * @task: scsi command task 8161d9edf02SMike Christie **/ 8171d9edf02SMike Christie static void 8181d9edf02SMike Christie iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 8191d9edf02SMike Christie struct iscsi_task *task) 8201d9edf02SMike Christie { 8211d9edf02SMike Christie struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr; 8221d9edf02SMike Christie struct scsi_cmnd *sc = task->sc; 8231d9edf02SMike Christie 8241d9edf02SMike Christie if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS)) 8251d9edf02SMike Christie return; 8261d9edf02SMike Christie 827edbc9aa0SMike Christie iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr); 8281d9edf02SMike Christie sc->result = (DID_OK << 16) | rhdr->cmd_status; 8291d9edf02SMike Christie conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; 8301d9edf02SMike Christie if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW | 8311d9edf02SMike Christie ISCSI_FLAG_DATA_OVERFLOW)) { 8321d9edf02SMike Christie int res_count = be32_to_cpu(rhdr->residual_count); 8331d9edf02SMike Christie 8341d9edf02SMike Christie if (res_count > 0 && 8351d9edf02SMike Christie (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || 8361d9edf02SMike Christie res_count <= scsi_in(sc)->length)) 8371d9edf02SMike Christie scsi_in(sc)->resid = res_count; 8381d9edf02SMike Christie else 8391d9edf02SMike Christie sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 8401d9edf02SMike Christie } 8411d9edf02SMike Christie 8423bbaaad9SMike Christie ISCSI_DBG_SESSION(conn->session, "data in with status done " 8433bbaaad9SMike Christie "[sc %p res %d itt 0x%x]\n", 8443bbaaad9SMike Christie sc, sc->result, task->itt); 8451d9edf02SMike Christie conn->scsirsp_pdus_cnt++; 846b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 8471d9edf02SMike Christie } 8481d9edf02SMike Christie 8497ea8b828SMike Christie static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr) 8507ea8b828SMike Christie { 8517ea8b828SMike Christie struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr; 8527ea8b828SMike Christie 8537ea8b828SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 8547ea8b828SMike Christie conn->tmfrsp_pdus_cnt++; 8557ea8b828SMike Christie 856843c0a8aSMike Christie if (conn->tmf_state != TMF_QUEUED) 8577ea8b828SMike Christie return; 8587ea8b828SMike Christie 8597ea8b828SMike Christie if (tmf->response == ISCSI_TMF_RSP_COMPLETE) 860843c0a8aSMike Christie conn->tmf_state = TMF_SUCCESS; 8617ea8b828SMike Christie else if (tmf->response == ISCSI_TMF_RSP_NO_TASK) 862843c0a8aSMike Christie conn->tmf_state = TMF_NOT_FOUND; 8637ea8b828SMike Christie else 864843c0a8aSMike Christie conn->tmf_state = TMF_FAILED; 8657ea8b828SMike Christie wake_up(&conn->ehwait); 8667ea8b828SMike Christie } 8677ea8b828SMike Christie 868f6d5180cSMike Christie static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr) 869f6d5180cSMike Christie { 870f6d5180cSMike Christie struct iscsi_nopout hdr; 8719c19a7d0SMike Christie struct iscsi_task *task; 872f6d5180cSMike Christie 8739c19a7d0SMike Christie if (!rhdr && conn->ping_task) 874f6d5180cSMike Christie return; 875f6d5180cSMike Christie 876f6d5180cSMike Christie memset(&hdr, 0, sizeof(struct iscsi_nopout)); 877f6d5180cSMike Christie hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE; 878f6d5180cSMike Christie hdr.flags = ISCSI_FLAG_CMD_FINAL; 879f6d5180cSMike Christie 880f6d5180cSMike Christie if (rhdr) { 881f6d5180cSMike Christie memcpy(hdr.lun, rhdr->lun, 8); 882f6d5180cSMike Christie hdr.ttt = rhdr->ttt; 883f6d5180cSMike Christie hdr.itt = RESERVED_ITT; 884f6d5180cSMike Christie } else 885f6d5180cSMike Christie hdr.ttt = RESERVED_ITT; 886f6d5180cSMike Christie 8879c19a7d0SMike Christie task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0); 8889c19a7d0SMike Christie if (!task) 889322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n"); 890d3acf022SMike Christie else if (!rhdr) { 891d3acf022SMike Christie /* only track our nops */ 892d3acf022SMike Christie conn->ping_task = task; 893d3acf022SMike Christie conn->last_ping = jiffies; 894d3acf022SMike Christie } 895f6d5180cSMike Christie } 896f6d5180cSMike Christie 8978afa1439SMike Christie static int iscsi_nop_out_rsp(struct iscsi_task *task, 8988afa1439SMike Christie struct iscsi_nopin *nop, char *data, int datalen) 8998afa1439SMike Christie { 9008afa1439SMike Christie struct iscsi_conn *conn = task->conn; 9018afa1439SMike Christie int rc = 0; 9028afa1439SMike Christie 9038afa1439SMike Christie if (conn->ping_task != task) { 9048afa1439SMike Christie /* 9058afa1439SMike Christie * If this is not in response to one of our 9068afa1439SMike Christie * nops then it must be from userspace. 9078afa1439SMike Christie */ 9088afa1439SMike Christie if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop, 9098afa1439SMike Christie data, datalen)) 9108afa1439SMike Christie rc = ISCSI_ERR_CONN_FAILED; 9118afa1439SMike Christie } else 9128afa1439SMike Christie mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout); 9138afa1439SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 9148afa1439SMike Christie return rc; 9158afa1439SMike Christie } 9168afa1439SMike Christie 91762f38300SMike Christie static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 91862f38300SMike Christie char *data, int datalen) 91962f38300SMike Christie { 92062f38300SMike Christie struct iscsi_reject *reject = (struct iscsi_reject *)hdr; 92162f38300SMike Christie struct iscsi_hdr rejected_pdu; 9228afa1439SMike Christie int opcode, rc = 0; 92362f38300SMike Christie 92462f38300SMike Christie conn->exp_statsn = be32_to_cpu(reject->statsn) + 1; 92562f38300SMike Christie 9268afa1439SMike Christie if (ntoh24(reject->dlength) > datalen || 9278afa1439SMike Christie ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) { 9288afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected " 9298afa1439SMike Christie "pdu. Invalid data length (pdu dlength " 9308afa1439SMike Christie "%u, datalen %d\n", ntoh24(reject->dlength), 9318afa1439SMike Christie datalen); 93262f38300SMike Christie return ISCSI_ERR_PROTO; 9338afa1439SMike Christie } 93462f38300SMike Christie memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr)); 9358afa1439SMike Christie opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK; 9368afa1439SMike Christie 9378afa1439SMike Christie switch (reject->reason) { 9388afa1439SMike Christie case ISCSI_REASON_DATA_DIGEST_ERROR: 939322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 9408afa1439SMike Christie "pdu (op 0x%x itt 0x%x) rejected " 941262ef636SMike Christie "due to DataDigest error.\n", 9428afa1439SMike Christie rejected_pdu.itt, opcode); 9438afa1439SMike Christie break; 9448afa1439SMike Christie case ISCSI_REASON_IMM_CMD_REJECT: 9458afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, 9468afa1439SMike Christie "pdu (op 0x%x itt 0x%x) rejected. Too many " 9478afa1439SMike Christie "immediate commands.\n", 9488afa1439SMike Christie rejected_pdu.itt, opcode); 9498afa1439SMike Christie /* 9508afa1439SMike Christie * We only send one TMF at a time so if the target could not 9518afa1439SMike Christie * handle it, then it should get fixed (RFC mandates that 9528afa1439SMike Christie * a target can handle one immediate TMF per conn). 9538afa1439SMike Christie * 9548afa1439SMike Christie * For nops-outs, we could have sent more than one if 9558afa1439SMike Christie * the target is sending us lots of nop-ins 9568afa1439SMike Christie */ 9578afa1439SMike Christie if (opcode != ISCSI_OP_NOOP_OUT) 95862f38300SMike Christie return 0; 9598afa1439SMike Christie 9608afa1439SMike Christie if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) 9618afa1439SMike Christie /* 9628afa1439SMike Christie * nop-out in response to target's nop-out rejected. 9638afa1439SMike Christie * Just resend. 9648afa1439SMike Christie */ 9658afa1439SMike Christie iscsi_send_nopout(conn, 9668afa1439SMike Christie (struct iscsi_nopin*)&rejected_pdu); 9678afa1439SMike Christie else { 9688afa1439SMike Christie struct iscsi_task *task; 9698afa1439SMike Christie /* 9708afa1439SMike Christie * Our nop as ping got dropped. We know the target 9718afa1439SMike Christie * and transport are ok so just clean up 9728afa1439SMike Christie */ 9738afa1439SMike Christie task = iscsi_itt_to_task(conn, rejected_pdu.itt); 9748afa1439SMike Christie if (!task) { 9758afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, 9768afa1439SMike Christie "Invalid pdu reject. Could " 9778afa1439SMike Christie "not lookup rejected task.\n"); 9788afa1439SMike Christie rc = ISCSI_ERR_BAD_ITT; 9798afa1439SMike Christie } else 9808afa1439SMike Christie rc = iscsi_nop_out_rsp(task, 9818afa1439SMike Christie (struct iscsi_nopin*)&rejected_pdu, 9828afa1439SMike Christie NULL, 0); 9838afa1439SMike Christie } 9848afa1439SMike Christie break; 9858afa1439SMike Christie default: 9868afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, 9878afa1439SMike Christie "pdu (op 0x%x itt 0x%x) rejected. Reason " 9888afa1439SMike Christie "code 0x%x\n", rejected_pdu.itt, 9898afa1439SMike Christie rejected_pdu.opcode, reject->reason); 9908afa1439SMike Christie break; 9918afa1439SMike Christie } 9928afa1439SMike Christie return rc; 99362f38300SMike Christie } 99462f38300SMike Christie 9957996a778SMike Christie /** 996913e5bf4SMike Christie * iscsi_itt_to_task - look up task by itt 997913e5bf4SMike Christie * @conn: iscsi connection 998913e5bf4SMike Christie * @itt: itt 999913e5bf4SMike Christie * 1000913e5bf4SMike Christie * This should be used for mgmt tasks like login and nops, or if 1001913e5bf4SMike Christie * the LDD's itt space does not include the session age. 1002913e5bf4SMike Christie * 1003913e5bf4SMike Christie * The session lock must be held. 1004913e5bf4SMike Christie */ 10058f9256ceSMike Christie struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt) 1006913e5bf4SMike Christie { 1007913e5bf4SMike Christie struct iscsi_session *session = conn->session; 1008262ef636SMike Christie int i; 1009913e5bf4SMike Christie 1010913e5bf4SMike Christie if (itt == RESERVED_ITT) 1011913e5bf4SMike Christie return NULL; 1012913e5bf4SMike Christie 1013262ef636SMike Christie if (session->tt->parse_pdu_itt) 1014262ef636SMike Christie session->tt->parse_pdu_itt(conn, itt, &i, NULL); 1015262ef636SMike Christie else 1016913e5bf4SMike Christie i = get_itt(itt); 1017913e5bf4SMike Christie if (i >= session->cmds_max) 1018913e5bf4SMike Christie return NULL; 1019913e5bf4SMike Christie 1020913e5bf4SMike Christie return session->cmds[i]; 1021913e5bf4SMike Christie } 10228f9256ceSMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_task); 1023913e5bf4SMike Christie 1024913e5bf4SMike Christie /** 10257996a778SMike Christie * __iscsi_complete_pdu - complete pdu 10267996a778SMike Christie * @conn: iscsi conn 10277996a778SMike Christie * @hdr: iscsi header 10287996a778SMike Christie * @data: data buffer 10297996a778SMike Christie * @datalen: len of data buffer 10307996a778SMike Christie * 10317996a778SMike Christie * Completes pdu processing by freeing any resources allocated at 10327996a778SMike Christie * queuecommand or send generic. session lock must be held and verify 10337996a778SMike Christie * itt must have been called. 10347996a778SMike Christie */ 1035913e5bf4SMike Christie int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 10367996a778SMike Christie char *data, int datalen) 10377996a778SMike Christie { 10387996a778SMike Christie struct iscsi_session *session = conn->session; 10397996a778SMike Christie int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0; 10409c19a7d0SMike Christie struct iscsi_task *task; 10417996a778SMike Christie uint32_t itt; 10427996a778SMike Christie 1043f6d5180cSMike Christie conn->last_recv = jiffies; 10440af967f5SMike Christie rc = iscsi_verify_itt(conn, hdr->itt); 10450af967f5SMike Christie if (rc) 10460af967f5SMike Christie return rc; 10470af967f5SMike Christie 1048b4377356SAl Viro if (hdr->itt != RESERVED_ITT) 1049b4377356SAl Viro itt = get_itt(hdr->itt); 10507996a778SMike Christie else 1051b4377356SAl Viro itt = ~0U; 10527996a778SMike Christie 10531b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n", 10543e5c28adSMike Christie opcode, conn->id, itt, datalen); 10557996a778SMike Christie 10563e5c28adSMike Christie if (itt == ~0U) { 105777a23c21SMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 105862f38300SMike Christie 10597996a778SMike Christie switch(opcode) { 10607996a778SMike Christie case ISCSI_OP_NOOP_IN: 106140527afeSMike Christie if (datalen) { 106240527afeSMike Christie rc = ISCSI_ERR_PROTO; 106340527afeSMike Christie break; 106440527afeSMike Christie } 106540527afeSMike Christie 1066b4377356SAl Viro if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG)) 106740527afeSMike Christie break; 106840527afeSMike Christie 1069f6d5180cSMike Christie iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr); 10707996a778SMike Christie break; 10717996a778SMike Christie case ISCSI_OP_REJECT: 107262f38300SMike Christie rc = iscsi_handle_reject(conn, hdr, data, datalen); 107362f38300SMike Christie break; 10747996a778SMike Christie case ISCSI_OP_ASYNC_EVENT: 10758d2860b3SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 10765831c737SMike Christie if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) 10775831c737SMike Christie rc = ISCSI_ERR_CONN_FAILED; 10787996a778SMike Christie break; 10797996a778SMike Christie default: 10807996a778SMike Christie rc = ISCSI_ERR_BAD_OPCODE; 10817996a778SMike Christie break; 10827996a778SMike Christie } 10833e5c28adSMike Christie goto out; 10843e5c28adSMike Christie } 10857996a778SMike Christie 10863e5c28adSMike Christie switch(opcode) { 10873e5c28adSMike Christie case ISCSI_OP_SCSI_CMD_RSP: 1088913e5bf4SMike Christie case ISCSI_OP_SCSI_DATA_IN: 1089913e5bf4SMike Christie task = iscsi_itt_to_ctask(conn, hdr->itt); 1090913e5bf4SMike Christie if (!task) 1091913e5bf4SMike Christie return ISCSI_ERR_BAD_ITT; 1092d355e57dSMike Christie task->last_xfer = jiffies; 10933e5c28adSMike Christie break; 1094913e5bf4SMike Christie case ISCSI_OP_R2T: 1095913e5bf4SMike Christie /* 1096913e5bf4SMike Christie * LLD handles R2Ts if they need to. 1097913e5bf4SMike Christie */ 1098913e5bf4SMike Christie return 0; 1099913e5bf4SMike Christie case ISCSI_OP_LOGOUT_RSP: 1100913e5bf4SMike Christie case ISCSI_OP_LOGIN_RSP: 1101913e5bf4SMike Christie case ISCSI_OP_TEXT_RSP: 1102913e5bf4SMike Christie case ISCSI_OP_SCSI_TMFUNC_RSP: 1103913e5bf4SMike Christie case ISCSI_OP_NOOP_IN: 1104913e5bf4SMike Christie task = iscsi_itt_to_task(conn, hdr->itt); 1105913e5bf4SMike Christie if (!task) 1106913e5bf4SMike Christie return ISCSI_ERR_BAD_ITT; 1107913e5bf4SMike Christie break; 1108913e5bf4SMike Christie default: 1109913e5bf4SMike Christie return ISCSI_ERR_BAD_OPCODE; 11103e5c28adSMike Christie } 1111913e5bf4SMike Christie 1112913e5bf4SMike Christie switch(opcode) { 1113913e5bf4SMike Christie case ISCSI_OP_SCSI_CMD_RSP: 11149c19a7d0SMike Christie iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen); 11153e5c28adSMike Christie break; 11163e5c28adSMike Christie case ISCSI_OP_SCSI_DATA_IN: 11171d9edf02SMike Christie iscsi_data_in_rsp(conn, hdr, task); 11183e5c28adSMike Christie break; 11193e5c28adSMike Christie case ISCSI_OP_LOGOUT_RSP: 11203e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 11213e5c28adSMike Christie if (datalen) { 11223e5c28adSMike Christie rc = ISCSI_ERR_PROTO; 11233e5c28adSMike Christie break; 11243e5c28adSMike Christie } 11253e5c28adSMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 11263e5c28adSMike Christie goto recv_pdu; 11273e5c28adSMike Christie case ISCSI_OP_LOGIN_RSP: 11283e5c28adSMike Christie case ISCSI_OP_TEXT_RSP: 11293e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 11303e5c28adSMike Christie /* 11313e5c28adSMike Christie * login related PDU's exp_statsn is handled in 11323e5c28adSMike Christie * userspace 11333e5c28adSMike Christie */ 11343e5c28adSMike Christie goto recv_pdu; 11353e5c28adSMike Christie case ISCSI_OP_SCSI_TMFUNC_RSP: 11363e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 11373e5c28adSMike Christie if (datalen) { 11383e5c28adSMike Christie rc = ISCSI_ERR_PROTO; 11393e5c28adSMike Christie break; 11403e5c28adSMike Christie } 11413e5c28adSMike Christie 11423e5c28adSMike Christie iscsi_tmf_rsp(conn, hdr); 1143b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 11443e5c28adSMike Christie break; 11453e5c28adSMike Christie case ISCSI_OP_NOOP_IN: 11463e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 11473e5c28adSMike Christie if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) { 11483e5c28adSMike Christie rc = ISCSI_ERR_PROTO; 11493e5c28adSMike Christie break; 11503e5c28adSMike Christie } 11513e5c28adSMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 11523e5c28adSMike Christie 11538afa1439SMike Christie rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr, 11548afa1439SMike Christie data, datalen); 11553e5c28adSMike Christie break; 11563e5c28adSMike Christie default: 11573e5c28adSMike Christie rc = ISCSI_ERR_BAD_OPCODE; 11583e5c28adSMike Christie break; 11593e5c28adSMike Christie } 11603e5c28adSMike Christie 11613e5c28adSMike Christie out: 11623e5c28adSMike Christie return rc; 11633e5c28adSMike Christie recv_pdu: 11643e5c28adSMike Christie if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) 11653e5c28adSMike Christie rc = ISCSI_ERR_CONN_FAILED; 1166b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 11677996a778SMike Christie return rc; 11687996a778SMike Christie } 1169913e5bf4SMike Christie EXPORT_SYMBOL_GPL(__iscsi_complete_pdu); 11707996a778SMike Christie 11717996a778SMike Christie int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 11727996a778SMike Christie char *data, int datalen) 11737996a778SMike Christie { 11747996a778SMike Christie int rc; 11757996a778SMike Christie 11767996a778SMike Christie spin_lock(&conn->session->lock); 11777996a778SMike Christie rc = __iscsi_complete_pdu(conn, hdr, data, datalen); 11787996a778SMike Christie spin_unlock(&conn->session->lock); 11797996a778SMike Christie return rc; 11807996a778SMike Christie } 11817996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_pdu); 11827996a778SMike Christie 11830af967f5SMike Christie int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt) 11847996a778SMike Christie { 11857996a778SMike Christie struct iscsi_session *session = conn->session; 1186262ef636SMike Christie int age = 0, i = 0; 11877996a778SMike Christie 11880af967f5SMike Christie if (itt == RESERVED_ITT) 11890af967f5SMike Christie return 0; 11900af967f5SMike Christie 1191262ef636SMike Christie if (session->tt->parse_pdu_itt) 1192262ef636SMike Christie session->tt->parse_pdu_itt(conn, itt, &i, &age); 1193262ef636SMike Christie else { 1194262ef636SMike Christie i = get_itt(itt); 1195262ef636SMike Christie age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK; 1196262ef636SMike Christie } 1197262ef636SMike Christie 1198262ef636SMike Christie if (age != session->age) { 1199322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 12000af967f5SMike Christie "received itt %x expected session age (%x)\n", 1201913e5bf4SMike Christie (__force u32)itt, session->age); 12027996a778SMike Christie return ISCSI_ERR_BAD_ITT; 12037996a778SMike Christie } 12047996a778SMike Christie 12053e5c28adSMike Christie if (i >= session->cmds_max) { 12063e5c28adSMike Christie iscsi_conn_printk(KERN_ERR, conn, 12073e5c28adSMike Christie "received invalid itt index %u (max cmds " 12083e5c28adSMike Christie "%u.\n", i, session->cmds_max); 12093e5c28adSMike Christie return ISCSI_ERR_BAD_ITT; 12107996a778SMike Christie } 12117996a778SMike Christie return 0; 12127996a778SMike Christie } 12137996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_verify_itt); 12147996a778SMike Christie 1215913e5bf4SMike Christie /** 1216913e5bf4SMike Christie * iscsi_itt_to_ctask - look up ctask by itt 1217913e5bf4SMike Christie * @conn: iscsi connection 1218913e5bf4SMike Christie * @itt: itt 1219913e5bf4SMike Christie * 1220913e5bf4SMike Christie * This should be used for cmd tasks. 1221913e5bf4SMike Christie * 1222913e5bf4SMike Christie * The session lock must be held. 1223913e5bf4SMike Christie */ 1224913e5bf4SMike Christie struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt) 12250af967f5SMike Christie { 12269c19a7d0SMike Christie struct iscsi_task *task; 12270af967f5SMike Christie 12280af967f5SMike Christie if (iscsi_verify_itt(conn, itt)) 12290af967f5SMike Christie return NULL; 12300af967f5SMike Christie 1231913e5bf4SMike Christie task = iscsi_itt_to_task(conn, itt); 1232913e5bf4SMike Christie if (!task || !task->sc) 12330af967f5SMike Christie return NULL; 12340af967f5SMike Christie 1235913e5bf4SMike Christie if (task->sc->SCp.phase != conn->session->age) { 1236913e5bf4SMike Christie iscsi_session_printk(KERN_ERR, conn->session, 1237913e5bf4SMike Christie "task's session age %d, expected %d\n", 1238913e5bf4SMike Christie task->sc->SCp.phase, conn->session->age); 12390af967f5SMike Christie return NULL; 1240913e5bf4SMike Christie } 12410af967f5SMike Christie 12429c19a7d0SMike Christie return task; 12430af967f5SMike Christie } 12440af967f5SMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask); 12450af967f5SMike Christie 124640a06e75SMike Christie void iscsi_session_failure(struct iscsi_session *session, 1247e5bd7b54SMike Christie enum iscsi_err err) 1248e5bd7b54SMike Christie { 1249e5bd7b54SMike Christie struct iscsi_conn *conn; 1250e5bd7b54SMike Christie struct device *dev; 1251e5bd7b54SMike Christie unsigned long flags; 1252e5bd7b54SMike Christie 1253e5bd7b54SMike Christie spin_lock_irqsave(&session->lock, flags); 1254e5bd7b54SMike Christie conn = session->leadconn; 1255e5bd7b54SMike Christie if (session->state == ISCSI_STATE_TERMINATE || !conn) { 1256e5bd7b54SMike Christie spin_unlock_irqrestore(&session->lock, flags); 1257e5bd7b54SMike Christie return; 1258e5bd7b54SMike Christie } 1259e5bd7b54SMike Christie 1260e5bd7b54SMike Christie dev = get_device(&conn->cls_conn->dev); 1261e5bd7b54SMike Christie spin_unlock_irqrestore(&session->lock, flags); 1262e5bd7b54SMike Christie if (!dev) 1263e5bd7b54SMike Christie return; 1264e5bd7b54SMike Christie /* 1265e5bd7b54SMike Christie * if the host is being removed bypass the connection 1266e5bd7b54SMike Christie * recovery initialization because we are going to kill 1267e5bd7b54SMike Christie * the session. 1268e5bd7b54SMike Christie */ 1269e5bd7b54SMike Christie if (err == ISCSI_ERR_INVALID_HOST) 1270e5bd7b54SMike Christie iscsi_conn_error_event(conn->cls_conn, err); 1271e5bd7b54SMike Christie else 1272e5bd7b54SMike Christie iscsi_conn_failure(conn, err); 1273e5bd7b54SMike Christie put_device(dev); 1274e5bd7b54SMike Christie } 1275e5bd7b54SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_failure); 1276e5bd7b54SMike Christie 12777996a778SMike Christie void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err) 12787996a778SMike Christie { 12797996a778SMike Christie struct iscsi_session *session = conn->session; 12807996a778SMike Christie unsigned long flags; 12817996a778SMike Christie 12827996a778SMike Christie spin_lock_irqsave(&session->lock, flags); 1283656cffc9SMike Christie if (session->state == ISCSI_STATE_FAILED) { 1284656cffc9SMike Christie spin_unlock_irqrestore(&session->lock, flags); 1285656cffc9SMike Christie return; 1286656cffc9SMike Christie } 1287656cffc9SMike Christie 128867a61114SMike Christie if (conn->stop_stage == 0) 12897996a778SMike Christie session->state = ISCSI_STATE_FAILED; 12907996a778SMike Christie spin_unlock_irqrestore(&session->lock, flags); 1291e5bd7b54SMike Christie 12927996a778SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 12937996a778SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); 1294e5bd7b54SMike Christie iscsi_conn_error_event(conn->cls_conn, err); 12957996a778SMike Christie } 12967996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_failure); 12977996a778SMike Christie 129877a23c21SMike Christie static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn) 129977a23c21SMike Christie { 130077a23c21SMike Christie struct iscsi_session *session = conn->session; 130177a23c21SMike Christie 130277a23c21SMike Christie /* 130377a23c21SMike Christie * Check for iSCSI window and take care of CmdSN wrap-around 130477a23c21SMike Christie */ 1305e0726407SMike Christie if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) { 13061b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn " 13071b2c7af8SMike Christie "%u MaxCmdSN %u CmdSN %u/%u\n", 13081b2c7af8SMike Christie session->exp_cmdsn, session->max_cmdsn, 13091b2c7af8SMike Christie session->cmdsn, session->queued_cmdsn); 131077a23c21SMike Christie return -ENOSPC; 131177a23c21SMike Christie } 131277a23c21SMike Christie return 0; 131377a23c21SMike Christie } 131477a23c21SMike Christie 13159c19a7d0SMike Christie static int iscsi_xmit_task(struct iscsi_conn *conn) 131677a23c21SMike Christie { 13179c19a7d0SMike Christie struct iscsi_task *task = conn->task; 1318843c0a8aSMike Christie int rc; 131977a23c21SMike Christie 132070b31c15SMike Christie if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) 132170b31c15SMike Christie return -ENODATA; 132270b31c15SMike Christie 13239c19a7d0SMike Christie __iscsi_get_task(task); 132477a23c21SMike Christie spin_unlock_bh(&conn->session->lock); 13259c19a7d0SMike Christie rc = conn->session->tt->xmit_task(task); 132677a23c21SMike Christie spin_lock_bh(&conn->session->lock); 1327d355e57dSMike Christie if (!rc) { 13289c19a7d0SMike Christie /* done with this task */ 1329d355e57dSMike Christie task->last_xfer = jiffies; 13309c19a7d0SMike Christie conn->task = NULL; 1331d355e57dSMike Christie } 1332d355e57dSMike Christie __iscsi_put_task(task); 133377a23c21SMike Christie return rc; 133477a23c21SMike Christie } 133577a23c21SMike Christie 13367996a778SMike Christie /** 13379c19a7d0SMike Christie * iscsi_requeue_task - requeue task to run from session workqueue 13389c19a7d0SMike Christie * @task: task to requeue 1339843c0a8aSMike Christie * 13409c19a7d0SMike Christie * LLDs that need to run a task from the session workqueue should call 1341052d0144SMike Christie * this. The session lock must be held. This should only be called 1342052d0144SMike Christie * by software drivers. 1343843c0a8aSMike Christie */ 13449c19a7d0SMike Christie void iscsi_requeue_task(struct iscsi_task *task) 1345843c0a8aSMike Christie { 13469c19a7d0SMike Christie struct iscsi_conn *conn = task->conn; 1347843c0a8aSMike Christie 13483bbaaad9SMike Christie /* 13493bbaaad9SMike Christie * this may be on the requeue list already if the xmit_task callout 13503bbaaad9SMike Christie * is handling the r2ts while we are adding new ones 13513bbaaad9SMike Christie */ 13523bbaaad9SMike Christie if (list_empty(&task->running)) 13533bbaaad9SMike Christie list_add_tail(&task->running, &conn->requeue); 135432ae763eSMike Christie iscsi_conn_queue_work(conn); 1355843c0a8aSMike Christie } 13569c19a7d0SMike Christie EXPORT_SYMBOL_GPL(iscsi_requeue_task); 1357843c0a8aSMike Christie 1358843c0a8aSMike Christie /** 13597996a778SMike Christie * iscsi_data_xmit - xmit any command into the scheduled connection 13607996a778SMike Christie * @conn: iscsi connection 13617996a778SMike Christie * 13627996a778SMike Christie * Notes: 13637996a778SMike Christie * The function can return -EAGAIN in which case the caller must 13647996a778SMike Christie * re-schedule it again later or recover. '0' return code means 13657996a778SMike Christie * successful xmit. 13667996a778SMike Christie **/ 13677996a778SMike Christie static int iscsi_data_xmit(struct iscsi_conn *conn) 13687996a778SMike Christie { 13693219e529SMike Christie int rc = 0; 13707996a778SMike Christie 137177a23c21SMike Christie spin_lock_bh(&conn->session->lock); 137270b31c15SMike Christie if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) { 13731b2c7af8SMike Christie ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n"); 137477a23c21SMike Christie spin_unlock_bh(&conn->session->lock); 13753219e529SMike Christie return -ENODATA; 13767996a778SMike Christie } 13777996a778SMike Christie 13789c19a7d0SMike Christie if (conn->task) { 13799c19a7d0SMike Christie rc = iscsi_xmit_task(conn); 13803219e529SMike Christie if (rc) 138170b31c15SMike Christie goto done; 13827996a778SMike Christie } 138377a23c21SMike Christie 138477a23c21SMike Christie /* 138577a23c21SMike Christie * process mgmt pdus like nops before commands since we should 138677a23c21SMike Christie * only have one nop-out as a ping from us and targets should not 138777a23c21SMike Christie * overflow us with nop-ins 138877a23c21SMike Christie */ 138977a23c21SMike Christie check_mgmt: 1390843c0a8aSMike Christie while (!list_empty(&conn->mgmtqueue)) { 13919c19a7d0SMike Christie conn->task = list_entry(conn->mgmtqueue.next, 13929c19a7d0SMike Christie struct iscsi_task, running); 13933bbaaad9SMike Christie list_del_init(&conn->task->running); 13949c19a7d0SMike Christie if (iscsi_prep_mgmt_task(conn, conn->task)) { 13959c19a7d0SMike Christie __iscsi_put_task(conn->task); 13969c19a7d0SMike Christie conn->task = NULL; 1397b3a7ea8dSMike Christie continue; 1398b3a7ea8dSMike Christie } 13999c19a7d0SMike Christie rc = iscsi_xmit_task(conn); 14003219e529SMike Christie if (rc) 140170b31c15SMike Christie goto done; 14027996a778SMike Christie } 14037996a778SMike Christie 1404843c0a8aSMike Christie /* process pending command queue */ 14053bbaaad9SMike Christie while (!list_empty(&conn->cmdqueue)) { 1406843c0a8aSMike Christie if (conn->tmf_state == TMF_QUEUED) 1407843c0a8aSMike Christie break; 1408843c0a8aSMike Christie 14093bbaaad9SMike Christie conn->task = list_entry(conn->cmdqueue.next, 14109c19a7d0SMike Christie struct iscsi_task, running); 14113bbaaad9SMike Christie list_del_init(&conn->task->running); 1412b3a7ea8dSMike Christie if (conn->session->state == ISCSI_STATE_LOGGING_OUT) { 1413b3cd5050SMike Christie fail_scsi_task(conn->task, DID_IMM_RETRY); 1414b3a7ea8dSMike Christie continue; 1415b3a7ea8dSMike Christie } 1416577577daSMike Christie rc = iscsi_prep_scsi_cmd_pdu(conn->task); 1417577577daSMike Christie if (rc) { 1418577577daSMike Christie if (rc == -ENOMEM) { 14193bbaaad9SMike Christie list_add_tail(&conn->task->running, 14203bbaaad9SMike Christie &conn->cmdqueue); 1421577577daSMike Christie conn->task = NULL; 142270b31c15SMike Christie goto done; 1423577577daSMike Christie } else 1424b3cd5050SMike Christie fail_scsi_task(conn->task, DID_ABORT); 1425004d6530SBoaz Harrosh continue; 1426004d6530SBoaz Harrosh } 14279c19a7d0SMike Christie rc = iscsi_xmit_task(conn); 14283219e529SMike Christie if (rc) 142970b31c15SMike Christie goto done; 143077a23c21SMike Christie /* 14319c19a7d0SMike Christie * we could continuously get new task requests so 143277a23c21SMike Christie * we need to check the mgmt queue for nops that need to 143377a23c21SMike Christie * be sent to aviod starvation 143477a23c21SMike Christie */ 1435843c0a8aSMike Christie if (!list_empty(&conn->mgmtqueue)) 1436843c0a8aSMike Christie goto check_mgmt; 1437843c0a8aSMike Christie } 1438843c0a8aSMike Christie 1439843c0a8aSMike Christie while (!list_empty(&conn->requeue)) { 1440843c0a8aSMike Christie if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL) 1441843c0a8aSMike Christie break; 1442843c0a8aSMike Christie 1443b3a7ea8dSMike Christie /* 1444b3a7ea8dSMike Christie * we always do fastlogout - conn stop code will clean up. 1445b3a7ea8dSMike Christie */ 1446b3a7ea8dSMike Christie if (conn->session->state == ISCSI_STATE_LOGGING_OUT) 1447b3a7ea8dSMike Christie break; 1448b3a7ea8dSMike Christie 14499c19a7d0SMike Christie conn->task = list_entry(conn->requeue.next, 14509c19a7d0SMike Christie struct iscsi_task, running); 14513bbaaad9SMike Christie list_del_init(&conn->task->running); 14529c19a7d0SMike Christie conn->task->state = ISCSI_TASK_RUNNING; 14539c19a7d0SMike Christie rc = iscsi_xmit_task(conn); 1454843c0a8aSMike Christie if (rc) 145570b31c15SMike Christie goto done; 1456843c0a8aSMike Christie if (!list_empty(&conn->mgmtqueue)) 145777a23c21SMike Christie goto check_mgmt; 14587996a778SMike Christie } 145977a23c21SMike Christie spin_unlock_bh(&conn->session->lock); 14603219e529SMike Christie return -ENODATA; 14617996a778SMike Christie 146270b31c15SMike Christie done: 146377a23c21SMike Christie spin_unlock_bh(&conn->session->lock); 14643219e529SMike Christie return rc; 14657996a778SMike Christie } 14667996a778SMike Christie 1467c4028958SDavid Howells static void iscsi_xmitworker(struct work_struct *work) 14687996a778SMike Christie { 1469c4028958SDavid Howells struct iscsi_conn *conn = 1470c4028958SDavid Howells container_of(work, struct iscsi_conn, xmitwork); 14713219e529SMike Christie int rc; 14727996a778SMike Christie /* 14737996a778SMike Christie * serialize Xmit worker on a per-connection basis. 14747996a778SMike Christie */ 14753219e529SMike Christie do { 14763219e529SMike Christie rc = iscsi_data_xmit(conn); 14773219e529SMike Christie } while (rc >= 0 || rc == -EAGAIN); 14787996a778SMike Christie } 14797996a778SMike Christie 1480577577daSMike Christie static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn, 1481577577daSMike Christie struct scsi_cmnd *sc) 1482577577daSMike Christie { 1483577577daSMike Christie struct iscsi_task *task; 1484577577daSMike Christie 1485577577daSMike Christie if (!__kfifo_get(conn->session->cmdpool.queue, 1486577577daSMike Christie (void *) &task, sizeof(void *))) 1487577577daSMike Christie return NULL; 1488577577daSMike Christie 1489577577daSMike Christie sc->SCp.phase = conn->session->age; 1490577577daSMike Christie sc->SCp.ptr = (char *) task; 1491577577daSMike Christie 1492577577daSMike Christie atomic_set(&task->refcount, 1); 1493577577daSMike Christie task->state = ISCSI_TASK_PENDING; 1494577577daSMike Christie task->conn = conn; 1495577577daSMike Christie task->sc = sc; 1496d355e57dSMike Christie task->have_checked_conn = false; 1497d355e57dSMike Christie task->last_timeout = jiffies; 1498d355e57dSMike Christie task->last_xfer = jiffies; 1499577577daSMike Christie INIT_LIST_HEAD(&task->running); 1500577577daSMike Christie return task; 1501577577daSMike Christie } 1502577577daSMike Christie 15037996a778SMike Christie enum { 15047996a778SMike Christie FAILURE_BAD_HOST = 1, 15057996a778SMike Christie FAILURE_SESSION_FAILED, 15067996a778SMike Christie FAILURE_SESSION_FREED, 15077996a778SMike Christie FAILURE_WINDOW_CLOSED, 150860ecebf5SMike Christie FAILURE_OOM, 15097996a778SMike Christie FAILURE_SESSION_TERMINATE, 1510656cffc9SMike Christie FAILURE_SESSION_IN_RECOVERY, 15117996a778SMike Christie FAILURE_SESSION_RECOVERY_TIMEOUT, 1512b3a7ea8dSMike Christie FAILURE_SESSION_LOGGING_OUT, 15136eabafbeSMike Christie FAILURE_SESSION_NOT_READY, 15147996a778SMike Christie }; 15157996a778SMike Christie 15167996a778SMike Christie int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *)) 15177996a778SMike Christie { 151875613521SMike Christie struct iscsi_cls_session *cls_session; 15197996a778SMike Christie struct Scsi_Host *host; 15201336aed1SMike Christie struct iscsi_host *ihost; 15217996a778SMike Christie int reason = 0; 15227996a778SMike Christie struct iscsi_session *session; 15237996a778SMike Christie struct iscsi_conn *conn; 15249c19a7d0SMike Christie struct iscsi_task *task = NULL; 15257996a778SMike Christie 15267996a778SMike Christie sc->scsi_done = done; 15277996a778SMike Christie sc->result = 0; 1528f47f2cf5SMike Christie sc->SCp.ptr = NULL; 15297996a778SMike Christie 15307996a778SMike Christie host = sc->device->host; 15311336aed1SMike Christie ihost = shost_priv(host); 15321040c99dSMike Christie spin_unlock(host->host_lock); 15337996a778SMike Christie 153475613521SMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 153575613521SMike Christie session = cls_session->dd_data; 15367996a778SMike Christie spin_lock(&session->lock); 15377996a778SMike Christie 153875613521SMike Christie reason = iscsi_session_chkready(cls_session); 15396eabafbeSMike Christie if (reason) { 15406eabafbeSMike Christie sc->result = reason; 15416eabafbeSMike Christie goto fault; 15426eabafbeSMike Christie } 15436eabafbeSMike Christie 1544301e0f7eSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) { 1545656cffc9SMike Christie /* 1546656cffc9SMike Christie * to handle the race between when we set the recovery state 1547656cffc9SMike Christie * and block the session we requeue here (commands could 1548656cffc9SMike Christie * be entering our queuecommand while a block is starting 1549656cffc9SMike Christie * up because the block code is not locked) 1550656cffc9SMike Christie */ 15519000bcd6SMike Christie switch (session->state) { 1552301e0f7eSMike Christie case ISCSI_STATE_FAILED: 15539000bcd6SMike Christie case ISCSI_STATE_IN_RECOVERY: 1554656cffc9SMike Christie reason = FAILURE_SESSION_IN_RECOVERY; 1555301e0f7eSMike Christie sc->result = DID_IMM_RETRY << 16; 1556301e0f7eSMike Christie break; 15579000bcd6SMike Christie case ISCSI_STATE_LOGGING_OUT: 15589000bcd6SMike Christie reason = FAILURE_SESSION_LOGGING_OUT; 1559301e0f7eSMike Christie sc->result = DID_IMM_RETRY << 16; 1560301e0f7eSMike Christie break; 1561b3a7ea8dSMike Christie case ISCSI_STATE_RECOVERY_FAILED: 1562656cffc9SMike Christie reason = FAILURE_SESSION_RECOVERY_TIMEOUT; 156356d7fcfaSMike Christie sc->result = DID_TRANSPORT_FAILFAST << 16; 1564b3a7ea8dSMike Christie break; 1565b3a7ea8dSMike Christie case ISCSI_STATE_TERMINATE: 1566656cffc9SMike Christie reason = FAILURE_SESSION_TERMINATE; 15676eabafbeSMike Christie sc->result = DID_NO_CONNECT << 16; 1568b3a7ea8dSMike Christie break; 1569b3a7ea8dSMike Christie default: 15707996a778SMike Christie reason = FAILURE_SESSION_FREED; 15716eabafbeSMike Christie sc->result = DID_NO_CONNECT << 16; 1572b3a7ea8dSMike Christie } 15737996a778SMike Christie goto fault; 15747996a778SMike Christie } 15757996a778SMike Christie 15767996a778SMike Christie conn = session->leadconn; 157798644047SMike Christie if (!conn) { 157898644047SMike Christie reason = FAILURE_SESSION_FREED; 15796eabafbeSMike Christie sc->result = DID_NO_CONNECT << 16; 158098644047SMike Christie goto fault; 158198644047SMike Christie } 15827996a778SMike Christie 1583661134adSMike Christie if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) { 1584661134adSMike Christie reason = FAILURE_SESSION_IN_RECOVERY; 1585661134adSMike Christie sc->result = DID_REQUEUE; 1586661134adSMike Christie goto fault; 1587661134adSMike Christie } 1588661134adSMike Christie 158977a23c21SMike Christie if (iscsi_check_cmdsn_window_closed(conn)) { 159077a23c21SMike Christie reason = FAILURE_WINDOW_CLOSED; 159177a23c21SMike Christie goto reject; 159277a23c21SMike Christie } 159377a23c21SMike Christie 1594577577daSMike Christie task = iscsi_alloc_task(conn, sc); 1595577577daSMike Christie if (!task) { 159660ecebf5SMike Christie reason = FAILURE_OOM; 159760ecebf5SMike Christie goto reject; 159860ecebf5SMike Christie } 15997996a778SMike Christie 16001336aed1SMike Christie if (!ihost->workq) { 1601577577daSMike Christie reason = iscsi_prep_scsi_cmd_pdu(task); 1602577577daSMike Christie if (reason) { 1603577577daSMike Christie if (reason == -ENOMEM) { 1604577577daSMike Christie reason = FAILURE_OOM; 1605577577daSMike Christie goto prepd_reject; 1606577577daSMike Christie } else { 1607052d0144SMike Christie sc->result = DID_ABORT << 16; 1608577577daSMike Christie goto prepd_fault; 1609577577daSMike Christie } 1610052d0144SMike Christie } 16119c19a7d0SMike Christie if (session->tt->xmit_task(task)) { 1612d3305f34SMike Christie session->cmdsn--; 1613052d0144SMike Christie reason = FAILURE_SESSION_NOT_READY; 1614577577daSMike Christie goto prepd_reject; 1615052d0144SMike Christie } 16163bbaaad9SMike Christie } else { 16173bbaaad9SMike Christie list_add_tail(&task->running, &conn->cmdqueue); 161832ae763eSMike Christie iscsi_conn_queue_work(conn); 16193bbaaad9SMike Christie } 1620052d0144SMike Christie 1621052d0144SMike Christie session->queued_cmdsn++; 1622052d0144SMike Christie spin_unlock(&session->lock); 16231040c99dSMike Christie spin_lock(host->host_lock); 16247996a778SMike Christie return 0; 16257996a778SMike Christie 1626577577daSMike Christie prepd_reject: 1627577577daSMike Christie sc->scsi_done = NULL; 1628b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 16297996a778SMike Christie reject: 16307996a778SMike Christie spin_unlock(&session->lock); 16311b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n", 16321b2c7af8SMike Christie sc->cmnd[0], reason); 16331040c99dSMike Christie spin_lock(host->host_lock); 1634d6d13ee1SMike Christie return SCSI_MLQUEUE_TARGET_BUSY; 16357996a778SMike Christie 1636577577daSMike Christie prepd_fault: 1637577577daSMike Christie sc->scsi_done = NULL; 1638b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 16397996a778SMike Christie fault: 16407996a778SMike Christie spin_unlock(&session->lock); 16411b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n", 16421b2c7af8SMike Christie sc->cmnd[0], reason); 1643c07d4444SBoaz Harrosh if (!scsi_bidi_cmnd(sc)) 16441c138991SFUJITA Tomonori scsi_set_resid(sc, scsi_bufflen(sc)); 1645c07d4444SBoaz Harrosh else { 1646c07d4444SBoaz Harrosh scsi_out(sc)->resid = scsi_out(sc)->length; 1647c07d4444SBoaz Harrosh scsi_in(sc)->resid = scsi_in(sc)->length; 1648c07d4444SBoaz Harrosh } 1649052d0144SMike Christie done(sc); 16501040c99dSMike Christie spin_lock(host->host_lock); 16517996a778SMike Christie return 0; 16527996a778SMike Christie } 16537996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_queuecommand); 16547996a778SMike Christie 1655e881a172SMike Christie int iscsi_change_queue_depth(struct scsi_device *sdev, int depth, int reason) 16567996a778SMike Christie { 1657e881a172SMike Christie if (reason != SCSI_QDEPTH_DEFAULT) 1658e881a172SMike Christie return -EOPNOTSUPP; 1659e881a172SMike Christie 16607996a778SMike Christie scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); 16617996a778SMike Christie return sdev->queue_depth; 16627996a778SMike Christie } 16637996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_change_queue_depth); 16647996a778SMike Christie 16656b5d6c44SMike Christie int iscsi_target_alloc(struct scsi_target *starget) 16666b5d6c44SMike Christie { 16676b5d6c44SMike Christie struct iscsi_cls_session *cls_session = starget_to_session(starget); 16686b5d6c44SMike Christie struct iscsi_session *session = cls_session->dd_data; 16696b5d6c44SMike Christie 16706b5d6c44SMike Christie starget->can_queue = session->scsi_cmds_max; 16716b5d6c44SMike Christie return 0; 16726b5d6c44SMike Christie } 16736b5d6c44SMike Christie EXPORT_SYMBOL_GPL(iscsi_target_alloc); 16746b5d6c44SMike Christie 16757996a778SMike Christie void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session) 16767996a778SMike Christie { 167775613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 16787996a778SMike Christie 16797996a778SMike Christie spin_lock_bh(&session->lock); 16807996a778SMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) { 1681656cffc9SMike Christie session->state = ISCSI_STATE_RECOVERY_FAILED; 1682843c0a8aSMike Christie if (session->leadconn) 1683843c0a8aSMike Christie wake_up(&session->leadconn->ehwait); 16847996a778SMike Christie } 16857996a778SMike Christie spin_unlock_bh(&session->lock); 16867996a778SMike Christie } 16877996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout); 16887996a778SMike Christie 16898e124525SMike Christie int iscsi_eh_target_reset(struct scsi_cmnd *sc) 16907996a778SMike Christie { 169175613521SMike Christie struct iscsi_cls_session *cls_session; 169275613521SMike Christie struct iscsi_session *session; 169375613521SMike Christie struct iscsi_conn *conn; 169475613521SMike Christie 169575613521SMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 169675613521SMike Christie session = cls_session->dd_data; 169775613521SMike Christie conn = session->leadconn; 16987996a778SMike Christie 1699bc436b27SMike Christie mutex_lock(&session->eh_mutex); 17007996a778SMike Christie spin_lock_bh(&session->lock); 17017996a778SMike Christie if (session->state == ISCSI_STATE_TERMINATE) { 17027996a778SMike Christie failed: 1703bd2199d4SErez Zilber ISCSI_DBG_EH(session, 1704bd2199d4SErez Zilber "failing target reset: Could not log back into " 1705bd2199d4SErez Zilber "target [age %d]\n", 17061b2c7af8SMike Christie session->age); 17077996a778SMike Christie spin_unlock_bh(&session->lock); 1708bc436b27SMike Christie mutex_unlock(&session->eh_mutex); 17097996a778SMike Christie return FAILED; 17107996a778SMike Christie } 17117996a778SMike Christie 17127996a778SMike Christie spin_unlock_bh(&session->lock); 1713bc436b27SMike Christie mutex_unlock(&session->eh_mutex); 17147996a778SMike Christie /* 17157996a778SMike Christie * we drop the lock here but the leadconn cannot be destoyed while 17167996a778SMike Christie * we are in the scsi eh 17177996a778SMike Christie */ 17187996a778SMike Christie iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 17197996a778SMike Christie 1720bd2199d4SErez Zilber ISCSI_DBG_EH(session, "wait for relogin\n"); 17217996a778SMike Christie wait_event_interruptible(conn->ehwait, 17227996a778SMike Christie session->state == ISCSI_STATE_TERMINATE || 17237996a778SMike Christie session->state == ISCSI_STATE_LOGGED_IN || 1724656cffc9SMike Christie session->state == ISCSI_STATE_RECOVERY_FAILED); 17257996a778SMike Christie if (signal_pending(current)) 17267996a778SMike Christie flush_signals(current); 17277996a778SMike Christie 1728bc436b27SMike Christie mutex_lock(&session->eh_mutex); 17297996a778SMike Christie spin_lock_bh(&session->lock); 1730bd2199d4SErez Zilber if (session->state == ISCSI_STATE_LOGGED_IN) { 1731bd2199d4SErez Zilber ISCSI_DBG_EH(session, 17328e124525SMike Christie "target reset succeeded\n"); 1733bd2199d4SErez Zilber } else 17347996a778SMike Christie goto failed; 17357996a778SMike Christie spin_unlock_bh(&session->lock); 1736bc436b27SMike Christie mutex_unlock(&session->eh_mutex); 17377996a778SMike Christie return SUCCESS; 17387996a778SMike Christie } 17398e124525SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_target_reset); 17407996a778SMike Christie 1741843c0a8aSMike Christie static void iscsi_tmf_timedout(unsigned long data) 17427996a778SMike Christie { 1743843c0a8aSMike Christie struct iscsi_conn *conn = (struct iscsi_conn *)data; 17447996a778SMike Christie struct iscsi_session *session = conn->session; 17457996a778SMike Christie 17467996a778SMike Christie spin_lock(&session->lock); 1747843c0a8aSMike Christie if (conn->tmf_state == TMF_QUEUED) { 1748843c0a8aSMike Christie conn->tmf_state = TMF_TIMEDOUT; 1749bd2199d4SErez Zilber ISCSI_DBG_EH(session, "tmf timedout\n"); 17507996a778SMike Christie /* unblock eh_abort() */ 17517996a778SMike Christie wake_up(&conn->ehwait); 17527996a778SMike Christie } 17537996a778SMike Christie spin_unlock(&session->lock); 17547996a778SMike Christie } 17557996a778SMike Christie 17569c19a7d0SMike Christie static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn, 1757f6d5180cSMike Christie struct iscsi_tm *hdr, int age, 1758f6d5180cSMike Christie int timeout) 17597996a778SMike Christie { 17607996a778SMike Christie struct iscsi_session *session = conn->session; 17619c19a7d0SMike Christie struct iscsi_task *task; 17627996a778SMike Christie 17639c19a7d0SMike Christie task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr, 17647996a778SMike Christie NULL, 0); 17659c19a7d0SMike Christie if (!task) { 17666724add1SMike Christie spin_unlock_bh(&session->lock); 17677996a778SMike Christie iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 1768843c0a8aSMike Christie spin_lock_bh(&session->lock); 1769bd2199d4SErez Zilber ISCSI_DBG_EH(session, "tmf exec failure\n"); 177077a23c21SMike Christie return -EPERM; 17717996a778SMike Christie } 17727996a778SMike Christie conn->tmfcmd_pdus_cnt++; 1773f6d5180cSMike Christie conn->tmf_timer.expires = timeout * HZ + jiffies; 1774843c0a8aSMike Christie conn->tmf_timer.function = iscsi_tmf_timedout; 1775843c0a8aSMike Christie conn->tmf_timer.data = (unsigned long)conn; 1776843c0a8aSMike Christie add_timer(&conn->tmf_timer); 1777bd2199d4SErez Zilber ISCSI_DBG_EH(session, "tmf set timeout\n"); 1778843c0a8aSMike Christie 17797996a778SMike Christie spin_unlock_bh(&session->lock); 17806724add1SMike Christie mutex_unlock(&session->eh_mutex); 17817996a778SMike Christie 17827996a778SMike Christie /* 17837996a778SMike Christie * block eh thread until: 17847996a778SMike Christie * 1785843c0a8aSMike Christie * 1) tmf response 1786843c0a8aSMike Christie * 2) tmf timeout 17877996a778SMike Christie * 3) session is terminated or restarted or userspace has 17887996a778SMike Christie * given up on recovery 17897996a778SMike Christie */ 1790843c0a8aSMike Christie wait_event_interruptible(conn->ehwait, age != session->age || 17917996a778SMike Christie session->state != ISCSI_STATE_LOGGED_IN || 1792843c0a8aSMike Christie conn->tmf_state != TMF_QUEUED); 17937996a778SMike Christie if (signal_pending(current)) 17947996a778SMike Christie flush_signals(current); 1795843c0a8aSMike Christie del_timer_sync(&conn->tmf_timer); 1796843c0a8aSMike Christie 17976724add1SMike Christie mutex_lock(&session->eh_mutex); 179877a23c21SMike Christie spin_lock_bh(&session->lock); 17999c19a7d0SMike Christie /* if the session drops it will clean up the task */ 1800843c0a8aSMike Christie if (age != session->age || 1801843c0a8aSMike Christie session->state != ISCSI_STATE_LOGGED_IN) 1802843c0a8aSMike Christie return -ENOTCONN; 18037996a778SMike Christie return 0; 18047996a778SMike Christie } 18057996a778SMike Christie 18067996a778SMike Christie /* 1807843c0a8aSMike Christie * Fail commands. session lock held and recv side suspended and xmit 1808843c0a8aSMike Christie * thread flushed 1809843c0a8aSMike Christie */ 18103bbaaad9SMike Christie static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun, 18116eabafbeSMike Christie int error) 1812843c0a8aSMike Christie { 18133bbaaad9SMike Christie struct iscsi_task *task; 18143bbaaad9SMike Christie int i; 1815843c0a8aSMike Christie 18163bbaaad9SMike Christie for (i = 0; i < conn->session->cmds_max; i++) { 18173bbaaad9SMike Christie task = conn->session->cmds[i]; 18183bbaaad9SMike Christie if (!task->sc || task->state == ISCSI_TASK_FREE) 18193bbaaad9SMike Christie continue; 1820843c0a8aSMike Christie 18213bbaaad9SMike Christie if (lun != -1 && lun != task->sc->device->lun) 18223bbaaad9SMike Christie continue; 18233bbaaad9SMike Christie 18241b2c7af8SMike Christie ISCSI_DBG_SESSION(conn->session, 18253bbaaad9SMike Christie "failing sc %p itt 0x%x state %d\n", 18263bbaaad9SMike Christie task->sc, task->itt, task->state); 1827b3cd5050SMike Christie fail_scsi_task(task, error); 1828843c0a8aSMike Christie } 1829843c0a8aSMike Christie } 1830843c0a8aSMike Christie 1831661134adSMike Christie /** 1832661134adSMike Christie * iscsi_suspend_queue - suspend iscsi_queuecommand 1833661134adSMike Christie * @conn: iscsi conn to stop queueing IO on 1834661134adSMike Christie * 1835661134adSMike Christie * This grabs the session lock to make sure no one is in 1836661134adSMike Christie * xmit_task/queuecommand, and then sets suspend to prevent 1837661134adSMike Christie * new commands from being queued. This only needs to be called 1838661134adSMike Christie * by offload drivers that need to sync a path like ep disconnect 1839661134adSMike Christie * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi 1840661134adSMike Christie * will call iscsi_start_tx and iscsi_unblock_session when in FFP. 1841661134adSMike Christie */ 1842661134adSMike Christie void iscsi_suspend_queue(struct iscsi_conn *conn) 1843661134adSMike Christie { 1844661134adSMike Christie spin_lock_bh(&conn->session->lock); 1845661134adSMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 1846661134adSMike Christie spin_unlock_bh(&conn->session->lock); 1847661134adSMike Christie } 1848661134adSMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_queue); 1849661134adSMike Christie 1850661134adSMike Christie /** 1851661134adSMike Christie * iscsi_suspend_tx - suspend iscsi_data_xmit 1852661134adSMike Christie * @conn: iscsi conn tp stop processing IO on. 1853661134adSMike Christie * 1854661134adSMike Christie * This function sets the suspend bit to prevent iscsi_data_xmit 1855661134adSMike Christie * from sending new IO, and if work is queued on the xmit thread 1856661134adSMike Christie * it will wait for it to be completed. 1857661134adSMike Christie */ 1858b40977d9SMike Christie void iscsi_suspend_tx(struct iscsi_conn *conn) 18596724add1SMike Christie { 186032ae763eSMike Christie struct Scsi_Host *shost = conn->session->host; 186132ae763eSMike Christie struct iscsi_host *ihost = shost_priv(shost); 186232ae763eSMike Christie 18636724add1SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 18641336aed1SMike Christie if (ihost->workq) 186532ae763eSMike Christie flush_workqueue(ihost->workq); 18666724add1SMike Christie } 1867b40977d9SMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_tx); 18686724add1SMike Christie 18696724add1SMike Christie static void iscsi_start_tx(struct iscsi_conn *conn) 18706724add1SMike Christie { 18716724add1SMike Christie clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 187232ae763eSMike Christie iscsi_conn_queue_work(conn); 18736724add1SMike Christie } 18746724add1SMike Christie 18754c48a829SMike Christie /* 18764c48a829SMike Christie * We want to make sure a ping is in flight. It has timed out. 18774c48a829SMike Christie * And we are not busy processing a pdu that is making 18784c48a829SMike Christie * progress but got started before the ping and is taking a while 18794c48a829SMike Christie * to complete so the ping is just stuck behind it in a queue. 18804c48a829SMike Christie */ 18814c48a829SMike Christie static int iscsi_has_ping_timed_out(struct iscsi_conn *conn) 18824c48a829SMike Christie { 18834c48a829SMike Christie if (conn->ping_task && 18844c48a829SMike Christie time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) + 18854c48a829SMike Christie (conn->ping_timeout * HZ), jiffies)) 18864c48a829SMike Christie return 1; 18874c48a829SMike Christie else 18884c48a829SMike Christie return 0; 18894c48a829SMike Christie } 18904c48a829SMike Christie 1891d355e57dSMike Christie static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc) 1892f6d5180cSMike Christie { 1893d355e57dSMike Christie enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED; 1894d355e57dSMike Christie struct iscsi_task *task = NULL; 1895f6d5180cSMike Christie struct iscsi_cls_session *cls_session; 1896f6d5180cSMike Christie struct iscsi_session *session; 1897f6d5180cSMike Christie struct iscsi_conn *conn; 1898f6d5180cSMike Christie 1899d355e57dSMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 190075613521SMike Christie session = cls_session->dd_data; 1901f6d5180cSMike Christie 1902bd2199d4SErez Zilber ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc); 1903f6d5180cSMike Christie 1904f6d5180cSMike Christie spin_lock(&session->lock); 1905f6d5180cSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) { 1906f6d5180cSMike Christie /* 1907f6d5180cSMike Christie * We are probably in the middle of iscsi recovery so let 1908f6d5180cSMike Christie * that complete and handle the error. 1909f6d5180cSMike Christie */ 1910242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 1911f6d5180cSMike Christie goto done; 1912f6d5180cSMike Christie } 1913f6d5180cSMike Christie 1914f6d5180cSMike Christie conn = session->leadconn; 1915f6d5180cSMike Christie if (!conn) { 1916f6d5180cSMike Christie /* In the middle of shuting down */ 1917242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 1918f6d5180cSMike Christie goto done; 1919f6d5180cSMike Christie } 1920f6d5180cSMike Christie 1921d355e57dSMike Christie task = (struct iscsi_task *)sc->SCp.ptr; 1922d355e57dSMike Christie if (!task) 1923d355e57dSMike Christie goto done; 1924d355e57dSMike Christie /* 1925d355e57dSMike Christie * If we have sent (at least queued to the network layer) a pdu or 1926d355e57dSMike Christie * recvd one for the task since the last timeout ask for 1927d355e57dSMike Christie * more time. If on the next timeout we have not made progress 1928d355e57dSMike Christie * we can check if it is the task or connection when we send the 1929d355e57dSMike Christie * nop as a ping. 1930d355e57dSMike Christie */ 1931d355e57dSMike Christie if (time_after_eq(task->last_xfer, task->last_timeout)) { 1932bd2199d4SErez Zilber ISCSI_DBG_EH(session, "Command making progress. Asking " 1933d355e57dSMike Christie "scsi-ml for more time to complete. " 1934d355e57dSMike Christie "Last data recv at %lu. Last timeout was at " 1935d355e57dSMike Christie "%lu\n.", task->last_xfer, task->last_timeout); 1936d355e57dSMike Christie task->have_checked_conn = false; 1937d355e57dSMike Christie rc = BLK_EH_RESET_TIMER; 1938d355e57dSMike Christie goto done; 1939d355e57dSMike Christie } 1940d355e57dSMike Christie 1941f6d5180cSMike Christie if (!conn->recv_timeout && !conn->ping_timeout) 1942f6d5180cSMike Christie goto done; 1943f6d5180cSMike Christie /* 1944f6d5180cSMike Christie * if the ping timedout then we are in the middle of cleaning up 1945f6d5180cSMike Christie * and can let the iscsi eh handle it 1946f6d5180cSMike Christie */ 19474c48a829SMike Christie if (iscsi_has_ping_timed_out(conn)) { 1948242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 19494c48a829SMike Christie goto done; 19504c48a829SMike Christie } 1951d355e57dSMike Christie 1952d355e57dSMike Christie /* Assumes nop timeout is shorter than scsi cmd timeout */ 1953d355e57dSMike Christie if (task->have_checked_conn) 1954d355e57dSMike Christie goto done; 1955d355e57dSMike Christie 1956f6d5180cSMike Christie /* 1957d355e57dSMike Christie * Checking the transport already or nop from a cmd timeout still 1958d355e57dSMike Christie * running 1959f6d5180cSMike Christie */ 1960d355e57dSMike Christie if (conn->ping_task) { 1961d355e57dSMike Christie task->have_checked_conn = true; 1962242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 19634c48a829SMike Christie goto done; 19644c48a829SMike Christie } 19654c48a829SMike Christie 1966d355e57dSMike Christie /* Make sure there is a transport check done */ 1967d355e57dSMike Christie iscsi_send_nopout(conn, NULL); 1968d355e57dSMike Christie task->have_checked_conn = true; 1969242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 1970d355e57dSMike Christie 1971f6d5180cSMike Christie done: 1972d355e57dSMike Christie if (task) 1973d355e57dSMike Christie task->last_timeout = jiffies; 1974f6d5180cSMike Christie spin_unlock(&session->lock); 1975bd2199d4SErez Zilber ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ? 1976242f9dcbSJens Axboe "timer reset" : "nh"); 1977f6d5180cSMike Christie return rc; 1978f6d5180cSMike Christie } 1979f6d5180cSMike Christie 1980f6d5180cSMike Christie static void iscsi_check_transport_timeouts(unsigned long data) 1981f6d5180cSMike Christie { 1982f6d5180cSMike Christie struct iscsi_conn *conn = (struct iscsi_conn *)data; 1983f6d5180cSMike Christie struct iscsi_session *session = conn->session; 19844cf10435SMike Christie unsigned long recv_timeout, next_timeout = 0, last_recv; 1985f6d5180cSMike Christie 1986f6d5180cSMike Christie spin_lock(&session->lock); 1987f6d5180cSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) 1988f6d5180cSMike Christie goto done; 1989f6d5180cSMike Christie 19904cf10435SMike Christie recv_timeout = conn->recv_timeout; 19914cf10435SMike Christie if (!recv_timeout) 1992f6d5180cSMike Christie goto done; 1993f6d5180cSMike Christie 19944cf10435SMike Christie recv_timeout *= HZ; 1995f6d5180cSMike Christie last_recv = conn->last_recv; 19964c48a829SMike Christie 19974c48a829SMike Christie if (iscsi_has_ping_timed_out(conn)) { 1998322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs " 19994c48a829SMike Christie "expired, recv timeout %d, last rx %lu, " 20004c48a829SMike Christie "last ping %lu, now %lu\n", 20014c48a829SMike Christie conn->ping_timeout, conn->recv_timeout, 20024c48a829SMike Christie last_recv, conn->last_ping, jiffies); 2003f6d5180cSMike Christie spin_unlock(&session->lock); 2004f6d5180cSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 2005f6d5180cSMike Christie return; 2006f6d5180cSMike Christie } 2007f6d5180cSMike Christie 20084cf10435SMike Christie if (time_before_eq(last_recv + recv_timeout, jiffies)) { 2009f6d5180cSMike Christie /* send a ping to try to provoke some traffic */ 20101b2c7af8SMike Christie ISCSI_DBG_CONN(conn, "Sending nopout as ping\n"); 2011f6d5180cSMike Christie iscsi_send_nopout(conn, NULL); 20124cf10435SMike Christie next_timeout = conn->last_ping + (conn->ping_timeout * HZ); 2013ad294e9cSMike Christie } else 20144cf10435SMike Christie next_timeout = last_recv + recv_timeout; 2015f6d5180cSMike Christie 20161b2c7af8SMike Christie ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout); 2017f6d5180cSMike Christie mod_timer(&conn->transport_timer, next_timeout); 2018f6d5180cSMike Christie done: 2019f6d5180cSMike Christie spin_unlock(&session->lock); 2020f6d5180cSMike Christie } 2021f6d5180cSMike Christie 20229c19a7d0SMike Christie static void iscsi_prep_abort_task_pdu(struct iscsi_task *task, 2023843c0a8aSMike Christie struct iscsi_tm *hdr) 2024843c0a8aSMike Christie { 2025843c0a8aSMike Christie memset(hdr, 0, sizeof(*hdr)); 2026843c0a8aSMike Christie hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 2027843c0a8aSMike Christie hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK; 2028843c0a8aSMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 2029577577daSMike Christie memcpy(hdr->lun, task->lun, sizeof(hdr->lun)); 2030577577daSMike Christie hdr->rtt = task->hdr_itt; 2031577577daSMike Christie hdr->refcmdsn = task->cmdsn; 2032843c0a8aSMike Christie } 2033843c0a8aSMike Christie 20347996a778SMike Christie int iscsi_eh_abort(struct scsi_cmnd *sc) 20357996a778SMike Christie { 203675613521SMike Christie struct iscsi_cls_session *cls_session; 203775613521SMike Christie struct iscsi_session *session; 2038f47f2cf5SMike Christie struct iscsi_conn *conn; 20399c19a7d0SMike Christie struct iscsi_task *task; 2040843c0a8aSMike Christie struct iscsi_tm *hdr; 2041843c0a8aSMike Christie int rc, age; 20427996a778SMike Christie 204375613521SMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 204475613521SMike Christie session = cls_session->dd_data; 204575613521SMike Christie 2046bd2199d4SErez Zilber ISCSI_DBG_EH(session, "aborting sc %p\n", sc); 20474421c9ebSMike Christie 20486724add1SMike Christie mutex_lock(&session->eh_mutex); 20496724add1SMike Christie spin_lock_bh(&session->lock); 2050f47f2cf5SMike Christie /* 2051f47f2cf5SMike Christie * if session was ISCSI_STATE_IN_RECOVERY then we may not have 2052f47f2cf5SMike Christie * got the command. 2053f47f2cf5SMike Christie */ 2054f47f2cf5SMike Christie if (!sc->SCp.ptr) { 2055bd2199d4SErez Zilber ISCSI_DBG_EH(session, "sc never reached iscsi layer or " 20561b2c7af8SMike Christie "it completed.\n"); 20576724add1SMike Christie spin_unlock_bh(&session->lock); 20586724add1SMike Christie mutex_unlock(&session->eh_mutex); 2059f47f2cf5SMike Christie return SUCCESS; 2060f47f2cf5SMike Christie } 2061f47f2cf5SMike Christie 20627996a778SMike Christie /* 20637996a778SMike Christie * If we are not logged in or we have started a new session 20647996a778SMike Christie * then let the host reset code handle this 20657996a778SMike Christie */ 2066843c0a8aSMike Christie if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN || 2067843c0a8aSMike Christie sc->SCp.phase != session->age) { 2068843c0a8aSMike Christie spin_unlock_bh(&session->lock); 2069843c0a8aSMike Christie mutex_unlock(&session->eh_mutex); 2070bd2199d4SErez Zilber ISCSI_DBG_EH(session, "failing abort due to dropped " 20714421c9ebSMike Christie "session.\n"); 2072843c0a8aSMike Christie return FAILED; 2073843c0a8aSMike Christie } 2074843c0a8aSMike Christie 2075843c0a8aSMike Christie conn = session->leadconn; 2076843c0a8aSMike Christie conn->eh_abort_cnt++; 2077843c0a8aSMike Christie age = session->age; 2078843c0a8aSMike Christie 20799c19a7d0SMike Christie task = (struct iscsi_task *)sc->SCp.ptr; 2080bd2199d4SErez Zilber ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n", 20811b2c7af8SMike Christie sc, task->itt); 20827996a778SMike Christie 20839c19a7d0SMike Christie /* task completed before time out */ 20849c19a7d0SMike Christie if (!task->sc) { 2085bd2199d4SErez Zilber ISCSI_DBG_EH(session, "sc completed while abort in progress\n"); 208677a23c21SMike Christie goto success; 20877ea8b828SMike Christie } 20887996a778SMike Christie 20899c19a7d0SMike Christie if (task->state == ISCSI_TASK_PENDING) { 2090b3cd5050SMike Christie fail_scsi_task(task, DID_ABORT); 209177a23c21SMike Christie goto success; 209277a23c21SMike Christie } 20937996a778SMike Christie 2094843c0a8aSMike Christie /* only have one tmf outstanding at a time */ 2095843c0a8aSMike Christie if (conn->tmf_state != TMF_INITIAL) 20967996a778SMike Christie goto failed; 2097843c0a8aSMike Christie conn->tmf_state = TMF_QUEUED; 20987996a778SMike Christie 2099843c0a8aSMike Christie hdr = &conn->tmhdr; 21009c19a7d0SMike Christie iscsi_prep_abort_task_pdu(task, hdr); 2101843c0a8aSMike Christie 21029c19a7d0SMike Christie if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) { 2103843c0a8aSMike Christie rc = FAILED; 2104843c0a8aSMike Christie goto failed; 2105843c0a8aSMike Christie } 2106843c0a8aSMike Christie 2107843c0a8aSMike Christie switch (conn->tmf_state) { 2108843c0a8aSMike Christie case TMF_SUCCESS: 21097ea8b828SMike Christie spin_unlock_bh(&session->lock); 2110913e5bf4SMike Christie /* 2111913e5bf4SMike Christie * stop tx side incase the target had sent a abort rsp but 2112913e5bf4SMike Christie * the initiator was still writing out data. 2113913e5bf4SMike Christie */ 21146724add1SMike Christie iscsi_suspend_tx(conn); 21157996a778SMike Christie /* 2116913e5bf4SMike Christie * we do not stop the recv side because targets have been 2117913e5bf4SMike Christie * good and have never sent us a successful tmf response 2118913e5bf4SMike Christie * then sent more data for the cmd. 21197996a778SMike Christie */ 21206187c242SMike Christie spin_lock_bh(&session->lock); 2121b3cd5050SMike Christie fail_scsi_task(task, DID_ABORT); 2122843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 21236187c242SMike Christie spin_unlock_bh(&session->lock); 21246724add1SMike Christie iscsi_start_tx(conn); 212577a23c21SMike Christie goto success_unlocked; 2126843c0a8aSMike Christie case TMF_TIMEDOUT: 2127843c0a8aSMike Christie spin_unlock_bh(&session->lock); 2128843c0a8aSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 2129843c0a8aSMike Christie goto failed_unlocked; 2130843c0a8aSMike Christie case TMF_NOT_FOUND: 2131843c0a8aSMike Christie if (!sc->SCp.ptr) { 2132843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 21339c19a7d0SMike Christie /* task completed before tmf abort response */ 2134bd2199d4SErez Zilber ISCSI_DBG_EH(session, "sc completed while abort in " 2135bd2199d4SErez Zilber "progress\n"); 213677a23c21SMike Christie goto success; 213777a23c21SMike Christie } 213877a23c21SMike Christie /* fall through */ 213977a23c21SMike Christie default: 2140843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 2141843c0a8aSMike Christie goto failed; 214277a23c21SMike Christie } 21437996a778SMike Christie 214477a23c21SMike Christie success: 214577a23c21SMike Christie spin_unlock_bh(&session->lock); 214677a23c21SMike Christie success_unlocked: 2147bd2199d4SErez Zilber ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n", 21481b2c7af8SMike Christie sc, task->itt); 21496724add1SMike Christie mutex_unlock(&session->eh_mutex); 21507996a778SMike Christie return SUCCESS; 21517996a778SMike Christie 21527996a778SMike Christie failed: 21537996a778SMike Christie spin_unlock_bh(&session->lock); 215477a23c21SMike Christie failed_unlocked: 2155bd2199d4SErez Zilber ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc, 21569c19a7d0SMike Christie task ? task->itt : 0); 21576724add1SMike Christie mutex_unlock(&session->eh_mutex); 21587996a778SMike Christie return FAILED; 21597996a778SMike Christie } 21607996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_abort); 21617996a778SMike Christie 2162843c0a8aSMike Christie static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr) 2163843c0a8aSMike Christie { 2164843c0a8aSMike Christie memset(hdr, 0, sizeof(*hdr)); 2165843c0a8aSMike Christie hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 2166843c0a8aSMike Christie hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK; 2167843c0a8aSMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 2168843c0a8aSMike Christie int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun); 2169f6d5180cSMike Christie hdr->rtt = RESERVED_ITT; 2170843c0a8aSMike Christie } 2171843c0a8aSMike Christie 2172843c0a8aSMike Christie int iscsi_eh_device_reset(struct scsi_cmnd *sc) 2173843c0a8aSMike Christie { 217475613521SMike Christie struct iscsi_cls_session *cls_session; 217575613521SMike Christie struct iscsi_session *session; 2176843c0a8aSMike Christie struct iscsi_conn *conn; 2177843c0a8aSMike Christie struct iscsi_tm *hdr; 2178843c0a8aSMike Christie int rc = FAILED; 2179843c0a8aSMike Christie 218075613521SMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 218175613521SMike Christie session = cls_session->dd_data; 218275613521SMike Christie 2183bd2199d4SErez Zilber ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun); 2184843c0a8aSMike Christie 2185843c0a8aSMike Christie mutex_lock(&session->eh_mutex); 2186843c0a8aSMike Christie spin_lock_bh(&session->lock); 2187843c0a8aSMike Christie /* 2188843c0a8aSMike Christie * Just check if we are not logged in. We cannot check for 2189843c0a8aSMike Christie * the phase because the reset could come from a ioctl. 2190843c0a8aSMike Christie */ 2191843c0a8aSMike Christie if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) 2192843c0a8aSMike Christie goto unlock; 2193843c0a8aSMike Christie conn = session->leadconn; 2194843c0a8aSMike Christie 2195843c0a8aSMike Christie /* only have one tmf outstanding at a time */ 2196843c0a8aSMike Christie if (conn->tmf_state != TMF_INITIAL) 2197843c0a8aSMike Christie goto unlock; 2198843c0a8aSMike Christie conn->tmf_state = TMF_QUEUED; 2199843c0a8aSMike Christie 2200843c0a8aSMike Christie hdr = &conn->tmhdr; 2201843c0a8aSMike Christie iscsi_prep_lun_reset_pdu(sc, hdr); 2202843c0a8aSMike Christie 22039c19a7d0SMike Christie if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age, 2204f6d5180cSMike Christie session->lu_reset_timeout)) { 2205843c0a8aSMike Christie rc = FAILED; 2206843c0a8aSMike Christie goto unlock; 2207843c0a8aSMike Christie } 2208843c0a8aSMike Christie 2209843c0a8aSMike Christie switch (conn->tmf_state) { 2210843c0a8aSMike Christie case TMF_SUCCESS: 2211843c0a8aSMike Christie break; 2212843c0a8aSMike Christie case TMF_TIMEDOUT: 2213843c0a8aSMike Christie spin_unlock_bh(&session->lock); 2214843c0a8aSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 2215843c0a8aSMike Christie goto done; 2216843c0a8aSMike Christie default: 2217843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 2218843c0a8aSMike Christie goto unlock; 2219843c0a8aSMike Christie } 2220843c0a8aSMike Christie 2221843c0a8aSMike Christie rc = SUCCESS; 2222843c0a8aSMike Christie spin_unlock_bh(&session->lock); 2223843c0a8aSMike Christie 2224843c0a8aSMike Christie iscsi_suspend_tx(conn); 2225913e5bf4SMike Christie 2226a3439148SMike Christie spin_lock_bh(&session->lock); 22273bbaaad9SMike Christie fail_scsi_tasks(conn, sc->device->lun, DID_ERROR); 2228843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 2229a3439148SMike Christie spin_unlock_bh(&session->lock); 2230843c0a8aSMike Christie 2231843c0a8aSMike Christie iscsi_start_tx(conn); 2232843c0a8aSMike Christie goto done; 2233843c0a8aSMike Christie 2234843c0a8aSMike Christie unlock: 2235843c0a8aSMike Christie spin_unlock_bh(&session->lock); 2236843c0a8aSMike Christie done: 2237bd2199d4SErez Zilber ISCSI_DBG_EH(session, "dev reset result = %s\n", 2238843c0a8aSMike Christie rc == SUCCESS ? "SUCCESS" : "FAILED"); 2239843c0a8aSMike Christie mutex_unlock(&session->eh_mutex); 2240843c0a8aSMike Christie return rc; 2241843c0a8aSMike Christie } 2242843c0a8aSMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_device_reset); 2243843c0a8aSMike Christie 22446320377fSOlaf Kirch /* 22456320377fSOlaf Kirch * Pre-allocate a pool of @max items of @item_size. By default, the pool 22466320377fSOlaf Kirch * should be accessed via kfifo_{get,put} on q->queue. 22476320377fSOlaf Kirch * Optionally, the caller can obtain the array of object pointers 22486320377fSOlaf Kirch * by passing in a non-NULL @items pointer 22496320377fSOlaf Kirch */ 22507996a778SMike Christie int 22516320377fSOlaf Kirch iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size) 22527996a778SMike Christie { 22536320377fSOlaf Kirch int i, num_arrays = 1; 22547996a778SMike Christie 22556320377fSOlaf Kirch memset(q, 0, sizeof(*q)); 22567996a778SMike Christie 22577996a778SMike Christie q->max = max; 22586320377fSOlaf Kirch 22596320377fSOlaf Kirch /* If the user passed an items pointer, he wants a copy of 22606320377fSOlaf Kirch * the array. */ 22616320377fSOlaf Kirch if (items) 22626320377fSOlaf Kirch num_arrays++; 22636320377fSOlaf Kirch q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL); 22646320377fSOlaf Kirch if (q->pool == NULL) 2265f474a37bSJean Delvare return -ENOMEM; 22667996a778SMike Christie 22677996a778SMike Christie q->queue = kfifo_init((void*)q->pool, max * sizeof(void*), 22687996a778SMike Christie GFP_KERNEL, NULL); 2269fd6e1c14SJean Delvare if (IS_ERR(q->queue)) { 2270fd6e1c14SJean Delvare q->queue = NULL; 22716320377fSOlaf Kirch goto enomem; 2272fd6e1c14SJean Delvare } 22737996a778SMike Christie 22747996a778SMike Christie for (i = 0; i < max; i++) { 22756320377fSOlaf Kirch q->pool[i] = kzalloc(item_size, GFP_KERNEL); 22767996a778SMike Christie if (q->pool[i] == NULL) { 22776320377fSOlaf Kirch q->max = i; 22786320377fSOlaf Kirch goto enomem; 22797996a778SMike Christie } 22807996a778SMike Christie __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*)); 22817996a778SMike Christie } 22826320377fSOlaf Kirch 22836320377fSOlaf Kirch if (items) { 22846320377fSOlaf Kirch *items = q->pool + max; 22856320377fSOlaf Kirch memcpy(*items, q->pool, max * sizeof(void *)); 22866320377fSOlaf Kirch } 22876320377fSOlaf Kirch 22887996a778SMike Christie return 0; 22896320377fSOlaf Kirch 22906320377fSOlaf Kirch enomem: 22916320377fSOlaf Kirch iscsi_pool_free(q); 22926320377fSOlaf Kirch return -ENOMEM; 22937996a778SMike Christie } 22947996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_init); 22957996a778SMike Christie 22966320377fSOlaf Kirch void iscsi_pool_free(struct iscsi_pool *q) 22977996a778SMike Christie { 22987996a778SMike Christie int i; 22997996a778SMike Christie 23007996a778SMike Christie for (i = 0; i < q->max; i++) 23016320377fSOlaf Kirch kfree(q->pool[i]); 23027996a778SMike Christie kfree(q->pool); 23032f5899a3SMike Christie kfree(q->queue); 23047996a778SMike Christie } 23057996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_free); 23067996a778SMike Christie 2307a4804cd6SMike Christie /** 2308a4804cd6SMike Christie * iscsi_host_add - add host to system 2309a4804cd6SMike Christie * @shost: scsi host 2310a4804cd6SMike Christie * @pdev: parent device 2311a4804cd6SMike Christie * 2312a4804cd6SMike Christie * This should be called by partial offload and software iscsi drivers 2313a4804cd6SMike Christie * to add a host to the system. 2314a4804cd6SMike Christie */ 2315a4804cd6SMike Christie int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev) 23167996a778SMike Christie { 23178e9a20ceSMike Christie if (!shost->can_queue) 23188e9a20ceSMike Christie shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX; 23198e9a20ceSMike Christie 23204d108350SMike Christie if (!shost->cmd_per_lun) 23214d108350SMike Christie shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN; 23224d108350SMike Christie 2323308cec14SMike Christie if (!shost->transportt->eh_timed_out) 2324308cec14SMike Christie shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out; 2325a4804cd6SMike Christie return scsi_add_host(shost, pdev); 2326a4804cd6SMike Christie } 2327a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_add); 2328a4804cd6SMike Christie 2329a4804cd6SMike Christie /** 2330a4804cd6SMike Christie * iscsi_host_alloc - allocate a host and driver data 2331a4804cd6SMike Christie * @sht: scsi host template 2332a4804cd6SMike Christie * @dd_data_size: driver host data size 233332ae763eSMike Christie * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue 2334a4804cd6SMike Christie * 2335a4804cd6SMike Christie * This should be called by partial offload and software iscsi drivers. 2336a4804cd6SMike Christie * To access the driver specific memory use the iscsi_host_priv() macro. 2337a4804cd6SMike Christie */ 2338a4804cd6SMike Christie struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht, 23394d108350SMike Christie int dd_data_size, bool xmit_can_sleep) 2340a4804cd6SMike Christie { 2341a4804cd6SMike Christie struct Scsi_Host *shost; 2342e5bd7b54SMike Christie struct iscsi_host *ihost; 2343a4804cd6SMike Christie 2344a4804cd6SMike Christie shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size); 2345a4804cd6SMike Christie if (!shost) 2346a4804cd6SMike Christie return NULL; 2347e5bd7b54SMike Christie ihost = shost_priv(shost); 234832ae763eSMike Christie 234932ae763eSMike Christie if (xmit_can_sleep) { 235032ae763eSMike Christie snprintf(ihost->workq_name, sizeof(ihost->workq_name), 235132ae763eSMike Christie "iscsi_q_%d", shost->host_no); 235232ae763eSMike Christie ihost->workq = create_singlethread_workqueue(ihost->workq_name); 235332ae763eSMike Christie if (!ihost->workq) 235432ae763eSMike Christie goto free_host; 235532ae763eSMike Christie } 235632ae763eSMike Christie 2357e5bd7b54SMike Christie spin_lock_init(&ihost->lock); 2358e5bd7b54SMike Christie ihost->state = ISCSI_HOST_SETUP; 2359e5bd7b54SMike Christie ihost->num_sessions = 0; 2360e5bd7b54SMike Christie init_waitqueue_head(&ihost->session_removal_wq); 2361a4804cd6SMike Christie return shost; 236232ae763eSMike Christie 236332ae763eSMike Christie free_host: 236432ae763eSMike Christie scsi_host_put(shost); 236532ae763eSMike Christie return NULL; 236675613521SMike Christie } 2367a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_alloc); 236875613521SMike Christie 2369e5bd7b54SMike Christie static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session) 2370e5bd7b54SMike Christie { 237140a06e75SMike Christie iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST); 2372e5bd7b54SMike Christie } 2373e5bd7b54SMike Christie 2374a4804cd6SMike Christie /** 2375a4804cd6SMike Christie * iscsi_host_remove - remove host and sessions 2376a4804cd6SMike Christie * @shost: scsi host 2377a4804cd6SMike Christie * 2378e5bd7b54SMike Christie * If there are any sessions left, this will initiate the removal and wait 2379e5bd7b54SMike Christie * for the completion. 2380a4804cd6SMike Christie */ 2381a4804cd6SMike Christie void iscsi_host_remove(struct Scsi_Host *shost) 2382a4804cd6SMike Christie { 2383e5bd7b54SMike Christie struct iscsi_host *ihost = shost_priv(shost); 2384e5bd7b54SMike Christie unsigned long flags; 2385e5bd7b54SMike Christie 2386e5bd7b54SMike Christie spin_lock_irqsave(&ihost->lock, flags); 2387e5bd7b54SMike Christie ihost->state = ISCSI_HOST_REMOVED; 2388e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 2389e5bd7b54SMike Christie 2390e5bd7b54SMike Christie iscsi_host_for_each_session(shost, iscsi_notify_host_removed); 2391e5bd7b54SMike Christie wait_event_interruptible(ihost->session_removal_wq, 2392e5bd7b54SMike Christie ihost->num_sessions == 0); 2393e5bd7b54SMike Christie if (signal_pending(current)) 2394e5bd7b54SMike Christie flush_signals(current); 2395e5bd7b54SMike Christie 2396a4804cd6SMike Christie scsi_remove_host(shost); 239732ae763eSMike Christie if (ihost->workq) 239832ae763eSMike Christie destroy_workqueue(ihost->workq); 2399a4804cd6SMike Christie } 2400a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_remove); 2401a4804cd6SMike Christie 2402a4804cd6SMike Christie void iscsi_host_free(struct Scsi_Host *shost) 240375613521SMike Christie { 240475613521SMike Christie struct iscsi_host *ihost = shost_priv(shost); 240575613521SMike Christie 240675613521SMike Christie kfree(ihost->netdev); 240775613521SMike Christie kfree(ihost->hwaddress); 240875613521SMike Christie kfree(ihost->initiatorname); 2409a4804cd6SMike Christie scsi_host_put(shost); 241075613521SMike Christie } 2411a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_free); 241275613521SMike Christie 2413e5bd7b54SMike Christie static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost) 2414e5bd7b54SMike Christie { 2415e5bd7b54SMike Christie struct iscsi_host *ihost = shost_priv(shost); 2416e5bd7b54SMike Christie unsigned long flags; 2417e5bd7b54SMike Christie 2418e5bd7b54SMike Christie shost = scsi_host_get(shost); 2419e5bd7b54SMike Christie if (!shost) { 2420e5bd7b54SMike Christie printk(KERN_ERR "Invalid state. Cannot notify host removal " 2421e5bd7b54SMike Christie "of session teardown event because host already " 2422e5bd7b54SMike Christie "removed.\n"); 2423e5bd7b54SMike Christie return; 2424e5bd7b54SMike Christie } 2425e5bd7b54SMike Christie 2426e5bd7b54SMike Christie spin_lock_irqsave(&ihost->lock, flags); 2427e5bd7b54SMike Christie ihost->num_sessions--; 2428e5bd7b54SMike Christie if (ihost->num_sessions == 0) 2429e5bd7b54SMike Christie wake_up(&ihost->session_removal_wq); 2430e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 2431e5bd7b54SMike Christie scsi_host_put(shost); 2432e5bd7b54SMike Christie } 2433e5bd7b54SMike Christie 243475613521SMike Christie /** 243575613521SMike Christie * iscsi_session_setup - create iscsi cls session and host and session 243675613521SMike Christie * @iscsit: iscsi transport template 243775613521SMike Christie * @shost: scsi host 243875613521SMike Christie * @cmds_max: session can queue 24399c19a7d0SMike Christie * @cmd_task_size: LLD task private data size 244075613521SMike Christie * @initial_cmdsn: initial CmdSN 244175613521SMike Christie * 244275613521SMike Christie * This can be used by software iscsi_transports that allocate 244375613521SMike Christie * a session per scsi host. 24443cf7b233SMike Christie * 24453cf7b233SMike Christie * Callers should set cmds_max to the largest total numer (mgmt + scsi) of 24463cf7b233SMike Christie * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks 24473cf7b233SMike Christie * for nop handling and login/logout requests. 244875613521SMike Christie */ 244975613521SMike Christie struct iscsi_cls_session * 245075613521SMike Christie iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost, 2451b8b9e1b8SJayamohan Kallickal uint16_t cmds_max, int dd_size, int cmd_task_size, 24527970634bSMike Christie uint32_t initial_cmdsn, unsigned int id) 245375613521SMike Christie { 2454e5bd7b54SMike Christie struct iscsi_host *ihost = shost_priv(shost); 245575613521SMike Christie struct iscsi_session *session; 245675613521SMike Christie struct iscsi_cls_session *cls_session; 24573cf7b233SMike Christie int cmd_i, scsi_cmds, total_cmds = cmds_max; 2458e5bd7b54SMike Christie unsigned long flags; 2459e5bd7b54SMike Christie 2460e5bd7b54SMike Christie spin_lock_irqsave(&ihost->lock, flags); 2461e5bd7b54SMike Christie if (ihost->state == ISCSI_HOST_REMOVED) { 2462e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 2463e5bd7b54SMike Christie return NULL; 2464e5bd7b54SMike Christie } 2465e5bd7b54SMike Christie ihost->num_sessions++; 2466e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 24678e9a20ceSMike Christie 24688e9a20ceSMike Christie if (!total_cmds) 24698e9a20ceSMike Christie total_cmds = ISCSI_DEF_XMIT_CMDS_MAX; 24703e5c28adSMike Christie /* 24713cf7b233SMike Christie * The iscsi layer needs some tasks for nop handling and tmfs, 24723cf7b233SMike Christie * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX 24733cf7b233SMike Christie * + 1 command for scsi IO. 24743e5c28adSMike Christie */ 24753cf7b233SMike Christie if (total_cmds < ISCSI_TOTAL_CMDS_MIN) { 24763cf7b233SMike Christie printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " 24773cf7b233SMike Christie "must be a power of two that is at least %d.\n", 24783cf7b233SMike Christie total_cmds, ISCSI_TOTAL_CMDS_MIN); 2479e5bd7b54SMike Christie goto dec_session_count; 24801548271eSMike Christie } 24813cf7b233SMike Christie 24823cf7b233SMike Christie if (total_cmds > ISCSI_TOTAL_CMDS_MAX) { 24833cf7b233SMike Christie printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " 24843cf7b233SMike Christie "must be a power of 2 less than or equal to %d.\n", 24853cf7b233SMike Christie cmds_max, ISCSI_TOTAL_CMDS_MAX); 24863cf7b233SMike Christie total_cmds = ISCSI_TOTAL_CMDS_MAX; 24873cf7b233SMike Christie } 24883cf7b233SMike Christie 24893cf7b233SMike Christie if (!is_power_of_2(total_cmds)) { 24903cf7b233SMike Christie printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " 24913cf7b233SMike Christie "must be a power of 2.\n", total_cmds); 24923cf7b233SMike Christie total_cmds = rounddown_pow_of_two(total_cmds); 24933cf7b233SMike Christie if (total_cmds < ISCSI_TOTAL_CMDS_MIN) 24943cf7b233SMike Christie return NULL; 24953cf7b233SMike Christie printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n", 24963cf7b233SMike Christie total_cmds); 24973cf7b233SMike Christie } 24983cf7b233SMike Christie scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX; 24991548271eSMike Christie 25005d91e209SMike Christie cls_session = iscsi_alloc_session(shost, iscsit, 2501b8b9e1b8SJayamohan Kallickal sizeof(struct iscsi_session) + 2502b8b9e1b8SJayamohan Kallickal dd_size); 250375613521SMike Christie if (!cls_session) 2504e5bd7b54SMike Christie goto dec_session_count; 250575613521SMike Christie session = cls_session->dd_data; 250675613521SMike Christie session->cls_session = cls_session; 25077996a778SMike Christie session->host = shost; 25087996a778SMike Christie session->state = ISCSI_STATE_FREE; 2509f6d5180cSMike Christie session->fast_abort = 1; 25104cd49ea1SMike Christie session->lu_reset_timeout = 15; 25114cd49ea1SMike Christie session->abort_timeout = 10; 25123cf7b233SMike Christie session->scsi_cmds_max = scsi_cmds; 25133cf7b233SMike Christie session->cmds_max = total_cmds; 2514e0726407SMike Christie session->queued_cmdsn = session->cmdsn = initial_cmdsn; 25157996a778SMike Christie session->exp_cmdsn = initial_cmdsn + 1; 25167996a778SMike Christie session->max_cmdsn = initial_cmdsn + 1; 25177996a778SMike Christie session->max_r2t = 1; 25187996a778SMike Christie session->tt = iscsit; 2519b8b9e1b8SJayamohan Kallickal session->dd_data = cls_session->dd_data + sizeof(*session); 25206724add1SMike Christie mutex_init(&session->eh_mutex); 252175613521SMike Christie spin_lock_init(&session->lock); 25227996a778SMike Christie 25237996a778SMike Christie /* initialize SCSI PDU commands pool */ 25247996a778SMike Christie if (iscsi_pool_init(&session->cmdpool, session->cmds_max, 25257996a778SMike Christie (void***)&session->cmds, 25269c19a7d0SMike Christie cmd_task_size + sizeof(struct iscsi_task))) 25277996a778SMike Christie goto cmdpool_alloc_fail; 25287996a778SMike Christie 25297996a778SMike Christie /* pre-format cmds pool with ITT */ 25307996a778SMike Christie for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { 25319c19a7d0SMike Christie struct iscsi_task *task = session->cmds[cmd_i]; 25327996a778SMike Christie 25339c19a7d0SMike Christie if (cmd_task_size) 25349c19a7d0SMike Christie task->dd_data = &task[1]; 25359c19a7d0SMike Christie task->itt = cmd_i; 25363bbaaad9SMike Christie task->state = ISCSI_TASK_FREE; 25379c19a7d0SMike Christie INIT_LIST_HEAD(&task->running); 25387996a778SMike Christie } 25397996a778SMike Christie 2540f53a88daSMike Christie if (!try_module_get(iscsit->owner)) 254175613521SMike Christie goto module_get_fail; 254275613521SMike Christie 25437970634bSMike Christie if (iscsi_add_session(cls_session, id)) 2544f53a88daSMike Christie goto cls_session_fail; 2545e5bd7b54SMike Christie 25467996a778SMike Christie return cls_session; 25477996a778SMike Christie 25487996a778SMike Christie cls_session_fail: 254975613521SMike Christie module_put(iscsit->owner); 255075613521SMike Christie module_get_fail: 25516320377fSOlaf Kirch iscsi_pool_free(&session->cmdpool); 25527996a778SMike Christie cmdpool_alloc_fail: 255375613521SMike Christie iscsi_free_session(cls_session); 2554e5bd7b54SMike Christie dec_session_count: 2555e5bd7b54SMike Christie iscsi_host_dec_session_cnt(shost); 25567996a778SMike Christie return NULL; 25577996a778SMike Christie } 25587996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_setup); 25597996a778SMike Christie 25607996a778SMike Christie /** 25617996a778SMike Christie * iscsi_session_teardown - destroy session, host, and cls_session 256275613521SMike Christie * @cls_session: iscsi session 25637996a778SMike Christie * 256475613521SMike Christie * The driver must have called iscsi_remove_session before 256575613521SMike Christie * calling this. 256675613521SMike Christie */ 25677996a778SMike Christie void iscsi_session_teardown(struct iscsi_cls_session *cls_session) 25687996a778SMike Christie { 256975613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 257063f75cc8SMike Christie struct module *owner = cls_session->transport->owner; 2571e5bd7b54SMike Christie struct Scsi_Host *shost = session->host; 25727996a778SMike Christie 25736320377fSOlaf Kirch iscsi_pool_free(&session->cmdpool); 25747996a778SMike Christie 2575b2c64167SMike Christie kfree(session->password); 2576b2c64167SMike Christie kfree(session->password_in); 2577b2c64167SMike Christie kfree(session->username); 2578b2c64167SMike Christie kfree(session->username_in); 2579f3ff0c36SMike Christie kfree(session->targetname); 258088dfd340SMike Christie kfree(session->initiatorname); 258188dfd340SMike Christie kfree(session->ifacename); 2582f3ff0c36SMike Christie 258375613521SMike Christie iscsi_destroy_session(cls_session); 2584e5bd7b54SMike Christie iscsi_host_dec_session_cnt(shost); 258563f75cc8SMike Christie module_put(owner); 25867996a778SMike Christie } 25877996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_teardown); 25887996a778SMike Christie 25897996a778SMike Christie /** 25907996a778SMike Christie * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn 25917996a778SMike Christie * @cls_session: iscsi_cls_session 25925d91e209SMike Christie * @dd_size: private driver data size 25937996a778SMike Christie * @conn_idx: cid 25945d91e209SMike Christie */ 25957996a778SMike Christie struct iscsi_cls_conn * 25965d91e209SMike Christie iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size, 25975d91e209SMike Christie uint32_t conn_idx) 25987996a778SMike Christie { 259975613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 26007996a778SMike Christie struct iscsi_conn *conn; 26017996a778SMike Christie struct iscsi_cls_conn *cls_conn; 2602d36ab6f3SMike Christie char *data; 26037996a778SMike Christie 26045d91e209SMike Christie cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size, 26055d91e209SMike Christie conn_idx); 26067996a778SMike Christie if (!cls_conn) 26077996a778SMike Christie return NULL; 26087996a778SMike Christie conn = cls_conn->dd_data; 26095d91e209SMike Christie memset(conn, 0, sizeof(*conn) + dd_size); 26107996a778SMike Christie 26115d91e209SMike Christie conn->dd_data = cls_conn->dd_data + sizeof(*conn); 26127996a778SMike Christie conn->session = session; 26137996a778SMike Christie conn->cls_conn = cls_conn; 26147996a778SMike Christie conn->c_stage = ISCSI_CONN_INITIAL_STAGE; 26157996a778SMike Christie conn->id = conn_idx; 26167996a778SMike Christie conn->exp_statsn = 0; 2617843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 2618f6d5180cSMike Christie 2619f6d5180cSMike Christie init_timer(&conn->transport_timer); 2620f6d5180cSMike Christie conn->transport_timer.data = (unsigned long)conn; 2621f6d5180cSMike Christie conn->transport_timer.function = iscsi_check_transport_timeouts; 2622f6d5180cSMike Christie 2623843c0a8aSMike Christie INIT_LIST_HEAD(&conn->mgmtqueue); 26243bbaaad9SMike Christie INIT_LIST_HEAD(&conn->cmdqueue); 2625843c0a8aSMike Christie INIT_LIST_HEAD(&conn->requeue); 2626c4028958SDavid Howells INIT_WORK(&conn->xmitwork, iscsi_xmitworker); 26277996a778SMike Christie 26289c19a7d0SMike Christie /* allocate login_task used for the login/text sequences */ 26297996a778SMike Christie spin_lock_bh(&session->lock); 26303e5c28adSMike Christie if (!__kfifo_get(session->cmdpool.queue, 26319c19a7d0SMike Christie (void*)&conn->login_task, 26327996a778SMike Christie sizeof(void*))) { 26337996a778SMike Christie spin_unlock_bh(&session->lock); 26349c19a7d0SMike Christie goto login_task_alloc_fail; 26357996a778SMike Christie } 26367996a778SMike Christie spin_unlock_bh(&session->lock); 26377996a778SMike Christie 2638cfeb2cf9SMike Christie data = (char *) __get_free_pages(GFP_KERNEL, 2639cfeb2cf9SMike Christie get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); 2640d36ab6f3SMike Christie if (!data) 26419c19a7d0SMike Christie goto login_task_data_alloc_fail; 26429c19a7d0SMike Christie conn->login_task->data = conn->data = data; 2643d36ab6f3SMike Christie 2644843c0a8aSMike Christie init_timer(&conn->tmf_timer); 26457996a778SMike Christie init_waitqueue_head(&conn->ehwait); 26467996a778SMike Christie 26477996a778SMike Christie return cls_conn; 26487996a778SMike Christie 26499c19a7d0SMike Christie login_task_data_alloc_fail: 26509c19a7d0SMike Christie __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task, 2651d36ab6f3SMike Christie sizeof(void*)); 26529c19a7d0SMike Christie login_task_alloc_fail: 26537996a778SMike Christie iscsi_destroy_conn(cls_conn); 26547996a778SMike Christie return NULL; 26557996a778SMike Christie } 26567996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_setup); 26577996a778SMike Christie 26587996a778SMike Christie /** 26597996a778SMike Christie * iscsi_conn_teardown - teardown iscsi connection 26607996a778SMike Christie * cls_conn: iscsi class connection 26617996a778SMike Christie * 26627996a778SMike Christie * TODO: we may need to make this into a two step process 26637996a778SMike Christie * like scsi-mls remove + put host 26647996a778SMike Christie */ 26657996a778SMike Christie void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn) 26667996a778SMike Christie { 26677996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 26687996a778SMike Christie struct iscsi_session *session = conn->session; 26697996a778SMike Christie unsigned long flags; 26707996a778SMike Christie 2671f6d5180cSMike Christie del_timer_sync(&conn->transport_timer); 2672f6d5180cSMike Christie 26737996a778SMike Christie spin_lock_bh(&session->lock); 26747996a778SMike Christie conn->c_stage = ISCSI_CONN_CLEANUP_WAIT; 26757996a778SMike Christie if (session->leadconn == conn) { 26767996a778SMike Christie /* 26777996a778SMike Christie * leading connection? then give up on recovery. 26787996a778SMike Christie */ 26797996a778SMike Christie session->state = ISCSI_STATE_TERMINATE; 26807996a778SMike Christie wake_up(&conn->ehwait); 26817996a778SMike Christie } 26827996a778SMike Christie spin_unlock_bh(&session->lock); 26837996a778SMike Christie 26847996a778SMike Christie /* 26857996a778SMike Christie * Block until all in-progress commands for this connection 26867996a778SMike Christie * time out or fail. 26877996a778SMike Christie */ 26887996a778SMike Christie for (;;) { 26897996a778SMike Christie spin_lock_irqsave(session->host->host_lock, flags); 26907996a778SMike Christie if (!session->host->host_busy) { /* OK for ERL == 0 */ 26917996a778SMike Christie spin_unlock_irqrestore(session->host->host_lock, flags); 26927996a778SMike Christie break; 26937996a778SMike Christie } 26947996a778SMike Christie spin_unlock_irqrestore(session->host->host_lock, flags); 26957996a778SMike Christie msleep_interruptible(500); 2696322d739dSMike Christie iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): " 2697322d739dSMike Christie "host_busy %d host_failed %d\n", 2698322d739dSMike Christie session->host->host_busy, 2699be2df72eSOr Gerlitz session->host->host_failed); 27007996a778SMike Christie /* 27017996a778SMike Christie * force eh_abort() to unblock 27027996a778SMike Christie */ 27037996a778SMike Christie wake_up(&conn->ehwait); 27047996a778SMike Christie } 27057996a778SMike Christie 2706779ea120SMike Christie /* flush queued up work because we free the connection below */ 2707843c0a8aSMike Christie iscsi_suspend_tx(conn); 2708779ea120SMike Christie 27097996a778SMike Christie spin_lock_bh(&session->lock); 2710cfeb2cf9SMike Christie free_pages((unsigned long) conn->data, 2711cfeb2cf9SMike Christie get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); 2712f3ff0c36SMike Christie kfree(conn->persistent_address); 27139c19a7d0SMike Christie __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task, 27147996a778SMike Christie sizeof(void*)); 2715e0726407SMike Christie if (session->leadconn == conn) 27167996a778SMike Christie session->leadconn = NULL; 27177996a778SMike Christie spin_unlock_bh(&session->lock); 27187996a778SMike Christie 27197996a778SMike Christie iscsi_destroy_conn(cls_conn); 27207996a778SMike Christie } 27217996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_teardown); 27227996a778SMike Christie 27237996a778SMike Christie int iscsi_conn_start(struct iscsi_cls_conn *cls_conn) 27247996a778SMike Christie { 27257996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 27267996a778SMike Christie struct iscsi_session *session = conn->session; 27277996a778SMike Christie 2728ffd0436eSMike Christie if (!session) { 2729322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 2730322d739dSMike Christie "can't start unbound connection\n"); 27317996a778SMike Christie return -EPERM; 27327996a778SMike Christie } 27337996a778SMike Christie 2734db98ccdeSMike Christie if ((session->imm_data_en || !session->initial_r2t_en) && 2735db98ccdeSMike Christie session->first_burst > session->max_burst) { 2736322d739dSMike Christie iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: " 2737ffd0436eSMike Christie "first_burst %d max_burst %d\n", 2738ffd0436eSMike Christie session->first_burst, session->max_burst); 2739ffd0436eSMike Christie return -EINVAL; 2740ffd0436eSMike Christie } 2741ffd0436eSMike Christie 2742f6d5180cSMike Christie if (conn->ping_timeout && !conn->recv_timeout) { 2743322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of " 2744322d739dSMike Christie "zero. Using 5 seconds\n."); 2745f6d5180cSMike Christie conn->recv_timeout = 5; 2746f6d5180cSMike Christie } 2747f6d5180cSMike Christie 2748f6d5180cSMike Christie if (conn->recv_timeout && !conn->ping_timeout) { 2749322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of " 2750322d739dSMike Christie "zero. Using 5 seconds.\n"); 2751f6d5180cSMike Christie conn->ping_timeout = 5; 2752f6d5180cSMike Christie } 2753f6d5180cSMike Christie 27547996a778SMike Christie spin_lock_bh(&session->lock); 27557996a778SMike Christie conn->c_stage = ISCSI_CONN_STARTED; 27567996a778SMike Christie session->state = ISCSI_STATE_LOGGED_IN; 2757e0726407SMike Christie session->queued_cmdsn = session->cmdsn; 27587996a778SMike Christie 2759f6d5180cSMike Christie conn->last_recv = jiffies; 2760f6d5180cSMike Christie conn->last_ping = jiffies; 2761f6d5180cSMike Christie if (conn->recv_timeout && conn->ping_timeout) 2762f6d5180cSMike Christie mod_timer(&conn->transport_timer, 2763f6d5180cSMike Christie jiffies + (conn->recv_timeout * HZ)); 2764f6d5180cSMike Christie 27657996a778SMike Christie switch(conn->stop_stage) { 27667996a778SMike Christie case STOP_CONN_RECOVER: 27677996a778SMike Christie /* 27687996a778SMike Christie * unblock eh_abort() if it is blocked. re-try all 27697996a778SMike Christie * commands after successful recovery 27707996a778SMike Christie */ 27717996a778SMike Christie conn->stop_stage = 0; 2772843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 27737996a778SMike Christie session->age++; 27748b1d0343SMike Christie if (session->age == 16) 27758b1d0343SMike Christie session->age = 0; 27766eabafbeSMike Christie break; 27777996a778SMike Christie case STOP_CONN_TERM: 27787996a778SMike Christie conn->stop_stage = 0; 27797996a778SMike Christie break; 27807996a778SMike Christie default: 27817996a778SMike Christie break; 27827996a778SMike Christie } 27837996a778SMike Christie spin_unlock_bh(&session->lock); 27847996a778SMike Christie 278575613521SMike Christie iscsi_unblock_session(session->cls_session); 27866eabafbeSMike Christie wake_up(&conn->ehwait); 27877996a778SMike Christie return 0; 27887996a778SMike Christie } 27897996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_start); 27907996a778SMike Christie 27917996a778SMike Christie static void 27923bbaaad9SMike Christie fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn) 27937996a778SMike Christie { 27943bbaaad9SMike Christie struct iscsi_task *task; 2795b3cd5050SMike Christie int i, state; 27967996a778SMike Christie 27973bbaaad9SMike Christie for (i = 0; i < conn->session->cmds_max; i++) { 27983bbaaad9SMike Christie task = conn->session->cmds[i]; 27993bbaaad9SMike Christie if (task->sc) 28003bbaaad9SMike Christie continue; 28013bbaaad9SMike Christie 28023bbaaad9SMike Christie if (task->state == ISCSI_TASK_FREE) 28033bbaaad9SMike Christie continue; 28043bbaaad9SMike Christie 28053bbaaad9SMike Christie ISCSI_DBG_SESSION(conn->session, 28063bbaaad9SMike Christie "failing mgmt itt 0x%x state %d\n", 28073bbaaad9SMike Christie task->itt, task->state); 2808b3cd5050SMike Christie state = ISCSI_TASK_ABRT_SESS_RECOV; 2809b3cd5050SMike Christie if (task->state == ISCSI_TASK_PENDING) 2810b3cd5050SMike Christie state = ISCSI_TASK_COMPLETED; 2811b3cd5050SMike Christie iscsi_complete_task(task, state); 2812b3cd5050SMike Christie 28137996a778SMike Christie } 28147996a778SMike Christie } 28157996a778SMike Christie 2816656cffc9SMike Christie static void iscsi_start_session_recovery(struct iscsi_session *session, 28177996a778SMike Christie struct iscsi_conn *conn, int flag) 28187996a778SMike Christie { 2819ed2abc7fSMike Christie int old_stop_stage; 2820ed2abc7fSMike Christie 28216724add1SMike Christie mutex_lock(&session->eh_mutex); 28227996a778SMike Christie spin_lock_bh(&session->lock); 2823ed2abc7fSMike Christie if (conn->stop_stage == STOP_CONN_TERM) { 28247996a778SMike Christie spin_unlock_bh(&session->lock); 28256724add1SMike Christie mutex_unlock(&session->eh_mutex); 28266724add1SMike Christie return; 28276724add1SMike Christie } 28286724add1SMike Christie 28296724add1SMike Christie /* 2830ed2abc7fSMike Christie * When this is called for the in_login state, we only want to clean 28319c19a7d0SMike Christie * up the login task and connection. We do not need to block and set 283267a61114SMike Christie * the recovery state again 2833ed2abc7fSMike Christie */ 283467a61114SMike Christie if (flag == STOP_CONN_TERM) 283567a61114SMike Christie session->state = ISCSI_STATE_TERMINATE; 283667a61114SMike Christie else if (conn->stop_stage != STOP_CONN_RECOVER) 283767a61114SMike Christie session->state = ISCSI_STATE_IN_RECOVERY; 283826013ad4SMike Christie spin_unlock_bh(&session->lock); 2839ed2abc7fSMike Christie 284026013ad4SMike Christie del_timer_sync(&conn->transport_timer); 284126013ad4SMike Christie iscsi_suspend_tx(conn); 284226013ad4SMike Christie 284326013ad4SMike Christie spin_lock_bh(&session->lock); 2844ed2abc7fSMike Christie old_stop_stage = conn->stop_stage; 28457996a778SMike Christie conn->stop_stage = flag; 284667a61114SMike Christie conn->c_stage = ISCSI_CONN_STOPPED; 28477996a778SMike Christie spin_unlock_bh(&session->lock); 28486724add1SMike Christie 28497996a778SMike Christie /* 28507996a778SMike Christie * for connection level recovery we should not calculate 28517996a778SMike Christie * header digest. conn->hdr_size used for optimization 28527996a778SMike Christie * in hdr_extract() and will be re-negotiated at 28537996a778SMike Christie * set_param() time. 28547996a778SMike Christie */ 28557996a778SMike Christie if (flag == STOP_CONN_RECOVER) { 28567996a778SMike Christie conn->hdrdgst_en = 0; 28577996a778SMike Christie conn->datadgst_en = 0; 2858656cffc9SMike Christie if (session->state == ISCSI_STATE_IN_RECOVERY && 285967a61114SMike Christie old_stop_stage != STOP_CONN_RECOVER) { 28601b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "blocking session\n"); 286175613521SMike Christie iscsi_block_session(session->cls_session); 28627996a778SMike Christie } 286367a61114SMike Christie } 2864656cffc9SMike Christie 2865656cffc9SMike Christie /* 2866656cffc9SMike Christie * flush queues. 2867656cffc9SMike Christie */ 2868656cffc9SMike Christie spin_lock_bh(&session->lock); 28693bbaaad9SMike Christie fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED); 28703bbaaad9SMike Christie fail_mgmt_tasks(session, conn); 2871656cffc9SMike Christie spin_unlock_bh(&session->lock); 28726724add1SMike Christie mutex_unlock(&session->eh_mutex); 28737996a778SMike Christie } 28747996a778SMike Christie 28757996a778SMike Christie void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) 28767996a778SMike Christie { 28777996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 28787996a778SMike Christie struct iscsi_session *session = conn->session; 28797996a778SMike Christie 28807996a778SMike Christie switch (flag) { 28817996a778SMike Christie case STOP_CONN_RECOVER: 28827996a778SMike Christie case STOP_CONN_TERM: 28837996a778SMike Christie iscsi_start_session_recovery(session, conn, flag); 28848d2860b3SMike Christie break; 28857996a778SMike Christie default: 2886322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 2887322d739dSMike Christie "invalid stop flag %d\n", flag); 28887996a778SMike Christie } 28897996a778SMike Christie } 28907996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_stop); 28917996a778SMike Christie 28927996a778SMike Christie int iscsi_conn_bind(struct iscsi_cls_session *cls_session, 28937996a778SMike Christie struct iscsi_cls_conn *cls_conn, int is_leading) 28947996a778SMike Christie { 289575613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 289698644047SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 28977996a778SMike Christie 28987996a778SMike Christie spin_lock_bh(&session->lock); 28997996a778SMike Christie if (is_leading) 29007996a778SMike Christie session->leadconn = conn; 290198644047SMike Christie spin_unlock_bh(&session->lock); 29027996a778SMike Christie 29037996a778SMike Christie /* 29047996a778SMike Christie * Unblock xmitworker(), Login Phase will pass through. 29057996a778SMike Christie */ 29067996a778SMike Christie clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); 29077996a778SMike Christie clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 29087996a778SMike Christie return 0; 29097996a778SMike Christie } 29107996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_bind); 29117996a778SMike Christie 29125700b1afSMike Christie static int iscsi_switch_str_param(char **param, char *new_val_buf) 29135700b1afSMike Christie { 29145700b1afSMike Christie char *new_val; 29155700b1afSMike Christie 29165700b1afSMike Christie if (*param) { 29175700b1afSMike Christie if (!strcmp(*param, new_val_buf)) 29185700b1afSMike Christie return 0; 29195700b1afSMike Christie } 29205700b1afSMike Christie 29215700b1afSMike Christie new_val = kstrdup(new_val_buf, GFP_NOIO); 29225700b1afSMike Christie if (!new_val) 29235700b1afSMike Christie return -ENOMEM; 29245700b1afSMike Christie 29255700b1afSMike Christie kfree(*param); 29265700b1afSMike Christie *param = new_val; 29275700b1afSMike Christie return 0; 29285700b1afSMike Christie } 2929a54a52caSMike Christie 2930a54a52caSMike Christie int iscsi_set_param(struct iscsi_cls_conn *cls_conn, 2931a54a52caSMike Christie enum iscsi_param param, char *buf, int buflen) 2932a54a52caSMike Christie { 2933a54a52caSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 2934a54a52caSMike Christie struct iscsi_session *session = conn->session; 2935a54a52caSMike Christie uint32_t value; 2936a54a52caSMike Christie 2937a54a52caSMike Christie switch(param) { 2938843c0a8aSMike Christie case ISCSI_PARAM_FAST_ABORT: 2939843c0a8aSMike Christie sscanf(buf, "%d", &session->fast_abort); 2940843c0a8aSMike Christie break; 2941f6d5180cSMike Christie case ISCSI_PARAM_ABORT_TMO: 2942f6d5180cSMike Christie sscanf(buf, "%d", &session->abort_timeout); 2943f6d5180cSMike Christie break; 2944f6d5180cSMike Christie case ISCSI_PARAM_LU_RESET_TMO: 2945f6d5180cSMike Christie sscanf(buf, "%d", &session->lu_reset_timeout); 2946f6d5180cSMike Christie break; 2947f6d5180cSMike Christie case ISCSI_PARAM_PING_TMO: 2948f6d5180cSMike Christie sscanf(buf, "%d", &conn->ping_timeout); 2949f6d5180cSMike Christie break; 2950f6d5180cSMike Christie case ISCSI_PARAM_RECV_TMO: 2951f6d5180cSMike Christie sscanf(buf, "%d", &conn->recv_timeout); 2952f6d5180cSMike Christie break; 2953a54a52caSMike Christie case ISCSI_PARAM_MAX_RECV_DLENGTH: 2954a54a52caSMike Christie sscanf(buf, "%d", &conn->max_recv_dlength); 2955a54a52caSMike Christie break; 2956a54a52caSMike Christie case ISCSI_PARAM_MAX_XMIT_DLENGTH: 2957a54a52caSMike Christie sscanf(buf, "%d", &conn->max_xmit_dlength); 2958a54a52caSMike Christie break; 2959a54a52caSMike Christie case ISCSI_PARAM_HDRDGST_EN: 2960a54a52caSMike Christie sscanf(buf, "%d", &conn->hdrdgst_en); 2961a54a52caSMike Christie break; 2962a54a52caSMike Christie case ISCSI_PARAM_DATADGST_EN: 2963a54a52caSMike Christie sscanf(buf, "%d", &conn->datadgst_en); 2964a54a52caSMike Christie break; 2965a54a52caSMike Christie case ISCSI_PARAM_INITIAL_R2T_EN: 2966a54a52caSMike Christie sscanf(buf, "%d", &session->initial_r2t_en); 2967a54a52caSMike Christie break; 2968a54a52caSMike Christie case ISCSI_PARAM_MAX_R2T: 2969a54a52caSMike Christie sscanf(buf, "%d", &session->max_r2t); 2970a54a52caSMike Christie break; 2971a54a52caSMike Christie case ISCSI_PARAM_IMM_DATA_EN: 2972a54a52caSMike Christie sscanf(buf, "%d", &session->imm_data_en); 2973a54a52caSMike Christie break; 2974a54a52caSMike Christie case ISCSI_PARAM_FIRST_BURST: 2975a54a52caSMike Christie sscanf(buf, "%d", &session->first_burst); 2976a54a52caSMike Christie break; 2977a54a52caSMike Christie case ISCSI_PARAM_MAX_BURST: 2978a54a52caSMike Christie sscanf(buf, "%d", &session->max_burst); 2979a54a52caSMike Christie break; 2980a54a52caSMike Christie case ISCSI_PARAM_PDU_INORDER_EN: 2981a54a52caSMike Christie sscanf(buf, "%d", &session->pdu_inorder_en); 2982a54a52caSMike Christie break; 2983a54a52caSMike Christie case ISCSI_PARAM_DATASEQ_INORDER_EN: 2984a54a52caSMike Christie sscanf(buf, "%d", &session->dataseq_inorder_en); 2985a54a52caSMike Christie break; 2986a54a52caSMike Christie case ISCSI_PARAM_ERL: 2987a54a52caSMike Christie sscanf(buf, "%d", &session->erl); 2988a54a52caSMike Christie break; 2989a54a52caSMike Christie case ISCSI_PARAM_IFMARKER_EN: 2990a54a52caSMike Christie sscanf(buf, "%d", &value); 2991a54a52caSMike Christie BUG_ON(value); 2992a54a52caSMike Christie break; 2993a54a52caSMike Christie case ISCSI_PARAM_OFMARKER_EN: 2994a54a52caSMike Christie sscanf(buf, "%d", &value); 2995a54a52caSMike Christie BUG_ON(value); 2996a54a52caSMike Christie break; 2997a54a52caSMike Christie case ISCSI_PARAM_EXP_STATSN: 2998a54a52caSMike Christie sscanf(buf, "%u", &conn->exp_statsn); 2999a54a52caSMike Christie break; 3000b2c64167SMike Christie case ISCSI_PARAM_USERNAME: 30015700b1afSMike Christie return iscsi_switch_str_param(&session->username, buf); 3002b2c64167SMike Christie case ISCSI_PARAM_USERNAME_IN: 30035700b1afSMike Christie return iscsi_switch_str_param(&session->username_in, buf); 3004b2c64167SMike Christie case ISCSI_PARAM_PASSWORD: 30055700b1afSMike Christie return iscsi_switch_str_param(&session->password, buf); 3006b2c64167SMike Christie case ISCSI_PARAM_PASSWORD_IN: 30075700b1afSMike Christie return iscsi_switch_str_param(&session->password_in, buf); 3008a54a52caSMike Christie case ISCSI_PARAM_TARGET_NAME: 30095700b1afSMike Christie return iscsi_switch_str_param(&session->targetname, buf); 3010a54a52caSMike Christie case ISCSI_PARAM_TPGT: 3011a54a52caSMike Christie sscanf(buf, "%d", &session->tpgt); 3012a54a52caSMike Christie break; 3013a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_PORT: 3014a54a52caSMike Christie sscanf(buf, "%d", &conn->persistent_port); 3015a54a52caSMike Christie break; 3016a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_ADDRESS: 30175700b1afSMike Christie return iscsi_switch_str_param(&conn->persistent_address, buf); 301888dfd340SMike Christie case ISCSI_PARAM_IFACE_NAME: 30195700b1afSMike Christie return iscsi_switch_str_param(&session->ifacename, buf); 302088dfd340SMike Christie case ISCSI_PARAM_INITIATOR_NAME: 30215700b1afSMike Christie return iscsi_switch_str_param(&session->initiatorname, buf); 3022a54a52caSMike Christie default: 3023a54a52caSMike Christie return -ENOSYS; 3024a54a52caSMike Christie } 3025a54a52caSMike Christie 3026a54a52caSMike Christie return 0; 3027a54a52caSMike Christie } 3028a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_set_param); 3029a54a52caSMike Christie 3030a54a52caSMike Christie int iscsi_session_get_param(struct iscsi_cls_session *cls_session, 3031a54a52caSMike Christie enum iscsi_param param, char *buf) 3032a54a52caSMike Christie { 303375613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 3034a54a52caSMike Christie int len; 3035a54a52caSMike Christie 3036a54a52caSMike Christie switch(param) { 3037843c0a8aSMike Christie case ISCSI_PARAM_FAST_ABORT: 3038843c0a8aSMike Christie len = sprintf(buf, "%d\n", session->fast_abort); 3039843c0a8aSMike Christie break; 3040f6d5180cSMike Christie case ISCSI_PARAM_ABORT_TMO: 3041f6d5180cSMike Christie len = sprintf(buf, "%d\n", session->abort_timeout); 3042f6d5180cSMike Christie break; 3043f6d5180cSMike Christie case ISCSI_PARAM_LU_RESET_TMO: 3044f6d5180cSMike Christie len = sprintf(buf, "%d\n", session->lu_reset_timeout); 3045f6d5180cSMike Christie break; 3046a54a52caSMike Christie case ISCSI_PARAM_INITIAL_R2T_EN: 3047a54a52caSMike Christie len = sprintf(buf, "%d\n", session->initial_r2t_en); 3048a54a52caSMike Christie break; 3049a54a52caSMike Christie case ISCSI_PARAM_MAX_R2T: 3050a54a52caSMike Christie len = sprintf(buf, "%hu\n", session->max_r2t); 3051a54a52caSMike Christie break; 3052a54a52caSMike Christie case ISCSI_PARAM_IMM_DATA_EN: 3053a54a52caSMike Christie len = sprintf(buf, "%d\n", session->imm_data_en); 3054a54a52caSMike Christie break; 3055a54a52caSMike Christie case ISCSI_PARAM_FIRST_BURST: 3056a54a52caSMike Christie len = sprintf(buf, "%u\n", session->first_burst); 3057a54a52caSMike Christie break; 3058a54a52caSMike Christie case ISCSI_PARAM_MAX_BURST: 3059a54a52caSMike Christie len = sprintf(buf, "%u\n", session->max_burst); 3060a54a52caSMike Christie break; 3061a54a52caSMike Christie case ISCSI_PARAM_PDU_INORDER_EN: 3062a54a52caSMike Christie len = sprintf(buf, "%d\n", session->pdu_inorder_en); 3063a54a52caSMike Christie break; 3064a54a52caSMike Christie case ISCSI_PARAM_DATASEQ_INORDER_EN: 3065a54a52caSMike Christie len = sprintf(buf, "%d\n", session->dataseq_inorder_en); 3066a54a52caSMike Christie break; 3067a54a52caSMike Christie case ISCSI_PARAM_ERL: 3068a54a52caSMike Christie len = sprintf(buf, "%d\n", session->erl); 3069a54a52caSMike Christie break; 3070a54a52caSMike Christie case ISCSI_PARAM_TARGET_NAME: 3071a54a52caSMike Christie len = sprintf(buf, "%s\n", session->targetname); 3072a54a52caSMike Christie break; 3073a54a52caSMike Christie case ISCSI_PARAM_TPGT: 3074a54a52caSMike Christie len = sprintf(buf, "%d\n", session->tpgt); 3075a54a52caSMike Christie break; 3076b2c64167SMike Christie case ISCSI_PARAM_USERNAME: 3077b2c64167SMike Christie len = sprintf(buf, "%s\n", session->username); 3078b2c64167SMike Christie break; 3079b2c64167SMike Christie case ISCSI_PARAM_USERNAME_IN: 3080b2c64167SMike Christie len = sprintf(buf, "%s\n", session->username_in); 3081b2c64167SMike Christie break; 3082b2c64167SMike Christie case ISCSI_PARAM_PASSWORD: 3083b2c64167SMike Christie len = sprintf(buf, "%s\n", session->password); 3084b2c64167SMike Christie break; 3085b2c64167SMike Christie case ISCSI_PARAM_PASSWORD_IN: 3086b2c64167SMike Christie len = sprintf(buf, "%s\n", session->password_in); 3087b2c64167SMike Christie break; 308888dfd340SMike Christie case ISCSI_PARAM_IFACE_NAME: 308988dfd340SMike Christie len = sprintf(buf, "%s\n", session->ifacename); 309088dfd340SMike Christie break; 309188dfd340SMike Christie case ISCSI_PARAM_INITIATOR_NAME: 309288dfd340SMike Christie len = sprintf(buf, "%s\n", session->initiatorname); 309388dfd340SMike Christie break; 3094a54a52caSMike Christie default: 3095a54a52caSMike Christie return -ENOSYS; 3096a54a52caSMike Christie } 3097a54a52caSMike Christie 3098a54a52caSMike Christie return len; 3099a54a52caSMike Christie } 3100a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_get_param); 3101a54a52caSMike Christie 3102a54a52caSMike Christie int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn, 3103a54a52caSMike Christie enum iscsi_param param, char *buf) 3104a54a52caSMike Christie { 3105a54a52caSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 3106a54a52caSMike Christie int len; 3107a54a52caSMike Christie 3108a54a52caSMike Christie switch(param) { 3109f6d5180cSMike Christie case ISCSI_PARAM_PING_TMO: 3110f6d5180cSMike Christie len = sprintf(buf, "%u\n", conn->ping_timeout); 3111f6d5180cSMike Christie break; 3112f6d5180cSMike Christie case ISCSI_PARAM_RECV_TMO: 3113f6d5180cSMike Christie len = sprintf(buf, "%u\n", conn->recv_timeout); 3114f6d5180cSMike Christie break; 3115a54a52caSMike Christie case ISCSI_PARAM_MAX_RECV_DLENGTH: 3116a54a52caSMike Christie len = sprintf(buf, "%u\n", conn->max_recv_dlength); 3117a54a52caSMike Christie break; 3118a54a52caSMike Christie case ISCSI_PARAM_MAX_XMIT_DLENGTH: 3119a54a52caSMike Christie len = sprintf(buf, "%u\n", conn->max_xmit_dlength); 3120a54a52caSMike Christie break; 3121a54a52caSMike Christie case ISCSI_PARAM_HDRDGST_EN: 3122a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->hdrdgst_en); 3123a54a52caSMike Christie break; 3124a54a52caSMike Christie case ISCSI_PARAM_DATADGST_EN: 3125a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->datadgst_en); 3126a54a52caSMike Christie break; 3127a54a52caSMike Christie case ISCSI_PARAM_IFMARKER_EN: 3128a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->ifmarker_en); 3129a54a52caSMike Christie break; 3130a54a52caSMike Christie case ISCSI_PARAM_OFMARKER_EN: 3131a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->ofmarker_en); 3132a54a52caSMike Christie break; 3133a54a52caSMike Christie case ISCSI_PARAM_EXP_STATSN: 3134a54a52caSMike Christie len = sprintf(buf, "%u\n", conn->exp_statsn); 3135a54a52caSMike Christie break; 3136a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_PORT: 3137a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->persistent_port); 3138a54a52caSMike Christie break; 3139a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_ADDRESS: 3140a54a52caSMike Christie len = sprintf(buf, "%s\n", conn->persistent_address); 3141a54a52caSMike Christie break; 3142a54a52caSMike Christie default: 3143a54a52caSMike Christie return -ENOSYS; 3144a54a52caSMike Christie } 3145a54a52caSMike Christie 3146a54a52caSMike Christie return len; 3147a54a52caSMike Christie } 3148a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_param); 3149a54a52caSMike Christie 31500801c242SMike Christie int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param, 31510801c242SMike Christie char *buf) 31520801c242SMike Christie { 315375613521SMike Christie struct iscsi_host *ihost = shost_priv(shost); 31540801c242SMike Christie int len; 31550801c242SMike Christie 31560801c242SMike Christie switch (param) { 3157d8196ed2SMike Christie case ISCSI_HOST_PARAM_NETDEV_NAME: 315875613521SMike Christie len = sprintf(buf, "%s\n", ihost->netdev); 3159d8196ed2SMike Christie break; 31600801c242SMike Christie case ISCSI_HOST_PARAM_HWADDRESS: 316175613521SMike Christie len = sprintf(buf, "%s\n", ihost->hwaddress); 31620801c242SMike Christie break; 31638ad5781aSMike Christie case ISCSI_HOST_PARAM_INITIATOR_NAME: 316475613521SMike Christie len = sprintf(buf, "%s\n", ihost->initiatorname); 31658ad5781aSMike Christie break; 316675613521SMike Christie case ISCSI_HOST_PARAM_IPADDRESS: 31675700b1afSMike Christie len = sprintf(buf, "%s\n", ihost->local_address); 316888dfd340SMike Christie break; 31690801c242SMike Christie default: 31700801c242SMike Christie return -ENOSYS; 31710801c242SMike Christie } 31720801c242SMike Christie 31730801c242SMike Christie return len; 31740801c242SMike Christie } 31750801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_get_param); 31760801c242SMike Christie 31770801c242SMike Christie int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param, 31780801c242SMike Christie char *buf, int buflen) 31790801c242SMike Christie { 318075613521SMike Christie struct iscsi_host *ihost = shost_priv(shost); 31810801c242SMike Christie 31820801c242SMike Christie switch (param) { 3183d8196ed2SMike Christie case ISCSI_HOST_PARAM_NETDEV_NAME: 31845700b1afSMike Christie return iscsi_switch_str_param(&ihost->netdev, buf); 31850801c242SMike Christie case ISCSI_HOST_PARAM_HWADDRESS: 31865700b1afSMike Christie return iscsi_switch_str_param(&ihost->hwaddress, buf); 31878ad5781aSMike Christie case ISCSI_HOST_PARAM_INITIATOR_NAME: 31885700b1afSMike Christie return iscsi_switch_str_param(&ihost->initiatorname, buf); 31890801c242SMike Christie default: 31900801c242SMike Christie return -ENOSYS; 31910801c242SMike Christie } 31920801c242SMike Christie 31930801c242SMike Christie return 0; 31940801c242SMike Christie } 31950801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_set_param); 31960801c242SMike Christie 31977996a778SMike Christie MODULE_AUTHOR("Mike Christie"); 31987996a778SMike Christie MODULE_DESCRIPTION("iSCSI library functions"); 31997996a778SMike Christie MODULE_LICENSE("GPL"); 3200