11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 27996a778SMike Christie /* 37996a778SMike Christie * iSCSI lib functions 47996a778SMike Christie * 57996a778SMike Christie * Copyright (C) 2006 Red Hat, Inc. All rights reserved. 67996a778SMike Christie * Copyright (C) 2004 - 2006 Mike Christie 77996a778SMike Christie * Copyright (C) 2004 - 2005 Dmitry Yusupov 87996a778SMike Christie * Copyright (C) 2004 - 2005 Alex Aizman 97996a778SMike Christie * maintained by open-iscsi@googlegroups.com 107996a778SMike Christie */ 117996a778SMike Christie #include <linux/types.h> 127996a778SMike Christie #include <linux/kfifo.h> 137996a778SMike Christie #include <linux/delay.h> 1411836572Svignesh babu #include <linux/log2.h> 155a0e3ad6STejun Heo #include <linux/slab.h> 163f07c014SIngo Molnar #include <linux/sched/signal.h> 17acf3368fSPaul Gortmaker #include <linux/module.h> 188eb00539SMike Christie #include <asm/unaligned.h> 197996a778SMike Christie #include <net/tcp.h> 207996a778SMike Christie #include <scsi/scsi_cmnd.h> 217996a778SMike Christie #include <scsi/scsi_device.h> 227996a778SMike Christie #include <scsi/scsi_eh.h> 237996a778SMike Christie #include <scsi/scsi_tcq.h> 247996a778SMike Christie #include <scsi/scsi_host.h> 257996a778SMike Christie #include <scsi/scsi.h> 267996a778SMike Christie #include <scsi/iscsi_proto.h> 277996a778SMike Christie #include <scsi/scsi_transport.h> 287996a778SMike Christie #include <scsi/scsi_transport_iscsi.h> 297996a778SMike Christie #include <scsi/libiscsi.h> 30c2332b00SFred Herard #include <trace/events/iscsi.h> 317996a778SMike Christie 32bd2199d4SErez Zilber static int iscsi_dbg_lib_conn; 33bd2199d4SErez Zilber module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int, 34bd2199d4SErez Zilber S_IRUGO | S_IWUSR); 35bd2199d4SErez Zilber MODULE_PARM_DESC(debug_libiscsi_conn, 36bd2199d4SErez Zilber "Turn on debugging for connections in libiscsi module. " 37bd2199d4SErez Zilber "Set to 1 to turn on, and zero to turn off. Default is off."); 38bd2199d4SErez Zilber 39bd2199d4SErez Zilber static int iscsi_dbg_lib_session; 40bd2199d4SErez Zilber module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int, 41bd2199d4SErez Zilber S_IRUGO | S_IWUSR); 42bd2199d4SErez Zilber MODULE_PARM_DESC(debug_libiscsi_session, 43bd2199d4SErez Zilber "Turn on debugging for sessions in libiscsi module. " 44bd2199d4SErez Zilber "Set to 1 to turn on, and zero to turn off. Default is off."); 45bd2199d4SErez Zilber 46bd2199d4SErez Zilber static int iscsi_dbg_lib_eh; 47bd2199d4SErez Zilber module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int, 48bd2199d4SErez Zilber S_IRUGO | S_IWUSR); 49bd2199d4SErez Zilber MODULE_PARM_DESC(debug_libiscsi_eh, 50bd2199d4SErez Zilber "Turn on debugging for error handling in libiscsi module. " 51bd2199d4SErez Zilber "Set to 1 to turn on, and zero to turn off. Default is off."); 521b2c7af8SMike Christie 531b2c7af8SMike Christie #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \ 541b2c7af8SMike Christie do { \ 55bd2199d4SErez Zilber if (iscsi_dbg_lib_conn) \ 561b2c7af8SMike Christie iscsi_conn_printk(KERN_INFO, _conn, \ 571b2c7af8SMike Christie "%s " dbg_fmt, \ 581b2c7af8SMike Christie __func__, ##arg); \ 59c2332b00SFred Herard iscsi_dbg_trace(trace_iscsi_dbg_conn, \ 60c2332b00SFred Herard &(_conn)->cls_conn->dev, \ 61c2332b00SFred Herard "%s " dbg_fmt, __func__, ##arg);\ 621b2c7af8SMike Christie } while (0); 631b2c7af8SMike Christie 641b2c7af8SMike Christie #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \ 651b2c7af8SMike Christie do { \ 66bd2199d4SErez Zilber if (iscsi_dbg_lib_session) \ 67bd2199d4SErez Zilber iscsi_session_printk(KERN_INFO, _session, \ 68bd2199d4SErez Zilber "%s " dbg_fmt, \ 69bd2199d4SErez Zilber __func__, ##arg); \ 70c2332b00SFred Herard iscsi_dbg_trace(trace_iscsi_dbg_session, \ 71c2332b00SFred Herard &(_session)->cls_session->dev, \ 72c2332b00SFred Herard "%s " dbg_fmt, __func__, ##arg); \ 73bd2199d4SErez Zilber } while (0); 74bd2199d4SErez Zilber 75bd2199d4SErez Zilber #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \ 76bd2199d4SErez Zilber do { \ 77bd2199d4SErez Zilber if (iscsi_dbg_lib_eh) \ 781b2c7af8SMike Christie iscsi_session_printk(KERN_INFO, _session, \ 791b2c7af8SMike Christie "%s " dbg_fmt, \ 801b2c7af8SMike Christie __func__, ##arg); \ 81c2332b00SFred Herard iscsi_dbg_trace(trace_iscsi_dbg_eh, \ 82c2332b00SFred Herard &(_session)->cls_session->dev, \ 83c2332b00SFred Herard "%s " dbg_fmt, __func__, ##arg); \ 841b2c7af8SMike Christie } while (0); 851b2c7af8SMike Christie 86e1c6a7ecSMike Christie #define ISCSI_CMD_COMPL_WAIT 5 87e1c6a7ecSMike Christie 884b9f8ce4SMike Christie inline void iscsi_conn_queue_xmit(struct iscsi_conn *conn) 8932ae763eSMike Christie { 9032ae763eSMike Christie struct Scsi_Host *shost = conn->session->host; 9132ae763eSMike Christie struct iscsi_host *ihost = shost_priv(shost); 9232ae763eSMike Christie 931336aed1SMike Christie if (ihost->workq) 9432ae763eSMike Christie queue_work(ihost->workq, &conn->xmitwork); 9532ae763eSMike Christie } 964b9f8ce4SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_queue_xmit); 9732ae763eSMike Christie 988af80996SMike Christie inline void iscsi_conn_queue_recv(struct iscsi_conn *conn) 998af80996SMike Christie { 1008af80996SMike Christie struct Scsi_Host *shost = conn->session->host; 1018af80996SMike Christie struct iscsi_host *ihost = shost_priv(shost); 1028af80996SMike Christie 1038af80996SMike Christie if (ihost->workq && !test_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags)) 1048af80996SMike Christie queue_work(ihost->workq, &conn->recvwork); 1058af80996SMike Christie } 1068af80996SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_queue_recv); 1078af80996SMike Christie 1084c0ba5d2SMike Christie static void __iscsi_update_cmdsn(struct iscsi_session *session, 1094c0ba5d2SMike Christie uint32_t exp_cmdsn, uint32_t max_cmdsn) 1107996a778SMike Christie { 11177a23c21SMike Christie /* 11277a23c21SMike Christie * standard specifies this check for when to update expected and 11377a23c21SMike Christie * max sequence numbers 11477a23c21SMike Christie */ 11577a23c21SMike Christie if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1)) 11677a23c21SMike Christie return; 11777a23c21SMike Christie 11877a23c21SMike Christie if (exp_cmdsn != session->exp_cmdsn && 11977a23c21SMike Christie !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn)) 1207996a778SMike Christie session->exp_cmdsn = exp_cmdsn; 1217996a778SMike Christie 12277a23c21SMike Christie if (max_cmdsn != session->max_cmdsn && 12346a84c65SMike Christie !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) 12477a23c21SMike Christie session->max_cmdsn = max_cmdsn; 12577a23c21SMike Christie } 1264c0ba5d2SMike Christie 1274c0ba5d2SMike Christie void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr) 1284c0ba5d2SMike Christie { 1294c0ba5d2SMike Christie __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn), 1304c0ba5d2SMike Christie be32_to_cpu(hdr->max_cmdsn)); 1314c0ba5d2SMike Christie } 13277a23c21SMike Christie EXPORT_SYMBOL_GPL(iscsi_update_cmdsn); 1337996a778SMike Christie 134577577daSMike Christie /** 135577577daSMike Christie * iscsi_prep_data_out_pdu - initialize Data-Out 136577577daSMike Christie * @task: scsi command task 137577577daSMike Christie * @r2t: R2T info 138577577daSMike Christie * @hdr: iscsi data in pdu 139577577daSMike Christie * 140577577daSMike Christie * Notes: 141577577daSMike Christie * Initialize Data-Out within this R2T sequence and finds 142577577daSMike Christie * proper data_offset within this SCSI command. 143577577daSMike Christie * 144577577daSMike Christie * This function is called with connection lock taken. 145577577daSMike Christie **/ 146577577daSMike Christie void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t, 147ffd0436eSMike Christie struct iscsi_data *hdr) 1487996a778SMike Christie { 1499c19a7d0SMike Christie struct iscsi_conn *conn = task->conn; 150577577daSMike Christie unsigned int left = r2t->data_length - r2t->sent; 151577577daSMike Christie 152577577daSMike Christie task->hdr_len = sizeof(struct iscsi_data); 1537996a778SMike Christie 1547996a778SMike Christie memset(hdr, 0, sizeof(struct iscsi_data)); 155577577daSMike Christie hdr->ttt = r2t->ttt; 156577577daSMike Christie hdr->datasn = cpu_to_be32(r2t->datasn); 157577577daSMike Christie r2t->datasn++; 1587996a778SMike Christie hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; 15955bdabdfSAndy Grover hdr->lun = task->lun; 160577577daSMike Christie hdr->itt = task->hdr_itt; 161577577daSMike Christie hdr->exp_statsn = r2t->exp_statsn; 162577577daSMike Christie hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent); 163577577daSMike Christie if (left > conn->max_xmit_dlength) { 1647996a778SMike Christie hton24(hdr->dlength, conn->max_xmit_dlength); 165577577daSMike Christie r2t->data_count = conn->max_xmit_dlength; 1667996a778SMike Christie hdr->flags = 0; 1677996a778SMike Christie } else { 168577577daSMike Christie hton24(hdr->dlength, left); 169577577daSMike Christie r2t->data_count = left; 1707996a778SMike Christie hdr->flags = ISCSI_FLAG_CMD_FINAL; 1717996a778SMike Christie } 172577577daSMike Christie conn->dataout_pdus_cnt++; 1737996a778SMike Christie } 174577577daSMike Christie EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu); 1757996a778SMike Christie 1769c19a7d0SMike Christie static int iscsi_add_hdr(struct iscsi_task *task, unsigned len) 177004d6530SBoaz Harrosh { 1789c19a7d0SMike Christie unsigned exp_len = task->hdr_len + len; 179004d6530SBoaz Harrosh 1809c19a7d0SMike Christie if (exp_len > task->hdr_max) { 181004d6530SBoaz Harrosh WARN_ON(1); 182004d6530SBoaz Harrosh return -EINVAL; 183004d6530SBoaz Harrosh } 184004d6530SBoaz Harrosh 185004d6530SBoaz Harrosh WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */ 1869c19a7d0SMike Christie task->hdr_len = exp_len; 187004d6530SBoaz Harrosh return 0; 188004d6530SBoaz Harrosh } 189004d6530SBoaz Harrosh 19038d1c069SBoaz Harrosh /* 19138d1c069SBoaz Harrosh * make an extended cdb AHS 19238d1c069SBoaz Harrosh */ 1939c19a7d0SMike Christie static int iscsi_prep_ecdb_ahs(struct iscsi_task *task) 19438d1c069SBoaz Harrosh { 1959c19a7d0SMike Christie struct scsi_cmnd *cmd = task->sc; 19638d1c069SBoaz Harrosh unsigned rlen, pad_len; 19738d1c069SBoaz Harrosh unsigned short ahslength; 19838d1c069SBoaz Harrosh struct iscsi_ecdb_ahdr *ecdb_ahdr; 19938d1c069SBoaz Harrosh int rc; 20038d1c069SBoaz Harrosh 2019c19a7d0SMike Christie ecdb_ahdr = iscsi_next_hdr(task); 20238d1c069SBoaz Harrosh rlen = cmd->cmd_len - ISCSI_CDB_SIZE; 20338d1c069SBoaz Harrosh 20438d1c069SBoaz Harrosh BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb)); 20538d1c069SBoaz Harrosh ahslength = rlen + sizeof(ecdb_ahdr->reserved); 20638d1c069SBoaz Harrosh 20738d1c069SBoaz Harrosh pad_len = iscsi_padding(rlen); 20838d1c069SBoaz Harrosh 2099c19a7d0SMike Christie rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) + 21038d1c069SBoaz Harrosh sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len); 21138d1c069SBoaz Harrosh if (rc) 21238d1c069SBoaz Harrosh return rc; 21338d1c069SBoaz Harrosh 21438d1c069SBoaz Harrosh if (pad_len) 21538d1c069SBoaz Harrosh memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len); 21638d1c069SBoaz Harrosh 21738d1c069SBoaz Harrosh ecdb_ahdr->ahslength = cpu_to_be16(ahslength); 21838d1c069SBoaz Harrosh ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB; 21938d1c069SBoaz Harrosh ecdb_ahdr->reserved = 0; 22038d1c069SBoaz Harrosh memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen); 22138d1c069SBoaz Harrosh 2221b2c7af8SMike Christie ISCSI_DBG_SESSION(task->conn->session, 2231b2c7af8SMike Christie "iscsi_prep_ecdb_ahs: varlen_cdb_len %d " 2241b2c7af8SMike Christie "rlen %d pad_len %d ahs_length %d iscsi_headers_size " 2251b2c7af8SMike Christie "%u\n", cmd->cmd_len, rlen, pad_len, ahslength, 2261b2c7af8SMike Christie task->hdr_len); 22738d1c069SBoaz Harrosh return 0; 22838d1c069SBoaz Harrosh } 22938d1c069SBoaz Harrosh 2307996a778SMike Christie /** 2315d12c05eSMike Christie * iscsi_check_tmf_restrictions - check if a task is affected by TMF 2325d12c05eSMike Christie * @task: iscsi task 2335d12c05eSMike Christie * @opcode: opcode to check for 2345d12c05eSMike Christie * 2355d12c05eSMike Christie * During TMF a task has to be checked if it's affected. 2365d12c05eSMike Christie * All unrelated I/O can be passed through, but I/O to the 2375d12c05eSMike Christie * affected LUN should be restricted. 2385d12c05eSMike Christie * If 'fast_abort' is set we won't be sending any I/O to the 2395d12c05eSMike Christie * affected LUN. 2405d12c05eSMike Christie * Otherwise the target is waiting for all TTTs to be completed, 2415d12c05eSMike Christie * so we have to send all outstanding Data-Out PDUs to the target. 2425d12c05eSMike Christie */ 2435d12c05eSMike Christie static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode) 2445d12c05eSMike Christie { 245ec29d0acSMike Christie struct iscsi_session *session = task->conn->session; 246ec29d0acSMike Christie struct iscsi_tm *tmf = &session->tmhdr; 2479cb78c16SHannes Reinecke u64 hdr_lun; 2485d12c05eSMike Christie 249ec29d0acSMike Christie if (session->tmf_state == TMF_INITIAL) 2505d12c05eSMike Christie return 0; 2515d12c05eSMike Christie 2525d12c05eSMike Christie if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC) 2535d12c05eSMike Christie return 0; 2545d12c05eSMike Christie 2555d12c05eSMike Christie switch (ISCSI_TM_FUNC_VALUE(tmf)) { 2565d12c05eSMike Christie case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET: 2575d12c05eSMike Christie /* 2585d12c05eSMike Christie * Allow PDUs for unrelated LUNs 2595d12c05eSMike Christie */ 26055bdabdfSAndy Grover hdr_lun = scsilun_to_int(&tmf->lun); 2615d12c05eSMike Christie if (hdr_lun != task->sc->device->lun) 2625d12c05eSMike Christie return 0; 263df561f66SGustavo A. R. Silva fallthrough; 2643fe5ae8bSMike Christie case ISCSI_TM_FUNC_TARGET_WARM_RESET: 2655d12c05eSMike Christie /* 2665d12c05eSMike Christie * Fail all SCSI cmd PDUs 2675d12c05eSMike Christie */ 2685d12c05eSMike Christie if (opcode != ISCSI_OP_SCSI_DATA_OUT) { 269ec29d0acSMike Christie iscsi_session_printk(KERN_INFO, session, 270ec29d0acSMike Christie "task [op %x itt 0x%x/0x%x] rejected.\n", 271ec29d0acSMike Christie opcode, task->itt, task->hdr_itt); 2725d12c05eSMike Christie return -EACCES; 2735d12c05eSMike Christie } 2745d12c05eSMike Christie /* 2755d12c05eSMike Christie * And also all data-out PDUs in response to R2T 2765d12c05eSMike Christie * if fast_abort is set. 2775d12c05eSMike Christie */ 278ec29d0acSMike Christie if (session->fast_abort) { 279ec29d0acSMike Christie iscsi_session_printk(KERN_INFO, session, 280ec29d0acSMike Christie "task [op %x itt 0x%x/0x%x] fast abort.\n", 281ec29d0acSMike Christie opcode, task->itt, task->hdr_itt); 2825d12c05eSMike Christie return -EACCES; 2835d12c05eSMike Christie } 2845d12c05eSMike Christie break; 2855d12c05eSMike Christie case ISCSI_TM_FUNC_ABORT_TASK: 2865d12c05eSMike Christie /* 2875d12c05eSMike Christie * the caller has already checked if the task 2885d12c05eSMike Christie * they want to abort was in the pending queue so if 2895d12c05eSMike Christie * we are here the cmd pdu has gone out already, and 2905d12c05eSMike Christie * we will only hit this for data-outs 2915d12c05eSMike Christie */ 2925d12c05eSMike Christie if (opcode == ISCSI_OP_SCSI_DATA_OUT && 2935d12c05eSMike Christie task->hdr_itt == tmf->rtt) { 294ec29d0acSMike Christie ISCSI_DBG_SESSION(session, 2955d12c05eSMike Christie "Preventing task %x/%x from sending " 2965d12c05eSMike Christie "data-out due to abort task in " 2975d12c05eSMike Christie "progress\n", task->itt, 2985d12c05eSMike Christie task->hdr_itt); 2995d12c05eSMike Christie return -EACCES; 3005d12c05eSMike Christie } 3015d12c05eSMike Christie break; 3025d12c05eSMike Christie } 3035d12c05eSMike Christie 3045d12c05eSMike Christie return 0; 3055d12c05eSMike Christie } 3065d12c05eSMike Christie 3075d12c05eSMike Christie /** 3087996a778SMike Christie * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu 3099c19a7d0SMike Christie * @task: iscsi task 3107996a778SMike Christie * 3117996a778SMike Christie * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set 3127996a778SMike Christie * fields like dlength or final based on how much data it sends 3137996a778SMike Christie */ 3149c19a7d0SMike Christie static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task) 3157996a778SMike Christie { 3169c19a7d0SMike Christie struct iscsi_conn *conn = task->conn; 3177996a778SMike Christie struct iscsi_session *session = conn->session; 3189c19a7d0SMike Christie struct scsi_cmnd *sc = task->sc; 31912352183SNicholas Bellinger struct iscsi_scsi_req *hdr; 320d77e6535SSagi Grimberg unsigned hdrlength, cmd_len, transfer_length; 321262ef636SMike Christie itt_t itt; 322004d6530SBoaz Harrosh int rc; 3237996a778SMike Christie 3245d12c05eSMike Christie rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD); 3255d12c05eSMike Christie if (rc) 3265d12c05eSMike Christie return rc; 3275d12c05eSMike Christie 328184b57c6SMike Christie if (conn->session->tt->alloc_pdu) { 3292ff79d52SMike Christie rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD); 330577577daSMike Christie if (rc) 331577577daSMike Christie return rc; 332184b57c6SMike Christie } 33312352183SNicholas Bellinger hdr = (struct iscsi_scsi_req *)task->hdr; 334262ef636SMike Christie itt = hdr->itt; 335577577daSMike Christie memset(hdr, 0, sizeof(*hdr)); 336577577daSMike Christie 3372ff79d52SMike Christie if (session->tt->parse_pdu_itt) 3382ff79d52SMike Christie hdr->itt = task->hdr_itt = itt; 3392ff79d52SMike Christie else 3402ff79d52SMike Christie hdr->itt = task->hdr_itt = build_itt(task->itt, 3412ff79d52SMike Christie task->conn->session->age); 3429c19a7d0SMike Christie task->hdr_len = 0; 3439c19a7d0SMike Christie rc = iscsi_add_hdr(task, sizeof(*hdr)); 344004d6530SBoaz Harrosh if (rc) 345004d6530SBoaz Harrosh return rc; 3467996a778SMike Christie hdr->opcode = ISCSI_OP_SCSI_CMD; 3477996a778SMike Christie hdr->flags = ISCSI_ATTR_SIMPLE; 34855bdabdfSAndy Grover int_to_scsilun(sc->device->lun, &hdr->lun); 34955bdabdfSAndy Grover task->lun = hdr->lun; 3507996a778SMike Christie hdr->exp_statsn = cpu_to_be32(conn->exp_statsn); 35138d1c069SBoaz Harrosh cmd_len = sc->cmd_len; 35238d1c069SBoaz Harrosh if (cmd_len < ISCSI_CDB_SIZE) 35338d1c069SBoaz Harrosh memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len); 35438d1c069SBoaz Harrosh else if (cmd_len > ISCSI_CDB_SIZE) { 3559c19a7d0SMike Christie rc = iscsi_prep_ecdb_ahs(task); 35638d1c069SBoaz Harrosh if (rc) 35738d1c069SBoaz Harrosh return rc; 35838d1c069SBoaz Harrosh cmd_len = ISCSI_CDB_SIZE; 35938d1c069SBoaz Harrosh } 36038d1c069SBoaz Harrosh memcpy(hdr->cdb, sc->cmnd, cmd_len); 3617996a778SMike Christie 3629c19a7d0SMike Christie task->imm_count = 0; 36355e51edaSSagi Grimberg if (scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) 36455e51edaSSagi Grimberg task->protected = true; 36555e51edaSSagi Grimberg 366d77e6535SSagi Grimberg transfer_length = scsi_transfer_length(sc); 367d77e6535SSagi Grimberg hdr->data_length = cpu_to_be32(transfer_length); 3687996a778SMike Christie if (sc->sc_data_direction == DMA_TO_DEVICE) { 369577577daSMike Christie struct iscsi_r2t_info *r2t = &task->unsol_r2t; 370577577daSMike Christie 3717996a778SMike Christie hdr->flags |= ISCSI_FLAG_CMD_WRITE; 3727996a778SMike Christie /* 3737996a778SMike Christie * Write counters: 3747996a778SMike Christie * 3757996a778SMike Christie * imm_count bytes to be sent right after 3767996a778SMike Christie * SCSI PDU Header 3777996a778SMike Christie * 3787996a778SMike Christie * unsol_count bytes(as Data-Out) to be sent 3797996a778SMike Christie * without R2T ack right after 3807996a778SMike Christie * immediate data 3817996a778SMike Christie * 382577577daSMike Christie * r2t data_length bytes to be sent via R2T ack's 3837996a778SMike Christie * 3847996a778SMike Christie * pad_count bytes to be sent as zero-padding 3857996a778SMike Christie */ 386577577daSMike Christie memset(r2t, 0, sizeof(*r2t)); 3877996a778SMike Christie 3887996a778SMike Christie if (session->imm_data_en) { 389d77e6535SSagi Grimberg if (transfer_length >= session->first_burst) 3909c19a7d0SMike Christie task->imm_count = min(session->first_burst, 3917996a778SMike Christie conn->max_xmit_dlength); 3927996a778SMike Christie else 393d77e6535SSagi Grimberg task->imm_count = min(transfer_length, 3947996a778SMike Christie conn->max_xmit_dlength); 3959c19a7d0SMike Christie hton24(hdr->dlength, task->imm_count); 3967996a778SMike Christie } else 397a8ac6311SOlaf Kirch zero_data(hdr->dlength); 3987996a778SMike Christie 399ffd0436eSMike Christie if (!session->initial_r2t_en) { 400d77e6535SSagi Grimberg r2t->data_length = min(session->first_burst, 401d77e6535SSagi Grimberg transfer_length) - 402577577daSMike Christie task->imm_count; 403577577daSMike Christie r2t->data_offset = task->imm_count; 404577577daSMike Christie r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG); 405577577daSMike Christie r2t->exp_statsn = cpu_to_be32(conn->exp_statsn); 406ffd0436eSMike Christie } 407ffd0436eSMike Christie 408577577daSMike Christie if (!task->unsol_r2t.data_length) 4097996a778SMike Christie /* No unsolicit Data-Out's */ 410a8ac6311SOlaf Kirch hdr->flags |= ISCSI_FLAG_CMD_FINAL; 4117996a778SMike Christie } else { 4127996a778SMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 4137996a778SMike Christie zero_data(hdr->dlength); 4147996a778SMike Christie 4157996a778SMike Christie if (sc->sc_data_direction == DMA_FROM_DEVICE) 4167996a778SMike Christie hdr->flags |= ISCSI_FLAG_CMD_READ; 4177996a778SMike Christie } 4187996a778SMike Christie 419004d6530SBoaz Harrosh /* calculate size of additional header segments (AHSs) */ 4209c19a7d0SMike Christie hdrlength = task->hdr_len - sizeof(*hdr); 421004d6530SBoaz Harrosh 422004d6530SBoaz Harrosh WARN_ON(hdrlength & (ISCSI_PAD_LEN-1)); 423004d6530SBoaz Harrosh hdrlength /= ISCSI_PAD_LEN; 424004d6530SBoaz Harrosh 425004d6530SBoaz Harrosh WARN_ON(hdrlength >= 256); 426004d6530SBoaz Harrosh hdr->hlength = hdrlength & 0xFF; 42796b1f96dSMike Christie hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn); 428004d6530SBoaz Harrosh 429577577daSMike Christie if (session->tt->init_task && session->tt->init_task(task)) 430052d0144SMike Christie return -EIO; 431052d0144SMike Christie 4329c19a7d0SMike Christie task->state = ISCSI_TASK_RUNNING; 433d3305f34SMike Christie session->cmdsn++; 43477a23c21SMike Christie 435a8ac6311SOlaf Kirch conn->scsicmd_pdus_cnt++; 4361b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x " 437ae3d56d8SChristoph Hellwig "itt 0x%x len %d cmdsn %d win %d]\n", 4381b2c7af8SMike Christie sc->sc_data_direction == DMA_TO_DEVICE ? 4391b2c7af8SMike Christie "write" : "read", conn->id, sc, sc->cmnd[0], 440d77e6535SSagi Grimberg task->itt, transfer_length, 4411b2c7af8SMike Christie session->cmdsn, 4421b2c7af8SMike Christie session->max_cmdsn - session->exp_cmdsn + 1); 443004d6530SBoaz Harrosh return 0; 4447996a778SMike Christie } 4457996a778SMike Christie 4467996a778SMike Christie /** 4473bbaaad9SMike Christie * iscsi_free_task - free a task 4489c19a7d0SMike Christie * @task: iscsi cmd task 4497996a778SMike Christie * 450659743b0SShlomo Pongratz * Must be called with session back_lock. 4513e5c28adSMike Christie * This function returns the scsi command to scsi-ml or cleans 4523e5c28adSMike Christie * up mgmt tasks then returns the task to the pool. 4537996a778SMike Christie */ 4543bbaaad9SMike Christie static void iscsi_free_task(struct iscsi_task *task) 4557996a778SMike Christie { 4569c19a7d0SMike Christie struct iscsi_conn *conn = task->conn; 457c1635cb7SMike Christie struct iscsi_session *session = conn->session; 4589c19a7d0SMike Christie struct scsi_cmnd *sc = task->sc; 459f41d4721SMike Christie int oldstate = task->state; 4607996a778SMike Christie 4614421c9ebSMike Christie ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n", 4624421c9ebSMike Christie task->itt, task->state, task->sc); 4634421c9ebSMike Christie 464577577daSMike Christie session->tt->cleanup_task(task); 4653bbaaad9SMike Christie task->state = ISCSI_TASK_FREE; 4669c19a7d0SMike Christie task->sc = NULL; 4673e5c28adSMike Christie /* 4689c19a7d0SMike Christie * login task is preallocated so do not free 4693e5c28adSMike Christie */ 4709c19a7d0SMike Christie if (conn->login_task == task) 4713e5c28adSMike Christie return; 4723e5c28adSMike Christie 4737acd72ebSStefani Seibold kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*)); 4743e5c28adSMike Christie 4753e5c28adSMike Christie if (sc) { 476f47f2cf5SMike Christie /* SCSI eh reuses commands to verify us */ 477db22de3eSBart Van Assche iscsi_cmd(sc)->task = NULL; 4783e5c28adSMike Christie /* 479f41d4721SMike Christie * queue command may call this to free the task, so 480f41d4721SMike Christie * it will decide how to return sc to scsi-ml. 4813e5c28adSMike Christie */ 482f41d4721SMike Christie if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ) 483b4b84edcSBart Van Assche scsi_done(sc); 4847996a778SMike Christie } 4853e5c28adSMike Christie } 4867996a778SMike Christie 487e1c6a7ecSMike Christie bool iscsi_get_task(struct iscsi_task *task) 48860ecebf5SMike Christie { 489e1c6a7ecSMike Christie return refcount_inc_not_zero(&task->refcount); 49060ecebf5SMike Christie } 491e1c6a7ecSMike Christie EXPORT_SYMBOL_GPL(iscsi_get_task); 49260ecebf5SMike Christie 4936d626150SMike Christie /** 4946d626150SMike Christie * __iscsi_put_task - drop the refcount on a task 4956d626150SMike Christie * @task: iscsi_task to drop the refcount on 4966d626150SMike Christie * 4976d626150SMike Christie * The back_lock must be held when calling in case it frees the task. 4986d626150SMike Christie */ 4998eea2f55SEddie Wai void __iscsi_put_task(struct iscsi_task *task) 50060ecebf5SMike Christie { 5016dc618cdSElena Reshetova if (refcount_dec_and_test(&task->refcount)) 5023bbaaad9SMike Christie iscsi_free_task(task); 50360ecebf5SMike Christie } 5048eea2f55SEddie Wai EXPORT_SYMBOL_GPL(__iscsi_put_task); 50560ecebf5SMike Christie 5069c19a7d0SMike Christie void iscsi_put_task(struct iscsi_task *task) 5073e5c28adSMike Christie { 5089c19a7d0SMike Christie struct iscsi_session *session = task->conn->session; 5093e5c28adSMike Christie 5106d626150SMike Christie if (refcount_dec_and_test(&task->refcount)) { 511659743b0SShlomo Pongratz spin_lock_bh(&session->back_lock); 5126d626150SMike Christie iscsi_free_task(task); 513659743b0SShlomo Pongratz spin_unlock_bh(&session->back_lock); 5143e5c28adSMike Christie } 5156d626150SMike Christie } 5169c19a7d0SMike Christie EXPORT_SYMBOL_GPL(iscsi_put_task); 5173e5c28adSMike Christie 5183bbaaad9SMike Christie /** 5193bbaaad9SMike Christie * iscsi_complete_task - finish a task 5203bbaaad9SMike Christie * @task: iscsi cmd task 521b3cd5050SMike Christie * @state: state to complete task with 5223bbaaad9SMike Christie * 523659743b0SShlomo Pongratz * Must be called with session back_lock. 524b3a7ea8dSMike Christie */ 525b3cd5050SMike Christie static void iscsi_complete_task(struct iscsi_task *task, int state) 526b3a7ea8dSMike Christie { 5273bbaaad9SMike Christie struct iscsi_conn *conn = task->conn; 5283bbaaad9SMike Christie 5294421c9ebSMike Christie ISCSI_DBG_SESSION(conn->session, 5304421c9ebSMike Christie "complete task itt 0x%x state %d sc %p\n", 5314421c9ebSMike Christie task->itt, task->state, task->sc); 532b3cd5050SMike Christie if (task->state == ISCSI_TASK_COMPLETED || 533b3cd5050SMike Christie task->state == ISCSI_TASK_ABRT_TMF || 534f41d4721SMike Christie task->state == ISCSI_TASK_ABRT_SESS_RECOV || 535f41d4721SMike Christie task->state == ISCSI_TASK_REQUEUE_SCSIQ) 5363bbaaad9SMike Christie return; 5373bbaaad9SMike Christie WARN_ON_ONCE(task->state == ISCSI_TASK_FREE); 538b3cd5050SMike Christie task->state = state; 5393bbaaad9SMike Christie 540fe0a8a95SLee Duncan if (READ_ONCE(conn->ping_task) == task) 541fe0a8a95SLee Duncan WRITE_ONCE(conn->ping_task, NULL); 5423bbaaad9SMike Christie 5433bbaaad9SMike Christie /* release get from queueing */ 5443bbaaad9SMike Christie __iscsi_put_task(task); 5453bbaaad9SMike Christie } 5463bbaaad9SMike Christie 5474c0ba5d2SMike Christie /** 5484c0ba5d2SMike Christie * iscsi_complete_scsi_task - finish scsi task normally 5494c0ba5d2SMike Christie * @task: iscsi task for scsi cmd 5504c0ba5d2SMike Christie * @exp_cmdsn: expected cmd sn in cpu format 5514c0ba5d2SMike Christie * @max_cmdsn: max cmd sn in cpu format 5524c0ba5d2SMike Christie * 5534c0ba5d2SMike Christie * This is used when drivers do not need or cannot perform 5544c0ba5d2SMike Christie * lower level pdu processing. 5554c0ba5d2SMike Christie * 556659743b0SShlomo Pongratz * Called with session back_lock 5574c0ba5d2SMike Christie */ 5584c0ba5d2SMike Christie void iscsi_complete_scsi_task(struct iscsi_task *task, 5594c0ba5d2SMike Christie uint32_t exp_cmdsn, uint32_t max_cmdsn) 5604c0ba5d2SMike Christie { 5614c0ba5d2SMike Christie struct iscsi_conn *conn = task->conn; 5624c0ba5d2SMike Christie 5634c0ba5d2SMike Christie ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt); 5644c0ba5d2SMike Christie 5654c0ba5d2SMike Christie conn->last_recv = jiffies; 5664c0ba5d2SMike Christie __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn); 5674c0ba5d2SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 5684c0ba5d2SMike Christie } 5694c0ba5d2SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task); 5704c0ba5d2SMike Christie 5715923d64bSMike Christie /* 5725923d64bSMike Christie * Must be called with back and frwd lock 5735923d64bSMike Christie */ 5745923d64bSMike Christie static bool cleanup_queued_task(struct iscsi_task *task) 5755923d64bSMike Christie { 5765923d64bSMike Christie struct iscsi_conn *conn = task->conn; 5775923d64bSMike Christie bool early_complete = false; 5785923d64bSMike Christie 579533ac412SMike Christie /* 580533ac412SMike Christie * We might have raced where we handled a R2T early and got a response 581533ac412SMike Christie * but have not yet taken the task off the requeue list, then a TMF or 582533ac412SMike Christie * recovery happened and so we can still see it here. 583533ac412SMike Christie */ 5845923d64bSMike Christie if (task->state == ISCSI_TASK_COMPLETED) 5855923d64bSMike Christie early_complete = true; 5865923d64bSMike Christie 5875923d64bSMike Christie if (!list_empty(&task->running)) { 5885923d64bSMike Christie list_del_init(&task->running); 5895923d64bSMike Christie /* 590533ac412SMike Christie * If it's on a list but still running this could be cleanup 591533ac412SMike Christie * from a TMF or session recovery. 5925923d64bSMike Christie */ 5935923d64bSMike Christie if (task->state == ISCSI_TASK_RUNNING || 5945923d64bSMike Christie task->state == ISCSI_TASK_COMPLETED) 5955923d64bSMike Christie __iscsi_put_task(task); 5965923d64bSMike Christie } 5975923d64bSMike Christie 59899b06033SMike Christie if (conn->session->running_aborted_task == task) { 59999b06033SMike Christie conn->session->running_aborted_task = NULL; 60099b06033SMike Christie __iscsi_put_task(task); 60199b06033SMike Christie } 60299b06033SMike Christie 6035923d64bSMike Christie if (conn->task == task) { 6045923d64bSMike Christie conn->task = NULL; 6055923d64bSMike Christie __iscsi_put_task(task); 6065923d64bSMike Christie } 6075923d64bSMike Christie 6085923d64bSMike Christie return early_complete; 6095923d64bSMike Christie } 6104c0ba5d2SMike Christie 6113bbaaad9SMike Christie /* 612e1c6a7ecSMike Christie * session back and frwd lock must be held and if not called for a task that 613e1c6a7ecSMike Christie * is still pending or from the xmit thread, then xmit thread must be suspended 6143bbaaad9SMike Christie */ 615e1c6a7ecSMike Christie static void __fail_scsi_task(struct iscsi_task *task, int err) 6163bbaaad9SMike Christie { 6173bbaaad9SMike Christie struct iscsi_conn *conn = task->conn; 618b3a7ea8dSMike Christie struct scsi_cmnd *sc; 619b3cd5050SMike Christie int state; 620b3a7ea8dSMike Christie 621e1c6a7ecSMike Christie if (cleanup_queued_task(task)) 6225923d64bSMike Christie return; 6235923d64bSMike Christie 624b3cd5050SMike Christie if (task->state == ISCSI_TASK_PENDING) { 625b3a7ea8dSMike Christie /* 626b3a7ea8dSMike Christie * cmd never made it to the xmit thread, so we should not count 627b3a7ea8dSMike Christie * the cmd in the sequencing 628b3a7ea8dSMike Christie */ 629b3a7ea8dSMike Christie conn->session->queued_cmdsn--; 630b3cd5050SMike Christie /* it was never sent so just complete like normal */ 631b3cd5050SMike Christie state = ISCSI_TASK_COMPLETED; 632b3cd5050SMike Christie } else if (err == DID_TRANSPORT_DISRUPTED) 633b3cd5050SMike Christie state = ISCSI_TASK_ABRT_SESS_RECOV; 634b3cd5050SMike Christie else 635b3cd5050SMike Christie state = ISCSI_TASK_ABRT_TMF; 636b3a7ea8dSMike Christie 63714936b1eSMike Christie sc = task->sc; 638b3cd5050SMike Christie sc->result = err << 16; 639b3a7ea8dSMike Christie scsi_set_resid(sc, scsi_bufflen(sc)); 640b3cd5050SMike Christie iscsi_complete_task(task, state); 641e1c6a7ecSMike Christie } 642e1c6a7ecSMike Christie 643e1c6a7ecSMike Christie static void fail_scsi_task(struct iscsi_task *task, int err) 644e1c6a7ecSMike Christie { 645e1c6a7ecSMike Christie struct iscsi_session *session = task->conn->session; 646e1c6a7ecSMike Christie 647e1c6a7ecSMike Christie spin_lock_bh(&session->back_lock); 648e1c6a7ecSMike Christie __fail_scsi_task(task, err); 649e1c6a7ecSMike Christie spin_unlock_bh(&session->back_lock); 650b3a7ea8dSMike Christie } 651b3a7ea8dSMike Christie 6523e5c28adSMike Christie static int iscsi_prep_mgmt_task(struct iscsi_conn *conn, 6539c19a7d0SMike Christie struct iscsi_task *task) 654052d0144SMike Christie { 655052d0144SMike Christie struct iscsi_session *session = conn->session; 656577577daSMike Christie struct iscsi_hdr *hdr = task->hdr; 657052d0144SMike Christie struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr; 6584f704dc0SMike Christie uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK; 659052d0144SMike Christie 660052d0144SMike Christie if (conn->session->state == ISCSI_STATE_LOGGING_OUT) 661052d0144SMike Christie return -ENOTCONN; 662052d0144SMike Christie 6634f704dc0SMike Christie if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT) 664052d0144SMike Christie nop->exp_statsn = cpu_to_be32(conn->exp_statsn); 665052d0144SMike Christie /* 666052d0144SMike Christie * pre-format CmdSN for outgoing PDU. 667052d0144SMike Christie */ 668052d0144SMike Christie nop->cmdsn = cpu_to_be32(session->cmdsn); 669052d0144SMike Christie if (hdr->itt != RESERVED_ITT) { 670052d0144SMike Christie /* 6714f704dc0SMike Christie * TODO: We always use immediate for normal session pdus. 672052d0144SMike Christie * If we start to send tmfs or nops as non-immediate then 673052d0144SMike Christie * we should start checking the cmdsn numbers for mgmt tasks. 6744f704dc0SMike Christie * 6754f704dc0SMike Christie * During discovery sessions iscsid sends TEXT as non immediate, 6764f704dc0SMike Christie * but we always only send one PDU at a time. 677052d0144SMike Christie */ 678052d0144SMike Christie if (conn->c_stage == ISCSI_CONN_STARTED && 679052d0144SMike Christie !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { 680052d0144SMike Christie session->queued_cmdsn++; 681052d0144SMike Christie session->cmdsn++; 682052d0144SMike Christie } 683052d0144SMike Christie } 684052d0144SMike Christie 685ae15f801SMike Christie if (session->tt->init_task && session->tt->init_task(task)) 686ae15f801SMike Christie return -EIO; 687052d0144SMike Christie 688052d0144SMike Christie if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) 689052d0144SMike Christie session->state = ISCSI_STATE_LOGGING_OUT; 690052d0144SMike Christie 691577577daSMike Christie task->state = ISCSI_TASK_RUNNING; 6921b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x " 6931b2c7af8SMike Christie "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK, 6941b2c7af8SMike Christie hdr->itt, task->data_count); 695052d0144SMike Christie return 0; 696052d0144SMike Christie } 697052d0144SMike Christie 6986e637b72SMike Christie /** 6996e637b72SMike Christie * iscsi_alloc_mgmt_task - allocate and setup a mgmt task. 7006e637b72SMike Christie * @conn: iscsi conn that the task will be sent on. 7016e637b72SMike Christie * @hdr: iscsi pdu that will be sent. 7026e637b72SMike Christie * @data: buffer for data segment if needed. 7036e637b72SMike Christie * @data_size: length of data in bytes. 7046e637b72SMike Christie */ 7059c19a7d0SMike Christie static struct iscsi_task * 7066e637b72SMike Christie iscsi_alloc_mgmt_task(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 707f6d5180cSMike Christie char *data, uint32_t data_size) 708f6d5180cSMike Christie { 709f6d5180cSMike Christie struct iscsi_session *session = conn->session; 7104f704dc0SMike Christie uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK; 7119c19a7d0SMike Christie struct iscsi_task *task; 712262ef636SMike Christie itt_t itt; 713f6d5180cSMike Christie 71444ac9710SMike Christie if (session->state == ISCSI_STATE_TERMINATE || 71544ac9710SMike Christie !test_bit(ISCSI_CONN_FLAG_BOUND, &conn->flags)) 716f6d5180cSMike Christie return NULL; 717f6d5180cSMike Christie 7184f704dc0SMike Christie if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) { 719f6d5180cSMike Christie /* 720f6d5180cSMike Christie * Login and Text are sent serially, in 721f6d5180cSMike Christie * request-followed-by-response sequence. 7223e5c28adSMike Christie * Same task can be used. Same ITT must be used. 7233e5c28adSMike Christie * Note that login_task is preallocated at conn_create(). 724f6d5180cSMike Christie */ 7254f704dc0SMike Christie if (conn->login_task->state != ISCSI_TASK_FREE) { 7264f704dc0SMike Christie iscsi_conn_printk(KERN_ERR, conn, "Login/Text in " 7274f704dc0SMike Christie "progress. Cannot start new task.\n"); 7284f704dc0SMike Christie return NULL; 7294f704dc0SMike Christie } 7304f704dc0SMike Christie 731cbaa4221SMike Christie if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) { 732cbaa4221SMike Christie iscsi_conn_printk(KERN_ERR, conn, "Invalid buffer len of %u for login task. Max len is %u\n", data_size, ISCSI_DEF_MAX_RECV_SEG_LEN); 733cbaa4221SMike Christie return NULL; 734cbaa4221SMike Christie } 735cbaa4221SMike Christie 7369c19a7d0SMike Christie task = conn->login_task; 7374f704dc0SMike Christie } else { 73826013ad4SMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) 73926013ad4SMike Christie return NULL; 74026013ad4SMike Christie 741cbaa4221SMike Christie if (data_size != 0) { 742cbaa4221SMike Christie iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode); 743cbaa4221SMike Christie return NULL; 744cbaa4221SMike Christie } 745cbaa4221SMike Christie 746f6d5180cSMike Christie BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); 747f6d5180cSMike Christie BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); 748f6d5180cSMike Christie 7497acd72ebSStefani Seibold if (!kfifo_out(&session->cmdpool.queue, 7509c19a7d0SMike Christie (void*)&task, sizeof(void*))) 751f6d5180cSMike Christie return NULL; 752f6d5180cSMike Christie } 7533e5c28adSMike Christie /* 7543e5c28adSMike Christie * released in complete pdu for task we expect a response for, and 7553e5c28adSMike Christie * released by the lld when it has transmitted the task for 7563e5c28adSMike Christie * pdus we do not expect a response for. 7573e5c28adSMike Christie */ 7586dc618cdSElena Reshetova refcount_set(&task->refcount, 1); 7599c19a7d0SMike Christie task->conn = conn; 7609c19a7d0SMike Christie task->sc = NULL; 7613bbaaad9SMike Christie INIT_LIST_HEAD(&task->running); 7623bbaaad9SMike Christie task->state = ISCSI_TASK_PENDING; 763f6d5180cSMike Christie 764f6d5180cSMike Christie if (data_size) { 7659c19a7d0SMike Christie memcpy(task->data, data, data_size); 7669c19a7d0SMike Christie task->data_count = data_size; 767f6d5180cSMike Christie } else 7689c19a7d0SMike Christie task->data_count = 0; 769f6d5180cSMike Christie 770184b57c6SMike Christie if (conn->session->tt->alloc_pdu) { 7712ff79d52SMike Christie if (conn->session->tt->alloc_pdu(task, hdr->opcode)) { 772577577daSMike Christie iscsi_conn_printk(KERN_ERR, conn, "Could not allocate " 773577577daSMike Christie "pdu for mgmt task.\n"); 7743bbaaad9SMike Christie goto free_task; 775577577daSMike Christie } 776184b57c6SMike Christie } 777184b57c6SMike Christie 778262ef636SMike Christie itt = task->hdr->itt; 779577577daSMike Christie task->hdr_len = sizeof(struct iscsi_hdr); 7809c19a7d0SMike Christie memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr)); 781262ef636SMike Christie 782262ef636SMike Christie if (hdr->itt != RESERVED_ITT) { 783262ef636SMike Christie if (session->tt->parse_pdu_itt) 784262ef636SMike Christie task->hdr->itt = itt; 785262ef636SMike Christie else 786262ef636SMike Christie task->hdr->itt = build_itt(task->itt, 787262ef636SMike Christie task->conn->session->age); 788262ef636SMike Christie } 789262ef636SMike Christie 7906e637b72SMike Christie return task; 7916e637b72SMike Christie 7926e637b72SMike Christie free_task: 7936e637b72SMike Christie iscsi_put_task(task); 7946e637b72SMike Christie return NULL; 7956e637b72SMike Christie } 7966e637b72SMike Christie 7976e637b72SMike Christie /** 7986e637b72SMike Christie * iscsi_send_mgmt_task - Send task created with iscsi_alloc_mgmt_task. 7996e637b72SMike Christie * @task: iscsi task to send. 8006e637b72SMike Christie * 8016e637b72SMike Christie * On failure this returns a non-zero error code, and the driver must free 8026e637b72SMike Christie * the task with iscsi_put_task; 8036e637b72SMike Christie */ 8046e637b72SMike Christie static int iscsi_send_mgmt_task(struct iscsi_task *task) 8056e637b72SMike Christie { 8066e637b72SMike Christie struct iscsi_conn *conn = task->conn; 8076e637b72SMike Christie struct iscsi_session *session = conn->session; 8086e637b72SMike Christie struct iscsi_host *ihost = shost_priv(conn->session->host); 8096e637b72SMike Christie int rc = 0; 810fe0a8a95SLee Duncan 8111336aed1SMike Christie if (!ihost->workq) { 8126e637b72SMike Christie rc = iscsi_prep_mgmt_task(conn, task); 8136e637b72SMike Christie if (rc) 8146e637b72SMike Christie return rc; 815052d0144SMike Christie 8166e637b72SMike Christie rc = session->tt->xmit_task(task); 8176e637b72SMike Christie if (rc) 8186e637b72SMike Christie return rc; 8193bbaaad9SMike Christie } else { 8203bbaaad9SMike Christie list_add_tail(&task->running, &conn->mgmtqueue); 8214b9f8ce4SMike Christie iscsi_conn_queue_xmit(conn); 8223bbaaad9SMike Christie } 823052d0144SMike Christie 8246e637b72SMike Christie return 0; 8256e637b72SMike Christie } 826577577daSMike Christie 8276e637b72SMike Christie static int __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 8286e637b72SMike Christie char *data, uint32_t data_size) 8296e637b72SMike Christie { 8306e637b72SMike Christie struct iscsi_task *task; 8316e637b72SMike Christie int rc; 8326e637b72SMike Christie 8336e637b72SMike Christie task = iscsi_alloc_mgmt_task(conn, hdr, data, data_size); 8346e637b72SMike Christie if (!task) 8356e637b72SMike Christie return -ENOMEM; 8366e637b72SMike Christie 8376e637b72SMike Christie rc = iscsi_send_mgmt_task(task); 8386e637b72SMike Christie if (rc) 8396e637b72SMike Christie iscsi_put_task(task); 8406e637b72SMike Christie return rc; 841f6d5180cSMike Christie } 842f6d5180cSMike Christie 843f6d5180cSMike Christie int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr, 844f6d5180cSMike Christie char *data, uint32_t data_size) 845f6d5180cSMike Christie { 846f6d5180cSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 847f6d5180cSMike Christie struct iscsi_session *session = conn->session; 848f6d5180cSMike Christie int err = 0; 849f6d5180cSMike Christie 850659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 8516e637b72SMike Christie if (__iscsi_conn_send_pdu(conn, hdr, data, data_size)) 852f6d5180cSMike Christie err = -EPERM; 853659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 854f6d5180cSMike Christie return err; 855f6d5180cSMike Christie } 856f6d5180cSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu); 857f6d5180cSMike Christie 8587996a778SMike Christie /** 859ae6b4e69SMauro Carvalho Chehab * iscsi_scsi_cmd_rsp - SCSI Command Response processing 8607996a778SMike Christie * @conn: iscsi connection 8617996a778SMike Christie * @hdr: iscsi header 8629c19a7d0SMike Christie * @task: scsi command task 8637996a778SMike Christie * @data: cmd data buffer 8647996a778SMike Christie * @datalen: len of buffer 8657996a778SMike Christie * 8667996a778SMike Christie * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and 867a656183eSLee Duncan * then completes the command and task. called under back_lock 8687996a778SMike Christie **/ 86977a23c21SMike Christie static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 8709c19a7d0SMike Christie struct iscsi_task *task, char *data, 8717996a778SMike Christie int datalen) 8727996a778SMike Christie { 87312352183SNicholas Bellinger struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr; 8747996a778SMike Christie struct iscsi_session *session = conn->session; 8759c19a7d0SMike Christie struct scsi_cmnd *sc = task->sc; 8767996a778SMike Christie 87777a23c21SMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); 8787996a778SMike Christie conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; 8797996a778SMike Christie 8807996a778SMike Christie sc->result = (DID_OK << 16) | rhdr->cmd_status; 8817996a778SMike Christie 88255e51edaSSagi Grimberg if (task->protected) { 88355e51edaSSagi Grimberg sector_t sector; 88455e51edaSSagi Grimberg u8 ascq; 88555e51edaSSagi Grimberg 88655e51edaSSagi Grimberg /** 88755e51edaSSagi Grimberg * Transports that didn't implement check_protection 88855e51edaSSagi Grimberg * callback but still published T10-PI support to scsi-mid 88955e51edaSSagi Grimberg * deserve this BUG_ON. 89055e51edaSSagi Grimberg **/ 89155e51edaSSagi Grimberg BUG_ON(!session->tt->check_protection); 89255e51edaSSagi Grimberg 89355e51edaSSagi Grimberg ascq = session->tt->check_protection(task, §or); 89455e51edaSSagi Grimberg if (ascq) { 895f2b1e9c6SHannes Reinecke scsi_build_sense(sc, 1, ILLEGAL_REQUEST, 0x10, ascq); 896a73c2a2fSSagi Grimberg scsi_set_sense_information(sc->sense_buffer, 897a73c2a2fSSagi Grimberg SCSI_SENSE_BUFFERSIZE, 898a73c2a2fSSagi Grimberg sector); 89955e51edaSSagi Grimberg goto out; 90055e51edaSSagi Grimberg } 90155e51edaSSagi Grimberg } 90255e51edaSSagi Grimberg 9037996a778SMike Christie if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) { 9047996a778SMike Christie sc->result = DID_ERROR << 16; 9057996a778SMike Christie goto out; 9067996a778SMike Christie } 9077996a778SMike Christie 9087996a778SMike Christie if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) { 9099b80cb4bSMike Christie uint16_t senselen; 9107996a778SMike Christie 9117996a778SMike Christie if (datalen < 2) { 9127996a778SMike Christie invalid_datalen: 913322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 914322d739dSMike Christie "Got CHECK_CONDITION but invalid data " 915322d739dSMike Christie "buffer size of %d\n", datalen); 9167996a778SMike Christie sc->result = DID_BAD_TARGET << 16; 9177996a778SMike Christie goto out; 9187996a778SMike Christie } 9197996a778SMike Christie 9208f333991SHarvey Harrison senselen = get_unaligned_be16(data); 9217996a778SMike Christie if (datalen < senselen) 9227996a778SMike Christie goto invalid_datalen; 9237996a778SMike Christie 9247996a778SMike Christie memcpy(sc->sense_buffer, data + 2, 9259b80cb4bSMike Christie min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE)); 9261b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n", 9271b2c7af8SMike Christie min_t(uint16_t, senselen, 9281b2c7af8SMike Christie SCSI_SENSE_BUFFERSIZE)); 9297996a778SMike Christie } 9307996a778SMike Christie 931c07d4444SBoaz Harrosh if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW | 932c07d4444SBoaz Harrosh ISCSI_FLAG_CMD_BIDI_OVERFLOW)) { 933c07d4444SBoaz Harrosh sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 934c07d4444SBoaz Harrosh } 935c07d4444SBoaz Harrosh 9367207fea4SBoaz Harrosh if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW | 9377207fea4SBoaz Harrosh ISCSI_FLAG_CMD_OVERFLOW)) { 9387996a778SMike Christie int res_count = be32_to_cpu(rhdr->residual_count); 9397996a778SMike Christie 9407207fea4SBoaz Harrosh if (res_count > 0 && 9417207fea4SBoaz Harrosh (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || 9427207fea4SBoaz Harrosh res_count <= scsi_bufflen(sc))) 943c07d4444SBoaz Harrosh /* write side for bidi or uni-io set_resid */ 9441c138991SFUJITA Tomonori scsi_set_resid(sc, res_count); 9457996a778SMike Christie else 9467996a778SMike Christie sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 947c07d4444SBoaz Harrosh } 9487996a778SMike Christie out: 9493bbaaad9SMike Christie ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n", 9501b2c7af8SMike Christie sc, sc->result, task->itt); 9517996a778SMike Christie conn->scsirsp_pdus_cnt++; 952b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 9537996a778SMike Christie } 9547996a778SMike Christie 9551d9edf02SMike Christie /** 9561d9edf02SMike Christie * iscsi_data_in_rsp - SCSI Data-In Response processing 9571d9edf02SMike Christie * @conn: iscsi connection 9581d9edf02SMike Christie * @hdr: iscsi pdu 9591d9edf02SMike Christie * @task: scsi command task 960a656183eSLee Duncan * 961a656183eSLee Duncan * iscsi_data_in_rsp sets up the scsi_cmnd fields based on the data received 962a656183eSLee Duncan * then completes the command and task. called under back_lock 9631d9edf02SMike Christie **/ 9641d9edf02SMike Christie static void 9651d9edf02SMike Christie iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 9661d9edf02SMike Christie struct iscsi_task *task) 9671d9edf02SMike Christie { 9681d9edf02SMike Christie struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr; 9691d9edf02SMike Christie struct scsi_cmnd *sc = task->sc; 9701d9edf02SMike Christie 9711d9edf02SMike Christie if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS)) 9721d9edf02SMike Christie return; 9731d9edf02SMike Christie 974edbc9aa0SMike Christie iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr); 9751d9edf02SMike Christie sc->result = (DID_OK << 16) | rhdr->cmd_status; 9761d9edf02SMike Christie conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; 9771d9edf02SMike Christie if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW | 9781d9edf02SMike Christie ISCSI_FLAG_DATA_OVERFLOW)) { 9791d9edf02SMike Christie int res_count = be32_to_cpu(rhdr->residual_count); 9801d9edf02SMike Christie 9811d9edf02SMike Christie if (res_count > 0 && 9821d9edf02SMike Christie (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || 983ae3d56d8SChristoph Hellwig res_count <= sc->sdb.length)) 984960bf87aSBart Van Assche scsi_set_resid(sc, res_count); 9851d9edf02SMike Christie else 9861d9edf02SMike Christie sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 9871d9edf02SMike Christie } 9881d9edf02SMike Christie 9893bbaaad9SMike Christie ISCSI_DBG_SESSION(conn->session, "data in with status done " 9903bbaaad9SMike Christie "[sc %p res %d itt 0x%x]\n", 9913bbaaad9SMike Christie sc, sc->result, task->itt); 9921d9edf02SMike Christie conn->scsirsp_pdus_cnt++; 993b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 9941d9edf02SMike Christie } 9951d9edf02SMike Christie 9967ea8b828SMike Christie static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr) 9977ea8b828SMike Christie { 9987ea8b828SMike Christie struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr; 999ec29d0acSMike Christie struct iscsi_session *session = conn->session; 10007ea8b828SMike Christie 10017ea8b828SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 10027ea8b828SMike Christie conn->tmfrsp_pdus_cnt++; 10037ea8b828SMike Christie 1004ec29d0acSMike Christie if (session->tmf_state != TMF_QUEUED) 10057ea8b828SMike Christie return; 10067ea8b828SMike Christie 10077ea8b828SMike Christie if (tmf->response == ISCSI_TMF_RSP_COMPLETE) 1008ec29d0acSMike Christie session->tmf_state = TMF_SUCCESS; 10097ea8b828SMike Christie else if (tmf->response == ISCSI_TMF_RSP_NO_TASK) 1010ec29d0acSMike Christie session->tmf_state = TMF_NOT_FOUND; 10117ea8b828SMike Christie else 1012ec29d0acSMike Christie session->tmf_state = TMF_FAILED; 1013ec29d0acSMike Christie wake_up(&session->ehwait); 10147ea8b828SMike Christie } 10157ea8b828SMike Christie 101652f5664aSAriel Nahum static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr) 1017f6d5180cSMike Christie { 1018f6d5180cSMike Christie struct iscsi_nopout hdr; 10199c19a7d0SMike Christie struct iscsi_task *task; 1020f6d5180cSMike Christie 1021fe0a8a95SLee Duncan if (!rhdr) { 1022fe0a8a95SLee Duncan if (READ_ONCE(conn->ping_task)) 102352f5664aSAriel Nahum return -EINVAL; 1024fe0a8a95SLee Duncan } 1025f6d5180cSMike Christie 1026f6d5180cSMike Christie memset(&hdr, 0, sizeof(struct iscsi_nopout)); 1027f6d5180cSMike Christie hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE; 1028f6d5180cSMike Christie hdr.flags = ISCSI_FLAG_CMD_FINAL; 1029f6d5180cSMike Christie 1030f6d5180cSMike Christie if (rhdr) { 103155bdabdfSAndy Grover hdr.lun = rhdr->lun; 1032f6d5180cSMike Christie hdr.ttt = rhdr->ttt; 1033f6d5180cSMike Christie hdr.itt = RESERVED_ITT; 1034f6d5180cSMike Christie } else 1035f6d5180cSMike Christie hdr.ttt = RESERVED_ITT; 1036f6d5180cSMike Christie 10376e637b72SMike Christie task = iscsi_alloc_mgmt_task(conn, (struct iscsi_hdr *)&hdr, NULL, 0); 10386e637b72SMike Christie if (!task) 10396e637b72SMike Christie return -ENOMEM; 10406e637b72SMike Christie 10416e637b72SMike Christie if (!rhdr) 10426e637b72SMike Christie WRITE_ONCE(conn->ping_task, task); 10436e637b72SMike Christie 10446e637b72SMike Christie if (iscsi_send_mgmt_task(task)) { 1045fe0a8a95SLee Duncan if (!rhdr) 1046fe0a8a95SLee Duncan WRITE_ONCE(conn->ping_task, NULL); 10476e637b72SMike Christie iscsi_put_task(task); 10486e637b72SMike Christie 1049322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n"); 105052f5664aSAriel Nahum return -EIO; 105152f5664aSAriel Nahum } else if (!rhdr) { 1052d3acf022SMike Christie /* only track our nops */ 1053d3acf022SMike Christie conn->last_ping = jiffies; 1054d3acf022SMike Christie } 105552f5664aSAriel Nahum 105652f5664aSAriel Nahum return 0; 1057f6d5180cSMike Christie } 1058f6d5180cSMike Christie 1059a656183eSLee Duncan /** 1060a656183eSLee Duncan * iscsi_nop_out_rsp - SCSI NOP Response processing 1061a656183eSLee Duncan * @task: scsi command task 1062a656183eSLee Duncan * @nop: the nop structure 1063a656183eSLee Duncan * @data: where to put the data 1064a656183eSLee Duncan * @datalen: length of data 1065a656183eSLee Duncan * 1066a656183eSLee Duncan * iscsi_nop_out_rsp handles nop response from use or 1067a656183eSLee Duncan * from user space. called under back_lock 1068a656183eSLee Duncan **/ 10698afa1439SMike Christie static int iscsi_nop_out_rsp(struct iscsi_task *task, 10708afa1439SMike Christie struct iscsi_nopin *nop, char *data, int datalen) 10718afa1439SMike Christie { 10728afa1439SMike Christie struct iscsi_conn *conn = task->conn; 10738afa1439SMike Christie int rc = 0; 10748afa1439SMike Christie 1075fe0a8a95SLee Duncan if (READ_ONCE(conn->ping_task) != task) { 10768afa1439SMike Christie /* 10778afa1439SMike Christie * If this is not in response to one of our 10788afa1439SMike Christie * nops then it must be from userspace. 10798afa1439SMike Christie */ 10808afa1439SMike Christie if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop, 10818afa1439SMike Christie data, datalen)) 10828afa1439SMike Christie rc = ISCSI_ERR_CONN_FAILED; 10838afa1439SMike Christie } else 10848afa1439SMike Christie mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout); 10858afa1439SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 10868afa1439SMike Christie return rc; 10878afa1439SMike Christie } 10888afa1439SMike Christie 108962f38300SMike Christie static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 109062f38300SMike Christie char *data, int datalen) 109162f38300SMike Christie { 109262f38300SMike Christie struct iscsi_reject *reject = (struct iscsi_reject *)hdr; 109362f38300SMike Christie struct iscsi_hdr rejected_pdu; 10948afa1439SMike Christie int opcode, rc = 0; 109562f38300SMike Christie 109662f38300SMike Christie conn->exp_statsn = be32_to_cpu(reject->statsn) + 1; 109762f38300SMike Christie 10988afa1439SMike Christie if (ntoh24(reject->dlength) > datalen || 10998afa1439SMike Christie ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) { 11008afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected " 11018afa1439SMike Christie "pdu. Invalid data length (pdu dlength " 11028afa1439SMike Christie "%u, datalen %d\n", ntoh24(reject->dlength), 11038afa1439SMike Christie datalen); 110462f38300SMike Christie return ISCSI_ERR_PROTO; 11058afa1439SMike Christie } 110662f38300SMike Christie memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr)); 11078afa1439SMike Christie opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK; 11088afa1439SMike Christie 11098afa1439SMike Christie switch (reject->reason) { 11108afa1439SMike Christie case ISCSI_REASON_DATA_DIGEST_ERROR: 1111322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 11128afa1439SMike Christie "pdu (op 0x%x itt 0x%x) rejected " 1113262ef636SMike Christie "due to DataDigest error.\n", 1114e5dbbe27SVaughan Cao opcode, rejected_pdu.itt); 11158afa1439SMike Christie break; 11168afa1439SMike Christie case ISCSI_REASON_IMM_CMD_REJECT: 11178afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, 11188afa1439SMike Christie "pdu (op 0x%x itt 0x%x) rejected. Too many " 11198afa1439SMike Christie "immediate commands.\n", 1120e5dbbe27SVaughan Cao opcode, rejected_pdu.itt); 11218afa1439SMike Christie /* 11228afa1439SMike Christie * We only send one TMF at a time so if the target could not 11238afa1439SMike Christie * handle it, then it should get fixed (RFC mandates that 11248afa1439SMike Christie * a target can handle one immediate TMF per conn). 11258afa1439SMike Christie * 11268afa1439SMike Christie * For nops-outs, we could have sent more than one if 11278afa1439SMike Christie * the target is sending us lots of nop-ins 11288afa1439SMike Christie */ 11298afa1439SMike Christie if (opcode != ISCSI_OP_NOOP_OUT) 113062f38300SMike Christie return 0; 11318afa1439SMike Christie 1132659743b0SShlomo Pongratz if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) { 11338afa1439SMike Christie /* 11348afa1439SMike Christie * nop-out in response to target's nop-out rejected. 11358afa1439SMike Christie * Just resend. 11368afa1439SMike Christie */ 1137659743b0SShlomo Pongratz /* In RX path we are under back lock */ 1138659743b0SShlomo Pongratz spin_unlock(&conn->session->back_lock); 1139659743b0SShlomo Pongratz spin_lock(&conn->session->frwd_lock); 11408afa1439SMike Christie iscsi_send_nopout(conn, 11418afa1439SMike Christie (struct iscsi_nopin*)&rejected_pdu); 1142659743b0SShlomo Pongratz spin_unlock(&conn->session->frwd_lock); 1143659743b0SShlomo Pongratz spin_lock(&conn->session->back_lock); 1144659743b0SShlomo Pongratz } else { 11458afa1439SMike Christie struct iscsi_task *task; 11468afa1439SMike Christie /* 11478afa1439SMike Christie * Our nop as ping got dropped. We know the target 11488afa1439SMike Christie * and transport are ok so just clean up 11498afa1439SMike Christie */ 11508afa1439SMike Christie task = iscsi_itt_to_task(conn, rejected_pdu.itt); 11518afa1439SMike Christie if (!task) { 11528afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, 11538afa1439SMike Christie "Invalid pdu reject. Could " 11548afa1439SMike Christie "not lookup rejected task.\n"); 11558afa1439SMike Christie rc = ISCSI_ERR_BAD_ITT; 11568afa1439SMike Christie } else 11578afa1439SMike Christie rc = iscsi_nop_out_rsp(task, 11588afa1439SMike Christie (struct iscsi_nopin*)&rejected_pdu, 11598afa1439SMike Christie NULL, 0); 11608afa1439SMike Christie } 11618afa1439SMike Christie break; 11628afa1439SMike Christie default: 11638afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, 11648afa1439SMike Christie "pdu (op 0x%x itt 0x%x) rejected. Reason " 1165e5dbbe27SVaughan Cao "code 0x%x\n", rejected_pdu.opcode, 1166e5dbbe27SVaughan Cao rejected_pdu.itt, reject->reason); 11678afa1439SMike Christie break; 11688afa1439SMike Christie } 11698afa1439SMike Christie return rc; 117062f38300SMike Christie } 117162f38300SMike Christie 11727996a778SMike Christie /** 1173913e5bf4SMike Christie * iscsi_itt_to_task - look up task by itt 1174913e5bf4SMike Christie * @conn: iscsi connection 1175913e5bf4SMike Christie * @itt: itt 1176913e5bf4SMike Christie * 1177913e5bf4SMike Christie * This should be used for mgmt tasks like login and nops, or if 1178913e5bf4SMike Christie * the LDD's itt space does not include the session age. 1179913e5bf4SMike Christie * 1180659743b0SShlomo Pongratz * The session back_lock must be held. 1181913e5bf4SMike Christie */ 11828f9256ceSMike Christie struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt) 1183913e5bf4SMike Christie { 1184913e5bf4SMike Christie struct iscsi_session *session = conn->session; 1185262ef636SMike Christie int i; 1186913e5bf4SMike Christie 1187913e5bf4SMike Christie if (itt == RESERVED_ITT) 1188913e5bf4SMike Christie return NULL; 1189913e5bf4SMike Christie 1190262ef636SMike Christie if (session->tt->parse_pdu_itt) 1191262ef636SMike Christie session->tt->parse_pdu_itt(conn, itt, &i, NULL); 1192262ef636SMike Christie else 1193913e5bf4SMike Christie i = get_itt(itt); 1194913e5bf4SMike Christie if (i >= session->cmds_max) 1195913e5bf4SMike Christie return NULL; 1196913e5bf4SMike Christie 1197913e5bf4SMike Christie return session->cmds[i]; 1198913e5bf4SMike Christie } 11998f9256ceSMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_task); 1200913e5bf4SMike Christie 1201913e5bf4SMike Christie /** 12027996a778SMike Christie * __iscsi_complete_pdu - complete pdu 12037996a778SMike Christie * @conn: iscsi conn 12047996a778SMike Christie * @hdr: iscsi header 12057996a778SMike Christie * @data: data buffer 12067996a778SMike Christie * @datalen: len of data buffer 12077996a778SMike Christie * 12087996a778SMike Christie * Completes pdu processing by freeing any resources allocated at 1209659743b0SShlomo Pongratz * queuecommand or send generic. session back_lock must be held and verify 12107996a778SMike Christie * itt must have been called. 12117996a778SMike Christie */ 1212913e5bf4SMike Christie int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 12137996a778SMike Christie char *data, int datalen) 12147996a778SMike Christie { 12157996a778SMike Christie struct iscsi_session *session = conn->session; 12167996a778SMike Christie int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0; 12179c19a7d0SMike Christie struct iscsi_task *task; 12187996a778SMike Christie uint32_t itt; 12197996a778SMike Christie 1220f6d5180cSMike Christie conn->last_recv = jiffies; 12210af967f5SMike Christie rc = iscsi_verify_itt(conn, hdr->itt); 12220af967f5SMike Christie if (rc) 12230af967f5SMike Christie return rc; 12240af967f5SMike Christie 1225b4377356SAl Viro if (hdr->itt != RESERVED_ITT) 1226b4377356SAl Viro itt = get_itt(hdr->itt); 12277996a778SMike Christie else 1228b4377356SAl Viro itt = ~0U; 12297996a778SMike Christie 12301b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n", 12313e5c28adSMike Christie opcode, conn->id, itt, datalen); 12327996a778SMike Christie 12333e5c28adSMike Christie if (itt == ~0U) { 123477a23c21SMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 123562f38300SMike Christie 12367996a778SMike Christie switch(opcode) { 12377996a778SMike Christie case ISCSI_OP_NOOP_IN: 123840527afeSMike Christie if (datalen) { 123940527afeSMike Christie rc = ISCSI_ERR_PROTO; 124040527afeSMike Christie break; 124140527afeSMike Christie } 124240527afeSMike Christie 1243b4377356SAl Viro if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG)) 124440527afeSMike Christie break; 124540527afeSMike Christie 1246659743b0SShlomo Pongratz /* In RX path we are under back lock */ 1247659743b0SShlomo Pongratz spin_unlock(&session->back_lock); 1248659743b0SShlomo Pongratz spin_lock(&session->frwd_lock); 1249f6d5180cSMike Christie iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr); 1250659743b0SShlomo Pongratz spin_unlock(&session->frwd_lock); 1251659743b0SShlomo Pongratz spin_lock(&session->back_lock); 12527996a778SMike Christie break; 12537996a778SMike Christie case ISCSI_OP_REJECT: 125462f38300SMike Christie rc = iscsi_handle_reject(conn, hdr, data, datalen); 125562f38300SMike Christie break; 12567996a778SMike Christie case ISCSI_OP_ASYNC_EVENT: 12578d2860b3SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 12585831c737SMike Christie if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) 12595831c737SMike Christie rc = ISCSI_ERR_CONN_FAILED; 12607996a778SMike Christie break; 12617996a778SMike Christie default: 12627996a778SMike Christie rc = ISCSI_ERR_BAD_OPCODE; 12637996a778SMike Christie break; 12647996a778SMike Christie } 12653e5c28adSMike Christie goto out; 12663e5c28adSMike Christie } 12677996a778SMike Christie 12683e5c28adSMike Christie switch(opcode) { 12693e5c28adSMike Christie case ISCSI_OP_SCSI_CMD_RSP: 1270913e5bf4SMike Christie case ISCSI_OP_SCSI_DATA_IN: 1271913e5bf4SMike Christie task = iscsi_itt_to_ctask(conn, hdr->itt); 1272913e5bf4SMike Christie if (!task) 1273913e5bf4SMike Christie return ISCSI_ERR_BAD_ITT; 1274d355e57dSMike Christie task->last_xfer = jiffies; 12753e5c28adSMike Christie break; 1276913e5bf4SMike Christie case ISCSI_OP_R2T: 1277913e5bf4SMike Christie /* 1278913e5bf4SMike Christie * LLD handles R2Ts if they need to. 1279913e5bf4SMike Christie */ 1280913e5bf4SMike Christie return 0; 1281913e5bf4SMike Christie case ISCSI_OP_LOGOUT_RSP: 1282913e5bf4SMike Christie case ISCSI_OP_LOGIN_RSP: 1283913e5bf4SMike Christie case ISCSI_OP_TEXT_RSP: 1284913e5bf4SMike Christie case ISCSI_OP_SCSI_TMFUNC_RSP: 1285913e5bf4SMike Christie case ISCSI_OP_NOOP_IN: 1286913e5bf4SMike Christie task = iscsi_itt_to_task(conn, hdr->itt); 1287913e5bf4SMike Christie if (!task) 1288913e5bf4SMike Christie return ISCSI_ERR_BAD_ITT; 1289913e5bf4SMike Christie break; 1290913e5bf4SMike Christie default: 1291913e5bf4SMike Christie return ISCSI_ERR_BAD_OPCODE; 12923e5c28adSMike Christie } 1293913e5bf4SMike Christie 1294913e5bf4SMike Christie switch(opcode) { 1295913e5bf4SMike Christie case ISCSI_OP_SCSI_CMD_RSP: 12969c19a7d0SMike Christie iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen); 12973e5c28adSMike Christie break; 12983e5c28adSMike Christie case ISCSI_OP_SCSI_DATA_IN: 12991d9edf02SMike Christie iscsi_data_in_rsp(conn, hdr, task); 13003e5c28adSMike Christie break; 13013e5c28adSMike Christie case ISCSI_OP_LOGOUT_RSP: 13023e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 13033e5c28adSMike Christie if (datalen) { 13043e5c28adSMike Christie rc = ISCSI_ERR_PROTO; 13053e5c28adSMike Christie break; 13063e5c28adSMike Christie } 13073e5c28adSMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 13083e5c28adSMike Christie goto recv_pdu; 13093e5c28adSMike Christie case ISCSI_OP_LOGIN_RSP: 13103e5c28adSMike Christie case ISCSI_OP_TEXT_RSP: 13113e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 13123e5c28adSMike Christie /* 13133e5c28adSMike Christie * login related PDU's exp_statsn is handled in 13143e5c28adSMike Christie * userspace 13153e5c28adSMike Christie */ 13163e5c28adSMike Christie goto recv_pdu; 13173e5c28adSMike Christie case ISCSI_OP_SCSI_TMFUNC_RSP: 13183e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 13193e5c28adSMike Christie if (datalen) { 13203e5c28adSMike Christie rc = ISCSI_ERR_PROTO; 13213e5c28adSMike Christie break; 13223e5c28adSMike Christie } 13233e5c28adSMike Christie 13243e5c28adSMike Christie iscsi_tmf_rsp(conn, hdr); 1325b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 13263e5c28adSMike Christie break; 13273e5c28adSMike Christie case ISCSI_OP_NOOP_IN: 13283e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 13293e5c28adSMike Christie if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) { 13303e5c28adSMike Christie rc = ISCSI_ERR_PROTO; 13313e5c28adSMike Christie break; 13323e5c28adSMike Christie } 13333e5c28adSMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 13343e5c28adSMike Christie 13358afa1439SMike Christie rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr, 13368afa1439SMike Christie data, datalen); 13373e5c28adSMike Christie break; 13383e5c28adSMike Christie default: 13393e5c28adSMike Christie rc = ISCSI_ERR_BAD_OPCODE; 13403e5c28adSMike Christie break; 13413e5c28adSMike Christie } 13423e5c28adSMike Christie 13433e5c28adSMike Christie out: 13443e5c28adSMike Christie return rc; 13453e5c28adSMike Christie recv_pdu: 13463e5c28adSMike Christie if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) 13473e5c28adSMike Christie rc = ISCSI_ERR_CONN_FAILED; 1348b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 13497996a778SMike Christie return rc; 13507996a778SMike Christie } 1351913e5bf4SMike Christie EXPORT_SYMBOL_GPL(__iscsi_complete_pdu); 13527996a778SMike Christie 13537996a778SMike Christie int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 13547996a778SMike Christie char *data, int datalen) 13557996a778SMike Christie { 13567996a778SMike Christie int rc; 13577996a778SMike Christie 1358659743b0SShlomo Pongratz spin_lock(&conn->session->back_lock); 13597996a778SMike Christie rc = __iscsi_complete_pdu(conn, hdr, data, datalen); 1360659743b0SShlomo Pongratz spin_unlock(&conn->session->back_lock); 13617996a778SMike Christie return rc; 13627996a778SMike Christie } 13637996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_pdu); 13647996a778SMike Christie 13650af967f5SMike Christie int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt) 13667996a778SMike Christie { 13677996a778SMike Christie struct iscsi_session *session = conn->session; 1368262ef636SMike Christie int age = 0, i = 0; 13697996a778SMike Christie 13700af967f5SMike Christie if (itt == RESERVED_ITT) 13710af967f5SMike Christie return 0; 13720af967f5SMike Christie 1373262ef636SMike Christie if (session->tt->parse_pdu_itt) 1374262ef636SMike Christie session->tt->parse_pdu_itt(conn, itt, &i, &age); 1375262ef636SMike Christie else { 1376262ef636SMike Christie i = get_itt(itt); 1377262ef636SMike Christie age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK; 1378262ef636SMike Christie } 1379262ef636SMike Christie 1380262ef636SMike Christie if (age != session->age) { 1381322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 13820af967f5SMike Christie "received itt %x expected session age (%x)\n", 1383913e5bf4SMike Christie (__force u32)itt, session->age); 13847996a778SMike Christie return ISCSI_ERR_BAD_ITT; 13857996a778SMike Christie } 13867996a778SMike Christie 13873e5c28adSMike Christie if (i >= session->cmds_max) { 13883e5c28adSMike Christie iscsi_conn_printk(KERN_ERR, conn, 13893e5c28adSMike Christie "received invalid itt index %u (max cmds " 13903e5c28adSMike Christie "%u.\n", i, session->cmds_max); 13913e5c28adSMike Christie return ISCSI_ERR_BAD_ITT; 13927996a778SMike Christie } 13937996a778SMike Christie return 0; 13947996a778SMike Christie } 13957996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_verify_itt); 13967996a778SMike Christie 1397913e5bf4SMike Christie /** 1398913e5bf4SMike Christie * iscsi_itt_to_ctask - look up ctask by itt 1399913e5bf4SMike Christie * @conn: iscsi connection 1400913e5bf4SMike Christie * @itt: itt 1401913e5bf4SMike Christie * 1402913e5bf4SMike Christie * This should be used for cmd tasks. 1403913e5bf4SMike Christie * 1404659743b0SShlomo Pongratz * The session back_lock must be held. 1405913e5bf4SMike Christie */ 1406913e5bf4SMike Christie struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt) 14070af967f5SMike Christie { 14089c19a7d0SMike Christie struct iscsi_task *task; 14090af967f5SMike Christie 14100af967f5SMike Christie if (iscsi_verify_itt(conn, itt)) 14110af967f5SMike Christie return NULL; 14120af967f5SMike Christie 1413913e5bf4SMike Christie task = iscsi_itt_to_task(conn, itt); 1414913e5bf4SMike Christie if (!task || !task->sc) 14150af967f5SMike Christie return NULL; 14160af967f5SMike Christie 1417db22de3eSBart Van Assche if (iscsi_cmd(task->sc)->age != conn->session->age) { 1418913e5bf4SMike Christie iscsi_session_printk(KERN_ERR, conn->session, 1419913e5bf4SMike Christie "task's session age %d, expected %d\n", 1420db22de3eSBart Van Assche iscsi_cmd(task->sc)->age, conn->session->age); 14210af967f5SMike Christie return NULL; 1422913e5bf4SMike Christie } 14230af967f5SMike Christie 14249c19a7d0SMike Christie return task; 14250af967f5SMike Christie } 14260af967f5SMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask); 14270af967f5SMike Christie 142840a06e75SMike Christie void iscsi_session_failure(struct iscsi_session *session, 1429e5bd7b54SMike Christie enum iscsi_err err) 1430e5bd7b54SMike Christie { 1431e5bd7b54SMike Christie struct iscsi_conn *conn; 1432e5bd7b54SMike Christie 1433659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 1434e5bd7b54SMike Christie conn = session->leadconn; 1435e5bd7b54SMike Christie if (session->state == ISCSI_STATE_TERMINATE || !conn) { 1436659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 1437e5bd7b54SMike Christie return; 1438e5bd7b54SMike Christie } 1439e5bd7b54SMike Christie 1440b1d19e8cSMike Christie iscsi_get_conn(conn->cls_conn); 1441659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 1442e5bd7b54SMike Christie /* 1443e5bd7b54SMike Christie * if the host is being removed bypass the connection 1444e5bd7b54SMike Christie * recovery initialization because we are going to kill 1445e5bd7b54SMike Christie * the session. 1446e5bd7b54SMike Christie */ 1447e5bd7b54SMike Christie if (err == ISCSI_ERR_INVALID_HOST) 1448e5bd7b54SMike Christie iscsi_conn_error_event(conn->cls_conn, err); 1449e5bd7b54SMike Christie else 1450e5bd7b54SMike Christie iscsi_conn_failure(conn, err); 1451b1d19e8cSMike Christie iscsi_put_conn(conn->cls_conn); 1452e5bd7b54SMike Christie } 1453e5bd7b54SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_failure); 1454e5bd7b54SMike Christie 1455891e2639SMike Christie static bool iscsi_set_conn_failed(struct iscsi_conn *conn) 14567996a778SMike Christie { 14577996a778SMike Christie struct iscsi_session *session = conn->session; 14587996a778SMike Christie 1459891e2639SMike Christie if (session->state == ISCSI_STATE_FAILED) 1460891e2639SMike Christie return false; 1461656cffc9SMike Christie 146267a61114SMike Christie if (conn->stop_stage == 0) 14637996a778SMike Christie session->state = ISCSI_STATE_FAILED; 1464e5bd7b54SMike Christie 14655bd85625SMike Christie set_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags); 14665bd85625SMike Christie set_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags); 1467891e2639SMike Christie return true; 1468891e2639SMike Christie } 1469891e2639SMike Christie 1470891e2639SMike Christie void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err) 1471891e2639SMike Christie { 1472891e2639SMike Christie struct iscsi_session *session = conn->session; 1473891e2639SMike Christie bool needs_evt; 1474891e2639SMike Christie 1475891e2639SMike Christie spin_lock_bh(&session->frwd_lock); 1476891e2639SMike Christie needs_evt = iscsi_set_conn_failed(conn); 1477891e2639SMike Christie spin_unlock_bh(&session->frwd_lock); 1478891e2639SMike Christie 1479891e2639SMike Christie if (needs_evt) 1480e5bd7b54SMike Christie iscsi_conn_error_event(conn->cls_conn, err); 14817996a778SMike Christie } 14827996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_failure); 14837996a778SMike Christie 148477a23c21SMike Christie static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn) 148577a23c21SMike Christie { 148677a23c21SMike Christie struct iscsi_session *session = conn->session; 148777a23c21SMike Christie 148877a23c21SMike Christie /* 148977a23c21SMike Christie * Check for iSCSI window and take care of CmdSN wrap-around 149077a23c21SMike Christie */ 1491e0726407SMike Christie if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) { 14921b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn " 14931b2c7af8SMike Christie "%u MaxCmdSN %u CmdSN %u/%u\n", 14941b2c7af8SMike Christie session->exp_cmdsn, session->max_cmdsn, 14951b2c7af8SMike Christie session->cmdsn, session->queued_cmdsn); 149677a23c21SMike Christie return -ENOSPC; 149777a23c21SMike Christie } 149877a23c21SMike Christie return 0; 149977a23c21SMike Christie } 150077a23c21SMike Christie 15015923d64bSMike Christie static int iscsi_xmit_task(struct iscsi_conn *conn, struct iscsi_task *task, 15025923d64bSMike Christie bool was_requeue) 150377a23c21SMike Christie { 1504843c0a8aSMike Christie int rc; 150577a23c21SMike Christie 15065923d64bSMike Christie if (!conn->task) { 1507e1c6a7ecSMike Christie /* 1508e1c6a7ecSMike Christie * Take a ref so we can access it after xmit_task(). 1509e1c6a7ecSMike Christie * 1510e1c6a7ecSMike Christie * This should never fail because the failure paths will have 1511e1c6a7ecSMike Christie * stopped the xmit thread. 1512e1c6a7ecSMike Christie */ 1513e1c6a7ecSMike Christie if (!iscsi_get_task(task)) { 1514e1c6a7ecSMike Christie WARN_ON_ONCE(1); 1515e1c6a7ecSMike Christie return 0; 1516e1c6a7ecSMike Christie } 15175923d64bSMike Christie } else { 15185923d64bSMike Christie /* Already have a ref from when we failed to send it last call */ 15195923d64bSMike Christie conn->task = NULL; 15205923d64bSMike Christie } 15215923d64bSMike Christie 15225923d64bSMike Christie /* 15235923d64bSMike Christie * If this was a requeue for a R2T we have an extra ref on the task in 15245923d64bSMike Christie * case a bad target sends a cmd rsp before we have handled the task. 15255923d64bSMike Christie */ 15265923d64bSMike Christie if (was_requeue) 15276d626150SMike Christie iscsi_put_task(task); 15285923d64bSMike Christie 15295923d64bSMike Christie /* 15305923d64bSMike Christie * Do this after dropping the extra ref because if this was a requeue 15315923d64bSMike Christie * it's removed from that list and cleanup_queued_task would miss it. 15325923d64bSMike Christie */ 15335bd85625SMike Christie if (test_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags)) { 15345923d64bSMike Christie /* 15355923d64bSMike Christie * Save the task and ref in case we weren't cleaning up this 15365923d64bSMike Christie * task and get woken up again. 15375923d64bSMike Christie */ 15385923d64bSMike Christie conn->task = task; 153979edd00dSAnoob Soman return -ENODATA; 154079edd00dSAnoob Soman } 15415923d64bSMike Christie 1542659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 15439c19a7d0SMike Christie rc = conn->session->tt->xmit_task(task); 1544659743b0SShlomo Pongratz spin_lock_bh(&conn->session->frwd_lock); 1545d355e57dSMike Christie if (!rc) { 15469c19a7d0SMike Christie /* done with this task */ 1547d355e57dSMike Christie task->last_xfer = jiffies; 15486d626150SMike Christie } else { 15495923d64bSMike Christie /* 15505923d64bSMike Christie * get an extra ref that is released next time we access it 15515923d64bSMike Christie * as conn->task above. 15525923d64bSMike Christie */ 1553e1c6a7ecSMike Christie iscsi_get_task(task); 15545923d64bSMike Christie conn->task = task; 15555923d64bSMike Christie } 15565923d64bSMike Christie 15576d626150SMike Christie iscsi_put_task(task); 155877a23c21SMike Christie return rc; 155977a23c21SMike Christie } 156077a23c21SMike Christie 15617996a778SMike Christie /** 15629c19a7d0SMike Christie * iscsi_requeue_task - requeue task to run from session workqueue 15639c19a7d0SMike Christie * @task: task to requeue 1564843c0a8aSMike Christie * 15655923d64bSMike Christie * Callers must have taken a ref to the task that is going to be requeued. 1566843c0a8aSMike Christie */ 15679c19a7d0SMike Christie void iscsi_requeue_task(struct iscsi_task *task) 1568843c0a8aSMike Christie { 15699c19a7d0SMike Christie struct iscsi_conn *conn = task->conn; 1570843c0a8aSMike Christie 15713bbaaad9SMike Christie /* 15723bbaaad9SMike Christie * this may be on the requeue list already if the xmit_task callout 15733bbaaad9SMike Christie * is handling the r2ts while we are adding new ones 15743bbaaad9SMike Christie */ 15755923d64bSMike Christie spin_lock_bh(&conn->session->frwd_lock); 15765923d64bSMike Christie if (list_empty(&task->running)) { 15773bbaaad9SMike Christie list_add_tail(&task->running, &conn->requeue); 15785923d64bSMike Christie } else { 15795923d64bSMike Christie /* 15805923d64bSMike Christie * Don't need the extra ref since it's already requeued and 15815923d64bSMike Christie * has a ref. 15825923d64bSMike Christie */ 15835923d64bSMike Christie iscsi_put_task(task); 15845923d64bSMike Christie } 15854b9f8ce4SMike Christie iscsi_conn_queue_xmit(conn); 15865923d64bSMike Christie spin_unlock_bh(&conn->session->frwd_lock); 1587843c0a8aSMike Christie } 15889c19a7d0SMike Christie EXPORT_SYMBOL_GPL(iscsi_requeue_task); 1589843c0a8aSMike Christie 1590843c0a8aSMike Christie /** 15917996a778SMike Christie * iscsi_data_xmit - xmit any command into the scheduled connection 15927996a778SMike Christie * @conn: iscsi connection 15937996a778SMike Christie * 15947996a778SMike Christie * Notes: 15957996a778SMike Christie * The function can return -EAGAIN in which case the caller must 15967996a778SMike Christie * re-schedule it again later or recover. '0' return code means 15977996a778SMike Christie * successful xmit. 15987996a778SMike Christie **/ 15997996a778SMike Christie static int iscsi_data_xmit(struct iscsi_conn *conn) 16007996a778SMike Christie { 16015d12c05eSMike Christie struct iscsi_task *task; 16023219e529SMike Christie int rc = 0; 16037996a778SMike Christie 1604659743b0SShlomo Pongratz spin_lock_bh(&conn->session->frwd_lock); 16055bd85625SMike Christie if (test_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags)) { 16061b2c7af8SMike Christie ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n"); 1607659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 16083219e529SMike Christie return -ENODATA; 16097996a778SMike Christie } 16107996a778SMike Christie 16119c19a7d0SMike Christie if (conn->task) { 16125923d64bSMike Christie rc = iscsi_xmit_task(conn, conn->task, false); 16133219e529SMike Christie if (rc) 161470b31c15SMike Christie goto done; 16157996a778SMike Christie } 161677a23c21SMike Christie 161777a23c21SMike Christie /* 161877a23c21SMike Christie * process mgmt pdus like nops before commands since we should 161977a23c21SMike Christie * only have one nop-out as a ping from us and targets should not 162077a23c21SMike Christie * overflow us with nop-ins 162177a23c21SMike Christie */ 162277a23c21SMike Christie check_mgmt: 1623843c0a8aSMike Christie while (!list_empty(&conn->mgmtqueue)) { 16245923d64bSMike Christie task = list_entry(conn->mgmtqueue.next, struct iscsi_task, 16255923d64bSMike Christie running); 16265923d64bSMike Christie list_del_init(&task->running); 16275923d64bSMike Christie if (iscsi_prep_mgmt_task(conn, task)) { 1628659743b0SShlomo Pongratz /* regular RX path uses back_lock */ 1629659743b0SShlomo Pongratz spin_lock_bh(&conn->session->back_lock); 16305923d64bSMike Christie __iscsi_put_task(task); 1631659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->back_lock); 1632b3a7ea8dSMike Christie continue; 1633b3a7ea8dSMike Christie } 16345923d64bSMike Christie rc = iscsi_xmit_task(conn, task, false); 16353219e529SMike Christie if (rc) 163670b31c15SMike Christie goto done; 16377996a778SMike Christie } 16387996a778SMike Christie 163965080c51SDmitry Bogdanov check_requeue: 164065080c51SDmitry Bogdanov while (!list_empty(&conn->requeue)) { 164165080c51SDmitry Bogdanov /* 164265080c51SDmitry Bogdanov * we always do fastlogout - conn stop code will clean up. 164365080c51SDmitry Bogdanov */ 164465080c51SDmitry Bogdanov if (conn->session->state == ISCSI_STATE_LOGGING_OUT) 164565080c51SDmitry Bogdanov break; 164665080c51SDmitry Bogdanov 164765080c51SDmitry Bogdanov task = list_entry(conn->requeue.next, struct iscsi_task, 164865080c51SDmitry Bogdanov running); 164965080c51SDmitry Bogdanov 165065080c51SDmitry Bogdanov if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT)) 165165080c51SDmitry Bogdanov break; 165265080c51SDmitry Bogdanov 165365080c51SDmitry Bogdanov list_del_init(&task->running); 165465080c51SDmitry Bogdanov rc = iscsi_xmit_task(conn, task, true); 165565080c51SDmitry Bogdanov if (rc) 165665080c51SDmitry Bogdanov goto done; 165765080c51SDmitry Bogdanov if (!list_empty(&conn->mgmtqueue)) 165865080c51SDmitry Bogdanov goto check_mgmt; 165965080c51SDmitry Bogdanov } 166065080c51SDmitry Bogdanov 1661843c0a8aSMike Christie /* process pending command queue */ 16623bbaaad9SMike Christie while (!list_empty(&conn->cmdqueue)) { 16635923d64bSMike Christie task = list_entry(conn->cmdqueue.next, struct iscsi_task, 16645d12c05eSMike Christie running); 16655923d64bSMike Christie list_del_init(&task->running); 1666b3a7ea8dSMike Christie if (conn->session->state == ISCSI_STATE_LOGGING_OUT) { 16675923d64bSMike Christie fail_scsi_task(task, DID_IMM_RETRY); 1668b3a7ea8dSMike Christie continue; 1669b3a7ea8dSMike Christie } 16705923d64bSMike Christie rc = iscsi_prep_scsi_cmd_pdu(task); 1671577577daSMike Christie if (rc) { 1672d28d48c6SMike Christie if (rc == -ENOMEM || rc == -EACCES) 16735923d64bSMike Christie fail_scsi_task(task, DID_IMM_RETRY); 1674d28d48c6SMike Christie else 16755923d64bSMike Christie fail_scsi_task(task, DID_ABORT); 1676004d6530SBoaz Harrosh continue; 1677004d6530SBoaz Harrosh } 16785923d64bSMike Christie rc = iscsi_xmit_task(conn, task, false); 16793219e529SMike Christie if (rc) 168070b31c15SMike Christie goto done; 168177a23c21SMike Christie /* 16829c19a7d0SMike Christie * we could continuously get new task requests so 168377a23c21SMike Christie * we need to check the mgmt queue for nops that need to 168477a23c21SMike Christie * be sent to aviod starvation 168577a23c21SMike Christie */ 1686843c0a8aSMike Christie if (!list_empty(&conn->mgmtqueue)) 1687843c0a8aSMike Christie goto check_mgmt; 168865080c51SDmitry Bogdanov if (!list_empty(&conn->requeue)) 168965080c51SDmitry Bogdanov goto check_requeue; 1690843c0a8aSMike Christie } 1691843c0a8aSMike Christie 1692659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 16933219e529SMike Christie return -ENODATA; 16947996a778SMike Christie 169570b31c15SMike Christie done: 1696659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 16973219e529SMike Christie return rc; 16987996a778SMike Christie } 16997996a778SMike Christie 1700c4028958SDavid Howells static void iscsi_xmitworker(struct work_struct *work) 17017996a778SMike Christie { 1702c4028958SDavid Howells struct iscsi_conn *conn = 1703c4028958SDavid Howells container_of(work, struct iscsi_conn, xmitwork); 17043219e529SMike Christie int rc; 17057996a778SMike Christie /* 17067996a778SMike Christie * serialize Xmit worker on a per-connection basis. 17077996a778SMike Christie */ 17083219e529SMike Christie do { 17093219e529SMike Christie rc = iscsi_data_xmit(conn); 17103219e529SMike Christie } while (rc >= 0 || rc == -EAGAIN); 17117996a778SMike Christie } 17127996a778SMike Christie 1713577577daSMike Christie static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn, 1714577577daSMike Christie struct scsi_cmnd *sc) 1715577577daSMike Christie { 1716577577daSMike Christie struct iscsi_task *task; 1717577577daSMike Christie 17187acd72ebSStefani Seibold if (!kfifo_out(&conn->session->cmdpool.queue, 1719577577daSMike Christie (void *) &task, sizeof(void *))) 1720577577daSMike Christie return NULL; 1721577577daSMike Christie 1722db22de3eSBart Van Assche iscsi_cmd(sc)->age = conn->session->age; 1723db22de3eSBart Van Assche iscsi_cmd(sc)->task = task; 1724577577daSMike Christie 17256dc618cdSElena Reshetova refcount_set(&task->refcount, 1); 1726577577daSMike Christie task->state = ISCSI_TASK_PENDING; 1727577577daSMike Christie task->conn = conn; 1728577577daSMike Christie task->sc = sc; 1729d355e57dSMike Christie task->have_checked_conn = false; 1730d355e57dSMike Christie task->last_timeout = jiffies; 1731d355e57dSMike Christie task->last_xfer = jiffies; 173255e51edaSSagi Grimberg task->protected = false; 1733577577daSMike Christie INIT_LIST_HEAD(&task->running); 1734577577daSMike Christie return task; 1735577577daSMike Christie } 1736577577daSMike Christie 17377996a778SMike Christie enum { 17387996a778SMike Christie FAILURE_BAD_HOST = 1, 17397996a778SMike Christie FAILURE_SESSION_FAILED, 17407996a778SMike Christie FAILURE_SESSION_FREED, 17417996a778SMike Christie FAILURE_WINDOW_CLOSED, 174260ecebf5SMike Christie FAILURE_OOM, 17437996a778SMike Christie FAILURE_SESSION_TERMINATE, 1744656cffc9SMike Christie FAILURE_SESSION_IN_RECOVERY, 17457996a778SMike Christie FAILURE_SESSION_RECOVERY_TIMEOUT, 1746b3a7ea8dSMike Christie FAILURE_SESSION_LOGGING_OUT, 17476eabafbeSMike Christie FAILURE_SESSION_NOT_READY, 17487996a778SMike Christie }; 17497996a778SMike Christie 1750f41d4721SMike Christie int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc) 17517996a778SMike Christie { 175275613521SMike Christie struct iscsi_cls_session *cls_session; 17531336aed1SMike Christie struct iscsi_host *ihost; 17547996a778SMike Christie int reason = 0; 17557996a778SMike Christie struct iscsi_session *session; 17567996a778SMike Christie struct iscsi_conn *conn; 17579c19a7d0SMike Christie struct iscsi_task *task = NULL; 17587996a778SMike Christie 17597996a778SMike Christie sc->result = 0; 1760db22de3eSBart Van Assche iscsi_cmd(sc)->task = NULL; 17617996a778SMike Christie 17621336aed1SMike Christie ihost = shost_priv(host); 17637996a778SMike Christie 176475613521SMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 176575613521SMike Christie session = cls_session->dd_data; 1766659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 17677996a778SMike Christie 176875613521SMike Christie reason = iscsi_session_chkready(cls_session); 17696eabafbeSMike Christie if (reason) { 17706eabafbeSMike Christie sc->result = reason; 17716eabafbeSMike Christie goto fault; 17726eabafbeSMike Christie } 17736eabafbeSMike Christie 1774301e0f7eSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) { 1775656cffc9SMike Christie /* 1776656cffc9SMike Christie * to handle the race between when we set the recovery state 1777656cffc9SMike Christie * and block the session we requeue here (commands could 1778656cffc9SMike Christie * be entering our queuecommand while a block is starting 1779656cffc9SMike Christie * up because the block code is not locked) 1780656cffc9SMike Christie */ 17819000bcd6SMike Christie switch (session->state) { 1782301e0f7eSMike Christie case ISCSI_STATE_FAILED: 1783d7549412SRafael David Tinoco /* 1784d7549412SRafael David Tinoco * cmds should fail during shutdown, if the session 1785d7549412SRafael David Tinoco * state is bad, allowing completion to happen 1786d7549412SRafael David Tinoco */ 1787d7549412SRafael David Tinoco if (unlikely(system_state != SYSTEM_RUNNING)) { 1788d7549412SRafael David Tinoco reason = FAILURE_SESSION_FAILED; 1789d7549412SRafael David Tinoco sc->result = DID_NO_CONNECT << 16; 1790d7549412SRafael David Tinoco break; 1791d7549412SRafael David Tinoco } 1792df561f66SGustavo A. R. Silva fallthrough; 17939000bcd6SMike Christie case ISCSI_STATE_IN_RECOVERY: 1794656cffc9SMike Christie reason = FAILURE_SESSION_IN_RECOVERY; 1795301e0f7eSMike Christie sc->result = DID_IMM_RETRY << 16; 1796301e0f7eSMike Christie break; 17979000bcd6SMike Christie case ISCSI_STATE_LOGGING_OUT: 17989000bcd6SMike Christie reason = FAILURE_SESSION_LOGGING_OUT; 1799301e0f7eSMike Christie sc->result = DID_IMM_RETRY << 16; 1800301e0f7eSMike Christie break; 1801b3a7ea8dSMike Christie case ISCSI_STATE_RECOVERY_FAILED: 1802656cffc9SMike Christie reason = FAILURE_SESSION_RECOVERY_TIMEOUT; 180356d7fcfaSMike Christie sc->result = DID_TRANSPORT_FAILFAST << 16; 1804b3a7ea8dSMike Christie break; 1805b3a7ea8dSMike Christie case ISCSI_STATE_TERMINATE: 1806656cffc9SMike Christie reason = FAILURE_SESSION_TERMINATE; 18076eabafbeSMike Christie sc->result = DID_NO_CONNECT << 16; 1808b3a7ea8dSMike Christie break; 1809b3a7ea8dSMike Christie default: 18107996a778SMike Christie reason = FAILURE_SESSION_FREED; 18116eabafbeSMike Christie sc->result = DID_NO_CONNECT << 16; 1812b3a7ea8dSMike Christie } 18137996a778SMike Christie goto fault; 18147996a778SMike Christie } 18157996a778SMike Christie 18167996a778SMike Christie conn = session->leadconn; 181798644047SMike Christie if (!conn) { 181898644047SMike Christie reason = FAILURE_SESSION_FREED; 18196eabafbeSMike Christie sc->result = DID_NO_CONNECT << 16; 182098644047SMike Christie goto fault; 182198644047SMike Christie } 18227996a778SMike Christie 18235bd85625SMike Christie if (test_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags)) { 1824661134adSMike Christie reason = FAILURE_SESSION_IN_RECOVERY; 1825eef9ffdfSJohannes Thumshirn sc->result = DID_REQUEUE << 16; 1826661134adSMike Christie goto fault; 1827661134adSMike Christie } 1828661134adSMike Christie 182977a23c21SMike Christie if (iscsi_check_cmdsn_window_closed(conn)) { 183077a23c21SMike Christie reason = FAILURE_WINDOW_CLOSED; 183177a23c21SMike Christie goto reject; 183277a23c21SMike Christie } 183377a23c21SMike Christie 1834577577daSMike Christie task = iscsi_alloc_task(conn, sc); 1835577577daSMike Christie if (!task) { 183660ecebf5SMike Christie reason = FAILURE_OOM; 183760ecebf5SMike Christie goto reject; 183860ecebf5SMike Christie } 18397996a778SMike Christie 18401336aed1SMike Christie if (!ihost->workq) { 1841577577daSMike Christie reason = iscsi_prep_scsi_cmd_pdu(task); 1842577577daSMike Christie if (reason) { 18435d12c05eSMike Christie if (reason == -ENOMEM || reason == -EACCES) { 1844577577daSMike Christie reason = FAILURE_OOM; 1845577577daSMike Christie goto prepd_reject; 1846577577daSMike Christie } else { 1847052d0144SMike Christie sc->result = DID_ABORT << 16; 1848577577daSMike Christie goto prepd_fault; 1849577577daSMike Christie } 1850052d0144SMike Christie } 18519c19a7d0SMike Christie if (session->tt->xmit_task(task)) { 1852d3305f34SMike Christie session->cmdsn--; 1853052d0144SMike Christie reason = FAILURE_SESSION_NOT_READY; 1854577577daSMike Christie goto prepd_reject; 1855052d0144SMike Christie } 18563bbaaad9SMike Christie } else { 18573bbaaad9SMike Christie list_add_tail(&task->running, &conn->cmdqueue); 18584b9f8ce4SMike Christie iscsi_conn_queue_xmit(conn); 18593bbaaad9SMike Christie } 1860052d0144SMike Christie 1861052d0144SMike Christie session->queued_cmdsn++; 1862659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 18637996a778SMike Christie return 0; 18647996a778SMike Christie 1865577577daSMike Christie prepd_reject: 1866a656183eSLee Duncan spin_lock_bh(&session->back_lock); 1867f41d4721SMike Christie iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ); 1868a656183eSLee Duncan spin_unlock_bh(&session->back_lock); 18697996a778SMike Christie reject: 1870659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 18711b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n", 18721b2c7af8SMike Christie sc->cmnd[0], reason); 1873d6d13ee1SMike Christie return SCSI_MLQUEUE_TARGET_BUSY; 18747996a778SMike Christie 1875577577daSMike Christie prepd_fault: 1876a656183eSLee Duncan spin_lock_bh(&session->back_lock); 1877f41d4721SMike Christie iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ); 1878a656183eSLee Duncan spin_unlock_bh(&session->back_lock); 18797996a778SMike Christie fault: 1880659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 18811b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n", 18821b2c7af8SMike Christie sc->cmnd[0], reason); 18831c138991SFUJITA Tomonori scsi_set_resid(sc, scsi_bufflen(sc)); 1884b4b84edcSBart Van Assche scsi_done(sc); 18857996a778SMike Christie return 0; 18867996a778SMike Christie } 18877996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_queuecommand); 18887996a778SMike Christie 18896b5d6c44SMike Christie int iscsi_target_alloc(struct scsi_target *starget) 18906b5d6c44SMike Christie { 18916b5d6c44SMike Christie struct iscsi_cls_session *cls_session = starget_to_session(starget); 18926b5d6c44SMike Christie struct iscsi_session *session = cls_session->dd_data; 18936b5d6c44SMike Christie 18946b5d6c44SMike Christie starget->can_queue = session->scsi_cmds_max; 18956b5d6c44SMike Christie return 0; 18966b5d6c44SMike Christie } 18976b5d6c44SMike Christie EXPORT_SYMBOL_GPL(iscsi_target_alloc); 18986b5d6c44SMike Christie 18992c4b9637SKees Cook static void iscsi_tmf_timedout(struct timer_list *t) 19007996a778SMike Christie { 1901ec29d0acSMike Christie struct iscsi_session *session = from_timer(session, t, tmf_timer); 19027996a778SMike Christie 1903659743b0SShlomo Pongratz spin_lock(&session->frwd_lock); 1904ec29d0acSMike Christie if (session->tmf_state == TMF_QUEUED) { 1905ec29d0acSMike Christie session->tmf_state = TMF_TIMEDOUT; 1906bd2199d4SErez Zilber ISCSI_DBG_EH(session, "tmf timedout\n"); 19077996a778SMike Christie /* unblock eh_abort() */ 1908ec29d0acSMike Christie wake_up(&session->ehwait); 19097996a778SMike Christie } 1910659743b0SShlomo Pongratz spin_unlock(&session->frwd_lock); 19117996a778SMike Christie } 19127996a778SMike Christie 19139c19a7d0SMike Christie static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn, 1914f6d5180cSMike Christie struct iscsi_tm *hdr, int age, 1915f6d5180cSMike Christie int timeout) 19161360c58aSBart Van Assche __must_hold(&session->frwd_lock) 19177996a778SMike Christie { 19187996a778SMike Christie struct iscsi_session *session = conn->session; 19197996a778SMike Christie 19206e637b72SMike Christie if (__iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0)) { 1921659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 1922df4da5cdSMike Christie iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n"); 19237996a778SMike Christie iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 1924659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 192577a23c21SMike Christie return -EPERM; 19267996a778SMike Christie } 19277996a778SMike Christie conn->tmfcmd_pdus_cnt++; 1928ec29d0acSMike Christie session->tmf_timer.expires = timeout * HZ + jiffies; 1929ec29d0acSMike Christie add_timer(&session->tmf_timer); 1930bd2199d4SErez Zilber ISCSI_DBG_EH(session, "tmf set timeout\n"); 1931843c0a8aSMike Christie 1932659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 19336724add1SMike Christie mutex_unlock(&session->eh_mutex); 19347996a778SMike Christie 19357996a778SMike Christie /* 19367996a778SMike Christie * block eh thread until: 19377996a778SMike Christie * 1938843c0a8aSMike Christie * 1) tmf response 1939843c0a8aSMike Christie * 2) tmf timeout 19407996a778SMike Christie * 3) session is terminated or restarted or userspace has 19417996a778SMike Christie * given up on recovery 19427996a778SMike Christie */ 1943ec29d0acSMike Christie wait_event_interruptible(session->ehwait, age != session->age || 19447996a778SMike Christie session->state != ISCSI_STATE_LOGGED_IN || 1945ec29d0acSMike Christie session->tmf_state != TMF_QUEUED); 19467996a778SMike Christie if (signal_pending(current)) 19477996a778SMike Christie flush_signals(current); 1948ec29d0acSMike Christie del_timer_sync(&session->tmf_timer); 1949843c0a8aSMike Christie 19506724add1SMike Christie mutex_lock(&session->eh_mutex); 1951659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 19529c19a7d0SMike Christie /* if the session drops it will clean up the task */ 1953843c0a8aSMike Christie if (age != session->age || 1954843c0a8aSMike Christie session->state != ISCSI_STATE_LOGGED_IN) 1955843c0a8aSMike Christie return -ENOTCONN; 19567996a778SMike Christie return 0; 19577996a778SMike Christie } 19587996a778SMike Christie 19597996a778SMike Christie /* 196014936b1eSMike Christie * Fail commands. session frwd lock held and xmit thread flushed. 1961843c0a8aSMike Christie */ 19629cb78c16SHannes Reinecke static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error) 1963843c0a8aSMike Christie { 196414936b1eSMike Christie struct iscsi_session *session = conn->session; 19653bbaaad9SMike Christie struct iscsi_task *task; 19663bbaaad9SMike Christie int i; 1967843c0a8aSMike Christie 1968e1c6a7ecSMike Christie restart_cmd_loop: 196914936b1eSMike Christie spin_lock_bh(&session->back_lock); 197014936b1eSMike Christie for (i = 0; i < session->cmds_max; i++) { 197114936b1eSMike Christie task = session->cmds[i]; 19723bbaaad9SMike Christie if (!task->sc || task->state == ISCSI_TASK_FREE) 19733bbaaad9SMike Christie continue; 1974843c0a8aSMike Christie 19753bbaaad9SMike Christie if (lun != -1 && lun != task->sc->device->lun) 19763bbaaad9SMike Christie continue; 1977e1c6a7ecSMike Christie /* 1978e1c6a7ecSMike Christie * The cmd is completing but if this is called from an eh 1979e1c6a7ecSMike Christie * callout path then when we return scsi-ml owns the cmd. Wait 1980e1c6a7ecSMike Christie * for the completion path to finish freeing the cmd. 1981e1c6a7ecSMike Christie */ 1982e1c6a7ecSMike Christie if (!iscsi_get_task(task)) { 198314936b1eSMike Christie spin_unlock_bh(&session->back_lock); 1984e1c6a7ecSMike Christie spin_unlock_bh(&session->frwd_lock); 1985e1c6a7ecSMike Christie udelay(ISCSI_CMD_COMPL_WAIT); 1986e1c6a7ecSMike Christie spin_lock_bh(&session->frwd_lock); 1987e1c6a7ecSMike Christie goto restart_cmd_loop; 1988e1c6a7ecSMike Christie } 198914936b1eSMike Christie 199014936b1eSMike Christie ISCSI_DBG_SESSION(session, 19913bbaaad9SMike Christie "failing sc %p itt 0x%x state %d\n", 19923bbaaad9SMike Christie task->sc, task->itt, task->state); 1993e1c6a7ecSMike Christie __fail_scsi_task(task, error); 1994e1c6a7ecSMike Christie __iscsi_put_task(task); 1995843c0a8aSMike Christie } 199614936b1eSMike Christie spin_unlock_bh(&session->back_lock); 1997843c0a8aSMike Christie } 1998843c0a8aSMike Christie 1999661134adSMike Christie /** 2000661134adSMike Christie * iscsi_suspend_queue - suspend iscsi_queuecommand 2001661134adSMike Christie * @conn: iscsi conn to stop queueing IO on 2002661134adSMike Christie * 2003659743b0SShlomo Pongratz * This grabs the session frwd_lock to make sure no one is in 2004661134adSMike Christie * xmit_task/queuecommand, and then sets suspend to prevent 2005661134adSMike Christie * new commands from being queued. This only needs to be called 2006661134adSMike Christie * by offload drivers that need to sync a path like ep disconnect 2007661134adSMike Christie * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi 2008661134adSMike Christie * will call iscsi_start_tx and iscsi_unblock_session when in FFP. 2009661134adSMike Christie */ 2010661134adSMike Christie void iscsi_suspend_queue(struct iscsi_conn *conn) 2011661134adSMike Christie { 2012659743b0SShlomo Pongratz spin_lock_bh(&conn->session->frwd_lock); 20135bd85625SMike Christie set_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags); 2014659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 2015661134adSMike Christie } 2016661134adSMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_queue); 2017661134adSMike Christie 2018661134adSMike Christie /** 2019661134adSMike Christie * iscsi_suspend_tx - suspend iscsi_data_xmit 20208af80996SMike Christie * @conn: iscsi conn to stop processing IO on. 2021661134adSMike Christie * 2022661134adSMike Christie * This function sets the suspend bit to prevent iscsi_data_xmit 2023661134adSMike Christie * from sending new IO, and if work is queued on the xmit thread 2024661134adSMike Christie * it will wait for it to be completed. 2025661134adSMike Christie */ 2026b40977d9SMike Christie void iscsi_suspend_tx(struct iscsi_conn *conn) 20276724add1SMike Christie { 202832ae763eSMike Christie struct Scsi_Host *shost = conn->session->host; 202932ae763eSMike Christie struct iscsi_host *ihost = shost_priv(shost); 203032ae763eSMike Christie 20315bd85625SMike Christie set_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags); 20321336aed1SMike Christie if (ihost->workq) 20338af80996SMike Christie flush_work(&conn->xmitwork); 20346724add1SMike Christie } 2035b40977d9SMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_tx); 20366724add1SMike Christie 20376724add1SMike Christie static void iscsi_start_tx(struct iscsi_conn *conn) 20386724add1SMike Christie { 20395bd85625SMike Christie clear_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags); 20404b9f8ce4SMike Christie iscsi_conn_queue_xmit(conn); 20416724add1SMike Christie } 20426724add1SMike Christie 20438af80996SMike Christie /** 20448af80996SMike Christie * iscsi_suspend_rx - Prevent recvwork from running again. 20458af80996SMike Christie * @conn: iscsi conn to stop. 20468af80996SMike Christie */ 20478af80996SMike Christie void iscsi_suspend_rx(struct iscsi_conn *conn) 20488af80996SMike Christie { 20498af80996SMike Christie struct Scsi_Host *shost = conn->session->host; 20508af80996SMike Christie struct iscsi_host *ihost = shost_priv(shost); 20518af80996SMike Christie 20528af80996SMike Christie set_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags); 20538af80996SMike Christie if (ihost->workq) 20548af80996SMike Christie flush_work(&conn->recvwork); 20558af80996SMike Christie } 20568af80996SMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_rx); 20578af80996SMike Christie 20584c48a829SMike Christie /* 20594c48a829SMike Christie * We want to make sure a ping is in flight. It has timed out. 20604c48a829SMike Christie * And we are not busy processing a pdu that is making 20614c48a829SMike Christie * progress but got started before the ping and is taking a while 20624c48a829SMike Christie * to complete so the ping is just stuck behind it in a queue. 20634c48a829SMike Christie */ 20644c48a829SMike Christie static int iscsi_has_ping_timed_out(struct iscsi_conn *conn) 20654c48a829SMike Christie { 2066fe0a8a95SLee Duncan if (READ_ONCE(conn->ping_task) && 20674c48a829SMike Christie time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) + 20684c48a829SMike Christie (conn->ping_timeout * HZ), jiffies)) 20694c48a829SMike Christie return 1; 20704c48a829SMike Christie else 20714c48a829SMike Christie return 0; 20724c48a829SMike Christie } 20734c48a829SMike Christie 2074*dee7121eSBart Van Assche enum scsi_timeout_action iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc) 2075f6d5180cSMike Christie { 2076*dee7121eSBart Van Assche enum scsi_timeout_action rc = SCSI_EH_NOT_HANDLED; 207792ed4d69SMike Christie struct iscsi_task *task = NULL, *running_task; 2078f6d5180cSMike Christie struct iscsi_cls_session *cls_session; 2079f6d5180cSMike Christie struct iscsi_session *session; 2080f6d5180cSMike Christie struct iscsi_conn *conn; 208192ed4d69SMike Christie int i; 2082f6d5180cSMike Christie 2083d355e57dSMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 208475613521SMike Christie session = cls_session->dd_data; 2085f6d5180cSMike Christie 2086bd2199d4SErez Zilber ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc); 2087f6d5180cSMike Christie 20885480e299SBart Van Assche spin_lock_bh(&session->frwd_lock); 208914936b1eSMike Christie spin_lock(&session->back_lock); 2090db22de3eSBart Van Assche task = iscsi_cmd(sc)->task; 2091e3d338a5SMike Christie if (!task) { 2092e3d338a5SMike Christie /* 2093e3d338a5SMike Christie * Raced with completion. Blk layer has taken ownership 2094e3d338a5SMike Christie * so let timeout code complete it now. 2095e3d338a5SMike Christie */ 2096*dee7121eSBart Van Assche rc = SCSI_EH_NOT_HANDLED; 209714936b1eSMike Christie spin_unlock(&session->back_lock); 2098e3d338a5SMike Christie goto done; 2099e3d338a5SMike Christie } 2100e1c6a7ecSMike Christie if (!iscsi_get_task(task)) { 2101e1c6a7ecSMike Christie /* 2102e1c6a7ecSMike Christie * Racing with the completion path right now, so give it more 2103e1c6a7ecSMike Christie * time so that path can complete it like normal. 2104e1c6a7ecSMike Christie */ 2105*dee7121eSBart Van Assche rc = SCSI_EH_RESET_TIMER; 2106e1c6a7ecSMike Christie task = NULL; 2107e1c6a7ecSMike Christie spin_unlock(&session->back_lock); 2108e1c6a7ecSMike Christie goto done; 2109e1c6a7ecSMike Christie } 211014936b1eSMike Christie spin_unlock(&session->back_lock); 2111e3d338a5SMike Christie 2112f6d5180cSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) { 2113f6d5180cSMike Christie /* 2114d7549412SRafael David Tinoco * During shutdown, if session is prematurely disconnected, 2115d7549412SRafael David Tinoco * recovery won't happen and there will be hung cmds. Not 2116d7549412SRafael David Tinoco * handling cmds would trigger EH, also bad in this case. 2117d7549412SRafael David Tinoco * Instead, handle cmd, allow completion to happen and let 2118d7549412SRafael David Tinoco * upper layer to deal with the result. 2119d7549412SRafael David Tinoco */ 2120d7549412SRafael David Tinoco if (unlikely(system_state != SYSTEM_RUNNING)) { 2121d7549412SRafael David Tinoco sc->result = DID_NO_CONNECT << 16; 2122d7549412SRafael David Tinoco ISCSI_DBG_EH(session, "sc on shutdown, handled\n"); 2123*dee7121eSBart Van Assche rc = SCSI_EH_NOT_HANDLED; 2124d7549412SRafael David Tinoco goto done; 2125d7549412SRafael David Tinoco } 2126d7549412SRafael David Tinoco /* 2127f6d5180cSMike Christie * We are probably in the middle of iscsi recovery so let 2128f6d5180cSMike Christie * that complete and handle the error. 2129f6d5180cSMike Christie */ 2130*dee7121eSBart Van Assche rc = SCSI_EH_RESET_TIMER; 2131f6d5180cSMike Christie goto done; 2132f6d5180cSMike Christie } 2133f6d5180cSMike Christie 2134f6d5180cSMike Christie conn = session->leadconn; 2135f6d5180cSMike Christie if (!conn) { 2136f6d5180cSMike Christie /* In the middle of shuting down */ 2137*dee7121eSBart Van Assche rc = SCSI_EH_RESET_TIMER; 2138f6d5180cSMike Christie goto done; 2139f6d5180cSMike Christie } 2140f6d5180cSMike Christie 2141d355e57dSMike Christie /* 2142d355e57dSMike Christie * If we have sent (at least queued to the network layer) a pdu or 2143d355e57dSMike Christie * recvd one for the task since the last timeout ask for 2144d355e57dSMike Christie * more time. If on the next timeout we have not made progress 2145d355e57dSMike Christie * we can check if it is the task or connection when we send the 2146d355e57dSMike Christie * nop as a ping. 2147d355e57dSMike Christie */ 214892ed4d69SMike Christie if (time_after(task->last_xfer, task->last_timeout)) { 2149bd2199d4SErez Zilber ISCSI_DBG_EH(session, "Command making progress. Asking " 2150d355e57dSMike Christie "scsi-ml for more time to complete. " 215192ed4d69SMike Christie "Last data xfer at %lu. Last timeout was at " 2152d355e57dSMike Christie "%lu\n.", task->last_xfer, task->last_timeout); 2153d355e57dSMike Christie task->have_checked_conn = false; 2154*dee7121eSBart Van Assche rc = SCSI_EH_RESET_TIMER; 2155d355e57dSMike Christie goto done; 2156d355e57dSMike Christie } 2157d355e57dSMike Christie 2158f6d5180cSMike Christie if (!conn->recv_timeout && !conn->ping_timeout) 2159f6d5180cSMike Christie goto done; 2160f6d5180cSMike Christie /* 2161f6d5180cSMike Christie * if the ping timedout then we are in the middle of cleaning up 2162f6d5180cSMike Christie * and can let the iscsi eh handle it 2163f6d5180cSMike Christie */ 21644c48a829SMike Christie if (iscsi_has_ping_timed_out(conn)) { 2165*dee7121eSBart Van Assche rc = SCSI_EH_RESET_TIMER; 21664c48a829SMike Christie goto done; 21674c48a829SMike Christie } 2168d355e57dSMike Christie 216914936b1eSMike Christie spin_lock(&session->back_lock); 217092ed4d69SMike Christie for (i = 0; i < conn->session->cmds_max; i++) { 217192ed4d69SMike Christie running_task = conn->session->cmds[i]; 217292ed4d69SMike Christie if (!running_task->sc || running_task == task || 217392ed4d69SMike Christie running_task->state != ISCSI_TASK_RUNNING) 217492ed4d69SMike Christie continue; 217592ed4d69SMike Christie 217692ed4d69SMike Christie /* 217792ed4d69SMike Christie * Only check if cmds started before this one have made 217892ed4d69SMike Christie * progress, or this could never fail 217992ed4d69SMike Christie */ 218092ed4d69SMike Christie if (time_after(running_task->sc->jiffies_at_alloc, 218192ed4d69SMike Christie task->sc->jiffies_at_alloc)) 218292ed4d69SMike Christie continue; 218392ed4d69SMike Christie 218492ed4d69SMike Christie if (time_after(running_task->last_xfer, task->last_timeout)) { 218592ed4d69SMike Christie /* 218692ed4d69SMike Christie * This task has not made progress, but a task 218792ed4d69SMike Christie * started before us has transferred data since 218892ed4d69SMike Christie * we started/last-checked. We could be queueing 218992ed4d69SMike Christie * too many tasks or the LU is bad. 219092ed4d69SMike Christie * 219192ed4d69SMike Christie * If the device is bad the cmds ahead of us on 219292ed4d69SMike Christie * other devs will complete, and this loop will 219392ed4d69SMike Christie * eventually fail starting the scsi eh. 219492ed4d69SMike Christie */ 219592ed4d69SMike Christie ISCSI_DBG_EH(session, "Command has not made progress " 219692ed4d69SMike Christie "but commands ahead of it have. " 219792ed4d69SMike Christie "Asking scsi-ml for more time to " 219892ed4d69SMike Christie "complete. Our last xfer vs running task " 219992ed4d69SMike Christie "last xfer %lu/%lu. Last check %lu.\n", 220092ed4d69SMike Christie task->last_xfer, running_task->last_xfer, 220192ed4d69SMike Christie task->last_timeout); 220214936b1eSMike Christie spin_unlock(&session->back_lock); 2203*dee7121eSBart Van Assche rc = SCSI_EH_RESET_TIMER; 220492ed4d69SMike Christie goto done; 220592ed4d69SMike Christie } 220692ed4d69SMike Christie } 220714936b1eSMike Christie spin_unlock(&session->back_lock); 220892ed4d69SMike Christie 2209d355e57dSMike Christie /* Assumes nop timeout is shorter than scsi cmd timeout */ 2210d355e57dSMike Christie if (task->have_checked_conn) 2211d355e57dSMike Christie goto done; 2212d355e57dSMike Christie 2213f6d5180cSMike Christie /* 2214d355e57dSMike Christie * Checking the transport already or nop from a cmd timeout still 2215d355e57dSMike Christie * running 2216f6d5180cSMike Christie */ 2217fe0a8a95SLee Duncan if (READ_ONCE(conn->ping_task)) { 2218d355e57dSMike Christie task->have_checked_conn = true; 2219*dee7121eSBart Van Assche rc = SCSI_EH_RESET_TIMER; 22204c48a829SMike Christie goto done; 22214c48a829SMike Christie } 22224c48a829SMike Christie 2223d355e57dSMike Christie /* Make sure there is a transport check done */ 2224d355e57dSMike Christie iscsi_send_nopout(conn, NULL); 2225d355e57dSMike Christie task->have_checked_conn = true; 2226*dee7121eSBart Van Assche rc = SCSI_EH_RESET_TIMER; 2227d355e57dSMike Christie 2228f6d5180cSMike Christie done: 22295480e299SBart Van Assche spin_unlock_bh(&session->frwd_lock); 223014936b1eSMike Christie 223114936b1eSMike Christie if (task) { 223214936b1eSMike Christie task->last_timeout = jiffies; 223314936b1eSMike Christie iscsi_put_task(task); 223414936b1eSMike Christie } 2235*dee7121eSBart Van Assche ISCSI_DBG_EH(session, "return %s\n", rc == SCSI_EH_RESET_TIMER ? 2236d7549412SRafael David Tinoco "timer reset" : "shutdown or nh"); 2237f6d5180cSMike Christie return rc; 2238f6d5180cSMike Christie } 2239b6a05c82SChristoph Hellwig EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out); 2240f6d5180cSMike Christie 22412c4b9637SKees Cook static void iscsi_check_transport_timeouts(struct timer_list *t) 2242f6d5180cSMike Christie { 22432c4b9637SKees Cook struct iscsi_conn *conn = from_timer(conn, t, transport_timer); 2244f6d5180cSMike Christie struct iscsi_session *session = conn->session; 22454cf10435SMike Christie unsigned long recv_timeout, next_timeout = 0, last_recv; 2246f6d5180cSMike Christie 2247659743b0SShlomo Pongratz spin_lock(&session->frwd_lock); 2248f6d5180cSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) 2249f6d5180cSMike Christie goto done; 2250f6d5180cSMike Christie 22514cf10435SMike Christie recv_timeout = conn->recv_timeout; 22524cf10435SMike Christie if (!recv_timeout) 2253f6d5180cSMike Christie goto done; 2254f6d5180cSMike Christie 22554cf10435SMike Christie recv_timeout *= HZ; 2256f6d5180cSMike Christie last_recv = conn->last_recv; 22574c48a829SMike Christie 22584c48a829SMike Christie if (iscsi_has_ping_timed_out(conn)) { 2259322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs " 22604c48a829SMike Christie "expired, recv timeout %d, last rx %lu, " 22614c48a829SMike Christie "last ping %lu, now %lu\n", 22624c48a829SMike Christie conn->ping_timeout, conn->recv_timeout, 22634c48a829SMike Christie last_recv, conn->last_ping, jiffies); 2264659743b0SShlomo Pongratz spin_unlock(&session->frwd_lock); 226509ff742cSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT); 2266f6d5180cSMike Christie return; 2267f6d5180cSMike Christie } 2268f6d5180cSMike Christie 22694cf10435SMike Christie if (time_before_eq(last_recv + recv_timeout, jiffies)) { 2270f6d5180cSMike Christie /* send a ping to try to provoke some traffic */ 22711b2c7af8SMike Christie ISCSI_DBG_CONN(conn, "Sending nopout as ping\n"); 227252f5664aSAriel Nahum if (iscsi_send_nopout(conn, NULL)) 227352f5664aSAriel Nahum next_timeout = jiffies + (1 * HZ); 227452f5664aSAriel Nahum else 22754cf10435SMike Christie next_timeout = conn->last_ping + (conn->ping_timeout * HZ); 2276ad294e9cSMike Christie } else 22774cf10435SMike Christie next_timeout = last_recv + recv_timeout; 2278f6d5180cSMike Christie 22791b2c7af8SMike Christie ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout); 2280f6d5180cSMike Christie mod_timer(&conn->transport_timer, next_timeout); 2281f6d5180cSMike Christie done: 2282659743b0SShlomo Pongratz spin_unlock(&session->frwd_lock); 2283f6d5180cSMike Christie } 2284f6d5180cSMike Christie 2285891e2639SMike Christie /** 2286891e2639SMike Christie * iscsi_conn_unbind - prevent queueing to conn. 2287891e2639SMike Christie * @cls_conn: iscsi conn ep is bound to. 2288891e2639SMike Christie * @is_active: is the conn in use for boot or is this for EH/termination 2289891e2639SMike Christie * 2290891e2639SMike Christie * This must be called by drivers implementing the ep_disconnect callout. 2291891e2639SMike Christie * It disables queueing to the connection from libiscsi in preparation for 2292891e2639SMike Christie * an ep_disconnect call. 2293891e2639SMike Christie */ 2294891e2639SMike Christie void iscsi_conn_unbind(struct iscsi_cls_conn *cls_conn, bool is_active) 2295891e2639SMike Christie { 2296891e2639SMike Christie struct iscsi_session *session; 2297891e2639SMike Christie struct iscsi_conn *conn; 2298891e2639SMike Christie 2299891e2639SMike Christie if (!cls_conn) 2300891e2639SMike Christie return; 2301891e2639SMike Christie 2302891e2639SMike Christie conn = cls_conn->dd_data; 2303891e2639SMike Christie session = conn->session; 2304891e2639SMike Christie /* 2305891e2639SMike Christie * Wait for iscsi_eh calls to exit. We don't wait for the tmf to 2306891e2639SMike Christie * complete or timeout. The caller just wants to know what's running 2307891e2639SMike Christie * is everything that needs to be cleaned up, and no cmds will be 2308891e2639SMike Christie * queued. 2309891e2639SMike Christie */ 2310891e2639SMike Christie mutex_lock(&session->eh_mutex); 2311891e2639SMike Christie 2312891e2639SMike Christie iscsi_suspend_queue(conn); 2313891e2639SMike Christie iscsi_suspend_tx(conn); 2314891e2639SMike Christie 2315891e2639SMike Christie spin_lock_bh(&session->frwd_lock); 231644ac9710SMike Christie clear_bit(ISCSI_CONN_FLAG_BOUND, &conn->flags); 231744ac9710SMike Christie 2318891e2639SMike Christie if (!is_active) { 2319891e2639SMike Christie /* 2320891e2639SMike Christie * if logout timed out before userspace could even send a PDU 2321891e2639SMike Christie * the state might still be in ISCSI_STATE_LOGGED_IN and 2322891e2639SMike Christie * allowing new cmds and TMFs. 2323891e2639SMike Christie */ 2324891e2639SMike Christie if (session->state == ISCSI_STATE_LOGGED_IN) 2325891e2639SMike Christie iscsi_set_conn_failed(conn); 2326891e2639SMike Christie } 2327891e2639SMike Christie spin_unlock_bh(&session->frwd_lock); 2328891e2639SMike Christie mutex_unlock(&session->eh_mutex); 2329891e2639SMike Christie } 2330891e2639SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_unbind); 2331891e2639SMike Christie 23329c19a7d0SMike Christie static void iscsi_prep_abort_task_pdu(struct iscsi_task *task, 2333843c0a8aSMike Christie struct iscsi_tm *hdr) 2334843c0a8aSMike Christie { 2335843c0a8aSMike Christie memset(hdr, 0, sizeof(*hdr)); 2336843c0a8aSMike Christie hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 2337843c0a8aSMike Christie hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK; 2338843c0a8aSMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 233955bdabdfSAndy Grover hdr->lun = task->lun; 2340577577daSMike Christie hdr->rtt = task->hdr_itt; 2341577577daSMike Christie hdr->refcmdsn = task->cmdsn; 2342843c0a8aSMike Christie } 2343843c0a8aSMike Christie 23447996a778SMike Christie int iscsi_eh_abort(struct scsi_cmnd *sc) 23457996a778SMike Christie { 234675613521SMike Christie struct iscsi_cls_session *cls_session; 234775613521SMike Christie struct iscsi_session *session; 2348f47f2cf5SMike Christie struct iscsi_conn *conn; 23499c19a7d0SMike Christie struct iscsi_task *task; 2350843c0a8aSMike Christie struct iscsi_tm *hdr; 2351e9e410e8SBart Van Assche int age; 23527996a778SMike Christie 235375613521SMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 235475613521SMike Christie session = cls_session->dd_data; 235575613521SMike Christie 2356bd2199d4SErez Zilber ISCSI_DBG_EH(session, "aborting sc %p\n", sc); 23574421c9ebSMike Christie 2358e1c6a7ecSMike Christie completion_check: 23596724add1SMike Christie mutex_lock(&session->eh_mutex); 2360659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 2361f47f2cf5SMike Christie /* 2362f47f2cf5SMike Christie * if session was ISCSI_STATE_IN_RECOVERY then we may not have 2363f47f2cf5SMike Christie * got the command. 2364f47f2cf5SMike Christie */ 2365db22de3eSBart Van Assche if (!iscsi_cmd(sc)->task) { 2366bd2199d4SErez Zilber ISCSI_DBG_EH(session, "sc never reached iscsi layer or " 23671b2c7af8SMike Christie "it completed.\n"); 2368659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 23696724add1SMike Christie mutex_unlock(&session->eh_mutex); 2370f47f2cf5SMike Christie return SUCCESS; 2371f47f2cf5SMike Christie } 2372f47f2cf5SMike Christie 23737996a778SMike Christie /* 23747996a778SMike Christie * If we are not logged in or we have started a new session 23757996a778SMike Christie * then let the host reset code handle this 23767996a778SMike Christie */ 2377843c0a8aSMike Christie if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN || 2378db22de3eSBart Van Assche iscsi_cmd(sc)->age != session->age) { 2379659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2380843c0a8aSMike Christie mutex_unlock(&session->eh_mutex); 2381bd2199d4SErez Zilber ISCSI_DBG_EH(session, "failing abort due to dropped " 23824421c9ebSMike Christie "session.\n"); 2383843c0a8aSMike Christie return FAILED; 2384843c0a8aSMike Christie } 2385843c0a8aSMike Christie 238614936b1eSMike Christie spin_lock(&session->back_lock); 2387db22de3eSBart Van Assche task = iscsi_cmd(sc)->task; 238814936b1eSMike Christie if (!task || !task->sc) { 23899c19a7d0SMike Christie /* task completed before time out */ 2390bd2199d4SErez Zilber ISCSI_DBG_EH(session, "sc completed while abort in progress\n"); 239114936b1eSMike Christie 239214936b1eSMike Christie spin_unlock(&session->back_lock); 2393258aad75SMike Christie spin_unlock_bh(&session->frwd_lock); 2394258aad75SMike Christie mutex_unlock(&session->eh_mutex); 2395258aad75SMike Christie return SUCCESS; 23967ea8b828SMike Christie } 2397258aad75SMike Christie 2398e1c6a7ecSMike Christie if (!iscsi_get_task(task)) { 2399e1c6a7ecSMike Christie spin_unlock(&session->back_lock); 2400e1c6a7ecSMike Christie spin_unlock_bh(&session->frwd_lock); 2401e1c6a7ecSMike Christie mutex_unlock(&session->eh_mutex); 2402e1c6a7ecSMike Christie /* We are just about to call iscsi_free_task so wait for it. */ 2403e1c6a7ecSMike Christie udelay(ISCSI_CMD_COMPL_WAIT); 2404e1c6a7ecSMike Christie goto completion_check; 2405e1c6a7ecSMike Christie } 2406e1c6a7ecSMike Christie 2407e1c6a7ecSMike Christie ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n", sc, task->itt); 2408258aad75SMike Christie conn = session->leadconn; 2409258aad75SMike Christie iscsi_get_conn(conn->cls_conn); 2410258aad75SMike Christie conn->eh_abort_cnt++; 2411258aad75SMike Christie age = session->age; 241214936b1eSMike Christie spin_unlock(&session->back_lock); 24137996a778SMike Christie 24149c19a7d0SMike Christie if (task->state == ISCSI_TASK_PENDING) { 2415b3cd5050SMike Christie fail_scsi_task(task, DID_ABORT); 241677a23c21SMike Christie goto success; 241777a23c21SMike Christie } 24187996a778SMike Christie 2419843c0a8aSMike Christie /* only have one tmf outstanding at a time */ 2420ec29d0acSMike Christie if (session->tmf_state != TMF_INITIAL) 24217996a778SMike Christie goto failed; 2422ec29d0acSMike Christie session->tmf_state = TMF_QUEUED; 24237996a778SMike Christie 2424ec29d0acSMike Christie hdr = &session->tmhdr; 24259c19a7d0SMike Christie iscsi_prep_abort_task_pdu(task, hdr); 2426843c0a8aSMike Christie 2427e9e410e8SBart Van Assche if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) 2428843c0a8aSMike Christie goto failed; 2429843c0a8aSMike Christie 2430ec29d0acSMike Christie switch (session->tmf_state) { 2431843c0a8aSMike Christie case TMF_SUCCESS: 2432659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2433913e5bf4SMike Christie /* 2434913e5bf4SMike Christie * stop tx side incase the target had sent a abort rsp but 2435913e5bf4SMike Christie * the initiator was still writing out data. 2436913e5bf4SMike Christie */ 24376724add1SMike Christie iscsi_suspend_tx(conn); 24387996a778SMike Christie /* 2439913e5bf4SMike Christie * we do not stop the recv side because targets have been 2440913e5bf4SMike Christie * good and have never sent us a successful tmf response 2441913e5bf4SMike Christie * then sent more data for the cmd. 24427996a778SMike Christie */ 2443659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 2444b3cd5050SMike Christie fail_scsi_task(task, DID_ABORT); 2445ec29d0acSMike Christie session->tmf_state = TMF_INITIAL; 24465d12c05eSMike Christie memset(hdr, 0, sizeof(*hdr)); 2447659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 24486724add1SMike Christie iscsi_start_tx(conn); 244977a23c21SMike Christie goto success_unlocked; 2450843c0a8aSMike Christie case TMF_TIMEDOUT: 245199b06033SMike Christie session->running_aborted_task = task; 2452659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2453df4da5cdSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 2454843c0a8aSMike Christie goto failed_unlocked; 2455843c0a8aSMike Christie case TMF_NOT_FOUND: 2456f6f96457SMike Christie if (iscsi_task_is_completed(task)) { 2457ec29d0acSMike Christie session->tmf_state = TMF_INITIAL; 24585d12c05eSMike Christie memset(hdr, 0, sizeof(*hdr)); 24599c19a7d0SMike Christie /* task completed before tmf abort response */ 2460bd2199d4SErez Zilber ISCSI_DBG_EH(session, "sc completed while abort in " 2461bd2199d4SErez Zilber "progress\n"); 246277a23c21SMike Christie goto success; 246377a23c21SMike Christie } 2464df561f66SGustavo A. R. Silva fallthrough; 246577a23c21SMike Christie default: 2466ec29d0acSMike Christie session->tmf_state = TMF_INITIAL; 2467843c0a8aSMike Christie goto failed; 246877a23c21SMike Christie } 24697996a778SMike Christie 247077a23c21SMike Christie success: 2471659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 247277a23c21SMike Christie success_unlocked: 2473bd2199d4SErez Zilber ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n", 24741b2c7af8SMike Christie sc, task->itt); 247514936b1eSMike Christie iscsi_put_task(task); 2476d39df158SMike Christie iscsi_put_conn(conn->cls_conn); 24776724add1SMike Christie mutex_unlock(&session->eh_mutex); 24787996a778SMike Christie return SUCCESS; 24797996a778SMike Christie 24807996a778SMike Christie failed: 2481659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 248277a23c21SMike Christie failed_unlocked: 2483bd2199d4SErez Zilber ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc, 24849c19a7d0SMike Christie task ? task->itt : 0); 248599b06033SMike Christie /* 248699b06033SMike Christie * The driver might be accessing the task so hold the ref. The conn 248799b06033SMike Christie * stop cleanup will drop the ref after ep_disconnect so we know the 248899b06033SMike Christie * driver's no longer touching the task. 248999b06033SMike Christie */ 249099b06033SMike Christie if (!session->running_aborted_task) 249114936b1eSMike Christie iscsi_put_task(task); 249299b06033SMike Christie 2493d39df158SMike Christie iscsi_put_conn(conn->cls_conn); 24946724add1SMike Christie mutex_unlock(&session->eh_mutex); 24957996a778SMike Christie return FAILED; 24967996a778SMike Christie } 24977996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_abort); 24987996a778SMike Christie 2499843c0a8aSMike Christie static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr) 2500843c0a8aSMike Christie { 2501843c0a8aSMike Christie memset(hdr, 0, sizeof(*hdr)); 2502843c0a8aSMike Christie hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 2503843c0a8aSMike Christie hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK; 2504843c0a8aSMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 250555bdabdfSAndy Grover int_to_scsilun(sc->device->lun, &hdr->lun); 2506f6d5180cSMike Christie hdr->rtt = RESERVED_ITT; 2507843c0a8aSMike Christie } 2508843c0a8aSMike Christie 2509843c0a8aSMike Christie int iscsi_eh_device_reset(struct scsi_cmnd *sc) 2510843c0a8aSMike Christie { 251175613521SMike Christie struct iscsi_cls_session *cls_session; 251275613521SMike Christie struct iscsi_session *session; 2513843c0a8aSMike Christie struct iscsi_conn *conn; 2514843c0a8aSMike Christie struct iscsi_tm *hdr; 2515843c0a8aSMike Christie int rc = FAILED; 2516843c0a8aSMike Christie 251775613521SMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 251875613521SMike Christie session = cls_session->dd_data; 251975613521SMike Christie 25209cb78c16SHannes Reinecke ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc, 25219cb78c16SHannes Reinecke sc->device->lun); 2522843c0a8aSMike Christie 2523843c0a8aSMike Christie mutex_lock(&session->eh_mutex); 2524659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 2525843c0a8aSMike Christie /* 2526843c0a8aSMike Christie * Just check if we are not logged in. We cannot check for 2527843c0a8aSMike Christie * the phase because the reset could come from a ioctl. 2528843c0a8aSMike Christie */ 2529843c0a8aSMike Christie if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) 2530843c0a8aSMike Christie goto unlock; 2531843c0a8aSMike Christie conn = session->leadconn; 2532843c0a8aSMike Christie 2533843c0a8aSMike Christie /* only have one tmf outstanding at a time */ 2534ec29d0acSMike Christie if (session->tmf_state != TMF_INITIAL) 2535843c0a8aSMike Christie goto unlock; 2536ec29d0acSMike Christie session->tmf_state = TMF_QUEUED; 2537843c0a8aSMike Christie 2538ec29d0acSMike Christie hdr = &session->tmhdr; 2539843c0a8aSMike Christie iscsi_prep_lun_reset_pdu(sc, hdr); 2540843c0a8aSMike Christie 25419c19a7d0SMike Christie if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age, 2542f6d5180cSMike Christie session->lu_reset_timeout)) { 2543843c0a8aSMike Christie rc = FAILED; 2544843c0a8aSMike Christie goto unlock; 2545843c0a8aSMike Christie } 2546843c0a8aSMike Christie 2547ec29d0acSMike Christie switch (session->tmf_state) { 2548843c0a8aSMike Christie case TMF_SUCCESS: 2549843c0a8aSMike Christie break; 2550843c0a8aSMike Christie case TMF_TIMEDOUT: 2551659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2552df4da5cdSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 2553843c0a8aSMike Christie goto done; 2554843c0a8aSMike Christie default: 2555ec29d0acSMike Christie session->tmf_state = TMF_INITIAL; 2556843c0a8aSMike Christie goto unlock; 2557843c0a8aSMike Christie } 2558843c0a8aSMike Christie 2559843c0a8aSMike Christie rc = SUCCESS; 2560659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2561843c0a8aSMike Christie 2562843c0a8aSMike Christie iscsi_suspend_tx(conn); 2563913e5bf4SMike Christie 2564659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 25655d12c05eSMike Christie memset(hdr, 0, sizeof(*hdr)); 25663bbaaad9SMike Christie fail_scsi_tasks(conn, sc->device->lun, DID_ERROR); 2567ec29d0acSMike Christie session->tmf_state = TMF_INITIAL; 2568659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2569843c0a8aSMike Christie 2570843c0a8aSMike Christie iscsi_start_tx(conn); 2571843c0a8aSMike Christie goto done; 2572843c0a8aSMike Christie 2573843c0a8aSMike Christie unlock: 2574659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2575843c0a8aSMike Christie done: 2576bd2199d4SErez Zilber ISCSI_DBG_EH(session, "dev reset result = %s\n", 2577843c0a8aSMike Christie rc == SUCCESS ? "SUCCESS" : "FAILED"); 2578843c0a8aSMike Christie mutex_unlock(&session->eh_mutex); 2579843c0a8aSMike Christie return rc; 2580843c0a8aSMike Christie } 2581843c0a8aSMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_device_reset); 2582843c0a8aSMike Christie 25833fe5ae8bSMike Christie void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session) 25843fe5ae8bSMike Christie { 25853fe5ae8bSMike Christie struct iscsi_session *session = cls_session->dd_data; 25863fe5ae8bSMike Christie 2587659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 25883fe5ae8bSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) { 25893fe5ae8bSMike Christie session->state = ISCSI_STATE_RECOVERY_FAILED; 2590ec29d0acSMike Christie wake_up(&session->ehwait); 25913fe5ae8bSMike Christie } 2592659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 25933fe5ae8bSMike Christie } 25943fe5ae8bSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout); 25953fe5ae8bSMike Christie 25963fe5ae8bSMike Christie /** 25973fe5ae8bSMike Christie * iscsi_eh_session_reset - drop session and attempt relogin 25983fe5ae8bSMike Christie * @sc: scsi command 25993fe5ae8bSMike Christie * 26003fe5ae8bSMike Christie * This function will wait for a relogin, session termination from 26013fe5ae8bSMike Christie * userspace, or a recovery/replacement timeout. 26023fe5ae8bSMike Christie */ 2603309ce156SJayamohan Kallickal int iscsi_eh_session_reset(struct scsi_cmnd *sc) 26043fe5ae8bSMike Christie { 26053fe5ae8bSMike Christie struct iscsi_cls_session *cls_session; 26063fe5ae8bSMike Christie struct iscsi_session *session; 26073fe5ae8bSMike Christie struct iscsi_conn *conn; 26083fe5ae8bSMike Christie 26093fe5ae8bSMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 26103fe5ae8bSMike Christie session = cls_session->dd_data; 26113fe5ae8bSMike Christie 26123fe5ae8bSMike Christie mutex_lock(&session->eh_mutex); 2613659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 26143fe5ae8bSMike Christie if (session->state == ISCSI_STATE_TERMINATE) { 26153fe5ae8bSMike Christie failed: 26163fe5ae8bSMike Christie ISCSI_DBG_EH(session, 26173fe5ae8bSMike Christie "failing session reset: Could not log back into " 26185db6dd14SFred Herard "%s [age %d]\n", session->targetname, 26195db6dd14SFred Herard session->age); 2620659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 26213fe5ae8bSMike Christie mutex_unlock(&session->eh_mutex); 26223fe5ae8bSMike Christie return FAILED; 26233fe5ae8bSMike Christie } 26243fe5ae8bSMike Christie 2625fda290c5SMike Christie conn = session->leadconn; 2626fda290c5SMike Christie iscsi_get_conn(conn->cls_conn); 2627fda290c5SMike Christie 2628659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 26293fe5ae8bSMike Christie mutex_unlock(&session->eh_mutex); 2630fda290c5SMike Christie 2631df4da5cdSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 2632fda290c5SMike Christie iscsi_put_conn(conn->cls_conn); 26333fe5ae8bSMike Christie 26343fe5ae8bSMike Christie ISCSI_DBG_EH(session, "wait for relogin\n"); 2635ec29d0acSMike Christie wait_event_interruptible(session->ehwait, 26363fe5ae8bSMike Christie session->state == ISCSI_STATE_TERMINATE || 26373fe5ae8bSMike Christie session->state == ISCSI_STATE_LOGGED_IN || 26383fe5ae8bSMike Christie session->state == ISCSI_STATE_RECOVERY_FAILED); 26393fe5ae8bSMike Christie if (signal_pending(current)) 26403fe5ae8bSMike Christie flush_signals(current); 26413fe5ae8bSMike Christie 26423fe5ae8bSMike Christie mutex_lock(&session->eh_mutex); 2643659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 26443fe5ae8bSMike Christie if (session->state == ISCSI_STATE_LOGGED_IN) { 26453fe5ae8bSMike Christie ISCSI_DBG_EH(session, 26463fe5ae8bSMike Christie "session reset succeeded for %s,%s\n", 26473fe5ae8bSMike Christie session->targetname, conn->persistent_address); 26483fe5ae8bSMike Christie } else 26493fe5ae8bSMike Christie goto failed; 2650659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 26513fe5ae8bSMike Christie mutex_unlock(&session->eh_mutex); 26523fe5ae8bSMike Christie return SUCCESS; 26533fe5ae8bSMike Christie } 2654309ce156SJayamohan Kallickal EXPORT_SYMBOL_GPL(iscsi_eh_session_reset); 26553fe5ae8bSMike Christie 26563fe5ae8bSMike Christie static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr) 26573fe5ae8bSMike Christie { 26583fe5ae8bSMike Christie memset(hdr, 0, sizeof(*hdr)); 26593fe5ae8bSMike Christie hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 26603fe5ae8bSMike Christie hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK; 26613fe5ae8bSMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 26623fe5ae8bSMike Christie hdr->rtt = RESERVED_ITT; 26633fe5ae8bSMike Christie } 26643fe5ae8bSMike Christie 26653fe5ae8bSMike Christie /** 26663fe5ae8bSMike Christie * iscsi_eh_target_reset - reset target 26673fe5ae8bSMike Christie * @sc: scsi command 26683fe5ae8bSMike Christie * 2669309ce156SJayamohan Kallickal * This will attempt to send a warm target reset. 26703fe5ae8bSMike Christie */ 26713907adf6SBart Van Assche static int iscsi_eh_target_reset(struct scsi_cmnd *sc) 26723fe5ae8bSMike Christie { 26733fe5ae8bSMike Christie struct iscsi_cls_session *cls_session; 26743fe5ae8bSMike Christie struct iscsi_session *session; 26753fe5ae8bSMike Christie struct iscsi_conn *conn; 26763fe5ae8bSMike Christie struct iscsi_tm *hdr; 26773fe5ae8bSMike Christie int rc = FAILED; 26783fe5ae8bSMike Christie 26793fe5ae8bSMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 26803fe5ae8bSMike Christie session = cls_session->dd_data; 26813fe5ae8bSMike Christie 26823fe5ae8bSMike Christie ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc, 26833fe5ae8bSMike Christie session->targetname); 26843fe5ae8bSMike Christie 26853fe5ae8bSMike Christie mutex_lock(&session->eh_mutex); 2686659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 26873fe5ae8bSMike Christie /* 26883fe5ae8bSMike Christie * Just check if we are not logged in. We cannot check for 26893fe5ae8bSMike Christie * the phase because the reset could come from a ioctl. 26903fe5ae8bSMike Christie */ 26913fe5ae8bSMike Christie if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) 26923fe5ae8bSMike Christie goto unlock; 26933fe5ae8bSMike Christie conn = session->leadconn; 26943fe5ae8bSMike Christie 26953fe5ae8bSMike Christie /* only have one tmf outstanding at a time */ 2696ec29d0acSMike Christie if (session->tmf_state != TMF_INITIAL) 26973fe5ae8bSMike Christie goto unlock; 2698ec29d0acSMike Christie session->tmf_state = TMF_QUEUED; 26993fe5ae8bSMike Christie 2700ec29d0acSMike Christie hdr = &session->tmhdr; 27013fe5ae8bSMike Christie iscsi_prep_tgt_reset_pdu(sc, hdr); 27023fe5ae8bSMike Christie 27033fe5ae8bSMike Christie if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age, 27043fe5ae8bSMike Christie session->tgt_reset_timeout)) { 27053fe5ae8bSMike Christie rc = FAILED; 27063fe5ae8bSMike Christie goto unlock; 27073fe5ae8bSMike Christie } 27083fe5ae8bSMike Christie 2709ec29d0acSMike Christie switch (session->tmf_state) { 27103fe5ae8bSMike Christie case TMF_SUCCESS: 27113fe5ae8bSMike Christie break; 27123fe5ae8bSMike Christie case TMF_TIMEDOUT: 2713659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2714df4da5cdSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 27153fe5ae8bSMike Christie goto done; 27163fe5ae8bSMike Christie default: 2717ec29d0acSMike Christie session->tmf_state = TMF_INITIAL; 27183fe5ae8bSMike Christie goto unlock; 27193fe5ae8bSMike Christie } 27203fe5ae8bSMike Christie 27213fe5ae8bSMike Christie rc = SUCCESS; 2722659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 27233fe5ae8bSMike Christie 27243fe5ae8bSMike Christie iscsi_suspend_tx(conn); 27253fe5ae8bSMike Christie 2726659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 27273fe5ae8bSMike Christie memset(hdr, 0, sizeof(*hdr)); 27283fe5ae8bSMike Christie fail_scsi_tasks(conn, -1, DID_ERROR); 2729ec29d0acSMike Christie session->tmf_state = TMF_INITIAL; 2730659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 27313fe5ae8bSMike Christie 27323fe5ae8bSMike Christie iscsi_start_tx(conn); 27333fe5ae8bSMike Christie goto done; 27343fe5ae8bSMike Christie 27353fe5ae8bSMike Christie unlock: 2736659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 27373fe5ae8bSMike Christie done: 27383fe5ae8bSMike Christie ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname, 27393fe5ae8bSMike Christie rc == SUCCESS ? "SUCCESS" : "FAILED"); 27403fe5ae8bSMike Christie mutex_unlock(&session->eh_mutex); 2741309ce156SJayamohan Kallickal return rc; 2742309ce156SJayamohan Kallickal } 27433fe5ae8bSMike Christie 2744309ce156SJayamohan Kallickal /** 2745309ce156SJayamohan Kallickal * iscsi_eh_recover_target - reset target and possibly the session 2746309ce156SJayamohan Kallickal * @sc: scsi command 2747309ce156SJayamohan Kallickal * 2748309ce156SJayamohan Kallickal * This will attempt to send a warm target reset. If that fails, 2749309ce156SJayamohan Kallickal * we will escalate to ERL0 session recovery. 2750309ce156SJayamohan Kallickal */ 2751309ce156SJayamohan Kallickal int iscsi_eh_recover_target(struct scsi_cmnd *sc) 2752309ce156SJayamohan Kallickal { 2753309ce156SJayamohan Kallickal int rc; 2754309ce156SJayamohan Kallickal 2755309ce156SJayamohan Kallickal rc = iscsi_eh_target_reset(sc); 27563fe5ae8bSMike Christie if (rc == FAILED) 27573fe5ae8bSMike Christie rc = iscsi_eh_session_reset(sc); 27583fe5ae8bSMike Christie return rc; 27593fe5ae8bSMike Christie } 2760309ce156SJayamohan Kallickal EXPORT_SYMBOL_GPL(iscsi_eh_recover_target); 27613fe5ae8bSMike Christie 27626320377fSOlaf Kirch /* 27636320377fSOlaf Kirch * Pre-allocate a pool of @max items of @item_size. By default, the pool 27646320377fSOlaf Kirch * should be accessed via kfifo_{get,put} on q->queue. 27656320377fSOlaf Kirch * Optionally, the caller can obtain the array of object pointers 27666320377fSOlaf Kirch * by passing in a non-NULL @items pointer 27676320377fSOlaf Kirch */ 27687996a778SMike Christie int 27696320377fSOlaf Kirch iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size) 27707996a778SMike Christie { 27716320377fSOlaf Kirch int i, num_arrays = 1; 27727996a778SMike Christie 27736320377fSOlaf Kirch memset(q, 0, sizeof(*q)); 27747996a778SMike Christie 27757996a778SMike Christie q->max = max; 27766320377fSOlaf Kirch 27776320377fSOlaf Kirch /* If the user passed an items pointer, he wants a copy of 27786320377fSOlaf Kirch * the array. */ 27796320377fSOlaf Kirch if (items) 27806320377fSOlaf Kirch num_arrays++; 2781778e1cddSKees Cook q->pool = kvcalloc(num_arrays * max, sizeof(void *), GFP_KERNEL); 27826320377fSOlaf Kirch if (q->pool == NULL) 2783f474a37bSJean Delvare return -ENOMEM; 27847996a778SMike Christie 2785c1e13f25SStefani Seibold kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*)); 27867996a778SMike Christie 27877996a778SMike Christie for (i = 0; i < max; i++) { 27886320377fSOlaf Kirch q->pool[i] = kzalloc(item_size, GFP_KERNEL); 27897996a778SMike Christie if (q->pool[i] == NULL) { 27906320377fSOlaf Kirch q->max = i; 27916320377fSOlaf Kirch goto enomem; 27927996a778SMike Christie } 27937acd72ebSStefani Seibold kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*)); 27947996a778SMike Christie } 27956320377fSOlaf Kirch 27966320377fSOlaf Kirch if (items) { 27976320377fSOlaf Kirch *items = q->pool + max; 27986320377fSOlaf Kirch memcpy(*items, q->pool, max * sizeof(void *)); 27996320377fSOlaf Kirch } 28006320377fSOlaf Kirch 28017996a778SMike Christie return 0; 28026320377fSOlaf Kirch 28036320377fSOlaf Kirch enomem: 28046320377fSOlaf Kirch iscsi_pool_free(q); 28056320377fSOlaf Kirch return -ENOMEM; 28067996a778SMike Christie } 28077996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_init); 28087996a778SMike Christie 28096320377fSOlaf Kirch void iscsi_pool_free(struct iscsi_pool *q) 28107996a778SMike Christie { 28117996a778SMike Christie int i; 28127996a778SMike Christie 28137996a778SMike Christie for (i = 0; i < q->max; i++) 28146320377fSOlaf Kirch kfree(q->pool[i]); 2815bfcc62edSKyle Fortin kvfree(q->pool); 28167996a778SMike Christie } 28177996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_free); 28187996a778SMike Christie 2819b4046922SMike Christie int iscsi_host_get_max_scsi_cmds(struct Scsi_Host *shost, 2820b4046922SMike Christie uint16_t requested_cmds_max) 2821b4046922SMike Christie { 2822b4046922SMike Christie int scsi_cmds, total_cmds = requested_cmds_max; 2823b4046922SMike Christie 2824b4046922SMike Christie check: 2825b4046922SMike Christie if (!total_cmds) 2826b4046922SMike Christie total_cmds = ISCSI_DEF_XMIT_CMDS_MAX; 2827b4046922SMike Christie /* 2828b4046922SMike Christie * The iscsi layer needs some tasks for nop handling and tmfs, 2829b4046922SMike Christie * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX 2830b4046922SMike Christie * + 1 command for scsi IO. 2831b4046922SMike Christie */ 2832b4046922SMike Christie if (total_cmds < ISCSI_TOTAL_CMDS_MIN) { 2833b4046922SMike Christie printk(KERN_ERR "iscsi: invalid max cmds of %d. Must be a power of two that is at least %d.\n", 2834b4046922SMike Christie total_cmds, ISCSI_TOTAL_CMDS_MIN); 2835b4046922SMike Christie return -EINVAL; 2836b4046922SMike Christie } 2837b4046922SMike Christie 2838b4046922SMike Christie if (total_cmds > ISCSI_TOTAL_CMDS_MAX) { 2839b4046922SMike Christie printk(KERN_INFO "iscsi: invalid max cmds of %d. Must be a power of 2 less than or equal to %d. Using %d.\n", 2840b4046922SMike Christie requested_cmds_max, ISCSI_TOTAL_CMDS_MAX, 2841b4046922SMike Christie ISCSI_TOTAL_CMDS_MAX); 2842b4046922SMike Christie total_cmds = ISCSI_TOTAL_CMDS_MAX; 2843b4046922SMike Christie } 2844b4046922SMike Christie 2845b4046922SMike Christie if (!is_power_of_2(total_cmds)) { 2846b4046922SMike Christie total_cmds = rounddown_pow_of_two(total_cmds); 2847b4046922SMike Christie if (total_cmds < ISCSI_TOTAL_CMDS_MIN) { 2848b4046922SMike Christie printk(KERN_ERR "iscsi: invalid max cmds of %d. Must be a power of 2 greater than %d.\n", requested_cmds_max, ISCSI_TOTAL_CMDS_MIN); 2849b4046922SMike Christie return -EINVAL; 2850b4046922SMike Christie } 2851b4046922SMike Christie 2852b4046922SMike Christie printk(KERN_INFO "iscsi: invalid max cmds %d. Must be a power of 2. Rounding max cmds down to %d.\n", 2853b4046922SMike Christie requested_cmds_max, total_cmds); 2854b4046922SMike Christie } 2855b4046922SMike Christie 2856b4046922SMike Christie scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX; 2857b4046922SMike Christie if (shost->can_queue && scsi_cmds > shost->can_queue) { 2858b4046922SMike Christie total_cmds = shost->can_queue; 2859b4046922SMike Christie 2860b4046922SMike Christie printk(KERN_INFO "iscsi: requested max cmds %u is higher than driver limit. Using driver limit %u\n", 2861b4046922SMike Christie requested_cmds_max, shost->can_queue); 2862b4046922SMike Christie goto check; 2863b4046922SMike Christie } 2864b4046922SMike Christie 2865b4046922SMike Christie return scsi_cmds; 2866b4046922SMike Christie } 2867b4046922SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_get_max_scsi_cmds); 2868b4046922SMike Christie 2869a4804cd6SMike Christie /** 2870a4804cd6SMike Christie * iscsi_host_add - add host to system 2871a4804cd6SMike Christie * @shost: scsi host 2872a4804cd6SMike Christie * @pdev: parent device 2873a4804cd6SMike Christie * 2874a4804cd6SMike Christie * This should be called by partial offload and software iscsi drivers 2875a4804cd6SMike Christie * to add a host to the system. 2876a4804cd6SMike Christie */ 2877a4804cd6SMike Christie int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev) 28787996a778SMike Christie { 28798e9a20ceSMike Christie if (!shost->can_queue) 28808e9a20ceSMike Christie shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX; 28818e9a20ceSMike Christie 28824d108350SMike Christie if (!shost->cmd_per_lun) 28834d108350SMike Christie shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN; 28844d108350SMike Christie 2885a4804cd6SMike Christie return scsi_add_host(shost, pdev); 2886a4804cd6SMike Christie } 2887a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_add); 2888a4804cd6SMike Christie 2889a4804cd6SMike Christie /** 2890a4804cd6SMike Christie * iscsi_host_alloc - allocate a host and driver data 2891a4804cd6SMike Christie * @sht: scsi host template 2892a4804cd6SMike Christie * @dd_data_size: driver host data size 289332ae763eSMike Christie * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue 2894a4804cd6SMike Christie * 2895a4804cd6SMike Christie * This should be called by partial offload and software iscsi drivers. 2896a4804cd6SMike Christie * To access the driver specific memory use the iscsi_host_priv() macro. 2897a4804cd6SMike Christie */ 2898a4804cd6SMike Christie struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht, 28994d108350SMike Christie int dd_data_size, bool xmit_can_sleep) 2900a4804cd6SMike Christie { 2901a4804cd6SMike Christie struct Scsi_Host *shost; 2902e5bd7b54SMike Christie struct iscsi_host *ihost; 2903a4804cd6SMike Christie 2904a4804cd6SMike Christie shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size); 2905a4804cd6SMike Christie if (!shost) 2906a4804cd6SMike Christie return NULL; 2907e5bd7b54SMike Christie ihost = shost_priv(shost); 290832ae763eSMike Christie 290932ae763eSMike Christie if (xmit_can_sleep) { 291069af1c95SMike Christie ihost->workq = alloc_workqueue("iscsi_q_%d", 29113ce41966SBob Liu WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND, 291269af1c95SMike Christie 1, shost->host_no); 291332ae763eSMike Christie if (!ihost->workq) 291432ae763eSMike Christie goto free_host; 291532ae763eSMike Christie } 291632ae763eSMike Christie 2917e5bd7b54SMike Christie spin_lock_init(&ihost->lock); 2918e5bd7b54SMike Christie ihost->state = ISCSI_HOST_SETUP; 2919e5bd7b54SMike Christie ihost->num_sessions = 0; 2920e5bd7b54SMike Christie init_waitqueue_head(&ihost->session_removal_wq); 2921a4804cd6SMike Christie return shost; 292232ae763eSMike Christie 292332ae763eSMike Christie free_host: 292432ae763eSMike Christie scsi_host_put(shost); 292532ae763eSMike Christie return NULL; 292675613521SMike Christie } 2927a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_alloc); 292875613521SMike Christie 2929e5bd7b54SMike Christie static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session) 2930e5bd7b54SMike Christie { 293140a06e75SMike Christie iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST); 2932e5bd7b54SMike Christie } 2933e5bd7b54SMike Christie 2934a4804cd6SMike Christie /** 2935a4804cd6SMike Christie * iscsi_host_remove - remove host and sessions 2936a4804cd6SMike Christie * @shost: scsi host 293731500e90SMike Christie * @is_shutdown: true if called from a driver shutdown callout 2938a4804cd6SMike Christie * 2939e5bd7b54SMike Christie * If there are any sessions left, this will initiate the removal and wait 2940e5bd7b54SMike Christie * for the completion. 2941a4804cd6SMike Christie */ 294231500e90SMike Christie void iscsi_host_remove(struct Scsi_Host *shost, bool is_shutdown) 2943a4804cd6SMike Christie { 2944e5bd7b54SMike Christie struct iscsi_host *ihost = shost_priv(shost); 2945e5bd7b54SMike Christie unsigned long flags; 2946e5bd7b54SMike Christie 2947e5bd7b54SMike Christie spin_lock_irqsave(&ihost->lock, flags); 2948e5bd7b54SMike Christie ihost->state = ISCSI_HOST_REMOVED; 2949e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 2950e5bd7b54SMike Christie 295131500e90SMike Christie if (!is_shutdown) 2952e5bd7b54SMike Christie iscsi_host_for_each_session(shost, iscsi_notify_host_removed); 295331500e90SMike Christie else 295431500e90SMike Christie iscsi_host_for_each_session(shost, iscsi_force_destroy_session); 295531500e90SMike Christie 2956e5bd7b54SMike Christie wait_event_interruptible(ihost->session_removal_wq, 2957e5bd7b54SMike Christie ihost->num_sessions == 0); 2958e5bd7b54SMike Christie if (signal_pending(current)) 2959e5bd7b54SMike Christie flush_signals(current); 2960e5bd7b54SMike Christie 2961a4804cd6SMike Christie scsi_remove_host(shost); 2962a4804cd6SMike Christie } 2963a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_remove); 2964a4804cd6SMike Christie 2965a4804cd6SMike Christie void iscsi_host_free(struct Scsi_Host *shost) 296675613521SMike Christie { 296775613521SMike Christie struct iscsi_host *ihost = shost_priv(shost); 296875613521SMike Christie 2969c435f0a9SMike Christie if (ihost->workq) 2970c435f0a9SMike Christie destroy_workqueue(ihost->workq); 2971c435f0a9SMike Christie 297275613521SMike Christie kfree(ihost->netdev); 297375613521SMike Christie kfree(ihost->hwaddress); 297475613521SMike Christie kfree(ihost->initiatorname); 2975a4804cd6SMike Christie scsi_host_put(shost); 297675613521SMike Christie } 2977a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_free); 297875613521SMike Christie 2979e5bd7b54SMike Christie static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost) 2980e5bd7b54SMike Christie { 2981e5bd7b54SMike Christie struct iscsi_host *ihost = shost_priv(shost); 2982e5bd7b54SMike Christie unsigned long flags; 2983e5bd7b54SMike Christie 2984e5bd7b54SMike Christie shost = scsi_host_get(shost); 2985e5bd7b54SMike Christie if (!shost) { 2986e5bd7b54SMike Christie printk(KERN_ERR "Invalid state. Cannot notify host removal " 2987e5bd7b54SMike Christie "of session teardown event because host already " 2988e5bd7b54SMike Christie "removed.\n"); 2989e5bd7b54SMike Christie return; 2990e5bd7b54SMike Christie } 2991e5bd7b54SMike Christie 2992e5bd7b54SMike Christie spin_lock_irqsave(&ihost->lock, flags); 2993e5bd7b54SMike Christie ihost->num_sessions--; 2994e5bd7b54SMike Christie if (ihost->num_sessions == 0) 2995e5bd7b54SMike Christie wake_up(&ihost->session_removal_wq); 2996e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 2997e5bd7b54SMike Christie scsi_host_put(shost); 2998e5bd7b54SMike Christie } 2999e5bd7b54SMike Christie 300075613521SMike Christie /** 300175613521SMike Christie * iscsi_session_setup - create iscsi cls session and host and session 300275613521SMike Christie * @iscsit: iscsi transport template 300375613521SMike Christie * @shost: scsi host 300475613521SMike Christie * @cmds_max: session can queue 3005ccd4a430SRandy Dunlap * @dd_size: private driver data size, added to session allocation size 30069c19a7d0SMike Christie * @cmd_task_size: LLD task private data size 300775613521SMike Christie * @initial_cmdsn: initial CmdSN 3008ccd4a430SRandy Dunlap * @id: target ID to add to this session 300975613521SMike Christie * 301075613521SMike Christie * This can be used by software iscsi_transports that allocate 301175613521SMike Christie * a session per scsi host. 30123cf7b233SMike Christie * 30133cf7b233SMike Christie * Callers should set cmds_max to the largest total numer (mgmt + scsi) of 30143cf7b233SMike Christie * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks 30153cf7b233SMike Christie * for nop handling and login/logout requests. 301675613521SMike Christie */ 301775613521SMike Christie struct iscsi_cls_session * 301875613521SMike Christie iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost, 3019b8b9e1b8SJayamohan Kallickal uint16_t cmds_max, int dd_size, int cmd_task_size, 30207970634bSMike Christie uint32_t initial_cmdsn, unsigned int id) 302175613521SMike Christie { 3022e5bd7b54SMike Christie struct iscsi_host *ihost = shost_priv(shost); 302375613521SMike Christie struct iscsi_session *session; 302475613521SMike Christie struct iscsi_cls_session *cls_session; 3025b4046922SMike Christie int cmd_i, scsi_cmds; 3026e5bd7b54SMike Christie unsigned long flags; 3027e5bd7b54SMike Christie 3028e5bd7b54SMike Christie spin_lock_irqsave(&ihost->lock, flags); 3029e5bd7b54SMike Christie if (ihost->state == ISCSI_HOST_REMOVED) { 3030e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 3031e5bd7b54SMike Christie return NULL; 3032e5bd7b54SMike Christie } 3033e5bd7b54SMike Christie ihost->num_sessions++; 3034e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 30358e9a20ceSMike Christie 3036b4046922SMike Christie scsi_cmds = iscsi_host_get_max_scsi_cmds(shost, cmds_max); 3037b4046922SMike Christie if (scsi_cmds < 0) 3038e5bd7b54SMike Christie goto dec_session_count; 30391548271eSMike Christie 30405d91e209SMike Christie cls_session = iscsi_alloc_session(shost, iscsit, 3041b8b9e1b8SJayamohan Kallickal sizeof(struct iscsi_session) + 3042b8b9e1b8SJayamohan Kallickal dd_size); 304375613521SMike Christie if (!cls_session) 3044e5bd7b54SMike Christie goto dec_session_count; 304575613521SMike Christie session = cls_session->dd_data; 304675613521SMike Christie session->cls_session = cls_session; 30477996a778SMike Christie session->host = shost; 30487996a778SMike Christie session->state = ISCSI_STATE_FREE; 3049f6d5180cSMike Christie session->fast_abort = 1; 30503fe5ae8bSMike Christie session->tgt_reset_timeout = 30; 30514cd49ea1SMike Christie session->lu_reset_timeout = 15; 30524cd49ea1SMike Christie session->abort_timeout = 10; 30533cf7b233SMike Christie session->scsi_cmds_max = scsi_cmds; 3054b4046922SMike Christie session->cmds_max = scsi_cmds + ISCSI_MGMT_CMDS_MAX; 3055e0726407SMike Christie session->queued_cmdsn = session->cmdsn = initial_cmdsn; 30567996a778SMike Christie session->exp_cmdsn = initial_cmdsn + 1; 30577996a778SMike Christie session->max_cmdsn = initial_cmdsn + 1; 30587996a778SMike Christie session->max_r2t = 1; 30597996a778SMike Christie session->tt = iscsit; 3060b8b9e1b8SJayamohan Kallickal session->dd_data = cls_session->dd_data + sizeof(*session); 3061659743b0SShlomo Pongratz 3062ec29d0acSMike Christie session->tmf_state = TMF_INITIAL; 3063ec29d0acSMike Christie timer_setup(&session->tmf_timer, iscsi_tmf_timedout, 0); 30646724add1SMike Christie mutex_init(&session->eh_mutex); 3065e018f03dSDing Hui init_waitqueue_head(&session->ehwait); 3066ec29d0acSMike Christie 3067659743b0SShlomo Pongratz spin_lock_init(&session->frwd_lock); 3068659743b0SShlomo Pongratz spin_lock_init(&session->back_lock); 30697996a778SMike Christie 30707996a778SMike Christie /* initialize SCSI PDU commands pool */ 30717996a778SMike Christie if (iscsi_pool_init(&session->cmdpool, session->cmds_max, 30727996a778SMike Christie (void***)&session->cmds, 30739c19a7d0SMike Christie cmd_task_size + sizeof(struct iscsi_task))) 30747996a778SMike Christie goto cmdpool_alloc_fail; 30757996a778SMike Christie 30767996a778SMike Christie /* pre-format cmds pool with ITT */ 30777996a778SMike Christie for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { 30789c19a7d0SMike Christie struct iscsi_task *task = session->cmds[cmd_i]; 30797996a778SMike Christie 30809c19a7d0SMike Christie if (cmd_task_size) 30819c19a7d0SMike Christie task->dd_data = &task[1]; 30829c19a7d0SMike Christie task->itt = cmd_i; 30833bbaaad9SMike Christie task->state = ISCSI_TASK_FREE; 30849c19a7d0SMike Christie INIT_LIST_HEAD(&task->running); 30857996a778SMike Christie } 30867996a778SMike Christie 3087f53a88daSMike Christie if (!try_module_get(iscsit->owner)) 308875613521SMike Christie goto module_get_fail; 308975613521SMike Christie 30907970634bSMike Christie if (iscsi_add_session(cls_session, id)) 3091f53a88daSMike Christie goto cls_session_fail; 3092e5bd7b54SMike Christie 30937996a778SMike Christie return cls_session; 30947996a778SMike Christie 30957996a778SMike Christie cls_session_fail: 309675613521SMike Christie module_put(iscsit->owner); 309775613521SMike Christie module_get_fail: 30986320377fSOlaf Kirch iscsi_pool_free(&session->cmdpool); 30997996a778SMike Christie cmdpool_alloc_fail: 310075613521SMike Christie iscsi_free_session(cls_session); 3101e5bd7b54SMike Christie dec_session_count: 3102e5bd7b54SMike Christie iscsi_host_dec_session_cnt(shost); 31037996a778SMike Christie return NULL; 31047996a778SMike Christie } 31057996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_setup); 31067996a778SMike Christie 31077996a778SMike Christie /** 31087996a778SMike Christie * iscsi_session_teardown - destroy session, host, and cls_session 310975613521SMike Christie * @cls_session: iscsi session 311075613521SMike Christie */ 31117996a778SMike Christie void iscsi_session_teardown(struct iscsi_cls_session *cls_session) 31127996a778SMike Christie { 311375613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 311463f75cc8SMike Christie struct module *owner = cls_session->transport->owner; 3115e5bd7b54SMike Christie struct Scsi_Host *shost = session->host; 31167996a778SMike Christie 31179e10b512SKhazhismel Kumykov iscsi_remove_session(cls_session); 31189e10b512SKhazhismel Kumykov 3119a1f3486bSMike Christie iscsi_pool_free(&session->cmdpool); 3120b2c64167SMike Christie kfree(session->password); 3121b2c64167SMike Christie kfree(session->password_in); 3122b2c64167SMike Christie kfree(session->username); 3123b2c64167SMike Christie kfree(session->username_in); 3124f3ff0c36SMike Christie kfree(session->targetname); 31253c5c4801SVikas Chaudhary kfree(session->targetalias); 312688dfd340SMike Christie kfree(session->initiatorname); 31273b9373e9SEddie Wai kfree(session->boot_root); 31283b9373e9SEddie Wai kfree(session->boot_nic); 31293b9373e9SEddie Wai kfree(session->boot_target); 313088dfd340SMike Christie kfree(session->ifacename); 3131f8525eb4SAdheer Chandravanshi kfree(session->portal_type); 3132f8525eb4SAdheer Chandravanshi kfree(session->discovery_parent_type); 3133f3ff0c36SMike Christie 31349e10b512SKhazhismel Kumykov iscsi_free_session(cls_session); 31359e10b512SKhazhismel Kumykov 3136e5bd7b54SMike Christie iscsi_host_dec_session_cnt(shost); 313763f75cc8SMike Christie module_put(owner); 31387996a778SMike Christie } 31397996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_teardown); 31407996a778SMike Christie 31417996a778SMike Christie /** 31427996a778SMike Christie * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn 31437996a778SMike Christie * @cls_session: iscsi_cls_session 31445d91e209SMike Christie * @dd_size: private driver data size 31457996a778SMike Christie * @conn_idx: cid 31465d91e209SMike Christie */ 31477996a778SMike Christie struct iscsi_cls_conn * 31485d91e209SMike Christie iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size, 31495d91e209SMike Christie uint32_t conn_idx) 31507996a778SMike Christie { 315175613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 31527996a778SMike Christie struct iscsi_conn *conn; 31537996a778SMike Christie struct iscsi_cls_conn *cls_conn; 3154d36ab6f3SMike Christie char *data; 31557dae459fSWenchao Hao int err; 31567996a778SMike Christie 31577dae459fSWenchao Hao cls_conn = iscsi_alloc_conn(cls_session, sizeof(*conn) + dd_size, 31585d91e209SMike Christie conn_idx); 31597996a778SMike Christie if (!cls_conn) 31607996a778SMike Christie return NULL; 31617996a778SMike Christie conn = cls_conn->dd_data; 31627996a778SMike Christie 31635d91e209SMike Christie conn->dd_data = cls_conn->dd_data + sizeof(*conn); 31647996a778SMike Christie conn->session = session; 31657996a778SMike Christie conn->cls_conn = cls_conn; 31667996a778SMike Christie conn->c_stage = ISCSI_CONN_INITIAL_STAGE; 31677996a778SMike Christie conn->id = conn_idx; 31687996a778SMike Christie conn->exp_statsn = 0; 3169f6d5180cSMike Christie 31702c4b9637SKees Cook timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0); 3171f6d5180cSMike Christie 3172843c0a8aSMike Christie INIT_LIST_HEAD(&conn->mgmtqueue); 31733bbaaad9SMike Christie INIT_LIST_HEAD(&conn->cmdqueue); 3174843c0a8aSMike Christie INIT_LIST_HEAD(&conn->requeue); 3175c4028958SDavid Howells INIT_WORK(&conn->xmitwork, iscsi_xmitworker); 31767996a778SMike Christie 31779c19a7d0SMike Christie /* allocate login_task used for the login/text sequences */ 3178659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 31797acd72ebSStefani Seibold if (!kfifo_out(&session->cmdpool.queue, 31809c19a7d0SMike Christie (void*)&conn->login_task, 31817996a778SMike Christie sizeof(void*))) { 3182659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 31839c19a7d0SMike Christie goto login_task_alloc_fail; 31847996a778SMike Christie } 3185659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 31867996a778SMike Christie 3187cfeb2cf9SMike Christie data = (char *) __get_free_pages(GFP_KERNEL, 3188cfeb2cf9SMike Christie get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); 3189d36ab6f3SMike Christie if (!data) 31909c19a7d0SMike Christie goto login_task_data_alloc_fail; 31919c19a7d0SMike Christie conn->login_task->data = conn->data = data; 3192d36ab6f3SMike Christie 31937dae459fSWenchao Hao err = iscsi_add_conn(cls_conn); 31947dae459fSWenchao Hao if (err) 31957dae459fSWenchao Hao goto login_task_add_dev_fail; 31967dae459fSWenchao Hao 31977996a778SMike Christie return cls_conn; 31987996a778SMike Christie 31997dae459fSWenchao Hao login_task_add_dev_fail: 32007dae459fSWenchao Hao free_pages((unsigned long) conn->data, 32017dae459fSWenchao Hao get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); 32027dae459fSWenchao Hao 32039c19a7d0SMike Christie login_task_data_alloc_fail: 32047acd72ebSStefani Seibold kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task, 3205d36ab6f3SMike Christie sizeof(void*)); 32069c19a7d0SMike Christie login_task_alloc_fail: 32077dae459fSWenchao Hao iscsi_put_conn(cls_conn); 32087996a778SMike Christie return NULL; 32097996a778SMike Christie } 32107996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_setup); 32117996a778SMike Christie 32127996a778SMike Christie /** 32137996a778SMike Christie * iscsi_conn_teardown - teardown iscsi connection 3214ccd4a430SRandy Dunlap * @cls_conn: iscsi class connection 32157996a778SMike Christie * 32167996a778SMike Christie * TODO: we may need to make this into a two step process 32177996a778SMike Christie * like scsi-mls remove + put host 32187996a778SMike Christie */ 32197996a778SMike Christie void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn) 32207996a778SMike Christie { 32217996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 32227996a778SMike Christie struct iscsi_session *session = conn->session; 32238709c323SWenchao Hao 32248709c323SWenchao Hao iscsi_remove_conn(cls_conn); 32257996a778SMike Christie 3226f6d5180cSMike Christie del_timer_sync(&conn->transport_timer); 3227f6d5180cSMike Christie 3228660d0831SJohn Soni Jose mutex_lock(&session->eh_mutex); 3229659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 32307996a778SMike Christie conn->c_stage = ISCSI_CONN_CLEANUP_WAIT; 32317996a778SMike Christie if (session->leadconn == conn) { 32327996a778SMike Christie /* 32337996a778SMike Christie * leading connection? then give up on recovery. 32347996a778SMike Christie */ 32357996a778SMike Christie session->state = ISCSI_STATE_TERMINATE; 3236ec29d0acSMike Christie wake_up(&session->ehwait); 32377996a778SMike Christie } 3238659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 32397996a778SMike Christie 3240779ea120SMike Christie /* flush queued up work because we free the connection below */ 3241843c0a8aSMike Christie iscsi_suspend_tx(conn); 3242779ea120SMike Christie 3243659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 3244cfeb2cf9SMike Christie free_pages((unsigned long) conn->data, 3245cfeb2cf9SMike Christie get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); 32468709c323SWenchao Hao kfree(conn->persistent_address); 32478709c323SWenchao Hao kfree(conn->local_ipaddr); 3248659743b0SShlomo Pongratz /* regular RX path uses back_lock */ 3249659743b0SShlomo Pongratz spin_lock_bh(&session->back_lock); 32507acd72ebSStefani Seibold kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task, 32517996a778SMike Christie sizeof(void*)); 3252659743b0SShlomo Pongratz spin_unlock_bh(&session->back_lock); 3253e0726407SMike Christie if (session->leadconn == conn) 32547996a778SMike Christie session->leadconn = NULL; 3255659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 3256660d0831SJohn Soni Jose mutex_unlock(&session->eh_mutex); 32577996a778SMike Christie 32588709c323SWenchao Hao iscsi_put_conn(cls_conn); 32597996a778SMike Christie } 32607996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_teardown); 32617996a778SMike Christie 32627996a778SMike Christie int iscsi_conn_start(struct iscsi_cls_conn *cls_conn) 32637996a778SMike Christie { 32647996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 32657996a778SMike Christie struct iscsi_session *session = conn->session; 32667996a778SMike Christie 3267ffd0436eSMike Christie if (!session) { 3268322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 3269322d739dSMike Christie "can't start unbound connection\n"); 32707996a778SMike Christie return -EPERM; 32717996a778SMike Christie } 32727996a778SMike Christie 3273db98ccdeSMike Christie if ((session->imm_data_en || !session->initial_r2t_en) && 3274db98ccdeSMike Christie session->first_burst > session->max_burst) { 3275322d739dSMike Christie iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: " 3276ffd0436eSMike Christie "first_burst %d max_burst %d\n", 3277ffd0436eSMike Christie session->first_burst, session->max_burst); 3278ffd0436eSMike Christie return -EINVAL; 3279ffd0436eSMike Christie } 3280ffd0436eSMike Christie 3281f6d5180cSMike Christie if (conn->ping_timeout && !conn->recv_timeout) { 3282322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of " 3283322d739dSMike Christie "zero. Using 5 seconds\n."); 3284f6d5180cSMike Christie conn->recv_timeout = 5; 3285f6d5180cSMike Christie } 3286f6d5180cSMike Christie 3287f6d5180cSMike Christie if (conn->recv_timeout && !conn->ping_timeout) { 3288322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of " 3289322d739dSMike Christie "zero. Using 5 seconds.\n"); 3290f6d5180cSMike Christie conn->ping_timeout = 5; 3291f6d5180cSMike Christie } 3292f6d5180cSMike Christie 3293659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 32947996a778SMike Christie conn->c_stage = ISCSI_CONN_STARTED; 32957996a778SMike Christie session->state = ISCSI_STATE_LOGGED_IN; 3296e0726407SMike Christie session->queued_cmdsn = session->cmdsn; 32977996a778SMike Christie 3298f6d5180cSMike Christie conn->last_recv = jiffies; 3299f6d5180cSMike Christie conn->last_ping = jiffies; 3300f6d5180cSMike Christie if (conn->recv_timeout && conn->ping_timeout) 3301f6d5180cSMike Christie mod_timer(&conn->transport_timer, 3302f6d5180cSMike Christie jiffies + (conn->recv_timeout * HZ)); 3303f6d5180cSMike Christie 33047996a778SMike Christie switch(conn->stop_stage) { 33057996a778SMike Christie case STOP_CONN_RECOVER: 33067996a778SMike Christie /* 33077996a778SMike Christie * unblock eh_abort() if it is blocked. re-try all 33087996a778SMike Christie * commands after successful recovery 33097996a778SMike Christie */ 33107996a778SMike Christie conn->stop_stage = 0; 3311ec29d0acSMike Christie session->tmf_state = TMF_INITIAL; 33127996a778SMike Christie session->age++; 33138b1d0343SMike Christie if (session->age == 16) 33148b1d0343SMike Christie session->age = 0; 33156eabafbeSMike Christie break; 33167996a778SMike Christie case STOP_CONN_TERM: 33177996a778SMike Christie conn->stop_stage = 0; 33187996a778SMike Christie break; 33197996a778SMike Christie default: 33207996a778SMike Christie break; 33217996a778SMike Christie } 3322659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 33237996a778SMike Christie 332475613521SMike Christie iscsi_unblock_session(session->cls_session); 3325ec29d0acSMike Christie wake_up(&session->ehwait); 33267996a778SMike Christie return 0; 33277996a778SMike Christie } 33287996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_start); 33297996a778SMike Christie 33307996a778SMike Christie static void 33313bbaaad9SMike Christie fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn) 33327996a778SMike Christie { 33333bbaaad9SMike Christie struct iscsi_task *task; 3334b3cd5050SMike Christie int i, state; 33357996a778SMike Christie 33363bbaaad9SMike Christie for (i = 0; i < conn->session->cmds_max; i++) { 33373bbaaad9SMike Christie task = conn->session->cmds[i]; 33383bbaaad9SMike Christie if (task->sc) 33393bbaaad9SMike Christie continue; 33403bbaaad9SMike Christie 33413bbaaad9SMike Christie if (task->state == ISCSI_TASK_FREE) 33423bbaaad9SMike Christie continue; 33433bbaaad9SMike Christie 33443bbaaad9SMike Christie ISCSI_DBG_SESSION(conn->session, 33453bbaaad9SMike Christie "failing mgmt itt 0x%x state %d\n", 33463bbaaad9SMike Christie task->itt, task->state); 33475923d64bSMike Christie 33485923d64bSMike Christie spin_lock_bh(&session->back_lock); 33495923d64bSMike Christie if (cleanup_queued_task(task)) { 33505923d64bSMike Christie spin_unlock_bh(&session->back_lock); 33515923d64bSMike Christie continue; 33525923d64bSMike Christie } 33535923d64bSMike Christie 3354b3cd5050SMike Christie state = ISCSI_TASK_ABRT_SESS_RECOV; 3355b3cd5050SMike Christie if (task->state == ISCSI_TASK_PENDING) 3356b3cd5050SMike Christie state = ISCSI_TASK_COMPLETED; 3357b3cd5050SMike Christie iscsi_complete_task(task, state); 3358a656183eSLee Duncan spin_unlock_bh(&session->back_lock); 33597996a778SMike Christie } 33607996a778SMike Christie } 33617996a778SMike Christie 33620dcf8febSMike Christie void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) 33637996a778SMike Christie { 33640dcf8febSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 33650dcf8febSMike Christie struct iscsi_session *session = conn->session; 3366ed2abc7fSMike Christie int old_stop_stage; 3367ed2abc7fSMike Christie 33686724add1SMike Christie mutex_lock(&session->eh_mutex); 3369659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 3370ed2abc7fSMike Christie if (conn->stop_stage == STOP_CONN_TERM) { 3371659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 33726724add1SMike Christie mutex_unlock(&session->eh_mutex); 33736724add1SMike Christie return; 33746724add1SMike Christie } 33756724add1SMike Christie 33766724add1SMike Christie /* 3377ed2abc7fSMike Christie * When this is called for the in_login state, we only want to clean 33789c19a7d0SMike Christie * up the login task and connection. We do not need to block and set 337967a61114SMike Christie * the recovery state again 3380ed2abc7fSMike Christie */ 338167a61114SMike Christie if (flag == STOP_CONN_TERM) 338267a61114SMike Christie session->state = ISCSI_STATE_TERMINATE; 338367a61114SMike Christie else if (conn->stop_stage != STOP_CONN_RECOVER) 338467a61114SMike Christie session->state = ISCSI_STATE_IN_RECOVERY; 33854ae0a6c1SMike Christie 33864ae0a6c1SMike Christie old_stop_stage = conn->stop_stage; 33874ae0a6c1SMike Christie conn->stop_stage = flag; 3388659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 3389ed2abc7fSMike Christie 339026013ad4SMike Christie del_timer_sync(&conn->transport_timer); 339126013ad4SMike Christie iscsi_suspend_tx(conn); 339226013ad4SMike Christie 3393659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 339467a61114SMike Christie conn->c_stage = ISCSI_CONN_STOPPED; 3395659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 33966724add1SMike Christie 33977996a778SMike Christie /* 33987996a778SMike Christie * for connection level recovery we should not calculate 33997996a778SMike Christie * header digest. conn->hdr_size used for optimization 34007996a778SMike Christie * in hdr_extract() and will be re-negotiated at 34017996a778SMike Christie * set_param() time. 34027996a778SMike Christie */ 34037996a778SMike Christie if (flag == STOP_CONN_RECOVER) { 34047996a778SMike Christie conn->hdrdgst_en = 0; 34057996a778SMike Christie conn->datadgst_en = 0; 3406656cffc9SMike Christie if (session->state == ISCSI_STATE_IN_RECOVERY && 340767a61114SMike Christie old_stop_stage != STOP_CONN_RECOVER) { 34081b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "blocking session\n"); 340975613521SMike Christie iscsi_block_session(session->cls_session); 34107996a778SMike Christie } 341167a61114SMike Christie } 3412656cffc9SMike Christie 3413656cffc9SMike Christie /* 3414656cffc9SMike Christie * flush queues. 3415656cffc9SMike Christie */ 3416659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 34173bbaaad9SMike Christie fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED); 34183bbaaad9SMike Christie fail_mgmt_tasks(session, conn); 3419ec29d0acSMike Christie memset(&session->tmhdr, 0, sizeof(session->tmhdr)); 3420659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 34216724add1SMike Christie mutex_unlock(&session->eh_mutex); 34227996a778SMike Christie } 34237996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_stop); 34247996a778SMike Christie 34257996a778SMike Christie int iscsi_conn_bind(struct iscsi_cls_session *cls_session, 34267996a778SMike Christie struct iscsi_cls_conn *cls_conn, int is_leading) 34277996a778SMike Christie { 342875613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 342998644047SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 34307996a778SMike Christie 3431659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 34327996a778SMike Christie if (is_leading) 34337996a778SMike Christie session->leadconn = conn; 343444ac9710SMike Christie 343544ac9710SMike Christie set_bit(ISCSI_CONN_FLAG_BOUND, &conn->flags); 3436659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 34377996a778SMike Christie 34387996a778SMike Christie /* 3439c8447e4cSMike Christie * The target could have reduced it's window size between logins, so 3440c8447e4cSMike Christie * we have to reset max/exp cmdsn so we can see the new values. 3441c8447e4cSMike Christie */ 3442c8447e4cSMike Christie spin_lock_bh(&session->back_lock); 3443c8447e4cSMike Christie session->max_cmdsn = session->exp_cmdsn = session->cmdsn + 1; 3444c8447e4cSMike Christie spin_unlock_bh(&session->back_lock); 3445c8447e4cSMike Christie /* 34467996a778SMike Christie * Unblock xmitworker(), Login Phase will pass through. 34477996a778SMike Christie */ 34485bd85625SMike Christie clear_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags); 34495bd85625SMike Christie clear_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags); 34507996a778SMike Christie return 0; 34517996a778SMike Christie } 34527996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_bind); 34537996a778SMike Christie 3454adaf6990SAdheer Chandravanshi int iscsi_switch_str_param(char **param, char *new_val_buf) 34555700b1afSMike Christie { 34565700b1afSMike Christie char *new_val; 34575700b1afSMike Christie 34585700b1afSMike Christie if (*param) { 34595700b1afSMike Christie if (!strcmp(*param, new_val_buf)) 34605700b1afSMike Christie return 0; 34615700b1afSMike Christie } 34625700b1afSMike Christie 34635700b1afSMike Christie new_val = kstrdup(new_val_buf, GFP_NOIO); 34645700b1afSMike Christie if (!new_val) 34655700b1afSMike Christie return -ENOMEM; 34665700b1afSMike Christie 34675700b1afSMike Christie kfree(*param); 34685700b1afSMike Christie *param = new_val; 34695700b1afSMike Christie return 0; 34705700b1afSMike Christie } 3471adaf6990SAdheer Chandravanshi EXPORT_SYMBOL_GPL(iscsi_switch_str_param); 3472a54a52caSMike Christie 3473a54a52caSMike Christie int iscsi_set_param(struct iscsi_cls_conn *cls_conn, 3474a54a52caSMike Christie enum iscsi_param param, char *buf, int buflen) 3475a54a52caSMike Christie { 3476a54a52caSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 3477a54a52caSMike Christie struct iscsi_session *session = conn->session; 34786a06a4b8SOr Gerlitz int val; 3479a54a52caSMike Christie 3480a54a52caSMike Christie switch(param) { 3481843c0a8aSMike Christie case ISCSI_PARAM_FAST_ABORT: 3482843c0a8aSMike Christie sscanf(buf, "%d", &session->fast_abort); 3483843c0a8aSMike Christie break; 3484f6d5180cSMike Christie case ISCSI_PARAM_ABORT_TMO: 3485f6d5180cSMike Christie sscanf(buf, "%d", &session->abort_timeout); 3486f6d5180cSMike Christie break; 3487f6d5180cSMike Christie case ISCSI_PARAM_LU_RESET_TMO: 3488f6d5180cSMike Christie sscanf(buf, "%d", &session->lu_reset_timeout); 3489f6d5180cSMike Christie break; 34903fe5ae8bSMike Christie case ISCSI_PARAM_TGT_RESET_TMO: 34913fe5ae8bSMike Christie sscanf(buf, "%d", &session->tgt_reset_timeout); 34923fe5ae8bSMike Christie break; 3493f6d5180cSMike Christie case ISCSI_PARAM_PING_TMO: 3494f6d5180cSMike Christie sscanf(buf, "%d", &conn->ping_timeout); 3495f6d5180cSMike Christie break; 3496f6d5180cSMike Christie case ISCSI_PARAM_RECV_TMO: 3497f6d5180cSMike Christie sscanf(buf, "%d", &conn->recv_timeout); 3498f6d5180cSMike Christie break; 3499a54a52caSMike Christie case ISCSI_PARAM_MAX_RECV_DLENGTH: 3500a54a52caSMike Christie sscanf(buf, "%d", &conn->max_recv_dlength); 3501a54a52caSMike Christie break; 3502a54a52caSMike Christie case ISCSI_PARAM_MAX_XMIT_DLENGTH: 3503a54a52caSMike Christie sscanf(buf, "%d", &conn->max_xmit_dlength); 3504a54a52caSMike Christie break; 3505a54a52caSMike Christie case ISCSI_PARAM_HDRDGST_EN: 3506a54a52caSMike Christie sscanf(buf, "%d", &conn->hdrdgst_en); 3507a54a52caSMike Christie break; 3508a54a52caSMike Christie case ISCSI_PARAM_DATADGST_EN: 3509a54a52caSMike Christie sscanf(buf, "%d", &conn->datadgst_en); 3510a54a52caSMike Christie break; 3511a54a52caSMike Christie case ISCSI_PARAM_INITIAL_R2T_EN: 3512a54a52caSMike Christie sscanf(buf, "%d", &session->initial_r2t_en); 3513a54a52caSMike Christie break; 3514a54a52caSMike Christie case ISCSI_PARAM_MAX_R2T: 35151304be5fSMike Christie sscanf(buf, "%hu", &session->max_r2t); 3516a54a52caSMike Christie break; 3517a54a52caSMike Christie case ISCSI_PARAM_IMM_DATA_EN: 3518a54a52caSMike Christie sscanf(buf, "%d", &session->imm_data_en); 3519a54a52caSMike Christie break; 3520a54a52caSMike Christie case ISCSI_PARAM_FIRST_BURST: 3521a54a52caSMike Christie sscanf(buf, "%d", &session->first_burst); 3522a54a52caSMike Christie break; 3523a54a52caSMike Christie case ISCSI_PARAM_MAX_BURST: 3524a54a52caSMike Christie sscanf(buf, "%d", &session->max_burst); 3525a54a52caSMike Christie break; 3526a54a52caSMike Christie case ISCSI_PARAM_PDU_INORDER_EN: 3527a54a52caSMike Christie sscanf(buf, "%d", &session->pdu_inorder_en); 3528a54a52caSMike Christie break; 3529a54a52caSMike Christie case ISCSI_PARAM_DATASEQ_INORDER_EN: 3530a54a52caSMike Christie sscanf(buf, "%d", &session->dataseq_inorder_en); 3531a54a52caSMike Christie break; 3532a54a52caSMike Christie case ISCSI_PARAM_ERL: 3533a54a52caSMike Christie sscanf(buf, "%d", &session->erl); 3534a54a52caSMike Christie break; 3535a54a52caSMike Christie case ISCSI_PARAM_EXP_STATSN: 3536a54a52caSMike Christie sscanf(buf, "%u", &conn->exp_statsn); 3537a54a52caSMike Christie break; 3538b2c64167SMike Christie case ISCSI_PARAM_USERNAME: 35395700b1afSMike Christie return iscsi_switch_str_param(&session->username, buf); 3540b2c64167SMike Christie case ISCSI_PARAM_USERNAME_IN: 35415700b1afSMike Christie return iscsi_switch_str_param(&session->username_in, buf); 3542b2c64167SMike Christie case ISCSI_PARAM_PASSWORD: 35435700b1afSMike Christie return iscsi_switch_str_param(&session->password, buf); 3544b2c64167SMike Christie case ISCSI_PARAM_PASSWORD_IN: 35455700b1afSMike Christie return iscsi_switch_str_param(&session->password_in, buf); 3546a54a52caSMike Christie case ISCSI_PARAM_TARGET_NAME: 35475700b1afSMike Christie return iscsi_switch_str_param(&session->targetname, buf); 35483c5c4801SVikas Chaudhary case ISCSI_PARAM_TARGET_ALIAS: 35493c5c4801SVikas Chaudhary return iscsi_switch_str_param(&session->targetalias, buf); 3550a54a52caSMike Christie case ISCSI_PARAM_TPGT: 3551a54a52caSMike Christie sscanf(buf, "%d", &session->tpgt); 3552a54a52caSMike Christie break; 3553a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_PORT: 3554a54a52caSMike Christie sscanf(buf, "%d", &conn->persistent_port); 3555a54a52caSMike Christie break; 3556a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_ADDRESS: 35575700b1afSMike Christie return iscsi_switch_str_param(&conn->persistent_address, buf); 355888dfd340SMike Christie case ISCSI_PARAM_IFACE_NAME: 35595700b1afSMike Christie return iscsi_switch_str_param(&session->ifacename, buf); 356088dfd340SMike Christie case ISCSI_PARAM_INITIATOR_NAME: 35615700b1afSMike Christie return iscsi_switch_str_param(&session->initiatorname, buf); 35623b9373e9SEddie Wai case ISCSI_PARAM_BOOT_ROOT: 35633b9373e9SEddie Wai return iscsi_switch_str_param(&session->boot_root, buf); 35643b9373e9SEddie Wai case ISCSI_PARAM_BOOT_NIC: 35653b9373e9SEddie Wai return iscsi_switch_str_param(&session->boot_nic, buf); 35663b9373e9SEddie Wai case ISCSI_PARAM_BOOT_TARGET: 35673b9373e9SEddie Wai return iscsi_switch_str_param(&session->boot_target, buf); 3568f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_PORTAL_TYPE: 3569f8525eb4SAdheer Chandravanshi return iscsi_switch_str_param(&session->portal_type, buf); 3570f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_PARENT_TYPE: 3571f8525eb4SAdheer Chandravanshi return iscsi_switch_str_param(&session->discovery_parent_type, 3572f8525eb4SAdheer Chandravanshi buf); 35736a06a4b8SOr Gerlitz case ISCSI_PARAM_DISCOVERY_SESS: 35746a06a4b8SOr Gerlitz sscanf(buf, "%d", &val); 35756a06a4b8SOr Gerlitz session->discovery_sess = !!val; 35766a06a4b8SOr Gerlitz break; 3577ae56ff40SAdheer Chandravanshi case ISCSI_PARAM_LOCAL_IPADDR: 3578ae56ff40SAdheer Chandravanshi return iscsi_switch_str_param(&conn->local_ipaddr, buf); 3579a54a52caSMike Christie default: 3580a54a52caSMike Christie return -ENOSYS; 3581a54a52caSMike Christie } 3582a54a52caSMike Christie 3583a54a52caSMike Christie return 0; 3584a54a52caSMike Christie } 3585a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_set_param); 3586a54a52caSMike Christie 3587a54a52caSMike Christie int iscsi_session_get_param(struct iscsi_cls_session *cls_session, 3588a54a52caSMike Christie enum iscsi_param param, char *buf) 3589a54a52caSMike Christie { 359075613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 3591a54a52caSMike Christie int len; 3592a54a52caSMike Christie 3593a54a52caSMike Christie switch(param) { 3594843c0a8aSMike Christie case ISCSI_PARAM_FAST_ABORT: 3595ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->fast_abort); 3596843c0a8aSMike Christie break; 3597f6d5180cSMike Christie case ISCSI_PARAM_ABORT_TMO: 3598ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->abort_timeout); 3599f6d5180cSMike Christie break; 3600f6d5180cSMike Christie case ISCSI_PARAM_LU_RESET_TMO: 3601ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->lu_reset_timeout); 3602f6d5180cSMike Christie break; 36033fe5ae8bSMike Christie case ISCSI_PARAM_TGT_RESET_TMO: 3604ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->tgt_reset_timeout); 36053fe5ae8bSMike Christie break; 3606a54a52caSMike Christie case ISCSI_PARAM_INITIAL_R2T_EN: 3607ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->initial_r2t_en); 3608a54a52caSMike Christie break; 3609a54a52caSMike Christie case ISCSI_PARAM_MAX_R2T: 3610ec98ea70SChris Leech len = sysfs_emit(buf, "%hu\n", session->max_r2t); 3611a54a52caSMike Christie break; 3612a54a52caSMike Christie case ISCSI_PARAM_IMM_DATA_EN: 3613ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->imm_data_en); 3614a54a52caSMike Christie break; 3615a54a52caSMike Christie case ISCSI_PARAM_FIRST_BURST: 3616ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->first_burst); 3617a54a52caSMike Christie break; 3618a54a52caSMike Christie case ISCSI_PARAM_MAX_BURST: 3619ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->max_burst); 3620a54a52caSMike Christie break; 3621a54a52caSMike Christie case ISCSI_PARAM_PDU_INORDER_EN: 3622ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->pdu_inorder_en); 3623a54a52caSMike Christie break; 3624a54a52caSMike Christie case ISCSI_PARAM_DATASEQ_INORDER_EN: 3625ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->dataseq_inorder_en); 3626a54a52caSMike Christie break; 36275f09e1f3SAdheer Chandravanshi case ISCSI_PARAM_DEF_TASKMGMT_TMO: 3628ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->def_taskmgmt_tmo); 36295f09e1f3SAdheer Chandravanshi break; 3630a54a52caSMike Christie case ISCSI_PARAM_ERL: 3631ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->erl); 3632a54a52caSMike Christie break; 3633a54a52caSMike Christie case ISCSI_PARAM_TARGET_NAME: 3634ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->targetname); 3635a54a52caSMike Christie break; 36363c5c4801SVikas Chaudhary case ISCSI_PARAM_TARGET_ALIAS: 3637ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->targetalias); 36383c5c4801SVikas Chaudhary break; 3639a54a52caSMike Christie case ISCSI_PARAM_TPGT: 3640ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->tpgt); 3641a54a52caSMike Christie break; 3642b2c64167SMike Christie case ISCSI_PARAM_USERNAME: 3643ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->username); 3644b2c64167SMike Christie break; 3645b2c64167SMike Christie case ISCSI_PARAM_USERNAME_IN: 3646ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->username_in); 3647b2c64167SMike Christie break; 3648b2c64167SMike Christie case ISCSI_PARAM_PASSWORD: 3649ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->password); 3650b2c64167SMike Christie break; 3651b2c64167SMike Christie case ISCSI_PARAM_PASSWORD_IN: 3652ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->password_in); 3653b2c64167SMike Christie break; 365488dfd340SMike Christie case ISCSI_PARAM_IFACE_NAME: 3655ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->ifacename); 365688dfd340SMike Christie break; 365788dfd340SMike Christie case ISCSI_PARAM_INITIATOR_NAME: 3658ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->initiatorname); 365988dfd340SMike Christie break; 36603b9373e9SEddie Wai case ISCSI_PARAM_BOOT_ROOT: 3661ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->boot_root); 36623b9373e9SEddie Wai break; 36633b9373e9SEddie Wai case ISCSI_PARAM_BOOT_NIC: 3664ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->boot_nic); 36653b9373e9SEddie Wai break; 36663b9373e9SEddie Wai case ISCSI_PARAM_BOOT_TARGET: 3667ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->boot_target); 36689a2307b1SAdheer Chandravanshi break; 3669f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_AUTO_SND_TGT_DISABLE: 3670ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->auto_snd_tgt_disable); 3671f8525eb4SAdheer Chandravanshi break; 3672f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_SESS: 3673ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->discovery_sess); 3674f8525eb4SAdheer Chandravanshi break; 3675f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_PORTAL_TYPE: 3676ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->portal_type); 3677f8525eb4SAdheer Chandravanshi break; 3678f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_CHAP_AUTH_EN: 3679ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->chap_auth_en); 3680f8525eb4SAdheer Chandravanshi break; 3681f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_LOGOUT_EN: 3682ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->discovery_logout_en); 3683f8525eb4SAdheer Chandravanshi break; 3684f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_BIDI_CHAP_EN: 3685ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->bidi_chap_en); 3686f8525eb4SAdheer Chandravanshi break; 3687f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL: 3688ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->discovery_auth_optional); 3689f8525eb4SAdheer Chandravanshi break; 3690f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DEF_TIME2WAIT: 3691ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->time2wait); 3692f8525eb4SAdheer Chandravanshi break; 3693f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DEF_TIME2RETAIN: 3694ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->time2retain); 3695f8525eb4SAdheer Chandravanshi break; 3696f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TSID: 3697ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->tsid); 3698f8525eb4SAdheer Chandravanshi break; 3699f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_ISID: 3700ec98ea70SChris Leech len = sysfs_emit(buf, "%02x%02x%02x%02x%02x%02x\n", 3701f8525eb4SAdheer Chandravanshi session->isid[0], session->isid[1], 3702f8525eb4SAdheer Chandravanshi session->isid[2], session->isid[3], 3703f8525eb4SAdheer Chandravanshi session->isid[4], session->isid[5]); 3704f8525eb4SAdheer Chandravanshi break; 3705f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_PARENT_IDX: 3706ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->discovery_parent_idx); 3707f8525eb4SAdheer Chandravanshi break; 3708f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_PARENT_TYPE: 3709f8525eb4SAdheer Chandravanshi if (session->discovery_parent_type) 3710ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", 3711f8525eb4SAdheer Chandravanshi session->discovery_parent_type); 3712f8525eb4SAdheer Chandravanshi else 3713ec98ea70SChris Leech len = sysfs_emit(buf, "\n"); 37143b9373e9SEddie Wai break; 3715a54a52caSMike Christie default: 3716a54a52caSMike Christie return -ENOSYS; 3717a54a52caSMike Christie } 3718a54a52caSMike Christie 3719a54a52caSMike Christie return len; 3720a54a52caSMike Christie } 3721a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_get_param); 3722a54a52caSMike Christie 372300f3708eSMike Christie int iscsi_conn_get_addr_param(struct sockaddr_storage *addr, 372400f3708eSMike Christie enum iscsi_param param, char *buf) 372500f3708eSMike Christie { 372600f3708eSMike Christie struct sockaddr_in6 *sin6 = NULL; 372700f3708eSMike Christie struct sockaddr_in *sin = NULL; 372800f3708eSMike Christie int len; 372900f3708eSMike Christie 373000f3708eSMike Christie switch (addr->ss_family) { 373100f3708eSMike Christie case AF_INET: 373200f3708eSMike Christie sin = (struct sockaddr_in *)addr; 373300f3708eSMike Christie break; 373400f3708eSMike Christie case AF_INET6: 373500f3708eSMike Christie sin6 = (struct sockaddr_in6 *)addr; 373600f3708eSMike Christie break; 373700f3708eSMike Christie default: 373800f3708eSMike Christie return -EINVAL; 373900f3708eSMike Christie } 374000f3708eSMike Christie 374100f3708eSMike Christie switch (param) { 374200f3708eSMike Christie case ISCSI_PARAM_CONN_ADDRESS: 374300f3708eSMike Christie case ISCSI_HOST_PARAM_IPADDRESS: 374400f3708eSMike Christie if (sin) 3745ec98ea70SChris Leech len = sysfs_emit(buf, "%pI4\n", &sin->sin_addr.s_addr); 374600f3708eSMike Christie else 3747ec98ea70SChris Leech len = sysfs_emit(buf, "%pI6\n", &sin6->sin6_addr); 374800f3708eSMike Christie break; 374900f3708eSMike Christie case ISCSI_PARAM_CONN_PORT: 37504bfb8ebfSMike Christie case ISCSI_PARAM_LOCAL_PORT: 375100f3708eSMike Christie if (sin) 3752ec98ea70SChris Leech len = sysfs_emit(buf, "%hu\n", be16_to_cpu(sin->sin_port)); 375300f3708eSMike Christie else 3754ec98ea70SChris Leech len = sysfs_emit(buf, "%hu\n", 375500f3708eSMike Christie be16_to_cpu(sin6->sin6_port)); 375600f3708eSMike Christie break; 375700f3708eSMike Christie default: 375800f3708eSMike Christie return -EINVAL; 375900f3708eSMike Christie } 376000f3708eSMike Christie 376100f3708eSMike Christie return len; 376200f3708eSMike Christie } 376300f3708eSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param); 376400f3708eSMike Christie 3765a54a52caSMike Christie int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn, 3766a54a52caSMike Christie enum iscsi_param param, char *buf) 3767a54a52caSMike Christie { 3768a54a52caSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 3769a54a52caSMike Christie int len; 3770a54a52caSMike Christie 3771a54a52caSMike Christie switch(param) { 3772f6d5180cSMike Christie case ISCSI_PARAM_PING_TMO: 3773ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->ping_timeout); 3774f6d5180cSMike Christie break; 3775f6d5180cSMike Christie case ISCSI_PARAM_RECV_TMO: 3776ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->recv_timeout); 3777f6d5180cSMike Christie break; 3778a54a52caSMike Christie case ISCSI_PARAM_MAX_RECV_DLENGTH: 3779ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->max_recv_dlength); 3780a54a52caSMike Christie break; 3781a54a52caSMike Christie case ISCSI_PARAM_MAX_XMIT_DLENGTH: 3782ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->max_xmit_dlength); 3783a54a52caSMike Christie break; 3784a54a52caSMike Christie case ISCSI_PARAM_HDRDGST_EN: 3785ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", conn->hdrdgst_en); 3786a54a52caSMike Christie break; 3787a54a52caSMike Christie case ISCSI_PARAM_DATADGST_EN: 3788ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", conn->datadgst_en); 3789a54a52caSMike Christie break; 3790a54a52caSMike Christie case ISCSI_PARAM_IFMARKER_EN: 3791ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", conn->ifmarker_en); 3792a54a52caSMike Christie break; 3793a54a52caSMike Christie case ISCSI_PARAM_OFMARKER_EN: 3794ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", conn->ofmarker_en); 3795a54a52caSMike Christie break; 3796a54a52caSMike Christie case ISCSI_PARAM_EXP_STATSN: 3797ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->exp_statsn); 3798a54a52caSMike Christie break; 3799a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_PORT: 3800ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", conn->persistent_port); 3801a54a52caSMike Christie break; 3802a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_ADDRESS: 3803ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", conn->persistent_address); 3804a54a52caSMike Christie break; 3805f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_STATSN: 3806ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->statsn); 3807f8525eb4SAdheer Chandravanshi break; 3808f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_MAX_SEGMENT_SIZE: 3809ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->max_segment_size); 3810f8525eb4SAdheer Chandravanshi break; 3811f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_KEEPALIVE_TMO: 3812ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->keepalive_tmo); 3813f8525eb4SAdheer Chandravanshi break; 3814f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_LOCAL_PORT: 3815ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->local_port); 3816f8525eb4SAdheer Chandravanshi break; 3817f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_TIMESTAMP_STAT: 3818ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_stat); 3819f8525eb4SAdheer Chandravanshi break; 3820f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_NAGLE_DISABLE: 3821ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->tcp_nagle_disable); 3822f8525eb4SAdheer Chandravanshi break; 3823f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_WSF_DISABLE: 3824ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->tcp_wsf_disable); 3825f8525eb4SAdheer Chandravanshi break; 3826f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_TIMER_SCALE: 3827ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->tcp_timer_scale); 3828f8525eb4SAdheer Chandravanshi break; 3829f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_TIMESTAMP_EN: 3830ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_en); 3831f8525eb4SAdheer Chandravanshi break; 3832f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_IP_FRAGMENT_DISABLE: 3833ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->fragment_disable); 3834f8525eb4SAdheer Chandravanshi break; 3835f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_IPV4_TOS: 3836ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->ipv4_tos); 3837f8525eb4SAdheer Chandravanshi break; 3838f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_IPV6_TC: 3839ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->ipv6_traffic_class); 3840f8525eb4SAdheer Chandravanshi break; 38415f09e1f3SAdheer Chandravanshi case ISCSI_PARAM_IPV6_FLOW_LABEL: 3842ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->ipv6_flow_label); 38435f09e1f3SAdheer Chandravanshi break; 3844f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6: 3845ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->is_fw_assigned_ipv6); 3846f8525eb4SAdheer Chandravanshi break; 3847f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_XMIT_WSF: 3848ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->tcp_xmit_wsf); 3849f8525eb4SAdheer Chandravanshi break; 3850f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_RECV_WSF: 3851ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->tcp_recv_wsf); 3852f8525eb4SAdheer Chandravanshi break; 3853ae56ff40SAdheer Chandravanshi case ISCSI_PARAM_LOCAL_IPADDR: 3854ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", conn->local_ipaddr); 3855ae56ff40SAdheer Chandravanshi break; 3856a54a52caSMike Christie default: 3857a54a52caSMike Christie return -ENOSYS; 3858a54a52caSMike Christie } 3859a54a52caSMike Christie 3860a54a52caSMike Christie return len; 3861a54a52caSMike Christie } 3862a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_param); 3863a54a52caSMike Christie 38640801c242SMike Christie int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param, 38650801c242SMike Christie char *buf) 38660801c242SMike Christie { 386775613521SMike Christie struct iscsi_host *ihost = shost_priv(shost); 38680801c242SMike Christie int len; 38690801c242SMike Christie 38700801c242SMike Christie switch (param) { 3871d8196ed2SMike Christie case ISCSI_HOST_PARAM_NETDEV_NAME: 3872ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", ihost->netdev); 3873d8196ed2SMike Christie break; 38740801c242SMike Christie case ISCSI_HOST_PARAM_HWADDRESS: 3875ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", ihost->hwaddress); 38760801c242SMike Christie break; 38778ad5781aSMike Christie case ISCSI_HOST_PARAM_INITIATOR_NAME: 3878ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", ihost->initiatorname); 38798ad5781aSMike Christie break; 38800801c242SMike Christie default: 38810801c242SMike Christie return -ENOSYS; 38820801c242SMike Christie } 38830801c242SMike Christie 38840801c242SMike Christie return len; 38850801c242SMike Christie } 38860801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_get_param); 38870801c242SMike Christie 38880801c242SMike Christie int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param, 38890801c242SMike Christie char *buf, int buflen) 38900801c242SMike Christie { 389175613521SMike Christie struct iscsi_host *ihost = shost_priv(shost); 38920801c242SMike Christie 38930801c242SMike Christie switch (param) { 3894d8196ed2SMike Christie case ISCSI_HOST_PARAM_NETDEV_NAME: 38955700b1afSMike Christie return iscsi_switch_str_param(&ihost->netdev, buf); 38960801c242SMike Christie case ISCSI_HOST_PARAM_HWADDRESS: 38975700b1afSMike Christie return iscsi_switch_str_param(&ihost->hwaddress, buf); 38988ad5781aSMike Christie case ISCSI_HOST_PARAM_INITIATOR_NAME: 38995700b1afSMike Christie return iscsi_switch_str_param(&ihost->initiatorname, buf); 39000801c242SMike Christie default: 39010801c242SMike Christie return -ENOSYS; 39020801c242SMike Christie } 39030801c242SMike Christie 39040801c242SMike Christie return 0; 39050801c242SMike Christie } 39060801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_set_param); 39070801c242SMike Christie 39087996a778SMike Christie MODULE_AUTHOR("Mike Christie"); 39097996a778SMike Christie MODULE_DESCRIPTION("iSCSI library functions"); 39107996a778SMike Christie MODULE_LICENSE("GPL"); 3911