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