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