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