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