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
= build_itt(ctask
->itt
, conn
->id
, session
->age
);
117 hdr
->data_length
= cpu_to_be32(sc
->request_bufflen
);
118 hdr
->cmdsn
= cpu_to_be32(session
->cmdsn
);
120 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
121 memcpy(hdr
->cdb
, sc
->cmnd
, sc
->cmd_len
);
122 memset(&hdr
->cdb
[sc
->cmd_len
], 0, MAX_COMMAND_SIZE
- sc
->cmd_len
);
124 ctask
->data_count
= 0;
125 if (sc
->sc_data_direction
== DMA_TO_DEVICE
) {
126 hdr
->flags
|= ISCSI_FLAG_CMD_WRITE
;
130 * imm_count bytes to be sent right after
133 * unsol_count bytes(as Data-Out) to be sent
134 * without R2T ack right after
137 * r2t_data_count bytes to be sent via R2T ack's
139 * pad_count bytes to be sent as zero-padding
141 ctask
->imm_count
= 0;
142 ctask
->unsol_count
= 0;
143 ctask
->unsol_offset
= 0;
144 ctask
->unsol_datasn
= 0;
146 if (session
->imm_data_en
) {
147 if (ctask
->total_length
>= session
->first_burst
)
148 ctask
->imm_count
= min(session
->first_burst
,
149 conn
->max_xmit_dlength
);
151 ctask
->imm_count
= min(ctask
->total_length
,
152 conn
->max_xmit_dlength
);
153 hton24(ctask
->hdr
->dlength
, ctask
->imm_count
);
155 zero_data(ctask
->hdr
->dlength
);
157 if (!session
->initial_r2t_en
) {
158 ctask
->unsol_count
= min(session
->first_burst
,
159 ctask
->total_length
) - ctask
->imm_count
;
160 ctask
->unsol_offset
= ctask
->imm_count
;
163 if (!ctask
->unsol_count
)
164 /* No unsolicit Data-Out's */
165 ctask
->hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
168 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
169 zero_data(hdr
->dlength
);
171 if (sc
->sc_data_direction
== DMA_FROM_DEVICE
)
172 hdr
->flags
|= ISCSI_FLAG_CMD_READ
;
175 conn
->scsicmd_pdus_cnt
++;
177 EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu
);
180 * iscsi_complete_command - return command back to scsi-ml
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_cmd_task
*ctask
)
189 struct iscsi_session
*session
= ctask
->conn
->session
;
190 struct scsi_cmnd
*sc
= ctask
->sc
;
192 ctask
->state
= ISCSI_TASK_COMPLETED
;
194 /* SCSI eh reuses commands to verify us */
196 list_del_init(&ctask
->running
);
197 __kfifo_put(session
->cmdpool
.queue
, (void*)&ctask
, sizeof(void*));
201 static void __iscsi_get_ctask(struct iscsi_cmd_task
*ctask
)
203 atomic_inc(&ctask
->refcount
);
206 static void iscsi_get_ctask(struct iscsi_cmd_task
*ctask
)
208 spin_lock_bh(&ctask
->conn
->session
->lock
);
209 __iscsi_get_ctask(ctask
);
210 spin_unlock_bh(&ctask
->conn
->session
->lock
);
213 static void __iscsi_put_ctask(struct iscsi_cmd_task
*ctask
)
215 if (atomic_dec_and_test(&ctask
->refcount
))
216 iscsi_complete_command(ctask
);
219 static void iscsi_put_ctask(struct iscsi_cmd_task
*ctask
)
221 spin_lock_bh(&ctask
->conn
->session
->lock
);
222 __iscsi_put_ctask(ctask
);
223 spin_unlock_bh(&ctask
->conn
->session
->lock
);
227 * iscsi_cmd_rsp - SCSI Command Response processing
228 * @conn: iscsi connection
230 * @ctask: scsi command task
231 * @data: cmd data buffer
232 * @datalen: len of buffer
234 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
235 * then completes the command and task.
237 static int iscsi_scsi_cmd_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
238 struct iscsi_cmd_task
*ctask
, char *data
,
242 struct iscsi_cmd_rsp
*rhdr
= (struct iscsi_cmd_rsp
*)hdr
;
243 struct iscsi_session
*session
= conn
->session
;
244 struct scsi_cmnd
*sc
= ctask
->sc
;
246 rc
= iscsi_check_assign_cmdsn(session
, (struct iscsi_nopin
*)rhdr
);
248 sc
->result
= DID_ERROR
<< 16;
252 conn
->exp_statsn
= be32_to_cpu(rhdr
->statsn
) + 1;
254 sc
->result
= (DID_OK
<< 16) | rhdr
->cmd_status
;
256 if (rhdr
->response
!= ISCSI_STATUS_CMD_COMPLETED
) {
257 sc
->result
= DID_ERROR
<< 16;
261 if (rhdr
->cmd_status
== SAM_STAT_CHECK_CONDITION
) {
266 printk(KERN_ERR
"iscsi: Got CHECK_CONDITION but "
267 "invalid data buffer size of %d\n", datalen
);
268 sc
->result
= DID_BAD_TARGET
<< 16;
272 senselen
= be16_to_cpu(*(__be16
*)data
);
273 if (datalen
< senselen
)
274 goto invalid_datalen
;
276 memcpy(sc
->sense_buffer
, data
+ 2,
277 min_t(uint16_t, senselen
, SCSI_SENSE_BUFFERSIZE
));
278 debug_scsi("copied %d bytes of sense\n",
279 min(senselen
, SCSI_SENSE_BUFFERSIZE
));
282 if (sc
->sc_data_direction
== DMA_TO_DEVICE
)
285 if (rhdr
->flags
& ISCSI_FLAG_CMD_UNDERFLOW
) {
286 int res_count
= be32_to_cpu(rhdr
->residual_count
);
288 if (res_count
> 0 && res_count
<= sc
->request_bufflen
)
289 sc
->resid
= res_count
;
291 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
292 } else if (rhdr
->flags
& ISCSI_FLAG_CMD_BIDI_UNDERFLOW
)
293 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
294 else if (rhdr
->flags
& ISCSI_FLAG_CMD_OVERFLOW
)
295 sc
->resid
= be32_to_cpu(rhdr
->residual_count
);
298 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
299 (long)sc
, sc
->result
, ctask
->itt
);
300 conn
->scsirsp_pdus_cnt
++;
302 __iscsi_put_ctask(ctask
);
306 static void iscsi_tmf_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
)
308 struct iscsi_tm_rsp
*tmf
= (struct iscsi_tm_rsp
*)hdr
;
310 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
311 conn
->tmfrsp_pdus_cnt
++;
313 if (conn
->tmabort_state
!= TMABORT_INITIAL
)
316 if (tmf
->response
== ISCSI_TMF_RSP_COMPLETE
)
317 conn
->tmabort_state
= TMABORT_SUCCESS
;
318 else if (tmf
->response
== ISCSI_TMF_RSP_NO_TASK
)
319 conn
->tmabort_state
= TMABORT_NOT_FOUND
;
321 conn
->tmabort_state
= TMABORT_FAILED
;
322 wake_up(&conn
->ehwait
);
325 static int iscsi_handle_reject(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
326 char *data
, int datalen
)
328 struct iscsi_reject
*reject
= (struct iscsi_reject
*)hdr
;
329 struct iscsi_hdr rejected_pdu
;
332 conn
->exp_statsn
= be32_to_cpu(reject
->statsn
) + 1;
334 if (reject
->reason
== ISCSI_REASON_DATA_DIGEST_ERROR
) {
335 if (ntoh24(reject
->dlength
) > datalen
)
336 return ISCSI_ERR_PROTO
;
338 if (ntoh24(reject
->dlength
) >= sizeof(struct iscsi_hdr
)) {
339 memcpy(&rejected_pdu
, data
, sizeof(struct iscsi_hdr
));
340 itt
= get_itt(rejected_pdu
.itt
);
341 printk(KERN_ERR
"itt 0x%x had pdu (op 0x%x) rejected "
342 "due to DataDigest error.\n", itt
,
343 rejected_pdu
.opcode
);
350 * __iscsi_complete_pdu - complete pdu
354 * @datalen: len of data buffer
356 * Completes pdu processing by freeing any resources allocated at
357 * queuecommand or send generic. session lock must be held and verify
358 * itt must have been called.
360 int __iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
361 char *data
, int datalen
)
363 struct iscsi_session
*session
= conn
->session
;
364 int opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
, rc
= 0;
365 struct iscsi_cmd_task
*ctask
;
366 struct iscsi_mgmt_task
*mtask
;
369 if (hdr
->itt
!= RESERVED_ITT
)
370 itt
= get_itt(hdr
->itt
);
374 if (itt
< session
->cmds_max
) {
375 ctask
= session
->cmds
[itt
];
377 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
378 opcode
, conn
->id
, ctask
->itt
, datalen
);
381 case ISCSI_OP_SCSI_CMD_RSP
:
382 BUG_ON((void*)ctask
!= ctask
->sc
->SCp
.ptr
);
383 rc
= iscsi_scsi_cmd_rsp(conn
, hdr
, ctask
, data
,
386 case ISCSI_OP_SCSI_DATA_IN
:
387 BUG_ON((void*)ctask
!= ctask
->sc
->SCp
.ptr
);
388 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
389 conn
->scsirsp_pdus_cnt
++;
390 __iscsi_put_ctask(ctask
);
394 /* LLD handles this for now */
397 rc
= ISCSI_ERR_BAD_OPCODE
;
400 } else if (itt
>= ISCSI_MGMT_ITT_OFFSET
&&
401 itt
< ISCSI_MGMT_ITT_OFFSET
+ session
->mgmtpool_max
) {
402 mtask
= session
->mgmt_cmds
[itt
- ISCSI_MGMT_ITT_OFFSET
];
404 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
405 opcode
, conn
->id
, mtask
->itt
, datalen
);
407 rc
= iscsi_check_assign_cmdsn(session
,
408 (struct iscsi_nopin
*)hdr
);
413 case ISCSI_OP_LOGOUT_RSP
:
415 rc
= ISCSI_ERR_PROTO
;
418 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
420 case ISCSI_OP_LOGIN_RSP
:
421 case ISCSI_OP_TEXT_RSP
:
423 * login related PDU's exp_statsn is handled in
426 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
427 rc
= ISCSI_ERR_CONN_FAILED
;
428 list_del(&mtask
->running
);
429 if (conn
->login_mtask
!= mtask
)
430 __kfifo_put(session
->mgmtpool
.queue
,
431 (void*)&mtask
, sizeof(void*));
433 case ISCSI_OP_SCSI_TMFUNC_RSP
:
435 rc
= ISCSI_ERR_PROTO
;
439 iscsi_tmf_rsp(conn
, hdr
);
441 case ISCSI_OP_NOOP_IN
:
442 if (hdr
->ttt
!= cpu_to_be32(ISCSI_RESERVED_TAG
) || datalen
) {
443 rc
= ISCSI_ERR_PROTO
;
446 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
448 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
449 rc
= ISCSI_ERR_CONN_FAILED
;
450 list_del(&mtask
->running
);
451 if (conn
->login_mtask
!= mtask
)
452 __kfifo_put(session
->mgmtpool
.queue
,
453 (void*)&mtask
, sizeof(void*));
456 rc
= ISCSI_ERR_BAD_OPCODE
;
459 } else if (itt
== ~0U) {
460 rc
= iscsi_check_assign_cmdsn(session
,
461 (struct iscsi_nopin
*)hdr
);
466 case ISCSI_OP_NOOP_IN
:
468 rc
= ISCSI_ERR_PROTO
;
472 if (hdr
->ttt
== cpu_to_be32(ISCSI_RESERVED_TAG
))
475 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, NULL
, 0))
476 rc
= ISCSI_ERR_CONN_FAILED
;
478 case ISCSI_OP_REJECT
:
479 rc
= iscsi_handle_reject(conn
, hdr
, data
, datalen
);
481 case ISCSI_OP_ASYNC_EVENT
:
482 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
483 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
484 rc
= ISCSI_ERR_CONN_FAILED
;
487 rc
= ISCSI_ERR_BAD_OPCODE
;
491 rc
= ISCSI_ERR_BAD_ITT
;
496 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu
);
498 int iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
499 char *data
, int datalen
)
503 spin_lock(&conn
->session
->lock
);
504 rc
= __iscsi_complete_pdu(conn
, hdr
, data
, datalen
);
505 spin_unlock(&conn
->session
->lock
);
508 EXPORT_SYMBOL_GPL(iscsi_complete_pdu
);
510 /* verify itt (itt encoding: age+cid+itt) */
511 int iscsi_verify_itt(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
514 struct iscsi_session
*session
= conn
->session
;
515 struct iscsi_cmd_task
*ctask
;
518 if (hdr
->itt
!= RESERVED_ITT
) {
519 if (((__force u32
)hdr
->itt
& ISCSI_AGE_MASK
) !=
520 (session
->age
<< ISCSI_AGE_SHIFT
)) {
521 printk(KERN_ERR
"iscsi: received itt %x expected "
522 "session age (%x)\n", (__force u32
)hdr
->itt
,
523 session
->age
& ISCSI_AGE_MASK
);
524 return ISCSI_ERR_BAD_ITT
;
527 if (((__force u32
)hdr
->itt
& ISCSI_CID_MASK
) !=
528 (conn
->id
<< ISCSI_CID_SHIFT
)) {
529 printk(KERN_ERR
"iscsi: received itt %x, expected "
530 "CID (%x)\n", (__force u32
)hdr
->itt
, conn
->id
);
531 return ISCSI_ERR_BAD_ITT
;
533 itt
= get_itt(hdr
->itt
);
537 if (itt
< session
->cmds_max
) {
538 ctask
= session
->cmds
[itt
];
541 printk(KERN_INFO
"iscsi: dropping ctask with "
542 "itt 0x%x\n", ctask
->itt
);
544 return ISCSI_ERR_NO_SCSI_CMD
;
547 if (ctask
->sc
->SCp
.phase
!= session
->age
) {
548 printk(KERN_ERR
"iscsi: ctask's session age %d, "
549 "expected %d\n", ctask
->sc
->SCp
.phase
,
551 return ISCSI_ERR_SESSION_FAILED
;
558 EXPORT_SYMBOL_GPL(iscsi_verify_itt
);
560 void iscsi_conn_failure(struct iscsi_conn
*conn
, enum iscsi_err err
)
562 struct iscsi_session
*session
= conn
->session
;
565 spin_lock_irqsave(&session
->lock
, flags
);
566 if (session
->state
== ISCSI_STATE_FAILED
) {
567 spin_unlock_irqrestore(&session
->lock
, flags
);
571 if (conn
->stop_stage
== 0)
572 session
->state
= ISCSI_STATE_FAILED
;
573 spin_unlock_irqrestore(&session
->lock
, flags
);
574 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
575 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
576 iscsi_conn_error(conn
->cls_conn
, err
);
578 EXPORT_SYMBOL_GPL(iscsi_conn_failure
);
580 static int iscsi_xmit_mtask(struct iscsi_conn
*conn
)
582 struct iscsi_hdr
*hdr
= conn
->mtask
->hdr
;
583 int rc
, was_logout
= 0;
585 if ((hdr
->opcode
& ISCSI_OPCODE_MASK
) == ISCSI_OP_LOGOUT
) {
586 conn
->session
->state
= ISCSI_STATE_IN_RECOVERY
;
587 iscsi_block_session(session_to_cls(conn
->session
));
590 rc
= conn
->session
->tt
->xmit_mgmt_task(conn
, conn
->mtask
);
594 /* done with this in-progress mtask */
598 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
605 * iscsi_data_xmit - xmit any command into the scheduled connection
606 * @conn: iscsi connection
609 * The function can return -EAGAIN in which case the caller must
610 * re-schedule it again later or recover. '0' return code means
613 static int iscsi_data_xmit(struct iscsi_conn
*conn
)
615 struct iscsi_transport
*tt
;
618 if (unlikely(conn
->suspend_tx
)) {
619 debug_scsi("conn %d Tx suspended!\n", conn
->id
);
622 tt
= conn
->session
->tt
;
625 * Transmit in the following order:
627 * 1) un-finished xmit (ctask or mtask)
628 * 2) immediate control PDUs
631 * 5) non-immediate control PDUs
633 * No need to lock around __kfifo_get as long as
634 * there's one producer and one consumer.
637 BUG_ON(conn
->ctask
&& conn
->mtask
);
640 iscsi_get_ctask(conn
->ctask
);
641 rc
= tt
->xmit_cmd_task(conn
, conn
->ctask
);
642 iscsi_put_ctask(conn
->ctask
);
645 /* done with this in-progress ctask */
649 rc
= iscsi_xmit_mtask(conn
);
654 /* process immediate first */
655 if (unlikely(__kfifo_len(conn
->immqueue
))) {
656 while (__kfifo_get(conn
->immqueue
, (void*)&conn
->mtask
,
658 spin_lock_bh(&conn
->session
->lock
);
659 list_add_tail(&conn
->mtask
->running
,
660 &conn
->mgmt_run_list
);
661 spin_unlock_bh(&conn
->session
->lock
);
662 rc
= iscsi_xmit_mtask(conn
);
668 /* process command queue */
669 spin_lock_bh(&conn
->session
->lock
);
670 while (!list_empty(&conn
->xmitqueue
)) {
672 * iscsi tcp may readd the task to the xmitqueue to send
675 conn
->ctask
= list_entry(conn
->xmitqueue
.next
,
676 struct iscsi_cmd_task
, running
);
677 conn
->ctask
->state
= ISCSI_TASK_RUNNING
;
678 list_move_tail(conn
->xmitqueue
.next
, &conn
->run_list
);
679 __iscsi_get_ctask(conn
->ctask
);
680 spin_unlock_bh(&conn
->session
->lock
);
682 rc
= tt
->xmit_cmd_task(conn
, conn
->ctask
);
684 spin_lock_bh(&conn
->session
->lock
);
685 __iscsi_put_ctask(conn
->ctask
);
687 spin_unlock_bh(&conn
->session
->lock
);
691 spin_unlock_bh(&conn
->session
->lock
);
692 /* done with this ctask */
695 /* process the rest control plane PDUs, if any */
696 if (unlikely(__kfifo_len(conn
->mgmtqueue
))) {
697 while (__kfifo_get(conn
->mgmtqueue
, (void*)&conn
->mtask
,
699 spin_lock_bh(&conn
->session
->lock
);
700 list_add_tail(&conn
->mtask
->running
,
701 &conn
->mgmt_run_list
);
702 spin_unlock_bh(&conn
->session
->lock
);
703 rc
= iscsi_xmit_mtask(conn
);
712 if (unlikely(conn
->suspend_tx
))
718 static void iscsi_xmitworker(struct work_struct
*work
)
720 struct iscsi_conn
*conn
=
721 container_of(work
, struct iscsi_conn
, xmitwork
);
724 * serialize Xmit worker on a per-connection basis.
726 mutex_lock(&conn
->xmitmutex
);
728 rc
= iscsi_data_xmit(conn
);
729 } while (rc
>= 0 || rc
== -EAGAIN
);
730 mutex_unlock(&conn
->xmitmutex
);
734 FAILURE_BAD_HOST
= 1,
735 FAILURE_SESSION_FAILED
,
736 FAILURE_SESSION_FREED
,
737 FAILURE_WINDOW_CLOSED
,
739 FAILURE_SESSION_TERMINATE
,
740 FAILURE_SESSION_IN_RECOVERY
,
741 FAILURE_SESSION_RECOVERY_TIMEOUT
,
744 int iscsi_queuecommand(struct scsi_cmnd
*sc
, void (*done
)(struct scsi_cmnd
*))
746 struct Scsi_Host
*host
;
748 struct iscsi_session
*session
;
749 struct iscsi_conn
*conn
;
750 struct iscsi_cmd_task
*ctask
= NULL
;
752 sc
->scsi_done
= done
;
756 host
= sc
->device
->host
;
757 session
= iscsi_hostdata(host
->hostdata
);
759 spin_lock(&session
->lock
);
762 * ISCSI_STATE_FAILED is a temp. state. The recovery
763 * code will decide what is best to do with command queued
766 if (session
->state
!= ISCSI_STATE_LOGGED_IN
&&
767 session
->state
!= ISCSI_STATE_FAILED
) {
769 * to handle the race between when we set the recovery state
770 * and block the session we requeue here (commands could
771 * be entering our queuecommand while a block is starting
772 * up because the block code is not locked)
774 if (session
->state
== ISCSI_STATE_IN_RECOVERY
) {
775 reason
= FAILURE_SESSION_IN_RECOVERY
;
779 if (session
->state
== ISCSI_STATE_RECOVERY_FAILED
)
780 reason
= FAILURE_SESSION_RECOVERY_TIMEOUT
;
781 else if (session
->state
== ISCSI_STATE_TERMINATE
)
782 reason
= FAILURE_SESSION_TERMINATE
;
784 reason
= FAILURE_SESSION_FREED
;
789 * Check for iSCSI window and take care of CmdSN wrap-around
791 if ((int)(session
->max_cmdsn
- session
->cmdsn
) < 0) {
792 reason
= FAILURE_WINDOW_CLOSED
;
796 conn
= session
->leadconn
;
798 reason
= FAILURE_SESSION_FREED
;
802 if (!__kfifo_get(session
->cmdpool
.queue
, (void*)&ctask
,
804 reason
= FAILURE_OOM
;
807 sc
->SCp
.phase
= session
->age
;
808 sc
->SCp
.ptr
= (char *)ctask
;
810 atomic_set(&ctask
->refcount
, 1);
811 ctask
->state
= ISCSI_TASK_PENDING
;
815 INIT_LIST_HEAD(&ctask
->running
);
816 ctask
->total_length
= sc
->request_bufflen
;
817 iscsi_prep_scsi_cmd_pdu(ctask
);
819 session
->tt
->init_cmd_task(ctask
);
821 list_add_tail(&ctask
->running
, &conn
->xmitqueue
);
823 "ctask enq [%s cid %d sc %p cdb 0x%x itt 0x%x len %d cmdsn %d "
825 sc
->sc_data_direction
== DMA_TO_DEVICE
? "write" : "read",
826 conn
->id
, sc
, sc
->cmnd
[0], ctask
->itt
, sc
->request_bufflen
,
827 session
->cmdsn
, session
->max_cmdsn
- session
->exp_cmdsn
+ 1);
828 spin_unlock(&session
->lock
);
830 scsi_queue_work(host
, &conn
->xmitwork
);
834 spin_unlock(&session
->lock
);
835 debug_scsi("cmd 0x%x rejected (%d)\n", sc
->cmnd
[0], reason
);
836 return SCSI_MLQUEUE_HOST_BUSY
;
839 spin_unlock(&session
->lock
);
840 printk(KERN_ERR
"iscsi: cmd 0x%x is not queued (%d)\n",
841 sc
->cmnd
[0], reason
);
842 sc
->result
= (DID_NO_CONNECT
<< 16);
843 sc
->resid
= sc
->request_bufflen
;
847 EXPORT_SYMBOL_GPL(iscsi_queuecommand
);
849 int iscsi_change_queue_depth(struct scsi_device
*sdev
, int depth
)
851 if (depth
> ISCSI_MAX_CMD_PER_LUN
)
852 depth
= ISCSI_MAX_CMD_PER_LUN
;
853 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), depth
);
854 return sdev
->queue_depth
;
856 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth
);
859 iscsi_conn_send_generic(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
860 char *data
, uint32_t data_size
)
862 struct iscsi_session
*session
= conn
->session
;
863 struct iscsi_nopout
*nop
= (struct iscsi_nopout
*)hdr
;
864 struct iscsi_mgmt_task
*mtask
;
866 spin_lock_bh(&session
->lock
);
867 if (session
->state
== ISCSI_STATE_TERMINATE
) {
868 spin_unlock_bh(&session
->lock
);
871 if (hdr
->opcode
== (ISCSI_OP_LOGIN
| ISCSI_OP_IMMEDIATE
) ||
872 hdr
->opcode
== (ISCSI_OP_TEXT
| ISCSI_OP_IMMEDIATE
))
874 * Login and Text are sent serially, in
875 * request-followed-by-response sequence.
876 * Same mtask can be used. Same ITT must be used.
877 * Note that login_mtask is preallocated at conn_create().
879 mtask
= conn
->login_mtask
;
881 BUG_ON(conn
->c_stage
== ISCSI_CONN_INITIAL_STAGE
);
882 BUG_ON(conn
->c_stage
== ISCSI_CONN_STOPPED
);
884 nop
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
885 if (!__kfifo_get(session
->mgmtpool
.queue
,
886 (void*)&mtask
, sizeof(void*))) {
887 spin_unlock_bh(&session
->lock
);
893 * pre-format CmdSN for outgoing PDU.
895 if (hdr
->itt
!= RESERVED_ITT
) {
896 hdr
->itt
= build_itt(mtask
->itt
, conn
->id
, session
->age
);
897 nop
->cmdsn
= cpu_to_be32(session
->cmdsn
);
898 if (conn
->c_stage
== ISCSI_CONN_STARTED
&&
899 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
))
902 /* do not advance CmdSN */
903 nop
->cmdsn
= cpu_to_be32(session
->cmdsn
);
906 memcpy(mtask
->data
, data
, data_size
);
907 mtask
->data_count
= data_size
;
909 mtask
->data_count
= 0;
911 INIT_LIST_HEAD(&mtask
->running
);
912 memcpy(mtask
->hdr
, hdr
, sizeof(struct iscsi_hdr
));
913 if (session
->tt
->init_mgmt_task
)
914 session
->tt
->init_mgmt_task(conn
, mtask
, data
, data_size
);
915 spin_unlock_bh(&session
->lock
);
917 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
918 hdr
->opcode
, hdr
->itt
, data_size
);
921 * since send_pdu() could be called at least from two contexts,
922 * we need to serialize __kfifo_put, so we don't have to take
923 * additional lock on fast data-path
925 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
)
926 __kfifo_put(conn
->immqueue
, (void*)&mtask
, sizeof(void*));
928 __kfifo_put(conn
->mgmtqueue
, (void*)&mtask
, sizeof(void*));
930 scsi_queue_work(session
->host
, &conn
->xmitwork
);
934 int iscsi_conn_send_pdu(struct iscsi_cls_conn
*cls_conn
, struct iscsi_hdr
*hdr
,
935 char *data
, uint32_t data_size
)
937 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
940 mutex_lock(&conn
->xmitmutex
);
941 rc
= iscsi_conn_send_generic(conn
, hdr
, data
, data_size
);
942 mutex_unlock(&conn
->xmitmutex
);
946 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu
);
948 void iscsi_session_recovery_timedout(struct iscsi_cls_session
*cls_session
)
950 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
951 struct iscsi_conn
*conn
= session
->leadconn
;
953 spin_lock_bh(&session
->lock
);
954 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
955 session
->state
= ISCSI_STATE_RECOVERY_FAILED
;
957 wake_up(&conn
->ehwait
);
959 spin_unlock_bh(&session
->lock
);
961 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout
);
963 int iscsi_eh_host_reset(struct scsi_cmnd
*sc
)
965 struct Scsi_Host
*host
= sc
->device
->host
;
966 struct iscsi_session
*session
= iscsi_hostdata(host
->hostdata
);
967 struct iscsi_conn
*conn
= session
->leadconn
;
968 int fail_session
= 0;
970 spin_lock_bh(&session
->lock
);
971 if (session
->state
== ISCSI_STATE_TERMINATE
) {
973 debug_scsi("failing host reset: session terminated "
974 "[CID %d age %d]\n", conn
->id
, session
->age
);
975 spin_unlock_bh(&session
->lock
);
979 if (sc
->SCp
.phase
== session
->age
) {
980 debug_scsi("failing connection CID %d due to SCSI host reset\n",
984 spin_unlock_bh(&session
->lock
);
987 * we drop the lock here but the leadconn cannot be destoyed while
988 * we are in the scsi eh
991 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
993 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
994 wait_event_interruptible(conn
->ehwait
,
995 session
->state
== ISCSI_STATE_TERMINATE
||
996 session
->state
== ISCSI_STATE_LOGGED_IN
||
997 session
->state
== ISCSI_STATE_RECOVERY_FAILED
);
998 if (signal_pending(current
))
999 flush_signals(current
);
1001 spin_lock_bh(&session
->lock
);
1002 if (session
->state
== ISCSI_STATE_LOGGED_IN
)
1003 printk(KERN_INFO
"iscsi: host reset succeeded\n");
1006 spin_unlock_bh(&session
->lock
);
1010 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset
);
1012 static void iscsi_tmabort_timedout(unsigned long data
)
1014 struct iscsi_cmd_task
*ctask
= (struct iscsi_cmd_task
*)data
;
1015 struct iscsi_conn
*conn
= ctask
->conn
;
1016 struct iscsi_session
*session
= conn
->session
;
1018 spin_lock(&session
->lock
);
1019 if (conn
->tmabort_state
== TMABORT_INITIAL
) {
1020 conn
->tmabort_state
= TMABORT_TIMEDOUT
;
1021 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
1022 ctask
->sc
, ctask
->itt
);
1023 /* unblock eh_abort() */
1024 wake_up(&conn
->ehwait
);
1026 spin_unlock(&session
->lock
);
1029 /* must be called with the mutex lock */
1030 static int iscsi_exec_abort_task(struct scsi_cmnd
*sc
,
1031 struct iscsi_cmd_task
*ctask
)
1033 struct iscsi_conn
*conn
= ctask
->conn
;
1034 struct iscsi_session
*session
= conn
->session
;
1035 struct iscsi_tm
*hdr
= &conn
->tmhdr
;
1039 * ctask timed out but session is OK requests must be serialized.
1041 memset(hdr
, 0, sizeof(struct iscsi_tm
));
1042 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
1043 hdr
->flags
= ISCSI_TM_FUNC_ABORT_TASK
;
1044 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
1045 memcpy(hdr
->lun
, ctask
->hdr
->lun
, sizeof(hdr
->lun
));
1046 hdr
->rtt
= ctask
->hdr
->itt
;
1047 hdr
->refcmdsn
= ctask
->hdr
->cmdsn
;
1049 rc
= iscsi_conn_send_generic(conn
, (struct iscsi_hdr
*)hdr
,
1052 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1053 debug_scsi("abort sent failure [itt 0x%x] %d\n", ctask
->itt
,
1058 debug_scsi("abort sent [itt 0x%x]\n", ctask
->itt
);
1060 spin_lock_bh(&session
->lock
);
1061 ctask
->mtask
= (struct iscsi_mgmt_task
*)
1062 session
->mgmt_cmds
[get_itt(hdr
->itt
) -
1063 ISCSI_MGMT_ITT_OFFSET
];
1065 if (conn
->tmabort_state
== TMABORT_INITIAL
) {
1066 conn
->tmfcmd_pdus_cnt
++;
1067 conn
->tmabort_timer
.expires
= 10*HZ
+ jiffies
;
1068 conn
->tmabort_timer
.function
= iscsi_tmabort_timedout
;
1069 conn
->tmabort_timer
.data
= (unsigned long)ctask
;
1070 add_timer(&conn
->tmabort_timer
);
1071 debug_scsi("abort set timeout [itt 0x%x]\n", ctask
->itt
);
1073 spin_unlock_bh(&session
->lock
);
1074 mutex_unlock(&conn
->xmitmutex
);
1077 * block eh thread until:
1081 * 3) session is terminated or restarted or userspace has
1082 * given up on recovery
1084 wait_event_interruptible(conn
->ehwait
,
1085 sc
->SCp
.phase
!= session
->age
||
1086 session
->state
!= ISCSI_STATE_LOGGED_IN
||
1087 conn
->tmabort_state
!= TMABORT_INITIAL
);
1088 if (signal_pending(current
))
1089 flush_signals(current
);
1090 del_timer_sync(&conn
->tmabort_timer
);
1092 mutex_lock(&conn
->xmitmutex
);
1097 * xmit mutex and session lock must be held
1099 static struct iscsi_mgmt_task
*
1100 iscsi_remove_mgmt_task(struct kfifo
*fifo
, uint32_t itt
)
1102 int i
, nr_tasks
= __kfifo_len(fifo
) / sizeof(void*);
1103 struct iscsi_mgmt_task
*task
;
1105 debug_scsi("searching %d tasks\n", nr_tasks
);
1107 for (i
= 0; i
< nr_tasks
; i
++) {
1108 __kfifo_get(fifo
, (void*)&task
, sizeof(void*));
1109 debug_scsi("check task %u\n", task
->itt
);
1111 if (task
->itt
== itt
) {
1112 debug_scsi("matched task\n");
1116 __kfifo_put(fifo
, (void*)&task
, sizeof(void*));
1121 static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task
*ctask
)
1123 struct iscsi_conn
*conn
= ctask
->conn
;
1124 struct iscsi_session
*session
= conn
->session
;
1129 if (!iscsi_remove_mgmt_task(conn
->immqueue
, ctask
->mtask
->itt
))
1130 list_del(&ctask
->mtask
->running
);
1131 __kfifo_put(session
->mgmtpool
.queue
, (void*)&ctask
->mtask
,
1133 ctask
->mtask
= NULL
;
1138 * session lock and xmitmutex must be held
1140 static void fail_command(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
,
1143 struct scsi_cmnd
*sc
;
1149 conn
->session
->tt
->cleanup_cmd_task(conn
, ctask
);
1150 iscsi_ctask_mtask_cleanup(ctask
);
1153 sc
->resid
= sc
->request_bufflen
;
1154 /* release ref from queuecommand */
1155 __iscsi_put_ctask(ctask
);
1158 int iscsi_eh_abort(struct scsi_cmnd
*sc
)
1160 struct iscsi_cmd_task
*ctask
;
1161 struct iscsi_conn
*conn
;
1162 struct iscsi_session
*session
;
1166 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1170 debug_scsi("sc never reached iscsi layer or it completed.\n");
1174 ctask
= (struct iscsi_cmd_task
*)sc
->SCp
.ptr
;
1176 session
= conn
->session
;
1178 conn
->eh_abort_cnt
++;
1179 debug_scsi("aborting [sc %p itt 0x%x]\n", sc
, ctask
->itt
);
1181 mutex_lock(&conn
->xmitmutex
);
1182 spin_lock_bh(&session
->lock
);
1185 * If we are not logged in or we have started a new session
1186 * then let the host reset code handle this
1188 if (session
->state
!= ISCSI_STATE_LOGGED_IN
||
1189 sc
->SCp
.phase
!= session
->age
)
1192 /* ctask completed before time out */
1194 spin_unlock_bh(&session
->lock
);
1195 debug_scsi("sc completed while abort in progress\n");
1196 goto success_rel_mutex
;
1199 /* what should we do here ? */
1200 if (conn
->ctask
== ctask
) {
1201 printk(KERN_INFO
"iscsi: sc %p itt 0x%x partially sent. "
1202 "Failing abort\n", sc
, ctask
->itt
);
1206 if (ctask
->state
== ISCSI_TASK_PENDING
)
1207 goto success_cleanup
;
1209 conn
->tmabort_state
= TMABORT_INITIAL
;
1211 spin_unlock_bh(&session
->lock
);
1212 rc
= iscsi_exec_abort_task(sc
, ctask
);
1213 spin_lock_bh(&session
->lock
);
1215 if (rc
|| sc
->SCp
.phase
!= session
->age
||
1216 session
->state
!= ISCSI_STATE_LOGGED_IN
)
1218 iscsi_ctask_mtask_cleanup(ctask
);
1220 switch (conn
->tmabort_state
) {
1221 case TMABORT_SUCCESS
:
1222 goto success_cleanup
;
1223 case TMABORT_NOT_FOUND
:
1225 /* ctask completed before tmf abort response */
1226 spin_unlock_bh(&session
->lock
);
1227 debug_scsi("sc completed while abort in progress\n");
1228 goto success_rel_mutex
;
1232 /* timedout or failed */
1233 spin_unlock_bh(&session
->lock
);
1234 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1235 spin_lock_bh(&session
->lock
);
1240 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc
, ctask
->itt
);
1241 spin_unlock_bh(&session
->lock
);
1244 * clean up task if aborted. we have the xmitmutex so grab
1245 * the recv lock as a writer
1247 write_lock_bh(conn
->recv_lock
);
1248 spin_lock(&session
->lock
);
1249 fail_command(conn
, ctask
, DID_ABORT
<< 16);
1250 spin_unlock(&session
->lock
);
1251 write_unlock_bh(conn
->recv_lock
);
1254 mutex_unlock(&conn
->xmitmutex
);
1258 spin_unlock_bh(&session
->lock
);
1259 mutex_unlock(&conn
->xmitmutex
);
1261 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc
, ctask
->itt
);
1264 EXPORT_SYMBOL_GPL(iscsi_eh_abort
);
1267 iscsi_pool_init(struct iscsi_queue
*q
, int max
, void ***items
, int item_size
)
1271 *items
= kmalloc(max
* sizeof(void*), GFP_KERNEL
);
1276 q
->pool
= kmalloc(max
* sizeof(void*), GFP_KERNEL
);
1277 if (q
->pool
== NULL
) {
1282 q
->queue
= kfifo_init((void*)q
->pool
, max
* sizeof(void*),
1284 if (q
->queue
== ERR_PTR(-ENOMEM
)) {
1290 for (i
= 0; i
< max
; i
++) {
1291 q
->pool
[i
] = kmalloc(item_size
, GFP_KERNEL
);
1292 if (q
->pool
[i
] == NULL
) {
1295 for (j
= 0; j
< i
; j
++)
1298 kfifo_free(q
->queue
);
1303 memset(q
->pool
[i
], 0, item_size
);
1304 (*items
)[i
] = q
->pool
[i
];
1305 __kfifo_put(q
->queue
, (void*)&q
->pool
[i
], sizeof(void*));
1309 EXPORT_SYMBOL_GPL(iscsi_pool_init
);
1311 void iscsi_pool_free(struct iscsi_queue
*q
, void **items
)
1315 for (i
= 0; i
< q
->max
; i
++)
1320 EXPORT_SYMBOL_GPL(iscsi_pool_free
);
1323 * iSCSI Session's hostdata organization:
1325 * *------------------* <== hostdata_session(host->hostdata)
1326 * | ptr to class sess|
1327 * |------------------| <== iscsi_hostdata(host->hostdata)
1329 * *------------------*
1332 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1333 _sz % sizeof(unsigned long))
1335 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1338 * iscsi_session_setup - create iscsi cls session and host and session
1339 * @scsit: scsi transport template
1340 * @iscsit: iscsi transport template
1341 * @initial_cmdsn: initial CmdSN
1342 * @hostno: host no allocated
1344 * This can be used by software iscsi_transports that allocate
1345 * a session per scsi host.
1347 struct iscsi_cls_session
*
1348 iscsi_session_setup(struct iscsi_transport
*iscsit
,
1349 struct scsi_transport_template
*scsit
,
1350 int cmd_task_size
, int mgmt_task_size
,
1351 uint32_t initial_cmdsn
, uint32_t *hostno
)
1353 struct Scsi_Host
*shost
;
1354 struct iscsi_session
*session
;
1355 struct iscsi_cls_session
*cls_session
;
1358 shost
= scsi_host_alloc(iscsit
->host_template
,
1359 hostdata_privsize(sizeof(*session
)));
1364 shost
->max_channel
= 0;
1365 shost
->max_lun
= iscsit
->max_lun
;
1366 shost
->max_cmd_len
= iscsit
->max_cmd_len
;
1367 shost
->transportt
= scsit
;
1368 shost
->transportt
->create_work_queue
= 1;
1369 *hostno
= shost
->host_no
;
1371 session
= iscsi_hostdata(shost
->hostdata
);
1372 memset(session
, 0, sizeof(struct iscsi_session
));
1373 session
->host
= shost
;
1374 session
->state
= ISCSI_STATE_FREE
;
1375 session
->mgmtpool_max
= ISCSI_MGMT_CMDS_MAX
;
1376 session
->cmds_max
= ISCSI_XMIT_CMDS_MAX
;
1377 session
->cmdsn
= initial_cmdsn
;
1378 session
->exp_cmdsn
= initial_cmdsn
+ 1;
1379 session
->max_cmdsn
= initial_cmdsn
+ 1;
1380 session
->max_r2t
= 1;
1381 session
->tt
= iscsit
;
1383 /* initialize SCSI PDU commands pool */
1384 if (iscsi_pool_init(&session
->cmdpool
, session
->cmds_max
,
1385 (void***)&session
->cmds
,
1386 cmd_task_size
+ sizeof(struct iscsi_cmd_task
)))
1387 goto cmdpool_alloc_fail
;
1389 /* pre-format cmds pool with ITT */
1390 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
1391 struct iscsi_cmd_task
*ctask
= session
->cmds
[cmd_i
];
1394 ctask
->dd_data
= &ctask
[1];
1396 INIT_LIST_HEAD(&ctask
->running
);
1399 spin_lock_init(&session
->lock
);
1401 /* initialize immediate command pool */
1402 if (iscsi_pool_init(&session
->mgmtpool
, session
->mgmtpool_max
,
1403 (void***)&session
->mgmt_cmds
,
1404 mgmt_task_size
+ sizeof(struct iscsi_mgmt_task
)))
1405 goto mgmtpool_alloc_fail
;
1408 /* pre-format immediate cmds pool with ITT */
1409 for (cmd_i
= 0; cmd_i
< session
->mgmtpool_max
; cmd_i
++) {
1410 struct iscsi_mgmt_task
*mtask
= session
->mgmt_cmds
[cmd_i
];
1413 mtask
->dd_data
= &mtask
[1];
1414 mtask
->itt
= ISCSI_MGMT_ITT_OFFSET
+ cmd_i
;
1415 INIT_LIST_HEAD(&mtask
->running
);
1418 if (scsi_add_host(shost
, NULL
))
1421 if (!try_module_get(iscsit
->owner
))
1422 goto cls_session_fail
;
1424 cls_session
= iscsi_create_session(shost
, iscsit
, 0);
1427 *(unsigned long*)shost
->hostdata
= (unsigned long)cls_session
;
1432 module_put(iscsit
->owner
);
1434 scsi_remove_host(shost
);
1436 iscsi_pool_free(&session
->mgmtpool
, (void**)session
->mgmt_cmds
);
1437 mgmtpool_alloc_fail
:
1438 iscsi_pool_free(&session
->cmdpool
, (void**)session
->cmds
);
1440 scsi_host_put(shost
);
1443 EXPORT_SYMBOL_GPL(iscsi_session_setup
);
1446 * iscsi_session_teardown - destroy session, host, and cls_session
1449 * This can be used by software iscsi_transports that allocate
1450 * a session per scsi host.
1452 void iscsi_session_teardown(struct iscsi_cls_session
*cls_session
)
1454 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
1455 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
1456 struct module
*owner
= cls_session
->transport
->owner
;
1458 scsi_remove_host(shost
);
1460 iscsi_pool_free(&session
->mgmtpool
, (void**)session
->mgmt_cmds
);
1461 iscsi_pool_free(&session
->cmdpool
, (void**)session
->cmds
);
1463 kfree(session
->targetname
);
1465 iscsi_destroy_session(cls_session
);
1466 scsi_host_put(shost
);
1469 EXPORT_SYMBOL_GPL(iscsi_session_teardown
);
1472 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1473 * @cls_session: iscsi_cls_session
1476 struct iscsi_cls_conn
*
1477 iscsi_conn_setup(struct iscsi_cls_session
*cls_session
, uint32_t conn_idx
)
1479 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1480 struct iscsi_conn
*conn
;
1481 struct iscsi_cls_conn
*cls_conn
;
1484 cls_conn
= iscsi_create_conn(cls_session
, conn_idx
);
1487 conn
= cls_conn
->dd_data
;
1488 memset(conn
, 0, sizeof(*conn
));
1490 conn
->session
= session
;
1491 conn
->cls_conn
= cls_conn
;
1492 conn
->c_stage
= ISCSI_CONN_INITIAL_STAGE
;
1493 conn
->id
= conn_idx
;
1494 conn
->exp_statsn
= 0;
1495 conn
->tmabort_state
= TMABORT_INITIAL
;
1496 INIT_LIST_HEAD(&conn
->run_list
);
1497 INIT_LIST_HEAD(&conn
->mgmt_run_list
);
1498 INIT_LIST_HEAD(&conn
->xmitqueue
);
1500 /* initialize general immediate & non-immediate PDU commands queue */
1501 conn
->immqueue
= kfifo_alloc(session
->mgmtpool_max
* sizeof(void*),
1503 if (conn
->immqueue
== ERR_PTR(-ENOMEM
))
1504 goto immqueue_alloc_fail
;
1506 conn
->mgmtqueue
= kfifo_alloc(session
->mgmtpool_max
* sizeof(void*),
1508 if (conn
->mgmtqueue
== ERR_PTR(-ENOMEM
))
1509 goto mgmtqueue_alloc_fail
;
1511 INIT_WORK(&conn
->xmitwork
, iscsi_xmitworker
);
1513 /* allocate login_mtask used for the login/text sequences */
1514 spin_lock_bh(&session
->lock
);
1515 if (!__kfifo_get(session
->mgmtpool
.queue
,
1516 (void*)&conn
->login_mtask
,
1518 spin_unlock_bh(&session
->lock
);
1519 goto login_mtask_alloc_fail
;
1521 spin_unlock_bh(&session
->lock
);
1523 data
= kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN
, GFP_KERNEL
);
1525 goto login_mtask_data_alloc_fail
;
1526 conn
->login_mtask
->data
= conn
->data
= data
;
1528 init_timer(&conn
->tmabort_timer
);
1529 mutex_init(&conn
->xmitmutex
);
1530 init_waitqueue_head(&conn
->ehwait
);
1534 login_mtask_data_alloc_fail
:
1535 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->login_mtask
,
1537 login_mtask_alloc_fail
:
1538 kfifo_free(conn
->mgmtqueue
);
1539 mgmtqueue_alloc_fail
:
1540 kfifo_free(conn
->immqueue
);
1541 immqueue_alloc_fail
:
1542 iscsi_destroy_conn(cls_conn
);
1545 EXPORT_SYMBOL_GPL(iscsi_conn_setup
);
1548 * iscsi_conn_teardown - teardown iscsi connection
1549 * cls_conn: iscsi class connection
1551 * TODO: we may need to make this into a two step process
1552 * like scsi-mls remove + put host
1554 void iscsi_conn_teardown(struct iscsi_cls_conn
*cls_conn
)
1556 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1557 struct iscsi_session
*session
= conn
->session
;
1558 unsigned long flags
;
1560 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1561 mutex_lock(&conn
->xmitmutex
);
1563 spin_lock_bh(&session
->lock
);
1564 conn
->c_stage
= ISCSI_CONN_CLEANUP_WAIT
;
1565 if (session
->leadconn
== conn
) {
1567 * leading connection? then give up on recovery.
1569 session
->state
= ISCSI_STATE_TERMINATE
;
1570 wake_up(&conn
->ehwait
);
1572 spin_unlock_bh(&session
->lock
);
1574 mutex_unlock(&conn
->xmitmutex
);
1577 * Block until all in-progress commands for this connection
1581 spin_lock_irqsave(session
->host
->host_lock
, flags
);
1582 if (!session
->host
->host_busy
) { /* OK for ERL == 0 */
1583 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
1586 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
1587 msleep_interruptible(500);
1588 printk(KERN_INFO
"iscsi: scsi conn_destroy(): host_busy %d "
1589 "host_failed %d\n", session
->host
->host_busy
,
1590 session
->host
->host_failed
);
1592 * force eh_abort() to unblock
1594 wake_up(&conn
->ehwait
);
1597 /* flush queued up work because we free the connection below */
1598 scsi_flush_work(session
->host
);
1600 spin_lock_bh(&session
->lock
);
1602 kfree(conn
->persistent_address
);
1603 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->login_mtask
,
1605 if (session
->leadconn
== conn
) {
1606 session
->leadconn
= NULL
;
1607 /* no connections exits.. reset sequencing */
1608 session
->cmdsn
= session
->max_cmdsn
= session
->exp_cmdsn
= 1;
1610 spin_unlock_bh(&session
->lock
);
1612 kfifo_free(conn
->immqueue
);
1613 kfifo_free(conn
->mgmtqueue
);
1615 iscsi_destroy_conn(cls_conn
);
1617 EXPORT_SYMBOL_GPL(iscsi_conn_teardown
);
1619 int iscsi_conn_start(struct iscsi_cls_conn
*cls_conn
)
1621 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1622 struct iscsi_session
*session
= conn
->session
;
1625 printk(KERN_ERR
"iscsi: can't start unbound connection\n");
1629 if ((session
->imm_data_en
|| !session
->initial_r2t_en
) &&
1630 session
->first_burst
> session
->max_burst
) {
1631 printk("iscsi: invalid burst lengths: "
1632 "first_burst %d max_burst %d\n",
1633 session
->first_burst
, session
->max_burst
);
1637 spin_lock_bh(&session
->lock
);
1638 conn
->c_stage
= ISCSI_CONN_STARTED
;
1639 session
->state
= ISCSI_STATE_LOGGED_IN
;
1641 switch(conn
->stop_stage
) {
1642 case STOP_CONN_RECOVER
:
1644 * unblock eh_abort() if it is blocked. re-try all
1645 * commands after successful recovery
1647 conn
->stop_stage
= 0;
1648 conn
->tmabort_state
= TMABORT_INITIAL
;
1650 spin_unlock_bh(&session
->lock
);
1652 iscsi_unblock_session(session_to_cls(session
));
1653 wake_up(&conn
->ehwait
);
1655 case STOP_CONN_TERM
:
1656 conn
->stop_stage
= 0;
1661 spin_unlock_bh(&session
->lock
);
1665 EXPORT_SYMBOL_GPL(iscsi_conn_start
);
1668 flush_control_queues(struct iscsi_session
*session
, struct iscsi_conn
*conn
)
1670 struct iscsi_mgmt_task
*mtask
, *tmp
;
1672 /* handle pending */
1673 while (__kfifo_get(conn
->immqueue
, (void*)&mtask
, sizeof(void*)) ||
1674 __kfifo_get(conn
->mgmtqueue
, (void*)&mtask
, sizeof(void*))) {
1675 if (mtask
== conn
->login_mtask
)
1677 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask
->itt
);
1678 __kfifo_put(session
->mgmtpool
.queue
, (void*)&mtask
,
1682 /* handle running */
1683 list_for_each_entry_safe(mtask
, tmp
, &conn
->mgmt_run_list
, running
) {
1684 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask
->itt
);
1685 list_del(&mtask
->running
);
1687 if (mtask
== conn
->login_mtask
)
1689 __kfifo_put(session
->mgmtpool
.queue
, (void*)&mtask
,
1696 /* Fail commands. Mutex and session lock held and recv side suspended */
1697 static void fail_all_commands(struct iscsi_conn
*conn
)
1699 struct iscsi_cmd_task
*ctask
, *tmp
;
1702 list_for_each_entry_safe(ctask
, tmp
, &conn
->xmitqueue
, running
) {
1703 debug_scsi("failing pending sc %p itt 0x%x\n", ctask
->sc
,
1705 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1708 /* fail all other running */
1709 list_for_each_entry_safe(ctask
, tmp
, &conn
->run_list
, running
) {
1710 debug_scsi("failing in progress sc %p itt 0x%x\n",
1711 ctask
->sc
, ctask
->itt
);
1712 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1718 static void iscsi_start_session_recovery(struct iscsi_session
*session
,
1719 struct iscsi_conn
*conn
, int flag
)
1723 spin_lock_bh(&session
->lock
);
1724 if (conn
->stop_stage
== STOP_CONN_TERM
) {
1725 spin_unlock_bh(&session
->lock
);
1730 * When this is called for the in_login state, we only want to clean
1731 * up the login task and connection. We do not need to block and set
1732 * the recovery state again
1734 if (flag
== STOP_CONN_TERM
)
1735 session
->state
= ISCSI_STATE_TERMINATE
;
1736 else if (conn
->stop_stage
!= STOP_CONN_RECOVER
)
1737 session
->state
= ISCSI_STATE_IN_RECOVERY
;
1739 old_stop_stage
= conn
->stop_stage
;
1740 conn
->stop_stage
= flag
;
1741 conn
->c_stage
= ISCSI_CONN_STOPPED
;
1742 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1743 spin_unlock_bh(&session
->lock
);
1745 write_lock_bh(conn
->recv_lock
);
1746 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
1747 write_unlock_bh(conn
->recv_lock
);
1749 mutex_lock(&conn
->xmitmutex
);
1751 * for connection level recovery we should not calculate
1752 * header digest. conn->hdr_size used for optimization
1753 * in hdr_extract() and will be re-negotiated at
1756 if (flag
== STOP_CONN_RECOVER
) {
1757 conn
->hdrdgst_en
= 0;
1758 conn
->datadgst_en
= 0;
1759 if (session
->state
== ISCSI_STATE_IN_RECOVERY
&&
1760 old_stop_stage
!= STOP_CONN_RECOVER
) {
1761 debug_scsi("blocking session\n");
1762 iscsi_block_session(session_to_cls(session
));
1769 spin_lock_bh(&session
->lock
);
1770 fail_all_commands(conn
);
1771 flush_control_queues(session
, conn
);
1772 spin_unlock_bh(&session
->lock
);
1774 mutex_unlock(&conn
->xmitmutex
);
1777 void iscsi_conn_stop(struct iscsi_cls_conn
*cls_conn
, int flag
)
1779 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1780 struct iscsi_session
*session
= conn
->session
;
1783 case STOP_CONN_RECOVER
:
1784 case STOP_CONN_TERM
:
1785 iscsi_start_session_recovery(session
, conn
, flag
);
1788 printk(KERN_ERR
"iscsi: invalid stop flag %d\n", flag
);
1791 EXPORT_SYMBOL_GPL(iscsi_conn_stop
);
1793 int iscsi_conn_bind(struct iscsi_cls_session
*cls_session
,
1794 struct iscsi_cls_conn
*cls_conn
, int is_leading
)
1796 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1797 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1799 spin_lock_bh(&session
->lock
);
1801 session
->leadconn
= conn
;
1802 spin_unlock_bh(&session
->lock
);
1805 * Unblock xmitworker(), Login Phase will pass through.
1807 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
1808 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1811 EXPORT_SYMBOL_GPL(iscsi_conn_bind
);
1814 int iscsi_set_param(struct iscsi_cls_conn
*cls_conn
,
1815 enum iscsi_param param
, char *buf
, int buflen
)
1817 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1818 struct iscsi_session
*session
= conn
->session
;
1822 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
1823 sscanf(buf
, "%d", &conn
->max_recv_dlength
);
1825 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
1826 sscanf(buf
, "%d", &conn
->max_xmit_dlength
);
1828 case ISCSI_PARAM_HDRDGST_EN
:
1829 sscanf(buf
, "%d", &conn
->hdrdgst_en
);
1831 case ISCSI_PARAM_DATADGST_EN
:
1832 sscanf(buf
, "%d", &conn
->datadgst_en
);
1834 case ISCSI_PARAM_INITIAL_R2T_EN
:
1835 sscanf(buf
, "%d", &session
->initial_r2t_en
);
1837 case ISCSI_PARAM_MAX_R2T
:
1838 sscanf(buf
, "%d", &session
->max_r2t
);
1840 case ISCSI_PARAM_IMM_DATA_EN
:
1841 sscanf(buf
, "%d", &session
->imm_data_en
);
1843 case ISCSI_PARAM_FIRST_BURST
:
1844 sscanf(buf
, "%d", &session
->first_burst
);
1846 case ISCSI_PARAM_MAX_BURST
:
1847 sscanf(buf
, "%d", &session
->max_burst
);
1849 case ISCSI_PARAM_PDU_INORDER_EN
:
1850 sscanf(buf
, "%d", &session
->pdu_inorder_en
);
1852 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
1853 sscanf(buf
, "%d", &session
->dataseq_inorder_en
);
1855 case ISCSI_PARAM_ERL
:
1856 sscanf(buf
, "%d", &session
->erl
);
1858 case ISCSI_PARAM_IFMARKER_EN
:
1859 sscanf(buf
, "%d", &value
);
1862 case ISCSI_PARAM_OFMARKER_EN
:
1863 sscanf(buf
, "%d", &value
);
1866 case ISCSI_PARAM_EXP_STATSN
:
1867 sscanf(buf
, "%u", &conn
->exp_statsn
);
1869 case ISCSI_PARAM_TARGET_NAME
:
1870 /* this should not change between logins */
1871 if (session
->targetname
)
1874 session
->targetname
= kstrdup(buf
, GFP_KERNEL
);
1875 if (!session
->targetname
)
1878 case ISCSI_PARAM_TPGT
:
1879 sscanf(buf
, "%d", &session
->tpgt
);
1881 case ISCSI_PARAM_PERSISTENT_PORT
:
1882 sscanf(buf
, "%d", &conn
->persistent_port
);
1884 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
1886 * this is the address returned in discovery so it should
1887 * not change between logins.
1889 if (conn
->persistent_address
)
1892 conn
->persistent_address
= kstrdup(buf
, GFP_KERNEL
);
1893 if (!conn
->persistent_address
)
1902 EXPORT_SYMBOL_GPL(iscsi_set_param
);
1904 int iscsi_session_get_param(struct iscsi_cls_session
*cls_session
,
1905 enum iscsi_param param
, char *buf
)
1907 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
1908 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
1912 case ISCSI_PARAM_INITIAL_R2T_EN
:
1913 len
= sprintf(buf
, "%d\n", session
->initial_r2t_en
);
1915 case ISCSI_PARAM_MAX_R2T
:
1916 len
= sprintf(buf
, "%hu\n", session
->max_r2t
);
1918 case ISCSI_PARAM_IMM_DATA_EN
:
1919 len
= sprintf(buf
, "%d\n", session
->imm_data_en
);
1921 case ISCSI_PARAM_FIRST_BURST
:
1922 len
= sprintf(buf
, "%u\n", session
->first_burst
);
1924 case ISCSI_PARAM_MAX_BURST
:
1925 len
= sprintf(buf
, "%u\n", session
->max_burst
);
1927 case ISCSI_PARAM_PDU_INORDER_EN
:
1928 len
= sprintf(buf
, "%d\n", session
->pdu_inorder_en
);
1930 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
1931 len
= sprintf(buf
, "%d\n", session
->dataseq_inorder_en
);
1933 case ISCSI_PARAM_ERL
:
1934 len
= sprintf(buf
, "%d\n", session
->erl
);
1936 case ISCSI_PARAM_TARGET_NAME
:
1937 len
= sprintf(buf
, "%s\n", session
->targetname
);
1939 case ISCSI_PARAM_TPGT
:
1940 len
= sprintf(buf
, "%d\n", session
->tpgt
);
1948 EXPORT_SYMBOL_GPL(iscsi_session_get_param
);
1950 int iscsi_conn_get_param(struct iscsi_cls_conn
*cls_conn
,
1951 enum iscsi_param param
, char *buf
)
1953 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1957 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
1958 len
= sprintf(buf
, "%u\n", conn
->max_recv_dlength
);
1960 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
1961 len
= sprintf(buf
, "%u\n", conn
->max_xmit_dlength
);
1963 case ISCSI_PARAM_HDRDGST_EN
:
1964 len
= sprintf(buf
, "%d\n", conn
->hdrdgst_en
);
1966 case ISCSI_PARAM_DATADGST_EN
:
1967 len
= sprintf(buf
, "%d\n", conn
->datadgst_en
);
1969 case ISCSI_PARAM_IFMARKER_EN
:
1970 len
= sprintf(buf
, "%d\n", conn
->ifmarker_en
);
1972 case ISCSI_PARAM_OFMARKER_EN
:
1973 len
= sprintf(buf
, "%d\n", conn
->ofmarker_en
);
1975 case ISCSI_PARAM_EXP_STATSN
:
1976 len
= sprintf(buf
, "%u\n", conn
->exp_statsn
);
1978 case ISCSI_PARAM_PERSISTENT_PORT
:
1979 len
= sprintf(buf
, "%d\n", conn
->persistent_port
);
1981 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
1982 len
= sprintf(buf
, "%s\n", conn
->persistent_address
);
1990 EXPORT_SYMBOL_GPL(iscsi_conn_get_param
);
1992 MODULE_AUTHOR("Mike Christie");
1993 MODULE_DESCRIPTION("iSCSI library functions");
1994 MODULE_LICENSE("GPL");