4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/types.h>
25 #include <linux/mutex.h>
26 #include <linux/kfifo.h>
27 #include <linux/delay.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi.h>
35 #include <scsi/iscsi_proto.h>
36 #include <scsi/scsi_transport.h>
37 #include <scsi/scsi_transport_iscsi.h>
38 #include <scsi/libiscsi.h>
40 struct iscsi_session
*
41 class_to_transport_session(struct iscsi_cls_session
*cls_session
)
43 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
44 return iscsi_hostdata(shost
->hostdata
);
46 EXPORT_SYMBOL_GPL(class_to_transport_session
);
48 #define INVALID_SN_DELTA 0xffff
51 iscsi_check_assign_cmdsn(struct iscsi_session
*session
, struct iscsi_nopin
*hdr
)
53 uint32_t max_cmdsn
= be32_to_cpu(hdr
->max_cmdsn
);
54 uint32_t exp_cmdsn
= be32_to_cpu(hdr
->exp_cmdsn
);
56 if (max_cmdsn
< exp_cmdsn
-1 &&
57 max_cmdsn
> exp_cmdsn
- INVALID_SN_DELTA
)
58 return ISCSI_ERR_MAX_CMDSN
;
59 if (max_cmdsn
> session
->max_cmdsn
||
60 max_cmdsn
< session
->max_cmdsn
- INVALID_SN_DELTA
)
61 session
->max_cmdsn
= max_cmdsn
;
62 if (exp_cmdsn
> session
->exp_cmdsn
||
63 exp_cmdsn
< session
->exp_cmdsn
- INVALID_SN_DELTA
)
64 session
->exp_cmdsn
= exp_cmdsn
;
68 EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn
);
70 void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task
*ctask
,
71 struct iscsi_data
*hdr
,
72 int transport_data_cnt
)
74 struct iscsi_conn
*conn
= ctask
->conn
;
76 memset(hdr
, 0, sizeof(struct iscsi_data
));
77 hdr
->ttt
= cpu_to_be32(ISCSI_RESERVED_TAG
);
78 hdr
->datasn
= cpu_to_be32(ctask
->unsol_datasn
);
79 ctask
->unsol_datasn
++;
80 hdr
->opcode
= ISCSI_OP_SCSI_DATA_OUT
;
81 memcpy(hdr
->lun
, ctask
->hdr
->lun
, sizeof(hdr
->lun
));
83 hdr
->itt
= ctask
->hdr
->itt
;
84 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
86 hdr
->offset
= cpu_to_be32(ctask
->total_length
-
90 if (ctask
->unsol_count
> conn
->max_xmit_dlength
) {
91 hton24(hdr
->dlength
, conn
->max_xmit_dlength
);
92 ctask
->data_count
= conn
->max_xmit_dlength
;
95 hton24(hdr
->dlength
, ctask
->unsol_count
);
96 ctask
->data_count
= ctask
->unsol_count
;
97 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
100 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu
);
103 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
104 * @ctask: iscsi cmd task
106 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
107 * fields like dlength or final based on how much data it sends
109 static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task
*ctask
)
111 struct iscsi_conn
*conn
= ctask
->conn
;
112 struct iscsi_session
*session
= conn
->session
;
113 struct iscsi_cmd
*hdr
= ctask
->hdr
;
114 struct scsi_cmnd
*sc
= ctask
->sc
;
116 hdr
->opcode
= ISCSI_OP_SCSI_CMD
;
117 hdr
->flags
= ISCSI_ATTR_SIMPLE
;
118 int_to_scsilun(sc
->device
->lun
, (struct scsi_lun
*)hdr
->lun
);
119 hdr
->itt
= ctask
->itt
| (conn
->id
<< ISCSI_CID_SHIFT
) |
120 (session
->age
<< ISCSI_AGE_SHIFT
);
121 hdr
->data_length
= cpu_to_be32(sc
->request_bufflen
);
122 hdr
->cmdsn
= cpu_to_be32(session
->cmdsn
);
124 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
125 memcpy(hdr
->cdb
, sc
->cmnd
, sc
->cmd_len
);
126 memset(&hdr
->cdb
[sc
->cmd_len
], 0, MAX_COMMAND_SIZE
- sc
->cmd_len
);
128 if (sc
->sc_data_direction
== DMA_TO_DEVICE
) {
129 hdr
->flags
|= ISCSI_FLAG_CMD_WRITE
;
133 * imm_count bytes to be sent right after
136 * unsol_count bytes(as Data-Out) to be sent
137 * without R2T ack right after
140 * r2t_data_count bytes to be sent via R2T ack's
142 * pad_count bytes to be sent as zero-padding
144 ctask
->imm_count
= 0;
145 ctask
->unsol_count
= 0;
146 ctask
->unsol_datasn
= 0;
148 if (session
->imm_data_en
) {
149 if (ctask
->total_length
>= session
->first_burst
)
150 ctask
->imm_count
= min(session
->first_burst
,
151 conn
->max_xmit_dlength
);
153 ctask
->imm_count
= min(ctask
->total_length
,
154 conn
->max_xmit_dlength
);
155 hton24(ctask
->hdr
->dlength
, ctask
->imm_count
);
157 zero_data(ctask
->hdr
->dlength
);
159 if (!session
->initial_r2t_en
)
160 ctask
->unsol_count
= min(session
->first_burst
,
161 ctask
->total_length
) - ctask
->imm_count
;
162 if (!ctask
->unsol_count
)
163 /* No unsolicit Data-Out's */
164 ctask
->hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
167 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
168 zero_data(hdr
->dlength
);
170 if (sc
->sc_data_direction
== DMA_FROM_DEVICE
)
171 hdr
->flags
|= ISCSI_FLAG_CMD_READ
;
174 conn
->scsicmd_pdus_cnt
++;
176 EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu
);
179 * iscsi_complete_command - return command back to scsi-ml
180 * @session: iscsi session
181 * @ctask: iscsi cmd task
183 * Must be called with session lock.
184 * This function returns the scsi command to scsi-ml and returns
185 * the cmd task to the pool of available cmd tasks.
187 static void iscsi_complete_command(struct iscsi_session
*session
,
188 struct iscsi_cmd_task
*ctask
)
190 struct scsi_cmnd
*sc
= ctask
->sc
;
192 ctask
->state
= ISCSI_TASK_COMPLETED
;
194 list_del_init(&ctask
->running
);
195 __kfifo_put(session
->cmdpool
.queue
, (void*)&ctask
, sizeof(void*));
200 * iscsi_cmd_rsp - SCSI Command Response processing
201 * @conn: iscsi connection
203 * @ctask: scsi command task
204 * @data: cmd data buffer
205 * @datalen: len of buffer
207 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
208 * then completes the command and task.
210 static int iscsi_scsi_cmd_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
211 struct iscsi_cmd_task
*ctask
, char *data
,
215 struct iscsi_cmd_rsp
*rhdr
= (struct iscsi_cmd_rsp
*)hdr
;
216 struct iscsi_session
*session
= conn
->session
;
217 struct scsi_cmnd
*sc
= ctask
->sc
;
219 rc
= iscsi_check_assign_cmdsn(session
, (struct iscsi_nopin
*)rhdr
);
221 sc
->result
= DID_ERROR
<< 16;
225 conn
->exp_statsn
= be32_to_cpu(rhdr
->statsn
) + 1;
227 sc
->result
= (DID_OK
<< 16) | rhdr
->cmd_status
;
229 if (rhdr
->response
!= ISCSI_STATUS_CMD_COMPLETED
) {
230 sc
->result
= DID_ERROR
<< 16;
234 if (rhdr
->cmd_status
== SAM_STAT_CHECK_CONDITION
) {
239 printk(KERN_ERR
"iscsi: Got CHECK_CONDITION but "
240 "invalid data buffer size of %d\n", datalen
);
241 sc
->result
= DID_BAD_TARGET
<< 16;
245 senselen
= (data
[0] << 8) | data
[1];
246 if (datalen
< senselen
)
247 goto invalid_datalen
;
249 memcpy(sc
->sense_buffer
, data
+ 2,
250 min(senselen
, SCSI_SENSE_BUFFERSIZE
));
251 debug_scsi("copied %d bytes of sense\n",
252 min(senselen
, SCSI_SENSE_BUFFERSIZE
));
255 if (sc
->sc_data_direction
== DMA_TO_DEVICE
)
258 if (rhdr
->flags
& ISCSI_FLAG_CMD_UNDERFLOW
) {
259 int res_count
= be32_to_cpu(rhdr
->residual_count
);
261 if (res_count
> 0 && res_count
<= sc
->request_bufflen
)
262 sc
->resid
= res_count
;
264 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
265 } else if (rhdr
->flags
& ISCSI_FLAG_CMD_BIDI_UNDERFLOW
)
266 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
267 else if (rhdr
->flags
& ISCSI_FLAG_CMD_OVERFLOW
)
268 sc
->resid
= be32_to_cpu(rhdr
->residual_count
);
271 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
272 (long)sc
, sc
->result
, ctask
->itt
);
273 conn
->scsirsp_pdus_cnt
++;
275 iscsi_complete_command(conn
->session
, ctask
);
279 static void iscsi_tmf_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
)
281 struct iscsi_tm_rsp
*tmf
= (struct iscsi_tm_rsp
*)hdr
;
283 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
284 conn
->tmfrsp_pdus_cnt
++;
286 if (conn
->tmabort_state
!= TMABORT_INITIAL
)
289 if (tmf
->response
== ISCSI_TMF_RSP_COMPLETE
)
290 conn
->tmabort_state
= TMABORT_SUCCESS
;
291 else if (tmf
->response
== ISCSI_TMF_RSP_NO_TASK
)
292 conn
->tmabort_state
= TMABORT_NOT_FOUND
;
294 conn
->tmabort_state
= TMABORT_FAILED
;
295 wake_up(&conn
->ehwait
);
299 * __iscsi_complete_pdu - complete pdu
303 * @datalen: len of data buffer
305 * Completes pdu processing by freeing any resources allocated at
306 * queuecommand or send generic. session lock must be held and verify
307 * itt must have been called.
309 int __iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
310 char *data
, int datalen
)
312 struct iscsi_session
*session
= conn
->session
;
313 int opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
, rc
= 0;
314 struct iscsi_cmd_task
*ctask
;
315 struct iscsi_mgmt_task
*mtask
;
318 if (hdr
->itt
!= cpu_to_be32(ISCSI_RESERVED_TAG
))
319 itt
= hdr
->itt
& ISCSI_ITT_MASK
;
323 if (itt
< session
->cmds_max
) {
324 ctask
= session
->cmds
[itt
];
326 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
327 opcode
, conn
->id
, ctask
->itt
, datalen
);
330 case ISCSI_OP_SCSI_CMD_RSP
:
331 BUG_ON((void*)ctask
!= ctask
->sc
->SCp
.ptr
);
332 rc
= iscsi_scsi_cmd_rsp(conn
, hdr
, ctask
, data
,
335 case ISCSI_OP_SCSI_DATA_IN
:
336 BUG_ON((void*)ctask
!= ctask
->sc
->SCp
.ptr
);
337 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
338 conn
->scsirsp_pdus_cnt
++;
339 iscsi_complete_command(session
, ctask
);
343 /* LLD handles this for now */
346 rc
= ISCSI_ERR_BAD_OPCODE
;
349 } else if (itt
>= ISCSI_MGMT_ITT_OFFSET
&&
350 itt
< ISCSI_MGMT_ITT_OFFSET
+ session
->mgmtpool_max
) {
351 mtask
= session
->mgmt_cmds
[itt
- ISCSI_MGMT_ITT_OFFSET
];
353 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
354 opcode
, conn
->id
, mtask
->itt
, datalen
);
356 rc
= iscsi_check_assign_cmdsn(session
,
357 (struct iscsi_nopin
*)hdr
);
362 case ISCSI_OP_LOGOUT_RSP
:
364 rc
= ISCSI_ERR_PROTO
;
367 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
369 case ISCSI_OP_LOGIN_RSP
:
370 case ISCSI_OP_TEXT_RSP
:
372 * login related PDU's exp_statsn is handled in
375 rc
= iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
);
376 list_del(&mtask
->running
);
377 if (conn
->login_mtask
!= mtask
)
378 __kfifo_put(session
->mgmtpool
.queue
,
379 (void*)&mtask
, sizeof(void*));
381 case ISCSI_OP_SCSI_TMFUNC_RSP
:
383 rc
= ISCSI_ERR_PROTO
;
387 iscsi_tmf_rsp(conn
, hdr
);
389 case ISCSI_OP_NOOP_IN
:
390 if (hdr
->ttt
!= ISCSI_RESERVED_TAG
|| datalen
) {
391 rc
= ISCSI_ERR_PROTO
;
394 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
396 rc
= iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
);
397 list_del(&mtask
->running
);
398 if (conn
->login_mtask
!= mtask
)
399 __kfifo_put(session
->mgmtpool
.queue
,
400 (void*)&mtask
, sizeof(void*));
403 rc
= ISCSI_ERR_BAD_OPCODE
;
406 } else if (itt
== ISCSI_RESERVED_TAG
) {
408 case ISCSI_OP_NOOP_IN
:
410 rc
= iscsi_check_assign_cmdsn(session
,
411 (struct iscsi_nopin
*)hdr
);
412 if (!rc
&& hdr
->ttt
!= ISCSI_RESERVED_TAG
)
413 rc
= iscsi_recv_pdu(conn
->cls_conn
,
416 rc
= ISCSI_ERR_PROTO
;
418 case ISCSI_OP_REJECT
:
419 /* we need sth like iscsi_reject_rsp()*/
420 case ISCSI_OP_ASYNC_EVENT
:
421 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
422 /* we need sth like iscsi_async_event_rsp() */
423 rc
= ISCSI_ERR_BAD_OPCODE
;
426 rc
= ISCSI_ERR_BAD_OPCODE
;
430 rc
= ISCSI_ERR_BAD_ITT
;
435 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu
);
437 int iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
438 char *data
, int datalen
)
442 spin_lock(&conn
->session
->lock
);
443 rc
= __iscsi_complete_pdu(conn
, hdr
, data
, datalen
);
444 spin_unlock(&conn
->session
->lock
);
447 EXPORT_SYMBOL_GPL(iscsi_complete_pdu
);
449 /* verify itt (itt encoding: age+cid+itt) */
450 int iscsi_verify_itt(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
453 struct iscsi_session
*session
= conn
->session
;
454 struct iscsi_cmd_task
*ctask
;
457 if (hdr
->itt
!= cpu_to_be32(ISCSI_RESERVED_TAG
)) {
458 if ((hdr
->itt
& ISCSI_AGE_MASK
) !=
459 (session
->age
<< ISCSI_AGE_SHIFT
)) {
460 printk(KERN_ERR
"iscsi: received itt %x expected "
461 "session age (%x)\n", hdr
->itt
,
462 session
->age
& ISCSI_AGE_MASK
);
463 return ISCSI_ERR_BAD_ITT
;
466 if ((hdr
->itt
& ISCSI_CID_MASK
) !=
467 (conn
->id
<< ISCSI_CID_SHIFT
)) {
468 printk(KERN_ERR
"iscsi: received itt %x, expected "
469 "CID (%x)\n", hdr
->itt
, conn
->id
);
470 return ISCSI_ERR_BAD_ITT
;
472 itt
= hdr
->itt
& ISCSI_ITT_MASK
;
476 if (itt
< session
->cmds_max
) {
477 ctask
= session
->cmds
[itt
];
480 printk(KERN_INFO
"iscsi: dropping ctask with "
481 "itt 0x%x\n", ctask
->itt
);
483 return ISCSI_ERR_NO_SCSI_CMD
;
486 if (ctask
->sc
->SCp
.phase
!= session
->age
) {
487 printk(KERN_ERR
"iscsi: ctask's session age %d, "
488 "expected %d\n", ctask
->sc
->SCp
.phase
,
490 return ISCSI_ERR_SESSION_FAILED
;
497 EXPORT_SYMBOL_GPL(iscsi_verify_itt
);
499 void iscsi_conn_failure(struct iscsi_conn
*conn
, enum iscsi_err err
)
501 struct iscsi_session
*session
= conn
->session
;
504 spin_lock_irqsave(&session
->lock
, flags
);
505 if (session
->state
== ISCSI_STATE_FAILED
) {
506 spin_unlock_irqrestore(&session
->lock
, flags
);
510 if (conn
->stop_stage
== 0)
511 session
->state
= ISCSI_STATE_FAILED
;
512 spin_unlock_irqrestore(&session
->lock
, flags
);
513 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
514 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
515 iscsi_conn_error(conn
->cls_conn
, err
);
517 EXPORT_SYMBOL_GPL(iscsi_conn_failure
);
520 * iscsi_data_xmit - xmit any command into the scheduled connection
521 * @conn: iscsi connection
524 * The function can return -EAGAIN in which case the caller must
525 * re-schedule it again later or recover. '0' return code means
528 static int iscsi_data_xmit(struct iscsi_conn
*conn
)
530 struct iscsi_transport
*tt
;
533 if (unlikely(conn
->suspend_tx
)) {
534 debug_scsi("conn %d Tx suspended!\n", conn
->id
);
537 tt
= conn
->session
->tt
;
540 * Transmit in the following order:
542 * 1) un-finished xmit (ctask or mtask)
543 * 2) immediate control PDUs
546 * 5) non-immediate control PDUs
548 * No need to lock around __kfifo_get as long as
549 * there's one producer and one consumer.
552 BUG_ON(conn
->ctask
&& conn
->mtask
);
555 rc
= tt
->xmit_cmd_task(conn
, conn
->ctask
);
558 /* done with this in-progress ctask */
562 rc
= tt
->xmit_mgmt_task(conn
, conn
->mtask
);
565 /* done with this in-progress mtask */
569 /* process immediate first */
570 if (unlikely(__kfifo_len(conn
->immqueue
))) {
571 while (__kfifo_get(conn
->immqueue
, (void*)&conn
->mtask
,
573 spin_lock_bh(&conn
->session
->lock
);
574 list_add_tail(&conn
->mtask
->running
,
575 &conn
->mgmt_run_list
);
576 spin_unlock_bh(&conn
->session
->lock
);
577 rc
= tt
->xmit_mgmt_task(conn
, conn
->mtask
);
581 /* done with this mtask */
585 /* process command queue */
586 spin_lock_bh(&conn
->session
->lock
);
587 while (!list_empty(&conn
->xmitqueue
)) {
589 * iscsi tcp may readd the task to the xmitqueue to send
592 conn
->ctask
= list_entry(conn
->xmitqueue
.next
,
593 struct iscsi_cmd_task
, running
);
594 conn
->ctask
->state
= ISCSI_TASK_RUNNING
;
595 list_move_tail(conn
->xmitqueue
.next
, &conn
->run_list
);
596 spin_unlock_bh(&conn
->session
->lock
);
598 rc
= tt
->xmit_cmd_task(conn
, conn
->ctask
);
601 spin_lock_bh(&conn
->session
->lock
);
603 spin_unlock_bh(&conn
->session
->lock
);
604 /* done with this ctask */
607 /* process the rest control plane PDUs, if any */
608 if (unlikely(__kfifo_len(conn
->mgmtqueue
))) {
609 while (__kfifo_get(conn
->mgmtqueue
, (void*)&conn
->mtask
,
611 spin_lock_bh(&conn
->session
->lock
);
612 list_add_tail(&conn
->mtask
->running
,
613 &conn
->mgmt_run_list
);
614 spin_unlock_bh(&conn
->session
->lock
);
615 rc
= tt
->xmit_mgmt_task(conn
, conn
->mtask
);
619 /* done with this mtask */
626 if (unlikely(conn
->suspend_tx
))
632 static void iscsi_xmitworker(void *data
)
634 struct iscsi_conn
*conn
= data
;
637 * serialize Xmit worker on a per-connection basis.
639 mutex_lock(&conn
->xmitmutex
);
641 rc
= iscsi_data_xmit(conn
);
642 } while (rc
>= 0 || rc
== -EAGAIN
);
643 mutex_unlock(&conn
->xmitmutex
);
647 FAILURE_BAD_HOST
= 1,
648 FAILURE_SESSION_FAILED
,
649 FAILURE_SESSION_FREED
,
650 FAILURE_WINDOW_CLOSED
,
651 FAILURE_SESSION_TERMINATE
,
652 FAILURE_SESSION_IN_RECOVERY
,
653 FAILURE_SESSION_RECOVERY_TIMEOUT
,
656 int iscsi_queuecommand(struct scsi_cmnd
*sc
, void (*done
)(struct scsi_cmnd
*))
658 struct Scsi_Host
*host
;
660 struct iscsi_session
*session
;
661 struct iscsi_conn
*conn
;
662 struct iscsi_cmd_task
*ctask
= NULL
;
664 sc
->scsi_done
= done
;
667 host
= sc
->device
->host
;
668 session
= iscsi_hostdata(host
->hostdata
);
670 spin_lock(&session
->lock
);
673 * ISCSI_STATE_FAILED is a temp. state. The recovery
674 * code will decide what is best to do with command queued
677 if (session
->state
!= ISCSI_STATE_LOGGED_IN
&&
678 session
->state
!= ISCSI_STATE_FAILED
) {
680 * to handle the race between when we set the recovery state
681 * and block the session we requeue here (commands could
682 * be entering our queuecommand while a block is starting
683 * up because the block code is not locked)
685 if (session
->state
== ISCSI_STATE_IN_RECOVERY
) {
686 reason
= FAILURE_SESSION_IN_RECOVERY
;
690 if (session
->state
== ISCSI_STATE_RECOVERY_FAILED
)
691 reason
= FAILURE_SESSION_RECOVERY_TIMEOUT
;
692 else if (session
->state
== ISCSI_STATE_TERMINATE
)
693 reason
= FAILURE_SESSION_TERMINATE
;
695 reason
= FAILURE_SESSION_FREED
;
700 * Check for iSCSI window and take care of CmdSN wrap-around
702 if ((int)(session
->max_cmdsn
- session
->cmdsn
) < 0) {
703 reason
= FAILURE_WINDOW_CLOSED
;
707 conn
= session
->leadconn
;
709 __kfifo_get(session
->cmdpool
.queue
, (void*)&ctask
, sizeof(void*));
710 sc
->SCp
.phase
= session
->age
;
711 sc
->SCp
.ptr
= (char *)ctask
;
713 ctask
->state
= ISCSI_TASK_PENDING
;
717 INIT_LIST_HEAD(&ctask
->running
);
718 ctask
->total_length
= sc
->request_bufflen
;
719 iscsi_prep_scsi_cmd_pdu(ctask
);
721 session
->tt
->init_cmd_task(ctask
);
723 list_add_tail(&ctask
->running
, &conn
->xmitqueue
);
725 "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
726 sc
->sc_data_direction
== DMA_TO_DEVICE
? "write" : "read",
727 conn
->id
, (long)sc
, ctask
->itt
, sc
->request_bufflen
,
728 session
->cmdsn
, session
->max_cmdsn
- session
->exp_cmdsn
+ 1);
729 spin_unlock(&session
->lock
);
731 scsi_queue_work(host
, &conn
->xmitwork
);
735 spin_unlock(&session
->lock
);
736 debug_scsi("cmd 0x%x rejected (%d)\n", sc
->cmnd
[0], reason
);
737 return SCSI_MLQUEUE_HOST_BUSY
;
740 spin_unlock(&session
->lock
);
741 printk(KERN_ERR
"iscsi: cmd 0x%x is not queued (%d)\n",
742 sc
->cmnd
[0], reason
);
743 sc
->result
= (DID_NO_CONNECT
<< 16);
744 sc
->resid
= sc
->request_bufflen
;
748 EXPORT_SYMBOL_GPL(iscsi_queuecommand
);
750 int iscsi_change_queue_depth(struct scsi_device
*sdev
, int depth
)
752 if (depth
> ISCSI_MAX_CMD_PER_LUN
)
753 depth
= ISCSI_MAX_CMD_PER_LUN
;
754 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), depth
);
755 return sdev
->queue_depth
;
757 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth
);
760 iscsi_conn_send_generic(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
761 char *data
, uint32_t data_size
)
763 struct iscsi_session
*session
= conn
->session
;
764 struct iscsi_nopout
*nop
= (struct iscsi_nopout
*)hdr
;
765 struct iscsi_mgmt_task
*mtask
;
767 spin_lock_bh(&session
->lock
);
768 if (session
->state
== ISCSI_STATE_TERMINATE
) {
769 spin_unlock_bh(&session
->lock
);
772 if (hdr
->opcode
== (ISCSI_OP_LOGIN
| ISCSI_OP_IMMEDIATE
) ||
773 hdr
->opcode
== (ISCSI_OP_TEXT
| ISCSI_OP_IMMEDIATE
))
775 * Login and Text are sent serially, in
776 * request-followed-by-response sequence.
777 * Same mtask can be used. Same ITT must be used.
778 * Note that login_mtask is preallocated at conn_create().
780 mtask
= conn
->login_mtask
;
782 BUG_ON(conn
->c_stage
== ISCSI_CONN_INITIAL_STAGE
);
783 BUG_ON(conn
->c_stage
== ISCSI_CONN_STOPPED
);
785 nop
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
786 if (!__kfifo_get(session
->mgmtpool
.queue
,
787 (void*)&mtask
, sizeof(void*))) {
788 spin_unlock_bh(&session
->lock
);
794 * pre-format CmdSN for outgoing PDU.
796 if (hdr
->itt
!= cpu_to_be32(ISCSI_RESERVED_TAG
)) {
797 hdr
->itt
= mtask
->itt
| (conn
->id
<< ISCSI_CID_SHIFT
) |
798 (session
->age
<< ISCSI_AGE_SHIFT
);
799 nop
->cmdsn
= cpu_to_be32(session
->cmdsn
);
800 if (conn
->c_stage
== ISCSI_CONN_STARTED
&&
801 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
))
804 /* do not advance CmdSN */
805 nop
->cmdsn
= cpu_to_be32(session
->cmdsn
);
808 memcpy(mtask
->data
, data
, data_size
);
809 mtask
->data_count
= data_size
;
811 mtask
->data_count
= 0;
813 INIT_LIST_HEAD(&mtask
->running
);
814 memcpy(mtask
->hdr
, hdr
, sizeof(struct iscsi_hdr
));
815 if (session
->tt
->init_mgmt_task
)
816 session
->tt
->init_mgmt_task(conn
, mtask
, data
, data_size
);
817 spin_unlock_bh(&session
->lock
);
819 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
820 hdr
->opcode
, hdr
->itt
, data_size
);
823 * since send_pdu() could be called at least from two contexts,
824 * we need to serialize __kfifo_put, so we don't have to take
825 * additional lock on fast data-path
827 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
)
828 __kfifo_put(conn
->immqueue
, (void*)&mtask
, sizeof(void*));
830 __kfifo_put(conn
->mgmtqueue
, (void*)&mtask
, sizeof(void*));
832 scsi_queue_work(session
->host
, &conn
->xmitwork
);
836 int iscsi_conn_send_pdu(struct iscsi_cls_conn
*cls_conn
, struct iscsi_hdr
*hdr
,
837 char *data
, uint32_t data_size
)
839 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
842 mutex_lock(&conn
->xmitmutex
);
843 rc
= iscsi_conn_send_generic(conn
, hdr
, data
, data_size
);
844 mutex_unlock(&conn
->xmitmutex
);
848 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu
);
850 void iscsi_session_recovery_timedout(struct iscsi_cls_session
*cls_session
)
852 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
853 struct iscsi_conn
*conn
= session
->leadconn
;
855 spin_lock_bh(&session
->lock
);
856 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
857 session
->state
= ISCSI_STATE_RECOVERY_FAILED
;
859 wake_up(&conn
->ehwait
);
861 spin_unlock_bh(&session
->lock
);
863 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout
);
865 int iscsi_eh_host_reset(struct scsi_cmnd
*sc
)
867 struct Scsi_Host
*host
= sc
->device
->host
;
868 struct iscsi_session
*session
= iscsi_hostdata(host
->hostdata
);
869 struct iscsi_conn
*conn
= session
->leadconn
;
870 int fail_session
= 0;
872 spin_lock_bh(&session
->lock
);
873 if (session
->state
== ISCSI_STATE_TERMINATE
) {
875 debug_scsi("failing host reset: session terminated "
876 "[CID %d age %d]", conn
->id
, session
->age
);
877 spin_unlock_bh(&session
->lock
);
881 if (sc
->SCp
.phase
== session
->age
) {
882 debug_scsi("failing connection CID %d due to SCSI host reset",
886 spin_unlock_bh(&session
->lock
);
889 * we drop the lock here but the leadconn cannot be destoyed while
890 * we are in the scsi eh
893 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
895 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
896 wait_event_interruptible(conn
->ehwait
,
897 session
->state
== ISCSI_STATE_TERMINATE
||
898 session
->state
== ISCSI_STATE_LOGGED_IN
||
899 session
->state
== ISCSI_STATE_RECOVERY_FAILED
);
900 if (signal_pending(current
))
901 flush_signals(current
);
903 spin_lock_bh(&session
->lock
);
904 if (session
->state
== ISCSI_STATE_LOGGED_IN
)
905 printk(KERN_INFO
"iscsi: host reset succeeded\n");
908 spin_unlock_bh(&session
->lock
);
912 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset
);
914 static void iscsi_tmabort_timedout(unsigned long data
)
916 struct iscsi_cmd_task
*ctask
= (struct iscsi_cmd_task
*)data
;
917 struct iscsi_conn
*conn
= ctask
->conn
;
918 struct iscsi_session
*session
= conn
->session
;
920 spin_lock(&session
->lock
);
921 if (conn
->tmabort_state
== TMABORT_INITIAL
) {
922 conn
->tmabort_state
= TMABORT_TIMEDOUT
;
923 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
924 ctask
->sc
, ctask
->itt
);
925 /* unblock eh_abort() */
926 wake_up(&conn
->ehwait
);
928 spin_unlock(&session
->lock
);
931 /* must be called with the mutex lock */
932 static int iscsi_exec_abort_task(struct scsi_cmnd
*sc
,
933 struct iscsi_cmd_task
*ctask
)
935 struct iscsi_conn
*conn
= ctask
->conn
;
936 struct iscsi_session
*session
= conn
->session
;
937 struct iscsi_tm
*hdr
= &conn
->tmhdr
;
941 * ctask timed out but session is OK requests must be serialized.
943 memset(hdr
, 0, sizeof(struct iscsi_tm
));
944 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
945 hdr
->flags
= ISCSI_TM_FUNC_ABORT_TASK
;
946 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
947 memcpy(hdr
->lun
, ctask
->hdr
->lun
, sizeof(hdr
->lun
));
948 hdr
->rtt
= ctask
->hdr
->itt
;
949 hdr
->refcmdsn
= ctask
->hdr
->cmdsn
;
951 rc
= iscsi_conn_send_generic(conn
, (struct iscsi_hdr
*)hdr
,
954 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
955 debug_scsi("abort sent failure [itt 0x%x] %d", ctask
->itt
, rc
);
959 debug_scsi("abort sent [itt 0x%x]\n", ctask
->itt
);
961 spin_lock_bh(&session
->lock
);
962 ctask
->mtask
= (struct iscsi_mgmt_task
*)
963 session
->mgmt_cmds
[(hdr
->itt
& ISCSI_ITT_MASK
) -
964 ISCSI_MGMT_ITT_OFFSET
];
966 if (conn
->tmabort_state
== TMABORT_INITIAL
) {
967 conn
->tmfcmd_pdus_cnt
++;
968 conn
->tmabort_timer
.expires
= 10*HZ
+ jiffies
;
969 conn
->tmabort_timer
.function
= iscsi_tmabort_timedout
;
970 conn
->tmabort_timer
.data
= (unsigned long)ctask
;
971 add_timer(&conn
->tmabort_timer
);
972 debug_scsi("abort set timeout [itt 0x%x]", ctask
->itt
);
974 spin_unlock_bh(&session
->lock
);
975 mutex_unlock(&conn
->xmitmutex
);
978 * block eh thread until:
982 * 3) session is terminated or restarted or userspace has
983 * given up on recovery
985 wait_event_interruptible(conn
->ehwait
,
986 sc
->SCp
.phase
!= session
->age
||
987 session
->state
!= ISCSI_STATE_LOGGED_IN
||
988 conn
->tmabort_state
!= TMABORT_INITIAL
);
989 if (signal_pending(current
))
990 flush_signals(current
);
991 del_timer_sync(&conn
->tmabort_timer
);
993 mutex_lock(&conn
->xmitmutex
);
998 * xmit mutex and session lock must be held
1000 static struct iscsi_mgmt_task
*
1001 iscsi_remove_mgmt_task(struct kfifo
*fifo
, uint32_t itt
)
1003 int i
, nr_tasks
= __kfifo_len(fifo
) / sizeof(void*);
1004 struct iscsi_mgmt_task
*task
;
1006 debug_scsi("searching %d tasks\n", nr_tasks
);
1008 for (i
= 0; i
< nr_tasks
; i
++) {
1009 __kfifo_get(fifo
, (void*)&task
, sizeof(void*));
1010 debug_scsi("check task %u\n", task
->itt
);
1012 if (task
->itt
== itt
) {
1013 debug_scsi("matched task\n");
1017 __kfifo_put(fifo
, (void*)&task
, sizeof(void*));
1022 static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task
*ctask
)
1024 struct iscsi_conn
*conn
= ctask
->conn
;
1025 struct iscsi_session
*session
= conn
->session
;
1030 if (!iscsi_remove_mgmt_task(conn
->immqueue
, ctask
->mtask
->itt
))
1031 list_del(&ctask
->mtask
->running
);
1032 __kfifo_put(session
->mgmtpool
.queue
, (void*)&ctask
->mtask
,
1034 ctask
->mtask
= NULL
;
1039 * session lock and xmitmutex must be held
1041 static void fail_command(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
,
1044 struct scsi_cmnd
*sc
;
1050 conn
->session
->tt
->cleanup_cmd_task(conn
, ctask
);
1051 iscsi_ctask_mtask_cleanup(ctask
);
1054 sc
->resid
= sc
->request_bufflen
;
1055 iscsi_complete_command(conn
->session
, ctask
);
1058 int iscsi_eh_abort(struct scsi_cmnd
*sc
)
1060 struct iscsi_cmd_task
*ctask
= (struct iscsi_cmd_task
*)sc
->SCp
.ptr
;
1061 struct iscsi_conn
*conn
= ctask
->conn
;
1062 struct iscsi_session
*session
= conn
->session
;
1065 conn
->eh_abort_cnt
++;
1066 debug_scsi("aborting [sc %p itt 0x%x]\n", sc
, ctask
->itt
);
1068 mutex_lock(&conn
->xmitmutex
);
1069 spin_lock_bh(&session
->lock
);
1072 * If we are not logged in or we have started a new session
1073 * then let the host reset code handle this
1075 if (session
->state
!= ISCSI_STATE_LOGGED_IN
||
1076 sc
->SCp
.phase
!= session
->age
)
1079 /* ctask completed before time out */
1081 spin_unlock_bh(&session
->lock
);
1082 debug_scsi("sc completed while abort in progress\n");
1083 goto success_rel_mutex
;
1086 /* what should we do here ? */
1087 if (conn
->ctask
== ctask
) {
1088 printk(KERN_INFO
"iscsi: sc %p itt 0x%x partially sent. "
1089 "Failing abort\n", sc
, ctask
->itt
);
1093 if (ctask
->state
== ISCSI_TASK_PENDING
)
1094 goto success_cleanup
;
1096 conn
->tmabort_state
= TMABORT_INITIAL
;
1098 spin_unlock_bh(&session
->lock
);
1099 rc
= iscsi_exec_abort_task(sc
, ctask
);
1100 spin_lock_bh(&session
->lock
);
1102 if (rc
|| sc
->SCp
.phase
!= session
->age
||
1103 session
->state
!= ISCSI_STATE_LOGGED_IN
)
1105 iscsi_ctask_mtask_cleanup(ctask
);
1107 switch (conn
->tmabort_state
) {
1108 case TMABORT_SUCCESS
:
1109 goto success_cleanup
;
1110 case TMABORT_NOT_FOUND
:
1112 /* ctask completed before tmf abort response */
1113 spin_unlock_bh(&session
->lock
);
1114 debug_scsi("sc completed while abort in progress\n");
1115 goto success_rel_mutex
;
1119 /* timedout or failed */
1120 spin_unlock_bh(&session
->lock
);
1121 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1122 spin_lock_bh(&session
->lock
);
1127 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc
, ctask
->itt
);
1128 spin_unlock_bh(&session
->lock
);
1131 * clean up task if aborted. we have the xmitmutex so grab
1132 * the recv lock as a writer
1134 write_lock_bh(conn
->recv_lock
);
1135 spin_lock(&session
->lock
);
1136 fail_command(conn
, ctask
, DID_ABORT
<< 16);
1137 spin_unlock(&session
->lock
);
1138 write_unlock_bh(conn
->recv_lock
);
1141 mutex_unlock(&conn
->xmitmutex
);
1145 spin_unlock_bh(&session
->lock
);
1146 mutex_unlock(&conn
->xmitmutex
);
1148 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc
, ctask
->itt
);
1151 EXPORT_SYMBOL_GPL(iscsi_eh_abort
);
1154 iscsi_pool_init(struct iscsi_queue
*q
, int max
, void ***items
, int item_size
)
1158 *items
= kmalloc(max
* sizeof(void*), GFP_KERNEL
);
1163 q
->pool
= kmalloc(max
* sizeof(void*), GFP_KERNEL
);
1164 if (q
->pool
== NULL
) {
1169 q
->queue
= kfifo_init((void*)q
->pool
, max
* sizeof(void*),
1171 if (q
->queue
== ERR_PTR(-ENOMEM
)) {
1177 for (i
= 0; i
< max
; i
++) {
1178 q
->pool
[i
] = kmalloc(item_size
, GFP_KERNEL
);
1179 if (q
->pool
[i
] == NULL
) {
1182 for (j
= 0; j
< i
; j
++)
1185 kfifo_free(q
->queue
);
1190 memset(q
->pool
[i
], 0, item_size
);
1191 (*items
)[i
] = q
->pool
[i
];
1192 __kfifo_put(q
->queue
, (void*)&q
->pool
[i
], sizeof(void*));
1196 EXPORT_SYMBOL_GPL(iscsi_pool_init
);
1198 void iscsi_pool_free(struct iscsi_queue
*q
, void **items
)
1202 for (i
= 0; i
< q
->max
; i
++)
1207 EXPORT_SYMBOL_GPL(iscsi_pool_free
);
1210 * iSCSI Session's hostdata organization:
1212 * *------------------* <== hostdata_session(host->hostdata)
1213 * | ptr to class sess|
1214 * |------------------| <== iscsi_hostdata(host->hostdata)
1216 * *------------------*
1219 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1220 _sz % sizeof(unsigned long))
1222 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1225 * iscsi_session_setup - create iscsi cls session and host and session
1226 * @scsit: scsi transport template
1227 * @iscsit: iscsi transport template
1228 * @initial_cmdsn: initial CmdSN
1229 * @hostno: host no allocated
1231 * This can be used by software iscsi_transports that allocate
1232 * a session per scsi host.
1234 struct iscsi_cls_session
*
1235 iscsi_session_setup(struct iscsi_transport
*iscsit
,
1236 struct scsi_transport_template
*scsit
,
1237 int cmd_task_size
, int mgmt_task_size
,
1238 uint32_t initial_cmdsn
, uint32_t *hostno
)
1240 struct Scsi_Host
*shost
;
1241 struct iscsi_session
*session
;
1242 struct iscsi_cls_session
*cls_session
;
1245 shost
= scsi_host_alloc(iscsit
->host_template
,
1246 hostdata_privsize(sizeof(*session
)));
1251 shost
->max_channel
= 0;
1252 shost
->max_lun
= iscsit
->max_lun
;
1253 shost
->max_cmd_len
= iscsit
->max_cmd_len
;
1254 shost
->transportt
= scsit
;
1255 shost
->transportt
->create_work_queue
= 1;
1256 *hostno
= shost
->host_no
;
1258 session
= iscsi_hostdata(shost
->hostdata
);
1259 memset(session
, 0, sizeof(struct iscsi_session
));
1260 session
->host
= shost
;
1261 session
->state
= ISCSI_STATE_FREE
;
1262 session
->mgmtpool_max
= ISCSI_MGMT_CMDS_MAX
;
1263 session
->cmds_max
= ISCSI_XMIT_CMDS_MAX
;
1264 session
->cmdsn
= initial_cmdsn
;
1265 session
->exp_cmdsn
= initial_cmdsn
+ 1;
1266 session
->max_cmdsn
= initial_cmdsn
+ 1;
1267 session
->max_r2t
= 1;
1268 session
->tt
= iscsit
;
1270 /* initialize SCSI PDU commands pool */
1271 if (iscsi_pool_init(&session
->cmdpool
, session
->cmds_max
,
1272 (void***)&session
->cmds
,
1273 cmd_task_size
+ sizeof(struct iscsi_cmd_task
)))
1274 goto cmdpool_alloc_fail
;
1276 /* pre-format cmds pool with ITT */
1277 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
1278 struct iscsi_cmd_task
*ctask
= session
->cmds
[cmd_i
];
1281 ctask
->dd_data
= &ctask
[1];
1283 INIT_LIST_HEAD(&ctask
->running
);
1286 spin_lock_init(&session
->lock
);
1287 INIT_LIST_HEAD(&session
->connections
);
1289 /* initialize immediate command pool */
1290 if (iscsi_pool_init(&session
->mgmtpool
, session
->mgmtpool_max
,
1291 (void***)&session
->mgmt_cmds
,
1292 mgmt_task_size
+ sizeof(struct iscsi_mgmt_task
)))
1293 goto mgmtpool_alloc_fail
;
1296 /* pre-format immediate cmds pool with ITT */
1297 for (cmd_i
= 0; cmd_i
< session
->mgmtpool_max
; cmd_i
++) {
1298 struct iscsi_mgmt_task
*mtask
= session
->mgmt_cmds
[cmd_i
];
1301 mtask
->dd_data
= &mtask
[1];
1302 mtask
->itt
= ISCSI_MGMT_ITT_OFFSET
+ cmd_i
;
1303 INIT_LIST_HEAD(&mtask
->running
);
1306 if (scsi_add_host(shost
, NULL
))
1309 if (!try_module_get(iscsit
->owner
))
1310 goto cls_session_fail
;
1312 cls_session
= iscsi_create_session(shost
, iscsit
, 0);
1315 *(unsigned long*)shost
->hostdata
= (unsigned long)cls_session
;
1320 module_put(iscsit
->owner
);
1322 scsi_remove_host(shost
);
1324 iscsi_pool_free(&session
->mgmtpool
, (void**)session
->mgmt_cmds
);
1325 mgmtpool_alloc_fail
:
1326 iscsi_pool_free(&session
->cmdpool
, (void**)session
->cmds
);
1328 scsi_host_put(shost
);
1331 EXPORT_SYMBOL_GPL(iscsi_session_setup
);
1334 * iscsi_session_teardown - destroy session, host, and cls_session
1337 * This can be used by software iscsi_transports that allocate
1338 * a session per scsi host.
1340 void iscsi_session_teardown(struct iscsi_cls_session
*cls_session
)
1342 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
1343 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
1344 struct module
*owner
= cls_session
->transport
->owner
;
1346 scsi_remove_host(shost
);
1348 iscsi_pool_free(&session
->mgmtpool
, (void**)session
->mgmt_cmds
);
1349 iscsi_pool_free(&session
->cmdpool
, (void**)session
->cmds
);
1351 iscsi_destroy_session(cls_session
);
1352 scsi_host_put(shost
);
1355 EXPORT_SYMBOL_GPL(iscsi_session_teardown
);
1358 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1359 * @cls_session: iscsi_cls_session
1362 struct iscsi_cls_conn
*
1363 iscsi_conn_setup(struct iscsi_cls_session
*cls_session
, uint32_t conn_idx
)
1365 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1366 struct iscsi_conn
*conn
;
1367 struct iscsi_cls_conn
*cls_conn
;
1370 cls_conn
= iscsi_create_conn(cls_session
, conn_idx
);
1373 conn
= cls_conn
->dd_data
;
1374 memset(conn
, 0, sizeof(*conn
));
1376 conn
->session
= session
;
1377 conn
->cls_conn
= cls_conn
;
1378 conn
->c_stage
= ISCSI_CONN_INITIAL_STAGE
;
1379 conn
->id
= conn_idx
;
1380 conn
->exp_statsn
= 0;
1381 conn
->tmabort_state
= TMABORT_INITIAL
;
1382 INIT_LIST_HEAD(&conn
->run_list
);
1383 INIT_LIST_HEAD(&conn
->mgmt_run_list
);
1384 INIT_LIST_HEAD(&conn
->xmitqueue
);
1386 /* initialize general immediate & non-immediate PDU commands queue */
1387 conn
->immqueue
= kfifo_alloc(session
->mgmtpool_max
* sizeof(void*),
1389 if (conn
->immqueue
== ERR_PTR(-ENOMEM
))
1390 goto immqueue_alloc_fail
;
1392 conn
->mgmtqueue
= kfifo_alloc(session
->mgmtpool_max
* sizeof(void*),
1394 if (conn
->mgmtqueue
== ERR_PTR(-ENOMEM
))
1395 goto mgmtqueue_alloc_fail
;
1397 INIT_WORK(&conn
->xmitwork
, iscsi_xmitworker
, conn
);
1399 /* allocate login_mtask used for the login/text sequences */
1400 spin_lock_bh(&session
->lock
);
1401 if (!__kfifo_get(session
->mgmtpool
.queue
,
1402 (void*)&conn
->login_mtask
,
1404 spin_unlock_bh(&session
->lock
);
1405 goto login_mtask_alloc_fail
;
1407 spin_unlock_bh(&session
->lock
);
1409 data
= kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH
, GFP_KERNEL
);
1411 goto login_mtask_data_alloc_fail
;
1412 conn
->login_mtask
->data
= conn
->data
= data
;
1414 init_timer(&conn
->tmabort_timer
);
1415 mutex_init(&conn
->xmitmutex
);
1416 init_waitqueue_head(&conn
->ehwait
);
1420 login_mtask_data_alloc_fail
:
1421 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->login_mtask
,
1423 login_mtask_alloc_fail
:
1424 kfifo_free(conn
->mgmtqueue
);
1425 mgmtqueue_alloc_fail
:
1426 kfifo_free(conn
->immqueue
);
1427 immqueue_alloc_fail
:
1428 iscsi_destroy_conn(cls_conn
);
1431 EXPORT_SYMBOL_GPL(iscsi_conn_setup
);
1434 * iscsi_conn_teardown - teardown iscsi connection
1435 * cls_conn: iscsi class connection
1437 * TODO: we may need to make this into a two step process
1438 * like scsi-mls remove + put host
1440 void iscsi_conn_teardown(struct iscsi_cls_conn
*cls_conn
)
1442 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1443 struct iscsi_session
*session
= conn
->session
;
1444 unsigned long flags
;
1446 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1447 mutex_lock(&conn
->xmitmutex
);
1449 spin_lock_bh(&session
->lock
);
1450 conn
->c_stage
= ISCSI_CONN_CLEANUP_WAIT
;
1451 if (session
->leadconn
== conn
) {
1453 * leading connection? then give up on recovery.
1455 session
->state
= ISCSI_STATE_TERMINATE
;
1456 wake_up(&conn
->ehwait
);
1458 spin_unlock_bh(&session
->lock
);
1460 mutex_unlock(&conn
->xmitmutex
);
1463 * Block until all in-progress commands for this connection
1467 spin_lock_irqsave(session
->host
->host_lock
, flags
);
1468 if (!session
->host
->host_busy
) { /* OK for ERL == 0 */
1469 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
1472 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
1473 msleep_interruptible(500);
1474 printk(KERN_INFO
"iscsi: scsi conn_destroy(): host_busy %d "
1475 "host_failed %d\n", session
->host
->host_busy
,
1476 session
->host
->host_failed
);
1478 * force eh_abort() to unblock
1480 wake_up(&conn
->ehwait
);
1483 spin_lock_bh(&session
->lock
);
1485 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->login_mtask
,
1487 list_del(&conn
->item
);
1488 if (list_empty(&session
->connections
))
1489 session
->leadconn
= NULL
;
1490 if (session
->leadconn
&& session
->leadconn
== conn
)
1491 session
->leadconn
= container_of(session
->connections
.next
,
1492 struct iscsi_conn
, item
);
1494 if (session
->leadconn
== NULL
)
1495 /* no connections exits.. reset sequencing */
1496 session
->cmdsn
= session
->max_cmdsn
= session
->exp_cmdsn
= 1;
1497 spin_unlock_bh(&session
->lock
);
1499 kfifo_free(conn
->immqueue
);
1500 kfifo_free(conn
->mgmtqueue
);
1502 iscsi_destroy_conn(cls_conn
);
1504 EXPORT_SYMBOL_GPL(iscsi_conn_teardown
);
1506 int iscsi_conn_start(struct iscsi_cls_conn
*cls_conn
)
1508 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1509 struct iscsi_session
*session
= conn
->session
;
1511 if (session
== NULL
) {
1512 printk(KERN_ERR
"iscsi: can't start unbound connection\n");
1516 spin_lock_bh(&session
->lock
);
1517 conn
->c_stage
= ISCSI_CONN_STARTED
;
1518 session
->state
= ISCSI_STATE_LOGGED_IN
;
1520 switch(conn
->stop_stage
) {
1521 case STOP_CONN_RECOVER
:
1523 * unblock eh_abort() if it is blocked. re-try all
1524 * commands after successful recovery
1526 conn
->stop_stage
= 0;
1527 conn
->tmabort_state
= TMABORT_INITIAL
;
1529 spin_unlock_bh(&session
->lock
);
1531 iscsi_unblock_session(session_to_cls(session
));
1532 wake_up(&conn
->ehwait
);
1534 case STOP_CONN_TERM
:
1535 conn
->stop_stage
= 0;
1540 spin_unlock_bh(&session
->lock
);
1544 EXPORT_SYMBOL_GPL(iscsi_conn_start
);
1547 flush_control_queues(struct iscsi_session
*session
, struct iscsi_conn
*conn
)
1549 struct iscsi_mgmt_task
*mtask
, *tmp
;
1551 /* handle pending */
1552 while (__kfifo_get(conn
->immqueue
, (void*)&mtask
, sizeof(void*)) ||
1553 __kfifo_get(conn
->mgmtqueue
, (void*)&mtask
, sizeof(void*))) {
1554 if (mtask
== conn
->login_mtask
)
1556 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask
->itt
);
1557 __kfifo_put(session
->mgmtpool
.queue
, (void*)&mtask
,
1561 /* handle running */
1562 list_for_each_entry_safe(mtask
, tmp
, &conn
->mgmt_run_list
, running
) {
1563 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask
->itt
);
1564 list_del(&mtask
->running
);
1566 if (mtask
== conn
->login_mtask
)
1568 __kfifo_put(session
->mgmtpool
.queue
, (void*)&mtask
,
1575 /* Fail commands. Mutex and session lock held and recv side suspended */
1576 static void fail_all_commands(struct iscsi_conn
*conn
)
1578 struct iscsi_cmd_task
*ctask
, *tmp
;
1581 list_for_each_entry_safe(ctask
, tmp
, &conn
->xmitqueue
, running
) {
1582 debug_scsi("failing pending sc %p itt 0x%x\n", ctask
->sc
,
1584 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1587 /* fail all other running */
1588 list_for_each_entry_safe(ctask
, tmp
, &conn
->run_list
, running
) {
1589 debug_scsi("failing in progress sc %p itt 0x%x\n",
1590 ctask
->sc
, ctask
->itt
);
1591 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1597 static void iscsi_start_session_recovery(struct iscsi_session
*session
,
1598 struct iscsi_conn
*conn
, int flag
)
1602 spin_lock_bh(&session
->lock
);
1603 if (conn
->stop_stage
== STOP_CONN_TERM
) {
1604 spin_unlock_bh(&session
->lock
);
1609 * When this is called for the in_login state, we only want to clean
1610 * up the login task and connection. We do not need to block and set
1611 * the recovery state again
1613 if (flag
== STOP_CONN_TERM
)
1614 session
->state
= ISCSI_STATE_TERMINATE
;
1615 else if (conn
->stop_stage
!= STOP_CONN_RECOVER
)
1616 session
->state
= ISCSI_STATE_IN_RECOVERY
;
1618 old_stop_stage
= conn
->stop_stage
;
1619 conn
->stop_stage
= flag
;
1620 conn
->c_stage
= ISCSI_CONN_STOPPED
;
1621 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1622 spin_unlock_bh(&session
->lock
);
1624 write_lock_bh(conn
->recv_lock
);
1625 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
1626 write_unlock_bh(conn
->recv_lock
);
1628 mutex_lock(&conn
->xmitmutex
);
1630 * for connection level recovery we should not calculate
1631 * header digest. conn->hdr_size used for optimization
1632 * in hdr_extract() and will be re-negotiated at
1635 if (flag
== STOP_CONN_RECOVER
) {
1636 conn
->hdrdgst_en
= 0;
1637 conn
->datadgst_en
= 0;
1638 if (session
->state
== ISCSI_STATE_IN_RECOVERY
&&
1639 old_stop_stage
!= STOP_CONN_RECOVER
) {
1640 debug_scsi("blocking session\n");
1641 iscsi_block_session(session_to_cls(session
));
1648 spin_lock_bh(&session
->lock
);
1649 fail_all_commands(conn
);
1650 flush_control_queues(session
, conn
);
1651 spin_unlock_bh(&session
->lock
);
1653 mutex_unlock(&conn
->xmitmutex
);
1656 void iscsi_conn_stop(struct iscsi_cls_conn
*cls_conn
, int flag
)
1658 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1659 struct iscsi_session
*session
= conn
->session
;
1662 case STOP_CONN_RECOVER
:
1663 case STOP_CONN_TERM
:
1664 iscsi_start_session_recovery(session
, conn
, flag
);
1667 printk(KERN_ERR
"iscsi: invalid stop flag %d\n", flag
);
1670 EXPORT_SYMBOL_GPL(iscsi_conn_stop
);
1672 int iscsi_conn_bind(struct iscsi_cls_session
*cls_session
,
1673 struct iscsi_cls_conn
*cls_conn
, int is_leading
)
1675 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1676 struct iscsi_conn
*tmp
= ERR_PTR(-EEXIST
), *conn
= cls_conn
->dd_data
;
1678 /* lookup for existing connection */
1679 spin_lock_bh(&session
->lock
);
1680 list_for_each_entry(tmp
, &session
->connections
, item
) {
1682 if (conn
->c_stage
!= ISCSI_CONN_STOPPED
||
1683 conn
->stop_stage
== STOP_CONN_TERM
) {
1684 printk(KERN_ERR
"iscsi: can't bind "
1685 "non-stopped connection (%d:%d)\n",
1686 conn
->c_stage
, conn
->stop_stage
);
1687 spin_unlock_bh(&session
->lock
);
1694 /* bind new iSCSI connection to session */
1695 conn
->session
= session
;
1696 list_add(&conn
->item
, &session
->connections
);
1698 spin_unlock_bh(&session
->lock
);
1701 session
->leadconn
= conn
;
1704 * Unblock xmitworker(), Login Phase will pass through.
1706 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
1707 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1710 EXPORT_SYMBOL_GPL(iscsi_conn_bind
);
1713 int iscsi_set_param(struct iscsi_cls_conn
*cls_conn
,
1714 enum iscsi_param param
, char *buf
, int buflen
)
1716 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1717 struct iscsi_session
*session
= conn
->session
;
1721 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
1722 sscanf(buf
, "%d", &conn
->max_recv_dlength
);
1724 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
1725 sscanf(buf
, "%d", &conn
->max_xmit_dlength
);
1727 case ISCSI_PARAM_HDRDGST_EN
:
1728 sscanf(buf
, "%d", &conn
->hdrdgst_en
);
1730 case ISCSI_PARAM_DATADGST_EN
:
1731 sscanf(buf
, "%d", &conn
->datadgst_en
);
1733 case ISCSI_PARAM_INITIAL_R2T_EN
:
1734 sscanf(buf
, "%d", &session
->initial_r2t_en
);
1736 case ISCSI_PARAM_MAX_R2T
:
1737 sscanf(buf
, "%d", &session
->max_r2t
);
1739 case ISCSI_PARAM_IMM_DATA_EN
:
1740 sscanf(buf
, "%d", &session
->imm_data_en
);
1742 case ISCSI_PARAM_FIRST_BURST
:
1743 sscanf(buf
, "%d", &session
->first_burst
);
1745 case ISCSI_PARAM_MAX_BURST
:
1746 sscanf(buf
, "%d", &session
->max_burst
);
1748 case ISCSI_PARAM_PDU_INORDER_EN
:
1749 sscanf(buf
, "%d", &session
->pdu_inorder_en
);
1751 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
1752 sscanf(buf
, "%d", &session
->dataseq_inorder_en
);
1754 case ISCSI_PARAM_ERL
:
1755 sscanf(buf
, "%d", &session
->erl
);
1757 case ISCSI_PARAM_IFMARKER_EN
:
1758 sscanf(buf
, "%d", &value
);
1761 case ISCSI_PARAM_OFMARKER_EN
:
1762 sscanf(buf
, "%d", &value
);
1765 case ISCSI_PARAM_EXP_STATSN
:
1766 sscanf(buf
, "%u", &conn
->exp_statsn
);
1768 case ISCSI_PARAM_TARGET_NAME
:
1769 /* this should not change between logins */
1770 if (session
->targetname
)
1773 session
->targetname
= kstrdup(buf
, GFP_KERNEL
);
1774 if (!session
->targetname
)
1777 case ISCSI_PARAM_TPGT
:
1778 sscanf(buf
, "%d", &session
->tpgt
);
1780 case ISCSI_PARAM_PERSISTENT_PORT
:
1781 sscanf(buf
, "%d", &conn
->persistent_port
);
1783 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
1785 * this is the address returned in discovery so it should
1786 * not change between logins.
1788 if (conn
->persistent_address
)
1791 conn
->persistent_address
= kstrdup(buf
, GFP_KERNEL
);
1792 if (!conn
->persistent_address
)
1801 EXPORT_SYMBOL_GPL(iscsi_set_param
);
1803 int iscsi_session_get_param(struct iscsi_cls_session
*cls_session
,
1804 enum iscsi_param param
, char *buf
)
1806 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
1807 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
1811 case ISCSI_PARAM_INITIAL_R2T_EN
:
1812 len
= sprintf(buf
, "%d\n", session
->initial_r2t_en
);
1814 case ISCSI_PARAM_MAX_R2T
:
1815 len
= sprintf(buf
, "%hu\n", session
->max_r2t
);
1817 case ISCSI_PARAM_IMM_DATA_EN
:
1818 len
= sprintf(buf
, "%d\n", session
->imm_data_en
);
1820 case ISCSI_PARAM_FIRST_BURST
:
1821 len
= sprintf(buf
, "%u\n", session
->first_burst
);
1823 case ISCSI_PARAM_MAX_BURST
:
1824 len
= sprintf(buf
, "%u\n", session
->max_burst
);
1826 case ISCSI_PARAM_PDU_INORDER_EN
:
1827 len
= sprintf(buf
, "%d\n", session
->pdu_inorder_en
);
1829 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
1830 len
= sprintf(buf
, "%d\n", session
->dataseq_inorder_en
);
1832 case ISCSI_PARAM_ERL
:
1833 len
= sprintf(buf
, "%d\n", session
->erl
);
1835 case ISCSI_PARAM_TARGET_NAME
:
1836 len
= sprintf(buf
, "%s\n", session
->targetname
);
1838 case ISCSI_PARAM_TPGT
:
1839 len
= sprintf(buf
, "%d\n", session
->tpgt
);
1847 EXPORT_SYMBOL_GPL(iscsi_session_get_param
);
1849 int iscsi_conn_get_param(struct iscsi_cls_conn
*cls_conn
,
1850 enum iscsi_param param
, char *buf
)
1852 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1856 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
1857 len
= sprintf(buf
, "%u\n", conn
->max_recv_dlength
);
1859 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
1860 len
= sprintf(buf
, "%u\n", conn
->max_xmit_dlength
);
1862 case ISCSI_PARAM_HDRDGST_EN
:
1863 len
= sprintf(buf
, "%d\n", conn
->hdrdgst_en
);
1865 case ISCSI_PARAM_DATADGST_EN
:
1866 len
= sprintf(buf
, "%d\n", conn
->datadgst_en
);
1868 case ISCSI_PARAM_IFMARKER_EN
:
1869 len
= sprintf(buf
, "%d\n", conn
->ifmarker_en
);
1871 case ISCSI_PARAM_OFMARKER_EN
:
1872 len
= sprintf(buf
, "%d\n", conn
->ofmarker_en
);
1874 case ISCSI_PARAM_EXP_STATSN
:
1875 len
= sprintf(buf
, "%u\n", conn
->exp_statsn
);
1877 case ISCSI_PARAM_PERSISTENT_PORT
:
1878 len
= sprintf(buf
, "%d\n", conn
->persistent_port
);
1880 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
1881 len
= sprintf(buf
, "%s\n", conn
->persistent_address
);
1889 EXPORT_SYMBOL_GPL(iscsi_conn_get_param
);
1891 MODULE_AUTHOR("Mike Christie");
1892 MODULE_DESCRIPTION("iSCSI library functions");
1893 MODULE_LICENSE("GPL");