Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1 | /* |
| 2 | * iSCSI lib functions |
| 3 | * |
| 4 | * Copyright (C) 2006 Red Hat, Inc. All rights reserved. |
| 5 | * Copyright (C) 2004 - 2006 Mike Christie |
| 6 | * Copyright (C) 2004 - 2005 Dmitry Yusupov |
| 7 | * Copyright (C) 2004 - 2005 Alex Aizman |
| 8 | * maintained by open-iscsi@googlegroups.com |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 23 | */ |
| 24 | #include <linux/types.h> |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 25 | #include <linux/kfifo.h> |
| 26 | #include <linux/delay.h> |
vignesh babu | 1183657 | 2007-12-13 12:43:41 -0600 | [diff] [blame] | 27 | #include <linux/log2.h> |
Mike Christie | 8eb0053 | 2007-02-28 17:32:19 -0600 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 29 | #include <net/tcp.h> |
| 30 | #include <scsi/scsi_cmnd.h> |
| 31 | #include <scsi/scsi_device.h> |
| 32 | #include <scsi/scsi_eh.h> |
| 33 | #include <scsi/scsi_tcq.h> |
| 34 | #include <scsi/scsi_host.h> |
| 35 | #include <scsi/scsi.h> |
| 36 | #include <scsi/iscsi_proto.h> |
| 37 | #include <scsi/scsi_transport.h> |
| 38 | #include <scsi/scsi_transport_iscsi.h> |
| 39 | #include <scsi/libiscsi.h> |
| 40 | |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 41 | static int iscsi_dbg_lib_conn; |
| 42 | module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int, |
| 43 | S_IRUGO | S_IWUSR); |
| 44 | MODULE_PARM_DESC(debug_libiscsi_conn, |
| 45 | "Turn on debugging for connections in libiscsi module. " |
| 46 | "Set to 1 to turn on, and zero to turn off. Default is off."); |
| 47 | |
| 48 | static int iscsi_dbg_lib_session; |
| 49 | module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int, |
| 50 | S_IRUGO | S_IWUSR); |
| 51 | MODULE_PARM_DESC(debug_libiscsi_session, |
| 52 | "Turn on debugging for sessions in libiscsi module. " |
| 53 | "Set to 1 to turn on, and zero to turn off. Default is off."); |
| 54 | |
| 55 | static int iscsi_dbg_lib_eh; |
| 56 | module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int, |
| 57 | S_IRUGO | S_IWUSR); |
| 58 | MODULE_PARM_DESC(debug_libiscsi_eh, |
| 59 | "Turn on debugging for error handling in libiscsi module. " |
| 60 | "Set to 1 to turn on, and zero to turn off. Default is off."); |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 61 | |
| 62 | #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \ |
| 63 | do { \ |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 64 | if (iscsi_dbg_lib_conn) \ |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 65 | iscsi_conn_printk(KERN_INFO, _conn, \ |
| 66 | "%s " dbg_fmt, \ |
| 67 | __func__, ##arg); \ |
| 68 | } while (0); |
| 69 | |
| 70 | #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \ |
| 71 | do { \ |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 72 | if (iscsi_dbg_lib_session) \ |
| 73 | iscsi_session_printk(KERN_INFO, _session, \ |
| 74 | "%s " dbg_fmt, \ |
| 75 | __func__, ##arg); \ |
| 76 | } while (0); |
| 77 | |
| 78 | #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \ |
| 79 | do { \ |
| 80 | if (iscsi_dbg_lib_eh) \ |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 81 | iscsi_session_printk(KERN_INFO, _session, \ |
| 82 | "%s " dbg_fmt, \ |
| 83 | __func__, ##arg); \ |
| 84 | } while (0); |
| 85 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 86 | /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */ |
| 87 | #define SNA32_CHECK 2147483648UL |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 88 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 89 | static int iscsi_sna_lt(u32 n1, u32 n2) |
| 90 | { |
| 91 | return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) || |
| 92 | (n1 > n2 && (n2 - n1 < SNA32_CHECK))); |
| 93 | } |
| 94 | |
| 95 | /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */ |
| 96 | static int iscsi_sna_lte(u32 n1, u32 n2) |
| 97 | { |
| 98 | return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) || |
| 99 | (n1 > n2 && (n2 - n1 < SNA32_CHECK))); |
| 100 | } |
| 101 | |
Mike Christie | 32ae763 | 2009-03-05 14:46:03 -0600 | [diff] [blame] | 102 | inline void iscsi_conn_queue_work(struct iscsi_conn *conn) |
| 103 | { |
| 104 | struct Scsi_Host *shost = conn->session->host; |
| 105 | struct iscsi_host *ihost = shost_priv(shost); |
| 106 | |
Mike Christie | 1336aed | 2009-05-13 17:57:48 -0500 | [diff] [blame] | 107 | if (ihost->workq) |
| 108 | queue_work(ihost->workq, &conn->xmitwork); |
Mike Christie | 32ae763 | 2009-03-05 14:46:03 -0600 | [diff] [blame] | 109 | } |
| 110 | EXPORT_SYMBOL_GPL(iscsi_conn_queue_work); |
| 111 | |
Mike Christie | 4c0ba5d | 2009-09-05 07:34:23 +0530 | [diff] [blame] | 112 | static void __iscsi_update_cmdsn(struct iscsi_session *session, |
| 113 | uint32_t exp_cmdsn, uint32_t max_cmdsn) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 114 | { |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 115 | /* |
| 116 | * standard specifies this check for when to update expected and |
| 117 | * max sequence numbers |
| 118 | */ |
| 119 | if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1)) |
| 120 | return; |
| 121 | |
| 122 | if (exp_cmdsn != session->exp_cmdsn && |
| 123 | !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn)) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 124 | session->exp_cmdsn = exp_cmdsn; |
| 125 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 126 | if (max_cmdsn != session->max_cmdsn && |
| 127 | !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) { |
| 128 | session->max_cmdsn = max_cmdsn; |
| 129 | /* |
| 130 | * if the window closed with IO queued, then kick the |
| 131 | * xmit thread |
| 132 | */ |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 133 | if (!list_empty(&session->leadconn->cmdqueue) || |
Mike Christie | 1336aed | 2009-05-13 17:57:48 -0500 | [diff] [blame] | 134 | !list_empty(&session->leadconn->mgmtqueue)) |
| 135 | iscsi_conn_queue_work(session->leadconn); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 136 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 137 | } |
Mike Christie | 4c0ba5d | 2009-09-05 07:34:23 +0530 | [diff] [blame] | 138 | |
| 139 | void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr) |
| 140 | { |
| 141 | __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn), |
| 142 | be32_to_cpu(hdr->max_cmdsn)); |
| 143 | } |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 144 | EXPORT_SYMBOL_GPL(iscsi_update_cmdsn); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 145 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 146 | /** |
| 147 | * iscsi_prep_data_out_pdu - initialize Data-Out |
| 148 | * @task: scsi command task |
| 149 | * @r2t: R2T info |
| 150 | * @hdr: iscsi data in pdu |
| 151 | * |
| 152 | * Notes: |
| 153 | * Initialize Data-Out within this R2T sequence and finds |
| 154 | * proper data_offset within this SCSI command. |
| 155 | * |
| 156 | * This function is called with connection lock taken. |
| 157 | **/ |
| 158 | void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t, |
| 159 | struct iscsi_data *hdr) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 160 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 161 | struct iscsi_conn *conn = task->conn; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 162 | unsigned int left = r2t->data_length - r2t->sent; |
| 163 | |
| 164 | task->hdr_len = sizeof(struct iscsi_data); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 165 | |
| 166 | memset(hdr, 0, sizeof(struct iscsi_data)); |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 167 | hdr->ttt = r2t->ttt; |
| 168 | hdr->datasn = cpu_to_be32(r2t->datasn); |
| 169 | r2t->datasn++; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 170 | hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 171 | memcpy(hdr->lun, task->lun, sizeof(hdr->lun)); |
| 172 | hdr->itt = task->hdr_itt; |
| 173 | hdr->exp_statsn = r2t->exp_statsn; |
| 174 | hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent); |
| 175 | if (left > conn->max_xmit_dlength) { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 176 | hton24(hdr->dlength, conn->max_xmit_dlength); |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 177 | r2t->data_count = conn->max_xmit_dlength; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 178 | hdr->flags = 0; |
| 179 | } else { |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 180 | hton24(hdr->dlength, left); |
| 181 | r2t->data_count = left; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 182 | hdr->flags = ISCSI_FLAG_CMD_FINAL; |
| 183 | } |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 184 | conn->dataout_pdus_cnt++; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 185 | } |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 186 | EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 187 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 188 | static int iscsi_add_hdr(struct iscsi_task *task, unsigned len) |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 189 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 190 | unsigned exp_len = task->hdr_len + len; |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 191 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 192 | if (exp_len > task->hdr_max) { |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 193 | WARN_ON(1); |
| 194 | return -EINVAL; |
| 195 | } |
| 196 | |
| 197 | WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 198 | task->hdr_len = exp_len; |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 202 | /* |
| 203 | * make an extended cdb AHS |
| 204 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 205 | static int iscsi_prep_ecdb_ahs(struct iscsi_task *task) |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 206 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 207 | struct scsi_cmnd *cmd = task->sc; |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 208 | unsigned rlen, pad_len; |
| 209 | unsigned short ahslength; |
| 210 | struct iscsi_ecdb_ahdr *ecdb_ahdr; |
| 211 | int rc; |
| 212 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 213 | ecdb_ahdr = iscsi_next_hdr(task); |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 214 | rlen = cmd->cmd_len - ISCSI_CDB_SIZE; |
| 215 | |
| 216 | BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb)); |
| 217 | ahslength = rlen + sizeof(ecdb_ahdr->reserved); |
| 218 | |
| 219 | pad_len = iscsi_padding(rlen); |
| 220 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 221 | rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) + |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 222 | sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len); |
| 223 | if (rc) |
| 224 | return rc; |
| 225 | |
| 226 | if (pad_len) |
| 227 | memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len); |
| 228 | |
| 229 | ecdb_ahdr->ahslength = cpu_to_be16(ahslength); |
| 230 | ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB; |
| 231 | ecdb_ahdr->reserved = 0; |
| 232 | memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen); |
| 233 | |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 234 | ISCSI_DBG_SESSION(task->conn->session, |
| 235 | "iscsi_prep_ecdb_ahs: varlen_cdb_len %d " |
| 236 | "rlen %d pad_len %d ahs_length %d iscsi_headers_size " |
| 237 | "%u\n", cmd->cmd_len, rlen, pad_len, ahslength, |
| 238 | task->hdr_len); |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 242 | static int iscsi_prep_bidi_ahs(struct iscsi_task *task) |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 243 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 244 | struct scsi_cmnd *sc = task->sc; |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 245 | struct iscsi_rlength_ahdr *rlen_ahdr; |
| 246 | int rc; |
| 247 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 248 | rlen_ahdr = iscsi_next_hdr(task); |
| 249 | rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr)); |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 250 | if (rc) |
| 251 | return rc; |
| 252 | |
| 253 | rlen_ahdr->ahslength = |
| 254 | cpu_to_be16(sizeof(rlen_ahdr->read_length) + |
| 255 | sizeof(rlen_ahdr->reserved)); |
| 256 | rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH; |
| 257 | rlen_ahdr->reserved = 0; |
| 258 | rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length); |
| 259 | |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 260 | ISCSI_DBG_SESSION(task->conn->session, |
| 261 | "bidi-in rlen_ahdr->read_length(%d) " |
| 262 | "rlen_ahdr->ahslength(%d)\n", |
| 263 | be32_to_cpu(rlen_ahdr->read_length), |
| 264 | be16_to_cpu(rlen_ahdr->ahslength)); |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 268 | /** |
| 269 | * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 270 | * @task: iscsi task |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 271 | * |
| 272 | * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set |
| 273 | * fields like dlength or final based on how much data it sends |
| 274 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 275 | static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 276 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 277 | struct iscsi_conn *conn = task->conn; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 278 | struct iscsi_session *session = conn->session; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 279 | struct scsi_cmnd *sc = task->sc; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 280 | struct iscsi_cmd *hdr; |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 281 | unsigned hdrlength, cmd_len; |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 282 | itt_t itt; |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 283 | int rc; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 284 | |
Mike Christie | 184b57c | 2009-05-13 17:57:39 -0500 | [diff] [blame] | 285 | if (conn->session->tt->alloc_pdu) { |
| 286 | rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD); |
| 287 | if (rc) |
| 288 | return rc; |
| 289 | } |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 290 | hdr = (struct iscsi_cmd *) task->hdr; |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 291 | itt = hdr->itt; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 292 | memset(hdr, 0, sizeof(*hdr)); |
| 293 | |
Mike Christie | 2ff79d5 | 2008-12-02 00:32:14 -0600 | [diff] [blame] | 294 | if (session->tt->parse_pdu_itt) |
| 295 | hdr->itt = task->hdr_itt = itt; |
| 296 | else |
| 297 | hdr->itt = task->hdr_itt = build_itt(task->itt, |
| 298 | task->conn->session->age); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 299 | task->hdr_len = 0; |
| 300 | rc = iscsi_add_hdr(task, sizeof(*hdr)); |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 301 | if (rc) |
| 302 | return rc; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 303 | hdr->opcode = ISCSI_OP_SCSI_CMD; |
| 304 | hdr->flags = ISCSI_ATTR_SIMPLE; |
| 305 | int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun); |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 306 | memcpy(task->lun, hdr->lun, sizeof(task->lun)); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 307 | hdr->exp_statsn = cpu_to_be32(conn->exp_statsn); |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 308 | cmd_len = sc->cmd_len; |
| 309 | if (cmd_len < ISCSI_CDB_SIZE) |
| 310 | memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len); |
| 311 | else if (cmd_len > ISCSI_CDB_SIZE) { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 312 | rc = iscsi_prep_ecdb_ahs(task); |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 313 | if (rc) |
| 314 | return rc; |
| 315 | cmd_len = ISCSI_CDB_SIZE; |
| 316 | } |
| 317 | memcpy(hdr->cdb, sc->cmnd, cmd_len); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 318 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 319 | task->imm_count = 0; |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 320 | if (scsi_bidi_cmnd(sc)) { |
| 321 | hdr->flags |= ISCSI_FLAG_CMD_READ; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 322 | rc = iscsi_prep_bidi_ahs(task); |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 323 | if (rc) |
| 324 | return rc; |
| 325 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 326 | if (sc->sc_data_direction == DMA_TO_DEVICE) { |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 327 | unsigned out_len = scsi_out(sc)->length; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 328 | struct iscsi_r2t_info *r2t = &task->unsol_r2t; |
| 329 | |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 330 | hdr->data_length = cpu_to_be32(out_len); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 331 | hdr->flags |= ISCSI_FLAG_CMD_WRITE; |
| 332 | /* |
| 333 | * Write counters: |
| 334 | * |
| 335 | * imm_count bytes to be sent right after |
| 336 | * SCSI PDU Header |
| 337 | * |
| 338 | * unsol_count bytes(as Data-Out) to be sent |
| 339 | * without R2T ack right after |
| 340 | * immediate data |
| 341 | * |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 342 | * r2t data_length bytes to be sent via R2T ack's |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 343 | * |
| 344 | * pad_count bytes to be sent as zero-padding |
| 345 | */ |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 346 | memset(r2t, 0, sizeof(*r2t)); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 347 | |
| 348 | if (session->imm_data_en) { |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 349 | if (out_len >= session->first_burst) |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 350 | task->imm_count = min(session->first_burst, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 351 | conn->max_xmit_dlength); |
| 352 | else |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 353 | task->imm_count = min(out_len, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 354 | conn->max_xmit_dlength); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 355 | hton24(hdr->dlength, task->imm_count); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 356 | } else |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 357 | zero_data(hdr->dlength); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 358 | |
Mike Christie | ffd0436 | 2006-08-31 18:09:24 -0400 | [diff] [blame] | 359 | if (!session->initial_r2t_en) { |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 360 | r2t->data_length = min(session->first_burst, out_len) - |
| 361 | task->imm_count; |
| 362 | r2t->data_offset = task->imm_count; |
| 363 | r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG); |
| 364 | r2t->exp_statsn = cpu_to_be32(conn->exp_statsn); |
Mike Christie | ffd0436 | 2006-08-31 18:09:24 -0400 | [diff] [blame] | 365 | } |
| 366 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 367 | if (!task->unsol_r2t.data_length) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 368 | /* No unsolicit Data-Out's */ |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 369 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 370 | } else { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 371 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
| 372 | zero_data(hdr->dlength); |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 373 | hdr->data_length = cpu_to_be32(scsi_in(sc)->length); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 374 | |
| 375 | if (sc->sc_data_direction == DMA_FROM_DEVICE) |
| 376 | hdr->flags |= ISCSI_FLAG_CMD_READ; |
| 377 | } |
| 378 | |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 379 | /* calculate size of additional header segments (AHSs) */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 380 | hdrlength = task->hdr_len - sizeof(*hdr); |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 381 | |
| 382 | WARN_ON(hdrlength & (ISCSI_PAD_LEN-1)); |
| 383 | hdrlength /= ISCSI_PAD_LEN; |
| 384 | |
| 385 | WARN_ON(hdrlength >= 256); |
| 386 | hdr->hlength = hdrlength & 0xFF; |
| 387 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 388 | if (session->tt->init_task && session->tt->init_task(task)) |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 389 | return -EIO; |
| 390 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 391 | task->state = ISCSI_TASK_RUNNING; |
Mike Christie | d3305f3 | 2009-08-20 15:10:58 -0500 | [diff] [blame] | 392 | hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn); |
| 393 | session->cmdsn++; |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 394 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 395 | conn->scsicmd_pdus_cnt++; |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 396 | ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x " |
| 397 | "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n", |
| 398 | scsi_bidi_cmnd(sc) ? "bidirectional" : |
| 399 | sc->sc_data_direction == DMA_TO_DEVICE ? |
| 400 | "write" : "read", conn->id, sc, sc->cmnd[0], |
| 401 | task->itt, scsi_bufflen(sc), |
| 402 | scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0, |
| 403 | session->cmdsn, |
| 404 | session->max_cmdsn - session->exp_cmdsn + 1); |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 405 | return 0; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 406 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 407 | |
| 408 | /** |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 409 | * iscsi_free_task - free a task |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 410 | * @task: iscsi cmd task |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 411 | * |
| 412 | * Must be called with session lock. |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 413 | * This function returns the scsi command to scsi-ml or cleans |
| 414 | * up mgmt tasks then returns the task to the pool. |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 415 | */ |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 416 | static void iscsi_free_task(struct iscsi_task *task) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 417 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 418 | struct iscsi_conn *conn = task->conn; |
Mike Christie | c1635cb | 2007-12-13 12:43:33 -0600 | [diff] [blame] | 419 | struct iscsi_session *session = conn->session; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 420 | struct scsi_cmnd *sc = task->sc; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 421 | |
Mike Christie | 4421c9e | 2009-05-13 17:57:50 -0500 | [diff] [blame] | 422 | ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n", |
| 423 | task->itt, task->state, task->sc); |
| 424 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 425 | session->tt->cleanup_task(task); |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 426 | task->state = ISCSI_TASK_FREE; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 427 | task->sc = NULL; |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 428 | /* |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 429 | * login task is preallocated so do not free |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 430 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 431 | if (conn->login_task == task) |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 432 | return; |
| 433 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 434 | __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*)); |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 435 | |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 436 | if (sc) { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 437 | task->sc = NULL; |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 438 | /* SCSI eh reuses commands to verify us */ |
| 439 | sc->SCp.ptr = NULL; |
| 440 | /* |
| 441 | * queue command may call this to free the task, but |
| 442 | * not have setup the sc callback |
| 443 | */ |
| 444 | if (sc->scsi_done) |
| 445 | sc->scsi_done(sc); |
| 446 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 447 | } |
| 448 | |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 449 | void __iscsi_get_task(struct iscsi_task *task) |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 450 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 451 | atomic_inc(&task->refcount); |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 452 | } |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 453 | EXPORT_SYMBOL_GPL(__iscsi_get_task); |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 454 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 455 | static void __iscsi_put_task(struct iscsi_task *task) |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 456 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 457 | if (atomic_dec_and_test(&task->refcount)) |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 458 | iscsi_free_task(task); |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 459 | } |
| 460 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 461 | void iscsi_put_task(struct iscsi_task *task) |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 462 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 463 | struct iscsi_session *session = task->conn->session; |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 464 | |
| 465 | spin_lock_bh(&session->lock); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 466 | __iscsi_put_task(task); |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 467 | spin_unlock_bh(&session->lock); |
| 468 | } |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 469 | EXPORT_SYMBOL_GPL(iscsi_put_task); |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 470 | |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 471 | /** |
| 472 | * iscsi_complete_task - finish a task |
| 473 | * @task: iscsi cmd task |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 474 | * @state: state to complete task with |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 475 | * |
| 476 | * Must be called with session lock. |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 477 | */ |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 478 | static void iscsi_complete_task(struct iscsi_task *task, int state) |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 479 | { |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 480 | struct iscsi_conn *conn = task->conn; |
| 481 | |
Mike Christie | 4421c9e | 2009-05-13 17:57:50 -0500 | [diff] [blame] | 482 | ISCSI_DBG_SESSION(conn->session, |
| 483 | "complete task itt 0x%x state %d sc %p\n", |
| 484 | task->itt, task->state, task->sc); |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 485 | if (task->state == ISCSI_TASK_COMPLETED || |
| 486 | task->state == ISCSI_TASK_ABRT_TMF || |
| 487 | task->state == ISCSI_TASK_ABRT_SESS_RECOV) |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 488 | return; |
| 489 | WARN_ON_ONCE(task->state == ISCSI_TASK_FREE); |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 490 | task->state = state; |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 491 | |
| 492 | if (!list_empty(&task->running)) |
| 493 | list_del_init(&task->running); |
| 494 | |
| 495 | if (conn->task == task) |
| 496 | conn->task = NULL; |
| 497 | |
| 498 | if (conn->ping_task == task) |
| 499 | conn->ping_task = NULL; |
| 500 | |
| 501 | /* release get from queueing */ |
| 502 | __iscsi_put_task(task); |
| 503 | } |
| 504 | |
Mike Christie | 4c0ba5d | 2009-09-05 07:34:23 +0530 | [diff] [blame] | 505 | /** |
| 506 | * iscsi_complete_scsi_task - finish scsi task normally |
| 507 | * @task: iscsi task for scsi cmd |
| 508 | * @exp_cmdsn: expected cmd sn in cpu format |
| 509 | * @max_cmdsn: max cmd sn in cpu format |
| 510 | * |
| 511 | * This is used when drivers do not need or cannot perform |
| 512 | * lower level pdu processing. |
| 513 | * |
| 514 | * Called with session lock |
| 515 | */ |
| 516 | void iscsi_complete_scsi_task(struct iscsi_task *task, |
| 517 | uint32_t exp_cmdsn, uint32_t max_cmdsn) |
| 518 | { |
| 519 | struct iscsi_conn *conn = task->conn; |
| 520 | |
| 521 | ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt); |
| 522 | |
| 523 | conn->last_recv = jiffies; |
| 524 | __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn); |
| 525 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
| 526 | } |
| 527 | EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task); |
| 528 | |
| 529 | |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 530 | /* |
| 531 | * session lock must be held and if not called for a task that is |
| 532 | * still pending or from the xmit thread, then xmit thread must |
| 533 | * be suspended. |
| 534 | */ |
| 535 | static void fail_scsi_task(struct iscsi_task *task, int err) |
| 536 | { |
| 537 | struct iscsi_conn *conn = task->conn; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 538 | struct scsi_cmnd *sc; |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 539 | int state; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 540 | |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 541 | /* |
| 542 | * if a command completes and we get a successful tmf response |
| 543 | * we will hit this because the scsi eh abort code does not take |
| 544 | * a ref to the task. |
| 545 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 546 | sc = task->sc; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 547 | if (!sc) |
| 548 | return; |
| 549 | |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 550 | if (task->state == ISCSI_TASK_PENDING) { |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 551 | /* |
| 552 | * cmd never made it to the xmit thread, so we should not count |
| 553 | * the cmd in the sequencing |
| 554 | */ |
| 555 | conn->session->queued_cmdsn--; |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 556 | /* it was never sent so just complete like normal */ |
| 557 | state = ISCSI_TASK_COMPLETED; |
| 558 | } else if (err == DID_TRANSPORT_DISRUPTED) |
| 559 | state = ISCSI_TASK_ABRT_SESS_RECOV; |
| 560 | else |
| 561 | state = ISCSI_TASK_ABRT_TMF; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 562 | |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 563 | sc->result = err << 16; |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 564 | if (!scsi_bidi_cmnd(sc)) |
| 565 | scsi_set_resid(sc, scsi_bufflen(sc)); |
| 566 | else { |
| 567 | scsi_out(sc)->resid = scsi_out(sc)->length; |
| 568 | scsi_in(sc)->resid = scsi_in(sc)->length; |
| 569 | } |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 570 | |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 571 | iscsi_complete_task(task, state); |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 572 | } |
| 573 | |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 574 | static int iscsi_prep_mgmt_task(struct iscsi_conn *conn, |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 575 | struct iscsi_task *task) |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 576 | { |
| 577 | struct iscsi_session *session = conn->session; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 578 | struct iscsi_hdr *hdr = task->hdr; |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 579 | struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr; |
| 580 | |
| 581 | if (conn->session->state == ISCSI_STATE_LOGGING_OUT) |
| 582 | return -ENOTCONN; |
| 583 | |
| 584 | if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) && |
| 585 | hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE)) |
| 586 | nop->exp_statsn = cpu_to_be32(conn->exp_statsn); |
| 587 | /* |
| 588 | * pre-format CmdSN for outgoing PDU. |
| 589 | */ |
| 590 | nop->cmdsn = cpu_to_be32(session->cmdsn); |
| 591 | if (hdr->itt != RESERVED_ITT) { |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 592 | /* |
| 593 | * TODO: We always use immediate, so we never hit this. |
| 594 | * If we start to send tmfs or nops as non-immediate then |
| 595 | * we should start checking the cmdsn numbers for mgmt tasks. |
| 596 | */ |
| 597 | if (conn->c_stage == ISCSI_CONN_STARTED && |
| 598 | !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { |
| 599 | session->queued_cmdsn++; |
| 600 | session->cmdsn++; |
| 601 | } |
| 602 | } |
| 603 | |
Mike Christie | ae15f80 | 2008-12-02 00:32:15 -0600 | [diff] [blame] | 604 | if (session->tt->init_task && session->tt->init_task(task)) |
| 605 | return -EIO; |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 606 | |
| 607 | if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) |
| 608 | session->state = ISCSI_STATE_LOGGING_OUT; |
| 609 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 610 | task->state = ISCSI_TASK_RUNNING; |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 611 | ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x " |
| 612 | "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK, |
| 613 | hdr->itt, task->data_count); |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 614 | return 0; |
| 615 | } |
| 616 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 617 | static struct iscsi_task * |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 618 | __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
| 619 | char *data, uint32_t data_size) |
| 620 | { |
| 621 | struct iscsi_session *session = conn->session; |
Mike Christie | 1336aed | 2009-05-13 17:57:48 -0500 | [diff] [blame] | 622 | struct iscsi_host *ihost = shost_priv(session->host); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 623 | struct iscsi_task *task; |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 624 | itt_t itt; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 625 | |
| 626 | if (session->state == ISCSI_STATE_TERMINATE) |
| 627 | return NULL; |
| 628 | |
| 629 | if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) || |
| 630 | hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE)) |
| 631 | /* |
| 632 | * Login and Text are sent serially, in |
| 633 | * request-followed-by-response sequence. |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 634 | * Same task can be used. Same ITT must be used. |
| 635 | * Note that login_task is preallocated at conn_create(). |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 636 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 637 | task = conn->login_task; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 638 | else { |
Mike Christie | 26013ad | 2009-05-13 17:57:43 -0500 | [diff] [blame] | 639 | if (session->state != ISCSI_STATE_LOGGED_IN) |
| 640 | return NULL; |
| 641 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 642 | BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); |
| 643 | BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); |
| 644 | |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 645 | if (!__kfifo_get(session->cmdpool.queue, |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 646 | (void*)&task, sizeof(void*))) |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 647 | return NULL; |
| 648 | } |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 649 | /* |
| 650 | * released in complete pdu for task we expect a response for, and |
| 651 | * released by the lld when it has transmitted the task for |
| 652 | * pdus we do not expect a response for. |
| 653 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 654 | atomic_set(&task->refcount, 1); |
| 655 | task->conn = conn; |
| 656 | task->sc = NULL; |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 657 | INIT_LIST_HEAD(&task->running); |
| 658 | task->state = ISCSI_TASK_PENDING; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 659 | |
| 660 | if (data_size) { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 661 | memcpy(task->data, data, data_size); |
| 662 | task->data_count = data_size; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 663 | } else |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 664 | task->data_count = 0; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 665 | |
Mike Christie | 184b57c | 2009-05-13 17:57:39 -0500 | [diff] [blame] | 666 | if (conn->session->tt->alloc_pdu) { |
| 667 | if (conn->session->tt->alloc_pdu(task, hdr->opcode)) { |
| 668 | iscsi_conn_printk(KERN_ERR, conn, "Could not allocate " |
| 669 | "pdu for mgmt task.\n"); |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 670 | goto free_task; |
Mike Christie | 184b57c | 2009-05-13 17:57:39 -0500 | [diff] [blame] | 671 | } |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 672 | } |
Mike Christie | 184b57c | 2009-05-13 17:57:39 -0500 | [diff] [blame] | 673 | |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 674 | itt = task->hdr->itt; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 675 | task->hdr_len = sizeof(struct iscsi_hdr); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 676 | memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr)); |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 677 | |
| 678 | if (hdr->itt != RESERVED_ITT) { |
| 679 | if (session->tt->parse_pdu_itt) |
| 680 | task->hdr->itt = itt; |
| 681 | else |
| 682 | task->hdr->itt = build_itt(task->itt, |
| 683 | task->conn->session->age); |
| 684 | } |
| 685 | |
Mike Christie | 1336aed | 2009-05-13 17:57:48 -0500 | [diff] [blame] | 686 | if (!ihost->workq) { |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 687 | if (iscsi_prep_mgmt_task(conn, task)) |
| 688 | goto free_task; |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 689 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 690 | if (session->tt->xmit_task(task)) |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 691 | goto free_task; |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 692 | } else { |
| 693 | list_add_tail(&task->running, &conn->mgmtqueue); |
Mike Christie | 32ae763 | 2009-03-05 14:46:03 -0600 | [diff] [blame] | 694 | iscsi_conn_queue_work(conn); |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 695 | } |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 696 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 697 | return task; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 698 | |
| 699 | free_task: |
| 700 | __iscsi_put_task(task); |
| 701 | return NULL; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr, |
| 705 | char *data, uint32_t data_size) |
| 706 | { |
| 707 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 708 | struct iscsi_session *session = conn->session; |
| 709 | int err = 0; |
| 710 | |
| 711 | spin_lock_bh(&session->lock); |
| 712 | if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size)) |
| 713 | err = -EPERM; |
| 714 | spin_unlock_bh(&session->lock); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 715 | return err; |
| 716 | } |
| 717 | EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu); |
| 718 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 719 | /** |
| 720 | * iscsi_cmd_rsp - SCSI Command Response processing |
| 721 | * @conn: iscsi connection |
| 722 | * @hdr: iscsi header |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 723 | * @task: scsi command task |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 724 | * @data: cmd data buffer |
| 725 | * @datalen: len of buffer |
| 726 | * |
| 727 | * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 728 | * then completes the command and task. |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 729 | **/ |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 730 | static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 731 | struct iscsi_task *task, char *data, |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 732 | int datalen) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 733 | { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 734 | struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr; |
| 735 | struct iscsi_session *session = conn->session; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 736 | struct scsi_cmnd *sc = task->sc; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 737 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 738 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 739 | conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; |
| 740 | |
| 741 | sc->result = (DID_OK << 16) | rhdr->cmd_status; |
| 742 | |
| 743 | if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) { |
| 744 | sc->result = DID_ERROR << 16; |
| 745 | goto out; |
| 746 | } |
| 747 | |
| 748 | if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) { |
Mike Christie | 9b80cb4 | 2006-12-17 12:10:28 -0600 | [diff] [blame] | 749 | uint16_t senselen; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 750 | |
| 751 | if (datalen < 2) { |
| 752 | invalid_datalen: |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 753 | iscsi_conn_printk(KERN_ERR, conn, |
| 754 | "Got CHECK_CONDITION but invalid data " |
| 755 | "buffer size of %d\n", datalen); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 756 | sc->result = DID_BAD_TARGET << 16; |
| 757 | goto out; |
| 758 | } |
| 759 | |
Harvey Harrison | 8f333991 | 2008-05-21 15:54:20 -0500 | [diff] [blame] | 760 | senselen = get_unaligned_be16(data); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 761 | if (datalen < senselen) |
| 762 | goto invalid_datalen; |
| 763 | |
| 764 | memcpy(sc->sense_buffer, data + 2, |
Mike Christie | 9b80cb4 | 2006-12-17 12:10:28 -0600 | [diff] [blame] | 765 | min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE)); |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 766 | ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n", |
| 767 | min_t(uint16_t, senselen, |
| 768 | SCSI_SENSE_BUFFERSIZE)); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 769 | } |
| 770 | |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 771 | if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW | |
| 772 | ISCSI_FLAG_CMD_BIDI_OVERFLOW)) { |
| 773 | int res_count = be32_to_cpu(rhdr->bi_residual_count); |
| 774 | |
| 775 | if (scsi_bidi_cmnd(sc) && res_count > 0 && |
| 776 | (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW || |
| 777 | res_count <= scsi_in(sc)->length)) |
| 778 | scsi_in(sc)->resid = res_count; |
| 779 | else |
| 780 | sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; |
| 781 | } |
| 782 | |
Boaz Harrosh | 7207fea | 2007-12-13 12:43:22 -0600 | [diff] [blame] | 783 | if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW | |
| 784 | ISCSI_FLAG_CMD_OVERFLOW)) { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 785 | int res_count = be32_to_cpu(rhdr->residual_count); |
| 786 | |
Boaz Harrosh | 7207fea | 2007-12-13 12:43:22 -0600 | [diff] [blame] | 787 | if (res_count > 0 && |
| 788 | (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || |
| 789 | res_count <= scsi_bufflen(sc))) |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 790 | /* write side for bidi or uni-io set_resid */ |
FUJITA Tomonori | 1c13899 | 2007-06-14 22:13:17 +0900 | [diff] [blame] | 791 | scsi_set_resid(sc, res_count); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 792 | else |
| 793 | sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 794 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 795 | out: |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 796 | ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n", |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 797 | sc, sc->result, task->itt); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 798 | conn->scsirsp_pdus_cnt++; |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 799 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 800 | } |
| 801 | |
Mike Christie | 1d9edf0 | 2008-09-24 11:46:09 -0500 | [diff] [blame] | 802 | /** |
| 803 | * iscsi_data_in_rsp - SCSI Data-In Response processing |
| 804 | * @conn: iscsi connection |
| 805 | * @hdr: iscsi pdu |
| 806 | * @task: scsi command task |
| 807 | **/ |
| 808 | static void |
| 809 | iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
| 810 | struct iscsi_task *task) |
| 811 | { |
| 812 | struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr; |
| 813 | struct scsi_cmnd *sc = task->sc; |
| 814 | |
| 815 | if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS)) |
| 816 | return; |
| 817 | |
Mike Christie | edbc9aa05 | 2009-05-13 17:57:42 -0500 | [diff] [blame] | 818 | iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr); |
Mike Christie | 1d9edf0 | 2008-09-24 11:46:09 -0500 | [diff] [blame] | 819 | sc->result = (DID_OK << 16) | rhdr->cmd_status; |
| 820 | conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; |
| 821 | if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW | |
| 822 | ISCSI_FLAG_DATA_OVERFLOW)) { |
| 823 | int res_count = be32_to_cpu(rhdr->residual_count); |
| 824 | |
| 825 | if (res_count > 0 && |
| 826 | (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || |
| 827 | res_count <= scsi_in(sc)->length)) |
| 828 | scsi_in(sc)->resid = res_count; |
| 829 | else |
| 830 | sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; |
| 831 | } |
| 832 | |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 833 | ISCSI_DBG_SESSION(conn->session, "data in with status done " |
| 834 | "[sc %p res %d itt 0x%x]\n", |
| 835 | sc, sc->result, task->itt); |
Mike Christie | 1d9edf0 | 2008-09-24 11:46:09 -0500 | [diff] [blame] | 836 | conn->scsirsp_pdus_cnt++; |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 837 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
Mike Christie | 1d9edf0 | 2008-09-24 11:46:09 -0500 | [diff] [blame] | 838 | } |
| 839 | |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 840 | static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr) |
| 841 | { |
| 842 | struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr; |
| 843 | |
| 844 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; |
| 845 | conn->tmfrsp_pdus_cnt++; |
| 846 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 847 | if (conn->tmf_state != TMF_QUEUED) |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 848 | return; |
| 849 | |
| 850 | if (tmf->response == ISCSI_TMF_RSP_COMPLETE) |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 851 | conn->tmf_state = TMF_SUCCESS; |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 852 | else if (tmf->response == ISCSI_TMF_RSP_NO_TASK) |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 853 | conn->tmf_state = TMF_NOT_FOUND; |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 854 | else |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 855 | conn->tmf_state = TMF_FAILED; |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 856 | wake_up(&conn->ehwait); |
| 857 | } |
| 858 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 859 | static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr) |
| 860 | { |
| 861 | struct iscsi_nopout hdr; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 862 | struct iscsi_task *task; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 863 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 864 | if (!rhdr && conn->ping_task) |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 865 | return; |
| 866 | |
| 867 | memset(&hdr, 0, sizeof(struct iscsi_nopout)); |
| 868 | hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE; |
| 869 | hdr.flags = ISCSI_FLAG_CMD_FINAL; |
| 870 | |
| 871 | if (rhdr) { |
| 872 | memcpy(hdr.lun, rhdr->lun, 8); |
| 873 | hdr.ttt = rhdr->ttt; |
| 874 | hdr.itt = RESERVED_ITT; |
| 875 | } else |
| 876 | hdr.ttt = RESERVED_ITT; |
| 877 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 878 | task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0); |
| 879 | if (!task) |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 880 | iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n"); |
Mike Christie | d3acf02 | 2008-12-01 12:13:00 -0600 | [diff] [blame] | 881 | else if (!rhdr) { |
| 882 | /* only track our nops */ |
| 883 | conn->ping_task = task; |
| 884 | conn->last_ping = jiffies; |
| 885 | } |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 886 | } |
| 887 | |
Mike Christie | 8afa143 | 2009-08-20 15:10:59 -0500 | [diff] [blame] | 888 | static int iscsi_nop_out_rsp(struct iscsi_task *task, |
| 889 | struct iscsi_nopin *nop, char *data, int datalen) |
| 890 | { |
| 891 | struct iscsi_conn *conn = task->conn; |
| 892 | int rc = 0; |
| 893 | |
| 894 | if (conn->ping_task != task) { |
| 895 | /* |
| 896 | * If this is not in response to one of our |
| 897 | * nops then it must be from userspace. |
| 898 | */ |
| 899 | if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop, |
| 900 | data, datalen)) |
| 901 | rc = ISCSI_ERR_CONN_FAILED; |
| 902 | } else |
| 903 | mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout); |
| 904 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
| 905 | return rc; |
| 906 | } |
| 907 | |
Mike Christie | 62f3830 | 2006-08-31 18:09:27 -0400 | [diff] [blame] | 908 | static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
| 909 | char *data, int datalen) |
| 910 | { |
| 911 | struct iscsi_reject *reject = (struct iscsi_reject *)hdr; |
| 912 | struct iscsi_hdr rejected_pdu; |
Mike Christie | 8afa143 | 2009-08-20 15:10:59 -0500 | [diff] [blame] | 913 | int opcode, rc = 0; |
Mike Christie | 62f3830 | 2006-08-31 18:09:27 -0400 | [diff] [blame] | 914 | |
| 915 | conn->exp_statsn = be32_to_cpu(reject->statsn) + 1; |
| 916 | |
Mike Christie | 8afa143 | 2009-08-20 15:10:59 -0500 | [diff] [blame] | 917 | if (ntoh24(reject->dlength) > datalen || |
| 918 | ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) { |
| 919 | iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected " |
| 920 | "pdu. Invalid data length (pdu dlength " |
| 921 | "%u, datalen %d\n", ntoh24(reject->dlength), |
| 922 | datalen); |
| 923 | return ISCSI_ERR_PROTO; |
Mike Christie | 62f3830 | 2006-08-31 18:09:27 -0400 | [diff] [blame] | 924 | } |
Mike Christie | 8afa143 | 2009-08-20 15:10:59 -0500 | [diff] [blame] | 925 | memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr)); |
| 926 | opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK; |
| 927 | |
| 928 | switch (reject->reason) { |
| 929 | case ISCSI_REASON_DATA_DIGEST_ERROR: |
| 930 | iscsi_conn_printk(KERN_ERR, conn, |
| 931 | "pdu (op 0x%x itt 0x%x) rejected " |
| 932 | "due to DataDigest error.\n", |
| 933 | rejected_pdu.itt, opcode); |
| 934 | break; |
| 935 | case ISCSI_REASON_IMM_CMD_REJECT: |
| 936 | iscsi_conn_printk(KERN_ERR, conn, |
| 937 | "pdu (op 0x%x itt 0x%x) rejected. Too many " |
| 938 | "immediate commands.\n", |
| 939 | rejected_pdu.itt, opcode); |
| 940 | /* |
| 941 | * We only send one TMF at a time so if the target could not |
| 942 | * handle it, then it should get fixed (RFC mandates that |
| 943 | * a target can handle one immediate TMF per conn). |
| 944 | * |
| 945 | * For nops-outs, we could have sent more than one if |
| 946 | * the target is sending us lots of nop-ins |
| 947 | */ |
| 948 | if (opcode != ISCSI_OP_NOOP_OUT) |
| 949 | return 0; |
| 950 | |
| 951 | if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) |
| 952 | /* |
| 953 | * nop-out in response to target's nop-out rejected. |
| 954 | * Just resend. |
| 955 | */ |
| 956 | iscsi_send_nopout(conn, |
| 957 | (struct iscsi_nopin*)&rejected_pdu); |
| 958 | else { |
| 959 | struct iscsi_task *task; |
| 960 | /* |
| 961 | * Our nop as ping got dropped. We know the target |
| 962 | * and transport are ok so just clean up |
| 963 | */ |
| 964 | task = iscsi_itt_to_task(conn, rejected_pdu.itt); |
| 965 | if (!task) { |
| 966 | iscsi_conn_printk(KERN_ERR, conn, |
| 967 | "Invalid pdu reject. Could " |
| 968 | "not lookup rejected task.\n"); |
| 969 | rc = ISCSI_ERR_BAD_ITT; |
| 970 | } else |
| 971 | rc = iscsi_nop_out_rsp(task, |
| 972 | (struct iscsi_nopin*)&rejected_pdu, |
| 973 | NULL, 0); |
| 974 | } |
| 975 | break; |
| 976 | default: |
| 977 | iscsi_conn_printk(KERN_ERR, conn, |
| 978 | "pdu (op 0x%x itt 0x%x) rejected. Reason " |
| 979 | "code 0x%x\n", rejected_pdu.itt, |
| 980 | rejected_pdu.opcode, reject->reason); |
| 981 | break; |
| 982 | } |
| 983 | return rc; |
Mike Christie | 62f3830 | 2006-08-31 18:09:27 -0400 | [diff] [blame] | 984 | } |
| 985 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 986 | /** |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 987 | * iscsi_itt_to_task - look up task by itt |
| 988 | * @conn: iscsi connection |
| 989 | * @itt: itt |
| 990 | * |
| 991 | * This should be used for mgmt tasks like login and nops, or if |
| 992 | * the LDD's itt space does not include the session age. |
| 993 | * |
| 994 | * The session lock must be held. |
| 995 | */ |
Mike Christie | 8f9256c | 2009-05-13 17:57:41 -0500 | [diff] [blame] | 996 | struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt) |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 997 | { |
| 998 | struct iscsi_session *session = conn->session; |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 999 | int i; |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1000 | |
| 1001 | if (itt == RESERVED_ITT) |
| 1002 | return NULL; |
| 1003 | |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 1004 | if (session->tt->parse_pdu_itt) |
| 1005 | session->tt->parse_pdu_itt(conn, itt, &i, NULL); |
| 1006 | else |
| 1007 | i = get_itt(itt); |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1008 | if (i >= session->cmds_max) |
| 1009 | return NULL; |
| 1010 | |
| 1011 | return session->cmds[i]; |
| 1012 | } |
Mike Christie | 8f9256c | 2009-05-13 17:57:41 -0500 | [diff] [blame] | 1013 | EXPORT_SYMBOL_GPL(iscsi_itt_to_task); |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1014 | |
| 1015 | /** |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1016 | * __iscsi_complete_pdu - complete pdu |
| 1017 | * @conn: iscsi conn |
| 1018 | * @hdr: iscsi header |
| 1019 | * @data: data buffer |
| 1020 | * @datalen: len of data buffer |
| 1021 | * |
| 1022 | * Completes pdu processing by freeing any resources allocated at |
| 1023 | * queuecommand or send generic. session lock must be held and verify |
| 1024 | * itt must have been called. |
| 1025 | */ |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1026 | int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
| 1027 | char *data, int datalen) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1028 | { |
| 1029 | struct iscsi_session *session = conn->session; |
| 1030 | int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1031 | struct iscsi_task *task; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1032 | uint32_t itt; |
| 1033 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1034 | conn->last_recv = jiffies; |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1035 | rc = iscsi_verify_itt(conn, hdr->itt); |
| 1036 | if (rc) |
| 1037 | return rc; |
| 1038 | |
Al Viro | b437735 | 2007-02-09 16:39:40 +0000 | [diff] [blame] | 1039 | if (hdr->itt != RESERVED_ITT) |
| 1040 | itt = get_itt(hdr->itt); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1041 | else |
Al Viro | b437735 | 2007-02-09 16:39:40 +0000 | [diff] [blame] | 1042 | itt = ~0U; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1043 | |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 1044 | ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n", |
| 1045 | opcode, conn->id, itt, datalen); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1046 | |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 1047 | if (itt == ~0U) { |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1048 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); |
Mike Christie | 62f3830 | 2006-08-31 18:09:27 -0400 | [diff] [blame] | 1049 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1050 | switch(opcode) { |
| 1051 | case ISCSI_OP_NOOP_IN: |
Mike Christie | 40527af | 2006-07-24 15:47:45 -0500 | [diff] [blame] | 1052 | if (datalen) { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1053 | rc = ISCSI_ERR_PROTO; |
Mike Christie | 40527af | 2006-07-24 15:47:45 -0500 | [diff] [blame] | 1054 | break; |
| 1055 | } |
| 1056 | |
Al Viro | b437735 | 2007-02-09 16:39:40 +0000 | [diff] [blame] | 1057 | if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG)) |
Mike Christie | 40527af | 2006-07-24 15:47:45 -0500 | [diff] [blame] | 1058 | break; |
| 1059 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1060 | iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1061 | break; |
| 1062 | case ISCSI_OP_REJECT: |
Mike Christie | 62f3830 | 2006-08-31 18:09:27 -0400 | [diff] [blame] | 1063 | rc = iscsi_handle_reject(conn, hdr, data, datalen); |
| 1064 | break; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1065 | case ISCSI_OP_ASYNC_EVENT: |
Mike Christie | 8d2860b | 2006-05-02 19:46:47 -0500 | [diff] [blame] | 1066 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; |
Mike Christie | 5831c73 | 2006-10-16 18:09:41 -0400 | [diff] [blame] | 1067 | if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) |
| 1068 | rc = ISCSI_ERR_CONN_FAILED; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1069 | break; |
| 1070 | default: |
| 1071 | rc = ISCSI_ERR_BAD_OPCODE; |
| 1072 | break; |
| 1073 | } |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 1074 | goto out; |
| 1075 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1076 | |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 1077 | switch(opcode) { |
| 1078 | case ISCSI_OP_SCSI_CMD_RSP: |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1079 | case ISCSI_OP_SCSI_DATA_IN: |
| 1080 | task = iscsi_itt_to_ctask(conn, hdr->itt); |
| 1081 | if (!task) |
| 1082 | return ISCSI_ERR_BAD_ITT; |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1083 | task->last_xfer = jiffies; |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1084 | break; |
| 1085 | case ISCSI_OP_R2T: |
| 1086 | /* |
| 1087 | * LLD handles R2Ts if they need to. |
| 1088 | */ |
| 1089 | return 0; |
| 1090 | case ISCSI_OP_LOGOUT_RSP: |
| 1091 | case ISCSI_OP_LOGIN_RSP: |
| 1092 | case ISCSI_OP_TEXT_RSP: |
| 1093 | case ISCSI_OP_SCSI_TMFUNC_RSP: |
| 1094 | case ISCSI_OP_NOOP_IN: |
| 1095 | task = iscsi_itt_to_task(conn, hdr->itt); |
| 1096 | if (!task) |
| 1097 | return ISCSI_ERR_BAD_ITT; |
| 1098 | break; |
| 1099 | default: |
| 1100 | return ISCSI_ERR_BAD_OPCODE; |
| 1101 | } |
| 1102 | |
| 1103 | switch(opcode) { |
| 1104 | case ISCSI_OP_SCSI_CMD_RSP: |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1105 | iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen); |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 1106 | break; |
| 1107 | case ISCSI_OP_SCSI_DATA_IN: |
Mike Christie | 1d9edf0 | 2008-09-24 11:46:09 -0500 | [diff] [blame] | 1108 | iscsi_data_in_rsp(conn, hdr, task); |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 1109 | break; |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 1110 | case ISCSI_OP_LOGOUT_RSP: |
| 1111 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); |
| 1112 | if (datalen) { |
| 1113 | rc = ISCSI_ERR_PROTO; |
| 1114 | break; |
| 1115 | } |
| 1116 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; |
| 1117 | goto recv_pdu; |
| 1118 | case ISCSI_OP_LOGIN_RSP: |
| 1119 | case ISCSI_OP_TEXT_RSP: |
| 1120 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); |
| 1121 | /* |
| 1122 | * login related PDU's exp_statsn is handled in |
| 1123 | * userspace |
| 1124 | */ |
| 1125 | goto recv_pdu; |
| 1126 | case ISCSI_OP_SCSI_TMFUNC_RSP: |
| 1127 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); |
| 1128 | if (datalen) { |
| 1129 | rc = ISCSI_ERR_PROTO; |
| 1130 | break; |
| 1131 | } |
| 1132 | |
| 1133 | iscsi_tmf_rsp(conn, hdr); |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 1134 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 1135 | break; |
| 1136 | case ISCSI_OP_NOOP_IN: |
| 1137 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); |
| 1138 | if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) { |
| 1139 | rc = ISCSI_ERR_PROTO; |
| 1140 | break; |
| 1141 | } |
| 1142 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; |
| 1143 | |
Mike Christie | 8afa143 | 2009-08-20 15:10:59 -0500 | [diff] [blame] | 1144 | rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr, |
| 1145 | data, datalen); |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 1146 | break; |
| 1147 | default: |
| 1148 | rc = ISCSI_ERR_BAD_OPCODE; |
| 1149 | break; |
| 1150 | } |
| 1151 | |
| 1152 | out: |
| 1153 | return rc; |
| 1154 | recv_pdu: |
| 1155 | if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) |
| 1156 | rc = ISCSI_ERR_CONN_FAILED; |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 1157 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1158 | return rc; |
| 1159 | } |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1160 | EXPORT_SYMBOL_GPL(__iscsi_complete_pdu); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1161 | |
| 1162 | int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
| 1163 | char *data, int datalen) |
| 1164 | { |
| 1165 | int rc; |
| 1166 | |
| 1167 | spin_lock(&conn->session->lock); |
| 1168 | rc = __iscsi_complete_pdu(conn, hdr, data, datalen); |
| 1169 | spin_unlock(&conn->session->lock); |
| 1170 | return rc; |
| 1171 | } |
| 1172 | EXPORT_SYMBOL_GPL(iscsi_complete_pdu); |
| 1173 | |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1174 | int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1175 | { |
| 1176 | struct iscsi_session *session = conn->session; |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 1177 | int age = 0, i = 0; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1178 | |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1179 | if (itt == RESERVED_ITT) |
| 1180 | return 0; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1181 | |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 1182 | if (session->tt->parse_pdu_itt) |
| 1183 | session->tt->parse_pdu_itt(conn, itt, &i, &age); |
| 1184 | else { |
| 1185 | i = get_itt(itt); |
| 1186 | age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK; |
| 1187 | } |
| 1188 | |
| 1189 | if (age != session->age) { |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1190 | iscsi_conn_printk(KERN_ERR, conn, |
| 1191 | "received itt %x expected session age (%x)\n", |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1192 | (__force u32)itt, session->age); |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1193 | return ISCSI_ERR_BAD_ITT; |
| 1194 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1195 | |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 1196 | if (i >= session->cmds_max) { |
| 1197 | iscsi_conn_printk(KERN_ERR, conn, |
| 1198 | "received invalid itt index %u (max cmds " |
| 1199 | "%u.\n", i, session->cmds_max); |
| 1200 | return ISCSI_ERR_BAD_ITT; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1201 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1202 | return 0; |
| 1203 | } |
| 1204 | EXPORT_SYMBOL_GPL(iscsi_verify_itt); |
| 1205 | |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1206 | /** |
| 1207 | * iscsi_itt_to_ctask - look up ctask by itt |
| 1208 | * @conn: iscsi connection |
| 1209 | * @itt: itt |
| 1210 | * |
| 1211 | * This should be used for cmd tasks. |
| 1212 | * |
| 1213 | * The session lock must be held. |
| 1214 | */ |
| 1215 | struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt) |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1216 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1217 | struct iscsi_task *task; |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1218 | |
| 1219 | if (iscsi_verify_itt(conn, itt)) |
| 1220 | return NULL; |
| 1221 | |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1222 | task = iscsi_itt_to_task(conn, itt); |
| 1223 | if (!task || !task->sc) |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1224 | return NULL; |
| 1225 | |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1226 | if (task->sc->SCp.phase != conn->session->age) { |
| 1227 | iscsi_session_printk(KERN_ERR, conn->session, |
| 1228 | "task's session age %d, expected %d\n", |
| 1229 | task->sc->SCp.phase, conn->session->age); |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1230 | return NULL; |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1231 | } |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1232 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1233 | return task; |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1234 | } |
| 1235 | EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask); |
| 1236 | |
Mike Christie | 40a06e7 | 2009-03-05 14:46:05 -0600 | [diff] [blame] | 1237 | void iscsi_session_failure(struct iscsi_session *session, |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 1238 | enum iscsi_err err) |
| 1239 | { |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 1240 | struct iscsi_conn *conn; |
| 1241 | struct device *dev; |
| 1242 | unsigned long flags; |
| 1243 | |
| 1244 | spin_lock_irqsave(&session->lock, flags); |
| 1245 | conn = session->leadconn; |
| 1246 | if (session->state == ISCSI_STATE_TERMINATE || !conn) { |
| 1247 | spin_unlock_irqrestore(&session->lock, flags); |
| 1248 | return; |
| 1249 | } |
| 1250 | |
| 1251 | dev = get_device(&conn->cls_conn->dev); |
| 1252 | spin_unlock_irqrestore(&session->lock, flags); |
| 1253 | if (!dev) |
| 1254 | return; |
| 1255 | /* |
| 1256 | * if the host is being removed bypass the connection |
| 1257 | * recovery initialization because we are going to kill |
| 1258 | * the session. |
| 1259 | */ |
| 1260 | if (err == ISCSI_ERR_INVALID_HOST) |
| 1261 | iscsi_conn_error_event(conn->cls_conn, err); |
| 1262 | else |
| 1263 | iscsi_conn_failure(conn, err); |
| 1264 | put_device(dev); |
| 1265 | } |
| 1266 | EXPORT_SYMBOL_GPL(iscsi_session_failure); |
| 1267 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1268 | void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err) |
| 1269 | { |
| 1270 | struct iscsi_session *session = conn->session; |
| 1271 | unsigned long flags; |
| 1272 | |
| 1273 | spin_lock_irqsave(&session->lock, flags); |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1274 | if (session->state == ISCSI_STATE_FAILED) { |
| 1275 | spin_unlock_irqrestore(&session->lock, flags); |
| 1276 | return; |
| 1277 | } |
| 1278 | |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 1279 | if (conn->stop_stage == 0) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1280 | session->state = ISCSI_STATE_FAILED; |
| 1281 | spin_unlock_irqrestore(&session->lock, flags); |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 1282 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1283 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
| 1284 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 1285 | iscsi_conn_error_event(conn->cls_conn, err); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1286 | } |
| 1287 | EXPORT_SYMBOL_GPL(iscsi_conn_failure); |
| 1288 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1289 | static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn) |
| 1290 | { |
| 1291 | struct iscsi_session *session = conn->session; |
| 1292 | |
| 1293 | /* |
| 1294 | * Check for iSCSI window and take care of CmdSN wrap-around |
| 1295 | */ |
Mike Christie | e072640 | 2007-07-26 12:46:48 -0500 | [diff] [blame] | 1296 | if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) { |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 1297 | ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn " |
| 1298 | "%u MaxCmdSN %u CmdSN %u/%u\n", |
| 1299 | session->exp_cmdsn, session->max_cmdsn, |
| 1300 | session->cmdsn, session->queued_cmdsn); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1301 | return -ENOSPC; |
| 1302 | } |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1306 | static int iscsi_xmit_task(struct iscsi_conn *conn) |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1307 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1308 | struct iscsi_task *task = conn->task; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1309 | int rc; |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1310 | |
Mike Christie | 70b31c1 | 2009-08-20 15:11:03 -0500 | [diff] [blame] | 1311 | if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) |
| 1312 | return -ENODATA; |
| 1313 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1314 | __iscsi_get_task(task); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1315 | spin_unlock_bh(&conn->session->lock); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1316 | rc = conn->session->tt->xmit_task(task); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1317 | spin_lock_bh(&conn->session->lock); |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1318 | if (!rc) { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1319 | /* done with this task */ |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1320 | task->last_xfer = jiffies; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1321 | conn->task = NULL; |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1322 | } |
| 1323 | __iscsi_put_task(task); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1324 | return rc; |
| 1325 | } |
| 1326 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1327 | /** |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1328 | * iscsi_requeue_task - requeue task to run from session workqueue |
| 1329 | * @task: task to requeue |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1330 | * |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1331 | * LLDs that need to run a task from the session workqueue should call |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 1332 | * this. The session lock must be held. This should only be called |
| 1333 | * by software drivers. |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1334 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1335 | void iscsi_requeue_task(struct iscsi_task *task) |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1336 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1337 | struct iscsi_conn *conn = task->conn; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1338 | |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 1339 | /* |
| 1340 | * this may be on the requeue list already if the xmit_task callout |
| 1341 | * is handling the r2ts while we are adding new ones |
| 1342 | */ |
| 1343 | if (list_empty(&task->running)) |
| 1344 | list_add_tail(&task->running, &conn->requeue); |
Mike Christie | 32ae763 | 2009-03-05 14:46:03 -0600 | [diff] [blame] | 1345 | iscsi_conn_queue_work(conn); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1346 | } |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1347 | EXPORT_SYMBOL_GPL(iscsi_requeue_task); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1348 | |
| 1349 | /** |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1350 | * iscsi_data_xmit - xmit any command into the scheduled connection |
| 1351 | * @conn: iscsi connection |
| 1352 | * |
| 1353 | * Notes: |
| 1354 | * The function can return -EAGAIN in which case the caller must |
| 1355 | * re-schedule it again later or recover. '0' return code means |
| 1356 | * successful xmit. |
| 1357 | **/ |
| 1358 | static int iscsi_data_xmit(struct iscsi_conn *conn) |
| 1359 | { |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1360 | int rc = 0; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1361 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1362 | spin_lock_bh(&conn->session->lock); |
Mike Christie | 70b31c1 | 2009-08-20 15:11:03 -0500 | [diff] [blame] | 1363 | if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) { |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 1364 | ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n"); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1365 | spin_unlock_bh(&conn->session->lock); |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1366 | return -ENODATA; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1367 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1368 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1369 | if (conn->task) { |
| 1370 | rc = iscsi_xmit_task(conn); |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1371 | if (rc) |
Mike Christie | 70b31c1 | 2009-08-20 15:11:03 -0500 | [diff] [blame] | 1372 | goto done; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1373 | } |
| 1374 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1375 | /* |
| 1376 | * process mgmt pdus like nops before commands since we should |
| 1377 | * only have one nop-out as a ping from us and targets should not |
| 1378 | * overflow us with nop-ins |
| 1379 | */ |
| 1380 | check_mgmt: |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1381 | while (!list_empty(&conn->mgmtqueue)) { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1382 | conn->task = list_entry(conn->mgmtqueue.next, |
| 1383 | struct iscsi_task, running); |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 1384 | list_del_init(&conn->task->running); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1385 | if (iscsi_prep_mgmt_task(conn, conn->task)) { |
| 1386 | __iscsi_put_task(conn->task); |
| 1387 | conn->task = NULL; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1388 | continue; |
| 1389 | } |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1390 | rc = iscsi_xmit_task(conn); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1391 | if (rc) |
Mike Christie | 70b31c1 | 2009-08-20 15:11:03 -0500 | [diff] [blame] | 1392 | goto done; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1393 | } |
| 1394 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1395 | /* process pending command queue */ |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 1396 | while (!list_empty(&conn->cmdqueue)) { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1397 | if (conn->tmf_state == TMF_QUEUED) |
| 1398 | break; |
| 1399 | |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 1400 | conn->task = list_entry(conn->cmdqueue.next, |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1401 | struct iscsi_task, running); |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 1402 | list_del_init(&conn->task->running); |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1403 | if (conn->session->state == ISCSI_STATE_LOGGING_OUT) { |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 1404 | fail_scsi_task(conn->task, DID_IMM_RETRY); |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1405 | continue; |
| 1406 | } |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1407 | rc = iscsi_prep_scsi_cmd_pdu(conn->task); |
| 1408 | if (rc) { |
| 1409 | if (rc == -ENOMEM) { |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 1410 | list_add_tail(&conn->task->running, |
| 1411 | &conn->cmdqueue); |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1412 | conn->task = NULL; |
Mike Christie | 70b31c1 | 2009-08-20 15:11:03 -0500 | [diff] [blame] | 1413 | goto done; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1414 | } else |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 1415 | fail_scsi_task(conn->task, DID_ABORT); |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 1416 | continue; |
| 1417 | } |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1418 | rc = iscsi_xmit_task(conn); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1419 | if (rc) |
Mike Christie | 70b31c1 | 2009-08-20 15:11:03 -0500 | [diff] [blame] | 1420 | goto done; |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1421 | /* |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1422 | * we could continuously get new task requests so |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1423 | * we need to check the mgmt queue for nops that need to |
| 1424 | * be sent to aviod starvation |
| 1425 | */ |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1426 | if (!list_empty(&conn->mgmtqueue)) |
| 1427 | goto check_mgmt; |
| 1428 | } |
| 1429 | |
| 1430 | while (!list_empty(&conn->requeue)) { |
| 1431 | if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL) |
| 1432 | break; |
| 1433 | |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1434 | /* |
| 1435 | * we always do fastlogout - conn stop code will clean up. |
| 1436 | */ |
| 1437 | if (conn->session->state == ISCSI_STATE_LOGGING_OUT) |
| 1438 | break; |
| 1439 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1440 | conn->task = list_entry(conn->requeue.next, |
| 1441 | struct iscsi_task, running); |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 1442 | list_del_init(&conn->task->running); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1443 | conn->task->state = ISCSI_TASK_RUNNING; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1444 | rc = iscsi_xmit_task(conn); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1445 | if (rc) |
Mike Christie | 70b31c1 | 2009-08-20 15:11:03 -0500 | [diff] [blame] | 1446 | goto done; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1447 | if (!list_empty(&conn->mgmtqueue)) |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1448 | goto check_mgmt; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1449 | } |
Mike Christie | b6c395e | 2006-07-24 15:47:15 -0500 | [diff] [blame] | 1450 | spin_unlock_bh(&conn->session->lock); |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1451 | return -ENODATA; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1452 | |
Mike Christie | 70b31c1 | 2009-08-20 15:11:03 -0500 | [diff] [blame] | 1453 | done: |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1454 | spin_unlock_bh(&conn->session->lock); |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1455 | return rc; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1456 | } |
| 1457 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1458 | static void iscsi_xmitworker(struct work_struct *work) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1459 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1460 | struct iscsi_conn *conn = |
| 1461 | container_of(work, struct iscsi_conn, xmitwork); |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1462 | int rc; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1463 | /* |
| 1464 | * serialize Xmit worker on a per-connection basis. |
| 1465 | */ |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1466 | do { |
| 1467 | rc = iscsi_data_xmit(conn); |
| 1468 | } while (rc >= 0 || rc == -EAGAIN); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1469 | } |
| 1470 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1471 | static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn, |
| 1472 | struct scsi_cmnd *sc) |
| 1473 | { |
| 1474 | struct iscsi_task *task; |
| 1475 | |
| 1476 | if (!__kfifo_get(conn->session->cmdpool.queue, |
| 1477 | (void *) &task, sizeof(void *))) |
| 1478 | return NULL; |
| 1479 | |
| 1480 | sc->SCp.phase = conn->session->age; |
| 1481 | sc->SCp.ptr = (char *) task; |
| 1482 | |
| 1483 | atomic_set(&task->refcount, 1); |
| 1484 | task->state = ISCSI_TASK_PENDING; |
| 1485 | task->conn = conn; |
| 1486 | task->sc = sc; |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1487 | task->have_checked_conn = false; |
| 1488 | task->last_timeout = jiffies; |
| 1489 | task->last_xfer = jiffies; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1490 | INIT_LIST_HEAD(&task->running); |
| 1491 | return task; |
| 1492 | } |
| 1493 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1494 | enum { |
| 1495 | FAILURE_BAD_HOST = 1, |
| 1496 | FAILURE_SESSION_FAILED, |
| 1497 | FAILURE_SESSION_FREED, |
| 1498 | FAILURE_WINDOW_CLOSED, |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 1499 | FAILURE_OOM, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1500 | FAILURE_SESSION_TERMINATE, |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1501 | FAILURE_SESSION_IN_RECOVERY, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1502 | FAILURE_SESSION_RECOVERY_TIMEOUT, |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1503 | FAILURE_SESSION_LOGGING_OUT, |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 1504 | FAILURE_SESSION_NOT_READY, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1505 | }; |
| 1506 | |
| 1507 | int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *)) |
| 1508 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1509 | struct iscsi_cls_session *cls_session; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1510 | struct Scsi_Host *host; |
Mike Christie | 1336aed | 2009-05-13 17:57:48 -0500 | [diff] [blame] | 1511 | struct iscsi_host *ihost; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1512 | int reason = 0; |
| 1513 | struct iscsi_session *session; |
| 1514 | struct iscsi_conn *conn; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1515 | struct iscsi_task *task = NULL; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1516 | |
| 1517 | sc->scsi_done = done; |
| 1518 | sc->result = 0; |
Mike Christie | f47f2cf | 2006-08-31 18:09:33 -0400 | [diff] [blame] | 1519 | sc->SCp.ptr = NULL; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1520 | |
| 1521 | host = sc->device->host; |
Mike Christie | 1336aed | 2009-05-13 17:57:48 -0500 | [diff] [blame] | 1522 | ihost = shost_priv(host); |
Mike Christie | 1040c99 | 2007-12-13 12:43:34 -0600 | [diff] [blame] | 1523 | spin_unlock(host->host_lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1524 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1525 | cls_session = starget_to_session(scsi_target(sc->device)); |
| 1526 | session = cls_session->dd_data; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1527 | spin_lock(&session->lock); |
| 1528 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1529 | reason = iscsi_session_chkready(cls_session); |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 1530 | if (reason) { |
| 1531 | sc->result = reason; |
| 1532 | goto fault; |
| 1533 | } |
| 1534 | |
Mike Christie | 301e0f7 | 2009-05-13 17:57:47 -0500 | [diff] [blame] | 1535 | if (session->state != ISCSI_STATE_LOGGED_IN) { |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1536 | /* |
| 1537 | * to handle the race between when we set the recovery state |
| 1538 | * and block the session we requeue here (commands could |
| 1539 | * be entering our queuecommand while a block is starting |
| 1540 | * up because the block code is not locked) |
| 1541 | */ |
Mike Christie | 9000bcd | 2007-12-13 12:43:32 -0600 | [diff] [blame] | 1542 | switch (session->state) { |
Mike Christie | 301e0f7 | 2009-05-13 17:57:47 -0500 | [diff] [blame] | 1543 | case ISCSI_STATE_FAILED: |
Mike Christie | 9000bcd | 2007-12-13 12:43:32 -0600 | [diff] [blame] | 1544 | case ISCSI_STATE_IN_RECOVERY: |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1545 | reason = FAILURE_SESSION_IN_RECOVERY; |
Mike Christie | 301e0f7 | 2009-05-13 17:57:47 -0500 | [diff] [blame] | 1546 | sc->result = DID_IMM_RETRY << 16; |
| 1547 | break; |
Mike Christie | 9000bcd | 2007-12-13 12:43:32 -0600 | [diff] [blame] | 1548 | case ISCSI_STATE_LOGGING_OUT: |
| 1549 | reason = FAILURE_SESSION_LOGGING_OUT; |
Mike Christie | 301e0f7 | 2009-05-13 17:57:47 -0500 | [diff] [blame] | 1550 | sc->result = DID_IMM_RETRY << 16; |
| 1551 | break; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1552 | case ISCSI_STATE_RECOVERY_FAILED: |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1553 | reason = FAILURE_SESSION_RECOVERY_TIMEOUT; |
Mike Christie | 56d7fcf | 2008-08-19 18:45:26 -0500 | [diff] [blame] | 1554 | sc->result = DID_TRANSPORT_FAILFAST << 16; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1555 | break; |
| 1556 | case ISCSI_STATE_TERMINATE: |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1557 | reason = FAILURE_SESSION_TERMINATE; |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 1558 | sc->result = DID_NO_CONNECT << 16; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1559 | break; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1560 | default: |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1561 | reason = FAILURE_SESSION_FREED; |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 1562 | sc->result = DID_NO_CONNECT << 16; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1563 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1564 | goto fault; |
| 1565 | } |
| 1566 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1567 | conn = session->leadconn; |
Mike Christie | 9864404 | 2006-10-16 18:09:39 -0400 | [diff] [blame] | 1568 | if (!conn) { |
| 1569 | reason = FAILURE_SESSION_FREED; |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 1570 | sc->result = DID_NO_CONNECT << 16; |
Mike Christie | 9864404 | 2006-10-16 18:09:39 -0400 | [diff] [blame] | 1571 | goto fault; |
| 1572 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1573 | |
Mike Christie | 661134a | 2009-09-05 07:35:33 +0530 | [diff] [blame] | 1574 | if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) { |
| 1575 | reason = FAILURE_SESSION_IN_RECOVERY; |
| 1576 | sc->result = DID_REQUEUE; |
| 1577 | goto fault; |
| 1578 | } |
| 1579 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1580 | if (iscsi_check_cmdsn_window_closed(conn)) { |
| 1581 | reason = FAILURE_WINDOW_CLOSED; |
| 1582 | goto reject; |
| 1583 | } |
| 1584 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1585 | task = iscsi_alloc_task(conn, sc); |
| 1586 | if (!task) { |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 1587 | reason = FAILURE_OOM; |
| 1588 | goto reject; |
| 1589 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1590 | |
Mike Christie | 1336aed | 2009-05-13 17:57:48 -0500 | [diff] [blame] | 1591 | if (!ihost->workq) { |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1592 | reason = iscsi_prep_scsi_cmd_pdu(task); |
| 1593 | if (reason) { |
| 1594 | if (reason == -ENOMEM) { |
| 1595 | reason = FAILURE_OOM; |
| 1596 | goto prepd_reject; |
| 1597 | } else { |
| 1598 | sc->result = DID_ABORT << 16; |
| 1599 | goto prepd_fault; |
| 1600 | } |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 1601 | } |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1602 | if (session->tt->xmit_task(task)) { |
Mike Christie | d3305f3 | 2009-08-20 15:10:58 -0500 | [diff] [blame] | 1603 | session->cmdsn--; |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 1604 | reason = FAILURE_SESSION_NOT_READY; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1605 | goto prepd_reject; |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 1606 | } |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 1607 | } else { |
| 1608 | list_add_tail(&task->running, &conn->cmdqueue); |
Mike Christie | 32ae763 | 2009-03-05 14:46:03 -0600 | [diff] [blame] | 1609 | iscsi_conn_queue_work(conn); |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 1610 | } |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 1611 | |
| 1612 | session->queued_cmdsn++; |
| 1613 | spin_unlock(&session->lock); |
Mike Christie | 1040c99 | 2007-12-13 12:43:34 -0600 | [diff] [blame] | 1614 | spin_lock(host->host_lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1615 | return 0; |
| 1616 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1617 | prepd_reject: |
| 1618 | sc->scsi_done = NULL; |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 1619 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1620 | reject: |
| 1621 | spin_unlock(&session->lock); |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 1622 | ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n", |
| 1623 | sc->cmnd[0], reason); |
Mike Christie | 1040c99 | 2007-12-13 12:43:34 -0600 | [diff] [blame] | 1624 | spin_lock(host->host_lock); |
Mike Christie | d6d13ee | 2008-08-17 15:24:43 -0500 | [diff] [blame] | 1625 | return SCSI_MLQUEUE_TARGET_BUSY; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1626 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1627 | prepd_fault: |
| 1628 | sc->scsi_done = NULL; |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 1629 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1630 | fault: |
| 1631 | spin_unlock(&session->lock); |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 1632 | ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n", |
| 1633 | sc->cmnd[0], reason); |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 1634 | if (!scsi_bidi_cmnd(sc)) |
| 1635 | scsi_set_resid(sc, scsi_bufflen(sc)); |
| 1636 | else { |
| 1637 | scsi_out(sc)->resid = scsi_out(sc)->length; |
| 1638 | scsi_in(sc)->resid = scsi_in(sc)->length; |
| 1639 | } |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 1640 | done(sc); |
Mike Christie | 1040c99 | 2007-12-13 12:43:34 -0600 | [diff] [blame] | 1641 | spin_lock(host->host_lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1642 | return 0; |
| 1643 | } |
| 1644 | EXPORT_SYMBOL_GPL(iscsi_queuecommand); |
| 1645 | |
| 1646 | int iscsi_change_queue_depth(struct scsi_device *sdev, int depth) |
| 1647 | { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1648 | scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); |
| 1649 | return sdev->queue_depth; |
| 1650 | } |
| 1651 | EXPORT_SYMBOL_GPL(iscsi_change_queue_depth); |
| 1652 | |
Mike Christie | 6b5d6c4 | 2009-04-21 15:32:32 -0500 | [diff] [blame] | 1653 | int iscsi_target_alloc(struct scsi_target *starget) |
| 1654 | { |
| 1655 | struct iscsi_cls_session *cls_session = starget_to_session(starget); |
| 1656 | struct iscsi_session *session = cls_session->dd_data; |
| 1657 | |
| 1658 | starget->can_queue = session->scsi_cmds_max; |
| 1659 | return 0; |
| 1660 | } |
| 1661 | EXPORT_SYMBOL_GPL(iscsi_target_alloc); |
| 1662 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1663 | void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session) |
| 1664 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1665 | struct iscsi_session *session = cls_session->dd_data; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1666 | |
| 1667 | spin_lock_bh(&session->lock); |
| 1668 | if (session->state != ISCSI_STATE_LOGGED_IN) { |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1669 | session->state = ISCSI_STATE_RECOVERY_FAILED; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1670 | if (session->leadconn) |
| 1671 | wake_up(&session->leadconn->ehwait); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1672 | } |
| 1673 | spin_unlock_bh(&session->lock); |
| 1674 | } |
| 1675 | EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout); |
| 1676 | |
Mike Christie | 8e12452 | 2008-09-24 11:46:12 -0500 | [diff] [blame] | 1677 | int iscsi_eh_target_reset(struct scsi_cmnd *sc) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1678 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1679 | struct iscsi_cls_session *cls_session; |
| 1680 | struct iscsi_session *session; |
| 1681 | struct iscsi_conn *conn; |
| 1682 | |
| 1683 | cls_session = starget_to_session(scsi_target(sc->device)); |
| 1684 | session = cls_session->dd_data; |
| 1685 | conn = session->leadconn; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1686 | |
Mike Christie | bc436b2 | 2007-12-13 12:43:28 -0600 | [diff] [blame] | 1687 | mutex_lock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1688 | spin_lock_bh(&session->lock); |
| 1689 | if (session->state == ISCSI_STATE_TERMINATE) { |
| 1690 | failed: |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 1691 | ISCSI_DBG_EH(session, |
| 1692 | "failing target reset: Could not log back into " |
| 1693 | "target [age %d]\n", |
| 1694 | session->age); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1695 | spin_unlock_bh(&session->lock); |
Mike Christie | bc436b2 | 2007-12-13 12:43:28 -0600 | [diff] [blame] | 1696 | mutex_unlock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1697 | return FAILED; |
| 1698 | } |
| 1699 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1700 | spin_unlock_bh(&session->lock); |
Mike Christie | bc436b2 | 2007-12-13 12:43:28 -0600 | [diff] [blame] | 1701 | mutex_unlock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1702 | /* |
| 1703 | * we drop the lock here but the leadconn cannot be destoyed while |
| 1704 | * we are in the scsi eh |
| 1705 | */ |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1706 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1707 | |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 1708 | ISCSI_DBG_EH(session, "wait for relogin\n"); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1709 | wait_event_interruptible(conn->ehwait, |
| 1710 | session->state == ISCSI_STATE_TERMINATE || |
| 1711 | session->state == ISCSI_STATE_LOGGED_IN || |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1712 | session->state == ISCSI_STATE_RECOVERY_FAILED); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1713 | if (signal_pending(current)) |
| 1714 | flush_signals(current); |
| 1715 | |
Mike Christie | bc436b2 | 2007-12-13 12:43:28 -0600 | [diff] [blame] | 1716 | mutex_lock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1717 | spin_lock_bh(&session->lock); |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 1718 | if (session->state == ISCSI_STATE_LOGGED_IN) { |
| 1719 | ISCSI_DBG_EH(session, |
| 1720 | "target reset succeeded\n"); |
| 1721 | } else |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1722 | goto failed; |
| 1723 | spin_unlock_bh(&session->lock); |
Mike Christie | bc436b2 | 2007-12-13 12:43:28 -0600 | [diff] [blame] | 1724 | mutex_unlock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1725 | return SUCCESS; |
| 1726 | } |
Mike Christie | 8e12452 | 2008-09-24 11:46:12 -0500 | [diff] [blame] | 1727 | EXPORT_SYMBOL_GPL(iscsi_eh_target_reset); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1728 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1729 | static void iscsi_tmf_timedout(unsigned long data) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1730 | { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1731 | struct iscsi_conn *conn = (struct iscsi_conn *)data; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1732 | struct iscsi_session *session = conn->session; |
| 1733 | |
| 1734 | spin_lock(&session->lock); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1735 | if (conn->tmf_state == TMF_QUEUED) { |
| 1736 | conn->tmf_state = TMF_TIMEDOUT; |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 1737 | ISCSI_DBG_EH(session, "tmf timedout\n"); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1738 | /* unblock eh_abort() */ |
| 1739 | wake_up(&conn->ehwait); |
| 1740 | } |
| 1741 | spin_unlock(&session->lock); |
| 1742 | } |
| 1743 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1744 | static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn, |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1745 | struct iscsi_tm *hdr, int age, |
| 1746 | int timeout) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1747 | { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1748 | struct iscsi_session *session = conn->session; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1749 | struct iscsi_task *task; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1750 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1751 | task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr, |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1752 | NULL, 0); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1753 | if (!task) { |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1754 | spin_unlock_bh(&session->lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1755 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1756 | spin_lock_bh(&session->lock); |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 1757 | ISCSI_DBG_EH(session, "tmf exec failure\n"); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1758 | return -EPERM; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1759 | } |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1760 | conn->tmfcmd_pdus_cnt++; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1761 | conn->tmf_timer.expires = timeout * HZ + jiffies; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1762 | conn->tmf_timer.function = iscsi_tmf_timedout; |
| 1763 | conn->tmf_timer.data = (unsigned long)conn; |
| 1764 | add_timer(&conn->tmf_timer); |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 1765 | ISCSI_DBG_EH(session, "tmf set timeout\n"); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1766 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1767 | spin_unlock_bh(&session->lock); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1768 | mutex_unlock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1769 | |
| 1770 | /* |
| 1771 | * block eh thread until: |
| 1772 | * |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1773 | * 1) tmf response |
| 1774 | * 2) tmf timeout |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1775 | * 3) session is terminated or restarted or userspace has |
| 1776 | * given up on recovery |
| 1777 | */ |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1778 | wait_event_interruptible(conn->ehwait, age != session->age || |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1779 | session->state != ISCSI_STATE_LOGGED_IN || |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1780 | conn->tmf_state != TMF_QUEUED); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1781 | if (signal_pending(current)) |
| 1782 | flush_signals(current); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1783 | del_timer_sync(&conn->tmf_timer); |
| 1784 | |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1785 | mutex_lock(&session->eh_mutex); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1786 | spin_lock_bh(&session->lock); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1787 | /* if the session drops it will clean up the task */ |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1788 | if (age != session->age || |
| 1789 | session->state != ISCSI_STATE_LOGGED_IN) |
| 1790 | return -ENOTCONN; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1791 | return 0; |
| 1792 | } |
| 1793 | |
| 1794 | /* |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1795 | * Fail commands. session lock held and recv side suspended and xmit |
| 1796 | * thread flushed |
| 1797 | */ |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 1798 | static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun, |
| 1799 | int error) |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1800 | { |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 1801 | struct iscsi_task *task; |
| 1802 | int i; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1803 | |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 1804 | for (i = 0; i < conn->session->cmds_max; i++) { |
| 1805 | task = conn->session->cmds[i]; |
| 1806 | if (!task->sc || task->state == ISCSI_TASK_FREE) |
| 1807 | continue; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1808 | |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 1809 | if (lun != -1 && lun != task->sc->device->lun) |
| 1810 | continue; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1811 | |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 1812 | ISCSI_DBG_SESSION(conn->session, |
| 1813 | "failing sc %p itt 0x%x state %d\n", |
| 1814 | task->sc, task->itt, task->state); |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 1815 | fail_scsi_task(task, error); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1816 | } |
| 1817 | } |
| 1818 | |
Mike Christie | 661134a | 2009-09-05 07:35:33 +0530 | [diff] [blame] | 1819 | /** |
| 1820 | * iscsi_suspend_queue - suspend iscsi_queuecommand |
| 1821 | * @conn: iscsi conn to stop queueing IO on |
| 1822 | * |
| 1823 | * This grabs the session lock to make sure no one is in |
| 1824 | * xmit_task/queuecommand, and then sets suspend to prevent |
| 1825 | * new commands from being queued. This only needs to be called |
| 1826 | * by offload drivers that need to sync a path like ep disconnect |
| 1827 | * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi |
| 1828 | * will call iscsi_start_tx and iscsi_unblock_session when in FFP. |
| 1829 | */ |
| 1830 | void iscsi_suspend_queue(struct iscsi_conn *conn) |
| 1831 | { |
| 1832 | spin_lock_bh(&conn->session->lock); |
| 1833 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
| 1834 | spin_unlock_bh(&conn->session->lock); |
| 1835 | } |
| 1836 | EXPORT_SYMBOL_GPL(iscsi_suspend_queue); |
| 1837 | |
| 1838 | /** |
| 1839 | * iscsi_suspend_tx - suspend iscsi_data_xmit |
| 1840 | * @conn: iscsi conn tp stop processing IO on. |
| 1841 | * |
| 1842 | * This function sets the suspend bit to prevent iscsi_data_xmit |
| 1843 | * from sending new IO, and if work is queued on the xmit thread |
| 1844 | * it will wait for it to be completed. |
| 1845 | */ |
Mike Christie | b40977d | 2008-05-21 15:54:03 -0500 | [diff] [blame] | 1846 | void iscsi_suspend_tx(struct iscsi_conn *conn) |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1847 | { |
Mike Christie | 32ae763 | 2009-03-05 14:46:03 -0600 | [diff] [blame] | 1848 | struct Scsi_Host *shost = conn->session->host; |
| 1849 | struct iscsi_host *ihost = shost_priv(shost); |
| 1850 | |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1851 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
Mike Christie | 1336aed | 2009-05-13 17:57:48 -0500 | [diff] [blame] | 1852 | if (ihost->workq) |
Mike Christie | 32ae763 | 2009-03-05 14:46:03 -0600 | [diff] [blame] | 1853 | flush_workqueue(ihost->workq); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1854 | } |
Mike Christie | b40977d | 2008-05-21 15:54:03 -0500 | [diff] [blame] | 1855 | EXPORT_SYMBOL_GPL(iscsi_suspend_tx); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1856 | |
| 1857 | static void iscsi_start_tx(struct iscsi_conn *conn) |
| 1858 | { |
| 1859 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
Mike Christie | 1336aed | 2009-05-13 17:57:48 -0500 | [diff] [blame] | 1860 | iscsi_conn_queue_work(conn); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1861 | } |
| 1862 | |
Mike Christie | 4c48a82 | 2009-05-13 17:57:45 -0500 | [diff] [blame] | 1863 | /* |
| 1864 | * We want to make sure a ping is in flight. It has timed out. |
| 1865 | * And we are not busy processing a pdu that is making |
| 1866 | * progress but got started before the ping and is taking a while |
| 1867 | * to complete so the ping is just stuck behind it in a queue. |
| 1868 | */ |
| 1869 | static int iscsi_has_ping_timed_out(struct iscsi_conn *conn) |
| 1870 | { |
| 1871 | if (conn->ping_task && |
| 1872 | time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) + |
| 1873 | (conn->ping_timeout * HZ), jiffies)) |
| 1874 | return 1; |
| 1875 | else |
| 1876 | return 0; |
| 1877 | } |
| 1878 | |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1879 | static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc) |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1880 | { |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1881 | enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED; |
| 1882 | struct iscsi_task *task = NULL; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1883 | struct iscsi_cls_session *cls_session; |
| 1884 | struct iscsi_session *session; |
| 1885 | struct iscsi_conn *conn; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1886 | |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1887 | cls_session = starget_to_session(scsi_target(sc->device)); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1888 | session = cls_session->dd_data; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1889 | |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 1890 | ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1891 | |
| 1892 | spin_lock(&session->lock); |
| 1893 | if (session->state != ISCSI_STATE_LOGGED_IN) { |
| 1894 | /* |
| 1895 | * We are probably in the middle of iscsi recovery so let |
| 1896 | * that complete and handle the error. |
| 1897 | */ |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1898 | rc = BLK_EH_RESET_TIMER; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1899 | goto done; |
| 1900 | } |
| 1901 | |
| 1902 | conn = session->leadconn; |
| 1903 | if (!conn) { |
| 1904 | /* In the middle of shuting down */ |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1905 | rc = BLK_EH_RESET_TIMER; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1906 | goto done; |
| 1907 | } |
| 1908 | |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1909 | task = (struct iscsi_task *)sc->SCp.ptr; |
| 1910 | if (!task) |
| 1911 | goto done; |
| 1912 | /* |
| 1913 | * If we have sent (at least queued to the network layer) a pdu or |
| 1914 | * recvd one for the task since the last timeout ask for |
| 1915 | * more time. If on the next timeout we have not made progress |
| 1916 | * we can check if it is the task or connection when we send the |
| 1917 | * nop as a ping. |
| 1918 | */ |
| 1919 | if (time_after_eq(task->last_xfer, task->last_timeout)) { |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 1920 | ISCSI_DBG_EH(session, "Command making progress. Asking " |
| 1921 | "scsi-ml for more time to complete. " |
| 1922 | "Last data recv at %lu. Last timeout was at " |
| 1923 | "%lu\n.", task->last_xfer, task->last_timeout); |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1924 | task->have_checked_conn = false; |
| 1925 | rc = BLK_EH_RESET_TIMER; |
| 1926 | goto done; |
| 1927 | } |
| 1928 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1929 | if (!conn->recv_timeout && !conn->ping_timeout) |
| 1930 | goto done; |
| 1931 | /* |
| 1932 | * if the ping timedout then we are in the middle of cleaning up |
| 1933 | * and can let the iscsi eh handle it |
| 1934 | */ |
Mike Christie | 4c48a82 | 2009-05-13 17:57:45 -0500 | [diff] [blame] | 1935 | if (iscsi_has_ping_timed_out(conn)) { |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1936 | rc = BLK_EH_RESET_TIMER; |
Mike Christie | 4c48a82 | 2009-05-13 17:57:45 -0500 | [diff] [blame] | 1937 | goto done; |
| 1938 | } |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1939 | |
| 1940 | /* Assumes nop timeout is shorter than scsi cmd timeout */ |
| 1941 | if (task->have_checked_conn) |
| 1942 | goto done; |
| 1943 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1944 | /* |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1945 | * Checking the transport already or nop from a cmd timeout still |
| 1946 | * running |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1947 | */ |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1948 | if (conn->ping_task) { |
| 1949 | task->have_checked_conn = true; |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1950 | rc = BLK_EH_RESET_TIMER; |
Mike Christie | 4c48a82 | 2009-05-13 17:57:45 -0500 | [diff] [blame] | 1951 | goto done; |
| 1952 | } |
| 1953 | |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1954 | /* Make sure there is a transport check done */ |
| 1955 | iscsi_send_nopout(conn, NULL); |
| 1956 | task->have_checked_conn = true; |
| 1957 | rc = BLK_EH_RESET_TIMER; |
| 1958 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1959 | done: |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 1960 | if (task) |
| 1961 | task->last_timeout = jiffies; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1962 | spin_unlock(&session->lock); |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 1963 | ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ? |
| 1964 | "timer reset" : "nh"); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1965 | return rc; |
| 1966 | } |
| 1967 | |
| 1968 | static void iscsi_check_transport_timeouts(unsigned long data) |
| 1969 | { |
| 1970 | struct iscsi_conn *conn = (struct iscsi_conn *)data; |
| 1971 | struct iscsi_session *session = conn->session; |
Mike Christie | 4cf1043 | 2008-05-07 20:43:52 -0500 | [diff] [blame] | 1972 | unsigned long recv_timeout, next_timeout = 0, last_recv; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1973 | |
| 1974 | spin_lock(&session->lock); |
| 1975 | if (session->state != ISCSI_STATE_LOGGED_IN) |
| 1976 | goto done; |
| 1977 | |
Mike Christie | 4cf1043 | 2008-05-07 20:43:52 -0500 | [diff] [blame] | 1978 | recv_timeout = conn->recv_timeout; |
| 1979 | if (!recv_timeout) |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1980 | goto done; |
| 1981 | |
Mike Christie | 4cf1043 | 2008-05-07 20:43:52 -0500 | [diff] [blame] | 1982 | recv_timeout *= HZ; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1983 | last_recv = conn->last_recv; |
Mike Christie | 4c48a82 | 2009-05-13 17:57:45 -0500 | [diff] [blame] | 1984 | |
| 1985 | if (iscsi_has_ping_timed_out(conn)) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 1986 | iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs " |
Mike Christie | 4c48a82 | 2009-05-13 17:57:45 -0500 | [diff] [blame] | 1987 | "expired, recv timeout %d, last rx %lu, " |
| 1988 | "last ping %lu, now %lu\n", |
| 1989 | conn->ping_timeout, conn->recv_timeout, |
| 1990 | last_recv, conn->last_ping, jiffies); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1991 | spin_unlock(&session->lock); |
| 1992 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 1993 | return; |
| 1994 | } |
| 1995 | |
Mike Christie | 4cf1043 | 2008-05-07 20:43:52 -0500 | [diff] [blame] | 1996 | if (time_before_eq(last_recv + recv_timeout, jiffies)) { |
Mike Christie | c8611f9 | 2008-05-08 20:15:34 -0500 | [diff] [blame] | 1997 | /* send a ping to try to provoke some traffic */ |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 1998 | ISCSI_DBG_CONN(conn, "Sending nopout as ping\n"); |
Mike Christie | c8611f9 | 2008-05-08 20:15:34 -0500 | [diff] [blame] | 1999 | iscsi_send_nopout(conn, NULL); |
Mike Christie | 4cf1043 | 2008-05-07 20:43:52 -0500 | [diff] [blame] | 2000 | next_timeout = conn->last_ping + (conn->ping_timeout * HZ); |
Mike Christie | ad294e9 | 2008-01-31 13:36:50 -0600 | [diff] [blame] | 2001 | } else |
Mike Christie | 4cf1043 | 2008-05-07 20:43:52 -0500 | [diff] [blame] | 2002 | next_timeout = last_recv + recv_timeout; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2003 | |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 2004 | ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout); |
Mike Christie | ad294e9 | 2008-01-31 13:36:50 -0600 | [diff] [blame] | 2005 | mod_timer(&conn->transport_timer, next_timeout); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2006 | done: |
| 2007 | spin_unlock(&session->lock); |
| 2008 | } |
| 2009 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2010 | static void iscsi_prep_abort_task_pdu(struct iscsi_task *task, |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2011 | struct iscsi_tm *hdr) |
| 2012 | { |
| 2013 | memset(hdr, 0, sizeof(*hdr)); |
| 2014 | hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; |
| 2015 | hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK; |
| 2016 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 2017 | memcpy(hdr->lun, task->lun, sizeof(hdr->lun)); |
| 2018 | hdr->rtt = task->hdr_itt; |
| 2019 | hdr->refcmdsn = task->cmdsn; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2020 | } |
| 2021 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2022 | int iscsi_eh_abort(struct scsi_cmnd *sc) |
| 2023 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2024 | struct iscsi_cls_session *cls_session; |
| 2025 | struct iscsi_session *session; |
Mike Christie | f47f2cf | 2006-08-31 18:09:33 -0400 | [diff] [blame] | 2026 | struct iscsi_conn *conn; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2027 | struct iscsi_task *task; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2028 | struct iscsi_tm *hdr; |
| 2029 | int rc, age; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2030 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2031 | cls_session = starget_to_session(scsi_target(sc->device)); |
| 2032 | session = cls_session->dd_data; |
| 2033 | |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 2034 | ISCSI_DBG_EH(session, "aborting sc %p\n", sc); |
Mike Christie | 4421c9e | 2009-05-13 17:57:50 -0500 | [diff] [blame] | 2035 | |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2036 | mutex_lock(&session->eh_mutex); |
| 2037 | spin_lock_bh(&session->lock); |
Mike Christie | f47f2cf | 2006-08-31 18:09:33 -0400 | [diff] [blame] | 2038 | /* |
| 2039 | * if session was ISCSI_STATE_IN_RECOVERY then we may not have |
| 2040 | * got the command. |
| 2041 | */ |
| 2042 | if (!sc->SCp.ptr) { |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 2043 | ISCSI_DBG_EH(session, "sc never reached iscsi layer or " |
| 2044 | "it completed.\n"); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2045 | spin_unlock_bh(&session->lock); |
| 2046 | mutex_unlock(&session->eh_mutex); |
Mike Christie | f47f2cf | 2006-08-31 18:09:33 -0400 | [diff] [blame] | 2047 | return SUCCESS; |
| 2048 | } |
| 2049 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2050 | /* |
| 2051 | * If we are not logged in or we have started a new session |
| 2052 | * then let the host reset code handle this |
| 2053 | */ |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2054 | if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN || |
| 2055 | sc->SCp.phase != session->age) { |
| 2056 | spin_unlock_bh(&session->lock); |
| 2057 | mutex_unlock(&session->eh_mutex); |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 2058 | ISCSI_DBG_EH(session, "failing abort due to dropped " |
Mike Christie | 4421c9e | 2009-05-13 17:57:50 -0500 | [diff] [blame] | 2059 | "session.\n"); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2060 | return FAILED; |
| 2061 | } |
| 2062 | |
| 2063 | conn = session->leadconn; |
| 2064 | conn->eh_abort_cnt++; |
| 2065 | age = session->age; |
| 2066 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2067 | task = (struct iscsi_task *)sc->SCp.ptr; |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 2068 | ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n", |
| 2069 | sc, task->itt); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2070 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2071 | /* task completed before time out */ |
| 2072 | if (!task->sc) { |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 2073 | ISCSI_DBG_EH(session, "sc completed while abort in progress\n"); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 2074 | goto success; |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 2075 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2076 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2077 | if (task->state == ISCSI_TASK_PENDING) { |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 2078 | fail_scsi_task(task, DID_ABORT); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 2079 | goto success; |
| 2080 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2081 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2082 | /* only have one tmf outstanding at a time */ |
| 2083 | if (conn->tmf_state != TMF_INITIAL) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2084 | goto failed; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2085 | conn->tmf_state = TMF_QUEUED; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2086 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2087 | hdr = &conn->tmhdr; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2088 | iscsi_prep_abort_task_pdu(task, hdr); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2089 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2090 | if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2091 | rc = FAILED; |
| 2092 | goto failed; |
| 2093 | } |
| 2094 | |
| 2095 | switch (conn->tmf_state) { |
| 2096 | case TMF_SUCCESS: |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 2097 | spin_unlock_bh(&session->lock); |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 2098 | /* |
| 2099 | * stop tx side incase the target had sent a abort rsp but |
| 2100 | * the initiator was still writing out data. |
| 2101 | */ |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2102 | iscsi_suspend_tx(conn); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 2103 | /* |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 2104 | * we do not stop the recv side because targets have been |
| 2105 | * good and have never sent us a successful tmf response |
| 2106 | * then sent more data for the cmd. |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 2107 | */ |
Mike Christie | 6187c24 | 2009-07-15 15:02:57 -0500 | [diff] [blame] | 2108 | spin_lock_bh(&session->lock); |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 2109 | fail_scsi_task(task, DID_ABORT); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2110 | conn->tmf_state = TMF_INITIAL; |
Mike Christie | 6187c24 | 2009-07-15 15:02:57 -0500 | [diff] [blame] | 2111 | spin_unlock_bh(&session->lock); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2112 | iscsi_start_tx(conn); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 2113 | goto success_unlocked; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2114 | case TMF_TIMEDOUT: |
| 2115 | spin_unlock_bh(&session->lock); |
| 2116 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 2117 | goto failed_unlocked; |
| 2118 | case TMF_NOT_FOUND: |
| 2119 | if (!sc->SCp.ptr) { |
| 2120 | conn->tmf_state = TMF_INITIAL; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2121 | /* task completed before tmf abort response */ |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 2122 | ISCSI_DBG_EH(session, "sc completed while abort in " |
| 2123 | "progress\n"); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 2124 | goto success; |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 2125 | } |
| 2126 | /* fall through */ |
| 2127 | default: |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2128 | conn->tmf_state = TMF_INITIAL; |
| 2129 | goto failed; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2130 | } |
| 2131 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 2132 | success: |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2133 | spin_unlock_bh(&session->lock); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 2134 | success_unlocked: |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 2135 | ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n", |
| 2136 | sc, task->itt); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2137 | mutex_unlock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2138 | return SUCCESS; |
| 2139 | |
| 2140 | failed: |
| 2141 | spin_unlock_bh(&session->lock); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 2142 | failed_unlocked: |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 2143 | ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc, |
| 2144 | task ? task->itt : 0); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2145 | mutex_unlock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2146 | return FAILED; |
| 2147 | } |
| 2148 | EXPORT_SYMBOL_GPL(iscsi_eh_abort); |
| 2149 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2150 | static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr) |
| 2151 | { |
| 2152 | memset(hdr, 0, sizeof(*hdr)); |
| 2153 | hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; |
| 2154 | hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK; |
| 2155 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
| 2156 | int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2157 | hdr->rtt = RESERVED_ITT; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | int iscsi_eh_device_reset(struct scsi_cmnd *sc) |
| 2161 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2162 | struct iscsi_cls_session *cls_session; |
| 2163 | struct iscsi_session *session; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2164 | struct iscsi_conn *conn; |
| 2165 | struct iscsi_tm *hdr; |
| 2166 | int rc = FAILED; |
| 2167 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2168 | cls_session = starget_to_session(scsi_target(sc->device)); |
| 2169 | session = cls_session->dd_data; |
| 2170 | |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 2171 | ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2172 | |
| 2173 | mutex_lock(&session->eh_mutex); |
| 2174 | spin_lock_bh(&session->lock); |
| 2175 | /* |
| 2176 | * Just check if we are not logged in. We cannot check for |
| 2177 | * the phase because the reset could come from a ioctl. |
| 2178 | */ |
| 2179 | if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) |
| 2180 | goto unlock; |
| 2181 | conn = session->leadconn; |
| 2182 | |
| 2183 | /* only have one tmf outstanding at a time */ |
| 2184 | if (conn->tmf_state != TMF_INITIAL) |
| 2185 | goto unlock; |
| 2186 | conn->tmf_state = TMF_QUEUED; |
| 2187 | |
| 2188 | hdr = &conn->tmhdr; |
| 2189 | iscsi_prep_lun_reset_pdu(sc, hdr); |
| 2190 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2191 | if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age, |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2192 | session->lu_reset_timeout)) { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2193 | rc = FAILED; |
| 2194 | goto unlock; |
| 2195 | } |
| 2196 | |
| 2197 | switch (conn->tmf_state) { |
| 2198 | case TMF_SUCCESS: |
| 2199 | break; |
| 2200 | case TMF_TIMEDOUT: |
| 2201 | spin_unlock_bh(&session->lock); |
| 2202 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 2203 | goto done; |
| 2204 | default: |
| 2205 | conn->tmf_state = TMF_INITIAL; |
| 2206 | goto unlock; |
| 2207 | } |
| 2208 | |
| 2209 | rc = SUCCESS; |
| 2210 | spin_unlock_bh(&session->lock); |
| 2211 | |
| 2212 | iscsi_suspend_tx(conn); |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 2213 | |
Mike Christie | a343914 | 2008-09-24 11:46:15 -0500 | [diff] [blame] | 2214 | spin_lock_bh(&session->lock); |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 2215 | fail_scsi_tasks(conn, sc->device->lun, DID_ERROR); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2216 | conn->tmf_state = TMF_INITIAL; |
Mike Christie | a343914 | 2008-09-24 11:46:15 -0500 | [diff] [blame] | 2217 | spin_unlock_bh(&session->lock); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2218 | |
| 2219 | iscsi_start_tx(conn); |
| 2220 | goto done; |
| 2221 | |
| 2222 | unlock: |
| 2223 | spin_unlock_bh(&session->lock); |
| 2224 | done: |
Erez Zilber | bd2199d | 2009-06-15 22:11:10 -0500 | [diff] [blame] | 2225 | ISCSI_DBG_EH(session, "dev reset result = %s\n", |
| 2226 | rc == SUCCESS ? "SUCCESS" : "FAILED"); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2227 | mutex_unlock(&session->eh_mutex); |
| 2228 | return rc; |
| 2229 | } |
| 2230 | EXPORT_SYMBOL_GPL(iscsi_eh_device_reset); |
| 2231 | |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2232 | /* |
| 2233 | * Pre-allocate a pool of @max items of @item_size. By default, the pool |
| 2234 | * should be accessed via kfifo_{get,put} on q->queue. |
| 2235 | * Optionally, the caller can obtain the array of object pointers |
| 2236 | * by passing in a non-NULL @items pointer |
| 2237 | */ |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2238 | int |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2239 | iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2240 | { |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2241 | int i, num_arrays = 1; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2242 | |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2243 | memset(q, 0, sizeof(*q)); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2244 | |
| 2245 | q->max = max; |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2246 | |
| 2247 | /* If the user passed an items pointer, he wants a copy of |
| 2248 | * the array. */ |
| 2249 | if (items) |
| 2250 | num_arrays++; |
| 2251 | q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL); |
| 2252 | if (q->pool == NULL) |
Jean Delvare | f474a37 | 2009-03-05 14:45:55 -0600 | [diff] [blame] | 2253 | return -ENOMEM; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2254 | |
| 2255 | q->queue = kfifo_init((void*)q->pool, max * sizeof(void*), |
| 2256 | GFP_KERNEL, NULL); |
Jean Delvare | fd6e1c1 | 2009-04-01 13:11:29 -0500 | [diff] [blame] | 2257 | if (IS_ERR(q->queue)) { |
| 2258 | q->queue = NULL; |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2259 | goto enomem; |
Jean Delvare | fd6e1c1 | 2009-04-01 13:11:29 -0500 | [diff] [blame] | 2260 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2261 | |
| 2262 | for (i = 0; i < max; i++) { |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2263 | q->pool[i] = kzalloc(item_size, GFP_KERNEL); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2264 | if (q->pool[i] == NULL) { |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2265 | q->max = i; |
| 2266 | goto enomem; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2267 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2268 | __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*)); |
| 2269 | } |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2270 | |
| 2271 | if (items) { |
| 2272 | *items = q->pool + max; |
| 2273 | memcpy(*items, q->pool, max * sizeof(void *)); |
| 2274 | } |
| 2275 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2276 | return 0; |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2277 | |
| 2278 | enomem: |
| 2279 | iscsi_pool_free(q); |
| 2280 | return -ENOMEM; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2281 | } |
| 2282 | EXPORT_SYMBOL_GPL(iscsi_pool_init); |
| 2283 | |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2284 | void iscsi_pool_free(struct iscsi_pool *q) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2285 | { |
| 2286 | int i; |
| 2287 | |
| 2288 | for (i = 0; i < q->max; i++) |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2289 | kfree(q->pool[i]); |
Jean Delvare | f474a37 | 2009-03-05 14:45:55 -0600 | [diff] [blame] | 2290 | kfree(q->pool); |
Mike Christie | 2f5899a | 2009-01-16 12:36:51 -0600 | [diff] [blame] | 2291 | kfree(q->queue); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2292 | } |
| 2293 | EXPORT_SYMBOL_GPL(iscsi_pool_free); |
| 2294 | |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2295 | /** |
| 2296 | * iscsi_host_add - add host to system |
| 2297 | * @shost: scsi host |
| 2298 | * @pdev: parent device |
| 2299 | * |
| 2300 | * This should be called by partial offload and software iscsi drivers |
| 2301 | * to add a host to the system. |
| 2302 | */ |
| 2303 | int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2304 | { |
Mike Christie | 8e9a20c | 2008-06-16 10:11:33 -0500 | [diff] [blame] | 2305 | if (!shost->can_queue) |
| 2306 | shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX; |
| 2307 | |
Mike Christie | 4d10835 | 2009-03-05 14:46:04 -0600 | [diff] [blame] | 2308 | if (!shost->cmd_per_lun) |
| 2309 | shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN; |
| 2310 | |
Mike Christie | 308cec1 | 2009-02-06 12:06:20 -0600 | [diff] [blame] | 2311 | if (!shost->transportt->eh_timed_out) |
| 2312 | shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out; |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2313 | return scsi_add_host(shost, pdev); |
| 2314 | } |
| 2315 | EXPORT_SYMBOL_GPL(iscsi_host_add); |
| 2316 | |
| 2317 | /** |
| 2318 | * iscsi_host_alloc - allocate a host and driver data |
| 2319 | * @sht: scsi host template |
| 2320 | * @dd_data_size: driver host data size |
Mike Christie | 32ae763 | 2009-03-05 14:46:03 -0600 | [diff] [blame] | 2321 | * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2322 | * |
| 2323 | * This should be called by partial offload and software iscsi drivers. |
| 2324 | * To access the driver specific memory use the iscsi_host_priv() macro. |
| 2325 | */ |
| 2326 | struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht, |
Mike Christie | 4d10835 | 2009-03-05 14:46:04 -0600 | [diff] [blame] | 2327 | int dd_data_size, bool xmit_can_sleep) |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2328 | { |
| 2329 | struct Scsi_Host *shost; |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2330 | struct iscsi_host *ihost; |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2331 | |
| 2332 | shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size); |
| 2333 | if (!shost) |
| 2334 | return NULL; |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2335 | ihost = shost_priv(shost); |
Mike Christie | 32ae763 | 2009-03-05 14:46:03 -0600 | [diff] [blame] | 2336 | |
| 2337 | if (xmit_can_sleep) { |
| 2338 | snprintf(ihost->workq_name, sizeof(ihost->workq_name), |
| 2339 | "iscsi_q_%d", shost->host_no); |
| 2340 | ihost->workq = create_singlethread_workqueue(ihost->workq_name); |
| 2341 | if (!ihost->workq) |
| 2342 | goto free_host; |
| 2343 | } |
| 2344 | |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2345 | spin_lock_init(&ihost->lock); |
| 2346 | ihost->state = ISCSI_HOST_SETUP; |
| 2347 | ihost->num_sessions = 0; |
| 2348 | init_waitqueue_head(&ihost->session_removal_wq); |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2349 | return shost; |
Mike Christie | 32ae763 | 2009-03-05 14:46:03 -0600 | [diff] [blame] | 2350 | |
| 2351 | free_host: |
| 2352 | scsi_host_put(shost); |
| 2353 | return NULL; |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2354 | } |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2355 | EXPORT_SYMBOL_GPL(iscsi_host_alloc); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2356 | |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2357 | static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session) |
| 2358 | { |
Mike Christie | 40a06e7 | 2009-03-05 14:46:05 -0600 | [diff] [blame] | 2359 | iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST); |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2360 | } |
| 2361 | |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2362 | /** |
| 2363 | * iscsi_host_remove - remove host and sessions |
| 2364 | * @shost: scsi host |
| 2365 | * |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2366 | * If there are any sessions left, this will initiate the removal and wait |
| 2367 | * for the completion. |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2368 | */ |
| 2369 | void iscsi_host_remove(struct Scsi_Host *shost) |
| 2370 | { |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2371 | struct iscsi_host *ihost = shost_priv(shost); |
| 2372 | unsigned long flags; |
| 2373 | |
| 2374 | spin_lock_irqsave(&ihost->lock, flags); |
| 2375 | ihost->state = ISCSI_HOST_REMOVED; |
| 2376 | spin_unlock_irqrestore(&ihost->lock, flags); |
| 2377 | |
| 2378 | iscsi_host_for_each_session(shost, iscsi_notify_host_removed); |
| 2379 | wait_event_interruptible(ihost->session_removal_wq, |
| 2380 | ihost->num_sessions == 0); |
| 2381 | if (signal_pending(current)) |
| 2382 | flush_signals(current); |
| 2383 | |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2384 | scsi_remove_host(shost); |
Mike Christie | 32ae763 | 2009-03-05 14:46:03 -0600 | [diff] [blame] | 2385 | if (ihost->workq) |
| 2386 | destroy_workqueue(ihost->workq); |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2387 | } |
| 2388 | EXPORT_SYMBOL_GPL(iscsi_host_remove); |
| 2389 | |
| 2390 | void iscsi_host_free(struct Scsi_Host *shost) |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2391 | { |
| 2392 | struct iscsi_host *ihost = shost_priv(shost); |
| 2393 | |
| 2394 | kfree(ihost->netdev); |
| 2395 | kfree(ihost->hwaddress); |
| 2396 | kfree(ihost->initiatorname); |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2397 | scsi_host_put(shost); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2398 | } |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2399 | EXPORT_SYMBOL_GPL(iscsi_host_free); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2400 | |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2401 | static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost) |
| 2402 | { |
| 2403 | struct iscsi_host *ihost = shost_priv(shost); |
| 2404 | unsigned long flags; |
| 2405 | |
| 2406 | shost = scsi_host_get(shost); |
| 2407 | if (!shost) { |
| 2408 | printk(KERN_ERR "Invalid state. Cannot notify host removal " |
| 2409 | "of session teardown event because host already " |
| 2410 | "removed.\n"); |
| 2411 | return; |
| 2412 | } |
| 2413 | |
| 2414 | spin_lock_irqsave(&ihost->lock, flags); |
| 2415 | ihost->num_sessions--; |
| 2416 | if (ihost->num_sessions == 0) |
| 2417 | wake_up(&ihost->session_removal_wq); |
| 2418 | spin_unlock_irqrestore(&ihost->lock, flags); |
| 2419 | scsi_host_put(shost); |
| 2420 | } |
| 2421 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2422 | /** |
| 2423 | * iscsi_session_setup - create iscsi cls session and host and session |
| 2424 | * @iscsit: iscsi transport template |
| 2425 | * @shost: scsi host |
| 2426 | * @cmds_max: session can queue |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2427 | * @cmd_task_size: LLD task private data size |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2428 | * @initial_cmdsn: initial CmdSN |
| 2429 | * |
| 2430 | * This can be used by software iscsi_transports that allocate |
| 2431 | * a session per scsi host. |
Mike Christie | 3cf7b23 | 2008-05-21 15:54:17 -0500 | [diff] [blame] | 2432 | * |
| 2433 | * Callers should set cmds_max to the largest total numer (mgmt + scsi) of |
| 2434 | * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks |
| 2435 | * for nop handling and login/logout requests. |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2436 | */ |
| 2437 | struct iscsi_cls_session * |
| 2438 | iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost, |
Mike Christie | 3cf7b23 | 2008-05-21 15:54:17 -0500 | [diff] [blame] | 2439 | uint16_t cmds_max, int cmd_task_size, |
Mike Christie | 7970634 | 2008-05-21 15:54:12 -0500 | [diff] [blame] | 2440 | uint32_t initial_cmdsn, unsigned int id) |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2441 | { |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2442 | struct iscsi_host *ihost = shost_priv(shost); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2443 | struct iscsi_session *session; |
| 2444 | struct iscsi_cls_session *cls_session; |
Mike Christie | 3cf7b23 | 2008-05-21 15:54:17 -0500 | [diff] [blame] | 2445 | int cmd_i, scsi_cmds, total_cmds = cmds_max; |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2446 | unsigned long flags; |
| 2447 | |
| 2448 | spin_lock_irqsave(&ihost->lock, flags); |
| 2449 | if (ihost->state == ISCSI_HOST_REMOVED) { |
| 2450 | spin_unlock_irqrestore(&ihost->lock, flags); |
| 2451 | return NULL; |
| 2452 | } |
| 2453 | ihost->num_sessions++; |
| 2454 | spin_unlock_irqrestore(&ihost->lock, flags); |
Mike Christie | 8e9a20c | 2008-06-16 10:11:33 -0500 | [diff] [blame] | 2455 | |
| 2456 | if (!total_cmds) |
| 2457 | total_cmds = ISCSI_DEF_XMIT_CMDS_MAX; |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 2458 | /* |
Mike Christie | 3cf7b23 | 2008-05-21 15:54:17 -0500 | [diff] [blame] | 2459 | * The iscsi layer needs some tasks for nop handling and tmfs, |
| 2460 | * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX |
| 2461 | * + 1 command for scsi IO. |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 2462 | */ |
Mike Christie | 3cf7b23 | 2008-05-21 15:54:17 -0500 | [diff] [blame] | 2463 | if (total_cmds < ISCSI_TOTAL_CMDS_MIN) { |
| 2464 | printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " |
| 2465 | "must be a power of two that is at least %d.\n", |
| 2466 | total_cmds, ISCSI_TOTAL_CMDS_MIN); |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2467 | goto dec_session_count; |
Mike Christie | 1548271 | 2007-05-30 12:57:19 -0500 | [diff] [blame] | 2468 | } |
Mike Christie | 3cf7b23 | 2008-05-21 15:54:17 -0500 | [diff] [blame] | 2469 | |
| 2470 | if (total_cmds > ISCSI_TOTAL_CMDS_MAX) { |
| 2471 | printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " |
| 2472 | "must be a power of 2 less than or equal to %d.\n", |
| 2473 | cmds_max, ISCSI_TOTAL_CMDS_MAX); |
| 2474 | total_cmds = ISCSI_TOTAL_CMDS_MAX; |
| 2475 | } |
| 2476 | |
| 2477 | if (!is_power_of_2(total_cmds)) { |
| 2478 | printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " |
| 2479 | "must be a power of 2.\n", total_cmds); |
| 2480 | total_cmds = rounddown_pow_of_two(total_cmds); |
| 2481 | if (total_cmds < ISCSI_TOTAL_CMDS_MIN) |
| 2482 | return NULL; |
| 2483 | printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n", |
| 2484 | total_cmds); |
| 2485 | } |
| 2486 | scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX; |
Mike Christie | 1548271 | 2007-05-30 12:57:19 -0500 | [diff] [blame] | 2487 | |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 2488 | cls_session = iscsi_alloc_session(shost, iscsit, |
| 2489 | sizeof(struct iscsi_session)); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2490 | if (!cls_session) |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2491 | goto dec_session_count; |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2492 | session = cls_session->dd_data; |
| 2493 | session->cls_session = cls_session; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2494 | session->host = shost; |
| 2495 | session->state = ISCSI_STATE_FREE; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2496 | session->fast_abort = 1; |
Mike Christie | 4cd49ea | 2007-12-13 12:43:38 -0600 | [diff] [blame] | 2497 | session->lu_reset_timeout = 15; |
| 2498 | session->abort_timeout = 10; |
Mike Christie | 3cf7b23 | 2008-05-21 15:54:17 -0500 | [diff] [blame] | 2499 | session->scsi_cmds_max = scsi_cmds; |
| 2500 | session->cmds_max = total_cmds; |
Mike Christie | e072640 | 2007-07-26 12:46:48 -0500 | [diff] [blame] | 2501 | session->queued_cmdsn = session->cmdsn = initial_cmdsn; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2502 | session->exp_cmdsn = initial_cmdsn + 1; |
| 2503 | session->max_cmdsn = initial_cmdsn + 1; |
| 2504 | session->max_r2t = 1; |
| 2505 | session->tt = iscsit; |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2506 | mutex_init(&session->eh_mutex); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2507 | spin_lock_init(&session->lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2508 | |
| 2509 | /* initialize SCSI PDU commands pool */ |
| 2510 | if (iscsi_pool_init(&session->cmdpool, session->cmds_max, |
| 2511 | (void***)&session->cmds, |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2512 | cmd_task_size + sizeof(struct iscsi_task))) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2513 | goto cmdpool_alloc_fail; |
| 2514 | |
| 2515 | /* pre-format cmds pool with ITT */ |
| 2516 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2517 | struct iscsi_task *task = session->cmds[cmd_i]; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2518 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2519 | if (cmd_task_size) |
| 2520 | task->dd_data = &task[1]; |
| 2521 | task->itt = cmd_i; |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 2522 | task->state = ISCSI_TASK_FREE; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2523 | INIT_LIST_HEAD(&task->running); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2524 | } |
| 2525 | |
Mike Christie | f53a88d | 2006-06-28 12:00:27 -0500 | [diff] [blame] | 2526 | if (!try_module_get(iscsit->owner)) |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2527 | goto module_get_fail; |
| 2528 | |
Mike Christie | 7970634 | 2008-05-21 15:54:12 -0500 | [diff] [blame] | 2529 | if (iscsi_add_session(cls_session, id)) |
Mike Christie | f53a88d | 2006-06-28 12:00:27 -0500 | [diff] [blame] | 2530 | goto cls_session_fail; |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2531 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2532 | return cls_session; |
| 2533 | |
| 2534 | cls_session_fail: |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2535 | module_put(iscsit->owner); |
| 2536 | module_get_fail: |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2537 | iscsi_pool_free(&session->cmdpool); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2538 | cmdpool_alloc_fail: |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2539 | iscsi_free_session(cls_session); |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2540 | dec_session_count: |
| 2541 | iscsi_host_dec_session_cnt(shost); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2542 | return NULL; |
| 2543 | } |
| 2544 | EXPORT_SYMBOL_GPL(iscsi_session_setup); |
| 2545 | |
| 2546 | /** |
| 2547 | * iscsi_session_teardown - destroy session, host, and cls_session |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2548 | * @cls_session: iscsi session |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2549 | * |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2550 | * The driver must have called iscsi_remove_session before |
| 2551 | * calling this. |
| 2552 | */ |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2553 | void iscsi_session_teardown(struct iscsi_cls_session *cls_session) |
| 2554 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2555 | struct iscsi_session *session = cls_session->dd_data; |
Mike Christie | 63f75cc | 2006-07-24 15:47:29 -0500 | [diff] [blame] | 2556 | struct module *owner = cls_session->transport->owner; |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2557 | struct Scsi_Host *shost = session->host; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2558 | |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2559 | iscsi_pool_free(&session->cmdpool); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2560 | |
Mike Christie | b2c6416 | 2007-05-30 12:57:16 -0500 | [diff] [blame] | 2561 | kfree(session->password); |
| 2562 | kfree(session->password_in); |
| 2563 | kfree(session->username); |
| 2564 | kfree(session->username_in); |
Mike Christie | f3ff0c3 | 2006-07-24 15:47:50 -0500 | [diff] [blame] | 2565 | kfree(session->targetname); |
Mike Christie | 88dfd34 | 2008-05-21 15:54:16 -0500 | [diff] [blame] | 2566 | kfree(session->initiatorname); |
| 2567 | kfree(session->ifacename); |
Mike Christie | f3ff0c3 | 2006-07-24 15:47:50 -0500 | [diff] [blame] | 2568 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2569 | iscsi_destroy_session(cls_session); |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2570 | iscsi_host_dec_session_cnt(shost); |
Mike Christie | 63f75cc | 2006-07-24 15:47:29 -0500 | [diff] [blame] | 2571 | module_put(owner); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2572 | } |
| 2573 | EXPORT_SYMBOL_GPL(iscsi_session_teardown); |
| 2574 | |
| 2575 | /** |
| 2576 | * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn |
| 2577 | * @cls_session: iscsi_cls_session |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 2578 | * @dd_size: private driver data size |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2579 | * @conn_idx: cid |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 2580 | */ |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2581 | struct iscsi_cls_conn * |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 2582 | iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size, |
| 2583 | uint32_t conn_idx) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2584 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2585 | struct iscsi_session *session = cls_session->dd_data; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2586 | struct iscsi_conn *conn; |
| 2587 | struct iscsi_cls_conn *cls_conn; |
Mike Christie | d36ab6f | 2006-05-18 20:31:34 -0500 | [diff] [blame] | 2588 | char *data; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2589 | |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 2590 | cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size, |
| 2591 | conn_idx); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2592 | if (!cls_conn) |
| 2593 | return NULL; |
| 2594 | conn = cls_conn->dd_data; |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 2595 | memset(conn, 0, sizeof(*conn) + dd_size); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2596 | |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 2597 | conn->dd_data = cls_conn->dd_data + sizeof(*conn); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2598 | conn->session = session; |
| 2599 | conn->cls_conn = cls_conn; |
| 2600 | conn->c_stage = ISCSI_CONN_INITIAL_STAGE; |
| 2601 | conn->id = conn_idx; |
| 2602 | conn->exp_statsn = 0; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2603 | conn->tmf_state = TMF_INITIAL; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2604 | |
| 2605 | init_timer(&conn->transport_timer); |
| 2606 | conn->transport_timer.data = (unsigned long)conn; |
| 2607 | conn->transport_timer.function = iscsi_check_transport_timeouts; |
| 2608 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2609 | INIT_LIST_HEAD(&conn->mgmtqueue); |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 2610 | INIT_LIST_HEAD(&conn->cmdqueue); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2611 | INIT_LIST_HEAD(&conn->requeue); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 2612 | INIT_WORK(&conn->xmitwork, iscsi_xmitworker); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2613 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2614 | /* allocate login_task used for the login/text sequences */ |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2615 | spin_lock_bh(&session->lock); |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 2616 | if (!__kfifo_get(session->cmdpool.queue, |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2617 | (void*)&conn->login_task, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2618 | sizeof(void*))) { |
| 2619 | spin_unlock_bh(&session->lock); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2620 | goto login_task_alloc_fail; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2621 | } |
| 2622 | spin_unlock_bh(&session->lock); |
| 2623 | |
Mike Christie | cfeb2cf | 2008-12-02 00:32:09 -0600 | [diff] [blame] | 2624 | data = (char *) __get_free_pages(GFP_KERNEL, |
| 2625 | get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); |
Mike Christie | d36ab6f | 2006-05-18 20:31:34 -0500 | [diff] [blame] | 2626 | if (!data) |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2627 | goto login_task_data_alloc_fail; |
| 2628 | conn->login_task->data = conn->data = data; |
Mike Christie | d36ab6f | 2006-05-18 20:31:34 -0500 | [diff] [blame] | 2629 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2630 | init_timer(&conn->tmf_timer); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2631 | init_waitqueue_head(&conn->ehwait); |
| 2632 | |
| 2633 | return cls_conn; |
| 2634 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2635 | login_task_data_alloc_fail: |
| 2636 | __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task, |
Mike Christie | d36ab6f | 2006-05-18 20:31:34 -0500 | [diff] [blame] | 2637 | sizeof(void*)); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2638 | login_task_alloc_fail: |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2639 | iscsi_destroy_conn(cls_conn); |
| 2640 | return NULL; |
| 2641 | } |
| 2642 | EXPORT_SYMBOL_GPL(iscsi_conn_setup); |
| 2643 | |
| 2644 | /** |
| 2645 | * iscsi_conn_teardown - teardown iscsi connection |
| 2646 | * cls_conn: iscsi class connection |
| 2647 | * |
| 2648 | * TODO: we may need to make this into a two step process |
| 2649 | * like scsi-mls remove + put host |
| 2650 | */ |
| 2651 | void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn) |
| 2652 | { |
| 2653 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 2654 | struct iscsi_session *session = conn->session; |
| 2655 | unsigned long flags; |
| 2656 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2657 | del_timer_sync(&conn->transport_timer); |
| 2658 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2659 | spin_lock_bh(&session->lock); |
| 2660 | conn->c_stage = ISCSI_CONN_CLEANUP_WAIT; |
| 2661 | if (session->leadconn == conn) { |
| 2662 | /* |
| 2663 | * leading connection? then give up on recovery. |
| 2664 | */ |
| 2665 | session->state = ISCSI_STATE_TERMINATE; |
| 2666 | wake_up(&conn->ehwait); |
| 2667 | } |
| 2668 | spin_unlock_bh(&session->lock); |
| 2669 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2670 | /* |
| 2671 | * Block until all in-progress commands for this connection |
| 2672 | * time out or fail. |
| 2673 | */ |
| 2674 | for (;;) { |
| 2675 | spin_lock_irqsave(session->host->host_lock, flags); |
| 2676 | if (!session->host->host_busy) { /* OK for ERL == 0 */ |
| 2677 | spin_unlock_irqrestore(session->host->host_lock, flags); |
| 2678 | break; |
| 2679 | } |
| 2680 | spin_unlock_irqrestore(session->host->host_lock, flags); |
| 2681 | msleep_interruptible(500); |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 2682 | iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): " |
| 2683 | "host_busy %d host_failed %d\n", |
| 2684 | session->host->host_busy, |
| 2685 | session->host->host_failed); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2686 | /* |
| 2687 | * force eh_abort() to unblock |
| 2688 | */ |
| 2689 | wake_up(&conn->ehwait); |
| 2690 | } |
| 2691 | |
Mike Christie | 779ea12 | 2007-02-28 17:32:15 -0600 | [diff] [blame] | 2692 | /* flush queued up work because we free the connection below */ |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2693 | iscsi_suspend_tx(conn); |
Mike Christie | 779ea12 | 2007-02-28 17:32:15 -0600 | [diff] [blame] | 2694 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2695 | spin_lock_bh(&session->lock); |
Mike Christie | cfeb2cf | 2008-12-02 00:32:09 -0600 | [diff] [blame] | 2696 | free_pages((unsigned long) conn->data, |
| 2697 | get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); |
Mike Christie | f3ff0c3 | 2006-07-24 15:47:50 -0500 | [diff] [blame] | 2698 | kfree(conn->persistent_address); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2699 | __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2700 | sizeof(void*)); |
Mike Christie | e072640 | 2007-07-26 12:46:48 -0500 | [diff] [blame] | 2701 | if (session->leadconn == conn) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2702 | session->leadconn = NULL; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2703 | spin_unlock_bh(&session->lock); |
| 2704 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2705 | iscsi_destroy_conn(cls_conn); |
| 2706 | } |
| 2707 | EXPORT_SYMBOL_GPL(iscsi_conn_teardown); |
| 2708 | |
| 2709 | int iscsi_conn_start(struct iscsi_cls_conn *cls_conn) |
| 2710 | { |
| 2711 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 2712 | struct iscsi_session *session = conn->session; |
| 2713 | |
Mike Christie | ffd0436 | 2006-08-31 18:09:24 -0400 | [diff] [blame] | 2714 | if (!session) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 2715 | iscsi_conn_printk(KERN_ERR, conn, |
| 2716 | "can't start unbound connection\n"); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2717 | return -EPERM; |
| 2718 | } |
| 2719 | |
Mike Christie | db98ccd | 2006-08-31 18:09:31 -0400 | [diff] [blame] | 2720 | if ((session->imm_data_en || !session->initial_r2t_en) && |
| 2721 | session->first_burst > session->max_burst) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 2722 | iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: " |
| 2723 | "first_burst %d max_burst %d\n", |
| 2724 | session->first_burst, session->max_burst); |
Mike Christie | ffd0436 | 2006-08-31 18:09:24 -0400 | [diff] [blame] | 2725 | return -EINVAL; |
| 2726 | } |
| 2727 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2728 | if (conn->ping_timeout && !conn->recv_timeout) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 2729 | iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of " |
| 2730 | "zero. Using 5 seconds\n."); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2731 | conn->recv_timeout = 5; |
| 2732 | } |
| 2733 | |
| 2734 | if (conn->recv_timeout && !conn->ping_timeout) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 2735 | iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of " |
| 2736 | "zero. Using 5 seconds.\n"); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2737 | conn->ping_timeout = 5; |
| 2738 | } |
| 2739 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2740 | spin_lock_bh(&session->lock); |
| 2741 | conn->c_stage = ISCSI_CONN_STARTED; |
| 2742 | session->state = ISCSI_STATE_LOGGED_IN; |
Mike Christie | e072640 | 2007-07-26 12:46:48 -0500 | [diff] [blame] | 2743 | session->queued_cmdsn = session->cmdsn; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2744 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2745 | conn->last_recv = jiffies; |
| 2746 | conn->last_ping = jiffies; |
| 2747 | if (conn->recv_timeout && conn->ping_timeout) |
| 2748 | mod_timer(&conn->transport_timer, |
| 2749 | jiffies + (conn->recv_timeout * HZ)); |
| 2750 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2751 | switch(conn->stop_stage) { |
| 2752 | case STOP_CONN_RECOVER: |
| 2753 | /* |
| 2754 | * unblock eh_abort() if it is blocked. re-try all |
| 2755 | * commands after successful recovery |
| 2756 | */ |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2757 | conn->stop_stage = 0; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2758 | conn->tmf_state = TMF_INITIAL; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2759 | session->age++; |
Mike Christie | 8b1d034 | 2008-01-31 13:36:53 -0600 | [diff] [blame] | 2760 | if (session->age == 16) |
| 2761 | session->age = 0; |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 2762 | break; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2763 | case STOP_CONN_TERM: |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2764 | conn->stop_stage = 0; |
| 2765 | break; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2766 | default: |
| 2767 | break; |
| 2768 | } |
| 2769 | spin_unlock_bh(&session->lock); |
| 2770 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2771 | iscsi_unblock_session(session->cls_session); |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 2772 | wake_up(&conn->ehwait); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2773 | return 0; |
| 2774 | } |
| 2775 | EXPORT_SYMBOL_GPL(iscsi_conn_start); |
| 2776 | |
| 2777 | static void |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 2778 | fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2779 | { |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 2780 | struct iscsi_task *task; |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 2781 | int i, state; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2782 | |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 2783 | for (i = 0; i < conn->session->cmds_max; i++) { |
| 2784 | task = conn->session->cmds[i]; |
| 2785 | if (task->sc) |
| 2786 | continue; |
| 2787 | |
| 2788 | if (task->state == ISCSI_TASK_FREE) |
| 2789 | continue; |
| 2790 | |
| 2791 | ISCSI_DBG_SESSION(conn->session, |
| 2792 | "failing mgmt itt 0x%x state %d\n", |
| 2793 | task->itt, task->state); |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 2794 | state = ISCSI_TASK_ABRT_SESS_RECOV; |
| 2795 | if (task->state == ISCSI_TASK_PENDING) |
| 2796 | state = ISCSI_TASK_COMPLETED; |
| 2797 | iscsi_complete_task(task, state); |
| 2798 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2799 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2800 | } |
| 2801 | |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 2802 | static void iscsi_start_session_recovery(struct iscsi_session *session, |
| 2803 | struct iscsi_conn *conn, int flag) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2804 | { |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 2805 | int old_stop_stage; |
| 2806 | |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2807 | mutex_lock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2808 | spin_lock_bh(&session->lock); |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 2809 | if (conn->stop_stage == STOP_CONN_TERM) { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2810 | spin_unlock_bh(&session->lock); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2811 | mutex_unlock(&session->eh_mutex); |
| 2812 | return; |
| 2813 | } |
| 2814 | |
| 2815 | /* |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 2816 | * When this is called for the in_login state, we only want to clean |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2817 | * up the login task and connection. We do not need to block and set |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 2818 | * the recovery state again |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 2819 | */ |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 2820 | if (flag == STOP_CONN_TERM) |
| 2821 | session->state = ISCSI_STATE_TERMINATE; |
| 2822 | else if (conn->stop_stage != STOP_CONN_RECOVER) |
| 2823 | session->state = ISCSI_STATE_IN_RECOVERY; |
Mike Christie | 26013ad | 2009-05-13 17:57:43 -0500 | [diff] [blame] | 2824 | spin_unlock_bh(&session->lock); |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 2825 | |
Mike Christie | 26013ad | 2009-05-13 17:57:43 -0500 | [diff] [blame] | 2826 | del_timer_sync(&conn->transport_timer); |
| 2827 | iscsi_suspend_tx(conn); |
| 2828 | |
| 2829 | spin_lock_bh(&session->lock); |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 2830 | old_stop_stage = conn->stop_stage; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2831 | conn->stop_stage = flag; |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 2832 | conn->c_stage = ISCSI_CONN_STOPPED; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2833 | spin_unlock_bh(&session->lock); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2834 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2835 | /* |
| 2836 | * for connection level recovery we should not calculate |
| 2837 | * header digest. conn->hdr_size used for optimization |
| 2838 | * in hdr_extract() and will be re-negotiated at |
| 2839 | * set_param() time. |
| 2840 | */ |
| 2841 | if (flag == STOP_CONN_RECOVER) { |
| 2842 | conn->hdrdgst_en = 0; |
| 2843 | conn->datadgst_en = 0; |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 2844 | if (session->state == ISCSI_STATE_IN_RECOVERY && |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 2845 | old_stop_stage != STOP_CONN_RECOVER) { |
Mike Christie | 1b2c7af | 2009-03-05 14:45:58 -0600 | [diff] [blame] | 2846 | ISCSI_DBG_SESSION(session, "blocking session\n"); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2847 | iscsi_block_session(session->cls_session); |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 2848 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2849 | } |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 2850 | |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 2851 | /* |
| 2852 | * flush queues. |
| 2853 | */ |
| 2854 | spin_lock_bh(&session->lock); |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 2855 | fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED); |
Mike Christie | 3bbaaad | 2009-05-13 17:57:46 -0500 | [diff] [blame] | 2856 | fail_mgmt_tasks(session, conn); |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 2857 | spin_unlock_bh(&session->lock); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2858 | mutex_unlock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2859 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2860 | |
| 2861 | void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) |
| 2862 | { |
| 2863 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 2864 | struct iscsi_session *session = conn->session; |
| 2865 | |
| 2866 | switch (flag) { |
| 2867 | case STOP_CONN_RECOVER: |
| 2868 | case STOP_CONN_TERM: |
| 2869 | iscsi_start_session_recovery(session, conn, flag); |
Mike Christie | 8d2860b | 2006-05-02 19:46:47 -0500 | [diff] [blame] | 2870 | break; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2871 | default: |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 2872 | iscsi_conn_printk(KERN_ERR, conn, |
| 2873 | "invalid stop flag %d\n", flag); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2874 | } |
| 2875 | } |
| 2876 | EXPORT_SYMBOL_GPL(iscsi_conn_stop); |
| 2877 | |
| 2878 | int iscsi_conn_bind(struct iscsi_cls_session *cls_session, |
| 2879 | struct iscsi_cls_conn *cls_conn, int is_leading) |
| 2880 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2881 | struct iscsi_session *session = cls_session->dd_data; |
Mike Christie | 9864404 | 2006-10-16 18:09:39 -0400 | [diff] [blame] | 2882 | struct iscsi_conn *conn = cls_conn->dd_data; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2883 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2884 | spin_lock_bh(&session->lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2885 | if (is_leading) |
| 2886 | session->leadconn = conn; |
Mike Christie | 9864404 | 2006-10-16 18:09:39 -0400 | [diff] [blame] | 2887 | spin_unlock_bh(&session->lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2888 | |
| 2889 | /* |
| 2890 | * Unblock xmitworker(), Login Phase will pass through. |
| 2891 | */ |
| 2892 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); |
| 2893 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
| 2894 | return 0; |
| 2895 | } |
| 2896 | EXPORT_SYMBOL_GPL(iscsi_conn_bind); |
| 2897 | |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 2898 | static int iscsi_switch_str_param(char **param, char *new_val_buf) |
| 2899 | { |
| 2900 | char *new_val; |
| 2901 | |
| 2902 | if (*param) { |
| 2903 | if (!strcmp(*param, new_val_buf)) |
| 2904 | return 0; |
| 2905 | } |
| 2906 | |
| 2907 | new_val = kstrdup(new_val_buf, GFP_NOIO); |
| 2908 | if (!new_val) |
| 2909 | return -ENOMEM; |
| 2910 | |
| 2911 | kfree(*param); |
| 2912 | *param = new_val; |
| 2913 | return 0; |
| 2914 | } |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 2915 | |
| 2916 | int iscsi_set_param(struct iscsi_cls_conn *cls_conn, |
| 2917 | enum iscsi_param param, char *buf, int buflen) |
| 2918 | { |
| 2919 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 2920 | struct iscsi_session *session = conn->session; |
| 2921 | uint32_t value; |
| 2922 | |
| 2923 | switch(param) { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2924 | case ISCSI_PARAM_FAST_ABORT: |
| 2925 | sscanf(buf, "%d", &session->fast_abort); |
| 2926 | break; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2927 | case ISCSI_PARAM_ABORT_TMO: |
| 2928 | sscanf(buf, "%d", &session->abort_timeout); |
| 2929 | break; |
| 2930 | case ISCSI_PARAM_LU_RESET_TMO: |
| 2931 | sscanf(buf, "%d", &session->lu_reset_timeout); |
| 2932 | break; |
| 2933 | case ISCSI_PARAM_PING_TMO: |
| 2934 | sscanf(buf, "%d", &conn->ping_timeout); |
| 2935 | break; |
| 2936 | case ISCSI_PARAM_RECV_TMO: |
| 2937 | sscanf(buf, "%d", &conn->recv_timeout); |
| 2938 | break; |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 2939 | case ISCSI_PARAM_MAX_RECV_DLENGTH: |
| 2940 | sscanf(buf, "%d", &conn->max_recv_dlength); |
| 2941 | break; |
| 2942 | case ISCSI_PARAM_MAX_XMIT_DLENGTH: |
| 2943 | sscanf(buf, "%d", &conn->max_xmit_dlength); |
| 2944 | break; |
| 2945 | case ISCSI_PARAM_HDRDGST_EN: |
| 2946 | sscanf(buf, "%d", &conn->hdrdgst_en); |
| 2947 | break; |
| 2948 | case ISCSI_PARAM_DATADGST_EN: |
| 2949 | sscanf(buf, "%d", &conn->datadgst_en); |
| 2950 | break; |
| 2951 | case ISCSI_PARAM_INITIAL_R2T_EN: |
| 2952 | sscanf(buf, "%d", &session->initial_r2t_en); |
| 2953 | break; |
| 2954 | case ISCSI_PARAM_MAX_R2T: |
| 2955 | sscanf(buf, "%d", &session->max_r2t); |
| 2956 | break; |
| 2957 | case ISCSI_PARAM_IMM_DATA_EN: |
| 2958 | sscanf(buf, "%d", &session->imm_data_en); |
| 2959 | break; |
| 2960 | case ISCSI_PARAM_FIRST_BURST: |
| 2961 | sscanf(buf, "%d", &session->first_burst); |
| 2962 | break; |
| 2963 | case ISCSI_PARAM_MAX_BURST: |
| 2964 | sscanf(buf, "%d", &session->max_burst); |
| 2965 | break; |
| 2966 | case ISCSI_PARAM_PDU_INORDER_EN: |
| 2967 | sscanf(buf, "%d", &session->pdu_inorder_en); |
| 2968 | break; |
| 2969 | case ISCSI_PARAM_DATASEQ_INORDER_EN: |
| 2970 | sscanf(buf, "%d", &session->dataseq_inorder_en); |
| 2971 | break; |
| 2972 | case ISCSI_PARAM_ERL: |
| 2973 | sscanf(buf, "%d", &session->erl); |
| 2974 | break; |
| 2975 | case ISCSI_PARAM_IFMARKER_EN: |
| 2976 | sscanf(buf, "%d", &value); |
| 2977 | BUG_ON(value); |
| 2978 | break; |
| 2979 | case ISCSI_PARAM_OFMARKER_EN: |
| 2980 | sscanf(buf, "%d", &value); |
| 2981 | BUG_ON(value); |
| 2982 | break; |
| 2983 | case ISCSI_PARAM_EXP_STATSN: |
| 2984 | sscanf(buf, "%u", &conn->exp_statsn); |
| 2985 | break; |
Mike Christie | b2c6416 | 2007-05-30 12:57:16 -0500 | [diff] [blame] | 2986 | case ISCSI_PARAM_USERNAME: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 2987 | return iscsi_switch_str_param(&session->username, buf); |
Mike Christie | b2c6416 | 2007-05-30 12:57:16 -0500 | [diff] [blame] | 2988 | case ISCSI_PARAM_USERNAME_IN: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 2989 | return iscsi_switch_str_param(&session->username_in, buf); |
Mike Christie | b2c6416 | 2007-05-30 12:57:16 -0500 | [diff] [blame] | 2990 | case ISCSI_PARAM_PASSWORD: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 2991 | return iscsi_switch_str_param(&session->password, buf); |
Mike Christie | b2c6416 | 2007-05-30 12:57:16 -0500 | [diff] [blame] | 2992 | case ISCSI_PARAM_PASSWORD_IN: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 2993 | return iscsi_switch_str_param(&session->password_in, buf); |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 2994 | case ISCSI_PARAM_TARGET_NAME: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 2995 | return iscsi_switch_str_param(&session->targetname, buf); |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 2996 | case ISCSI_PARAM_TPGT: |
| 2997 | sscanf(buf, "%d", &session->tpgt); |
| 2998 | break; |
| 2999 | case ISCSI_PARAM_PERSISTENT_PORT: |
| 3000 | sscanf(buf, "%d", &conn->persistent_port); |
| 3001 | break; |
| 3002 | case ISCSI_PARAM_PERSISTENT_ADDRESS: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 3003 | return iscsi_switch_str_param(&conn->persistent_address, buf); |
Mike Christie | 88dfd34 | 2008-05-21 15:54:16 -0500 | [diff] [blame] | 3004 | case ISCSI_PARAM_IFACE_NAME: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 3005 | return iscsi_switch_str_param(&session->ifacename, buf); |
Mike Christie | 88dfd34 | 2008-05-21 15:54:16 -0500 | [diff] [blame] | 3006 | case ISCSI_PARAM_INITIATOR_NAME: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 3007 | return iscsi_switch_str_param(&session->initiatorname, buf); |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 3008 | default: |
| 3009 | return -ENOSYS; |
| 3010 | } |
| 3011 | |
| 3012 | return 0; |
| 3013 | } |
| 3014 | EXPORT_SYMBOL_GPL(iscsi_set_param); |
| 3015 | |
| 3016 | int iscsi_session_get_param(struct iscsi_cls_session *cls_session, |
| 3017 | enum iscsi_param param, char *buf) |
| 3018 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 3019 | struct iscsi_session *session = cls_session->dd_data; |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 3020 | int len; |
| 3021 | |
| 3022 | switch(param) { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 3023 | case ISCSI_PARAM_FAST_ABORT: |
| 3024 | len = sprintf(buf, "%d\n", session->fast_abort); |
| 3025 | break; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 3026 | case ISCSI_PARAM_ABORT_TMO: |
| 3027 | len = sprintf(buf, "%d\n", session->abort_timeout); |
| 3028 | break; |
| 3029 | case ISCSI_PARAM_LU_RESET_TMO: |
| 3030 | len = sprintf(buf, "%d\n", session->lu_reset_timeout); |
| 3031 | break; |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 3032 | case ISCSI_PARAM_INITIAL_R2T_EN: |
| 3033 | len = sprintf(buf, "%d\n", session->initial_r2t_en); |
| 3034 | break; |
| 3035 | case ISCSI_PARAM_MAX_R2T: |
| 3036 | len = sprintf(buf, "%hu\n", session->max_r2t); |
| 3037 | break; |
| 3038 | case ISCSI_PARAM_IMM_DATA_EN: |
| 3039 | len = sprintf(buf, "%d\n", session->imm_data_en); |
| 3040 | break; |
| 3041 | case ISCSI_PARAM_FIRST_BURST: |
| 3042 | len = sprintf(buf, "%u\n", session->first_burst); |
| 3043 | break; |
| 3044 | case ISCSI_PARAM_MAX_BURST: |
| 3045 | len = sprintf(buf, "%u\n", session->max_burst); |
| 3046 | break; |
| 3047 | case ISCSI_PARAM_PDU_INORDER_EN: |
| 3048 | len = sprintf(buf, "%d\n", session->pdu_inorder_en); |
| 3049 | break; |
| 3050 | case ISCSI_PARAM_DATASEQ_INORDER_EN: |
| 3051 | len = sprintf(buf, "%d\n", session->dataseq_inorder_en); |
| 3052 | break; |
| 3053 | case ISCSI_PARAM_ERL: |
| 3054 | len = sprintf(buf, "%d\n", session->erl); |
| 3055 | break; |
| 3056 | case ISCSI_PARAM_TARGET_NAME: |
| 3057 | len = sprintf(buf, "%s\n", session->targetname); |
| 3058 | break; |
| 3059 | case ISCSI_PARAM_TPGT: |
| 3060 | len = sprintf(buf, "%d\n", session->tpgt); |
| 3061 | break; |
Mike Christie | b2c6416 | 2007-05-30 12:57:16 -0500 | [diff] [blame] | 3062 | case ISCSI_PARAM_USERNAME: |
| 3063 | len = sprintf(buf, "%s\n", session->username); |
| 3064 | break; |
| 3065 | case ISCSI_PARAM_USERNAME_IN: |
| 3066 | len = sprintf(buf, "%s\n", session->username_in); |
| 3067 | break; |
| 3068 | case ISCSI_PARAM_PASSWORD: |
| 3069 | len = sprintf(buf, "%s\n", session->password); |
| 3070 | break; |
| 3071 | case ISCSI_PARAM_PASSWORD_IN: |
| 3072 | len = sprintf(buf, "%s\n", session->password_in); |
| 3073 | break; |
Mike Christie | 88dfd34 | 2008-05-21 15:54:16 -0500 | [diff] [blame] | 3074 | case ISCSI_PARAM_IFACE_NAME: |
| 3075 | len = sprintf(buf, "%s\n", session->ifacename); |
| 3076 | break; |
| 3077 | case ISCSI_PARAM_INITIATOR_NAME: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 3078 | len = sprintf(buf, "%s\n", session->initiatorname); |
Mike Christie | 88dfd34 | 2008-05-21 15:54:16 -0500 | [diff] [blame] | 3079 | break; |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 3080 | default: |
| 3081 | return -ENOSYS; |
| 3082 | } |
| 3083 | |
| 3084 | return len; |
| 3085 | } |
| 3086 | EXPORT_SYMBOL_GPL(iscsi_session_get_param); |
| 3087 | |
| 3088 | int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn, |
| 3089 | enum iscsi_param param, char *buf) |
| 3090 | { |
| 3091 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 3092 | int len; |
| 3093 | |
| 3094 | switch(param) { |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 3095 | case ISCSI_PARAM_PING_TMO: |
| 3096 | len = sprintf(buf, "%u\n", conn->ping_timeout); |
| 3097 | break; |
| 3098 | case ISCSI_PARAM_RECV_TMO: |
| 3099 | len = sprintf(buf, "%u\n", conn->recv_timeout); |
| 3100 | break; |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 3101 | case ISCSI_PARAM_MAX_RECV_DLENGTH: |
| 3102 | len = sprintf(buf, "%u\n", conn->max_recv_dlength); |
| 3103 | break; |
| 3104 | case ISCSI_PARAM_MAX_XMIT_DLENGTH: |
| 3105 | len = sprintf(buf, "%u\n", conn->max_xmit_dlength); |
| 3106 | break; |
| 3107 | case ISCSI_PARAM_HDRDGST_EN: |
| 3108 | len = sprintf(buf, "%d\n", conn->hdrdgst_en); |
| 3109 | break; |
| 3110 | case ISCSI_PARAM_DATADGST_EN: |
| 3111 | len = sprintf(buf, "%d\n", conn->datadgst_en); |
| 3112 | break; |
| 3113 | case ISCSI_PARAM_IFMARKER_EN: |
| 3114 | len = sprintf(buf, "%d\n", conn->ifmarker_en); |
| 3115 | break; |
| 3116 | case ISCSI_PARAM_OFMARKER_EN: |
| 3117 | len = sprintf(buf, "%d\n", conn->ofmarker_en); |
| 3118 | break; |
| 3119 | case ISCSI_PARAM_EXP_STATSN: |
| 3120 | len = sprintf(buf, "%u\n", conn->exp_statsn); |
| 3121 | break; |
| 3122 | case ISCSI_PARAM_PERSISTENT_PORT: |
| 3123 | len = sprintf(buf, "%d\n", conn->persistent_port); |
| 3124 | break; |
| 3125 | case ISCSI_PARAM_PERSISTENT_ADDRESS: |
| 3126 | len = sprintf(buf, "%s\n", conn->persistent_address); |
| 3127 | break; |
| 3128 | default: |
| 3129 | return -ENOSYS; |
| 3130 | } |
| 3131 | |
| 3132 | return len; |
| 3133 | } |
| 3134 | EXPORT_SYMBOL_GPL(iscsi_conn_get_param); |
| 3135 | |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 3136 | int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param, |
| 3137 | char *buf) |
| 3138 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 3139 | struct iscsi_host *ihost = shost_priv(shost); |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 3140 | int len; |
| 3141 | |
| 3142 | switch (param) { |
Mike Christie | d8196ed | 2007-05-30 12:57:25 -0500 | [diff] [blame] | 3143 | case ISCSI_HOST_PARAM_NETDEV_NAME: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 3144 | len = sprintf(buf, "%s\n", ihost->netdev); |
Mike Christie | d8196ed | 2007-05-30 12:57:25 -0500 | [diff] [blame] | 3145 | break; |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 3146 | case ISCSI_HOST_PARAM_HWADDRESS: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 3147 | len = sprintf(buf, "%s\n", ihost->hwaddress); |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 3148 | break; |
Mike Christie | 8ad5781 | 2007-05-30 12:57:13 -0500 | [diff] [blame] | 3149 | case ISCSI_HOST_PARAM_INITIATOR_NAME: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 3150 | len = sprintf(buf, "%s\n", ihost->initiatorname); |
Mike Christie | 8ad5781 | 2007-05-30 12:57:13 -0500 | [diff] [blame] | 3151 | break; |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 3152 | case ISCSI_HOST_PARAM_IPADDRESS: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 3153 | len = sprintf(buf, "%s\n", ihost->local_address); |
Mike Christie | 88dfd34 | 2008-05-21 15:54:16 -0500 | [diff] [blame] | 3154 | break; |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 3155 | default: |
| 3156 | return -ENOSYS; |
| 3157 | } |
| 3158 | |
| 3159 | return len; |
| 3160 | } |
| 3161 | EXPORT_SYMBOL_GPL(iscsi_host_get_param); |
| 3162 | |
| 3163 | int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param, |
| 3164 | char *buf, int buflen) |
| 3165 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 3166 | struct iscsi_host *ihost = shost_priv(shost); |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 3167 | |
| 3168 | switch (param) { |
Mike Christie | d8196ed | 2007-05-30 12:57:25 -0500 | [diff] [blame] | 3169 | case ISCSI_HOST_PARAM_NETDEV_NAME: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 3170 | return iscsi_switch_str_param(&ihost->netdev, buf); |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 3171 | case ISCSI_HOST_PARAM_HWADDRESS: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 3172 | return iscsi_switch_str_param(&ihost->hwaddress, buf); |
Mike Christie | 8ad5781 | 2007-05-30 12:57:13 -0500 | [diff] [blame] | 3173 | case ISCSI_HOST_PARAM_INITIATOR_NAME: |
Mike Christie | 5700b1a | 2009-05-13 17:57:40 -0500 | [diff] [blame] | 3174 | return iscsi_switch_str_param(&ihost->initiatorname, buf); |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 3175 | default: |
| 3176 | return -ENOSYS; |
| 3177 | } |
| 3178 | |
| 3179 | return 0; |
| 3180 | } |
| 3181 | EXPORT_SYMBOL_GPL(iscsi_host_set_param); |
| 3182 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 3183 | MODULE_AUTHOR("Mike Christie"); |
| 3184 | MODULE_DESCRIPTION("iSCSI library functions"); |
| 3185 | MODULE_LICENSE("GPL"); |