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