4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/types.h>
25 #include <linux/mutex.h>
26 #include <linux/kfifo.h>
27 #include <linux/delay.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi.h>
35 #include <scsi/iscsi_proto.h>
36 #include <scsi/scsi_transport.h>
37 #include <scsi/scsi_transport_iscsi.h>
38 #include <scsi/libiscsi.h>
40 struct iscsi_session
*
41 class_to_transport_session(struct iscsi_cls_session
*cls_session
)
43 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
44 return iscsi_hostdata(shost
->hostdata
);
46 EXPORT_SYMBOL_GPL(class_to_transport_session
);
48 #define INVALID_SN_DELTA 0xffff
51 iscsi_check_assign_cmdsn(struct iscsi_session
*session
, struct iscsi_nopin
*hdr
)
53 uint32_t max_cmdsn
= be32_to_cpu(hdr
->max_cmdsn
);
54 uint32_t exp_cmdsn
= be32_to_cpu(hdr
->exp_cmdsn
);
56 if (max_cmdsn
< exp_cmdsn
-1 &&
57 max_cmdsn
> exp_cmdsn
- INVALID_SN_DELTA
)
58 return ISCSI_ERR_MAX_CMDSN
;
59 if (max_cmdsn
> session
->max_cmdsn
||
60 max_cmdsn
< session
->max_cmdsn
- INVALID_SN_DELTA
)
61 session
->max_cmdsn
= max_cmdsn
;
62 if (exp_cmdsn
> session
->exp_cmdsn
||
63 exp_cmdsn
< session
->exp_cmdsn
- INVALID_SN_DELTA
)
64 session
->exp_cmdsn
= exp_cmdsn
;
68 EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn
);
70 void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task
*ctask
,
71 struct iscsi_data
*hdr
)
73 struct iscsi_conn
*conn
= ctask
->conn
;
75 memset(hdr
, 0, sizeof(struct iscsi_data
));
76 hdr
->ttt
= cpu_to_be32(ISCSI_RESERVED_TAG
);
77 hdr
->datasn
= cpu_to_be32(ctask
->unsol_datasn
);
78 ctask
->unsol_datasn
++;
79 hdr
->opcode
= ISCSI_OP_SCSI_DATA_OUT
;
80 memcpy(hdr
->lun
, ctask
->hdr
->lun
, sizeof(hdr
->lun
));
82 hdr
->itt
= ctask
->hdr
->itt
;
83 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
84 hdr
->offset
= cpu_to_be32(ctask
->unsol_offset
);
86 if (ctask
->unsol_count
> conn
->max_xmit_dlength
) {
87 hton24(hdr
->dlength
, conn
->max_xmit_dlength
);
88 ctask
->data_count
= conn
->max_xmit_dlength
;
89 ctask
->unsol_offset
+= ctask
->data_count
;
92 hton24(hdr
->dlength
, ctask
->unsol_count
);
93 ctask
->data_count
= ctask
->unsol_count
;
94 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
97 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu
);
100 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
101 * @ctask: iscsi cmd task
103 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
104 * fields like dlength or final based on how much data it sends
106 static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task
*ctask
)
108 struct iscsi_conn
*conn
= ctask
->conn
;
109 struct iscsi_session
*session
= conn
->session
;
110 struct iscsi_cmd
*hdr
= ctask
->hdr
;
111 struct scsi_cmnd
*sc
= ctask
->sc
;
113 hdr
->opcode
= ISCSI_OP_SCSI_CMD
;
114 hdr
->flags
= ISCSI_ATTR_SIMPLE
;
115 int_to_scsilun(sc
->device
->lun
, (struct scsi_lun
*)hdr
->lun
);
116 hdr
->itt
= ctask
->itt
| (conn
->id
<< ISCSI_CID_SHIFT
) |
117 (session
->age
<< ISCSI_AGE_SHIFT
);
118 hdr
->data_length
= cpu_to_be32(sc
->request_bufflen
);
119 hdr
->cmdsn
= cpu_to_be32(session
->cmdsn
);
121 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
122 memcpy(hdr
->cdb
, sc
->cmnd
, sc
->cmd_len
);
123 memset(&hdr
->cdb
[sc
->cmd_len
], 0, MAX_COMMAND_SIZE
- sc
->cmd_len
);
125 ctask
->data_count
= 0;
126 if (sc
->sc_data_direction
== DMA_TO_DEVICE
) {
127 hdr
->flags
|= ISCSI_FLAG_CMD_WRITE
;
131 * imm_count bytes to be sent right after
134 * unsol_count bytes(as Data-Out) to be sent
135 * without R2T ack right after
138 * r2t_data_count bytes to be sent via R2T ack's
140 * pad_count bytes to be sent as zero-padding
142 ctask
->imm_count
= 0;
143 ctask
->unsol_count
= 0;
144 ctask
->unsol_offset
= 0;
145 ctask
->unsol_datasn
= 0;
147 if (session
->imm_data_en
) {
148 if (ctask
->total_length
>= session
->first_burst
)
149 ctask
->imm_count
= min(session
->first_burst
,
150 conn
->max_xmit_dlength
);
152 ctask
->imm_count
= min(ctask
->total_length
,
153 conn
->max_xmit_dlength
);
154 hton24(ctask
->hdr
->dlength
, ctask
->imm_count
);
156 zero_data(ctask
->hdr
->dlength
);
158 if (!session
->initial_r2t_en
) {
159 ctask
->unsol_count
= min(session
->first_burst
,
160 ctask
->total_length
) - ctask
->imm_count
;
161 ctask
->unsol_offset
= ctask
->imm_count
;
164 if (!ctask
->unsol_count
)
165 /* No unsolicit Data-Out's */
166 ctask
->hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
169 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
170 zero_data(hdr
->dlength
);
172 if (sc
->sc_data_direction
== DMA_FROM_DEVICE
)
173 hdr
->flags
|= ISCSI_FLAG_CMD_READ
;
176 conn
->scsicmd_pdus_cnt
++;
178 EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu
);
181 * iscsi_complete_command - return command back to scsi-ml
182 * @ctask: iscsi cmd task
184 * Must be called with session lock.
185 * This function returns the scsi command to scsi-ml and returns
186 * the cmd task to the pool of available cmd tasks.
188 static void iscsi_complete_command(struct iscsi_cmd_task
*ctask
)
190 struct iscsi_session
*session
= ctask
->conn
->session
;
191 struct scsi_cmnd
*sc
= ctask
->sc
;
193 ctask
->state
= ISCSI_TASK_COMPLETED
;
195 list_del_init(&ctask
->running
);
196 __kfifo_put(session
->cmdpool
.queue
, (void*)&ctask
, sizeof(void*));
200 static void __iscsi_get_ctask(struct iscsi_cmd_task
*ctask
)
202 atomic_inc(&ctask
->refcount
);
205 static void iscsi_get_ctask(struct iscsi_cmd_task
*ctask
)
207 spin_lock_bh(&ctask
->conn
->session
->lock
);
208 __iscsi_get_ctask(ctask
);
209 spin_unlock_bh(&ctask
->conn
->session
->lock
);
212 static void __iscsi_put_ctask(struct iscsi_cmd_task
*ctask
)
214 struct iscsi_conn
*conn
= ctask
->conn
;
216 if (atomic_dec_and_test(&ctask
->refcount
)) {
217 conn
->session
->tt
->cleanup_cmd_task(conn
, ctask
);
218 iscsi_complete_command(ctask
);
222 static void iscsi_put_ctask(struct iscsi_cmd_task
*ctask
)
224 spin_lock_bh(&ctask
->conn
->session
->lock
);
225 __iscsi_put_ctask(ctask
);
226 spin_unlock_bh(&ctask
->conn
->session
->lock
);
230 * iscsi_cmd_rsp - SCSI Command Response processing
231 * @conn: iscsi connection
233 * @ctask: scsi command task
234 * @data: cmd data buffer
235 * @datalen: len of buffer
237 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
238 * then completes the command and task.
240 static int iscsi_scsi_cmd_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
241 struct iscsi_cmd_task
*ctask
, char *data
,
245 struct iscsi_cmd_rsp
*rhdr
= (struct iscsi_cmd_rsp
*)hdr
;
246 struct iscsi_session
*session
= conn
->session
;
247 struct scsi_cmnd
*sc
= ctask
->sc
;
249 rc
= iscsi_check_assign_cmdsn(session
, (struct iscsi_nopin
*)rhdr
);
251 sc
->result
= DID_ERROR
<< 16;
255 conn
->exp_statsn
= be32_to_cpu(rhdr
->statsn
) + 1;
257 sc
->result
= (DID_OK
<< 16) | rhdr
->cmd_status
;
259 if (rhdr
->response
!= ISCSI_STATUS_CMD_COMPLETED
) {
260 sc
->result
= DID_ERROR
<< 16;
264 if (rhdr
->cmd_status
== SAM_STAT_CHECK_CONDITION
) {
269 printk(KERN_ERR
"iscsi: Got CHECK_CONDITION but "
270 "invalid data buffer size of %d\n", datalen
);
271 sc
->result
= DID_BAD_TARGET
<< 16;
275 senselen
= (data
[0] << 8) | data
[1];
276 if (datalen
< senselen
)
277 goto invalid_datalen
;
279 memcpy(sc
->sense_buffer
, data
+ 2,
280 min(senselen
, SCSI_SENSE_BUFFERSIZE
));
281 debug_scsi("copied %d bytes of sense\n",
282 min(senselen
, SCSI_SENSE_BUFFERSIZE
));
285 if (sc
->sc_data_direction
== DMA_TO_DEVICE
)
288 if (rhdr
->flags
& ISCSI_FLAG_CMD_UNDERFLOW
) {
289 int res_count
= be32_to_cpu(rhdr
->residual_count
);
291 if (res_count
> 0 && res_count
<= sc
->request_bufflen
)
292 sc
->resid
= res_count
;
294 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
295 } else if (rhdr
->flags
& ISCSI_FLAG_CMD_BIDI_UNDERFLOW
)
296 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
297 else if (rhdr
->flags
& ISCSI_FLAG_CMD_OVERFLOW
)
298 sc
->resid
= be32_to_cpu(rhdr
->residual_count
);
301 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
302 (long)sc
, sc
->result
, ctask
->itt
);
303 conn
->scsirsp_pdus_cnt
++;
305 __iscsi_put_ctask(ctask
);
309 static void iscsi_tmf_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
)
311 struct iscsi_tm_rsp
*tmf
= (struct iscsi_tm_rsp
*)hdr
;
313 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
314 conn
->tmfrsp_pdus_cnt
++;
316 if (conn
->tmabort_state
!= TMABORT_INITIAL
)
319 if (tmf
->response
== ISCSI_TMF_RSP_COMPLETE
)
320 conn
->tmabort_state
= TMABORT_SUCCESS
;
321 else if (tmf
->response
== ISCSI_TMF_RSP_NO_TASK
)
322 conn
->tmabort_state
= TMABORT_NOT_FOUND
;
324 conn
->tmabort_state
= TMABORT_FAILED
;
325 wake_up(&conn
->ehwait
);
328 static int iscsi_handle_reject(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
329 char *data
, int datalen
)
331 struct iscsi_reject
*reject
= (struct iscsi_reject
*)hdr
;
332 struct iscsi_hdr rejected_pdu
;
335 conn
->exp_statsn
= be32_to_cpu(reject
->statsn
) + 1;
337 if (reject
->reason
== ISCSI_REASON_DATA_DIGEST_ERROR
) {
338 if (ntoh24(reject
->dlength
) > datalen
)
339 return ISCSI_ERR_PROTO
;
341 if (ntoh24(reject
->dlength
) >= sizeof(struct iscsi_hdr
)) {
342 memcpy(&rejected_pdu
, data
, sizeof(struct iscsi_hdr
));
343 itt
= rejected_pdu
.itt
& ISCSI_ITT_MASK
;
344 printk(KERN_ERR
"itt 0x%x had pdu (op 0x%x) rejected "
345 "due to DataDigest error.\n", itt
,
346 rejected_pdu
.opcode
);
353 * __iscsi_complete_pdu - complete pdu
357 * @datalen: len of data buffer
359 * Completes pdu processing by freeing any resources allocated at
360 * queuecommand or send generic. session lock must be held and verify
361 * itt must have been called.
363 int __iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
364 char *data
, int datalen
)
366 struct iscsi_session
*session
= conn
->session
;
367 int opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
, rc
= 0;
368 struct iscsi_cmd_task
*ctask
;
369 struct iscsi_mgmt_task
*mtask
;
372 if (hdr
->itt
!= cpu_to_be32(ISCSI_RESERVED_TAG
))
373 itt
= hdr
->itt
& ISCSI_ITT_MASK
;
377 if (itt
< session
->cmds_max
) {
378 ctask
= session
->cmds
[itt
];
380 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
381 opcode
, conn
->id
, ctask
->itt
, datalen
);
384 case ISCSI_OP_SCSI_CMD_RSP
:
385 BUG_ON((void*)ctask
!= ctask
->sc
->SCp
.ptr
);
386 rc
= iscsi_scsi_cmd_rsp(conn
, hdr
, ctask
, data
,
389 case ISCSI_OP_SCSI_DATA_IN
:
390 BUG_ON((void*)ctask
!= ctask
->sc
->SCp
.ptr
);
391 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
392 conn
->scsirsp_pdus_cnt
++;
393 __iscsi_put_ctask(ctask
);
397 /* LLD handles this for now */
400 rc
= ISCSI_ERR_BAD_OPCODE
;
403 } else if (itt
>= ISCSI_MGMT_ITT_OFFSET
&&
404 itt
< ISCSI_MGMT_ITT_OFFSET
+ session
->mgmtpool_max
) {
405 mtask
= session
->mgmt_cmds
[itt
- ISCSI_MGMT_ITT_OFFSET
];
407 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
408 opcode
, conn
->id
, mtask
->itt
, datalen
);
410 rc
= iscsi_check_assign_cmdsn(session
,
411 (struct iscsi_nopin
*)hdr
);
416 case ISCSI_OP_LOGOUT_RSP
:
418 rc
= ISCSI_ERR_PROTO
;
421 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
423 case ISCSI_OP_LOGIN_RSP
:
424 case ISCSI_OP_TEXT_RSP
:
426 * login related PDU's exp_statsn is handled in
429 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
430 rc
= ISCSI_ERR_CONN_FAILED
;
431 list_del(&mtask
->running
);
432 if (conn
->login_mtask
!= mtask
)
433 __kfifo_put(session
->mgmtpool
.queue
,
434 (void*)&mtask
, sizeof(void*));
436 case ISCSI_OP_SCSI_TMFUNC_RSP
:
438 rc
= ISCSI_ERR_PROTO
;
442 iscsi_tmf_rsp(conn
, hdr
);
444 case ISCSI_OP_NOOP_IN
:
445 if (hdr
->ttt
!= ISCSI_RESERVED_TAG
|| datalen
) {
446 rc
= ISCSI_ERR_PROTO
;
449 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
451 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
452 rc
= ISCSI_ERR_CONN_FAILED
;
453 list_del(&mtask
->running
);
454 if (conn
->login_mtask
!= mtask
)
455 __kfifo_put(session
->mgmtpool
.queue
,
456 (void*)&mtask
, sizeof(void*));
459 rc
= ISCSI_ERR_BAD_OPCODE
;
462 } else if (itt
== ISCSI_RESERVED_TAG
) {
463 rc
= iscsi_check_assign_cmdsn(session
,
464 (struct iscsi_nopin
*)hdr
);
469 case ISCSI_OP_NOOP_IN
:
471 rc
= ISCSI_ERR_PROTO
;
475 if (hdr
->ttt
== ISCSI_RESERVED_TAG
)
478 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, NULL
, 0))
479 rc
= ISCSI_ERR_CONN_FAILED
;
481 case ISCSI_OP_REJECT
:
482 rc
= iscsi_handle_reject(conn
, hdr
, data
, datalen
);
484 case ISCSI_OP_ASYNC_EVENT
:
485 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
486 /* we need sth like iscsi_async_event_rsp() */
487 rc
= ISCSI_ERR_BAD_OPCODE
;
490 rc
= ISCSI_ERR_BAD_OPCODE
;
494 rc
= ISCSI_ERR_BAD_ITT
;
499 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu
);
501 int iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
502 char *data
, int datalen
)
506 spin_lock(&conn
->session
->lock
);
507 rc
= __iscsi_complete_pdu(conn
, hdr
, data
, datalen
);
508 spin_unlock(&conn
->session
->lock
);
511 EXPORT_SYMBOL_GPL(iscsi_complete_pdu
);
513 /* verify itt (itt encoding: age+cid+itt) */
514 int iscsi_verify_itt(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
517 struct iscsi_session
*session
= conn
->session
;
518 struct iscsi_cmd_task
*ctask
;
521 if (hdr
->itt
!= cpu_to_be32(ISCSI_RESERVED_TAG
)) {
522 if ((hdr
->itt
& ISCSI_AGE_MASK
) !=
523 (session
->age
<< ISCSI_AGE_SHIFT
)) {
524 printk(KERN_ERR
"iscsi: received itt %x expected "
525 "session age (%x)\n", hdr
->itt
,
526 session
->age
& ISCSI_AGE_MASK
);
527 return ISCSI_ERR_BAD_ITT
;
530 if ((hdr
->itt
& ISCSI_CID_MASK
) !=
531 (conn
->id
<< ISCSI_CID_SHIFT
)) {
532 printk(KERN_ERR
"iscsi: received itt %x, expected "
533 "CID (%x)\n", hdr
->itt
, conn
->id
);
534 return ISCSI_ERR_BAD_ITT
;
536 itt
= hdr
->itt
& ISCSI_ITT_MASK
;
540 if (itt
< session
->cmds_max
) {
541 ctask
= session
->cmds
[itt
];
544 printk(KERN_INFO
"iscsi: dropping ctask with "
545 "itt 0x%x\n", ctask
->itt
);
547 return ISCSI_ERR_NO_SCSI_CMD
;
550 if (ctask
->sc
->SCp
.phase
!= session
->age
) {
551 printk(KERN_ERR
"iscsi: ctask's session age %d, "
552 "expected %d\n", ctask
->sc
->SCp
.phase
,
554 return ISCSI_ERR_SESSION_FAILED
;
561 EXPORT_SYMBOL_GPL(iscsi_verify_itt
);
563 void iscsi_conn_failure(struct iscsi_conn
*conn
, enum iscsi_err err
)
565 struct iscsi_session
*session
= conn
->session
;
568 spin_lock_irqsave(&session
->lock
, flags
);
569 if (session
->state
== ISCSI_STATE_FAILED
) {
570 spin_unlock_irqrestore(&session
->lock
, flags
);
574 if (conn
->stop_stage
== 0)
575 session
->state
= ISCSI_STATE_FAILED
;
576 spin_unlock_irqrestore(&session
->lock
, flags
);
577 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
578 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
579 iscsi_conn_error(conn
->cls_conn
, err
);
581 EXPORT_SYMBOL_GPL(iscsi_conn_failure
);
584 * iscsi_data_xmit - xmit any command into the scheduled connection
585 * @conn: iscsi connection
588 * The function can return -EAGAIN in which case the caller must
589 * re-schedule it again later or recover. '0' return code means
592 static int iscsi_data_xmit(struct iscsi_conn
*conn
)
594 struct iscsi_transport
*tt
;
597 if (unlikely(conn
->suspend_tx
)) {
598 debug_scsi("conn %d Tx suspended!\n", conn
->id
);
601 tt
= conn
->session
->tt
;
604 * Transmit in the following order:
606 * 1) un-finished xmit (ctask or mtask)
607 * 2) immediate control PDUs
610 * 5) non-immediate control PDUs
612 * No need to lock around __kfifo_get as long as
613 * there's one producer and one consumer.
616 BUG_ON(conn
->ctask
&& conn
->mtask
);
619 iscsi_get_ctask(conn
->ctask
);
620 rc
= tt
->xmit_cmd_task(conn
, conn
->ctask
);
621 iscsi_put_ctask(conn
->ctask
);
624 /* done with this in-progress ctask */
628 rc
= tt
->xmit_mgmt_task(conn
, conn
->mtask
);
631 /* done with this in-progress mtask */
635 /* process immediate first */
636 if (unlikely(__kfifo_len(conn
->immqueue
))) {
637 while (__kfifo_get(conn
->immqueue
, (void*)&conn
->mtask
,
639 spin_lock_bh(&conn
->session
->lock
);
640 list_add_tail(&conn
->mtask
->running
,
641 &conn
->mgmt_run_list
);
642 spin_unlock_bh(&conn
->session
->lock
);
643 rc
= tt
->xmit_mgmt_task(conn
, conn
->mtask
);
647 /* done with this mtask */
651 /* process command queue */
652 spin_lock_bh(&conn
->session
->lock
);
653 while (!list_empty(&conn
->xmitqueue
)) {
655 * iscsi tcp may readd the task to the xmitqueue to send
658 conn
->ctask
= list_entry(conn
->xmitqueue
.next
,
659 struct iscsi_cmd_task
, running
);
660 conn
->ctask
->state
= ISCSI_TASK_RUNNING
;
661 list_move_tail(conn
->xmitqueue
.next
, &conn
->run_list
);
662 __iscsi_get_ctask(conn
->ctask
);
663 spin_unlock_bh(&conn
->session
->lock
);
665 rc
= tt
->xmit_cmd_task(conn
, conn
->ctask
);
669 spin_lock_bh(&conn
->session
->lock
);
670 __iscsi_put_ctask(conn
->ctask
);
672 spin_unlock_bh(&conn
->session
->lock
);
676 spin_unlock_bh(&conn
->session
->lock
);
677 /* done with this ctask */
680 /* process the rest control plane PDUs, if any */
681 if (unlikely(__kfifo_len(conn
->mgmtqueue
))) {
682 while (__kfifo_get(conn
->mgmtqueue
, (void*)&conn
->mtask
,
684 spin_lock_bh(&conn
->session
->lock
);
685 list_add_tail(&conn
->mtask
->running
,
686 &conn
->mgmt_run_list
);
687 spin_unlock_bh(&conn
->session
->lock
);
688 rc
= tt
->xmit_mgmt_task(conn
, conn
->mtask
);
692 /* done with this mtask */
699 if (unlikely(conn
->suspend_tx
))
705 static void iscsi_xmitworker(void *data
)
707 struct iscsi_conn
*conn
= data
;
710 * serialize Xmit worker on a per-connection basis.
712 mutex_lock(&conn
->xmitmutex
);
714 rc
= iscsi_data_xmit(conn
);
715 } while (rc
>= 0 || rc
== -EAGAIN
);
716 mutex_unlock(&conn
->xmitmutex
);
720 FAILURE_BAD_HOST
= 1,
721 FAILURE_SESSION_FAILED
,
722 FAILURE_SESSION_FREED
,
723 FAILURE_WINDOW_CLOSED
,
725 FAILURE_SESSION_TERMINATE
,
726 FAILURE_SESSION_IN_RECOVERY
,
727 FAILURE_SESSION_RECOVERY_TIMEOUT
,
730 int iscsi_queuecommand(struct scsi_cmnd
*sc
, void (*done
)(struct scsi_cmnd
*))
732 struct Scsi_Host
*host
;
734 struct iscsi_session
*session
;
735 struct iscsi_conn
*conn
;
736 struct iscsi_cmd_task
*ctask
= NULL
;
738 sc
->scsi_done
= done
;
741 host
= sc
->device
->host
;
742 session
= iscsi_hostdata(host
->hostdata
);
744 spin_lock(&session
->lock
);
747 * ISCSI_STATE_FAILED is a temp. state. The recovery
748 * code will decide what is best to do with command queued
751 if (session
->state
!= ISCSI_STATE_LOGGED_IN
&&
752 session
->state
!= ISCSI_STATE_FAILED
) {
754 * to handle the race between when we set the recovery state
755 * and block the session we requeue here (commands could
756 * be entering our queuecommand while a block is starting
757 * up because the block code is not locked)
759 if (session
->state
== ISCSI_STATE_IN_RECOVERY
) {
760 reason
= FAILURE_SESSION_IN_RECOVERY
;
764 if (session
->state
== ISCSI_STATE_RECOVERY_FAILED
)
765 reason
= FAILURE_SESSION_RECOVERY_TIMEOUT
;
766 else if (session
->state
== ISCSI_STATE_TERMINATE
)
767 reason
= FAILURE_SESSION_TERMINATE
;
769 reason
= FAILURE_SESSION_FREED
;
774 * Check for iSCSI window and take care of CmdSN wrap-around
776 if ((int)(session
->max_cmdsn
- session
->cmdsn
) < 0) {
777 reason
= FAILURE_WINDOW_CLOSED
;
781 conn
= session
->leadconn
;
783 if (!__kfifo_get(session
->cmdpool
.queue
, (void*)&ctask
,
785 reason
= FAILURE_OOM
;
788 sc
->SCp
.phase
= session
->age
;
789 sc
->SCp
.ptr
= (char *)ctask
;
791 atomic_set(&ctask
->refcount
, 1);
792 ctask
->state
= ISCSI_TASK_PENDING
;
796 INIT_LIST_HEAD(&ctask
->running
);
797 ctask
->total_length
= sc
->request_bufflen
;
798 iscsi_prep_scsi_cmd_pdu(ctask
);
800 session
->tt
->init_cmd_task(ctask
);
802 list_add_tail(&ctask
->running
, &conn
->xmitqueue
);
804 "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
805 sc
->sc_data_direction
== DMA_TO_DEVICE
? "write" : "read",
806 conn
->id
, (long)sc
, 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
;
1128 iscsi_ctask_mtask_cleanup(ctask
);
1131 sc
->resid
= sc
->request_bufflen
;
1132 __iscsi_put_ctask(ctask
);
1135 int iscsi_eh_abort(struct scsi_cmnd
*sc
)
1137 struct iscsi_cmd_task
*ctask
= (struct iscsi_cmd_task
*)sc
->SCp
.ptr
;
1138 struct iscsi_conn
*conn
= ctask
->conn
;
1139 struct iscsi_session
*session
= conn
->session
;
1142 conn
->eh_abort_cnt
++;
1143 debug_scsi("aborting [sc %p itt 0x%x]\n", sc
, ctask
->itt
);
1145 mutex_lock(&conn
->xmitmutex
);
1146 spin_lock_bh(&session
->lock
);
1149 * If we are not logged in or we have started a new session
1150 * then let the host reset code handle this
1152 if (session
->state
!= ISCSI_STATE_LOGGED_IN
||
1153 sc
->SCp
.phase
!= session
->age
)
1156 /* ctask completed before time out */
1158 spin_unlock_bh(&session
->lock
);
1159 debug_scsi("sc completed while abort in progress\n");
1160 goto success_rel_mutex
;
1163 /* what should we do here ? */
1164 if (conn
->ctask
== ctask
) {
1165 printk(KERN_INFO
"iscsi: sc %p itt 0x%x partially sent. "
1166 "Failing abort\n", sc
, ctask
->itt
);
1170 if (ctask
->state
== ISCSI_TASK_PENDING
)
1171 goto success_cleanup
;
1173 conn
->tmabort_state
= TMABORT_INITIAL
;
1175 spin_unlock_bh(&session
->lock
);
1176 rc
= iscsi_exec_abort_task(sc
, ctask
);
1177 spin_lock_bh(&session
->lock
);
1179 if (rc
|| sc
->SCp
.phase
!= session
->age
||
1180 session
->state
!= ISCSI_STATE_LOGGED_IN
)
1182 iscsi_ctask_mtask_cleanup(ctask
);
1184 switch (conn
->tmabort_state
) {
1185 case TMABORT_SUCCESS
:
1186 goto success_cleanup
;
1187 case TMABORT_NOT_FOUND
:
1189 /* ctask completed before tmf abort response */
1190 spin_unlock_bh(&session
->lock
);
1191 debug_scsi("sc completed while abort in progress\n");
1192 goto success_rel_mutex
;
1196 /* timedout or failed */
1197 spin_unlock_bh(&session
->lock
);
1198 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1199 spin_lock_bh(&session
->lock
);
1204 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc
, ctask
->itt
);
1205 spin_unlock_bh(&session
->lock
);
1208 * clean up task if aborted. we have the xmitmutex so grab
1209 * the recv lock as a writer
1211 write_lock_bh(conn
->recv_lock
);
1212 spin_lock(&session
->lock
);
1213 fail_command(conn
, ctask
, DID_ABORT
<< 16);
1214 spin_unlock(&session
->lock
);
1215 write_unlock_bh(conn
->recv_lock
);
1218 mutex_unlock(&conn
->xmitmutex
);
1222 spin_unlock_bh(&session
->lock
);
1223 mutex_unlock(&conn
->xmitmutex
);
1225 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc
, ctask
->itt
);
1228 EXPORT_SYMBOL_GPL(iscsi_eh_abort
);
1231 iscsi_pool_init(struct iscsi_queue
*q
, int max
, void ***items
, int item_size
)
1235 *items
= kmalloc(max
* sizeof(void*), GFP_KERNEL
);
1240 q
->pool
= kmalloc(max
* sizeof(void*), GFP_KERNEL
);
1241 if (q
->pool
== NULL
) {
1246 q
->queue
= kfifo_init((void*)q
->pool
, max
* sizeof(void*),
1248 if (q
->queue
== ERR_PTR(-ENOMEM
)) {
1254 for (i
= 0; i
< max
; i
++) {
1255 q
->pool
[i
] = kmalloc(item_size
, GFP_KERNEL
);
1256 if (q
->pool
[i
] == NULL
) {
1259 for (j
= 0; j
< i
; j
++)
1262 kfifo_free(q
->queue
);
1267 memset(q
->pool
[i
], 0, item_size
);
1268 (*items
)[i
] = q
->pool
[i
];
1269 __kfifo_put(q
->queue
, (void*)&q
->pool
[i
], sizeof(void*));
1273 EXPORT_SYMBOL_GPL(iscsi_pool_init
);
1275 void iscsi_pool_free(struct iscsi_queue
*q
, void **items
)
1279 for (i
= 0; i
< q
->max
; i
++)
1284 EXPORT_SYMBOL_GPL(iscsi_pool_free
);
1287 * iSCSI Session's hostdata organization:
1289 * *------------------* <== hostdata_session(host->hostdata)
1290 * | ptr to class sess|
1291 * |------------------| <== iscsi_hostdata(host->hostdata)
1293 * *------------------*
1296 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1297 _sz % sizeof(unsigned long))
1299 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1302 * iscsi_session_setup - create iscsi cls session and host and session
1303 * @scsit: scsi transport template
1304 * @iscsit: iscsi transport template
1305 * @initial_cmdsn: initial CmdSN
1306 * @hostno: host no allocated
1308 * This can be used by software iscsi_transports that allocate
1309 * a session per scsi host.
1311 struct iscsi_cls_session
*
1312 iscsi_session_setup(struct iscsi_transport
*iscsit
,
1313 struct scsi_transport_template
*scsit
,
1314 int cmd_task_size
, int mgmt_task_size
,
1315 uint32_t initial_cmdsn
, uint32_t *hostno
)
1317 struct Scsi_Host
*shost
;
1318 struct iscsi_session
*session
;
1319 struct iscsi_cls_session
*cls_session
;
1322 shost
= scsi_host_alloc(iscsit
->host_template
,
1323 hostdata_privsize(sizeof(*session
)));
1328 shost
->max_channel
= 0;
1329 shost
->max_lun
= iscsit
->max_lun
;
1330 shost
->max_cmd_len
= iscsit
->max_cmd_len
;
1331 shost
->transportt
= scsit
;
1332 shost
->transportt
->create_work_queue
= 1;
1333 *hostno
= shost
->host_no
;
1335 session
= iscsi_hostdata(shost
->hostdata
);
1336 memset(session
, 0, sizeof(struct iscsi_session
));
1337 session
->host
= shost
;
1338 session
->state
= ISCSI_STATE_FREE
;
1339 session
->mgmtpool_max
= ISCSI_MGMT_CMDS_MAX
;
1340 session
->cmds_max
= ISCSI_XMIT_CMDS_MAX
;
1341 session
->cmdsn
= initial_cmdsn
;
1342 session
->exp_cmdsn
= initial_cmdsn
+ 1;
1343 session
->max_cmdsn
= initial_cmdsn
+ 1;
1344 session
->max_r2t
= 1;
1345 session
->tt
= iscsit
;
1347 /* initialize SCSI PDU commands pool */
1348 if (iscsi_pool_init(&session
->cmdpool
, session
->cmds_max
,
1349 (void***)&session
->cmds
,
1350 cmd_task_size
+ sizeof(struct iscsi_cmd_task
)))
1351 goto cmdpool_alloc_fail
;
1353 /* pre-format cmds pool with ITT */
1354 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
1355 struct iscsi_cmd_task
*ctask
= session
->cmds
[cmd_i
];
1358 ctask
->dd_data
= &ctask
[1];
1360 INIT_LIST_HEAD(&ctask
->running
);
1363 spin_lock_init(&session
->lock
);
1364 INIT_LIST_HEAD(&session
->connections
);
1366 /* initialize immediate command pool */
1367 if (iscsi_pool_init(&session
->mgmtpool
, session
->mgmtpool_max
,
1368 (void***)&session
->mgmt_cmds
,
1369 mgmt_task_size
+ sizeof(struct iscsi_mgmt_task
)))
1370 goto mgmtpool_alloc_fail
;
1373 /* pre-format immediate cmds pool with ITT */
1374 for (cmd_i
= 0; cmd_i
< session
->mgmtpool_max
; cmd_i
++) {
1375 struct iscsi_mgmt_task
*mtask
= session
->mgmt_cmds
[cmd_i
];
1378 mtask
->dd_data
= &mtask
[1];
1379 mtask
->itt
= ISCSI_MGMT_ITT_OFFSET
+ cmd_i
;
1380 INIT_LIST_HEAD(&mtask
->running
);
1383 if (scsi_add_host(shost
, NULL
))
1386 if (!try_module_get(iscsit
->owner
))
1387 goto cls_session_fail
;
1389 cls_session
= iscsi_create_session(shost
, iscsit
, 0);
1392 *(unsigned long*)shost
->hostdata
= (unsigned long)cls_session
;
1397 module_put(iscsit
->owner
);
1399 scsi_remove_host(shost
);
1401 iscsi_pool_free(&session
->mgmtpool
, (void**)session
->mgmt_cmds
);
1402 mgmtpool_alloc_fail
:
1403 iscsi_pool_free(&session
->cmdpool
, (void**)session
->cmds
);
1405 scsi_host_put(shost
);
1408 EXPORT_SYMBOL_GPL(iscsi_session_setup
);
1411 * iscsi_session_teardown - destroy session, host, and cls_session
1414 * This can be used by software iscsi_transports that allocate
1415 * a session per scsi host.
1417 void iscsi_session_teardown(struct iscsi_cls_session
*cls_session
)
1419 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
1420 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
1421 struct module
*owner
= cls_session
->transport
->owner
;
1423 scsi_remove_host(shost
);
1425 iscsi_pool_free(&session
->mgmtpool
, (void**)session
->mgmt_cmds
);
1426 iscsi_pool_free(&session
->cmdpool
, (void**)session
->cmds
);
1428 kfree(session
->targetname
);
1430 iscsi_destroy_session(cls_session
);
1431 scsi_host_put(shost
);
1434 EXPORT_SYMBOL_GPL(iscsi_session_teardown
);
1437 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1438 * @cls_session: iscsi_cls_session
1441 struct iscsi_cls_conn
*
1442 iscsi_conn_setup(struct iscsi_cls_session
*cls_session
, uint32_t conn_idx
)
1444 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1445 struct iscsi_conn
*conn
;
1446 struct iscsi_cls_conn
*cls_conn
;
1449 cls_conn
= iscsi_create_conn(cls_session
, conn_idx
);
1452 conn
= cls_conn
->dd_data
;
1453 memset(conn
, 0, sizeof(*conn
));
1455 conn
->session
= session
;
1456 conn
->cls_conn
= cls_conn
;
1457 conn
->c_stage
= ISCSI_CONN_INITIAL_STAGE
;
1458 conn
->id
= conn_idx
;
1459 conn
->exp_statsn
= 0;
1460 conn
->tmabort_state
= TMABORT_INITIAL
;
1461 INIT_LIST_HEAD(&conn
->run_list
);
1462 INIT_LIST_HEAD(&conn
->mgmt_run_list
);
1463 INIT_LIST_HEAD(&conn
->xmitqueue
);
1465 /* initialize general immediate & non-immediate PDU commands queue */
1466 conn
->immqueue
= kfifo_alloc(session
->mgmtpool_max
* sizeof(void*),
1468 if (conn
->immqueue
== ERR_PTR(-ENOMEM
))
1469 goto immqueue_alloc_fail
;
1471 conn
->mgmtqueue
= kfifo_alloc(session
->mgmtpool_max
* sizeof(void*),
1473 if (conn
->mgmtqueue
== ERR_PTR(-ENOMEM
))
1474 goto mgmtqueue_alloc_fail
;
1476 INIT_WORK(&conn
->xmitwork
, iscsi_xmitworker
, conn
);
1478 /* allocate login_mtask used for the login/text sequences */
1479 spin_lock_bh(&session
->lock
);
1480 if (!__kfifo_get(session
->mgmtpool
.queue
,
1481 (void*)&conn
->login_mtask
,
1483 spin_unlock_bh(&session
->lock
);
1484 goto login_mtask_alloc_fail
;
1486 spin_unlock_bh(&session
->lock
);
1488 data
= kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH
, GFP_KERNEL
);
1490 goto login_mtask_data_alloc_fail
;
1491 conn
->login_mtask
->data
= conn
->data
= data
;
1493 init_timer(&conn
->tmabort_timer
);
1494 mutex_init(&conn
->xmitmutex
);
1495 init_waitqueue_head(&conn
->ehwait
);
1499 login_mtask_data_alloc_fail
:
1500 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->login_mtask
,
1502 login_mtask_alloc_fail
:
1503 kfifo_free(conn
->mgmtqueue
);
1504 mgmtqueue_alloc_fail
:
1505 kfifo_free(conn
->immqueue
);
1506 immqueue_alloc_fail
:
1507 iscsi_destroy_conn(cls_conn
);
1510 EXPORT_SYMBOL_GPL(iscsi_conn_setup
);
1513 * iscsi_conn_teardown - teardown iscsi connection
1514 * cls_conn: iscsi class connection
1516 * TODO: we may need to make this into a two step process
1517 * like scsi-mls remove + put host
1519 void iscsi_conn_teardown(struct iscsi_cls_conn
*cls_conn
)
1521 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1522 struct iscsi_session
*session
= conn
->session
;
1523 unsigned long flags
;
1525 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1526 mutex_lock(&conn
->xmitmutex
);
1528 spin_lock_bh(&session
->lock
);
1529 conn
->c_stage
= ISCSI_CONN_CLEANUP_WAIT
;
1530 if (session
->leadconn
== conn
) {
1532 * leading connection? then give up on recovery.
1534 session
->state
= ISCSI_STATE_TERMINATE
;
1535 wake_up(&conn
->ehwait
);
1537 spin_unlock_bh(&session
->lock
);
1539 mutex_unlock(&conn
->xmitmutex
);
1542 * Block until all in-progress commands for this connection
1546 spin_lock_irqsave(session
->host
->host_lock
, flags
);
1547 if (!session
->host
->host_busy
) { /* OK for ERL == 0 */
1548 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
1551 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
1552 msleep_interruptible(500);
1553 printk(KERN_INFO
"iscsi: scsi conn_destroy(): host_busy %d "
1554 "host_failed %d\n", session
->host
->host_busy
,
1555 session
->host
->host_failed
);
1557 * force eh_abort() to unblock
1559 wake_up(&conn
->ehwait
);
1562 spin_lock_bh(&session
->lock
);
1564 kfree(conn
->persistent_address
);
1565 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->login_mtask
,
1567 list_del(&conn
->item
);
1568 if (list_empty(&session
->connections
))
1569 session
->leadconn
= NULL
;
1570 if (session
->leadconn
&& session
->leadconn
== conn
)
1571 session
->leadconn
= container_of(session
->connections
.next
,
1572 struct iscsi_conn
, item
);
1574 if (session
->leadconn
== NULL
)
1575 /* no connections exits.. reset sequencing */
1576 session
->cmdsn
= session
->max_cmdsn
= session
->exp_cmdsn
= 1;
1577 spin_unlock_bh(&session
->lock
);
1579 kfifo_free(conn
->immqueue
);
1580 kfifo_free(conn
->mgmtqueue
);
1582 iscsi_destroy_conn(cls_conn
);
1584 EXPORT_SYMBOL_GPL(iscsi_conn_teardown
);
1586 int iscsi_conn_start(struct iscsi_cls_conn
*cls_conn
)
1588 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1589 struct iscsi_session
*session
= conn
->session
;
1592 printk(KERN_ERR
"iscsi: can't start unbound connection\n");
1596 if ((session
->imm_data_en
|| !session
->initial_r2t_en
) &&
1597 session
->first_burst
> session
->max_burst
) {
1598 printk("iscsi: invalid burst lengths: "
1599 "first_burst %d max_burst %d\n",
1600 session
->first_burst
, session
->max_burst
);
1604 spin_lock_bh(&session
->lock
);
1605 conn
->c_stage
= ISCSI_CONN_STARTED
;
1606 session
->state
= ISCSI_STATE_LOGGED_IN
;
1608 switch(conn
->stop_stage
) {
1609 case STOP_CONN_RECOVER
:
1611 * unblock eh_abort() if it is blocked. re-try all
1612 * commands after successful recovery
1614 conn
->stop_stage
= 0;
1615 conn
->tmabort_state
= TMABORT_INITIAL
;
1617 spin_unlock_bh(&session
->lock
);
1619 iscsi_unblock_session(session_to_cls(session
));
1620 wake_up(&conn
->ehwait
);
1622 case STOP_CONN_TERM
:
1623 conn
->stop_stage
= 0;
1628 spin_unlock_bh(&session
->lock
);
1632 EXPORT_SYMBOL_GPL(iscsi_conn_start
);
1635 flush_control_queues(struct iscsi_session
*session
, struct iscsi_conn
*conn
)
1637 struct iscsi_mgmt_task
*mtask
, *tmp
;
1639 /* handle pending */
1640 while (__kfifo_get(conn
->immqueue
, (void*)&mtask
, sizeof(void*)) ||
1641 __kfifo_get(conn
->mgmtqueue
, (void*)&mtask
, sizeof(void*))) {
1642 if (mtask
== conn
->login_mtask
)
1644 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask
->itt
);
1645 __kfifo_put(session
->mgmtpool
.queue
, (void*)&mtask
,
1649 /* handle running */
1650 list_for_each_entry_safe(mtask
, tmp
, &conn
->mgmt_run_list
, running
) {
1651 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask
->itt
);
1652 list_del(&mtask
->running
);
1654 if (mtask
== conn
->login_mtask
)
1656 __kfifo_put(session
->mgmtpool
.queue
, (void*)&mtask
,
1663 /* Fail commands. Mutex and session lock held and recv side suspended */
1664 static void fail_all_commands(struct iscsi_conn
*conn
)
1666 struct iscsi_cmd_task
*ctask
, *tmp
;
1669 list_for_each_entry_safe(ctask
, tmp
, &conn
->xmitqueue
, running
) {
1670 debug_scsi("failing pending sc %p itt 0x%x\n", ctask
->sc
,
1672 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1675 /* fail all other running */
1676 list_for_each_entry_safe(ctask
, tmp
, &conn
->run_list
, running
) {
1677 debug_scsi("failing in progress sc %p itt 0x%x\n",
1678 ctask
->sc
, ctask
->itt
);
1679 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1685 static void iscsi_start_session_recovery(struct iscsi_session
*session
,
1686 struct iscsi_conn
*conn
, int flag
)
1690 spin_lock_bh(&session
->lock
);
1691 if (conn
->stop_stage
== STOP_CONN_TERM
) {
1692 spin_unlock_bh(&session
->lock
);
1697 * When this is called for the in_login state, we only want to clean
1698 * up the login task and connection. We do not need to block and set
1699 * the recovery state again
1701 if (flag
== STOP_CONN_TERM
)
1702 session
->state
= ISCSI_STATE_TERMINATE
;
1703 else if (conn
->stop_stage
!= STOP_CONN_RECOVER
)
1704 session
->state
= ISCSI_STATE_IN_RECOVERY
;
1706 old_stop_stage
= conn
->stop_stage
;
1707 conn
->stop_stage
= flag
;
1708 conn
->c_stage
= ISCSI_CONN_STOPPED
;
1709 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1710 spin_unlock_bh(&session
->lock
);
1712 write_lock_bh(conn
->recv_lock
);
1713 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
1714 write_unlock_bh(conn
->recv_lock
);
1716 mutex_lock(&conn
->xmitmutex
);
1718 * for connection level recovery we should not calculate
1719 * header digest. conn->hdr_size used for optimization
1720 * in hdr_extract() and will be re-negotiated at
1723 if (flag
== STOP_CONN_RECOVER
) {
1724 conn
->hdrdgst_en
= 0;
1725 conn
->datadgst_en
= 0;
1726 if (session
->state
== ISCSI_STATE_IN_RECOVERY
&&
1727 old_stop_stage
!= STOP_CONN_RECOVER
) {
1728 debug_scsi("blocking session\n");
1729 iscsi_block_session(session_to_cls(session
));
1736 spin_lock_bh(&session
->lock
);
1737 fail_all_commands(conn
);
1738 flush_control_queues(session
, conn
);
1739 spin_unlock_bh(&session
->lock
);
1741 mutex_unlock(&conn
->xmitmutex
);
1744 void iscsi_conn_stop(struct iscsi_cls_conn
*cls_conn
, int flag
)
1746 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1747 struct iscsi_session
*session
= conn
->session
;
1750 case STOP_CONN_RECOVER
:
1751 case STOP_CONN_TERM
:
1752 iscsi_start_session_recovery(session
, conn
, flag
);
1755 printk(KERN_ERR
"iscsi: invalid stop flag %d\n", flag
);
1758 EXPORT_SYMBOL_GPL(iscsi_conn_stop
);
1760 int iscsi_conn_bind(struct iscsi_cls_session
*cls_session
,
1761 struct iscsi_cls_conn
*cls_conn
, int is_leading
)
1763 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1764 struct iscsi_conn
*tmp
= ERR_PTR(-EEXIST
), *conn
= cls_conn
->dd_data
;
1766 /* lookup for existing connection */
1767 spin_lock_bh(&session
->lock
);
1768 list_for_each_entry(tmp
, &session
->connections
, item
) {
1770 if (conn
->c_stage
!= ISCSI_CONN_STOPPED
||
1771 conn
->stop_stage
== STOP_CONN_TERM
) {
1772 printk(KERN_ERR
"iscsi: can't bind "
1773 "non-stopped connection (%d:%d)\n",
1774 conn
->c_stage
, conn
->stop_stage
);
1775 spin_unlock_bh(&session
->lock
);
1782 /* bind new iSCSI connection to session */
1783 conn
->session
= session
;
1784 list_add(&conn
->item
, &session
->connections
);
1786 spin_unlock_bh(&session
->lock
);
1789 session
->leadconn
= conn
;
1792 * Unblock xmitworker(), Login Phase will pass through.
1794 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
1795 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1798 EXPORT_SYMBOL_GPL(iscsi_conn_bind
);
1801 int iscsi_set_param(struct iscsi_cls_conn
*cls_conn
,
1802 enum iscsi_param param
, char *buf
, int buflen
)
1804 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1805 struct iscsi_session
*session
= conn
->session
;
1809 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
1810 sscanf(buf
, "%d", &conn
->max_recv_dlength
);
1812 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
1813 sscanf(buf
, "%d", &conn
->max_xmit_dlength
);
1815 case ISCSI_PARAM_HDRDGST_EN
:
1816 sscanf(buf
, "%d", &conn
->hdrdgst_en
);
1818 case ISCSI_PARAM_DATADGST_EN
:
1819 sscanf(buf
, "%d", &conn
->datadgst_en
);
1821 case ISCSI_PARAM_INITIAL_R2T_EN
:
1822 sscanf(buf
, "%d", &session
->initial_r2t_en
);
1824 case ISCSI_PARAM_MAX_R2T
:
1825 sscanf(buf
, "%d", &session
->max_r2t
);
1827 case ISCSI_PARAM_IMM_DATA_EN
:
1828 sscanf(buf
, "%d", &session
->imm_data_en
);
1830 case ISCSI_PARAM_FIRST_BURST
:
1831 sscanf(buf
, "%d", &session
->first_burst
);
1833 case ISCSI_PARAM_MAX_BURST
:
1834 sscanf(buf
, "%d", &session
->max_burst
);
1836 case ISCSI_PARAM_PDU_INORDER_EN
:
1837 sscanf(buf
, "%d", &session
->pdu_inorder_en
);
1839 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
1840 sscanf(buf
, "%d", &session
->dataseq_inorder_en
);
1842 case ISCSI_PARAM_ERL
:
1843 sscanf(buf
, "%d", &session
->erl
);
1845 case ISCSI_PARAM_IFMARKER_EN
:
1846 sscanf(buf
, "%d", &value
);
1849 case ISCSI_PARAM_OFMARKER_EN
:
1850 sscanf(buf
, "%d", &value
);
1853 case ISCSI_PARAM_EXP_STATSN
:
1854 sscanf(buf
, "%u", &conn
->exp_statsn
);
1856 case ISCSI_PARAM_TARGET_NAME
:
1857 /* this should not change between logins */
1858 if (session
->targetname
)
1861 session
->targetname
= kstrdup(buf
, GFP_KERNEL
);
1862 if (!session
->targetname
)
1865 case ISCSI_PARAM_TPGT
:
1866 sscanf(buf
, "%d", &session
->tpgt
);
1868 case ISCSI_PARAM_PERSISTENT_PORT
:
1869 sscanf(buf
, "%d", &conn
->persistent_port
);
1871 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
1873 * this is the address returned in discovery so it should
1874 * not change between logins.
1876 if (conn
->persistent_address
)
1879 conn
->persistent_address
= kstrdup(buf
, GFP_KERNEL
);
1880 if (!conn
->persistent_address
)
1889 EXPORT_SYMBOL_GPL(iscsi_set_param
);
1891 int iscsi_session_get_param(struct iscsi_cls_session
*cls_session
,
1892 enum iscsi_param param
, char *buf
)
1894 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
1895 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
1899 case ISCSI_PARAM_INITIAL_R2T_EN
:
1900 len
= sprintf(buf
, "%d\n", session
->initial_r2t_en
);
1902 case ISCSI_PARAM_MAX_R2T
:
1903 len
= sprintf(buf
, "%hu\n", session
->max_r2t
);
1905 case ISCSI_PARAM_IMM_DATA_EN
:
1906 len
= sprintf(buf
, "%d\n", session
->imm_data_en
);
1908 case ISCSI_PARAM_FIRST_BURST
:
1909 len
= sprintf(buf
, "%u\n", session
->first_burst
);
1911 case ISCSI_PARAM_MAX_BURST
:
1912 len
= sprintf(buf
, "%u\n", session
->max_burst
);
1914 case ISCSI_PARAM_PDU_INORDER_EN
:
1915 len
= sprintf(buf
, "%d\n", session
->pdu_inorder_en
);
1917 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
1918 len
= sprintf(buf
, "%d\n", session
->dataseq_inorder_en
);
1920 case ISCSI_PARAM_ERL
:
1921 len
= sprintf(buf
, "%d\n", session
->erl
);
1923 case ISCSI_PARAM_TARGET_NAME
:
1924 len
= sprintf(buf
, "%s\n", session
->targetname
);
1926 case ISCSI_PARAM_TPGT
:
1927 len
= sprintf(buf
, "%d\n", session
->tpgt
);
1935 EXPORT_SYMBOL_GPL(iscsi_session_get_param
);
1937 int iscsi_conn_get_param(struct iscsi_cls_conn
*cls_conn
,
1938 enum iscsi_param param
, char *buf
)
1940 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1944 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
1945 len
= sprintf(buf
, "%u\n", conn
->max_recv_dlength
);
1947 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
1948 len
= sprintf(buf
, "%u\n", conn
->max_xmit_dlength
);
1950 case ISCSI_PARAM_HDRDGST_EN
:
1951 len
= sprintf(buf
, "%d\n", conn
->hdrdgst_en
);
1953 case ISCSI_PARAM_DATADGST_EN
:
1954 len
= sprintf(buf
, "%d\n", conn
->datadgst_en
);
1956 case ISCSI_PARAM_IFMARKER_EN
:
1957 len
= sprintf(buf
, "%d\n", conn
->ifmarker_en
);
1959 case ISCSI_PARAM_OFMARKER_EN
:
1960 len
= sprintf(buf
, "%d\n", conn
->ofmarker_en
);
1962 case ISCSI_PARAM_EXP_STATSN
:
1963 len
= sprintf(buf
, "%u\n", conn
->exp_statsn
);
1965 case ISCSI_PARAM_PERSISTENT_PORT
:
1966 len
= sprintf(buf
, "%d\n", conn
->persistent_port
);
1968 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
1969 len
= sprintf(buf
, "%s\n", conn
->persistent_address
);
1977 EXPORT_SYMBOL_GPL(iscsi_conn_get_param
);
1979 MODULE_AUTHOR("Mike Christie");
1980 MODULE_DESCRIPTION("iSCSI library functions");
1981 MODULE_LICENSE("GPL");