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 <linux/log2.h>
28 #include <asm/unaligned.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi.h>
36 #include <scsi/iscsi_proto.h>
37 #include <scsi/scsi_transport.h>
38 #include <scsi/scsi_transport_iscsi.h>
39 #include <scsi/libiscsi.h>
41 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
42 #define SNA32_CHECK 2147483648UL
44 static int iscsi_sna_lt(u32 n1
, u32 n2
)
46 return n1
!= n2
&& ((n1
< n2
&& (n2
- n1
< SNA32_CHECK
)) ||
47 (n1
> n2
&& (n2
- n1
< SNA32_CHECK
)));
50 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
51 static int iscsi_sna_lte(u32 n1
, u32 n2
)
53 return n1
== n2
|| ((n1
< n2
&& (n2
- n1
< SNA32_CHECK
)) ||
54 (n1
> n2
&& (n2
- n1
< SNA32_CHECK
)));
58 iscsi_update_cmdsn(struct iscsi_session
*session
, struct iscsi_nopin
*hdr
)
60 uint32_t max_cmdsn
= be32_to_cpu(hdr
->max_cmdsn
);
61 uint32_t exp_cmdsn
= be32_to_cpu(hdr
->exp_cmdsn
);
64 * standard specifies this check for when to update expected and
65 * max sequence numbers
67 if (iscsi_sna_lt(max_cmdsn
, exp_cmdsn
- 1))
70 if (exp_cmdsn
!= session
->exp_cmdsn
&&
71 !iscsi_sna_lt(exp_cmdsn
, session
->exp_cmdsn
))
72 session
->exp_cmdsn
= exp_cmdsn
;
74 if (max_cmdsn
!= session
->max_cmdsn
&&
75 !iscsi_sna_lt(max_cmdsn
, session
->max_cmdsn
)) {
76 session
->max_cmdsn
= max_cmdsn
;
78 * if the window closed with IO queued, then kick the
81 if (!list_empty(&session
->leadconn
->xmitqueue
) ||
82 !list_empty(&session
->leadconn
->mgmtqueue
)) {
83 if (!(session
->tt
->caps
& CAP_DATA_PATH_OFFLOAD
))
84 scsi_queue_work(session
->host
,
85 &session
->leadconn
->xmitwork
);
89 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn
);
91 void iscsi_prep_unsolicit_data_pdu(struct iscsi_task
*task
,
92 struct iscsi_data
*hdr
)
94 struct iscsi_conn
*conn
= task
->conn
;
96 memset(hdr
, 0, sizeof(struct iscsi_data
));
97 hdr
->ttt
= cpu_to_be32(ISCSI_RESERVED_TAG
);
98 hdr
->datasn
= cpu_to_be32(task
->unsol_datasn
);
100 hdr
->opcode
= ISCSI_OP_SCSI_DATA_OUT
;
101 memcpy(hdr
->lun
, task
->hdr
->lun
, sizeof(hdr
->lun
));
103 hdr
->itt
= task
->hdr
->itt
;
104 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
105 hdr
->offset
= cpu_to_be32(task
->unsol_offset
);
107 if (task
->unsol_count
> conn
->max_xmit_dlength
) {
108 hton24(hdr
->dlength
, conn
->max_xmit_dlength
);
109 task
->data_count
= conn
->max_xmit_dlength
;
110 task
->unsol_offset
+= task
->data_count
;
113 hton24(hdr
->dlength
, task
->unsol_count
);
114 task
->data_count
= task
->unsol_count
;
115 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
118 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu
);
120 static int iscsi_add_hdr(struct iscsi_task
*task
, unsigned len
)
122 unsigned exp_len
= task
->hdr_len
+ len
;
124 if (exp_len
> task
->hdr_max
) {
129 WARN_ON(len
& (ISCSI_PAD_LEN
- 1)); /* caller must pad the AHS */
130 task
->hdr_len
= exp_len
;
135 * make an extended cdb AHS
137 static int iscsi_prep_ecdb_ahs(struct iscsi_task
*task
)
139 struct scsi_cmnd
*cmd
= task
->sc
;
140 unsigned rlen
, pad_len
;
141 unsigned short ahslength
;
142 struct iscsi_ecdb_ahdr
*ecdb_ahdr
;
145 ecdb_ahdr
= iscsi_next_hdr(task
);
146 rlen
= cmd
->cmd_len
- ISCSI_CDB_SIZE
;
148 BUG_ON(rlen
> sizeof(ecdb_ahdr
->ecdb
));
149 ahslength
= rlen
+ sizeof(ecdb_ahdr
->reserved
);
151 pad_len
= iscsi_padding(rlen
);
153 rc
= iscsi_add_hdr(task
, sizeof(ecdb_ahdr
->ahslength
) +
154 sizeof(ecdb_ahdr
->ahstype
) + ahslength
+ pad_len
);
159 memset(&ecdb_ahdr
->ecdb
[rlen
], 0, pad_len
);
161 ecdb_ahdr
->ahslength
= cpu_to_be16(ahslength
);
162 ecdb_ahdr
->ahstype
= ISCSI_AHSTYPE_CDB
;
163 ecdb_ahdr
->reserved
= 0;
164 memcpy(ecdb_ahdr
->ecdb
, cmd
->cmnd
+ ISCSI_CDB_SIZE
, rlen
);
166 debug_scsi("iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
167 "rlen %d pad_len %d ahs_length %d iscsi_headers_size %u\n",
168 cmd
->cmd_len
, rlen
, pad_len
, ahslength
, task
->hdr_len
);
173 static int iscsi_prep_bidi_ahs(struct iscsi_task
*task
)
175 struct scsi_cmnd
*sc
= task
->sc
;
176 struct iscsi_rlength_ahdr
*rlen_ahdr
;
179 rlen_ahdr
= iscsi_next_hdr(task
);
180 rc
= iscsi_add_hdr(task
, sizeof(*rlen_ahdr
));
184 rlen_ahdr
->ahslength
=
185 cpu_to_be16(sizeof(rlen_ahdr
->read_length
) +
186 sizeof(rlen_ahdr
->reserved
));
187 rlen_ahdr
->ahstype
= ISCSI_AHSTYPE_RLENGTH
;
188 rlen_ahdr
->reserved
= 0;
189 rlen_ahdr
->read_length
= cpu_to_be32(scsi_in(sc
)->length
);
191 debug_scsi("bidi-in rlen_ahdr->read_length(%d) "
192 "rlen_ahdr->ahslength(%d)\n",
193 be32_to_cpu(rlen_ahdr
->read_length
),
194 be16_to_cpu(rlen_ahdr
->ahslength
));
199 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
202 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
203 * fields like dlength or final based on how much data it sends
205 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task
*task
)
207 struct iscsi_conn
*conn
= task
->conn
;
208 struct iscsi_session
*session
= conn
->session
;
209 struct iscsi_cmd
*hdr
= task
->hdr
;
210 struct scsi_cmnd
*sc
= task
->sc
;
211 unsigned hdrlength
, cmd_len
;
215 rc
= iscsi_add_hdr(task
, sizeof(*hdr
));
218 hdr
->opcode
= ISCSI_OP_SCSI_CMD
;
219 hdr
->flags
= ISCSI_ATTR_SIMPLE
;
220 int_to_scsilun(sc
->device
->lun
, (struct scsi_lun
*)hdr
->lun
);
221 hdr
->itt
= build_itt(task
->itt
, session
->age
);
222 hdr
->cmdsn
= cpu_to_be32(session
->cmdsn
);
224 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
225 cmd_len
= sc
->cmd_len
;
226 if (cmd_len
< ISCSI_CDB_SIZE
)
227 memset(&hdr
->cdb
[cmd_len
], 0, ISCSI_CDB_SIZE
- cmd_len
);
228 else if (cmd_len
> ISCSI_CDB_SIZE
) {
229 rc
= iscsi_prep_ecdb_ahs(task
);
232 cmd_len
= ISCSI_CDB_SIZE
;
234 memcpy(hdr
->cdb
, sc
->cmnd
, cmd_len
);
237 if (scsi_bidi_cmnd(sc
)) {
238 hdr
->flags
|= ISCSI_FLAG_CMD_READ
;
239 rc
= iscsi_prep_bidi_ahs(task
);
243 if (sc
->sc_data_direction
== DMA_TO_DEVICE
) {
244 unsigned out_len
= scsi_out(sc
)->length
;
245 hdr
->data_length
= cpu_to_be32(out_len
);
246 hdr
->flags
|= ISCSI_FLAG_CMD_WRITE
;
250 * imm_count bytes to be sent right after
253 * unsol_count bytes(as Data-Out) to be sent
254 * without R2T ack right after
257 * r2t_data_count bytes to be sent via R2T ack's
259 * pad_count bytes to be sent as zero-padding
261 task
->unsol_count
= 0;
262 task
->unsol_offset
= 0;
263 task
->unsol_datasn
= 0;
265 if (session
->imm_data_en
) {
266 if (out_len
>= session
->first_burst
)
267 task
->imm_count
= min(session
->first_burst
,
268 conn
->max_xmit_dlength
);
270 task
->imm_count
= min(out_len
,
271 conn
->max_xmit_dlength
);
272 hton24(hdr
->dlength
, task
->imm_count
);
274 zero_data(hdr
->dlength
);
276 if (!session
->initial_r2t_en
) {
277 task
->unsol_count
= min(session
->first_burst
, out_len
)
279 task
->unsol_offset
= task
->imm_count
;
282 if (!task
->unsol_count
)
283 /* No unsolicit Data-Out's */
284 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
286 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
287 zero_data(hdr
->dlength
);
288 hdr
->data_length
= cpu_to_be32(scsi_in(sc
)->length
);
290 if (sc
->sc_data_direction
== DMA_FROM_DEVICE
)
291 hdr
->flags
|= ISCSI_FLAG_CMD_READ
;
294 /* calculate size of additional header segments (AHSs) */
295 hdrlength
= task
->hdr_len
- sizeof(*hdr
);
297 WARN_ON(hdrlength
& (ISCSI_PAD_LEN
-1));
298 hdrlength
/= ISCSI_PAD_LEN
;
300 WARN_ON(hdrlength
>= 256);
301 hdr
->hlength
= hdrlength
& 0xFF;
303 if (conn
->session
->tt
->init_task
&&
304 conn
->session
->tt
->init_task(task
))
307 task
->state
= ISCSI_TASK_RUNNING
;
308 list_move_tail(&task
->running
, &conn
->run_list
);
310 conn
->scsicmd_pdus_cnt
++;
311 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
312 "bidi_len %d cmdsn %d win %d]\n", scsi_bidi_cmnd(sc
) ?
313 "bidirectional" : sc
->sc_data_direction
== DMA_TO_DEVICE
?
314 "write" : "read", conn
->id
, sc
, sc
->cmnd
[0], task
->itt
,
316 scsi_bidi_cmnd(sc
) ? scsi_in(sc
)->length
: 0,
317 session
->cmdsn
, session
->max_cmdsn
- session
->exp_cmdsn
+ 1);
322 * iscsi_complete_command - finish a task
323 * @task: iscsi cmd task
325 * Must be called with session lock.
326 * This function returns the scsi command to scsi-ml or cleans
327 * up mgmt tasks then returns the task to the pool.
329 static void iscsi_complete_command(struct iscsi_task
*task
)
331 struct iscsi_conn
*conn
= task
->conn
;
332 struct iscsi_session
*session
= conn
->session
;
333 struct scsi_cmnd
*sc
= task
->sc
;
335 list_del_init(&task
->running
);
336 task
->state
= ISCSI_TASK_COMPLETED
;
339 if (conn
->task
== task
)
342 * login task is preallocated so do not free
344 if (conn
->login_task
== task
)
347 __kfifo_put(session
->cmdpool
.queue
, (void*)&task
, sizeof(void*));
349 if (conn
->ping_task
== task
)
350 conn
->ping_task
= NULL
;
354 /* SCSI eh reuses commands to verify us */
357 * queue command may call this to free the task, but
358 * not have setup the sc callback
365 void __iscsi_get_task(struct iscsi_task
*task
)
367 atomic_inc(&task
->refcount
);
369 EXPORT_SYMBOL_GPL(__iscsi_get_task
);
371 static void __iscsi_put_task(struct iscsi_task
*task
)
373 if (atomic_dec_and_test(&task
->refcount
))
374 iscsi_complete_command(task
);
377 void iscsi_put_task(struct iscsi_task
*task
)
379 struct iscsi_session
*session
= task
->conn
->session
;
381 spin_lock_bh(&session
->lock
);
382 __iscsi_put_task(task
);
383 spin_unlock_bh(&session
->lock
);
385 EXPORT_SYMBOL_GPL(iscsi_put_task
);
388 * session lock must be held
390 static void fail_command(struct iscsi_conn
*conn
, struct iscsi_task
*task
,
393 struct scsi_cmnd
*sc
;
399 if (task
->state
== ISCSI_TASK_PENDING
)
401 * cmd never made it to the xmit thread, so we should not count
402 * the cmd in the sequencing
404 conn
->session
->queued_cmdsn
--;
406 conn
->session
->tt
->cleanup_task(conn
, task
);
408 * Check if cleanup_task dropped the lock and the command completed,
414 if (!scsi_bidi_cmnd(sc
))
415 scsi_set_resid(sc
, scsi_bufflen(sc
));
417 scsi_out(sc
)->resid
= scsi_out(sc
)->length
;
418 scsi_in(sc
)->resid
= scsi_in(sc
)->length
;
421 if (conn
->task
== task
)
423 /* release ref from queuecommand */
424 __iscsi_put_task(task
);
427 static int iscsi_prep_mgmt_task(struct iscsi_conn
*conn
,
428 struct iscsi_task
*task
)
430 struct iscsi_session
*session
= conn
->session
;
431 struct iscsi_hdr
*hdr
= (struct iscsi_hdr
*)task
->hdr
;
432 struct iscsi_nopout
*nop
= (struct iscsi_nopout
*)hdr
;
434 if (conn
->session
->state
== ISCSI_STATE_LOGGING_OUT
)
437 if (hdr
->opcode
!= (ISCSI_OP_LOGIN
| ISCSI_OP_IMMEDIATE
) &&
438 hdr
->opcode
!= (ISCSI_OP_TEXT
| ISCSI_OP_IMMEDIATE
))
439 nop
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
441 * pre-format CmdSN for outgoing PDU.
443 nop
->cmdsn
= cpu_to_be32(session
->cmdsn
);
444 if (hdr
->itt
!= RESERVED_ITT
) {
445 hdr
->itt
= build_itt(task
->itt
, session
->age
);
447 * TODO: We always use immediate, so we never hit this.
448 * If we start to send tmfs or nops as non-immediate then
449 * we should start checking the cmdsn numbers for mgmt tasks.
451 if (conn
->c_stage
== ISCSI_CONN_STARTED
&&
452 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
453 session
->queued_cmdsn
++;
458 if (session
->tt
->init_task
)
459 session
->tt
->init_task(task
);
461 if ((hdr
->opcode
& ISCSI_OPCODE_MASK
) == ISCSI_OP_LOGOUT
)
462 session
->state
= ISCSI_STATE_LOGGING_OUT
;
464 list_move_tail(&task
->running
, &conn
->mgmt_run_list
);
465 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
466 hdr
->opcode
& ISCSI_OPCODE_MASK
, hdr
->itt
,
471 static struct iscsi_task
*
472 __iscsi_conn_send_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
473 char *data
, uint32_t data_size
)
475 struct iscsi_session
*session
= conn
->session
;
476 struct iscsi_task
*task
;
478 if (session
->state
== ISCSI_STATE_TERMINATE
)
481 if (hdr
->opcode
== (ISCSI_OP_LOGIN
| ISCSI_OP_IMMEDIATE
) ||
482 hdr
->opcode
== (ISCSI_OP_TEXT
| ISCSI_OP_IMMEDIATE
))
484 * Login and Text are sent serially, in
485 * request-followed-by-response sequence.
486 * Same task can be used. Same ITT must be used.
487 * Note that login_task is preallocated at conn_create().
489 task
= conn
->login_task
;
491 BUG_ON(conn
->c_stage
== ISCSI_CONN_INITIAL_STAGE
);
492 BUG_ON(conn
->c_stage
== ISCSI_CONN_STOPPED
);
494 if (!__kfifo_get(session
->cmdpool
.queue
,
495 (void*)&task
, sizeof(void*)))
498 if ((hdr
->opcode
== (ISCSI_OP_NOOP_OUT
| ISCSI_OP_IMMEDIATE
)) &&
499 hdr
->ttt
== RESERVED_ITT
) {
500 conn
->ping_task
= task
;
501 conn
->last_ping
= jiffies
;
505 * released in complete pdu for task we expect a response for, and
506 * released by the lld when it has transmitted the task for
507 * pdus we do not expect a response for.
509 atomic_set(&task
->refcount
, 1);
514 memcpy(task
->data
, data
, data_size
);
515 task
->data_count
= data_size
;
517 task
->data_count
= 0;
519 memcpy(task
->hdr
, hdr
, sizeof(struct iscsi_hdr
));
520 INIT_LIST_HEAD(&task
->running
);
521 list_add_tail(&task
->running
, &conn
->mgmtqueue
);
523 if (session
->tt
->caps
& CAP_DATA_PATH_OFFLOAD
) {
524 if (iscsi_prep_mgmt_task(conn
, task
)) {
525 __iscsi_put_task(task
);
529 if (session
->tt
->xmit_task(task
))
533 scsi_queue_work(conn
->session
->host
, &conn
->xmitwork
);
538 int iscsi_conn_send_pdu(struct iscsi_cls_conn
*cls_conn
, struct iscsi_hdr
*hdr
,
539 char *data
, uint32_t data_size
)
541 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
542 struct iscsi_session
*session
= conn
->session
;
545 spin_lock_bh(&session
->lock
);
546 if (!__iscsi_conn_send_pdu(conn
, hdr
, data
, data_size
))
548 spin_unlock_bh(&session
->lock
);
551 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu
);
554 * iscsi_cmd_rsp - SCSI Command Response processing
555 * @conn: iscsi connection
557 * @task: scsi command task
558 * @data: cmd data buffer
559 * @datalen: len of buffer
561 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
562 * then completes the command and task.
564 static void iscsi_scsi_cmd_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
565 struct iscsi_task
*task
, char *data
,
568 struct iscsi_cmd_rsp
*rhdr
= (struct iscsi_cmd_rsp
*)hdr
;
569 struct iscsi_session
*session
= conn
->session
;
570 struct scsi_cmnd
*sc
= task
->sc
;
572 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)rhdr
);
573 conn
->exp_statsn
= be32_to_cpu(rhdr
->statsn
) + 1;
575 sc
->result
= (DID_OK
<< 16) | rhdr
->cmd_status
;
577 if (rhdr
->response
!= ISCSI_STATUS_CMD_COMPLETED
) {
578 sc
->result
= DID_ERROR
<< 16;
582 if (rhdr
->cmd_status
== SAM_STAT_CHECK_CONDITION
) {
587 iscsi_conn_printk(KERN_ERR
, conn
,
588 "Got CHECK_CONDITION but invalid data "
589 "buffer size of %d\n", datalen
);
590 sc
->result
= DID_BAD_TARGET
<< 16;
594 senselen
= get_unaligned_be16(data
);
595 if (datalen
< senselen
)
596 goto invalid_datalen
;
598 memcpy(sc
->sense_buffer
, data
+ 2,
599 min_t(uint16_t, senselen
, SCSI_SENSE_BUFFERSIZE
));
600 debug_scsi("copied %d bytes of sense\n",
601 min_t(uint16_t, senselen
, SCSI_SENSE_BUFFERSIZE
));
604 if (rhdr
->flags
& (ISCSI_FLAG_CMD_BIDI_UNDERFLOW
|
605 ISCSI_FLAG_CMD_BIDI_OVERFLOW
)) {
606 int res_count
= be32_to_cpu(rhdr
->bi_residual_count
);
608 if (scsi_bidi_cmnd(sc
) && res_count
> 0 &&
609 (rhdr
->flags
& ISCSI_FLAG_CMD_BIDI_OVERFLOW
||
610 res_count
<= scsi_in(sc
)->length
))
611 scsi_in(sc
)->resid
= res_count
;
613 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
616 if (rhdr
->flags
& (ISCSI_FLAG_CMD_UNDERFLOW
|
617 ISCSI_FLAG_CMD_OVERFLOW
)) {
618 int res_count
= be32_to_cpu(rhdr
->residual_count
);
621 (rhdr
->flags
& ISCSI_FLAG_CMD_OVERFLOW
||
622 res_count
<= scsi_bufflen(sc
)))
623 /* write side for bidi or uni-io set_resid */
624 scsi_set_resid(sc
, res_count
);
626 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
629 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
630 (long)sc
, sc
->result
, task
->itt
);
631 conn
->scsirsp_pdus_cnt
++;
633 __iscsi_put_task(task
);
636 static void iscsi_tmf_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
)
638 struct iscsi_tm_rsp
*tmf
= (struct iscsi_tm_rsp
*)hdr
;
640 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
641 conn
->tmfrsp_pdus_cnt
++;
643 if (conn
->tmf_state
!= TMF_QUEUED
)
646 if (tmf
->response
== ISCSI_TMF_RSP_COMPLETE
)
647 conn
->tmf_state
= TMF_SUCCESS
;
648 else if (tmf
->response
== ISCSI_TMF_RSP_NO_TASK
)
649 conn
->tmf_state
= TMF_NOT_FOUND
;
651 conn
->tmf_state
= TMF_FAILED
;
652 wake_up(&conn
->ehwait
);
655 static void iscsi_send_nopout(struct iscsi_conn
*conn
, struct iscsi_nopin
*rhdr
)
657 struct iscsi_nopout hdr
;
658 struct iscsi_task
*task
;
660 if (!rhdr
&& conn
->ping_task
)
663 memset(&hdr
, 0, sizeof(struct iscsi_nopout
));
664 hdr
.opcode
= ISCSI_OP_NOOP_OUT
| ISCSI_OP_IMMEDIATE
;
665 hdr
.flags
= ISCSI_FLAG_CMD_FINAL
;
668 memcpy(hdr
.lun
, rhdr
->lun
, 8);
670 hdr
.itt
= RESERVED_ITT
;
672 hdr
.ttt
= RESERVED_ITT
;
674 task
= __iscsi_conn_send_pdu(conn
, (struct iscsi_hdr
*)&hdr
, NULL
, 0);
676 iscsi_conn_printk(KERN_ERR
, conn
, "Could not send nopout\n");
679 static int iscsi_handle_reject(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
680 char *data
, int datalen
)
682 struct iscsi_reject
*reject
= (struct iscsi_reject
*)hdr
;
683 struct iscsi_hdr rejected_pdu
;
686 conn
->exp_statsn
= be32_to_cpu(reject
->statsn
) + 1;
688 if (reject
->reason
== ISCSI_REASON_DATA_DIGEST_ERROR
) {
689 if (ntoh24(reject
->dlength
) > datalen
)
690 return ISCSI_ERR_PROTO
;
692 if (ntoh24(reject
->dlength
) >= sizeof(struct iscsi_hdr
)) {
693 memcpy(&rejected_pdu
, data
, sizeof(struct iscsi_hdr
));
694 itt
= get_itt(rejected_pdu
.itt
);
695 iscsi_conn_printk(KERN_ERR
, conn
,
696 "itt 0x%x had pdu (op 0x%x) rejected "
697 "due to DataDigest error.\n", itt
,
698 rejected_pdu
.opcode
);
705 * iscsi_itt_to_task - look up task by itt
706 * @conn: iscsi connection
709 * This should be used for mgmt tasks like login and nops, or if
710 * the LDD's itt space does not include the session age.
712 * The session lock must be held.
714 static struct iscsi_task
*iscsi_itt_to_task(struct iscsi_conn
*conn
, itt_t itt
)
716 struct iscsi_session
*session
= conn
->session
;
719 if (itt
== RESERVED_ITT
)
723 if (i
>= session
->cmds_max
)
726 return session
->cmds
[i
];
730 * __iscsi_complete_pdu - complete pdu
734 * @datalen: len of data buffer
736 * Completes pdu processing by freeing any resources allocated at
737 * queuecommand or send generic. session lock must be held and verify
738 * itt must have been called.
740 int __iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
741 char *data
, int datalen
)
743 struct iscsi_session
*session
= conn
->session
;
744 int opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
, rc
= 0;
745 struct iscsi_task
*task
;
748 conn
->last_recv
= jiffies
;
749 rc
= iscsi_verify_itt(conn
, hdr
->itt
);
753 if (hdr
->itt
!= RESERVED_ITT
)
754 itt
= get_itt(hdr
->itt
);
758 debug_scsi("[op 0x%x cid %d itt 0x%x len %d]\n",
759 opcode
, conn
->id
, itt
, datalen
);
762 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
765 case ISCSI_OP_NOOP_IN
:
767 rc
= ISCSI_ERR_PROTO
;
771 if (hdr
->ttt
== cpu_to_be32(ISCSI_RESERVED_TAG
))
774 iscsi_send_nopout(conn
, (struct iscsi_nopin
*)hdr
);
776 case ISCSI_OP_REJECT
:
777 rc
= iscsi_handle_reject(conn
, hdr
, data
, datalen
);
779 case ISCSI_OP_ASYNC_EVENT
:
780 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
781 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
782 rc
= ISCSI_ERR_CONN_FAILED
;
785 rc
= ISCSI_ERR_BAD_OPCODE
;
792 case ISCSI_OP_SCSI_CMD_RSP
:
793 case ISCSI_OP_SCSI_DATA_IN
:
794 task
= iscsi_itt_to_ctask(conn
, hdr
->itt
);
796 return ISCSI_ERR_BAD_ITT
;
800 * LLD handles R2Ts if they need to.
803 case ISCSI_OP_LOGOUT_RSP
:
804 case ISCSI_OP_LOGIN_RSP
:
805 case ISCSI_OP_TEXT_RSP
:
806 case ISCSI_OP_SCSI_TMFUNC_RSP
:
807 case ISCSI_OP_NOOP_IN
:
808 task
= iscsi_itt_to_task(conn
, hdr
->itt
);
810 return ISCSI_ERR_BAD_ITT
;
813 return ISCSI_ERR_BAD_OPCODE
;
817 case ISCSI_OP_SCSI_CMD_RSP
:
818 iscsi_scsi_cmd_rsp(conn
, hdr
, task
, data
, datalen
);
820 case ISCSI_OP_SCSI_DATA_IN
:
821 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
822 conn
->scsirsp_pdus_cnt
++;
823 iscsi_update_cmdsn(session
,
824 (struct iscsi_nopin
*) hdr
);
825 __iscsi_put_task(task
);
828 case ISCSI_OP_LOGOUT_RSP
:
829 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
831 rc
= ISCSI_ERR_PROTO
;
834 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
836 case ISCSI_OP_LOGIN_RSP
:
837 case ISCSI_OP_TEXT_RSP
:
838 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
840 * login related PDU's exp_statsn is handled in
844 case ISCSI_OP_SCSI_TMFUNC_RSP
:
845 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
847 rc
= ISCSI_ERR_PROTO
;
851 iscsi_tmf_rsp(conn
, hdr
);
852 __iscsi_put_task(task
);
854 case ISCSI_OP_NOOP_IN
:
855 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
856 if (hdr
->ttt
!= cpu_to_be32(ISCSI_RESERVED_TAG
) || datalen
) {
857 rc
= ISCSI_ERR_PROTO
;
860 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
862 if (conn
->ping_task
!= task
)
864 * If this is not in response to one of our
865 * nops then it must be from userspace.
869 mod_timer(&conn
->transport_timer
, jiffies
+ conn
->recv_timeout
);
870 __iscsi_put_task(task
);
873 rc
= ISCSI_ERR_BAD_OPCODE
;
880 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
881 rc
= ISCSI_ERR_CONN_FAILED
;
882 __iscsi_put_task(task
);
885 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu
);
887 int iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
888 char *data
, int datalen
)
892 spin_lock(&conn
->session
->lock
);
893 rc
= __iscsi_complete_pdu(conn
, hdr
, data
, datalen
);
894 spin_unlock(&conn
->session
->lock
);
897 EXPORT_SYMBOL_GPL(iscsi_complete_pdu
);
899 int iscsi_verify_itt(struct iscsi_conn
*conn
, itt_t itt
)
901 struct iscsi_session
*session
= conn
->session
;
904 if (itt
== RESERVED_ITT
)
907 if (((__force u32
)itt
& ISCSI_AGE_MASK
) !=
908 (session
->age
<< ISCSI_AGE_SHIFT
)) {
909 iscsi_conn_printk(KERN_ERR
, conn
,
910 "received itt %x expected session age (%x)\n",
911 (__force u32
)itt
, session
->age
);
912 return ISCSI_ERR_BAD_ITT
;
916 if (i
>= session
->cmds_max
) {
917 iscsi_conn_printk(KERN_ERR
, conn
,
918 "received invalid itt index %u (max cmds "
919 "%u.\n", i
, session
->cmds_max
);
920 return ISCSI_ERR_BAD_ITT
;
924 EXPORT_SYMBOL_GPL(iscsi_verify_itt
);
927 * iscsi_itt_to_ctask - look up ctask by itt
928 * @conn: iscsi connection
931 * This should be used for cmd tasks.
933 * The session lock must be held.
935 struct iscsi_task
*iscsi_itt_to_ctask(struct iscsi_conn
*conn
, itt_t itt
)
937 struct iscsi_task
*task
;
939 if (iscsi_verify_itt(conn
, itt
))
942 task
= iscsi_itt_to_task(conn
, itt
);
943 if (!task
|| !task
->sc
)
946 if (task
->sc
->SCp
.phase
!= conn
->session
->age
) {
947 iscsi_session_printk(KERN_ERR
, conn
->session
,
948 "task's session age %d, expected %d\n",
949 task
->sc
->SCp
.phase
, conn
->session
->age
);
955 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask
);
957 void iscsi_conn_failure(struct iscsi_conn
*conn
, enum iscsi_err err
)
959 struct iscsi_session
*session
= conn
->session
;
962 spin_lock_irqsave(&session
->lock
, flags
);
963 if (session
->state
== ISCSI_STATE_FAILED
) {
964 spin_unlock_irqrestore(&session
->lock
, flags
);
968 if (conn
->stop_stage
== 0)
969 session
->state
= ISCSI_STATE_FAILED
;
970 spin_unlock_irqrestore(&session
->lock
, flags
);
971 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
972 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
973 iscsi_conn_error(conn
->cls_conn
, err
);
975 EXPORT_SYMBOL_GPL(iscsi_conn_failure
);
977 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn
*conn
)
979 struct iscsi_session
*session
= conn
->session
;
982 * Check for iSCSI window and take care of CmdSN wrap-around
984 if (!iscsi_sna_lte(session
->queued_cmdsn
, session
->max_cmdsn
)) {
985 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
986 "CmdSN %u/%u\n", session
->exp_cmdsn
,
987 session
->max_cmdsn
, session
->cmdsn
,
988 session
->queued_cmdsn
);
994 static int iscsi_xmit_task(struct iscsi_conn
*conn
)
996 struct iscsi_task
*task
= conn
->task
;
999 __iscsi_get_task(task
);
1000 spin_unlock_bh(&conn
->session
->lock
);
1001 rc
= conn
->session
->tt
->xmit_task(task
);
1002 spin_lock_bh(&conn
->session
->lock
);
1003 __iscsi_put_task(task
);
1005 /* done with this task */
1011 * iscsi_requeue_task - requeue task to run from session workqueue
1012 * @task: task to requeue
1014 * LLDs that need to run a task from the session workqueue should call
1015 * this. The session lock must be held. This should only be called
1016 * by software drivers.
1018 void iscsi_requeue_task(struct iscsi_task
*task
)
1020 struct iscsi_conn
*conn
= task
->conn
;
1022 list_move_tail(&task
->running
, &conn
->requeue
);
1023 scsi_queue_work(conn
->session
->host
, &conn
->xmitwork
);
1025 EXPORT_SYMBOL_GPL(iscsi_requeue_task
);
1028 * iscsi_data_xmit - xmit any command into the scheduled connection
1029 * @conn: iscsi connection
1032 * The function can return -EAGAIN in which case the caller must
1033 * re-schedule it again later or recover. '0' return code means
1036 static int iscsi_data_xmit(struct iscsi_conn
*conn
)
1040 spin_lock_bh(&conn
->session
->lock
);
1041 if (unlikely(conn
->suspend_tx
)) {
1042 debug_scsi("conn %d Tx suspended!\n", conn
->id
);
1043 spin_unlock_bh(&conn
->session
->lock
);
1048 rc
= iscsi_xmit_task(conn
);
1054 * process mgmt pdus like nops before commands since we should
1055 * only have one nop-out as a ping from us and targets should not
1056 * overflow us with nop-ins
1059 while (!list_empty(&conn
->mgmtqueue
)) {
1060 conn
->task
= list_entry(conn
->mgmtqueue
.next
,
1061 struct iscsi_task
, running
);
1062 if (iscsi_prep_mgmt_task(conn
, conn
->task
)) {
1063 __iscsi_put_task(conn
->task
);
1067 rc
= iscsi_xmit_task(conn
);
1072 /* process pending command queue */
1073 while (!list_empty(&conn
->xmitqueue
)) {
1074 if (conn
->tmf_state
== TMF_QUEUED
)
1077 conn
->task
= list_entry(conn
->xmitqueue
.next
,
1078 struct iscsi_task
, running
);
1079 if (conn
->session
->state
== ISCSI_STATE_LOGGING_OUT
) {
1080 fail_command(conn
, conn
->task
, DID_IMM_RETRY
<< 16);
1083 if (iscsi_prep_scsi_cmd_pdu(conn
->task
)) {
1084 fail_command(conn
, conn
->task
, DID_ABORT
<< 16);
1087 rc
= iscsi_xmit_task(conn
);
1091 * we could continuously get new task requests so
1092 * we need to check the mgmt queue for nops that need to
1093 * be sent to aviod starvation
1095 if (!list_empty(&conn
->mgmtqueue
))
1099 while (!list_empty(&conn
->requeue
)) {
1100 if (conn
->session
->fast_abort
&& conn
->tmf_state
!= TMF_INITIAL
)
1104 * we always do fastlogout - conn stop code will clean up.
1106 if (conn
->session
->state
== ISCSI_STATE_LOGGING_OUT
)
1109 conn
->task
= list_entry(conn
->requeue
.next
,
1110 struct iscsi_task
, running
);
1111 conn
->task
->state
= ISCSI_TASK_RUNNING
;
1112 list_move_tail(conn
->requeue
.next
, &conn
->run_list
);
1113 rc
= iscsi_xmit_task(conn
);
1116 if (!list_empty(&conn
->mgmtqueue
))
1119 spin_unlock_bh(&conn
->session
->lock
);
1123 if (unlikely(conn
->suspend_tx
))
1125 spin_unlock_bh(&conn
->session
->lock
);
1129 static void iscsi_xmitworker(struct work_struct
*work
)
1131 struct iscsi_conn
*conn
=
1132 container_of(work
, struct iscsi_conn
, xmitwork
);
1135 * serialize Xmit worker on a per-connection basis.
1138 rc
= iscsi_data_xmit(conn
);
1139 } while (rc
>= 0 || rc
== -EAGAIN
);
1143 FAILURE_BAD_HOST
= 1,
1144 FAILURE_SESSION_FAILED
,
1145 FAILURE_SESSION_FREED
,
1146 FAILURE_WINDOW_CLOSED
,
1148 FAILURE_SESSION_TERMINATE
,
1149 FAILURE_SESSION_IN_RECOVERY
,
1150 FAILURE_SESSION_RECOVERY_TIMEOUT
,
1151 FAILURE_SESSION_LOGGING_OUT
,
1152 FAILURE_SESSION_NOT_READY
,
1155 int iscsi_queuecommand(struct scsi_cmnd
*sc
, void (*done
)(struct scsi_cmnd
*))
1157 struct iscsi_cls_session
*cls_session
;
1158 struct Scsi_Host
*host
;
1160 struct iscsi_session
*session
;
1161 struct iscsi_conn
*conn
;
1162 struct iscsi_task
*task
= NULL
;
1164 sc
->scsi_done
= done
;
1168 host
= sc
->device
->host
;
1169 spin_unlock(host
->host_lock
);
1171 cls_session
= starget_to_session(scsi_target(sc
->device
));
1172 session
= cls_session
->dd_data
;
1173 spin_lock(&session
->lock
);
1175 reason
= iscsi_session_chkready(cls_session
);
1177 sc
->result
= reason
;
1182 * ISCSI_STATE_FAILED is a temp. state. The recovery
1183 * code will decide what is best to do with command queued
1186 if (session
->state
!= ISCSI_STATE_LOGGED_IN
&&
1187 session
->state
!= ISCSI_STATE_FAILED
) {
1189 * to handle the race between when we set the recovery state
1190 * and block the session we requeue here (commands could
1191 * be entering our queuecommand while a block is starting
1192 * up because the block code is not locked)
1194 switch (session
->state
) {
1195 case ISCSI_STATE_IN_RECOVERY
:
1196 reason
= FAILURE_SESSION_IN_RECOVERY
;
1197 sc
->result
= DID_IMM_RETRY
<< 16;
1199 case ISCSI_STATE_LOGGING_OUT
:
1200 reason
= FAILURE_SESSION_LOGGING_OUT
;
1201 sc
->result
= DID_IMM_RETRY
<< 16;
1203 case ISCSI_STATE_RECOVERY_FAILED
:
1204 reason
= FAILURE_SESSION_RECOVERY_TIMEOUT
;
1205 sc
->result
= DID_NO_CONNECT
<< 16;
1207 case ISCSI_STATE_TERMINATE
:
1208 reason
= FAILURE_SESSION_TERMINATE
;
1209 sc
->result
= DID_NO_CONNECT
<< 16;
1212 reason
= FAILURE_SESSION_FREED
;
1213 sc
->result
= DID_NO_CONNECT
<< 16;
1218 conn
= session
->leadconn
;
1220 reason
= FAILURE_SESSION_FREED
;
1221 sc
->result
= DID_NO_CONNECT
<< 16;
1225 if (iscsi_check_cmdsn_window_closed(conn
)) {
1226 reason
= FAILURE_WINDOW_CLOSED
;
1230 if (!__kfifo_get(session
->cmdpool
.queue
, (void*)&task
,
1232 reason
= FAILURE_OOM
;
1235 sc
->SCp
.phase
= session
->age
;
1236 sc
->SCp
.ptr
= (char *)task
;
1238 atomic_set(&task
->refcount
, 1);
1239 task
->state
= ISCSI_TASK_PENDING
;
1242 INIT_LIST_HEAD(&task
->running
);
1243 list_add_tail(&task
->running
, &conn
->xmitqueue
);
1245 if (session
->tt
->caps
& CAP_DATA_PATH_OFFLOAD
) {
1246 if (iscsi_prep_scsi_cmd_pdu(task
)) {
1247 sc
->result
= DID_ABORT
<< 16;
1248 sc
->scsi_done
= NULL
;
1249 iscsi_complete_command(task
);
1252 if (session
->tt
->xmit_task(task
)) {
1253 sc
->scsi_done
= NULL
;
1254 iscsi_complete_command(task
);
1255 reason
= FAILURE_SESSION_NOT_READY
;
1259 scsi_queue_work(session
->host
, &conn
->xmitwork
);
1261 session
->queued_cmdsn
++;
1262 spin_unlock(&session
->lock
);
1263 spin_lock(host
->host_lock
);
1267 spin_unlock(&session
->lock
);
1268 debug_scsi("cmd 0x%x rejected (%d)\n", sc
->cmnd
[0], reason
);
1269 spin_lock(host
->host_lock
);
1270 return SCSI_MLQUEUE_HOST_BUSY
;
1273 spin_unlock(&session
->lock
);
1274 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc
->cmnd
[0], reason
);
1275 if (!scsi_bidi_cmnd(sc
))
1276 scsi_set_resid(sc
, scsi_bufflen(sc
));
1278 scsi_out(sc
)->resid
= scsi_out(sc
)->length
;
1279 scsi_in(sc
)->resid
= scsi_in(sc
)->length
;
1282 spin_lock(host
->host_lock
);
1285 EXPORT_SYMBOL_GPL(iscsi_queuecommand
);
1287 int iscsi_change_queue_depth(struct scsi_device
*sdev
, int depth
)
1289 if (depth
> ISCSI_MAX_CMD_PER_LUN
)
1290 depth
= ISCSI_MAX_CMD_PER_LUN
;
1291 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), depth
);
1292 return sdev
->queue_depth
;
1294 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth
);
1296 void iscsi_session_recovery_timedout(struct iscsi_cls_session
*cls_session
)
1298 struct iscsi_session
*session
= cls_session
->dd_data
;
1300 spin_lock_bh(&session
->lock
);
1301 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
1302 session
->state
= ISCSI_STATE_RECOVERY_FAILED
;
1303 if (session
->leadconn
)
1304 wake_up(&session
->leadconn
->ehwait
);
1306 spin_unlock_bh(&session
->lock
);
1308 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout
);
1310 int iscsi_eh_host_reset(struct scsi_cmnd
*sc
)
1312 struct iscsi_cls_session
*cls_session
;
1313 struct iscsi_session
*session
;
1314 struct iscsi_conn
*conn
;
1316 cls_session
= starget_to_session(scsi_target(sc
->device
));
1317 session
= cls_session
->dd_data
;
1318 conn
= session
->leadconn
;
1320 mutex_lock(&session
->eh_mutex
);
1321 spin_lock_bh(&session
->lock
);
1322 if (session
->state
== ISCSI_STATE_TERMINATE
) {
1324 debug_scsi("failing host reset: session terminated "
1325 "[CID %d age %d]\n", conn
->id
, session
->age
);
1326 spin_unlock_bh(&session
->lock
);
1327 mutex_unlock(&session
->eh_mutex
);
1331 spin_unlock_bh(&session
->lock
);
1332 mutex_unlock(&session
->eh_mutex
);
1334 * we drop the lock here but the leadconn cannot be destoyed while
1335 * we are in the scsi eh
1337 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1339 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1340 wait_event_interruptible(conn
->ehwait
,
1341 session
->state
== ISCSI_STATE_TERMINATE
||
1342 session
->state
== ISCSI_STATE_LOGGED_IN
||
1343 session
->state
== ISCSI_STATE_RECOVERY_FAILED
);
1344 if (signal_pending(current
))
1345 flush_signals(current
);
1347 mutex_lock(&session
->eh_mutex
);
1348 spin_lock_bh(&session
->lock
);
1349 if (session
->state
== ISCSI_STATE_LOGGED_IN
)
1350 iscsi_session_printk(KERN_INFO
, session
,
1351 "host reset succeeded\n");
1354 spin_unlock_bh(&session
->lock
);
1355 mutex_unlock(&session
->eh_mutex
);
1358 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset
);
1360 static void iscsi_tmf_timedout(unsigned long data
)
1362 struct iscsi_conn
*conn
= (struct iscsi_conn
*)data
;
1363 struct iscsi_session
*session
= conn
->session
;
1365 spin_lock(&session
->lock
);
1366 if (conn
->tmf_state
== TMF_QUEUED
) {
1367 conn
->tmf_state
= TMF_TIMEDOUT
;
1368 debug_scsi("tmf timedout\n");
1369 /* unblock eh_abort() */
1370 wake_up(&conn
->ehwait
);
1372 spin_unlock(&session
->lock
);
1375 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn
*conn
,
1376 struct iscsi_tm
*hdr
, int age
,
1379 struct iscsi_session
*session
= conn
->session
;
1380 struct iscsi_task
*task
;
1382 task
= __iscsi_conn_send_pdu(conn
, (struct iscsi_hdr
*)hdr
,
1385 spin_unlock_bh(&session
->lock
);
1386 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1387 spin_lock_bh(&session
->lock
);
1388 debug_scsi("tmf exec failure\n");
1391 conn
->tmfcmd_pdus_cnt
++;
1392 conn
->tmf_timer
.expires
= timeout
* HZ
+ jiffies
;
1393 conn
->tmf_timer
.function
= iscsi_tmf_timedout
;
1394 conn
->tmf_timer
.data
= (unsigned long)conn
;
1395 add_timer(&conn
->tmf_timer
);
1396 debug_scsi("tmf set timeout\n");
1398 spin_unlock_bh(&session
->lock
);
1399 mutex_unlock(&session
->eh_mutex
);
1402 * block eh thread until:
1406 * 3) session is terminated or restarted or userspace has
1407 * given up on recovery
1409 wait_event_interruptible(conn
->ehwait
, age
!= session
->age
||
1410 session
->state
!= ISCSI_STATE_LOGGED_IN
||
1411 conn
->tmf_state
!= TMF_QUEUED
);
1412 if (signal_pending(current
))
1413 flush_signals(current
);
1414 del_timer_sync(&conn
->tmf_timer
);
1416 mutex_lock(&session
->eh_mutex
);
1417 spin_lock_bh(&session
->lock
);
1418 /* if the session drops it will clean up the task */
1419 if (age
!= session
->age
||
1420 session
->state
!= ISCSI_STATE_LOGGED_IN
)
1426 * Fail commands. session lock held and recv side suspended and xmit
1429 static void fail_all_commands(struct iscsi_conn
*conn
, unsigned lun
,
1432 struct iscsi_task
*task
, *tmp
;
1434 if (conn
->task
&& (conn
->task
->sc
->device
->lun
== lun
|| lun
== -1))
1438 list_for_each_entry_safe(task
, tmp
, &conn
->xmitqueue
, running
) {
1439 if (lun
== task
->sc
->device
->lun
|| lun
== -1) {
1440 debug_scsi("failing pending sc %p itt 0x%x\n",
1441 task
->sc
, task
->itt
);
1442 fail_command(conn
, task
, error
<< 16);
1446 list_for_each_entry_safe(task
, tmp
, &conn
->requeue
, running
) {
1447 if (lun
== task
->sc
->device
->lun
|| lun
== -1) {
1448 debug_scsi("failing requeued sc %p itt 0x%x\n",
1449 task
->sc
, task
->itt
);
1450 fail_command(conn
, task
, error
<< 16);
1454 /* fail all other running */
1455 list_for_each_entry_safe(task
, tmp
, &conn
->run_list
, running
) {
1456 if (lun
== task
->sc
->device
->lun
|| lun
== -1) {
1457 debug_scsi("failing in progress sc %p itt 0x%x\n",
1458 task
->sc
, task
->itt
);
1459 fail_command(conn
, task
, DID_BUS_BUSY
<< 16);
1464 void iscsi_suspend_tx(struct iscsi_conn
*conn
)
1466 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1467 if (!(conn
->session
->tt
->caps
& CAP_DATA_PATH_OFFLOAD
))
1468 scsi_flush_work(conn
->session
->host
);
1470 EXPORT_SYMBOL_GPL(iscsi_suspend_tx
);
1472 static void iscsi_start_tx(struct iscsi_conn
*conn
)
1474 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1475 if (!(conn
->session
->tt
->caps
& CAP_DATA_PATH_OFFLOAD
))
1476 scsi_queue_work(conn
->session
->host
, &conn
->xmitwork
);
1479 static enum scsi_eh_timer_return
iscsi_eh_cmd_timed_out(struct scsi_cmnd
*scmd
)
1481 struct iscsi_cls_session
*cls_session
;
1482 struct iscsi_session
*session
;
1483 struct iscsi_conn
*conn
;
1484 enum scsi_eh_timer_return rc
= EH_NOT_HANDLED
;
1486 cls_session
= starget_to_session(scsi_target(scmd
->device
));
1487 session
= cls_session
->dd_data
;
1489 debug_scsi("scsi cmd %p timedout\n", scmd
);
1491 spin_lock(&session
->lock
);
1492 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
1494 * We are probably in the middle of iscsi recovery so let
1495 * that complete and handle the error.
1497 rc
= EH_RESET_TIMER
;
1501 conn
= session
->leadconn
;
1503 /* In the middle of shuting down */
1504 rc
= EH_RESET_TIMER
;
1508 if (!conn
->recv_timeout
&& !conn
->ping_timeout
)
1511 * if the ping timedout then we are in the middle of cleaning up
1512 * and can let the iscsi eh handle it
1514 if (time_before_eq(conn
->last_recv
+ (conn
->recv_timeout
* HZ
) +
1515 (conn
->ping_timeout
* HZ
), jiffies
))
1516 rc
= EH_RESET_TIMER
;
1518 * if we are about to check the transport then give the command
1521 if (time_before_eq(conn
->last_recv
+ (conn
->recv_timeout
* HZ
),
1523 rc
= EH_RESET_TIMER
;
1524 /* if in the middle of checking the transport then give us more time */
1525 if (conn
->ping_task
)
1526 rc
= EH_RESET_TIMER
;
1528 spin_unlock(&session
->lock
);
1529 debug_scsi("return %s\n", rc
== EH_RESET_TIMER
? "timer reset" : "nh");
1533 static void iscsi_check_transport_timeouts(unsigned long data
)
1535 struct iscsi_conn
*conn
= (struct iscsi_conn
*)data
;
1536 struct iscsi_session
*session
= conn
->session
;
1537 unsigned long recv_timeout
, next_timeout
= 0, last_recv
;
1539 spin_lock(&session
->lock
);
1540 if (session
->state
!= ISCSI_STATE_LOGGED_IN
)
1543 recv_timeout
= conn
->recv_timeout
;
1548 last_recv
= conn
->last_recv
;
1549 if (conn
->ping_task
&&
1550 time_before_eq(conn
->last_ping
+ (conn
->ping_timeout
* HZ
),
1552 iscsi_conn_printk(KERN_ERR
, conn
, "ping timeout of %d secs "
1553 "expired, last rx %lu, last ping %lu, "
1554 "now %lu\n", conn
->ping_timeout
, last_recv
,
1555 conn
->last_ping
, jiffies
);
1556 spin_unlock(&session
->lock
);
1557 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1561 if (time_before_eq(last_recv
+ recv_timeout
, jiffies
)) {
1562 /* send a ping to try to provoke some traffic */
1563 debug_scsi("Sending nopout as ping on conn %p\n", conn
);
1564 iscsi_send_nopout(conn
, NULL
);
1565 next_timeout
= conn
->last_ping
+ (conn
->ping_timeout
* HZ
);
1567 next_timeout
= last_recv
+ recv_timeout
;
1569 debug_scsi("Setting next tmo %lu\n", next_timeout
);
1570 mod_timer(&conn
->transport_timer
, next_timeout
);
1572 spin_unlock(&session
->lock
);
1575 static void iscsi_prep_abort_task_pdu(struct iscsi_task
*task
,
1576 struct iscsi_tm
*hdr
)
1578 memset(hdr
, 0, sizeof(*hdr
));
1579 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
1580 hdr
->flags
= ISCSI_TM_FUNC_ABORT_TASK
& ISCSI_FLAG_TM_FUNC_MASK
;
1581 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
1582 memcpy(hdr
->lun
, task
->hdr
->lun
, sizeof(hdr
->lun
));
1583 hdr
->rtt
= task
->hdr
->itt
;
1584 hdr
->refcmdsn
= task
->hdr
->cmdsn
;
1587 int iscsi_eh_abort(struct scsi_cmnd
*sc
)
1589 struct iscsi_cls_session
*cls_session
;
1590 struct iscsi_session
*session
;
1591 struct iscsi_conn
*conn
;
1592 struct iscsi_task
*task
;
1593 struct iscsi_tm
*hdr
;
1596 cls_session
= starget_to_session(scsi_target(sc
->device
));
1597 session
= cls_session
->dd_data
;
1599 mutex_lock(&session
->eh_mutex
);
1600 spin_lock_bh(&session
->lock
);
1602 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1606 debug_scsi("sc never reached iscsi layer or it completed.\n");
1607 spin_unlock_bh(&session
->lock
);
1608 mutex_unlock(&session
->eh_mutex
);
1613 * If we are not logged in or we have started a new session
1614 * then let the host reset code handle this
1616 if (!session
->leadconn
|| session
->state
!= ISCSI_STATE_LOGGED_IN
||
1617 sc
->SCp
.phase
!= session
->age
) {
1618 spin_unlock_bh(&session
->lock
);
1619 mutex_unlock(&session
->eh_mutex
);
1623 conn
= session
->leadconn
;
1624 conn
->eh_abort_cnt
++;
1627 task
= (struct iscsi_task
*)sc
->SCp
.ptr
;
1628 debug_scsi("aborting [sc %p itt 0x%x]\n", sc
, task
->itt
);
1630 /* task completed before time out */
1632 debug_scsi("sc completed while abort in progress\n");
1636 if (task
->state
== ISCSI_TASK_PENDING
) {
1637 fail_command(conn
, task
, DID_ABORT
<< 16);
1641 /* only have one tmf outstanding at a time */
1642 if (conn
->tmf_state
!= TMF_INITIAL
)
1644 conn
->tmf_state
= TMF_QUEUED
;
1647 iscsi_prep_abort_task_pdu(task
, hdr
);
1649 if (iscsi_exec_task_mgmt_fn(conn
, hdr
, age
, session
->abort_timeout
)) {
1654 switch (conn
->tmf_state
) {
1656 spin_unlock_bh(&session
->lock
);
1658 * stop tx side incase the target had sent a abort rsp but
1659 * the initiator was still writing out data.
1661 iscsi_suspend_tx(conn
);
1663 * we do not stop the recv side because targets have been
1664 * good and have never sent us a successful tmf response
1665 * then sent more data for the cmd.
1667 spin_lock(&session
->lock
);
1668 fail_command(conn
, task
, DID_ABORT
<< 16);
1669 conn
->tmf_state
= TMF_INITIAL
;
1670 spin_unlock(&session
->lock
);
1671 iscsi_start_tx(conn
);
1672 goto success_unlocked
;
1674 spin_unlock_bh(&session
->lock
);
1675 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1676 goto failed_unlocked
;
1679 conn
->tmf_state
= TMF_INITIAL
;
1680 /* task completed before tmf abort response */
1681 debug_scsi("sc completed while abort in progress\n");
1686 conn
->tmf_state
= TMF_INITIAL
;
1691 spin_unlock_bh(&session
->lock
);
1693 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc
, task
->itt
);
1694 mutex_unlock(&session
->eh_mutex
);
1698 spin_unlock_bh(&session
->lock
);
1700 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc
,
1701 task
? task
->itt
: 0);
1702 mutex_unlock(&session
->eh_mutex
);
1705 EXPORT_SYMBOL_GPL(iscsi_eh_abort
);
1707 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd
*sc
, struct iscsi_tm
*hdr
)
1709 memset(hdr
, 0, sizeof(*hdr
));
1710 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
1711 hdr
->flags
= ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
& ISCSI_FLAG_TM_FUNC_MASK
;
1712 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
1713 int_to_scsilun(sc
->device
->lun
, (struct scsi_lun
*)hdr
->lun
);
1714 hdr
->rtt
= RESERVED_ITT
;
1717 int iscsi_eh_device_reset(struct scsi_cmnd
*sc
)
1719 struct iscsi_cls_session
*cls_session
;
1720 struct iscsi_session
*session
;
1721 struct iscsi_conn
*conn
;
1722 struct iscsi_tm
*hdr
;
1725 cls_session
= starget_to_session(scsi_target(sc
->device
));
1726 session
= cls_session
->dd_data
;
1728 debug_scsi("LU Reset [sc %p lun %u]\n", sc
, sc
->device
->lun
);
1730 mutex_lock(&session
->eh_mutex
);
1731 spin_lock_bh(&session
->lock
);
1733 * Just check if we are not logged in. We cannot check for
1734 * the phase because the reset could come from a ioctl.
1736 if (!session
->leadconn
|| session
->state
!= ISCSI_STATE_LOGGED_IN
)
1738 conn
= session
->leadconn
;
1740 /* only have one tmf outstanding at a time */
1741 if (conn
->tmf_state
!= TMF_INITIAL
)
1743 conn
->tmf_state
= TMF_QUEUED
;
1746 iscsi_prep_lun_reset_pdu(sc
, hdr
);
1748 if (iscsi_exec_task_mgmt_fn(conn
, hdr
, session
->age
,
1749 session
->lu_reset_timeout
)) {
1754 switch (conn
->tmf_state
) {
1758 spin_unlock_bh(&session
->lock
);
1759 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1762 conn
->tmf_state
= TMF_INITIAL
;
1767 spin_unlock_bh(&session
->lock
);
1769 iscsi_suspend_tx(conn
);
1771 spin_lock(&session
->lock
);
1772 fail_all_commands(conn
, sc
->device
->lun
, DID_ERROR
);
1773 conn
->tmf_state
= TMF_INITIAL
;
1774 spin_unlock(&session
->lock
);
1776 iscsi_start_tx(conn
);
1780 spin_unlock_bh(&session
->lock
);
1782 debug_scsi("iscsi_eh_device_reset %s\n",
1783 rc
== SUCCESS
? "SUCCESS" : "FAILED");
1784 mutex_unlock(&session
->eh_mutex
);
1787 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset
);
1790 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1791 * should be accessed via kfifo_{get,put} on q->queue.
1792 * Optionally, the caller can obtain the array of object pointers
1793 * by passing in a non-NULL @items pointer
1796 iscsi_pool_init(struct iscsi_pool
*q
, int max
, void ***items
, int item_size
)
1798 int i
, num_arrays
= 1;
1800 memset(q
, 0, sizeof(*q
));
1804 /* If the user passed an items pointer, he wants a copy of
1808 q
->pool
= kzalloc(num_arrays
* max
* sizeof(void*), GFP_KERNEL
);
1809 if (q
->pool
== NULL
)
1812 q
->queue
= kfifo_init((void*)q
->pool
, max
* sizeof(void*),
1814 if (q
->queue
== ERR_PTR(-ENOMEM
))
1817 for (i
= 0; i
< max
; i
++) {
1818 q
->pool
[i
] = kzalloc(item_size
, GFP_KERNEL
);
1819 if (q
->pool
[i
] == NULL
) {
1823 __kfifo_put(q
->queue
, (void*)&q
->pool
[i
], sizeof(void*));
1827 *items
= q
->pool
+ max
;
1828 memcpy(*items
, q
->pool
, max
* sizeof(void *));
1837 EXPORT_SYMBOL_GPL(iscsi_pool_init
);
1839 void iscsi_pool_free(struct iscsi_pool
*q
)
1843 for (i
= 0; i
< q
->max
; i
++)
1848 EXPORT_SYMBOL_GPL(iscsi_pool_free
);
1851 * iscsi_host_add - add host to system
1853 * @pdev: parent device
1855 * This should be called by partial offload and software iscsi drivers
1856 * to add a host to the system.
1858 int iscsi_host_add(struct Scsi_Host
*shost
, struct device
*pdev
)
1860 if (!shost
->can_queue
)
1861 shost
->can_queue
= ISCSI_DEF_XMIT_CMDS_MAX
;
1863 return scsi_add_host(shost
, pdev
);
1865 EXPORT_SYMBOL_GPL(iscsi_host_add
);
1868 * iscsi_host_alloc - allocate a host and driver data
1869 * @sht: scsi host template
1870 * @dd_data_size: driver host data size
1871 * @qdepth: default device queue depth
1873 * This should be called by partial offload and software iscsi drivers.
1874 * To access the driver specific memory use the iscsi_host_priv() macro.
1876 struct Scsi_Host
*iscsi_host_alloc(struct scsi_host_template
*sht
,
1877 int dd_data_size
, uint16_t qdepth
)
1879 struct Scsi_Host
*shost
;
1881 shost
= scsi_host_alloc(sht
, sizeof(struct iscsi_host
) + dd_data_size
);
1884 shost
->transportt
->eh_timed_out
= iscsi_eh_cmd_timed_out
;
1886 if (qdepth
> ISCSI_MAX_CMD_PER_LUN
|| qdepth
< 1) {
1888 printk(KERN_ERR
"iscsi: invalid queue depth of %d. "
1889 "Queue depth must be between 1 and %d.\n",
1890 qdepth
, ISCSI_MAX_CMD_PER_LUN
);
1891 qdepth
= ISCSI_DEF_CMD_PER_LUN
;
1893 shost
->cmd_per_lun
= qdepth
;
1896 EXPORT_SYMBOL_GPL(iscsi_host_alloc
);
1899 * iscsi_host_remove - remove host and sessions
1902 * This will also remove any sessions attached to the host, but if userspace
1903 * is managing the session at the same time this will break. TODO: add
1904 * refcounting to the netlink iscsi interface so a rmmod or host hot unplug
1905 * does not remove the memory from under us.
1907 void iscsi_host_remove(struct Scsi_Host
*shost
)
1909 iscsi_host_for_each_session(shost
, iscsi_session_teardown
);
1910 scsi_remove_host(shost
);
1912 EXPORT_SYMBOL_GPL(iscsi_host_remove
);
1914 void iscsi_host_free(struct Scsi_Host
*shost
)
1916 struct iscsi_host
*ihost
= shost_priv(shost
);
1918 kfree(ihost
->netdev
);
1919 kfree(ihost
->hwaddress
);
1920 kfree(ihost
->initiatorname
);
1921 scsi_host_put(shost
);
1923 EXPORT_SYMBOL_GPL(iscsi_host_free
);
1926 * iscsi_session_setup - create iscsi cls session and host and session
1927 * @iscsit: iscsi transport template
1929 * @cmds_max: session can queue
1930 * @cmd_task_size: LLD task private data size
1931 * @initial_cmdsn: initial CmdSN
1933 * This can be used by software iscsi_transports that allocate
1934 * a session per scsi host.
1936 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
1937 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
1938 * for nop handling and login/logout requests.
1940 struct iscsi_cls_session
*
1941 iscsi_session_setup(struct iscsi_transport
*iscsit
, struct Scsi_Host
*shost
,
1942 uint16_t cmds_max
, int cmd_task_size
,
1943 uint32_t initial_cmdsn
, unsigned int id
)
1945 struct iscsi_session
*session
;
1946 struct iscsi_cls_session
*cls_session
;
1947 int cmd_i
, scsi_cmds
, total_cmds
= cmds_max
;
1950 total_cmds
= ISCSI_DEF_XMIT_CMDS_MAX
;
1952 * The iscsi layer needs some tasks for nop handling and tmfs,
1953 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
1954 * + 1 command for scsi IO.
1956 if (total_cmds
< ISCSI_TOTAL_CMDS_MIN
) {
1957 printk(KERN_ERR
"iscsi: invalid can_queue of %d. can_queue "
1958 "must be a power of two that is at least %d.\n",
1959 total_cmds
, ISCSI_TOTAL_CMDS_MIN
);
1963 if (total_cmds
> ISCSI_TOTAL_CMDS_MAX
) {
1964 printk(KERN_ERR
"iscsi: invalid can_queue of %d. can_queue "
1965 "must be a power of 2 less than or equal to %d.\n",
1966 cmds_max
, ISCSI_TOTAL_CMDS_MAX
);
1967 total_cmds
= ISCSI_TOTAL_CMDS_MAX
;
1970 if (!is_power_of_2(total_cmds
)) {
1971 printk(KERN_ERR
"iscsi: invalid can_queue of %d. can_queue "
1972 "must be a power of 2.\n", total_cmds
);
1973 total_cmds
= rounddown_pow_of_two(total_cmds
);
1974 if (total_cmds
< ISCSI_TOTAL_CMDS_MIN
)
1976 printk(KERN_INFO
"iscsi: Rounding can_queue to %d.\n",
1979 scsi_cmds
= total_cmds
- ISCSI_MGMT_CMDS_MAX
;
1981 cls_session
= iscsi_alloc_session(shost
, iscsit
,
1982 sizeof(struct iscsi_session
));
1985 session
= cls_session
->dd_data
;
1986 session
->cls_session
= cls_session
;
1987 session
->host
= shost
;
1988 session
->state
= ISCSI_STATE_FREE
;
1989 session
->fast_abort
= 1;
1990 session
->lu_reset_timeout
= 15;
1991 session
->abort_timeout
= 10;
1992 session
->scsi_cmds_max
= scsi_cmds
;
1993 session
->cmds_max
= total_cmds
;
1994 session
->queued_cmdsn
= session
->cmdsn
= initial_cmdsn
;
1995 session
->exp_cmdsn
= initial_cmdsn
+ 1;
1996 session
->max_cmdsn
= initial_cmdsn
+ 1;
1997 session
->max_r2t
= 1;
1998 session
->tt
= iscsit
;
1999 mutex_init(&session
->eh_mutex
);
2000 spin_lock_init(&session
->lock
);
2002 /* initialize SCSI PDU commands pool */
2003 if (iscsi_pool_init(&session
->cmdpool
, session
->cmds_max
,
2004 (void***)&session
->cmds
,
2005 cmd_task_size
+ sizeof(struct iscsi_task
)))
2006 goto cmdpool_alloc_fail
;
2008 /* pre-format cmds pool with ITT */
2009 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
2010 struct iscsi_task
*task
= session
->cmds
[cmd_i
];
2013 task
->dd_data
= &task
[1];
2015 INIT_LIST_HEAD(&task
->running
);
2018 if (!try_module_get(iscsit
->owner
))
2019 goto module_get_fail
;
2021 if (iscsi_add_session(cls_session
, id
))
2022 goto cls_session_fail
;
2026 module_put(iscsit
->owner
);
2028 iscsi_pool_free(&session
->cmdpool
);
2030 iscsi_free_session(cls_session
);
2033 EXPORT_SYMBOL_GPL(iscsi_session_setup
);
2036 * iscsi_session_teardown - destroy session, host, and cls_session
2037 * @cls_session: iscsi session
2039 * The driver must have called iscsi_remove_session before
2042 void iscsi_session_teardown(struct iscsi_cls_session
*cls_session
)
2044 struct iscsi_session
*session
= cls_session
->dd_data
;
2045 struct module
*owner
= cls_session
->transport
->owner
;
2047 iscsi_pool_free(&session
->cmdpool
);
2049 kfree(session
->password
);
2050 kfree(session
->password_in
);
2051 kfree(session
->username
);
2052 kfree(session
->username_in
);
2053 kfree(session
->targetname
);
2054 kfree(session
->initiatorname
);
2055 kfree(session
->ifacename
);
2057 iscsi_destroy_session(cls_session
);
2060 EXPORT_SYMBOL_GPL(iscsi_session_teardown
);
2063 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2064 * @cls_session: iscsi_cls_session
2065 * @dd_size: private driver data size
2068 struct iscsi_cls_conn
*
2069 iscsi_conn_setup(struct iscsi_cls_session
*cls_session
, int dd_size
,
2072 struct iscsi_session
*session
= cls_session
->dd_data
;
2073 struct iscsi_conn
*conn
;
2074 struct iscsi_cls_conn
*cls_conn
;
2077 cls_conn
= iscsi_create_conn(cls_session
, sizeof(*conn
) + dd_size
,
2081 conn
= cls_conn
->dd_data
;
2082 memset(conn
, 0, sizeof(*conn
) + dd_size
);
2084 conn
->dd_data
= cls_conn
->dd_data
+ sizeof(*conn
);
2085 conn
->session
= session
;
2086 conn
->cls_conn
= cls_conn
;
2087 conn
->c_stage
= ISCSI_CONN_INITIAL_STAGE
;
2088 conn
->id
= conn_idx
;
2089 conn
->exp_statsn
= 0;
2090 conn
->tmf_state
= TMF_INITIAL
;
2092 init_timer(&conn
->transport_timer
);
2093 conn
->transport_timer
.data
= (unsigned long)conn
;
2094 conn
->transport_timer
.function
= iscsi_check_transport_timeouts
;
2096 INIT_LIST_HEAD(&conn
->run_list
);
2097 INIT_LIST_HEAD(&conn
->mgmt_run_list
);
2098 INIT_LIST_HEAD(&conn
->mgmtqueue
);
2099 INIT_LIST_HEAD(&conn
->xmitqueue
);
2100 INIT_LIST_HEAD(&conn
->requeue
);
2101 INIT_WORK(&conn
->xmitwork
, iscsi_xmitworker
);
2103 /* allocate login_task used for the login/text sequences */
2104 spin_lock_bh(&session
->lock
);
2105 if (!__kfifo_get(session
->cmdpool
.queue
,
2106 (void*)&conn
->login_task
,
2108 spin_unlock_bh(&session
->lock
);
2109 goto login_task_alloc_fail
;
2111 spin_unlock_bh(&session
->lock
);
2113 data
= kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN
, GFP_KERNEL
);
2115 goto login_task_data_alloc_fail
;
2116 conn
->login_task
->data
= conn
->data
= data
;
2118 init_timer(&conn
->tmf_timer
);
2119 init_waitqueue_head(&conn
->ehwait
);
2123 login_task_data_alloc_fail
:
2124 __kfifo_put(session
->cmdpool
.queue
, (void*)&conn
->login_task
,
2126 login_task_alloc_fail
:
2127 iscsi_destroy_conn(cls_conn
);
2130 EXPORT_SYMBOL_GPL(iscsi_conn_setup
);
2133 * iscsi_conn_teardown - teardown iscsi connection
2134 * cls_conn: iscsi class connection
2136 * TODO: we may need to make this into a two step process
2137 * like scsi-mls remove + put host
2139 void iscsi_conn_teardown(struct iscsi_cls_conn
*cls_conn
)
2141 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2142 struct iscsi_session
*session
= conn
->session
;
2143 unsigned long flags
;
2145 del_timer_sync(&conn
->transport_timer
);
2147 spin_lock_bh(&session
->lock
);
2148 conn
->c_stage
= ISCSI_CONN_CLEANUP_WAIT
;
2149 if (session
->leadconn
== conn
) {
2151 * leading connection? then give up on recovery.
2153 session
->state
= ISCSI_STATE_TERMINATE
;
2154 wake_up(&conn
->ehwait
);
2156 spin_unlock_bh(&session
->lock
);
2159 * Block until all in-progress commands for this connection
2163 spin_lock_irqsave(session
->host
->host_lock
, flags
);
2164 if (!session
->host
->host_busy
) { /* OK for ERL == 0 */
2165 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
2168 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
2169 msleep_interruptible(500);
2170 iscsi_conn_printk(KERN_INFO
, conn
, "iscsi conn_destroy(): "
2171 "host_busy %d host_failed %d\n",
2172 session
->host
->host_busy
,
2173 session
->host
->host_failed
);
2175 * force eh_abort() to unblock
2177 wake_up(&conn
->ehwait
);
2180 /* flush queued up work because we free the connection below */
2181 iscsi_suspend_tx(conn
);
2183 spin_lock_bh(&session
->lock
);
2185 kfree(conn
->persistent_address
);
2186 __kfifo_put(session
->cmdpool
.queue
, (void*)&conn
->login_task
,
2188 if (session
->leadconn
== conn
)
2189 session
->leadconn
= NULL
;
2190 spin_unlock_bh(&session
->lock
);
2192 iscsi_destroy_conn(cls_conn
);
2194 EXPORT_SYMBOL_GPL(iscsi_conn_teardown
);
2196 int iscsi_conn_start(struct iscsi_cls_conn
*cls_conn
)
2198 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2199 struct iscsi_session
*session
= conn
->session
;
2202 iscsi_conn_printk(KERN_ERR
, conn
,
2203 "can't start unbound connection\n");
2207 if ((session
->imm_data_en
|| !session
->initial_r2t_en
) &&
2208 session
->first_burst
> session
->max_burst
) {
2209 iscsi_conn_printk(KERN_INFO
, conn
, "invalid burst lengths: "
2210 "first_burst %d max_burst %d\n",
2211 session
->first_burst
, session
->max_burst
);
2215 if (conn
->ping_timeout
&& !conn
->recv_timeout
) {
2216 iscsi_conn_printk(KERN_ERR
, conn
, "invalid recv timeout of "
2217 "zero. Using 5 seconds\n.");
2218 conn
->recv_timeout
= 5;
2221 if (conn
->recv_timeout
&& !conn
->ping_timeout
) {
2222 iscsi_conn_printk(KERN_ERR
, conn
, "invalid ping timeout of "
2223 "zero. Using 5 seconds.\n");
2224 conn
->ping_timeout
= 5;
2227 spin_lock_bh(&session
->lock
);
2228 conn
->c_stage
= ISCSI_CONN_STARTED
;
2229 session
->state
= ISCSI_STATE_LOGGED_IN
;
2230 session
->queued_cmdsn
= session
->cmdsn
;
2232 conn
->last_recv
= jiffies
;
2233 conn
->last_ping
= jiffies
;
2234 if (conn
->recv_timeout
&& conn
->ping_timeout
)
2235 mod_timer(&conn
->transport_timer
,
2236 jiffies
+ (conn
->recv_timeout
* HZ
));
2238 switch(conn
->stop_stage
) {
2239 case STOP_CONN_RECOVER
:
2241 * unblock eh_abort() if it is blocked. re-try all
2242 * commands after successful recovery
2244 conn
->stop_stage
= 0;
2245 conn
->tmf_state
= TMF_INITIAL
;
2247 if (session
->age
== 16)
2250 case STOP_CONN_TERM
:
2251 conn
->stop_stage
= 0;
2256 spin_unlock_bh(&session
->lock
);
2258 iscsi_unblock_session(session
->cls_session
);
2259 wake_up(&conn
->ehwait
);
2262 EXPORT_SYMBOL_GPL(iscsi_conn_start
);
2265 flush_control_queues(struct iscsi_session
*session
, struct iscsi_conn
*conn
)
2267 struct iscsi_task
*task
, *tmp
;
2269 /* handle pending */
2270 list_for_each_entry_safe(task
, tmp
, &conn
->mgmtqueue
, running
) {
2271 debug_scsi("flushing pending mgmt task itt 0x%x\n", task
->itt
);
2272 /* release ref from prep task */
2273 __iscsi_put_task(task
);
2276 /* handle running */
2277 list_for_each_entry_safe(task
, tmp
, &conn
->mgmt_run_list
, running
) {
2278 debug_scsi("flushing running mgmt task itt 0x%x\n", task
->itt
);
2279 /* release ref from prep task */
2280 __iscsi_put_task(task
);
2286 static void iscsi_start_session_recovery(struct iscsi_session
*session
,
2287 struct iscsi_conn
*conn
, int flag
)
2291 del_timer_sync(&conn
->transport_timer
);
2293 mutex_lock(&session
->eh_mutex
);
2294 spin_lock_bh(&session
->lock
);
2295 if (conn
->stop_stage
== STOP_CONN_TERM
) {
2296 spin_unlock_bh(&session
->lock
);
2297 mutex_unlock(&session
->eh_mutex
);
2302 * When this is called for the in_login state, we only want to clean
2303 * up the login task and connection. We do not need to block and set
2304 * the recovery state again
2306 if (flag
== STOP_CONN_TERM
)
2307 session
->state
= ISCSI_STATE_TERMINATE
;
2308 else if (conn
->stop_stage
!= STOP_CONN_RECOVER
)
2309 session
->state
= ISCSI_STATE_IN_RECOVERY
;
2311 old_stop_stage
= conn
->stop_stage
;
2312 conn
->stop_stage
= flag
;
2313 conn
->c_stage
= ISCSI_CONN_STOPPED
;
2314 spin_unlock_bh(&session
->lock
);
2316 iscsi_suspend_tx(conn
);
2318 * for connection level recovery we should not calculate
2319 * header digest. conn->hdr_size used for optimization
2320 * in hdr_extract() and will be re-negotiated at
2323 if (flag
== STOP_CONN_RECOVER
) {
2324 conn
->hdrdgst_en
= 0;
2325 conn
->datadgst_en
= 0;
2326 if (session
->state
== ISCSI_STATE_IN_RECOVERY
&&
2327 old_stop_stage
!= STOP_CONN_RECOVER
) {
2328 debug_scsi("blocking session\n");
2329 iscsi_block_session(session
->cls_session
);
2336 spin_lock_bh(&session
->lock
);
2337 fail_all_commands(conn
, -1,
2338 STOP_CONN_RECOVER
? DID_BUS_BUSY
: DID_ERROR
);
2339 flush_control_queues(session
, conn
);
2340 spin_unlock_bh(&session
->lock
);
2341 mutex_unlock(&session
->eh_mutex
);
2344 void iscsi_conn_stop(struct iscsi_cls_conn
*cls_conn
, int flag
)
2346 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2347 struct iscsi_session
*session
= conn
->session
;
2350 case STOP_CONN_RECOVER
:
2351 case STOP_CONN_TERM
:
2352 iscsi_start_session_recovery(session
, conn
, flag
);
2355 iscsi_conn_printk(KERN_ERR
, conn
,
2356 "invalid stop flag %d\n", flag
);
2359 EXPORT_SYMBOL_GPL(iscsi_conn_stop
);
2361 int iscsi_conn_bind(struct iscsi_cls_session
*cls_session
,
2362 struct iscsi_cls_conn
*cls_conn
, int is_leading
)
2364 struct iscsi_session
*session
= cls_session
->dd_data
;
2365 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2367 spin_lock_bh(&session
->lock
);
2369 session
->leadconn
= conn
;
2370 spin_unlock_bh(&session
->lock
);
2373 * Unblock xmitworker(), Login Phase will pass through.
2375 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
2376 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
2379 EXPORT_SYMBOL_GPL(iscsi_conn_bind
);
2382 int iscsi_set_param(struct iscsi_cls_conn
*cls_conn
,
2383 enum iscsi_param param
, char *buf
, int buflen
)
2385 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2386 struct iscsi_session
*session
= conn
->session
;
2390 case ISCSI_PARAM_FAST_ABORT
:
2391 sscanf(buf
, "%d", &session
->fast_abort
);
2393 case ISCSI_PARAM_ABORT_TMO
:
2394 sscanf(buf
, "%d", &session
->abort_timeout
);
2396 case ISCSI_PARAM_LU_RESET_TMO
:
2397 sscanf(buf
, "%d", &session
->lu_reset_timeout
);
2399 case ISCSI_PARAM_PING_TMO
:
2400 sscanf(buf
, "%d", &conn
->ping_timeout
);
2402 case ISCSI_PARAM_RECV_TMO
:
2403 sscanf(buf
, "%d", &conn
->recv_timeout
);
2405 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
2406 sscanf(buf
, "%d", &conn
->max_recv_dlength
);
2408 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
2409 sscanf(buf
, "%d", &conn
->max_xmit_dlength
);
2411 case ISCSI_PARAM_HDRDGST_EN
:
2412 sscanf(buf
, "%d", &conn
->hdrdgst_en
);
2414 case ISCSI_PARAM_DATADGST_EN
:
2415 sscanf(buf
, "%d", &conn
->datadgst_en
);
2417 case ISCSI_PARAM_INITIAL_R2T_EN
:
2418 sscanf(buf
, "%d", &session
->initial_r2t_en
);
2420 case ISCSI_PARAM_MAX_R2T
:
2421 sscanf(buf
, "%d", &session
->max_r2t
);
2423 case ISCSI_PARAM_IMM_DATA_EN
:
2424 sscanf(buf
, "%d", &session
->imm_data_en
);
2426 case ISCSI_PARAM_FIRST_BURST
:
2427 sscanf(buf
, "%d", &session
->first_burst
);
2429 case ISCSI_PARAM_MAX_BURST
:
2430 sscanf(buf
, "%d", &session
->max_burst
);
2432 case ISCSI_PARAM_PDU_INORDER_EN
:
2433 sscanf(buf
, "%d", &session
->pdu_inorder_en
);
2435 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
2436 sscanf(buf
, "%d", &session
->dataseq_inorder_en
);
2438 case ISCSI_PARAM_ERL
:
2439 sscanf(buf
, "%d", &session
->erl
);
2441 case ISCSI_PARAM_IFMARKER_EN
:
2442 sscanf(buf
, "%d", &value
);
2445 case ISCSI_PARAM_OFMARKER_EN
:
2446 sscanf(buf
, "%d", &value
);
2449 case ISCSI_PARAM_EXP_STATSN
:
2450 sscanf(buf
, "%u", &conn
->exp_statsn
);
2452 case ISCSI_PARAM_USERNAME
:
2453 kfree(session
->username
);
2454 session
->username
= kstrdup(buf
, GFP_KERNEL
);
2455 if (!session
->username
)
2458 case ISCSI_PARAM_USERNAME_IN
:
2459 kfree(session
->username_in
);
2460 session
->username_in
= kstrdup(buf
, GFP_KERNEL
);
2461 if (!session
->username_in
)
2464 case ISCSI_PARAM_PASSWORD
:
2465 kfree(session
->password
);
2466 session
->password
= kstrdup(buf
, GFP_KERNEL
);
2467 if (!session
->password
)
2470 case ISCSI_PARAM_PASSWORD_IN
:
2471 kfree(session
->password_in
);
2472 session
->password_in
= kstrdup(buf
, GFP_KERNEL
);
2473 if (!session
->password_in
)
2476 case ISCSI_PARAM_TARGET_NAME
:
2477 /* this should not change between logins */
2478 if (session
->targetname
)
2481 session
->targetname
= kstrdup(buf
, GFP_KERNEL
);
2482 if (!session
->targetname
)
2485 case ISCSI_PARAM_TPGT
:
2486 sscanf(buf
, "%d", &session
->tpgt
);
2488 case ISCSI_PARAM_PERSISTENT_PORT
:
2489 sscanf(buf
, "%d", &conn
->persistent_port
);
2491 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
2493 * this is the address returned in discovery so it should
2494 * not change between logins.
2496 if (conn
->persistent_address
)
2499 conn
->persistent_address
= kstrdup(buf
, GFP_KERNEL
);
2500 if (!conn
->persistent_address
)
2503 case ISCSI_PARAM_IFACE_NAME
:
2504 if (!session
->ifacename
)
2505 session
->ifacename
= kstrdup(buf
, GFP_KERNEL
);
2507 case ISCSI_PARAM_INITIATOR_NAME
:
2508 if (!session
->initiatorname
)
2509 session
->initiatorname
= kstrdup(buf
, GFP_KERNEL
);
2517 EXPORT_SYMBOL_GPL(iscsi_set_param
);
2519 int iscsi_session_get_param(struct iscsi_cls_session
*cls_session
,
2520 enum iscsi_param param
, char *buf
)
2522 struct iscsi_session
*session
= cls_session
->dd_data
;
2526 case ISCSI_PARAM_FAST_ABORT
:
2527 len
= sprintf(buf
, "%d\n", session
->fast_abort
);
2529 case ISCSI_PARAM_ABORT_TMO
:
2530 len
= sprintf(buf
, "%d\n", session
->abort_timeout
);
2532 case ISCSI_PARAM_LU_RESET_TMO
:
2533 len
= sprintf(buf
, "%d\n", session
->lu_reset_timeout
);
2535 case ISCSI_PARAM_INITIAL_R2T_EN
:
2536 len
= sprintf(buf
, "%d\n", session
->initial_r2t_en
);
2538 case ISCSI_PARAM_MAX_R2T
:
2539 len
= sprintf(buf
, "%hu\n", session
->max_r2t
);
2541 case ISCSI_PARAM_IMM_DATA_EN
:
2542 len
= sprintf(buf
, "%d\n", session
->imm_data_en
);
2544 case ISCSI_PARAM_FIRST_BURST
:
2545 len
= sprintf(buf
, "%u\n", session
->first_burst
);
2547 case ISCSI_PARAM_MAX_BURST
:
2548 len
= sprintf(buf
, "%u\n", session
->max_burst
);
2550 case ISCSI_PARAM_PDU_INORDER_EN
:
2551 len
= sprintf(buf
, "%d\n", session
->pdu_inorder_en
);
2553 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
2554 len
= sprintf(buf
, "%d\n", session
->dataseq_inorder_en
);
2556 case ISCSI_PARAM_ERL
:
2557 len
= sprintf(buf
, "%d\n", session
->erl
);
2559 case ISCSI_PARAM_TARGET_NAME
:
2560 len
= sprintf(buf
, "%s\n", session
->targetname
);
2562 case ISCSI_PARAM_TPGT
:
2563 len
= sprintf(buf
, "%d\n", session
->tpgt
);
2565 case ISCSI_PARAM_USERNAME
:
2566 len
= sprintf(buf
, "%s\n", session
->username
);
2568 case ISCSI_PARAM_USERNAME_IN
:
2569 len
= sprintf(buf
, "%s\n", session
->username_in
);
2571 case ISCSI_PARAM_PASSWORD
:
2572 len
= sprintf(buf
, "%s\n", session
->password
);
2574 case ISCSI_PARAM_PASSWORD_IN
:
2575 len
= sprintf(buf
, "%s\n", session
->password_in
);
2577 case ISCSI_PARAM_IFACE_NAME
:
2578 len
= sprintf(buf
, "%s\n", session
->ifacename
);
2580 case ISCSI_PARAM_INITIATOR_NAME
:
2581 if (!session
->initiatorname
)
2582 len
= sprintf(buf
, "%s\n", "unknown");
2584 len
= sprintf(buf
, "%s\n", session
->initiatorname
);
2592 EXPORT_SYMBOL_GPL(iscsi_session_get_param
);
2594 int iscsi_conn_get_param(struct iscsi_cls_conn
*cls_conn
,
2595 enum iscsi_param param
, char *buf
)
2597 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2601 case ISCSI_PARAM_PING_TMO
:
2602 len
= sprintf(buf
, "%u\n", conn
->ping_timeout
);
2604 case ISCSI_PARAM_RECV_TMO
:
2605 len
= sprintf(buf
, "%u\n", conn
->recv_timeout
);
2607 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
2608 len
= sprintf(buf
, "%u\n", conn
->max_recv_dlength
);
2610 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
2611 len
= sprintf(buf
, "%u\n", conn
->max_xmit_dlength
);
2613 case ISCSI_PARAM_HDRDGST_EN
:
2614 len
= sprintf(buf
, "%d\n", conn
->hdrdgst_en
);
2616 case ISCSI_PARAM_DATADGST_EN
:
2617 len
= sprintf(buf
, "%d\n", conn
->datadgst_en
);
2619 case ISCSI_PARAM_IFMARKER_EN
:
2620 len
= sprintf(buf
, "%d\n", conn
->ifmarker_en
);
2622 case ISCSI_PARAM_OFMARKER_EN
:
2623 len
= sprintf(buf
, "%d\n", conn
->ofmarker_en
);
2625 case ISCSI_PARAM_EXP_STATSN
:
2626 len
= sprintf(buf
, "%u\n", conn
->exp_statsn
);
2628 case ISCSI_PARAM_PERSISTENT_PORT
:
2629 len
= sprintf(buf
, "%d\n", conn
->persistent_port
);
2631 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
2632 len
= sprintf(buf
, "%s\n", conn
->persistent_address
);
2640 EXPORT_SYMBOL_GPL(iscsi_conn_get_param
);
2642 int iscsi_host_get_param(struct Scsi_Host
*shost
, enum iscsi_host_param param
,
2645 struct iscsi_host
*ihost
= shost_priv(shost
);
2649 case ISCSI_HOST_PARAM_NETDEV_NAME
:
2651 len
= sprintf(buf
, "%s\n", "default");
2653 len
= sprintf(buf
, "%s\n", ihost
->netdev
);
2655 case ISCSI_HOST_PARAM_HWADDRESS
:
2656 if (!ihost
->hwaddress
)
2657 len
= sprintf(buf
, "%s\n", "default");
2659 len
= sprintf(buf
, "%s\n", ihost
->hwaddress
);
2661 case ISCSI_HOST_PARAM_INITIATOR_NAME
:
2662 if (!ihost
->initiatorname
)
2663 len
= sprintf(buf
, "%s\n", "unknown");
2665 len
= sprintf(buf
, "%s\n", ihost
->initiatorname
);
2667 case ISCSI_HOST_PARAM_IPADDRESS
:
2668 if (!strlen(ihost
->local_address
))
2669 len
= sprintf(buf
, "%s\n", "unknown");
2671 len
= sprintf(buf
, "%s\n",
2672 ihost
->local_address
);
2680 EXPORT_SYMBOL_GPL(iscsi_host_get_param
);
2682 int iscsi_host_set_param(struct Scsi_Host
*shost
, enum iscsi_host_param param
,
2683 char *buf
, int buflen
)
2685 struct iscsi_host
*ihost
= shost_priv(shost
);
2688 case ISCSI_HOST_PARAM_NETDEV_NAME
:
2690 ihost
->netdev
= kstrdup(buf
, GFP_KERNEL
);
2692 case ISCSI_HOST_PARAM_HWADDRESS
:
2693 if (!ihost
->hwaddress
)
2694 ihost
->hwaddress
= kstrdup(buf
, GFP_KERNEL
);
2696 case ISCSI_HOST_PARAM_INITIATOR_NAME
:
2697 if (!ihost
->initiatorname
)
2698 ihost
->initiatorname
= kstrdup(buf
, GFP_KERNEL
);
2706 EXPORT_SYMBOL_GPL(iscsi_host_set_param
);
2708 MODULE_AUTHOR("Mike Christie");
2709 MODULE_DESCRIPTION("iSCSI library functions");
2710 MODULE_LICENSE("GPL");