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/kfifo.h>
26 #include <linux/delay.h>
27 #include <asm/unaligned.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 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
49 #define SNA32_CHECK 2147483648UL
51 static int iscsi_sna_lt(u32 n1
, u32 n2
)
53 return n1
!= n2
&& ((n1
< n2
&& (n2
- n1
< SNA32_CHECK
)) ||
54 (n1
> n2
&& (n2
- n1
< SNA32_CHECK
)));
57 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
58 static int iscsi_sna_lte(u32 n1
, u32 n2
)
60 return n1
== n2
|| ((n1
< n2
&& (n2
- n1
< SNA32_CHECK
)) ||
61 (n1
> n2
&& (n2
- n1
< SNA32_CHECK
)));
65 iscsi_update_cmdsn(struct iscsi_session
*session
, struct iscsi_nopin
*hdr
)
67 uint32_t max_cmdsn
= be32_to_cpu(hdr
->max_cmdsn
);
68 uint32_t exp_cmdsn
= be32_to_cpu(hdr
->exp_cmdsn
);
71 * standard specifies this check for when to update expected and
72 * max sequence numbers
74 if (iscsi_sna_lt(max_cmdsn
, exp_cmdsn
- 1))
77 if (exp_cmdsn
!= session
->exp_cmdsn
&&
78 !iscsi_sna_lt(exp_cmdsn
, session
->exp_cmdsn
))
79 session
->exp_cmdsn
= exp_cmdsn
;
81 if (max_cmdsn
!= session
->max_cmdsn
&&
82 !iscsi_sna_lt(max_cmdsn
, session
->max_cmdsn
)) {
83 session
->max_cmdsn
= max_cmdsn
;
85 * if the window closed with IO queued, then kick the
88 if (!list_empty(&session
->leadconn
->xmitqueue
) ||
89 !list_empty(&session
->leadconn
->mgmtqueue
))
90 scsi_queue_work(session
->host
,
91 &session
->leadconn
->xmitwork
);
94 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn
);
96 void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task
*ctask
,
97 struct iscsi_data
*hdr
)
99 struct iscsi_conn
*conn
= ctask
->conn
;
101 memset(hdr
, 0, sizeof(struct iscsi_data
));
102 hdr
->ttt
= cpu_to_be32(ISCSI_RESERVED_TAG
);
103 hdr
->datasn
= cpu_to_be32(ctask
->unsol_datasn
);
104 ctask
->unsol_datasn
++;
105 hdr
->opcode
= ISCSI_OP_SCSI_DATA_OUT
;
106 memcpy(hdr
->lun
, ctask
->hdr
->lun
, sizeof(hdr
->lun
));
108 hdr
->itt
= ctask
->hdr
->itt
;
109 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
110 hdr
->offset
= cpu_to_be32(ctask
->unsol_offset
);
112 if (ctask
->unsol_count
> conn
->max_xmit_dlength
) {
113 hton24(hdr
->dlength
, conn
->max_xmit_dlength
);
114 ctask
->data_count
= conn
->max_xmit_dlength
;
115 ctask
->unsol_offset
+= ctask
->data_count
;
118 hton24(hdr
->dlength
, ctask
->unsol_count
);
119 ctask
->data_count
= ctask
->unsol_count
;
120 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
123 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu
);
125 static int iscsi_add_hdr(struct iscsi_cmd_task
*ctask
, unsigned len
)
127 unsigned exp_len
= ctask
->hdr_len
+ len
;
129 if (exp_len
> ctask
->hdr_max
) {
134 WARN_ON(len
& (ISCSI_PAD_LEN
- 1)); /* caller must pad the AHS */
135 ctask
->hdr_len
= exp_len
;
140 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
141 * @ctask: iscsi cmd task
143 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
144 * fields like dlength or final based on how much data it sends
146 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task
*ctask
)
148 struct iscsi_conn
*conn
= ctask
->conn
;
149 struct iscsi_session
*session
= conn
->session
;
150 struct iscsi_cmd
*hdr
= ctask
->hdr
;
151 struct scsi_cmnd
*sc
= ctask
->sc
;
156 rc
= iscsi_add_hdr(ctask
, sizeof(*hdr
));
159 hdr
->opcode
= ISCSI_OP_SCSI_CMD
;
160 hdr
->flags
= ISCSI_ATTR_SIMPLE
;
161 int_to_scsilun(sc
->device
->lun
, (struct scsi_lun
*)hdr
->lun
);
162 hdr
->itt
= build_itt(ctask
->itt
, conn
->id
, session
->age
);
163 hdr
->data_length
= cpu_to_be32(scsi_bufflen(sc
));
164 hdr
->cmdsn
= cpu_to_be32(session
->cmdsn
);
166 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
167 memcpy(hdr
->cdb
, sc
->cmnd
, sc
->cmd_len
);
168 if (sc
->cmd_len
< MAX_COMMAND_SIZE
)
169 memset(&hdr
->cdb
[sc
->cmd_len
], 0,
170 MAX_COMMAND_SIZE
- sc
->cmd_len
);
172 ctask
->data_count
= 0;
173 ctask
->imm_count
= 0;
174 if (sc
->sc_data_direction
== DMA_TO_DEVICE
) {
175 hdr
->flags
|= ISCSI_FLAG_CMD_WRITE
;
179 * imm_count bytes to be sent right after
182 * unsol_count bytes(as Data-Out) to be sent
183 * without R2T ack right after
186 * r2t_data_count bytes to be sent via R2T ack's
188 * pad_count bytes to be sent as zero-padding
190 ctask
->unsol_count
= 0;
191 ctask
->unsol_offset
= 0;
192 ctask
->unsol_datasn
= 0;
194 if (session
->imm_data_en
) {
195 if (scsi_bufflen(sc
) >= session
->first_burst
)
196 ctask
->imm_count
= min(session
->first_burst
,
197 conn
->max_xmit_dlength
);
199 ctask
->imm_count
= min(scsi_bufflen(sc
),
200 conn
->max_xmit_dlength
);
201 hton24(ctask
->hdr
->dlength
, ctask
->imm_count
);
203 zero_data(ctask
->hdr
->dlength
);
205 if (!session
->initial_r2t_en
) {
206 ctask
->unsol_count
= min((session
->first_burst
),
207 (scsi_bufflen(sc
))) - ctask
->imm_count
;
208 ctask
->unsol_offset
= ctask
->imm_count
;
211 if (!ctask
->unsol_count
)
212 /* No unsolicit Data-Out's */
213 ctask
->hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
215 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
216 zero_data(hdr
->dlength
);
218 if (sc
->sc_data_direction
== DMA_FROM_DEVICE
)
219 hdr
->flags
|= ISCSI_FLAG_CMD_READ
;
222 /* calculate size of additional header segments (AHSs) */
223 hdrlength
= ctask
->hdr_len
- sizeof(*hdr
);
225 WARN_ON(hdrlength
& (ISCSI_PAD_LEN
-1));
226 hdrlength
/= ISCSI_PAD_LEN
;
228 WARN_ON(hdrlength
>= 256);
229 hdr
->hlength
= hdrlength
& 0xFF;
231 conn
->scsicmd_pdus_cnt
++;
233 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
234 "cmdsn %d win %d]\n",
235 sc
->sc_data_direction
== DMA_TO_DEVICE
? "write" : "read",
236 conn
->id
, sc
, sc
->cmnd
[0], ctask
->itt
, scsi_bufflen(sc
),
237 session
->cmdsn
, session
->max_cmdsn
- session
->exp_cmdsn
+ 1);
242 * iscsi_complete_command - return command back to scsi-ml
243 * @ctask: iscsi cmd task
245 * Must be called with session lock.
246 * This function returns the scsi command to scsi-ml and returns
247 * the cmd task to the pool of available cmd tasks.
249 static void iscsi_complete_command(struct iscsi_cmd_task
*ctask
)
251 struct iscsi_conn
*conn
= ctask
->conn
;
252 struct iscsi_session
*session
= conn
->session
;
253 struct scsi_cmnd
*sc
= ctask
->sc
;
255 ctask
->state
= ISCSI_TASK_COMPLETED
;
257 /* SCSI eh reuses commands to verify us */
259 if (conn
->ctask
== ctask
)
261 list_del_init(&ctask
->running
);
262 __kfifo_put(session
->cmdpool
.queue
, (void*)&ctask
, sizeof(void*));
266 static void __iscsi_get_ctask(struct iscsi_cmd_task
*ctask
)
268 atomic_inc(&ctask
->refcount
);
271 static void __iscsi_put_ctask(struct iscsi_cmd_task
*ctask
)
273 if (atomic_dec_and_test(&ctask
->refcount
))
274 iscsi_complete_command(ctask
);
278 * session lock must be held
280 static void fail_command(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
,
283 struct scsi_cmnd
*sc
;
289 if (ctask
->state
== ISCSI_TASK_PENDING
)
291 * cmd never made it to the xmit thread, so we should not count
292 * the cmd in the sequencing
294 conn
->session
->queued_cmdsn
--;
296 conn
->session
->tt
->cleanup_cmd_task(conn
, ctask
);
299 scsi_set_resid(sc
, scsi_bufflen(sc
));
300 if (conn
->ctask
== ctask
)
302 /* release ref from queuecommand */
303 __iscsi_put_ctask(ctask
);
307 * iscsi_free_mgmt_task - return mgmt task back to pool
308 * @conn: iscsi connection
311 * Must be called with session lock.
313 void iscsi_free_mgmt_task(struct iscsi_conn
*conn
,
314 struct iscsi_mgmt_task
*mtask
)
316 list_del_init(&mtask
->running
);
317 if (conn
->login_mtask
== mtask
)
320 if (conn
->ping_mtask
== mtask
)
321 conn
->ping_mtask
= NULL
;
322 __kfifo_put(conn
->session
->mgmtpool
.queue
,
323 (void*)&mtask
, sizeof(void*));
325 EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task
);
327 static struct iscsi_mgmt_task
*
328 __iscsi_conn_send_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
329 char *data
, uint32_t data_size
)
331 struct iscsi_session
*session
= conn
->session
;
332 struct iscsi_mgmt_task
*mtask
;
334 if (session
->state
== ISCSI_STATE_TERMINATE
)
337 if (hdr
->opcode
== (ISCSI_OP_LOGIN
| ISCSI_OP_IMMEDIATE
) ||
338 hdr
->opcode
== (ISCSI_OP_TEXT
| ISCSI_OP_IMMEDIATE
))
340 * Login and Text are sent serially, in
341 * request-followed-by-response sequence.
342 * Same mtask can be used. Same ITT must be used.
343 * Note that login_mtask is preallocated at conn_create().
345 mtask
= conn
->login_mtask
;
347 BUG_ON(conn
->c_stage
== ISCSI_CONN_INITIAL_STAGE
);
348 BUG_ON(conn
->c_stage
== ISCSI_CONN_STOPPED
);
350 if (!__kfifo_get(session
->mgmtpool
.queue
,
351 (void*)&mtask
, sizeof(void*)))
356 memcpy(mtask
->data
, data
, data_size
);
357 mtask
->data_count
= data_size
;
359 mtask
->data_count
= 0;
361 memcpy(mtask
->hdr
, hdr
, sizeof(struct iscsi_hdr
));
362 INIT_LIST_HEAD(&mtask
->running
);
363 list_add_tail(&mtask
->running
, &conn
->mgmtqueue
);
367 int iscsi_conn_send_pdu(struct iscsi_cls_conn
*cls_conn
, struct iscsi_hdr
*hdr
,
368 char *data
, uint32_t data_size
)
370 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
371 struct iscsi_session
*session
= conn
->session
;
374 spin_lock_bh(&session
->lock
);
375 if (!__iscsi_conn_send_pdu(conn
, hdr
, data
, data_size
))
377 spin_unlock_bh(&session
->lock
);
378 scsi_queue_work(session
->host
, &conn
->xmitwork
);
381 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu
);
384 * iscsi_cmd_rsp - SCSI Command Response processing
385 * @conn: iscsi connection
387 * @ctask: scsi command task
388 * @data: cmd data buffer
389 * @datalen: len of buffer
391 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
392 * then completes the command and task.
394 static void iscsi_scsi_cmd_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
395 struct iscsi_cmd_task
*ctask
, char *data
,
398 struct iscsi_cmd_rsp
*rhdr
= (struct iscsi_cmd_rsp
*)hdr
;
399 struct iscsi_session
*session
= conn
->session
;
400 struct scsi_cmnd
*sc
= ctask
->sc
;
402 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)rhdr
);
403 conn
->exp_statsn
= be32_to_cpu(rhdr
->statsn
) + 1;
405 sc
->result
= (DID_OK
<< 16) | rhdr
->cmd_status
;
407 if (rhdr
->response
!= ISCSI_STATUS_CMD_COMPLETED
) {
408 sc
->result
= DID_ERROR
<< 16;
412 if (rhdr
->cmd_status
== SAM_STAT_CHECK_CONDITION
) {
417 printk(KERN_ERR
"iscsi: Got CHECK_CONDITION but "
418 "invalid data buffer size of %d\n", datalen
);
419 sc
->result
= DID_BAD_TARGET
<< 16;
423 senselen
= be16_to_cpu(get_unaligned((__be16
*) data
));
424 if (datalen
< senselen
)
425 goto invalid_datalen
;
427 memcpy(sc
->sense_buffer
, data
+ 2,
428 min_t(uint16_t, senselen
, SCSI_SENSE_BUFFERSIZE
));
429 debug_scsi("copied %d bytes of sense\n",
430 min_t(uint16_t, senselen
, SCSI_SENSE_BUFFERSIZE
));
433 if (rhdr
->flags
& (ISCSI_FLAG_CMD_UNDERFLOW
|
434 ISCSI_FLAG_CMD_OVERFLOW
)) {
435 int res_count
= be32_to_cpu(rhdr
->residual_count
);
438 (rhdr
->flags
& ISCSI_FLAG_CMD_OVERFLOW
||
439 res_count
<= scsi_bufflen(sc
)))
440 scsi_set_resid(sc
, res_count
);
442 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
443 } else if (rhdr
->flags
& (ISCSI_FLAG_CMD_BIDI_UNDERFLOW
|
444 ISCSI_FLAG_CMD_BIDI_OVERFLOW
))
445 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
448 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
449 (long)sc
, sc
->result
, ctask
->itt
);
450 conn
->scsirsp_pdus_cnt
++;
452 __iscsi_put_ctask(ctask
);
455 static void iscsi_tmf_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
)
457 struct iscsi_tm_rsp
*tmf
= (struct iscsi_tm_rsp
*)hdr
;
459 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
460 conn
->tmfrsp_pdus_cnt
++;
462 if (conn
->tmf_state
!= TMF_QUEUED
)
465 if (tmf
->response
== ISCSI_TMF_RSP_COMPLETE
)
466 conn
->tmf_state
= TMF_SUCCESS
;
467 else if (tmf
->response
== ISCSI_TMF_RSP_NO_TASK
)
468 conn
->tmf_state
= TMF_NOT_FOUND
;
470 conn
->tmf_state
= TMF_FAILED
;
471 wake_up(&conn
->ehwait
);
474 static void iscsi_send_nopout(struct iscsi_conn
*conn
, struct iscsi_nopin
*rhdr
)
476 struct iscsi_nopout hdr
;
477 struct iscsi_mgmt_task
*mtask
;
479 if (!rhdr
&& conn
->ping_mtask
)
482 memset(&hdr
, 0, sizeof(struct iscsi_nopout
));
483 hdr
.opcode
= ISCSI_OP_NOOP_OUT
| ISCSI_OP_IMMEDIATE
;
484 hdr
.flags
= ISCSI_FLAG_CMD_FINAL
;
487 memcpy(hdr
.lun
, rhdr
->lun
, 8);
489 hdr
.itt
= RESERVED_ITT
;
491 hdr
.ttt
= RESERVED_ITT
;
493 mtask
= __iscsi_conn_send_pdu(conn
, (struct iscsi_hdr
*)&hdr
, NULL
, 0);
495 printk(KERN_ERR
"Could not send nopout\n");
499 /* only track our nops */
501 conn
->ping_mtask
= mtask
;
502 conn
->last_ping
= jiffies
;
504 scsi_queue_work(conn
->session
->host
, &conn
->xmitwork
);
507 static int iscsi_handle_reject(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
508 char *data
, int datalen
)
510 struct iscsi_reject
*reject
= (struct iscsi_reject
*)hdr
;
511 struct iscsi_hdr rejected_pdu
;
514 conn
->exp_statsn
= be32_to_cpu(reject
->statsn
) + 1;
516 if (reject
->reason
== ISCSI_REASON_DATA_DIGEST_ERROR
) {
517 if (ntoh24(reject
->dlength
) > datalen
)
518 return ISCSI_ERR_PROTO
;
520 if (ntoh24(reject
->dlength
) >= sizeof(struct iscsi_hdr
)) {
521 memcpy(&rejected_pdu
, data
, sizeof(struct iscsi_hdr
));
522 itt
= get_itt(rejected_pdu
.itt
);
523 printk(KERN_ERR
"itt 0x%x had pdu (op 0x%x) rejected "
524 "due to DataDigest error.\n", itt
,
525 rejected_pdu
.opcode
);
532 * __iscsi_complete_pdu - complete pdu
536 * @datalen: len of data buffer
538 * Completes pdu processing by freeing any resources allocated at
539 * queuecommand or send generic. session lock must be held and verify
540 * itt must have been called.
542 int __iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
543 char *data
, int datalen
)
545 struct iscsi_session
*session
= conn
->session
;
546 int opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
, rc
= 0;
547 struct iscsi_cmd_task
*ctask
;
548 struct iscsi_mgmt_task
*mtask
;
551 conn
->last_recv
= jiffies
;
552 if (hdr
->itt
!= RESERVED_ITT
)
553 itt
= get_itt(hdr
->itt
);
557 if (itt
< session
->cmds_max
) {
558 ctask
= session
->cmds
[itt
];
560 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
561 opcode
, conn
->id
, ctask
->itt
, datalen
);
564 case ISCSI_OP_SCSI_CMD_RSP
:
565 BUG_ON((void*)ctask
!= ctask
->sc
->SCp
.ptr
);
566 iscsi_scsi_cmd_rsp(conn
, hdr
, ctask
, data
,
569 case ISCSI_OP_SCSI_DATA_IN
:
570 BUG_ON((void*)ctask
!= ctask
->sc
->SCp
.ptr
);
571 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
572 conn
->scsirsp_pdus_cnt
++;
573 __iscsi_put_ctask(ctask
);
577 /* LLD handles this for now */
580 rc
= ISCSI_ERR_BAD_OPCODE
;
583 } else if (itt
>= ISCSI_MGMT_ITT_OFFSET
&&
584 itt
< ISCSI_MGMT_ITT_OFFSET
+ session
->mgmtpool_max
) {
585 mtask
= session
->mgmt_cmds
[itt
- ISCSI_MGMT_ITT_OFFSET
];
587 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
588 opcode
, conn
->id
, mtask
->itt
, datalen
);
590 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
592 case ISCSI_OP_LOGOUT_RSP
:
594 rc
= ISCSI_ERR_PROTO
;
597 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
599 case ISCSI_OP_LOGIN_RSP
:
600 case ISCSI_OP_TEXT_RSP
:
602 * login related PDU's exp_statsn is handled in
605 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
606 rc
= ISCSI_ERR_CONN_FAILED
;
607 iscsi_free_mgmt_task(conn
, mtask
);
609 case ISCSI_OP_SCSI_TMFUNC_RSP
:
611 rc
= ISCSI_ERR_PROTO
;
615 iscsi_tmf_rsp(conn
, hdr
);
616 iscsi_free_mgmt_task(conn
, mtask
);
618 case ISCSI_OP_NOOP_IN
:
619 if (hdr
->ttt
!= cpu_to_be32(ISCSI_RESERVED_TAG
) ||
621 rc
= ISCSI_ERR_PROTO
;
624 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
626 if (conn
->ping_mtask
!= mtask
) {
628 * If this is not in response to one of our
629 * nops then it must be from userspace.
631 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
,
633 rc
= ISCSI_ERR_CONN_FAILED
;
635 iscsi_free_mgmt_task(conn
, mtask
);
638 rc
= ISCSI_ERR_BAD_OPCODE
;
641 } else if (itt
== ~0U) {
642 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
645 case ISCSI_OP_NOOP_IN
:
647 rc
= ISCSI_ERR_PROTO
;
651 if (hdr
->ttt
== cpu_to_be32(ISCSI_RESERVED_TAG
))
654 iscsi_send_nopout(conn
, (struct iscsi_nopin
*)hdr
);
656 case ISCSI_OP_REJECT
:
657 rc
= iscsi_handle_reject(conn
, hdr
, data
, datalen
);
659 case ISCSI_OP_ASYNC_EVENT
:
660 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
661 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
662 rc
= ISCSI_ERR_CONN_FAILED
;
665 rc
= ISCSI_ERR_BAD_OPCODE
;
669 rc
= ISCSI_ERR_BAD_ITT
;
673 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu
);
675 int iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
676 char *data
, int datalen
)
680 spin_lock(&conn
->session
->lock
);
681 rc
= __iscsi_complete_pdu(conn
, hdr
, data
, datalen
);
682 spin_unlock(&conn
->session
->lock
);
685 EXPORT_SYMBOL_GPL(iscsi_complete_pdu
);
687 /* verify itt (itt encoding: age+cid+itt) */
688 int iscsi_verify_itt(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
691 struct iscsi_session
*session
= conn
->session
;
692 struct iscsi_cmd_task
*ctask
;
695 if (hdr
->itt
!= RESERVED_ITT
) {
696 if (((__force u32
)hdr
->itt
& ISCSI_AGE_MASK
) !=
697 (session
->age
<< ISCSI_AGE_SHIFT
)) {
698 printk(KERN_ERR
"iscsi: received itt %x expected "
699 "session age (%x)\n", (__force u32
)hdr
->itt
,
700 session
->age
& ISCSI_AGE_MASK
);
701 return ISCSI_ERR_BAD_ITT
;
704 if (((__force u32
)hdr
->itt
& ISCSI_CID_MASK
) !=
705 (conn
->id
<< ISCSI_CID_SHIFT
)) {
706 printk(KERN_ERR
"iscsi: received itt %x, expected "
707 "CID (%x)\n", (__force u32
)hdr
->itt
, conn
->id
);
708 return ISCSI_ERR_BAD_ITT
;
710 itt
= get_itt(hdr
->itt
);
714 if (itt
< session
->cmds_max
) {
715 ctask
= session
->cmds
[itt
];
718 printk(KERN_INFO
"iscsi: dropping ctask with "
719 "itt 0x%x\n", ctask
->itt
);
721 return ISCSI_ERR_NO_SCSI_CMD
;
724 if (ctask
->sc
->SCp
.phase
!= session
->age
) {
725 printk(KERN_ERR
"iscsi: ctask's session age %d, "
726 "expected %d\n", ctask
->sc
->SCp
.phase
,
728 return ISCSI_ERR_SESSION_FAILED
;
735 EXPORT_SYMBOL_GPL(iscsi_verify_itt
);
737 void iscsi_conn_failure(struct iscsi_conn
*conn
, enum iscsi_err err
)
739 struct iscsi_session
*session
= conn
->session
;
742 spin_lock_irqsave(&session
->lock
, flags
);
743 if (session
->state
== ISCSI_STATE_FAILED
) {
744 spin_unlock_irqrestore(&session
->lock
, flags
);
748 if (conn
->stop_stage
== 0)
749 session
->state
= ISCSI_STATE_FAILED
;
750 spin_unlock_irqrestore(&session
->lock
, flags
);
751 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
752 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
753 iscsi_conn_error(conn
->cls_conn
, err
);
755 EXPORT_SYMBOL_GPL(iscsi_conn_failure
);
757 static void iscsi_prep_mtask(struct iscsi_conn
*conn
,
758 struct iscsi_mgmt_task
*mtask
)
760 struct iscsi_session
*session
= conn
->session
;
761 struct iscsi_hdr
*hdr
= mtask
->hdr
;
762 struct iscsi_nopout
*nop
= (struct iscsi_nopout
*)hdr
;
764 if (hdr
->opcode
!= (ISCSI_OP_LOGIN
| ISCSI_OP_IMMEDIATE
) &&
765 hdr
->opcode
!= (ISCSI_OP_TEXT
| ISCSI_OP_IMMEDIATE
))
766 nop
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
768 * pre-format CmdSN for outgoing PDU.
770 nop
->cmdsn
= cpu_to_be32(session
->cmdsn
);
771 if (hdr
->itt
!= RESERVED_ITT
) {
772 hdr
->itt
= build_itt(mtask
->itt
, conn
->id
, session
->age
);
774 * TODO: We always use immediate, so we never hit this.
775 * If we start to send tmfs or nops as non-immediate then
776 * we should start checking the cmdsn numbers for mgmt tasks.
778 if (conn
->c_stage
== ISCSI_CONN_STARTED
&&
779 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
780 session
->queued_cmdsn
++;
785 if (session
->tt
->init_mgmt_task
)
786 session
->tt
->init_mgmt_task(conn
, mtask
);
788 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
789 hdr
->opcode
& ISCSI_OPCODE_MASK
, hdr
->itt
,
793 static int iscsi_xmit_mtask(struct iscsi_conn
*conn
)
795 struct iscsi_hdr
*hdr
= conn
->mtask
->hdr
;
798 if ((hdr
->opcode
& ISCSI_OPCODE_MASK
) == ISCSI_OP_LOGOUT
)
799 conn
->session
->state
= ISCSI_STATE_LOGGING_OUT
;
800 spin_unlock_bh(&conn
->session
->lock
);
802 rc
= conn
->session
->tt
->xmit_mgmt_task(conn
, conn
->mtask
);
803 spin_lock_bh(&conn
->session
->lock
);
807 /* done with this in-progress mtask */
812 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn
*conn
)
814 struct iscsi_session
*session
= conn
->session
;
817 * Check for iSCSI window and take care of CmdSN wrap-around
819 if (!iscsi_sna_lte(session
->queued_cmdsn
, session
->max_cmdsn
)) {
820 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
821 "CmdSN %u/%u\n", session
->exp_cmdsn
,
822 session
->max_cmdsn
, session
->cmdsn
,
823 session
->queued_cmdsn
);
829 static int iscsi_xmit_ctask(struct iscsi_conn
*conn
)
831 struct iscsi_cmd_task
*ctask
= conn
->ctask
;
834 __iscsi_get_ctask(ctask
);
835 spin_unlock_bh(&conn
->session
->lock
);
836 rc
= conn
->session
->tt
->xmit_cmd_task(conn
, ctask
);
837 spin_lock_bh(&conn
->session
->lock
);
838 __iscsi_put_ctask(ctask
);
840 /* done with this ctask */
846 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
847 * @ctask: ctask to requeue
849 * LLDs that need to run a ctask from the session workqueue should call
850 * this. The session lock must be held.
852 void iscsi_requeue_ctask(struct iscsi_cmd_task
*ctask
)
854 struct iscsi_conn
*conn
= ctask
->conn
;
856 list_move_tail(&ctask
->running
, &conn
->requeue
);
857 scsi_queue_work(conn
->session
->host
, &conn
->xmitwork
);
859 EXPORT_SYMBOL_GPL(iscsi_requeue_ctask
);
862 * iscsi_data_xmit - xmit any command into the scheduled connection
863 * @conn: iscsi connection
866 * The function can return -EAGAIN in which case the caller must
867 * re-schedule it again later or recover. '0' return code means
870 static int iscsi_data_xmit(struct iscsi_conn
*conn
)
874 spin_lock_bh(&conn
->session
->lock
);
875 if (unlikely(conn
->suspend_tx
)) {
876 debug_scsi("conn %d Tx suspended!\n", conn
->id
);
877 spin_unlock_bh(&conn
->session
->lock
);
882 rc
= iscsi_xmit_ctask(conn
);
888 rc
= iscsi_xmit_mtask(conn
);
894 * process mgmt pdus like nops before commands since we should
895 * only have one nop-out as a ping from us and targets should not
896 * overflow us with nop-ins
899 while (!list_empty(&conn
->mgmtqueue
)) {
900 conn
->mtask
= list_entry(conn
->mgmtqueue
.next
,
901 struct iscsi_mgmt_task
, running
);
902 if (conn
->session
->state
== ISCSI_STATE_LOGGING_OUT
) {
903 iscsi_free_mgmt_task(conn
, conn
->mtask
);
908 iscsi_prep_mtask(conn
, conn
->mtask
);
909 list_move_tail(conn
->mgmtqueue
.next
, &conn
->mgmt_run_list
);
910 rc
= iscsi_xmit_mtask(conn
);
915 /* process pending command queue */
916 while (!list_empty(&conn
->xmitqueue
)) {
917 if (conn
->tmf_state
== TMF_QUEUED
)
920 conn
->ctask
= list_entry(conn
->xmitqueue
.next
,
921 struct iscsi_cmd_task
, running
);
922 if (conn
->session
->state
== ISCSI_STATE_LOGGING_OUT
) {
923 fail_command(conn
, conn
->ctask
, DID_IMM_RETRY
<< 16);
926 if (iscsi_prep_scsi_cmd_pdu(conn
->ctask
)) {
927 fail_command(conn
, conn
->ctask
, DID_ABORT
<< 16);
930 conn
->session
->tt
->init_cmd_task(conn
->ctask
);
931 conn
->ctask
->state
= ISCSI_TASK_RUNNING
;
932 list_move_tail(conn
->xmitqueue
.next
, &conn
->run_list
);
933 rc
= iscsi_xmit_ctask(conn
);
937 * we could continuously get new ctask requests so
938 * we need to check the mgmt queue for nops that need to
939 * be sent to aviod starvation
941 if (!list_empty(&conn
->mgmtqueue
))
945 while (!list_empty(&conn
->requeue
)) {
946 if (conn
->session
->fast_abort
&& conn
->tmf_state
!= TMF_INITIAL
)
950 * we always do fastlogout - conn stop code will clean up.
952 if (conn
->session
->state
== ISCSI_STATE_LOGGING_OUT
)
955 conn
->ctask
= list_entry(conn
->requeue
.next
,
956 struct iscsi_cmd_task
, running
);
957 conn
->ctask
->state
= ISCSI_TASK_RUNNING
;
958 list_move_tail(conn
->requeue
.next
, &conn
->run_list
);
959 rc
= iscsi_xmit_ctask(conn
);
962 if (!list_empty(&conn
->mgmtqueue
))
965 spin_unlock_bh(&conn
->session
->lock
);
969 if (unlikely(conn
->suspend_tx
))
971 spin_unlock_bh(&conn
->session
->lock
);
975 static void iscsi_xmitworker(struct work_struct
*work
)
977 struct iscsi_conn
*conn
=
978 container_of(work
, struct iscsi_conn
, xmitwork
);
981 * serialize Xmit worker on a per-connection basis.
984 rc
= iscsi_data_xmit(conn
);
985 } while (rc
>= 0 || rc
== -EAGAIN
);
989 FAILURE_BAD_HOST
= 1,
990 FAILURE_SESSION_FAILED
,
991 FAILURE_SESSION_FREED
,
992 FAILURE_WINDOW_CLOSED
,
994 FAILURE_SESSION_TERMINATE
,
995 FAILURE_SESSION_IN_RECOVERY
,
996 FAILURE_SESSION_RECOVERY_TIMEOUT
,
997 FAILURE_SESSION_LOGGING_OUT
,
1000 int iscsi_queuecommand(struct scsi_cmnd
*sc
, void (*done
)(struct scsi_cmnd
*))
1002 struct Scsi_Host
*host
;
1004 struct iscsi_session
*session
;
1005 struct iscsi_conn
*conn
;
1006 struct iscsi_cmd_task
*ctask
= NULL
;
1008 sc
->scsi_done
= done
;
1012 host
= sc
->device
->host
;
1013 spin_unlock(host
->host_lock
);
1015 session
= iscsi_hostdata(host
->hostdata
);
1016 spin_lock(&session
->lock
);
1019 * ISCSI_STATE_FAILED is a temp. state. The recovery
1020 * code will decide what is best to do with command queued
1023 if (session
->state
!= ISCSI_STATE_LOGGED_IN
&&
1024 session
->state
!= ISCSI_STATE_FAILED
) {
1026 * to handle the race between when we set the recovery state
1027 * and block the session we requeue here (commands could
1028 * be entering our queuecommand while a block is starting
1029 * up because the block code is not locked)
1031 switch (session
->state
) {
1032 case ISCSI_STATE_IN_RECOVERY
:
1033 reason
= FAILURE_SESSION_IN_RECOVERY
;
1035 case ISCSI_STATE_LOGGING_OUT
:
1036 reason
= FAILURE_SESSION_LOGGING_OUT
;
1038 case ISCSI_STATE_RECOVERY_FAILED
:
1039 reason
= FAILURE_SESSION_RECOVERY_TIMEOUT
;
1041 case ISCSI_STATE_TERMINATE
:
1042 reason
= FAILURE_SESSION_TERMINATE
;
1045 reason
= FAILURE_SESSION_FREED
;
1050 conn
= session
->leadconn
;
1052 reason
= FAILURE_SESSION_FREED
;
1056 if (iscsi_check_cmdsn_window_closed(conn
)) {
1057 reason
= FAILURE_WINDOW_CLOSED
;
1061 if (!__kfifo_get(session
->cmdpool
.queue
, (void*)&ctask
,
1063 reason
= FAILURE_OOM
;
1066 session
->queued_cmdsn
++;
1068 sc
->SCp
.phase
= session
->age
;
1069 sc
->SCp
.ptr
= (char *)ctask
;
1071 atomic_set(&ctask
->refcount
, 1);
1072 ctask
->state
= ISCSI_TASK_PENDING
;
1075 INIT_LIST_HEAD(&ctask
->running
);
1077 list_add_tail(&ctask
->running
, &conn
->xmitqueue
);
1078 spin_unlock(&session
->lock
);
1080 scsi_queue_work(host
, &conn
->xmitwork
);
1081 spin_lock(host
->host_lock
);
1085 spin_unlock(&session
->lock
);
1086 debug_scsi("cmd 0x%x rejected (%d)\n", sc
->cmnd
[0], reason
);
1087 spin_lock(host
->host_lock
);
1088 return SCSI_MLQUEUE_HOST_BUSY
;
1091 spin_unlock(&session
->lock
);
1092 printk(KERN_ERR
"iscsi: cmd 0x%x is not queued (%d)\n",
1093 sc
->cmnd
[0], reason
);
1094 sc
->result
= (DID_NO_CONNECT
<< 16);
1095 scsi_set_resid(sc
, scsi_bufflen(sc
));
1097 spin_lock(host
->host_lock
);
1100 EXPORT_SYMBOL_GPL(iscsi_queuecommand
);
1102 int iscsi_change_queue_depth(struct scsi_device
*sdev
, int depth
)
1104 if (depth
> ISCSI_MAX_CMD_PER_LUN
)
1105 depth
= ISCSI_MAX_CMD_PER_LUN
;
1106 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), depth
);
1107 return sdev
->queue_depth
;
1109 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth
);
1111 void iscsi_session_recovery_timedout(struct iscsi_cls_session
*cls_session
)
1113 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1115 spin_lock_bh(&session
->lock
);
1116 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
1117 session
->state
= ISCSI_STATE_RECOVERY_FAILED
;
1118 if (session
->leadconn
)
1119 wake_up(&session
->leadconn
->ehwait
);
1121 spin_unlock_bh(&session
->lock
);
1123 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout
);
1125 int iscsi_eh_host_reset(struct scsi_cmnd
*sc
)
1127 struct Scsi_Host
*host
= sc
->device
->host
;
1128 struct iscsi_session
*session
= iscsi_hostdata(host
->hostdata
);
1129 struct iscsi_conn
*conn
= session
->leadconn
;
1131 mutex_lock(&session
->eh_mutex
);
1132 spin_lock_bh(&session
->lock
);
1133 if (session
->state
== ISCSI_STATE_TERMINATE
) {
1135 debug_scsi("failing host reset: session terminated "
1136 "[CID %d age %d]\n", conn
->id
, session
->age
);
1137 spin_unlock_bh(&session
->lock
);
1138 mutex_unlock(&session
->eh_mutex
);
1142 spin_unlock_bh(&session
->lock
);
1143 mutex_unlock(&session
->eh_mutex
);
1145 * we drop the lock here but the leadconn cannot be destoyed while
1146 * we are in the scsi eh
1148 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1150 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1151 wait_event_interruptible(conn
->ehwait
,
1152 session
->state
== ISCSI_STATE_TERMINATE
||
1153 session
->state
== ISCSI_STATE_LOGGED_IN
||
1154 session
->state
== ISCSI_STATE_RECOVERY_FAILED
);
1155 if (signal_pending(current
))
1156 flush_signals(current
);
1158 mutex_lock(&session
->eh_mutex
);
1159 spin_lock_bh(&session
->lock
);
1160 if (session
->state
== ISCSI_STATE_LOGGED_IN
)
1161 printk(KERN_INFO
"iscsi: host reset succeeded\n");
1164 spin_unlock_bh(&session
->lock
);
1165 mutex_unlock(&session
->eh_mutex
);
1168 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset
);
1170 static void iscsi_tmf_timedout(unsigned long data
)
1172 struct iscsi_conn
*conn
= (struct iscsi_conn
*)data
;
1173 struct iscsi_session
*session
= conn
->session
;
1175 spin_lock(&session
->lock
);
1176 if (conn
->tmf_state
== TMF_QUEUED
) {
1177 conn
->tmf_state
= TMF_TIMEDOUT
;
1178 debug_scsi("tmf timedout\n");
1179 /* unblock eh_abort() */
1180 wake_up(&conn
->ehwait
);
1182 spin_unlock(&session
->lock
);
1185 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn
*conn
,
1186 struct iscsi_tm
*hdr
, int age
,
1189 struct iscsi_session
*session
= conn
->session
;
1190 struct iscsi_mgmt_task
*mtask
;
1192 mtask
= __iscsi_conn_send_pdu(conn
, (struct iscsi_hdr
*)hdr
,
1195 spin_unlock_bh(&session
->lock
);
1196 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1197 spin_lock_bh(&session
->lock
);
1198 debug_scsi("tmf exec failure\n");
1201 conn
->tmfcmd_pdus_cnt
++;
1202 conn
->tmf_timer
.expires
= timeout
* HZ
+ jiffies
;
1203 conn
->tmf_timer
.function
= iscsi_tmf_timedout
;
1204 conn
->tmf_timer
.data
= (unsigned long)conn
;
1205 add_timer(&conn
->tmf_timer
);
1206 debug_scsi("tmf set timeout\n");
1208 spin_unlock_bh(&session
->lock
);
1209 mutex_unlock(&session
->eh_mutex
);
1210 scsi_queue_work(session
->host
, &conn
->xmitwork
);
1213 * block eh thread until:
1217 * 3) session is terminated or restarted or userspace has
1218 * given up on recovery
1220 wait_event_interruptible(conn
->ehwait
, age
!= session
->age
||
1221 session
->state
!= ISCSI_STATE_LOGGED_IN
||
1222 conn
->tmf_state
!= TMF_QUEUED
);
1223 if (signal_pending(current
))
1224 flush_signals(current
);
1225 del_timer_sync(&conn
->tmf_timer
);
1227 mutex_lock(&session
->eh_mutex
);
1228 spin_lock_bh(&session
->lock
);
1229 /* if the session drops it will clean up the mtask */
1230 if (age
!= session
->age
||
1231 session
->state
!= ISCSI_STATE_LOGGED_IN
)
1237 * Fail commands. session lock held and recv side suspended and xmit
1240 static void fail_all_commands(struct iscsi_conn
*conn
, unsigned lun
)
1242 struct iscsi_cmd_task
*ctask
, *tmp
;
1244 if (conn
->ctask
&& (conn
->ctask
->sc
->device
->lun
== lun
|| lun
== -1))
1248 list_for_each_entry_safe(ctask
, tmp
, &conn
->xmitqueue
, running
) {
1249 if (lun
== ctask
->sc
->device
->lun
|| lun
== -1) {
1250 debug_scsi("failing pending sc %p itt 0x%x\n",
1251 ctask
->sc
, ctask
->itt
);
1252 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1256 list_for_each_entry_safe(ctask
, tmp
, &conn
->requeue
, running
) {
1257 if (lun
== ctask
->sc
->device
->lun
|| lun
== -1) {
1258 debug_scsi("failing requeued sc %p itt 0x%x\n",
1259 ctask
->sc
, ctask
->itt
);
1260 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1264 /* fail all other running */
1265 list_for_each_entry_safe(ctask
, tmp
, &conn
->run_list
, running
) {
1266 if (lun
== ctask
->sc
->device
->lun
|| lun
== -1) {
1267 debug_scsi("failing in progress sc %p itt 0x%x\n",
1268 ctask
->sc
, ctask
->itt
);
1269 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1274 static void iscsi_suspend_tx(struct iscsi_conn
*conn
)
1276 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1277 scsi_flush_work(conn
->session
->host
);
1280 static void iscsi_start_tx(struct iscsi_conn
*conn
)
1282 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1283 scsi_queue_work(conn
->session
->host
, &conn
->xmitwork
);
1286 static enum scsi_eh_timer_return
iscsi_eh_cmd_timed_out(struct scsi_cmnd
*scmd
)
1288 struct iscsi_cls_session
*cls_session
;
1289 struct iscsi_session
*session
;
1290 struct iscsi_conn
*conn
;
1291 enum scsi_eh_timer_return rc
= EH_NOT_HANDLED
;
1293 cls_session
= starget_to_session(scsi_target(scmd
->device
));
1294 session
= class_to_transport_session(cls_session
);
1296 debug_scsi("scsi cmd %p timedout\n", scmd
);
1298 spin_lock(&session
->lock
);
1299 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
1301 * We are probably in the middle of iscsi recovery so let
1302 * that complete and handle the error.
1304 rc
= EH_RESET_TIMER
;
1308 conn
= session
->leadconn
;
1310 /* In the middle of shuting down */
1311 rc
= EH_RESET_TIMER
;
1315 if (!conn
->recv_timeout
&& !conn
->ping_timeout
)
1318 * if the ping timedout then we are in the middle of cleaning up
1319 * and can let the iscsi eh handle it
1321 if (time_before_eq(conn
->last_recv
+ (conn
->recv_timeout
* HZ
) +
1322 (conn
->ping_timeout
* HZ
), jiffies
))
1323 rc
= EH_RESET_TIMER
;
1325 * if we are about to check the transport then give the command
1328 if (time_before_eq(conn
->last_recv
+ (conn
->recv_timeout
* HZ
),
1330 rc
= EH_RESET_TIMER
;
1331 /* if in the middle of checking the transport then give us more time */
1332 if (conn
->ping_mtask
)
1333 rc
= EH_RESET_TIMER
;
1335 spin_unlock(&session
->lock
);
1336 debug_scsi("return %s\n", rc
== EH_RESET_TIMER
? "timer reset" : "nh");
1340 static void iscsi_check_transport_timeouts(unsigned long data
)
1342 struct iscsi_conn
*conn
= (struct iscsi_conn
*)data
;
1343 struct iscsi_session
*session
= conn
->session
;
1344 unsigned long timeout
, next_timeout
= 0, last_recv
;
1346 spin_lock(&session
->lock
);
1347 if (session
->state
!= ISCSI_STATE_LOGGED_IN
)
1350 timeout
= conn
->recv_timeout
;
1355 last_recv
= conn
->last_recv
;
1356 if (time_before_eq(last_recv
+ timeout
+ (conn
->ping_timeout
* HZ
),
1358 printk(KERN_ERR
"ping timeout of %d secs expired, "
1359 "last rx %lu, last ping %lu, now %lu\n",
1360 conn
->ping_timeout
, last_recv
,
1361 conn
->last_ping
, jiffies
);
1362 spin_unlock(&session
->lock
);
1363 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1367 if (time_before_eq(last_recv
+ timeout
, jiffies
)) {
1368 if (time_before_eq(conn
->last_ping
, last_recv
)) {
1369 /* send a ping to try to provoke some traffic */
1370 debug_scsi("Sending nopout as ping on conn %p\n", conn
);
1371 iscsi_send_nopout(conn
, NULL
);
1373 next_timeout
= last_recv
+ timeout
+ (conn
->ping_timeout
* HZ
);
1375 next_timeout
= last_recv
+ timeout
;
1379 debug_scsi("Setting next tmo %lu\n", next_timeout
);
1380 mod_timer(&conn
->transport_timer
, next_timeout
);
1383 spin_unlock(&session
->lock
);
1386 static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task
*ctask
,
1387 struct iscsi_tm
*hdr
)
1389 memset(hdr
, 0, sizeof(*hdr
));
1390 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
1391 hdr
->flags
= ISCSI_TM_FUNC_ABORT_TASK
& ISCSI_FLAG_TM_FUNC_MASK
;
1392 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
1393 memcpy(hdr
->lun
, ctask
->hdr
->lun
, sizeof(hdr
->lun
));
1394 hdr
->rtt
= ctask
->hdr
->itt
;
1395 hdr
->refcmdsn
= ctask
->hdr
->cmdsn
;
1398 int iscsi_eh_abort(struct scsi_cmnd
*sc
)
1400 struct Scsi_Host
*host
= sc
->device
->host
;
1401 struct iscsi_session
*session
= iscsi_hostdata(host
->hostdata
);
1402 struct iscsi_conn
*conn
;
1403 struct iscsi_cmd_task
*ctask
;
1404 struct iscsi_tm
*hdr
;
1407 mutex_lock(&session
->eh_mutex
);
1408 spin_lock_bh(&session
->lock
);
1410 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1414 debug_scsi("sc never reached iscsi layer or it completed.\n");
1415 spin_unlock_bh(&session
->lock
);
1416 mutex_unlock(&session
->eh_mutex
);
1421 * If we are not logged in or we have started a new session
1422 * then let the host reset code handle this
1424 if (!session
->leadconn
|| session
->state
!= ISCSI_STATE_LOGGED_IN
||
1425 sc
->SCp
.phase
!= session
->age
) {
1426 spin_unlock_bh(&session
->lock
);
1427 mutex_unlock(&session
->eh_mutex
);
1431 conn
= session
->leadconn
;
1432 conn
->eh_abort_cnt
++;
1435 ctask
= (struct iscsi_cmd_task
*)sc
->SCp
.ptr
;
1436 debug_scsi("aborting [sc %p itt 0x%x]\n", sc
, ctask
->itt
);
1438 /* ctask completed before time out */
1440 debug_scsi("sc completed while abort in progress\n");
1444 if (ctask
->state
== ISCSI_TASK_PENDING
) {
1445 fail_command(conn
, ctask
, DID_ABORT
<< 16);
1449 /* only have one tmf outstanding at a time */
1450 if (conn
->tmf_state
!= TMF_INITIAL
)
1452 conn
->tmf_state
= TMF_QUEUED
;
1455 iscsi_prep_abort_task_pdu(ctask
, hdr
);
1457 if (iscsi_exec_task_mgmt_fn(conn
, hdr
, age
, session
->abort_timeout
)) {
1462 switch (conn
->tmf_state
) {
1464 spin_unlock_bh(&session
->lock
);
1465 iscsi_suspend_tx(conn
);
1467 * clean up task if aborted. grab the recv lock as a writer
1469 write_lock_bh(conn
->recv_lock
);
1470 spin_lock(&session
->lock
);
1471 fail_command(conn
, ctask
, DID_ABORT
<< 16);
1472 conn
->tmf_state
= TMF_INITIAL
;
1473 spin_unlock(&session
->lock
);
1474 write_unlock_bh(conn
->recv_lock
);
1475 iscsi_start_tx(conn
);
1476 goto success_unlocked
;
1478 spin_unlock_bh(&session
->lock
);
1479 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1480 goto failed_unlocked
;
1483 conn
->tmf_state
= TMF_INITIAL
;
1484 /* ctask completed before tmf abort response */
1485 debug_scsi("sc completed while abort in progress\n");
1490 conn
->tmf_state
= TMF_INITIAL
;
1495 spin_unlock_bh(&session
->lock
);
1497 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc
, ctask
->itt
);
1498 mutex_unlock(&session
->eh_mutex
);
1502 spin_unlock_bh(&session
->lock
);
1504 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc
,
1505 ctask
? ctask
->itt
: 0);
1506 mutex_unlock(&session
->eh_mutex
);
1509 EXPORT_SYMBOL_GPL(iscsi_eh_abort
);
1511 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd
*sc
, struct iscsi_tm
*hdr
)
1513 memset(hdr
, 0, sizeof(*hdr
));
1514 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
1515 hdr
->flags
= ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
& ISCSI_FLAG_TM_FUNC_MASK
;
1516 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
1517 int_to_scsilun(sc
->device
->lun
, (struct scsi_lun
*)hdr
->lun
);
1518 hdr
->rtt
= RESERVED_ITT
;
1521 int iscsi_eh_device_reset(struct scsi_cmnd
*sc
)
1523 struct Scsi_Host
*host
= sc
->device
->host
;
1524 struct iscsi_session
*session
= iscsi_hostdata(host
->hostdata
);
1525 struct iscsi_conn
*conn
;
1526 struct iscsi_tm
*hdr
;
1529 debug_scsi("LU Reset [sc %p lun %u]\n", sc
, sc
->device
->lun
);
1531 mutex_lock(&session
->eh_mutex
);
1532 spin_lock_bh(&session
->lock
);
1534 * Just check if we are not logged in. We cannot check for
1535 * the phase because the reset could come from a ioctl.
1537 if (!session
->leadconn
|| session
->state
!= ISCSI_STATE_LOGGED_IN
)
1539 conn
= session
->leadconn
;
1541 /* only have one tmf outstanding at a time */
1542 if (conn
->tmf_state
!= TMF_INITIAL
)
1544 conn
->tmf_state
= TMF_QUEUED
;
1547 iscsi_prep_lun_reset_pdu(sc
, hdr
);
1549 if (iscsi_exec_task_mgmt_fn(conn
, hdr
, session
->age
,
1550 session
->lu_reset_timeout
)) {
1555 switch (conn
->tmf_state
) {
1559 spin_unlock_bh(&session
->lock
);
1560 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1563 conn
->tmf_state
= TMF_INITIAL
;
1568 spin_unlock_bh(&session
->lock
);
1570 iscsi_suspend_tx(conn
);
1571 /* need to grab the recv lock then session lock */
1572 write_lock_bh(conn
->recv_lock
);
1573 spin_lock(&session
->lock
);
1574 fail_all_commands(conn
, sc
->device
->lun
);
1575 conn
->tmf_state
= TMF_INITIAL
;
1576 spin_unlock(&session
->lock
);
1577 write_unlock_bh(conn
->recv_lock
);
1579 iscsi_start_tx(conn
);
1583 spin_unlock_bh(&session
->lock
);
1585 debug_scsi("iscsi_eh_device_reset %s\n",
1586 rc
== SUCCESS
? "SUCCESS" : "FAILED");
1587 mutex_unlock(&session
->eh_mutex
);
1590 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset
);
1593 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1594 * should be accessed via kfifo_{get,put} on q->queue.
1595 * Optionally, the caller can obtain the array of object pointers
1596 * by passing in a non-NULL @items pointer
1599 iscsi_pool_init(struct iscsi_pool
*q
, int max
, void ***items
, int item_size
)
1601 int i
, num_arrays
= 1;
1603 memset(q
, 0, sizeof(*q
));
1607 /* If the user passed an items pointer, he wants a copy of
1611 q
->pool
= kzalloc(num_arrays
* max
* sizeof(void*), GFP_KERNEL
);
1612 if (q
->pool
== NULL
)
1615 q
->queue
= kfifo_init((void*)q
->pool
, max
* sizeof(void*),
1617 if (q
->queue
== ERR_PTR(-ENOMEM
))
1620 for (i
= 0; i
< max
; i
++) {
1621 q
->pool
[i
] = kzalloc(item_size
, GFP_KERNEL
);
1622 if (q
->pool
[i
] == NULL
) {
1626 __kfifo_put(q
->queue
, (void*)&q
->pool
[i
], sizeof(void*));
1630 *items
= q
->pool
+ max
;
1631 memcpy(*items
, q
->pool
, max
* sizeof(void *));
1640 EXPORT_SYMBOL_GPL(iscsi_pool_init
);
1642 void iscsi_pool_free(struct iscsi_pool
*q
)
1646 for (i
= 0; i
< q
->max
; i
++)
1651 EXPORT_SYMBOL_GPL(iscsi_pool_free
);
1654 * iSCSI Session's hostdata organization:
1656 * *------------------* <== hostdata_session(host->hostdata)
1657 * | ptr to class sess|
1658 * |------------------| <== iscsi_hostdata(host->hostdata)
1660 * *------------------*
1663 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1664 _sz % sizeof(unsigned long))
1666 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1669 * iscsi_session_setup - create iscsi cls session and host and session
1670 * @scsit: scsi transport template
1671 * @iscsit: iscsi transport template
1672 * @cmds_max: scsi host can queue
1673 * @qdepth: scsi host cmds per lun
1674 * @cmd_task_size: LLD ctask private data size
1675 * @mgmt_task_size: LLD mtask private data size
1676 * @initial_cmdsn: initial CmdSN
1677 * @hostno: host no allocated
1679 * This can be used by software iscsi_transports that allocate
1680 * a session per scsi host.
1682 struct iscsi_cls_session
*
1683 iscsi_session_setup(struct iscsi_transport
*iscsit
,
1684 struct scsi_transport_template
*scsit
,
1685 uint16_t cmds_max
, uint16_t qdepth
,
1686 int cmd_task_size
, int mgmt_task_size
,
1687 uint32_t initial_cmdsn
, uint32_t *hostno
)
1689 struct Scsi_Host
*shost
;
1690 struct iscsi_session
*session
;
1691 struct iscsi_cls_session
*cls_session
;
1694 if (qdepth
> ISCSI_MAX_CMD_PER_LUN
|| qdepth
< 1) {
1696 printk(KERN_ERR
"iscsi: invalid queue depth of %d. "
1697 "Queue depth must be between 1 and %d.\n",
1698 qdepth
, ISCSI_MAX_CMD_PER_LUN
);
1699 qdepth
= ISCSI_DEF_CMD_PER_LUN
;
1702 if (cmds_max
< 2 || (cmds_max
& (cmds_max
- 1)) ||
1703 cmds_max
>= ISCSI_MGMT_ITT_OFFSET
) {
1705 printk(KERN_ERR
"iscsi: invalid can_queue of %d. "
1706 "can_queue must be a power of 2 and between "
1707 "2 and %d - setting to %d.\n", cmds_max
,
1708 ISCSI_MGMT_ITT_OFFSET
, ISCSI_DEF_XMIT_CMDS_MAX
);
1709 cmds_max
= ISCSI_DEF_XMIT_CMDS_MAX
;
1712 shost
= scsi_host_alloc(iscsit
->host_template
,
1713 hostdata_privsize(sizeof(*session
)));
1717 /* the iscsi layer takes one task for reserve */
1718 shost
->can_queue
= cmds_max
- 1;
1719 shost
->cmd_per_lun
= qdepth
;
1721 shost
->max_channel
= 0;
1722 shost
->max_lun
= iscsit
->max_lun
;
1723 shost
->max_cmd_len
= iscsit
->max_cmd_len
;
1724 shost
->transportt
= scsit
;
1725 shost
->transportt
->create_work_queue
= 1;
1726 shost
->transportt
->eh_timed_out
= iscsi_eh_cmd_timed_out
;
1727 *hostno
= shost
->host_no
;
1729 session
= iscsi_hostdata(shost
->hostdata
);
1730 memset(session
, 0, sizeof(struct iscsi_session
));
1731 session
->host
= shost
;
1732 session
->state
= ISCSI_STATE_FREE
;
1733 session
->fast_abort
= 1;
1734 session
->mgmtpool_max
= ISCSI_MGMT_CMDS_MAX
;
1735 session
->cmds_max
= cmds_max
;
1736 session
->queued_cmdsn
= session
->cmdsn
= initial_cmdsn
;
1737 session
->exp_cmdsn
= initial_cmdsn
+ 1;
1738 session
->max_cmdsn
= initial_cmdsn
+ 1;
1739 session
->max_r2t
= 1;
1740 session
->tt
= iscsit
;
1741 mutex_init(&session
->eh_mutex
);
1743 /* initialize SCSI PDU commands pool */
1744 if (iscsi_pool_init(&session
->cmdpool
, session
->cmds_max
,
1745 (void***)&session
->cmds
,
1746 cmd_task_size
+ sizeof(struct iscsi_cmd_task
)))
1747 goto cmdpool_alloc_fail
;
1749 /* pre-format cmds pool with ITT */
1750 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
1751 struct iscsi_cmd_task
*ctask
= session
->cmds
[cmd_i
];
1754 ctask
->dd_data
= &ctask
[1];
1756 INIT_LIST_HEAD(&ctask
->running
);
1759 spin_lock_init(&session
->lock
);
1761 /* initialize immediate command pool */
1762 if (iscsi_pool_init(&session
->mgmtpool
, session
->mgmtpool_max
,
1763 (void***)&session
->mgmt_cmds
,
1764 mgmt_task_size
+ sizeof(struct iscsi_mgmt_task
)))
1765 goto mgmtpool_alloc_fail
;
1768 /* pre-format immediate cmds pool with ITT */
1769 for (cmd_i
= 0; cmd_i
< session
->mgmtpool_max
; cmd_i
++) {
1770 struct iscsi_mgmt_task
*mtask
= session
->mgmt_cmds
[cmd_i
];
1773 mtask
->dd_data
= &mtask
[1];
1774 mtask
->itt
= ISCSI_MGMT_ITT_OFFSET
+ cmd_i
;
1775 INIT_LIST_HEAD(&mtask
->running
);
1778 if (scsi_add_host(shost
, NULL
))
1781 if (!try_module_get(iscsit
->owner
))
1782 goto cls_session_fail
;
1784 cls_session
= iscsi_create_session(shost
, iscsit
, 0);
1787 *(unsigned long*)shost
->hostdata
= (unsigned long)cls_session
;
1792 module_put(iscsit
->owner
);
1794 scsi_remove_host(shost
);
1796 iscsi_pool_free(&session
->mgmtpool
);
1797 mgmtpool_alloc_fail
:
1798 iscsi_pool_free(&session
->cmdpool
);
1800 scsi_host_put(shost
);
1803 EXPORT_SYMBOL_GPL(iscsi_session_setup
);
1806 * iscsi_session_teardown - destroy session, host, and cls_session
1809 * This can be used by software iscsi_transports that allocate
1810 * a session per scsi host.
1812 void iscsi_session_teardown(struct iscsi_cls_session
*cls_session
)
1814 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
1815 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
1816 struct module
*owner
= cls_session
->transport
->owner
;
1818 iscsi_remove_session(cls_session
);
1819 scsi_remove_host(shost
);
1821 iscsi_pool_free(&session
->mgmtpool
);
1822 iscsi_pool_free(&session
->cmdpool
);
1824 kfree(session
->password
);
1825 kfree(session
->password_in
);
1826 kfree(session
->username
);
1827 kfree(session
->username_in
);
1828 kfree(session
->targetname
);
1829 kfree(session
->netdev
);
1830 kfree(session
->hwaddress
);
1831 kfree(session
->initiatorname
);
1833 iscsi_free_session(cls_session
);
1834 scsi_host_put(shost
);
1837 EXPORT_SYMBOL_GPL(iscsi_session_teardown
);
1840 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1841 * @cls_session: iscsi_cls_session
1844 struct iscsi_cls_conn
*
1845 iscsi_conn_setup(struct iscsi_cls_session
*cls_session
, uint32_t conn_idx
)
1847 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1848 struct iscsi_conn
*conn
;
1849 struct iscsi_cls_conn
*cls_conn
;
1852 cls_conn
= iscsi_create_conn(cls_session
, conn_idx
);
1855 conn
= cls_conn
->dd_data
;
1856 memset(conn
, 0, sizeof(*conn
));
1858 conn
->session
= session
;
1859 conn
->cls_conn
= cls_conn
;
1860 conn
->c_stage
= ISCSI_CONN_INITIAL_STAGE
;
1861 conn
->id
= conn_idx
;
1862 conn
->exp_statsn
= 0;
1863 conn
->tmf_state
= TMF_INITIAL
;
1865 init_timer(&conn
->transport_timer
);
1866 conn
->transport_timer
.data
= (unsigned long)conn
;
1867 conn
->transport_timer
.function
= iscsi_check_transport_timeouts
;
1869 INIT_LIST_HEAD(&conn
->run_list
);
1870 INIT_LIST_HEAD(&conn
->mgmt_run_list
);
1871 INIT_LIST_HEAD(&conn
->mgmtqueue
);
1872 INIT_LIST_HEAD(&conn
->xmitqueue
);
1873 INIT_LIST_HEAD(&conn
->requeue
);
1874 INIT_WORK(&conn
->xmitwork
, iscsi_xmitworker
);
1876 /* allocate login_mtask used for the login/text sequences */
1877 spin_lock_bh(&session
->lock
);
1878 if (!__kfifo_get(session
->mgmtpool
.queue
,
1879 (void*)&conn
->login_mtask
,
1881 spin_unlock_bh(&session
->lock
);
1882 goto login_mtask_alloc_fail
;
1884 spin_unlock_bh(&session
->lock
);
1886 data
= kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN
, GFP_KERNEL
);
1888 goto login_mtask_data_alloc_fail
;
1889 conn
->login_mtask
->data
= conn
->data
= data
;
1891 init_timer(&conn
->tmf_timer
);
1892 init_waitqueue_head(&conn
->ehwait
);
1896 login_mtask_data_alloc_fail
:
1897 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->login_mtask
,
1899 login_mtask_alloc_fail
:
1900 iscsi_destroy_conn(cls_conn
);
1903 EXPORT_SYMBOL_GPL(iscsi_conn_setup
);
1906 * iscsi_conn_teardown - teardown iscsi connection
1907 * cls_conn: iscsi class connection
1909 * TODO: we may need to make this into a two step process
1910 * like scsi-mls remove + put host
1912 void iscsi_conn_teardown(struct iscsi_cls_conn
*cls_conn
)
1914 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1915 struct iscsi_session
*session
= conn
->session
;
1916 unsigned long flags
;
1918 del_timer_sync(&conn
->transport_timer
);
1920 spin_lock_bh(&session
->lock
);
1921 conn
->c_stage
= ISCSI_CONN_CLEANUP_WAIT
;
1922 if (session
->leadconn
== conn
) {
1924 * leading connection? then give up on recovery.
1926 session
->state
= ISCSI_STATE_TERMINATE
;
1927 wake_up(&conn
->ehwait
);
1929 spin_unlock_bh(&session
->lock
);
1932 * Block until all in-progress commands for this connection
1936 spin_lock_irqsave(session
->host
->host_lock
, flags
);
1937 if (!session
->host
->host_busy
) { /* OK for ERL == 0 */
1938 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
1941 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
1942 msleep_interruptible(500);
1943 printk(KERN_INFO
"iscsi: scsi conn_destroy(): host_busy %d "
1944 "host_failed %d\n", session
->host
->host_busy
,
1945 session
->host
->host_failed
);
1947 * force eh_abort() to unblock
1949 wake_up(&conn
->ehwait
);
1952 /* flush queued up work because we free the connection below */
1953 iscsi_suspend_tx(conn
);
1955 spin_lock_bh(&session
->lock
);
1957 kfree(conn
->persistent_address
);
1958 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->login_mtask
,
1960 if (session
->leadconn
== conn
)
1961 session
->leadconn
= NULL
;
1962 spin_unlock_bh(&session
->lock
);
1964 iscsi_destroy_conn(cls_conn
);
1966 EXPORT_SYMBOL_GPL(iscsi_conn_teardown
);
1968 int iscsi_conn_start(struct iscsi_cls_conn
*cls_conn
)
1970 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1971 struct iscsi_session
*session
= conn
->session
;
1974 printk(KERN_ERR
"iscsi: can't start unbound connection\n");
1978 if ((session
->imm_data_en
|| !session
->initial_r2t_en
) &&
1979 session
->first_burst
> session
->max_burst
) {
1980 printk("iscsi: invalid burst lengths: "
1981 "first_burst %d max_burst %d\n",
1982 session
->first_burst
, session
->max_burst
);
1986 if (conn
->ping_timeout
&& !conn
->recv_timeout
) {
1987 printk(KERN_ERR
"iscsi: invalid recv timeout of zero "
1988 "Using 5 seconds\n.");
1989 conn
->recv_timeout
= 5;
1992 if (conn
->recv_timeout
&& !conn
->ping_timeout
) {
1993 printk(KERN_ERR
"iscsi: invalid ping timeout of zero "
1994 "Using 5 seconds.\n");
1995 conn
->ping_timeout
= 5;
1998 spin_lock_bh(&session
->lock
);
1999 conn
->c_stage
= ISCSI_CONN_STARTED
;
2000 session
->state
= ISCSI_STATE_LOGGED_IN
;
2001 session
->queued_cmdsn
= session
->cmdsn
;
2003 conn
->last_recv
= jiffies
;
2004 conn
->last_ping
= jiffies
;
2005 if (conn
->recv_timeout
&& conn
->ping_timeout
)
2006 mod_timer(&conn
->transport_timer
,
2007 jiffies
+ (conn
->recv_timeout
* HZ
));
2009 switch(conn
->stop_stage
) {
2010 case STOP_CONN_RECOVER
:
2012 * unblock eh_abort() if it is blocked. re-try all
2013 * commands after successful recovery
2015 conn
->stop_stage
= 0;
2016 conn
->tmf_state
= TMF_INITIAL
;
2018 spin_unlock_bh(&session
->lock
);
2020 iscsi_unblock_session(session_to_cls(session
));
2021 wake_up(&conn
->ehwait
);
2023 case STOP_CONN_TERM
:
2024 conn
->stop_stage
= 0;
2029 spin_unlock_bh(&session
->lock
);
2033 EXPORT_SYMBOL_GPL(iscsi_conn_start
);
2036 flush_control_queues(struct iscsi_session
*session
, struct iscsi_conn
*conn
)
2038 struct iscsi_mgmt_task
*mtask
, *tmp
;
2040 /* handle pending */
2041 list_for_each_entry_safe(mtask
, tmp
, &conn
->mgmtqueue
, running
) {
2042 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask
->itt
);
2043 iscsi_free_mgmt_task(conn
, mtask
);
2046 /* handle running */
2047 list_for_each_entry_safe(mtask
, tmp
, &conn
->mgmt_run_list
, running
) {
2048 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask
->itt
);
2049 iscsi_free_mgmt_task(conn
, mtask
);
2055 static void iscsi_start_session_recovery(struct iscsi_session
*session
,
2056 struct iscsi_conn
*conn
, int flag
)
2060 del_timer_sync(&conn
->transport_timer
);
2062 mutex_lock(&session
->eh_mutex
);
2063 spin_lock_bh(&session
->lock
);
2064 if (conn
->stop_stage
== STOP_CONN_TERM
) {
2065 spin_unlock_bh(&session
->lock
);
2066 mutex_unlock(&session
->eh_mutex
);
2071 * The LLD either freed/unset the lock on us, or userspace called
2072 * stop but did not create a proper connection (connection was never
2073 * bound or it was unbound then stop was called).
2075 if (!conn
->recv_lock
) {
2076 spin_unlock_bh(&session
->lock
);
2077 mutex_unlock(&session
->eh_mutex
);
2082 * When this is called for the in_login state, we only want to clean
2083 * up the login task and connection. We do not need to block and set
2084 * the recovery state again
2086 if (flag
== STOP_CONN_TERM
)
2087 session
->state
= ISCSI_STATE_TERMINATE
;
2088 else if (conn
->stop_stage
!= STOP_CONN_RECOVER
)
2089 session
->state
= ISCSI_STATE_IN_RECOVERY
;
2091 old_stop_stage
= conn
->stop_stage
;
2092 conn
->stop_stage
= flag
;
2093 conn
->c_stage
= ISCSI_CONN_STOPPED
;
2094 spin_unlock_bh(&session
->lock
);
2096 iscsi_suspend_tx(conn
);
2098 write_lock_bh(conn
->recv_lock
);
2099 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
2100 write_unlock_bh(conn
->recv_lock
);
2103 * for connection level recovery we should not calculate
2104 * header digest. conn->hdr_size used for optimization
2105 * in hdr_extract() and will be re-negotiated at
2108 if (flag
== STOP_CONN_RECOVER
) {
2109 conn
->hdrdgst_en
= 0;
2110 conn
->datadgst_en
= 0;
2111 if (session
->state
== ISCSI_STATE_IN_RECOVERY
&&
2112 old_stop_stage
!= STOP_CONN_RECOVER
) {
2113 debug_scsi("blocking session\n");
2114 iscsi_block_session(session_to_cls(session
));
2121 spin_lock_bh(&session
->lock
);
2122 fail_all_commands(conn
, -1);
2123 flush_control_queues(session
, conn
);
2124 spin_unlock_bh(&session
->lock
);
2125 mutex_unlock(&session
->eh_mutex
);
2128 void iscsi_conn_stop(struct iscsi_cls_conn
*cls_conn
, int flag
)
2130 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2131 struct iscsi_session
*session
= conn
->session
;
2134 case STOP_CONN_RECOVER
:
2135 case STOP_CONN_TERM
:
2136 iscsi_start_session_recovery(session
, conn
, flag
);
2139 printk(KERN_ERR
"iscsi: invalid stop flag %d\n", flag
);
2142 EXPORT_SYMBOL_GPL(iscsi_conn_stop
);
2144 int iscsi_conn_bind(struct iscsi_cls_session
*cls_session
,
2145 struct iscsi_cls_conn
*cls_conn
, int is_leading
)
2147 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
2148 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2150 spin_lock_bh(&session
->lock
);
2152 session
->leadconn
= conn
;
2153 spin_unlock_bh(&session
->lock
);
2156 * Unblock xmitworker(), Login Phase will pass through.
2158 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
2159 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
2162 EXPORT_SYMBOL_GPL(iscsi_conn_bind
);
2165 int iscsi_set_param(struct iscsi_cls_conn
*cls_conn
,
2166 enum iscsi_param param
, char *buf
, int buflen
)
2168 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2169 struct iscsi_session
*session
= conn
->session
;
2173 case ISCSI_PARAM_FAST_ABORT
:
2174 sscanf(buf
, "%d", &session
->fast_abort
);
2176 case ISCSI_PARAM_ABORT_TMO
:
2177 sscanf(buf
, "%d", &session
->abort_timeout
);
2179 case ISCSI_PARAM_LU_RESET_TMO
:
2180 sscanf(buf
, "%d", &session
->lu_reset_timeout
);
2182 case ISCSI_PARAM_PING_TMO
:
2183 sscanf(buf
, "%d", &conn
->ping_timeout
);
2185 case ISCSI_PARAM_RECV_TMO
:
2186 sscanf(buf
, "%d", &conn
->recv_timeout
);
2188 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
2189 sscanf(buf
, "%d", &conn
->max_recv_dlength
);
2191 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
2192 sscanf(buf
, "%d", &conn
->max_xmit_dlength
);
2194 case ISCSI_PARAM_HDRDGST_EN
:
2195 sscanf(buf
, "%d", &conn
->hdrdgst_en
);
2197 case ISCSI_PARAM_DATADGST_EN
:
2198 sscanf(buf
, "%d", &conn
->datadgst_en
);
2200 case ISCSI_PARAM_INITIAL_R2T_EN
:
2201 sscanf(buf
, "%d", &session
->initial_r2t_en
);
2203 case ISCSI_PARAM_MAX_R2T
:
2204 sscanf(buf
, "%d", &session
->max_r2t
);
2206 case ISCSI_PARAM_IMM_DATA_EN
:
2207 sscanf(buf
, "%d", &session
->imm_data_en
);
2209 case ISCSI_PARAM_FIRST_BURST
:
2210 sscanf(buf
, "%d", &session
->first_burst
);
2212 case ISCSI_PARAM_MAX_BURST
:
2213 sscanf(buf
, "%d", &session
->max_burst
);
2215 case ISCSI_PARAM_PDU_INORDER_EN
:
2216 sscanf(buf
, "%d", &session
->pdu_inorder_en
);
2218 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
2219 sscanf(buf
, "%d", &session
->dataseq_inorder_en
);
2221 case ISCSI_PARAM_ERL
:
2222 sscanf(buf
, "%d", &session
->erl
);
2224 case ISCSI_PARAM_IFMARKER_EN
:
2225 sscanf(buf
, "%d", &value
);
2228 case ISCSI_PARAM_OFMARKER_EN
:
2229 sscanf(buf
, "%d", &value
);
2232 case ISCSI_PARAM_EXP_STATSN
:
2233 sscanf(buf
, "%u", &conn
->exp_statsn
);
2235 case ISCSI_PARAM_USERNAME
:
2236 kfree(session
->username
);
2237 session
->username
= kstrdup(buf
, GFP_KERNEL
);
2238 if (!session
->username
)
2241 case ISCSI_PARAM_USERNAME_IN
:
2242 kfree(session
->username_in
);
2243 session
->username_in
= kstrdup(buf
, GFP_KERNEL
);
2244 if (!session
->username_in
)
2247 case ISCSI_PARAM_PASSWORD
:
2248 kfree(session
->password
);
2249 session
->password
= kstrdup(buf
, GFP_KERNEL
);
2250 if (!session
->password
)
2253 case ISCSI_PARAM_PASSWORD_IN
:
2254 kfree(session
->password_in
);
2255 session
->password_in
= kstrdup(buf
, GFP_KERNEL
);
2256 if (!session
->password_in
)
2259 case ISCSI_PARAM_TARGET_NAME
:
2260 /* this should not change between logins */
2261 if (session
->targetname
)
2264 session
->targetname
= kstrdup(buf
, GFP_KERNEL
);
2265 if (!session
->targetname
)
2268 case ISCSI_PARAM_TPGT
:
2269 sscanf(buf
, "%d", &session
->tpgt
);
2271 case ISCSI_PARAM_PERSISTENT_PORT
:
2272 sscanf(buf
, "%d", &conn
->persistent_port
);
2274 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
2276 * this is the address returned in discovery so it should
2277 * not change between logins.
2279 if (conn
->persistent_address
)
2282 conn
->persistent_address
= kstrdup(buf
, GFP_KERNEL
);
2283 if (!conn
->persistent_address
)
2292 EXPORT_SYMBOL_GPL(iscsi_set_param
);
2294 int iscsi_session_get_param(struct iscsi_cls_session
*cls_session
,
2295 enum iscsi_param param
, char *buf
)
2297 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
2298 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
2302 case ISCSI_PARAM_FAST_ABORT
:
2303 len
= sprintf(buf
, "%d\n", session
->fast_abort
);
2305 case ISCSI_PARAM_ABORT_TMO
:
2306 len
= sprintf(buf
, "%d\n", session
->abort_timeout
);
2308 case ISCSI_PARAM_LU_RESET_TMO
:
2309 len
= sprintf(buf
, "%d\n", session
->lu_reset_timeout
);
2311 case ISCSI_PARAM_INITIAL_R2T_EN
:
2312 len
= sprintf(buf
, "%d\n", session
->initial_r2t_en
);
2314 case ISCSI_PARAM_MAX_R2T
:
2315 len
= sprintf(buf
, "%hu\n", session
->max_r2t
);
2317 case ISCSI_PARAM_IMM_DATA_EN
:
2318 len
= sprintf(buf
, "%d\n", session
->imm_data_en
);
2320 case ISCSI_PARAM_FIRST_BURST
:
2321 len
= sprintf(buf
, "%u\n", session
->first_burst
);
2323 case ISCSI_PARAM_MAX_BURST
:
2324 len
= sprintf(buf
, "%u\n", session
->max_burst
);
2326 case ISCSI_PARAM_PDU_INORDER_EN
:
2327 len
= sprintf(buf
, "%d\n", session
->pdu_inorder_en
);
2329 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
2330 len
= sprintf(buf
, "%d\n", session
->dataseq_inorder_en
);
2332 case ISCSI_PARAM_ERL
:
2333 len
= sprintf(buf
, "%d\n", session
->erl
);
2335 case ISCSI_PARAM_TARGET_NAME
:
2336 len
= sprintf(buf
, "%s\n", session
->targetname
);
2338 case ISCSI_PARAM_TPGT
:
2339 len
= sprintf(buf
, "%d\n", session
->tpgt
);
2341 case ISCSI_PARAM_USERNAME
:
2342 len
= sprintf(buf
, "%s\n", session
->username
);
2344 case ISCSI_PARAM_USERNAME_IN
:
2345 len
= sprintf(buf
, "%s\n", session
->username_in
);
2347 case ISCSI_PARAM_PASSWORD
:
2348 len
= sprintf(buf
, "%s\n", session
->password
);
2350 case ISCSI_PARAM_PASSWORD_IN
:
2351 len
= sprintf(buf
, "%s\n", session
->password_in
);
2359 EXPORT_SYMBOL_GPL(iscsi_session_get_param
);
2361 int iscsi_conn_get_param(struct iscsi_cls_conn
*cls_conn
,
2362 enum iscsi_param param
, char *buf
)
2364 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2368 case ISCSI_PARAM_PING_TMO
:
2369 len
= sprintf(buf
, "%u\n", conn
->ping_timeout
);
2371 case ISCSI_PARAM_RECV_TMO
:
2372 len
= sprintf(buf
, "%u\n", conn
->recv_timeout
);
2374 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
2375 len
= sprintf(buf
, "%u\n", conn
->max_recv_dlength
);
2377 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
2378 len
= sprintf(buf
, "%u\n", conn
->max_xmit_dlength
);
2380 case ISCSI_PARAM_HDRDGST_EN
:
2381 len
= sprintf(buf
, "%d\n", conn
->hdrdgst_en
);
2383 case ISCSI_PARAM_DATADGST_EN
:
2384 len
= sprintf(buf
, "%d\n", conn
->datadgst_en
);
2386 case ISCSI_PARAM_IFMARKER_EN
:
2387 len
= sprintf(buf
, "%d\n", conn
->ifmarker_en
);
2389 case ISCSI_PARAM_OFMARKER_EN
:
2390 len
= sprintf(buf
, "%d\n", conn
->ofmarker_en
);
2392 case ISCSI_PARAM_EXP_STATSN
:
2393 len
= sprintf(buf
, "%u\n", conn
->exp_statsn
);
2395 case ISCSI_PARAM_PERSISTENT_PORT
:
2396 len
= sprintf(buf
, "%d\n", conn
->persistent_port
);
2398 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
2399 len
= sprintf(buf
, "%s\n", conn
->persistent_address
);
2407 EXPORT_SYMBOL_GPL(iscsi_conn_get_param
);
2409 int iscsi_host_get_param(struct Scsi_Host
*shost
, enum iscsi_host_param param
,
2412 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
2416 case ISCSI_HOST_PARAM_NETDEV_NAME
:
2417 if (!session
->netdev
)
2418 len
= sprintf(buf
, "%s\n", "default");
2420 len
= sprintf(buf
, "%s\n", session
->netdev
);
2422 case ISCSI_HOST_PARAM_HWADDRESS
:
2423 if (!session
->hwaddress
)
2424 len
= sprintf(buf
, "%s\n", "default");
2426 len
= sprintf(buf
, "%s\n", session
->hwaddress
);
2428 case ISCSI_HOST_PARAM_INITIATOR_NAME
:
2429 if (!session
->initiatorname
)
2430 len
= sprintf(buf
, "%s\n", "unknown");
2432 len
= sprintf(buf
, "%s\n", session
->initiatorname
);
2441 EXPORT_SYMBOL_GPL(iscsi_host_get_param
);
2443 int iscsi_host_set_param(struct Scsi_Host
*shost
, enum iscsi_host_param param
,
2444 char *buf
, int buflen
)
2446 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
2449 case ISCSI_HOST_PARAM_NETDEV_NAME
:
2450 if (!session
->netdev
)
2451 session
->netdev
= kstrdup(buf
, GFP_KERNEL
);
2453 case ISCSI_HOST_PARAM_HWADDRESS
:
2454 if (!session
->hwaddress
)
2455 session
->hwaddress
= kstrdup(buf
, GFP_KERNEL
);
2457 case ISCSI_HOST_PARAM_INITIATOR_NAME
:
2458 if (!session
->initiatorname
)
2459 session
->initiatorname
= kstrdup(buf
, GFP_KERNEL
);
2467 EXPORT_SYMBOL_GPL(iscsi_host_set_param
);
2469 MODULE_AUTHOR("Mike Christie");
2470 MODULE_DESCRIPTION("iSCSI library functions");
2471 MODULE_LICENSE("GPL");