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