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
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.
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.
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.
24 #include <linux/types.h>
25 #include <linux/mutex.h>
26 #include <linux/kfifo.h>
27 #include <linux/delay.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>
40 struct iscsi_session *
41 class_to_transport_session(struct iscsi_cls_session *cls_session)
43 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
44 return iscsi_hostdata(shost->hostdata);
46 EXPORT_SYMBOL_GPL(class_to_transport_session);
48 #define INVALID_SN_DELTA 0xffff
51 iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
53 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
54 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
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;
68 EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn);
70 void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
71 struct iscsi_data *hdr)
73 struct iscsi_conn *conn = ctask->conn;
75 memset(hdr, 0, sizeof(struct iscsi_data));
76 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
77 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
78 ctask->unsol_datasn++;
79 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
80 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
82 hdr->itt = ctask->hdr->itt;
83 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
84 hdr->offset = cpu_to_be32(ctask->unsol_offset);
86 if (ctask->unsol_count > conn->max_xmit_dlength) {
87 hton24(hdr->dlength, conn->max_xmit_dlength);
88 ctask->data_count = conn->max_xmit_dlength;
89 ctask->unsol_offset += ctask->data_count;
92 hton24(hdr->dlength, ctask->unsol_count);
93 ctask->data_count = ctask->unsol_count;
94 hdr->flags = ISCSI_FLAG_CMD_FINAL;
97 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
100 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
101 * @ctask: iscsi cmd task
103 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
104 * fields like dlength or final based on how much data it sends
106 static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
108 struct iscsi_conn *conn = ctask->conn;
109 struct iscsi_session *session = conn->session;
110 struct iscsi_cmd *hdr = ctask->hdr;
111 struct scsi_cmnd *sc = ctask->sc;
113 hdr->opcode = ISCSI_OP_SCSI_CMD;
114 hdr->flags = ISCSI_ATTR_SIMPLE;
115 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
116 hdr->itt = ctask->itt | (conn->id << ISCSI_CID_SHIFT) |
117 (session->age << ISCSI_AGE_SHIFT);
118 hdr->data_length = cpu_to_be32(sc->request_bufflen);
119 hdr->cmdsn = cpu_to_be32(session->cmdsn);
121 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
122 memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
123 memset(&hdr->cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
125 ctask->data_count = 0;
126 if (sc->sc_data_direction == DMA_TO_DEVICE) {
127 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
131 * imm_count bytes to be sent right after
134 * unsol_count bytes(as Data-Out) to be sent
135 * without R2T ack right after
138 * r2t_data_count bytes to be sent via R2T ack's
140 * pad_count bytes to be sent as zero-padding
142 ctask->imm_count = 0;
143 ctask->unsol_count = 0;
144 ctask->unsol_offset = 0;
145 ctask->unsol_datasn = 0;
147 if (session->imm_data_en) {
148 if (ctask->total_length >= session->first_burst)
149 ctask->imm_count = min(session->first_burst,
150 conn->max_xmit_dlength);
152 ctask->imm_count = min(ctask->total_length,
153 conn->max_xmit_dlength);
154 hton24(ctask->hdr->dlength, ctask->imm_count);
156 zero_data(ctask->hdr->dlength);
158 if (!session->initial_r2t_en) {
159 ctask->unsol_count = min(session->first_burst,
160 ctask->total_length) - ctask->imm_count;
161 ctask->unsol_offset = ctask->imm_count;
164 if (!ctask->unsol_count)
165 /* No unsolicit Data-Out's */
166 ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
169 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
170 zero_data(hdr->dlength);
172 if (sc->sc_data_direction == DMA_FROM_DEVICE)
173 hdr->flags |= ISCSI_FLAG_CMD_READ;
176 conn->scsicmd_pdus_cnt++;
178 EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu);
181 * iscsi_complete_command - return command back to scsi-ml
182 * @ctask: iscsi cmd task
184 * Must be called with session lock.
185 * This function returns the scsi command to scsi-ml and returns
186 * the cmd task to the pool of available cmd tasks.
188 static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
190 struct iscsi_session *session = ctask->conn->session;
191 struct scsi_cmnd *sc = ctask->sc;
193 ctask->state = ISCSI_TASK_COMPLETED;
195 list_del_init(&ctask->running);
196 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
200 static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
202 atomic_inc(&ctask->refcount);
205 static void iscsi_get_ctask(struct iscsi_cmd_task *ctask)
207 spin_lock_bh(&ctask->conn->session->lock);
208 __iscsi_get_ctask(ctask);
209 spin_unlock_bh(&ctask->conn->session->lock);
212 static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
214 struct iscsi_conn *conn = ctask->conn;
216 if (atomic_dec_and_test(&ctask->refcount)) {
217 conn->session->tt->cleanup_cmd_task(conn, ctask);
218 iscsi_complete_command(ctask);
222 static void iscsi_put_ctask(struct iscsi_cmd_task *ctask)
224 spin_lock_bh(&ctask->conn->session->lock);
225 __iscsi_put_ctask(ctask);
226 spin_unlock_bh(&ctask->conn->session->lock);
230 * iscsi_cmd_rsp - SCSI Command Response processing
231 * @conn: iscsi connection
233 * @ctask: scsi command task
234 * @data: cmd data buffer
235 * @datalen: len of buffer
237 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
238 * then completes the command and task.
240 static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
241 struct iscsi_cmd_task *ctask, char *data,
245 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
246 struct iscsi_session *session = conn->session;
247 struct scsi_cmnd *sc = ctask->sc;
249 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
251 sc->result = DID_ERROR << 16;
255 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
257 sc->result = (DID_OK << 16) | rhdr->cmd_status;
259 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
260 sc->result = DID_ERROR << 16;
264 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
269 printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
270 "invalid data buffer size of %d\n", datalen);
271 sc->result = DID_BAD_TARGET << 16;
275 senselen = (data[0] << 8) | data[1];
276 if (datalen < senselen)
277 goto invalid_datalen;
279 memcpy(sc->sense_buffer, data + 2,
280 min(senselen, SCSI_SENSE_BUFFERSIZE));
281 debug_scsi("copied %d bytes of sense\n",
282 min(senselen, SCSI_SENSE_BUFFERSIZE));
285 if (sc->sc_data_direction == DMA_TO_DEVICE)
288 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
289 int res_count = be32_to_cpu(rhdr->residual_count);
291 if (res_count > 0 && res_count <= sc->request_bufflen)
292 sc->resid = res_count;
294 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
295 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
296 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
297 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
298 sc->resid = be32_to_cpu(rhdr->residual_count);
301 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
302 (long)sc, sc->result, ctask->itt);
303 conn->scsirsp_pdus_cnt++;
305 __iscsi_put_ctask(ctask);
309 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
311 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
313 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
314 conn->tmfrsp_pdus_cnt++;
316 if (conn->tmabort_state != TMABORT_INITIAL)
319 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
320 conn->tmabort_state = TMABORT_SUCCESS;
321 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
322 conn->tmabort_state = TMABORT_NOT_FOUND;
324 conn->tmabort_state = TMABORT_FAILED;
325 wake_up(&conn->ehwait);
329 * __iscsi_complete_pdu - complete pdu
333 * @datalen: len of data buffer
335 * Completes pdu processing by freeing any resources allocated at
336 * queuecommand or send generic. session lock must be held and verify
337 * itt must have been called.
339 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
340 char *data, int datalen)
342 struct iscsi_session *session = conn->session;
343 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
344 struct iscsi_cmd_task *ctask;
345 struct iscsi_mgmt_task *mtask;
348 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
349 itt = hdr->itt & ISCSI_ITT_MASK;
353 if (itt < session->cmds_max) {
354 ctask = session->cmds[itt];
356 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
357 opcode, conn->id, ctask->itt, datalen);
360 case ISCSI_OP_SCSI_CMD_RSP:
361 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
362 rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
365 case ISCSI_OP_SCSI_DATA_IN:
366 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
367 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
368 conn->scsirsp_pdus_cnt++;
369 __iscsi_put_ctask(ctask);
373 /* LLD handles this for now */
376 rc = ISCSI_ERR_BAD_OPCODE;
379 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
380 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
381 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
383 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
384 opcode, conn->id, mtask->itt, datalen);
386 rc = iscsi_check_assign_cmdsn(session,
387 (struct iscsi_nopin*)hdr);
392 case ISCSI_OP_LOGOUT_RSP:
394 rc = ISCSI_ERR_PROTO;
397 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
399 case ISCSI_OP_LOGIN_RSP:
400 case ISCSI_OP_TEXT_RSP:
402 * login related PDU's exp_statsn is handled in
405 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
406 rc = ISCSI_ERR_CONN_FAILED;
407 list_del(&mtask->running);
408 if (conn->login_mtask != mtask)
409 __kfifo_put(session->mgmtpool.queue,
410 (void*)&mtask, sizeof(void*));
412 case ISCSI_OP_SCSI_TMFUNC_RSP:
414 rc = ISCSI_ERR_PROTO;
418 iscsi_tmf_rsp(conn, hdr);
420 case ISCSI_OP_NOOP_IN:
421 if (hdr->ttt != ISCSI_RESERVED_TAG || datalen) {
422 rc = ISCSI_ERR_PROTO;
425 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
427 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
428 rc = ISCSI_ERR_CONN_FAILED;
429 list_del(&mtask->running);
430 if (conn->login_mtask != mtask)
431 __kfifo_put(session->mgmtpool.queue,
432 (void*)&mtask, sizeof(void*));
435 rc = ISCSI_ERR_BAD_OPCODE;
438 } else if (itt == ISCSI_RESERVED_TAG) {
440 case ISCSI_OP_NOOP_IN:
442 rc = ISCSI_ERR_PROTO;
446 rc = iscsi_check_assign_cmdsn(session,
447 (struct iscsi_nopin*)hdr);
451 if (hdr->ttt == ISCSI_RESERVED_TAG)
454 if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
455 rc = ISCSI_ERR_CONN_FAILED;
457 case ISCSI_OP_REJECT:
458 /* we need sth like iscsi_reject_rsp()*/
459 case ISCSI_OP_ASYNC_EVENT:
460 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
461 /* we need sth like iscsi_async_event_rsp() */
462 rc = ISCSI_ERR_BAD_OPCODE;
465 rc = ISCSI_ERR_BAD_OPCODE;
469 rc = ISCSI_ERR_BAD_ITT;
474 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
476 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
477 char *data, int datalen)
481 spin_lock(&conn->session->lock);
482 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
483 spin_unlock(&conn->session->lock);
486 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
488 /* verify itt (itt encoding: age+cid+itt) */
489 int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
492 struct iscsi_session *session = conn->session;
493 struct iscsi_cmd_task *ctask;
496 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
497 if ((hdr->itt & ISCSI_AGE_MASK) !=
498 (session->age << ISCSI_AGE_SHIFT)) {
499 printk(KERN_ERR "iscsi: received itt %x expected "
500 "session age (%x)\n", hdr->itt,
501 session->age & ISCSI_AGE_MASK);
502 return ISCSI_ERR_BAD_ITT;
505 if ((hdr->itt & ISCSI_CID_MASK) !=
506 (conn->id << ISCSI_CID_SHIFT)) {
507 printk(KERN_ERR "iscsi: received itt %x, expected "
508 "CID (%x)\n", hdr->itt, conn->id);
509 return ISCSI_ERR_BAD_ITT;
511 itt = hdr->itt & ISCSI_ITT_MASK;
515 if (itt < session->cmds_max) {
516 ctask = session->cmds[itt];
519 printk(KERN_INFO "iscsi: dropping ctask with "
520 "itt 0x%x\n", ctask->itt);
522 return ISCSI_ERR_NO_SCSI_CMD;
525 if (ctask->sc->SCp.phase != session->age) {
526 printk(KERN_ERR "iscsi: ctask's session age %d, "
527 "expected %d\n", ctask->sc->SCp.phase,
529 return ISCSI_ERR_SESSION_FAILED;
536 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
538 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
540 struct iscsi_session *session = conn->session;
543 spin_lock_irqsave(&session->lock, flags);
544 if (session->state == ISCSI_STATE_FAILED) {
545 spin_unlock_irqrestore(&session->lock, flags);
549 if (conn->stop_stage == 0)
550 session->state = ISCSI_STATE_FAILED;
551 spin_unlock_irqrestore(&session->lock, flags);
552 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
553 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
554 iscsi_conn_error(conn->cls_conn, err);
556 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
559 * iscsi_data_xmit - xmit any command into the scheduled connection
560 * @conn: iscsi connection
563 * The function can return -EAGAIN in which case the caller must
564 * re-schedule it again later or recover. '0' return code means
567 static int iscsi_data_xmit(struct iscsi_conn *conn)
569 struct iscsi_transport *tt;
572 if (unlikely(conn->suspend_tx)) {
573 debug_scsi("conn %d Tx suspended!\n", conn->id);
576 tt = conn->session->tt;
579 * Transmit in the following order:
581 * 1) un-finished xmit (ctask or mtask)
582 * 2) immediate control PDUs
585 * 5) non-immediate control PDUs
587 * No need to lock around __kfifo_get as long as
588 * there's one producer and one consumer.
591 BUG_ON(conn->ctask && conn->mtask);
594 iscsi_get_ctask(conn->ctask);
595 rc = tt->xmit_cmd_task(conn, conn->ctask);
596 iscsi_put_ctask(conn->ctask);
599 /* done with this in-progress ctask */
603 rc = tt->xmit_mgmt_task(conn, conn->mtask);
606 /* done with this in-progress mtask */
610 /* process immediate first */
611 if (unlikely(__kfifo_len(conn->immqueue))) {
612 while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
614 spin_lock_bh(&conn->session->lock);
615 list_add_tail(&conn->mtask->running,
616 &conn->mgmt_run_list);
617 spin_unlock_bh(&conn->session->lock);
618 rc = tt->xmit_mgmt_task(conn, conn->mtask);
622 /* done with this mtask */
626 /* process command queue */
627 spin_lock_bh(&conn->session->lock);
628 while (!list_empty(&conn->xmitqueue)) {
630 * iscsi tcp may readd the task to the xmitqueue to send
633 conn->ctask = list_entry(conn->xmitqueue.next,
634 struct iscsi_cmd_task, running);
635 conn->ctask->state = ISCSI_TASK_RUNNING;
636 list_move_tail(conn->xmitqueue.next, &conn->run_list);
637 __iscsi_get_ctask(conn->ctask);
638 spin_unlock_bh(&conn->session->lock);
640 rc = tt->xmit_cmd_task(conn, conn->ctask);
644 spin_lock_bh(&conn->session->lock);
645 __iscsi_put_ctask(conn->ctask);
647 spin_unlock_bh(&conn->session->lock);
651 spin_unlock_bh(&conn->session->lock);
652 /* done with this ctask */
655 /* process the rest control plane PDUs, if any */
656 if (unlikely(__kfifo_len(conn->mgmtqueue))) {
657 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
659 spin_lock_bh(&conn->session->lock);
660 list_add_tail(&conn->mtask->running,
661 &conn->mgmt_run_list);
662 spin_unlock_bh(&conn->session->lock);
663 rc = tt->xmit_mgmt_task(conn, conn->mtask);
667 /* done with this mtask */
674 if (unlikely(conn->suspend_tx))
680 static void iscsi_xmitworker(void *data)
682 struct iscsi_conn *conn = data;
685 * serialize Xmit worker on a per-connection basis.
687 mutex_lock(&conn->xmitmutex);
689 rc = iscsi_data_xmit(conn);
690 } while (rc >= 0 || rc == -EAGAIN);
691 mutex_unlock(&conn->xmitmutex);
695 FAILURE_BAD_HOST = 1,
696 FAILURE_SESSION_FAILED,
697 FAILURE_SESSION_FREED,
698 FAILURE_WINDOW_CLOSED,
700 FAILURE_SESSION_TERMINATE,
701 FAILURE_SESSION_IN_RECOVERY,
702 FAILURE_SESSION_RECOVERY_TIMEOUT,
705 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
707 struct Scsi_Host *host;
709 struct iscsi_session *session;
710 struct iscsi_conn *conn;
711 struct iscsi_cmd_task *ctask = NULL;
713 sc->scsi_done = done;
716 host = sc->device->host;
717 session = iscsi_hostdata(host->hostdata);
719 spin_lock(&session->lock);
722 * ISCSI_STATE_FAILED is a temp. state. The recovery
723 * code will decide what is best to do with command queued
726 if (session->state != ISCSI_STATE_LOGGED_IN &&
727 session->state != ISCSI_STATE_FAILED) {
729 * to handle the race between when we set the recovery state
730 * and block the session we requeue here (commands could
731 * be entering our queuecommand while a block is starting
732 * up because the block code is not locked)
734 if (session->state == ISCSI_STATE_IN_RECOVERY) {
735 reason = FAILURE_SESSION_IN_RECOVERY;
739 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
740 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
741 else if (session->state == ISCSI_STATE_TERMINATE)
742 reason = FAILURE_SESSION_TERMINATE;
744 reason = FAILURE_SESSION_FREED;
749 * Check for iSCSI window and take care of CmdSN wrap-around
751 if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
752 reason = FAILURE_WINDOW_CLOSED;
756 conn = session->leadconn;
758 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
760 reason = FAILURE_OOM;
763 sc->SCp.phase = session->age;
764 sc->SCp.ptr = (char *)ctask;
766 atomic_set(&ctask->refcount, 1);
767 ctask->state = ISCSI_TASK_PENDING;
771 INIT_LIST_HEAD(&ctask->running);
772 ctask->total_length = sc->request_bufflen;
773 iscsi_prep_scsi_cmd_pdu(ctask);
775 session->tt->init_cmd_task(ctask);
777 list_add_tail(&ctask->running, &conn->xmitqueue);
779 "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
780 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
781 conn->id, (long)sc, ctask->itt, sc->request_bufflen,
782 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
783 spin_unlock(&session->lock);
785 scsi_queue_work(host, &conn->xmitwork);
789 spin_unlock(&session->lock);
790 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
791 return SCSI_MLQUEUE_HOST_BUSY;
794 spin_unlock(&session->lock);
795 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
796 sc->cmnd[0], reason);
797 sc->result = (DID_NO_CONNECT << 16);
798 sc->resid = sc->request_bufflen;
802 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
804 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
806 if (depth > ISCSI_MAX_CMD_PER_LUN)
807 depth = ISCSI_MAX_CMD_PER_LUN;
808 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
809 return sdev->queue_depth;
811 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
814 iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
815 char *data, uint32_t data_size)
817 struct iscsi_session *session = conn->session;
818 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
819 struct iscsi_mgmt_task *mtask;
821 spin_lock_bh(&session->lock);
822 if (session->state == ISCSI_STATE_TERMINATE) {
823 spin_unlock_bh(&session->lock);
826 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
827 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
829 * Login and Text are sent serially, in
830 * request-followed-by-response sequence.
831 * Same mtask can be used. Same ITT must be used.
832 * Note that login_mtask is preallocated at conn_create().
834 mtask = conn->login_mtask;
836 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
837 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
839 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
840 if (!__kfifo_get(session->mgmtpool.queue,
841 (void*)&mtask, sizeof(void*))) {
842 spin_unlock_bh(&session->lock);
848 * pre-format CmdSN for outgoing PDU.
850 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
851 hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
852 (session->age << ISCSI_AGE_SHIFT);
853 nop->cmdsn = cpu_to_be32(session->cmdsn);
854 if (conn->c_stage == ISCSI_CONN_STARTED &&
855 !(hdr->opcode & ISCSI_OP_IMMEDIATE))
858 /* do not advance CmdSN */
859 nop->cmdsn = cpu_to_be32(session->cmdsn);
862 memcpy(mtask->data, data, data_size);
863 mtask->data_count = data_size;
865 mtask->data_count = 0;
867 INIT_LIST_HEAD(&mtask->running);
868 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
869 if (session->tt->init_mgmt_task)
870 session->tt->init_mgmt_task(conn, mtask, data, data_size);
871 spin_unlock_bh(&session->lock);
873 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
874 hdr->opcode, hdr->itt, data_size);
877 * since send_pdu() could be called at least from two contexts,
878 * we need to serialize __kfifo_put, so we don't have to take
879 * additional lock on fast data-path
881 if (hdr->opcode & ISCSI_OP_IMMEDIATE)
882 __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
884 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
886 scsi_queue_work(session->host, &conn->xmitwork);
890 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
891 char *data, uint32_t data_size)
893 struct iscsi_conn *conn = cls_conn->dd_data;
896 mutex_lock(&conn->xmitmutex);
897 rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
898 mutex_unlock(&conn->xmitmutex);
902 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
904 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
906 struct iscsi_session *session = class_to_transport_session(cls_session);
907 struct iscsi_conn *conn = session->leadconn;
909 spin_lock_bh(&session->lock);
910 if (session->state != ISCSI_STATE_LOGGED_IN) {
911 session->state = ISCSI_STATE_RECOVERY_FAILED;
913 wake_up(&conn->ehwait);
915 spin_unlock_bh(&session->lock);
917 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
919 int iscsi_eh_host_reset(struct scsi_cmnd *sc)
921 struct Scsi_Host *host = sc->device->host;
922 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
923 struct iscsi_conn *conn = session->leadconn;
924 int fail_session = 0;
926 spin_lock_bh(&session->lock);
927 if (session->state == ISCSI_STATE_TERMINATE) {
929 debug_scsi("failing host reset: session terminated "
930 "[CID %d age %d]", conn->id, session->age);
931 spin_unlock_bh(&session->lock);
935 if (sc->SCp.phase == session->age) {
936 debug_scsi("failing connection CID %d due to SCSI host reset",
940 spin_unlock_bh(&session->lock);
943 * we drop the lock here but the leadconn cannot be destoyed while
944 * we are in the scsi eh
947 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
949 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
950 wait_event_interruptible(conn->ehwait,
951 session->state == ISCSI_STATE_TERMINATE ||
952 session->state == ISCSI_STATE_LOGGED_IN ||
953 session->state == ISCSI_STATE_RECOVERY_FAILED);
954 if (signal_pending(current))
955 flush_signals(current);
957 spin_lock_bh(&session->lock);
958 if (session->state == ISCSI_STATE_LOGGED_IN)
959 printk(KERN_INFO "iscsi: host reset succeeded\n");
962 spin_unlock_bh(&session->lock);
966 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
968 static void iscsi_tmabort_timedout(unsigned long data)
970 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
971 struct iscsi_conn *conn = ctask->conn;
972 struct iscsi_session *session = conn->session;
974 spin_lock(&session->lock);
975 if (conn->tmabort_state == TMABORT_INITIAL) {
976 conn->tmabort_state = TMABORT_TIMEDOUT;
977 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
978 ctask->sc, ctask->itt);
979 /* unblock eh_abort() */
980 wake_up(&conn->ehwait);
982 spin_unlock(&session->lock);
985 /* must be called with the mutex lock */
986 static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
987 struct iscsi_cmd_task *ctask)
989 struct iscsi_conn *conn = ctask->conn;
990 struct iscsi_session *session = conn->session;
991 struct iscsi_tm *hdr = &conn->tmhdr;
995 * ctask timed out but session is OK requests must be serialized.
997 memset(hdr, 0, sizeof(struct iscsi_tm));
998 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
999 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
1000 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1001 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1002 hdr->rtt = ctask->hdr->itt;
1003 hdr->refcmdsn = ctask->hdr->cmdsn;
1005 rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
1008 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1009 debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc);
1013 debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
1015 spin_lock_bh(&session->lock);
1016 ctask->mtask = (struct iscsi_mgmt_task *)
1017 session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
1018 ISCSI_MGMT_ITT_OFFSET];
1020 if (conn->tmabort_state == TMABORT_INITIAL) {
1021 conn->tmfcmd_pdus_cnt++;
1022 conn->tmabort_timer.expires = 10*HZ + jiffies;
1023 conn->tmabort_timer.function = iscsi_tmabort_timedout;
1024 conn->tmabort_timer.data = (unsigned long)ctask;
1025 add_timer(&conn->tmabort_timer);
1026 debug_scsi("abort set timeout [itt 0x%x]", ctask->itt);
1028 spin_unlock_bh(&session->lock);
1029 mutex_unlock(&conn->xmitmutex);
1032 * block eh thread until:
1036 * 3) session is terminated or restarted or userspace has
1037 * given up on recovery
1039 wait_event_interruptible(conn->ehwait,
1040 sc->SCp.phase != session->age ||
1041 session->state != ISCSI_STATE_LOGGED_IN ||
1042 conn->tmabort_state != TMABORT_INITIAL);
1043 if (signal_pending(current))
1044 flush_signals(current);
1045 del_timer_sync(&conn->tmabort_timer);
1047 mutex_lock(&conn->xmitmutex);
1052 * xmit mutex and session lock must be held
1054 static struct iscsi_mgmt_task *
1055 iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1057 int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1058 struct iscsi_mgmt_task *task;
1060 debug_scsi("searching %d tasks\n", nr_tasks);
1062 for (i = 0; i < nr_tasks; i++) {
1063 __kfifo_get(fifo, (void*)&task, sizeof(void*));
1064 debug_scsi("check task %u\n", task->itt);
1066 if (task->itt == itt) {
1067 debug_scsi("matched task\n");
1071 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1076 static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1078 struct iscsi_conn *conn = ctask->conn;
1079 struct iscsi_session *session = conn->session;
1084 if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
1085 list_del(&ctask->mtask->running);
1086 __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1088 ctask->mtask = NULL;
1093 * session lock and xmitmutex must be held
1095 static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1098 struct scsi_cmnd *sc;
1103 iscsi_ctask_mtask_cleanup(ctask);
1106 sc->resid = sc->request_bufflen;
1107 __iscsi_put_ctask(ctask);
1110 int iscsi_eh_abort(struct scsi_cmnd *sc)
1112 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1113 struct iscsi_conn *conn = ctask->conn;
1114 struct iscsi_session *session = conn->session;
1117 conn->eh_abort_cnt++;
1118 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1120 mutex_lock(&conn->xmitmutex);
1121 spin_lock_bh(&session->lock);
1124 * If we are not logged in or we have started a new session
1125 * then let the host reset code handle this
1127 if (session->state != ISCSI_STATE_LOGGED_IN ||
1128 sc->SCp.phase != session->age)
1131 /* ctask completed before time out */
1133 spin_unlock_bh(&session->lock);
1134 debug_scsi("sc completed while abort in progress\n");
1135 goto success_rel_mutex;
1138 /* what should we do here ? */
1139 if (conn->ctask == ctask) {
1140 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1141 "Failing abort\n", sc, ctask->itt);
1145 if (ctask->state == ISCSI_TASK_PENDING)
1146 goto success_cleanup;
1148 conn->tmabort_state = TMABORT_INITIAL;
1150 spin_unlock_bh(&session->lock);
1151 rc = iscsi_exec_abort_task(sc, ctask);
1152 spin_lock_bh(&session->lock);
1154 if (rc || sc->SCp.phase != session->age ||
1155 session->state != ISCSI_STATE_LOGGED_IN)
1157 iscsi_ctask_mtask_cleanup(ctask);
1159 switch (conn->tmabort_state) {
1160 case TMABORT_SUCCESS:
1161 goto success_cleanup;
1162 case TMABORT_NOT_FOUND:
1164 /* ctask completed before tmf abort response */
1165 spin_unlock_bh(&session->lock);
1166 debug_scsi("sc completed while abort in progress\n");
1167 goto success_rel_mutex;
1171 /* timedout or failed */
1172 spin_unlock_bh(&session->lock);
1173 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1174 spin_lock_bh(&session->lock);
1179 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1180 spin_unlock_bh(&session->lock);
1183 * clean up task if aborted. we have the xmitmutex so grab
1184 * the recv lock as a writer
1186 write_lock_bh(conn->recv_lock);
1187 spin_lock(&session->lock);
1188 fail_command(conn, ctask, DID_ABORT << 16);
1189 spin_unlock(&session->lock);
1190 write_unlock_bh(conn->recv_lock);
1193 mutex_unlock(&conn->xmitmutex);
1197 spin_unlock_bh(&session->lock);
1198 mutex_unlock(&conn->xmitmutex);
1200 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1203 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1206 iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1210 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1215 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1216 if (q->pool == NULL) {
1221 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1223 if (q->queue == ERR_PTR(-ENOMEM)) {
1229 for (i = 0; i < max; i++) {
1230 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1231 if (q->pool[i] == NULL) {
1234 for (j = 0; j < i; j++)
1237 kfifo_free(q->queue);
1242 memset(q->pool[i], 0, item_size);
1243 (*items)[i] = q->pool[i];
1244 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1248 EXPORT_SYMBOL_GPL(iscsi_pool_init);
1250 void iscsi_pool_free(struct iscsi_queue *q, void **items)
1254 for (i = 0; i < q->max; i++)
1259 EXPORT_SYMBOL_GPL(iscsi_pool_free);
1262 * iSCSI Session's hostdata organization:
1264 * *------------------* <== hostdata_session(host->hostdata)
1265 * | ptr to class sess|
1266 * |------------------| <== iscsi_hostdata(host->hostdata)
1268 * *------------------*
1271 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1272 _sz % sizeof(unsigned long))
1274 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1277 * iscsi_session_setup - create iscsi cls session and host and session
1278 * @scsit: scsi transport template
1279 * @iscsit: iscsi transport template
1280 * @initial_cmdsn: initial CmdSN
1281 * @hostno: host no allocated
1283 * This can be used by software iscsi_transports that allocate
1284 * a session per scsi host.
1286 struct iscsi_cls_session *
1287 iscsi_session_setup(struct iscsi_transport *iscsit,
1288 struct scsi_transport_template *scsit,
1289 int cmd_task_size, int mgmt_task_size,
1290 uint32_t initial_cmdsn, uint32_t *hostno)
1292 struct Scsi_Host *shost;
1293 struct iscsi_session *session;
1294 struct iscsi_cls_session *cls_session;
1297 shost = scsi_host_alloc(iscsit->host_template,
1298 hostdata_privsize(sizeof(*session)));
1303 shost->max_channel = 0;
1304 shost->max_lun = iscsit->max_lun;
1305 shost->max_cmd_len = iscsit->max_cmd_len;
1306 shost->transportt = scsit;
1307 shost->transportt->create_work_queue = 1;
1308 *hostno = shost->host_no;
1310 session = iscsi_hostdata(shost->hostdata);
1311 memset(session, 0, sizeof(struct iscsi_session));
1312 session->host = shost;
1313 session->state = ISCSI_STATE_FREE;
1314 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1315 session->cmds_max = ISCSI_XMIT_CMDS_MAX;
1316 session->cmdsn = initial_cmdsn;
1317 session->exp_cmdsn = initial_cmdsn + 1;
1318 session->max_cmdsn = initial_cmdsn + 1;
1319 session->max_r2t = 1;
1320 session->tt = iscsit;
1322 /* initialize SCSI PDU commands pool */
1323 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1324 (void***)&session->cmds,
1325 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1326 goto cmdpool_alloc_fail;
1328 /* pre-format cmds pool with ITT */
1329 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1330 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1333 ctask->dd_data = &ctask[1];
1335 INIT_LIST_HEAD(&ctask->running);
1338 spin_lock_init(&session->lock);
1339 INIT_LIST_HEAD(&session->connections);
1341 /* initialize immediate command pool */
1342 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1343 (void***)&session->mgmt_cmds,
1344 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1345 goto mgmtpool_alloc_fail;
1348 /* pre-format immediate cmds pool with ITT */
1349 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1350 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1353 mtask->dd_data = &mtask[1];
1354 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
1355 INIT_LIST_HEAD(&mtask->running);
1358 if (scsi_add_host(shost, NULL))
1361 if (!try_module_get(iscsit->owner))
1362 goto cls_session_fail;
1364 cls_session = iscsi_create_session(shost, iscsit, 0);
1367 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1372 module_put(iscsit->owner);
1374 scsi_remove_host(shost);
1376 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1377 mgmtpool_alloc_fail:
1378 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1380 scsi_host_put(shost);
1383 EXPORT_SYMBOL_GPL(iscsi_session_setup);
1386 * iscsi_session_teardown - destroy session, host, and cls_session
1389 * This can be used by software iscsi_transports that allocate
1390 * a session per scsi host.
1392 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1394 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1395 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1396 struct module *owner = cls_session->transport->owner;
1398 scsi_remove_host(shost);
1400 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1401 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1403 kfree(session->targetname);
1405 iscsi_destroy_session(cls_session);
1406 scsi_host_put(shost);
1409 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1412 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1413 * @cls_session: iscsi_cls_session
1416 struct iscsi_cls_conn *
1417 iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1419 struct iscsi_session *session = class_to_transport_session(cls_session);
1420 struct iscsi_conn *conn;
1421 struct iscsi_cls_conn *cls_conn;
1424 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1427 conn = cls_conn->dd_data;
1428 memset(conn, 0, sizeof(*conn));
1430 conn->session = session;
1431 conn->cls_conn = cls_conn;
1432 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1433 conn->id = conn_idx;
1434 conn->exp_statsn = 0;
1435 conn->tmabort_state = TMABORT_INITIAL;
1436 INIT_LIST_HEAD(&conn->run_list);
1437 INIT_LIST_HEAD(&conn->mgmt_run_list);
1438 INIT_LIST_HEAD(&conn->xmitqueue);
1440 /* initialize general immediate & non-immediate PDU commands queue */
1441 conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1443 if (conn->immqueue == ERR_PTR(-ENOMEM))
1444 goto immqueue_alloc_fail;
1446 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1448 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1449 goto mgmtqueue_alloc_fail;
1451 INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
1453 /* allocate login_mtask used for the login/text sequences */
1454 spin_lock_bh(&session->lock);
1455 if (!__kfifo_get(session->mgmtpool.queue,
1456 (void*)&conn->login_mtask,
1458 spin_unlock_bh(&session->lock);
1459 goto login_mtask_alloc_fail;
1461 spin_unlock_bh(&session->lock);
1463 data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
1465 goto login_mtask_data_alloc_fail;
1466 conn->login_mtask->data = conn->data = data;
1468 init_timer(&conn->tmabort_timer);
1469 mutex_init(&conn->xmitmutex);
1470 init_waitqueue_head(&conn->ehwait);
1474 login_mtask_data_alloc_fail:
1475 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1477 login_mtask_alloc_fail:
1478 kfifo_free(conn->mgmtqueue);
1479 mgmtqueue_alloc_fail:
1480 kfifo_free(conn->immqueue);
1481 immqueue_alloc_fail:
1482 iscsi_destroy_conn(cls_conn);
1485 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1488 * iscsi_conn_teardown - teardown iscsi connection
1489 * cls_conn: iscsi class connection
1491 * TODO: we may need to make this into a two step process
1492 * like scsi-mls remove + put host
1494 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1496 struct iscsi_conn *conn = cls_conn->dd_data;
1497 struct iscsi_session *session = conn->session;
1498 unsigned long flags;
1500 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1501 mutex_lock(&conn->xmitmutex);
1503 spin_lock_bh(&session->lock);
1504 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1505 if (session->leadconn == conn) {
1507 * leading connection? then give up on recovery.
1509 session->state = ISCSI_STATE_TERMINATE;
1510 wake_up(&conn->ehwait);
1512 spin_unlock_bh(&session->lock);
1514 mutex_unlock(&conn->xmitmutex);
1517 * Block until all in-progress commands for this connection
1521 spin_lock_irqsave(session->host->host_lock, flags);
1522 if (!session->host->host_busy) { /* OK for ERL == 0 */
1523 spin_unlock_irqrestore(session->host->host_lock, flags);
1526 spin_unlock_irqrestore(session->host->host_lock, flags);
1527 msleep_interruptible(500);
1528 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1529 "host_failed %d\n", session->host->host_busy,
1530 session->host->host_failed);
1532 * force eh_abort() to unblock
1534 wake_up(&conn->ehwait);
1537 spin_lock_bh(&session->lock);
1539 kfree(conn->persistent_address);
1540 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1542 list_del(&conn->item);
1543 if (list_empty(&session->connections))
1544 session->leadconn = NULL;
1545 if (session->leadconn && session->leadconn == conn)
1546 session->leadconn = container_of(session->connections.next,
1547 struct iscsi_conn, item);
1549 if (session->leadconn == NULL)
1550 /* no connections exits.. reset sequencing */
1551 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
1552 spin_unlock_bh(&session->lock);
1554 kfifo_free(conn->immqueue);
1555 kfifo_free(conn->mgmtqueue);
1557 iscsi_destroy_conn(cls_conn);
1559 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1561 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1563 struct iscsi_conn *conn = cls_conn->dd_data;
1564 struct iscsi_session *session = conn->session;
1567 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1571 if (session->first_burst > session->max_burst) {
1572 printk("iscsi: invalid burst lengths: "
1573 "first_burst %d max_burst %d\n",
1574 session->first_burst, session->max_burst);
1578 spin_lock_bh(&session->lock);
1579 conn->c_stage = ISCSI_CONN_STARTED;
1580 session->state = ISCSI_STATE_LOGGED_IN;
1582 switch(conn->stop_stage) {
1583 case STOP_CONN_RECOVER:
1585 * unblock eh_abort() if it is blocked. re-try all
1586 * commands after successful recovery
1588 conn->stop_stage = 0;
1589 conn->tmabort_state = TMABORT_INITIAL;
1591 spin_unlock_bh(&session->lock);
1593 iscsi_unblock_session(session_to_cls(session));
1594 wake_up(&conn->ehwait);
1596 case STOP_CONN_TERM:
1597 conn->stop_stage = 0;
1602 spin_unlock_bh(&session->lock);
1606 EXPORT_SYMBOL_GPL(iscsi_conn_start);
1609 flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1611 struct iscsi_mgmt_task *mtask, *tmp;
1613 /* handle pending */
1614 while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
1615 __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
1616 if (mtask == conn->login_mtask)
1618 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1619 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1623 /* handle running */
1624 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1625 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
1626 list_del(&mtask->running);
1628 if (mtask == conn->login_mtask)
1630 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1637 /* Fail commands. Mutex and session lock held and recv side suspended */
1638 static void fail_all_commands(struct iscsi_conn *conn)
1640 struct iscsi_cmd_task *ctask, *tmp;
1643 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1644 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1646 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1649 /* fail all other running */
1650 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1651 debug_scsi("failing in progress sc %p itt 0x%x\n",
1652 ctask->sc, ctask->itt);
1653 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1659 static void iscsi_start_session_recovery(struct iscsi_session *session,
1660 struct iscsi_conn *conn, int flag)
1664 spin_lock_bh(&session->lock);
1665 if (conn->stop_stage == STOP_CONN_TERM) {
1666 spin_unlock_bh(&session->lock);
1671 * When this is called for the in_login state, we only want to clean
1672 * up the login task and connection. We do not need to block and set
1673 * the recovery state again
1675 if (flag == STOP_CONN_TERM)
1676 session->state = ISCSI_STATE_TERMINATE;
1677 else if (conn->stop_stage != STOP_CONN_RECOVER)
1678 session->state = ISCSI_STATE_IN_RECOVERY;
1680 old_stop_stage = conn->stop_stage;
1681 conn->stop_stage = flag;
1682 conn->c_stage = ISCSI_CONN_STOPPED;
1683 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1684 spin_unlock_bh(&session->lock);
1686 write_lock_bh(conn->recv_lock);
1687 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1688 write_unlock_bh(conn->recv_lock);
1690 mutex_lock(&conn->xmitmutex);
1692 * for connection level recovery we should not calculate
1693 * header digest. conn->hdr_size used for optimization
1694 * in hdr_extract() and will be re-negotiated at
1697 if (flag == STOP_CONN_RECOVER) {
1698 conn->hdrdgst_en = 0;
1699 conn->datadgst_en = 0;
1700 if (session->state == ISCSI_STATE_IN_RECOVERY &&
1701 old_stop_stage != STOP_CONN_RECOVER) {
1702 debug_scsi("blocking session\n");
1703 iscsi_block_session(session_to_cls(session));
1710 spin_lock_bh(&session->lock);
1711 fail_all_commands(conn);
1712 flush_control_queues(session, conn);
1713 spin_unlock_bh(&session->lock);
1715 mutex_unlock(&conn->xmitmutex);
1718 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1720 struct iscsi_conn *conn = cls_conn->dd_data;
1721 struct iscsi_session *session = conn->session;
1724 case STOP_CONN_RECOVER:
1725 case STOP_CONN_TERM:
1726 iscsi_start_session_recovery(session, conn, flag);
1729 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
1732 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1734 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1735 struct iscsi_cls_conn *cls_conn, int is_leading)
1737 struct iscsi_session *session = class_to_transport_session(cls_session);
1738 struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data;
1740 /* lookup for existing connection */
1741 spin_lock_bh(&session->lock);
1742 list_for_each_entry(tmp, &session->connections, item) {
1744 if (conn->c_stage != ISCSI_CONN_STOPPED ||
1745 conn->stop_stage == STOP_CONN_TERM) {
1746 printk(KERN_ERR "iscsi: can't bind "
1747 "non-stopped connection (%d:%d)\n",
1748 conn->c_stage, conn->stop_stage);
1749 spin_unlock_bh(&session->lock);
1756 /* bind new iSCSI connection to session */
1757 conn->session = session;
1758 list_add(&conn->item, &session->connections);
1760 spin_unlock_bh(&session->lock);
1763 session->leadconn = conn;
1766 * Unblock xmitworker(), Login Phase will pass through.
1768 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1769 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1772 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1775 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1776 enum iscsi_param param, char *buf, int buflen)
1778 struct iscsi_conn *conn = cls_conn->dd_data;
1779 struct iscsi_session *session = conn->session;
1783 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1784 sscanf(buf, "%d", &conn->max_recv_dlength);
1786 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1787 sscanf(buf, "%d", &conn->max_xmit_dlength);
1789 case ISCSI_PARAM_HDRDGST_EN:
1790 sscanf(buf, "%d", &conn->hdrdgst_en);
1792 case ISCSI_PARAM_DATADGST_EN:
1793 sscanf(buf, "%d", &conn->datadgst_en);
1795 case ISCSI_PARAM_INITIAL_R2T_EN:
1796 sscanf(buf, "%d", &session->initial_r2t_en);
1798 case ISCSI_PARAM_MAX_R2T:
1799 sscanf(buf, "%d", &session->max_r2t);
1801 case ISCSI_PARAM_IMM_DATA_EN:
1802 sscanf(buf, "%d", &session->imm_data_en);
1804 case ISCSI_PARAM_FIRST_BURST:
1805 sscanf(buf, "%d", &session->first_burst);
1807 case ISCSI_PARAM_MAX_BURST:
1808 sscanf(buf, "%d", &session->max_burst);
1810 case ISCSI_PARAM_PDU_INORDER_EN:
1811 sscanf(buf, "%d", &session->pdu_inorder_en);
1813 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1814 sscanf(buf, "%d", &session->dataseq_inorder_en);
1816 case ISCSI_PARAM_ERL:
1817 sscanf(buf, "%d", &session->erl);
1819 case ISCSI_PARAM_IFMARKER_EN:
1820 sscanf(buf, "%d", &value);
1823 case ISCSI_PARAM_OFMARKER_EN:
1824 sscanf(buf, "%d", &value);
1827 case ISCSI_PARAM_EXP_STATSN:
1828 sscanf(buf, "%u", &conn->exp_statsn);
1830 case ISCSI_PARAM_TARGET_NAME:
1831 /* this should not change between logins */
1832 if (session->targetname)
1835 session->targetname = kstrdup(buf, GFP_KERNEL);
1836 if (!session->targetname)
1839 case ISCSI_PARAM_TPGT:
1840 sscanf(buf, "%d", &session->tpgt);
1842 case ISCSI_PARAM_PERSISTENT_PORT:
1843 sscanf(buf, "%d", &conn->persistent_port);
1845 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1847 * this is the address returned in discovery so it should
1848 * not change between logins.
1850 if (conn->persistent_address)
1853 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1854 if (!conn->persistent_address)
1863 EXPORT_SYMBOL_GPL(iscsi_set_param);
1865 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1866 enum iscsi_param param, char *buf)
1868 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1869 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1873 case ISCSI_PARAM_INITIAL_R2T_EN:
1874 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1876 case ISCSI_PARAM_MAX_R2T:
1877 len = sprintf(buf, "%hu\n", session->max_r2t);
1879 case ISCSI_PARAM_IMM_DATA_EN:
1880 len = sprintf(buf, "%d\n", session->imm_data_en);
1882 case ISCSI_PARAM_FIRST_BURST:
1883 len = sprintf(buf, "%u\n", session->first_burst);
1885 case ISCSI_PARAM_MAX_BURST:
1886 len = sprintf(buf, "%u\n", session->max_burst);
1888 case ISCSI_PARAM_PDU_INORDER_EN:
1889 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
1891 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1892 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
1894 case ISCSI_PARAM_ERL:
1895 len = sprintf(buf, "%d\n", session->erl);
1897 case ISCSI_PARAM_TARGET_NAME:
1898 len = sprintf(buf, "%s\n", session->targetname);
1900 case ISCSI_PARAM_TPGT:
1901 len = sprintf(buf, "%d\n", session->tpgt);
1909 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
1911 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
1912 enum iscsi_param param, char *buf)
1914 struct iscsi_conn *conn = cls_conn->dd_data;
1918 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1919 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
1921 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1922 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
1924 case ISCSI_PARAM_HDRDGST_EN:
1925 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
1927 case ISCSI_PARAM_DATADGST_EN:
1928 len = sprintf(buf, "%d\n", conn->datadgst_en);
1930 case ISCSI_PARAM_IFMARKER_EN:
1931 len = sprintf(buf, "%d\n", conn->ifmarker_en);
1933 case ISCSI_PARAM_OFMARKER_EN:
1934 len = sprintf(buf, "%d\n", conn->ofmarker_en);
1936 case ISCSI_PARAM_EXP_STATSN:
1937 len = sprintf(buf, "%u\n", conn->exp_statsn);
1939 case ISCSI_PARAM_PERSISTENT_PORT:
1940 len = sprintf(buf, "%d\n", conn->persistent_port);
1942 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1943 len = sprintf(buf, "%s\n", conn->persistent_address);
1951 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
1953 MODULE_AUTHOR("Mike Christie");
1954 MODULE_DESCRIPTION("iSCSI library functions");
1955 MODULE_LICENSE("GPL");