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