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