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 session
= iscsi_hostdata(host
->hostdata
);
1015 spin_lock(&session
->lock
);
1018 * ISCSI_STATE_FAILED is a temp. state. The recovery
1019 * code will decide what is best to do with command queued
1022 if (session
->state
!= ISCSI_STATE_LOGGED_IN
&&
1023 session
->state
!= ISCSI_STATE_FAILED
) {
1025 * to handle the race between when we set the recovery state
1026 * and block the session we requeue here (commands could
1027 * be entering our queuecommand while a block is starting
1028 * up because the block code is not locked)
1030 switch (session
->state
) {
1031 case ISCSI_STATE_IN_RECOVERY
:
1032 reason
= FAILURE_SESSION_IN_RECOVERY
;
1034 case ISCSI_STATE_LOGGING_OUT
:
1035 reason
= FAILURE_SESSION_LOGGING_OUT
;
1037 case ISCSI_STATE_RECOVERY_FAILED
:
1038 reason
= FAILURE_SESSION_RECOVERY_TIMEOUT
;
1040 case ISCSI_STATE_TERMINATE
:
1041 reason
= FAILURE_SESSION_TERMINATE
;
1044 reason
= FAILURE_SESSION_FREED
;
1049 conn
= session
->leadconn
;
1051 reason
= FAILURE_SESSION_FREED
;
1055 if (iscsi_check_cmdsn_window_closed(conn
)) {
1056 reason
= FAILURE_WINDOW_CLOSED
;
1060 if (!__kfifo_get(session
->cmdpool
.queue
, (void*)&ctask
,
1062 reason
= FAILURE_OOM
;
1065 session
->queued_cmdsn
++;
1067 sc
->SCp
.phase
= session
->age
;
1068 sc
->SCp
.ptr
= (char *)ctask
;
1070 atomic_set(&ctask
->refcount
, 1);
1071 ctask
->state
= ISCSI_TASK_PENDING
;
1074 INIT_LIST_HEAD(&ctask
->running
);
1076 list_add_tail(&ctask
->running
, &conn
->xmitqueue
);
1077 spin_unlock(&session
->lock
);
1079 scsi_queue_work(host
, &conn
->xmitwork
);
1083 spin_unlock(&session
->lock
);
1084 debug_scsi("cmd 0x%x rejected (%d)\n", sc
->cmnd
[0], reason
);
1085 return SCSI_MLQUEUE_HOST_BUSY
;
1088 spin_unlock(&session
->lock
);
1089 printk(KERN_ERR
"iscsi: cmd 0x%x is not queued (%d)\n",
1090 sc
->cmnd
[0], reason
);
1091 sc
->result
= (DID_NO_CONNECT
<< 16);
1092 scsi_set_resid(sc
, scsi_bufflen(sc
));
1096 EXPORT_SYMBOL_GPL(iscsi_queuecommand
);
1098 int iscsi_change_queue_depth(struct scsi_device
*sdev
, int depth
)
1100 if (depth
> ISCSI_MAX_CMD_PER_LUN
)
1101 depth
= ISCSI_MAX_CMD_PER_LUN
;
1102 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), depth
);
1103 return sdev
->queue_depth
;
1105 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth
);
1107 void iscsi_session_recovery_timedout(struct iscsi_cls_session
*cls_session
)
1109 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1111 spin_lock_bh(&session
->lock
);
1112 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
1113 session
->state
= ISCSI_STATE_RECOVERY_FAILED
;
1114 if (session
->leadconn
)
1115 wake_up(&session
->leadconn
->ehwait
);
1117 spin_unlock_bh(&session
->lock
);
1119 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout
);
1121 int iscsi_eh_host_reset(struct scsi_cmnd
*sc
)
1123 struct Scsi_Host
*host
= sc
->device
->host
;
1124 struct iscsi_session
*session
= iscsi_hostdata(host
->hostdata
);
1125 struct iscsi_conn
*conn
= session
->leadconn
;
1127 mutex_lock(&session
->eh_mutex
);
1128 spin_lock_bh(&session
->lock
);
1129 if (session
->state
== ISCSI_STATE_TERMINATE
) {
1131 debug_scsi("failing host reset: session terminated "
1132 "[CID %d age %d]\n", conn
->id
, session
->age
);
1133 spin_unlock_bh(&session
->lock
);
1134 mutex_unlock(&session
->eh_mutex
);
1138 spin_unlock_bh(&session
->lock
);
1139 mutex_unlock(&session
->eh_mutex
);
1141 * we drop the lock here but the leadconn cannot be destoyed while
1142 * we are in the scsi eh
1144 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1146 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1147 wait_event_interruptible(conn
->ehwait
,
1148 session
->state
== ISCSI_STATE_TERMINATE
||
1149 session
->state
== ISCSI_STATE_LOGGED_IN
||
1150 session
->state
== ISCSI_STATE_RECOVERY_FAILED
);
1151 if (signal_pending(current
))
1152 flush_signals(current
);
1154 mutex_lock(&session
->eh_mutex
);
1155 spin_lock_bh(&session
->lock
);
1156 if (session
->state
== ISCSI_STATE_LOGGED_IN
)
1157 printk(KERN_INFO
"iscsi: host reset succeeded\n");
1160 spin_unlock_bh(&session
->lock
);
1161 mutex_unlock(&session
->eh_mutex
);
1164 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset
);
1166 static void iscsi_tmf_timedout(unsigned long data
)
1168 struct iscsi_conn
*conn
= (struct iscsi_conn
*)data
;
1169 struct iscsi_session
*session
= conn
->session
;
1171 spin_lock(&session
->lock
);
1172 if (conn
->tmf_state
== TMF_QUEUED
) {
1173 conn
->tmf_state
= TMF_TIMEDOUT
;
1174 debug_scsi("tmf timedout\n");
1175 /* unblock eh_abort() */
1176 wake_up(&conn
->ehwait
);
1178 spin_unlock(&session
->lock
);
1181 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn
*conn
,
1182 struct iscsi_tm
*hdr
, int age
,
1185 struct iscsi_session
*session
= conn
->session
;
1186 struct iscsi_mgmt_task
*mtask
;
1188 mtask
= __iscsi_conn_send_pdu(conn
, (struct iscsi_hdr
*)hdr
,
1191 spin_unlock_bh(&session
->lock
);
1192 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1193 spin_lock_bh(&session
->lock
);
1194 debug_scsi("tmf exec failure\n");
1197 conn
->tmfcmd_pdus_cnt
++;
1198 conn
->tmf_timer
.expires
= timeout
* HZ
+ jiffies
;
1199 conn
->tmf_timer
.function
= iscsi_tmf_timedout
;
1200 conn
->tmf_timer
.data
= (unsigned long)conn
;
1201 add_timer(&conn
->tmf_timer
);
1202 debug_scsi("tmf set timeout\n");
1204 spin_unlock_bh(&session
->lock
);
1205 mutex_unlock(&session
->eh_mutex
);
1206 scsi_queue_work(session
->host
, &conn
->xmitwork
);
1209 * block eh thread until:
1213 * 3) session is terminated or restarted or userspace has
1214 * given up on recovery
1216 wait_event_interruptible(conn
->ehwait
, age
!= session
->age
||
1217 session
->state
!= ISCSI_STATE_LOGGED_IN
||
1218 conn
->tmf_state
!= TMF_QUEUED
);
1219 if (signal_pending(current
))
1220 flush_signals(current
);
1221 del_timer_sync(&conn
->tmf_timer
);
1223 mutex_lock(&session
->eh_mutex
);
1224 spin_lock_bh(&session
->lock
);
1225 /* if the session drops it will clean up the mtask */
1226 if (age
!= session
->age
||
1227 session
->state
!= ISCSI_STATE_LOGGED_IN
)
1233 * Fail commands. session lock held and recv side suspended and xmit
1236 static void fail_all_commands(struct iscsi_conn
*conn
, unsigned lun
)
1238 struct iscsi_cmd_task
*ctask
, *tmp
;
1240 if (conn
->ctask
&& (conn
->ctask
->sc
->device
->lun
== lun
|| lun
== -1))
1244 list_for_each_entry_safe(ctask
, tmp
, &conn
->xmitqueue
, running
) {
1245 if (lun
== ctask
->sc
->device
->lun
|| lun
== -1) {
1246 debug_scsi("failing pending sc %p itt 0x%x\n",
1247 ctask
->sc
, ctask
->itt
);
1248 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1252 list_for_each_entry_safe(ctask
, tmp
, &conn
->requeue
, running
) {
1253 if (lun
== ctask
->sc
->device
->lun
|| lun
== -1) {
1254 debug_scsi("failing requeued sc %p itt 0x%x\n",
1255 ctask
->sc
, ctask
->itt
);
1256 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1260 /* fail all other running */
1261 list_for_each_entry_safe(ctask
, tmp
, &conn
->run_list
, running
) {
1262 if (lun
== ctask
->sc
->device
->lun
|| lun
== -1) {
1263 debug_scsi("failing in progress sc %p itt 0x%x\n",
1264 ctask
->sc
, ctask
->itt
);
1265 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1270 static void iscsi_suspend_tx(struct iscsi_conn
*conn
)
1272 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1273 scsi_flush_work(conn
->session
->host
);
1276 static void iscsi_start_tx(struct iscsi_conn
*conn
)
1278 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1279 scsi_queue_work(conn
->session
->host
, &conn
->xmitwork
);
1282 static enum scsi_eh_timer_return
iscsi_eh_cmd_timed_out(struct scsi_cmnd
*scmd
)
1284 struct iscsi_cls_session
*cls_session
;
1285 struct iscsi_session
*session
;
1286 struct iscsi_conn
*conn
;
1287 enum scsi_eh_timer_return rc
= EH_NOT_HANDLED
;
1289 cls_session
= starget_to_session(scsi_target(scmd
->device
));
1290 session
= class_to_transport_session(cls_session
);
1292 debug_scsi("scsi cmd %p timedout\n", scmd
);
1294 spin_lock(&session
->lock
);
1295 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
1297 * We are probably in the middle of iscsi recovery so let
1298 * that complete and handle the error.
1300 rc
= EH_RESET_TIMER
;
1304 conn
= session
->leadconn
;
1306 /* In the middle of shuting down */
1307 rc
= EH_RESET_TIMER
;
1311 if (!conn
->recv_timeout
&& !conn
->ping_timeout
)
1314 * if the ping timedout then we are in the middle of cleaning up
1315 * and can let the iscsi eh handle it
1317 if (time_before_eq(conn
->last_recv
+ (conn
->recv_timeout
* HZ
) +
1318 (conn
->ping_timeout
* HZ
), jiffies
))
1319 rc
= EH_RESET_TIMER
;
1321 * if we are about to check the transport then give the command
1324 if (time_before_eq(conn
->last_recv
+ (conn
->recv_timeout
* HZ
),
1326 rc
= EH_RESET_TIMER
;
1327 /* if in the middle of checking the transport then give us more time */
1328 if (conn
->ping_mtask
)
1329 rc
= EH_RESET_TIMER
;
1331 spin_unlock(&session
->lock
);
1332 debug_scsi("return %s\n", rc
== EH_RESET_TIMER
? "timer reset" : "nh");
1336 static void iscsi_check_transport_timeouts(unsigned long data
)
1338 struct iscsi_conn
*conn
= (struct iscsi_conn
*)data
;
1339 struct iscsi_session
*session
= conn
->session
;
1340 unsigned long timeout
, next_timeout
= 0, last_recv
;
1342 spin_lock(&session
->lock
);
1343 if (session
->state
!= ISCSI_STATE_LOGGED_IN
)
1346 timeout
= conn
->recv_timeout
;
1351 last_recv
= conn
->last_recv
;
1352 if (time_before_eq(last_recv
+ timeout
+ (conn
->ping_timeout
* HZ
),
1354 printk(KERN_ERR
"ping timeout of %d secs expired, "
1355 "last rx %lu, last ping %lu, now %lu\n",
1356 conn
->ping_timeout
, last_recv
,
1357 conn
->last_ping
, jiffies
);
1358 spin_unlock(&session
->lock
);
1359 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1363 if (time_before_eq(last_recv
+ timeout
, jiffies
)) {
1364 if (time_before_eq(conn
->last_ping
, last_recv
)) {
1365 /* send a ping to try to provoke some traffic */
1366 debug_scsi("Sending nopout as ping on conn %p\n", conn
);
1367 iscsi_send_nopout(conn
, NULL
);
1369 next_timeout
= last_recv
+ timeout
+ (conn
->ping_timeout
* HZ
);
1371 next_timeout
= last_recv
+ timeout
;
1375 debug_scsi("Setting next tmo %lu\n", next_timeout
);
1376 mod_timer(&conn
->transport_timer
, next_timeout
);
1379 spin_unlock(&session
->lock
);
1382 static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task
*ctask
,
1383 struct iscsi_tm
*hdr
)
1385 memset(hdr
, 0, sizeof(*hdr
));
1386 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
1387 hdr
->flags
= ISCSI_TM_FUNC_ABORT_TASK
& ISCSI_FLAG_TM_FUNC_MASK
;
1388 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
1389 memcpy(hdr
->lun
, ctask
->hdr
->lun
, sizeof(hdr
->lun
));
1390 hdr
->rtt
= ctask
->hdr
->itt
;
1391 hdr
->refcmdsn
= ctask
->hdr
->cmdsn
;
1394 int iscsi_eh_abort(struct scsi_cmnd
*sc
)
1396 struct Scsi_Host
*host
= sc
->device
->host
;
1397 struct iscsi_session
*session
= iscsi_hostdata(host
->hostdata
);
1398 struct iscsi_conn
*conn
;
1399 struct iscsi_cmd_task
*ctask
;
1400 struct iscsi_tm
*hdr
;
1403 mutex_lock(&session
->eh_mutex
);
1404 spin_lock_bh(&session
->lock
);
1406 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1410 debug_scsi("sc never reached iscsi layer or it completed.\n");
1411 spin_unlock_bh(&session
->lock
);
1412 mutex_unlock(&session
->eh_mutex
);
1417 * If we are not logged in or we have started a new session
1418 * then let the host reset code handle this
1420 if (!session
->leadconn
|| session
->state
!= ISCSI_STATE_LOGGED_IN
||
1421 sc
->SCp
.phase
!= session
->age
) {
1422 spin_unlock_bh(&session
->lock
);
1423 mutex_unlock(&session
->eh_mutex
);
1427 conn
= session
->leadconn
;
1428 conn
->eh_abort_cnt
++;
1431 ctask
= (struct iscsi_cmd_task
*)sc
->SCp
.ptr
;
1432 debug_scsi("aborting [sc %p itt 0x%x]\n", sc
, ctask
->itt
);
1434 /* ctask completed before time out */
1436 debug_scsi("sc completed while abort in progress\n");
1440 if (ctask
->state
== ISCSI_TASK_PENDING
) {
1441 fail_command(conn
, ctask
, DID_ABORT
<< 16);
1445 /* only have one tmf outstanding at a time */
1446 if (conn
->tmf_state
!= TMF_INITIAL
)
1448 conn
->tmf_state
= TMF_QUEUED
;
1451 iscsi_prep_abort_task_pdu(ctask
, hdr
);
1453 if (iscsi_exec_task_mgmt_fn(conn
, hdr
, age
, session
->abort_timeout
)) {
1458 switch (conn
->tmf_state
) {
1460 spin_unlock_bh(&session
->lock
);
1461 iscsi_suspend_tx(conn
);
1463 * clean up task if aborted. grab the recv lock as a writer
1465 write_lock_bh(conn
->recv_lock
);
1466 spin_lock(&session
->lock
);
1467 fail_command(conn
, ctask
, DID_ABORT
<< 16);
1468 conn
->tmf_state
= TMF_INITIAL
;
1469 spin_unlock(&session
->lock
);
1470 write_unlock_bh(conn
->recv_lock
);
1471 iscsi_start_tx(conn
);
1472 goto success_unlocked
;
1474 spin_unlock_bh(&session
->lock
);
1475 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1476 goto failed_unlocked
;
1479 conn
->tmf_state
= TMF_INITIAL
;
1480 /* ctask completed before tmf abort response */
1481 debug_scsi("sc completed while abort in progress\n");
1486 conn
->tmf_state
= TMF_INITIAL
;
1491 spin_unlock_bh(&session
->lock
);
1493 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc
, ctask
->itt
);
1494 mutex_unlock(&session
->eh_mutex
);
1498 spin_unlock_bh(&session
->lock
);
1500 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc
,
1501 ctask
? ctask
->itt
: 0);
1502 mutex_unlock(&session
->eh_mutex
);
1505 EXPORT_SYMBOL_GPL(iscsi_eh_abort
);
1507 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd
*sc
, struct iscsi_tm
*hdr
)
1509 memset(hdr
, 0, sizeof(*hdr
));
1510 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
1511 hdr
->flags
= ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
& ISCSI_FLAG_TM_FUNC_MASK
;
1512 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
1513 int_to_scsilun(sc
->device
->lun
, (struct scsi_lun
*)hdr
->lun
);
1514 hdr
->rtt
= RESERVED_ITT
;
1517 int iscsi_eh_device_reset(struct scsi_cmnd
*sc
)
1519 struct Scsi_Host
*host
= sc
->device
->host
;
1520 struct iscsi_session
*session
= iscsi_hostdata(host
->hostdata
);
1521 struct iscsi_conn
*conn
;
1522 struct iscsi_tm
*hdr
;
1525 debug_scsi("LU Reset [sc %p lun %u]\n", sc
, sc
->device
->lun
);
1527 mutex_lock(&session
->eh_mutex
);
1528 spin_lock_bh(&session
->lock
);
1530 * Just check if we are not logged in. We cannot check for
1531 * the phase because the reset could come from a ioctl.
1533 if (!session
->leadconn
|| session
->state
!= ISCSI_STATE_LOGGED_IN
)
1535 conn
= session
->leadconn
;
1537 /* only have one tmf outstanding at a time */
1538 if (conn
->tmf_state
!= TMF_INITIAL
)
1540 conn
->tmf_state
= TMF_QUEUED
;
1543 iscsi_prep_lun_reset_pdu(sc
, hdr
);
1545 if (iscsi_exec_task_mgmt_fn(conn
, hdr
, session
->age
,
1546 session
->lu_reset_timeout
)) {
1551 switch (conn
->tmf_state
) {
1555 spin_unlock_bh(&session
->lock
);
1556 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1559 conn
->tmf_state
= TMF_INITIAL
;
1564 spin_unlock_bh(&session
->lock
);
1566 iscsi_suspend_tx(conn
);
1567 /* need to grab the recv lock then session lock */
1568 write_lock_bh(conn
->recv_lock
);
1569 spin_lock(&session
->lock
);
1570 fail_all_commands(conn
, sc
->device
->lun
);
1571 conn
->tmf_state
= TMF_INITIAL
;
1572 spin_unlock(&session
->lock
);
1573 write_unlock_bh(conn
->recv_lock
);
1575 iscsi_start_tx(conn
);
1579 spin_unlock_bh(&session
->lock
);
1581 debug_scsi("iscsi_eh_device_reset %s\n",
1582 rc
== SUCCESS
? "SUCCESS" : "FAILED");
1583 mutex_unlock(&session
->eh_mutex
);
1586 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset
);
1589 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1590 * should be accessed via kfifo_{get,put} on q->queue.
1591 * Optionally, the caller can obtain the array of object pointers
1592 * by passing in a non-NULL @items pointer
1595 iscsi_pool_init(struct iscsi_pool
*q
, int max
, void ***items
, int item_size
)
1597 int i
, num_arrays
= 1;
1599 memset(q
, 0, sizeof(*q
));
1603 /* If the user passed an items pointer, he wants a copy of
1607 q
->pool
= kzalloc(num_arrays
* max
* sizeof(void*), GFP_KERNEL
);
1608 if (q
->pool
== NULL
)
1611 q
->queue
= kfifo_init((void*)q
->pool
, max
* sizeof(void*),
1613 if (q
->queue
== ERR_PTR(-ENOMEM
))
1616 for (i
= 0; i
< max
; i
++) {
1617 q
->pool
[i
] = kzalloc(item_size
, GFP_KERNEL
);
1618 if (q
->pool
[i
] == NULL
) {
1622 __kfifo_put(q
->queue
, (void*)&q
->pool
[i
], sizeof(void*));
1626 *items
= q
->pool
+ max
;
1627 memcpy(*items
, q
->pool
, max
* sizeof(void *));
1636 EXPORT_SYMBOL_GPL(iscsi_pool_init
);
1638 void iscsi_pool_free(struct iscsi_pool
*q
)
1642 for (i
= 0; i
< q
->max
; i
++)
1647 EXPORT_SYMBOL_GPL(iscsi_pool_free
);
1650 * iSCSI Session's hostdata organization:
1652 * *------------------* <== hostdata_session(host->hostdata)
1653 * | ptr to class sess|
1654 * |------------------| <== iscsi_hostdata(host->hostdata)
1656 * *------------------*
1659 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1660 _sz % sizeof(unsigned long))
1662 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1665 * iscsi_session_setup - create iscsi cls session and host and session
1666 * @scsit: scsi transport template
1667 * @iscsit: iscsi transport template
1668 * @cmds_max: scsi host can queue
1669 * @qdepth: scsi host cmds per lun
1670 * @cmd_task_size: LLD ctask private data size
1671 * @mgmt_task_size: LLD mtask private data size
1672 * @initial_cmdsn: initial CmdSN
1673 * @hostno: host no allocated
1675 * This can be used by software iscsi_transports that allocate
1676 * a session per scsi host.
1678 struct iscsi_cls_session
*
1679 iscsi_session_setup(struct iscsi_transport
*iscsit
,
1680 struct scsi_transport_template
*scsit
,
1681 uint16_t cmds_max
, uint16_t qdepth
,
1682 int cmd_task_size
, int mgmt_task_size
,
1683 uint32_t initial_cmdsn
, uint32_t *hostno
)
1685 struct Scsi_Host
*shost
;
1686 struct iscsi_session
*session
;
1687 struct iscsi_cls_session
*cls_session
;
1690 if (qdepth
> ISCSI_MAX_CMD_PER_LUN
|| qdepth
< 1) {
1692 printk(KERN_ERR
"iscsi: invalid queue depth of %d. "
1693 "Queue depth must be between 1 and %d.\n",
1694 qdepth
, ISCSI_MAX_CMD_PER_LUN
);
1695 qdepth
= ISCSI_DEF_CMD_PER_LUN
;
1698 if (cmds_max
< 2 || (cmds_max
& (cmds_max
- 1)) ||
1699 cmds_max
>= ISCSI_MGMT_ITT_OFFSET
) {
1701 printk(KERN_ERR
"iscsi: invalid can_queue of %d. "
1702 "can_queue must be a power of 2 and between "
1703 "2 and %d - setting to %d.\n", cmds_max
,
1704 ISCSI_MGMT_ITT_OFFSET
, ISCSI_DEF_XMIT_CMDS_MAX
);
1705 cmds_max
= ISCSI_DEF_XMIT_CMDS_MAX
;
1708 shost
= scsi_host_alloc(iscsit
->host_template
,
1709 hostdata_privsize(sizeof(*session
)));
1713 /* the iscsi layer takes one task for reserve */
1714 shost
->can_queue
= cmds_max
- 1;
1715 shost
->cmd_per_lun
= qdepth
;
1717 shost
->max_channel
= 0;
1718 shost
->max_lun
= iscsit
->max_lun
;
1719 shost
->max_cmd_len
= iscsit
->max_cmd_len
;
1720 shost
->transportt
= scsit
;
1721 shost
->transportt
->create_work_queue
= 1;
1722 shost
->transportt
->eh_timed_out
= iscsi_eh_cmd_timed_out
;
1723 *hostno
= shost
->host_no
;
1725 session
= iscsi_hostdata(shost
->hostdata
);
1726 memset(session
, 0, sizeof(struct iscsi_session
));
1727 session
->host
= shost
;
1728 session
->state
= ISCSI_STATE_FREE
;
1729 session
->fast_abort
= 1;
1730 session
->mgmtpool_max
= ISCSI_MGMT_CMDS_MAX
;
1731 session
->cmds_max
= cmds_max
;
1732 session
->queued_cmdsn
= session
->cmdsn
= initial_cmdsn
;
1733 session
->exp_cmdsn
= initial_cmdsn
+ 1;
1734 session
->max_cmdsn
= initial_cmdsn
+ 1;
1735 session
->max_r2t
= 1;
1736 session
->tt
= iscsit
;
1737 mutex_init(&session
->eh_mutex
);
1739 /* initialize SCSI PDU commands pool */
1740 if (iscsi_pool_init(&session
->cmdpool
, session
->cmds_max
,
1741 (void***)&session
->cmds
,
1742 cmd_task_size
+ sizeof(struct iscsi_cmd_task
)))
1743 goto cmdpool_alloc_fail
;
1745 /* pre-format cmds pool with ITT */
1746 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
1747 struct iscsi_cmd_task
*ctask
= session
->cmds
[cmd_i
];
1750 ctask
->dd_data
= &ctask
[1];
1752 INIT_LIST_HEAD(&ctask
->running
);
1755 spin_lock_init(&session
->lock
);
1757 /* initialize immediate command pool */
1758 if (iscsi_pool_init(&session
->mgmtpool
, session
->mgmtpool_max
,
1759 (void***)&session
->mgmt_cmds
,
1760 mgmt_task_size
+ sizeof(struct iscsi_mgmt_task
)))
1761 goto mgmtpool_alloc_fail
;
1764 /* pre-format immediate cmds pool with ITT */
1765 for (cmd_i
= 0; cmd_i
< session
->mgmtpool_max
; cmd_i
++) {
1766 struct iscsi_mgmt_task
*mtask
= session
->mgmt_cmds
[cmd_i
];
1769 mtask
->dd_data
= &mtask
[1];
1770 mtask
->itt
= ISCSI_MGMT_ITT_OFFSET
+ cmd_i
;
1771 INIT_LIST_HEAD(&mtask
->running
);
1774 if (scsi_add_host(shost
, NULL
))
1777 if (!try_module_get(iscsit
->owner
))
1778 goto cls_session_fail
;
1780 cls_session
= iscsi_create_session(shost
, iscsit
, 0);
1783 *(unsigned long*)shost
->hostdata
= (unsigned long)cls_session
;
1788 module_put(iscsit
->owner
);
1790 scsi_remove_host(shost
);
1792 iscsi_pool_free(&session
->mgmtpool
);
1793 mgmtpool_alloc_fail
:
1794 iscsi_pool_free(&session
->cmdpool
);
1796 scsi_host_put(shost
);
1799 EXPORT_SYMBOL_GPL(iscsi_session_setup
);
1802 * iscsi_session_teardown - destroy session, host, and cls_session
1805 * This can be used by software iscsi_transports that allocate
1806 * a session per scsi host.
1808 void iscsi_session_teardown(struct iscsi_cls_session
*cls_session
)
1810 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
1811 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
1812 struct module
*owner
= cls_session
->transport
->owner
;
1814 iscsi_remove_session(cls_session
);
1815 scsi_remove_host(shost
);
1817 iscsi_pool_free(&session
->mgmtpool
);
1818 iscsi_pool_free(&session
->cmdpool
);
1820 kfree(session
->password
);
1821 kfree(session
->password_in
);
1822 kfree(session
->username
);
1823 kfree(session
->username_in
);
1824 kfree(session
->targetname
);
1825 kfree(session
->netdev
);
1826 kfree(session
->hwaddress
);
1827 kfree(session
->initiatorname
);
1829 iscsi_free_session(cls_session
);
1830 scsi_host_put(shost
);
1833 EXPORT_SYMBOL_GPL(iscsi_session_teardown
);
1836 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1837 * @cls_session: iscsi_cls_session
1840 struct iscsi_cls_conn
*
1841 iscsi_conn_setup(struct iscsi_cls_session
*cls_session
, uint32_t conn_idx
)
1843 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1844 struct iscsi_conn
*conn
;
1845 struct iscsi_cls_conn
*cls_conn
;
1848 cls_conn
= iscsi_create_conn(cls_session
, conn_idx
);
1851 conn
= cls_conn
->dd_data
;
1852 memset(conn
, 0, sizeof(*conn
));
1854 conn
->session
= session
;
1855 conn
->cls_conn
= cls_conn
;
1856 conn
->c_stage
= ISCSI_CONN_INITIAL_STAGE
;
1857 conn
->id
= conn_idx
;
1858 conn
->exp_statsn
= 0;
1859 conn
->tmf_state
= TMF_INITIAL
;
1861 init_timer(&conn
->transport_timer
);
1862 conn
->transport_timer
.data
= (unsigned long)conn
;
1863 conn
->transport_timer
.function
= iscsi_check_transport_timeouts
;
1865 INIT_LIST_HEAD(&conn
->run_list
);
1866 INIT_LIST_HEAD(&conn
->mgmt_run_list
);
1867 INIT_LIST_HEAD(&conn
->mgmtqueue
);
1868 INIT_LIST_HEAD(&conn
->xmitqueue
);
1869 INIT_LIST_HEAD(&conn
->requeue
);
1870 INIT_WORK(&conn
->xmitwork
, iscsi_xmitworker
);
1872 /* allocate login_mtask used for the login/text sequences */
1873 spin_lock_bh(&session
->lock
);
1874 if (!__kfifo_get(session
->mgmtpool
.queue
,
1875 (void*)&conn
->login_mtask
,
1877 spin_unlock_bh(&session
->lock
);
1878 goto login_mtask_alloc_fail
;
1880 spin_unlock_bh(&session
->lock
);
1882 data
= kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN
, GFP_KERNEL
);
1884 goto login_mtask_data_alloc_fail
;
1885 conn
->login_mtask
->data
= conn
->data
= data
;
1887 init_timer(&conn
->tmf_timer
);
1888 init_waitqueue_head(&conn
->ehwait
);
1892 login_mtask_data_alloc_fail
:
1893 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->login_mtask
,
1895 login_mtask_alloc_fail
:
1896 iscsi_destroy_conn(cls_conn
);
1899 EXPORT_SYMBOL_GPL(iscsi_conn_setup
);
1902 * iscsi_conn_teardown - teardown iscsi connection
1903 * cls_conn: iscsi class connection
1905 * TODO: we may need to make this into a two step process
1906 * like scsi-mls remove + put host
1908 void iscsi_conn_teardown(struct iscsi_cls_conn
*cls_conn
)
1910 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1911 struct iscsi_session
*session
= conn
->session
;
1912 unsigned long flags
;
1914 del_timer_sync(&conn
->transport_timer
);
1916 spin_lock_bh(&session
->lock
);
1917 conn
->c_stage
= ISCSI_CONN_CLEANUP_WAIT
;
1918 if (session
->leadconn
== conn
) {
1920 * leading connection? then give up on recovery.
1922 session
->state
= ISCSI_STATE_TERMINATE
;
1923 wake_up(&conn
->ehwait
);
1925 spin_unlock_bh(&session
->lock
);
1928 * Block until all in-progress commands for this connection
1932 spin_lock_irqsave(session
->host
->host_lock
, flags
);
1933 if (!session
->host
->host_busy
) { /* OK for ERL == 0 */
1934 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
1937 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
1938 msleep_interruptible(500);
1939 printk(KERN_INFO
"iscsi: scsi conn_destroy(): host_busy %d "
1940 "host_failed %d\n", session
->host
->host_busy
,
1941 session
->host
->host_failed
);
1943 * force eh_abort() to unblock
1945 wake_up(&conn
->ehwait
);
1948 /* flush queued up work because we free the connection below */
1949 iscsi_suspend_tx(conn
);
1951 spin_lock_bh(&session
->lock
);
1953 kfree(conn
->persistent_address
);
1954 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->login_mtask
,
1956 if (session
->leadconn
== conn
)
1957 session
->leadconn
= NULL
;
1958 spin_unlock_bh(&session
->lock
);
1960 iscsi_destroy_conn(cls_conn
);
1962 EXPORT_SYMBOL_GPL(iscsi_conn_teardown
);
1964 int iscsi_conn_start(struct iscsi_cls_conn
*cls_conn
)
1966 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1967 struct iscsi_session
*session
= conn
->session
;
1970 printk(KERN_ERR
"iscsi: can't start unbound connection\n");
1974 if ((session
->imm_data_en
|| !session
->initial_r2t_en
) &&
1975 session
->first_burst
> session
->max_burst
) {
1976 printk("iscsi: invalid burst lengths: "
1977 "first_burst %d max_burst %d\n",
1978 session
->first_burst
, session
->max_burst
);
1982 if (conn
->ping_timeout
&& !conn
->recv_timeout
) {
1983 printk(KERN_ERR
"iscsi: invalid recv timeout of zero "
1984 "Using 5 seconds\n.");
1985 conn
->recv_timeout
= 5;
1988 if (conn
->recv_timeout
&& !conn
->ping_timeout
) {
1989 printk(KERN_ERR
"iscsi: invalid ping timeout of zero "
1990 "Using 5 seconds.\n");
1991 conn
->ping_timeout
= 5;
1994 spin_lock_bh(&session
->lock
);
1995 conn
->c_stage
= ISCSI_CONN_STARTED
;
1996 session
->state
= ISCSI_STATE_LOGGED_IN
;
1997 session
->queued_cmdsn
= session
->cmdsn
;
1999 conn
->last_recv
= jiffies
;
2000 conn
->last_ping
= jiffies
;
2001 if (conn
->recv_timeout
&& conn
->ping_timeout
)
2002 mod_timer(&conn
->transport_timer
,
2003 jiffies
+ (conn
->recv_timeout
* HZ
));
2005 switch(conn
->stop_stage
) {
2006 case STOP_CONN_RECOVER
:
2008 * unblock eh_abort() if it is blocked. re-try all
2009 * commands after successful recovery
2011 conn
->stop_stage
= 0;
2012 conn
->tmf_state
= TMF_INITIAL
;
2014 spin_unlock_bh(&session
->lock
);
2016 iscsi_unblock_session(session_to_cls(session
));
2017 wake_up(&conn
->ehwait
);
2019 case STOP_CONN_TERM
:
2020 conn
->stop_stage
= 0;
2025 spin_unlock_bh(&session
->lock
);
2029 EXPORT_SYMBOL_GPL(iscsi_conn_start
);
2032 flush_control_queues(struct iscsi_session
*session
, struct iscsi_conn
*conn
)
2034 struct iscsi_mgmt_task
*mtask
, *tmp
;
2036 /* handle pending */
2037 list_for_each_entry_safe(mtask
, tmp
, &conn
->mgmtqueue
, running
) {
2038 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask
->itt
);
2039 iscsi_free_mgmt_task(conn
, mtask
);
2042 /* handle running */
2043 list_for_each_entry_safe(mtask
, tmp
, &conn
->mgmt_run_list
, running
) {
2044 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask
->itt
);
2045 iscsi_free_mgmt_task(conn
, mtask
);
2051 static void iscsi_start_session_recovery(struct iscsi_session
*session
,
2052 struct iscsi_conn
*conn
, int flag
)
2056 del_timer_sync(&conn
->transport_timer
);
2058 mutex_lock(&session
->eh_mutex
);
2059 spin_lock_bh(&session
->lock
);
2060 if (conn
->stop_stage
== STOP_CONN_TERM
) {
2061 spin_unlock_bh(&session
->lock
);
2062 mutex_unlock(&session
->eh_mutex
);
2067 * The LLD either freed/unset the lock on us, or userspace called
2068 * stop but did not create a proper connection (connection was never
2069 * bound or it was unbound then stop was called).
2071 if (!conn
->recv_lock
) {
2072 spin_unlock_bh(&session
->lock
);
2073 mutex_unlock(&session
->eh_mutex
);
2078 * When this is called for the in_login state, we only want to clean
2079 * up the login task and connection. We do not need to block and set
2080 * the recovery state again
2082 if (flag
== STOP_CONN_TERM
)
2083 session
->state
= ISCSI_STATE_TERMINATE
;
2084 else if (conn
->stop_stage
!= STOP_CONN_RECOVER
)
2085 session
->state
= ISCSI_STATE_IN_RECOVERY
;
2087 old_stop_stage
= conn
->stop_stage
;
2088 conn
->stop_stage
= flag
;
2089 conn
->c_stage
= ISCSI_CONN_STOPPED
;
2090 spin_unlock_bh(&session
->lock
);
2092 iscsi_suspend_tx(conn
);
2094 write_lock_bh(conn
->recv_lock
);
2095 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
2096 write_unlock_bh(conn
->recv_lock
);
2099 * for connection level recovery we should not calculate
2100 * header digest. conn->hdr_size used for optimization
2101 * in hdr_extract() and will be re-negotiated at
2104 if (flag
== STOP_CONN_RECOVER
) {
2105 conn
->hdrdgst_en
= 0;
2106 conn
->datadgst_en
= 0;
2107 if (session
->state
== ISCSI_STATE_IN_RECOVERY
&&
2108 old_stop_stage
!= STOP_CONN_RECOVER
) {
2109 debug_scsi("blocking session\n");
2110 iscsi_block_session(session_to_cls(session
));
2117 spin_lock_bh(&session
->lock
);
2118 fail_all_commands(conn
, -1);
2119 flush_control_queues(session
, conn
);
2120 spin_unlock_bh(&session
->lock
);
2121 mutex_unlock(&session
->eh_mutex
);
2124 void iscsi_conn_stop(struct iscsi_cls_conn
*cls_conn
, int flag
)
2126 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2127 struct iscsi_session
*session
= conn
->session
;
2130 case STOP_CONN_RECOVER
:
2131 case STOP_CONN_TERM
:
2132 iscsi_start_session_recovery(session
, conn
, flag
);
2135 printk(KERN_ERR
"iscsi: invalid stop flag %d\n", flag
);
2138 EXPORT_SYMBOL_GPL(iscsi_conn_stop
);
2140 int iscsi_conn_bind(struct iscsi_cls_session
*cls_session
,
2141 struct iscsi_cls_conn
*cls_conn
, int is_leading
)
2143 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
2144 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2146 spin_lock_bh(&session
->lock
);
2148 session
->leadconn
= conn
;
2149 spin_unlock_bh(&session
->lock
);
2152 * Unblock xmitworker(), Login Phase will pass through.
2154 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
2155 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
2158 EXPORT_SYMBOL_GPL(iscsi_conn_bind
);
2161 int iscsi_set_param(struct iscsi_cls_conn
*cls_conn
,
2162 enum iscsi_param param
, char *buf
, int buflen
)
2164 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2165 struct iscsi_session
*session
= conn
->session
;
2169 case ISCSI_PARAM_FAST_ABORT
:
2170 sscanf(buf
, "%d", &session
->fast_abort
);
2172 case ISCSI_PARAM_ABORT_TMO
:
2173 sscanf(buf
, "%d", &session
->abort_timeout
);
2175 case ISCSI_PARAM_LU_RESET_TMO
:
2176 sscanf(buf
, "%d", &session
->lu_reset_timeout
);
2178 case ISCSI_PARAM_PING_TMO
:
2179 sscanf(buf
, "%d", &conn
->ping_timeout
);
2181 case ISCSI_PARAM_RECV_TMO
:
2182 sscanf(buf
, "%d", &conn
->recv_timeout
);
2184 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
2185 sscanf(buf
, "%d", &conn
->max_recv_dlength
);
2187 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
2188 sscanf(buf
, "%d", &conn
->max_xmit_dlength
);
2190 case ISCSI_PARAM_HDRDGST_EN
:
2191 sscanf(buf
, "%d", &conn
->hdrdgst_en
);
2193 case ISCSI_PARAM_DATADGST_EN
:
2194 sscanf(buf
, "%d", &conn
->datadgst_en
);
2196 case ISCSI_PARAM_INITIAL_R2T_EN
:
2197 sscanf(buf
, "%d", &session
->initial_r2t_en
);
2199 case ISCSI_PARAM_MAX_R2T
:
2200 sscanf(buf
, "%d", &session
->max_r2t
);
2202 case ISCSI_PARAM_IMM_DATA_EN
:
2203 sscanf(buf
, "%d", &session
->imm_data_en
);
2205 case ISCSI_PARAM_FIRST_BURST
:
2206 sscanf(buf
, "%d", &session
->first_burst
);
2208 case ISCSI_PARAM_MAX_BURST
:
2209 sscanf(buf
, "%d", &session
->max_burst
);
2211 case ISCSI_PARAM_PDU_INORDER_EN
:
2212 sscanf(buf
, "%d", &session
->pdu_inorder_en
);
2214 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
2215 sscanf(buf
, "%d", &session
->dataseq_inorder_en
);
2217 case ISCSI_PARAM_ERL
:
2218 sscanf(buf
, "%d", &session
->erl
);
2220 case ISCSI_PARAM_IFMARKER_EN
:
2221 sscanf(buf
, "%d", &value
);
2224 case ISCSI_PARAM_OFMARKER_EN
:
2225 sscanf(buf
, "%d", &value
);
2228 case ISCSI_PARAM_EXP_STATSN
:
2229 sscanf(buf
, "%u", &conn
->exp_statsn
);
2231 case ISCSI_PARAM_USERNAME
:
2232 kfree(session
->username
);
2233 session
->username
= kstrdup(buf
, GFP_KERNEL
);
2234 if (!session
->username
)
2237 case ISCSI_PARAM_USERNAME_IN
:
2238 kfree(session
->username_in
);
2239 session
->username_in
= kstrdup(buf
, GFP_KERNEL
);
2240 if (!session
->username_in
)
2243 case ISCSI_PARAM_PASSWORD
:
2244 kfree(session
->password
);
2245 session
->password
= kstrdup(buf
, GFP_KERNEL
);
2246 if (!session
->password
)
2249 case ISCSI_PARAM_PASSWORD_IN
:
2250 kfree(session
->password_in
);
2251 session
->password_in
= kstrdup(buf
, GFP_KERNEL
);
2252 if (!session
->password_in
)
2255 case ISCSI_PARAM_TARGET_NAME
:
2256 /* this should not change between logins */
2257 if (session
->targetname
)
2260 session
->targetname
= kstrdup(buf
, GFP_KERNEL
);
2261 if (!session
->targetname
)
2264 case ISCSI_PARAM_TPGT
:
2265 sscanf(buf
, "%d", &session
->tpgt
);
2267 case ISCSI_PARAM_PERSISTENT_PORT
:
2268 sscanf(buf
, "%d", &conn
->persistent_port
);
2270 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
2272 * this is the address returned in discovery so it should
2273 * not change between logins.
2275 if (conn
->persistent_address
)
2278 conn
->persistent_address
= kstrdup(buf
, GFP_KERNEL
);
2279 if (!conn
->persistent_address
)
2288 EXPORT_SYMBOL_GPL(iscsi_set_param
);
2290 int iscsi_session_get_param(struct iscsi_cls_session
*cls_session
,
2291 enum iscsi_param param
, char *buf
)
2293 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
2294 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
2298 case ISCSI_PARAM_FAST_ABORT
:
2299 len
= sprintf(buf
, "%d\n", session
->fast_abort
);
2301 case ISCSI_PARAM_ABORT_TMO
:
2302 len
= sprintf(buf
, "%d\n", session
->abort_timeout
);
2304 case ISCSI_PARAM_LU_RESET_TMO
:
2305 len
= sprintf(buf
, "%d\n", session
->lu_reset_timeout
);
2307 case ISCSI_PARAM_INITIAL_R2T_EN
:
2308 len
= sprintf(buf
, "%d\n", session
->initial_r2t_en
);
2310 case ISCSI_PARAM_MAX_R2T
:
2311 len
= sprintf(buf
, "%hu\n", session
->max_r2t
);
2313 case ISCSI_PARAM_IMM_DATA_EN
:
2314 len
= sprintf(buf
, "%d\n", session
->imm_data_en
);
2316 case ISCSI_PARAM_FIRST_BURST
:
2317 len
= sprintf(buf
, "%u\n", session
->first_burst
);
2319 case ISCSI_PARAM_MAX_BURST
:
2320 len
= sprintf(buf
, "%u\n", session
->max_burst
);
2322 case ISCSI_PARAM_PDU_INORDER_EN
:
2323 len
= sprintf(buf
, "%d\n", session
->pdu_inorder_en
);
2325 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
2326 len
= sprintf(buf
, "%d\n", session
->dataseq_inorder_en
);
2328 case ISCSI_PARAM_ERL
:
2329 len
= sprintf(buf
, "%d\n", session
->erl
);
2331 case ISCSI_PARAM_TARGET_NAME
:
2332 len
= sprintf(buf
, "%s\n", session
->targetname
);
2334 case ISCSI_PARAM_TPGT
:
2335 len
= sprintf(buf
, "%d\n", session
->tpgt
);
2337 case ISCSI_PARAM_USERNAME
:
2338 len
= sprintf(buf
, "%s\n", session
->username
);
2340 case ISCSI_PARAM_USERNAME_IN
:
2341 len
= sprintf(buf
, "%s\n", session
->username_in
);
2343 case ISCSI_PARAM_PASSWORD
:
2344 len
= sprintf(buf
, "%s\n", session
->password
);
2346 case ISCSI_PARAM_PASSWORD_IN
:
2347 len
= sprintf(buf
, "%s\n", session
->password_in
);
2355 EXPORT_SYMBOL_GPL(iscsi_session_get_param
);
2357 int iscsi_conn_get_param(struct iscsi_cls_conn
*cls_conn
,
2358 enum iscsi_param param
, char *buf
)
2360 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2364 case ISCSI_PARAM_PING_TMO
:
2365 len
= sprintf(buf
, "%u\n", conn
->ping_timeout
);
2367 case ISCSI_PARAM_RECV_TMO
:
2368 len
= sprintf(buf
, "%u\n", conn
->recv_timeout
);
2370 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
2371 len
= sprintf(buf
, "%u\n", conn
->max_recv_dlength
);
2373 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
2374 len
= sprintf(buf
, "%u\n", conn
->max_xmit_dlength
);
2376 case ISCSI_PARAM_HDRDGST_EN
:
2377 len
= sprintf(buf
, "%d\n", conn
->hdrdgst_en
);
2379 case ISCSI_PARAM_DATADGST_EN
:
2380 len
= sprintf(buf
, "%d\n", conn
->datadgst_en
);
2382 case ISCSI_PARAM_IFMARKER_EN
:
2383 len
= sprintf(buf
, "%d\n", conn
->ifmarker_en
);
2385 case ISCSI_PARAM_OFMARKER_EN
:
2386 len
= sprintf(buf
, "%d\n", conn
->ofmarker_en
);
2388 case ISCSI_PARAM_EXP_STATSN
:
2389 len
= sprintf(buf
, "%u\n", conn
->exp_statsn
);
2391 case ISCSI_PARAM_PERSISTENT_PORT
:
2392 len
= sprintf(buf
, "%d\n", conn
->persistent_port
);
2394 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
2395 len
= sprintf(buf
, "%s\n", conn
->persistent_address
);
2403 EXPORT_SYMBOL_GPL(iscsi_conn_get_param
);
2405 int iscsi_host_get_param(struct Scsi_Host
*shost
, enum iscsi_host_param param
,
2408 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
2412 case ISCSI_HOST_PARAM_NETDEV_NAME
:
2413 if (!session
->netdev
)
2414 len
= sprintf(buf
, "%s\n", "default");
2416 len
= sprintf(buf
, "%s\n", session
->netdev
);
2418 case ISCSI_HOST_PARAM_HWADDRESS
:
2419 if (!session
->hwaddress
)
2420 len
= sprintf(buf
, "%s\n", "default");
2422 len
= sprintf(buf
, "%s\n", session
->hwaddress
);
2424 case ISCSI_HOST_PARAM_INITIATOR_NAME
:
2425 if (!session
->initiatorname
)
2426 len
= sprintf(buf
, "%s\n", "unknown");
2428 len
= sprintf(buf
, "%s\n", session
->initiatorname
);
2437 EXPORT_SYMBOL_GPL(iscsi_host_get_param
);
2439 int iscsi_host_set_param(struct Scsi_Host
*shost
, enum iscsi_host_param param
,
2440 char *buf
, int buflen
)
2442 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
2445 case ISCSI_HOST_PARAM_NETDEV_NAME
:
2446 if (!session
->netdev
)
2447 session
->netdev
= kstrdup(buf
, GFP_KERNEL
);
2449 case ISCSI_HOST_PARAM_HWADDRESS
:
2450 if (!session
->hwaddress
)
2451 session
->hwaddress
= kstrdup(buf
, GFP_KERNEL
);
2453 case ISCSI_HOST_PARAM_INITIATOR_NAME
:
2454 if (!session
->initiatorname
)
2455 session
->initiatorname
= kstrdup(buf
, GFP_KERNEL
);
2463 EXPORT_SYMBOL_GPL(iscsi_host_set_param
);
2465 MODULE_AUTHOR("Mike Christie");
2466 MODULE_DESCRIPTION("iSCSI library functions");
2467 MODULE_LICENSE("GPL");