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