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 * @session: iscsi session
183 * @ctask: iscsi cmd task
185 * Must be called with session lock.
186 * This function returns the scsi command to scsi-ml and returns
187 * the cmd task to the pool of available cmd tasks.
189 static void iscsi_complete_command(struct iscsi_session *session,
190 struct iscsi_cmd_task *ctask)
192 struct scsi_cmnd *sc = ctask->sc;
194 ctask->state = ISCSI_TASK_COMPLETED;
196 list_del_init(&ctask->running);
197 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
202 * iscsi_cmd_rsp - SCSI Command Response processing
203 * @conn: iscsi connection
205 * @ctask: scsi command task
206 * @data: cmd data buffer
207 * @datalen: len of buffer
209 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
210 * then completes the command and task.
212 static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
213 struct iscsi_cmd_task *ctask, char *data,
217 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
218 struct iscsi_session *session = conn->session;
219 struct scsi_cmnd *sc = ctask->sc;
221 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
223 sc->result = DID_ERROR << 16;
227 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
229 sc->result = (DID_OK << 16) | rhdr->cmd_status;
231 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
232 sc->result = DID_ERROR << 16;
236 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
241 printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
242 "invalid data buffer size of %d\n", datalen);
243 sc->result = DID_BAD_TARGET << 16;
247 senselen = (data[0] << 8) | data[1];
248 if (datalen < senselen)
249 goto invalid_datalen;
251 memcpy(sc->sense_buffer, data + 2,
252 min(senselen, SCSI_SENSE_BUFFERSIZE));
253 debug_scsi("copied %d bytes of sense\n",
254 min(senselen, SCSI_SENSE_BUFFERSIZE));
257 if (sc->sc_data_direction == DMA_TO_DEVICE)
260 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
261 int res_count = be32_to_cpu(rhdr->residual_count);
263 if (res_count > 0 && res_count <= sc->request_bufflen)
264 sc->resid = res_count;
266 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
267 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
268 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
269 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
270 sc->resid = be32_to_cpu(rhdr->residual_count);
273 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
274 (long)sc, sc->result, ctask->itt);
275 conn->scsirsp_pdus_cnt++;
277 iscsi_complete_command(conn->session, ctask);
281 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
283 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
285 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
286 conn->tmfrsp_pdus_cnt++;
288 if (conn->tmabort_state != TMABORT_INITIAL)
291 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
292 conn->tmabort_state = TMABORT_SUCCESS;
293 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
294 conn->tmabort_state = TMABORT_NOT_FOUND;
296 conn->tmabort_state = TMABORT_FAILED;
297 wake_up(&conn->ehwait);
301 * __iscsi_complete_pdu - complete pdu
305 * @datalen: len of data buffer
307 * Completes pdu processing by freeing any resources allocated at
308 * queuecommand or send generic. session lock must be held and verify
309 * itt must have been called.
311 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
312 char *data, int datalen)
314 struct iscsi_session *session = conn->session;
315 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
316 struct iscsi_cmd_task *ctask;
317 struct iscsi_mgmt_task *mtask;
320 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
321 itt = hdr->itt & ISCSI_ITT_MASK;
325 if (itt < session->cmds_max) {
326 ctask = session->cmds[itt];
328 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
329 opcode, conn->id, ctask->itt, datalen);
332 case ISCSI_OP_SCSI_CMD_RSP:
333 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
334 rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
337 case ISCSI_OP_SCSI_DATA_IN:
338 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
339 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
340 conn->scsirsp_pdus_cnt++;
341 iscsi_complete_command(session, ctask);
345 /* LLD handles this for now */
348 rc = ISCSI_ERR_BAD_OPCODE;
351 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
352 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
353 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
355 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
356 opcode, conn->id, mtask->itt, datalen);
358 rc = iscsi_check_assign_cmdsn(session,
359 (struct iscsi_nopin*)hdr);
364 case ISCSI_OP_LOGOUT_RSP:
366 rc = ISCSI_ERR_PROTO;
369 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
371 case ISCSI_OP_LOGIN_RSP:
372 case ISCSI_OP_TEXT_RSP:
374 * login related PDU's exp_statsn is handled in
377 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
378 rc = ISCSI_ERR_CONN_FAILED;
379 list_del(&mtask->running);
380 if (conn->login_mtask != mtask)
381 __kfifo_put(session->mgmtpool.queue,
382 (void*)&mtask, sizeof(void*));
384 case ISCSI_OP_SCSI_TMFUNC_RSP:
386 rc = ISCSI_ERR_PROTO;
390 iscsi_tmf_rsp(conn, hdr);
392 case ISCSI_OP_NOOP_IN:
393 if (hdr->ttt != ISCSI_RESERVED_TAG || datalen) {
394 rc = ISCSI_ERR_PROTO;
397 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
399 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
400 rc = ISCSI_ERR_CONN_FAILED;
401 list_del(&mtask->running);
402 if (conn->login_mtask != mtask)
403 __kfifo_put(session->mgmtpool.queue,
404 (void*)&mtask, sizeof(void*));
407 rc = ISCSI_ERR_BAD_OPCODE;
410 } else if (itt == ISCSI_RESERVED_TAG) {
412 case ISCSI_OP_NOOP_IN:
414 rc = ISCSI_ERR_PROTO;
418 rc = iscsi_check_assign_cmdsn(session,
419 (struct iscsi_nopin*)hdr);
423 if (hdr->ttt == ISCSI_RESERVED_TAG)
426 if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
427 rc = ISCSI_ERR_CONN_FAILED;
429 case ISCSI_OP_REJECT:
430 /* we need sth like iscsi_reject_rsp()*/
431 case ISCSI_OP_ASYNC_EVENT:
432 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
433 /* we need sth like iscsi_async_event_rsp() */
434 rc = ISCSI_ERR_BAD_OPCODE;
437 rc = ISCSI_ERR_BAD_OPCODE;
441 rc = ISCSI_ERR_BAD_ITT;
446 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
448 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
449 char *data, int datalen)
453 spin_lock(&conn->session->lock);
454 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
455 spin_unlock(&conn->session->lock);
458 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
460 /* verify itt (itt encoding: age+cid+itt) */
461 int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
464 struct iscsi_session *session = conn->session;
465 struct iscsi_cmd_task *ctask;
468 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
469 if ((hdr->itt & ISCSI_AGE_MASK) !=
470 (session->age << ISCSI_AGE_SHIFT)) {
471 printk(KERN_ERR "iscsi: received itt %x expected "
472 "session age (%x)\n", hdr->itt,
473 session->age & ISCSI_AGE_MASK);
474 return ISCSI_ERR_BAD_ITT;
477 if ((hdr->itt & ISCSI_CID_MASK) !=
478 (conn->id << ISCSI_CID_SHIFT)) {
479 printk(KERN_ERR "iscsi: received itt %x, expected "
480 "CID (%x)\n", hdr->itt, conn->id);
481 return ISCSI_ERR_BAD_ITT;
483 itt = hdr->itt & ISCSI_ITT_MASK;
487 if (itt < session->cmds_max) {
488 ctask = session->cmds[itt];
491 printk(KERN_INFO "iscsi: dropping ctask with "
492 "itt 0x%x\n", ctask->itt);
494 return ISCSI_ERR_NO_SCSI_CMD;
497 if (ctask->sc->SCp.phase != session->age) {
498 printk(KERN_ERR "iscsi: ctask's session age %d, "
499 "expected %d\n", ctask->sc->SCp.phase,
501 return ISCSI_ERR_SESSION_FAILED;
508 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
510 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
512 struct iscsi_session *session = conn->session;
515 spin_lock_irqsave(&session->lock, flags);
516 if (session->state == ISCSI_STATE_FAILED) {
517 spin_unlock_irqrestore(&session->lock, flags);
521 if (conn->stop_stage == 0)
522 session->state = ISCSI_STATE_FAILED;
523 spin_unlock_irqrestore(&session->lock, flags);
524 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
525 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
526 iscsi_conn_error(conn->cls_conn, err);
528 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
531 * iscsi_data_xmit - xmit any command into the scheduled connection
532 * @conn: iscsi connection
535 * The function can return -EAGAIN in which case the caller must
536 * re-schedule it again later or recover. '0' return code means
539 static int iscsi_data_xmit(struct iscsi_conn *conn)
541 struct iscsi_transport *tt;
544 if (unlikely(conn->suspend_tx)) {
545 debug_scsi("conn %d Tx suspended!\n", conn->id);
548 tt = conn->session->tt;
551 * Transmit in the following order:
553 * 1) un-finished xmit (ctask or mtask)
554 * 2) immediate control PDUs
557 * 5) non-immediate control PDUs
559 * No need to lock around __kfifo_get as long as
560 * there's one producer and one consumer.
563 BUG_ON(conn->ctask && conn->mtask);
566 rc = tt->xmit_cmd_task(conn, conn->ctask);
569 /* done with this in-progress ctask */
573 rc = tt->xmit_mgmt_task(conn, conn->mtask);
576 /* done with this in-progress mtask */
580 /* process immediate first */
581 if (unlikely(__kfifo_len(conn->immqueue))) {
582 while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
584 spin_lock_bh(&conn->session->lock);
585 list_add_tail(&conn->mtask->running,
586 &conn->mgmt_run_list);
587 spin_unlock_bh(&conn->session->lock);
588 rc = tt->xmit_mgmt_task(conn, conn->mtask);
592 /* done with this mtask */
596 /* process command queue */
597 spin_lock_bh(&conn->session->lock);
598 while (!list_empty(&conn->xmitqueue)) {
600 * iscsi tcp may readd the task to the xmitqueue to send
603 conn->ctask = list_entry(conn->xmitqueue.next,
604 struct iscsi_cmd_task, running);
605 conn->ctask->state = ISCSI_TASK_RUNNING;
606 list_move_tail(conn->xmitqueue.next, &conn->run_list);
607 spin_unlock_bh(&conn->session->lock);
609 rc = tt->xmit_cmd_task(conn, conn->ctask);
612 spin_lock_bh(&conn->session->lock);
614 spin_unlock_bh(&conn->session->lock);
615 /* done with this ctask */
618 /* process the rest control plane PDUs, if any */
619 if (unlikely(__kfifo_len(conn->mgmtqueue))) {
620 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
622 spin_lock_bh(&conn->session->lock);
623 list_add_tail(&conn->mtask->running,
624 &conn->mgmt_run_list);
625 spin_unlock_bh(&conn->session->lock);
626 rc = tt->xmit_mgmt_task(conn, conn->mtask);
630 /* done with this mtask */
637 if (unlikely(conn->suspend_tx))
643 static void iscsi_xmitworker(void *data)
645 struct iscsi_conn *conn = data;
648 * serialize Xmit worker on a per-connection basis.
650 mutex_lock(&conn->xmitmutex);
652 rc = iscsi_data_xmit(conn);
653 } while (rc >= 0 || rc == -EAGAIN);
654 mutex_unlock(&conn->xmitmutex);
658 FAILURE_BAD_HOST = 1,
659 FAILURE_SESSION_FAILED,
660 FAILURE_SESSION_FREED,
661 FAILURE_WINDOW_CLOSED,
662 FAILURE_SESSION_TERMINATE,
663 FAILURE_SESSION_IN_RECOVERY,
664 FAILURE_SESSION_RECOVERY_TIMEOUT,
667 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
669 struct Scsi_Host *host;
671 struct iscsi_session *session;
672 struct iscsi_conn *conn;
673 struct iscsi_cmd_task *ctask = NULL;
675 sc->scsi_done = done;
678 host = sc->device->host;
679 session = iscsi_hostdata(host->hostdata);
681 spin_lock(&session->lock);
684 * ISCSI_STATE_FAILED is a temp. state. The recovery
685 * code will decide what is best to do with command queued
688 if (session->state != ISCSI_STATE_LOGGED_IN &&
689 session->state != ISCSI_STATE_FAILED) {
691 * to handle the race between when we set the recovery state
692 * and block the session we requeue here (commands could
693 * be entering our queuecommand while a block is starting
694 * up because the block code is not locked)
696 if (session->state == ISCSI_STATE_IN_RECOVERY) {
697 reason = FAILURE_SESSION_IN_RECOVERY;
701 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
702 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
703 else if (session->state == ISCSI_STATE_TERMINATE)
704 reason = FAILURE_SESSION_TERMINATE;
706 reason = FAILURE_SESSION_FREED;
711 * Check for iSCSI window and take care of CmdSN wrap-around
713 if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
714 reason = FAILURE_WINDOW_CLOSED;
718 conn = session->leadconn;
720 __kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
721 sc->SCp.phase = session->age;
722 sc->SCp.ptr = (char *)ctask;
724 ctask->state = ISCSI_TASK_PENDING;
728 INIT_LIST_HEAD(&ctask->running);
729 ctask->total_length = sc->request_bufflen;
730 iscsi_prep_scsi_cmd_pdu(ctask);
732 session->tt->init_cmd_task(ctask);
734 list_add_tail(&ctask->running, &conn->xmitqueue);
736 "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
737 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
738 conn->id, (long)sc, ctask->itt, sc->request_bufflen,
739 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
740 spin_unlock(&session->lock);
742 scsi_queue_work(host, &conn->xmitwork);
746 spin_unlock(&session->lock);
747 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
748 return SCSI_MLQUEUE_HOST_BUSY;
751 spin_unlock(&session->lock);
752 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
753 sc->cmnd[0], reason);
754 sc->result = (DID_NO_CONNECT << 16);
755 sc->resid = sc->request_bufflen;
759 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
761 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
763 if (depth > ISCSI_MAX_CMD_PER_LUN)
764 depth = ISCSI_MAX_CMD_PER_LUN;
765 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
766 return sdev->queue_depth;
768 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
771 iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
772 char *data, uint32_t data_size)
774 struct iscsi_session *session = conn->session;
775 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
776 struct iscsi_mgmt_task *mtask;
778 spin_lock_bh(&session->lock);
779 if (session->state == ISCSI_STATE_TERMINATE) {
780 spin_unlock_bh(&session->lock);
783 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
784 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
786 * Login and Text are sent serially, in
787 * request-followed-by-response sequence.
788 * Same mtask can be used. Same ITT must be used.
789 * Note that login_mtask is preallocated at conn_create().
791 mtask = conn->login_mtask;
793 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
794 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
796 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
797 if (!__kfifo_get(session->mgmtpool.queue,
798 (void*)&mtask, sizeof(void*))) {
799 spin_unlock_bh(&session->lock);
805 * pre-format CmdSN for outgoing PDU.
807 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
808 hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
809 (session->age << ISCSI_AGE_SHIFT);
810 nop->cmdsn = cpu_to_be32(session->cmdsn);
811 if (conn->c_stage == ISCSI_CONN_STARTED &&
812 !(hdr->opcode & ISCSI_OP_IMMEDIATE))
815 /* do not advance CmdSN */
816 nop->cmdsn = cpu_to_be32(session->cmdsn);
819 memcpy(mtask->data, data, data_size);
820 mtask->data_count = data_size;
822 mtask->data_count = 0;
824 INIT_LIST_HEAD(&mtask->running);
825 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
826 if (session->tt->init_mgmt_task)
827 session->tt->init_mgmt_task(conn, mtask, data, data_size);
828 spin_unlock_bh(&session->lock);
830 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
831 hdr->opcode, hdr->itt, data_size);
834 * since send_pdu() could be called at least from two contexts,
835 * we need to serialize __kfifo_put, so we don't have to take
836 * additional lock on fast data-path
838 if (hdr->opcode & ISCSI_OP_IMMEDIATE)
839 __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
841 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
843 scsi_queue_work(session->host, &conn->xmitwork);
847 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
848 char *data, uint32_t data_size)
850 struct iscsi_conn *conn = cls_conn->dd_data;
853 mutex_lock(&conn->xmitmutex);
854 rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
855 mutex_unlock(&conn->xmitmutex);
859 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
861 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
863 struct iscsi_session *session = class_to_transport_session(cls_session);
864 struct iscsi_conn *conn = session->leadconn;
866 spin_lock_bh(&session->lock);
867 if (session->state != ISCSI_STATE_LOGGED_IN) {
868 session->state = ISCSI_STATE_RECOVERY_FAILED;
870 wake_up(&conn->ehwait);
872 spin_unlock_bh(&session->lock);
874 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
876 int iscsi_eh_host_reset(struct scsi_cmnd *sc)
878 struct Scsi_Host *host = sc->device->host;
879 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
880 struct iscsi_conn *conn = session->leadconn;
881 int fail_session = 0;
883 spin_lock_bh(&session->lock);
884 if (session->state == ISCSI_STATE_TERMINATE) {
886 debug_scsi("failing host reset: session terminated "
887 "[CID %d age %d]", conn->id, session->age);
888 spin_unlock_bh(&session->lock);
892 if (sc->SCp.phase == session->age) {
893 debug_scsi("failing connection CID %d due to SCSI host reset",
897 spin_unlock_bh(&session->lock);
900 * we drop the lock here but the leadconn cannot be destoyed while
901 * we are in the scsi eh
904 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
906 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
907 wait_event_interruptible(conn->ehwait,
908 session->state == ISCSI_STATE_TERMINATE ||
909 session->state == ISCSI_STATE_LOGGED_IN ||
910 session->state == ISCSI_STATE_RECOVERY_FAILED);
911 if (signal_pending(current))
912 flush_signals(current);
914 spin_lock_bh(&session->lock);
915 if (session->state == ISCSI_STATE_LOGGED_IN)
916 printk(KERN_INFO "iscsi: host reset succeeded\n");
919 spin_unlock_bh(&session->lock);
923 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
925 static void iscsi_tmabort_timedout(unsigned long data)
927 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
928 struct iscsi_conn *conn = ctask->conn;
929 struct iscsi_session *session = conn->session;
931 spin_lock(&session->lock);
932 if (conn->tmabort_state == TMABORT_INITIAL) {
933 conn->tmabort_state = TMABORT_TIMEDOUT;
934 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
935 ctask->sc, ctask->itt);
936 /* unblock eh_abort() */
937 wake_up(&conn->ehwait);
939 spin_unlock(&session->lock);
942 /* must be called with the mutex lock */
943 static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
944 struct iscsi_cmd_task *ctask)
946 struct iscsi_conn *conn = ctask->conn;
947 struct iscsi_session *session = conn->session;
948 struct iscsi_tm *hdr = &conn->tmhdr;
952 * ctask timed out but session is OK requests must be serialized.
954 memset(hdr, 0, sizeof(struct iscsi_tm));
955 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
956 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
957 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
958 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
959 hdr->rtt = ctask->hdr->itt;
960 hdr->refcmdsn = ctask->hdr->cmdsn;
962 rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
965 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
966 debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc);
970 debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
972 spin_lock_bh(&session->lock);
973 ctask->mtask = (struct iscsi_mgmt_task *)
974 session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
975 ISCSI_MGMT_ITT_OFFSET];
977 if (conn->tmabort_state == TMABORT_INITIAL) {
978 conn->tmfcmd_pdus_cnt++;
979 conn->tmabort_timer.expires = 10*HZ + jiffies;
980 conn->tmabort_timer.function = iscsi_tmabort_timedout;
981 conn->tmabort_timer.data = (unsigned long)ctask;
982 add_timer(&conn->tmabort_timer);
983 debug_scsi("abort set timeout [itt 0x%x]", ctask->itt);
985 spin_unlock_bh(&session->lock);
986 mutex_unlock(&conn->xmitmutex);
989 * block eh thread until:
993 * 3) session is terminated or restarted or userspace has
994 * given up on recovery
996 wait_event_interruptible(conn->ehwait,
997 sc->SCp.phase != session->age ||
998 session->state != ISCSI_STATE_LOGGED_IN ||
999 conn->tmabort_state != TMABORT_INITIAL);
1000 if (signal_pending(current))
1001 flush_signals(current);
1002 del_timer_sync(&conn->tmabort_timer);
1004 mutex_lock(&conn->xmitmutex);
1009 * xmit mutex and session lock must be held
1011 static struct iscsi_mgmt_task *
1012 iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1014 int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1015 struct iscsi_mgmt_task *task;
1017 debug_scsi("searching %d tasks\n", nr_tasks);
1019 for (i = 0; i < nr_tasks; i++) {
1020 __kfifo_get(fifo, (void*)&task, sizeof(void*));
1021 debug_scsi("check task %u\n", task->itt);
1023 if (task->itt == itt) {
1024 debug_scsi("matched task\n");
1028 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1033 static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1035 struct iscsi_conn *conn = ctask->conn;
1036 struct iscsi_session *session = conn->session;
1041 if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
1042 list_del(&ctask->mtask->running);
1043 __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1045 ctask->mtask = NULL;
1050 * session lock and xmitmutex must be held
1052 static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1055 struct scsi_cmnd *sc;
1061 conn->session->tt->cleanup_cmd_task(conn, ctask);
1062 iscsi_ctask_mtask_cleanup(ctask);
1065 sc->resid = sc->request_bufflen;
1066 iscsi_complete_command(conn->session, ctask);
1069 int iscsi_eh_abort(struct scsi_cmnd *sc)
1071 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1072 struct iscsi_conn *conn = ctask->conn;
1073 struct iscsi_session *session = conn->session;
1076 conn->eh_abort_cnt++;
1077 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1079 mutex_lock(&conn->xmitmutex);
1080 spin_lock_bh(&session->lock);
1083 * If we are not logged in or we have started a new session
1084 * then let the host reset code handle this
1086 if (session->state != ISCSI_STATE_LOGGED_IN ||
1087 sc->SCp.phase != session->age)
1090 /* ctask completed before time out */
1092 spin_unlock_bh(&session->lock);
1093 debug_scsi("sc completed while abort in progress\n");
1094 goto success_rel_mutex;
1097 /* what should we do here ? */
1098 if (conn->ctask == ctask) {
1099 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1100 "Failing abort\n", sc, ctask->itt);
1104 if (ctask->state == ISCSI_TASK_PENDING)
1105 goto success_cleanup;
1107 conn->tmabort_state = TMABORT_INITIAL;
1109 spin_unlock_bh(&session->lock);
1110 rc = iscsi_exec_abort_task(sc, ctask);
1111 spin_lock_bh(&session->lock);
1113 if (rc || sc->SCp.phase != session->age ||
1114 session->state != ISCSI_STATE_LOGGED_IN)
1116 iscsi_ctask_mtask_cleanup(ctask);
1118 switch (conn->tmabort_state) {
1119 case TMABORT_SUCCESS:
1120 goto success_cleanup;
1121 case TMABORT_NOT_FOUND:
1123 /* ctask completed before tmf abort response */
1124 spin_unlock_bh(&session->lock);
1125 debug_scsi("sc completed while abort in progress\n");
1126 goto success_rel_mutex;
1130 /* timedout or failed */
1131 spin_unlock_bh(&session->lock);
1132 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1133 spin_lock_bh(&session->lock);
1138 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1139 spin_unlock_bh(&session->lock);
1142 * clean up task if aborted. we have the xmitmutex so grab
1143 * the recv lock as a writer
1145 write_lock_bh(conn->recv_lock);
1146 spin_lock(&session->lock);
1147 fail_command(conn, ctask, DID_ABORT << 16);
1148 spin_unlock(&session->lock);
1149 write_unlock_bh(conn->recv_lock);
1152 mutex_unlock(&conn->xmitmutex);
1156 spin_unlock_bh(&session->lock);
1157 mutex_unlock(&conn->xmitmutex);
1159 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1162 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1165 iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1169 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1174 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1175 if (q->pool == NULL) {
1180 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1182 if (q->queue == ERR_PTR(-ENOMEM)) {
1188 for (i = 0; i < max; i++) {
1189 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1190 if (q->pool[i] == NULL) {
1193 for (j = 0; j < i; j++)
1196 kfifo_free(q->queue);
1201 memset(q->pool[i], 0, item_size);
1202 (*items)[i] = q->pool[i];
1203 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1207 EXPORT_SYMBOL_GPL(iscsi_pool_init);
1209 void iscsi_pool_free(struct iscsi_queue *q, void **items)
1213 for (i = 0; i < q->max; i++)
1218 EXPORT_SYMBOL_GPL(iscsi_pool_free);
1221 * iSCSI Session's hostdata organization:
1223 * *------------------* <== hostdata_session(host->hostdata)
1224 * | ptr to class sess|
1225 * |------------------| <== iscsi_hostdata(host->hostdata)
1227 * *------------------*
1230 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1231 _sz % sizeof(unsigned long))
1233 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1236 * iscsi_session_setup - create iscsi cls session and host and session
1237 * @scsit: scsi transport template
1238 * @iscsit: iscsi transport template
1239 * @initial_cmdsn: initial CmdSN
1240 * @hostno: host no allocated
1242 * This can be used by software iscsi_transports that allocate
1243 * a session per scsi host.
1245 struct iscsi_cls_session *
1246 iscsi_session_setup(struct iscsi_transport *iscsit,
1247 struct scsi_transport_template *scsit,
1248 int cmd_task_size, int mgmt_task_size,
1249 uint32_t initial_cmdsn, uint32_t *hostno)
1251 struct Scsi_Host *shost;
1252 struct iscsi_session *session;
1253 struct iscsi_cls_session *cls_session;
1256 shost = scsi_host_alloc(iscsit->host_template,
1257 hostdata_privsize(sizeof(*session)));
1262 shost->max_channel = 0;
1263 shost->max_lun = iscsit->max_lun;
1264 shost->max_cmd_len = iscsit->max_cmd_len;
1265 shost->transportt = scsit;
1266 shost->transportt->create_work_queue = 1;
1267 *hostno = shost->host_no;
1269 session = iscsi_hostdata(shost->hostdata);
1270 memset(session, 0, sizeof(struct iscsi_session));
1271 session->host = shost;
1272 session->state = ISCSI_STATE_FREE;
1273 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1274 session->cmds_max = ISCSI_XMIT_CMDS_MAX;
1275 session->cmdsn = initial_cmdsn;
1276 session->exp_cmdsn = initial_cmdsn + 1;
1277 session->max_cmdsn = initial_cmdsn + 1;
1278 session->max_r2t = 1;
1279 session->tt = iscsit;
1281 /* initialize SCSI PDU commands pool */
1282 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1283 (void***)&session->cmds,
1284 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1285 goto cmdpool_alloc_fail;
1287 /* pre-format cmds pool with ITT */
1288 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1289 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1292 ctask->dd_data = &ctask[1];
1294 INIT_LIST_HEAD(&ctask->running);
1297 spin_lock_init(&session->lock);
1298 INIT_LIST_HEAD(&session->connections);
1300 /* initialize immediate command pool */
1301 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1302 (void***)&session->mgmt_cmds,
1303 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1304 goto mgmtpool_alloc_fail;
1307 /* pre-format immediate cmds pool with ITT */
1308 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1309 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1312 mtask->dd_data = &mtask[1];
1313 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
1314 INIT_LIST_HEAD(&mtask->running);
1317 if (scsi_add_host(shost, NULL))
1320 if (!try_module_get(iscsit->owner))
1321 goto cls_session_fail;
1323 cls_session = iscsi_create_session(shost, iscsit, 0);
1326 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1331 module_put(iscsit->owner);
1333 scsi_remove_host(shost);
1335 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1336 mgmtpool_alloc_fail:
1337 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1339 scsi_host_put(shost);
1342 EXPORT_SYMBOL_GPL(iscsi_session_setup);
1345 * iscsi_session_teardown - destroy session, host, and cls_session
1348 * This can be used by software iscsi_transports that allocate
1349 * a session per scsi host.
1351 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1353 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1354 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1355 struct module *owner = cls_session->transport->owner;
1357 scsi_remove_host(shost);
1359 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1360 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1362 kfree(session->targetname);
1364 iscsi_destroy_session(cls_session);
1365 scsi_host_put(shost);
1368 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1371 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1372 * @cls_session: iscsi_cls_session
1375 struct iscsi_cls_conn *
1376 iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1378 struct iscsi_session *session = class_to_transport_session(cls_session);
1379 struct iscsi_conn *conn;
1380 struct iscsi_cls_conn *cls_conn;
1383 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1386 conn = cls_conn->dd_data;
1387 memset(conn, 0, sizeof(*conn));
1389 conn->session = session;
1390 conn->cls_conn = cls_conn;
1391 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1392 conn->id = conn_idx;
1393 conn->exp_statsn = 0;
1394 conn->tmabort_state = TMABORT_INITIAL;
1395 INIT_LIST_HEAD(&conn->run_list);
1396 INIT_LIST_HEAD(&conn->mgmt_run_list);
1397 INIT_LIST_HEAD(&conn->xmitqueue);
1399 /* initialize general immediate & non-immediate PDU commands queue */
1400 conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1402 if (conn->immqueue == ERR_PTR(-ENOMEM))
1403 goto immqueue_alloc_fail;
1405 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1407 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1408 goto mgmtqueue_alloc_fail;
1410 INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
1412 /* allocate login_mtask used for the login/text sequences */
1413 spin_lock_bh(&session->lock);
1414 if (!__kfifo_get(session->mgmtpool.queue,
1415 (void*)&conn->login_mtask,
1417 spin_unlock_bh(&session->lock);
1418 goto login_mtask_alloc_fail;
1420 spin_unlock_bh(&session->lock);
1422 data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
1424 goto login_mtask_data_alloc_fail;
1425 conn->login_mtask->data = conn->data = data;
1427 init_timer(&conn->tmabort_timer);
1428 mutex_init(&conn->xmitmutex);
1429 init_waitqueue_head(&conn->ehwait);
1433 login_mtask_data_alloc_fail:
1434 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1436 login_mtask_alloc_fail:
1437 kfifo_free(conn->mgmtqueue);
1438 mgmtqueue_alloc_fail:
1439 kfifo_free(conn->immqueue);
1440 immqueue_alloc_fail:
1441 iscsi_destroy_conn(cls_conn);
1444 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1447 * iscsi_conn_teardown - teardown iscsi connection
1448 * cls_conn: iscsi class connection
1450 * TODO: we may need to make this into a two step process
1451 * like scsi-mls remove + put host
1453 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1455 struct iscsi_conn *conn = cls_conn->dd_data;
1456 struct iscsi_session *session = conn->session;
1457 unsigned long flags;
1459 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1460 mutex_lock(&conn->xmitmutex);
1462 spin_lock_bh(&session->lock);
1463 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1464 if (session->leadconn == conn) {
1466 * leading connection? then give up on recovery.
1468 session->state = ISCSI_STATE_TERMINATE;
1469 wake_up(&conn->ehwait);
1471 spin_unlock_bh(&session->lock);
1473 mutex_unlock(&conn->xmitmutex);
1476 * Block until all in-progress commands for this connection
1480 spin_lock_irqsave(session->host->host_lock, flags);
1481 if (!session->host->host_busy) { /* OK for ERL == 0 */
1482 spin_unlock_irqrestore(session->host->host_lock, flags);
1485 spin_unlock_irqrestore(session->host->host_lock, flags);
1486 msleep_interruptible(500);
1487 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1488 "host_failed %d\n", session->host->host_busy,
1489 session->host->host_failed);
1491 * force eh_abort() to unblock
1493 wake_up(&conn->ehwait);
1496 spin_lock_bh(&session->lock);
1498 kfree(conn->persistent_address);
1499 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1501 list_del(&conn->item);
1502 if (list_empty(&session->connections))
1503 session->leadconn = NULL;
1504 if (session->leadconn && session->leadconn == conn)
1505 session->leadconn = container_of(session->connections.next,
1506 struct iscsi_conn, item);
1508 if (session->leadconn == NULL)
1509 /* no connections exits.. reset sequencing */
1510 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
1511 spin_unlock_bh(&session->lock);
1513 kfifo_free(conn->immqueue);
1514 kfifo_free(conn->mgmtqueue);
1516 iscsi_destroy_conn(cls_conn);
1518 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1520 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1522 struct iscsi_conn *conn = cls_conn->dd_data;
1523 struct iscsi_session *session = conn->session;
1526 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1530 if (session->first_burst > session->max_burst) {
1531 printk("iscsi: invalid burst lengths: "
1532 "first_burst %d max_burst %d\n",
1533 session->first_burst, session->max_burst);
1537 spin_lock_bh(&session->lock);
1538 conn->c_stage = ISCSI_CONN_STARTED;
1539 session->state = ISCSI_STATE_LOGGED_IN;
1541 switch(conn->stop_stage) {
1542 case STOP_CONN_RECOVER:
1544 * unblock eh_abort() if it is blocked. re-try all
1545 * commands after successful recovery
1547 conn->stop_stage = 0;
1548 conn->tmabort_state = TMABORT_INITIAL;
1550 spin_unlock_bh(&session->lock);
1552 iscsi_unblock_session(session_to_cls(session));
1553 wake_up(&conn->ehwait);
1555 case STOP_CONN_TERM:
1556 conn->stop_stage = 0;
1561 spin_unlock_bh(&session->lock);
1565 EXPORT_SYMBOL_GPL(iscsi_conn_start);
1568 flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1570 struct iscsi_mgmt_task *mtask, *tmp;
1572 /* handle pending */
1573 while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
1574 __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
1575 if (mtask == conn->login_mtask)
1577 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1578 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1582 /* handle running */
1583 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1584 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
1585 list_del(&mtask->running);
1587 if (mtask == conn->login_mtask)
1589 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1596 /* Fail commands. Mutex and session lock held and recv side suspended */
1597 static void fail_all_commands(struct iscsi_conn *conn)
1599 struct iscsi_cmd_task *ctask, *tmp;
1602 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1603 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1605 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1608 /* fail all other running */
1609 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1610 debug_scsi("failing in progress sc %p itt 0x%x\n",
1611 ctask->sc, ctask->itt);
1612 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1618 static void iscsi_start_session_recovery(struct iscsi_session *session,
1619 struct iscsi_conn *conn, int flag)
1623 spin_lock_bh(&session->lock);
1624 if (conn->stop_stage == STOP_CONN_TERM) {
1625 spin_unlock_bh(&session->lock);
1630 * When this is called for the in_login state, we only want to clean
1631 * up the login task and connection. We do not need to block and set
1632 * the recovery state again
1634 if (flag == STOP_CONN_TERM)
1635 session->state = ISCSI_STATE_TERMINATE;
1636 else if (conn->stop_stage != STOP_CONN_RECOVER)
1637 session->state = ISCSI_STATE_IN_RECOVERY;
1639 old_stop_stage = conn->stop_stage;
1640 conn->stop_stage = flag;
1641 conn->c_stage = ISCSI_CONN_STOPPED;
1642 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1643 spin_unlock_bh(&session->lock);
1645 write_lock_bh(conn->recv_lock);
1646 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1647 write_unlock_bh(conn->recv_lock);
1649 mutex_lock(&conn->xmitmutex);
1651 * for connection level recovery we should not calculate
1652 * header digest. conn->hdr_size used for optimization
1653 * in hdr_extract() and will be re-negotiated at
1656 if (flag == STOP_CONN_RECOVER) {
1657 conn->hdrdgst_en = 0;
1658 conn->datadgst_en = 0;
1659 if (session->state == ISCSI_STATE_IN_RECOVERY &&
1660 old_stop_stage != STOP_CONN_RECOVER) {
1661 debug_scsi("blocking session\n");
1662 iscsi_block_session(session_to_cls(session));
1669 spin_lock_bh(&session->lock);
1670 fail_all_commands(conn);
1671 flush_control_queues(session, conn);
1672 spin_unlock_bh(&session->lock);
1674 mutex_unlock(&conn->xmitmutex);
1677 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1679 struct iscsi_conn *conn = cls_conn->dd_data;
1680 struct iscsi_session *session = conn->session;
1683 case STOP_CONN_RECOVER:
1684 case STOP_CONN_TERM:
1685 iscsi_start_session_recovery(session, conn, flag);
1688 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
1691 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1693 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1694 struct iscsi_cls_conn *cls_conn, int is_leading)
1696 struct iscsi_session *session = class_to_transport_session(cls_session);
1697 struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data;
1699 /* lookup for existing connection */
1700 spin_lock_bh(&session->lock);
1701 list_for_each_entry(tmp, &session->connections, item) {
1703 if (conn->c_stage != ISCSI_CONN_STOPPED ||
1704 conn->stop_stage == STOP_CONN_TERM) {
1705 printk(KERN_ERR "iscsi: can't bind "
1706 "non-stopped connection (%d:%d)\n",
1707 conn->c_stage, conn->stop_stage);
1708 spin_unlock_bh(&session->lock);
1715 /* bind new iSCSI connection to session */
1716 conn->session = session;
1717 list_add(&conn->item, &session->connections);
1719 spin_unlock_bh(&session->lock);
1722 session->leadconn = conn;
1725 * Unblock xmitworker(), Login Phase will pass through.
1727 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1728 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1731 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1734 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1735 enum iscsi_param param, char *buf, int buflen)
1737 struct iscsi_conn *conn = cls_conn->dd_data;
1738 struct iscsi_session *session = conn->session;
1742 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1743 sscanf(buf, "%d", &conn->max_recv_dlength);
1745 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1746 sscanf(buf, "%d", &conn->max_xmit_dlength);
1748 case ISCSI_PARAM_HDRDGST_EN:
1749 sscanf(buf, "%d", &conn->hdrdgst_en);
1751 case ISCSI_PARAM_DATADGST_EN:
1752 sscanf(buf, "%d", &conn->datadgst_en);
1754 case ISCSI_PARAM_INITIAL_R2T_EN:
1755 sscanf(buf, "%d", &session->initial_r2t_en);
1757 case ISCSI_PARAM_MAX_R2T:
1758 sscanf(buf, "%d", &session->max_r2t);
1760 case ISCSI_PARAM_IMM_DATA_EN:
1761 sscanf(buf, "%d", &session->imm_data_en);
1763 case ISCSI_PARAM_FIRST_BURST:
1764 sscanf(buf, "%d", &session->first_burst);
1766 case ISCSI_PARAM_MAX_BURST:
1767 sscanf(buf, "%d", &session->max_burst);
1769 case ISCSI_PARAM_PDU_INORDER_EN:
1770 sscanf(buf, "%d", &session->pdu_inorder_en);
1772 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1773 sscanf(buf, "%d", &session->dataseq_inorder_en);
1775 case ISCSI_PARAM_ERL:
1776 sscanf(buf, "%d", &session->erl);
1778 case ISCSI_PARAM_IFMARKER_EN:
1779 sscanf(buf, "%d", &value);
1782 case ISCSI_PARAM_OFMARKER_EN:
1783 sscanf(buf, "%d", &value);
1786 case ISCSI_PARAM_EXP_STATSN:
1787 sscanf(buf, "%u", &conn->exp_statsn);
1789 case ISCSI_PARAM_TARGET_NAME:
1790 /* this should not change between logins */
1791 if (session->targetname)
1794 session->targetname = kstrdup(buf, GFP_KERNEL);
1795 if (!session->targetname)
1798 case ISCSI_PARAM_TPGT:
1799 sscanf(buf, "%d", &session->tpgt);
1801 case ISCSI_PARAM_PERSISTENT_PORT:
1802 sscanf(buf, "%d", &conn->persistent_port);
1804 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1806 * this is the address returned in discovery so it should
1807 * not change between logins.
1809 if (conn->persistent_address)
1812 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1813 if (!conn->persistent_address)
1822 EXPORT_SYMBOL_GPL(iscsi_set_param);
1824 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1825 enum iscsi_param param, char *buf)
1827 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1828 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1832 case ISCSI_PARAM_INITIAL_R2T_EN:
1833 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1835 case ISCSI_PARAM_MAX_R2T:
1836 len = sprintf(buf, "%hu\n", session->max_r2t);
1838 case ISCSI_PARAM_IMM_DATA_EN:
1839 len = sprintf(buf, "%d\n", session->imm_data_en);
1841 case ISCSI_PARAM_FIRST_BURST:
1842 len = sprintf(buf, "%u\n", session->first_burst);
1844 case ISCSI_PARAM_MAX_BURST:
1845 len = sprintf(buf, "%u\n", session->max_burst);
1847 case ISCSI_PARAM_PDU_INORDER_EN:
1848 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
1850 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1851 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
1853 case ISCSI_PARAM_ERL:
1854 len = sprintf(buf, "%d\n", session->erl);
1856 case ISCSI_PARAM_TARGET_NAME:
1857 len = sprintf(buf, "%s\n", session->targetname);
1859 case ISCSI_PARAM_TPGT:
1860 len = sprintf(buf, "%d\n", session->tpgt);
1868 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
1870 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
1871 enum iscsi_param param, char *buf)
1873 struct iscsi_conn *conn = cls_conn->dd_data;
1877 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1878 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
1880 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1881 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
1883 case ISCSI_PARAM_HDRDGST_EN:
1884 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
1886 case ISCSI_PARAM_DATADGST_EN:
1887 len = sprintf(buf, "%d\n", conn->datadgst_en);
1889 case ISCSI_PARAM_IFMARKER_EN:
1890 len = sprintf(buf, "%d\n", conn->ifmarker_en);
1892 case ISCSI_PARAM_OFMARKER_EN:
1893 len = sprintf(buf, "%d\n", conn->ofmarker_en);
1895 case ISCSI_PARAM_EXP_STATSN:
1896 len = sprintf(buf, "%u\n", conn->exp_statsn);
1898 case ISCSI_PARAM_PERSISTENT_PORT:
1899 len = sprintf(buf, "%d\n", conn->persistent_port);
1901 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1902 len = sprintf(buf, "%s\n", conn->persistent_address);
1910 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
1912 MODULE_AUTHOR("Mike Christie");
1913 MODULE_DESCRIPTION("iSCSI library functions");
1914 MODULE_LICENSE("GPL");