4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/types.h>
25 #include <linux/mutex.h>
26 #include <linux/kfifo.h>
27 #include <linux/delay.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi.h>
35 #include <scsi/iscsi_proto.h>
36 #include <scsi/scsi_transport.h>
37 #include <scsi/scsi_transport_iscsi.h>
38 #include <scsi/libiscsi.h>
40 struct iscsi_session *
41 class_to_transport_session(struct iscsi_cls_session *cls_session)
43 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
44 return iscsi_hostdata(shost->hostdata);
46 EXPORT_SYMBOL_GPL(class_to_transport_session);
48 #define INVALID_SN_DELTA 0xffff
51 iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
53 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
54 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
56 if (max_cmdsn < exp_cmdsn -1 &&
57 max_cmdsn > exp_cmdsn - INVALID_SN_DELTA)
58 return ISCSI_ERR_MAX_CMDSN;
59 if (max_cmdsn > session->max_cmdsn ||
60 max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA)
61 session->max_cmdsn = max_cmdsn;
62 if (exp_cmdsn > session->exp_cmdsn ||
63 exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA)
64 session->exp_cmdsn = exp_cmdsn;
68 EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn);
70 void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
71 struct iscsi_data *hdr)
73 struct iscsi_conn *conn = ctask->conn;
75 memset(hdr, 0, sizeof(struct iscsi_data));
76 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
77 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
78 ctask->unsol_datasn++;
79 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
80 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
82 hdr->itt = ctask->hdr->itt;
83 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
84 hdr->offset = cpu_to_be32(ctask->unsol_offset);
86 if (ctask->unsol_count > conn->max_xmit_dlength) {
87 hton24(hdr->dlength, conn->max_xmit_dlength);
88 ctask->data_count = conn->max_xmit_dlength;
89 ctask->unsol_offset += ctask->data_count;
92 hton24(hdr->dlength, ctask->unsol_count);
93 ctask->data_count = ctask->unsol_count;
94 hdr->flags = ISCSI_FLAG_CMD_FINAL;
97 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
100 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
101 * @ctask: iscsi cmd task
103 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
104 * fields like dlength or final based on how much data it sends
106 static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
108 struct iscsi_conn *conn = ctask->conn;
109 struct iscsi_session *session = conn->session;
110 struct iscsi_cmd *hdr = ctask->hdr;
111 struct scsi_cmnd *sc = ctask->sc;
113 hdr->opcode = ISCSI_OP_SCSI_CMD;
114 hdr->flags = ISCSI_ATTR_SIMPLE;
115 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
116 hdr->itt = ctask->itt | (conn->id << ISCSI_CID_SHIFT) |
117 (session->age << ISCSI_AGE_SHIFT);
118 hdr->data_length = cpu_to_be32(sc->request_bufflen);
119 hdr->cmdsn = cpu_to_be32(session->cmdsn);
121 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
122 memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
123 memset(&hdr->cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
125 ctask->data_count = 0;
126 if (sc->sc_data_direction == DMA_TO_DEVICE) {
127 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
131 * imm_count bytes to be sent right after
134 * unsol_count bytes(as Data-Out) to be sent
135 * without R2T ack right after
138 * r2t_data_count bytes to be sent via R2T ack's
140 * pad_count bytes to be sent as zero-padding
142 ctask->imm_count = 0;
143 ctask->unsol_count = 0;
144 ctask->unsol_offset = 0;
145 ctask->unsol_datasn = 0;
147 if (session->imm_data_en) {
148 if (ctask->total_length >= session->first_burst)
149 ctask->imm_count = min(session->first_burst,
150 conn->max_xmit_dlength);
152 ctask->imm_count = min(ctask->total_length,
153 conn->max_xmit_dlength);
154 hton24(ctask->hdr->dlength, ctask->imm_count);
156 zero_data(ctask->hdr->dlength);
158 if (!session->initial_r2t_en) {
159 ctask->unsol_count = min(session->first_burst,
160 ctask->total_length) - ctask->imm_count;
161 ctask->unsol_offset = ctask->imm_count;
164 if (!ctask->unsol_count)
165 /* No unsolicit Data-Out's */
166 ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
169 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
170 zero_data(hdr->dlength);
172 if (sc->sc_data_direction == DMA_FROM_DEVICE)
173 hdr->flags |= ISCSI_FLAG_CMD_READ;
176 conn->scsicmd_pdus_cnt++;
178 EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu);
181 * iscsi_complete_command - return command back to scsi-ml
182 * @ctask: iscsi cmd task
184 * Must be called with session lock.
185 * This function returns the scsi command to scsi-ml and returns
186 * the cmd task to the pool of available cmd tasks.
188 static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
190 struct iscsi_session *session = ctask->conn->session;
191 struct scsi_cmnd *sc = ctask->sc;
193 ctask->state = ISCSI_TASK_COMPLETED;
195 /* SCSI eh reuses commands to verify us */
197 list_del_init(&ctask->running);
198 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
202 static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
204 atomic_inc(&ctask->refcount);
207 static void iscsi_get_ctask(struct iscsi_cmd_task *ctask)
209 spin_lock_bh(&ctask->conn->session->lock);
210 __iscsi_get_ctask(ctask);
211 spin_unlock_bh(&ctask->conn->session->lock);
214 static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
216 if (atomic_dec_and_test(&ctask->refcount))
217 iscsi_complete_command(ctask);
220 static void iscsi_put_ctask(struct iscsi_cmd_task *ctask)
222 spin_lock_bh(&ctask->conn->session->lock);
223 __iscsi_put_ctask(ctask);
224 spin_unlock_bh(&ctask->conn->session->lock);
228 * iscsi_cmd_rsp - SCSI Command Response processing
229 * @conn: iscsi connection
231 * @ctask: scsi command task
232 * @data: cmd data buffer
233 * @datalen: len of buffer
235 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
236 * then completes the command and task.
238 static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
239 struct iscsi_cmd_task *ctask, char *data,
243 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
244 struct iscsi_session *session = conn->session;
245 struct scsi_cmnd *sc = ctask->sc;
247 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
249 sc->result = DID_ERROR << 16;
253 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
255 sc->result = (DID_OK << 16) | rhdr->cmd_status;
257 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
258 sc->result = DID_ERROR << 16;
262 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
267 printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
268 "invalid data buffer size of %d\n", datalen);
269 sc->result = DID_BAD_TARGET << 16;
273 senselen = (data[0] << 8) | data[1];
274 if (datalen < senselen)
275 goto invalid_datalen;
277 memcpy(sc->sense_buffer, data + 2,
278 min(senselen, SCSI_SENSE_BUFFERSIZE));
279 debug_scsi("copied %d bytes of sense\n",
280 min(senselen, SCSI_SENSE_BUFFERSIZE));
283 if (sc->sc_data_direction == DMA_TO_DEVICE)
286 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
287 int res_count = be32_to_cpu(rhdr->residual_count);
289 if (res_count > 0 && res_count <= sc->request_bufflen)
290 sc->resid = res_count;
292 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
293 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
294 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
295 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
296 sc->resid = be32_to_cpu(rhdr->residual_count);
299 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
300 (long)sc, sc->result, ctask->itt);
301 conn->scsirsp_pdus_cnt++;
303 __iscsi_put_ctask(ctask);
307 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
309 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
311 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
312 conn->tmfrsp_pdus_cnt++;
314 if (conn->tmabort_state != TMABORT_INITIAL)
317 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
318 conn->tmabort_state = TMABORT_SUCCESS;
319 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
320 conn->tmabort_state = TMABORT_NOT_FOUND;
322 conn->tmabort_state = TMABORT_FAILED;
323 wake_up(&conn->ehwait);
326 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
327 char *data, int datalen)
329 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
330 struct iscsi_hdr rejected_pdu;
333 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
335 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
336 if (ntoh24(reject->dlength) > datalen)
337 return ISCSI_ERR_PROTO;
339 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
340 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
341 itt = rejected_pdu.itt & ISCSI_ITT_MASK;
342 printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
343 "due to DataDigest error.\n", itt,
344 rejected_pdu.opcode);
351 * __iscsi_complete_pdu - complete pdu
355 * @datalen: len of data buffer
357 * Completes pdu processing by freeing any resources allocated at
358 * queuecommand or send generic. session lock must be held and verify
359 * itt must have been called.
361 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
362 char *data, int datalen)
364 struct iscsi_session *session = conn->session;
365 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
366 struct iscsi_cmd_task *ctask;
367 struct iscsi_mgmt_task *mtask;
370 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
371 itt = hdr->itt & ISCSI_ITT_MASK;
375 if (itt < session->cmds_max) {
376 ctask = session->cmds[itt];
378 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
379 opcode, conn->id, ctask->itt, datalen);
382 case ISCSI_OP_SCSI_CMD_RSP:
383 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
384 rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
387 case ISCSI_OP_SCSI_DATA_IN:
388 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
389 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
390 conn->scsirsp_pdus_cnt++;
391 __iscsi_put_ctask(ctask);
395 /* LLD handles this for now */
398 rc = ISCSI_ERR_BAD_OPCODE;
401 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
402 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
403 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
405 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
406 opcode, conn->id, mtask->itt, datalen);
408 rc = iscsi_check_assign_cmdsn(session,
409 (struct iscsi_nopin*)hdr);
414 case ISCSI_OP_LOGOUT_RSP:
416 rc = ISCSI_ERR_PROTO;
419 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
421 case ISCSI_OP_LOGIN_RSP:
422 case ISCSI_OP_TEXT_RSP:
424 * login related PDU's exp_statsn is handled in
427 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
428 rc = ISCSI_ERR_CONN_FAILED;
429 list_del(&mtask->running);
430 if (conn->login_mtask != mtask)
431 __kfifo_put(session->mgmtpool.queue,
432 (void*)&mtask, sizeof(void*));
434 case ISCSI_OP_SCSI_TMFUNC_RSP:
436 rc = ISCSI_ERR_PROTO;
440 iscsi_tmf_rsp(conn, hdr);
442 case ISCSI_OP_NOOP_IN:
443 if (hdr->ttt != ISCSI_RESERVED_TAG || datalen) {
444 rc = ISCSI_ERR_PROTO;
447 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
449 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
450 rc = ISCSI_ERR_CONN_FAILED;
451 list_del(&mtask->running);
452 if (conn->login_mtask != mtask)
453 __kfifo_put(session->mgmtpool.queue,
454 (void*)&mtask, sizeof(void*));
457 rc = ISCSI_ERR_BAD_OPCODE;
460 } else if (itt == ISCSI_RESERVED_TAG) {
461 rc = iscsi_check_assign_cmdsn(session,
462 (struct iscsi_nopin*)hdr);
467 case ISCSI_OP_NOOP_IN:
469 rc = ISCSI_ERR_PROTO;
473 if (hdr->ttt == ISCSI_RESERVED_TAG)
476 if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
477 rc = ISCSI_ERR_CONN_FAILED;
479 case ISCSI_OP_REJECT:
480 rc = iscsi_handle_reject(conn, hdr, data, datalen);
482 case ISCSI_OP_ASYNC_EVENT:
483 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
484 /* we need sth like iscsi_async_event_rsp() */
485 rc = ISCSI_ERR_BAD_OPCODE;
488 rc = ISCSI_ERR_BAD_OPCODE;
492 rc = ISCSI_ERR_BAD_ITT;
497 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
499 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
500 char *data, int datalen)
504 spin_lock(&conn->session->lock);
505 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
506 spin_unlock(&conn->session->lock);
509 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
511 /* verify itt (itt encoding: age+cid+itt) */
512 int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
515 struct iscsi_session *session = conn->session;
516 struct iscsi_cmd_task *ctask;
519 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
520 if ((hdr->itt & ISCSI_AGE_MASK) !=
521 (session->age << ISCSI_AGE_SHIFT)) {
522 printk(KERN_ERR "iscsi: received itt %x expected "
523 "session age (%x)\n", hdr->itt,
524 session->age & ISCSI_AGE_MASK);
525 return ISCSI_ERR_BAD_ITT;
528 if ((hdr->itt & ISCSI_CID_MASK) !=
529 (conn->id << ISCSI_CID_SHIFT)) {
530 printk(KERN_ERR "iscsi: received itt %x, expected "
531 "CID (%x)\n", hdr->itt, conn->id);
532 return ISCSI_ERR_BAD_ITT;
534 itt = hdr->itt & ISCSI_ITT_MASK;
538 if (itt < session->cmds_max) {
539 ctask = session->cmds[itt];
542 printk(KERN_INFO "iscsi: dropping ctask with "
543 "itt 0x%x\n", ctask->itt);
545 return ISCSI_ERR_NO_SCSI_CMD;
548 if (ctask->sc->SCp.phase != session->age) {
549 printk(KERN_ERR "iscsi: ctask's session age %d, "
550 "expected %d\n", ctask->sc->SCp.phase,
552 return ISCSI_ERR_SESSION_FAILED;
559 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
561 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
563 struct iscsi_session *session = conn->session;
566 spin_lock_irqsave(&session->lock, flags);
567 if (session->state == ISCSI_STATE_FAILED) {
568 spin_unlock_irqrestore(&session->lock, flags);
572 if (conn->stop_stage == 0)
573 session->state = ISCSI_STATE_FAILED;
574 spin_unlock_irqrestore(&session->lock, flags);
575 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
576 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
577 iscsi_conn_error(conn->cls_conn, err);
579 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
582 * iscsi_data_xmit - xmit any command into the scheduled connection
583 * @conn: iscsi connection
586 * The function can return -EAGAIN in which case the caller must
587 * re-schedule it again later or recover. '0' return code means
590 static int iscsi_data_xmit(struct iscsi_conn *conn)
592 struct iscsi_transport *tt;
595 if (unlikely(conn->suspend_tx)) {
596 debug_scsi("conn %d Tx suspended!\n", conn->id);
599 tt = conn->session->tt;
602 * Transmit in the following order:
604 * 1) un-finished xmit (ctask or mtask)
605 * 2) immediate control PDUs
608 * 5) non-immediate control PDUs
610 * No need to lock around __kfifo_get as long as
611 * there's one producer and one consumer.
614 BUG_ON(conn->ctask && conn->mtask);
617 iscsi_get_ctask(conn->ctask);
618 rc = tt->xmit_cmd_task(conn, conn->ctask);
619 iscsi_put_ctask(conn->ctask);
622 /* done with this in-progress ctask */
626 rc = tt->xmit_mgmt_task(conn, conn->mtask);
629 /* done with this in-progress mtask */
633 /* process immediate first */
634 if (unlikely(__kfifo_len(conn->immqueue))) {
635 while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
637 spin_lock_bh(&conn->session->lock);
638 list_add_tail(&conn->mtask->running,
639 &conn->mgmt_run_list);
640 spin_unlock_bh(&conn->session->lock);
641 rc = tt->xmit_mgmt_task(conn, conn->mtask);
645 /* done with this mtask */
649 /* process command queue */
650 spin_lock_bh(&conn->session->lock);
651 while (!list_empty(&conn->xmitqueue)) {
653 * iscsi tcp may readd the task to the xmitqueue to send
656 conn->ctask = list_entry(conn->xmitqueue.next,
657 struct iscsi_cmd_task, running);
658 conn->ctask->state = ISCSI_TASK_RUNNING;
659 list_move_tail(conn->xmitqueue.next, &conn->run_list);
660 __iscsi_get_ctask(conn->ctask);
661 spin_unlock_bh(&conn->session->lock);
663 rc = tt->xmit_cmd_task(conn, conn->ctask);
667 spin_lock_bh(&conn->session->lock);
668 __iscsi_put_ctask(conn->ctask);
670 spin_unlock_bh(&conn->session->lock);
674 spin_unlock_bh(&conn->session->lock);
675 /* done with this ctask */
678 /* process the rest control plane PDUs, if any */
679 if (unlikely(__kfifo_len(conn->mgmtqueue))) {
680 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
682 spin_lock_bh(&conn->session->lock);
683 list_add_tail(&conn->mtask->running,
684 &conn->mgmt_run_list);
685 spin_unlock_bh(&conn->session->lock);
686 rc = tt->xmit_mgmt_task(conn, conn->mtask);
690 /* done with this mtask */
697 if (unlikely(conn->suspend_tx))
703 static void iscsi_xmitworker(void *data)
705 struct iscsi_conn *conn = data;
708 * serialize Xmit worker on a per-connection basis.
710 mutex_lock(&conn->xmitmutex);
712 rc = iscsi_data_xmit(conn);
713 } while (rc >= 0 || rc == -EAGAIN);
714 mutex_unlock(&conn->xmitmutex);
718 FAILURE_BAD_HOST = 1,
719 FAILURE_SESSION_FAILED,
720 FAILURE_SESSION_FREED,
721 FAILURE_WINDOW_CLOSED,
723 FAILURE_SESSION_TERMINATE,
724 FAILURE_SESSION_IN_RECOVERY,
725 FAILURE_SESSION_RECOVERY_TIMEOUT,
728 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
730 struct Scsi_Host *host;
732 struct iscsi_session *session;
733 struct iscsi_conn *conn;
734 struct iscsi_cmd_task *ctask = NULL;
736 sc->scsi_done = done;
740 host = sc->device->host;
741 session = iscsi_hostdata(host->hostdata);
743 spin_lock(&session->lock);
746 * ISCSI_STATE_FAILED is a temp. state. The recovery
747 * code will decide what is best to do with command queued
750 if (session->state != ISCSI_STATE_LOGGED_IN &&
751 session->state != ISCSI_STATE_FAILED) {
753 * to handle the race between when we set the recovery state
754 * and block the session we requeue here (commands could
755 * be entering our queuecommand while a block is starting
756 * up because the block code is not locked)
758 if (session->state == ISCSI_STATE_IN_RECOVERY) {
759 reason = FAILURE_SESSION_IN_RECOVERY;
763 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
764 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
765 else if (session->state == ISCSI_STATE_TERMINATE)
766 reason = FAILURE_SESSION_TERMINATE;
768 reason = FAILURE_SESSION_FREED;
773 * Check for iSCSI window and take care of CmdSN wrap-around
775 if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
776 reason = FAILURE_WINDOW_CLOSED;
780 conn = session->leadconn;
782 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
784 reason = FAILURE_OOM;
787 sc->SCp.phase = session->age;
788 sc->SCp.ptr = (char *)ctask;
790 atomic_set(&ctask->refcount, 1);
791 ctask->state = ISCSI_TASK_PENDING;
795 INIT_LIST_HEAD(&ctask->running);
796 ctask->total_length = sc->request_bufflen;
797 iscsi_prep_scsi_cmd_pdu(ctask);
799 session->tt->init_cmd_task(ctask);
801 list_add_tail(&ctask->running, &conn->xmitqueue);
803 "ctask enq [%s cid %d sc %p cdb 0x%x itt 0x%x len %d cmdsn %d "
805 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
806 conn->id, sc, sc->cmnd[0], ctask->itt, sc->request_bufflen,
807 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
808 spin_unlock(&session->lock);
810 scsi_queue_work(host, &conn->xmitwork);
814 spin_unlock(&session->lock);
815 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
816 return SCSI_MLQUEUE_HOST_BUSY;
819 spin_unlock(&session->lock);
820 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
821 sc->cmnd[0], reason);
822 sc->result = (DID_NO_CONNECT << 16);
823 sc->resid = sc->request_bufflen;
827 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
829 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
831 if (depth > ISCSI_MAX_CMD_PER_LUN)
832 depth = ISCSI_MAX_CMD_PER_LUN;
833 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
834 return sdev->queue_depth;
836 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
839 iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
840 char *data, uint32_t data_size)
842 struct iscsi_session *session = conn->session;
843 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
844 struct iscsi_mgmt_task *mtask;
846 spin_lock_bh(&session->lock);
847 if (session->state == ISCSI_STATE_TERMINATE) {
848 spin_unlock_bh(&session->lock);
851 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
852 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
854 * Login and Text are sent serially, in
855 * request-followed-by-response sequence.
856 * Same mtask can be used. Same ITT must be used.
857 * Note that login_mtask is preallocated at conn_create().
859 mtask = conn->login_mtask;
861 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
862 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
864 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
865 if (!__kfifo_get(session->mgmtpool.queue,
866 (void*)&mtask, sizeof(void*))) {
867 spin_unlock_bh(&session->lock);
873 * pre-format CmdSN for outgoing PDU.
875 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
876 hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
877 (session->age << ISCSI_AGE_SHIFT);
878 nop->cmdsn = cpu_to_be32(session->cmdsn);
879 if (conn->c_stage == ISCSI_CONN_STARTED &&
880 !(hdr->opcode & ISCSI_OP_IMMEDIATE))
883 /* do not advance CmdSN */
884 nop->cmdsn = cpu_to_be32(session->cmdsn);
887 memcpy(mtask->data, data, data_size);
888 mtask->data_count = data_size;
890 mtask->data_count = 0;
892 INIT_LIST_HEAD(&mtask->running);
893 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
894 if (session->tt->init_mgmt_task)
895 session->tt->init_mgmt_task(conn, mtask, data, data_size);
896 spin_unlock_bh(&session->lock);
898 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
899 hdr->opcode, hdr->itt, data_size);
902 * since send_pdu() could be called at least from two contexts,
903 * we need to serialize __kfifo_put, so we don't have to take
904 * additional lock on fast data-path
906 if (hdr->opcode & ISCSI_OP_IMMEDIATE)
907 __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
909 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
911 scsi_queue_work(session->host, &conn->xmitwork);
915 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
916 char *data, uint32_t data_size)
918 struct iscsi_conn *conn = cls_conn->dd_data;
921 mutex_lock(&conn->xmitmutex);
922 rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
923 mutex_unlock(&conn->xmitmutex);
927 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
929 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
931 struct iscsi_session *session = class_to_transport_session(cls_session);
932 struct iscsi_conn *conn = session->leadconn;
934 spin_lock_bh(&session->lock);
935 if (session->state != ISCSI_STATE_LOGGED_IN) {
936 session->state = ISCSI_STATE_RECOVERY_FAILED;
938 wake_up(&conn->ehwait);
940 spin_unlock_bh(&session->lock);
942 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
944 int iscsi_eh_host_reset(struct scsi_cmnd *sc)
946 struct Scsi_Host *host = sc->device->host;
947 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
948 struct iscsi_conn *conn = session->leadconn;
949 int fail_session = 0;
951 spin_lock_bh(&session->lock);
952 if (session->state == ISCSI_STATE_TERMINATE) {
954 debug_scsi("failing host reset: session terminated "
955 "[CID %d age %d]", conn->id, session->age);
956 spin_unlock_bh(&session->lock);
960 if (sc->SCp.phase == session->age) {
961 debug_scsi("failing connection CID %d due to SCSI host reset",
965 spin_unlock_bh(&session->lock);
968 * we drop the lock here but the leadconn cannot be destoyed while
969 * we are in the scsi eh
972 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
974 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
975 wait_event_interruptible(conn->ehwait,
976 session->state == ISCSI_STATE_TERMINATE ||
977 session->state == ISCSI_STATE_LOGGED_IN ||
978 session->state == ISCSI_STATE_RECOVERY_FAILED);
979 if (signal_pending(current))
980 flush_signals(current);
982 spin_lock_bh(&session->lock);
983 if (session->state == ISCSI_STATE_LOGGED_IN)
984 printk(KERN_INFO "iscsi: host reset succeeded\n");
987 spin_unlock_bh(&session->lock);
991 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
993 static void iscsi_tmabort_timedout(unsigned long data)
995 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
996 struct iscsi_conn *conn = ctask->conn;
997 struct iscsi_session *session = conn->session;
999 spin_lock(&session->lock);
1000 if (conn->tmabort_state == TMABORT_INITIAL) {
1001 conn->tmabort_state = TMABORT_TIMEDOUT;
1002 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
1003 ctask->sc, ctask->itt);
1004 /* unblock eh_abort() */
1005 wake_up(&conn->ehwait);
1007 spin_unlock(&session->lock);
1010 /* must be called with the mutex lock */
1011 static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
1012 struct iscsi_cmd_task *ctask)
1014 struct iscsi_conn *conn = ctask->conn;
1015 struct iscsi_session *session = conn->session;
1016 struct iscsi_tm *hdr = &conn->tmhdr;
1020 * ctask timed out but session is OK requests must be serialized.
1022 memset(hdr, 0, sizeof(struct iscsi_tm));
1023 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1024 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
1025 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1026 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1027 hdr->rtt = ctask->hdr->itt;
1028 hdr->refcmdsn = ctask->hdr->cmdsn;
1030 rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
1033 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1034 debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc);
1038 debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
1040 spin_lock_bh(&session->lock);
1041 ctask->mtask = (struct iscsi_mgmt_task *)
1042 session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
1043 ISCSI_MGMT_ITT_OFFSET];
1045 if (conn->tmabort_state == TMABORT_INITIAL) {
1046 conn->tmfcmd_pdus_cnt++;
1047 conn->tmabort_timer.expires = 10*HZ + jiffies;
1048 conn->tmabort_timer.function = iscsi_tmabort_timedout;
1049 conn->tmabort_timer.data = (unsigned long)ctask;
1050 add_timer(&conn->tmabort_timer);
1051 debug_scsi("abort set timeout [itt 0x%x]", ctask->itt);
1053 spin_unlock_bh(&session->lock);
1054 mutex_unlock(&conn->xmitmutex);
1057 * block eh thread until:
1061 * 3) session is terminated or restarted or userspace has
1062 * given up on recovery
1064 wait_event_interruptible(conn->ehwait,
1065 sc->SCp.phase != session->age ||
1066 session->state != ISCSI_STATE_LOGGED_IN ||
1067 conn->tmabort_state != TMABORT_INITIAL);
1068 if (signal_pending(current))
1069 flush_signals(current);
1070 del_timer_sync(&conn->tmabort_timer);
1072 mutex_lock(&conn->xmitmutex);
1077 * xmit mutex and session lock must be held
1079 static struct iscsi_mgmt_task *
1080 iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1082 int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1083 struct iscsi_mgmt_task *task;
1085 debug_scsi("searching %d tasks\n", nr_tasks);
1087 for (i = 0; i < nr_tasks; i++) {
1088 __kfifo_get(fifo, (void*)&task, sizeof(void*));
1089 debug_scsi("check task %u\n", task->itt);
1091 if (task->itt == itt) {
1092 debug_scsi("matched task\n");
1096 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1101 static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1103 struct iscsi_conn *conn = ctask->conn;
1104 struct iscsi_session *session = conn->session;
1109 if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
1110 list_del(&ctask->mtask->running);
1111 __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1113 ctask->mtask = NULL;
1118 * session lock and xmitmutex must be held
1120 static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1123 struct scsi_cmnd *sc;
1129 conn->session->tt->cleanup_cmd_task(conn, ctask);
1130 iscsi_ctask_mtask_cleanup(ctask);
1133 sc->resid = sc->request_bufflen;
1134 /* release ref from queuecommand */
1135 __iscsi_put_ctask(ctask);
1138 int iscsi_eh_abort(struct scsi_cmnd *sc)
1140 struct iscsi_cmd_task *ctask;
1141 struct iscsi_conn *conn;
1142 struct iscsi_session *session;
1146 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1150 debug_scsi("sc never reached iscsi layer or it completed.\n");
1154 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1156 session = conn->session;
1158 conn->eh_abort_cnt++;
1159 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1161 mutex_lock(&conn->xmitmutex);
1162 spin_lock_bh(&session->lock);
1165 * If we are not logged in or we have started a new session
1166 * then let the host reset code handle this
1168 if (session->state != ISCSI_STATE_LOGGED_IN ||
1169 sc->SCp.phase != session->age)
1172 /* ctask completed before time out */
1174 spin_unlock_bh(&session->lock);
1175 debug_scsi("sc completed while abort in progress\n");
1176 goto success_rel_mutex;
1179 /* what should we do here ? */
1180 if (conn->ctask == ctask) {
1181 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1182 "Failing abort\n", sc, ctask->itt);
1186 if (ctask->state == ISCSI_TASK_PENDING)
1187 goto success_cleanup;
1189 conn->tmabort_state = TMABORT_INITIAL;
1191 spin_unlock_bh(&session->lock);
1192 rc = iscsi_exec_abort_task(sc, ctask);
1193 spin_lock_bh(&session->lock);
1195 if (rc || sc->SCp.phase != session->age ||
1196 session->state != ISCSI_STATE_LOGGED_IN)
1198 iscsi_ctask_mtask_cleanup(ctask);
1200 switch (conn->tmabort_state) {
1201 case TMABORT_SUCCESS:
1202 goto success_cleanup;
1203 case TMABORT_NOT_FOUND:
1205 /* ctask completed before tmf abort response */
1206 spin_unlock_bh(&session->lock);
1207 debug_scsi("sc completed while abort in progress\n");
1208 goto success_rel_mutex;
1212 /* timedout or failed */
1213 spin_unlock_bh(&session->lock);
1214 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1215 spin_lock_bh(&session->lock);
1220 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1221 spin_unlock_bh(&session->lock);
1224 * clean up task if aborted. we have the xmitmutex so grab
1225 * the recv lock as a writer
1227 write_lock_bh(conn->recv_lock);
1228 spin_lock(&session->lock);
1229 fail_command(conn, ctask, DID_ABORT << 16);
1230 spin_unlock(&session->lock);
1231 write_unlock_bh(conn->recv_lock);
1234 mutex_unlock(&conn->xmitmutex);
1238 spin_unlock_bh(&session->lock);
1239 mutex_unlock(&conn->xmitmutex);
1241 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1244 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1247 iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1251 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1256 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1257 if (q->pool == NULL) {
1262 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1264 if (q->queue == ERR_PTR(-ENOMEM)) {
1270 for (i = 0; i < max; i++) {
1271 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1272 if (q->pool[i] == NULL) {
1275 for (j = 0; j < i; j++)
1278 kfifo_free(q->queue);
1283 memset(q->pool[i], 0, item_size);
1284 (*items)[i] = q->pool[i];
1285 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1289 EXPORT_SYMBOL_GPL(iscsi_pool_init);
1291 void iscsi_pool_free(struct iscsi_queue *q, void **items)
1295 for (i = 0; i < q->max; i++)
1300 EXPORT_SYMBOL_GPL(iscsi_pool_free);
1303 * iSCSI Session's hostdata organization:
1305 * *------------------* <== hostdata_session(host->hostdata)
1306 * | ptr to class sess|
1307 * |------------------| <== iscsi_hostdata(host->hostdata)
1309 * *------------------*
1312 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1313 _sz % sizeof(unsigned long))
1315 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1318 * iscsi_session_setup - create iscsi cls session and host and session
1319 * @scsit: scsi transport template
1320 * @iscsit: iscsi transport template
1321 * @initial_cmdsn: initial CmdSN
1322 * @hostno: host no allocated
1324 * This can be used by software iscsi_transports that allocate
1325 * a session per scsi host.
1327 struct iscsi_cls_session *
1328 iscsi_session_setup(struct iscsi_transport *iscsit,
1329 struct scsi_transport_template *scsit,
1330 int cmd_task_size, int mgmt_task_size,
1331 uint32_t initial_cmdsn, uint32_t *hostno)
1333 struct Scsi_Host *shost;
1334 struct iscsi_session *session;
1335 struct iscsi_cls_session *cls_session;
1338 shost = scsi_host_alloc(iscsit->host_template,
1339 hostdata_privsize(sizeof(*session)));
1344 shost->max_channel = 0;
1345 shost->max_lun = iscsit->max_lun;
1346 shost->max_cmd_len = iscsit->max_cmd_len;
1347 shost->transportt = scsit;
1348 shost->transportt->create_work_queue = 1;
1349 *hostno = shost->host_no;
1351 session = iscsi_hostdata(shost->hostdata);
1352 memset(session, 0, sizeof(struct iscsi_session));
1353 session->host = shost;
1354 session->state = ISCSI_STATE_FREE;
1355 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1356 session->cmds_max = ISCSI_XMIT_CMDS_MAX;
1357 session->cmdsn = initial_cmdsn;
1358 session->exp_cmdsn = initial_cmdsn + 1;
1359 session->max_cmdsn = initial_cmdsn + 1;
1360 session->max_r2t = 1;
1361 session->tt = iscsit;
1363 /* initialize SCSI PDU commands pool */
1364 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1365 (void***)&session->cmds,
1366 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1367 goto cmdpool_alloc_fail;
1369 /* pre-format cmds pool with ITT */
1370 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1371 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1374 ctask->dd_data = &ctask[1];
1376 INIT_LIST_HEAD(&ctask->running);
1379 spin_lock_init(&session->lock);
1380 INIT_LIST_HEAD(&session->connections);
1382 /* initialize immediate command pool */
1383 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1384 (void***)&session->mgmt_cmds,
1385 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1386 goto mgmtpool_alloc_fail;
1389 /* pre-format immediate cmds pool with ITT */
1390 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1391 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1394 mtask->dd_data = &mtask[1];
1395 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
1396 INIT_LIST_HEAD(&mtask->running);
1399 if (scsi_add_host(shost, NULL))
1402 if (!try_module_get(iscsit->owner))
1403 goto cls_session_fail;
1405 cls_session = iscsi_create_session(shost, iscsit, 0);
1408 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1413 module_put(iscsit->owner);
1415 scsi_remove_host(shost);
1417 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1418 mgmtpool_alloc_fail:
1419 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1421 scsi_host_put(shost);
1424 EXPORT_SYMBOL_GPL(iscsi_session_setup);
1427 * iscsi_session_teardown - destroy session, host, and cls_session
1430 * This can be used by software iscsi_transports that allocate
1431 * a session per scsi host.
1433 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1435 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1436 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1437 struct module *owner = cls_session->transport->owner;
1439 scsi_remove_host(shost);
1441 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1442 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1444 kfree(session->targetname);
1446 iscsi_destroy_session(cls_session);
1447 scsi_host_put(shost);
1450 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1453 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1454 * @cls_session: iscsi_cls_session
1457 struct iscsi_cls_conn *
1458 iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1460 struct iscsi_session *session = class_to_transport_session(cls_session);
1461 struct iscsi_conn *conn;
1462 struct iscsi_cls_conn *cls_conn;
1465 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1468 conn = cls_conn->dd_data;
1469 memset(conn, 0, sizeof(*conn));
1471 conn->session = session;
1472 conn->cls_conn = cls_conn;
1473 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1474 conn->id = conn_idx;
1475 conn->exp_statsn = 0;
1476 conn->tmabort_state = TMABORT_INITIAL;
1477 INIT_LIST_HEAD(&conn->run_list);
1478 INIT_LIST_HEAD(&conn->mgmt_run_list);
1479 INIT_LIST_HEAD(&conn->xmitqueue);
1481 /* initialize general immediate & non-immediate PDU commands queue */
1482 conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1484 if (conn->immqueue == ERR_PTR(-ENOMEM))
1485 goto immqueue_alloc_fail;
1487 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1489 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1490 goto mgmtqueue_alloc_fail;
1492 INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
1494 /* allocate login_mtask used for the login/text sequences */
1495 spin_lock_bh(&session->lock);
1496 if (!__kfifo_get(session->mgmtpool.queue,
1497 (void*)&conn->login_mtask,
1499 spin_unlock_bh(&session->lock);
1500 goto login_mtask_alloc_fail;
1502 spin_unlock_bh(&session->lock);
1504 data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
1506 goto login_mtask_data_alloc_fail;
1507 conn->login_mtask->data = conn->data = data;
1509 init_timer(&conn->tmabort_timer);
1510 mutex_init(&conn->xmitmutex);
1511 init_waitqueue_head(&conn->ehwait);
1515 login_mtask_data_alloc_fail:
1516 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1518 login_mtask_alloc_fail:
1519 kfifo_free(conn->mgmtqueue);
1520 mgmtqueue_alloc_fail:
1521 kfifo_free(conn->immqueue);
1522 immqueue_alloc_fail:
1523 iscsi_destroy_conn(cls_conn);
1526 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1529 * iscsi_conn_teardown - teardown iscsi connection
1530 * cls_conn: iscsi class connection
1532 * TODO: we may need to make this into a two step process
1533 * like scsi-mls remove + put host
1535 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1537 struct iscsi_conn *conn = cls_conn->dd_data;
1538 struct iscsi_session *session = conn->session;
1539 unsigned long flags;
1541 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1542 mutex_lock(&conn->xmitmutex);
1544 spin_lock_bh(&session->lock);
1545 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1546 if (session->leadconn == conn) {
1548 * leading connection? then give up on recovery.
1550 session->state = ISCSI_STATE_TERMINATE;
1551 wake_up(&conn->ehwait);
1553 spin_unlock_bh(&session->lock);
1555 mutex_unlock(&conn->xmitmutex);
1558 * Block until all in-progress commands for this connection
1562 spin_lock_irqsave(session->host->host_lock, flags);
1563 if (!session->host->host_busy) { /* OK for ERL == 0 */
1564 spin_unlock_irqrestore(session->host->host_lock, flags);
1567 spin_unlock_irqrestore(session->host->host_lock, flags);
1568 msleep_interruptible(500);
1569 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1570 "host_failed %d\n", session->host->host_busy,
1571 session->host->host_failed);
1573 * force eh_abort() to unblock
1575 wake_up(&conn->ehwait);
1578 spin_lock_bh(&session->lock);
1580 kfree(conn->persistent_address);
1581 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1583 list_del(&conn->item);
1584 if (list_empty(&session->connections))
1585 session->leadconn = NULL;
1586 if (session->leadconn && session->leadconn == conn)
1587 session->leadconn = container_of(session->connections.next,
1588 struct iscsi_conn, item);
1590 if (session->leadconn == NULL)
1591 /* no connections exits.. reset sequencing */
1592 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
1593 spin_unlock_bh(&session->lock);
1595 kfifo_free(conn->immqueue);
1596 kfifo_free(conn->mgmtqueue);
1598 iscsi_destroy_conn(cls_conn);
1600 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1602 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1604 struct iscsi_conn *conn = cls_conn->dd_data;
1605 struct iscsi_session *session = conn->session;
1608 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1612 if ((session->imm_data_en || !session->initial_r2t_en) &&
1613 session->first_burst > session->max_burst) {
1614 printk("iscsi: invalid burst lengths: "
1615 "first_burst %d max_burst %d\n",
1616 session->first_burst, session->max_burst);
1620 spin_lock_bh(&session->lock);
1621 conn->c_stage = ISCSI_CONN_STARTED;
1622 session->state = ISCSI_STATE_LOGGED_IN;
1624 switch(conn->stop_stage) {
1625 case STOP_CONN_RECOVER:
1627 * unblock eh_abort() if it is blocked. re-try all
1628 * commands after successful recovery
1630 conn->stop_stage = 0;
1631 conn->tmabort_state = TMABORT_INITIAL;
1633 spin_unlock_bh(&session->lock);
1635 iscsi_unblock_session(session_to_cls(session));
1636 wake_up(&conn->ehwait);
1638 case STOP_CONN_TERM:
1639 conn->stop_stage = 0;
1644 spin_unlock_bh(&session->lock);
1648 EXPORT_SYMBOL_GPL(iscsi_conn_start);
1651 flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1653 struct iscsi_mgmt_task *mtask, *tmp;
1655 /* handle pending */
1656 while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
1657 __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
1658 if (mtask == conn->login_mtask)
1660 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1661 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1665 /* handle running */
1666 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1667 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
1668 list_del(&mtask->running);
1670 if (mtask == conn->login_mtask)
1672 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1679 /* Fail commands. Mutex and session lock held and recv side suspended */
1680 static void fail_all_commands(struct iscsi_conn *conn)
1682 struct iscsi_cmd_task *ctask, *tmp;
1685 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1686 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1688 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1691 /* fail all other running */
1692 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1693 debug_scsi("failing in progress sc %p itt 0x%x\n",
1694 ctask->sc, ctask->itt);
1695 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1701 static void iscsi_start_session_recovery(struct iscsi_session *session,
1702 struct iscsi_conn *conn, int flag)
1706 spin_lock_bh(&session->lock);
1707 if (conn->stop_stage == STOP_CONN_TERM) {
1708 spin_unlock_bh(&session->lock);
1713 * When this is called for the in_login state, we only want to clean
1714 * up the login task and connection. We do not need to block and set
1715 * the recovery state again
1717 if (flag == STOP_CONN_TERM)
1718 session->state = ISCSI_STATE_TERMINATE;
1719 else if (conn->stop_stage != STOP_CONN_RECOVER)
1720 session->state = ISCSI_STATE_IN_RECOVERY;
1722 old_stop_stage = conn->stop_stage;
1723 conn->stop_stage = flag;
1724 conn->c_stage = ISCSI_CONN_STOPPED;
1725 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1726 spin_unlock_bh(&session->lock);
1728 write_lock_bh(conn->recv_lock);
1729 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1730 write_unlock_bh(conn->recv_lock);
1732 mutex_lock(&conn->xmitmutex);
1734 * for connection level recovery we should not calculate
1735 * header digest. conn->hdr_size used for optimization
1736 * in hdr_extract() and will be re-negotiated at
1739 if (flag == STOP_CONN_RECOVER) {
1740 conn->hdrdgst_en = 0;
1741 conn->datadgst_en = 0;
1742 if (session->state == ISCSI_STATE_IN_RECOVERY &&
1743 old_stop_stage != STOP_CONN_RECOVER) {
1744 debug_scsi("blocking session\n");
1745 iscsi_block_session(session_to_cls(session));
1752 spin_lock_bh(&session->lock);
1753 fail_all_commands(conn);
1754 flush_control_queues(session, conn);
1755 spin_unlock_bh(&session->lock);
1757 mutex_unlock(&conn->xmitmutex);
1760 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1762 struct iscsi_conn *conn = cls_conn->dd_data;
1763 struct iscsi_session *session = conn->session;
1766 case STOP_CONN_RECOVER:
1767 case STOP_CONN_TERM:
1768 iscsi_start_session_recovery(session, conn, flag);
1771 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
1774 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1776 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1777 struct iscsi_cls_conn *cls_conn, int is_leading)
1779 struct iscsi_session *session = class_to_transport_session(cls_session);
1780 struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data;
1782 /* lookup for existing connection */
1783 spin_lock_bh(&session->lock);
1784 list_for_each_entry(tmp, &session->connections, item) {
1786 if (conn->c_stage != ISCSI_CONN_STOPPED ||
1787 conn->stop_stage == STOP_CONN_TERM) {
1788 printk(KERN_ERR "iscsi: can't bind "
1789 "non-stopped connection (%d:%d)\n",
1790 conn->c_stage, conn->stop_stage);
1791 spin_unlock_bh(&session->lock);
1798 /* bind new iSCSI connection to session */
1799 conn->session = session;
1800 list_add(&conn->item, &session->connections);
1802 spin_unlock_bh(&session->lock);
1805 session->leadconn = conn;
1808 * Unblock xmitworker(), Login Phase will pass through.
1810 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1811 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1814 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1817 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1818 enum iscsi_param param, char *buf, int buflen)
1820 struct iscsi_conn *conn = cls_conn->dd_data;
1821 struct iscsi_session *session = conn->session;
1825 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1826 sscanf(buf, "%d", &conn->max_recv_dlength);
1828 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1829 sscanf(buf, "%d", &conn->max_xmit_dlength);
1831 case ISCSI_PARAM_HDRDGST_EN:
1832 sscanf(buf, "%d", &conn->hdrdgst_en);
1834 case ISCSI_PARAM_DATADGST_EN:
1835 sscanf(buf, "%d", &conn->datadgst_en);
1837 case ISCSI_PARAM_INITIAL_R2T_EN:
1838 sscanf(buf, "%d", &session->initial_r2t_en);
1840 case ISCSI_PARAM_MAX_R2T:
1841 sscanf(buf, "%d", &session->max_r2t);
1843 case ISCSI_PARAM_IMM_DATA_EN:
1844 sscanf(buf, "%d", &session->imm_data_en);
1846 case ISCSI_PARAM_FIRST_BURST:
1847 sscanf(buf, "%d", &session->first_burst);
1849 case ISCSI_PARAM_MAX_BURST:
1850 sscanf(buf, "%d", &session->max_burst);
1852 case ISCSI_PARAM_PDU_INORDER_EN:
1853 sscanf(buf, "%d", &session->pdu_inorder_en);
1855 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1856 sscanf(buf, "%d", &session->dataseq_inorder_en);
1858 case ISCSI_PARAM_ERL:
1859 sscanf(buf, "%d", &session->erl);
1861 case ISCSI_PARAM_IFMARKER_EN:
1862 sscanf(buf, "%d", &value);
1865 case ISCSI_PARAM_OFMARKER_EN:
1866 sscanf(buf, "%d", &value);
1869 case ISCSI_PARAM_EXP_STATSN:
1870 sscanf(buf, "%u", &conn->exp_statsn);
1872 case ISCSI_PARAM_TARGET_NAME:
1873 /* this should not change between logins */
1874 if (session->targetname)
1877 session->targetname = kstrdup(buf, GFP_KERNEL);
1878 if (!session->targetname)
1881 case ISCSI_PARAM_TPGT:
1882 sscanf(buf, "%d", &session->tpgt);
1884 case ISCSI_PARAM_PERSISTENT_PORT:
1885 sscanf(buf, "%d", &conn->persistent_port);
1887 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1889 * this is the address returned in discovery so it should
1890 * not change between logins.
1892 if (conn->persistent_address)
1895 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1896 if (!conn->persistent_address)
1905 EXPORT_SYMBOL_GPL(iscsi_set_param);
1907 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1908 enum iscsi_param param, char *buf)
1910 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1911 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1915 case ISCSI_PARAM_INITIAL_R2T_EN:
1916 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1918 case ISCSI_PARAM_MAX_R2T:
1919 len = sprintf(buf, "%hu\n", session->max_r2t);
1921 case ISCSI_PARAM_IMM_DATA_EN:
1922 len = sprintf(buf, "%d\n", session->imm_data_en);
1924 case ISCSI_PARAM_FIRST_BURST:
1925 len = sprintf(buf, "%u\n", session->first_burst);
1927 case ISCSI_PARAM_MAX_BURST:
1928 len = sprintf(buf, "%u\n", session->max_burst);
1930 case ISCSI_PARAM_PDU_INORDER_EN:
1931 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
1933 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1934 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
1936 case ISCSI_PARAM_ERL:
1937 len = sprintf(buf, "%d\n", session->erl);
1939 case ISCSI_PARAM_TARGET_NAME:
1940 len = sprintf(buf, "%s\n", session->targetname);
1942 case ISCSI_PARAM_TPGT:
1943 len = sprintf(buf, "%d\n", session->tpgt);
1951 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
1953 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
1954 enum iscsi_param param, char *buf)
1956 struct iscsi_conn *conn = cls_conn->dd_data;
1960 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1961 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
1963 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1964 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
1966 case ISCSI_PARAM_HDRDGST_EN:
1967 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
1969 case ISCSI_PARAM_DATADGST_EN:
1970 len = sprintf(buf, "%d\n", conn->datadgst_en);
1972 case ISCSI_PARAM_IFMARKER_EN:
1973 len = sprintf(buf, "%d\n", conn->ifmarker_en);
1975 case ISCSI_PARAM_OFMARKER_EN:
1976 len = sprintf(buf, "%d\n", conn->ofmarker_en);
1978 case ISCSI_PARAM_EXP_STATSN:
1979 len = sprintf(buf, "%u\n", conn->exp_statsn);
1981 case ISCSI_PARAM_PERSISTENT_PORT:
1982 len = sprintf(buf, "%d\n", conn->persistent_port);
1984 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1985 len = sprintf(buf, "%s\n", conn->persistent_address);
1993 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
1995 MODULE_AUTHOR("Mike Christie");
1996 MODULE_DESCRIPTION("iSCSI library functions");
1997 MODULE_LICENSE("GPL");