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