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