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,
72 int transport_data_cnt)
74 struct iscsi_conn *conn = ctask->conn;
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));
83 hdr->itt = ctask->hdr->itt;
84 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
86 hdr->offset = cpu_to_be32(ctask->total_length -
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;
95 hton24(hdr->dlength, ctask->unsol_count);
96 ctask->data_count = ctask->unsol_count;
97 hdr->flags = ISCSI_FLAG_CMD_FINAL;
100 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
103 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
104 * @ctask: iscsi cmd task
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
109 static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
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;
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);
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);
128 if (sc->sc_data_direction == DMA_TO_DEVICE) {
129 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
133 * imm_count bytes to be sent right after
136 * unsol_count bytes(as Data-Out) to be sent
137 * without R2T ack right after
140 * r2t_data_count bytes to be sent via R2T ack's
142 * pad_count bytes to be sent as zero-padding
144 ctask->imm_count = 0;
145 ctask->unsol_count = 0;
146 ctask->unsol_datasn = 0;
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);
153 ctask->imm_count = min(ctask->total_length,
154 conn->max_xmit_dlength);
155 hton24(ctask->hdr->dlength, ctask->imm_count);
157 zero_data(ctask->hdr->dlength);
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;
167 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
168 zero_data(hdr->dlength);
170 if (sc->sc_data_direction == DMA_FROM_DEVICE)
171 hdr->flags |= ISCSI_FLAG_CMD_READ;
174 conn->scsicmd_pdus_cnt++;
176 EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu);
179 * iscsi_complete_command - return command back to scsi-ml
180 * @session: iscsi session
181 * @ctask: iscsi cmd task
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.
187 static void iscsi_complete_command(struct iscsi_session *session,
188 struct iscsi_cmd_task *ctask)
190 struct scsi_cmnd *sc = ctask->sc;
192 ctask->state = ISCSI_TASK_COMPLETED;
194 list_del_init(&ctask->running);
195 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
200 * iscsi_cmd_rsp - SCSI Command Response processing
201 * @conn: iscsi connection
203 * @ctask: scsi command task
204 * @data: cmd data buffer
205 * @datalen: len of buffer
207 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
208 * then completes the command and task.
210 static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
211 struct iscsi_cmd_task *ctask, char *data,
215 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
216 struct iscsi_session *session = conn->session;
217 struct scsi_cmnd *sc = ctask->sc;
219 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
221 sc->result = DID_ERROR << 16;
225 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
227 sc->result = (DID_OK << 16) | rhdr->cmd_status;
229 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
230 sc->result = DID_ERROR << 16;
234 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
239 printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
240 "invalid data buffer size of %d\n", datalen);
241 sc->result = DID_BAD_TARGET << 16;
245 senselen = (data[0] << 8) | data[1];
246 if (datalen < senselen)
247 goto invalid_datalen;
249 memcpy(sc->sense_buffer, data + 2,
250 min(senselen, SCSI_SENSE_BUFFERSIZE));
251 debug_scsi("copied %d bytes of sense\n",
252 min(senselen, SCSI_SENSE_BUFFERSIZE));
255 if (sc->sc_data_direction == DMA_TO_DEVICE)
258 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
259 int res_count = be32_to_cpu(rhdr->residual_count);
261 if (res_count > 0 && res_count <= sc->request_bufflen)
262 sc->resid = res_count;
264 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
265 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
266 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
267 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
268 sc->resid = be32_to_cpu(rhdr->residual_count);
271 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
272 (long)sc, sc->result, ctask->itt);
273 conn->scsirsp_pdus_cnt++;
275 iscsi_complete_command(conn->session, ctask);
279 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
281 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
283 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
284 conn->tmfrsp_pdus_cnt++;
286 if (conn->tmabort_state != TMABORT_INITIAL)
289 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
290 conn->tmabort_state = TMABORT_SUCCESS;
291 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
292 conn->tmabort_state = TMABORT_NOT_FOUND;
294 conn->tmabort_state = TMABORT_FAILED;
295 wake_up(&conn->ehwait);
299 * __iscsi_complete_pdu - complete pdu
303 * @datalen: len of data buffer
305 * Completes pdu processing by freeing any resources allocated at
306 * queuecommand or send generic. session lock must be held and verify
307 * itt must have been called.
309 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
310 char *data, int datalen)
312 struct iscsi_session *session = conn->session;
313 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
314 struct iscsi_cmd_task *ctask;
315 struct iscsi_mgmt_task *mtask;
318 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
319 itt = hdr->itt & ISCSI_ITT_MASK;
323 if (itt < session->cmds_max) {
324 ctask = session->cmds[itt];
326 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
327 opcode, conn->id, ctask->itt, datalen);
330 case ISCSI_OP_SCSI_CMD_RSP:
331 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
332 rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
335 case ISCSI_OP_SCSI_DATA_IN:
336 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
337 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
338 conn->scsirsp_pdus_cnt++;
339 iscsi_complete_command(session, ctask);
343 /* LLD handles this for now */
346 rc = ISCSI_ERR_BAD_OPCODE;
349 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
350 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
351 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
353 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
354 opcode, conn->id, mtask->itt, datalen);
356 rc = iscsi_check_assign_cmdsn(session,
357 (struct iscsi_nopin*)hdr);
362 case ISCSI_OP_LOGOUT_RSP:
364 rc = ISCSI_ERR_PROTO;
367 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
369 case ISCSI_OP_LOGIN_RSP:
370 case ISCSI_OP_TEXT_RSP:
372 * login related PDU's exp_statsn is handled in
375 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
376 rc = ISCSI_ERR_CONN_FAILED;
377 list_del(&mtask->running);
378 if (conn->login_mtask != mtask)
379 __kfifo_put(session->mgmtpool.queue,
380 (void*)&mtask, sizeof(void*));
382 case ISCSI_OP_SCSI_TMFUNC_RSP:
384 rc = ISCSI_ERR_PROTO;
388 iscsi_tmf_rsp(conn, hdr);
390 case ISCSI_OP_NOOP_IN:
391 if (hdr->ttt != ISCSI_RESERVED_TAG || datalen) {
392 rc = ISCSI_ERR_PROTO;
395 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
397 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
398 rc = ISCSI_ERR_CONN_FAILED;
399 list_del(&mtask->running);
400 if (conn->login_mtask != mtask)
401 __kfifo_put(session->mgmtpool.queue,
402 (void*)&mtask, sizeof(void*));
405 rc = ISCSI_ERR_BAD_OPCODE;
408 } else if (itt == ISCSI_RESERVED_TAG) {
410 case ISCSI_OP_NOOP_IN:
412 rc = ISCSI_ERR_PROTO;
416 rc = iscsi_check_assign_cmdsn(session,
417 (struct iscsi_nopin*)hdr);
421 if (hdr->ttt == ISCSI_RESERVED_TAG)
424 if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
425 rc = ISCSI_ERR_CONN_FAILED;
427 case ISCSI_OP_REJECT:
428 /* we need sth like iscsi_reject_rsp()*/
429 case ISCSI_OP_ASYNC_EVENT:
430 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
431 /* we need sth like iscsi_async_event_rsp() */
432 rc = ISCSI_ERR_BAD_OPCODE;
435 rc = ISCSI_ERR_BAD_OPCODE;
439 rc = ISCSI_ERR_BAD_ITT;
444 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
446 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
447 char *data, int datalen)
451 spin_lock(&conn->session->lock);
452 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
453 spin_unlock(&conn->session->lock);
456 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
458 /* verify itt (itt encoding: age+cid+itt) */
459 int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
462 struct iscsi_session *session = conn->session;
463 struct iscsi_cmd_task *ctask;
466 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
467 if ((hdr->itt & ISCSI_AGE_MASK) !=
468 (session->age << ISCSI_AGE_SHIFT)) {
469 printk(KERN_ERR "iscsi: received itt %x expected "
470 "session age (%x)\n", hdr->itt,
471 session->age & ISCSI_AGE_MASK);
472 return ISCSI_ERR_BAD_ITT;
475 if ((hdr->itt & ISCSI_CID_MASK) !=
476 (conn->id << ISCSI_CID_SHIFT)) {
477 printk(KERN_ERR "iscsi: received itt %x, expected "
478 "CID (%x)\n", hdr->itt, conn->id);
479 return ISCSI_ERR_BAD_ITT;
481 itt = hdr->itt & ISCSI_ITT_MASK;
485 if (itt < session->cmds_max) {
486 ctask = session->cmds[itt];
489 printk(KERN_INFO "iscsi: dropping ctask with "
490 "itt 0x%x\n", ctask->itt);
492 return ISCSI_ERR_NO_SCSI_CMD;
495 if (ctask->sc->SCp.phase != session->age) {
496 printk(KERN_ERR "iscsi: ctask's session age %d, "
497 "expected %d\n", ctask->sc->SCp.phase,
499 return ISCSI_ERR_SESSION_FAILED;
506 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
508 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
510 struct iscsi_session *session = conn->session;
513 spin_lock_irqsave(&session->lock, flags);
514 if (session->state == ISCSI_STATE_FAILED) {
515 spin_unlock_irqrestore(&session->lock, flags);
519 if (conn->stop_stage == 0)
520 session->state = ISCSI_STATE_FAILED;
521 spin_unlock_irqrestore(&session->lock, flags);
522 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
523 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
524 iscsi_conn_error(conn->cls_conn, err);
526 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
529 * iscsi_data_xmit - xmit any command into the scheduled connection
530 * @conn: iscsi connection
533 * The function can return -EAGAIN in which case the caller must
534 * re-schedule it again later or recover. '0' return code means
537 static int iscsi_data_xmit(struct iscsi_conn *conn)
539 struct iscsi_transport *tt;
542 if (unlikely(conn->suspend_tx)) {
543 debug_scsi("conn %d Tx suspended!\n", conn->id);
546 tt = conn->session->tt;
549 * Transmit in the following order:
551 * 1) un-finished xmit (ctask or mtask)
552 * 2) immediate control PDUs
555 * 5) non-immediate control PDUs
557 * No need to lock around __kfifo_get as long as
558 * there's one producer and one consumer.
561 BUG_ON(conn->ctask && conn->mtask);
564 rc = tt->xmit_cmd_task(conn, conn->ctask);
567 /* done with this in-progress ctask */
571 rc = tt->xmit_mgmt_task(conn, conn->mtask);
574 /* done with this in-progress mtask */
578 /* process immediate first */
579 if (unlikely(__kfifo_len(conn->immqueue))) {
580 while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
582 spin_lock_bh(&conn->session->lock);
583 list_add_tail(&conn->mtask->running,
584 &conn->mgmt_run_list);
585 spin_unlock_bh(&conn->session->lock);
586 rc = tt->xmit_mgmt_task(conn, conn->mtask);
590 /* done with this mtask */
594 /* process command queue */
595 spin_lock_bh(&conn->session->lock);
596 while (!list_empty(&conn->xmitqueue)) {
598 * iscsi tcp may readd the task to the xmitqueue to send
601 conn->ctask = list_entry(conn->xmitqueue.next,
602 struct iscsi_cmd_task, running);
603 conn->ctask->state = ISCSI_TASK_RUNNING;
604 list_move_tail(conn->xmitqueue.next, &conn->run_list);
605 spin_unlock_bh(&conn->session->lock);
607 rc = tt->xmit_cmd_task(conn, conn->ctask);
610 spin_lock_bh(&conn->session->lock);
612 spin_unlock_bh(&conn->session->lock);
613 /* done with this ctask */
616 /* process the rest control plane PDUs, if any */
617 if (unlikely(__kfifo_len(conn->mgmtqueue))) {
618 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
620 spin_lock_bh(&conn->session->lock);
621 list_add_tail(&conn->mtask->running,
622 &conn->mgmt_run_list);
623 spin_unlock_bh(&conn->session->lock);
624 rc = tt->xmit_mgmt_task(conn, conn->mtask);
628 /* done with this mtask */
635 if (unlikely(conn->suspend_tx))
641 static void iscsi_xmitworker(void *data)
643 struct iscsi_conn *conn = data;
646 * serialize Xmit worker on a per-connection basis.
648 mutex_lock(&conn->xmitmutex);
650 rc = iscsi_data_xmit(conn);
651 } while (rc >= 0 || rc == -EAGAIN);
652 mutex_unlock(&conn->xmitmutex);
656 FAILURE_BAD_HOST = 1,
657 FAILURE_SESSION_FAILED,
658 FAILURE_SESSION_FREED,
659 FAILURE_WINDOW_CLOSED,
660 FAILURE_SESSION_TERMINATE,
661 FAILURE_SESSION_IN_RECOVERY,
662 FAILURE_SESSION_RECOVERY_TIMEOUT,
665 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
667 struct Scsi_Host *host;
669 struct iscsi_session *session;
670 struct iscsi_conn *conn;
671 struct iscsi_cmd_task *ctask = NULL;
673 sc->scsi_done = done;
676 host = sc->device->host;
677 session = iscsi_hostdata(host->hostdata);
679 spin_lock(&session->lock);
682 * ISCSI_STATE_FAILED is a temp. state. The recovery
683 * code will decide what is best to do with command queued
686 if (session->state != ISCSI_STATE_LOGGED_IN &&
687 session->state != ISCSI_STATE_FAILED) {
689 * to handle the race between when we set the recovery state
690 * and block the session we requeue here (commands could
691 * be entering our queuecommand while a block is starting
692 * up because the block code is not locked)
694 if (session->state == ISCSI_STATE_IN_RECOVERY) {
695 reason = FAILURE_SESSION_IN_RECOVERY;
699 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
700 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
701 else if (session->state == ISCSI_STATE_TERMINATE)
702 reason = FAILURE_SESSION_TERMINATE;
704 reason = FAILURE_SESSION_FREED;
709 * Check for iSCSI window and take care of CmdSN wrap-around
711 if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
712 reason = FAILURE_WINDOW_CLOSED;
716 conn = session->leadconn;
718 __kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
719 sc->SCp.phase = session->age;
720 sc->SCp.ptr = (char *)ctask;
722 ctask->state = ISCSI_TASK_PENDING;
726 INIT_LIST_HEAD(&ctask->running);
727 ctask->total_length = sc->request_bufflen;
728 iscsi_prep_scsi_cmd_pdu(ctask);
730 session->tt->init_cmd_task(ctask);
732 list_add_tail(&ctask->running, &conn->xmitqueue);
734 "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
735 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
736 conn->id, (long)sc, ctask->itt, sc->request_bufflen,
737 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
738 spin_unlock(&session->lock);
740 scsi_queue_work(host, &conn->xmitwork);
744 spin_unlock(&session->lock);
745 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
746 return SCSI_MLQUEUE_HOST_BUSY;
749 spin_unlock(&session->lock);
750 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
751 sc->cmnd[0], reason);
752 sc->result = (DID_NO_CONNECT << 16);
753 sc->resid = sc->request_bufflen;
757 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
759 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
761 if (depth > ISCSI_MAX_CMD_PER_LUN)
762 depth = ISCSI_MAX_CMD_PER_LUN;
763 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
764 return sdev->queue_depth;
766 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
769 iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
770 char *data, uint32_t data_size)
772 struct iscsi_session *session = conn->session;
773 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
774 struct iscsi_mgmt_task *mtask;
776 spin_lock_bh(&session->lock);
777 if (session->state == ISCSI_STATE_TERMINATE) {
778 spin_unlock_bh(&session->lock);
781 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
782 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
784 * Login and Text are sent serially, in
785 * request-followed-by-response sequence.
786 * Same mtask can be used. Same ITT must be used.
787 * Note that login_mtask is preallocated at conn_create().
789 mtask = conn->login_mtask;
791 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
792 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
794 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
795 if (!__kfifo_get(session->mgmtpool.queue,
796 (void*)&mtask, sizeof(void*))) {
797 spin_unlock_bh(&session->lock);
803 * pre-format CmdSN for outgoing PDU.
805 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
806 hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
807 (session->age << ISCSI_AGE_SHIFT);
808 nop->cmdsn = cpu_to_be32(session->cmdsn);
809 if (conn->c_stage == ISCSI_CONN_STARTED &&
810 !(hdr->opcode & ISCSI_OP_IMMEDIATE))
813 /* do not advance CmdSN */
814 nop->cmdsn = cpu_to_be32(session->cmdsn);
817 memcpy(mtask->data, data, data_size);
818 mtask->data_count = data_size;
820 mtask->data_count = 0;
822 INIT_LIST_HEAD(&mtask->running);
823 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
824 if (session->tt->init_mgmt_task)
825 session->tt->init_mgmt_task(conn, mtask, data, data_size);
826 spin_unlock_bh(&session->lock);
828 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
829 hdr->opcode, hdr->itt, data_size);
832 * since send_pdu() could be called at least from two contexts,
833 * we need to serialize __kfifo_put, so we don't have to take
834 * additional lock on fast data-path
836 if (hdr->opcode & ISCSI_OP_IMMEDIATE)
837 __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
839 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
841 scsi_queue_work(session->host, &conn->xmitwork);
845 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
846 char *data, uint32_t data_size)
848 struct iscsi_conn *conn = cls_conn->dd_data;
851 mutex_lock(&conn->xmitmutex);
852 rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
853 mutex_unlock(&conn->xmitmutex);
857 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
859 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
861 struct iscsi_session *session = class_to_transport_session(cls_session);
862 struct iscsi_conn *conn = session->leadconn;
864 spin_lock_bh(&session->lock);
865 if (session->state != ISCSI_STATE_LOGGED_IN) {
866 session->state = ISCSI_STATE_RECOVERY_FAILED;
868 wake_up(&conn->ehwait);
870 spin_unlock_bh(&session->lock);
872 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
874 int iscsi_eh_host_reset(struct scsi_cmnd *sc)
876 struct Scsi_Host *host = sc->device->host;
877 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
878 struct iscsi_conn *conn = session->leadconn;
879 int fail_session = 0;
881 spin_lock_bh(&session->lock);
882 if (session->state == ISCSI_STATE_TERMINATE) {
884 debug_scsi("failing host reset: session terminated "
885 "[CID %d age %d]", conn->id, session->age);
886 spin_unlock_bh(&session->lock);
890 if (sc->SCp.phase == session->age) {
891 debug_scsi("failing connection CID %d due to SCSI host reset",
895 spin_unlock_bh(&session->lock);
898 * we drop the lock here but the leadconn cannot be destoyed while
899 * we are in the scsi eh
902 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
904 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
905 wait_event_interruptible(conn->ehwait,
906 session->state == ISCSI_STATE_TERMINATE ||
907 session->state == ISCSI_STATE_LOGGED_IN ||
908 session->state == ISCSI_STATE_RECOVERY_FAILED);
909 if (signal_pending(current))
910 flush_signals(current);
912 spin_lock_bh(&session->lock);
913 if (session->state == ISCSI_STATE_LOGGED_IN)
914 printk(KERN_INFO "iscsi: host reset succeeded\n");
917 spin_unlock_bh(&session->lock);
921 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
923 static void iscsi_tmabort_timedout(unsigned long data)
925 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
926 struct iscsi_conn *conn = ctask->conn;
927 struct iscsi_session *session = conn->session;
929 spin_lock(&session->lock);
930 if (conn->tmabort_state == TMABORT_INITIAL) {
931 conn->tmabort_state = TMABORT_TIMEDOUT;
932 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
933 ctask->sc, ctask->itt);
934 /* unblock eh_abort() */
935 wake_up(&conn->ehwait);
937 spin_unlock(&session->lock);
940 /* must be called with the mutex lock */
941 static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
942 struct iscsi_cmd_task *ctask)
944 struct iscsi_conn *conn = ctask->conn;
945 struct iscsi_session *session = conn->session;
946 struct iscsi_tm *hdr = &conn->tmhdr;
950 * ctask timed out but session is OK requests must be serialized.
952 memset(hdr, 0, sizeof(struct iscsi_tm));
953 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
954 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
955 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
956 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
957 hdr->rtt = ctask->hdr->itt;
958 hdr->refcmdsn = ctask->hdr->cmdsn;
960 rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
963 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
964 debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc);
968 debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
970 spin_lock_bh(&session->lock);
971 ctask->mtask = (struct iscsi_mgmt_task *)
972 session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
973 ISCSI_MGMT_ITT_OFFSET];
975 if (conn->tmabort_state == TMABORT_INITIAL) {
976 conn->tmfcmd_pdus_cnt++;
977 conn->tmabort_timer.expires = 10*HZ + jiffies;
978 conn->tmabort_timer.function = iscsi_tmabort_timedout;
979 conn->tmabort_timer.data = (unsigned long)ctask;
980 add_timer(&conn->tmabort_timer);
981 debug_scsi("abort set timeout [itt 0x%x]", ctask->itt);
983 spin_unlock_bh(&session->lock);
984 mutex_unlock(&conn->xmitmutex);
987 * block eh thread until:
991 * 3) session is terminated or restarted or userspace has
992 * given up on recovery
994 wait_event_interruptible(conn->ehwait,
995 sc->SCp.phase != session->age ||
996 session->state != ISCSI_STATE_LOGGED_IN ||
997 conn->tmabort_state != TMABORT_INITIAL);
998 if (signal_pending(current))
999 flush_signals(current);
1000 del_timer_sync(&conn->tmabort_timer);
1002 mutex_lock(&conn->xmitmutex);
1007 * xmit mutex and session lock must be held
1009 static struct iscsi_mgmt_task *
1010 iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1012 int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1013 struct iscsi_mgmt_task *task;
1015 debug_scsi("searching %d tasks\n", nr_tasks);
1017 for (i = 0; i < nr_tasks; i++) {
1018 __kfifo_get(fifo, (void*)&task, sizeof(void*));
1019 debug_scsi("check task %u\n", task->itt);
1021 if (task->itt == itt) {
1022 debug_scsi("matched task\n");
1026 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1031 static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1033 struct iscsi_conn *conn = ctask->conn;
1034 struct iscsi_session *session = conn->session;
1039 if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
1040 list_del(&ctask->mtask->running);
1041 __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1043 ctask->mtask = NULL;
1048 * session lock and xmitmutex must be held
1050 static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1053 struct scsi_cmnd *sc;
1059 conn->session->tt->cleanup_cmd_task(conn, ctask);
1060 iscsi_ctask_mtask_cleanup(ctask);
1063 sc->resid = sc->request_bufflen;
1064 iscsi_complete_command(conn->session, ctask);
1067 int iscsi_eh_abort(struct scsi_cmnd *sc)
1069 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1070 struct iscsi_conn *conn = ctask->conn;
1071 struct iscsi_session *session = conn->session;
1074 conn->eh_abort_cnt++;
1075 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1077 mutex_lock(&conn->xmitmutex);
1078 spin_lock_bh(&session->lock);
1081 * If we are not logged in or we have started a new session
1082 * then let the host reset code handle this
1084 if (session->state != ISCSI_STATE_LOGGED_IN ||
1085 sc->SCp.phase != session->age)
1088 /* ctask completed before time out */
1090 spin_unlock_bh(&session->lock);
1091 debug_scsi("sc completed while abort in progress\n");
1092 goto success_rel_mutex;
1095 /* what should we do here ? */
1096 if (conn->ctask == ctask) {
1097 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1098 "Failing abort\n", sc, ctask->itt);
1102 if (ctask->state == ISCSI_TASK_PENDING)
1103 goto success_cleanup;
1105 conn->tmabort_state = TMABORT_INITIAL;
1107 spin_unlock_bh(&session->lock);
1108 rc = iscsi_exec_abort_task(sc, ctask);
1109 spin_lock_bh(&session->lock);
1111 if (rc || sc->SCp.phase != session->age ||
1112 session->state != ISCSI_STATE_LOGGED_IN)
1114 iscsi_ctask_mtask_cleanup(ctask);
1116 switch (conn->tmabort_state) {
1117 case TMABORT_SUCCESS:
1118 goto success_cleanup;
1119 case TMABORT_NOT_FOUND:
1121 /* ctask completed before tmf abort response */
1122 spin_unlock_bh(&session->lock);
1123 debug_scsi("sc completed while abort in progress\n");
1124 goto success_rel_mutex;
1128 /* timedout or failed */
1129 spin_unlock_bh(&session->lock);
1130 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1131 spin_lock_bh(&session->lock);
1136 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1137 spin_unlock_bh(&session->lock);
1140 * clean up task if aborted. we have the xmitmutex so grab
1141 * the recv lock as a writer
1143 write_lock_bh(conn->recv_lock);
1144 spin_lock(&session->lock);
1145 fail_command(conn, ctask, DID_ABORT << 16);
1146 spin_unlock(&session->lock);
1147 write_unlock_bh(conn->recv_lock);
1150 mutex_unlock(&conn->xmitmutex);
1154 spin_unlock_bh(&session->lock);
1155 mutex_unlock(&conn->xmitmutex);
1157 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1160 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1163 iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1167 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1172 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1173 if (q->pool == NULL) {
1178 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1180 if (q->queue == ERR_PTR(-ENOMEM)) {
1186 for (i = 0; i < max; i++) {
1187 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1188 if (q->pool[i] == NULL) {
1191 for (j = 0; j < i; j++)
1194 kfifo_free(q->queue);
1199 memset(q->pool[i], 0, item_size);
1200 (*items)[i] = q->pool[i];
1201 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1205 EXPORT_SYMBOL_GPL(iscsi_pool_init);
1207 void iscsi_pool_free(struct iscsi_queue *q, void **items)
1211 for (i = 0; i < q->max; i++)
1216 EXPORT_SYMBOL_GPL(iscsi_pool_free);
1219 * iSCSI Session's hostdata organization:
1221 * *------------------* <== hostdata_session(host->hostdata)
1222 * | ptr to class sess|
1223 * |------------------| <== iscsi_hostdata(host->hostdata)
1225 * *------------------*
1228 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1229 _sz % sizeof(unsigned long))
1231 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1234 * iscsi_session_setup - create iscsi cls session and host and session
1235 * @scsit: scsi transport template
1236 * @iscsit: iscsi transport template
1237 * @initial_cmdsn: initial CmdSN
1238 * @hostno: host no allocated
1240 * This can be used by software iscsi_transports that allocate
1241 * a session per scsi host.
1243 struct iscsi_cls_session *
1244 iscsi_session_setup(struct iscsi_transport *iscsit,
1245 struct scsi_transport_template *scsit,
1246 int cmd_task_size, int mgmt_task_size,
1247 uint32_t initial_cmdsn, uint32_t *hostno)
1249 struct Scsi_Host *shost;
1250 struct iscsi_session *session;
1251 struct iscsi_cls_session *cls_session;
1254 shost = scsi_host_alloc(iscsit->host_template,
1255 hostdata_privsize(sizeof(*session)));
1260 shost->max_channel = 0;
1261 shost->max_lun = iscsit->max_lun;
1262 shost->max_cmd_len = iscsit->max_cmd_len;
1263 shost->transportt = scsit;
1264 shost->transportt->create_work_queue = 1;
1265 *hostno = shost->host_no;
1267 session = iscsi_hostdata(shost->hostdata);
1268 memset(session, 0, sizeof(struct iscsi_session));
1269 session->host = shost;
1270 session->state = ISCSI_STATE_FREE;
1271 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1272 session->cmds_max = ISCSI_XMIT_CMDS_MAX;
1273 session->cmdsn = initial_cmdsn;
1274 session->exp_cmdsn = initial_cmdsn + 1;
1275 session->max_cmdsn = initial_cmdsn + 1;
1276 session->max_r2t = 1;
1277 session->tt = iscsit;
1279 /* initialize SCSI PDU commands pool */
1280 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1281 (void***)&session->cmds,
1282 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1283 goto cmdpool_alloc_fail;
1285 /* pre-format cmds pool with ITT */
1286 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1287 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1290 ctask->dd_data = &ctask[1];
1292 INIT_LIST_HEAD(&ctask->running);
1295 spin_lock_init(&session->lock);
1296 INIT_LIST_HEAD(&session->connections);
1298 /* initialize immediate command pool */
1299 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1300 (void***)&session->mgmt_cmds,
1301 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1302 goto mgmtpool_alloc_fail;
1305 /* pre-format immediate cmds pool with ITT */
1306 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1307 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1310 mtask->dd_data = &mtask[1];
1311 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
1312 INIT_LIST_HEAD(&mtask->running);
1315 if (scsi_add_host(shost, NULL))
1318 if (!try_module_get(iscsit->owner))
1319 goto cls_session_fail;
1321 cls_session = iscsi_create_session(shost, iscsit, 0);
1324 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1329 module_put(iscsit->owner);
1331 scsi_remove_host(shost);
1333 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1334 mgmtpool_alloc_fail:
1335 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1337 scsi_host_put(shost);
1340 EXPORT_SYMBOL_GPL(iscsi_session_setup);
1343 * iscsi_session_teardown - destroy session, host, and cls_session
1346 * This can be used by software iscsi_transports that allocate
1347 * a session per scsi host.
1349 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1351 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1352 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1353 struct module *owner = cls_session->transport->owner;
1355 scsi_remove_host(shost);
1357 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1358 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1360 kfree(session->targetname);
1362 iscsi_destroy_session(cls_session);
1363 scsi_host_put(shost);
1366 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1369 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1370 * @cls_session: iscsi_cls_session
1373 struct iscsi_cls_conn *
1374 iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1376 struct iscsi_session *session = class_to_transport_session(cls_session);
1377 struct iscsi_conn *conn;
1378 struct iscsi_cls_conn *cls_conn;
1381 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1384 conn = cls_conn->dd_data;
1385 memset(conn, 0, sizeof(*conn));
1387 conn->session = session;
1388 conn->cls_conn = cls_conn;
1389 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1390 conn->id = conn_idx;
1391 conn->exp_statsn = 0;
1392 conn->tmabort_state = TMABORT_INITIAL;
1393 INIT_LIST_HEAD(&conn->run_list);
1394 INIT_LIST_HEAD(&conn->mgmt_run_list);
1395 INIT_LIST_HEAD(&conn->xmitqueue);
1397 /* initialize general immediate & non-immediate PDU commands queue */
1398 conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1400 if (conn->immqueue == ERR_PTR(-ENOMEM))
1401 goto immqueue_alloc_fail;
1403 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1405 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1406 goto mgmtqueue_alloc_fail;
1408 INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
1410 /* allocate login_mtask used for the login/text sequences */
1411 spin_lock_bh(&session->lock);
1412 if (!__kfifo_get(session->mgmtpool.queue,
1413 (void*)&conn->login_mtask,
1415 spin_unlock_bh(&session->lock);
1416 goto login_mtask_alloc_fail;
1418 spin_unlock_bh(&session->lock);
1420 data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
1422 goto login_mtask_data_alloc_fail;
1423 conn->login_mtask->data = conn->data = data;
1425 init_timer(&conn->tmabort_timer);
1426 mutex_init(&conn->xmitmutex);
1427 init_waitqueue_head(&conn->ehwait);
1431 login_mtask_data_alloc_fail:
1432 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1434 login_mtask_alloc_fail:
1435 kfifo_free(conn->mgmtqueue);
1436 mgmtqueue_alloc_fail:
1437 kfifo_free(conn->immqueue);
1438 immqueue_alloc_fail:
1439 iscsi_destroy_conn(cls_conn);
1442 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1445 * iscsi_conn_teardown - teardown iscsi connection
1446 * cls_conn: iscsi class connection
1448 * TODO: we may need to make this into a two step process
1449 * like scsi-mls remove + put host
1451 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1453 struct iscsi_conn *conn = cls_conn->dd_data;
1454 struct iscsi_session *session = conn->session;
1455 unsigned long flags;
1457 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1458 mutex_lock(&conn->xmitmutex);
1460 spin_lock_bh(&session->lock);
1461 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1462 if (session->leadconn == conn) {
1464 * leading connection? then give up on recovery.
1466 session->state = ISCSI_STATE_TERMINATE;
1467 wake_up(&conn->ehwait);
1469 spin_unlock_bh(&session->lock);
1471 mutex_unlock(&conn->xmitmutex);
1474 * Block until all in-progress commands for this connection
1478 spin_lock_irqsave(session->host->host_lock, flags);
1479 if (!session->host->host_busy) { /* OK for ERL == 0 */
1480 spin_unlock_irqrestore(session->host->host_lock, flags);
1483 spin_unlock_irqrestore(session->host->host_lock, flags);
1484 msleep_interruptible(500);
1485 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1486 "host_failed %d\n", session->host->host_busy,
1487 session->host->host_failed);
1489 * force eh_abort() to unblock
1491 wake_up(&conn->ehwait);
1494 spin_lock_bh(&session->lock);
1496 kfree(conn->persistent_address);
1497 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1499 list_del(&conn->item);
1500 if (list_empty(&session->connections))
1501 session->leadconn = NULL;
1502 if (session->leadconn && session->leadconn == conn)
1503 session->leadconn = container_of(session->connections.next,
1504 struct iscsi_conn, item);
1506 if (session->leadconn == NULL)
1507 /* no connections exits.. reset sequencing */
1508 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
1509 spin_unlock_bh(&session->lock);
1511 kfifo_free(conn->immqueue);
1512 kfifo_free(conn->mgmtqueue);
1514 iscsi_destroy_conn(cls_conn);
1516 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1518 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1520 struct iscsi_conn *conn = cls_conn->dd_data;
1521 struct iscsi_session *session = conn->session;
1523 if (session == NULL) {
1524 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1528 spin_lock_bh(&session->lock);
1529 conn->c_stage = ISCSI_CONN_STARTED;
1530 session->state = ISCSI_STATE_LOGGED_IN;
1532 switch(conn->stop_stage) {
1533 case STOP_CONN_RECOVER:
1535 * unblock eh_abort() if it is blocked. re-try all
1536 * commands after successful recovery
1538 conn->stop_stage = 0;
1539 conn->tmabort_state = TMABORT_INITIAL;
1541 spin_unlock_bh(&session->lock);
1543 iscsi_unblock_session(session_to_cls(session));
1544 wake_up(&conn->ehwait);
1546 case STOP_CONN_TERM:
1547 conn->stop_stage = 0;
1552 spin_unlock_bh(&session->lock);
1556 EXPORT_SYMBOL_GPL(iscsi_conn_start);
1559 flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1561 struct iscsi_mgmt_task *mtask, *tmp;
1563 /* handle pending */
1564 while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
1565 __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
1566 if (mtask == conn->login_mtask)
1568 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1569 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1573 /* handle running */
1574 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1575 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
1576 list_del(&mtask->running);
1578 if (mtask == conn->login_mtask)
1580 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1587 /* Fail commands. Mutex and session lock held and recv side suspended */
1588 static void fail_all_commands(struct iscsi_conn *conn)
1590 struct iscsi_cmd_task *ctask, *tmp;
1593 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1594 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1596 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1599 /* fail all other running */
1600 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1601 debug_scsi("failing in progress sc %p itt 0x%x\n",
1602 ctask->sc, ctask->itt);
1603 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1609 static void iscsi_start_session_recovery(struct iscsi_session *session,
1610 struct iscsi_conn *conn, int flag)
1614 spin_lock_bh(&session->lock);
1615 if (conn->stop_stage == STOP_CONN_TERM) {
1616 spin_unlock_bh(&session->lock);
1621 * When this is called for the in_login state, we only want to clean
1622 * up the login task and connection. We do not need to block and set
1623 * the recovery state again
1625 if (flag == STOP_CONN_TERM)
1626 session->state = ISCSI_STATE_TERMINATE;
1627 else if (conn->stop_stage != STOP_CONN_RECOVER)
1628 session->state = ISCSI_STATE_IN_RECOVERY;
1630 old_stop_stage = conn->stop_stage;
1631 conn->stop_stage = flag;
1632 conn->c_stage = ISCSI_CONN_STOPPED;
1633 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1634 spin_unlock_bh(&session->lock);
1636 write_lock_bh(conn->recv_lock);
1637 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1638 write_unlock_bh(conn->recv_lock);
1640 mutex_lock(&conn->xmitmutex);
1642 * for connection level recovery we should not calculate
1643 * header digest. conn->hdr_size used for optimization
1644 * in hdr_extract() and will be re-negotiated at
1647 if (flag == STOP_CONN_RECOVER) {
1648 conn->hdrdgst_en = 0;
1649 conn->datadgst_en = 0;
1650 if (session->state == ISCSI_STATE_IN_RECOVERY &&
1651 old_stop_stage != STOP_CONN_RECOVER) {
1652 debug_scsi("blocking session\n");
1653 iscsi_block_session(session_to_cls(session));
1660 spin_lock_bh(&session->lock);
1661 fail_all_commands(conn);
1662 flush_control_queues(session, conn);
1663 spin_unlock_bh(&session->lock);
1665 mutex_unlock(&conn->xmitmutex);
1668 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1670 struct iscsi_conn *conn = cls_conn->dd_data;
1671 struct iscsi_session *session = conn->session;
1674 case STOP_CONN_RECOVER:
1675 case STOP_CONN_TERM:
1676 iscsi_start_session_recovery(session, conn, flag);
1679 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
1682 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1684 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1685 struct iscsi_cls_conn *cls_conn, int is_leading)
1687 struct iscsi_session *session = class_to_transport_session(cls_session);
1688 struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data;
1690 /* lookup for existing connection */
1691 spin_lock_bh(&session->lock);
1692 list_for_each_entry(tmp, &session->connections, item) {
1694 if (conn->c_stage != ISCSI_CONN_STOPPED ||
1695 conn->stop_stage == STOP_CONN_TERM) {
1696 printk(KERN_ERR "iscsi: can't bind "
1697 "non-stopped connection (%d:%d)\n",
1698 conn->c_stage, conn->stop_stage);
1699 spin_unlock_bh(&session->lock);
1706 /* bind new iSCSI connection to session */
1707 conn->session = session;
1708 list_add(&conn->item, &session->connections);
1710 spin_unlock_bh(&session->lock);
1713 session->leadconn = conn;
1716 * Unblock xmitworker(), Login Phase will pass through.
1718 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1719 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1722 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1725 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1726 enum iscsi_param param, char *buf, int buflen)
1728 struct iscsi_conn *conn = cls_conn->dd_data;
1729 struct iscsi_session *session = conn->session;
1733 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1734 sscanf(buf, "%d", &conn->max_recv_dlength);
1736 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1737 sscanf(buf, "%d", &conn->max_xmit_dlength);
1739 case ISCSI_PARAM_HDRDGST_EN:
1740 sscanf(buf, "%d", &conn->hdrdgst_en);
1742 case ISCSI_PARAM_DATADGST_EN:
1743 sscanf(buf, "%d", &conn->datadgst_en);
1745 case ISCSI_PARAM_INITIAL_R2T_EN:
1746 sscanf(buf, "%d", &session->initial_r2t_en);
1748 case ISCSI_PARAM_MAX_R2T:
1749 sscanf(buf, "%d", &session->max_r2t);
1751 case ISCSI_PARAM_IMM_DATA_EN:
1752 sscanf(buf, "%d", &session->imm_data_en);
1754 case ISCSI_PARAM_FIRST_BURST:
1755 sscanf(buf, "%d", &session->first_burst);
1757 case ISCSI_PARAM_MAX_BURST:
1758 sscanf(buf, "%d", &session->max_burst);
1760 case ISCSI_PARAM_PDU_INORDER_EN:
1761 sscanf(buf, "%d", &session->pdu_inorder_en);
1763 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1764 sscanf(buf, "%d", &session->dataseq_inorder_en);
1766 case ISCSI_PARAM_ERL:
1767 sscanf(buf, "%d", &session->erl);
1769 case ISCSI_PARAM_IFMARKER_EN:
1770 sscanf(buf, "%d", &value);
1773 case ISCSI_PARAM_OFMARKER_EN:
1774 sscanf(buf, "%d", &value);
1777 case ISCSI_PARAM_EXP_STATSN:
1778 sscanf(buf, "%u", &conn->exp_statsn);
1780 case ISCSI_PARAM_TARGET_NAME:
1781 /* this should not change between logins */
1782 if (session->targetname)
1785 session->targetname = kstrdup(buf, GFP_KERNEL);
1786 if (!session->targetname)
1789 case ISCSI_PARAM_TPGT:
1790 sscanf(buf, "%d", &session->tpgt);
1792 case ISCSI_PARAM_PERSISTENT_PORT:
1793 sscanf(buf, "%d", &conn->persistent_port);
1795 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1797 * this is the address returned in discovery so it should
1798 * not change between logins.
1800 if (conn->persistent_address)
1803 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1804 if (!conn->persistent_address)
1813 EXPORT_SYMBOL_GPL(iscsi_set_param);
1815 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1816 enum iscsi_param param, char *buf)
1818 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1819 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1823 case ISCSI_PARAM_INITIAL_R2T_EN:
1824 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1826 case ISCSI_PARAM_MAX_R2T:
1827 len = sprintf(buf, "%hu\n", session->max_r2t);
1829 case ISCSI_PARAM_IMM_DATA_EN:
1830 len = sprintf(buf, "%d\n", session->imm_data_en);
1832 case ISCSI_PARAM_FIRST_BURST:
1833 len = sprintf(buf, "%u\n", session->first_burst);
1835 case ISCSI_PARAM_MAX_BURST:
1836 len = sprintf(buf, "%u\n", session->max_burst);
1838 case ISCSI_PARAM_PDU_INORDER_EN:
1839 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
1841 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1842 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
1844 case ISCSI_PARAM_ERL:
1845 len = sprintf(buf, "%d\n", session->erl);
1847 case ISCSI_PARAM_TARGET_NAME:
1848 len = sprintf(buf, "%s\n", session->targetname);
1850 case ISCSI_PARAM_TPGT:
1851 len = sprintf(buf, "%d\n", session->tpgt);
1859 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
1861 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
1862 enum iscsi_param param, char *buf)
1864 struct iscsi_conn *conn = cls_conn->dd_data;
1868 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1869 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
1871 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1872 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
1874 case ISCSI_PARAM_HDRDGST_EN:
1875 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
1877 case ISCSI_PARAM_DATADGST_EN:
1878 len = sprintf(buf, "%d\n", conn->datadgst_en);
1880 case ISCSI_PARAM_IFMARKER_EN:
1881 len = sprintf(buf, "%d\n", conn->ifmarker_en);
1883 case ISCSI_PARAM_OFMARKER_EN:
1884 len = sprintf(buf, "%d\n", conn->ofmarker_en);
1886 case ISCSI_PARAM_EXP_STATSN:
1887 len = sprintf(buf, "%u\n", conn->exp_statsn);
1889 case ISCSI_PARAM_PERSISTENT_PORT:
1890 len = sprintf(buf, "%d\n", conn->persistent_port);
1892 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1893 len = sprintf(buf, "%s\n", conn->persistent_address);
1901 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
1903 MODULE_AUTHOR("Mike Christie");
1904 MODULE_DESCRIPTION("iSCSI library functions");
1905 MODULE_LICENSE("GPL");