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