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 8632ae763eSMike Christie inline void iscsi_conn_queue_work(struct iscsi_conn *conn) 8732ae763eSMike Christie { 8832ae763eSMike Christie struct Scsi_Host *shost = conn->session->host; 8932ae763eSMike Christie struct iscsi_host *ihost = shost_priv(shost); 9032ae763eSMike Christie 911336aed1SMike Christie if (ihost->workq) 9232ae763eSMike Christie queue_work(ihost->workq, &conn->xmitwork); 9332ae763eSMike Christie } 9432ae763eSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_queue_work); 9532ae763eSMike Christie 964c0ba5d2SMike Christie static void __iscsi_update_cmdsn(struct iscsi_session *session, 974c0ba5d2SMike Christie uint32_t exp_cmdsn, uint32_t max_cmdsn) 987996a778SMike Christie { 9977a23c21SMike Christie /* 10077a23c21SMike Christie * standard specifies this check for when to update expected and 10177a23c21SMike Christie * max sequence numbers 10277a23c21SMike Christie */ 10377a23c21SMike Christie if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1)) 10477a23c21SMike Christie return; 10577a23c21SMike Christie 10677a23c21SMike Christie if (exp_cmdsn != session->exp_cmdsn && 10777a23c21SMike Christie !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn)) 1087996a778SMike Christie session->exp_cmdsn = exp_cmdsn; 1097996a778SMike Christie 11077a23c21SMike Christie if (max_cmdsn != session->max_cmdsn && 11146a84c65SMike Christie !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) 11277a23c21SMike Christie session->max_cmdsn = max_cmdsn; 11377a23c21SMike Christie } 1144c0ba5d2SMike Christie 1154c0ba5d2SMike Christie void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr) 1164c0ba5d2SMike Christie { 1174c0ba5d2SMike Christie __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn), 1184c0ba5d2SMike Christie be32_to_cpu(hdr->max_cmdsn)); 1194c0ba5d2SMike Christie } 12077a23c21SMike Christie EXPORT_SYMBOL_GPL(iscsi_update_cmdsn); 1217996a778SMike Christie 122577577daSMike Christie /** 123577577daSMike Christie * iscsi_prep_data_out_pdu - initialize Data-Out 124577577daSMike Christie * @task: scsi command task 125577577daSMike Christie * @r2t: R2T info 126577577daSMike Christie * @hdr: iscsi data in pdu 127577577daSMike Christie * 128577577daSMike Christie * Notes: 129577577daSMike Christie * Initialize Data-Out within this R2T sequence and finds 130577577daSMike Christie * proper data_offset within this SCSI command. 131577577daSMike Christie * 132577577daSMike Christie * This function is called with connection lock taken. 133577577daSMike Christie **/ 134577577daSMike Christie void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t, 135ffd0436eSMike Christie struct iscsi_data *hdr) 1367996a778SMike Christie { 1379c19a7d0SMike Christie struct iscsi_conn *conn = task->conn; 138577577daSMike Christie unsigned int left = r2t->data_length - r2t->sent; 139577577daSMike Christie 140577577daSMike Christie task->hdr_len = sizeof(struct iscsi_data); 1417996a778SMike Christie 1427996a778SMike Christie memset(hdr, 0, sizeof(struct iscsi_data)); 143577577daSMike Christie hdr->ttt = r2t->ttt; 144577577daSMike Christie hdr->datasn = cpu_to_be32(r2t->datasn); 145577577daSMike Christie r2t->datasn++; 1467996a778SMike Christie hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; 14755bdabdfSAndy Grover hdr->lun = task->lun; 148577577daSMike Christie hdr->itt = task->hdr_itt; 149577577daSMike Christie hdr->exp_statsn = r2t->exp_statsn; 150577577daSMike Christie hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent); 151577577daSMike Christie if (left > conn->max_xmit_dlength) { 1527996a778SMike Christie hton24(hdr->dlength, conn->max_xmit_dlength); 153577577daSMike Christie r2t->data_count = conn->max_xmit_dlength; 1547996a778SMike Christie hdr->flags = 0; 1557996a778SMike Christie } else { 156577577daSMike Christie hton24(hdr->dlength, left); 157577577daSMike Christie r2t->data_count = left; 1587996a778SMike Christie hdr->flags = ISCSI_FLAG_CMD_FINAL; 1597996a778SMike Christie } 160577577daSMike Christie conn->dataout_pdus_cnt++; 1617996a778SMike Christie } 162577577daSMike Christie EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu); 1637996a778SMike Christie 1649c19a7d0SMike Christie static int iscsi_add_hdr(struct iscsi_task *task, unsigned len) 165004d6530SBoaz Harrosh { 1669c19a7d0SMike Christie unsigned exp_len = task->hdr_len + len; 167004d6530SBoaz Harrosh 1689c19a7d0SMike Christie if (exp_len > task->hdr_max) { 169004d6530SBoaz Harrosh WARN_ON(1); 170004d6530SBoaz Harrosh return -EINVAL; 171004d6530SBoaz Harrosh } 172004d6530SBoaz Harrosh 173004d6530SBoaz Harrosh WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */ 1749c19a7d0SMike Christie task->hdr_len = exp_len; 175004d6530SBoaz Harrosh return 0; 176004d6530SBoaz Harrosh } 177004d6530SBoaz Harrosh 17838d1c069SBoaz Harrosh /* 17938d1c069SBoaz Harrosh * make an extended cdb AHS 18038d1c069SBoaz Harrosh */ 1819c19a7d0SMike Christie static int iscsi_prep_ecdb_ahs(struct iscsi_task *task) 18238d1c069SBoaz Harrosh { 1839c19a7d0SMike Christie struct scsi_cmnd *cmd = task->sc; 18438d1c069SBoaz Harrosh unsigned rlen, pad_len; 18538d1c069SBoaz Harrosh unsigned short ahslength; 18638d1c069SBoaz Harrosh struct iscsi_ecdb_ahdr *ecdb_ahdr; 18738d1c069SBoaz Harrosh int rc; 18838d1c069SBoaz Harrosh 1899c19a7d0SMike Christie ecdb_ahdr = iscsi_next_hdr(task); 19038d1c069SBoaz Harrosh rlen = cmd->cmd_len - ISCSI_CDB_SIZE; 19138d1c069SBoaz Harrosh 19238d1c069SBoaz Harrosh BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb)); 19338d1c069SBoaz Harrosh ahslength = rlen + sizeof(ecdb_ahdr->reserved); 19438d1c069SBoaz Harrosh 19538d1c069SBoaz Harrosh pad_len = iscsi_padding(rlen); 19638d1c069SBoaz Harrosh 1979c19a7d0SMike Christie rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) + 19838d1c069SBoaz Harrosh sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len); 19938d1c069SBoaz Harrosh if (rc) 20038d1c069SBoaz Harrosh return rc; 20138d1c069SBoaz Harrosh 20238d1c069SBoaz Harrosh if (pad_len) 20338d1c069SBoaz Harrosh memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len); 20438d1c069SBoaz Harrosh 20538d1c069SBoaz Harrosh ecdb_ahdr->ahslength = cpu_to_be16(ahslength); 20638d1c069SBoaz Harrosh ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB; 20738d1c069SBoaz Harrosh ecdb_ahdr->reserved = 0; 20838d1c069SBoaz Harrosh memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen); 20938d1c069SBoaz Harrosh 2101b2c7af8SMike Christie ISCSI_DBG_SESSION(task->conn->session, 2111b2c7af8SMike Christie "iscsi_prep_ecdb_ahs: varlen_cdb_len %d " 2121b2c7af8SMike Christie "rlen %d pad_len %d ahs_length %d iscsi_headers_size " 2131b2c7af8SMike Christie "%u\n", cmd->cmd_len, rlen, pad_len, ahslength, 2141b2c7af8SMike Christie task->hdr_len); 21538d1c069SBoaz Harrosh return 0; 21638d1c069SBoaz Harrosh } 21738d1c069SBoaz Harrosh 2187996a778SMike Christie /** 2195d12c05eSMike Christie * iscsi_check_tmf_restrictions - check if a task is affected by TMF 2205d12c05eSMike Christie * @task: iscsi task 2215d12c05eSMike Christie * @opcode: opcode to check for 2225d12c05eSMike Christie * 2235d12c05eSMike Christie * During TMF a task has to be checked if it's affected. 2245d12c05eSMike Christie * All unrelated I/O can be passed through, but I/O to the 2255d12c05eSMike Christie * affected LUN should be restricted. 2265d12c05eSMike Christie * If 'fast_abort' is set we won't be sending any I/O to the 2275d12c05eSMike Christie * affected LUN. 2285d12c05eSMike Christie * Otherwise the target is waiting for all TTTs to be completed, 2295d12c05eSMike Christie * so we have to send all outstanding Data-Out PDUs to the target. 2305d12c05eSMike Christie */ 2315d12c05eSMike Christie static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode) 2325d12c05eSMike Christie { 2335d12c05eSMike Christie struct iscsi_conn *conn = task->conn; 2345d12c05eSMike Christie struct iscsi_tm *tmf = &conn->tmhdr; 2359cb78c16SHannes Reinecke u64 hdr_lun; 2365d12c05eSMike Christie 2375d12c05eSMike Christie if (conn->tmf_state == TMF_INITIAL) 2385d12c05eSMike Christie return 0; 2395d12c05eSMike Christie 2405d12c05eSMike Christie if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC) 2415d12c05eSMike Christie return 0; 2425d12c05eSMike Christie 2435d12c05eSMike Christie switch (ISCSI_TM_FUNC_VALUE(tmf)) { 2445d12c05eSMike Christie case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET: 2455d12c05eSMike Christie /* 2465d12c05eSMike Christie * Allow PDUs for unrelated LUNs 2475d12c05eSMike Christie */ 24855bdabdfSAndy Grover hdr_lun = scsilun_to_int(&tmf->lun); 2495d12c05eSMike Christie if (hdr_lun != task->sc->device->lun) 2505d12c05eSMike Christie return 0; 251df561f66SGustavo A. R. Silva fallthrough; 2523fe5ae8bSMike Christie case ISCSI_TM_FUNC_TARGET_WARM_RESET: 2535d12c05eSMike Christie /* 2545d12c05eSMike Christie * Fail all SCSI cmd PDUs 2555d12c05eSMike Christie */ 2565d12c05eSMike Christie if (opcode != ISCSI_OP_SCSI_DATA_OUT) { 2575d12c05eSMike Christie iscsi_conn_printk(KERN_INFO, conn, 258a17037e7SVarun Prakash "task [op %x itt " 2593fe5ae8bSMike Christie "0x%x/0x%x] " 2605d12c05eSMike Christie "rejected.\n", 261a17037e7SVarun Prakash opcode, task->itt, 262a17037e7SVarun Prakash task->hdr_itt); 2635d12c05eSMike Christie return -EACCES; 2645d12c05eSMike Christie } 2655d12c05eSMike Christie /* 2665d12c05eSMike Christie * And also all data-out PDUs in response to R2T 2675d12c05eSMike Christie * if fast_abort is set. 2685d12c05eSMike Christie */ 2695d12c05eSMike Christie if (conn->session->fast_abort) { 2705d12c05eSMike Christie iscsi_conn_printk(KERN_INFO, conn, 271a17037e7SVarun Prakash "task [op %x itt " 2723fe5ae8bSMike Christie "0x%x/0x%x] fast abort.\n", 273a17037e7SVarun Prakash opcode, task->itt, 274a17037e7SVarun Prakash task->hdr_itt); 2755d12c05eSMike Christie return -EACCES; 2765d12c05eSMike Christie } 2775d12c05eSMike Christie break; 2785d12c05eSMike Christie case ISCSI_TM_FUNC_ABORT_TASK: 2795d12c05eSMike Christie /* 2805d12c05eSMike Christie * the caller has already checked if the task 2815d12c05eSMike Christie * they want to abort was in the pending queue so if 2825d12c05eSMike Christie * we are here the cmd pdu has gone out already, and 2835d12c05eSMike Christie * we will only hit this for data-outs 2845d12c05eSMike Christie */ 2855d12c05eSMike Christie if (opcode == ISCSI_OP_SCSI_DATA_OUT && 2865d12c05eSMike Christie task->hdr_itt == tmf->rtt) { 2875d12c05eSMike Christie ISCSI_DBG_SESSION(conn->session, 2885d12c05eSMike Christie "Preventing task %x/%x from sending " 2895d12c05eSMike Christie "data-out due to abort task in " 2905d12c05eSMike Christie "progress\n", task->itt, 2915d12c05eSMike Christie task->hdr_itt); 2925d12c05eSMike Christie return -EACCES; 2935d12c05eSMike Christie } 2945d12c05eSMike Christie break; 2955d12c05eSMike Christie } 2965d12c05eSMike Christie 2975d12c05eSMike Christie return 0; 2985d12c05eSMike Christie } 2995d12c05eSMike Christie 3005d12c05eSMike Christie /** 3017996a778SMike Christie * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu 3029c19a7d0SMike Christie * @task: iscsi task 3037996a778SMike Christie * 3047996a778SMike Christie * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set 3057996a778SMike Christie * fields like dlength or final based on how much data it sends 3067996a778SMike Christie */ 3079c19a7d0SMike Christie static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task) 3087996a778SMike Christie { 3099c19a7d0SMike Christie struct iscsi_conn *conn = task->conn; 3107996a778SMike Christie struct iscsi_session *session = conn->session; 3119c19a7d0SMike Christie struct scsi_cmnd *sc = task->sc; 31212352183SNicholas Bellinger struct iscsi_scsi_req *hdr; 313d77e6535SSagi Grimberg unsigned hdrlength, cmd_len, transfer_length; 314262ef636SMike Christie itt_t itt; 315004d6530SBoaz Harrosh int rc; 3167996a778SMike Christie 3175d12c05eSMike Christie rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD); 3185d12c05eSMike Christie if (rc) 3195d12c05eSMike Christie return rc; 3205d12c05eSMike Christie 321184b57c6SMike Christie if (conn->session->tt->alloc_pdu) { 3222ff79d52SMike Christie rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD); 323577577daSMike Christie if (rc) 324577577daSMike Christie return rc; 325184b57c6SMike Christie } 32612352183SNicholas Bellinger hdr = (struct iscsi_scsi_req *)task->hdr; 327262ef636SMike Christie itt = hdr->itt; 328577577daSMike Christie memset(hdr, 0, sizeof(*hdr)); 329577577daSMike Christie 3302ff79d52SMike Christie if (session->tt->parse_pdu_itt) 3312ff79d52SMike Christie hdr->itt = task->hdr_itt = itt; 3322ff79d52SMike Christie else 3332ff79d52SMike Christie hdr->itt = task->hdr_itt = build_itt(task->itt, 3342ff79d52SMike Christie task->conn->session->age); 3359c19a7d0SMike Christie task->hdr_len = 0; 3369c19a7d0SMike Christie rc = iscsi_add_hdr(task, sizeof(*hdr)); 337004d6530SBoaz Harrosh if (rc) 338004d6530SBoaz Harrosh return rc; 3397996a778SMike Christie hdr->opcode = ISCSI_OP_SCSI_CMD; 3407996a778SMike Christie hdr->flags = ISCSI_ATTR_SIMPLE; 34155bdabdfSAndy Grover int_to_scsilun(sc->device->lun, &hdr->lun); 34255bdabdfSAndy Grover task->lun = hdr->lun; 3437996a778SMike Christie hdr->exp_statsn = cpu_to_be32(conn->exp_statsn); 34438d1c069SBoaz Harrosh cmd_len = sc->cmd_len; 34538d1c069SBoaz Harrosh if (cmd_len < ISCSI_CDB_SIZE) 34638d1c069SBoaz Harrosh memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len); 34738d1c069SBoaz Harrosh else if (cmd_len > ISCSI_CDB_SIZE) { 3489c19a7d0SMike Christie rc = iscsi_prep_ecdb_ahs(task); 34938d1c069SBoaz Harrosh if (rc) 35038d1c069SBoaz Harrosh return rc; 35138d1c069SBoaz Harrosh cmd_len = ISCSI_CDB_SIZE; 35238d1c069SBoaz Harrosh } 35338d1c069SBoaz Harrosh memcpy(hdr->cdb, sc->cmnd, cmd_len); 3547996a778SMike Christie 3559c19a7d0SMike Christie task->imm_count = 0; 35655e51edaSSagi Grimberg if (scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) 35755e51edaSSagi Grimberg task->protected = true; 35855e51edaSSagi Grimberg 359d77e6535SSagi Grimberg transfer_length = scsi_transfer_length(sc); 360d77e6535SSagi Grimberg hdr->data_length = cpu_to_be32(transfer_length); 3617996a778SMike Christie if (sc->sc_data_direction == DMA_TO_DEVICE) { 362577577daSMike Christie struct iscsi_r2t_info *r2t = &task->unsol_r2t; 363577577daSMike Christie 3647996a778SMike Christie hdr->flags |= ISCSI_FLAG_CMD_WRITE; 3657996a778SMike Christie /* 3667996a778SMike Christie * Write counters: 3677996a778SMike Christie * 3687996a778SMike Christie * imm_count bytes to be sent right after 3697996a778SMike Christie * SCSI PDU Header 3707996a778SMike Christie * 3717996a778SMike Christie * unsol_count bytes(as Data-Out) to be sent 3727996a778SMike Christie * without R2T ack right after 3737996a778SMike Christie * immediate data 3747996a778SMike Christie * 375577577daSMike Christie * r2t data_length bytes to be sent via R2T ack's 3767996a778SMike Christie * 3777996a778SMike Christie * pad_count bytes to be sent as zero-padding 3787996a778SMike Christie */ 379577577daSMike Christie memset(r2t, 0, sizeof(*r2t)); 3807996a778SMike Christie 3817996a778SMike Christie if (session->imm_data_en) { 382d77e6535SSagi Grimberg if (transfer_length >= session->first_burst) 3839c19a7d0SMike Christie task->imm_count = min(session->first_burst, 3847996a778SMike Christie conn->max_xmit_dlength); 3857996a778SMike Christie else 386d77e6535SSagi Grimberg task->imm_count = min(transfer_length, 3877996a778SMike Christie conn->max_xmit_dlength); 3889c19a7d0SMike Christie hton24(hdr->dlength, task->imm_count); 3897996a778SMike Christie } else 390a8ac6311SOlaf Kirch zero_data(hdr->dlength); 3917996a778SMike Christie 392ffd0436eSMike Christie if (!session->initial_r2t_en) { 393d77e6535SSagi Grimberg r2t->data_length = min(session->first_burst, 394d77e6535SSagi Grimberg transfer_length) - 395577577daSMike Christie task->imm_count; 396577577daSMike Christie r2t->data_offset = task->imm_count; 397577577daSMike Christie r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG); 398577577daSMike Christie r2t->exp_statsn = cpu_to_be32(conn->exp_statsn); 399ffd0436eSMike Christie } 400ffd0436eSMike Christie 401577577daSMike Christie if (!task->unsol_r2t.data_length) 4027996a778SMike Christie /* No unsolicit Data-Out's */ 403a8ac6311SOlaf Kirch hdr->flags |= ISCSI_FLAG_CMD_FINAL; 4047996a778SMike Christie } else { 4057996a778SMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 4067996a778SMike Christie zero_data(hdr->dlength); 4077996a778SMike Christie 4087996a778SMike Christie if (sc->sc_data_direction == DMA_FROM_DEVICE) 4097996a778SMike Christie hdr->flags |= ISCSI_FLAG_CMD_READ; 4107996a778SMike Christie } 4117996a778SMike Christie 412004d6530SBoaz Harrosh /* calculate size of additional header segments (AHSs) */ 4139c19a7d0SMike Christie hdrlength = task->hdr_len - sizeof(*hdr); 414004d6530SBoaz Harrosh 415004d6530SBoaz Harrosh WARN_ON(hdrlength & (ISCSI_PAD_LEN-1)); 416004d6530SBoaz Harrosh hdrlength /= ISCSI_PAD_LEN; 417004d6530SBoaz Harrosh 418004d6530SBoaz Harrosh WARN_ON(hdrlength >= 256); 419004d6530SBoaz Harrosh hdr->hlength = hdrlength & 0xFF; 42096b1f96dSMike Christie hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn); 421004d6530SBoaz Harrosh 422577577daSMike Christie if (session->tt->init_task && session->tt->init_task(task)) 423052d0144SMike Christie return -EIO; 424052d0144SMike Christie 4259c19a7d0SMike Christie task->state = ISCSI_TASK_RUNNING; 426d3305f34SMike Christie session->cmdsn++; 42777a23c21SMike Christie 428a8ac6311SOlaf Kirch conn->scsicmd_pdus_cnt++; 4291b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x " 430ae3d56d8SChristoph Hellwig "itt 0x%x len %d cmdsn %d win %d]\n", 4311b2c7af8SMike Christie sc->sc_data_direction == DMA_TO_DEVICE ? 4321b2c7af8SMike Christie "write" : "read", conn->id, sc, sc->cmnd[0], 433d77e6535SSagi Grimberg task->itt, transfer_length, 4341b2c7af8SMike Christie session->cmdsn, 4351b2c7af8SMike Christie session->max_cmdsn - session->exp_cmdsn + 1); 436004d6530SBoaz Harrosh return 0; 4377996a778SMike Christie } 4387996a778SMike Christie 4397996a778SMike Christie /** 4403bbaaad9SMike Christie * iscsi_free_task - free a task 4419c19a7d0SMike Christie * @task: iscsi cmd task 4427996a778SMike Christie * 443659743b0SShlomo Pongratz * Must be called with session back_lock. 4443e5c28adSMike Christie * This function returns the scsi command to scsi-ml or cleans 4453e5c28adSMike Christie * up mgmt tasks then returns the task to the pool. 4467996a778SMike Christie */ 4473bbaaad9SMike Christie static void iscsi_free_task(struct iscsi_task *task) 4487996a778SMike Christie { 4499c19a7d0SMike Christie struct iscsi_conn *conn = task->conn; 450c1635cb7SMike Christie struct iscsi_session *session = conn->session; 4519c19a7d0SMike Christie struct scsi_cmnd *sc = task->sc; 452f41d4721SMike Christie int oldstate = task->state; 4537996a778SMike Christie 4544421c9ebSMike Christie ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n", 4554421c9ebSMike Christie task->itt, task->state, task->sc); 4564421c9ebSMike Christie 457577577daSMike Christie session->tt->cleanup_task(task); 4583bbaaad9SMike Christie task->state = ISCSI_TASK_FREE; 4599c19a7d0SMike Christie task->sc = NULL; 4603e5c28adSMike Christie /* 4619c19a7d0SMike Christie * login task is preallocated so do not free 4623e5c28adSMike Christie */ 4639c19a7d0SMike Christie if (conn->login_task == task) 4643e5c28adSMike Christie return; 4653e5c28adSMike Christie 4667acd72ebSStefani Seibold kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*)); 4673e5c28adSMike Christie 4683e5c28adSMike Christie if (sc) { 469f47f2cf5SMike Christie /* SCSI eh reuses commands to verify us */ 470f47f2cf5SMike Christie sc->SCp.ptr = NULL; 4713e5c28adSMike Christie /* 472f41d4721SMike Christie * queue command may call this to free the task, so 473f41d4721SMike Christie * it will decide how to return sc to scsi-ml. 4743e5c28adSMike Christie */ 475f41d4721SMike Christie if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ) 4767996a778SMike Christie sc->scsi_done(sc); 4777996a778SMike Christie } 4783e5c28adSMike Christie } 4797996a778SMike Christie 480913e5bf4SMike Christie void __iscsi_get_task(struct iscsi_task *task) 48160ecebf5SMike Christie { 4826dc618cdSElena Reshetova refcount_inc(&task->refcount); 48360ecebf5SMike Christie } 484913e5bf4SMike Christie EXPORT_SYMBOL_GPL(__iscsi_get_task); 48560ecebf5SMike Christie 4868eea2f55SEddie Wai void __iscsi_put_task(struct iscsi_task *task) 48760ecebf5SMike Christie { 4886dc618cdSElena Reshetova if (refcount_dec_and_test(&task->refcount)) 4893bbaaad9SMike Christie iscsi_free_task(task); 49060ecebf5SMike Christie } 4918eea2f55SEddie Wai EXPORT_SYMBOL_GPL(__iscsi_put_task); 49260ecebf5SMike Christie 4939c19a7d0SMike Christie void iscsi_put_task(struct iscsi_task *task) 4943e5c28adSMike Christie { 4959c19a7d0SMike Christie struct iscsi_session *session = task->conn->session; 4963e5c28adSMike Christie 497659743b0SShlomo Pongratz /* regular RX path uses back_lock */ 498659743b0SShlomo Pongratz spin_lock_bh(&session->back_lock); 4999c19a7d0SMike Christie __iscsi_put_task(task); 500659743b0SShlomo Pongratz spin_unlock_bh(&session->back_lock); 5013e5c28adSMike Christie } 5029c19a7d0SMike Christie EXPORT_SYMBOL_GPL(iscsi_put_task); 5033e5c28adSMike Christie 5043bbaaad9SMike Christie /** 5053bbaaad9SMike Christie * iscsi_complete_task - finish a task 5063bbaaad9SMike Christie * @task: iscsi cmd task 507b3cd5050SMike Christie * @state: state to complete task with 5083bbaaad9SMike Christie * 509659743b0SShlomo Pongratz * Must be called with session back_lock. 510b3a7ea8dSMike Christie */ 511b3cd5050SMike Christie static void iscsi_complete_task(struct iscsi_task *task, int state) 512b3a7ea8dSMike Christie { 5133bbaaad9SMike Christie struct iscsi_conn *conn = task->conn; 5143bbaaad9SMike Christie 5154421c9ebSMike Christie ISCSI_DBG_SESSION(conn->session, 5164421c9ebSMike Christie "complete task itt 0x%x state %d sc %p\n", 5174421c9ebSMike Christie task->itt, task->state, task->sc); 518b3cd5050SMike Christie if (task->state == ISCSI_TASK_COMPLETED || 519b3cd5050SMike Christie task->state == ISCSI_TASK_ABRT_TMF || 520f41d4721SMike Christie task->state == ISCSI_TASK_ABRT_SESS_RECOV || 521f41d4721SMike Christie task->state == ISCSI_TASK_REQUEUE_SCSIQ) 5223bbaaad9SMike Christie return; 5233bbaaad9SMike Christie WARN_ON_ONCE(task->state == ISCSI_TASK_FREE); 524b3cd5050SMike Christie task->state = state; 5253bbaaad9SMike Christie 526fe0a8a95SLee Duncan if (READ_ONCE(conn->ping_task) == task) 527fe0a8a95SLee Duncan WRITE_ONCE(conn->ping_task, NULL); 5283bbaaad9SMike Christie 5293bbaaad9SMike Christie /* release get from queueing */ 5303bbaaad9SMike Christie __iscsi_put_task(task); 5313bbaaad9SMike Christie } 5323bbaaad9SMike Christie 5334c0ba5d2SMike Christie /** 5344c0ba5d2SMike Christie * iscsi_complete_scsi_task - finish scsi task normally 5354c0ba5d2SMike Christie * @task: iscsi task for scsi cmd 5364c0ba5d2SMike Christie * @exp_cmdsn: expected cmd sn in cpu format 5374c0ba5d2SMike Christie * @max_cmdsn: max cmd sn in cpu format 5384c0ba5d2SMike Christie * 5394c0ba5d2SMike Christie * This is used when drivers do not need or cannot perform 5404c0ba5d2SMike Christie * lower level pdu processing. 5414c0ba5d2SMike Christie * 542659743b0SShlomo Pongratz * Called with session back_lock 5434c0ba5d2SMike Christie */ 5444c0ba5d2SMike Christie void iscsi_complete_scsi_task(struct iscsi_task *task, 5454c0ba5d2SMike Christie uint32_t exp_cmdsn, uint32_t max_cmdsn) 5464c0ba5d2SMike Christie { 5474c0ba5d2SMike Christie struct iscsi_conn *conn = task->conn; 5484c0ba5d2SMike Christie 5494c0ba5d2SMike Christie ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt); 5504c0ba5d2SMike Christie 5514c0ba5d2SMike Christie conn->last_recv = jiffies; 5524c0ba5d2SMike Christie __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn); 5534c0ba5d2SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 5544c0ba5d2SMike Christie } 5554c0ba5d2SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task); 5564c0ba5d2SMike Christie 5575923d64bSMike Christie /* 5585923d64bSMike Christie * Must be called with back and frwd lock 5595923d64bSMike Christie */ 5605923d64bSMike Christie static bool cleanup_queued_task(struct iscsi_task *task) 5615923d64bSMike Christie { 5625923d64bSMike Christie struct iscsi_conn *conn = task->conn; 5635923d64bSMike Christie bool early_complete = false; 5645923d64bSMike Christie 5655923d64bSMike Christie /* Bad target might have completed task while it was still running */ 5665923d64bSMike Christie if (task->state == ISCSI_TASK_COMPLETED) 5675923d64bSMike Christie early_complete = true; 5685923d64bSMike Christie 5695923d64bSMike Christie if (!list_empty(&task->running)) { 5705923d64bSMike Christie list_del_init(&task->running); 5715923d64bSMike Christie /* 5725923d64bSMike Christie * If it's on a list but still running, this could be from 5735923d64bSMike Christie * a bad target sending a rsp early, cleanup from a TMF, or 5745923d64bSMike Christie * session recovery. 5755923d64bSMike Christie */ 5765923d64bSMike Christie if (task->state == ISCSI_TASK_RUNNING || 5775923d64bSMike Christie task->state == ISCSI_TASK_COMPLETED) 5785923d64bSMike Christie __iscsi_put_task(task); 5795923d64bSMike Christie } 5805923d64bSMike Christie 5815923d64bSMike Christie if (conn->task == task) { 5825923d64bSMike Christie conn->task = NULL; 5835923d64bSMike Christie __iscsi_put_task(task); 5845923d64bSMike Christie } 5855923d64bSMike Christie 5865923d64bSMike Christie return early_complete; 5875923d64bSMike Christie } 5884c0ba5d2SMike Christie 5893bbaaad9SMike Christie /* 59014936b1eSMike Christie * session frwd lock must be held and if not called for a task that is still 59114936b1eSMike Christie * pending or from the xmit thread, then xmit thread must be suspended 5923bbaaad9SMike Christie */ 5933bbaaad9SMike Christie static void fail_scsi_task(struct iscsi_task *task, int err) 5943bbaaad9SMike Christie { 5953bbaaad9SMike Christie struct iscsi_conn *conn = task->conn; 596b3a7ea8dSMike Christie struct scsi_cmnd *sc; 597b3cd5050SMike Christie int state; 598b3a7ea8dSMike Christie 5995923d64bSMike Christie spin_lock_bh(&conn->session->back_lock); 6005923d64bSMike Christie if (cleanup_queued_task(task)) { 6015923d64bSMike Christie spin_unlock_bh(&conn->session->back_lock); 6025923d64bSMike Christie return; 6035923d64bSMike Christie } 6045923d64bSMike Christie 605b3cd5050SMike Christie if (task->state == ISCSI_TASK_PENDING) { 606b3a7ea8dSMike Christie /* 607b3a7ea8dSMike Christie * cmd never made it to the xmit thread, so we should not count 608b3a7ea8dSMike Christie * the cmd in the sequencing 609b3a7ea8dSMike Christie */ 610b3a7ea8dSMike Christie conn->session->queued_cmdsn--; 611b3cd5050SMike Christie /* it was never sent so just complete like normal */ 612b3cd5050SMike Christie state = ISCSI_TASK_COMPLETED; 613b3cd5050SMike Christie } else if (err == DID_TRANSPORT_DISRUPTED) 614b3cd5050SMike Christie state = ISCSI_TASK_ABRT_SESS_RECOV; 615b3cd5050SMike Christie else 616b3cd5050SMike Christie state = ISCSI_TASK_ABRT_TMF; 617b3a7ea8dSMike Christie 61814936b1eSMike Christie sc = task->sc; 619b3cd5050SMike Christie sc->result = err << 16; 620b3a7ea8dSMike Christie scsi_set_resid(sc, scsi_bufflen(sc)); 621b3cd5050SMike Christie iscsi_complete_task(task, state); 622659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->back_lock); 623b3a7ea8dSMike Christie } 624b3a7ea8dSMike Christie 6253e5c28adSMike Christie static int iscsi_prep_mgmt_task(struct iscsi_conn *conn, 6269c19a7d0SMike Christie struct iscsi_task *task) 627052d0144SMike Christie { 628052d0144SMike Christie struct iscsi_session *session = conn->session; 629577577daSMike Christie struct iscsi_hdr *hdr = task->hdr; 630052d0144SMike Christie struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr; 6314f704dc0SMike Christie uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK; 632052d0144SMike Christie 633052d0144SMike Christie if (conn->session->state == ISCSI_STATE_LOGGING_OUT) 634052d0144SMike Christie return -ENOTCONN; 635052d0144SMike Christie 6364f704dc0SMike Christie if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT) 637052d0144SMike Christie nop->exp_statsn = cpu_to_be32(conn->exp_statsn); 638052d0144SMike Christie /* 639052d0144SMike Christie * pre-format CmdSN for outgoing PDU. 640052d0144SMike Christie */ 641052d0144SMike Christie nop->cmdsn = cpu_to_be32(session->cmdsn); 642052d0144SMike Christie if (hdr->itt != RESERVED_ITT) { 643052d0144SMike Christie /* 6444f704dc0SMike Christie * TODO: We always use immediate for normal session pdus. 645052d0144SMike Christie * If we start to send tmfs or nops as non-immediate then 646052d0144SMike Christie * we should start checking the cmdsn numbers for mgmt tasks. 6474f704dc0SMike Christie * 6484f704dc0SMike Christie * During discovery sessions iscsid sends TEXT as non immediate, 6494f704dc0SMike Christie * but we always only send one PDU at a time. 650052d0144SMike Christie */ 651052d0144SMike Christie if (conn->c_stage == ISCSI_CONN_STARTED && 652052d0144SMike Christie !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { 653052d0144SMike Christie session->queued_cmdsn++; 654052d0144SMike Christie session->cmdsn++; 655052d0144SMike Christie } 656052d0144SMike Christie } 657052d0144SMike Christie 658ae15f801SMike Christie if (session->tt->init_task && session->tt->init_task(task)) 659ae15f801SMike Christie return -EIO; 660052d0144SMike Christie 661052d0144SMike Christie if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) 662052d0144SMike Christie session->state = ISCSI_STATE_LOGGING_OUT; 663052d0144SMike Christie 664577577daSMike Christie task->state = ISCSI_TASK_RUNNING; 6651b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x " 6661b2c7af8SMike Christie "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK, 6671b2c7af8SMike Christie hdr->itt, task->data_count); 668052d0144SMike Christie return 0; 669052d0144SMike Christie } 670052d0144SMike Christie 6719c19a7d0SMike Christie static struct iscsi_task * 672f6d5180cSMike Christie __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 673f6d5180cSMike Christie char *data, uint32_t data_size) 674f6d5180cSMike Christie { 675f6d5180cSMike Christie struct iscsi_session *session = conn->session; 6761336aed1SMike Christie struct iscsi_host *ihost = shost_priv(session->host); 6774f704dc0SMike Christie uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK; 6789c19a7d0SMike Christie struct iscsi_task *task; 679262ef636SMike Christie itt_t itt; 680f6d5180cSMike Christie 681f6d5180cSMike Christie if (session->state == ISCSI_STATE_TERMINATE) 682f6d5180cSMike Christie return NULL; 683f6d5180cSMike Christie 6844f704dc0SMike Christie if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) { 685f6d5180cSMike Christie /* 686f6d5180cSMike Christie * Login and Text are sent serially, in 687f6d5180cSMike Christie * request-followed-by-response sequence. 6883e5c28adSMike Christie * Same task can be used. Same ITT must be used. 6893e5c28adSMike Christie * Note that login_task is preallocated at conn_create(). 690f6d5180cSMike Christie */ 6914f704dc0SMike Christie if (conn->login_task->state != ISCSI_TASK_FREE) { 6924f704dc0SMike Christie iscsi_conn_printk(KERN_ERR, conn, "Login/Text in " 6934f704dc0SMike Christie "progress. Cannot start new task.\n"); 6944f704dc0SMike Christie return NULL; 6954f704dc0SMike Christie } 6964f704dc0SMike Christie 697cbaa4221SMike Christie if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) { 698cbaa4221SMike 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); 699cbaa4221SMike Christie return NULL; 700cbaa4221SMike Christie } 701cbaa4221SMike Christie 7029c19a7d0SMike Christie task = conn->login_task; 7034f704dc0SMike Christie } else { 70426013ad4SMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) 70526013ad4SMike Christie return NULL; 70626013ad4SMike Christie 707cbaa4221SMike Christie if (data_size != 0) { 708cbaa4221SMike Christie iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode); 709cbaa4221SMike Christie return NULL; 710cbaa4221SMike Christie } 711cbaa4221SMike Christie 712f6d5180cSMike Christie BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); 713f6d5180cSMike Christie BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); 714f6d5180cSMike Christie 7157acd72ebSStefani Seibold if (!kfifo_out(&session->cmdpool.queue, 7169c19a7d0SMike Christie (void*)&task, sizeof(void*))) 717f6d5180cSMike Christie return NULL; 718f6d5180cSMike Christie } 7193e5c28adSMike Christie /* 7203e5c28adSMike Christie * released in complete pdu for task we expect a response for, and 7213e5c28adSMike Christie * released by the lld when it has transmitted the task for 7223e5c28adSMike Christie * pdus we do not expect a response for. 7233e5c28adSMike Christie */ 7246dc618cdSElena Reshetova refcount_set(&task->refcount, 1); 7259c19a7d0SMike Christie task->conn = conn; 7269c19a7d0SMike Christie task->sc = NULL; 7273bbaaad9SMike Christie INIT_LIST_HEAD(&task->running); 7283bbaaad9SMike Christie task->state = ISCSI_TASK_PENDING; 729f6d5180cSMike Christie 730f6d5180cSMike Christie if (data_size) { 7319c19a7d0SMike Christie memcpy(task->data, data, data_size); 7329c19a7d0SMike Christie task->data_count = data_size; 733f6d5180cSMike Christie } else 7349c19a7d0SMike Christie task->data_count = 0; 735f6d5180cSMike Christie 736184b57c6SMike Christie if (conn->session->tt->alloc_pdu) { 7372ff79d52SMike Christie if (conn->session->tt->alloc_pdu(task, hdr->opcode)) { 738577577daSMike Christie iscsi_conn_printk(KERN_ERR, conn, "Could not allocate " 739577577daSMike Christie "pdu for mgmt task.\n"); 7403bbaaad9SMike Christie goto free_task; 741577577daSMike Christie } 742184b57c6SMike Christie } 743184b57c6SMike Christie 744262ef636SMike Christie itt = task->hdr->itt; 745577577daSMike Christie task->hdr_len = sizeof(struct iscsi_hdr); 7469c19a7d0SMike Christie memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr)); 747262ef636SMike Christie 748262ef636SMike Christie if (hdr->itt != RESERVED_ITT) { 749262ef636SMike Christie if (session->tt->parse_pdu_itt) 750262ef636SMike Christie task->hdr->itt = itt; 751262ef636SMike Christie else 752262ef636SMike Christie task->hdr->itt = build_itt(task->itt, 753262ef636SMike Christie task->conn->session->age); 754262ef636SMike Christie } 755262ef636SMike Christie 756fe0a8a95SLee Duncan if (unlikely(READ_ONCE(conn->ping_task) == INVALID_SCSI_TASK)) 757fe0a8a95SLee Duncan WRITE_ONCE(conn->ping_task, task); 758fe0a8a95SLee Duncan 7591336aed1SMike Christie if (!ihost->workq) { 760577577daSMike Christie if (iscsi_prep_mgmt_task(conn, task)) 761577577daSMike Christie goto free_task; 762052d0144SMike Christie 7639c19a7d0SMike Christie if (session->tt->xmit_task(task)) 764577577daSMike Christie goto free_task; 7653bbaaad9SMike Christie } else { 7663bbaaad9SMike Christie list_add_tail(&task->running, &conn->mgmtqueue); 76732ae763eSMike Christie iscsi_conn_queue_work(conn); 7683bbaaad9SMike Christie } 769052d0144SMike Christie 7709c19a7d0SMike Christie return task; 771577577daSMike Christie 772577577daSMike Christie free_task: 773659743b0SShlomo Pongratz /* regular RX path uses back_lock */ 7744fa50799SJitendra Bhivare spin_lock(&session->back_lock); 775577577daSMike Christie __iscsi_put_task(task); 7764fa50799SJitendra Bhivare spin_unlock(&session->back_lock); 777577577daSMike Christie return NULL; 778f6d5180cSMike Christie } 779f6d5180cSMike Christie 780f6d5180cSMike Christie int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr, 781f6d5180cSMike Christie char *data, uint32_t data_size) 782f6d5180cSMike Christie { 783f6d5180cSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 784f6d5180cSMike Christie struct iscsi_session *session = conn->session; 785f6d5180cSMike Christie int err = 0; 786f6d5180cSMike Christie 787659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 788f6d5180cSMike Christie if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size)) 789f6d5180cSMike Christie err = -EPERM; 790659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 791f6d5180cSMike Christie return err; 792f6d5180cSMike Christie } 793f6d5180cSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu); 794f6d5180cSMike Christie 7957996a778SMike Christie /** 796ae6b4e69SMauro Carvalho Chehab * iscsi_scsi_cmd_rsp - SCSI Command Response processing 7977996a778SMike Christie * @conn: iscsi connection 7987996a778SMike Christie * @hdr: iscsi header 7999c19a7d0SMike Christie * @task: scsi command task 8007996a778SMike Christie * @data: cmd data buffer 8017996a778SMike Christie * @datalen: len of buffer 8027996a778SMike Christie * 8037996a778SMike Christie * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and 804a656183eSLee Duncan * then completes the command and task. called under back_lock 8057996a778SMike Christie **/ 80677a23c21SMike Christie static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 8079c19a7d0SMike Christie struct iscsi_task *task, char *data, 8087996a778SMike Christie int datalen) 8097996a778SMike Christie { 81012352183SNicholas Bellinger struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr; 8117996a778SMike Christie struct iscsi_session *session = conn->session; 8129c19a7d0SMike Christie struct scsi_cmnd *sc = task->sc; 8137996a778SMike Christie 81477a23c21SMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); 8157996a778SMike Christie conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; 8167996a778SMike Christie 8177996a778SMike Christie sc->result = (DID_OK << 16) | rhdr->cmd_status; 8187996a778SMike Christie 81955e51edaSSagi Grimberg if (task->protected) { 82055e51edaSSagi Grimberg sector_t sector; 82155e51edaSSagi Grimberg u8 ascq; 82255e51edaSSagi Grimberg 82355e51edaSSagi Grimberg /** 82455e51edaSSagi Grimberg * Transports that didn't implement check_protection 82555e51edaSSagi Grimberg * callback but still published T10-PI support to scsi-mid 82655e51edaSSagi Grimberg * deserve this BUG_ON. 82755e51edaSSagi Grimberg **/ 82855e51edaSSagi Grimberg BUG_ON(!session->tt->check_protection); 82955e51edaSSagi Grimberg 83055e51edaSSagi Grimberg ascq = session->tt->check_protection(task, §or); 83155e51edaSSagi Grimberg if (ascq) { 832*f2b1e9c6SHannes Reinecke scsi_build_sense(sc, 1, ILLEGAL_REQUEST, 0x10, ascq); 833a73c2a2fSSagi Grimberg scsi_set_sense_information(sc->sense_buffer, 834a73c2a2fSSagi Grimberg SCSI_SENSE_BUFFERSIZE, 835a73c2a2fSSagi Grimberg sector); 83655e51edaSSagi Grimberg goto out; 83755e51edaSSagi Grimberg } 83855e51edaSSagi Grimberg } 83955e51edaSSagi Grimberg 8407996a778SMike Christie if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) { 8417996a778SMike Christie sc->result = DID_ERROR << 16; 8427996a778SMike Christie goto out; 8437996a778SMike Christie } 8447996a778SMike Christie 8457996a778SMike Christie if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) { 8469b80cb4bSMike Christie uint16_t senselen; 8477996a778SMike Christie 8487996a778SMike Christie if (datalen < 2) { 8497996a778SMike Christie invalid_datalen: 850322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 851322d739dSMike Christie "Got CHECK_CONDITION but invalid data " 852322d739dSMike Christie "buffer size of %d\n", datalen); 8537996a778SMike Christie sc->result = DID_BAD_TARGET << 16; 8547996a778SMike Christie goto out; 8557996a778SMike Christie } 8567996a778SMike Christie 8578f333991SHarvey Harrison senselen = get_unaligned_be16(data); 8587996a778SMike Christie if (datalen < senselen) 8597996a778SMike Christie goto invalid_datalen; 8607996a778SMike Christie 8617996a778SMike Christie memcpy(sc->sense_buffer, data + 2, 8629b80cb4bSMike Christie min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE)); 8631b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n", 8641b2c7af8SMike Christie min_t(uint16_t, senselen, 8651b2c7af8SMike Christie SCSI_SENSE_BUFFERSIZE)); 8667996a778SMike Christie } 8677996a778SMike Christie 868c07d4444SBoaz Harrosh if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW | 869c07d4444SBoaz Harrosh ISCSI_FLAG_CMD_BIDI_OVERFLOW)) { 870c07d4444SBoaz Harrosh sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 871c07d4444SBoaz Harrosh } 872c07d4444SBoaz Harrosh 8737207fea4SBoaz Harrosh if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW | 8747207fea4SBoaz Harrosh ISCSI_FLAG_CMD_OVERFLOW)) { 8757996a778SMike Christie int res_count = be32_to_cpu(rhdr->residual_count); 8767996a778SMike Christie 8777207fea4SBoaz Harrosh if (res_count > 0 && 8787207fea4SBoaz Harrosh (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || 8797207fea4SBoaz Harrosh res_count <= scsi_bufflen(sc))) 880c07d4444SBoaz Harrosh /* write side for bidi or uni-io set_resid */ 8811c138991SFUJITA Tomonori scsi_set_resid(sc, res_count); 8827996a778SMike Christie else 8837996a778SMike Christie sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 884c07d4444SBoaz Harrosh } 8857996a778SMike Christie out: 8863bbaaad9SMike Christie ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n", 8871b2c7af8SMike Christie sc, sc->result, task->itt); 8887996a778SMike Christie conn->scsirsp_pdus_cnt++; 889b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 8907996a778SMike Christie } 8917996a778SMike Christie 8921d9edf02SMike Christie /** 8931d9edf02SMike Christie * iscsi_data_in_rsp - SCSI Data-In Response processing 8941d9edf02SMike Christie * @conn: iscsi connection 8951d9edf02SMike Christie * @hdr: iscsi pdu 8961d9edf02SMike Christie * @task: scsi command task 897a656183eSLee Duncan * 898a656183eSLee Duncan * iscsi_data_in_rsp sets up the scsi_cmnd fields based on the data received 899a656183eSLee Duncan * then completes the command and task. called under back_lock 9001d9edf02SMike Christie **/ 9011d9edf02SMike Christie static void 9021d9edf02SMike Christie iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 9031d9edf02SMike Christie struct iscsi_task *task) 9041d9edf02SMike Christie { 9051d9edf02SMike Christie struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr; 9061d9edf02SMike Christie struct scsi_cmnd *sc = task->sc; 9071d9edf02SMike Christie 9081d9edf02SMike Christie if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS)) 9091d9edf02SMike Christie return; 9101d9edf02SMike Christie 911edbc9aa0SMike Christie iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr); 9121d9edf02SMike Christie sc->result = (DID_OK << 16) | rhdr->cmd_status; 9131d9edf02SMike Christie conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; 9141d9edf02SMike Christie if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW | 9151d9edf02SMike Christie ISCSI_FLAG_DATA_OVERFLOW)) { 9161d9edf02SMike Christie int res_count = be32_to_cpu(rhdr->residual_count); 9171d9edf02SMike Christie 9181d9edf02SMike Christie if (res_count > 0 && 9191d9edf02SMike Christie (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || 920ae3d56d8SChristoph Hellwig res_count <= sc->sdb.length)) 921960bf87aSBart Van Assche scsi_set_resid(sc, res_count); 9221d9edf02SMike Christie else 9231d9edf02SMike Christie sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 9241d9edf02SMike Christie } 9251d9edf02SMike Christie 9263bbaaad9SMike Christie ISCSI_DBG_SESSION(conn->session, "data in with status done " 9273bbaaad9SMike Christie "[sc %p res %d itt 0x%x]\n", 9283bbaaad9SMike Christie sc, sc->result, task->itt); 9291d9edf02SMike Christie conn->scsirsp_pdus_cnt++; 930b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 9311d9edf02SMike Christie } 9321d9edf02SMike Christie 9337ea8b828SMike Christie static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr) 9347ea8b828SMike Christie { 9357ea8b828SMike Christie struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr; 9367ea8b828SMike Christie 9377ea8b828SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 9387ea8b828SMike Christie conn->tmfrsp_pdus_cnt++; 9397ea8b828SMike Christie 940843c0a8aSMike Christie if (conn->tmf_state != TMF_QUEUED) 9417ea8b828SMike Christie return; 9427ea8b828SMike Christie 9437ea8b828SMike Christie if (tmf->response == ISCSI_TMF_RSP_COMPLETE) 944843c0a8aSMike Christie conn->tmf_state = TMF_SUCCESS; 9457ea8b828SMike Christie else if (tmf->response == ISCSI_TMF_RSP_NO_TASK) 946843c0a8aSMike Christie conn->tmf_state = TMF_NOT_FOUND; 9477ea8b828SMike Christie else 948843c0a8aSMike Christie conn->tmf_state = TMF_FAILED; 9497ea8b828SMike Christie wake_up(&conn->ehwait); 9507ea8b828SMike Christie } 9517ea8b828SMike Christie 95252f5664aSAriel Nahum static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr) 953f6d5180cSMike Christie { 954f6d5180cSMike Christie struct iscsi_nopout hdr; 9559c19a7d0SMike Christie struct iscsi_task *task; 956f6d5180cSMike Christie 957fe0a8a95SLee Duncan if (!rhdr) { 958fe0a8a95SLee Duncan if (READ_ONCE(conn->ping_task)) 95952f5664aSAriel Nahum return -EINVAL; 960fe0a8a95SLee Duncan WRITE_ONCE(conn->ping_task, INVALID_SCSI_TASK); 961fe0a8a95SLee Duncan } 962f6d5180cSMike Christie 963f6d5180cSMike Christie memset(&hdr, 0, sizeof(struct iscsi_nopout)); 964f6d5180cSMike Christie hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE; 965f6d5180cSMike Christie hdr.flags = ISCSI_FLAG_CMD_FINAL; 966f6d5180cSMike Christie 967f6d5180cSMike Christie if (rhdr) { 96855bdabdfSAndy Grover hdr.lun = rhdr->lun; 969f6d5180cSMike Christie hdr.ttt = rhdr->ttt; 970f6d5180cSMike Christie hdr.itt = RESERVED_ITT; 971f6d5180cSMike Christie } else 972f6d5180cSMike Christie hdr.ttt = RESERVED_ITT; 973f6d5180cSMike Christie 9749c19a7d0SMike Christie task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0); 97552f5664aSAriel Nahum if (!task) { 976fe0a8a95SLee Duncan if (!rhdr) 977fe0a8a95SLee Duncan WRITE_ONCE(conn->ping_task, NULL); 978322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n"); 97952f5664aSAriel Nahum return -EIO; 98052f5664aSAriel Nahum } else if (!rhdr) { 981d3acf022SMike Christie /* only track our nops */ 982d3acf022SMike Christie conn->last_ping = jiffies; 983d3acf022SMike Christie } 98452f5664aSAriel Nahum 98552f5664aSAriel Nahum return 0; 986f6d5180cSMike Christie } 987f6d5180cSMike Christie 988a656183eSLee Duncan /** 989a656183eSLee Duncan * iscsi_nop_out_rsp - SCSI NOP Response processing 990a656183eSLee Duncan * @task: scsi command task 991a656183eSLee Duncan * @nop: the nop structure 992a656183eSLee Duncan * @data: where to put the data 993a656183eSLee Duncan * @datalen: length of data 994a656183eSLee Duncan * 995a656183eSLee Duncan * iscsi_nop_out_rsp handles nop response from use or 996a656183eSLee Duncan * from user space. called under back_lock 997a656183eSLee Duncan **/ 9988afa1439SMike Christie static int iscsi_nop_out_rsp(struct iscsi_task *task, 9998afa1439SMike Christie struct iscsi_nopin *nop, char *data, int datalen) 10008afa1439SMike Christie { 10018afa1439SMike Christie struct iscsi_conn *conn = task->conn; 10028afa1439SMike Christie int rc = 0; 10038afa1439SMike Christie 1004fe0a8a95SLee Duncan if (READ_ONCE(conn->ping_task) != task) { 10058afa1439SMike Christie /* 10068afa1439SMike Christie * If this is not in response to one of our 10078afa1439SMike Christie * nops then it must be from userspace. 10088afa1439SMike Christie */ 10098afa1439SMike Christie if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop, 10108afa1439SMike Christie data, datalen)) 10118afa1439SMike Christie rc = ISCSI_ERR_CONN_FAILED; 10128afa1439SMike Christie } else 10138afa1439SMike Christie mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout); 10148afa1439SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 10158afa1439SMike Christie return rc; 10168afa1439SMike Christie } 10178afa1439SMike Christie 101862f38300SMike Christie static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 101962f38300SMike Christie char *data, int datalen) 102062f38300SMike Christie { 102162f38300SMike Christie struct iscsi_reject *reject = (struct iscsi_reject *)hdr; 102262f38300SMike Christie struct iscsi_hdr rejected_pdu; 10238afa1439SMike Christie int opcode, rc = 0; 102462f38300SMike Christie 102562f38300SMike Christie conn->exp_statsn = be32_to_cpu(reject->statsn) + 1; 102662f38300SMike Christie 10278afa1439SMike Christie if (ntoh24(reject->dlength) > datalen || 10288afa1439SMike Christie ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) { 10298afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected " 10308afa1439SMike Christie "pdu. Invalid data length (pdu dlength " 10318afa1439SMike Christie "%u, datalen %d\n", ntoh24(reject->dlength), 10328afa1439SMike Christie datalen); 103362f38300SMike Christie return ISCSI_ERR_PROTO; 10348afa1439SMike Christie } 103562f38300SMike Christie memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr)); 10368afa1439SMike Christie opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK; 10378afa1439SMike Christie 10388afa1439SMike Christie switch (reject->reason) { 10398afa1439SMike Christie case ISCSI_REASON_DATA_DIGEST_ERROR: 1040322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 10418afa1439SMike Christie "pdu (op 0x%x itt 0x%x) rejected " 1042262ef636SMike Christie "due to DataDigest error.\n", 1043e5dbbe27SVaughan Cao opcode, rejected_pdu.itt); 10448afa1439SMike Christie break; 10458afa1439SMike Christie case ISCSI_REASON_IMM_CMD_REJECT: 10468afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, 10478afa1439SMike Christie "pdu (op 0x%x itt 0x%x) rejected. Too many " 10488afa1439SMike Christie "immediate commands.\n", 1049e5dbbe27SVaughan Cao opcode, rejected_pdu.itt); 10508afa1439SMike Christie /* 10518afa1439SMike Christie * We only send one TMF at a time so if the target could not 10528afa1439SMike Christie * handle it, then it should get fixed (RFC mandates that 10538afa1439SMike Christie * a target can handle one immediate TMF per conn). 10548afa1439SMike Christie * 10558afa1439SMike Christie * For nops-outs, we could have sent more than one if 10568afa1439SMike Christie * the target is sending us lots of nop-ins 10578afa1439SMike Christie */ 10588afa1439SMike Christie if (opcode != ISCSI_OP_NOOP_OUT) 105962f38300SMike Christie return 0; 10608afa1439SMike Christie 1061659743b0SShlomo Pongratz if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) { 10628afa1439SMike Christie /* 10638afa1439SMike Christie * nop-out in response to target's nop-out rejected. 10648afa1439SMike Christie * Just resend. 10658afa1439SMike Christie */ 1066659743b0SShlomo Pongratz /* In RX path we are under back lock */ 1067659743b0SShlomo Pongratz spin_unlock(&conn->session->back_lock); 1068659743b0SShlomo Pongratz spin_lock(&conn->session->frwd_lock); 10698afa1439SMike Christie iscsi_send_nopout(conn, 10708afa1439SMike Christie (struct iscsi_nopin*)&rejected_pdu); 1071659743b0SShlomo Pongratz spin_unlock(&conn->session->frwd_lock); 1072659743b0SShlomo Pongratz spin_lock(&conn->session->back_lock); 1073659743b0SShlomo Pongratz } else { 10748afa1439SMike Christie struct iscsi_task *task; 10758afa1439SMike Christie /* 10768afa1439SMike Christie * Our nop as ping got dropped. We know the target 10778afa1439SMike Christie * and transport are ok so just clean up 10788afa1439SMike Christie */ 10798afa1439SMike Christie task = iscsi_itt_to_task(conn, rejected_pdu.itt); 10808afa1439SMike Christie if (!task) { 10818afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, 10828afa1439SMike Christie "Invalid pdu reject. Could " 10838afa1439SMike Christie "not lookup rejected task.\n"); 10848afa1439SMike Christie rc = ISCSI_ERR_BAD_ITT; 10858afa1439SMike Christie } else 10868afa1439SMike Christie rc = iscsi_nop_out_rsp(task, 10878afa1439SMike Christie (struct iscsi_nopin*)&rejected_pdu, 10888afa1439SMike Christie NULL, 0); 10898afa1439SMike Christie } 10908afa1439SMike Christie break; 10918afa1439SMike Christie default: 10928afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, 10938afa1439SMike Christie "pdu (op 0x%x itt 0x%x) rejected. Reason " 1094e5dbbe27SVaughan Cao "code 0x%x\n", rejected_pdu.opcode, 1095e5dbbe27SVaughan Cao rejected_pdu.itt, reject->reason); 10968afa1439SMike Christie break; 10978afa1439SMike Christie } 10988afa1439SMike Christie return rc; 109962f38300SMike Christie } 110062f38300SMike Christie 11017996a778SMike Christie /** 1102913e5bf4SMike Christie * iscsi_itt_to_task - look up task by itt 1103913e5bf4SMike Christie * @conn: iscsi connection 1104913e5bf4SMike Christie * @itt: itt 1105913e5bf4SMike Christie * 1106913e5bf4SMike Christie * This should be used for mgmt tasks like login and nops, or if 1107913e5bf4SMike Christie * the LDD's itt space does not include the session age. 1108913e5bf4SMike Christie * 1109659743b0SShlomo Pongratz * The session back_lock must be held. 1110913e5bf4SMike Christie */ 11118f9256ceSMike Christie struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt) 1112913e5bf4SMike Christie { 1113913e5bf4SMike Christie struct iscsi_session *session = conn->session; 1114262ef636SMike Christie int i; 1115913e5bf4SMike Christie 1116913e5bf4SMike Christie if (itt == RESERVED_ITT) 1117913e5bf4SMike Christie return NULL; 1118913e5bf4SMike Christie 1119262ef636SMike Christie if (session->tt->parse_pdu_itt) 1120262ef636SMike Christie session->tt->parse_pdu_itt(conn, itt, &i, NULL); 1121262ef636SMike Christie else 1122913e5bf4SMike Christie i = get_itt(itt); 1123913e5bf4SMike Christie if (i >= session->cmds_max) 1124913e5bf4SMike Christie return NULL; 1125913e5bf4SMike Christie 1126913e5bf4SMike Christie return session->cmds[i]; 1127913e5bf4SMike Christie } 11288f9256ceSMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_task); 1129913e5bf4SMike Christie 1130913e5bf4SMike Christie /** 11317996a778SMike Christie * __iscsi_complete_pdu - complete pdu 11327996a778SMike Christie * @conn: iscsi conn 11337996a778SMike Christie * @hdr: iscsi header 11347996a778SMike Christie * @data: data buffer 11357996a778SMike Christie * @datalen: len of data buffer 11367996a778SMike Christie * 11377996a778SMike Christie * Completes pdu processing by freeing any resources allocated at 1138659743b0SShlomo Pongratz * queuecommand or send generic. session back_lock must be held and verify 11397996a778SMike Christie * itt must have been called. 11407996a778SMike Christie */ 1141913e5bf4SMike Christie int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 11427996a778SMike Christie char *data, int datalen) 11437996a778SMike Christie { 11447996a778SMike Christie struct iscsi_session *session = conn->session; 11457996a778SMike Christie int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0; 11469c19a7d0SMike Christie struct iscsi_task *task; 11477996a778SMike Christie uint32_t itt; 11487996a778SMike Christie 1149f6d5180cSMike Christie conn->last_recv = jiffies; 11500af967f5SMike Christie rc = iscsi_verify_itt(conn, hdr->itt); 11510af967f5SMike Christie if (rc) 11520af967f5SMike Christie return rc; 11530af967f5SMike Christie 1154b4377356SAl Viro if (hdr->itt != RESERVED_ITT) 1155b4377356SAl Viro itt = get_itt(hdr->itt); 11567996a778SMike Christie else 1157b4377356SAl Viro itt = ~0U; 11587996a778SMike Christie 11591b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n", 11603e5c28adSMike Christie opcode, conn->id, itt, datalen); 11617996a778SMike Christie 11623e5c28adSMike Christie if (itt == ~0U) { 116377a23c21SMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 116462f38300SMike Christie 11657996a778SMike Christie switch(opcode) { 11667996a778SMike Christie case ISCSI_OP_NOOP_IN: 116740527afeSMike Christie if (datalen) { 116840527afeSMike Christie rc = ISCSI_ERR_PROTO; 116940527afeSMike Christie break; 117040527afeSMike Christie } 117140527afeSMike Christie 1172b4377356SAl Viro if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG)) 117340527afeSMike Christie break; 117440527afeSMike Christie 1175659743b0SShlomo Pongratz /* In RX path we are under back lock */ 1176659743b0SShlomo Pongratz spin_unlock(&session->back_lock); 1177659743b0SShlomo Pongratz spin_lock(&session->frwd_lock); 1178f6d5180cSMike Christie iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr); 1179659743b0SShlomo Pongratz spin_unlock(&session->frwd_lock); 1180659743b0SShlomo Pongratz spin_lock(&session->back_lock); 11817996a778SMike Christie break; 11827996a778SMike Christie case ISCSI_OP_REJECT: 118362f38300SMike Christie rc = iscsi_handle_reject(conn, hdr, data, datalen); 118462f38300SMike Christie break; 11857996a778SMike Christie case ISCSI_OP_ASYNC_EVENT: 11868d2860b3SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 11875831c737SMike Christie if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) 11885831c737SMike Christie rc = ISCSI_ERR_CONN_FAILED; 11897996a778SMike Christie break; 11907996a778SMike Christie default: 11917996a778SMike Christie rc = ISCSI_ERR_BAD_OPCODE; 11927996a778SMike Christie break; 11937996a778SMike Christie } 11943e5c28adSMike Christie goto out; 11953e5c28adSMike Christie } 11967996a778SMike Christie 11973e5c28adSMike Christie switch(opcode) { 11983e5c28adSMike Christie case ISCSI_OP_SCSI_CMD_RSP: 1199913e5bf4SMike Christie case ISCSI_OP_SCSI_DATA_IN: 1200913e5bf4SMike Christie task = iscsi_itt_to_ctask(conn, hdr->itt); 1201913e5bf4SMike Christie if (!task) 1202913e5bf4SMike Christie return ISCSI_ERR_BAD_ITT; 1203d355e57dSMike Christie task->last_xfer = jiffies; 12043e5c28adSMike Christie break; 1205913e5bf4SMike Christie case ISCSI_OP_R2T: 1206913e5bf4SMike Christie /* 1207913e5bf4SMike Christie * LLD handles R2Ts if they need to. 1208913e5bf4SMike Christie */ 1209913e5bf4SMike Christie return 0; 1210913e5bf4SMike Christie case ISCSI_OP_LOGOUT_RSP: 1211913e5bf4SMike Christie case ISCSI_OP_LOGIN_RSP: 1212913e5bf4SMike Christie case ISCSI_OP_TEXT_RSP: 1213913e5bf4SMike Christie case ISCSI_OP_SCSI_TMFUNC_RSP: 1214913e5bf4SMike Christie case ISCSI_OP_NOOP_IN: 1215913e5bf4SMike Christie task = iscsi_itt_to_task(conn, hdr->itt); 1216913e5bf4SMike Christie if (!task) 1217913e5bf4SMike Christie return ISCSI_ERR_BAD_ITT; 1218913e5bf4SMike Christie break; 1219913e5bf4SMike Christie default: 1220913e5bf4SMike Christie return ISCSI_ERR_BAD_OPCODE; 12213e5c28adSMike Christie } 1222913e5bf4SMike Christie 1223913e5bf4SMike Christie switch(opcode) { 1224913e5bf4SMike Christie case ISCSI_OP_SCSI_CMD_RSP: 12259c19a7d0SMike Christie iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen); 12263e5c28adSMike Christie break; 12273e5c28adSMike Christie case ISCSI_OP_SCSI_DATA_IN: 12281d9edf02SMike Christie iscsi_data_in_rsp(conn, hdr, task); 12293e5c28adSMike Christie break; 12303e5c28adSMike Christie case ISCSI_OP_LOGOUT_RSP: 12313e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 12323e5c28adSMike Christie if (datalen) { 12333e5c28adSMike Christie rc = ISCSI_ERR_PROTO; 12343e5c28adSMike Christie break; 12353e5c28adSMike Christie } 12363e5c28adSMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 12373e5c28adSMike Christie goto recv_pdu; 12383e5c28adSMike Christie case ISCSI_OP_LOGIN_RSP: 12393e5c28adSMike Christie case ISCSI_OP_TEXT_RSP: 12403e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 12413e5c28adSMike Christie /* 12423e5c28adSMike Christie * login related PDU's exp_statsn is handled in 12433e5c28adSMike Christie * userspace 12443e5c28adSMike Christie */ 12453e5c28adSMike Christie goto recv_pdu; 12463e5c28adSMike Christie case ISCSI_OP_SCSI_TMFUNC_RSP: 12473e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 12483e5c28adSMike Christie if (datalen) { 12493e5c28adSMike Christie rc = ISCSI_ERR_PROTO; 12503e5c28adSMike Christie break; 12513e5c28adSMike Christie } 12523e5c28adSMike Christie 12533e5c28adSMike Christie iscsi_tmf_rsp(conn, hdr); 1254b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 12553e5c28adSMike Christie break; 12563e5c28adSMike Christie case ISCSI_OP_NOOP_IN: 12573e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 12583e5c28adSMike Christie if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) { 12593e5c28adSMike Christie rc = ISCSI_ERR_PROTO; 12603e5c28adSMike Christie break; 12613e5c28adSMike Christie } 12623e5c28adSMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 12633e5c28adSMike Christie 12648afa1439SMike Christie rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr, 12658afa1439SMike Christie data, datalen); 12663e5c28adSMike Christie break; 12673e5c28adSMike Christie default: 12683e5c28adSMike Christie rc = ISCSI_ERR_BAD_OPCODE; 12693e5c28adSMike Christie break; 12703e5c28adSMike Christie } 12713e5c28adSMike Christie 12723e5c28adSMike Christie out: 12733e5c28adSMike Christie return rc; 12743e5c28adSMike Christie recv_pdu: 12753e5c28adSMike Christie if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) 12763e5c28adSMike Christie rc = ISCSI_ERR_CONN_FAILED; 1277b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 12787996a778SMike Christie return rc; 12797996a778SMike Christie } 1280913e5bf4SMike Christie EXPORT_SYMBOL_GPL(__iscsi_complete_pdu); 12817996a778SMike Christie 12827996a778SMike Christie int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 12837996a778SMike Christie char *data, int datalen) 12847996a778SMike Christie { 12857996a778SMike Christie int rc; 12867996a778SMike Christie 1287659743b0SShlomo Pongratz spin_lock(&conn->session->back_lock); 12887996a778SMike Christie rc = __iscsi_complete_pdu(conn, hdr, data, datalen); 1289659743b0SShlomo Pongratz spin_unlock(&conn->session->back_lock); 12907996a778SMike Christie return rc; 12917996a778SMike Christie } 12927996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_pdu); 12937996a778SMike Christie 12940af967f5SMike Christie int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt) 12957996a778SMike Christie { 12967996a778SMike Christie struct iscsi_session *session = conn->session; 1297262ef636SMike Christie int age = 0, i = 0; 12987996a778SMike Christie 12990af967f5SMike Christie if (itt == RESERVED_ITT) 13000af967f5SMike Christie return 0; 13010af967f5SMike Christie 1302262ef636SMike Christie if (session->tt->parse_pdu_itt) 1303262ef636SMike Christie session->tt->parse_pdu_itt(conn, itt, &i, &age); 1304262ef636SMike Christie else { 1305262ef636SMike Christie i = get_itt(itt); 1306262ef636SMike Christie age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK; 1307262ef636SMike Christie } 1308262ef636SMike Christie 1309262ef636SMike Christie if (age != session->age) { 1310322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 13110af967f5SMike Christie "received itt %x expected session age (%x)\n", 1312913e5bf4SMike Christie (__force u32)itt, session->age); 13137996a778SMike Christie return ISCSI_ERR_BAD_ITT; 13147996a778SMike Christie } 13157996a778SMike Christie 13163e5c28adSMike Christie if (i >= session->cmds_max) { 13173e5c28adSMike Christie iscsi_conn_printk(KERN_ERR, conn, 13183e5c28adSMike Christie "received invalid itt index %u (max cmds " 13193e5c28adSMike Christie "%u.\n", i, session->cmds_max); 13203e5c28adSMike Christie return ISCSI_ERR_BAD_ITT; 13217996a778SMike Christie } 13227996a778SMike Christie return 0; 13237996a778SMike Christie } 13247996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_verify_itt); 13257996a778SMike Christie 1326913e5bf4SMike Christie /** 1327913e5bf4SMike Christie * iscsi_itt_to_ctask - look up ctask by itt 1328913e5bf4SMike Christie * @conn: iscsi connection 1329913e5bf4SMike Christie * @itt: itt 1330913e5bf4SMike Christie * 1331913e5bf4SMike Christie * This should be used for cmd tasks. 1332913e5bf4SMike Christie * 1333659743b0SShlomo Pongratz * The session back_lock must be held. 1334913e5bf4SMike Christie */ 1335913e5bf4SMike Christie struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt) 13360af967f5SMike Christie { 13379c19a7d0SMike Christie struct iscsi_task *task; 13380af967f5SMike Christie 13390af967f5SMike Christie if (iscsi_verify_itt(conn, itt)) 13400af967f5SMike Christie return NULL; 13410af967f5SMike Christie 1342913e5bf4SMike Christie task = iscsi_itt_to_task(conn, itt); 1343913e5bf4SMike Christie if (!task || !task->sc) 13440af967f5SMike Christie return NULL; 13450af967f5SMike Christie 1346913e5bf4SMike Christie if (task->sc->SCp.phase != conn->session->age) { 1347913e5bf4SMike Christie iscsi_session_printk(KERN_ERR, conn->session, 1348913e5bf4SMike Christie "task's session age %d, expected %d\n", 1349913e5bf4SMike Christie task->sc->SCp.phase, conn->session->age); 13500af967f5SMike Christie return NULL; 1351913e5bf4SMike Christie } 13520af967f5SMike Christie 13539c19a7d0SMike Christie return task; 13540af967f5SMike Christie } 13550af967f5SMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask); 13560af967f5SMike Christie 135740a06e75SMike Christie void iscsi_session_failure(struct iscsi_session *session, 1358e5bd7b54SMike Christie enum iscsi_err err) 1359e5bd7b54SMike Christie { 1360e5bd7b54SMike Christie struct iscsi_conn *conn; 1361e5bd7b54SMike Christie struct device *dev; 1362e5bd7b54SMike Christie 1363659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 1364e5bd7b54SMike Christie conn = session->leadconn; 1365e5bd7b54SMike Christie if (session->state == ISCSI_STATE_TERMINATE || !conn) { 1366659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 1367e5bd7b54SMike Christie return; 1368e5bd7b54SMike Christie } 1369e5bd7b54SMike Christie 1370e5bd7b54SMike Christie dev = get_device(&conn->cls_conn->dev); 1371659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 1372e5bd7b54SMike Christie if (!dev) 1373e5bd7b54SMike Christie return; 1374e5bd7b54SMike Christie /* 1375e5bd7b54SMike Christie * if the host is being removed bypass the connection 1376e5bd7b54SMike Christie * recovery initialization because we are going to kill 1377e5bd7b54SMike Christie * the session. 1378e5bd7b54SMike Christie */ 1379e5bd7b54SMike Christie if (err == ISCSI_ERR_INVALID_HOST) 1380e5bd7b54SMike Christie iscsi_conn_error_event(conn->cls_conn, err); 1381e5bd7b54SMike Christie else 1382e5bd7b54SMike Christie iscsi_conn_failure(conn, err); 1383e5bd7b54SMike Christie put_device(dev); 1384e5bd7b54SMike Christie } 1385e5bd7b54SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_failure); 1386e5bd7b54SMike Christie 13877996a778SMike Christie void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err) 13887996a778SMike Christie { 13897996a778SMike Christie struct iscsi_session *session = conn->session; 13907996a778SMike Christie 1391659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 1392656cffc9SMike Christie if (session->state == ISCSI_STATE_FAILED) { 1393659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 1394656cffc9SMike Christie return; 1395656cffc9SMike Christie } 1396656cffc9SMike Christie 139767a61114SMike Christie if (conn->stop_stage == 0) 13987996a778SMike Christie session->state = ISCSI_STATE_FAILED; 1399659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 1400e5bd7b54SMike Christie 14017996a778SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 14027996a778SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); 1403e5bd7b54SMike Christie iscsi_conn_error_event(conn->cls_conn, err); 14047996a778SMike Christie } 14057996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_failure); 14067996a778SMike Christie 140777a23c21SMike Christie static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn) 140877a23c21SMike Christie { 140977a23c21SMike Christie struct iscsi_session *session = conn->session; 141077a23c21SMike Christie 141177a23c21SMike Christie /* 141277a23c21SMike Christie * Check for iSCSI window and take care of CmdSN wrap-around 141377a23c21SMike Christie */ 1414e0726407SMike Christie if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) { 14151b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn " 14161b2c7af8SMike Christie "%u MaxCmdSN %u CmdSN %u/%u\n", 14171b2c7af8SMike Christie session->exp_cmdsn, session->max_cmdsn, 14181b2c7af8SMike Christie session->cmdsn, session->queued_cmdsn); 141977a23c21SMike Christie return -ENOSPC; 142077a23c21SMike Christie } 142177a23c21SMike Christie return 0; 142277a23c21SMike Christie } 142377a23c21SMike Christie 14245923d64bSMike Christie static int iscsi_xmit_task(struct iscsi_conn *conn, struct iscsi_task *task, 14255923d64bSMike Christie bool was_requeue) 142677a23c21SMike Christie { 1427843c0a8aSMike Christie int rc; 142877a23c21SMike Christie 142979edd00dSAnoob Soman spin_lock_bh(&conn->session->back_lock); 14305923d64bSMike Christie 14315923d64bSMike Christie if (!conn->task) { 14325923d64bSMike Christie /* Take a ref so we can access it after xmit_task() */ 14335923d64bSMike Christie __iscsi_get_task(task); 14345923d64bSMike Christie } else { 14355923d64bSMike Christie /* Already have a ref from when we failed to send it last call */ 14365923d64bSMike Christie conn->task = NULL; 14375923d64bSMike Christie } 14385923d64bSMike Christie 14395923d64bSMike Christie /* 14405923d64bSMike Christie * If this was a requeue for a R2T we have an extra ref on the task in 14415923d64bSMike Christie * case a bad target sends a cmd rsp before we have handled the task. 14425923d64bSMike Christie */ 14435923d64bSMike Christie if (was_requeue) 14445923d64bSMike Christie __iscsi_put_task(task); 14455923d64bSMike Christie 14465923d64bSMike Christie /* 14475923d64bSMike Christie * Do this after dropping the extra ref because if this was a requeue 14485923d64bSMike Christie * it's removed from that list and cleanup_queued_task would miss it. 14495923d64bSMike Christie */ 14505923d64bSMike Christie if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) { 14515923d64bSMike Christie /* 14525923d64bSMike Christie * Save the task and ref in case we weren't cleaning up this 14535923d64bSMike Christie * task and get woken up again. 14545923d64bSMike Christie */ 14555923d64bSMike Christie conn->task = task; 145679edd00dSAnoob Soman spin_unlock_bh(&conn->session->back_lock); 145779edd00dSAnoob Soman return -ENODATA; 145879edd00dSAnoob Soman } 145979edd00dSAnoob Soman spin_unlock_bh(&conn->session->back_lock); 14605923d64bSMike Christie 1461659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 14629c19a7d0SMike Christie rc = conn->session->tt->xmit_task(task); 1463659743b0SShlomo Pongratz spin_lock_bh(&conn->session->frwd_lock); 1464d355e57dSMike Christie if (!rc) { 14659c19a7d0SMike Christie /* done with this task */ 1466d355e57dSMike Christie task->last_xfer = jiffies; 1467d355e57dSMike Christie } 1468659743b0SShlomo Pongratz /* regular RX path uses back_lock */ 146972b97402SShlomo Pongratz spin_lock(&conn->session->back_lock); 14705923d64bSMike Christie if (rc && task->state == ISCSI_TASK_RUNNING) { 14715923d64bSMike Christie /* 14725923d64bSMike Christie * get an extra ref that is released next time we access it 14735923d64bSMike Christie * as conn->task above. 14745923d64bSMike Christie */ 14755923d64bSMike Christie __iscsi_get_task(task); 14765923d64bSMike Christie conn->task = task; 14775923d64bSMike Christie } 14785923d64bSMike Christie 1479d355e57dSMike Christie __iscsi_put_task(task); 148072b97402SShlomo Pongratz spin_unlock(&conn->session->back_lock); 148177a23c21SMike Christie return rc; 148277a23c21SMike Christie } 148377a23c21SMike Christie 14847996a778SMike Christie /** 14859c19a7d0SMike Christie * iscsi_requeue_task - requeue task to run from session workqueue 14869c19a7d0SMike Christie * @task: task to requeue 1487843c0a8aSMike Christie * 14885923d64bSMike Christie * Callers must have taken a ref to the task that is going to be requeued. 1489843c0a8aSMike Christie */ 14909c19a7d0SMike Christie void iscsi_requeue_task(struct iscsi_task *task) 1491843c0a8aSMike Christie { 14929c19a7d0SMike Christie struct iscsi_conn *conn = task->conn; 1493843c0a8aSMike Christie 14943bbaaad9SMike Christie /* 14953bbaaad9SMike Christie * this may be on the requeue list already if the xmit_task callout 14963bbaaad9SMike Christie * is handling the r2ts while we are adding new ones 14973bbaaad9SMike Christie */ 14985923d64bSMike Christie spin_lock_bh(&conn->session->frwd_lock); 14995923d64bSMike Christie if (list_empty(&task->running)) { 15003bbaaad9SMike Christie list_add_tail(&task->running, &conn->requeue); 15015923d64bSMike Christie } else { 15025923d64bSMike Christie /* 15035923d64bSMike Christie * Don't need the extra ref since it's already requeued and 15045923d64bSMike Christie * has a ref. 15055923d64bSMike Christie */ 15065923d64bSMike Christie iscsi_put_task(task); 15075923d64bSMike Christie } 150832ae763eSMike Christie iscsi_conn_queue_work(conn); 15095923d64bSMike Christie spin_unlock_bh(&conn->session->frwd_lock); 1510843c0a8aSMike Christie } 15119c19a7d0SMike Christie EXPORT_SYMBOL_GPL(iscsi_requeue_task); 1512843c0a8aSMike Christie 1513843c0a8aSMike Christie /** 15147996a778SMike Christie * iscsi_data_xmit - xmit any command into the scheduled connection 15157996a778SMike Christie * @conn: iscsi connection 15167996a778SMike Christie * 15177996a778SMike Christie * Notes: 15187996a778SMike Christie * The function can return -EAGAIN in which case the caller must 15197996a778SMike Christie * re-schedule it again later or recover. '0' return code means 15207996a778SMike Christie * successful xmit. 15217996a778SMike Christie **/ 15227996a778SMike Christie static int iscsi_data_xmit(struct iscsi_conn *conn) 15237996a778SMike Christie { 15245d12c05eSMike Christie struct iscsi_task *task; 15253219e529SMike Christie int rc = 0; 15267996a778SMike Christie 1527659743b0SShlomo Pongratz spin_lock_bh(&conn->session->frwd_lock); 152870b31c15SMike Christie if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) { 15291b2c7af8SMike Christie ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n"); 1530659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 15313219e529SMike Christie return -ENODATA; 15327996a778SMike Christie } 15337996a778SMike Christie 15349c19a7d0SMike Christie if (conn->task) { 15355923d64bSMike Christie rc = iscsi_xmit_task(conn, conn->task, false); 15363219e529SMike Christie if (rc) 153770b31c15SMike Christie goto done; 15387996a778SMike Christie } 153977a23c21SMike Christie 154077a23c21SMike Christie /* 154177a23c21SMike Christie * process mgmt pdus like nops before commands since we should 154277a23c21SMike Christie * only have one nop-out as a ping from us and targets should not 154377a23c21SMike Christie * overflow us with nop-ins 154477a23c21SMike Christie */ 154577a23c21SMike Christie check_mgmt: 1546843c0a8aSMike Christie while (!list_empty(&conn->mgmtqueue)) { 15475923d64bSMike Christie task = list_entry(conn->mgmtqueue.next, struct iscsi_task, 15485923d64bSMike Christie running); 15495923d64bSMike Christie list_del_init(&task->running); 15505923d64bSMike Christie if (iscsi_prep_mgmt_task(conn, task)) { 1551659743b0SShlomo Pongratz /* regular RX path uses back_lock */ 1552659743b0SShlomo Pongratz spin_lock_bh(&conn->session->back_lock); 15535923d64bSMike Christie __iscsi_put_task(task); 1554659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->back_lock); 1555b3a7ea8dSMike Christie continue; 1556b3a7ea8dSMike Christie } 15575923d64bSMike Christie rc = iscsi_xmit_task(conn, task, false); 15583219e529SMike Christie if (rc) 155970b31c15SMike Christie goto done; 15607996a778SMike Christie } 15617996a778SMike Christie 1562843c0a8aSMike Christie /* process pending command queue */ 15633bbaaad9SMike Christie while (!list_empty(&conn->cmdqueue)) { 15645923d64bSMike Christie task = list_entry(conn->cmdqueue.next, struct iscsi_task, 15655d12c05eSMike Christie running); 15665923d64bSMike Christie list_del_init(&task->running); 1567b3a7ea8dSMike Christie if (conn->session->state == ISCSI_STATE_LOGGING_OUT) { 15685923d64bSMike Christie fail_scsi_task(task, DID_IMM_RETRY); 1569b3a7ea8dSMike Christie continue; 1570b3a7ea8dSMike Christie } 15715923d64bSMike Christie rc = iscsi_prep_scsi_cmd_pdu(task); 1572577577daSMike Christie if (rc) { 1573d28d48c6SMike Christie if (rc == -ENOMEM || rc == -EACCES) 15745923d64bSMike Christie fail_scsi_task(task, DID_IMM_RETRY); 1575d28d48c6SMike Christie else 15765923d64bSMike Christie fail_scsi_task(task, DID_ABORT); 1577004d6530SBoaz Harrosh continue; 1578004d6530SBoaz Harrosh } 15795923d64bSMike Christie rc = iscsi_xmit_task(conn, task, false); 15803219e529SMike Christie if (rc) 158170b31c15SMike Christie goto done; 158277a23c21SMike Christie /* 15839c19a7d0SMike Christie * we could continuously get new task requests so 158477a23c21SMike Christie * we need to check the mgmt queue for nops that need to 158577a23c21SMike Christie * be sent to aviod starvation 158677a23c21SMike Christie */ 1587843c0a8aSMike Christie if (!list_empty(&conn->mgmtqueue)) 1588843c0a8aSMike Christie goto check_mgmt; 1589843c0a8aSMike Christie } 1590843c0a8aSMike Christie 1591843c0a8aSMike Christie while (!list_empty(&conn->requeue)) { 1592b3a7ea8dSMike Christie /* 1593b3a7ea8dSMike Christie * we always do fastlogout - conn stop code will clean up. 1594b3a7ea8dSMike Christie */ 1595b3a7ea8dSMike Christie if (conn->session->state == ISCSI_STATE_LOGGING_OUT) 1596b3a7ea8dSMike Christie break; 1597b3a7ea8dSMike Christie 15985d12c05eSMike Christie task = list_entry(conn->requeue.next, struct iscsi_task, 15995d12c05eSMike Christie running); 16005923d64bSMike Christie 16015d12c05eSMike Christie if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT)) 16025d12c05eSMike Christie break; 16035d12c05eSMike Christie 16045923d64bSMike Christie list_del_init(&task->running); 16055923d64bSMike Christie rc = iscsi_xmit_task(conn, task, true); 1606843c0a8aSMike Christie if (rc) 160770b31c15SMike Christie goto done; 1608843c0a8aSMike Christie if (!list_empty(&conn->mgmtqueue)) 160977a23c21SMike Christie goto check_mgmt; 16107996a778SMike Christie } 1611659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 16123219e529SMike Christie return -ENODATA; 16137996a778SMike Christie 161470b31c15SMike Christie done: 1615659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 16163219e529SMike Christie return rc; 16177996a778SMike Christie } 16187996a778SMike Christie 1619c4028958SDavid Howells static void iscsi_xmitworker(struct work_struct *work) 16207996a778SMike Christie { 1621c4028958SDavid Howells struct iscsi_conn *conn = 1622c4028958SDavid Howells container_of(work, struct iscsi_conn, xmitwork); 16233219e529SMike Christie int rc; 16247996a778SMike Christie /* 16257996a778SMike Christie * serialize Xmit worker on a per-connection basis. 16267996a778SMike Christie */ 16273219e529SMike Christie do { 16283219e529SMike Christie rc = iscsi_data_xmit(conn); 16293219e529SMike Christie } while (rc >= 0 || rc == -EAGAIN); 16307996a778SMike Christie } 16317996a778SMike Christie 1632577577daSMike Christie static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn, 1633577577daSMike Christie struct scsi_cmnd *sc) 1634577577daSMike Christie { 1635577577daSMike Christie struct iscsi_task *task; 1636577577daSMike Christie 16377acd72ebSStefani Seibold if (!kfifo_out(&conn->session->cmdpool.queue, 1638577577daSMike Christie (void *) &task, sizeof(void *))) 1639577577daSMike Christie return NULL; 1640577577daSMike Christie 1641577577daSMike Christie sc->SCp.phase = conn->session->age; 1642577577daSMike Christie sc->SCp.ptr = (char *) task; 1643577577daSMike Christie 16446dc618cdSElena Reshetova refcount_set(&task->refcount, 1); 1645577577daSMike Christie task->state = ISCSI_TASK_PENDING; 1646577577daSMike Christie task->conn = conn; 1647577577daSMike Christie task->sc = sc; 1648d355e57dSMike Christie task->have_checked_conn = false; 1649d355e57dSMike Christie task->last_timeout = jiffies; 1650d355e57dSMike Christie task->last_xfer = jiffies; 165155e51edaSSagi Grimberg task->protected = false; 1652577577daSMike Christie INIT_LIST_HEAD(&task->running); 1653577577daSMike Christie return task; 1654577577daSMike Christie } 1655577577daSMike Christie 16567996a778SMike Christie enum { 16577996a778SMike Christie FAILURE_BAD_HOST = 1, 16587996a778SMike Christie FAILURE_SESSION_FAILED, 16597996a778SMike Christie FAILURE_SESSION_FREED, 16607996a778SMike Christie FAILURE_WINDOW_CLOSED, 166160ecebf5SMike Christie FAILURE_OOM, 16627996a778SMike Christie FAILURE_SESSION_TERMINATE, 1663656cffc9SMike Christie FAILURE_SESSION_IN_RECOVERY, 16647996a778SMike Christie FAILURE_SESSION_RECOVERY_TIMEOUT, 1665b3a7ea8dSMike Christie FAILURE_SESSION_LOGGING_OUT, 16666eabafbeSMike Christie FAILURE_SESSION_NOT_READY, 16677996a778SMike Christie }; 16687996a778SMike Christie 1669f41d4721SMike Christie int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc) 16707996a778SMike Christie { 167175613521SMike Christie struct iscsi_cls_session *cls_session; 16721336aed1SMike Christie struct iscsi_host *ihost; 16737996a778SMike Christie int reason = 0; 16747996a778SMike Christie struct iscsi_session *session; 16757996a778SMike Christie struct iscsi_conn *conn; 16769c19a7d0SMike Christie struct iscsi_task *task = NULL; 16777996a778SMike Christie 16787996a778SMike Christie sc->result = 0; 1679f47f2cf5SMike Christie sc->SCp.ptr = NULL; 16807996a778SMike Christie 16811336aed1SMike Christie ihost = shost_priv(host); 16827996a778SMike Christie 168375613521SMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 168475613521SMike Christie session = cls_session->dd_data; 1685659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 16867996a778SMike Christie 168775613521SMike Christie reason = iscsi_session_chkready(cls_session); 16886eabafbeSMike Christie if (reason) { 16896eabafbeSMike Christie sc->result = reason; 16906eabafbeSMike Christie goto fault; 16916eabafbeSMike Christie } 16926eabafbeSMike Christie 1693301e0f7eSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) { 1694656cffc9SMike Christie /* 1695656cffc9SMike Christie * to handle the race between when we set the recovery state 1696656cffc9SMike Christie * and block the session we requeue here (commands could 1697656cffc9SMike Christie * be entering our queuecommand while a block is starting 1698656cffc9SMike Christie * up because the block code is not locked) 1699656cffc9SMike Christie */ 17009000bcd6SMike Christie switch (session->state) { 1701301e0f7eSMike Christie case ISCSI_STATE_FAILED: 1702d7549412SRafael David Tinoco /* 1703d7549412SRafael David Tinoco * cmds should fail during shutdown, if the session 1704d7549412SRafael David Tinoco * state is bad, allowing completion to happen 1705d7549412SRafael David Tinoco */ 1706d7549412SRafael David Tinoco if (unlikely(system_state != SYSTEM_RUNNING)) { 1707d7549412SRafael David Tinoco reason = FAILURE_SESSION_FAILED; 1708d7549412SRafael David Tinoco sc->result = DID_NO_CONNECT << 16; 1709d7549412SRafael David Tinoco break; 1710d7549412SRafael David Tinoco } 1711df561f66SGustavo A. R. Silva fallthrough; 17129000bcd6SMike Christie case ISCSI_STATE_IN_RECOVERY: 1713656cffc9SMike Christie reason = FAILURE_SESSION_IN_RECOVERY; 1714301e0f7eSMike Christie sc->result = DID_IMM_RETRY << 16; 1715301e0f7eSMike Christie break; 17169000bcd6SMike Christie case ISCSI_STATE_LOGGING_OUT: 17179000bcd6SMike Christie reason = FAILURE_SESSION_LOGGING_OUT; 1718301e0f7eSMike Christie sc->result = DID_IMM_RETRY << 16; 1719301e0f7eSMike Christie break; 1720b3a7ea8dSMike Christie case ISCSI_STATE_RECOVERY_FAILED: 1721656cffc9SMike Christie reason = FAILURE_SESSION_RECOVERY_TIMEOUT; 172256d7fcfaSMike Christie sc->result = DID_TRANSPORT_FAILFAST << 16; 1723b3a7ea8dSMike Christie break; 1724b3a7ea8dSMike Christie case ISCSI_STATE_TERMINATE: 1725656cffc9SMike Christie reason = FAILURE_SESSION_TERMINATE; 17266eabafbeSMike Christie sc->result = DID_NO_CONNECT << 16; 1727b3a7ea8dSMike Christie break; 1728b3a7ea8dSMike Christie default: 17297996a778SMike Christie reason = FAILURE_SESSION_FREED; 17306eabafbeSMike Christie sc->result = DID_NO_CONNECT << 16; 1731b3a7ea8dSMike Christie } 17327996a778SMike Christie goto fault; 17337996a778SMike Christie } 17347996a778SMike Christie 17357996a778SMike Christie conn = session->leadconn; 173698644047SMike Christie if (!conn) { 173798644047SMike Christie reason = FAILURE_SESSION_FREED; 17386eabafbeSMike Christie sc->result = DID_NO_CONNECT << 16; 173998644047SMike Christie goto fault; 174098644047SMike Christie } 17417996a778SMike Christie 1742661134adSMike Christie if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) { 1743661134adSMike Christie reason = FAILURE_SESSION_IN_RECOVERY; 1744eef9ffdfSJohannes Thumshirn sc->result = DID_REQUEUE << 16; 1745661134adSMike Christie goto fault; 1746661134adSMike Christie } 1747661134adSMike Christie 174877a23c21SMike Christie if (iscsi_check_cmdsn_window_closed(conn)) { 174977a23c21SMike Christie reason = FAILURE_WINDOW_CLOSED; 175077a23c21SMike Christie goto reject; 175177a23c21SMike Christie } 175277a23c21SMike Christie 1753577577daSMike Christie task = iscsi_alloc_task(conn, sc); 1754577577daSMike Christie if (!task) { 175560ecebf5SMike Christie reason = FAILURE_OOM; 175660ecebf5SMike Christie goto reject; 175760ecebf5SMike Christie } 17587996a778SMike Christie 17591336aed1SMike Christie if (!ihost->workq) { 1760577577daSMike Christie reason = iscsi_prep_scsi_cmd_pdu(task); 1761577577daSMike Christie if (reason) { 17625d12c05eSMike Christie if (reason == -ENOMEM || reason == -EACCES) { 1763577577daSMike Christie reason = FAILURE_OOM; 1764577577daSMike Christie goto prepd_reject; 1765577577daSMike Christie } else { 1766052d0144SMike Christie sc->result = DID_ABORT << 16; 1767577577daSMike Christie goto prepd_fault; 1768577577daSMike Christie } 1769052d0144SMike Christie } 17709c19a7d0SMike Christie if (session->tt->xmit_task(task)) { 1771d3305f34SMike Christie session->cmdsn--; 1772052d0144SMike Christie reason = FAILURE_SESSION_NOT_READY; 1773577577daSMike Christie goto prepd_reject; 1774052d0144SMike Christie } 17753bbaaad9SMike Christie } else { 17763bbaaad9SMike Christie list_add_tail(&task->running, &conn->cmdqueue); 177732ae763eSMike Christie iscsi_conn_queue_work(conn); 17783bbaaad9SMike Christie } 1779052d0144SMike Christie 1780052d0144SMike Christie session->queued_cmdsn++; 1781659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 17827996a778SMike Christie return 0; 17837996a778SMike Christie 1784577577daSMike Christie prepd_reject: 1785a656183eSLee Duncan spin_lock_bh(&session->back_lock); 1786f41d4721SMike Christie iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ); 1787a656183eSLee Duncan spin_unlock_bh(&session->back_lock); 17887996a778SMike Christie reject: 1789659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 17901b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n", 17911b2c7af8SMike Christie sc->cmnd[0], reason); 1792d6d13ee1SMike Christie return SCSI_MLQUEUE_TARGET_BUSY; 17937996a778SMike Christie 1794577577daSMike Christie prepd_fault: 1795a656183eSLee Duncan spin_lock_bh(&session->back_lock); 1796f41d4721SMike Christie iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ); 1797a656183eSLee Duncan spin_unlock_bh(&session->back_lock); 17987996a778SMike Christie fault: 1799659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 18001b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n", 18011b2c7af8SMike Christie sc->cmnd[0], reason); 18021c138991SFUJITA Tomonori scsi_set_resid(sc, scsi_bufflen(sc)); 1803f41d4721SMike Christie sc->scsi_done(sc); 18047996a778SMike Christie return 0; 18057996a778SMike Christie } 18067996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_queuecommand); 18077996a778SMike Christie 18086b5d6c44SMike Christie int iscsi_target_alloc(struct scsi_target *starget) 18096b5d6c44SMike Christie { 18106b5d6c44SMike Christie struct iscsi_cls_session *cls_session = starget_to_session(starget); 18116b5d6c44SMike Christie struct iscsi_session *session = cls_session->dd_data; 18126b5d6c44SMike Christie 18136b5d6c44SMike Christie starget->can_queue = session->scsi_cmds_max; 18146b5d6c44SMike Christie return 0; 18156b5d6c44SMike Christie } 18166b5d6c44SMike Christie EXPORT_SYMBOL_GPL(iscsi_target_alloc); 18176b5d6c44SMike Christie 18182c4b9637SKees Cook static void iscsi_tmf_timedout(struct timer_list *t) 18197996a778SMike Christie { 18202c4b9637SKees Cook struct iscsi_conn *conn = from_timer(conn, t, tmf_timer); 18217996a778SMike Christie struct iscsi_session *session = conn->session; 18227996a778SMike Christie 1823659743b0SShlomo Pongratz spin_lock(&session->frwd_lock); 1824843c0a8aSMike Christie if (conn->tmf_state == TMF_QUEUED) { 1825843c0a8aSMike Christie conn->tmf_state = TMF_TIMEDOUT; 1826bd2199d4SErez Zilber ISCSI_DBG_EH(session, "tmf timedout\n"); 18277996a778SMike Christie /* unblock eh_abort() */ 18287996a778SMike Christie wake_up(&conn->ehwait); 18297996a778SMike Christie } 1830659743b0SShlomo Pongratz spin_unlock(&session->frwd_lock); 18317996a778SMike Christie } 18327996a778SMike Christie 18339c19a7d0SMike Christie static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn, 1834f6d5180cSMike Christie struct iscsi_tm *hdr, int age, 1835f6d5180cSMike Christie int timeout) 18361360c58aSBart Van Assche __must_hold(&session->frwd_lock) 18377996a778SMike Christie { 18387996a778SMike Christie struct iscsi_session *session = conn->session; 18399c19a7d0SMike Christie struct iscsi_task *task; 18407996a778SMike Christie 18419c19a7d0SMike Christie task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr, 18427996a778SMike Christie NULL, 0); 18439c19a7d0SMike Christie if (!task) { 1844659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 1845df4da5cdSMike Christie iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n"); 18467996a778SMike Christie iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 1847659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 184877a23c21SMike Christie return -EPERM; 18497996a778SMike Christie } 18507996a778SMike Christie conn->tmfcmd_pdus_cnt++; 1851f6d5180cSMike Christie conn->tmf_timer.expires = timeout * HZ + jiffies; 1852843c0a8aSMike Christie add_timer(&conn->tmf_timer); 1853bd2199d4SErez Zilber ISCSI_DBG_EH(session, "tmf set timeout\n"); 1854843c0a8aSMike Christie 1855659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 18566724add1SMike Christie mutex_unlock(&session->eh_mutex); 18577996a778SMike Christie 18587996a778SMike Christie /* 18597996a778SMike Christie * block eh thread until: 18607996a778SMike Christie * 1861843c0a8aSMike Christie * 1) tmf response 1862843c0a8aSMike Christie * 2) tmf timeout 18637996a778SMike Christie * 3) session is terminated or restarted or userspace has 18647996a778SMike Christie * given up on recovery 18657996a778SMike Christie */ 1866843c0a8aSMike Christie wait_event_interruptible(conn->ehwait, age != session->age || 18677996a778SMike Christie session->state != ISCSI_STATE_LOGGED_IN || 1868843c0a8aSMike Christie conn->tmf_state != TMF_QUEUED); 18697996a778SMike Christie if (signal_pending(current)) 18707996a778SMike Christie flush_signals(current); 1871843c0a8aSMike Christie del_timer_sync(&conn->tmf_timer); 1872843c0a8aSMike Christie 18736724add1SMike Christie mutex_lock(&session->eh_mutex); 1874659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 18759c19a7d0SMike Christie /* if the session drops it will clean up the task */ 1876843c0a8aSMike Christie if (age != session->age || 1877843c0a8aSMike Christie session->state != ISCSI_STATE_LOGGED_IN) 1878843c0a8aSMike Christie return -ENOTCONN; 18797996a778SMike Christie return 0; 18807996a778SMike Christie } 18817996a778SMike Christie 18827996a778SMike Christie /* 188314936b1eSMike Christie * Fail commands. session frwd lock held and xmit thread flushed. 1884843c0a8aSMike Christie */ 18859cb78c16SHannes Reinecke static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error) 1886843c0a8aSMike Christie { 188714936b1eSMike Christie struct iscsi_session *session = conn->session; 18883bbaaad9SMike Christie struct iscsi_task *task; 18893bbaaad9SMike Christie int i; 1890843c0a8aSMike Christie 189114936b1eSMike Christie spin_lock_bh(&session->back_lock); 189214936b1eSMike Christie for (i = 0; i < session->cmds_max; i++) { 189314936b1eSMike Christie task = session->cmds[i]; 18943bbaaad9SMike Christie if (!task->sc || task->state == ISCSI_TASK_FREE) 18953bbaaad9SMike Christie continue; 1896843c0a8aSMike Christie 18973bbaaad9SMike Christie if (lun != -1 && lun != task->sc->device->lun) 18983bbaaad9SMike Christie continue; 18993bbaaad9SMike Christie 190014936b1eSMike Christie __iscsi_get_task(task); 190114936b1eSMike Christie spin_unlock_bh(&session->back_lock); 190214936b1eSMike Christie 190314936b1eSMike Christie ISCSI_DBG_SESSION(session, 19043bbaaad9SMike Christie "failing sc %p itt 0x%x state %d\n", 19053bbaaad9SMike Christie task->sc, task->itt, task->state); 1906b3cd5050SMike Christie fail_scsi_task(task, error); 190714936b1eSMike Christie 190814936b1eSMike Christie spin_unlock_bh(&session->frwd_lock); 190914936b1eSMike Christie iscsi_put_task(task); 191014936b1eSMike Christie spin_lock_bh(&session->frwd_lock); 191114936b1eSMike Christie 191214936b1eSMike Christie spin_lock_bh(&session->back_lock); 1913843c0a8aSMike Christie } 191414936b1eSMike Christie 191514936b1eSMike Christie spin_unlock_bh(&session->back_lock); 1916843c0a8aSMike Christie } 1917843c0a8aSMike Christie 1918661134adSMike Christie /** 1919661134adSMike Christie * iscsi_suspend_queue - suspend iscsi_queuecommand 1920661134adSMike Christie * @conn: iscsi conn to stop queueing IO on 1921661134adSMike Christie * 1922659743b0SShlomo Pongratz * This grabs the session frwd_lock to make sure no one is in 1923661134adSMike Christie * xmit_task/queuecommand, and then sets suspend to prevent 1924661134adSMike Christie * new commands from being queued. This only needs to be called 1925661134adSMike Christie * by offload drivers that need to sync a path like ep disconnect 1926661134adSMike Christie * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi 1927661134adSMike Christie * will call iscsi_start_tx and iscsi_unblock_session when in FFP. 1928661134adSMike Christie */ 1929661134adSMike Christie void iscsi_suspend_queue(struct iscsi_conn *conn) 1930661134adSMike Christie { 1931659743b0SShlomo Pongratz spin_lock_bh(&conn->session->frwd_lock); 1932661134adSMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 1933659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 1934661134adSMike Christie } 1935661134adSMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_queue); 1936661134adSMike Christie 1937661134adSMike Christie /** 1938661134adSMike Christie * iscsi_suspend_tx - suspend iscsi_data_xmit 1939661134adSMike Christie * @conn: iscsi conn tp stop processing IO on. 1940661134adSMike Christie * 1941661134adSMike Christie * This function sets the suspend bit to prevent iscsi_data_xmit 1942661134adSMike Christie * from sending new IO, and if work is queued on the xmit thread 1943661134adSMike Christie * it will wait for it to be completed. 1944661134adSMike Christie */ 1945b40977d9SMike Christie void iscsi_suspend_tx(struct iscsi_conn *conn) 19466724add1SMike Christie { 194732ae763eSMike Christie struct Scsi_Host *shost = conn->session->host; 194832ae763eSMike Christie struct iscsi_host *ihost = shost_priv(shost); 194932ae763eSMike Christie 19506724add1SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 19511336aed1SMike Christie if (ihost->workq) 195232ae763eSMike Christie flush_workqueue(ihost->workq); 19536724add1SMike Christie } 1954b40977d9SMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_tx); 19556724add1SMike Christie 19566724add1SMike Christie static void iscsi_start_tx(struct iscsi_conn *conn) 19576724add1SMike Christie { 19586724add1SMike Christie clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 195932ae763eSMike Christie iscsi_conn_queue_work(conn); 19606724add1SMike Christie } 19616724add1SMike Christie 19624c48a829SMike Christie /* 19634c48a829SMike Christie * We want to make sure a ping is in flight. It has timed out. 19644c48a829SMike Christie * And we are not busy processing a pdu that is making 19654c48a829SMike Christie * progress but got started before the ping and is taking a while 19664c48a829SMike Christie * to complete so the ping is just stuck behind it in a queue. 19674c48a829SMike Christie */ 19684c48a829SMike Christie static int iscsi_has_ping_timed_out(struct iscsi_conn *conn) 19694c48a829SMike Christie { 1970fe0a8a95SLee Duncan if (READ_ONCE(conn->ping_task) && 19714c48a829SMike Christie time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) + 19724c48a829SMike Christie (conn->ping_timeout * HZ), jiffies)) 19734c48a829SMike Christie return 1; 19744c48a829SMike Christie else 19754c48a829SMike Christie return 0; 19764c48a829SMike Christie } 19774c48a829SMike Christie 1978b6a05c82SChristoph Hellwig enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc) 1979f6d5180cSMike Christie { 19806600593cSChristoph Hellwig enum blk_eh_timer_return rc = BLK_EH_DONE; 198192ed4d69SMike Christie struct iscsi_task *task = NULL, *running_task; 1982f6d5180cSMike Christie struct iscsi_cls_session *cls_session; 1983f6d5180cSMike Christie struct iscsi_session *session; 1984f6d5180cSMike Christie struct iscsi_conn *conn; 198592ed4d69SMike Christie int i; 1986f6d5180cSMike Christie 1987d355e57dSMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 198875613521SMike Christie session = cls_session->dd_data; 1989f6d5180cSMike Christie 1990bd2199d4SErez Zilber ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc); 1991f6d5180cSMike Christie 19925480e299SBart Van Assche spin_lock_bh(&session->frwd_lock); 199314936b1eSMike Christie spin_lock(&session->back_lock); 1994e3d338a5SMike Christie task = (struct iscsi_task *)sc->SCp.ptr; 1995e3d338a5SMike Christie if (!task) { 1996e3d338a5SMike Christie /* 1997e3d338a5SMike Christie * Raced with completion. Blk layer has taken ownership 1998e3d338a5SMike Christie * so let timeout code complete it now. 1999e3d338a5SMike Christie */ 2000adb2b769SChristoph Hellwig rc = BLK_EH_DONE; 200114936b1eSMike Christie spin_unlock(&session->back_lock); 2002e3d338a5SMike Christie goto done; 2003e3d338a5SMike Christie } 200414936b1eSMike Christie __iscsi_get_task(task); 200514936b1eSMike Christie spin_unlock(&session->back_lock); 2006e3d338a5SMike Christie 2007f6d5180cSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) { 2008f6d5180cSMike Christie /* 2009d7549412SRafael David Tinoco * During shutdown, if session is prematurely disconnected, 2010d7549412SRafael David Tinoco * recovery won't happen and there will be hung cmds. Not 2011d7549412SRafael David Tinoco * handling cmds would trigger EH, also bad in this case. 2012d7549412SRafael David Tinoco * Instead, handle cmd, allow completion to happen and let 2013d7549412SRafael David Tinoco * upper layer to deal with the result. 2014d7549412SRafael David Tinoco */ 2015d7549412SRafael David Tinoco if (unlikely(system_state != SYSTEM_RUNNING)) { 2016d7549412SRafael David Tinoco sc->result = DID_NO_CONNECT << 16; 2017d7549412SRafael David Tinoco ISCSI_DBG_EH(session, "sc on shutdown, handled\n"); 2018adb2b769SChristoph Hellwig rc = BLK_EH_DONE; 2019d7549412SRafael David Tinoco goto done; 2020d7549412SRafael David Tinoco } 2021d7549412SRafael David Tinoco /* 2022f6d5180cSMike Christie * We are probably in the middle of iscsi recovery so let 2023f6d5180cSMike Christie * that complete and handle the error. 2024f6d5180cSMike Christie */ 2025242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 2026f6d5180cSMike Christie goto done; 2027f6d5180cSMike Christie } 2028f6d5180cSMike Christie 2029f6d5180cSMike Christie conn = session->leadconn; 2030f6d5180cSMike Christie if (!conn) { 2031f6d5180cSMike Christie /* In the middle of shuting down */ 2032242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 2033f6d5180cSMike Christie goto done; 2034f6d5180cSMike Christie } 2035f6d5180cSMike Christie 2036d355e57dSMike Christie /* 2037d355e57dSMike Christie * If we have sent (at least queued to the network layer) a pdu or 2038d355e57dSMike Christie * recvd one for the task since the last timeout ask for 2039d355e57dSMike Christie * more time. If on the next timeout we have not made progress 2040d355e57dSMike Christie * we can check if it is the task or connection when we send the 2041d355e57dSMike Christie * nop as a ping. 2042d355e57dSMike Christie */ 204392ed4d69SMike Christie if (time_after(task->last_xfer, task->last_timeout)) { 2044bd2199d4SErez Zilber ISCSI_DBG_EH(session, "Command making progress. Asking " 2045d355e57dSMike Christie "scsi-ml for more time to complete. " 204692ed4d69SMike Christie "Last data xfer at %lu. Last timeout was at " 2047d355e57dSMike Christie "%lu\n.", task->last_xfer, task->last_timeout); 2048d355e57dSMike Christie task->have_checked_conn = false; 2049d355e57dSMike Christie rc = BLK_EH_RESET_TIMER; 2050d355e57dSMike Christie goto done; 2051d355e57dSMike Christie } 2052d355e57dSMike Christie 2053f6d5180cSMike Christie if (!conn->recv_timeout && !conn->ping_timeout) 2054f6d5180cSMike Christie goto done; 2055f6d5180cSMike Christie /* 2056f6d5180cSMike Christie * if the ping timedout then we are in the middle of cleaning up 2057f6d5180cSMike Christie * and can let the iscsi eh handle it 2058f6d5180cSMike Christie */ 20594c48a829SMike Christie if (iscsi_has_ping_timed_out(conn)) { 2060242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 20614c48a829SMike Christie goto done; 20624c48a829SMike Christie } 2063d355e57dSMike Christie 206414936b1eSMike Christie spin_lock(&session->back_lock); 206592ed4d69SMike Christie for (i = 0; i < conn->session->cmds_max; i++) { 206692ed4d69SMike Christie running_task = conn->session->cmds[i]; 206792ed4d69SMike Christie if (!running_task->sc || running_task == task || 206892ed4d69SMike Christie running_task->state != ISCSI_TASK_RUNNING) 206992ed4d69SMike Christie continue; 207092ed4d69SMike Christie 207192ed4d69SMike Christie /* 207292ed4d69SMike Christie * Only check if cmds started before this one have made 207392ed4d69SMike Christie * progress, or this could never fail 207492ed4d69SMike Christie */ 207592ed4d69SMike Christie if (time_after(running_task->sc->jiffies_at_alloc, 207692ed4d69SMike Christie task->sc->jiffies_at_alloc)) 207792ed4d69SMike Christie continue; 207892ed4d69SMike Christie 207992ed4d69SMike Christie if (time_after(running_task->last_xfer, task->last_timeout)) { 208092ed4d69SMike Christie /* 208192ed4d69SMike Christie * This task has not made progress, but a task 208292ed4d69SMike Christie * started before us has transferred data since 208392ed4d69SMike Christie * we started/last-checked. We could be queueing 208492ed4d69SMike Christie * too many tasks or the LU is bad. 208592ed4d69SMike Christie * 208692ed4d69SMike Christie * If the device is bad the cmds ahead of us on 208792ed4d69SMike Christie * other devs will complete, and this loop will 208892ed4d69SMike Christie * eventually fail starting the scsi eh. 208992ed4d69SMike Christie */ 209092ed4d69SMike Christie ISCSI_DBG_EH(session, "Command has not made progress " 209192ed4d69SMike Christie "but commands ahead of it have. " 209292ed4d69SMike Christie "Asking scsi-ml for more time to " 209392ed4d69SMike Christie "complete. Our last xfer vs running task " 209492ed4d69SMike Christie "last xfer %lu/%lu. Last check %lu.\n", 209592ed4d69SMike Christie task->last_xfer, running_task->last_xfer, 209692ed4d69SMike Christie task->last_timeout); 209714936b1eSMike Christie spin_unlock(&session->back_lock); 209892ed4d69SMike Christie rc = BLK_EH_RESET_TIMER; 209992ed4d69SMike Christie goto done; 210092ed4d69SMike Christie } 210192ed4d69SMike Christie } 210214936b1eSMike Christie spin_unlock(&session->back_lock); 210392ed4d69SMike Christie 2104d355e57dSMike Christie /* Assumes nop timeout is shorter than scsi cmd timeout */ 2105d355e57dSMike Christie if (task->have_checked_conn) 2106d355e57dSMike Christie goto done; 2107d355e57dSMike Christie 2108f6d5180cSMike Christie /* 2109d355e57dSMike Christie * Checking the transport already or nop from a cmd timeout still 2110d355e57dSMike Christie * running 2111f6d5180cSMike Christie */ 2112fe0a8a95SLee Duncan if (READ_ONCE(conn->ping_task)) { 2113d355e57dSMike Christie task->have_checked_conn = true; 2114242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 21154c48a829SMike Christie goto done; 21164c48a829SMike Christie } 21174c48a829SMike Christie 2118d355e57dSMike Christie /* Make sure there is a transport check done */ 2119d355e57dSMike Christie iscsi_send_nopout(conn, NULL); 2120d355e57dSMike Christie task->have_checked_conn = true; 2121242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 2122d355e57dSMike Christie 2123f6d5180cSMike Christie done: 21245480e299SBart Van Assche spin_unlock_bh(&session->frwd_lock); 212514936b1eSMike Christie 212614936b1eSMike Christie if (task) { 212714936b1eSMike Christie task->last_timeout = jiffies; 212814936b1eSMike Christie iscsi_put_task(task); 212914936b1eSMike Christie } 2130bd2199d4SErez Zilber ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ? 2131d7549412SRafael David Tinoco "timer reset" : "shutdown or nh"); 2132f6d5180cSMike Christie return rc; 2133f6d5180cSMike Christie } 2134b6a05c82SChristoph Hellwig EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out); 2135f6d5180cSMike Christie 21362c4b9637SKees Cook static void iscsi_check_transport_timeouts(struct timer_list *t) 2137f6d5180cSMike Christie { 21382c4b9637SKees Cook struct iscsi_conn *conn = from_timer(conn, t, transport_timer); 2139f6d5180cSMike Christie struct iscsi_session *session = conn->session; 21404cf10435SMike Christie unsigned long recv_timeout, next_timeout = 0, last_recv; 2141f6d5180cSMike Christie 2142659743b0SShlomo Pongratz spin_lock(&session->frwd_lock); 2143f6d5180cSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) 2144f6d5180cSMike Christie goto done; 2145f6d5180cSMike Christie 21464cf10435SMike Christie recv_timeout = conn->recv_timeout; 21474cf10435SMike Christie if (!recv_timeout) 2148f6d5180cSMike Christie goto done; 2149f6d5180cSMike Christie 21504cf10435SMike Christie recv_timeout *= HZ; 2151f6d5180cSMike Christie last_recv = conn->last_recv; 21524c48a829SMike Christie 21534c48a829SMike Christie if (iscsi_has_ping_timed_out(conn)) { 2154322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs " 21554c48a829SMike Christie "expired, recv timeout %d, last rx %lu, " 21564c48a829SMike Christie "last ping %lu, now %lu\n", 21574c48a829SMike Christie conn->ping_timeout, conn->recv_timeout, 21584c48a829SMike Christie last_recv, conn->last_ping, jiffies); 2159659743b0SShlomo Pongratz spin_unlock(&session->frwd_lock); 216009ff742cSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT); 2161f6d5180cSMike Christie return; 2162f6d5180cSMike Christie } 2163f6d5180cSMike Christie 21644cf10435SMike Christie if (time_before_eq(last_recv + recv_timeout, jiffies)) { 2165f6d5180cSMike Christie /* send a ping to try to provoke some traffic */ 21661b2c7af8SMike Christie ISCSI_DBG_CONN(conn, "Sending nopout as ping\n"); 216752f5664aSAriel Nahum if (iscsi_send_nopout(conn, NULL)) 216852f5664aSAriel Nahum next_timeout = jiffies + (1 * HZ); 216952f5664aSAriel Nahum else 21704cf10435SMike Christie next_timeout = conn->last_ping + (conn->ping_timeout * HZ); 2171ad294e9cSMike Christie } else 21724cf10435SMike Christie next_timeout = last_recv + recv_timeout; 2173f6d5180cSMike Christie 21741b2c7af8SMike Christie ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout); 2175f6d5180cSMike Christie mod_timer(&conn->transport_timer, next_timeout); 2176f6d5180cSMike Christie done: 2177659743b0SShlomo Pongratz spin_unlock(&session->frwd_lock); 2178f6d5180cSMike Christie } 2179f6d5180cSMike Christie 21809c19a7d0SMike Christie static void iscsi_prep_abort_task_pdu(struct iscsi_task *task, 2181843c0a8aSMike Christie struct iscsi_tm *hdr) 2182843c0a8aSMike Christie { 2183843c0a8aSMike Christie memset(hdr, 0, sizeof(*hdr)); 2184843c0a8aSMike Christie hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 2185843c0a8aSMike Christie hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK; 2186843c0a8aSMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 218755bdabdfSAndy Grover hdr->lun = task->lun; 2188577577daSMike Christie hdr->rtt = task->hdr_itt; 2189577577daSMike Christie hdr->refcmdsn = task->cmdsn; 2190843c0a8aSMike Christie } 2191843c0a8aSMike Christie 21927996a778SMike Christie int iscsi_eh_abort(struct scsi_cmnd *sc) 21937996a778SMike Christie { 219475613521SMike Christie struct iscsi_cls_session *cls_session; 219575613521SMike Christie struct iscsi_session *session; 2196f47f2cf5SMike Christie struct iscsi_conn *conn; 21979c19a7d0SMike Christie struct iscsi_task *task; 2198843c0a8aSMike Christie struct iscsi_tm *hdr; 2199e9e410e8SBart Van Assche int age; 22007996a778SMike Christie 220175613521SMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 220275613521SMike Christie session = cls_session->dd_data; 220375613521SMike Christie 2204bd2199d4SErez Zilber ISCSI_DBG_EH(session, "aborting sc %p\n", sc); 22054421c9ebSMike Christie 22066724add1SMike Christie mutex_lock(&session->eh_mutex); 2207659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 2208f47f2cf5SMike Christie /* 2209f47f2cf5SMike Christie * if session was ISCSI_STATE_IN_RECOVERY then we may not have 2210f47f2cf5SMike Christie * got the command. 2211f47f2cf5SMike Christie */ 2212f47f2cf5SMike Christie if (!sc->SCp.ptr) { 2213bd2199d4SErez Zilber ISCSI_DBG_EH(session, "sc never reached iscsi layer or " 22141b2c7af8SMike Christie "it completed.\n"); 2215659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 22166724add1SMike Christie mutex_unlock(&session->eh_mutex); 2217f47f2cf5SMike Christie return SUCCESS; 2218f47f2cf5SMike Christie } 2219f47f2cf5SMike Christie 22207996a778SMike Christie /* 22217996a778SMike Christie * If we are not logged in or we have started a new session 22227996a778SMike Christie * then let the host reset code handle this 22237996a778SMike Christie */ 2224843c0a8aSMike Christie if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN || 2225843c0a8aSMike Christie sc->SCp.phase != session->age) { 2226659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2227843c0a8aSMike Christie mutex_unlock(&session->eh_mutex); 2228bd2199d4SErez Zilber ISCSI_DBG_EH(session, "failing abort due to dropped " 22294421c9ebSMike Christie "session.\n"); 2230843c0a8aSMike Christie return FAILED; 2231843c0a8aSMike Christie } 2232843c0a8aSMike Christie 2233843c0a8aSMike Christie conn = session->leadconn; 2234843c0a8aSMike Christie conn->eh_abort_cnt++; 2235843c0a8aSMike Christie age = session->age; 2236843c0a8aSMike Christie 223714936b1eSMike Christie spin_lock(&session->back_lock); 22389c19a7d0SMike Christie task = (struct iscsi_task *)sc->SCp.ptr; 223914936b1eSMike Christie if (!task || !task->sc) { 22409c19a7d0SMike Christie /* task completed before time out */ 2241bd2199d4SErez Zilber ISCSI_DBG_EH(session, "sc completed while abort in progress\n"); 224214936b1eSMike Christie 224314936b1eSMike Christie spin_unlock(&session->back_lock); 224414936b1eSMike Christie spin_unlock_bh(&session->frwd_lock); 224514936b1eSMike Christie mutex_unlock(&session->eh_mutex); 224614936b1eSMike Christie return SUCCESS; 22477ea8b828SMike Christie } 224814936b1eSMike Christie ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n", sc, task->itt); 224914936b1eSMike Christie __iscsi_get_task(task); 225014936b1eSMike Christie spin_unlock(&session->back_lock); 22517996a778SMike Christie 22529c19a7d0SMike Christie if (task->state == ISCSI_TASK_PENDING) { 2253b3cd5050SMike Christie fail_scsi_task(task, DID_ABORT); 225477a23c21SMike Christie goto success; 225577a23c21SMike Christie } 22567996a778SMike Christie 2257843c0a8aSMike Christie /* only have one tmf outstanding at a time */ 2258843c0a8aSMike Christie if (conn->tmf_state != TMF_INITIAL) 22597996a778SMike Christie goto failed; 2260843c0a8aSMike Christie conn->tmf_state = TMF_QUEUED; 22617996a778SMike Christie 2262843c0a8aSMike Christie hdr = &conn->tmhdr; 22639c19a7d0SMike Christie iscsi_prep_abort_task_pdu(task, hdr); 2264843c0a8aSMike Christie 2265e9e410e8SBart Van Assche if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) 2266843c0a8aSMike Christie goto failed; 2267843c0a8aSMike Christie 2268843c0a8aSMike Christie switch (conn->tmf_state) { 2269843c0a8aSMike Christie case TMF_SUCCESS: 2270659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2271913e5bf4SMike Christie /* 2272913e5bf4SMike Christie * stop tx side incase the target had sent a abort rsp but 2273913e5bf4SMike Christie * the initiator was still writing out data. 2274913e5bf4SMike Christie */ 22756724add1SMike Christie iscsi_suspend_tx(conn); 22767996a778SMike Christie /* 2277913e5bf4SMike Christie * we do not stop the recv side because targets have been 2278913e5bf4SMike Christie * good and have never sent us a successful tmf response 2279913e5bf4SMike Christie * then sent more data for the cmd. 22807996a778SMike Christie */ 2281659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 2282b3cd5050SMike Christie fail_scsi_task(task, DID_ABORT); 2283843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 22845d12c05eSMike Christie memset(hdr, 0, sizeof(*hdr)); 2285659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 22866724add1SMike Christie iscsi_start_tx(conn); 228777a23c21SMike Christie goto success_unlocked; 2288843c0a8aSMike Christie case TMF_TIMEDOUT: 2289659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2290df4da5cdSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 2291843c0a8aSMike Christie goto failed_unlocked; 2292843c0a8aSMike Christie case TMF_NOT_FOUND: 2293843c0a8aSMike Christie if (!sc->SCp.ptr) { 2294843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 22955d12c05eSMike Christie memset(hdr, 0, sizeof(*hdr)); 22969c19a7d0SMike Christie /* task completed before tmf abort response */ 2297bd2199d4SErez Zilber ISCSI_DBG_EH(session, "sc completed while abort in " 2298bd2199d4SErez Zilber "progress\n"); 229977a23c21SMike Christie goto success; 230077a23c21SMike Christie } 2301df561f66SGustavo A. R. Silva fallthrough; 230277a23c21SMike Christie default: 2303843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 2304843c0a8aSMike Christie goto failed; 230577a23c21SMike Christie } 23067996a778SMike Christie 230777a23c21SMike Christie success: 2308659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 230977a23c21SMike Christie success_unlocked: 2310bd2199d4SErez Zilber ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n", 23111b2c7af8SMike Christie sc, task->itt); 231214936b1eSMike Christie iscsi_put_task(task); 23136724add1SMike Christie mutex_unlock(&session->eh_mutex); 23147996a778SMike Christie return SUCCESS; 23157996a778SMike Christie 23167996a778SMike Christie failed: 2317659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 231877a23c21SMike Christie failed_unlocked: 2319bd2199d4SErez Zilber ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc, 23209c19a7d0SMike Christie task ? task->itt : 0); 232114936b1eSMike Christie iscsi_put_task(task); 23226724add1SMike Christie mutex_unlock(&session->eh_mutex); 23237996a778SMike Christie return FAILED; 23247996a778SMike Christie } 23257996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_abort); 23267996a778SMike Christie 2327843c0a8aSMike Christie static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr) 2328843c0a8aSMike Christie { 2329843c0a8aSMike Christie memset(hdr, 0, sizeof(*hdr)); 2330843c0a8aSMike Christie hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 2331843c0a8aSMike Christie hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK; 2332843c0a8aSMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 233355bdabdfSAndy Grover int_to_scsilun(sc->device->lun, &hdr->lun); 2334f6d5180cSMike Christie hdr->rtt = RESERVED_ITT; 2335843c0a8aSMike Christie } 2336843c0a8aSMike Christie 2337843c0a8aSMike Christie int iscsi_eh_device_reset(struct scsi_cmnd *sc) 2338843c0a8aSMike Christie { 233975613521SMike Christie struct iscsi_cls_session *cls_session; 234075613521SMike Christie struct iscsi_session *session; 2341843c0a8aSMike Christie struct iscsi_conn *conn; 2342843c0a8aSMike Christie struct iscsi_tm *hdr; 2343843c0a8aSMike Christie int rc = FAILED; 2344843c0a8aSMike Christie 234575613521SMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 234675613521SMike Christie session = cls_session->dd_data; 234775613521SMike Christie 23489cb78c16SHannes Reinecke ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc, 23499cb78c16SHannes Reinecke sc->device->lun); 2350843c0a8aSMike Christie 2351843c0a8aSMike Christie mutex_lock(&session->eh_mutex); 2352659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 2353843c0a8aSMike Christie /* 2354843c0a8aSMike Christie * Just check if we are not logged in. We cannot check for 2355843c0a8aSMike Christie * the phase because the reset could come from a ioctl. 2356843c0a8aSMike Christie */ 2357843c0a8aSMike Christie if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) 2358843c0a8aSMike Christie goto unlock; 2359843c0a8aSMike Christie conn = session->leadconn; 2360843c0a8aSMike Christie 2361843c0a8aSMike Christie /* only have one tmf outstanding at a time */ 2362843c0a8aSMike Christie if (conn->tmf_state != TMF_INITIAL) 2363843c0a8aSMike Christie goto unlock; 2364843c0a8aSMike Christie conn->tmf_state = TMF_QUEUED; 2365843c0a8aSMike Christie 2366843c0a8aSMike Christie hdr = &conn->tmhdr; 2367843c0a8aSMike Christie iscsi_prep_lun_reset_pdu(sc, hdr); 2368843c0a8aSMike Christie 23699c19a7d0SMike Christie if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age, 2370f6d5180cSMike Christie session->lu_reset_timeout)) { 2371843c0a8aSMike Christie rc = FAILED; 2372843c0a8aSMike Christie goto unlock; 2373843c0a8aSMike Christie } 2374843c0a8aSMike Christie 2375843c0a8aSMike Christie switch (conn->tmf_state) { 2376843c0a8aSMike Christie case TMF_SUCCESS: 2377843c0a8aSMike Christie break; 2378843c0a8aSMike Christie case TMF_TIMEDOUT: 2379659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2380df4da5cdSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 2381843c0a8aSMike Christie goto done; 2382843c0a8aSMike Christie default: 2383843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 2384843c0a8aSMike Christie goto unlock; 2385843c0a8aSMike Christie } 2386843c0a8aSMike Christie 2387843c0a8aSMike Christie rc = SUCCESS; 2388659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2389843c0a8aSMike Christie 2390843c0a8aSMike Christie iscsi_suspend_tx(conn); 2391913e5bf4SMike Christie 2392659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 23935d12c05eSMike Christie memset(hdr, 0, sizeof(*hdr)); 23943bbaaad9SMike Christie fail_scsi_tasks(conn, sc->device->lun, DID_ERROR); 2395843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 2396659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2397843c0a8aSMike Christie 2398843c0a8aSMike Christie iscsi_start_tx(conn); 2399843c0a8aSMike Christie goto done; 2400843c0a8aSMike Christie 2401843c0a8aSMike Christie unlock: 2402659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2403843c0a8aSMike Christie done: 2404bd2199d4SErez Zilber ISCSI_DBG_EH(session, "dev reset result = %s\n", 2405843c0a8aSMike Christie rc == SUCCESS ? "SUCCESS" : "FAILED"); 2406843c0a8aSMike Christie mutex_unlock(&session->eh_mutex); 2407843c0a8aSMike Christie return rc; 2408843c0a8aSMike Christie } 2409843c0a8aSMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_device_reset); 2410843c0a8aSMike Christie 24113fe5ae8bSMike Christie void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session) 24123fe5ae8bSMike Christie { 24133fe5ae8bSMike Christie struct iscsi_session *session = cls_session->dd_data; 24143fe5ae8bSMike Christie 2415659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 24163fe5ae8bSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) { 24173fe5ae8bSMike Christie session->state = ISCSI_STATE_RECOVERY_FAILED; 24183fe5ae8bSMike Christie if (session->leadconn) 24193fe5ae8bSMike Christie wake_up(&session->leadconn->ehwait); 24203fe5ae8bSMike Christie } 2421659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 24223fe5ae8bSMike Christie } 24233fe5ae8bSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout); 24243fe5ae8bSMike Christie 24253fe5ae8bSMike Christie /** 24263fe5ae8bSMike Christie * iscsi_eh_session_reset - drop session and attempt relogin 24273fe5ae8bSMike Christie * @sc: scsi command 24283fe5ae8bSMike Christie * 24293fe5ae8bSMike Christie * This function will wait for a relogin, session termination from 24303fe5ae8bSMike Christie * userspace, or a recovery/replacement timeout. 24313fe5ae8bSMike Christie */ 2432309ce156SJayamohan Kallickal int iscsi_eh_session_reset(struct scsi_cmnd *sc) 24333fe5ae8bSMike Christie { 24343fe5ae8bSMike Christie struct iscsi_cls_session *cls_session; 24353fe5ae8bSMike Christie struct iscsi_session *session; 24363fe5ae8bSMike Christie struct iscsi_conn *conn; 24373fe5ae8bSMike Christie 24383fe5ae8bSMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 24393fe5ae8bSMike Christie session = cls_session->dd_data; 24403fe5ae8bSMike Christie conn = session->leadconn; 24413fe5ae8bSMike Christie 24423fe5ae8bSMike Christie mutex_lock(&session->eh_mutex); 2443659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 24443fe5ae8bSMike Christie if (session->state == ISCSI_STATE_TERMINATE) { 24453fe5ae8bSMike Christie failed: 24463fe5ae8bSMike Christie ISCSI_DBG_EH(session, 24473fe5ae8bSMike Christie "failing session reset: Could not log back into " 24485db6dd14SFred Herard "%s [age %d]\n", session->targetname, 24495db6dd14SFred Herard session->age); 2450659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 24513fe5ae8bSMike Christie mutex_unlock(&session->eh_mutex); 24523fe5ae8bSMike Christie return FAILED; 24533fe5ae8bSMike Christie } 24543fe5ae8bSMike Christie 2455659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 24563fe5ae8bSMike Christie mutex_unlock(&session->eh_mutex); 24573fe5ae8bSMike Christie /* 24583fe5ae8bSMike Christie * we drop the lock here but the leadconn cannot be destoyed while 24593fe5ae8bSMike Christie * we are in the scsi eh 24603fe5ae8bSMike Christie */ 2461df4da5cdSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 24623fe5ae8bSMike Christie 24633fe5ae8bSMike Christie ISCSI_DBG_EH(session, "wait for relogin\n"); 24643fe5ae8bSMike Christie wait_event_interruptible(conn->ehwait, 24653fe5ae8bSMike Christie session->state == ISCSI_STATE_TERMINATE || 24663fe5ae8bSMike Christie session->state == ISCSI_STATE_LOGGED_IN || 24673fe5ae8bSMike Christie session->state == ISCSI_STATE_RECOVERY_FAILED); 24683fe5ae8bSMike Christie if (signal_pending(current)) 24693fe5ae8bSMike Christie flush_signals(current); 24703fe5ae8bSMike Christie 24713fe5ae8bSMike Christie mutex_lock(&session->eh_mutex); 2472659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 24733fe5ae8bSMike Christie if (session->state == ISCSI_STATE_LOGGED_IN) { 24743fe5ae8bSMike Christie ISCSI_DBG_EH(session, 24753fe5ae8bSMike Christie "session reset succeeded for %s,%s\n", 24763fe5ae8bSMike Christie session->targetname, conn->persistent_address); 24773fe5ae8bSMike Christie } else 24783fe5ae8bSMike Christie goto failed; 2479659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 24803fe5ae8bSMike Christie mutex_unlock(&session->eh_mutex); 24813fe5ae8bSMike Christie return SUCCESS; 24823fe5ae8bSMike Christie } 2483309ce156SJayamohan Kallickal EXPORT_SYMBOL_GPL(iscsi_eh_session_reset); 24843fe5ae8bSMike Christie 24853fe5ae8bSMike Christie static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr) 24863fe5ae8bSMike Christie { 24873fe5ae8bSMike Christie memset(hdr, 0, sizeof(*hdr)); 24883fe5ae8bSMike Christie hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 24893fe5ae8bSMike Christie hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK; 24903fe5ae8bSMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 24913fe5ae8bSMike Christie hdr->rtt = RESERVED_ITT; 24923fe5ae8bSMike Christie } 24933fe5ae8bSMike Christie 24943fe5ae8bSMike Christie /** 24953fe5ae8bSMike Christie * iscsi_eh_target_reset - reset target 24963fe5ae8bSMike Christie * @sc: scsi command 24973fe5ae8bSMike Christie * 2498309ce156SJayamohan Kallickal * This will attempt to send a warm target reset. 24993fe5ae8bSMike Christie */ 25003907adf6SBart Van Assche static int iscsi_eh_target_reset(struct scsi_cmnd *sc) 25013fe5ae8bSMike Christie { 25023fe5ae8bSMike Christie struct iscsi_cls_session *cls_session; 25033fe5ae8bSMike Christie struct iscsi_session *session; 25043fe5ae8bSMike Christie struct iscsi_conn *conn; 25053fe5ae8bSMike Christie struct iscsi_tm *hdr; 25063fe5ae8bSMike Christie int rc = FAILED; 25073fe5ae8bSMike Christie 25083fe5ae8bSMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 25093fe5ae8bSMike Christie session = cls_session->dd_data; 25103fe5ae8bSMike Christie 25113fe5ae8bSMike Christie ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc, 25123fe5ae8bSMike Christie session->targetname); 25133fe5ae8bSMike Christie 25143fe5ae8bSMike Christie mutex_lock(&session->eh_mutex); 2515659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 25163fe5ae8bSMike Christie /* 25173fe5ae8bSMike Christie * Just check if we are not logged in. We cannot check for 25183fe5ae8bSMike Christie * the phase because the reset could come from a ioctl. 25193fe5ae8bSMike Christie */ 25203fe5ae8bSMike Christie if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) 25213fe5ae8bSMike Christie goto unlock; 25223fe5ae8bSMike Christie conn = session->leadconn; 25233fe5ae8bSMike Christie 25243fe5ae8bSMike Christie /* only have one tmf outstanding at a time */ 25253fe5ae8bSMike Christie if (conn->tmf_state != TMF_INITIAL) 25263fe5ae8bSMike Christie goto unlock; 25273fe5ae8bSMike Christie conn->tmf_state = TMF_QUEUED; 25283fe5ae8bSMike Christie 25293fe5ae8bSMike Christie hdr = &conn->tmhdr; 25303fe5ae8bSMike Christie iscsi_prep_tgt_reset_pdu(sc, hdr); 25313fe5ae8bSMike Christie 25323fe5ae8bSMike Christie if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age, 25333fe5ae8bSMike Christie session->tgt_reset_timeout)) { 25343fe5ae8bSMike Christie rc = FAILED; 25353fe5ae8bSMike Christie goto unlock; 25363fe5ae8bSMike Christie } 25373fe5ae8bSMike Christie 25383fe5ae8bSMike Christie switch (conn->tmf_state) { 25393fe5ae8bSMike Christie case TMF_SUCCESS: 25403fe5ae8bSMike Christie break; 25413fe5ae8bSMike Christie case TMF_TIMEDOUT: 2542659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2543df4da5cdSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 25443fe5ae8bSMike Christie goto done; 25453fe5ae8bSMike Christie default: 25463fe5ae8bSMike Christie conn->tmf_state = TMF_INITIAL; 25473fe5ae8bSMike Christie goto unlock; 25483fe5ae8bSMike Christie } 25493fe5ae8bSMike Christie 25503fe5ae8bSMike Christie rc = SUCCESS; 2551659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 25523fe5ae8bSMike Christie 25533fe5ae8bSMike Christie iscsi_suspend_tx(conn); 25543fe5ae8bSMike Christie 2555659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 25563fe5ae8bSMike Christie memset(hdr, 0, sizeof(*hdr)); 25573fe5ae8bSMike Christie fail_scsi_tasks(conn, -1, DID_ERROR); 25583fe5ae8bSMike Christie conn->tmf_state = TMF_INITIAL; 2559659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 25603fe5ae8bSMike Christie 25613fe5ae8bSMike Christie iscsi_start_tx(conn); 25623fe5ae8bSMike Christie goto done; 25633fe5ae8bSMike Christie 25643fe5ae8bSMike Christie unlock: 2565659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 25663fe5ae8bSMike Christie done: 25673fe5ae8bSMike Christie ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname, 25683fe5ae8bSMike Christie rc == SUCCESS ? "SUCCESS" : "FAILED"); 25693fe5ae8bSMike Christie mutex_unlock(&session->eh_mutex); 2570309ce156SJayamohan Kallickal return rc; 2571309ce156SJayamohan Kallickal } 25723fe5ae8bSMike Christie 2573309ce156SJayamohan Kallickal /** 2574309ce156SJayamohan Kallickal * iscsi_eh_recover_target - reset target and possibly the session 2575309ce156SJayamohan Kallickal * @sc: scsi command 2576309ce156SJayamohan Kallickal * 2577309ce156SJayamohan Kallickal * This will attempt to send a warm target reset. If that fails, 2578309ce156SJayamohan Kallickal * we will escalate to ERL0 session recovery. 2579309ce156SJayamohan Kallickal */ 2580309ce156SJayamohan Kallickal int iscsi_eh_recover_target(struct scsi_cmnd *sc) 2581309ce156SJayamohan Kallickal { 2582309ce156SJayamohan Kallickal int rc; 2583309ce156SJayamohan Kallickal 2584309ce156SJayamohan Kallickal rc = iscsi_eh_target_reset(sc); 25853fe5ae8bSMike Christie if (rc == FAILED) 25863fe5ae8bSMike Christie rc = iscsi_eh_session_reset(sc); 25873fe5ae8bSMike Christie return rc; 25883fe5ae8bSMike Christie } 2589309ce156SJayamohan Kallickal EXPORT_SYMBOL_GPL(iscsi_eh_recover_target); 25903fe5ae8bSMike Christie 25916320377fSOlaf Kirch /* 25926320377fSOlaf Kirch * Pre-allocate a pool of @max items of @item_size. By default, the pool 25936320377fSOlaf Kirch * should be accessed via kfifo_{get,put} on q->queue. 25946320377fSOlaf Kirch * Optionally, the caller can obtain the array of object pointers 25956320377fSOlaf Kirch * by passing in a non-NULL @items pointer 25966320377fSOlaf Kirch */ 25977996a778SMike Christie int 25986320377fSOlaf Kirch iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size) 25997996a778SMike Christie { 26006320377fSOlaf Kirch int i, num_arrays = 1; 26017996a778SMike Christie 26026320377fSOlaf Kirch memset(q, 0, sizeof(*q)); 26037996a778SMike Christie 26047996a778SMike Christie q->max = max; 26056320377fSOlaf Kirch 26066320377fSOlaf Kirch /* If the user passed an items pointer, he wants a copy of 26076320377fSOlaf Kirch * the array. */ 26086320377fSOlaf Kirch if (items) 26096320377fSOlaf Kirch num_arrays++; 2610778e1cddSKees Cook q->pool = kvcalloc(num_arrays * max, sizeof(void *), GFP_KERNEL); 26116320377fSOlaf Kirch if (q->pool == NULL) 2612f474a37bSJean Delvare return -ENOMEM; 26137996a778SMike Christie 2614c1e13f25SStefani Seibold kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*)); 26157996a778SMike Christie 26167996a778SMike Christie for (i = 0; i < max; i++) { 26176320377fSOlaf Kirch q->pool[i] = kzalloc(item_size, GFP_KERNEL); 26187996a778SMike Christie if (q->pool[i] == NULL) { 26196320377fSOlaf Kirch q->max = i; 26206320377fSOlaf Kirch goto enomem; 26217996a778SMike Christie } 26227acd72ebSStefani Seibold kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*)); 26237996a778SMike Christie } 26246320377fSOlaf Kirch 26256320377fSOlaf Kirch if (items) { 26266320377fSOlaf Kirch *items = q->pool + max; 26276320377fSOlaf Kirch memcpy(*items, q->pool, max * sizeof(void *)); 26286320377fSOlaf Kirch } 26296320377fSOlaf Kirch 26307996a778SMike Christie return 0; 26316320377fSOlaf Kirch 26326320377fSOlaf Kirch enomem: 26336320377fSOlaf Kirch iscsi_pool_free(q); 26346320377fSOlaf Kirch return -ENOMEM; 26357996a778SMike Christie } 26367996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_init); 26377996a778SMike Christie 26386320377fSOlaf Kirch void iscsi_pool_free(struct iscsi_pool *q) 26397996a778SMike Christie { 26407996a778SMike Christie int i; 26417996a778SMike Christie 26427996a778SMike Christie for (i = 0; i < q->max; i++) 26436320377fSOlaf Kirch kfree(q->pool[i]); 2644bfcc62edSKyle Fortin kvfree(q->pool); 26457996a778SMike Christie } 26467996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_free); 26477996a778SMike Christie 2648b4046922SMike Christie int iscsi_host_get_max_scsi_cmds(struct Scsi_Host *shost, 2649b4046922SMike Christie uint16_t requested_cmds_max) 2650b4046922SMike Christie { 2651b4046922SMike Christie int scsi_cmds, total_cmds = requested_cmds_max; 2652b4046922SMike Christie 2653b4046922SMike Christie check: 2654b4046922SMike Christie if (!total_cmds) 2655b4046922SMike Christie total_cmds = ISCSI_DEF_XMIT_CMDS_MAX; 2656b4046922SMike Christie /* 2657b4046922SMike Christie * The iscsi layer needs some tasks for nop handling and tmfs, 2658b4046922SMike Christie * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX 2659b4046922SMike Christie * + 1 command for scsi IO. 2660b4046922SMike Christie */ 2661b4046922SMike Christie if (total_cmds < ISCSI_TOTAL_CMDS_MIN) { 2662b4046922SMike Christie printk(KERN_ERR "iscsi: invalid max cmds of %d. Must be a power of two that is at least %d.\n", 2663b4046922SMike Christie total_cmds, ISCSI_TOTAL_CMDS_MIN); 2664b4046922SMike Christie return -EINVAL; 2665b4046922SMike Christie } 2666b4046922SMike Christie 2667b4046922SMike Christie if (total_cmds > ISCSI_TOTAL_CMDS_MAX) { 2668b4046922SMike 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", 2669b4046922SMike Christie requested_cmds_max, ISCSI_TOTAL_CMDS_MAX, 2670b4046922SMike Christie ISCSI_TOTAL_CMDS_MAX); 2671b4046922SMike Christie total_cmds = ISCSI_TOTAL_CMDS_MAX; 2672b4046922SMike Christie } 2673b4046922SMike Christie 2674b4046922SMike Christie if (!is_power_of_2(total_cmds)) { 2675b4046922SMike Christie total_cmds = rounddown_pow_of_two(total_cmds); 2676b4046922SMike Christie if (total_cmds < ISCSI_TOTAL_CMDS_MIN) { 2677b4046922SMike 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); 2678b4046922SMike Christie return -EINVAL; 2679b4046922SMike Christie } 2680b4046922SMike Christie 2681b4046922SMike Christie printk(KERN_INFO "iscsi: invalid max cmds %d. Must be a power of 2. Rounding max cmds down to %d.\n", 2682b4046922SMike Christie requested_cmds_max, total_cmds); 2683b4046922SMike Christie } 2684b4046922SMike Christie 2685b4046922SMike Christie scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX; 2686b4046922SMike Christie if (shost->can_queue && scsi_cmds > shost->can_queue) { 2687b4046922SMike Christie total_cmds = shost->can_queue; 2688b4046922SMike Christie 2689b4046922SMike Christie printk(KERN_INFO "iscsi: requested max cmds %u is higher than driver limit. Using driver limit %u\n", 2690b4046922SMike Christie requested_cmds_max, shost->can_queue); 2691b4046922SMike Christie goto check; 2692b4046922SMike Christie } 2693b4046922SMike Christie 2694b4046922SMike Christie return scsi_cmds; 2695b4046922SMike Christie } 2696b4046922SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_get_max_scsi_cmds); 2697b4046922SMike Christie 2698a4804cd6SMike Christie /** 2699a4804cd6SMike Christie * iscsi_host_add - add host to system 2700a4804cd6SMike Christie * @shost: scsi host 2701a4804cd6SMike Christie * @pdev: parent device 2702a4804cd6SMike Christie * 2703a4804cd6SMike Christie * This should be called by partial offload and software iscsi drivers 2704a4804cd6SMike Christie * to add a host to the system. 2705a4804cd6SMike Christie */ 2706a4804cd6SMike Christie int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev) 27077996a778SMike Christie { 27088e9a20ceSMike Christie if (!shost->can_queue) 27098e9a20ceSMike Christie shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX; 27108e9a20ceSMike Christie 27114d108350SMike Christie if (!shost->cmd_per_lun) 27124d108350SMike Christie shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN; 27134d108350SMike Christie 2714a4804cd6SMike Christie return scsi_add_host(shost, pdev); 2715a4804cd6SMike Christie } 2716a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_add); 2717a4804cd6SMike Christie 2718a4804cd6SMike Christie /** 2719a4804cd6SMike Christie * iscsi_host_alloc - allocate a host and driver data 2720a4804cd6SMike Christie * @sht: scsi host template 2721a4804cd6SMike Christie * @dd_data_size: driver host data size 272232ae763eSMike Christie * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue 2723a4804cd6SMike Christie * 2724a4804cd6SMike Christie * This should be called by partial offload and software iscsi drivers. 2725a4804cd6SMike Christie * To access the driver specific memory use the iscsi_host_priv() macro. 2726a4804cd6SMike Christie */ 2727a4804cd6SMike Christie struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht, 27284d108350SMike Christie int dd_data_size, bool xmit_can_sleep) 2729a4804cd6SMike Christie { 2730a4804cd6SMike Christie struct Scsi_Host *shost; 2731e5bd7b54SMike Christie struct iscsi_host *ihost; 2732a4804cd6SMike Christie 2733a4804cd6SMike Christie shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size); 2734a4804cd6SMike Christie if (!shost) 2735a4804cd6SMike Christie return NULL; 2736e5bd7b54SMike Christie ihost = shost_priv(shost); 273732ae763eSMike Christie 273832ae763eSMike Christie if (xmit_can_sleep) { 273932ae763eSMike Christie snprintf(ihost->workq_name, sizeof(ihost->workq_name), 274032ae763eSMike Christie "iscsi_q_%d", shost->host_no); 27413ce41966SBob Liu ihost->workq = alloc_workqueue("%s", 27423ce41966SBob Liu WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND, 27431a982620SBob Liu 1, ihost->workq_name); 274432ae763eSMike Christie if (!ihost->workq) 274532ae763eSMike Christie goto free_host; 274632ae763eSMike Christie } 274732ae763eSMike Christie 2748e5bd7b54SMike Christie spin_lock_init(&ihost->lock); 2749e5bd7b54SMike Christie ihost->state = ISCSI_HOST_SETUP; 2750e5bd7b54SMike Christie ihost->num_sessions = 0; 2751e5bd7b54SMike Christie init_waitqueue_head(&ihost->session_removal_wq); 2752a4804cd6SMike Christie return shost; 275332ae763eSMike Christie 275432ae763eSMike Christie free_host: 275532ae763eSMike Christie scsi_host_put(shost); 275632ae763eSMike Christie return NULL; 275775613521SMike Christie } 2758a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_alloc); 275975613521SMike Christie 2760e5bd7b54SMike Christie static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session) 2761e5bd7b54SMike Christie { 276240a06e75SMike Christie iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST); 2763e5bd7b54SMike Christie } 2764e5bd7b54SMike Christie 2765a4804cd6SMike Christie /** 2766a4804cd6SMike Christie * iscsi_host_remove - remove host and sessions 2767a4804cd6SMike Christie * @shost: scsi host 2768a4804cd6SMike Christie * 2769e5bd7b54SMike Christie * If there are any sessions left, this will initiate the removal and wait 2770e5bd7b54SMike Christie * for the completion. 2771a4804cd6SMike Christie */ 2772a4804cd6SMike Christie void iscsi_host_remove(struct Scsi_Host *shost) 2773a4804cd6SMike Christie { 2774e5bd7b54SMike Christie struct iscsi_host *ihost = shost_priv(shost); 2775e5bd7b54SMike Christie unsigned long flags; 2776e5bd7b54SMike Christie 2777e5bd7b54SMike Christie spin_lock_irqsave(&ihost->lock, flags); 2778e5bd7b54SMike Christie ihost->state = ISCSI_HOST_REMOVED; 2779e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 2780e5bd7b54SMike Christie 2781e5bd7b54SMike Christie iscsi_host_for_each_session(shost, iscsi_notify_host_removed); 2782e5bd7b54SMike Christie wait_event_interruptible(ihost->session_removal_wq, 2783e5bd7b54SMike Christie ihost->num_sessions == 0); 2784e5bd7b54SMike Christie if (signal_pending(current)) 2785e5bd7b54SMike Christie flush_signals(current); 2786e5bd7b54SMike Christie 2787a4804cd6SMike Christie scsi_remove_host(shost); 2788a4804cd6SMike Christie } 2789a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_remove); 2790a4804cd6SMike Christie 2791a4804cd6SMike Christie void iscsi_host_free(struct Scsi_Host *shost) 279275613521SMike Christie { 279375613521SMike Christie struct iscsi_host *ihost = shost_priv(shost); 279475613521SMike Christie 2795c435f0a9SMike Christie if (ihost->workq) 2796c435f0a9SMike Christie destroy_workqueue(ihost->workq); 2797c435f0a9SMike Christie 279875613521SMike Christie kfree(ihost->netdev); 279975613521SMike Christie kfree(ihost->hwaddress); 280075613521SMike Christie kfree(ihost->initiatorname); 2801a4804cd6SMike Christie scsi_host_put(shost); 280275613521SMike Christie } 2803a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_free); 280475613521SMike Christie 2805e5bd7b54SMike Christie static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost) 2806e5bd7b54SMike Christie { 2807e5bd7b54SMike Christie struct iscsi_host *ihost = shost_priv(shost); 2808e5bd7b54SMike Christie unsigned long flags; 2809e5bd7b54SMike Christie 2810e5bd7b54SMike Christie shost = scsi_host_get(shost); 2811e5bd7b54SMike Christie if (!shost) { 2812e5bd7b54SMike Christie printk(KERN_ERR "Invalid state. Cannot notify host removal " 2813e5bd7b54SMike Christie "of session teardown event because host already " 2814e5bd7b54SMike Christie "removed.\n"); 2815e5bd7b54SMike Christie return; 2816e5bd7b54SMike Christie } 2817e5bd7b54SMike Christie 2818e5bd7b54SMike Christie spin_lock_irqsave(&ihost->lock, flags); 2819e5bd7b54SMike Christie ihost->num_sessions--; 2820e5bd7b54SMike Christie if (ihost->num_sessions == 0) 2821e5bd7b54SMike Christie wake_up(&ihost->session_removal_wq); 2822e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 2823e5bd7b54SMike Christie scsi_host_put(shost); 2824e5bd7b54SMike Christie } 2825e5bd7b54SMike Christie 282675613521SMike Christie /** 282775613521SMike Christie * iscsi_session_setup - create iscsi cls session and host and session 282875613521SMike Christie * @iscsit: iscsi transport template 282975613521SMike Christie * @shost: scsi host 283075613521SMike Christie * @cmds_max: session can queue 2831ccd4a430SRandy Dunlap * @dd_size: private driver data size, added to session allocation size 28329c19a7d0SMike Christie * @cmd_task_size: LLD task private data size 283375613521SMike Christie * @initial_cmdsn: initial CmdSN 2834ccd4a430SRandy Dunlap * @id: target ID to add to this session 283575613521SMike Christie * 283675613521SMike Christie * This can be used by software iscsi_transports that allocate 283775613521SMike Christie * a session per scsi host. 28383cf7b233SMike Christie * 28393cf7b233SMike Christie * Callers should set cmds_max to the largest total numer (mgmt + scsi) of 28403cf7b233SMike Christie * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks 28413cf7b233SMike Christie * for nop handling and login/logout requests. 284275613521SMike Christie */ 284375613521SMike Christie struct iscsi_cls_session * 284475613521SMike Christie iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost, 2845b8b9e1b8SJayamohan Kallickal uint16_t cmds_max, int dd_size, int cmd_task_size, 28467970634bSMike Christie uint32_t initial_cmdsn, unsigned int id) 284775613521SMike Christie { 2848e5bd7b54SMike Christie struct iscsi_host *ihost = shost_priv(shost); 284975613521SMike Christie struct iscsi_session *session; 285075613521SMike Christie struct iscsi_cls_session *cls_session; 2851b4046922SMike Christie int cmd_i, scsi_cmds; 2852e5bd7b54SMike Christie unsigned long flags; 2853e5bd7b54SMike Christie 2854e5bd7b54SMike Christie spin_lock_irqsave(&ihost->lock, flags); 2855e5bd7b54SMike Christie if (ihost->state == ISCSI_HOST_REMOVED) { 2856e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 2857e5bd7b54SMike Christie return NULL; 2858e5bd7b54SMike Christie } 2859e5bd7b54SMike Christie ihost->num_sessions++; 2860e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 28618e9a20ceSMike Christie 2862b4046922SMike Christie scsi_cmds = iscsi_host_get_max_scsi_cmds(shost, cmds_max); 2863b4046922SMike Christie if (scsi_cmds < 0) 2864e5bd7b54SMike Christie goto dec_session_count; 28651548271eSMike Christie 28665d91e209SMike Christie cls_session = iscsi_alloc_session(shost, iscsit, 2867b8b9e1b8SJayamohan Kallickal sizeof(struct iscsi_session) + 2868b8b9e1b8SJayamohan Kallickal dd_size); 286975613521SMike Christie if (!cls_session) 2870e5bd7b54SMike Christie goto dec_session_count; 287175613521SMike Christie session = cls_session->dd_data; 287275613521SMike Christie session->cls_session = cls_session; 28737996a778SMike Christie session->host = shost; 28747996a778SMike Christie session->state = ISCSI_STATE_FREE; 2875f6d5180cSMike Christie session->fast_abort = 1; 28763fe5ae8bSMike Christie session->tgt_reset_timeout = 30; 28774cd49ea1SMike Christie session->lu_reset_timeout = 15; 28784cd49ea1SMike Christie session->abort_timeout = 10; 28793cf7b233SMike Christie session->scsi_cmds_max = scsi_cmds; 2880b4046922SMike Christie session->cmds_max = scsi_cmds + ISCSI_MGMT_CMDS_MAX; 2881e0726407SMike Christie session->queued_cmdsn = session->cmdsn = initial_cmdsn; 28827996a778SMike Christie session->exp_cmdsn = initial_cmdsn + 1; 28837996a778SMike Christie session->max_cmdsn = initial_cmdsn + 1; 28847996a778SMike Christie session->max_r2t = 1; 28857996a778SMike Christie session->tt = iscsit; 2886b8b9e1b8SJayamohan Kallickal session->dd_data = cls_session->dd_data + sizeof(*session); 2887659743b0SShlomo Pongratz 28886724add1SMike Christie mutex_init(&session->eh_mutex); 2889659743b0SShlomo Pongratz spin_lock_init(&session->frwd_lock); 2890659743b0SShlomo Pongratz spin_lock_init(&session->back_lock); 28917996a778SMike Christie 28927996a778SMike Christie /* initialize SCSI PDU commands pool */ 28937996a778SMike Christie if (iscsi_pool_init(&session->cmdpool, session->cmds_max, 28947996a778SMike Christie (void***)&session->cmds, 28959c19a7d0SMike Christie cmd_task_size + sizeof(struct iscsi_task))) 28967996a778SMike Christie goto cmdpool_alloc_fail; 28977996a778SMike Christie 28987996a778SMike Christie /* pre-format cmds pool with ITT */ 28997996a778SMike Christie for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { 29009c19a7d0SMike Christie struct iscsi_task *task = session->cmds[cmd_i]; 29017996a778SMike Christie 29029c19a7d0SMike Christie if (cmd_task_size) 29039c19a7d0SMike Christie task->dd_data = &task[1]; 29049c19a7d0SMike Christie task->itt = cmd_i; 29053bbaaad9SMike Christie task->state = ISCSI_TASK_FREE; 29069c19a7d0SMike Christie INIT_LIST_HEAD(&task->running); 29077996a778SMike Christie } 29087996a778SMike Christie 2909f53a88daSMike Christie if (!try_module_get(iscsit->owner)) 291075613521SMike Christie goto module_get_fail; 291175613521SMike Christie 29127970634bSMike Christie if (iscsi_add_session(cls_session, id)) 2913f53a88daSMike Christie goto cls_session_fail; 2914e5bd7b54SMike Christie 29157996a778SMike Christie return cls_session; 29167996a778SMike Christie 29177996a778SMike Christie cls_session_fail: 291875613521SMike Christie module_put(iscsit->owner); 291975613521SMike Christie module_get_fail: 29206320377fSOlaf Kirch iscsi_pool_free(&session->cmdpool); 29217996a778SMike Christie cmdpool_alloc_fail: 292275613521SMike Christie iscsi_free_session(cls_session); 2923e5bd7b54SMike Christie dec_session_count: 2924e5bd7b54SMike Christie iscsi_host_dec_session_cnt(shost); 29257996a778SMike Christie return NULL; 29267996a778SMike Christie } 29277996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_setup); 29287996a778SMike Christie 29297996a778SMike Christie /** 29307996a778SMike Christie * iscsi_session_teardown - destroy session, host, and cls_session 293175613521SMike Christie * @cls_session: iscsi session 293275613521SMike Christie */ 29337996a778SMike Christie void iscsi_session_teardown(struct iscsi_cls_session *cls_session) 29347996a778SMike Christie { 293575613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 293663f75cc8SMike Christie struct module *owner = cls_session->transport->owner; 2937e5bd7b54SMike Christie struct Scsi_Host *shost = session->host; 29387996a778SMike Christie 29396320377fSOlaf Kirch iscsi_pool_free(&session->cmdpool); 29407996a778SMike Christie 29419e10b512SKhazhismel Kumykov iscsi_remove_session(cls_session); 29429e10b512SKhazhismel Kumykov 2943b2c64167SMike Christie kfree(session->password); 2944b2c64167SMike Christie kfree(session->password_in); 2945b2c64167SMike Christie kfree(session->username); 2946b2c64167SMike Christie kfree(session->username_in); 2947f3ff0c36SMike Christie kfree(session->targetname); 29483c5c4801SVikas Chaudhary kfree(session->targetalias); 294988dfd340SMike Christie kfree(session->initiatorname); 29503b9373e9SEddie Wai kfree(session->boot_root); 29513b9373e9SEddie Wai kfree(session->boot_nic); 29523b9373e9SEddie Wai kfree(session->boot_target); 295388dfd340SMike Christie kfree(session->ifacename); 2954f8525eb4SAdheer Chandravanshi kfree(session->portal_type); 2955f8525eb4SAdheer Chandravanshi kfree(session->discovery_parent_type); 2956f3ff0c36SMike Christie 29579e10b512SKhazhismel Kumykov iscsi_free_session(cls_session); 29589e10b512SKhazhismel Kumykov 2959e5bd7b54SMike Christie iscsi_host_dec_session_cnt(shost); 296063f75cc8SMike Christie module_put(owner); 29617996a778SMike Christie } 29627996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_teardown); 29637996a778SMike Christie 29647996a778SMike Christie /** 29657996a778SMike Christie * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn 29667996a778SMike Christie * @cls_session: iscsi_cls_session 29675d91e209SMike Christie * @dd_size: private driver data size 29687996a778SMike Christie * @conn_idx: cid 29695d91e209SMike Christie */ 29707996a778SMike Christie struct iscsi_cls_conn * 29715d91e209SMike Christie iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size, 29725d91e209SMike Christie uint32_t conn_idx) 29737996a778SMike Christie { 297475613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 29757996a778SMike Christie struct iscsi_conn *conn; 29767996a778SMike Christie struct iscsi_cls_conn *cls_conn; 2977d36ab6f3SMike Christie char *data; 29787996a778SMike Christie 29795d91e209SMike Christie cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size, 29805d91e209SMike Christie conn_idx); 29817996a778SMike Christie if (!cls_conn) 29827996a778SMike Christie return NULL; 29837996a778SMike Christie conn = cls_conn->dd_data; 29845d91e209SMike Christie memset(conn, 0, sizeof(*conn) + dd_size); 29857996a778SMike Christie 29865d91e209SMike Christie conn->dd_data = cls_conn->dd_data + sizeof(*conn); 29877996a778SMike Christie conn->session = session; 29887996a778SMike Christie conn->cls_conn = cls_conn; 29897996a778SMike Christie conn->c_stage = ISCSI_CONN_INITIAL_STAGE; 29907996a778SMike Christie conn->id = conn_idx; 29917996a778SMike Christie conn->exp_statsn = 0; 2992843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 2993f6d5180cSMike Christie 29942c4b9637SKees Cook timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0); 2995f6d5180cSMike Christie 2996843c0a8aSMike Christie INIT_LIST_HEAD(&conn->mgmtqueue); 29973bbaaad9SMike Christie INIT_LIST_HEAD(&conn->cmdqueue); 2998843c0a8aSMike Christie INIT_LIST_HEAD(&conn->requeue); 2999c4028958SDavid Howells INIT_WORK(&conn->xmitwork, iscsi_xmitworker); 30007996a778SMike Christie 30019c19a7d0SMike Christie /* allocate login_task used for the login/text sequences */ 3002659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 30037acd72ebSStefani Seibold if (!kfifo_out(&session->cmdpool.queue, 30049c19a7d0SMike Christie (void*)&conn->login_task, 30057996a778SMike Christie sizeof(void*))) { 3006659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 30079c19a7d0SMike Christie goto login_task_alloc_fail; 30087996a778SMike Christie } 3009659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 30107996a778SMike Christie 3011cfeb2cf9SMike Christie data = (char *) __get_free_pages(GFP_KERNEL, 3012cfeb2cf9SMike Christie get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); 3013d36ab6f3SMike Christie if (!data) 30149c19a7d0SMike Christie goto login_task_data_alloc_fail; 30159c19a7d0SMike Christie conn->login_task->data = conn->data = data; 3016d36ab6f3SMike Christie 30172c4b9637SKees Cook timer_setup(&conn->tmf_timer, iscsi_tmf_timedout, 0); 30187996a778SMike Christie init_waitqueue_head(&conn->ehwait); 30197996a778SMike Christie 30207996a778SMike Christie return cls_conn; 30217996a778SMike Christie 30229c19a7d0SMike Christie login_task_data_alloc_fail: 30237acd72ebSStefani Seibold kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task, 3024d36ab6f3SMike Christie sizeof(void*)); 30259c19a7d0SMike Christie login_task_alloc_fail: 30267996a778SMike Christie iscsi_destroy_conn(cls_conn); 30277996a778SMike Christie return NULL; 30287996a778SMike Christie } 30297996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_setup); 30307996a778SMike Christie 30317996a778SMike Christie /** 30327996a778SMike Christie * iscsi_conn_teardown - teardown iscsi connection 3033ccd4a430SRandy Dunlap * @cls_conn: iscsi class connection 30347996a778SMike Christie * 30357996a778SMike Christie * TODO: we may need to make this into a two step process 30367996a778SMike Christie * like scsi-mls remove + put host 30377996a778SMike Christie */ 30387996a778SMike Christie void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn) 30397996a778SMike Christie { 30407996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 30417996a778SMike Christie struct iscsi_session *session = conn->session; 30427996a778SMike Christie 3043f6d5180cSMike Christie del_timer_sync(&conn->transport_timer); 3044f6d5180cSMike Christie 3045660d0831SJohn Soni Jose mutex_lock(&session->eh_mutex); 3046659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 30477996a778SMike Christie conn->c_stage = ISCSI_CONN_CLEANUP_WAIT; 30487996a778SMike Christie if (session->leadconn == conn) { 30497996a778SMike Christie /* 30507996a778SMike Christie * leading connection? then give up on recovery. 30517996a778SMike Christie */ 30527996a778SMike Christie session->state = ISCSI_STATE_TERMINATE; 30537996a778SMike Christie wake_up(&conn->ehwait); 30547996a778SMike Christie } 3055659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 30567996a778SMike Christie 3057779ea120SMike Christie /* flush queued up work because we free the connection below */ 3058843c0a8aSMike Christie iscsi_suspend_tx(conn); 3059779ea120SMike Christie 3060659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 3061cfeb2cf9SMike Christie free_pages((unsigned long) conn->data, 3062cfeb2cf9SMike Christie get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); 3063f3ff0c36SMike Christie kfree(conn->persistent_address); 3064ae56ff40SAdheer Chandravanshi kfree(conn->local_ipaddr); 3065659743b0SShlomo Pongratz /* regular RX path uses back_lock */ 3066659743b0SShlomo Pongratz spin_lock_bh(&session->back_lock); 30677acd72ebSStefani Seibold kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task, 30687996a778SMike Christie sizeof(void*)); 3069659743b0SShlomo Pongratz spin_unlock_bh(&session->back_lock); 3070e0726407SMike Christie if (session->leadconn == conn) 30717996a778SMike Christie session->leadconn = NULL; 3072659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 3073660d0831SJohn Soni Jose mutex_unlock(&session->eh_mutex); 30747996a778SMike Christie 30757996a778SMike Christie iscsi_destroy_conn(cls_conn); 30767996a778SMike Christie } 30777996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_teardown); 30787996a778SMike Christie 30797996a778SMike Christie int iscsi_conn_start(struct iscsi_cls_conn *cls_conn) 30807996a778SMike Christie { 30817996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 30827996a778SMike Christie struct iscsi_session *session = conn->session; 30837996a778SMike Christie 3084ffd0436eSMike Christie if (!session) { 3085322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 3086322d739dSMike Christie "can't start unbound connection\n"); 30877996a778SMike Christie return -EPERM; 30887996a778SMike Christie } 30897996a778SMike Christie 3090db98ccdeSMike Christie if ((session->imm_data_en || !session->initial_r2t_en) && 3091db98ccdeSMike Christie session->first_burst > session->max_burst) { 3092322d739dSMike Christie iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: " 3093ffd0436eSMike Christie "first_burst %d max_burst %d\n", 3094ffd0436eSMike Christie session->first_burst, session->max_burst); 3095ffd0436eSMike Christie return -EINVAL; 3096ffd0436eSMike Christie } 3097ffd0436eSMike Christie 3098f6d5180cSMike Christie if (conn->ping_timeout && !conn->recv_timeout) { 3099322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of " 3100322d739dSMike Christie "zero. Using 5 seconds\n."); 3101f6d5180cSMike Christie conn->recv_timeout = 5; 3102f6d5180cSMike Christie } 3103f6d5180cSMike Christie 3104f6d5180cSMike Christie if (conn->recv_timeout && !conn->ping_timeout) { 3105322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of " 3106322d739dSMike Christie "zero. Using 5 seconds.\n"); 3107f6d5180cSMike Christie conn->ping_timeout = 5; 3108f6d5180cSMike Christie } 3109f6d5180cSMike Christie 3110659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 31117996a778SMike Christie conn->c_stage = ISCSI_CONN_STARTED; 31127996a778SMike Christie session->state = ISCSI_STATE_LOGGED_IN; 3113e0726407SMike Christie session->queued_cmdsn = session->cmdsn; 31147996a778SMike Christie 3115f6d5180cSMike Christie conn->last_recv = jiffies; 3116f6d5180cSMike Christie conn->last_ping = jiffies; 3117f6d5180cSMike Christie if (conn->recv_timeout && conn->ping_timeout) 3118f6d5180cSMike Christie mod_timer(&conn->transport_timer, 3119f6d5180cSMike Christie jiffies + (conn->recv_timeout * HZ)); 3120f6d5180cSMike Christie 31217996a778SMike Christie switch(conn->stop_stage) { 31227996a778SMike Christie case STOP_CONN_RECOVER: 31237996a778SMike Christie /* 31247996a778SMike Christie * unblock eh_abort() if it is blocked. re-try all 31257996a778SMike Christie * commands after successful recovery 31267996a778SMike Christie */ 31277996a778SMike Christie conn->stop_stage = 0; 3128843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 31297996a778SMike Christie session->age++; 31308b1d0343SMike Christie if (session->age == 16) 31318b1d0343SMike Christie session->age = 0; 31326eabafbeSMike Christie break; 31337996a778SMike Christie case STOP_CONN_TERM: 31347996a778SMike Christie conn->stop_stage = 0; 31357996a778SMike Christie break; 31367996a778SMike Christie default: 31377996a778SMike Christie break; 31387996a778SMike Christie } 3139659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 31407996a778SMike Christie 314175613521SMike Christie iscsi_unblock_session(session->cls_session); 31426eabafbeSMike Christie wake_up(&conn->ehwait); 31437996a778SMike Christie return 0; 31447996a778SMike Christie } 31457996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_start); 31467996a778SMike Christie 31477996a778SMike Christie static void 31483bbaaad9SMike Christie fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn) 31497996a778SMike Christie { 31503bbaaad9SMike Christie struct iscsi_task *task; 3151b3cd5050SMike Christie int i, state; 31527996a778SMike Christie 31533bbaaad9SMike Christie for (i = 0; i < conn->session->cmds_max; i++) { 31543bbaaad9SMike Christie task = conn->session->cmds[i]; 31553bbaaad9SMike Christie if (task->sc) 31563bbaaad9SMike Christie continue; 31573bbaaad9SMike Christie 31583bbaaad9SMike Christie if (task->state == ISCSI_TASK_FREE) 31593bbaaad9SMike Christie continue; 31603bbaaad9SMike Christie 31613bbaaad9SMike Christie ISCSI_DBG_SESSION(conn->session, 31623bbaaad9SMike Christie "failing mgmt itt 0x%x state %d\n", 31633bbaaad9SMike Christie task->itt, task->state); 31645923d64bSMike Christie 31655923d64bSMike Christie spin_lock_bh(&session->back_lock); 31665923d64bSMike Christie if (cleanup_queued_task(task)) { 31675923d64bSMike Christie spin_unlock_bh(&session->back_lock); 31685923d64bSMike Christie continue; 31695923d64bSMike Christie } 31705923d64bSMike Christie 3171b3cd5050SMike Christie state = ISCSI_TASK_ABRT_SESS_RECOV; 3172b3cd5050SMike Christie if (task->state == ISCSI_TASK_PENDING) 3173b3cd5050SMike Christie state = ISCSI_TASK_COMPLETED; 3174b3cd5050SMike Christie iscsi_complete_task(task, state); 3175a656183eSLee Duncan spin_unlock_bh(&session->back_lock); 31767996a778SMike Christie } 31777996a778SMike Christie } 31787996a778SMike Christie 31790dcf8febSMike Christie void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) 31807996a778SMike Christie { 31810dcf8febSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 31820dcf8febSMike Christie struct iscsi_session *session = conn->session; 3183ed2abc7fSMike Christie int old_stop_stage; 3184ed2abc7fSMike Christie 31856724add1SMike Christie mutex_lock(&session->eh_mutex); 3186659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 3187ed2abc7fSMike Christie if (conn->stop_stage == STOP_CONN_TERM) { 3188659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 31896724add1SMike Christie mutex_unlock(&session->eh_mutex); 31906724add1SMike Christie return; 31916724add1SMike Christie } 31926724add1SMike Christie 31936724add1SMike Christie /* 3194ed2abc7fSMike Christie * When this is called for the in_login state, we only want to clean 31959c19a7d0SMike Christie * up the login task and connection. We do not need to block and set 319667a61114SMike Christie * the recovery state again 3197ed2abc7fSMike Christie */ 319867a61114SMike Christie if (flag == STOP_CONN_TERM) 319967a61114SMike Christie session->state = ISCSI_STATE_TERMINATE; 320067a61114SMike Christie else if (conn->stop_stage != STOP_CONN_RECOVER) 320167a61114SMike Christie session->state = ISCSI_STATE_IN_RECOVERY; 32024ae0a6c1SMike Christie 32034ae0a6c1SMike Christie old_stop_stage = conn->stop_stage; 32044ae0a6c1SMike Christie conn->stop_stage = flag; 3205659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 3206ed2abc7fSMike Christie 320726013ad4SMike Christie del_timer_sync(&conn->transport_timer); 320826013ad4SMike Christie iscsi_suspend_tx(conn); 320926013ad4SMike Christie 3210659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 321167a61114SMike Christie conn->c_stage = ISCSI_CONN_STOPPED; 3212659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 32136724add1SMike Christie 32147996a778SMike Christie /* 32157996a778SMike Christie * for connection level recovery we should not calculate 32167996a778SMike Christie * header digest. conn->hdr_size used for optimization 32177996a778SMike Christie * in hdr_extract() and will be re-negotiated at 32187996a778SMike Christie * set_param() time. 32197996a778SMike Christie */ 32207996a778SMike Christie if (flag == STOP_CONN_RECOVER) { 32217996a778SMike Christie conn->hdrdgst_en = 0; 32227996a778SMike Christie conn->datadgst_en = 0; 3223656cffc9SMike Christie if (session->state == ISCSI_STATE_IN_RECOVERY && 322467a61114SMike Christie old_stop_stage != STOP_CONN_RECOVER) { 32251b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "blocking session\n"); 322675613521SMike Christie iscsi_block_session(session->cls_session); 32277996a778SMike Christie } 322867a61114SMike Christie } 3229656cffc9SMike Christie 3230656cffc9SMike Christie /* 3231656cffc9SMike Christie * flush queues. 3232656cffc9SMike Christie */ 3233659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 32343bbaaad9SMike Christie fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED); 32353bbaaad9SMike Christie fail_mgmt_tasks(session, conn); 32365d12c05eSMike Christie memset(&conn->tmhdr, 0, sizeof(conn->tmhdr)); 3237659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 32386724add1SMike Christie mutex_unlock(&session->eh_mutex); 32397996a778SMike Christie } 32407996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_stop); 32417996a778SMike Christie 32427996a778SMike Christie int iscsi_conn_bind(struct iscsi_cls_session *cls_session, 32437996a778SMike Christie struct iscsi_cls_conn *cls_conn, int is_leading) 32447996a778SMike Christie { 324575613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 324698644047SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 32477996a778SMike Christie 3248659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 32497996a778SMike Christie if (is_leading) 32507996a778SMike Christie session->leadconn = conn; 3251659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 32527996a778SMike Christie 32537996a778SMike Christie /* 3254c8447e4cSMike Christie * The target could have reduced it's window size between logins, so 3255c8447e4cSMike Christie * we have to reset max/exp cmdsn so we can see the new values. 3256c8447e4cSMike Christie */ 3257c8447e4cSMike Christie spin_lock_bh(&session->back_lock); 3258c8447e4cSMike Christie session->max_cmdsn = session->exp_cmdsn = session->cmdsn + 1; 3259c8447e4cSMike Christie spin_unlock_bh(&session->back_lock); 3260c8447e4cSMike Christie /* 32617996a778SMike Christie * Unblock xmitworker(), Login Phase will pass through. 32627996a778SMike Christie */ 32637996a778SMike Christie clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); 32647996a778SMike Christie clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 32657996a778SMike Christie return 0; 32667996a778SMike Christie } 32677996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_bind); 32687996a778SMike Christie 3269adaf6990SAdheer Chandravanshi int iscsi_switch_str_param(char **param, char *new_val_buf) 32705700b1afSMike Christie { 32715700b1afSMike Christie char *new_val; 32725700b1afSMike Christie 32735700b1afSMike Christie if (*param) { 32745700b1afSMike Christie if (!strcmp(*param, new_val_buf)) 32755700b1afSMike Christie return 0; 32765700b1afSMike Christie } 32775700b1afSMike Christie 32785700b1afSMike Christie new_val = kstrdup(new_val_buf, GFP_NOIO); 32795700b1afSMike Christie if (!new_val) 32805700b1afSMike Christie return -ENOMEM; 32815700b1afSMike Christie 32825700b1afSMike Christie kfree(*param); 32835700b1afSMike Christie *param = new_val; 32845700b1afSMike Christie return 0; 32855700b1afSMike Christie } 3286adaf6990SAdheer Chandravanshi EXPORT_SYMBOL_GPL(iscsi_switch_str_param); 3287a54a52caSMike Christie 3288a54a52caSMike Christie int iscsi_set_param(struct iscsi_cls_conn *cls_conn, 3289a54a52caSMike Christie enum iscsi_param param, char *buf, int buflen) 3290a54a52caSMike Christie { 3291a54a52caSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 3292a54a52caSMike Christie struct iscsi_session *session = conn->session; 32936a06a4b8SOr Gerlitz int val; 3294a54a52caSMike Christie 3295a54a52caSMike Christie switch(param) { 3296843c0a8aSMike Christie case ISCSI_PARAM_FAST_ABORT: 3297843c0a8aSMike Christie sscanf(buf, "%d", &session->fast_abort); 3298843c0a8aSMike Christie break; 3299f6d5180cSMike Christie case ISCSI_PARAM_ABORT_TMO: 3300f6d5180cSMike Christie sscanf(buf, "%d", &session->abort_timeout); 3301f6d5180cSMike Christie break; 3302f6d5180cSMike Christie case ISCSI_PARAM_LU_RESET_TMO: 3303f6d5180cSMike Christie sscanf(buf, "%d", &session->lu_reset_timeout); 3304f6d5180cSMike Christie break; 33053fe5ae8bSMike Christie case ISCSI_PARAM_TGT_RESET_TMO: 33063fe5ae8bSMike Christie sscanf(buf, "%d", &session->tgt_reset_timeout); 33073fe5ae8bSMike Christie break; 3308f6d5180cSMike Christie case ISCSI_PARAM_PING_TMO: 3309f6d5180cSMike Christie sscanf(buf, "%d", &conn->ping_timeout); 3310f6d5180cSMike Christie break; 3311f6d5180cSMike Christie case ISCSI_PARAM_RECV_TMO: 3312f6d5180cSMike Christie sscanf(buf, "%d", &conn->recv_timeout); 3313f6d5180cSMike Christie break; 3314a54a52caSMike Christie case ISCSI_PARAM_MAX_RECV_DLENGTH: 3315a54a52caSMike Christie sscanf(buf, "%d", &conn->max_recv_dlength); 3316a54a52caSMike Christie break; 3317a54a52caSMike Christie case ISCSI_PARAM_MAX_XMIT_DLENGTH: 3318a54a52caSMike Christie sscanf(buf, "%d", &conn->max_xmit_dlength); 3319a54a52caSMike Christie break; 3320a54a52caSMike Christie case ISCSI_PARAM_HDRDGST_EN: 3321a54a52caSMike Christie sscanf(buf, "%d", &conn->hdrdgst_en); 3322a54a52caSMike Christie break; 3323a54a52caSMike Christie case ISCSI_PARAM_DATADGST_EN: 3324a54a52caSMike Christie sscanf(buf, "%d", &conn->datadgst_en); 3325a54a52caSMike Christie break; 3326a54a52caSMike Christie case ISCSI_PARAM_INITIAL_R2T_EN: 3327a54a52caSMike Christie sscanf(buf, "%d", &session->initial_r2t_en); 3328a54a52caSMike Christie break; 3329a54a52caSMike Christie case ISCSI_PARAM_MAX_R2T: 33301304be5fSMike Christie sscanf(buf, "%hu", &session->max_r2t); 3331a54a52caSMike Christie break; 3332a54a52caSMike Christie case ISCSI_PARAM_IMM_DATA_EN: 3333a54a52caSMike Christie sscanf(buf, "%d", &session->imm_data_en); 3334a54a52caSMike Christie break; 3335a54a52caSMike Christie case ISCSI_PARAM_FIRST_BURST: 3336a54a52caSMike Christie sscanf(buf, "%d", &session->first_burst); 3337a54a52caSMike Christie break; 3338a54a52caSMike Christie case ISCSI_PARAM_MAX_BURST: 3339a54a52caSMike Christie sscanf(buf, "%d", &session->max_burst); 3340a54a52caSMike Christie break; 3341a54a52caSMike Christie case ISCSI_PARAM_PDU_INORDER_EN: 3342a54a52caSMike Christie sscanf(buf, "%d", &session->pdu_inorder_en); 3343a54a52caSMike Christie break; 3344a54a52caSMike Christie case ISCSI_PARAM_DATASEQ_INORDER_EN: 3345a54a52caSMike Christie sscanf(buf, "%d", &session->dataseq_inorder_en); 3346a54a52caSMike Christie break; 3347a54a52caSMike Christie case ISCSI_PARAM_ERL: 3348a54a52caSMike Christie sscanf(buf, "%d", &session->erl); 3349a54a52caSMike Christie break; 3350a54a52caSMike Christie case ISCSI_PARAM_EXP_STATSN: 3351a54a52caSMike Christie sscanf(buf, "%u", &conn->exp_statsn); 3352a54a52caSMike Christie break; 3353b2c64167SMike Christie case ISCSI_PARAM_USERNAME: 33545700b1afSMike Christie return iscsi_switch_str_param(&session->username, buf); 3355b2c64167SMike Christie case ISCSI_PARAM_USERNAME_IN: 33565700b1afSMike Christie return iscsi_switch_str_param(&session->username_in, buf); 3357b2c64167SMike Christie case ISCSI_PARAM_PASSWORD: 33585700b1afSMike Christie return iscsi_switch_str_param(&session->password, buf); 3359b2c64167SMike Christie case ISCSI_PARAM_PASSWORD_IN: 33605700b1afSMike Christie return iscsi_switch_str_param(&session->password_in, buf); 3361a54a52caSMike Christie case ISCSI_PARAM_TARGET_NAME: 33625700b1afSMike Christie return iscsi_switch_str_param(&session->targetname, buf); 33633c5c4801SVikas Chaudhary case ISCSI_PARAM_TARGET_ALIAS: 33643c5c4801SVikas Chaudhary return iscsi_switch_str_param(&session->targetalias, buf); 3365a54a52caSMike Christie case ISCSI_PARAM_TPGT: 3366a54a52caSMike Christie sscanf(buf, "%d", &session->tpgt); 3367a54a52caSMike Christie break; 3368a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_PORT: 3369a54a52caSMike Christie sscanf(buf, "%d", &conn->persistent_port); 3370a54a52caSMike Christie break; 3371a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_ADDRESS: 33725700b1afSMike Christie return iscsi_switch_str_param(&conn->persistent_address, buf); 337388dfd340SMike Christie case ISCSI_PARAM_IFACE_NAME: 33745700b1afSMike Christie return iscsi_switch_str_param(&session->ifacename, buf); 337588dfd340SMike Christie case ISCSI_PARAM_INITIATOR_NAME: 33765700b1afSMike Christie return iscsi_switch_str_param(&session->initiatorname, buf); 33773b9373e9SEddie Wai case ISCSI_PARAM_BOOT_ROOT: 33783b9373e9SEddie Wai return iscsi_switch_str_param(&session->boot_root, buf); 33793b9373e9SEddie Wai case ISCSI_PARAM_BOOT_NIC: 33803b9373e9SEddie Wai return iscsi_switch_str_param(&session->boot_nic, buf); 33813b9373e9SEddie Wai case ISCSI_PARAM_BOOT_TARGET: 33823b9373e9SEddie Wai return iscsi_switch_str_param(&session->boot_target, buf); 3383f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_PORTAL_TYPE: 3384f8525eb4SAdheer Chandravanshi return iscsi_switch_str_param(&session->portal_type, buf); 3385f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_PARENT_TYPE: 3386f8525eb4SAdheer Chandravanshi return iscsi_switch_str_param(&session->discovery_parent_type, 3387f8525eb4SAdheer Chandravanshi buf); 33886a06a4b8SOr Gerlitz case ISCSI_PARAM_DISCOVERY_SESS: 33896a06a4b8SOr Gerlitz sscanf(buf, "%d", &val); 33906a06a4b8SOr Gerlitz session->discovery_sess = !!val; 33916a06a4b8SOr Gerlitz break; 3392ae56ff40SAdheer Chandravanshi case ISCSI_PARAM_LOCAL_IPADDR: 3393ae56ff40SAdheer Chandravanshi return iscsi_switch_str_param(&conn->local_ipaddr, buf); 3394a54a52caSMike Christie default: 3395a54a52caSMike Christie return -ENOSYS; 3396a54a52caSMike Christie } 3397a54a52caSMike Christie 3398a54a52caSMike Christie return 0; 3399a54a52caSMike Christie } 3400a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_set_param); 3401a54a52caSMike Christie 3402a54a52caSMike Christie int iscsi_session_get_param(struct iscsi_cls_session *cls_session, 3403a54a52caSMike Christie enum iscsi_param param, char *buf) 3404a54a52caSMike Christie { 340575613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 3406a54a52caSMike Christie int len; 3407a54a52caSMike Christie 3408a54a52caSMike Christie switch(param) { 3409843c0a8aSMike Christie case ISCSI_PARAM_FAST_ABORT: 3410ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->fast_abort); 3411843c0a8aSMike Christie break; 3412f6d5180cSMike Christie case ISCSI_PARAM_ABORT_TMO: 3413ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->abort_timeout); 3414f6d5180cSMike Christie break; 3415f6d5180cSMike Christie case ISCSI_PARAM_LU_RESET_TMO: 3416ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->lu_reset_timeout); 3417f6d5180cSMike Christie break; 34183fe5ae8bSMike Christie case ISCSI_PARAM_TGT_RESET_TMO: 3419ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->tgt_reset_timeout); 34203fe5ae8bSMike Christie break; 3421a54a52caSMike Christie case ISCSI_PARAM_INITIAL_R2T_EN: 3422ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->initial_r2t_en); 3423a54a52caSMike Christie break; 3424a54a52caSMike Christie case ISCSI_PARAM_MAX_R2T: 3425ec98ea70SChris Leech len = sysfs_emit(buf, "%hu\n", session->max_r2t); 3426a54a52caSMike Christie break; 3427a54a52caSMike Christie case ISCSI_PARAM_IMM_DATA_EN: 3428ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->imm_data_en); 3429a54a52caSMike Christie break; 3430a54a52caSMike Christie case ISCSI_PARAM_FIRST_BURST: 3431ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->first_burst); 3432a54a52caSMike Christie break; 3433a54a52caSMike Christie case ISCSI_PARAM_MAX_BURST: 3434ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->max_burst); 3435a54a52caSMike Christie break; 3436a54a52caSMike Christie case ISCSI_PARAM_PDU_INORDER_EN: 3437ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->pdu_inorder_en); 3438a54a52caSMike Christie break; 3439a54a52caSMike Christie case ISCSI_PARAM_DATASEQ_INORDER_EN: 3440ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->dataseq_inorder_en); 3441a54a52caSMike Christie break; 34425f09e1f3SAdheer Chandravanshi case ISCSI_PARAM_DEF_TASKMGMT_TMO: 3443ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->def_taskmgmt_tmo); 34445f09e1f3SAdheer Chandravanshi break; 3445a54a52caSMike Christie case ISCSI_PARAM_ERL: 3446ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->erl); 3447a54a52caSMike Christie break; 3448a54a52caSMike Christie case ISCSI_PARAM_TARGET_NAME: 3449ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->targetname); 3450a54a52caSMike Christie break; 34513c5c4801SVikas Chaudhary case ISCSI_PARAM_TARGET_ALIAS: 3452ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->targetalias); 34533c5c4801SVikas Chaudhary break; 3454a54a52caSMike Christie case ISCSI_PARAM_TPGT: 3455ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->tpgt); 3456a54a52caSMike Christie break; 3457b2c64167SMike Christie case ISCSI_PARAM_USERNAME: 3458ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->username); 3459b2c64167SMike Christie break; 3460b2c64167SMike Christie case ISCSI_PARAM_USERNAME_IN: 3461ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->username_in); 3462b2c64167SMike Christie break; 3463b2c64167SMike Christie case ISCSI_PARAM_PASSWORD: 3464ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->password); 3465b2c64167SMike Christie break; 3466b2c64167SMike Christie case ISCSI_PARAM_PASSWORD_IN: 3467ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->password_in); 3468b2c64167SMike Christie break; 346988dfd340SMike Christie case ISCSI_PARAM_IFACE_NAME: 3470ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->ifacename); 347188dfd340SMike Christie break; 347288dfd340SMike Christie case ISCSI_PARAM_INITIATOR_NAME: 3473ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->initiatorname); 347488dfd340SMike Christie break; 34753b9373e9SEddie Wai case ISCSI_PARAM_BOOT_ROOT: 3476ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->boot_root); 34773b9373e9SEddie Wai break; 34783b9373e9SEddie Wai case ISCSI_PARAM_BOOT_NIC: 3479ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->boot_nic); 34803b9373e9SEddie Wai break; 34813b9373e9SEddie Wai case ISCSI_PARAM_BOOT_TARGET: 3482ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->boot_target); 34839a2307b1SAdheer Chandravanshi break; 3484f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_AUTO_SND_TGT_DISABLE: 3485ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->auto_snd_tgt_disable); 3486f8525eb4SAdheer Chandravanshi break; 3487f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_SESS: 3488ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->discovery_sess); 3489f8525eb4SAdheer Chandravanshi break; 3490f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_PORTAL_TYPE: 3491ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", session->portal_type); 3492f8525eb4SAdheer Chandravanshi break; 3493f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_CHAP_AUTH_EN: 3494ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->chap_auth_en); 3495f8525eb4SAdheer Chandravanshi break; 3496f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_LOGOUT_EN: 3497ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->discovery_logout_en); 3498f8525eb4SAdheer Chandravanshi break; 3499f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_BIDI_CHAP_EN: 3500ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->bidi_chap_en); 3501f8525eb4SAdheer Chandravanshi break; 3502f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL: 3503ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->discovery_auth_optional); 3504f8525eb4SAdheer Chandravanshi break; 3505f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DEF_TIME2WAIT: 3506ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->time2wait); 3507f8525eb4SAdheer Chandravanshi break; 3508f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DEF_TIME2RETAIN: 3509ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", session->time2retain); 3510f8525eb4SAdheer Chandravanshi break; 3511f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TSID: 3512ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->tsid); 3513f8525eb4SAdheer Chandravanshi break; 3514f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_ISID: 3515ec98ea70SChris Leech len = sysfs_emit(buf, "%02x%02x%02x%02x%02x%02x\n", 3516f8525eb4SAdheer Chandravanshi session->isid[0], session->isid[1], 3517f8525eb4SAdheer Chandravanshi session->isid[2], session->isid[3], 3518f8525eb4SAdheer Chandravanshi session->isid[4], session->isid[5]); 3519f8525eb4SAdheer Chandravanshi break; 3520f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_PARENT_IDX: 3521ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", session->discovery_parent_idx); 3522f8525eb4SAdheer Chandravanshi break; 3523f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_PARENT_TYPE: 3524f8525eb4SAdheer Chandravanshi if (session->discovery_parent_type) 3525ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", 3526f8525eb4SAdheer Chandravanshi session->discovery_parent_type); 3527f8525eb4SAdheer Chandravanshi else 3528ec98ea70SChris Leech len = sysfs_emit(buf, "\n"); 35293b9373e9SEddie Wai break; 3530a54a52caSMike Christie default: 3531a54a52caSMike Christie return -ENOSYS; 3532a54a52caSMike Christie } 3533a54a52caSMike Christie 3534a54a52caSMike Christie return len; 3535a54a52caSMike Christie } 3536a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_get_param); 3537a54a52caSMike Christie 353800f3708eSMike Christie int iscsi_conn_get_addr_param(struct sockaddr_storage *addr, 353900f3708eSMike Christie enum iscsi_param param, char *buf) 354000f3708eSMike Christie { 354100f3708eSMike Christie struct sockaddr_in6 *sin6 = NULL; 354200f3708eSMike Christie struct sockaddr_in *sin = NULL; 354300f3708eSMike Christie int len; 354400f3708eSMike Christie 354500f3708eSMike Christie switch (addr->ss_family) { 354600f3708eSMike Christie case AF_INET: 354700f3708eSMike Christie sin = (struct sockaddr_in *)addr; 354800f3708eSMike Christie break; 354900f3708eSMike Christie case AF_INET6: 355000f3708eSMike Christie sin6 = (struct sockaddr_in6 *)addr; 355100f3708eSMike Christie break; 355200f3708eSMike Christie default: 355300f3708eSMike Christie return -EINVAL; 355400f3708eSMike Christie } 355500f3708eSMike Christie 355600f3708eSMike Christie switch (param) { 355700f3708eSMike Christie case ISCSI_PARAM_CONN_ADDRESS: 355800f3708eSMike Christie case ISCSI_HOST_PARAM_IPADDRESS: 355900f3708eSMike Christie if (sin) 3560ec98ea70SChris Leech len = sysfs_emit(buf, "%pI4\n", &sin->sin_addr.s_addr); 356100f3708eSMike Christie else 3562ec98ea70SChris Leech len = sysfs_emit(buf, "%pI6\n", &sin6->sin6_addr); 356300f3708eSMike Christie break; 356400f3708eSMike Christie case ISCSI_PARAM_CONN_PORT: 35654bfb8ebfSMike Christie case ISCSI_PARAM_LOCAL_PORT: 356600f3708eSMike Christie if (sin) 3567ec98ea70SChris Leech len = sysfs_emit(buf, "%hu\n", be16_to_cpu(sin->sin_port)); 356800f3708eSMike Christie else 3569ec98ea70SChris Leech len = sysfs_emit(buf, "%hu\n", 357000f3708eSMike Christie be16_to_cpu(sin6->sin6_port)); 357100f3708eSMike Christie break; 357200f3708eSMike Christie default: 357300f3708eSMike Christie return -EINVAL; 357400f3708eSMike Christie } 357500f3708eSMike Christie 357600f3708eSMike Christie return len; 357700f3708eSMike Christie } 357800f3708eSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param); 357900f3708eSMike Christie 3580a54a52caSMike Christie int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn, 3581a54a52caSMike Christie enum iscsi_param param, char *buf) 3582a54a52caSMike Christie { 3583a54a52caSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 3584a54a52caSMike Christie int len; 3585a54a52caSMike Christie 3586a54a52caSMike Christie switch(param) { 3587f6d5180cSMike Christie case ISCSI_PARAM_PING_TMO: 3588ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->ping_timeout); 3589f6d5180cSMike Christie break; 3590f6d5180cSMike Christie case ISCSI_PARAM_RECV_TMO: 3591ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->recv_timeout); 3592f6d5180cSMike Christie break; 3593a54a52caSMike Christie case ISCSI_PARAM_MAX_RECV_DLENGTH: 3594ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->max_recv_dlength); 3595a54a52caSMike Christie break; 3596a54a52caSMike Christie case ISCSI_PARAM_MAX_XMIT_DLENGTH: 3597ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->max_xmit_dlength); 3598a54a52caSMike Christie break; 3599a54a52caSMike Christie case ISCSI_PARAM_HDRDGST_EN: 3600ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", conn->hdrdgst_en); 3601a54a52caSMike Christie break; 3602a54a52caSMike Christie case ISCSI_PARAM_DATADGST_EN: 3603ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", conn->datadgst_en); 3604a54a52caSMike Christie break; 3605a54a52caSMike Christie case ISCSI_PARAM_IFMARKER_EN: 3606ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", conn->ifmarker_en); 3607a54a52caSMike Christie break; 3608a54a52caSMike Christie case ISCSI_PARAM_OFMARKER_EN: 3609ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", conn->ofmarker_en); 3610a54a52caSMike Christie break; 3611a54a52caSMike Christie case ISCSI_PARAM_EXP_STATSN: 3612ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->exp_statsn); 3613a54a52caSMike Christie break; 3614a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_PORT: 3615ec98ea70SChris Leech len = sysfs_emit(buf, "%d\n", conn->persistent_port); 3616a54a52caSMike Christie break; 3617a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_ADDRESS: 3618ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", conn->persistent_address); 3619a54a52caSMike Christie break; 3620f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_STATSN: 3621ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->statsn); 3622f8525eb4SAdheer Chandravanshi break; 3623f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_MAX_SEGMENT_SIZE: 3624ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->max_segment_size); 3625f8525eb4SAdheer Chandravanshi break; 3626f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_KEEPALIVE_TMO: 3627ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->keepalive_tmo); 3628f8525eb4SAdheer Chandravanshi break; 3629f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_LOCAL_PORT: 3630ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->local_port); 3631f8525eb4SAdheer Chandravanshi break; 3632f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_TIMESTAMP_STAT: 3633ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_stat); 3634f8525eb4SAdheer Chandravanshi break; 3635f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_NAGLE_DISABLE: 3636ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->tcp_nagle_disable); 3637f8525eb4SAdheer Chandravanshi break; 3638f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_WSF_DISABLE: 3639ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->tcp_wsf_disable); 3640f8525eb4SAdheer Chandravanshi break; 3641f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_TIMER_SCALE: 3642ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->tcp_timer_scale); 3643f8525eb4SAdheer Chandravanshi break; 3644f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_TIMESTAMP_EN: 3645ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_en); 3646f8525eb4SAdheer Chandravanshi break; 3647f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_IP_FRAGMENT_DISABLE: 3648ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->fragment_disable); 3649f8525eb4SAdheer Chandravanshi break; 3650f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_IPV4_TOS: 3651ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->ipv4_tos); 3652f8525eb4SAdheer Chandravanshi break; 3653f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_IPV6_TC: 3654ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->ipv6_traffic_class); 3655f8525eb4SAdheer Chandravanshi break; 36565f09e1f3SAdheer Chandravanshi case ISCSI_PARAM_IPV6_FLOW_LABEL: 3657ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->ipv6_flow_label); 36585f09e1f3SAdheer Chandravanshi break; 3659f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6: 3660ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->is_fw_assigned_ipv6); 3661f8525eb4SAdheer Chandravanshi break; 3662f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_XMIT_WSF: 3663ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->tcp_xmit_wsf); 3664f8525eb4SAdheer Chandravanshi break; 3665f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_RECV_WSF: 3666ec98ea70SChris Leech len = sysfs_emit(buf, "%u\n", conn->tcp_recv_wsf); 3667f8525eb4SAdheer Chandravanshi break; 3668ae56ff40SAdheer Chandravanshi case ISCSI_PARAM_LOCAL_IPADDR: 3669ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", conn->local_ipaddr); 3670ae56ff40SAdheer Chandravanshi break; 3671a54a52caSMike Christie default: 3672a54a52caSMike Christie return -ENOSYS; 3673a54a52caSMike Christie } 3674a54a52caSMike Christie 3675a54a52caSMike Christie return len; 3676a54a52caSMike Christie } 3677a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_param); 3678a54a52caSMike Christie 36790801c242SMike Christie int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param, 36800801c242SMike Christie char *buf) 36810801c242SMike Christie { 368275613521SMike Christie struct iscsi_host *ihost = shost_priv(shost); 36830801c242SMike Christie int len; 36840801c242SMike Christie 36850801c242SMike Christie switch (param) { 3686d8196ed2SMike Christie case ISCSI_HOST_PARAM_NETDEV_NAME: 3687ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", ihost->netdev); 3688d8196ed2SMike Christie break; 36890801c242SMike Christie case ISCSI_HOST_PARAM_HWADDRESS: 3690ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", ihost->hwaddress); 36910801c242SMike Christie break; 36928ad5781aSMike Christie case ISCSI_HOST_PARAM_INITIATOR_NAME: 3693ec98ea70SChris Leech len = sysfs_emit(buf, "%s\n", ihost->initiatorname); 36948ad5781aSMike Christie break; 36950801c242SMike Christie default: 36960801c242SMike Christie return -ENOSYS; 36970801c242SMike Christie } 36980801c242SMike Christie 36990801c242SMike Christie return len; 37000801c242SMike Christie } 37010801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_get_param); 37020801c242SMike Christie 37030801c242SMike Christie int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param, 37040801c242SMike Christie char *buf, int buflen) 37050801c242SMike Christie { 370675613521SMike Christie struct iscsi_host *ihost = shost_priv(shost); 37070801c242SMike Christie 37080801c242SMike Christie switch (param) { 3709d8196ed2SMike Christie case ISCSI_HOST_PARAM_NETDEV_NAME: 37105700b1afSMike Christie return iscsi_switch_str_param(&ihost->netdev, buf); 37110801c242SMike Christie case ISCSI_HOST_PARAM_HWADDRESS: 37125700b1afSMike Christie return iscsi_switch_str_param(&ihost->hwaddress, buf); 37138ad5781aSMike Christie case ISCSI_HOST_PARAM_INITIATOR_NAME: 37145700b1afSMike Christie return iscsi_switch_str_param(&ihost->initiatorname, buf); 37150801c242SMike Christie default: 37160801c242SMike Christie return -ENOSYS; 37170801c242SMike Christie } 37180801c242SMike Christie 37190801c242SMike Christie return 0; 37200801c242SMike Christie } 37210801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_set_param); 37220801c242SMike Christie 37237996a778SMike Christie MODULE_AUTHOR("Mike Christie"); 37247996a778SMike Christie MODULE_DESCRIPTION("iSCSI library functions"); 37257996a778SMike Christie MODULE_LICENSE("GPL"); 3726