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 | |
| 336 | switch(opcode) { |
| 337 | case ISCSI_OP_LOGIN_RSP: |
| 338 | case ISCSI_OP_TEXT_RSP: |
| 339 | case ISCSI_OP_LOGOUT_RSP: |
| 340 | rc = iscsi_check_assign_cmdsn(session, |
| 341 | (struct iscsi_nopin*)hdr); |
| 342 | if (rc) |
| 343 | break; |
| 344 | |
| 345 | rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen); |
| 346 | list_del(&mtask->running); |
| 347 | if (conn->login_mtask != mtask) |
| 348 | __kfifo_put(session->mgmtpool.queue, |
| 349 | (void*)&mtask, sizeof(void*)); |
| 350 | break; |
| 351 | case ISCSI_OP_SCSI_TMFUNC_RSP: |
| 352 | rc = iscsi_check_assign_cmdsn(session, |
| 353 | (struct iscsi_nopin*)hdr); |
| 354 | if (rc) |
| 355 | break; |
| 356 | |
| 357 | if (datalen) { |
| 358 | rc = ISCSI_ERR_PROTO; |
| 359 | break; |
| 360 | } |
| 361 | conn->tmfrsp_pdus_cnt++; |
| 362 | if (conn->tmabort_state == TMABORT_INITIAL) { |
| 363 | conn->tmabort_state = |
| 364 | ((struct iscsi_tm_rsp *)hdr)-> |
| 365 | response == ISCSI_TMF_RSP_COMPLETE ? |
| 366 | TMABORT_SUCCESS:TMABORT_FAILED; |
| 367 | /* unblock eh_abort() */ |
| 368 | wake_up(&conn->ehwait); |
| 369 | } |
| 370 | break; |
| 371 | case ISCSI_OP_NOOP_IN: |
| 372 | if (hdr->ttt != ISCSI_RESERVED_TAG) { |
| 373 | rc = ISCSI_ERR_PROTO; |
| 374 | break; |
| 375 | } |
| 376 | rc = iscsi_check_assign_cmdsn(session, |
| 377 | (struct iscsi_nopin*)hdr); |
| 378 | if (rc) |
| 379 | break; |
| 380 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; |
| 381 | |
| 382 | rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen); |
| 383 | list_del(&mtask->running); |
| 384 | if (conn->login_mtask != mtask) |
| 385 | __kfifo_put(session->mgmtpool.queue, |
| 386 | (void*)&mtask, sizeof(void*)); |
| 387 | break; |
| 388 | default: |
| 389 | rc = ISCSI_ERR_BAD_OPCODE; |
| 390 | break; |
| 391 | } |
| 392 | } else if (itt == ISCSI_RESERVED_TAG) { |
| 393 | switch(opcode) { |
| 394 | case ISCSI_OP_NOOP_IN: |
| 395 | if (!datalen) { |
| 396 | rc = iscsi_check_assign_cmdsn(session, |
| 397 | (struct iscsi_nopin*)hdr); |
| 398 | if (!rc && hdr->ttt != ISCSI_RESERVED_TAG) |
| 399 | rc = iscsi_recv_pdu(conn->cls_conn, |
| 400 | hdr, NULL, 0); |
| 401 | } else |
| 402 | rc = ISCSI_ERR_PROTO; |
| 403 | break; |
| 404 | case ISCSI_OP_REJECT: |
| 405 | /* we need sth like iscsi_reject_rsp()*/ |
| 406 | case ISCSI_OP_ASYNC_EVENT: |
| 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 | |
| 417 | return rc; |
| 418 | } |
| 419 | EXPORT_SYMBOL_GPL(__iscsi_complete_pdu); |
| 420 | |
| 421 | int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
| 422 | char *data, int datalen) |
| 423 | { |
| 424 | int rc; |
| 425 | |
| 426 | spin_lock(&conn->session->lock); |
| 427 | rc = __iscsi_complete_pdu(conn, hdr, data, datalen); |
| 428 | spin_unlock(&conn->session->lock); |
| 429 | return rc; |
| 430 | } |
| 431 | EXPORT_SYMBOL_GPL(iscsi_complete_pdu); |
| 432 | |
| 433 | /* verify itt (itt encoding: age+cid+itt) */ |
| 434 | int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
| 435 | uint32_t *ret_itt) |
| 436 | { |
| 437 | struct iscsi_session *session = conn->session; |
| 438 | struct iscsi_cmd_task *ctask; |
| 439 | uint32_t itt; |
| 440 | |
| 441 | if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) { |
| 442 | if ((hdr->itt & ISCSI_AGE_MASK) != |
| 443 | (session->age << ISCSI_AGE_SHIFT)) { |
Or Gerlitz | be2df72 | 2006-05-02 19:46:43 -0500 | [diff] [blame^] | 444 | printk(KERN_ERR "iscsi: received itt %x expected " |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 445 | "session age (%x)\n", hdr->itt, |
| 446 | session->age & ISCSI_AGE_MASK); |
| 447 | return ISCSI_ERR_BAD_ITT; |
| 448 | } |
| 449 | |
| 450 | if ((hdr->itt & ISCSI_CID_MASK) != |
| 451 | (conn->id << ISCSI_CID_SHIFT)) { |
Or Gerlitz | be2df72 | 2006-05-02 19:46:43 -0500 | [diff] [blame^] | 452 | printk(KERN_ERR "iscsi: received itt %x, expected " |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 453 | "CID (%x)\n", hdr->itt, conn->id); |
| 454 | return ISCSI_ERR_BAD_ITT; |
| 455 | } |
| 456 | itt = hdr->itt & ISCSI_ITT_MASK; |
| 457 | } else |
| 458 | itt = hdr->itt; |
| 459 | |
| 460 | if (itt < session->cmds_max) { |
| 461 | ctask = session->cmds[itt]; |
| 462 | |
| 463 | if (!ctask->sc) { |
Or Gerlitz | be2df72 | 2006-05-02 19:46:43 -0500 | [diff] [blame^] | 464 | printk(KERN_INFO "iscsi: dropping ctask with " |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 465 | "itt 0x%x\n", ctask->itt); |
| 466 | /* force drop */ |
| 467 | return ISCSI_ERR_NO_SCSI_CMD; |
| 468 | } |
| 469 | |
| 470 | if (ctask->sc->SCp.phase != session->age) { |
Or Gerlitz | be2df72 | 2006-05-02 19:46:43 -0500 | [diff] [blame^] | 471 | printk(KERN_ERR "iscsi: ctask's session age %d, " |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 472 | "expected %d\n", ctask->sc->SCp.phase, |
| 473 | session->age); |
| 474 | return ISCSI_ERR_SESSION_FAILED; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | *ret_itt = itt; |
| 479 | return 0; |
| 480 | } |
| 481 | EXPORT_SYMBOL_GPL(iscsi_verify_itt); |
| 482 | |
| 483 | void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err) |
| 484 | { |
| 485 | struct iscsi_session *session = conn->session; |
| 486 | unsigned long flags; |
| 487 | |
| 488 | spin_lock_irqsave(&session->lock, flags); |
| 489 | if (session->conn_cnt == 1 || session->leadconn == conn) |
| 490 | session->state = ISCSI_STATE_FAILED; |
| 491 | spin_unlock_irqrestore(&session->lock, flags); |
| 492 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
| 493 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); |
| 494 | iscsi_conn_error(conn->cls_conn, err); |
| 495 | } |
| 496 | EXPORT_SYMBOL_GPL(iscsi_conn_failure); |
| 497 | |
| 498 | /** |
| 499 | * iscsi_data_xmit - xmit any command into the scheduled connection |
| 500 | * @conn: iscsi connection |
| 501 | * |
| 502 | * Notes: |
| 503 | * The function can return -EAGAIN in which case the caller must |
| 504 | * re-schedule it again later or recover. '0' return code means |
| 505 | * successful xmit. |
| 506 | **/ |
| 507 | static int iscsi_data_xmit(struct iscsi_conn *conn) |
| 508 | { |
| 509 | struct iscsi_transport *tt; |
| 510 | |
| 511 | if (unlikely(conn->suspend_tx)) { |
| 512 | debug_scsi("conn %d Tx suspended!\n", conn->id); |
| 513 | return 0; |
| 514 | } |
| 515 | tt = conn->session->tt; |
| 516 | |
| 517 | /* |
| 518 | * Transmit in the following order: |
| 519 | * |
| 520 | * 1) un-finished xmit (ctask or mtask) |
| 521 | * 2) immediate control PDUs |
| 522 | * 3) write data |
| 523 | * 4) SCSI commands |
| 524 | * 5) non-immediate control PDUs |
| 525 | * |
| 526 | * No need to lock around __kfifo_get as long as |
| 527 | * there's one producer and one consumer. |
| 528 | */ |
| 529 | |
| 530 | BUG_ON(conn->ctask && conn->mtask); |
| 531 | |
| 532 | if (conn->ctask) { |
| 533 | if (tt->xmit_cmd_task(conn, conn->ctask)) |
| 534 | goto again; |
| 535 | /* done with this in-progress ctask */ |
| 536 | conn->ctask = NULL; |
| 537 | } |
| 538 | if (conn->mtask) { |
| 539 | if (tt->xmit_mgmt_task(conn, conn->mtask)) |
| 540 | goto again; |
| 541 | /* done with this in-progress mtask */ |
| 542 | conn->mtask = NULL; |
| 543 | } |
| 544 | |
| 545 | /* process immediate first */ |
| 546 | if (unlikely(__kfifo_len(conn->immqueue))) { |
| 547 | while (__kfifo_get(conn->immqueue, (void*)&conn->mtask, |
| 548 | sizeof(void*))) { |
| 549 | list_add_tail(&conn->mtask->running, |
| 550 | &conn->mgmt_run_list); |
| 551 | if (tt->xmit_mgmt_task(conn, conn->mtask)) |
| 552 | goto again; |
| 553 | } |
| 554 | /* done with this mtask */ |
| 555 | conn->mtask = NULL; |
| 556 | } |
| 557 | |
| 558 | /* process command queue */ |
| 559 | while (__kfifo_get(conn->xmitqueue, (void*)&conn->ctask, |
| 560 | sizeof(void*))) { |
| 561 | /* |
| 562 | * iscsi tcp may readd the task to the xmitqueue to send |
| 563 | * write data |
| 564 | */ |
| 565 | if (list_empty(&conn->ctask->running)) |
| 566 | list_add_tail(&conn->ctask->running, &conn->run_list); |
| 567 | if (tt->xmit_cmd_task(conn, conn->ctask)) |
| 568 | goto again; |
| 569 | } |
| 570 | /* done with this ctask */ |
| 571 | conn->ctask = NULL; |
| 572 | |
| 573 | /* process the rest control plane PDUs, if any */ |
| 574 | if (unlikely(__kfifo_len(conn->mgmtqueue))) { |
| 575 | while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask, |
| 576 | sizeof(void*))) { |
| 577 | list_add_tail(&conn->mtask->running, |
| 578 | &conn->mgmt_run_list); |
| 579 | if (tt->xmit_mgmt_task(conn, conn->mtask)) |
| 580 | goto again; |
| 581 | } |
| 582 | /* done with this mtask */ |
| 583 | conn->mtask = NULL; |
| 584 | } |
| 585 | |
| 586 | return 0; |
| 587 | |
| 588 | again: |
| 589 | if (unlikely(conn->suspend_tx)) |
| 590 | return 0; |
| 591 | |
| 592 | return -EAGAIN; |
| 593 | } |
| 594 | |
| 595 | static void iscsi_xmitworker(void *data) |
| 596 | { |
| 597 | struct iscsi_conn *conn = data; |
| 598 | |
| 599 | /* |
| 600 | * serialize Xmit worker on a per-connection basis. |
| 601 | */ |
| 602 | mutex_lock(&conn->xmitmutex); |
| 603 | if (iscsi_data_xmit(conn)) |
| 604 | scsi_queue_work(conn->session->host, &conn->xmitwork); |
| 605 | mutex_unlock(&conn->xmitmutex); |
| 606 | } |
| 607 | |
| 608 | enum { |
| 609 | FAILURE_BAD_HOST = 1, |
| 610 | FAILURE_SESSION_FAILED, |
| 611 | FAILURE_SESSION_FREED, |
| 612 | FAILURE_WINDOW_CLOSED, |
| 613 | FAILURE_SESSION_TERMINATE, |
| 614 | FAILURE_SESSION_RECOVERY_TIMEOUT, |
| 615 | }; |
| 616 | |
| 617 | int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *)) |
| 618 | { |
| 619 | struct Scsi_Host *host; |
| 620 | int reason = 0; |
| 621 | struct iscsi_session *session; |
| 622 | struct iscsi_conn *conn; |
| 623 | struct iscsi_cmd_task *ctask = NULL; |
| 624 | |
| 625 | sc->scsi_done = done; |
| 626 | sc->result = 0; |
| 627 | |
| 628 | host = sc->device->host; |
| 629 | session = iscsi_hostdata(host->hostdata); |
| 630 | |
| 631 | spin_lock(&session->lock); |
| 632 | |
| 633 | if (session->state != ISCSI_STATE_LOGGED_IN) { |
| 634 | if (session->recovery_failed) { |
| 635 | reason = FAILURE_SESSION_RECOVERY_TIMEOUT; |
| 636 | goto fault; |
| 637 | } else if (session->state == ISCSI_STATE_FAILED) { |
| 638 | reason = FAILURE_SESSION_FAILED; |
| 639 | goto reject; |
| 640 | } else if (session->state == ISCSI_STATE_TERMINATE) { |
| 641 | reason = FAILURE_SESSION_TERMINATE; |
| 642 | goto fault; |
| 643 | } |
| 644 | reason = FAILURE_SESSION_FREED; |
| 645 | goto fault; |
| 646 | } |
| 647 | |
| 648 | /* |
| 649 | * Check for iSCSI window and take care of CmdSN wrap-around |
| 650 | */ |
| 651 | if ((int)(session->max_cmdsn - session->cmdsn) < 0) { |
| 652 | reason = FAILURE_WINDOW_CLOSED; |
| 653 | goto reject; |
| 654 | } |
| 655 | |
| 656 | conn = session->leadconn; |
| 657 | |
| 658 | __kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*)); |
| 659 | sc->SCp.phase = session->age; |
| 660 | sc->SCp.ptr = (char *)ctask; |
| 661 | |
| 662 | ctask->mtask = NULL; |
| 663 | ctask->conn = conn; |
| 664 | ctask->sc = sc; |
| 665 | INIT_LIST_HEAD(&ctask->running); |
| 666 | ctask->total_length = sc->request_bufflen; |
| 667 | iscsi_prep_scsi_cmd_pdu(ctask); |
| 668 | |
| 669 | session->tt->init_cmd_task(ctask); |
| 670 | |
| 671 | __kfifo_put(conn->xmitqueue, (void*)&ctask, sizeof(void*)); |
| 672 | debug_scsi( |
| 673 | "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n", |
| 674 | sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read", |
| 675 | conn->id, (long)sc, ctask->itt, sc->request_bufflen, |
| 676 | session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1); |
| 677 | spin_unlock(&session->lock); |
| 678 | |
| 679 | scsi_queue_work(host, &conn->xmitwork); |
| 680 | return 0; |
| 681 | |
| 682 | reject: |
| 683 | spin_unlock(&session->lock); |
| 684 | debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason); |
| 685 | return SCSI_MLQUEUE_HOST_BUSY; |
| 686 | |
| 687 | fault: |
| 688 | spin_unlock(&session->lock); |
Or Gerlitz | be2df72 | 2006-05-02 19:46:43 -0500 | [diff] [blame^] | 689 | printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n", |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 690 | sc->cmnd[0], reason); |
| 691 | sc->result = (DID_NO_CONNECT << 16); |
| 692 | sc->resid = sc->request_bufflen; |
| 693 | sc->scsi_done(sc); |
| 694 | return 0; |
| 695 | } |
| 696 | EXPORT_SYMBOL_GPL(iscsi_queuecommand); |
| 697 | |
| 698 | int iscsi_change_queue_depth(struct scsi_device *sdev, int depth) |
| 699 | { |
| 700 | if (depth > ISCSI_MAX_CMD_PER_LUN) |
| 701 | depth = ISCSI_MAX_CMD_PER_LUN; |
| 702 | scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); |
| 703 | return sdev->queue_depth; |
| 704 | } |
| 705 | EXPORT_SYMBOL_GPL(iscsi_change_queue_depth); |
| 706 | |
| 707 | static int |
| 708 | iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
| 709 | char *data, uint32_t data_size) |
| 710 | { |
| 711 | struct iscsi_session *session = conn->session; |
| 712 | struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr; |
| 713 | struct iscsi_mgmt_task *mtask; |
| 714 | |
| 715 | spin_lock_bh(&session->lock); |
| 716 | if (session->state == ISCSI_STATE_TERMINATE) { |
| 717 | spin_unlock_bh(&session->lock); |
| 718 | return -EPERM; |
| 719 | } |
| 720 | if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) || |
| 721 | hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE)) |
| 722 | /* |
| 723 | * Login and Text are sent serially, in |
| 724 | * request-followed-by-response sequence. |
| 725 | * Same mtask can be used. Same ITT must be used. |
| 726 | * Note that login_mtask is preallocated at conn_create(). |
| 727 | */ |
| 728 | mtask = conn->login_mtask; |
| 729 | else { |
| 730 | BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); |
| 731 | BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); |
| 732 | |
| 733 | if (!__kfifo_get(session->mgmtpool.queue, |
| 734 | (void*)&mtask, sizeof(void*))) { |
| 735 | spin_unlock_bh(&session->lock); |
| 736 | return -ENOSPC; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * pre-format CmdSN and ExpStatSN for outgoing PDU. |
| 742 | */ |
| 743 | if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) { |
| 744 | hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) | |
| 745 | (session->age << ISCSI_AGE_SHIFT); |
| 746 | nop->cmdsn = cpu_to_be32(session->cmdsn); |
| 747 | if (conn->c_stage == ISCSI_CONN_STARTED && |
| 748 | !(hdr->opcode & ISCSI_OP_IMMEDIATE)) |
| 749 | session->cmdsn++; |
| 750 | } else |
| 751 | /* do not advance CmdSN */ |
| 752 | nop->cmdsn = cpu_to_be32(session->cmdsn); |
| 753 | |
| 754 | nop->exp_statsn = cpu_to_be32(conn->exp_statsn); |
| 755 | |
| 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; |
| 1261 | mtask->data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, |
| 1262 | GFP_KERNEL); |
| 1263 | if (!mtask->data) { |
| 1264 | int j; |
| 1265 | |
| 1266 | for (j = 0; j < cmd_i; j++) |
| 1267 | kfree(session->mgmt_cmds[j]->data); |
| 1268 | goto immdata_alloc_fail; |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | if (scsi_add_host(shost, NULL)) |
| 1273 | goto add_host_fail; |
| 1274 | |
| 1275 | cls_session = iscsi_create_session(shost, iscsit, 0); |
| 1276 | if (!cls_session) |
| 1277 | goto cls_session_fail; |
| 1278 | *(unsigned long*)shost->hostdata = (unsigned long)cls_session; |
| 1279 | |
| 1280 | return cls_session; |
| 1281 | |
| 1282 | cls_session_fail: |
| 1283 | scsi_remove_host(shost); |
| 1284 | add_host_fail: |
| 1285 | for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) |
| 1286 | kfree(session->mgmt_cmds[cmd_i]->data); |
| 1287 | immdata_alloc_fail: |
| 1288 | iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds); |
| 1289 | mgmtpool_alloc_fail: |
| 1290 | iscsi_pool_free(&session->cmdpool, (void**)session->cmds); |
| 1291 | cmdpool_alloc_fail: |
| 1292 | scsi_host_put(shost); |
| 1293 | return NULL; |
| 1294 | } |
| 1295 | EXPORT_SYMBOL_GPL(iscsi_session_setup); |
| 1296 | |
| 1297 | /** |
| 1298 | * iscsi_session_teardown - destroy session, host, and cls_session |
| 1299 | * shost: scsi host |
| 1300 | * |
| 1301 | * This can be used by software iscsi_transports that allocate |
| 1302 | * a session per scsi host. |
| 1303 | **/ |
| 1304 | void iscsi_session_teardown(struct iscsi_cls_session *cls_session) |
| 1305 | { |
| 1306 | struct Scsi_Host *shost = iscsi_session_to_shost(cls_session); |
| 1307 | struct iscsi_session *session = iscsi_hostdata(shost->hostdata); |
| 1308 | int cmd_i; |
| 1309 | |
| 1310 | scsi_remove_host(shost); |
| 1311 | |
| 1312 | for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) |
| 1313 | kfree(session->mgmt_cmds[cmd_i]->data); |
| 1314 | |
| 1315 | iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds); |
| 1316 | iscsi_pool_free(&session->cmdpool, (void**)session->cmds); |
| 1317 | |
| 1318 | iscsi_destroy_session(cls_session); |
| 1319 | scsi_host_put(shost); |
| 1320 | } |
| 1321 | EXPORT_SYMBOL_GPL(iscsi_session_teardown); |
| 1322 | |
| 1323 | /** |
| 1324 | * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn |
| 1325 | * @cls_session: iscsi_cls_session |
| 1326 | * @conn_idx: cid |
| 1327 | **/ |
| 1328 | struct iscsi_cls_conn * |
| 1329 | iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx) |
| 1330 | { |
| 1331 | struct iscsi_session *session = class_to_transport_session(cls_session); |
| 1332 | struct iscsi_conn *conn; |
| 1333 | struct iscsi_cls_conn *cls_conn; |
| 1334 | |
| 1335 | cls_conn = iscsi_create_conn(cls_session, conn_idx); |
| 1336 | if (!cls_conn) |
| 1337 | return NULL; |
| 1338 | conn = cls_conn->dd_data; |
| 1339 | memset(conn, 0, sizeof(*conn)); |
| 1340 | |
| 1341 | conn->session = session; |
| 1342 | conn->cls_conn = cls_conn; |
| 1343 | conn->c_stage = ISCSI_CONN_INITIAL_STAGE; |
| 1344 | conn->id = conn_idx; |
| 1345 | conn->exp_statsn = 0; |
| 1346 | conn->tmabort_state = TMABORT_INITIAL; |
| 1347 | INIT_LIST_HEAD(&conn->run_list); |
| 1348 | INIT_LIST_HEAD(&conn->mgmt_run_list); |
| 1349 | |
| 1350 | /* initialize general xmit PDU commands queue */ |
| 1351 | conn->xmitqueue = kfifo_alloc(session->cmds_max * sizeof(void*), |
| 1352 | GFP_KERNEL, NULL); |
| 1353 | if (conn->xmitqueue == ERR_PTR(-ENOMEM)) |
| 1354 | goto xmitqueue_alloc_fail; |
| 1355 | |
| 1356 | /* initialize general immediate & non-immediate PDU commands queue */ |
| 1357 | conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*), |
| 1358 | GFP_KERNEL, NULL); |
| 1359 | if (conn->immqueue == ERR_PTR(-ENOMEM)) |
| 1360 | goto immqueue_alloc_fail; |
| 1361 | |
| 1362 | conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*), |
| 1363 | GFP_KERNEL, NULL); |
| 1364 | if (conn->mgmtqueue == ERR_PTR(-ENOMEM)) |
| 1365 | goto mgmtqueue_alloc_fail; |
| 1366 | |
| 1367 | INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn); |
| 1368 | |
| 1369 | /* allocate login_mtask used for the login/text sequences */ |
| 1370 | spin_lock_bh(&session->lock); |
| 1371 | if (!__kfifo_get(session->mgmtpool.queue, |
| 1372 | (void*)&conn->login_mtask, |
| 1373 | sizeof(void*))) { |
| 1374 | spin_unlock_bh(&session->lock); |
| 1375 | goto login_mtask_alloc_fail; |
| 1376 | } |
| 1377 | spin_unlock_bh(&session->lock); |
| 1378 | |
| 1379 | init_timer(&conn->tmabort_timer); |
| 1380 | mutex_init(&conn->xmitmutex); |
| 1381 | init_waitqueue_head(&conn->ehwait); |
| 1382 | |
| 1383 | return cls_conn; |
| 1384 | |
| 1385 | login_mtask_alloc_fail: |
| 1386 | kfifo_free(conn->mgmtqueue); |
| 1387 | mgmtqueue_alloc_fail: |
| 1388 | kfifo_free(conn->immqueue); |
| 1389 | immqueue_alloc_fail: |
| 1390 | kfifo_free(conn->xmitqueue); |
| 1391 | xmitqueue_alloc_fail: |
| 1392 | iscsi_destroy_conn(cls_conn); |
| 1393 | return NULL; |
| 1394 | } |
| 1395 | EXPORT_SYMBOL_GPL(iscsi_conn_setup); |
| 1396 | |
| 1397 | /** |
| 1398 | * iscsi_conn_teardown - teardown iscsi connection |
| 1399 | * cls_conn: iscsi class connection |
| 1400 | * |
| 1401 | * TODO: we may need to make this into a two step process |
| 1402 | * like scsi-mls remove + put host |
| 1403 | */ |
| 1404 | void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn) |
| 1405 | { |
| 1406 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 1407 | struct iscsi_session *session = conn->session; |
| 1408 | unsigned long flags; |
| 1409 | |
| 1410 | mutex_lock(&conn->xmitmutex); |
| 1411 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
| 1412 | if (conn->c_stage == ISCSI_CONN_INITIAL_STAGE) { |
| 1413 | if (session->tt->suspend_conn_recv) |
| 1414 | session->tt->suspend_conn_recv(conn); |
| 1415 | |
| 1416 | session->tt->terminate_conn(conn); |
| 1417 | } |
| 1418 | |
| 1419 | spin_lock_bh(&session->lock); |
| 1420 | conn->c_stage = ISCSI_CONN_CLEANUP_WAIT; |
| 1421 | if (session->leadconn == conn) { |
| 1422 | /* |
| 1423 | * leading connection? then give up on recovery. |
| 1424 | */ |
| 1425 | session->state = ISCSI_STATE_TERMINATE; |
| 1426 | wake_up(&conn->ehwait); |
| 1427 | } |
| 1428 | spin_unlock_bh(&session->lock); |
| 1429 | |
| 1430 | mutex_unlock(&conn->xmitmutex); |
| 1431 | |
| 1432 | /* |
| 1433 | * Block until all in-progress commands for this connection |
| 1434 | * time out or fail. |
| 1435 | */ |
| 1436 | for (;;) { |
| 1437 | spin_lock_irqsave(session->host->host_lock, flags); |
| 1438 | if (!session->host->host_busy) { /* OK for ERL == 0 */ |
| 1439 | spin_unlock_irqrestore(session->host->host_lock, flags); |
| 1440 | break; |
| 1441 | } |
| 1442 | spin_unlock_irqrestore(session->host->host_lock, flags); |
| 1443 | msleep_interruptible(500); |
Or Gerlitz | be2df72 | 2006-05-02 19:46:43 -0500 | [diff] [blame^] | 1444 | printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d " |
| 1445 | "host_failed %d\n", session->host->host_busy, |
| 1446 | session->host->host_failed); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1447 | /* |
| 1448 | * force eh_abort() to unblock |
| 1449 | */ |
| 1450 | wake_up(&conn->ehwait); |
| 1451 | } |
| 1452 | |
| 1453 | spin_lock_bh(&session->lock); |
| 1454 | __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask, |
| 1455 | sizeof(void*)); |
| 1456 | list_del(&conn->item); |
| 1457 | if (list_empty(&session->connections)) |
| 1458 | session->leadconn = NULL; |
| 1459 | if (session->leadconn && session->leadconn == conn) |
| 1460 | session->leadconn = container_of(session->connections.next, |
| 1461 | struct iscsi_conn, item); |
| 1462 | |
| 1463 | if (session->leadconn == NULL) |
| 1464 | /* no connections exits.. reset sequencing */ |
| 1465 | session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1; |
| 1466 | spin_unlock_bh(&session->lock); |
| 1467 | |
| 1468 | kfifo_free(conn->xmitqueue); |
| 1469 | kfifo_free(conn->immqueue); |
| 1470 | kfifo_free(conn->mgmtqueue); |
| 1471 | |
| 1472 | iscsi_destroy_conn(cls_conn); |
| 1473 | } |
| 1474 | EXPORT_SYMBOL_GPL(iscsi_conn_teardown); |
| 1475 | |
| 1476 | int iscsi_conn_start(struct iscsi_cls_conn *cls_conn) |
| 1477 | { |
| 1478 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 1479 | struct iscsi_session *session = conn->session; |
| 1480 | |
| 1481 | if (session == NULL) { |
| 1482 | printk(KERN_ERR "iscsi: can't start unbound connection\n"); |
| 1483 | return -EPERM; |
| 1484 | } |
| 1485 | |
| 1486 | spin_lock_bh(&session->lock); |
| 1487 | conn->c_stage = ISCSI_CONN_STARTED; |
| 1488 | session->state = ISCSI_STATE_LOGGED_IN; |
| 1489 | |
| 1490 | switch(conn->stop_stage) { |
| 1491 | case STOP_CONN_RECOVER: |
| 1492 | /* |
| 1493 | * unblock eh_abort() if it is blocked. re-try all |
| 1494 | * commands after successful recovery |
| 1495 | */ |
| 1496 | session->conn_cnt++; |
| 1497 | conn->stop_stage = 0; |
| 1498 | conn->tmabort_state = TMABORT_INITIAL; |
| 1499 | session->age++; |
| 1500 | session->recovery_failed = 0; |
| 1501 | spin_unlock_bh(&session->lock); |
| 1502 | |
| 1503 | iscsi_unblock_session(session_to_cls(session)); |
| 1504 | wake_up(&conn->ehwait); |
| 1505 | return 0; |
| 1506 | case STOP_CONN_TERM: |
| 1507 | session->conn_cnt++; |
| 1508 | conn->stop_stage = 0; |
| 1509 | break; |
| 1510 | case STOP_CONN_SUSPEND: |
| 1511 | conn->stop_stage = 0; |
| 1512 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); |
| 1513 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
| 1514 | break; |
| 1515 | default: |
| 1516 | break; |
| 1517 | } |
| 1518 | spin_unlock_bh(&session->lock); |
| 1519 | |
| 1520 | return 0; |
| 1521 | } |
| 1522 | EXPORT_SYMBOL_GPL(iscsi_conn_start); |
| 1523 | |
| 1524 | static void |
| 1525 | flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn) |
| 1526 | { |
| 1527 | struct iscsi_mgmt_task *mtask, *tmp; |
| 1528 | |
| 1529 | /* handle pending */ |
| 1530 | while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) || |
| 1531 | __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) { |
| 1532 | if (mtask == conn->login_mtask) |
| 1533 | continue; |
| 1534 | debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt); |
| 1535 | __kfifo_put(session->mgmtpool.queue, (void*)&mtask, |
| 1536 | sizeof(void*)); |
| 1537 | } |
| 1538 | |
| 1539 | /* handle running */ |
| 1540 | list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) { |
| 1541 | debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt); |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 1542 | list_del(&mtask->running); |
| 1543 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1544 | if (mtask == conn->login_mtask) |
| 1545 | continue; |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 1546 | __kfifo_put(session->mgmtpool.queue, (void*)&mtask, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1547 | sizeof(void*)); |
| 1548 | } |
| 1549 | |
| 1550 | conn->mtask = NULL; |
| 1551 | } |
| 1552 | |
| 1553 | /* Fail commands. Mutex and session lock held and recv side suspended */ |
| 1554 | static void fail_all_commands(struct iscsi_conn *conn) |
| 1555 | { |
| 1556 | struct iscsi_cmd_task *ctask, *tmp; |
| 1557 | |
| 1558 | /* flush pending */ |
| 1559 | while (__kfifo_get(conn->xmitqueue, (void*)&ctask, sizeof(void*))) { |
| 1560 | debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc, |
| 1561 | ctask->itt); |
| 1562 | fail_command(conn, ctask, DID_BUS_BUSY << 16); |
| 1563 | } |
| 1564 | |
| 1565 | /* fail all other running */ |
| 1566 | list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) { |
| 1567 | debug_scsi("failing in progress sc %p itt 0x%x\n", |
| 1568 | ctask->sc, ctask->itt); |
| 1569 | fail_command(conn, ctask, DID_BUS_BUSY << 16); |
| 1570 | } |
| 1571 | |
| 1572 | conn->ctask = NULL; |
| 1573 | } |
| 1574 | |
| 1575 | void iscsi_start_session_recovery(struct iscsi_session *session, |
| 1576 | struct iscsi_conn *conn, int flag) |
| 1577 | { |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 1578 | int old_stop_stage; |
| 1579 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1580 | spin_lock_bh(&session->lock); |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 1581 | if (conn->stop_stage == STOP_CONN_TERM) { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1582 | spin_unlock_bh(&session->lock); |
| 1583 | return; |
| 1584 | } |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 1585 | |
| 1586 | /* |
| 1587 | * When this is called for the in_login state, we only want to clean |
| 1588 | * up the login task and connection. |
| 1589 | */ |
| 1590 | if (conn->stop_stage != STOP_CONN_RECOVER) |
| 1591 | session->conn_cnt--; |
| 1592 | |
| 1593 | old_stop_stage = conn->stop_stage; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1594 | conn->stop_stage = flag; |
| 1595 | spin_unlock_bh(&session->lock); |
| 1596 | |
| 1597 | if (session->tt->suspend_conn_recv) |
| 1598 | session->tt->suspend_conn_recv(conn); |
| 1599 | |
| 1600 | mutex_lock(&conn->xmitmutex); |
| 1601 | spin_lock_bh(&session->lock); |
| 1602 | conn->c_stage = ISCSI_CONN_STOPPED; |
| 1603 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
| 1604 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1605 | if (session->conn_cnt == 0 || session->leadconn == conn) |
| 1606 | session->state = ISCSI_STATE_FAILED; |
| 1607 | |
| 1608 | spin_unlock_bh(&session->lock); |
| 1609 | |
| 1610 | session->tt->terminate_conn(conn); |
| 1611 | /* |
| 1612 | * flush queues. |
| 1613 | */ |
| 1614 | spin_lock_bh(&session->lock); |
| 1615 | fail_all_commands(conn); |
| 1616 | flush_control_queues(session, conn); |
| 1617 | spin_unlock_bh(&session->lock); |
| 1618 | |
| 1619 | /* |
| 1620 | * for connection level recovery we should not calculate |
| 1621 | * header digest. conn->hdr_size used for optimization |
| 1622 | * in hdr_extract() and will be re-negotiated at |
| 1623 | * set_param() time. |
| 1624 | */ |
| 1625 | if (flag == STOP_CONN_RECOVER) { |
| 1626 | conn->hdrdgst_en = 0; |
| 1627 | conn->datadgst_en = 0; |
| 1628 | |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 1629 | /* |
| 1630 | * if this is called from the eh and and from userspace |
| 1631 | * then we only need to block once. |
| 1632 | */ |
| 1633 | if (session->state == ISCSI_STATE_FAILED && |
| 1634 | old_stop_stage != STOP_CONN_RECOVER) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1635 | iscsi_block_session(session_to_cls(session)); |
| 1636 | } |
| 1637 | mutex_unlock(&conn->xmitmutex); |
| 1638 | } |
| 1639 | EXPORT_SYMBOL_GPL(iscsi_start_session_recovery); |
| 1640 | |
| 1641 | void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) |
| 1642 | { |
| 1643 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 1644 | struct iscsi_session *session = conn->session; |
| 1645 | |
| 1646 | switch (flag) { |
| 1647 | case STOP_CONN_RECOVER: |
| 1648 | case STOP_CONN_TERM: |
| 1649 | iscsi_start_session_recovery(session, conn, flag); |
| 1650 | return; |
| 1651 | case STOP_CONN_SUSPEND: |
| 1652 | if (session->tt->suspend_conn_recv) |
| 1653 | session->tt->suspend_conn_recv(conn); |
| 1654 | |
| 1655 | mutex_lock(&conn->xmitmutex); |
| 1656 | spin_lock_bh(&session->lock); |
| 1657 | |
| 1658 | conn->stop_stage = flag; |
| 1659 | conn->c_stage = ISCSI_CONN_STOPPED; |
| 1660 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
| 1661 | |
| 1662 | spin_unlock_bh(&session->lock); |
| 1663 | mutex_unlock(&conn->xmitmutex); |
| 1664 | break; |
| 1665 | default: |
Or Gerlitz | be2df72 | 2006-05-02 19:46:43 -0500 | [diff] [blame^] | 1666 | printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1667 | } |
| 1668 | } |
| 1669 | EXPORT_SYMBOL_GPL(iscsi_conn_stop); |
| 1670 | |
| 1671 | int iscsi_conn_bind(struct iscsi_cls_session *cls_session, |
| 1672 | struct iscsi_cls_conn *cls_conn, int is_leading) |
| 1673 | { |
| 1674 | struct iscsi_session *session = class_to_transport_session(cls_session); |
| 1675 | struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data; |
| 1676 | |
| 1677 | /* lookup for existing connection */ |
| 1678 | spin_lock_bh(&session->lock); |
| 1679 | list_for_each_entry(tmp, &session->connections, item) { |
| 1680 | if (tmp == conn) { |
| 1681 | if (conn->c_stage != ISCSI_CONN_STOPPED || |
| 1682 | conn->stop_stage == STOP_CONN_TERM) { |
Or Gerlitz | be2df72 | 2006-05-02 19:46:43 -0500 | [diff] [blame^] | 1683 | printk(KERN_ERR "iscsi: can't bind " |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1684 | "non-stopped connection (%d:%d)\n", |
| 1685 | conn->c_stage, conn->stop_stage); |
| 1686 | spin_unlock_bh(&session->lock); |
| 1687 | return -EIO; |
| 1688 | } |
| 1689 | break; |
| 1690 | } |
| 1691 | } |
| 1692 | if (tmp != conn) { |
| 1693 | /* bind new iSCSI connection to session */ |
| 1694 | conn->session = session; |
| 1695 | list_add(&conn->item, &session->connections); |
| 1696 | } |
| 1697 | spin_unlock_bh(&session->lock); |
| 1698 | |
| 1699 | if (is_leading) |
| 1700 | session->leadconn = conn; |
| 1701 | |
| 1702 | /* |
| 1703 | * Unblock xmitworker(), Login Phase will pass through. |
| 1704 | */ |
| 1705 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); |
| 1706 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
| 1707 | return 0; |
| 1708 | } |
| 1709 | EXPORT_SYMBOL_GPL(iscsi_conn_bind); |
| 1710 | |
| 1711 | MODULE_AUTHOR("Mike Christie"); |
| 1712 | MODULE_DESCRIPTION("iSCSI library functions"); |
| 1713 | MODULE_LICENSE("GPL"); |