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