4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/types.h>
25 #include <linux/mutex.h>
26 #include <linux/kfifo.h>
27 #include <linux/delay.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi.h>
35 #include <scsi/iscsi_proto.h>
36 #include <scsi/scsi_transport.h>
37 #include <scsi/scsi_transport_iscsi.h>
38 #include <scsi/libiscsi.h>
40 struct iscsi_session
*
41 class_to_transport_session(struct iscsi_cls_session
*cls_session
)
43 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
44 return iscsi_hostdata(shost
->hostdata
);
46 EXPORT_SYMBOL_GPL(class_to_transport_session
);
48 #define INVALID_SN_DELTA 0xffff
51 iscsi_check_assign_cmdsn(struct iscsi_session
*session
, struct iscsi_nopin
*hdr
)
53 uint32_t max_cmdsn
= be32_to_cpu(hdr
->max_cmdsn
);
54 uint32_t exp_cmdsn
= be32_to_cpu(hdr
->exp_cmdsn
);
56 if (max_cmdsn
< exp_cmdsn
-1 &&
57 max_cmdsn
> exp_cmdsn
- INVALID_SN_DELTA
)
58 return ISCSI_ERR_MAX_CMDSN
;
59 if (max_cmdsn
> session
->max_cmdsn
||
60 max_cmdsn
< session
->max_cmdsn
- INVALID_SN_DELTA
)
61 session
->max_cmdsn
= max_cmdsn
;
62 if (exp_cmdsn
> session
->exp_cmdsn
||
63 exp_cmdsn
< session
->exp_cmdsn
- INVALID_SN_DELTA
)
64 session
->exp_cmdsn
= exp_cmdsn
;
68 EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn
);
70 void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task
*ctask
,
71 struct iscsi_data
*hdr
,
72 int transport_data_cnt
)
74 struct iscsi_conn
*conn
= ctask
->conn
;
76 memset(hdr
, 0, sizeof(struct iscsi_data
));
77 hdr
->ttt
= cpu_to_be32(ISCSI_RESERVED_TAG
);
78 hdr
->datasn
= cpu_to_be32(ctask
->unsol_datasn
);
79 ctask
->unsol_datasn
++;
80 hdr
->opcode
= ISCSI_OP_SCSI_DATA_OUT
;
81 memcpy(hdr
->lun
, ctask
->hdr
->lun
, sizeof(hdr
->lun
));
83 hdr
->itt
= ctask
->hdr
->itt
;
84 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
86 hdr
->offset
= cpu_to_be32(ctask
->total_length
-
90 if (ctask
->unsol_count
> conn
->max_xmit_dlength
) {
91 hton24(hdr
->dlength
, conn
->max_xmit_dlength
);
92 ctask
->data_count
= conn
->max_xmit_dlength
;
95 hton24(hdr
->dlength
, ctask
->unsol_count
);
96 ctask
->data_count
= ctask
->unsol_count
;
97 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
100 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu
);
103 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
104 * @ctask: iscsi cmd task
106 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
107 * fields like dlength or final based on how much data it sends
109 static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task
*ctask
)
111 struct iscsi_conn
*conn
= ctask
->conn
;
112 struct iscsi_session
*session
= conn
->session
;
113 struct iscsi_cmd
*hdr
= ctask
->hdr
;
114 struct scsi_cmnd
*sc
= ctask
->sc
;
116 hdr
->opcode
= ISCSI_OP_SCSI_CMD
;
117 hdr
->flags
= ISCSI_ATTR_SIMPLE
;
118 int_to_scsilun(sc
->device
->lun
, (struct scsi_lun
*)hdr
->lun
);
119 hdr
->itt
= ctask
->itt
| (conn
->id
<< ISCSI_CID_SHIFT
) |
120 (session
->age
<< ISCSI_AGE_SHIFT
);
121 hdr
->data_length
= cpu_to_be32(sc
->request_bufflen
);
122 hdr
->cmdsn
= cpu_to_be32(session
->cmdsn
);
124 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
125 memcpy(hdr
->cdb
, sc
->cmnd
, sc
->cmd_len
);
126 memset(&hdr
->cdb
[sc
->cmd_len
], 0, MAX_COMMAND_SIZE
- sc
->cmd_len
);
128 if (sc
->sc_data_direction
== DMA_TO_DEVICE
) {
129 hdr
->flags
|= ISCSI_FLAG_CMD_WRITE
;
133 * imm_count bytes to be sent right after
136 * unsol_count bytes(as Data-Out) to be sent
137 * without R2T ack right after
140 * r2t_data_count bytes to be sent via R2T ack's
142 * pad_count bytes to be sent as zero-padding
144 ctask
->imm_count
= 0;
145 ctask
->unsol_count
= 0;
146 ctask
->unsol_datasn
= 0;
148 if (session
->imm_data_en
) {
149 if (ctask
->total_length
>= session
->first_burst
)
150 ctask
->imm_count
= min(session
->first_burst
,
151 conn
->max_xmit_dlength
);
153 ctask
->imm_count
= min(ctask
->total_length
,
154 conn
->max_xmit_dlength
);
155 hton24(ctask
->hdr
->dlength
, ctask
->imm_count
);
157 zero_data(ctask
->hdr
->dlength
);
159 if (!session
->initial_r2t_en
)
160 ctask
->unsol_count
= min(session
->first_burst
,
161 ctask
->total_length
) - ctask
->imm_count
;
162 if (!ctask
->unsol_count
)
163 /* No unsolicit Data-Out's */
164 ctask
->hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
167 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
168 zero_data(hdr
->dlength
);
170 if (sc
->sc_data_direction
== DMA_FROM_DEVICE
)
171 hdr
->flags
|= ISCSI_FLAG_CMD_READ
;
174 conn
->scsicmd_pdus_cnt
++;
176 EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu
);
179 * iscsi_complete_command - return command back to scsi-ml
180 * @session: iscsi session
181 * @ctask: iscsi cmd task
183 * Must be called with session lock.
184 * This function returns the scsi command to scsi-ml and returns
185 * the cmd task to the pool of available cmd tasks.
187 static void iscsi_complete_command(struct iscsi_session
*session
,
188 struct iscsi_cmd_task
*ctask
)
190 struct scsi_cmnd
*sc
= ctask
->sc
;
192 ctask
->state
= ISCSI_TASK_COMPLETED
;
194 list_del_init(&ctask
->running
);
195 __kfifo_put(session
->cmdpool
.queue
, (void*)&ctask
, sizeof(void*));
200 * iscsi_cmd_rsp - SCSI Command Response processing
201 * @conn: iscsi connection
203 * @ctask: scsi command task
204 * @data: cmd data buffer
205 * @datalen: len of buffer
207 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
208 * then completes the command and task.
210 static int iscsi_scsi_cmd_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
211 struct iscsi_cmd_task
*ctask
, char *data
,
215 struct iscsi_cmd_rsp
*rhdr
= (struct iscsi_cmd_rsp
*)hdr
;
216 struct iscsi_session
*session
= conn
->session
;
217 struct scsi_cmnd
*sc
= ctask
->sc
;
219 rc
= iscsi_check_assign_cmdsn(session
, (struct iscsi_nopin
*)rhdr
);
221 sc
->result
= DID_ERROR
<< 16;
225 conn
->exp_statsn
= be32_to_cpu(rhdr
->statsn
) + 1;
227 sc
->result
= (DID_OK
<< 16) | rhdr
->cmd_status
;
229 if (rhdr
->response
!= ISCSI_STATUS_CMD_COMPLETED
) {
230 sc
->result
= DID_ERROR
<< 16;
234 if (rhdr
->cmd_status
== SAM_STAT_CHECK_CONDITION
) {
239 printk(KERN_ERR
"iscsi: Got CHECK_CONDITION but "
240 "invalid data buffer size of %d\n", datalen
);
241 sc
->result
= DID_BAD_TARGET
<< 16;
245 senselen
= (data
[0] << 8) | data
[1];
246 if (datalen
< senselen
)
247 goto invalid_datalen
;
249 memcpy(sc
->sense_buffer
, data
+ 2,
250 min(senselen
, SCSI_SENSE_BUFFERSIZE
));
251 debug_scsi("copied %d bytes of sense\n",
252 min(senselen
, SCSI_SENSE_BUFFERSIZE
));
255 if (sc
->sc_data_direction
== DMA_TO_DEVICE
)
258 if (rhdr
->flags
& ISCSI_FLAG_CMD_UNDERFLOW
) {
259 int res_count
= be32_to_cpu(rhdr
->residual_count
);
261 if (res_count
> 0 && res_count
<= sc
->request_bufflen
)
262 sc
->resid
= res_count
;
264 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
265 } else if (rhdr
->flags
& ISCSI_FLAG_CMD_BIDI_UNDERFLOW
)
266 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
267 else if (rhdr
->flags
& ISCSI_FLAG_CMD_OVERFLOW
)
268 sc
->resid
= be32_to_cpu(rhdr
->residual_count
);
271 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
272 (long)sc
, sc
->result
, ctask
->itt
);
273 conn
->scsirsp_pdus_cnt
++;
275 iscsi_complete_command(conn
->session
, ctask
);
279 static void iscsi_tmf_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
)
281 struct iscsi_tm_rsp
*tmf
= (struct iscsi_tm_rsp
*)hdr
;
283 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
284 conn
->tmfrsp_pdus_cnt
++;
286 if (conn
->tmabort_state
!= TMABORT_INITIAL
)
289 if (tmf
->response
== ISCSI_TMF_RSP_COMPLETE
)
290 conn
->tmabort_state
= TMABORT_SUCCESS
;
291 else if (tmf
->response
== ISCSI_TMF_RSP_NO_TASK
)
292 conn
->tmabort_state
= TMABORT_NOT_FOUND
;
294 conn
->tmabort_state
= TMABORT_FAILED
;
295 wake_up(&conn
->ehwait
);
299 * __iscsi_complete_pdu - complete pdu
303 * @datalen: len of data buffer
305 * Completes pdu processing by freeing any resources allocated at
306 * queuecommand or send generic. session lock must be held and verify
307 * itt must have been called.
309 int __iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
310 char *data
, int datalen
)
312 struct iscsi_session
*session
= conn
->session
;
313 int opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
, rc
= 0;
314 struct iscsi_cmd_task
*ctask
;
315 struct iscsi_mgmt_task
*mtask
;
318 if (hdr
->itt
!= cpu_to_be32(ISCSI_RESERVED_TAG
))
319 itt
= hdr
->itt
& ISCSI_ITT_MASK
;
323 if (itt
< session
->cmds_max
) {
324 ctask
= session
->cmds
[itt
];
326 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
327 opcode
, conn
->id
, ctask
->itt
, datalen
);
330 case ISCSI_OP_SCSI_CMD_RSP
:
331 BUG_ON((void*)ctask
!= ctask
->sc
->SCp
.ptr
);
332 rc
= iscsi_scsi_cmd_rsp(conn
, hdr
, ctask
, data
,
335 case ISCSI_OP_SCSI_DATA_IN
:
336 BUG_ON((void*)ctask
!= ctask
->sc
->SCp
.ptr
);
337 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
338 conn
->scsirsp_pdus_cnt
++;
339 iscsi_complete_command(session
, ctask
);
343 /* LLD handles this for now */
346 rc
= ISCSI_ERR_BAD_OPCODE
;
349 } else if (itt
>= ISCSI_MGMT_ITT_OFFSET
&&
350 itt
< ISCSI_MGMT_ITT_OFFSET
+ session
->mgmtpool_max
) {
351 mtask
= session
->mgmt_cmds
[itt
- ISCSI_MGMT_ITT_OFFSET
];
353 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
354 opcode
, conn
->id
, mtask
->itt
, datalen
);
356 rc
= iscsi_check_assign_cmdsn(session
,
357 (struct iscsi_nopin
*)hdr
);
362 case ISCSI_OP_LOGOUT_RSP
:
363 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
365 case ISCSI_OP_LOGIN_RSP
:
366 case ISCSI_OP_TEXT_RSP
:
368 * login related PDU's exp_statsn is handled in
371 rc
= iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
);
372 list_del(&mtask
->running
);
373 if (conn
->login_mtask
!= mtask
)
374 __kfifo_put(session
->mgmtpool
.queue
,
375 (void*)&mtask
, sizeof(void*));
377 case ISCSI_OP_SCSI_TMFUNC_RSP
:
379 rc
= ISCSI_ERR_PROTO
;
383 iscsi_tmf_rsp(conn
, hdr
);
385 case ISCSI_OP_NOOP_IN
:
386 if (hdr
->ttt
!= ISCSI_RESERVED_TAG
) {
387 rc
= ISCSI_ERR_PROTO
;
390 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
392 rc
= iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
);
393 list_del(&mtask
->running
);
394 if (conn
->login_mtask
!= mtask
)
395 __kfifo_put(session
->mgmtpool
.queue
,
396 (void*)&mtask
, sizeof(void*));
399 rc
= ISCSI_ERR_BAD_OPCODE
;
402 } else if (itt
== ISCSI_RESERVED_TAG
) {
404 case ISCSI_OP_NOOP_IN
:
406 rc
= iscsi_check_assign_cmdsn(session
,
407 (struct iscsi_nopin
*)hdr
);
408 if (!rc
&& hdr
->ttt
!= ISCSI_RESERVED_TAG
)
409 rc
= iscsi_recv_pdu(conn
->cls_conn
,
412 rc
= ISCSI_ERR_PROTO
;
414 case ISCSI_OP_REJECT
:
415 /* we need sth like iscsi_reject_rsp()*/
416 case ISCSI_OP_ASYNC_EVENT
:
417 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
418 /* we need sth like iscsi_async_event_rsp() */
419 rc
= ISCSI_ERR_BAD_OPCODE
;
422 rc
= ISCSI_ERR_BAD_OPCODE
;
426 rc
= ISCSI_ERR_BAD_ITT
;
431 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu
);
433 int iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
434 char *data
, int datalen
)
438 spin_lock(&conn
->session
->lock
);
439 rc
= __iscsi_complete_pdu(conn
, hdr
, data
, datalen
);
440 spin_unlock(&conn
->session
->lock
);
443 EXPORT_SYMBOL_GPL(iscsi_complete_pdu
);
445 /* verify itt (itt encoding: age+cid+itt) */
446 int iscsi_verify_itt(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
449 struct iscsi_session
*session
= conn
->session
;
450 struct iscsi_cmd_task
*ctask
;
453 if (hdr
->itt
!= cpu_to_be32(ISCSI_RESERVED_TAG
)) {
454 if ((hdr
->itt
& ISCSI_AGE_MASK
) !=
455 (session
->age
<< ISCSI_AGE_SHIFT
)) {
456 printk(KERN_ERR
"iscsi: received itt %x expected "
457 "session age (%x)\n", hdr
->itt
,
458 session
->age
& ISCSI_AGE_MASK
);
459 return ISCSI_ERR_BAD_ITT
;
462 if ((hdr
->itt
& ISCSI_CID_MASK
) !=
463 (conn
->id
<< ISCSI_CID_SHIFT
)) {
464 printk(KERN_ERR
"iscsi: received itt %x, expected "
465 "CID (%x)\n", hdr
->itt
, conn
->id
);
466 return ISCSI_ERR_BAD_ITT
;
468 itt
= hdr
->itt
& ISCSI_ITT_MASK
;
472 if (itt
< session
->cmds_max
) {
473 ctask
= session
->cmds
[itt
];
476 printk(KERN_INFO
"iscsi: dropping ctask with "
477 "itt 0x%x\n", ctask
->itt
);
479 return ISCSI_ERR_NO_SCSI_CMD
;
482 if (ctask
->sc
->SCp
.phase
!= session
->age
) {
483 printk(KERN_ERR
"iscsi: ctask's session age %d, "
484 "expected %d\n", ctask
->sc
->SCp
.phase
,
486 return ISCSI_ERR_SESSION_FAILED
;
493 EXPORT_SYMBOL_GPL(iscsi_verify_itt
);
495 void iscsi_conn_failure(struct iscsi_conn
*conn
, enum iscsi_err err
)
497 struct iscsi_session
*session
= conn
->session
;
500 spin_lock_irqsave(&session
->lock
, flags
);
501 if (session
->state
== ISCSI_STATE_FAILED
) {
502 spin_unlock_irqrestore(&session
->lock
, flags
);
506 if (conn
->stop_stage
== 0)
507 session
->state
= ISCSI_STATE_FAILED
;
508 spin_unlock_irqrestore(&session
->lock
, flags
);
509 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
510 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
511 iscsi_conn_error(conn
->cls_conn
, err
);
513 EXPORT_SYMBOL_GPL(iscsi_conn_failure
);
516 * iscsi_data_xmit - xmit any command into the scheduled connection
517 * @conn: iscsi connection
520 * The function can return -EAGAIN in which case the caller must
521 * re-schedule it again later or recover. '0' return code means
524 static int iscsi_data_xmit(struct iscsi_conn
*conn
)
526 struct iscsi_transport
*tt
;
529 if (unlikely(conn
->suspend_tx
)) {
530 debug_scsi("conn %d Tx suspended!\n", conn
->id
);
533 tt
= conn
->session
->tt
;
536 * Transmit in the following order:
538 * 1) un-finished xmit (ctask or mtask)
539 * 2) immediate control PDUs
542 * 5) non-immediate control PDUs
544 * No need to lock around __kfifo_get as long as
545 * there's one producer and one consumer.
548 BUG_ON(conn
->ctask
&& conn
->mtask
);
551 rc
= tt
->xmit_cmd_task(conn
, conn
->ctask
);
554 /* done with this in-progress ctask */
558 rc
= tt
->xmit_mgmt_task(conn
, conn
->mtask
);
561 /* done with this in-progress mtask */
565 /* process immediate first */
566 if (unlikely(__kfifo_len(conn
->immqueue
))) {
567 while (__kfifo_get(conn
->immqueue
, (void*)&conn
->mtask
,
569 spin_lock_bh(&conn
->session
->lock
);
570 list_add_tail(&conn
->mtask
->running
,
571 &conn
->mgmt_run_list
);
572 spin_unlock_bh(&conn
->session
->lock
);
573 rc
= tt
->xmit_mgmt_task(conn
, conn
->mtask
);
577 /* done with this mtask */
581 /* process command queue */
582 spin_lock_bh(&conn
->session
->lock
);
583 while (!list_empty(&conn
->xmitqueue
)) {
585 * iscsi tcp may readd the task to the xmitqueue to send
588 conn
->ctask
= list_entry(conn
->xmitqueue
.next
,
589 struct iscsi_cmd_task
, running
);
590 conn
->ctask
->state
= ISCSI_TASK_RUNNING
;
591 list_move_tail(conn
->xmitqueue
.next
, &conn
->run_list
);
592 spin_unlock_bh(&conn
->session
->lock
);
594 rc
= tt
->xmit_cmd_task(conn
, conn
->ctask
);
597 spin_lock_bh(&conn
->session
->lock
);
599 spin_unlock_bh(&conn
->session
->lock
);
600 /* done with this ctask */
603 /* process the rest control plane PDUs, if any */
604 if (unlikely(__kfifo_len(conn
->mgmtqueue
))) {
605 while (__kfifo_get(conn
->mgmtqueue
, (void*)&conn
->mtask
,
607 spin_lock_bh(&conn
->session
->lock
);
608 list_add_tail(&conn
->mtask
->running
,
609 &conn
->mgmt_run_list
);
610 spin_unlock_bh(&conn
->session
->lock
);
611 rc
= tt
->xmit_mgmt_task(conn
, conn
->mtask
);
615 /* done with this mtask */
622 if (unlikely(conn
->suspend_tx
))
628 static void iscsi_xmitworker(void *data
)
630 struct iscsi_conn
*conn
= data
;
633 * serialize Xmit worker on a per-connection basis.
635 mutex_lock(&conn
->xmitmutex
);
637 rc
= iscsi_data_xmit(conn
);
638 } while (rc
>= 0 || rc
== -EAGAIN
);
639 mutex_unlock(&conn
->xmitmutex
);
643 FAILURE_BAD_HOST
= 1,
644 FAILURE_SESSION_FAILED
,
645 FAILURE_SESSION_FREED
,
646 FAILURE_WINDOW_CLOSED
,
647 FAILURE_SESSION_TERMINATE
,
648 FAILURE_SESSION_IN_RECOVERY
,
649 FAILURE_SESSION_RECOVERY_TIMEOUT
,
652 int iscsi_queuecommand(struct scsi_cmnd
*sc
, void (*done
)(struct scsi_cmnd
*))
654 struct Scsi_Host
*host
;
656 struct iscsi_session
*session
;
657 struct iscsi_conn
*conn
;
658 struct iscsi_cmd_task
*ctask
= NULL
;
660 sc
->scsi_done
= done
;
663 host
= sc
->device
->host
;
664 session
= iscsi_hostdata(host
->hostdata
);
666 spin_lock(&session
->lock
);
669 * ISCSI_STATE_FAILED is a temp. state. The recovery
670 * code will decide what is best to do with command queued
673 if (session
->state
!= ISCSI_STATE_LOGGED_IN
&&
674 session
->state
!= ISCSI_STATE_FAILED
) {
676 * to handle the race between when we set the recovery state
677 * and block the session we requeue here (commands could
678 * be entering our queuecommand while a block is starting
679 * up because the block code is not locked)
681 if (session
->state
== ISCSI_STATE_IN_RECOVERY
) {
682 reason
= FAILURE_SESSION_IN_RECOVERY
;
686 if (session
->state
== ISCSI_STATE_RECOVERY_FAILED
)
687 reason
= FAILURE_SESSION_RECOVERY_TIMEOUT
;
688 else if (session
->state
== ISCSI_STATE_TERMINATE
)
689 reason
= FAILURE_SESSION_TERMINATE
;
691 reason
= FAILURE_SESSION_FREED
;
696 * Check for iSCSI window and take care of CmdSN wrap-around
698 if ((int)(session
->max_cmdsn
- session
->cmdsn
) < 0) {
699 reason
= FAILURE_WINDOW_CLOSED
;
703 conn
= session
->leadconn
;
705 __kfifo_get(session
->cmdpool
.queue
, (void*)&ctask
, sizeof(void*));
706 sc
->SCp
.phase
= session
->age
;
707 sc
->SCp
.ptr
= (char *)ctask
;
709 ctask
->state
= ISCSI_TASK_PENDING
;
713 INIT_LIST_HEAD(&ctask
->running
);
714 ctask
->total_length
= sc
->request_bufflen
;
715 iscsi_prep_scsi_cmd_pdu(ctask
);
717 session
->tt
->init_cmd_task(ctask
);
719 list_add_tail(&ctask
->running
, &conn
->xmitqueue
);
721 "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
722 sc
->sc_data_direction
== DMA_TO_DEVICE
? "write" : "read",
723 conn
->id
, (long)sc
, ctask
->itt
, sc
->request_bufflen
,
724 session
->cmdsn
, session
->max_cmdsn
- session
->exp_cmdsn
+ 1);
725 spin_unlock(&session
->lock
);
727 scsi_queue_work(host
, &conn
->xmitwork
);
731 spin_unlock(&session
->lock
);
732 debug_scsi("cmd 0x%x rejected (%d)\n", sc
->cmnd
[0], reason
);
733 return SCSI_MLQUEUE_HOST_BUSY
;
736 spin_unlock(&session
->lock
);
737 printk(KERN_ERR
"iscsi: cmd 0x%x is not queued (%d)\n",
738 sc
->cmnd
[0], reason
);
739 sc
->result
= (DID_NO_CONNECT
<< 16);
740 sc
->resid
= sc
->request_bufflen
;
744 EXPORT_SYMBOL_GPL(iscsi_queuecommand
);
746 int iscsi_change_queue_depth(struct scsi_device
*sdev
, int depth
)
748 if (depth
> ISCSI_MAX_CMD_PER_LUN
)
749 depth
= ISCSI_MAX_CMD_PER_LUN
;
750 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), depth
);
751 return sdev
->queue_depth
;
753 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth
);
756 iscsi_conn_send_generic(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
757 char *data
, uint32_t data_size
)
759 struct iscsi_session
*session
= conn
->session
;
760 struct iscsi_nopout
*nop
= (struct iscsi_nopout
*)hdr
;
761 struct iscsi_mgmt_task
*mtask
;
763 spin_lock_bh(&session
->lock
);
764 if (session
->state
== ISCSI_STATE_TERMINATE
) {
765 spin_unlock_bh(&session
->lock
);
768 if (hdr
->opcode
== (ISCSI_OP_LOGIN
| ISCSI_OP_IMMEDIATE
) ||
769 hdr
->opcode
== (ISCSI_OP_TEXT
| ISCSI_OP_IMMEDIATE
))
771 * Login and Text are sent serially, in
772 * request-followed-by-response sequence.
773 * Same mtask can be used. Same ITT must be used.
774 * Note that login_mtask is preallocated at conn_create().
776 mtask
= conn
->login_mtask
;
778 BUG_ON(conn
->c_stage
== ISCSI_CONN_INITIAL_STAGE
);
779 BUG_ON(conn
->c_stage
== ISCSI_CONN_STOPPED
);
781 nop
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
782 if (!__kfifo_get(session
->mgmtpool
.queue
,
783 (void*)&mtask
, sizeof(void*))) {
784 spin_unlock_bh(&session
->lock
);
790 * pre-format CmdSN for outgoing PDU.
792 if (hdr
->itt
!= cpu_to_be32(ISCSI_RESERVED_TAG
)) {
793 hdr
->itt
= mtask
->itt
| (conn
->id
<< ISCSI_CID_SHIFT
) |
794 (session
->age
<< ISCSI_AGE_SHIFT
);
795 nop
->cmdsn
= cpu_to_be32(session
->cmdsn
);
796 if (conn
->c_stage
== ISCSI_CONN_STARTED
&&
797 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
))
800 /* do not advance CmdSN */
801 nop
->cmdsn
= cpu_to_be32(session
->cmdsn
);
804 memcpy(mtask
->data
, data
, data_size
);
805 mtask
->data_count
= data_size
;
807 mtask
->data_count
= 0;
809 INIT_LIST_HEAD(&mtask
->running
);
810 memcpy(mtask
->hdr
, hdr
, sizeof(struct iscsi_hdr
));
811 if (session
->tt
->init_mgmt_task
)
812 session
->tt
->init_mgmt_task(conn
, mtask
, data
, data_size
);
813 spin_unlock_bh(&session
->lock
);
815 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
816 hdr
->opcode
, hdr
->itt
, data_size
);
819 * since send_pdu() could be called at least from two contexts,
820 * we need to serialize __kfifo_put, so we don't have to take
821 * additional lock on fast data-path
823 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
)
824 __kfifo_put(conn
->immqueue
, (void*)&mtask
, sizeof(void*));
826 __kfifo_put(conn
->mgmtqueue
, (void*)&mtask
, sizeof(void*));
828 scsi_queue_work(session
->host
, &conn
->xmitwork
);
832 int iscsi_conn_send_pdu(struct iscsi_cls_conn
*cls_conn
, struct iscsi_hdr
*hdr
,
833 char *data
, uint32_t data_size
)
835 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
838 mutex_lock(&conn
->xmitmutex
);
839 rc
= iscsi_conn_send_generic(conn
, hdr
, data
, data_size
);
840 mutex_unlock(&conn
->xmitmutex
);
844 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu
);
846 void iscsi_session_recovery_timedout(struct iscsi_cls_session
*cls_session
)
848 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
849 struct iscsi_conn
*conn
= session
->leadconn
;
851 spin_lock_bh(&session
->lock
);
852 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
853 session
->state
= ISCSI_STATE_RECOVERY_FAILED
;
855 wake_up(&conn
->ehwait
);
857 spin_unlock_bh(&session
->lock
);
859 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout
);
861 int iscsi_eh_host_reset(struct scsi_cmnd
*sc
)
863 struct Scsi_Host
*host
= sc
->device
->host
;
864 struct iscsi_session
*session
= iscsi_hostdata(host
->hostdata
);
865 struct iscsi_conn
*conn
= session
->leadconn
;
866 int fail_session
= 0;
868 spin_lock_bh(&session
->lock
);
869 if (session
->state
== ISCSI_STATE_TERMINATE
) {
871 debug_scsi("failing host reset: session terminated "
872 "[CID %d age %d]", conn
->id
, session
->age
);
873 spin_unlock_bh(&session
->lock
);
877 if (sc
->SCp
.phase
== session
->age
) {
878 debug_scsi("failing connection CID %d due to SCSI host reset",
882 spin_unlock_bh(&session
->lock
);
885 * we drop the lock here but the leadconn cannot be destoyed while
886 * we are in the scsi eh
889 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
891 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
892 wait_event_interruptible(conn
->ehwait
,
893 session
->state
== ISCSI_STATE_TERMINATE
||
894 session
->state
== ISCSI_STATE_LOGGED_IN
||
895 session
->state
== ISCSI_STATE_RECOVERY_FAILED
);
896 if (signal_pending(current
))
897 flush_signals(current
);
899 spin_lock_bh(&session
->lock
);
900 if (session
->state
== ISCSI_STATE_LOGGED_IN
)
901 printk(KERN_INFO
"iscsi: host reset succeeded\n");
904 spin_unlock_bh(&session
->lock
);
908 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset
);
910 static void iscsi_tmabort_timedout(unsigned long data
)
912 struct iscsi_cmd_task
*ctask
= (struct iscsi_cmd_task
*)data
;
913 struct iscsi_conn
*conn
= ctask
->conn
;
914 struct iscsi_session
*session
= conn
->session
;
916 spin_lock(&session
->lock
);
917 if (conn
->tmabort_state
== TMABORT_INITIAL
) {
918 conn
->tmabort_state
= TMABORT_TIMEDOUT
;
919 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
920 ctask
->sc
, ctask
->itt
);
921 /* unblock eh_abort() */
922 wake_up(&conn
->ehwait
);
924 spin_unlock(&session
->lock
);
927 /* must be called with the mutex lock */
928 static int iscsi_exec_abort_task(struct scsi_cmnd
*sc
,
929 struct iscsi_cmd_task
*ctask
)
931 struct iscsi_conn
*conn
= ctask
->conn
;
932 struct iscsi_session
*session
= conn
->session
;
933 struct iscsi_tm
*hdr
= &conn
->tmhdr
;
937 * ctask timed out but session is OK requests must be serialized.
939 memset(hdr
, 0, sizeof(struct iscsi_tm
));
940 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
941 hdr
->flags
= ISCSI_TM_FUNC_ABORT_TASK
;
942 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
943 memcpy(hdr
->lun
, ctask
->hdr
->lun
, sizeof(hdr
->lun
));
944 hdr
->rtt
= ctask
->hdr
->itt
;
945 hdr
->refcmdsn
= ctask
->hdr
->cmdsn
;
947 rc
= iscsi_conn_send_generic(conn
, (struct iscsi_hdr
*)hdr
,
950 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
951 debug_scsi("abort sent failure [itt 0x%x] %d", ctask
->itt
, rc
);
955 debug_scsi("abort sent [itt 0x%x]\n", ctask
->itt
);
957 spin_lock_bh(&session
->lock
);
958 ctask
->mtask
= (struct iscsi_mgmt_task
*)
959 session
->mgmt_cmds
[(hdr
->itt
& ISCSI_ITT_MASK
) -
960 ISCSI_MGMT_ITT_OFFSET
];
962 if (conn
->tmabort_state
== TMABORT_INITIAL
) {
963 conn
->tmfcmd_pdus_cnt
++;
964 conn
->tmabort_timer
.expires
= 10*HZ
+ jiffies
;
965 conn
->tmabort_timer
.function
= iscsi_tmabort_timedout
;
966 conn
->tmabort_timer
.data
= (unsigned long)ctask
;
967 add_timer(&conn
->tmabort_timer
);
968 debug_scsi("abort set timeout [itt 0x%x]", ctask
->itt
);
970 spin_unlock_bh(&session
->lock
);
971 mutex_unlock(&conn
->xmitmutex
);
974 * block eh thread until:
978 * 3) session is terminated or restarted or userspace has
979 * given up on recovery
981 wait_event_interruptible(conn
->ehwait
,
982 sc
->SCp
.phase
!= session
->age
||
983 session
->state
!= ISCSI_STATE_LOGGED_IN
||
984 conn
->tmabort_state
!= TMABORT_INITIAL
);
985 if (signal_pending(current
))
986 flush_signals(current
);
987 del_timer_sync(&conn
->tmabort_timer
);
989 mutex_lock(&conn
->xmitmutex
);
994 * xmit mutex and session lock must be held
996 static struct iscsi_mgmt_task
*
997 iscsi_remove_mgmt_task(struct kfifo
*fifo
, uint32_t itt
)
999 int i
, nr_tasks
= __kfifo_len(fifo
) / sizeof(void*);
1000 struct iscsi_mgmt_task
*task
;
1002 debug_scsi("searching %d tasks\n", nr_tasks
);
1004 for (i
= 0; i
< nr_tasks
; i
++) {
1005 __kfifo_get(fifo
, (void*)&task
, sizeof(void*));
1006 debug_scsi("check task %u\n", task
->itt
);
1008 if (task
->itt
== itt
) {
1009 debug_scsi("matched task\n");
1013 __kfifo_put(fifo
, (void*)&task
, sizeof(void*));
1018 static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task
*ctask
)
1020 struct iscsi_conn
*conn
= ctask
->conn
;
1021 struct iscsi_session
*session
= conn
->session
;
1026 if (!iscsi_remove_mgmt_task(conn
->immqueue
, ctask
->mtask
->itt
))
1027 list_del(&ctask
->mtask
->running
);
1028 __kfifo_put(session
->mgmtpool
.queue
, (void*)&ctask
->mtask
,
1030 ctask
->mtask
= NULL
;
1035 * session lock and xmitmutex must be held
1037 static void fail_command(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
,
1040 struct scsi_cmnd
*sc
;
1046 conn
->session
->tt
->cleanup_cmd_task(conn
, ctask
);
1047 iscsi_ctask_mtask_cleanup(ctask
);
1050 sc
->resid
= sc
->request_bufflen
;
1051 iscsi_complete_command(conn
->session
, ctask
);
1054 int iscsi_eh_abort(struct scsi_cmnd
*sc
)
1056 struct iscsi_cmd_task
*ctask
= (struct iscsi_cmd_task
*)sc
->SCp
.ptr
;
1057 struct iscsi_conn
*conn
= ctask
->conn
;
1058 struct iscsi_session
*session
= conn
->session
;
1061 conn
->eh_abort_cnt
++;
1062 debug_scsi("aborting [sc %p itt 0x%x]\n", sc
, ctask
->itt
);
1064 mutex_lock(&conn
->xmitmutex
);
1065 spin_lock_bh(&session
->lock
);
1068 * If we are not logged in or we have started a new session
1069 * then let the host reset code handle this
1071 if (session
->state
!= ISCSI_STATE_LOGGED_IN
||
1072 sc
->SCp
.phase
!= session
->age
)
1075 /* ctask completed before time out */
1077 spin_unlock_bh(&session
->lock
);
1078 debug_scsi("sc completed while abort in progress\n");
1079 goto success_rel_mutex
;
1082 /* what should we do here ? */
1083 if (conn
->ctask
== ctask
) {
1084 printk(KERN_INFO
"iscsi: sc %p itt 0x%x partially sent. "
1085 "Failing abort\n", sc
, ctask
->itt
);
1089 if (ctask
->state
== ISCSI_TASK_PENDING
)
1090 goto success_cleanup
;
1092 conn
->tmabort_state
= TMABORT_INITIAL
;
1094 spin_unlock_bh(&session
->lock
);
1095 rc
= iscsi_exec_abort_task(sc
, ctask
);
1096 spin_lock_bh(&session
->lock
);
1098 if (rc
|| sc
->SCp
.phase
!= session
->age
||
1099 session
->state
!= ISCSI_STATE_LOGGED_IN
)
1101 iscsi_ctask_mtask_cleanup(ctask
);
1103 switch (conn
->tmabort_state
) {
1104 case TMABORT_SUCCESS
:
1105 goto success_cleanup
;
1106 case TMABORT_NOT_FOUND
:
1108 /* ctask completed before tmf abort response */
1109 spin_unlock_bh(&session
->lock
);
1110 debug_scsi("sc completed while abort in progress\n");
1111 goto success_rel_mutex
;
1115 /* timedout or failed */
1116 spin_unlock_bh(&session
->lock
);
1117 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1118 spin_lock_bh(&session
->lock
);
1123 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc
, ctask
->itt
);
1124 spin_unlock_bh(&session
->lock
);
1127 * clean up task if aborted. we have the xmitmutex so grab
1128 * the recv lock as a writer
1130 write_lock_bh(conn
->recv_lock
);
1131 spin_lock(&session
->lock
);
1132 fail_command(conn
, ctask
, DID_ABORT
<< 16);
1133 spin_unlock(&session
->lock
);
1134 write_unlock_bh(conn
->recv_lock
);
1137 mutex_unlock(&conn
->xmitmutex
);
1141 spin_unlock_bh(&session
->lock
);
1142 mutex_unlock(&conn
->xmitmutex
);
1144 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc
, ctask
->itt
);
1147 EXPORT_SYMBOL_GPL(iscsi_eh_abort
);
1150 iscsi_pool_init(struct iscsi_queue
*q
, int max
, void ***items
, int item_size
)
1154 *items
= kmalloc(max
* sizeof(void*), GFP_KERNEL
);
1159 q
->pool
= kmalloc(max
* sizeof(void*), GFP_KERNEL
);
1160 if (q
->pool
== NULL
) {
1165 q
->queue
= kfifo_init((void*)q
->pool
, max
* sizeof(void*),
1167 if (q
->queue
== ERR_PTR(-ENOMEM
)) {
1173 for (i
= 0; i
< max
; i
++) {
1174 q
->pool
[i
] = kmalloc(item_size
, GFP_KERNEL
);
1175 if (q
->pool
[i
] == NULL
) {
1178 for (j
= 0; j
< i
; j
++)
1181 kfifo_free(q
->queue
);
1186 memset(q
->pool
[i
], 0, item_size
);
1187 (*items
)[i
] = q
->pool
[i
];
1188 __kfifo_put(q
->queue
, (void*)&q
->pool
[i
], sizeof(void*));
1192 EXPORT_SYMBOL_GPL(iscsi_pool_init
);
1194 void iscsi_pool_free(struct iscsi_queue
*q
, void **items
)
1198 for (i
= 0; i
< q
->max
; i
++)
1203 EXPORT_SYMBOL_GPL(iscsi_pool_free
);
1206 * iSCSI Session's hostdata organization:
1208 * *------------------* <== hostdata_session(host->hostdata)
1209 * | ptr to class sess|
1210 * |------------------| <== iscsi_hostdata(host->hostdata)
1212 * *------------------*
1215 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1216 _sz % sizeof(unsigned long))
1218 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1221 * iscsi_session_setup - create iscsi cls session and host and session
1222 * @scsit: scsi transport template
1223 * @iscsit: iscsi transport template
1224 * @initial_cmdsn: initial CmdSN
1225 * @hostno: host no allocated
1227 * This can be used by software iscsi_transports that allocate
1228 * a session per scsi host.
1230 struct iscsi_cls_session
*
1231 iscsi_session_setup(struct iscsi_transport
*iscsit
,
1232 struct scsi_transport_template
*scsit
,
1233 int cmd_task_size
, int mgmt_task_size
,
1234 uint32_t initial_cmdsn
, uint32_t *hostno
)
1236 struct Scsi_Host
*shost
;
1237 struct iscsi_session
*session
;
1238 struct iscsi_cls_session
*cls_session
;
1241 shost
= scsi_host_alloc(iscsit
->host_template
,
1242 hostdata_privsize(sizeof(*session
)));
1247 shost
->max_channel
= 0;
1248 shost
->max_lun
= iscsit
->max_lun
;
1249 shost
->max_cmd_len
= iscsit
->max_cmd_len
;
1250 shost
->transportt
= scsit
;
1251 shost
->transportt
->create_work_queue
= 1;
1252 *hostno
= shost
->host_no
;
1254 session
= iscsi_hostdata(shost
->hostdata
);
1255 memset(session
, 0, sizeof(struct iscsi_session
));
1256 session
->host
= shost
;
1257 session
->state
= ISCSI_STATE_FREE
;
1258 session
->mgmtpool_max
= ISCSI_MGMT_CMDS_MAX
;
1259 session
->cmds_max
= ISCSI_XMIT_CMDS_MAX
;
1260 session
->cmdsn
= initial_cmdsn
;
1261 session
->exp_cmdsn
= initial_cmdsn
+ 1;
1262 session
->max_cmdsn
= initial_cmdsn
+ 1;
1263 session
->max_r2t
= 1;
1264 session
->tt
= iscsit
;
1266 /* initialize SCSI PDU commands pool */
1267 if (iscsi_pool_init(&session
->cmdpool
, session
->cmds_max
,
1268 (void***)&session
->cmds
,
1269 cmd_task_size
+ sizeof(struct iscsi_cmd_task
)))
1270 goto cmdpool_alloc_fail
;
1272 /* pre-format cmds pool with ITT */
1273 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
1274 struct iscsi_cmd_task
*ctask
= session
->cmds
[cmd_i
];
1277 ctask
->dd_data
= &ctask
[1];
1279 INIT_LIST_HEAD(&ctask
->running
);
1282 spin_lock_init(&session
->lock
);
1283 INIT_LIST_HEAD(&session
->connections
);
1285 /* initialize immediate command pool */
1286 if (iscsi_pool_init(&session
->mgmtpool
, session
->mgmtpool_max
,
1287 (void***)&session
->mgmt_cmds
,
1288 mgmt_task_size
+ sizeof(struct iscsi_mgmt_task
)))
1289 goto mgmtpool_alloc_fail
;
1292 /* pre-format immediate cmds pool with ITT */
1293 for (cmd_i
= 0; cmd_i
< session
->mgmtpool_max
; cmd_i
++) {
1294 struct iscsi_mgmt_task
*mtask
= session
->mgmt_cmds
[cmd_i
];
1297 mtask
->dd_data
= &mtask
[1];
1298 mtask
->itt
= ISCSI_MGMT_ITT_OFFSET
+ cmd_i
;
1299 INIT_LIST_HEAD(&mtask
->running
);
1302 if (scsi_add_host(shost
, NULL
))
1305 if (!try_module_get(iscsit
->owner
))
1306 goto cls_session_fail
;
1308 cls_session
= iscsi_create_session(shost
, iscsit
, 0);
1311 *(unsigned long*)shost
->hostdata
= (unsigned long)cls_session
;
1316 module_put(iscsit
->owner
);
1318 scsi_remove_host(shost
);
1320 iscsi_pool_free(&session
->mgmtpool
, (void**)session
->mgmt_cmds
);
1321 mgmtpool_alloc_fail
:
1322 iscsi_pool_free(&session
->cmdpool
, (void**)session
->cmds
);
1324 scsi_host_put(shost
);
1327 EXPORT_SYMBOL_GPL(iscsi_session_setup
);
1330 * iscsi_session_teardown - destroy session, host, and cls_session
1333 * This can be used by software iscsi_transports that allocate
1334 * a session per scsi host.
1336 void iscsi_session_teardown(struct iscsi_cls_session
*cls_session
)
1338 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
1339 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
1341 scsi_remove_host(shost
);
1343 iscsi_pool_free(&session
->mgmtpool
, (void**)session
->mgmt_cmds
);
1344 iscsi_pool_free(&session
->cmdpool
, (void**)session
->cmds
);
1346 iscsi_destroy_session(cls_session
);
1347 scsi_host_put(shost
);
1348 module_put(cls_session
->transport
->owner
);
1350 EXPORT_SYMBOL_GPL(iscsi_session_teardown
);
1353 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1354 * @cls_session: iscsi_cls_session
1357 struct iscsi_cls_conn
*
1358 iscsi_conn_setup(struct iscsi_cls_session
*cls_session
, uint32_t conn_idx
)
1360 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1361 struct iscsi_conn
*conn
;
1362 struct iscsi_cls_conn
*cls_conn
;
1365 cls_conn
= iscsi_create_conn(cls_session
, conn_idx
);
1368 conn
= cls_conn
->dd_data
;
1369 memset(conn
, 0, sizeof(*conn
));
1371 conn
->session
= session
;
1372 conn
->cls_conn
= cls_conn
;
1373 conn
->c_stage
= ISCSI_CONN_INITIAL_STAGE
;
1374 conn
->id
= conn_idx
;
1375 conn
->exp_statsn
= 0;
1376 conn
->tmabort_state
= TMABORT_INITIAL
;
1377 INIT_LIST_HEAD(&conn
->run_list
);
1378 INIT_LIST_HEAD(&conn
->mgmt_run_list
);
1379 INIT_LIST_HEAD(&conn
->xmitqueue
);
1381 /* initialize general immediate & non-immediate PDU commands queue */
1382 conn
->immqueue
= kfifo_alloc(session
->mgmtpool_max
* sizeof(void*),
1384 if (conn
->immqueue
== ERR_PTR(-ENOMEM
))
1385 goto immqueue_alloc_fail
;
1387 conn
->mgmtqueue
= kfifo_alloc(session
->mgmtpool_max
* sizeof(void*),
1389 if (conn
->mgmtqueue
== ERR_PTR(-ENOMEM
))
1390 goto mgmtqueue_alloc_fail
;
1392 INIT_WORK(&conn
->xmitwork
, iscsi_xmitworker
, conn
);
1394 /* allocate login_mtask used for the login/text sequences */
1395 spin_lock_bh(&session
->lock
);
1396 if (!__kfifo_get(session
->mgmtpool
.queue
,
1397 (void*)&conn
->login_mtask
,
1399 spin_unlock_bh(&session
->lock
);
1400 goto login_mtask_alloc_fail
;
1402 spin_unlock_bh(&session
->lock
);
1404 data
= kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH
, GFP_KERNEL
);
1406 goto login_mtask_data_alloc_fail
;
1407 conn
->login_mtask
->data
= data
;
1409 init_timer(&conn
->tmabort_timer
);
1410 mutex_init(&conn
->xmitmutex
);
1411 init_waitqueue_head(&conn
->ehwait
);
1415 login_mtask_data_alloc_fail
:
1416 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->login_mtask
,
1418 login_mtask_alloc_fail
:
1419 kfifo_free(conn
->mgmtqueue
);
1420 mgmtqueue_alloc_fail
:
1421 kfifo_free(conn
->immqueue
);
1422 immqueue_alloc_fail
:
1423 iscsi_destroy_conn(cls_conn
);
1426 EXPORT_SYMBOL_GPL(iscsi_conn_setup
);
1429 * iscsi_conn_teardown - teardown iscsi connection
1430 * cls_conn: iscsi class connection
1432 * TODO: we may need to make this into a two step process
1433 * like scsi-mls remove + put host
1435 void iscsi_conn_teardown(struct iscsi_cls_conn
*cls_conn
)
1437 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1438 struct iscsi_session
*session
= conn
->session
;
1439 unsigned long flags
;
1441 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1442 mutex_lock(&conn
->xmitmutex
);
1444 spin_lock_bh(&session
->lock
);
1445 conn
->c_stage
= ISCSI_CONN_CLEANUP_WAIT
;
1446 if (session
->leadconn
== conn
) {
1448 * leading connection? then give up on recovery.
1450 session
->state
= ISCSI_STATE_TERMINATE
;
1451 wake_up(&conn
->ehwait
);
1453 spin_unlock_bh(&session
->lock
);
1455 mutex_unlock(&conn
->xmitmutex
);
1458 * Block until all in-progress commands for this connection
1462 spin_lock_irqsave(session
->host
->host_lock
, flags
);
1463 if (!session
->host
->host_busy
) { /* OK for ERL == 0 */
1464 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
1467 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
1468 msleep_interruptible(500);
1469 printk(KERN_INFO
"iscsi: scsi conn_destroy(): host_busy %d "
1470 "host_failed %d\n", session
->host
->host_busy
,
1471 session
->host
->host_failed
);
1473 * force eh_abort() to unblock
1475 wake_up(&conn
->ehwait
);
1478 spin_lock_bh(&session
->lock
);
1479 kfree(conn
->login_mtask
->data
);
1480 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->login_mtask
,
1482 list_del(&conn
->item
);
1483 if (list_empty(&session
->connections
))
1484 session
->leadconn
= NULL
;
1485 if (session
->leadconn
&& session
->leadconn
== conn
)
1486 session
->leadconn
= container_of(session
->connections
.next
,
1487 struct iscsi_conn
, item
);
1489 if (session
->leadconn
== NULL
)
1490 /* no connections exits.. reset sequencing */
1491 session
->cmdsn
= session
->max_cmdsn
= session
->exp_cmdsn
= 1;
1492 spin_unlock_bh(&session
->lock
);
1494 kfifo_free(conn
->immqueue
);
1495 kfifo_free(conn
->mgmtqueue
);
1497 iscsi_destroy_conn(cls_conn
);
1499 EXPORT_SYMBOL_GPL(iscsi_conn_teardown
);
1501 int iscsi_conn_start(struct iscsi_cls_conn
*cls_conn
)
1503 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1504 struct iscsi_session
*session
= conn
->session
;
1506 if (session
== NULL
) {
1507 printk(KERN_ERR
"iscsi: can't start unbound connection\n");
1511 spin_lock_bh(&session
->lock
);
1512 conn
->c_stage
= ISCSI_CONN_STARTED
;
1513 session
->state
= ISCSI_STATE_LOGGED_IN
;
1515 switch(conn
->stop_stage
) {
1516 case STOP_CONN_RECOVER
:
1518 * unblock eh_abort() if it is blocked. re-try all
1519 * commands after successful recovery
1521 conn
->stop_stage
= 0;
1522 conn
->tmabort_state
= TMABORT_INITIAL
;
1524 spin_unlock_bh(&session
->lock
);
1526 iscsi_unblock_session(session_to_cls(session
));
1527 wake_up(&conn
->ehwait
);
1529 case STOP_CONN_TERM
:
1530 conn
->stop_stage
= 0;
1535 spin_unlock_bh(&session
->lock
);
1539 EXPORT_SYMBOL_GPL(iscsi_conn_start
);
1542 flush_control_queues(struct iscsi_session
*session
, struct iscsi_conn
*conn
)
1544 struct iscsi_mgmt_task
*mtask
, *tmp
;
1546 /* handle pending */
1547 while (__kfifo_get(conn
->immqueue
, (void*)&mtask
, sizeof(void*)) ||
1548 __kfifo_get(conn
->mgmtqueue
, (void*)&mtask
, sizeof(void*))) {
1549 if (mtask
== conn
->login_mtask
)
1551 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask
->itt
);
1552 __kfifo_put(session
->mgmtpool
.queue
, (void*)&mtask
,
1556 /* handle running */
1557 list_for_each_entry_safe(mtask
, tmp
, &conn
->mgmt_run_list
, running
) {
1558 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask
->itt
);
1559 list_del(&mtask
->running
);
1561 if (mtask
== conn
->login_mtask
)
1563 __kfifo_put(session
->mgmtpool
.queue
, (void*)&mtask
,
1570 /* Fail commands. Mutex and session lock held and recv side suspended */
1571 static void fail_all_commands(struct iscsi_conn
*conn
)
1573 struct iscsi_cmd_task
*ctask
, *tmp
;
1576 list_for_each_entry_safe(ctask
, tmp
, &conn
->xmitqueue
, running
) {
1577 debug_scsi("failing pending sc %p itt 0x%x\n", ctask
->sc
,
1579 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1582 /* fail all other running */
1583 list_for_each_entry_safe(ctask
, tmp
, &conn
->run_list
, running
) {
1584 debug_scsi("failing in progress sc %p itt 0x%x\n",
1585 ctask
->sc
, ctask
->itt
);
1586 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1592 static void iscsi_start_session_recovery(struct iscsi_session
*session
,
1593 struct iscsi_conn
*conn
, int flag
)
1597 spin_lock_bh(&session
->lock
);
1598 if (conn
->stop_stage
== STOP_CONN_TERM
) {
1599 spin_unlock_bh(&session
->lock
);
1604 * When this is called for the in_login state, we only want to clean
1605 * up the login task and connection. We do not need to block and set
1606 * the recovery state again
1608 if (flag
== STOP_CONN_TERM
)
1609 session
->state
= ISCSI_STATE_TERMINATE
;
1610 else if (conn
->stop_stage
!= STOP_CONN_RECOVER
)
1611 session
->state
= ISCSI_STATE_IN_RECOVERY
;
1613 old_stop_stage
= conn
->stop_stage
;
1614 conn
->stop_stage
= flag
;
1615 conn
->c_stage
= ISCSI_CONN_STOPPED
;
1616 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1617 spin_unlock_bh(&session
->lock
);
1619 write_lock_bh(conn
->recv_lock
);
1620 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
1621 write_unlock_bh(conn
->recv_lock
);
1623 mutex_lock(&conn
->xmitmutex
);
1625 * for connection level recovery we should not calculate
1626 * header digest. conn->hdr_size used for optimization
1627 * in hdr_extract() and will be re-negotiated at
1630 if (flag
== STOP_CONN_RECOVER
) {
1631 conn
->hdrdgst_en
= 0;
1632 conn
->datadgst_en
= 0;
1633 if (session
->state
== ISCSI_STATE_IN_RECOVERY
&&
1634 old_stop_stage
!= STOP_CONN_RECOVER
) {
1635 debug_scsi("blocking session\n");
1636 iscsi_block_session(session_to_cls(session
));
1643 spin_lock_bh(&session
->lock
);
1644 fail_all_commands(conn
);
1645 flush_control_queues(session
, conn
);
1646 spin_unlock_bh(&session
->lock
);
1648 mutex_unlock(&conn
->xmitmutex
);
1651 void iscsi_conn_stop(struct iscsi_cls_conn
*cls_conn
, int flag
)
1653 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1654 struct iscsi_session
*session
= conn
->session
;
1657 case STOP_CONN_RECOVER
:
1658 case STOP_CONN_TERM
:
1659 iscsi_start_session_recovery(session
, conn
, flag
);
1662 printk(KERN_ERR
"iscsi: invalid stop flag %d\n", flag
);
1665 EXPORT_SYMBOL_GPL(iscsi_conn_stop
);
1667 int iscsi_conn_bind(struct iscsi_cls_session
*cls_session
,
1668 struct iscsi_cls_conn
*cls_conn
, int is_leading
)
1670 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1671 struct iscsi_conn
*tmp
= ERR_PTR(-EEXIST
), *conn
= cls_conn
->dd_data
;
1673 /* lookup for existing connection */
1674 spin_lock_bh(&session
->lock
);
1675 list_for_each_entry(tmp
, &session
->connections
, item
) {
1677 if (conn
->c_stage
!= ISCSI_CONN_STOPPED
||
1678 conn
->stop_stage
== STOP_CONN_TERM
) {
1679 printk(KERN_ERR
"iscsi: can't bind "
1680 "non-stopped connection (%d:%d)\n",
1681 conn
->c_stage
, conn
->stop_stage
);
1682 spin_unlock_bh(&session
->lock
);
1689 /* bind new iSCSI connection to session */
1690 conn
->session
= session
;
1691 list_add(&conn
->item
, &session
->connections
);
1693 spin_unlock_bh(&session
->lock
);
1696 session
->leadconn
= conn
;
1699 * Unblock xmitworker(), Login Phase will pass through.
1701 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
1702 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1705 EXPORT_SYMBOL_GPL(iscsi_conn_bind
);
1708 int iscsi_set_param(struct iscsi_cls_conn
*cls_conn
,
1709 enum iscsi_param param
, char *buf
, int buflen
)
1711 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1712 struct iscsi_session
*session
= conn
->session
;
1716 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
1717 sscanf(buf
, "%d", &conn
->max_recv_dlength
);
1719 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
1720 sscanf(buf
, "%d", &conn
->max_xmit_dlength
);
1722 case ISCSI_PARAM_HDRDGST_EN
:
1723 sscanf(buf
, "%d", &conn
->hdrdgst_en
);
1725 case ISCSI_PARAM_DATADGST_EN
:
1726 sscanf(buf
, "%d", &conn
->datadgst_en
);
1728 case ISCSI_PARAM_INITIAL_R2T_EN
:
1729 sscanf(buf
, "%d", &session
->initial_r2t_en
);
1731 case ISCSI_PARAM_MAX_R2T
:
1732 sscanf(buf
, "%d", &session
->max_r2t
);
1734 case ISCSI_PARAM_IMM_DATA_EN
:
1735 sscanf(buf
, "%d", &session
->imm_data_en
);
1737 case ISCSI_PARAM_FIRST_BURST
:
1738 sscanf(buf
, "%d", &session
->first_burst
);
1740 case ISCSI_PARAM_MAX_BURST
:
1741 sscanf(buf
, "%d", &session
->max_burst
);
1743 case ISCSI_PARAM_PDU_INORDER_EN
:
1744 sscanf(buf
, "%d", &session
->pdu_inorder_en
);
1746 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
1747 sscanf(buf
, "%d", &session
->dataseq_inorder_en
);
1749 case ISCSI_PARAM_ERL
:
1750 sscanf(buf
, "%d", &session
->erl
);
1752 case ISCSI_PARAM_IFMARKER_EN
:
1753 sscanf(buf
, "%d", &value
);
1756 case ISCSI_PARAM_OFMARKER_EN
:
1757 sscanf(buf
, "%d", &value
);
1760 case ISCSI_PARAM_EXP_STATSN
:
1761 sscanf(buf
, "%u", &conn
->exp_statsn
);
1763 case ISCSI_PARAM_TARGET_NAME
:
1764 /* this should not change between logins */
1765 if (session
->targetname
)
1768 session
->targetname
= kstrdup(buf
, GFP_KERNEL
);
1769 if (!session
->targetname
)
1772 case ISCSI_PARAM_TPGT
:
1773 sscanf(buf
, "%d", &session
->tpgt
);
1775 case ISCSI_PARAM_PERSISTENT_PORT
:
1776 sscanf(buf
, "%d", &conn
->persistent_port
);
1778 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
1780 * this is the address returned in discovery so it should
1781 * not change between logins.
1783 if (conn
->persistent_address
)
1786 conn
->persistent_address
= kstrdup(buf
, GFP_KERNEL
);
1787 if (!conn
->persistent_address
)
1796 EXPORT_SYMBOL_GPL(iscsi_set_param
);
1798 int iscsi_session_get_param(struct iscsi_cls_session
*cls_session
,
1799 enum iscsi_param param
, char *buf
)
1801 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
1802 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
1806 case ISCSI_PARAM_INITIAL_R2T_EN
:
1807 len
= sprintf(buf
, "%d\n", session
->initial_r2t_en
);
1809 case ISCSI_PARAM_MAX_R2T
:
1810 len
= sprintf(buf
, "%hu\n", session
->max_r2t
);
1812 case ISCSI_PARAM_IMM_DATA_EN
:
1813 len
= sprintf(buf
, "%d\n", session
->imm_data_en
);
1815 case ISCSI_PARAM_FIRST_BURST
:
1816 len
= sprintf(buf
, "%u\n", session
->first_burst
);
1818 case ISCSI_PARAM_MAX_BURST
:
1819 len
= sprintf(buf
, "%u\n", session
->max_burst
);
1821 case ISCSI_PARAM_PDU_INORDER_EN
:
1822 len
= sprintf(buf
, "%d\n", session
->pdu_inorder_en
);
1824 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
1825 len
= sprintf(buf
, "%d\n", session
->dataseq_inorder_en
);
1827 case ISCSI_PARAM_ERL
:
1828 len
= sprintf(buf
, "%d\n", session
->erl
);
1830 case ISCSI_PARAM_TARGET_NAME
:
1831 len
= sprintf(buf
, "%s\n", session
->targetname
);
1833 case ISCSI_PARAM_TPGT
:
1834 len
= sprintf(buf
, "%d\n", session
->tpgt
);
1842 EXPORT_SYMBOL_GPL(iscsi_session_get_param
);
1844 int iscsi_conn_get_param(struct iscsi_cls_conn
*cls_conn
,
1845 enum iscsi_param param
, char *buf
)
1847 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1851 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
1852 len
= sprintf(buf
, "%u\n", conn
->max_recv_dlength
);
1854 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
1855 len
= sprintf(buf
, "%u\n", conn
->max_xmit_dlength
);
1857 case ISCSI_PARAM_HDRDGST_EN
:
1858 len
= sprintf(buf
, "%d\n", conn
->hdrdgst_en
);
1860 case ISCSI_PARAM_DATADGST_EN
:
1861 len
= sprintf(buf
, "%d\n", conn
->datadgst_en
);
1863 case ISCSI_PARAM_IFMARKER_EN
:
1864 len
= sprintf(buf
, "%d\n", conn
->ifmarker_en
);
1866 case ISCSI_PARAM_OFMARKER_EN
:
1867 len
= sprintf(buf
, "%d\n", conn
->ofmarker_en
);
1869 case ISCSI_PARAM_EXP_STATSN
:
1870 len
= sprintf(buf
, "%u\n", conn
->exp_statsn
);
1872 case ISCSI_PARAM_PERSISTENT_PORT
:
1873 len
= sprintf(buf
, "%d\n", conn
->persistent_port
);
1875 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
1876 len
= sprintf(buf
, "%s\n", conn
->persistent_address
);
1884 EXPORT_SYMBOL_GPL(iscsi_conn_get_param
);
1886 MODULE_AUTHOR("Mike Christie");
1887 MODULE_DESCRIPTION("iSCSI library functions");
1888 MODULE_LICENSE("GPL");