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