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