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