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; 2513fe5ae8bSMike Christie /* fall through */ 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 5266f8830f5SChris Leech spin_lock_bh(&conn->taskqueuelock); 5276f8830f5SChris Leech if (!list_empty(&task->running)) { 5286f8830f5SChris Leech pr_debug_once("%s while task on list", __func__); 5293bbaaad9SMike Christie list_del_init(&task->running); 5306f8830f5SChris Leech } 5316f8830f5SChris Leech spin_unlock_bh(&conn->taskqueuelock); 5323bbaaad9SMike Christie 5333bbaaad9SMike Christie if (conn->task == task) 5343bbaaad9SMike Christie conn->task = NULL; 5353bbaaad9SMike Christie 5363bbaaad9SMike Christie if (conn->ping_task == task) 5373bbaaad9SMike Christie conn->ping_task = NULL; 5383bbaaad9SMike Christie 5393bbaaad9SMike Christie /* release get from queueing */ 5403bbaaad9SMike Christie __iscsi_put_task(task); 5413bbaaad9SMike Christie } 5423bbaaad9SMike Christie 5434c0ba5d2SMike Christie /** 5444c0ba5d2SMike Christie * iscsi_complete_scsi_task - finish scsi task normally 5454c0ba5d2SMike Christie * @task: iscsi task for scsi cmd 5464c0ba5d2SMike Christie * @exp_cmdsn: expected cmd sn in cpu format 5474c0ba5d2SMike Christie * @max_cmdsn: max cmd sn in cpu format 5484c0ba5d2SMike Christie * 5494c0ba5d2SMike Christie * This is used when drivers do not need or cannot perform 5504c0ba5d2SMike Christie * lower level pdu processing. 5514c0ba5d2SMike Christie * 552659743b0SShlomo Pongratz * Called with session back_lock 5534c0ba5d2SMike Christie */ 5544c0ba5d2SMike Christie void iscsi_complete_scsi_task(struct iscsi_task *task, 5554c0ba5d2SMike Christie uint32_t exp_cmdsn, uint32_t max_cmdsn) 5564c0ba5d2SMike Christie { 5574c0ba5d2SMike Christie struct iscsi_conn *conn = task->conn; 5584c0ba5d2SMike Christie 5594c0ba5d2SMike Christie ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt); 5604c0ba5d2SMike Christie 5614c0ba5d2SMike Christie conn->last_recv = jiffies; 5624c0ba5d2SMike Christie __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn); 5634c0ba5d2SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 5644c0ba5d2SMike Christie } 5654c0ba5d2SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task); 5664c0ba5d2SMike Christie 5674c0ba5d2SMike Christie 5683bbaaad9SMike Christie /* 569659743b0SShlomo Pongratz * session back_lock must be held and if not called for a task that is 5703bbaaad9SMike Christie * still pending or from the xmit thread, then xmit thread must 5713bbaaad9SMike Christie * be suspended. 5723bbaaad9SMike Christie */ 5733bbaaad9SMike Christie static void fail_scsi_task(struct iscsi_task *task, int err) 5743bbaaad9SMike Christie { 5753bbaaad9SMike Christie struct iscsi_conn *conn = task->conn; 576b3a7ea8dSMike Christie struct scsi_cmnd *sc; 577b3cd5050SMike Christie int state; 578b3a7ea8dSMike Christie 5793bbaaad9SMike Christie /* 5803bbaaad9SMike Christie * if a command completes and we get a successful tmf response 5813bbaaad9SMike Christie * we will hit this because the scsi eh abort code does not take 5823bbaaad9SMike Christie * a ref to the task. 5833bbaaad9SMike Christie */ 5849c19a7d0SMike Christie sc = task->sc; 585b3a7ea8dSMike Christie if (!sc) 586b3a7ea8dSMike Christie return; 587b3a7ea8dSMike Christie 588b3cd5050SMike Christie if (task->state == ISCSI_TASK_PENDING) { 589b3a7ea8dSMike Christie /* 590b3a7ea8dSMike Christie * cmd never made it to the xmit thread, so we should not count 591b3a7ea8dSMike Christie * the cmd in the sequencing 592b3a7ea8dSMike Christie */ 593b3a7ea8dSMike Christie conn->session->queued_cmdsn--; 594b3cd5050SMike Christie /* it was never sent so just complete like normal */ 595b3cd5050SMike Christie state = ISCSI_TASK_COMPLETED; 596b3cd5050SMike Christie } else if (err == DID_TRANSPORT_DISRUPTED) 597b3cd5050SMike Christie state = ISCSI_TASK_ABRT_SESS_RECOV; 598b3cd5050SMike Christie else 599b3cd5050SMike Christie state = ISCSI_TASK_ABRT_TMF; 600b3a7ea8dSMike Christie 601b3cd5050SMike Christie sc->result = err << 16; 602b3a7ea8dSMike Christie scsi_set_resid(sc, scsi_bufflen(sc)); 6033e5c28adSMike Christie 604659743b0SShlomo Pongratz /* regular RX path uses back_lock */ 605659743b0SShlomo Pongratz spin_lock_bh(&conn->session->back_lock); 606b3cd5050SMike Christie iscsi_complete_task(task, state); 607659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->back_lock); 608b3a7ea8dSMike Christie } 609b3a7ea8dSMike Christie 6103e5c28adSMike Christie static int iscsi_prep_mgmt_task(struct iscsi_conn *conn, 6119c19a7d0SMike Christie struct iscsi_task *task) 612052d0144SMike Christie { 613052d0144SMike Christie struct iscsi_session *session = conn->session; 614577577daSMike Christie struct iscsi_hdr *hdr = task->hdr; 615052d0144SMike Christie struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr; 6164f704dc0SMike Christie uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK; 617052d0144SMike Christie 618052d0144SMike Christie if (conn->session->state == ISCSI_STATE_LOGGING_OUT) 619052d0144SMike Christie return -ENOTCONN; 620052d0144SMike Christie 6214f704dc0SMike Christie if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT) 622052d0144SMike Christie nop->exp_statsn = cpu_to_be32(conn->exp_statsn); 623052d0144SMike Christie /* 624052d0144SMike Christie * pre-format CmdSN for outgoing PDU. 625052d0144SMike Christie */ 626052d0144SMike Christie nop->cmdsn = cpu_to_be32(session->cmdsn); 627052d0144SMike Christie if (hdr->itt != RESERVED_ITT) { 628052d0144SMike Christie /* 6294f704dc0SMike Christie * TODO: We always use immediate for normal session pdus. 630052d0144SMike Christie * If we start to send tmfs or nops as non-immediate then 631052d0144SMike Christie * we should start checking the cmdsn numbers for mgmt tasks. 6324f704dc0SMike Christie * 6334f704dc0SMike Christie * During discovery sessions iscsid sends TEXT as non immediate, 6344f704dc0SMike Christie * but we always only send one PDU at a time. 635052d0144SMike Christie */ 636052d0144SMike Christie if (conn->c_stage == ISCSI_CONN_STARTED && 637052d0144SMike Christie !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { 638052d0144SMike Christie session->queued_cmdsn++; 639052d0144SMike Christie session->cmdsn++; 640052d0144SMike Christie } 641052d0144SMike Christie } 642052d0144SMike Christie 643ae15f801SMike Christie if (session->tt->init_task && session->tt->init_task(task)) 644ae15f801SMike Christie return -EIO; 645052d0144SMike Christie 646052d0144SMike Christie if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) 647052d0144SMike Christie session->state = ISCSI_STATE_LOGGING_OUT; 648052d0144SMike Christie 649577577daSMike Christie task->state = ISCSI_TASK_RUNNING; 6501b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x " 6511b2c7af8SMike Christie "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK, 6521b2c7af8SMike Christie hdr->itt, task->data_count); 653052d0144SMike Christie return 0; 654052d0144SMike Christie } 655052d0144SMike Christie 6569c19a7d0SMike Christie static struct iscsi_task * 657f6d5180cSMike Christie __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 658f6d5180cSMike Christie char *data, uint32_t data_size) 659f6d5180cSMike Christie { 660f6d5180cSMike Christie struct iscsi_session *session = conn->session; 6611336aed1SMike Christie struct iscsi_host *ihost = shost_priv(session->host); 6624f704dc0SMike Christie uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK; 6639c19a7d0SMike Christie struct iscsi_task *task; 664262ef636SMike Christie itt_t itt; 665f6d5180cSMike Christie 666f6d5180cSMike Christie if (session->state == ISCSI_STATE_TERMINATE) 667f6d5180cSMike Christie return NULL; 668f6d5180cSMike Christie 6694f704dc0SMike Christie if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) { 670f6d5180cSMike Christie /* 671f6d5180cSMike Christie * Login and Text are sent serially, in 672f6d5180cSMike Christie * request-followed-by-response sequence. 6733e5c28adSMike Christie * Same task can be used. Same ITT must be used. 6743e5c28adSMike Christie * Note that login_task is preallocated at conn_create(). 675f6d5180cSMike Christie */ 6764f704dc0SMike Christie if (conn->login_task->state != ISCSI_TASK_FREE) { 6774f704dc0SMike Christie iscsi_conn_printk(KERN_ERR, conn, "Login/Text in " 6784f704dc0SMike Christie "progress. Cannot start new task.\n"); 6794f704dc0SMike Christie return NULL; 6804f704dc0SMike Christie } 6814f704dc0SMike Christie 682cbaa4221SMike Christie if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) { 683cbaa4221SMike 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); 684cbaa4221SMike Christie return NULL; 685cbaa4221SMike Christie } 686cbaa4221SMike Christie 6879c19a7d0SMike Christie task = conn->login_task; 6884f704dc0SMike Christie } else { 68926013ad4SMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) 69026013ad4SMike Christie return NULL; 69126013ad4SMike Christie 692cbaa4221SMike Christie if (data_size != 0) { 693cbaa4221SMike Christie iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode); 694cbaa4221SMike Christie return NULL; 695cbaa4221SMike Christie } 696cbaa4221SMike Christie 697f6d5180cSMike Christie BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); 698f6d5180cSMike Christie BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); 699f6d5180cSMike Christie 7007acd72ebSStefani Seibold if (!kfifo_out(&session->cmdpool.queue, 7019c19a7d0SMike Christie (void*)&task, sizeof(void*))) 702f6d5180cSMike Christie return NULL; 703f6d5180cSMike Christie } 7043e5c28adSMike Christie /* 7053e5c28adSMike Christie * released in complete pdu for task we expect a response for, and 7063e5c28adSMike Christie * released by the lld when it has transmitted the task for 7073e5c28adSMike Christie * pdus we do not expect a response for. 7083e5c28adSMike Christie */ 7096dc618cdSElena Reshetova refcount_set(&task->refcount, 1); 7109c19a7d0SMike Christie task->conn = conn; 7119c19a7d0SMike Christie task->sc = NULL; 7123bbaaad9SMike Christie INIT_LIST_HEAD(&task->running); 7133bbaaad9SMike Christie task->state = ISCSI_TASK_PENDING; 714f6d5180cSMike Christie 715f6d5180cSMike Christie if (data_size) { 7169c19a7d0SMike Christie memcpy(task->data, data, data_size); 7179c19a7d0SMike Christie task->data_count = data_size; 718f6d5180cSMike Christie } else 7199c19a7d0SMike Christie task->data_count = 0; 720f6d5180cSMike Christie 721184b57c6SMike Christie if (conn->session->tt->alloc_pdu) { 7222ff79d52SMike Christie if (conn->session->tt->alloc_pdu(task, hdr->opcode)) { 723577577daSMike Christie iscsi_conn_printk(KERN_ERR, conn, "Could not allocate " 724577577daSMike Christie "pdu for mgmt task.\n"); 7253bbaaad9SMike Christie goto free_task; 726577577daSMike Christie } 727184b57c6SMike Christie } 728184b57c6SMike Christie 729262ef636SMike Christie itt = task->hdr->itt; 730577577daSMike Christie task->hdr_len = sizeof(struct iscsi_hdr); 7319c19a7d0SMike Christie memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr)); 732262ef636SMike Christie 733262ef636SMike Christie if (hdr->itt != RESERVED_ITT) { 734262ef636SMike Christie if (session->tt->parse_pdu_itt) 735262ef636SMike Christie task->hdr->itt = itt; 736262ef636SMike Christie else 737262ef636SMike Christie task->hdr->itt = build_itt(task->itt, 738262ef636SMike Christie task->conn->session->age); 739262ef636SMike Christie } 740262ef636SMike Christie 7411336aed1SMike Christie if (!ihost->workq) { 742577577daSMike Christie if (iscsi_prep_mgmt_task(conn, task)) 743577577daSMike Christie goto free_task; 744052d0144SMike Christie 7459c19a7d0SMike Christie if (session->tt->xmit_task(task)) 746577577daSMike Christie goto free_task; 7473bbaaad9SMike Christie } else { 7486f8830f5SChris Leech spin_lock_bh(&conn->taskqueuelock); 7493bbaaad9SMike Christie list_add_tail(&task->running, &conn->mgmtqueue); 7506f8830f5SChris Leech spin_unlock_bh(&conn->taskqueuelock); 75132ae763eSMike Christie iscsi_conn_queue_work(conn); 7523bbaaad9SMike Christie } 753052d0144SMike Christie 7549c19a7d0SMike Christie return task; 755577577daSMike Christie 756577577daSMike Christie free_task: 757659743b0SShlomo Pongratz /* regular RX path uses back_lock */ 7584fa50799SJitendra Bhivare spin_lock(&session->back_lock); 759577577daSMike Christie __iscsi_put_task(task); 7604fa50799SJitendra Bhivare spin_unlock(&session->back_lock); 761577577daSMike Christie return NULL; 762f6d5180cSMike Christie } 763f6d5180cSMike Christie 764f6d5180cSMike Christie int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr, 765f6d5180cSMike Christie char *data, uint32_t data_size) 766f6d5180cSMike Christie { 767f6d5180cSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 768f6d5180cSMike Christie struct iscsi_session *session = conn->session; 769f6d5180cSMike Christie int err = 0; 770f6d5180cSMike Christie 771659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 772f6d5180cSMike Christie if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size)) 773f6d5180cSMike Christie err = -EPERM; 774659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 775f6d5180cSMike Christie return err; 776f6d5180cSMike Christie } 777f6d5180cSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu); 778f6d5180cSMike Christie 7797996a778SMike Christie /** 7807996a778SMike Christie * iscsi_cmd_rsp - SCSI Command Response processing 7817996a778SMike Christie * @conn: iscsi connection 7827996a778SMike Christie * @hdr: iscsi header 7839c19a7d0SMike Christie * @task: scsi command task 7847996a778SMike Christie * @data: cmd data buffer 7857996a778SMike Christie * @datalen: len of buffer 7867996a778SMike Christie * 7877996a778SMike Christie * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and 788a656183eSLee Duncan * then completes the command and task. called under back_lock 7897996a778SMike Christie **/ 79077a23c21SMike Christie static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 7919c19a7d0SMike Christie struct iscsi_task *task, char *data, 7927996a778SMike Christie int datalen) 7937996a778SMike Christie { 79412352183SNicholas Bellinger struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr; 7957996a778SMike Christie struct iscsi_session *session = conn->session; 7969c19a7d0SMike Christie struct scsi_cmnd *sc = task->sc; 7977996a778SMike Christie 79877a23c21SMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); 7997996a778SMike Christie conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; 8007996a778SMike Christie 8017996a778SMike Christie sc->result = (DID_OK << 16) | rhdr->cmd_status; 8027996a778SMike Christie 80355e51edaSSagi Grimberg if (task->protected) { 80455e51edaSSagi Grimberg sector_t sector; 80555e51edaSSagi Grimberg u8 ascq; 80655e51edaSSagi Grimberg 80755e51edaSSagi Grimberg /** 80855e51edaSSagi Grimberg * Transports that didn't implement check_protection 80955e51edaSSagi Grimberg * callback but still published T10-PI support to scsi-mid 81055e51edaSSagi Grimberg * deserve this BUG_ON. 81155e51edaSSagi Grimberg **/ 81255e51edaSSagi Grimberg BUG_ON(!session->tt->check_protection); 81355e51edaSSagi Grimberg 81455e51edaSSagi Grimberg ascq = session->tt->check_protection(task, §or); 81555e51edaSSagi Grimberg if (ascq) { 81655e51edaSSagi Grimberg sc->result = DRIVER_SENSE << 24 | 81755e51edaSSagi Grimberg SAM_STAT_CHECK_CONDITION; 81855e51edaSSagi Grimberg scsi_build_sense_buffer(1, sc->sense_buffer, 81955e51edaSSagi Grimberg ILLEGAL_REQUEST, 0x10, ascq); 820a73c2a2fSSagi Grimberg scsi_set_sense_information(sc->sense_buffer, 821a73c2a2fSSagi Grimberg SCSI_SENSE_BUFFERSIZE, 822a73c2a2fSSagi Grimberg sector); 82355e51edaSSagi Grimberg goto out; 82455e51edaSSagi Grimberg } 82555e51edaSSagi Grimberg } 82655e51edaSSagi Grimberg 8277996a778SMike Christie if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) { 8287996a778SMike Christie sc->result = DID_ERROR << 16; 8297996a778SMike Christie goto out; 8307996a778SMike Christie } 8317996a778SMike Christie 8327996a778SMike Christie if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) { 8339b80cb4bSMike Christie uint16_t senselen; 8347996a778SMike Christie 8357996a778SMike Christie if (datalen < 2) { 8367996a778SMike Christie invalid_datalen: 837322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 838322d739dSMike Christie "Got CHECK_CONDITION but invalid data " 839322d739dSMike Christie "buffer size of %d\n", datalen); 8407996a778SMike Christie sc->result = DID_BAD_TARGET << 16; 8417996a778SMike Christie goto out; 8427996a778SMike Christie } 8437996a778SMike Christie 8448f333991SHarvey Harrison senselen = get_unaligned_be16(data); 8457996a778SMike Christie if (datalen < senselen) 8467996a778SMike Christie goto invalid_datalen; 8477996a778SMike Christie 8487996a778SMike Christie memcpy(sc->sense_buffer, data + 2, 8499b80cb4bSMike Christie min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE)); 8501b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n", 8511b2c7af8SMike Christie min_t(uint16_t, senselen, 8521b2c7af8SMike Christie SCSI_SENSE_BUFFERSIZE)); 8537996a778SMike Christie } 8547996a778SMike Christie 855c07d4444SBoaz Harrosh if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW | 856c07d4444SBoaz Harrosh ISCSI_FLAG_CMD_BIDI_OVERFLOW)) { 857c07d4444SBoaz Harrosh sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 858c07d4444SBoaz Harrosh } 859c07d4444SBoaz Harrosh 8607207fea4SBoaz Harrosh if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW | 8617207fea4SBoaz Harrosh ISCSI_FLAG_CMD_OVERFLOW)) { 8627996a778SMike Christie int res_count = be32_to_cpu(rhdr->residual_count); 8637996a778SMike Christie 8647207fea4SBoaz Harrosh if (res_count > 0 && 8657207fea4SBoaz Harrosh (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || 8667207fea4SBoaz Harrosh res_count <= scsi_bufflen(sc))) 867c07d4444SBoaz Harrosh /* write side for bidi or uni-io set_resid */ 8681c138991SFUJITA Tomonori scsi_set_resid(sc, res_count); 8697996a778SMike Christie else 8707996a778SMike Christie sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 871c07d4444SBoaz Harrosh } 8727996a778SMike Christie out: 8733bbaaad9SMike Christie ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n", 8741b2c7af8SMike Christie sc, sc->result, task->itt); 8757996a778SMike Christie conn->scsirsp_pdus_cnt++; 876b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 8777996a778SMike Christie } 8787996a778SMike Christie 8791d9edf02SMike Christie /** 8801d9edf02SMike Christie * iscsi_data_in_rsp - SCSI Data-In Response processing 8811d9edf02SMike Christie * @conn: iscsi connection 8821d9edf02SMike Christie * @hdr: iscsi pdu 8831d9edf02SMike Christie * @task: scsi command task 884a656183eSLee Duncan * 885a656183eSLee Duncan * iscsi_data_in_rsp sets up the scsi_cmnd fields based on the data received 886a656183eSLee Duncan * then completes the command and task. called under back_lock 8871d9edf02SMike Christie **/ 8881d9edf02SMike Christie static void 8891d9edf02SMike Christie iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 8901d9edf02SMike Christie struct iscsi_task *task) 8911d9edf02SMike Christie { 8921d9edf02SMike Christie struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr; 8931d9edf02SMike Christie struct scsi_cmnd *sc = task->sc; 8941d9edf02SMike Christie 8951d9edf02SMike Christie if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS)) 8961d9edf02SMike Christie return; 8971d9edf02SMike Christie 898edbc9aa0SMike Christie iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr); 8991d9edf02SMike Christie sc->result = (DID_OK << 16) | rhdr->cmd_status; 9001d9edf02SMike Christie conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; 9011d9edf02SMike Christie if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW | 9021d9edf02SMike Christie ISCSI_FLAG_DATA_OVERFLOW)) { 9031d9edf02SMike Christie int res_count = be32_to_cpu(rhdr->residual_count); 9041d9edf02SMike Christie 9051d9edf02SMike Christie if (res_count > 0 && 9061d9edf02SMike Christie (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || 907ae3d56d8SChristoph Hellwig res_count <= sc->sdb.length)) 908960bf87aSBart Van Assche scsi_set_resid(sc, res_count); 9091d9edf02SMike Christie else 9101d9edf02SMike Christie sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 9111d9edf02SMike Christie } 9121d9edf02SMike Christie 9133bbaaad9SMike Christie ISCSI_DBG_SESSION(conn->session, "data in with status done " 9143bbaaad9SMike Christie "[sc %p res %d itt 0x%x]\n", 9153bbaaad9SMike Christie sc, sc->result, task->itt); 9161d9edf02SMike Christie conn->scsirsp_pdus_cnt++; 917b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 9181d9edf02SMike Christie } 9191d9edf02SMike Christie 9207ea8b828SMike Christie static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr) 9217ea8b828SMike Christie { 9227ea8b828SMike Christie struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr; 9237ea8b828SMike Christie 9247ea8b828SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 9257ea8b828SMike Christie conn->tmfrsp_pdus_cnt++; 9267ea8b828SMike Christie 927843c0a8aSMike Christie if (conn->tmf_state != TMF_QUEUED) 9287ea8b828SMike Christie return; 9297ea8b828SMike Christie 9307ea8b828SMike Christie if (tmf->response == ISCSI_TMF_RSP_COMPLETE) 931843c0a8aSMike Christie conn->tmf_state = TMF_SUCCESS; 9327ea8b828SMike Christie else if (tmf->response == ISCSI_TMF_RSP_NO_TASK) 933843c0a8aSMike Christie conn->tmf_state = TMF_NOT_FOUND; 9347ea8b828SMike Christie else 935843c0a8aSMike Christie conn->tmf_state = TMF_FAILED; 9367ea8b828SMike Christie wake_up(&conn->ehwait); 9377ea8b828SMike Christie } 9387ea8b828SMike Christie 93952f5664aSAriel Nahum static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr) 940f6d5180cSMike Christie { 941f6d5180cSMike Christie struct iscsi_nopout hdr; 9429c19a7d0SMike Christie struct iscsi_task *task; 943f6d5180cSMike Christie 9449c19a7d0SMike Christie if (!rhdr && conn->ping_task) 94552f5664aSAriel Nahum return -EINVAL; 946f6d5180cSMike Christie 947f6d5180cSMike Christie memset(&hdr, 0, sizeof(struct iscsi_nopout)); 948f6d5180cSMike Christie hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE; 949f6d5180cSMike Christie hdr.flags = ISCSI_FLAG_CMD_FINAL; 950f6d5180cSMike Christie 951f6d5180cSMike Christie if (rhdr) { 95255bdabdfSAndy Grover hdr.lun = rhdr->lun; 953f6d5180cSMike Christie hdr.ttt = rhdr->ttt; 954f6d5180cSMike Christie hdr.itt = RESERVED_ITT; 955f6d5180cSMike Christie } else 956f6d5180cSMike Christie hdr.ttt = RESERVED_ITT; 957f6d5180cSMike Christie 9589c19a7d0SMike Christie task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0); 95952f5664aSAriel Nahum if (!task) { 960322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n"); 96152f5664aSAriel Nahum return -EIO; 96252f5664aSAriel Nahum } else if (!rhdr) { 963d3acf022SMike Christie /* only track our nops */ 964d3acf022SMike Christie conn->ping_task = task; 965d3acf022SMike Christie conn->last_ping = jiffies; 966d3acf022SMike Christie } 96752f5664aSAriel Nahum 96852f5664aSAriel Nahum return 0; 969f6d5180cSMike Christie } 970f6d5180cSMike Christie 971a656183eSLee Duncan /** 972a656183eSLee Duncan * iscsi_nop_out_rsp - SCSI NOP Response processing 973a656183eSLee Duncan * @task: scsi command task 974a656183eSLee Duncan * @nop: the nop structure 975a656183eSLee Duncan * @data: where to put the data 976a656183eSLee Duncan * @datalen: length of data 977a656183eSLee Duncan * 978a656183eSLee Duncan * iscsi_nop_out_rsp handles nop response from use or 979a656183eSLee Duncan * from user space. called under back_lock 980a656183eSLee Duncan **/ 9818afa1439SMike Christie static int iscsi_nop_out_rsp(struct iscsi_task *task, 9828afa1439SMike Christie struct iscsi_nopin *nop, char *data, int datalen) 9838afa1439SMike Christie { 9848afa1439SMike Christie struct iscsi_conn *conn = task->conn; 9858afa1439SMike Christie int rc = 0; 9868afa1439SMike Christie 9878afa1439SMike Christie if (conn->ping_task != task) { 9888afa1439SMike Christie /* 9898afa1439SMike Christie * If this is not in response to one of our 9908afa1439SMike Christie * nops then it must be from userspace. 9918afa1439SMike Christie */ 9928afa1439SMike Christie if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop, 9938afa1439SMike Christie data, datalen)) 9948afa1439SMike Christie rc = ISCSI_ERR_CONN_FAILED; 9958afa1439SMike Christie } else 9968afa1439SMike Christie mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout); 9978afa1439SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 9988afa1439SMike Christie return rc; 9998afa1439SMike Christie } 10008afa1439SMike Christie 100162f38300SMike Christie static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 100262f38300SMike Christie char *data, int datalen) 100362f38300SMike Christie { 100462f38300SMike Christie struct iscsi_reject *reject = (struct iscsi_reject *)hdr; 100562f38300SMike Christie struct iscsi_hdr rejected_pdu; 10068afa1439SMike Christie int opcode, rc = 0; 100762f38300SMike Christie 100862f38300SMike Christie conn->exp_statsn = be32_to_cpu(reject->statsn) + 1; 100962f38300SMike Christie 10108afa1439SMike Christie if (ntoh24(reject->dlength) > datalen || 10118afa1439SMike Christie ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) { 10128afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected " 10138afa1439SMike Christie "pdu. Invalid data length (pdu dlength " 10148afa1439SMike Christie "%u, datalen %d\n", ntoh24(reject->dlength), 10158afa1439SMike Christie datalen); 101662f38300SMike Christie return ISCSI_ERR_PROTO; 10178afa1439SMike Christie } 101862f38300SMike Christie memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr)); 10198afa1439SMike Christie opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK; 10208afa1439SMike Christie 10218afa1439SMike Christie switch (reject->reason) { 10228afa1439SMike Christie case ISCSI_REASON_DATA_DIGEST_ERROR: 1023322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 10248afa1439SMike Christie "pdu (op 0x%x itt 0x%x) rejected " 1025262ef636SMike Christie "due to DataDigest error.\n", 1026e5dbbe27SVaughan Cao opcode, rejected_pdu.itt); 10278afa1439SMike Christie break; 10288afa1439SMike Christie case ISCSI_REASON_IMM_CMD_REJECT: 10298afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, 10308afa1439SMike Christie "pdu (op 0x%x itt 0x%x) rejected. Too many " 10318afa1439SMike Christie "immediate commands.\n", 1032e5dbbe27SVaughan Cao opcode, rejected_pdu.itt); 10338afa1439SMike Christie /* 10348afa1439SMike Christie * We only send one TMF at a time so if the target could not 10358afa1439SMike Christie * handle it, then it should get fixed (RFC mandates that 10368afa1439SMike Christie * a target can handle one immediate TMF per conn). 10378afa1439SMike Christie * 10388afa1439SMike Christie * For nops-outs, we could have sent more than one if 10398afa1439SMike Christie * the target is sending us lots of nop-ins 10408afa1439SMike Christie */ 10418afa1439SMike Christie if (opcode != ISCSI_OP_NOOP_OUT) 104262f38300SMike Christie return 0; 10438afa1439SMike Christie 1044659743b0SShlomo Pongratz if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) { 10458afa1439SMike Christie /* 10468afa1439SMike Christie * nop-out in response to target's nop-out rejected. 10478afa1439SMike Christie * Just resend. 10488afa1439SMike Christie */ 1049659743b0SShlomo Pongratz /* In RX path we are under back lock */ 1050659743b0SShlomo Pongratz spin_unlock(&conn->session->back_lock); 1051659743b0SShlomo Pongratz spin_lock(&conn->session->frwd_lock); 10528afa1439SMike Christie iscsi_send_nopout(conn, 10538afa1439SMike Christie (struct iscsi_nopin*)&rejected_pdu); 1054659743b0SShlomo Pongratz spin_unlock(&conn->session->frwd_lock); 1055659743b0SShlomo Pongratz spin_lock(&conn->session->back_lock); 1056659743b0SShlomo Pongratz } else { 10578afa1439SMike Christie struct iscsi_task *task; 10588afa1439SMike Christie /* 10598afa1439SMike Christie * Our nop as ping got dropped. We know the target 10608afa1439SMike Christie * and transport are ok so just clean up 10618afa1439SMike Christie */ 10628afa1439SMike Christie task = iscsi_itt_to_task(conn, rejected_pdu.itt); 10638afa1439SMike Christie if (!task) { 10648afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, 10658afa1439SMike Christie "Invalid pdu reject. Could " 10668afa1439SMike Christie "not lookup rejected task.\n"); 10678afa1439SMike Christie rc = ISCSI_ERR_BAD_ITT; 10688afa1439SMike Christie } else 10698afa1439SMike Christie rc = iscsi_nop_out_rsp(task, 10708afa1439SMike Christie (struct iscsi_nopin*)&rejected_pdu, 10718afa1439SMike Christie NULL, 0); 10728afa1439SMike Christie } 10738afa1439SMike Christie break; 10748afa1439SMike Christie default: 10758afa1439SMike Christie iscsi_conn_printk(KERN_ERR, conn, 10768afa1439SMike Christie "pdu (op 0x%x itt 0x%x) rejected. Reason " 1077e5dbbe27SVaughan Cao "code 0x%x\n", rejected_pdu.opcode, 1078e5dbbe27SVaughan Cao rejected_pdu.itt, reject->reason); 10798afa1439SMike Christie break; 10808afa1439SMike Christie } 10818afa1439SMike Christie return rc; 108262f38300SMike Christie } 108362f38300SMike Christie 10847996a778SMike Christie /** 1085913e5bf4SMike Christie * iscsi_itt_to_task - look up task by itt 1086913e5bf4SMike Christie * @conn: iscsi connection 1087913e5bf4SMike Christie * @itt: itt 1088913e5bf4SMike Christie * 1089913e5bf4SMike Christie * This should be used for mgmt tasks like login and nops, or if 1090913e5bf4SMike Christie * the LDD's itt space does not include the session age. 1091913e5bf4SMike Christie * 1092659743b0SShlomo Pongratz * The session back_lock must be held. 1093913e5bf4SMike Christie */ 10948f9256ceSMike Christie struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt) 1095913e5bf4SMike Christie { 1096913e5bf4SMike Christie struct iscsi_session *session = conn->session; 1097262ef636SMike Christie int i; 1098913e5bf4SMike Christie 1099913e5bf4SMike Christie if (itt == RESERVED_ITT) 1100913e5bf4SMike Christie return NULL; 1101913e5bf4SMike Christie 1102262ef636SMike Christie if (session->tt->parse_pdu_itt) 1103262ef636SMike Christie session->tt->parse_pdu_itt(conn, itt, &i, NULL); 1104262ef636SMike Christie else 1105913e5bf4SMike Christie i = get_itt(itt); 1106913e5bf4SMike Christie if (i >= session->cmds_max) 1107913e5bf4SMike Christie return NULL; 1108913e5bf4SMike Christie 1109913e5bf4SMike Christie return session->cmds[i]; 1110913e5bf4SMike Christie } 11118f9256ceSMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_task); 1112913e5bf4SMike Christie 1113913e5bf4SMike Christie /** 11147996a778SMike Christie * __iscsi_complete_pdu - complete pdu 11157996a778SMike Christie * @conn: iscsi conn 11167996a778SMike Christie * @hdr: iscsi header 11177996a778SMike Christie * @data: data buffer 11187996a778SMike Christie * @datalen: len of data buffer 11197996a778SMike Christie * 11207996a778SMike Christie * Completes pdu processing by freeing any resources allocated at 1121659743b0SShlomo Pongratz * queuecommand or send generic. session back_lock must be held and verify 11227996a778SMike Christie * itt must have been called. 11237996a778SMike Christie */ 1124913e5bf4SMike Christie int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 11257996a778SMike Christie char *data, int datalen) 11267996a778SMike Christie { 11277996a778SMike Christie struct iscsi_session *session = conn->session; 11287996a778SMike Christie int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0; 11299c19a7d0SMike Christie struct iscsi_task *task; 11307996a778SMike Christie uint32_t itt; 11317996a778SMike Christie 1132f6d5180cSMike Christie conn->last_recv = jiffies; 11330af967f5SMike Christie rc = iscsi_verify_itt(conn, hdr->itt); 11340af967f5SMike Christie if (rc) 11350af967f5SMike Christie return rc; 11360af967f5SMike Christie 1137b4377356SAl Viro if (hdr->itt != RESERVED_ITT) 1138b4377356SAl Viro itt = get_itt(hdr->itt); 11397996a778SMike Christie else 1140b4377356SAl Viro itt = ~0U; 11417996a778SMike Christie 11421b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n", 11433e5c28adSMike Christie opcode, conn->id, itt, datalen); 11447996a778SMike Christie 11453e5c28adSMike Christie if (itt == ~0U) { 114677a23c21SMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 114762f38300SMike Christie 11487996a778SMike Christie switch(opcode) { 11497996a778SMike Christie case ISCSI_OP_NOOP_IN: 115040527afeSMike Christie if (datalen) { 115140527afeSMike Christie rc = ISCSI_ERR_PROTO; 115240527afeSMike Christie break; 115340527afeSMike Christie } 115440527afeSMike Christie 1155b4377356SAl Viro if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG)) 115640527afeSMike Christie break; 115740527afeSMike Christie 1158659743b0SShlomo Pongratz /* In RX path we are under back lock */ 1159659743b0SShlomo Pongratz spin_unlock(&session->back_lock); 1160659743b0SShlomo Pongratz spin_lock(&session->frwd_lock); 1161f6d5180cSMike Christie iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr); 1162659743b0SShlomo Pongratz spin_unlock(&session->frwd_lock); 1163659743b0SShlomo Pongratz spin_lock(&session->back_lock); 11647996a778SMike Christie break; 11657996a778SMike Christie case ISCSI_OP_REJECT: 116662f38300SMike Christie rc = iscsi_handle_reject(conn, hdr, data, datalen); 116762f38300SMike Christie break; 11687996a778SMike Christie case ISCSI_OP_ASYNC_EVENT: 11698d2860b3SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 11705831c737SMike Christie if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) 11715831c737SMike Christie rc = ISCSI_ERR_CONN_FAILED; 11727996a778SMike Christie break; 11737996a778SMike Christie default: 11747996a778SMike Christie rc = ISCSI_ERR_BAD_OPCODE; 11757996a778SMike Christie break; 11767996a778SMike Christie } 11773e5c28adSMike Christie goto out; 11783e5c28adSMike Christie } 11797996a778SMike Christie 11803e5c28adSMike Christie switch(opcode) { 11813e5c28adSMike Christie case ISCSI_OP_SCSI_CMD_RSP: 1182913e5bf4SMike Christie case ISCSI_OP_SCSI_DATA_IN: 1183913e5bf4SMike Christie task = iscsi_itt_to_ctask(conn, hdr->itt); 1184913e5bf4SMike Christie if (!task) 1185913e5bf4SMike Christie return ISCSI_ERR_BAD_ITT; 1186d355e57dSMike Christie task->last_xfer = jiffies; 11873e5c28adSMike Christie break; 1188913e5bf4SMike Christie case ISCSI_OP_R2T: 1189913e5bf4SMike Christie /* 1190913e5bf4SMike Christie * LLD handles R2Ts if they need to. 1191913e5bf4SMike Christie */ 1192913e5bf4SMike Christie return 0; 1193913e5bf4SMike Christie case ISCSI_OP_LOGOUT_RSP: 1194913e5bf4SMike Christie case ISCSI_OP_LOGIN_RSP: 1195913e5bf4SMike Christie case ISCSI_OP_TEXT_RSP: 1196913e5bf4SMike Christie case ISCSI_OP_SCSI_TMFUNC_RSP: 1197913e5bf4SMike Christie case ISCSI_OP_NOOP_IN: 1198913e5bf4SMike Christie task = iscsi_itt_to_task(conn, hdr->itt); 1199913e5bf4SMike Christie if (!task) 1200913e5bf4SMike Christie return ISCSI_ERR_BAD_ITT; 1201913e5bf4SMike Christie break; 1202913e5bf4SMike Christie default: 1203913e5bf4SMike Christie return ISCSI_ERR_BAD_OPCODE; 12043e5c28adSMike Christie } 1205913e5bf4SMike Christie 1206913e5bf4SMike Christie switch(opcode) { 1207913e5bf4SMike Christie case ISCSI_OP_SCSI_CMD_RSP: 12089c19a7d0SMike Christie iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen); 12093e5c28adSMike Christie break; 12103e5c28adSMike Christie case ISCSI_OP_SCSI_DATA_IN: 12111d9edf02SMike Christie iscsi_data_in_rsp(conn, hdr, task); 12123e5c28adSMike Christie break; 12133e5c28adSMike Christie case ISCSI_OP_LOGOUT_RSP: 12143e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 12153e5c28adSMike Christie if (datalen) { 12163e5c28adSMike Christie rc = ISCSI_ERR_PROTO; 12173e5c28adSMike Christie break; 12183e5c28adSMike Christie } 12193e5c28adSMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 12203e5c28adSMike Christie goto recv_pdu; 12213e5c28adSMike Christie case ISCSI_OP_LOGIN_RSP: 12223e5c28adSMike Christie case ISCSI_OP_TEXT_RSP: 12233e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 12243e5c28adSMike Christie /* 12253e5c28adSMike Christie * login related PDU's exp_statsn is handled in 12263e5c28adSMike Christie * userspace 12273e5c28adSMike Christie */ 12283e5c28adSMike Christie goto recv_pdu; 12293e5c28adSMike Christie case ISCSI_OP_SCSI_TMFUNC_RSP: 12303e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 12313e5c28adSMike Christie if (datalen) { 12323e5c28adSMike Christie rc = ISCSI_ERR_PROTO; 12333e5c28adSMike Christie break; 12343e5c28adSMike Christie } 12353e5c28adSMike Christie 12363e5c28adSMike Christie iscsi_tmf_rsp(conn, hdr); 1237b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 12383e5c28adSMike Christie break; 12393e5c28adSMike Christie case ISCSI_OP_NOOP_IN: 12403e5c28adSMike Christie iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 12413e5c28adSMike Christie if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) { 12423e5c28adSMike Christie rc = ISCSI_ERR_PROTO; 12433e5c28adSMike Christie break; 12443e5c28adSMike Christie } 12453e5c28adSMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 12463e5c28adSMike Christie 12478afa1439SMike Christie rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr, 12488afa1439SMike Christie data, datalen); 12493e5c28adSMike Christie break; 12503e5c28adSMike Christie default: 12513e5c28adSMike Christie rc = ISCSI_ERR_BAD_OPCODE; 12523e5c28adSMike Christie break; 12533e5c28adSMike Christie } 12543e5c28adSMike Christie 12553e5c28adSMike Christie out: 12563e5c28adSMike Christie return rc; 12573e5c28adSMike Christie recv_pdu: 12583e5c28adSMike Christie if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) 12593e5c28adSMike Christie rc = ISCSI_ERR_CONN_FAILED; 1260b3cd5050SMike Christie iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 12617996a778SMike Christie return rc; 12627996a778SMike Christie } 1263913e5bf4SMike Christie EXPORT_SYMBOL_GPL(__iscsi_complete_pdu); 12647996a778SMike Christie 12657996a778SMike Christie int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 12667996a778SMike Christie char *data, int datalen) 12677996a778SMike Christie { 12687996a778SMike Christie int rc; 12697996a778SMike Christie 1270659743b0SShlomo Pongratz spin_lock(&conn->session->back_lock); 12717996a778SMike Christie rc = __iscsi_complete_pdu(conn, hdr, data, datalen); 1272659743b0SShlomo Pongratz spin_unlock(&conn->session->back_lock); 12737996a778SMike Christie return rc; 12747996a778SMike Christie } 12757996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_pdu); 12767996a778SMike Christie 12770af967f5SMike Christie int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt) 12787996a778SMike Christie { 12797996a778SMike Christie struct iscsi_session *session = conn->session; 1280262ef636SMike Christie int age = 0, i = 0; 12817996a778SMike Christie 12820af967f5SMike Christie if (itt == RESERVED_ITT) 12830af967f5SMike Christie return 0; 12840af967f5SMike Christie 1285262ef636SMike Christie if (session->tt->parse_pdu_itt) 1286262ef636SMike Christie session->tt->parse_pdu_itt(conn, itt, &i, &age); 1287262ef636SMike Christie else { 1288262ef636SMike Christie i = get_itt(itt); 1289262ef636SMike Christie age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK; 1290262ef636SMike Christie } 1291262ef636SMike Christie 1292262ef636SMike Christie if (age != session->age) { 1293322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 12940af967f5SMike Christie "received itt %x expected session age (%x)\n", 1295913e5bf4SMike Christie (__force u32)itt, session->age); 12967996a778SMike Christie return ISCSI_ERR_BAD_ITT; 12977996a778SMike Christie } 12987996a778SMike Christie 12993e5c28adSMike Christie if (i >= session->cmds_max) { 13003e5c28adSMike Christie iscsi_conn_printk(KERN_ERR, conn, 13013e5c28adSMike Christie "received invalid itt index %u (max cmds " 13023e5c28adSMike Christie "%u.\n", i, session->cmds_max); 13033e5c28adSMike Christie return ISCSI_ERR_BAD_ITT; 13047996a778SMike Christie } 13057996a778SMike Christie return 0; 13067996a778SMike Christie } 13077996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_verify_itt); 13087996a778SMike Christie 1309913e5bf4SMike Christie /** 1310913e5bf4SMike Christie * iscsi_itt_to_ctask - look up ctask by itt 1311913e5bf4SMike Christie * @conn: iscsi connection 1312913e5bf4SMike Christie * @itt: itt 1313913e5bf4SMike Christie * 1314913e5bf4SMike Christie * This should be used for cmd tasks. 1315913e5bf4SMike Christie * 1316659743b0SShlomo Pongratz * The session back_lock must be held. 1317913e5bf4SMike Christie */ 1318913e5bf4SMike Christie struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt) 13190af967f5SMike Christie { 13209c19a7d0SMike Christie struct iscsi_task *task; 13210af967f5SMike Christie 13220af967f5SMike Christie if (iscsi_verify_itt(conn, itt)) 13230af967f5SMike Christie return NULL; 13240af967f5SMike Christie 1325913e5bf4SMike Christie task = iscsi_itt_to_task(conn, itt); 1326913e5bf4SMike Christie if (!task || !task->sc) 13270af967f5SMike Christie return NULL; 13280af967f5SMike Christie 1329913e5bf4SMike Christie if (task->sc->SCp.phase != conn->session->age) { 1330913e5bf4SMike Christie iscsi_session_printk(KERN_ERR, conn->session, 1331913e5bf4SMike Christie "task's session age %d, expected %d\n", 1332913e5bf4SMike Christie task->sc->SCp.phase, conn->session->age); 13330af967f5SMike Christie return NULL; 1334913e5bf4SMike Christie } 13350af967f5SMike Christie 13369c19a7d0SMike Christie return task; 13370af967f5SMike Christie } 13380af967f5SMike Christie EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask); 13390af967f5SMike Christie 134040a06e75SMike Christie void iscsi_session_failure(struct iscsi_session *session, 1341e5bd7b54SMike Christie enum iscsi_err err) 1342e5bd7b54SMike Christie { 1343e5bd7b54SMike Christie struct iscsi_conn *conn; 1344e5bd7b54SMike Christie struct device *dev; 1345e5bd7b54SMike Christie 1346659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 1347e5bd7b54SMike Christie conn = session->leadconn; 1348e5bd7b54SMike Christie if (session->state == ISCSI_STATE_TERMINATE || !conn) { 1349659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 1350e5bd7b54SMike Christie return; 1351e5bd7b54SMike Christie } 1352e5bd7b54SMike Christie 1353e5bd7b54SMike Christie dev = get_device(&conn->cls_conn->dev); 1354659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 1355e5bd7b54SMike Christie if (!dev) 1356e5bd7b54SMike Christie return; 1357e5bd7b54SMike Christie /* 1358e5bd7b54SMike Christie * if the host is being removed bypass the connection 1359e5bd7b54SMike Christie * recovery initialization because we are going to kill 1360e5bd7b54SMike Christie * the session. 1361e5bd7b54SMike Christie */ 1362e5bd7b54SMike Christie if (err == ISCSI_ERR_INVALID_HOST) 1363e5bd7b54SMike Christie iscsi_conn_error_event(conn->cls_conn, err); 1364e5bd7b54SMike Christie else 1365e5bd7b54SMike Christie iscsi_conn_failure(conn, err); 1366e5bd7b54SMike Christie put_device(dev); 1367e5bd7b54SMike Christie } 1368e5bd7b54SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_failure); 1369e5bd7b54SMike Christie 13707996a778SMike Christie void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err) 13717996a778SMike Christie { 13727996a778SMike Christie struct iscsi_session *session = conn->session; 13737996a778SMike Christie 1374659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 1375656cffc9SMike Christie if (session->state == ISCSI_STATE_FAILED) { 1376659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 1377656cffc9SMike Christie return; 1378656cffc9SMike Christie } 1379656cffc9SMike Christie 138067a61114SMike Christie if (conn->stop_stage == 0) 13817996a778SMike Christie session->state = ISCSI_STATE_FAILED; 1382659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 1383e5bd7b54SMike Christie 13847996a778SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 13857996a778SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); 1386e5bd7b54SMike Christie iscsi_conn_error_event(conn->cls_conn, err); 13877996a778SMike Christie } 13887996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_failure); 13897996a778SMike Christie 139077a23c21SMike Christie static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn) 139177a23c21SMike Christie { 139277a23c21SMike Christie struct iscsi_session *session = conn->session; 139377a23c21SMike Christie 139477a23c21SMike Christie /* 139577a23c21SMike Christie * Check for iSCSI window and take care of CmdSN wrap-around 139677a23c21SMike Christie */ 1397e0726407SMike Christie if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) { 13981b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn " 13991b2c7af8SMike Christie "%u MaxCmdSN %u CmdSN %u/%u\n", 14001b2c7af8SMike Christie session->exp_cmdsn, session->max_cmdsn, 14011b2c7af8SMike Christie session->cmdsn, session->queued_cmdsn); 140277a23c21SMike Christie return -ENOSPC; 140377a23c21SMike Christie } 140477a23c21SMike Christie return 0; 140577a23c21SMike Christie } 140677a23c21SMike Christie 14079c19a7d0SMike Christie static int iscsi_xmit_task(struct iscsi_conn *conn) 140877a23c21SMike Christie { 14099c19a7d0SMike Christie struct iscsi_task *task = conn->task; 1410843c0a8aSMike Christie int rc; 141177a23c21SMike Christie 141270b31c15SMike Christie if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) 141370b31c15SMike Christie return -ENODATA; 141470b31c15SMike Christie 141579edd00dSAnoob Soman spin_lock_bh(&conn->session->back_lock); 141679edd00dSAnoob Soman if (conn->task == NULL) { 141779edd00dSAnoob Soman spin_unlock_bh(&conn->session->back_lock); 141879edd00dSAnoob Soman return -ENODATA; 141979edd00dSAnoob Soman } 14209c19a7d0SMike Christie __iscsi_get_task(task); 142179edd00dSAnoob Soman spin_unlock_bh(&conn->session->back_lock); 1422659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 14239c19a7d0SMike Christie rc = conn->session->tt->xmit_task(task); 1424659743b0SShlomo Pongratz spin_lock_bh(&conn->session->frwd_lock); 1425d355e57dSMike Christie if (!rc) { 14269c19a7d0SMike Christie /* done with this task */ 1427d355e57dSMike Christie task->last_xfer = jiffies; 14289c19a7d0SMike Christie conn->task = NULL; 1429d355e57dSMike Christie } 1430659743b0SShlomo Pongratz /* regular RX path uses back_lock */ 143172b97402SShlomo Pongratz spin_lock(&conn->session->back_lock); 1432d355e57dSMike Christie __iscsi_put_task(task); 143372b97402SShlomo Pongratz spin_unlock(&conn->session->back_lock); 143477a23c21SMike Christie return rc; 143577a23c21SMike Christie } 143677a23c21SMike Christie 14377996a778SMike Christie /** 14389c19a7d0SMike Christie * iscsi_requeue_task - requeue task to run from session workqueue 14399c19a7d0SMike Christie * @task: task to requeue 1440843c0a8aSMike Christie * 14419c19a7d0SMike Christie * LLDs that need to run a task from the session workqueue should call 1442659743b0SShlomo Pongratz * this. The session frwd_lock must be held. This should only be called 1443052d0144SMike Christie * by software drivers. 1444843c0a8aSMike Christie */ 14459c19a7d0SMike Christie void iscsi_requeue_task(struct iscsi_task *task) 1446843c0a8aSMike Christie { 14479c19a7d0SMike Christie struct iscsi_conn *conn = task->conn; 1448843c0a8aSMike Christie 14493bbaaad9SMike Christie /* 14503bbaaad9SMike Christie * this may be on the requeue list already if the xmit_task callout 14513bbaaad9SMike Christie * is handling the r2ts while we are adding new ones 14523bbaaad9SMike Christie */ 14536f8830f5SChris Leech spin_lock_bh(&conn->taskqueuelock); 14543bbaaad9SMike Christie if (list_empty(&task->running)) 14553bbaaad9SMike Christie list_add_tail(&task->running, &conn->requeue); 14566f8830f5SChris Leech spin_unlock_bh(&conn->taskqueuelock); 145732ae763eSMike Christie iscsi_conn_queue_work(conn); 1458843c0a8aSMike Christie } 14599c19a7d0SMike Christie EXPORT_SYMBOL_GPL(iscsi_requeue_task); 1460843c0a8aSMike Christie 1461843c0a8aSMike Christie /** 14627996a778SMike Christie * iscsi_data_xmit - xmit any command into the scheduled connection 14637996a778SMike Christie * @conn: iscsi connection 14647996a778SMike Christie * 14657996a778SMike Christie * Notes: 14667996a778SMike Christie * The function can return -EAGAIN in which case the caller must 14677996a778SMike Christie * re-schedule it again later or recover. '0' return code means 14687996a778SMike Christie * successful xmit. 14697996a778SMike Christie **/ 14707996a778SMike Christie static int iscsi_data_xmit(struct iscsi_conn *conn) 14717996a778SMike Christie { 14725d12c05eSMike Christie struct iscsi_task *task; 14733219e529SMike Christie int rc = 0; 14747996a778SMike Christie 1475659743b0SShlomo Pongratz spin_lock_bh(&conn->session->frwd_lock); 147670b31c15SMike Christie if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) { 14771b2c7af8SMike Christie ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n"); 1478659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 14793219e529SMike Christie return -ENODATA; 14807996a778SMike Christie } 14817996a778SMike Christie 14829c19a7d0SMike Christie if (conn->task) { 14839c19a7d0SMike Christie rc = iscsi_xmit_task(conn); 14843219e529SMike Christie if (rc) 148570b31c15SMike Christie goto done; 14867996a778SMike Christie } 148777a23c21SMike Christie 148877a23c21SMike Christie /* 148977a23c21SMike Christie * process mgmt pdus like nops before commands since we should 149077a23c21SMike Christie * only have one nop-out as a ping from us and targets should not 149177a23c21SMike Christie * overflow us with nop-ins 149277a23c21SMike Christie */ 14936f8830f5SChris Leech spin_lock_bh(&conn->taskqueuelock); 149477a23c21SMike Christie check_mgmt: 1495843c0a8aSMike Christie while (!list_empty(&conn->mgmtqueue)) { 14969c19a7d0SMike Christie conn->task = list_entry(conn->mgmtqueue.next, 14979c19a7d0SMike Christie struct iscsi_task, running); 14983bbaaad9SMike Christie list_del_init(&conn->task->running); 14996f8830f5SChris Leech spin_unlock_bh(&conn->taskqueuelock); 15009c19a7d0SMike Christie if (iscsi_prep_mgmt_task(conn, conn->task)) { 1501659743b0SShlomo Pongratz /* regular RX path uses back_lock */ 1502659743b0SShlomo Pongratz spin_lock_bh(&conn->session->back_lock); 15039c19a7d0SMike Christie __iscsi_put_task(conn->task); 1504659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->back_lock); 15059c19a7d0SMike Christie conn->task = NULL; 15066f8830f5SChris Leech spin_lock_bh(&conn->taskqueuelock); 1507b3a7ea8dSMike Christie continue; 1508b3a7ea8dSMike Christie } 15099c19a7d0SMike Christie rc = iscsi_xmit_task(conn); 15103219e529SMike Christie if (rc) 151170b31c15SMike Christie goto done; 15126f8830f5SChris Leech spin_lock_bh(&conn->taskqueuelock); 15137996a778SMike Christie } 15147996a778SMike Christie 1515843c0a8aSMike Christie /* process pending command queue */ 15163bbaaad9SMike Christie while (!list_empty(&conn->cmdqueue)) { 15175d12c05eSMike Christie conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task, 15185d12c05eSMike Christie running); 15193bbaaad9SMike Christie list_del_init(&conn->task->running); 15206f8830f5SChris Leech spin_unlock_bh(&conn->taskqueuelock); 1521b3a7ea8dSMike Christie if (conn->session->state == ISCSI_STATE_LOGGING_OUT) { 1522b3cd5050SMike Christie fail_scsi_task(conn->task, DID_IMM_RETRY); 15236f8830f5SChris Leech spin_lock_bh(&conn->taskqueuelock); 1524b3a7ea8dSMike Christie continue; 1525b3a7ea8dSMike Christie } 1526577577daSMike Christie rc = iscsi_prep_scsi_cmd_pdu(conn->task); 1527577577daSMike Christie if (rc) { 15285d12c05eSMike Christie if (rc == -ENOMEM || rc == -EACCES) { 15296f8830f5SChris Leech spin_lock_bh(&conn->taskqueuelock); 15303bbaaad9SMike Christie list_add_tail(&conn->task->running, 15313bbaaad9SMike Christie &conn->cmdqueue); 1532577577daSMike Christie conn->task = NULL; 15336f8830f5SChris Leech spin_unlock_bh(&conn->taskqueuelock); 153470b31c15SMike Christie goto done; 1535577577daSMike Christie } else 1536b3cd5050SMike Christie fail_scsi_task(conn->task, DID_ABORT); 15376f8830f5SChris Leech spin_lock_bh(&conn->taskqueuelock); 1538004d6530SBoaz Harrosh continue; 1539004d6530SBoaz Harrosh } 15409c19a7d0SMike Christie rc = iscsi_xmit_task(conn); 15413219e529SMike Christie if (rc) 154270b31c15SMike Christie goto done; 154377a23c21SMike Christie /* 15449c19a7d0SMike Christie * we could continuously get new task requests so 154577a23c21SMike Christie * we need to check the mgmt queue for nops that need to 154677a23c21SMike Christie * be sent to aviod starvation 154777a23c21SMike Christie */ 15486f8830f5SChris Leech spin_lock_bh(&conn->taskqueuelock); 1549843c0a8aSMike Christie if (!list_empty(&conn->mgmtqueue)) 1550843c0a8aSMike Christie goto check_mgmt; 1551843c0a8aSMike Christie } 1552843c0a8aSMike Christie 1553843c0a8aSMike Christie while (!list_empty(&conn->requeue)) { 1554b3a7ea8dSMike Christie /* 1555b3a7ea8dSMike Christie * we always do fastlogout - conn stop code will clean up. 1556b3a7ea8dSMike Christie */ 1557b3a7ea8dSMike Christie if (conn->session->state == ISCSI_STATE_LOGGING_OUT) 1558b3a7ea8dSMike Christie break; 1559b3a7ea8dSMike Christie 15605d12c05eSMike Christie task = list_entry(conn->requeue.next, struct iscsi_task, 15615d12c05eSMike Christie running); 15625d12c05eSMike Christie if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT)) 15635d12c05eSMike Christie break; 15645d12c05eSMike Christie 15655d12c05eSMike Christie conn->task = task; 15663bbaaad9SMike Christie list_del_init(&conn->task->running); 15679c19a7d0SMike Christie conn->task->state = ISCSI_TASK_RUNNING; 15686f8830f5SChris Leech spin_unlock_bh(&conn->taskqueuelock); 15699c19a7d0SMike Christie rc = iscsi_xmit_task(conn); 1570843c0a8aSMike Christie if (rc) 157170b31c15SMike Christie goto done; 15726f8830f5SChris Leech spin_lock_bh(&conn->taskqueuelock); 1573843c0a8aSMike Christie if (!list_empty(&conn->mgmtqueue)) 157477a23c21SMike Christie goto check_mgmt; 15757996a778SMike Christie } 15766f8830f5SChris Leech spin_unlock_bh(&conn->taskqueuelock); 1577659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 15783219e529SMike Christie return -ENODATA; 15797996a778SMike Christie 158070b31c15SMike Christie done: 1581659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 15823219e529SMike Christie return rc; 15837996a778SMike Christie } 15847996a778SMike Christie 1585c4028958SDavid Howells static void iscsi_xmitworker(struct work_struct *work) 15867996a778SMike Christie { 1587c4028958SDavid Howells struct iscsi_conn *conn = 1588c4028958SDavid Howells container_of(work, struct iscsi_conn, xmitwork); 15893219e529SMike Christie int rc; 15907996a778SMike Christie /* 15917996a778SMike Christie * serialize Xmit worker on a per-connection basis. 15927996a778SMike Christie */ 15933219e529SMike Christie do { 15943219e529SMike Christie rc = iscsi_data_xmit(conn); 15953219e529SMike Christie } while (rc >= 0 || rc == -EAGAIN); 15967996a778SMike Christie } 15977996a778SMike Christie 1598577577daSMike Christie static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn, 1599577577daSMike Christie struct scsi_cmnd *sc) 1600577577daSMike Christie { 1601577577daSMike Christie struct iscsi_task *task; 1602577577daSMike Christie 16037acd72ebSStefani Seibold if (!kfifo_out(&conn->session->cmdpool.queue, 1604577577daSMike Christie (void *) &task, sizeof(void *))) 1605577577daSMike Christie return NULL; 1606577577daSMike Christie 1607577577daSMike Christie sc->SCp.phase = conn->session->age; 1608577577daSMike Christie sc->SCp.ptr = (char *) task; 1609577577daSMike Christie 16106dc618cdSElena Reshetova refcount_set(&task->refcount, 1); 1611577577daSMike Christie task->state = ISCSI_TASK_PENDING; 1612577577daSMike Christie task->conn = conn; 1613577577daSMike Christie task->sc = sc; 1614d355e57dSMike Christie task->have_checked_conn = false; 1615d355e57dSMike Christie task->last_timeout = jiffies; 1616d355e57dSMike Christie task->last_xfer = jiffies; 161755e51edaSSagi Grimberg task->protected = false; 1618577577daSMike Christie INIT_LIST_HEAD(&task->running); 1619577577daSMike Christie return task; 1620577577daSMike Christie } 1621577577daSMike Christie 16227996a778SMike Christie enum { 16237996a778SMike Christie FAILURE_BAD_HOST = 1, 16247996a778SMike Christie FAILURE_SESSION_FAILED, 16257996a778SMike Christie FAILURE_SESSION_FREED, 16267996a778SMike Christie FAILURE_WINDOW_CLOSED, 162760ecebf5SMike Christie FAILURE_OOM, 16287996a778SMike Christie FAILURE_SESSION_TERMINATE, 1629656cffc9SMike Christie FAILURE_SESSION_IN_RECOVERY, 16307996a778SMike Christie FAILURE_SESSION_RECOVERY_TIMEOUT, 1631b3a7ea8dSMike Christie FAILURE_SESSION_LOGGING_OUT, 16326eabafbeSMike Christie FAILURE_SESSION_NOT_READY, 16337996a778SMike Christie }; 16347996a778SMike Christie 1635f41d4721SMike Christie int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc) 16367996a778SMike Christie { 163775613521SMike Christie struct iscsi_cls_session *cls_session; 16381336aed1SMike Christie struct iscsi_host *ihost; 16397996a778SMike Christie int reason = 0; 16407996a778SMike Christie struct iscsi_session *session; 16417996a778SMike Christie struct iscsi_conn *conn; 16429c19a7d0SMike Christie struct iscsi_task *task = NULL; 16437996a778SMike Christie 16447996a778SMike Christie sc->result = 0; 1645f47f2cf5SMike Christie sc->SCp.ptr = NULL; 16467996a778SMike Christie 16471336aed1SMike Christie ihost = shost_priv(host); 16487996a778SMike Christie 164975613521SMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 165075613521SMike Christie session = cls_session->dd_data; 1651659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 16527996a778SMike Christie 165375613521SMike Christie reason = iscsi_session_chkready(cls_session); 16546eabafbeSMike Christie if (reason) { 16556eabafbeSMike Christie sc->result = reason; 16566eabafbeSMike Christie goto fault; 16576eabafbeSMike Christie } 16586eabafbeSMike Christie 1659301e0f7eSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) { 1660656cffc9SMike Christie /* 1661656cffc9SMike Christie * to handle the race between when we set the recovery state 1662656cffc9SMike Christie * and block the session we requeue here (commands could 1663656cffc9SMike Christie * be entering our queuecommand while a block is starting 1664656cffc9SMike Christie * up because the block code is not locked) 1665656cffc9SMike Christie */ 16669000bcd6SMike Christie switch (session->state) { 1667301e0f7eSMike Christie case ISCSI_STATE_FAILED: 1668d7549412SRafael David Tinoco /* 1669d7549412SRafael David Tinoco * cmds should fail during shutdown, if the session 1670d7549412SRafael David Tinoco * state is bad, allowing completion to happen 1671d7549412SRafael David Tinoco */ 1672d7549412SRafael David Tinoco if (unlikely(system_state != SYSTEM_RUNNING)) { 1673d7549412SRafael David Tinoco reason = FAILURE_SESSION_FAILED; 1674d7549412SRafael David Tinoco sc->result = DID_NO_CONNECT << 16; 1675d7549412SRafael David Tinoco break; 1676d7549412SRafael David Tinoco } 1677807cf197SBart Van Assche /* fall through */ 16789000bcd6SMike Christie case ISCSI_STATE_IN_RECOVERY: 1679656cffc9SMike Christie reason = FAILURE_SESSION_IN_RECOVERY; 1680301e0f7eSMike Christie sc->result = DID_IMM_RETRY << 16; 1681301e0f7eSMike Christie break; 16829000bcd6SMike Christie case ISCSI_STATE_LOGGING_OUT: 16839000bcd6SMike Christie reason = FAILURE_SESSION_LOGGING_OUT; 1684301e0f7eSMike Christie sc->result = DID_IMM_RETRY << 16; 1685301e0f7eSMike Christie break; 1686b3a7ea8dSMike Christie case ISCSI_STATE_RECOVERY_FAILED: 1687656cffc9SMike Christie reason = FAILURE_SESSION_RECOVERY_TIMEOUT; 168856d7fcfaSMike Christie sc->result = DID_TRANSPORT_FAILFAST << 16; 1689b3a7ea8dSMike Christie break; 1690b3a7ea8dSMike Christie case ISCSI_STATE_TERMINATE: 1691656cffc9SMike Christie reason = FAILURE_SESSION_TERMINATE; 16926eabafbeSMike Christie sc->result = DID_NO_CONNECT << 16; 1693b3a7ea8dSMike Christie break; 1694b3a7ea8dSMike Christie default: 16957996a778SMike Christie reason = FAILURE_SESSION_FREED; 16966eabafbeSMike Christie sc->result = DID_NO_CONNECT << 16; 1697b3a7ea8dSMike Christie } 16987996a778SMike Christie goto fault; 16997996a778SMike Christie } 17007996a778SMike Christie 17017996a778SMike Christie conn = session->leadconn; 170298644047SMike Christie if (!conn) { 170398644047SMike Christie reason = FAILURE_SESSION_FREED; 17046eabafbeSMike Christie sc->result = DID_NO_CONNECT << 16; 170598644047SMike Christie goto fault; 170698644047SMike Christie } 17077996a778SMike Christie 1708661134adSMike Christie if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) { 1709661134adSMike Christie reason = FAILURE_SESSION_IN_RECOVERY; 1710eef9ffdfSJohannes Thumshirn sc->result = DID_REQUEUE << 16; 1711661134adSMike Christie goto fault; 1712661134adSMike Christie } 1713661134adSMike Christie 171477a23c21SMike Christie if (iscsi_check_cmdsn_window_closed(conn)) { 171577a23c21SMike Christie reason = FAILURE_WINDOW_CLOSED; 171677a23c21SMike Christie goto reject; 171777a23c21SMike Christie } 171877a23c21SMike Christie 1719577577daSMike Christie task = iscsi_alloc_task(conn, sc); 1720577577daSMike Christie if (!task) { 172160ecebf5SMike Christie reason = FAILURE_OOM; 172260ecebf5SMike Christie goto reject; 172360ecebf5SMike Christie } 17247996a778SMike Christie 17251336aed1SMike Christie if (!ihost->workq) { 1726577577daSMike Christie reason = iscsi_prep_scsi_cmd_pdu(task); 1727577577daSMike Christie if (reason) { 17285d12c05eSMike Christie if (reason == -ENOMEM || reason == -EACCES) { 1729577577daSMike Christie reason = FAILURE_OOM; 1730577577daSMike Christie goto prepd_reject; 1731577577daSMike Christie } else { 1732052d0144SMike Christie sc->result = DID_ABORT << 16; 1733577577daSMike Christie goto prepd_fault; 1734577577daSMike Christie } 1735052d0144SMike Christie } 17369c19a7d0SMike Christie if (session->tt->xmit_task(task)) { 1737d3305f34SMike Christie session->cmdsn--; 1738052d0144SMike Christie reason = FAILURE_SESSION_NOT_READY; 1739577577daSMike Christie goto prepd_reject; 1740052d0144SMike Christie } 17413bbaaad9SMike Christie } else { 17426f8830f5SChris Leech spin_lock_bh(&conn->taskqueuelock); 17433bbaaad9SMike Christie list_add_tail(&task->running, &conn->cmdqueue); 17446f8830f5SChris Leech spin_unlock_bh(&conn->taskqueuelock); 174532ae763eSMike Christie iscsi_conn_queue_work(conn); 17463bbaaad9SMike Christie } 1747052d0144SMike Christie 1748052d0144SMike Christie session->queued_cmdsn++; 1749659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 17507996a778SMike Christie return 0; 17517996a778SMike Christie 1752577577daSMike Christie prepd_reject: 1753a656183eSLee Duncan spin_lock_bh(&session->back_lock); 1754f41d4721SMike Christie iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ); 1755a656183eSLee Duncan spin_unlock_bh(&session->back_lock); 17567996a778SMike Christie reject: 1757659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 17581b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n", 17591b2c7af8SMike Christie sc->cmnd[0], reason); 1760d6d13ee1SMike Christie return SCSI_MLQUEUE_TARGET_BUSY; 17617996a778SMike Christie 1762577577daSMike Christie prepd_fault: 1763a656183eSLee Duncan spin_lock_bh(&session->back_lock); 1764f41d4721SMike Christie iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ); 1765a656183eSLee Duncan spin_unlock_bh(&session->back_lock); 17667996a778SMike Christie fault: 1767659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 17681b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n", 17691b2c7af8SMike Christie sc->cmnd[0], reason); 17701c138991SFUJITA Tomonori scsi_set_resid(sc, scsi_bufflen(sc)); 1771f41d4721SMike Christie sc->scsi_done(sc); 17727996a778SMike Christie return 0; 17737996a778SMike Christie } 17747996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_queuecommand); 17757996a778SMike Christie 17766b5d6c44SMike Christie int iscsi_target_alloc(struct scsi_target *starget) 17776b5d6c44SMike Christie { 17786b5d6c44SMike Christie struct iscsi_cls_session *cls_session = starget_to_session(starget); 17796b5d6c44SMike Christie struct iscsi_session *session = cls_session->dd_data; 17806b5d6c44SMike Christie 17816b5d6c44SMike Christie starget->can_queue = session->scsi_cmds_max; 17826b5d6c44SMike Christie return 0; 17836b5d6c44SMike Christie } 17846b5d6c44SMike Christie EXPORT_SYMBOL_GPL(iscsi_target_alloc); 17856b5d6c44SMike Christie 17862c4b9637SKees Cook static void iscsi_tmf_timedout(struct timer_list *t) 17877996a778SMike Christie { 17882c4b9637SKees Cook struct iscsi_conn *conn = from_timer(conn, t, tmf_timer); 17897996a778SMike Christie struct iscsi_session *session = conn->session; 17907996a778SMike Christie 1791659743b0SShlomo Pongratz spin_lock(&session->frwd_lock); 1792843c0a8aSMike Christie if (conn->tmf_state == TMF_QUEUED) { 1793843c0a8aSMike Christie conn->tmf_state = TMF_TIMEDOUT; 1794bd2199d4SErez Zilber ISCSI_DBG_EH(session, "tmf timedout\n"); 17957996a778SMike Christie /* unblock eh_abort() */ 17967996a778SMike Christie wake_up(&conn->ehwait); 17977996a778SMike Christie } 1798659743b0SShlomo Pongratz spin_unlock(&session->frwd_lock); 17997996a778SMike Christie } 18007996a778SMike Christie 18019c19a7d0SMike Christie static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn, 1802f6d5180cSMike Christie struct iscsi_tm *hdr, int age, 1803f6d5180cSMike Christie int timeout) 18041360c58aSBart Van Assche __must_hold(&session->frwd_lock) 18057996a778SMike Christie { 18067996a778SMike Christie struct iscsi_session *session = conn->session; 18079c19a7d0SMike Christie struct iscsi_task *task; 18087996a778SMike Christie 18099c19a7d0SMike Christie task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr, 18107996a778SMike Christie NULL, 0); 18119c19a7d0SMike Christie if (!task) { 1812659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 1813df4da5cdSMike Christie iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n"); 18147996a778SMike Christie iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 1815659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 181677a23c21SMike Christie return -EPERM; 18177996a778SMike Christie } 18187996a778SMike Christie conn->tmfcmd_pdus_cnt++; 1819f6d5180cSMike Christie conn->tmf_timer.expires = timeout * HZ + jiffies; 1820843c0a8aSMike Christie add_timer(&conn->tmf_timer); 1821bd2199d4SErez Zilber ISCSI_DBG_EH(session, "tmf set timeout\n"); 1822843c0a8aSMike Christie 1823659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 18246724add1SMike Christie mutex_unlock(&session->eh_mutex); 18257996a778SMike Christie 18267996a778SMike Christie /* 18277996a778SMike Christie * block eh thread until: 18287996a778SMike Christie * 1829843c0a8aSMike Christie * 1) tmf response 1830843c0a8aSMike Christie * 2) tmf timeout 18317996a778SMike Christie * 3) session is terminated or restarted or userspace has 18327996a778SMike Christie * given up on recovery 18337996a778SMike Christie */ 1834843c0a8aSMike Christie wait_event_interruptible(conn->ehwait, age != session->age || 18357996a778SMike Christie session->state != ISCSI_STATE_LOGGED_IN || 1836843c0a8aSMike Christie conn->tmf_state != TMF_QUEUED); 18377996a778SMike Christie if (signal_pending(current)) 18387996a778SMike Christie flush_signals(current); 1839843c0a8aSMike Christie del_timer_sync(&conn->tmf_timer); 1840843c0a8aSMike Christie 18416724add1SMike Christie mutex_lock(&session->eh_mutex); 1842659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 18439c19a7d0SMike Christie /* if the session drops it will clean up the task */ 1844843c0a8aSMike Christie if (age != session->age || 1845843c0a8aSMike Christie session->state != ISCSI_STATE_LOGGED_IN) 1846843c0a8aSMike Christie return -ENOTCONN; 18477996a778SMike Christie return 0; 18487996a778SMike Christie } 18497996a778SMike Christie 18507996a778SMike Christie /* 1851843c0a8aSMike Christie * Fail commands. session lock held and recv side suspended and xmit 1852843c0a8aSMike Christie * thread flushed 1853843c0a8aSMike Christie */ 18549cb78c16SHannes Reinecke static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error) 1855843c0a8aSMike Christie { 18563bbaaad9SMike Christie struct iscsi_task *task; 18573bbaaad9SMike Christie int i; 1858843c0a8aSMike Christie 18593bbaaad9SMike Christie for (i = 0; i < conn->session->cmds_max; i++) { 18603bbaaad9SMike Christie task = conn->session->cmds[i]; 18613bbaaad9SMike Christie if (!task->sc || task->state == ISCSI_TASK_FREE) 18623bbaaad9SMike Christie continue; 1863843c0a8aSMike Christie 18643bbaaad9SMike Christie if (lun != -1 && lun != task->sc->device->lun) 18653bbaaad9SMike Christie continue; 18663bbaaad9SMike Christie 18671b2c7af8SMike Christie ISCSI_DBG_SESSION(conn->session, 18683bbaaad9SMike Christie "failing sc %p itt 0x%x state %d\n", 18693bbaaad9SMike Christie task->sc, task->itt, task->state); 1870b3cd5050SMike Christie fail_scsi_task(task, error); 1871843c0a8aSMike Christie } 1872843c0a8aSMike Christie } 1873843c0a8aSMike Christie 1874661134adSMike Christie /** 1875661134adSMike Christie * iscsi_suspend_queue - suspend iscsi_queuecommand 1876661134adSMike Christie * @conn: iscsi conn to stop queueing IO on 1877661134adSMike Christie * 1878659743b0SShlomo Pongratz * This grabs the session frwd_lock to make sure no one is in 1879661134adSMike Christie * xmit_task/queuecommand, and then sets suspend to prevent 1880661134adSMike Christie * new commands from being queued. This only needs to be called 1881661134adSMike Christie * by offload drivers that need to sync a path like ep disconnect 1882661134adSMike Christie * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi 1883661134adSMike Christie * will call iscsi_start_tx and iscsi_unblock_session when in FFP. 1884661134adSMike Christie */ 1885661134adSMike Christie void iscsi_suspend_queue(struct iscsi_conn *conn) 1886661134adSMike Christie { 1887659743b0SShlomo Pongratz spin_lock_bh(&conn->session->frwd_lock); 1888661134adSMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 1889659743b0SShlomo Pongratz spin_unlock_bh(&conn->session->frwd_lock); 1890661134adSMike Christie } 1891661134adSMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_queue); 1892661134adSMike Christie 1893661134adSMike Christie /** 1894661134adSMike Christie * iscsi_suspend_tx - suspend iscsi_data_xmit 1895661134adSMike Christie * @conn: iscsi conn tp stop processing IO on. 1896661134adSMike Christie * 1897661134adSMike Christie * This function sets the suspend bit to prevent iscsi_data_xmit 1898661134adSMike Christie * from sending new IO, and if work is queued on the xmit thread 1899661134adSMike Christie * it will wait for it to be completed. 1900661134adSMike Christie */ 1901b40977d9SMike Christie void iscsi_suspend_tx(struct iscsi_conn *conn) 19026724add1SMike Christie { 190332ae763eSMike Christie struct Scsi_Host *shost = conn->session->host; 190432ae763eSMike Christie struct iscsi_host *ihost = shost_priv(shost); 190532ae763eSMike Christie 19066724add1SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 19071336aed1SMike Christie if (ihost->workq) 190832ae763eSMike Christie flush_workqueue(ihost->workq); 19096724add1SMike Christie } 1910b40977d9SMike Christie EXPORT_SYMBOL_GPL(iscsi_suspend_tx); 19116724add1SMike Christie 19126724add1SMike Christie static void iscsi_start_tx(struct iscsi_conn *conn) 19136724add1SMike Christie { 19146724add1SMike Christie clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 191532ae763eSMike Christie iscsi_conn_queue_work(conn); 19166724add1SMike Christie } 19176724add1SMike Christie 19184c48a829SMike Christie /* 19194c48a829SMike Christie * We want to make sure a ping is in flight. It has timed out. 19204c48a829SMike Christie * And we are not busy processing a pdu that is making 19214c48a829SMike Christie * progress but got started before the ping and is taking a while 19224c48a829SMike Christie * to complete so the ping is just stuck behind it in a queue. 19234c48a829SMike Christie */ 19244c48a829SMike Christie static int iscsi_has_ping_timed_out(struct iscsi_conn *conn) 19254c48a829SMike Christie { 19264c48a829SMike Christie if (conn->ping_task && 19274c48a829SMike Christie time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) + 19284c48a829SMike Christie (conn->ping_timeout * HZ), jiffies)) 19294c48a829SMike Christie return 1; 19304c48a829SMike Christie else 19314c48a829SMike Christie return 0; 19324c48a829SMike Christie } 19334c48a829SMike Christie 1934b6a05c82SChristoph Hellwig enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc) 1935f6d5180cSMike Christie { 19366600593cSChristoph Hellwig enum blk_eh_timer_return rc = BLK_EH_DONE; 193792ed4d69SMike Christie struct iscsi_task *task = NULL, *running_task; 1938f6d5180cSMike Christie struct iscsi_cls_session *cls_session; 1939f6d5180cSMike Christie struct iscsi_session *session; 1940f6d5180cSMike Christie struct iscsi_conn *conn; 194192ed4d69SMike Christie int i; 1942f6d5180cSMike Christie 1943d355e57dSMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 194475613521SMike Christie session = cls_session->dd_data; 1945f6d5180cSMike Christie 1946bd2199d4SErez Zilber ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc); 1947f6d5180cSMike Christie 1948*5480e299SBart Van Assche spin_lock_bh(&session->frwd_lock); 1949e3d338a5SMike Christie task = (struct iscsi_task *)sc->SCp.ptr; 1950e3d338a5SMike Christie if (!task) { 1951e3d338a5SMike Christie /* 1952e3d338a5SMike Christie * Raced with completion. Blk layer has taken ownership 1953e3d338a5SMike Christie * so let timeout code complete it now. 1954e3d338a5SMike Christie */ 1955adb2b769SChristoph Hellwig rc = BLK_EH_DONE; 1956e3d338a5SMike Christie goto done; 1957e3d338a5SMike Christie } 1958e3d338a5SMike Christie 1959f6d5180cSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) { 1960f6d5180cSMike Christie /* 1961d7549412SRafael David Tinoco * During shutdown, if session is prematurely disconnected, 1962d7549412SRafael David Tinoco * recovery won't happen and there will be hung cmds. Not 1963d7549412SRafael David Tinoco * handling cmds would trigger EH, also bad in this case. 1964d7549412SRafael David Tinoco * Instead, handle cmd, allow completion to happen and let 1965d7549412SRafael David Tinoco * upper layer to deal with the result. 1966d7549412SRafael David Tinoco */ 1967d7549412SRafael David Tinoco if (unlikely(system_state != SYSTEM_RUNNING)) { 1968d7549412SRafael David Tinoco sc->result = DID_NO_CONNECT << 16; 1969d7549412SRafael David Tinoco ISCSI_DBG_EH(session, "sc on shutdown, handled\n"); 1970adb2b769SChristoph Hellwig rc = BLK_EH_DONE; 1971d7549412SRafael David Tinoco goto done; 1972d7549412SRafael David Tinoco } 1973d7549412SRafael David Tinoco /* 1974f6d5180cSMike Christie * We are probably in the middle of iscsi recovery so let 1975f6d5180cSMike Christie * that complete and handle the error. 1976f6d5180cSMike Christie */ 1977242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 1978f6d5180cSMike Christie goto done; 1979f6d5180cSMike Christie } 1980f6d5180cSMike Christie 1981f6d5180cSMike Christie conn = session->leadconn; 1982f6d5180cSMike Christie if (!conn) { 1983f6d5180cSMike Christie /* In the middle of shuting down */ 1984242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 1985f6d5180cSMike Christie goto done; 1986f6d5180cSMike Christie } 1987f6d5180cSMike Christie 1988d355e57dSMike Christie /* 1989d355e57dSMike Christie * If we have sent (at least queued to the network layer) a pdu or 1990d355e57dSMike Christie * recvd one for the task since the last timeout ask for 1991d355e57dSMike Christie * more time. If on the next timeout we have not made progress 1992d355e57dSMike Christie * we can check if it is the task or connection when we send the 1993d355e57dSMike Christie * nop as a ping. 1994d355e57dSMike Christie */ 199592ed4d69SMike Christie if (time_after(task->last_xfer, task->last_timeout)) { 1996bd2199d4SErez Zilber ISCSI_DBG_EH(session, "Command making progress. Asking " 1997d355e57dSMike Christie "scsi-ml for more time to complete. " 199892ed4d69SMike Christie "Last data xfer at %lu. Last timeout was at " 1999d355e57dSMike Christie "%lu\n.", task->last_xfer, task->last_timeout); 2000d355e57dSMike Christie task->have_checked_conn = false; 2001d355e57dSMike Christie rc = BLK_EH_RESET_TIMER; 2002d355e57dSMike Christie goto done; 2003d355e57dSMike Christie } 2004d355e57dSMike Christie 2005f6d5180cSMike Christie if (!conn->recv_timeout && !conn->ping_timeout) 2006f6d5180cSMike Christie goto done; 2007f6d5180cSMike Christie /* 2008f6d5180cSMike Christie * if the ping timedout then we are in the middle of cleaning up 2009f6d5180cSMike Christie * and can let the iscsi eh handle it 2010f6d5180cSMike Christie */ 20114c48a829SMike Christie if (iscsi_has_ping_timed_out(conn)) { 2012242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 20134c48a829SMike Christie goto done; 20144c48a829SMike Christie } 2015d355e57dSMike Christie 201692ed4d69SMike Christie for (i = 0; i < conn->session->cmds_max; i++) { 201792ed4d69SMike Christie running_task = conn->session->cmds[i]; 201892ed4d69SMike Christie if (!running_task->sc || running_task == task || 201992ed4d69SMike Christie running_task->state != ISCSI_TASK_RUNNING) 202092ed4d69SMike Christie continue; 202192ed4d69SMike Christie 202292ed4d69SMike Christie /* 202392ed4d69SMike Christie * Only check if cmds started before this one have made 202492ed4d69SMike Christie * progress, or this could never fail 202592ed4d69SMike Christie */ 202692ed4d69SMike Christie if (time_after(running_task->sc->jiffies_at_alloc, 202792ed4d69SMike Christie task->sc->jiffies_at_alloc)) 202892ed4d69SMike Christie continue; 202992ed4d69SMike Christie 203092ed4d69SMike Christie if (time_after(running_task->last_xfer, task->last_timeout)) { 203192ed4d69SMike Christie /* 203292ed4d69SMike Christie * This task has not made progress, but a task 203392ed4d69SMike Christie * started before us has transferred data since 203492ed4d69SMike Christie * we started/last-checked. We could be queueing 203592ed4d69SMike Christie * too many tasks or the LU is bad. 203692ed4d69SMike Christie * 203792ed4d69SMike Christie * If the device is bad the cmds ahead of us on 203892ed4d69SMike Christie * other devs will complete, and this loop will 203992ed4d69SMike Christie * eventually fail starting the scsi eh. 204092ed4d69SMike Christie */ 204192ed4d69SMike Christie ISCSI_DBG_EH(session, "Command has not made progress " 204292ed4d69SMike Christie "but commands ahead of it have. " 204392ed4d69SMike Christie "Asking scsi-ml for more time to " 204492ed4d69SMike Christie "complete. Our last xfer vs running task " 204592ed4d69SMike Christie "last xfer %lu/%lu. Last check %lu.\n", 204692ed4d69SMike Christie task->last_xfer, running_task->last_xfer, 204792ed4d69SMike Christie task->last_timeout); 204892ed4d69SMike Christie rc = BLK_EH_RESET_TIMER; 204992ed4d69SMike Christie goto done; 205092ed4d69SMike Christie } 205192ed4d69SMike Christie } 205292ed4d69SMike Christie 2053d355e57dSMike Christie /* Assumes nop timeout is shorter than scsi cmd timeout */ 2054d355e57dSMike Christie if (task->have_checked_conn) 2055d355e57dSMike Christie goto done; 2056d355e57dSMike Christie 2057f6d5180cSMike Christie /* 2058d355e57dSMike Christie * Checking the transport already or nop from a cmd timeout still 2059d355e57dSMike Christie * running 2060f6d5180cSMike Christie */ 2061d355e57dSMike Christie if (conn->ping_task) { 2062d355e57dSMike Christie task->have_checked_conn = true; 2063242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 20644c48a829SMike Christie goto done; 20654c48a829SMike Christie } 20664c48a829SMike Christie 2067d355e57dSMike Christie /* Make sure there is a transport check done */ 2068d355e57dSMike Christie iscsi_send_nopout(conn, NULL); 2069d355e57dSMike Christie task->have_checked_conn = true; 2070242f9dcbSJens Axboe rc = BLK_EH_RESET_TIMER; 2071d355e57dSMike Christie 2072f6d5180cSMike Christie done: 2073d355e57dSMike Christie if (task) 2074d355e57dSMike Christie task->last_timeout = jiffies; 2075*5480e299SBart Van Assche spin_unlock_bh(&session->frwd_lock); 2076bd2199d4SErez Zilber ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ? 2077d7549412SRafael David Tinoco "timer reset" : "shutdown or nh"); 2078f6d5180cSMike Christie return rc; 2079f6d5180cSMike Christie } 2080b6a05c82SChristoph Hellwig EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out); 2081f6d5180cSMike Christie 20822c4b9637SKees Cook static void iscsi_check_transport_timeouts(struct timer_list *t) 2083f6d5180cSMike Christie { 20842c4b9637SKees Cook struct iscsi_conn *conn = from_timer(conn, t, transport_timer); 2085f6d5180cSMike Christie struct iscsi_session *session = conn->session; 20864cf10435SMike Christie unsigned long recv_timeout, next_timeout = 0, last_recv; 2087f6d5180cSMike Christie 2088659743b0SShlomo Pongratz spin_lock(&session->frwd_lock); 2089f6d5180cSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) 2090f6d5180cSMike Christie goto done; 2091f6d5180cSMike Christie 20924cf10435SMike Christie recv_timeout = conn->recv_timeout; 20934cf10435SMike Christie if (!recv_timeout) 2094f6d5180cSMike Christie goto done; 2095f6d5180cSMike Christie 20964cf10435SMike Christie recv_timeout *= HZ; 2097f6d5180cSMike Christie last_recv = conn->last_recv; 20984c48a829SMike Christie 20994c48a829SMike Christie if (iscsi_has_ping_timed_out(conn)) { 2100322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs " 21014c48a829SMike Christie "expired, recv timeout %d, last rx %lu, " 21024c48a829SMike Christie "last ping %lu, now %lu\n", 21034c48a829SMike Christie conn->ping_timeout, conn->recv_timeout, 21044c48a829SMike Christie last_recv, conn->last_ping, jiffies); 2105659743b0SShlomo Pongratz spin_unlock(&session->frwd_lock); 210609ff742cSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT); 2107f6d5180cSMike Christie return; 2108f6d5180cSMike Christie } 2109f6d5180cSMike Christie 21104cf10435SMike Christie if (time_before_eq(last_recv + recv_timeout, jiffies)) { 2111f6d5180cSMike Christie /* send a ping to try to provoke some traffic */ 21121b2c7af8SMike Christie ISCSI_DBG_CONN(conn, "Sending nopout as ping\n"); 211352f5664aSAriel Nahum if (iscsi_send_nopout(conn, NULL)) 211452f5664aSAriel Nahum next_timeout = jiffies + (1 * HZ); 211552f5664aSAriel Nahum else 21164cf10435SMike Christie next_timeout = conn->last_ping + (conn->ping_timeout * HZ); 2117ad294e9cSMike Christie } else 21184cf10435SMike Christie next_timeout = last_recv + recv_timeout; 2119f6d5180cSMike Christie 21201b2c7af8SMike Christie ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout); 2121f6d5180cSMike Christie mod_timer(&conn->transport_timer, next_timeout); 2122f6d5180cSMike Christie done: 2123659743b0SShlomo Pongratz spin_unlock(&session->frwd_lock); 2124f6d5180cSMike Christie } 2125f6d5180cSMike Christie 21269c19a7d0SMike Christie static void iscsi_prep_abort_task_pdu(struct iscsi_task *task, 2127843c0a8aSMike Christie struct iscsi_tm *hdr) 2128843c0a8aSMike Christie { 2129843c0a8aSMike Christie memset(hdr, 0, sizeof(*hdr)); 2130843c0a8aSMike Christie hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 2131843c0a8aSMike Christie hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK; 2132843c0a8aSMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 213355bdabdfSAndy Grover hdr->lun = task->lun; 2134577577daSMike Christie hdr->rtt = task->hdr_itt; 2135577577daSMike Christie hdr->refcmdsn = task->cmdsn; 2136843c0a8aSMike Christie } 2137843c0a8aSMike Christie 21387996a778SMike Christie int iscsi_eh_abort(struct scsi_cmnd *sc) 21397996a778SMike Christie { 214075613521SMike Christie struct iscsi_cls_session *cls_session; 214175613521SMike Christie struct iscsi_session *session; 2142f47f2cf5SMike Christie struct iscsi_conn *conn; 21439c19a7d0SMike Christie struct iscsi_task *task; 2144843c0a8aSMike Christie struct iscsi_tm *hdr; 2145e9e410e8SBart Van Assche int age; 21467996a778SMike Christie 214775613521SMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 214875613521SMike Christie session = cls_session->dd_data; 214975613521SMike Christie 2150bd2199d4SErez Zilber ISCSI_DBG_EH(session, "aborting sc %p\n", sc); 21514421c9ebSMike Christie 21526724add1SMike Christie mutex_lock(&session->eh_mutex); 2153659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 2154f47f2cf5SMike Christie /* 2155f47f2cf5SMike Christie * if session was ISCSI_STATE_IN_RECOVERY then we may not have 2156f47f2cf5SMike Christie * got the command. 2157f47f2cf5SMike Christie */ 2158f47f2cf5SMike Christie if (!sc->SCp.ptr) { 2159bd2199d4SErez Zilber ISCSI_DBG_EH(session, "sc never reached iscsi layer or " 21601b2c7af8SMike Christie "it completed.\n"); 2161659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 21626724add1SMike Christie mutex_unlock(&session->eh_mutex); 2163f47f2cf5SMike Christie return SUCCESS; 2164f47f2cf5SMike Christie } 2165f47f2cf5SMike Christie 21667996a778SMike Christie /* 21677996a778SMike Christie * If we are not logged in or we have started a new session 21687996a778SMike Christie * then let the host reset code handle this 21697996a778SMike Christie */ 2170843c0a8aSMike Christie if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN || 2171843c0a8aSMike Christie sc->SCp.phase != session->age) { 2172659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2173843c0a8aSMike Christie mutex_unlock(&session->eh_mutex); 2174bd2199d4SErez Zilber ISCSI_DBG_EH(session, "failing abort due to dropped " 21754421c9ebSMike Christie "session.\n"); 2176843c0a8aSMike Christie return FAILED; 2177843c0a8aSMike Christie } 2178843c0a8aSMike Christie 2179843c0a8aSMike Christie conn = session->leadconn; 2180843c0a8aSMike Christie conn->eh_abort_cnt++; 2181843c0a8aSMike Christie age = session->age; 2182843c0a8aSMike Christie 21839c19a7d0SMike Christie task = (struct iscsi_task *)sc->SCp.ptr; 2184bd2199d4SErez Zilber ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n", 21851b2c7af8SMike Christie sc, task->itt); 21867996a778SMike Christie 21879c19a7d0SMike Christie /* task completed before time out */ 21889c19a7d0SMike Christie if (!task->sc) { 2189bd2199d4SErez Zilber ISCSI_DBG_EH(session, "sc completed while abort in progress\n"); 219077a23c21SMike Christie goto success; 21917ea8b828SMike Christie } 21927996a778SMike Christie 21939c19a7d0SMike Christie if (task->state == ISCSI_TASK_PENDING) { 2194b3cd5050SMike Christie fail_scsi_task(task, DID_ABORT); 219577a23c21SMike Christie goto success; 219677a23c21SMike Christie } 21977996a778SMike Christie 2198843c0a8aSMike Christie /* only have one tmf outstanding at a time */ 2199843c0a8aSMike Christie if (conn->tmf_state != TMF_INITIAL) 22007996a778SMike Christie goto failed; 2201843c0a8aSMike Christie conn->tmf_state = TMF_QUEUED; 22027996a778SMike Christie 2203843c0a8aSMike Christie hdr = &conn->tmhdr; 22049c19a7d0SMike Christie iscsi_prep_abort_task_pdu(task, hdr); 2205843c0a8aSMike Christie 2206e9e410e8SBart Van Assche if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) 2207843c0a8aSMike Christie goto failed; 2208843c0a8aSMike Christie 2209843c0a8aSMike Christie switch (conn->tmf_state) { 2210843c0a8aSMike Christie case TMF_SUCCESS: 2211659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2212913e5bf4SMike Christie /* 2213913e5bf4SMike Christie * stop tx side incase the target had sent a abort rsp but 2214913e5bf4SMike Christie * the initiator was still writing out data. 2215913e5bf4SMike Christie */ 22166724add1SMike Christie iscsi_suspend_tx(conn); 22177996a778SMike Christie /* 2218913e5bf4SMike Christie * we do not stop the recv side because targets have been 2219913e5bf4SMike Christie * good and have never sent us a successful tmf response 2220913e5bf4SMike Christie * then sent more data for the cmd. 22217996a778SMike Christie */ 2222659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 2223b3cd5050SMike Christie fail_scsi_task(task, DID_ABORT); 2224843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 22255d12c05eSMike Christie memset(hdr, 0, sizeof(*hdr)); 2226659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 22276724add1SMike Christie iscsi_start_tx(conn); 222877a23c21SMike Christie goto success_unlocked; 2229843c0a8aSMike Christie case TMF_TIMEDOUT: 2230659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2231df4da5cdSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 2232843c0a8aSMike Christie goto failed_unlocked; 2233843c0a8aSMike Christie case TMF_NOT_FOUND: 2234843c0a8aSMike Christie if (!sc->SCp.ptr) { 2235843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 22365d12c05eSMike Christie memset(hdr, 0, sizeof(*hdr)); 22379c19a7d0SMike Christie /* task completed before tmf abort response */ 2238bd2199d4SErez Zilber ISCSI_DBG_EH(session, "sc completed while abort in " 2239bd2199d4SErez Zilber "progress\n"); 224077a23c21SMike Christie goto success; 224177a23c21SMike Christie } 224277a23c21SMike Christie /* fall through */ 224377a23c21SMike Christie default: 2244843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 2245843c0a8aSMike Christie goto failed; 224677a23c21SMike Christie } 22477996a778SMike Christie 224877a23c21SMike Christie success: 2249659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 225077a23c21SMike Christie success_unlocked: 2251bd2199d4SErez Zilber ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n", 22521b2c7af8SMike Christie sc, task->itt); 22536724add1SMike Christie mutex_unlock(&session->eh_mutex); 22547996a778SMike Christie return SUCCESS; 22557996a778SMike Christie 22567996a778SMike Christie failed: 2257659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 225877a23c21SMike Christie failed_unlocked: 2259bd2199d4SErez Zilber ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc, 22609c19a7d0SMike Christie task ? task->itt : 0); 22616724add1SMike Christie mutex_unlock(&session->eh_mutex); 22627996a778SMike Christie return FAILED; 22637996a778SMike Christie } 22647996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_abort); 22657996a778SMike Christie 2266843c0a8aSMike Christie static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr) 2267843c0a8aSMike Christie { 2268843c0a8aSMike Christie memset(hdr, 0, sizeof(*hdr)); 2269843c0a8aSMike Christie hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 2270843c0a8aSMike Christie hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK; 2271843c0a8aSMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 227255bdabdfSAndy Grover int_to_scsilun(sc->device->lun, &hdr->lun); 2273f6d5180cSMike Christie hdr->rtt = RESERVED_ITT; 2274843c0a8aSMike Christie } 2275843c0a8aSMike Christie 2276843c0a8aSMike Christie int iscsi_eh_device_reset(struct scsi_cmnd *sc) 2277843c0a8aSMike Christie { 227875613521SMike Christie struct iscsi_cls_session *cls_session; 227975613521SMike Christie struct iscsi_session *session; 2280843c0a8aSMike Christie struct iscsi_conn *conn; 2281843c0a8aSMike Christie struct iscsi_tm *hdr; 2282843c0a8aSMike Christie int rc = FAILED; 2283843c0a8aSMike Christie 228475613521SMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 228575613521SMike Christie session = cls_session->dd_data; 228675613521SMike Christie 22879cb78c16SHannes Reinecke ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc, 22889cb78c16SHannes Reinecke sc->device->lun); 2289843c0a8aSMike Christie 2290843c0a8aSMike Christie mutex_lock(&session->eh_mutex); 2291659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 2292843c0a8aSMike Christie /* 2293843c0a8aSMike Christie * Just check if we are not logged in. We cannot check for 2294843c0a8aSMike Christie * the phase because the reset could come from a ioctl. 2295843c0a8aSMike Christie */ 2296843c0a8aSMike Christie if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) 2297843c0a8aSMike Christie goto unlock; 2298843c0a8aSMike Christie conn = session->leadconn; 2299843c0a8aSMike Christie 2300843c0a8aSMike Christie /* only have one tmf outstanding at a time */ 2301843c0a8aSMike Christie if (conn->tmf_state != TMF_INITIAL) 2302843c0a8aSMike Christie goto unlock; 2303843c0a8aSMike Christie conn->tmf_state = TMF_QUEUED; 2304843c0a8aSMike Christie 2305843c0a8aSMike Christie hdr = &conn->tmhdr; 2306843c0a8aSMike Christie iscsi_prep_lun_reset_pdu(sc, hdr); 2307843c0a8aSMike Christie 23089c19a7d0SMike Christie if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age, 2309f6d5180cSMike Christie session->lu_reset_timeout)) { 2310843c0a8aSMike Christie rc = FAILED; 2311843c0a8aSMike Christie goto unlock; 2312843c0a8aSMike Christie } 2313843c0a8aSMike Christie 2314843c0a8aSMike Christie switch (conn->tmf_state) { 2315843c0a8aSMike Christie case TMF_SUCCESS: 2316843c0a8aSMike Christie break; 2317843c0a8aSMike Christie case TMF_TIMEDOUT: 2318659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2319df4da5cdSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 2320843c0a8aSMike Christie goto done; 2321843c0a8aSMike Christie default: 2322843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 2323843c0a8aSMike Christie goto unlock; 2324843c0a8aSMike Christie } 2325843c0a8aSMike Christie 2326843c0a8aSMike Christie rc = SUCCESS; 2327659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2328843c0a8aSMike Christie 2329843c0a8aSMike Christie iscsi_suspend_tx(conn); 2330913e5bf4SMike Christie 2331659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 23325d12c05eSMike Christie memset(hdr, 0, sizeof(*hdr)); 23333bbaaad9SMike Christie fail_scsi_tasks(conn, sc->device->lun, DID_ERROR); 2334843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 2335659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2336843c0a8aSMike Christie 2337843c0a8aSMike Christie iscsi_start_tx(conn); 2338843c0a8aSMike Christie goto done; 2339843c0a8aSMike Christie 2340843c0a8aSMike Christie unlock: 2341659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2342843c0a8aSMike Christie done: 2343bd2199d4SErez Zilber ISCSI_DBG_EH(session, "dev reset result = %s\n", 2344843c0a8aSMike Christie rc == SUCCESS ? "SUCCESS" : "FAILED"); 2345843c0a8aSMike Christie mutex_unlock(&session->eh_mutex); 2346843c0a8aSMike Christie return rc; 2347843c0a8aSMike Christie } 2348843c0a8aSMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_device_reset); 2349843c0a8aSMike Christie 23503fe5ae8bSMike Christie void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session) 23513fe5ae8bSMike Christie { 23523fe5ae8bSMike Christie struct iscsi_session *session = cls_session->dd_data; 23533fe5ae8bSMike Christie 2354659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 23553fe5ae8bSMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) { 23563fe5ae8bSMike Christie session->state = ISCSI_STATE_RECOVERY_FAILED; 23573fe5ae8bSMike Christie if (session->leadconn) 23583fe5ae8bSMike Christie wake_up(&session->leadconn->ehwait); 23593fe5ae8bSMike Christie } 2360659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 23613fe5ae8bSMike Christie } 23623fe5ae8bSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout); 23633fe5ae8bSMike Christie 23643fe5ae8bSMike Christie /** 23653fe5ae8bSMike Christie * iscsi_eh_session_reset - drop session and attempt relogin 23663fe5ae8bSMike Christie * @sc: scsi command 23673fe5ae8bSMike Christie * 23683fe5ae8bSMike Christie * This function will wait for a relogin, session termination from 23693fe5ae8bSMike Christie * userspace, or a recovery/replacement timeout. 23703fe5ae8bSMike Christie */ 2371309ce156SJayamohan Kallickal int iscsi_eh_session_reset(struct scsi_cmnd *sc) 23723fe5ae8bSMike Christie { 23733fe5ae8bSMike Christie struct iscsi_cls_session *cls_session; 23743fe5ae8bSMike Christie struct iscsi_session *session; 23753fe5ae8bSMike Christie struct iscsi_conn *conn; 23763fe5ae8bSMike Christie 23773fe5ae8bSMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 23783fe5ae8bSMike Christie session = cls_session->dd_data; 23793fe5ae8bSMike Christie conn = session->leadconn; 23803fe5ae8bSMike Christie 23813fe5ae8bSMike Christie mutex_lock(&session->eh_mutex); 2382659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 23833fe5ae8bSMike Christie if (session->state == ISCSI_STATE_TERMINATE) { 23843fe5ae8bSMike Christie failed: 23853fe5ae8bSMike Christie ISCSI_DBG_EH(session, 23863fe5ae8bSMike Christie "failing session reset: Could not log back into " 23875db6dd14SFred Herard "%s [age %d]\n", session->targetname, 23885db6dd14SFred Herard session->age); 2389659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 23903fe5ae8bSMike Christie mutex_unlock(&session->eh_mutex); 23913fe5ae8bSMike Christie return FAILED; 23923fe5ae8bSMike Christie } 23933fe5ae8bSMike Christie 2394659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 23953fe5ae8bSMike Christie mutex_unlock(&session->eh_mutex); 23963fe5ae8bSMike Christie /* 23973fe5ae8bSMike Christie * we drop the lock here but the leadconn cannot be destoyed while 23983fe5ae8bSMike Christie * we are in the scsi eh 23993fe5ae8bSMike Christie */ 2400df4da5cdSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 24013fe5ae8bSMike Christie 24023fe5ae8bSMike Christie ISCSI_DBG_EH(session, "wait for relogin\n"); 24033fe5ae8bSMike Christie wait_event_interruptible(conn->ehwait, 24043fe5ae8bSMike Christie session->state == ISCSI_STATE_TERMINATE || 24053fe5ae8bSMike Christie session->state == ISCSI_STATE_LOGGED_IN || 24063fe5ae8bSMike Christie session->state == ISCSI_STATE_RECOVERY_FAILED); 24073fe5ae8bSMike Christie if (signal_pending(current)) 24083fe5ae8bSMike Christie flush_signals(current); 24093fe5ae8bSMike Christie 24103fe5ae8bSMike Christie mutex_lock(&session->eh_mutex); 2411659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 24123fe5ae8bSMike Christie if (session->state == ISCSI_STATE_LOGGED_IN) { 24133fe5ae8bSMike Christie ISCSI_DBG_EH(session, 24143fe5ae8bSMike Christie "session reset succeeded for %s,%s\n", 24153fe5ae8bSMike Christie session->targetname, conn->persistent_address); 24163fe5ae8bSMike Christie } else 24173fe5ae8bSMike Christie goto failed; 2418659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 24193fe5ae8bSMike Christie mutex_unlock(&session->eh_mutex); 24203fe5ae8bSMike Christie return SUCCESS; 24213fe5ae8bSMike Christie } 2422309ce156SJayamohan Kallickal EXPORT_SYMBOL_GPL(iscsi_eh_session_reset); 24233fe5ae8bSMike Christie 24243fe5ae8bSMike Christie static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr) 24253fe5ae8bSMike Christie { 24263fe5ae8bSMike Christie memset(hdr, 0, sizeof(*hdr)); 24273fe5ae8bSMike Christie hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 24283fe5ae8bSMike Christie hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK; 24293fe5ae8bSMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 24303fe5ae8bSMike Christie hdr->rtt = RESERVED_ITT; 24313fe5ae8bSMike Christie } 24323fe5ae8bSMike Christie 24333fe5ae8bSMike Christie /** 24343fe5ae8bSMike Christie * iscsi_eh_target_reset - reset target 24353fe5ae8bSMike Christie * @sc: scsi command 24363fe5ae8bSMike Christie * 2437309ce156SJayamohan Kallickal * This will attempt to send a warm target reset. 24383fe5ae8bSMike Christie */ 24393907adf6SBart Van Assche static int iscsi_eh_target_reset(struct scsi_cmnd *sc) 24403fe5ae8bSMike Christie { 24413fe5ae8bSMike Christie struct iscsi_cls_session *cls_session; 24423fe5ae8bSMike Christie struct iscsi_session *session; 24433fe5ae8bSMike Christie struct iscsi_conn *conn; 24443fe5ae8bSMike Christie struct iscsi_tm *hdr; 24453fe5ae8bSMike Christie int rc = FAILED; 24463fe5ae8bSMike Christie 24473fe5ae8bSMike Christie cls_session = starget_to_session(scsi_target(sc->device)); 24483fe5ae8bSMike Christie session = cls_session->dd_data; 24493fe5ae8bSMike Christie 24503fe5ae8bSMike Christie ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc, 24513fe5ae8bSMike Christie session->targetname); 24523fe5ae8bSMike Christie 24533fe5ae8bSMike Christie mutex_lock(&session->eh_mutex); 2454659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 24553fe5ae8bSMike Christie /* 24563fe5ae8bSMike Christie * Just check if we are not logged in. We cannot check for 24573fe5ae8bSMike Christie * the phase because the reset could come from a ioctl. 24583fe5ae8bSMike Christie */ 24593fe5ae8bSMike Christie if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) 24603fe5ae8bSMike Christie goto unlock; 24613fe5ae8bSMike Christie conn = session->leadconn; 24623fe5ae8bSMike Christie 24633fe5ae8bSMike Christie /* only have one tmf outstanding at a time */ 24643fe5ae8bSMike Christie if (conn->tmf_state != TMF_INITIAL) 24653fe5ae8bSMike Christie goto unlock; 24663fe5ae8bSMike Christie conn->tmf_state = TMF_QUEUED; 24673fe5ae8bSMike Christie 24683fe5ae8bSMike Christie hdr = &conn->tmhdr; 24693fe5ae8bSMike Christie iscsi_prep_tgt_reset_pdu(sc, hdr); 24703fe5ae8bSMike Christie 24713fe5ae8bSMike Christie if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age, 24723fe5ae8bSMike Christie session->tgt_reset_timeout)) { 24733fe5ae8bSMike Christie rc = FAILED; 24743fe5ae8bSMike Christie goto unlock; 24753fe5ae8bSMike Christie } 24763fe5ae8bSMike Christie 24773fe5ae8bSMike Christie switch (conn->tmf_state) { 24783fe5ae8bSMike Christie case TMF_SUCCESS: 24793fe5ae8bSMike Christie break; 24803fe5ae8bSMike Christie case TMF_TIMEDOUT: 2481659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2482df4da5cdSMike Christie iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 24833fe5ae8bSMike Christie goto done; 24843fe5ae8bSMike Christie default: 24853fe5ae8bSMike Christie conn->tmf_state = TMF_INITIAL; 24863fe5ae8bSMike Christie goto unlock; 24873fe5ae8bSMike Christie } 24883fe5ae8bSMike Christie 24893fe5ae8bSMike Christie rc = SUCCESS; 2490659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 24913fe5ae8bSMike Christie 24923fe5ae8bSMike Christie iscsi_suspend_tx(conn); 24933fe5ae8bSMike Christie 2494659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 24953fe5ae8bSMike Christie memset(hdr, 0, sizeof(*hdr)); 24963fe5ae8bSMike Christie fail_scsi_tasks(conn, -1, DID_ERROR); 24973fe5ae8bSMike Christie conn->tmf_state = TMF_INITIAL; 2498659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 24993fe5ae8bSMike Christie 25003fe5ae8bSMike Christie iscsi_start_tx(conn); 25013fe5ae8bSMike Christie goto done; 25023fe5ae8bSMike Christie 25033fe5ae8bSMike Christie unlock: 2504659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 25053fe5ae8bSMike Christie done: 25063fe5ae8bSMike Christie ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname, 25073fe5ae8bSMike Christie rc == SUCCESS ? "SUCCESS" : "FAILED"); 25083fe5ae8bSMike Christie mutex_unlock(&session->eh_mutex); 2509309ce156SJayamohan Kallickal return rc; 2510309ce156SJayamohan Kallickal } 25113fe5ae8bSMike Christie 2512309ce156SJayamohan Kallickal /** 2513309ce156SJayamohan Kallickal * iscsi_eh_recover_target - reset target and possibly the session 2514309ce156SJayamohan Kallickal * @sc: scsi command 2515309ce156SJayamohan Kallickal * 2516309ce156SJayamohan Kallickal * This will attempt to send a warm target reset. If that fails, 2517309ce156SJayamohan Kallickal * we will escalate to ERL0 session recovery. 2518309ce156SJayamohan Kallickal */ 2519309ce156SJayamohan Kallickal int iscsi_eh_recover_target(struct scsi_cmnd *sc) 2520309ce156SJayamohan Kallickal { 2521309ce156SJayamohan Kallickal int rc; 2522309ce156SJayamohan Kallickal 2523309ce156SJayamohan Kallickal rc = iscsi_eh_target_reset(sc); 25243fe5ae8bSMike Christie if (rc == FAILED) 25253fe5ae8bSMike Christie rc = iscsi_eh_session_reset(sc); 25263fe5ae8bSMike Christie return rc; 25273fe5ae8bSMike Christie } 2528309ce156SJayamohan Kallickal EXPORT_SYMBOL_GPL(iscsi_eh_recover_target); 25293fe5ae8bSMike Christie 25306320377fSOlaf Kirch /* 25316320377fSOlaf Kirch * Pre-allocate a pool of @max items of @item_size. By default, the pool 25326320377fSOlaf Kirch * should be accessed via kfifo_{get,put} on q->queue. 25336320377fSOlaf Kirch * Optionally, the caller can obtain the array of object pointers 25346320377fSOlaf Kirch * by passing in a non-NULL @items pointer 25356320377fSOlaf Kirch */ 25367996a778SMike Christie int 25376320377fSOlaf Kirch iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size) 25387996a778SMike Christie { 25396320377fSOlaf Kirch int i, num_arrays = 1; 25407996a778SMike Christie 25416320377fSOlaf Kirch memset(q, 0, sizeof(*q)); 25427996a778SMike Christie 25437996a778SMike Christie q->max = max; 25446320377fSOlaf Kirch 25456320377fSOlaf Kirch /* If the user passed an items pointer, he wants a copy of 25466320377fSOlaf Kirch * the array. */ 25476320377fSOlaf Kirch if (items) 25486320377fSOlaf Kirch num_arrays++; 2549778e1cddSKees Cook q->pool = kvcalloc(num_arrays * max, sizeof(void *), GFP_KERNEL); 25506320377fSOlaf Kirch if (q->pool == NULL) 2551f474a37bSJean Delvare return -ENOMEM; 25527996a778SMike Christie 2553c1e13f25SStefani Seibold kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*)); 25547996a778SMike Christie 25557996a778SMike Christie for (i = 0; i < max; i++) { 25566320377fSOlaf Kirch q->pool[i] = kzalloc(item_size, GFP_KERNEL); 25577996a778SMike Christie if (q->pool[i] == NULL) { 25586320377fSOlaf Kirch q->max = i; 25596320377fSOlaf Kirch goto enomem; 25607996a778SMike Christie } 25617acd72ebSStefani Seibold kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*)); 25627996a778SMike Christie } 25636320377fSOlaf Kirch 25646320377fSOlaf Kirch if (items) { 25656320377fSOlaf Kirch *items = q->pool + max; 25666320377fSOlaf Kirch memcpy(*items, q->pool, max * sizeof(void *)); 25676320377fSOlaf Kirch } 25686320377fSOlaf Kirch 25697996a778SMike Christie return 0; 25706320377fSOlaf Kirch 25716320377fSOlaf Kirch enomem: 25726320377fSOlaf Kirch iscsi_pool_free(q); 25736320377fSOlaf Kirch return -ENOMEM; 25747996a778SMike Christie } 25757996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_init); 25767996a778SMike Christie 25776320377fSOlaf Kirch void iscsi_pool_free(struct iscsi_pool *q) 25787996a778SMike Christie { 25797996a778SMike Christie int i; 25807996a778SMike Christie 25817996a778SMike Christie for (i = 0; i < q->max; i++) 25826320377fSOlaf Kirch kfree(q->pool[i]); 2583bfcc62edSKyle Fortin kvfree(q->pool); 25847996a778SMike Christie } 25857996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_free); 25867996a778SMike Christie 2587a4804cd6SMike Christie /** 2588a4804cd6SMike Christie * iscsi_host_add - add host to system 2589a4804cd6SMike Christie * @shost: scsi host 2590a4804cd6SMike Christie * @pdev: parent device 2591a4804cd6SMike Christie * 2592a4804cd6SMike Christie * This should be called by partial offload and software iscsi drivers 2593a4804cd6SMike Christie * to add a host to the system. 2594a4804cd6SMike Christie */ 2595a4804cd6SMike Christie int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev) 25967996a778SMike Christie { 25978e9a20ceSMike Christie if (!shost->can_queue) 25988e9a20ceSMike Christie shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX; 25998e9a20ceSMike Christie 26004d108350SMike Christie if (!shost->cmd_per_lun) 26014d108350SMike Christie shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN; 26024d108350SMike Christie 2603a4804cd6SMike Christie return scsi_add_host(shost, pdev); 2604a4804cd6SMike Christie } 2605a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_add); 2606a4804cd6SMike Christie 2607a4804cd6SMike Christie /** 2608a4804cd6SMike Christie * iscsi_host_alloc - allocate a host and driver data 2609a4804cd6SMike Christie * @sht: scsi host template 2610a4804cd6SMike Christie * @dd_data_size: driver host data size 261132ae763eSMike Christie * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue 2612a4804cd6SMike Christie * 2613a4804cd6SMike Christie * This should be called by partial offload and software iscsi drivers. 2614a4804cd6SMike Christie * To access the driver specific memory use the iscsi_host_priv() macro. 2615a4804cd6SMike Christie */ 2616a4804cd6SMike Christie struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht, 26174d108350SMike Christie int dd_data_size, bool xmit_can_sleep) 2618a4804cd6SMike Christie { 2619a4804cd6SMike Christie struct Scsi_Host *shost; 2620e5bd7b54SMike Christie struct iscsi_host *ihost; 2621a4804cd6SMike Christie 2622a4804cd6SMike Christie shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size); 2623a4804cd6SMike Christie if (!shost) 2624a4804cd6SMike Christie return NULL; 2625e5bd7b54SMike Christie ihost = shost_priv(shost); 262632ae763eSMike Christie 262732ae763eSMike Christie if (xmit_can_sleep) { 262832ae763eSMike Christie snprintf(ihost->workq_name, sizeof(ihost->workq_name), 262932ae763eSMike Christie "iscsi_q_%d", shost->host_no); 263032ae763eSMike Christie ihost->workq = create_singlethread_workqueue(ihost->workq_name); 263132ae763eSMike Christie if (!ihost->workq) 263232ae763eSMike Christie goto free_host; 263332ae763eSMike Christie } 263432ae763eSMike Christie 2635e5bd7b54SMike Christie spin_lock_init(&ihost->lock); 2636e5bd7b54SMike Christie ihost->state = ISCSI_HOST_SETUP; 2637e5bd7b54SMike Christie ihost->num_sessions = 0; 2638e5bd7b54SMike Christie init_waitqueue_head(&ihost->session_removal_wq); 2639a4804cd6SMike Christie return shost; 264032ae763eSMike Christie 264132ae763eSMike Christie free_host: 264232ae763eSMike Christie scsi_host_put(shost); 264332ae763eSMike Christie return NULL; 264475613521SMike Christie } 2645a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_alloc); 264675613521SMike Christie 2647e5bd7b54SMike Christie static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session) 2648e5bd7b54SMike Christie { 264940a06e75SMike Christie iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST); 2650e5bd7b54SMike Christie } 2651e5bd7b54SMike Christie 2652a4804cd6SMike Christie /** 2653a4804cd6SMike Christie * iscsi_host_remove - remove host and sessions 2654a4804cd6SMike Christie * @shost: scsi host 2655a4804cd6SMike Christie * 2656e5bd7b54SMike Christie * If there are any sessions left, this will initiate the removal and wait 2657e5bd7b54SMike Christie * for the completion. 2658a4804cd6SMike Christie */ 2659a4804cd6SMike Christie void iscsi_host_remove(struct Scsi_Host *shost) 2660a4804cd6SMike Christie { 2661e5bd7b54SMike Christie struct iscsi_host *ihost = shost_priv(shost); 2662e5bd7b54SMike Christie unsigned long flags; 2663e5bd7b54SMike Christie 2664e5bd7b54SMike Christie spin_lock_irqsave(&ihost->lock, flags); 2665e5bd7b54SMike Christie ihost->state = ISCSI_HOST_REMOVED; 2666e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 2667e5bd7b54SMike Christie 2668e5bd7b54SMike Christie iscsi_host_for_each_session(shost, iscsi_notify_host_removed); 2669e5bd7b54SMike Christie wait_event_interruptible(ihost->session_removal_wq, 2670e5bd7b54SMike Christie ihost->num_sessions == 0); 2671e5bd7b54SMike Christie if (signal_pending(current)) 2672e5bd7b54SMike Christie flush_signals(current); 2673e5bd7b54SMike Christie 2674a4804cd6SMike Christie scsi_remove_host(shost); 267532ae763eSMike Christie if (ihost->workq) 267632ae763eSMike Christie destroy_workqueue(ihost->workq); 2677a4804cd6SMike Christie } 2678a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_remove); 2679a4804cd6SMike Christie 2680a4804cd6SMike Christie void iscsi_host_free(struct Scsi_Host *shost) 268175613521SMike Christie { 268275613521SMike Christie struct iscsi_host *ihost = shost_priv(shost); 268375613521SMike Christie 268475613521SMike Christie kfree(ihost->netdev); 268575613521SMike Christie kfree(ihost->hwaddress); 268675613521SMike Christie kfree(ihost->initiatorname); 2687a4804cd6SMike Christie scsi_host_put(shost); 268875613521SMike Christie } 2689a4804cd6SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_free); 269075613521SMike Christie 2691e5bd7b54SMike Christie static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost) 2692e5bd7b54SMike Christie { 2693e5bd7b54SMike Christie struct iscsi_host *ihost = shost_priv(shost); 2694e5bd7b54SMike Christie unsigned long flags; 2695e5bd7b54SMike Christie 2696e5bd7b54SMike Christie shost = scsi_host_get(shost); 2697e5bd7b54SMike Christie if (!shost) { 2698e5bd7b54SMike Christie printk(KERN_ERR "Invalid state. Cannot notify host removal " 2699e5bd7b54SMike Christie "of session teardown event because host already " 2700e5bd7b54SMike Christie "removed.\n"); 2701e5bd7b54SMike Christie return; 2702e5bd7b54SMike Christie } 2703e5bd7b54SMike Christie 2704e5bd7b54SMike Christie spin_lock_irqsave(&ihost->lock, flags); 2705e5bd7b54SMike Christie ihost->num_sessions--; 2706e5bd7b54SMike Christie if (ihost->num_sessions == 0) 2707e5bd7b54SMike Christie wake_up(&ihost->session_removal_wq); 2708e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 2709e5bd7b54SMike Christie scsi_host_put(shost); 2710e5bd7b54SMike Christie } 2711e5bd7b54SMike Christie 271275613521SMike Christie /** 271375613521SMike Christie * iscsi_session_setup - create iscsi cls session and host and session 271475613521SMike Christie * @iscsit: iscsi transport template 271575613521SMike Christie * @shost: scsi host 271675613521SMike Christie * @cmds_max: session can queue 2717ccd4a430SRandy Dunlap * @dd_size: private driver data size, added to session allocation size 27189c19a7d0SMike Christie * @cmd_task_size: LLD task private data size 271975613521SMike Christie * @initial_cmdsn: initial CmdSN 2720ccd4a430SRandy Dunlap * @id: target ID to add to this session 272175613521SMike Christie * 272275613521SMike Christie * This can be used by software iscsi_transports that allocate 272375613521SMike Christie * a session per scsi host. 27243cf7b233SMike Christie * 27253cf7b233SMike Christie * Callers should set cmds_max to the largest total numer (mgmt + scsi) of 27263cf7b233SMike Christie * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks 27273cf7b233SMike Christie * for nop handling and login/logout requests. 272875613521SMike Christie */ 272975613521SMike Christie struct iscsi_cls_session * 273075613521SMike Christie iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost, 2731b8b9e1b8SJayamohan Kallickal uint16_t cmds_max, int dd_size, int cmd_task_size, 27327970634bSMike Christie uint32_t initial_cmdsn, unsigned int id) 273375613521SMike Christie { 2734e5bd7b54SMike Christie struct iscsi_host *ihost = shost_priv(shost); 273575613521SMike Christie struct iscsi_session *session; 273675613521SMike Christie struct iscsi_cls_session *cls_session; 27373cf7b233SMike Christie int cmd_i, scsi_cmds, total_cmds = cmds_max; 2738e5bd7b54SMike Christie unsigned long flags; 2739e5bd7b54SMike Christie 2740e5bd7b54SMike Christie spin_lock_irqsave(&ihost->lock, flags); 2741e5bd7b54SMike Christie if (ihost->state == ISCSI_HOST_REMOVED) { 2742e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 2743e5bd7b54SMike Christie return NULL; 2744e5bd7b54SMike Christie } 2745e5bd7b54SMike Christie ihost->num_sessions++; 2746e5bd7b54SMike Christie spin_unlock_irqrestore(&ihost->lock, flags); 27478e9a20ceSMike Christie 27488e9a20ceSMike Christie if (!total_cmds) 27498e9a20ceSMike Christie total_cmds = ISCSI_DEF_XMIT_CMDS_MAX; 27503e5c28adSMike Christie /* 27513cf7b233SMike Christie * The iscsi layer needs some tasks for nop handling and tmfs, 27523cf7b233SMike Christie * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX 27533cf7b233SMike Christie * + 1 command for scsi IO. 27543e5c28adSMike Christie */ 27553cf7b233SMike Christie if (total_cmds < ISCSI_TOTAL_CMDS_MIN) { 27563cf7b233SMike Christie printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " 27573cf7b233SMike Christie "must be a power of two that is at least %d.\n", 27583cf7b233SMike Christie total_cmds, ISCSI_TOTAL_CMDS_MIN); 2759e5bd7b54SMike Christie goto dec_session_count; 27601548271eSMike Christie } 27613cf7b233SMike Christie 27623cf7b233SMike Christie if (total_cmds > ISCSI_TOTAL_CMDS_MAX) { 27633cf7b233SMike Christie printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " 27643cf7b233SMike Christie "must be a power of 2 less than or equal to %d.\n", 27653cf7b233SMike Christie cmds_max, ISCSI_TOTAL_CMDS_MAX); 27663cf7b233SMike Christie total_cmds = ISCSI_TOTAL_CMDS_MAX; 27673cf7b233SMike Christie } 27683cf7b233SMike Christie 27693cf7b233SMike Christie if (!is_power_of_2(total_cmds)) { 27703cf7b233SMike Christie printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " 27713cf7b233SMike Christie "must be a power of 2.\n", total_cmds); 27723cf7b233SMike Christie total_cmds = rounddown_pow_of_two(total_cmds); 27733cf7b233SMike Christie if (total_cmds < ISCSI_TOTAL_CMDS_MIN) 27743cf7b233SMike Christie return NULL; 27753cf7b233SMike Christie printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n", 27763cf7b233SMike Christie total_cmds); 27773cf7b233SMike Christie } 27783cf7b233SMike Christie scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX; 27791548271eSMike Christie 27805d91e209SMike Christie cls_session = iscsi_alloc_session(shost, iscsit, 2781b8b9e1b8SJayamohan Kallickal sizeof(struct iscsi_session) + 2782b8b9e1b8SJayamohan Kallickal dd_size); 278375613521SMike Christie if (!cls_session) 2784e5bd7b54SMike Christie goto dec_session_count; 278575613521SMike Christie session = cls_session->dd_data; 278675613521SMike Christie session->cls_session = cls_session; 27877996a778SMike Christie session->host = shost; 27887996a778SMike Christie session->state = ISCSI_STATE_FREE; 2789f6d5180cSMike Christie session->fast_abort = 1; 27903fe5ae8bSMike Christie session->tgt_reset_timeout = 30; 27914cd49ea1SMike Christie session->lu_reset_timeout = 15; 27924cd49ea1SMike Christie session->abort_timeout = 10; 27933cf7b233SMike Christie session->scsi_cmds_max = scsi_cmds; 27943cf7b233SMike Christie session->cmds_max = total_cmds; 2795e0726407SMike Christie session->queued_cmdsn = session->cmdsn = initial_cmdsn; 27967996a778SMike Christie session->exp_cmdsn = initial_cmdsn + 1; 27977996a778SMike Christie session->max_cmdsn = initial_cmdsn + 1; 27987996a778SMike Christie session->max_r2t = 1; 27997996a778SMike Christie session->tt = iscsit; 2800b8b9e1b8SJayamohan Kallickal session->dd_data = cls_session->dd_data + sizeof(*session); 2801659743b0SShlomo Pongratz 28026724add1SMike Christie mutex_init(&session->eh_mutex); 2803659743b0SShlomo Pongratz spin_lock_init(&session->frwd_lock); 2804659743b0SShlomo Pongratz spin_lock_init(&session->back_lock); 28057996a778SMike Christie 28067996a778SMike Christie /* initialize SCSI PDU commands pool */ 28077996a778SMike Christie if (iscsi_pool_init(&session->cmdpool, session->cmds_max, 28087996a778SMike Christie (void***)&session->cmds, 28099c19a7d0SMike Christie cmd_task_size + sizeof(struct iscsi_task))) 28107996a778SMike Christie goto cmdpool_alloc_fail; 28117996a778SMike Christie 28127996a778SMike Christie /* pre-format cmds pool with ITT */ 28137996a778SMike Christie for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { 28149c19a7d0SMike Christie struct iscsi_task *task = session->cmds[cmd_i]; 28157996a778SMike Christie 28169c19a7d0SMike Christie if (cmd_task_size) 28179c19a7d0SMike Christie task->dd_data = &task[1]; 28189c19a7d0SMike Christie task->itt = cmd_i; 28193bbaaad9SMike Christie task->state = ISCSI_TASK_FREE; 28209c19a7d0SMike Christie INIT_LIST_HEAD(&task->running); 28217996a778SMike Christie } 28227996a778SMike Christie 2823f53a88daSMike Christie if (!try_module_get(iscsit->owner)) 282475613521SMike Christie goto module_get_fail; 282575613521SMike Christie 28267970634bSMike Christie if (iscsi_add_session(cls_session, id)) 2827f53a88daSMike Christie goto cls_session_fail; 2828e5bd7b54SMike Christie 28297996a778SMike Christie return cls_session; 28307996a778SMike Christie 28317996a778SMike Christie cls_session_fail: 283275613521SMike Christie module_put(iscsit->owner); 283375613521SMike Christie module_get_fail: 28346320377fSOlaf Kirch iscsi_pool_free(&session->cmdpool); 28357996a778SMike Christie cmdpool_alloc_fail: 283675613521SMike Christie iscsi_free_session(cls_session); 2837e5bd7b54SMike Christie dec_session_count: 2838e5bd7b54SMike Christie iscsi_host_dec_session_cnt(shost); 28397996a778SMike Christie return NULL; 28407996a778SMike Christie } 28417996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_setup); 28427996a778SMike Christie 28437996a778SMike Christie /** 28447996a778SMike Christie * iscsi_session_teardown - destroy session, host, and cls_session 284575613521SMike Christie * @cls_session: iscsi session 284675613521SMike Christie */ 28477996a778SMike Christie void iscsi_session_teardown(struct iscsi_cls_session *cls_session) 28487996a778SMike Christie { 284975613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 285063f75cc8SMike Christie struct module *owner = cls_session->transport->owner; 2851e5bd7b54SMike Christie struct Scsi_Host *shost = session->host; 28527996a778SMike Christie 28536320377fSOlaf Kirch iscsi_pool_free(&session->cmdpool); 28547996a778SMike Christie 28559e10b512SKhazhismel Kumykov iscsi_remove_session(cls_session); 28569e10b512SKhazhismel Kumykov 2857b2c64167SMike Christie kfree(session->password); 2858b2c64167SMike Christie kfree(session->password_in); 2859b2c64167SMike Christie kfree(session->username); 2860b2c64167SMike Christie kfree(session->username_in); 2861f3ff0c36SMike Christie kfree(session->targetname); 28623c5c4801SVikas Chaudhary kfree(session->targetalias); 286388dfd340SMike Christie kfree(session->initiatorname); 28643b9373e9SEddie Wai kfree(session->boot_root); 28653b9373e9SEddie Wai kfree(session->boot_nic); 28663b9373e9SEddie Wai kfree(session->boot_target); 286788dfd340SMike Christie kfree(session->ifacename); 2868f8525eb4SAdheer Chandravanshi kfree(session->portal_type); 2869f8525eb4SAdheer Chandravanshi kfree(session->discovery_parent_type); 2870f3ff0c36SMike Christie 28719e10b512SKhazhismel Kumykov iscsi_free_session(cls_session); 28729e10b512SKhazhismel Kumykov 2873e5bd7b54SMike Christie iscsi_host_dec_session_cnt(shost); 287463f75cc8SMike Christie module_put(owner); 28757996a778SMike Christie } 28767996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_teardown); 28777996a778SMike Christie 28787996a778SMike Christie /** 28797996a778SMike Christie * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn 28807996a778SMike Christie * @cls_session: iscsi_cls_session 28815d91e209SMike Christie * @dd_size: private driver data size 28827996a778SMike Christie * @conn_idx: cid 28835d91e209SMike Christie */ 28847996a778SMike Christie struct iscsi_cls_conn * 28855d91e209SMike Christie iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size, 28865d91e209SMike Christie uint32_t conn_idx) 28877996a778SMike Christie { 288875613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 28897996a778SMike Christie struct iscsi_conn *conn; 28907996a778SMike Christie struct iscsi_cls_conn *cls_conn; 2891d36ab6f3SMike Christie char *data; 28927996a778SMike Christie 28935d91e209SMike Christie cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size, 28945d91e209SMike Christie conn_idx); 28957996a778SMike Christie if (!cls_conn) 28967996a778SMike Christie return NULL; 28977996a778SMike Christie conn = cls_conn->dd_data; 28985d91e209SMike Christie memset(conn, 0, sizeof(*conn) + dd_size); 28997996a778SMike Christie 29005d91e209SMike Christie conn->dd_data = cls_conn->dd_data + sizeof(*conn); 29017996a778SMike Christie conn->session = session; 29027996a778SMike Christie conn->cls_conn = cls_conn; 29037996a778SMike Christie conn->c_stage = ISCSI_CONN_INITIAL_STAGE; 29047996a778SMike Christie conn->id = conn_idx; 29057996a778SMike Christie conn->exp_statsn = 0; 2906843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 2907f6d5180cSMike Christie 29082c4b9637SKees Cook timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0); 2909f6d5180cSMike Christie 2910843c0a8aSMike Christie INIT_LIST_HEAD(&conn->mgmtqueue); 29113bbaaad9SMike Christie INIT_LIST_HEAD(&conn->cmdqueue); 2912843c0a8aSMike Christie INIT_LIST_HEAD(&conn->requeue); 29136f8830f5SChris Leech spin_lock_init(&conn->taskqueuelock); 2914c4028958SDavid Howells INIT_WORK(&conn->xmitwork, iscsi_xmitworker); 29157996a778SMike Christie 29169c19a7d0SMike Christie /* allocate login_task used for the login/text sequences */ 2917659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 29187acd72ebSStefani Seibold if (!kfifo_out(&session->cmdpool.queue, 29199c19a7d0SMike Christie (void*)&conn->login_task, 29207996a778SMike Christie sizeof(void*))) { 2921659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 29229c19a7d0SMike Christie goto login_task_alloc_fail; 29237996a778SMike Christie } 2924659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 29257996a778SMike Christie 2926cfeb2cf9SMike Christie data = (char *) __get_free_pages(GFP_KERNEL, 2927cfeb2cf9SMike Christie get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); 2928d36ab6f3SMike Christie if (!data) 29299c19a7d0SMike Christie goto login_task_data_alloc_fail; 29309c19a7d0SMike Christie conn->login_task->data = conn->data = data; 2931d36ab6f3SMike Christie 29322c4b9637SKees Cook timer_setup(&conn->tmf_timer, iscsi_tmf_timedout, 0); 29337996a778SMike Christie init_waitqueue_head(&conn->ehwait); 29347996a778SMike Christie 29357996a778SMike Christie return cls_conn; 29367996a778SMike Christie 29379c19a7d0SMike Christie login_task_data_alloc_fail: 29387acd72ebSStefani Seibold kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task, 2939d36ab6f3SMike Christie sizeof(void*)); 29409c19a7d0SMike Christie login_task_alloc_fail: 29417996a778SMike Christie iscsi_destroy_conn(cls_conn); 29427996a778SMike Christie return NULL; 29437996a778SMike Christie } 29447996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_setup); 29457996a778SMike Christie 29467996a778SMike Christie /** 29477996a778SMike Christie * iscsi_conn_teardown - teardown iscsi connection 2948ccd4a430SRandy Dunlap * @cls_conn: iscsi class connection 29497996a778SMike Christie * 29507996a778SMike Christie * TODO: we may need to make this into a two step process 29517996a778SMike Christie * like scsi-mls remove + put host 29527996a778SMike Christie */ 29537996a778SMike Christie void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn) 29547996a778SMike Christie { 29557996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 29567996a778SMike Christie struct iscsi_session *session = conn->session; 29577996a778SMike Christie 2958f6d5180cSMike Christie del_timer_sync(&conn->transport_timer); 2959f6d5180cSMike Christie 2960660d0831SJohn Soni Jose mutex_lock(&session->eh_mutex); 2961659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 29627996a778SMike Christie conn->c_stage = ISCSI_CONN_CLEANUP_WAIT; 29637996a778SMike Christie if (session->leadconn == conn) { 29647996a778SMike Christie /* 29657996a778SMike Christie * leading connection? then give up on recovery. 29667996a778SMike Christie */ 29677996a778SMike Christie session->state = ISCSI_STATE_TERMINATE; 29687996a778SMike Christie wake_up(&conn->ehwait); 29697996a778SMike Christie } 2970659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 29717996a778SMike Christie 2972779ea120SMike Christie /* flush queued up work because we free the connection below */ 2973843c0a8aSMike Christie iscsi_suspend_tx(conn); 2974779ea120SMike Christie 2975659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 2976cfeb2cf9SMike Christie free_pages((unsigned long) conn->data, 2977cfeb2cf9SMike Christie get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); 2978f3ff0c36SMike Christie kfree(conn->persistent_address); 2979ae56ff40SAdheer Chandravanshi kfree(conn->local_ipaddr); 2980659743b0SShlomo Pongratz /* regular RX path uses back_lock */ 2981659743b0SShlomo Pongratz spin_lock_bh(&session->back_lock); 29827acd72ebSStefani Seibold kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task, 29837996a778SMike Christie sizeof(void*)); 2984659743b0SShlomo Pongratz spin_unlock_bh(&session->back_lock); 2985e0726407SMike Christie if (session->leadconn == conn) 29867996a778SMike Christie session->leadconn = NULL; 2987659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 2988660d0831SJohn Soni Jose mutex_unlock(&session->eh_mutex); 29897996a778SMike Christie 29907996a778SMike Christie iscsi_destroy_conn(cls_conn); 29917996a778SMike Christie } 29927996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_teardown); 29937996a778SMike Christie 29947996a778SMike Christie int iscsi_conn_start(struct iscsi_cls_conn *cls_conn) 29957996a778SMike Christie { 29967996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 29977996a778SMike Christie struct iscsi_session *session = conn->session; 29987996a778SMike Christie 2999ffd0436eSMike Christie if (!session) { 3000322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 3001322d739dSMike Christie "can't start unbound connection\n"); 30027996a778SMike Christie return -EPERM; 30037996a778SMike Christie } 30047996a778SMike Christie 3005db98ccdeSMike Christie if ((session->imm_data_en || !session->initial_r2t_en) && 3006db98ccdeSMike Christie session->first_burst > session->max_burst) { 3007322d739dSMike Christie iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: " 3008ffd0436eSMike Christie "first_burst %d max_burst %d\n", 3009ffd0436eSMike Christie session->first_burst, session->max_burst); 3010ffd0436eSMike Christie return -EINVAL; 3011ffd0436eSMike Christie } 3012ffd0436eSMike Christie 3013f6d5180cSMike Christie if (conn->ping_timeout && !conn->recv_timeout) { 3014322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of " 3015322d739dSMike Christie "zero. Using 5 seconds\n."); 3016f6d5180cSMike Christie conn->recv_timeout = 5; 3017f6d5180cSMike Christie } 3018f6d5180cSMike Christie 3019f6d5180cSMike Christie if (conn->recv_timeout && !conn->ping_timeout) { 3020322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of " 3021322d739dSMike Christie "zero. Using 5 seconds.\n"); 3022f6d5180cSMike Christie conn->ping_timeout = 5; 3023f6d5180cSMike Christie } 3024f6d5180cSMike Christie 3025659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 30267996a778SMike Christie conn->c_stage = ISCSI_CONN_STARTED; 30277996a778SMike Christie session->state = ISCSI_STATE_LOGGED_IN; 3028e0726407SMike Christie session->queued_cmdsn = session->cmdsn; 30297996a778SMike Christie 3030f6d5180cSMike Christie conn->last_recv = jiffies; 3031f6d5180cSMike Christie conn->last_ping = jiffies; 3032f6d5180cSMike Christie if (conn->recv_timeout && conn->ping_timeout) 3033f6d5180cSMike Christie mod_timer(&conn->transport_timer, 3034f6d5180cSMike Christie jiffies + (conn->recv_timeout * HZ)); 3035f6d5180cSMike Christie 30367996a778SMike Christie switch(conn->stop_stage) { 30377996a778SMike Christie case STOP_CONN_RECOVER: 30387996a778SMike Christie /* 30397996a778SMike Christie * unblock eh_abort() if it is blocked. re-try all 30407996a778SMike Christie * commands after successful recovery 30417996a778SMike Christie */ 30427996a778SMike Christie conn->stop_stage = 0; 3043843c0a8aSMike Christie conn->tmf_state = TMF_INITIAL; 30447996a778SMike Christie session->age++; 30458b1d0343SMike Christie if (session->age == 16) 30468b1d0343SMike Christie session->age = 0; 30476eabafbeSMike Christie break; 30487996a778SMike Christie case STOP_CONN_TERM: 30497996a778SMike Christie conn->stop_stage = 0; 30507996a778SMike Christie break; 30517996a778SMike Christie default: 30527996a778SMike Christie break; 30537996a778SMike Christie } 3054659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 30557996a778SMike Christie 305675613521SMike Christie iscsi_unblock_session(session->cls_session); 30576eabafbeSMike Christie wake_up(&conn->ehwait); 30587996a778SMike Christie return 0; 30597996a778SMike Christie } 30607996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_start); 30617996a778SMike Christie 30627996a778SMike Christie static void 30633bbaaad9SMike Christie fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn) 30647996a778SMike Christie { 30653bbaaad9SMike Christie struct iscsi_task *task; 3066b3cd5050SMike Christie int i, state; 30677996a778SMike Christie 30683bbaaad9SMike Christie for (i = 0; i < conn->session->cmds_max; i++) { 30693bbaaad9SMike Christie task = conn->session->cmds[i]; 30703bbaaad9SMike Christie if (task->sc) 30713bbaaad9SMike Christie continue; 30723bbaaad9SMike Christie 30733bbaaad9SMike Christie if (task->state == ISCSI_TASK_FREE) 30743bbaaad9SMike Christie continue; 30753bbaaad9SMike Christie 30763bbaaad9SMike Christie ISCSI_DBG_SESSION(conn->session, 30773bbaaad9SMike Christie "failing mgmt itt 0x%x state %d\n", 30783bbaaad9SMike Christie task->itt, task->state); 3079b3cd5050SMike Christie state = ISCSI_TASK_ABRT_SESS_RECOV; 3080b3cd5050SMike Christie if (task->state == ISCSI_TASK_PENDING) 3081b3cd5050SMike Christie state = ISCSI_TASK_COMPLETED; 3082a656183eSLee Duncan spin_lock_bh(&session->back_lock); 3083b3cd5050SMike Christie iscsi_complete_task(task, state); 3084a656183eSLee Duncan spin_unlock_bh(&session->back_lock); 30857996a778SMike Christie } 30867996a778SMike Christie } 30877996a778SMike Christie 3088656cffc9SMike Christie static void iscsi_start_session_recovery(struct iscsi_session *session, 30897996a778SMike Christie struct iscsi_conn *conn, int flag) 30907996a778SMike Christie { 3091ed2abc7fSMike Christie int old_stop_stage; 3092ed2abc7fSMike Christie 30936724add1SMike Christie mutex_lock(&session->eh_mutex); 3094659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 3095ed2abc7fSMike Christie if (conn->stop_stage == STOP_CONN_TERM) { 3096659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 30976724add1SMike Christie mutex_unlock(&session->eh_mutex); 30986724add1SMike Christie return; 30996724add1SMike Christie } 31006724add1SMike Christie 31016724add1SMike Christie /* 3102ed2abc7fSMike Christie * When this is called for the in_login state, we only want to clean 31039c19a7d0SMike Christie * up the login task and connection. We do not need to block and set 310467a61114SMike Christie * the recovery state again 3105ed2abc7fSMike Christie */ 310667a61114SMike Christie if (flag == STOP_CONN_TERM) 310767a61114SMike Christie session->state = ISCSI_STATE_TERMINATE; 310867a61114SMike Christie else if (conn->stop_stage != STOP_CONN_RECOVER) 310967a61114SMike Christie session->state = ISCSI_STATE_IN_RECOVERY; 31104ae0a6c1SMike Christie 31114ae0a6c1SMike Christie old_stop_stage = conn->stop_stage; 31124ae0a6c1SMike Christie conn->stop_stage = flag; 3113659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 3114ed2abc7fSMike Christie 311526013ad4SMike Christie del_timer_sync(&conn->transport_timer); 311626013ad4SMike Christie iscsi_suspend_tx(conn); 311726013ad4SMike Christie 3118659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 311967a61114SMike Christie conn->c_stage = ISCSI_CONN_STOPPED; 3120659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 31216724add1SMike Christie 31227996a778SMike Christie /* 31237996a778SMike Christie * for connection level recovery we should not calculate 31247996a778SMike Christie * header digest. conn->hdr_size used for optimization 31257996a778SMike Christie * in hdr_extract() and will be re-negotiated at 31267996a778SMike Christie * set_param() time. 31277996a778SMike Christie */ 31287996a778SMike Christie if (flag == STOP_CONN_RECOVER) { 31297996a778SMike Christie conn->hdrdgst_en = 0; 31307996a778SMike Christie conn->datadgst_en = 0; 3131656cffc9SMike Christie if (session->state == ISCSI_STATE_IN_RECOVERY && 313267a61114SMike Christie old_stop_stage != STOP_CONN_RECOVER) { 31331b2c7af8SMike Christie ISCSI_DBG_SESSION(session, "blocking session\n"); 313475613521SMike Christie iscsi_block_session(session->cls_session); 31357996a778SMike Christie } 313667a61114SMike Christie } 3137656cffc9SMike Christie 3138656cffc9SMike Christie /* 3139656cffc9SMike Christie * flush queues. 3140656cffc9SMike Christie */ 3141659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 31423bbaaad9SMike Christie fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED); 31433bbaaad9SMike Christie fail_mgmt_tasks(session, conn); 31445d12c05eSMike Christie memset(&conn->tmhdr, 0, sizeof(conn->tmhdr)); 3145659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 31466724add1SMike Christie mutex_unlock(&session->eh_mutex); 31477996a778SMike Christie } 31487996a778SMike Christie 31497996a778SMike Christie void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) 31507996a778SMike Christie { 31517996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 31527996a778SMike Christie struct iscsi_session *session = conn->session; 31537996a778SMike Christie 31547996a778SMike Christie switch (flag) { 31557996a778SMike Christie case STOP_CONN_RECOVER: 31567996a778SMike Christie case STOP_CONN_TERM: 31577996a778SMike Christie iscsi_start_session_recovery(session, conn, flag); 31588d2860b3SMike Christie break; 31597996a778SMike Christie default: 3160322d739dSMike Christie iscsi_conn_printk(KERN_ERR, conn, 3161322d739dSMike Christie "invalid stop flag %d\n", flag); 31627996a778SMike Christie } 31637996a778SMike Christie } 31647996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_stop); 31657996a778SMike Christie 31667996a778SMike Christie int iscsi_conn_bind(struct iscsi_cls_session *cls_session, 31677996a778SMike Christie struct iscsi_cls_conn *cls_conn, int is_leading) 31687996a778SMike Christie { 316975613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 317098644047SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 31717996a778SMike Christie 3172659743b0SShlomo Pongratz spin_lock_bh(&session->frwd_lock); 31737996a778SMike Christie if (is_leading) 31747996a778SMike Christie session->leadconn = conn; 3175659743b0SShlomo Pongratz spin_unlock_bh(&session->frwd_lock); 31767996a778SMike Christie 31777996a778SMike Christie /* 31787996a778SMike Christie * Unblock xmitworker(), Login Phase will pass through. 31797996a778SMike Christie */ 31807996a778SMike Christie clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); 31817996a778SMike Christie clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 31827996a778SMike Christie return 0; 31837996a778SMike Christie } 31847996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_bind); 31857996a778SMike Christie 3186adaf6990SAdheer Chandravanshi int iscsi_switch_str_param(char **param, char *new_val_buf) 31875700b1afSMike Christie { 31885700b1afSMike Christie char *new_val; 31895700b1afSMike Christie 31905700b1afSMike Christie if (*param) { 31915700b1afSMike Christie if (!strcmp(*param, new_val_buf)) 31925700b1afSMike Christie return 0; 31935700b1afSMike Christie } 31945700b1afSMike Christie 31955700b1afSMike Christie new_val = kstrdup(new_val_buf, GFP_NOIO); 31965700b1afSMike Christie if (!new_val) 31975700b1afSMike Christie return -ENOMEM; 31985700b1afSMike Christie 31995700b1afSMike Christie kfree(*param); 32005700b1afSMike Christie *param = new_val; 32015700b1afSMike Christie return 0; 32025700b1afSMike Christie } 3203adaf6990SAdheer Chandravanshi EXPORT_SYMBOL_GPL(iscsi_switch_str_param); 3204a54a52caSMike Christie 3205a54a52caSMike Christie int iscsi_set_param(struct iscsi_cls_conn *cls_conn, 3206a54a52caSMike Christie enum iscsi_param param, char *buf, int buflen) 3207a54a52caSMike Christie { 3208a54a52caSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 3209a54a52caSMike Christie struct iscsi_session *session = conn->session; 32106a06a4b8SOr Gerlitz int val; 3211a54a52caSMike Christie 3212a54a52caSMike Christie switch(param) { 3213843c0a8aSMike Christie case ISCSI_PARAM_FAST_ABORT: 3214843c0a8aSMike Christie sscanf(buf, "%d", &session->fast_abort); 3215843c0a8aSMike Christie break; 3216f6d5180cSMike Christie case ISCSI_PARAM_ABORT_TMO: 3217f6d5180cSMike Christie sscanf(buf, "%d", &session->abort_timeout); 3218f6d5180cSMike Christie break; 3219f6d5180cSMike Christie case ISCSI_PARAM_LU_RESET_TMO: 3220f6d5180cSMike Christie sscanf(buf, "%d", &session->lu_reset_timeout); 3221f6d5180cSMike Christie break; 32223fe5ae8bSMike Christie case ISCSI_PARAM_TGT_RESET_TMO: 32233fe5ae8bSMike Christie sscanf(buf, "%d", &session->tgt_reset_timeout); 32243fe5ae8bSMike Christie break; 3225f6d5180cSMike Christie case ISCSI_PARAM_PING_TMO: 3226f6d5180cSMike Christie sscanf(buf, "%d", &conn->ping_timeout); 3227f6d5180cSMike Christie break; 3228f6d5180cSMike Christie case ISCSI_PARAM_RECV_TMO: 3229f6d5180cSMike Christie sscanf(buf, "%d", &conn->recv_timeout); 3230f6d5180cSMike Christie break; 3231a54a52caSMike Christie case ISCSI_PARAM_MAX_RECV_DLENGTH: 3232a54a52caSMike Christie sscanf(buf, "%d", &conn->max_recv_dlength); 3233a54a52caSMike Christie break; 3234a54a52caSMike Christie case ISCSI_PARAM_MAX_XMIT_DLENGTH: 3235a54a52caSMike Christie sscanf(buf, "%d", &conn->max_xmit_dlength); 3236a54a52caSMike Christie break; 3237a54a52caSMike Christie case ISCSI_PARAM_HDRDGST_EN: 3238a54a52caSMike Christie sscanf(buf, "%d", &conn->hdrdgst_en); 3239a54a52caSMike Christie break; 3240a54a52caSMike Christie case ISCSI_PARAM_DATADGST_EN: 3241a54a52caSMike Christie sscanf(buf, "%d", &conn->datadgst_en); 3242a54a52caSMike Christie break; 3243a54a52caSMike Christie case ISCSI_PARAM_INITIAL_R2T_EN: 3244a54a52caSMike Christie sscanf(buf, "%d", &session->initial_r2t_en); 3245a54a52caSMike Christie break; 3246a54a52caSMike Christie case ISCSI_PARAM_MAX_R2T: 32471304be5fSMike Christie sscanf(buf, "%hu", &session->max_r2t); 3248a54a52caSMike Christie break; 3249a54a52caSMike Christie case ISCSI_PARAM_IMM_DATA_EN: 3250a54a52caSMike Christie sscanf(buf, "%d", &session->imm_data_en); 3251a54a52caSMike Christie break; 3252a54a52caSMike Christie case ISCSI_PARAM_FIRST_BURST: 3253a54a52caSMike Christie sscanf(buf, "%d", &session->first_burst); 3254a54a52caSMike Christie break; 3255a54a52caSMike Christie case ISCSI_PARAM_MAX_BURST: 3256a54a52caSMike Christie sscanf(buf, "%d", &session->max_burst); 3257a54a52caSMike Christie break; 3258a54a52caSMike Christie case ISCSI_PARAM_PDU_INORDER_EN: 3259a54a52caSMike Christie sscanf(buf, "%d", &session->pdu_inorder_en); 3260a54a52caSMike Christie break; 3261a54a52caSMike Christie case ISCSI_PARAM_DATASEQ_INORDER_EN: 3262a54a52caSMike Christie sscanf(buf, "%d", &session->dataseq_inorder_en); 3263a54a52caSMike Christie break; 3264a54a52caSMike Christie case ISCSI_PARAM_ERL: 3265a54a52caSMike Christie sscanf(buf, "%d", &session->erl); 3266a54a52caSMike Christie break; 3267a54a52caSMike Christie case ISCSI_PARAM_EXP_STATSN: 3268a54a52caSMike Christie sscanf(buf, "%u", &conn->exp_statsn); 3269a54a52caSMike Christie break; 3270b2c64167SMike Christie case ISCSI_PARAM_USERNAME: 32715700b1afSMike Christie return iscsi_switch_str_param(&session->username, buf); 3272b2c64167SMike Christie case ISCSI_PARAM_USERNAME_IN: 32735700b1afSMike Christie return iscsi_switch_str_param(&session->username_in, buf); 3274b2c64167SMike Christie case ISCSI_PARAM_PASSWORD: 32755700b1afSMike Christie return iscsi_switch_str_param(&session->password, buf); 3276b2c64167SMike Christie case ISCSI_PARAM_PASSWORD_IN: 32775700b1afSMike Christie return iscsi_switch_str_param(&session->password_in, buf); 3278a54a52caSMike Christie case ISCSI_PARAM_TARGET_NAME: 32795700b1afSMike Christie return iscsi_switch_str_param(&session->targetname, buf); 32803c5c4801SVikas Chaudhary case ISCSI_PARAM_TARGET_ALIAS: 32813c5c4801SVikas Chaudhary return iscsi_switch_str_param(&session->targetalias, buf); 3282a54a52caSMike Christie case ISCSI_PARAM_TPGT: 3283a54a52caSMike Christie sscanf(buf, "%d", &session->tpgt); 3284a54a52caSMike Christie break; 3285a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_PORT: 3286a54a52caSMike Christie sscanf(buf, "%d", &conn->persistent_port); 3287a54a52caSMike Christie break; 3288a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_ADDRESS: 32895700b1afSMike Christie return iscsi_switch_str_param(&conn->persistent_address, buf); 329088dfd340SMike Christie case ISCSI_PARAM_IFACE_NAME: 32915700b1afSMike Christie return iscsi_switch_str_param(&session->ifacename, buf); 329288dfd340SMike Christie case ISCSI_PARAM_INITIATOR_NAME: 32935700b1afSMike Christie return iscsi_switch_str_param(&session->initiatorname, buf); 32943b9373e9SEddie Wai case ISCSI_PARAM_BOOT_ROOT: 32953b9373e9SEddie Wai return iscsi_switch_str_param(&session->boot_root, buf); 32963b9373e9SEddie Wai case ISCSI_PARAM_BOOT_NIC: 32973b9373e9SEddie Wai return iscsi_switch_str_param(&session->boot_nic, buf); 32983b9373e9SEddie Wai case ISCSI_PARAM_BOOT_TARGET: 32993b9373e9SEddie Wai return iscsi_switch_str_param(&session->boot_target, buf); 3300f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_PORTAL_TYPE: 3301f8525eb4SAdheer Chandravanshi return iscsi_switch_str_param(&session->portal_type, buf); 3302f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_PARENT_TYPE: 3303f8525eb4SAdheer Chandravanshi return iscsi_switch_str_param(&session->discovery_parent_type, 3304f8525eb4SAdheer Chandravanshi buf); 33056a06a4b8SOr Gerlitz case ISCSI_PARAM_DISCOVERY_SESS: 33066a06a4b8SOr Gerlitz sscanf(buf, "%d", &val); 33076a06a4b8SOr Gerlitz session->discovery_sess = !!val; 33086a06a4b8SOr Gerlitz break; 3309ae56ff40SAdheer Chandravanshi case ISCSI_PARAM_LOCAL_IPADDR: 3310ae56ff40SAdheer Chandravanshi return iscsi_switch_str_param(&conn->local_ipaddr, buf); 3311a54a52caSMike Christie default: 3312a54a52caSMike Christie return -ENOSYS; 3313a54a52caSMike Christie } 3314a54a52caSMike Christie 3315a54a52caSMike Christie return 0; 3316a54a52caSMike Christie } 3317a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_set_param); 3318a54a52caSMike Christie 3319a54a52caSMike Christie int iscsi_session_get_param(struct iscsi_cls_session *cls_session, 3320a54a52caSMike Christie enum iscsi_param param, char *buf) 3321a54a52caSMike Christie { 332275613521SMike Christie struct iscsi_session *session = cls_session->dd_data; 3323a54a52caSMike Christie int len; 3324a54a52caSMike Christie 3325a54a52caSMike Christie switch(param) { 3326843c0a8aSMike Christie case ISCSI_PARAM_FAST_ABORT: 3327843c0a8aSMike Christie len = sprintf(buf, "%d\n", session->fast_abort); 3328843c0a8aSMike Christie break; 3329f6d5180cSMike Christie case ISCSI_PARAM_ABORT_TMO: 3330f6d5180cSMike Christie len = sprintf(buf, "%d\n", session->abort_timeout); 3331f6d5180cSMike Christie break; 3332f6d5180cSMike Christie case ISCSI_PARAM_LU_RESET_TMO: 3333f6d5180cSMike Christie len = sprintf(buf, "%d\n", session->lu_reset_timeout); 3334f6d5180cSMike Christie break; 33353fe5ae8bSMike Christie case ISCSI_PARAM_TGT_RESET_TMO: 33363fe5ae8bSMike Christie len = sprintf(buf, "%d\n", session->tgt_reset_timeout); 33373fe5ae8bSMike Christie break; 3338a54a52caSMike Christie case ISCSI_PARAM_INITIAL_R2T_EN: 3339a54a52caSMike Christie len = sprintf(buf, "%d\n", session->initial_r2t_en); 3340a54a52caSMike Christie break; 3341a54a52caSMike Christie case ISCSI_PARAM_MAX_R2T: 3342a54a52caSMike Christie len = sprintf(buf, "%hu\n", session->max_r2t); 3343a54a52caSMike Christie break; 3344a54a52caSMike Christie case ISCSI_PARAM_IMM_DATA_EN: 3345a54a52caSMike Christie len = sprintf(buf, "%d\n", session->imm_data_en); 3346a54a52caSMike Christie break; 3347a54a52caSMike Christie case ISCSI_PARAM_FIRST_BURST: 3348a54a52caSMike Christie len = sprintf(buf, "%u\n", session->first_burst); 3349a54a52caSMike Christie break; 3350a54a52caSMike Christie case ISCSI_PARAM_MAX_BURST: 3351a54a52caSMike Christie len = sprintf(buf, "%u\n", session->max_burst); 3352a54a52caSMike Christie break; 3353a54a52caSMike Christie case ISCSI_PARAM_PDU_INORDER_EN: 3354a54a52caSMike Christie len = sprintf(buf, "%d\n", session->pdu_inorder_en); 3355a54a52caSMike Christie break; 3356a54a52caSMike Christie case ISCSI_PARAM_DATASEQ_INORDER_EN: 3357a54a52caSMike Christie len = sprintf(buf, "%d\n", session->dataseq_inorder_en); 3358a54a52caSMike Christie break; 33595f09e1f3SAdheer Chandravanshi case ISCSI_PARAM_DEF_TASKMGMT_TMO: 33605f09e1f3SAdheer Chandravanshi len = sprintf(buf, "%d\n", session->def_taskmgmt_tmo); 33615f09e1f3SAdheer Chandravanshi break; 3362a54a52caSMike Christie case ISCSI_PARAM_ERL: 3363a54a52caSMike Christie len = sprintf(buf, "%d\n", session->erl); 3364a54a52caSMike Christie break; 3365a54a52caSMike Christie case ISCSI_PARAM_TARGET_NAME: 3366a54a52caSMike Christie len = sprintf(buf, "%s\n", session->targetname); 3367a54a52caSMike Christie break; 33683c5c4801SVikas Chaudhary case ISCSI_PARAM_TARGET_ALIAS: 33693c5c4801SVikas Chaudhary len = sprintf(buf, "%s\n", session->targetalias); 33703c5c4801SVikas Chaudhary break; 3371a54a52caSMike Christie case ISCSI_PARAM_TPGT: 3372a54a52caSMike Christie len = sprintf(buf, "%d\n", session->tpgt); 3373a54a52caSMike Christie break; 3374b2c64167SMike Christie case ISCSI_PARAM_USERNAME: 3375b2c64167SMike Christie len = sprintf(buf, "%s\n", session->username); 3376b2c64167SMike Christie break; 3377b2c64167SMike Christie case ISCSI_PARAM_USERNAME_IN: 3378b2c64167SMike Christie len = sprintf(buf, "%s\n", session->username_in); 3379b2c64167SMike Christie break; 3380b2c64167SMike Christie case ISCSI_PARAM_PASSWORD: 3381b2c64167SMike Christie len = sprintf(buf, "%s\n", session->password); 3382b2c64167SMike Christie break; 3383b2c64167SMike Christie case ISCSI_PARAM_PASSWORD_IN: 3384b2c64167SMike Christie len = sprintf(buf, "%s\n", session->password_in); 3385b2c64167SMike Christie break; 338688dfd340SMike Christie case ISCSI_PARAM_IFACE_NAME: 338788dfd340SMike Christie len = sprintf(buf, "%s\n", session->ifacename); 338888dfd340SMike Christie break; 338988dfd340SMike Christie case ISCSI_PARAM_INITIATOR_NAME: 339088dfd340SMike Christie len = sprintf(buf, "%s\n", session->initiatorname); 339188dfd340SMike Christie break; 33923b9373e9SEddie Wai case ISCSI_PARAM_BOOT_ROOT: 33933b9373e9SEddie Wai len = sprintf(buf, "%s\n", session->boot_root); 33943b9373e9SEddie Wai break; 33953b9373e9SEddie Wai case ISCSI_PARAM_BOOT_NIC: 33963b9373e9SEddie Wai len = sprintf(buf, "%s\n", session->boot_nic); 33973b9373e9SEddie Wai break; 33983b9373e9SEddie Wai case ISCSI_PARAM_BOOT_TARGET: 33993b9373e9SEddie Wai len = sprintf(buf, "%s\n", session->boot_target); 34009a2307b1SAdheer Chandravanshi break; 3401f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_AUTO_SND_TGT_DISABLE: 3402f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", session->auto_snd_tgt_disable); 3403f8525eb4SAdheer Chandravanshi break; 3404f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_SESS: 3405f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", session->discovery_sess); 3406f8525eb4SAdheer Chandravanshi break; 3407f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_PORTAL_TYPE: 3408f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%s\n", session->portal_type); 3409f8525eb4SAdheer Chandravanshi break; 3410f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_CHAP_AUTH_EN: 3411f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", session->chap_auth_en); 3412f8525eb4SAdheer Chandravanshi break; 3413f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_LOGOUT_EN: 3414f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", session->discovery_logout_en); 3415f8525eb4SAdheer Chandravanshi break; 3416f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_BIDI_CHAP_EN: 3417f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", session->bidi_chap_en); 3418f8525eb4SAdheer Chandravanshi break; 3419f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL: 3420f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", session->discovery_auth_optional); 3421f8525eb4SAdheer Chandravanshi break; 3422f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DEF_TIME2WAIT: 3423f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%d\n", session->time2wait); 3424f8525eb4SAdheer Chandravanshi break; 3425f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DEF_TIME2RETAIN: 3426f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%d\n", session->time2retain); 3427f8525eb4SAdheer Chandravanshi break; 3428f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TSID: 3429f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", session->tsid); 3430f8525eb4SAdheer Chandravanshi break; 3431f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_ISID: 3432f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%02x%02x%02x%02x%02x%02x\n", 3433f8525eb4SAdheer Chandravanshi session->isid[0], session->isid[1], 3434f8525eb4SAdheer Chandravanshi session->isid[2], session->isid[3], 3435f8525eb4SAdheer Chandravanshi session->isid[4], session->isid[5]); 3436f8525eb4SAdheer Chandravanshi break; 3437f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_PARENT_IDX: 3438f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", session->discovery_parent_idx); 3439f8525eb4SAdheer Chandravanshi break; 3440f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_DISCOVERY_PARENT_TYPE: 3441f8525eb4SAdheer Chandravanshi if (session->discovery_parent_type) 3442f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%s\n", 3443f8525eb4SAdheer Chandravanshi session->discovery_parent_type); 3444f8525eb4SAdheer Chandravanshi else 3445f8525eb4SAdheer Chandravanshi len = sprintf(buf, "\n"); 34463b9373e9SEddie Wai break; 3447a54a52caSMike Christie default: 3448a54a52caSMike Christie return -ENOSYS; 3449a54a52caSMike Christie } 3450a54a52caSMike Christie 3451a54a52caSMike Christie return len; 3452a54a52caSMike Christie } 3453a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_get_param); 3454a54a52caSMike Christie 345500f3708eSMike Christie int iscsi_conn_get_addr_param(struct sockaddr_storage *addr, 345600f3708eSMike Christie enum iscsi_param param, char *buf) 345700f3708eSMike Christie { 345800f3708eSMike Christie struct sockaddr_in6 *sin6 = NULL; 345900f3708eSMike Christie struct sockaddr_in *sin = NULL; 346000f3708eSMike Christie int len; 346100f3708eSMike Christie 346200f3708eSMike Christie switch (addr->ss_family) { 346300f3708eSMike Christie case AF_INET: 346400f3708eSMike Christie sin = (struct sockaddr_in *)addr; 346500f3708eSMike Christie break; 346600f3708eSMike Christie case AF_INET6: 346700f3708eSMike Christie sin6 = (struct sockaddr_in6 *)addr; 346800f3708eSMike Christie break; 346900f3708eSMike Christie default: 347000f3708eSMike Christie return -EINVAL; 347100f3708eSMike Christie } 347200f3708eSMike Christie 347300f3708eSMike Christie switch (param) { 347400f3708eSMike Christie case ISCSI_PARAM_CONN_ADDRESS: 347500f3708eSMike Christie case ISCSI_HOST_PARAM_IPADDRESS: 347600f3708eSMike Christie if (sin) 347700f3708eSMike Christie len = sprintf(buf, "%pI4\n", &sin->sin_addr.s_addr); 347800f3708eSMike Christie else 347900f3708eSMike Christie len = sprintf(buf, "%pI6\n", &sin6->sin6_addr); 348000f3708eSMike Christie break; 348100f3708eSMike Christie case ISCSI_PARAM_CONN_PORT: 34824bfb8ebfSMike Christie case ISCSI_PARAM_LOCAL_PORT: 348300f3708eSMike Christie if (sin) 348400f3708eSMike Christie len = sprintf(buf, "%hu\n", be16_to_cpu(sin->sin_port)); 348500f3708eSMike Christie else 348600f3708eSMike Christie len = sprintf(buf, "%hu\n", 348700f3708eSMike Christie be16_to_cpu(sin6->sin6_port)); 348800f3708eSMike Christie break; 348900f3708eSMike Christie default: 349000f3708eSMike Christie return -EINVAL; 349100f3708eSMike Christie } 349200f3708eSMike Christie 349300f3708eSMike Christie return len; 349400f3708eSMike Christie } 349500f3708eSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param); 349600f3708eSMike Christie 3497a54a52caSMike Christie int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn, 3498a54a52caSMike Christie enum iscsi_param param, char *buf) 3499a54a52caSMike Christie { 3500a54a52caSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 3501a54a52caSMike Christie int len; 3502a54a52caSMike Christie 3503a54a52caSMike Christie switch(param) { 3504f6d5180cSMike Christie case ISCSI_PARAM_PING_TMO: 3505f6d5180cSMike Christie len = sprintf(buf, "%u\n", conn->ping_timeout); 3506f6d5180cSMike Christie break; 3507f6d5180cSMike Christie case ISCSI_PARAM_RECV_TMO: 3508f6d5180cSMike Christie len = sprintf(buf, "%u\n", conn->recv_timeout); 3509f6d5180cSMike Christie break; 3510a54a52caSMike Christie case ISCSI_PARAM_MAX_RECV_DLENGTH: 3511a54a52caSMike Christie len = sprintf(buf, "%u\n", conn->max_recv_dlength); 3512a54a52caSMike Christie break; 3513a54a52caSMike Christie case ISCSI_PARAM_MAX_XMIT_DLENGTH: 3514a54a52caSMike Christie len = sprintf(buf, "%u\n", conn->max_xmit_dlength); 3515a54a52caSMike Christie break; 3516a54a52caSMike Christie case ISCSI_PARAM_HDRDGST_EN: 3517a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->hdrdgst_en); 3518a54a52caSMike Christie break; 3519a54a52caSMike Christie case ISCSI_PARAM_DATADGST_EN: 3520a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->datadgst_en); 3521a54a52caSMike Christie break; 3522a54a52caSMike Christie case ISCSI_PARAM_IFMARKER_EN: 3523a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->ifmarker_en); 3524a54a52caSMike Christie break; 3525a54a52caSMike Christie case ISCSI_PARAM_OFMARKER_EN: 3526a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->ofmarker_en); 3527a54a52caSMike Christie break; 3528a54a52caSMike Christie case ISCSI_PARAM_EXP_STATSN: 3529a54a52caSMike Christie len = sprintf(buf, "%u\n", conn->exp_statsn); 3530a54a52caSMike Christie break; 3531a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_PORT: 3532a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->persistent_port); 3533a54a52caSMike Christie break; 3534a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_ADDRESS: 3535a54a52caSMike Christie len = sprintf(buf, "%s\n", conn->persistent_address); 3536a54a52caSMike Christie break; 3537f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_STATSN: 3538f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->statsn); 3539f8525eb4SAdheer Chandravanshi break; 3540f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_MAX_SEGMENT_SIZE: 3541f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->max_segment_size); 3542f8525eb4SAdheer Chandravanshi break; 3543f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_KEEPALIVE_TMO: 3544f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->keepalive_tmo); 3545f8525eb4SAdheer Chandravanshi break; 3546f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_LOCAL_PORT: 3547f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->local_port); 3548f8525eb4SAdheer Chandravanshi break; 3549f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_TIMESTAMP_STAT: 3550f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->tcp_timestamp_stat); 3551f8525eb4SAdheer Chandravanshi break; 3552f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_NAGLE_DISABLE: 3553f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->tcp_nagle_disable); 3554f8525eb4SAdheer Chandravanshi break; 3555f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_WSF_DISABLE: 3556f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->tcp_wsf_disable); 3557f8525eb4SAdheer Chandravanshi break; 3558f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_TIMER_SCALE: 3559f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->tcp_timer_scale); 3560f8525eb4SAdheer Chandravanshi break; 3561f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_TIMESTAMP_EN: 3562f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->tcp_timestamp_en); 3563f8525eb4SAdheer Chandravanshi break; 3564f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_IP_FRAGMENT_DISABLE: 3565f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->fragment_disable); 3566f8525eb4SAdheer Chandravanshi break; 3567f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_IPV4_TOS: 3568f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->ipv4_tos); 3569f8525eb4SAdheer Chandravanshi break; 3570f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_IPV6_TC: 3571f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->ipv6_traffic_class); 3572f8525eb4SAdheer Chandravanshi break; 35735f09e1f3SAdheer Chandravanshi case ISCSI_PARAM_IPV6_FLOW_LABEL: 35745f09e1f3SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->ipv6_flow_label); 35755f09e1f3SAdheer Chandravanshi break; 3576f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6: 3577f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->is_fw_assigned_ipv6); 3578f8525eb4SAdheer Chandravanshi break; 3579f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_XMIT_WSF: 3580f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->tcp_xmit_wsf); 3581f8525eb4SAdheer Chandravanshi break; 3582f8525eb4SAdheer Chandravanshi case ISCSI_PARAM_TCP_RECV_WSF: 3583f8525eb4SAdheer Chandravanshi len = sprintf(buf, "%u\n", conn->tcp_recv_wsf); 3584f8525eb4SAdheer Chandravanshi break; 3585ae56ff40SAdheer Chandravanshi case ISCSI_PARAM_LOCAL_IPADDR: 3586ae56ff40SAdheer Chandravanshi len = sprintf(buf, "%s\n", conn->local_ipaddr); 3587ae56ff40SAdheer Chandravanshi break; 3588a54a52caSMike Christie default: 3589a54a52caSMike Christie return -ENOSYS; 3590a54a52caSMike Christie } 3591a54a52caSMike Christie 3592a54a52caSMike Christie return len; 3593a54a52caSMike Christie } 3594a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_param); 3595a54a52caSMike Christie 35960801c242SMike Christie int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param, 35970801c242SMike Christie char *buf) 35980801c242SMike Christie { 359975613521SMike Christie struct iscsi_host *ihost = shost_priv(shost); 36000801c242SMike Christie int len; 36010801c242SMike Christie 36020801c242SMike Christie switch (param) { 3603d8196ed2SMike Christie case ISCSI_HOST_PARAM_NETDEV_NAME: 360475613521SMike Christie len = sprintf(buf, "%s\n", ihost->netdev); 3605d8196ed2SMike Christie break; 36060801c242SMike Christie case ISCSI_HOST_PARAM_HWADDRESS: 360775613521SMike Christie len = sprintf(buf, "%s\n", ihost->hwaddress); 36080801c242SMike Christie break; 36098ad5781aSMike Christie case ISCSI_HOST_PARAM_INITIATOR_NAME: 361075613521SMike Christie len = sprintf(buf, "%s\n", ihost->initiatorname); 36118ad5781aSMike Christie break; 36120801c242SMike Christie default: 36130801c242SMike Christie return -ENOSYS; 36140801c242SMike Christie } 36150801c242SMike Christie 36160801c242SMike Christie return len; 36170801c242SMike Christie } 36180801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_get_param); 36190801c242SMike Christie 36200801c242SMike Christie int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param, 36210801c242SMike Christie char *buf, int buflen) 36220801c242SMike Christie { 362375613521SMike Christie struct iscsi_host *ihost = shost_priv(shost); 36240801c242SMike Christie 36250801c242SMike Christie switch (param) { 3626d8196ed2SMike Christie case ISCSI_HOST_PARAM_NETDEV_NAME: 36275700b1afSMike Christie return iscsi_switch_str_param(&ihost->netdev, buf); 36280801c242SMike Christie case ISCSI_HOST_PARAM_HWADDRESS: 36295700b1afSMike Christie return iscsi_switch_str_param(&ihost->hwaddress, buf); 36308ad5781aSMike Christie case ISCSI_HOST_PARAM_INITIATOR_NAME: 36315700b1afSMike Christie return iscsi_switch_str_param(&ihost->initiatorname, buf); 36320801c242SMike Christie default: 36330801c242SMike Christie return -ENOSYS; 36340801c242SMike Christie } 36350801c242SMike Christie 36360801c242SMike Christie return 0; 36370801c242SMike Christie } 36380801c242SMike Christie EXPORT_SYMBOL_GPL(iscsi_host_set_param); 36390801c242SMike Christie 36407996a778SMike Christie MODULE_AUTHOR("Mike Christie"); 36417996a778SMike Christie MODULE_DESCRIPTION("iSCSI library functions"); 36427996a778SMike Christie MODULE_LICENSE("GPL"); 3643