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 <linux/slab.h>
29 #include <linux/module.h>
30 #include <asm/unaligned.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_eh.h>
35 #include <scsi/scsi_tcq.h>
36 #include <scsi/scsi_host.h>
37 #include <scsi/scsi.h>
38 #include <scsi/iscsi_proto.h>
39 #include <scsi/scsi_transport.h>
40 #include <scsi/scsi_transport_iscsi.h>
41 #include <scsi/libiscsi.h>
43 static int iscsi_dbg_lib_conn
;
44 module_param_named(debug_libiscsi_conn
, iscsi_dbg_lib_conn
, int,
46 MODULE_PARM_DESC(debug_libiscsi_conn
,
47 "Turn on debugging for connections in libiscsi module. "
48 "Set to 1 to turn on, and zero to turn off. Default is off.");
50 static int iscsi_dbg_lib_session
;
51 module_param_named(debug_libiscsi_session
, iscsi_dbg_lib_session
, int,
53 MODULE_PARM_DESC(debug_libiscsi_session
,
54 "Turn on debugging for sessions in libiscsi module. "
55 "Set to 1 to turn on, and zero to turn off. Default is off.");
57 static int iscsi_dbg_lib_eh
;
58 module_param_named(debug_libiscsi_eh
, iscsi_dbg_lib_eh
, int,
60 MODULE_PARM_DESC(debug_libiscsi_eh
,
61 "Turn on debugging for error handling in libiscsi module. "
62 "Set to 1 to turn on, and zero to turn off. Default is off.");
64 #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
66 if (iscsi_dbg_lib_conn) \
67 iscsi_conn_printk(KERN_INFO, _conn, \
72 #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
74 if (iscsi_dbg_lib_session) \
75 iscsi_session_printk(KERN_INFO, _session, \
80 #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
82 if (iscsi_dbg_lib_eh) \
83 iscsi_session_printk(KERN_INFO, _session, \
88 inline void iscsi_conn_queue_work(struct iscsi_conn
*conn
)
90 struct Scsi_Host
*shost
= conn
->session
->host
;
91 struct iscsi_host
*ihost
= shost_priv(shost
);
94 queue_work(ihost
->workq
, &conn
->xmitwork
);
96 EXPORT_SYMBOL_GPL(iscsi_conn_queue_work
);
98 static void __iscsi_update_cmdsn(struct iscsi_session
*session
,
99 uint32_t exp_cmdsn
, uint32_t max_cmdsn
)
102 * standard specifies this check for when to update expected and
103 * max sequence numbers
105 if (iscsi_sna_lt(max_cmdsn
, exp_cmdsn
- 1))
108 if (exp_cmdsn
!= session
->exp_cmdsn
&&
109 !iscsi_sna_lt(exp_cmdsn
, session
->exp_cmdsn
))
110 session
->exp_cmdsn
= exp_cmdsn
;
112 if (max_cmdsn
!= session
->max_cmdsn
&&
113 !iscsi_sna_lt(max_cmdsn
, session
->max_cmdsn
)) {
114 session
->max_cmdsn
= max_cmdsn
;
116 * if the window closed with IO queued, then kick the
119 if (!list_empty(&session
->leadconn
->cmdqueue
) ||
120 !list_empty(&session
->leadconn
->mgmtqueue
))
121 iscsi_conn_queue_work(session
->leadconn
);
125 void iscsi_update_cmdsn(struct iscsi_session
*session
, struct iscsi_nopin
*hdr
)
127 __iscsi_update_cmdsn(session
, be32_to_cpu(hdr
->exp_cmdsn
),
128 be32_to_cpu(hdr
->max_cmdsn
));
130 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn
);
133 * iscsi_prep_data_out_pdu - initialize Data-Out
134 * @task: scsi command task
136 * @hdr: iscsi data in pdu
139 * Initialize Data-Out within this R2T sequence and finds
140 * proper data_offset within this SCSI command.
142 * This function is called with connection lock taken.
144 void iscsi_prep_data_out_pdu(struct iscsi_task
*task
, struct iscsi_r2t_info
*r2t
,
145 struct iscsi_data
*hdr
)
147 struct iscsi_conn
*conn
= task
->conn
;
148 unsigned int left
= r2t
->data_length
- r2t
->sent
;
150 task
->hdr_len
= sizeof(struct iscsi_data
);
152 memset(hdr
, 0, sizeof(struct iscsi_data
));
154 hdr
->datasn
= cpu_to_be32(r2t
->datasn
);
156 hdr
->opcode
= ISCSI_OP_SCSI_DATA_OUT
;
157 hdr
->lun
= task
->lun
;
158 hdr
->itt
= task
->hdr_itt
;
159 hdr
->exp_statsn
= r2t
->exp_statsn
;
160 hdr
->offset
= cpu_to_be32(r2t
->data_offset
+ r2t
->sent
);
161 if (left
> conn
->max_xmit_dlength
) {
162 hton24(hdr
->dlength
, conn
->max_xmit_dlength
);
163 r2t
->data_count
= conn
->max_xmit_dlength
;
166 hton24(hdr
->dlength
, left
);
167 r2t
->data_count
= left
;
168 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
170 conn
->dataout_pdus_cnt
++;
172 EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu
);
174 static int iscsi_add_hdr(struct iscsi_task
*task
, unsigned len
)
176 unsigned exp_len
= task
->hdr_len
+ len
;
178 if (exp_len
> task
->hdr_max
) {
183 WARN_ON(len
& (ISCSI_PAD_LEN
- 1)); /* caller must pad the AHS */
184 task
->hdr_len
= exp_len
;
189 * make an extended cdb AHS
191 static int iscsi_prep_ecdb_ahs(struct iscsi_task
*task
)
193 struct scsi_cmnd
*cmd
= task
->sc
;
194 unsigned rlen
, pad_len
;
195 unsigned short ahslength
;
196 struct iscsi_ecdb_ahdr
*ecdb_ahdr
;
199 ecdb_ahdr
= iscsi_next_hdr(task
);
200 rlen
= cmd
->cmd_len
- ISCSI_CDB_SIZE
;
202 BUG_ON(rlen
> sizeof(ecdb_ahdr
->ecdb
));
203 ahslength
= rlen
+ sizeof(ecdb_ahdr
->reserved
);
205 pad_len
= iscsi_padding(rlen
);
207 rc
= iscsi_add_hdr(task
, sizeof(ecdb_ahdr
->ahslength
) +
208 sizeof(ecdb_ahdr
->ahstype
) + ahslength
+ pad_len
);
213 memset(&ecdb_ahdr
->ecdb
[rlen
], 0, pad_len
);
215 ecdb_ahdr
->ahslength
= cpu_to_be16(ahslength
);
216 ecdb_ahdr
->ahstype
= ISCSI_AHSTYPE_CDB
;
217 ecdb_ahdr
->reserved
= 0;
218 memcpy(ecdb_ahdr
->ecdb
, cmd
->cmnd
+ ISCSI_CDB_SIZE
, rlen
);
220 ISCSI_DBG_SESSION(task
->conn
->session
,
221 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
222 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
223 "%u\n", cmd
->cmd_len
, rlen
, pad_len
, ahslength
,
228 static int iscsi_prep_bidi_ahs(struct iscsi_task
*task
)
230 struct scsi_cmnd
*sc
= task
->sc
;
231 struct iscsi_rlength_ahdr
*rlen_ahdr
;
234 rlen_ahdr
= iscsi_next_hdr(task
);
235 rc
= iscsi_add_hdr(task
, sizeof(*rlen_ahdr
));
239 rlen_ahdr
->ahslength
=
240 cpu_to_be16(sizeof(rlen_ahdr
->read_length
) +
241 sizeof(rlen_ahdr
->reserved
));
242 rlen_ahdr
->ahstype
= ISCSI_AHSTYPE_RLENGTH
;
243 rlen_ahdr
->reserved
= 0;
244 rlen_ahdr
->read_length
= cpu_to_be32(scsi_in(sc
)->length
);
246 ISCSI_DBG_SESSION(task
->conn
->session
,
247 "bidi-in rlen_ahdr->read_length(%d) "
248 "rlen_ahdr->ahslength(%d)\n",
249 be32_to_cpu(rlen_ahdr
->read_length
),
250 be16_to_cpu(rlen_ahdr
->ahslength
));
255 * iscsi_check_tmf_restrictions - check if a task is affected by TMF
257 * @opcode: opcode to check for
259 * During TMF a task has to be checked if it's affected.
260 * All unrelated I/O can be passed through, but I/O to the
261 * affected LUN should be restricted.
262 * If 'fast_abort' is set we won't be sending any I/O to the
264 * Otherwise the target is waiting for all TTTs to be completed,
265 * so we have to send all outstanding Data-Out PDUs to the target.
267 static int iscsi_check_tmf_restrictions(struct iscsi_task
*task
, int opcode
)
269 struct iscsi_conn
*conn
= task
->conn
;
270 struct iscsi_tm
*tmf
= &conn
->tmhdr
;
271 unsigned int hdr_lun
;
273 if (conn
->tmf_state
== TMF_INITIAL
)
276 if ((tmf
->opcode
& ISCSI_OPCODE_MASK
) != ISCSI_OP_SCSI_TMFUNC
)
279 switch (ISCSI_TM_FUNC_VALUE(tmf
)) {
280 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
282 * Allow PDUs for unrelated LUNs
284 hdr_lun
= scsilun_to_int(&tmf
->lun
);
285 if (hdr_lun
!= task
->sc
->device
->lun
)
288 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
290 * Fail all SCSI cmd PDUs
292 if (opcode
!= ISCSI_OP_SCSI_DATA_OUT
) {
293 iscsi_conn_printk(KERN_INFO
, conn
,
294 "task [op %x/%x itt "
297 task
->hdr
->opcode
, opcode
,
298 task
->itt
, task
->hdr_itt
);
302 * And also all data-out PDUs in response to R2T
303 * if fast_abort is set.
305 if (conn
->session
->fast_abort
) {
306 iscsi_conn_printk(KERN_INFO
, conn
,
307 "task [op %x/%x itt "
308 "0x%x/0x%x] fast abort.\n",
309 task
->hdr
->opcode
, opcode
,
310 task
->itt
, task
->hdr_itt
);
314 case ISCSI_TM_FUNC_ABORT_TASK
:
316 * the caller has already checked if the task
317 * they want to abort was in the pending queue so if
318 * we are here the cmd pdu has gone out already, and
319 * we will only hit this for data-outs
321 if (opcode
== ISCSI_OP_SCSI_DATA_OUT
&&
322 task
->hdr_itt
== tmf
->rtt
) {
323 ISCSI_DBG_SESSION(conn
->session
,
324 "Preventing task %x/%x from sending "
325 "data-out due to abort task in "
326 "progress\n", task
->itt
,
337 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
340 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
341 * fields like dlength or final based on how much data it sends
343 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task
*task
)
345 struct iscsi_conn
*conn
= task
->conn
;
346 struct iscsi_session
*session
= conn
->session
;
347 struct scsi_cmnd
*sc
= task
->sc
;
348 struct iscsi_scsi_req
*hdr
;
349 unsigned hdrlength
, cmd_len
;
353 rc
= iscsi_check_tmf_restrictions(task
, ISCSI_OP_SCSI_CMD
);
357 if (conn
->session
->tt
->alloc_pdu
) {
358 rc
= conn
->session
->tt
->alloc_pdu(task
, ISCSI_OP_SCSI_CMD
);
362 hdr
= (struct iscsi_scsi_req
*)task
->hdr
;
364 memset(hdr
, 0, sizeof(*hdr
));
366 if (session
->tt
->parse_pdu_itt
)
367 hdr
->itt
= task
->hdr_itt
= itt
;
369 hdr
->itt
= task
->hdr_itt
= build_itt(task
->itt
,
370 task
->conn
->session
->age
);
372 rc
= iscsi_add_hdr(task
, sizeof(*hdr
));
375 hdr
->opcode
= ISCSI_OP_SCSI_CMD
;
376 hdr
->flags
= ISCSI_ATTR_SIMPLE
;
377 int_to_scsilun(sc
->device
->lun
, &hdr
->lun
);
378 task
->lun
= hdr
->lun
;
379 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
380 cmd_len
= sc
->cmd_len
;
381 if (cmd_len
< ISCSI_CDB_SIZE
)
382 memset(&hdr
->cdb
[cmd_len
], 0, ISCSI_CDB_SIZE
- cmd_len
);
383 else if (cmd_len
> ISCSI_CDB_SIZE
) {
384 rc
= iscsi_prep_ecdb_ahs(task
);
387 cmd_len
= ISCSI_CDB_SIZE
;
389 memcpy(hdr
->cdb
, sc
->cmnd
, cmd_len
);
392 if (scsi_bidi_cmnd(sc
)) {
393 hdr
->flags
|= ISCSI_FLAG_CMD_READ
;
394 rc
= iscsi_prep_bidi_ahs(task
);
399 if (scsi_get_prot_op(sc
) != SCSI_PROT_NORMAL
)
400 task
->protected = true;
402 if (sc
->sc_data_direction
== DMA_TO_DEVICE
) {
403 unsigned out_len
= scsi_out(sc
)->length
;
404 struct iscsi_r2t_info
*r2t
= &task
->unsol_r2t
;
406 hdr
->data_length
= cpu_to_be32(out_len
);
407 hdr
->flags
|= ISCSI_FLAG_CMD_WRITE
;
411 * imm_count bytes to be sent right after
414 * unsol_count bytes(as Data-Out) to be sent
415 * without R2T ack right after
418 * r2t data_length bytes to be sent via R2T ack's
420 * pad_count bytes to be sent as zero-padding
422 memset(r2t
, 0, sizeof(*r2t
));
424 if (session
->imm_data_en
) {
425 if (out_len
>= session
->first_burst
)
426 task
->imm_count
= min(session
->first_burst
,
427 conn
->max_xmit_dlength
);
429 task
->imm_count
= min(out_len
,
430 conn
->max_xmit_dlength
);
431 hton24(hdr
->dlength
, task
->imm_count
);
433 zero_data(hdr
->dlength
);
435 if (!session
->initial_r2t_en
) {
436 r2t
->data_length
= min(session
->first_burst
, out_len
) -
438 r2t
->data_offset
= task
->imm_count
;
439 r2t
->ttt
= cpu_to_be32(ISCSI_RESERVED_TAG
);
440 r2t
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
443 if (!task
->unsol_r2t
.data_length
)
444 /* No unsolicit Data-Out's */
445 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
447 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
448 zero_data(hdr
->dlength
);
449 hdr
->data_length
= cpu_to_be32(scsi_in(sc
)->length
);
451 if (sc
->sc_data_direction
== DMA_FROM_DEVICE
)
452 hdr
->flags
|= ISCSI_FLAG_CMD_READ
;
455 /* calculate size of additional header segments (AHSs) */
456 hdrlength
= task
->hdr_len
- sizeof(*hdr
);
458 WARN_ON(hdrlength
& (ISCSI_PAD_LEN
-1));
459 hdrlength
/= ISCSI_PAD_LEN
;
461 WARN_ON(hdrlength
>= 256);
462 hdr
->hlength
= hdrlength
& 0xFF;
463 hdr
->cmdsn
= task
->cmdsn
= cpu_to_be32(session
->cmdsn
);
465 if (session
->tt
->init_task
&& session
->tt
->init_task(task
))
468 task
->state
= ISCSI_TASK_RUNNING
;
471 conn
->scsicmd_pdus_cnt
++;
472 ISCSI_DBG_SESSION(session
, "iscsi prep [%s cid %d sc %p cdb 0x%x "
473 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
474 scsi_bidi_cmnd(sc
) ? "bidirectional" :
475 sc
->sc_data_direction
== DMA_TO_DEVICE
?
476 "write" : "read", conn
->id
, sc
, sc
->cmnd
[0],
477 task
->itt
, scsi_bufflen(sc
),
478 scsi_bidi_cmnd(sc
) ? scsi_in(sc
)->length
: 0,
480 session
->max_cmdsn
- session
->exp_cmdsn
+ 1);
485 * iscsi_free_task - free a task
486 * @task: iscsi cmd task
488 * Must be called with session lock.
489 * This function returns the scsi command to scsi-ml or cleans
490 * up mgmt tasks then returns the task to the pool.
492 static void iscsi_free_task(struct iscsi_task
*task
)
494 struct iscsi_conn
*conn
= task
->conn
;
495 struct iscsi_session
*session
= conn
->session
;
496 struct scsi_cmnd
*sc
= task
->sc
;
497 int oldstate
= task
->state
;
499 ISCSI_DBG_SESSION(session
, "freeing task itt 0x%x state %d sc %p\n",
500 task
->itt
, task
->state
, task
->sc
);
502 session
->tt
->cleanup_task(task
);
503 task
->state
= ISCSI_TASK_FREE
;
506 * login task is preallocated so do not free
508 if (conn
->login_task
== task
)
511 kfifo_in(&session
->cmdpool
.queue
, (void*)&task
, sizeof(void*));
514 /* SCSI eh reuses commands to verify us */
517 * queue command may call this to free the task, so
518 * it will decide how to return sc to scsi-ml.
520 if (oldstate
!= ISCSI_TASK_REQUEUE_SCSIQ
)
525 void __iscsi_get_task(struct iscsi_task
*task
)
527 atomic_inc(&task
->refcount
);
529 EXPORT_SYMBOL_GPL(__iscsi_get_task
);
531 void __iscsi_put_task(struct iscsi_task
*task
)
533 if (atomic_dec_and_test(&task
->refcount
))
534 iscsi_free_task(task
);
536 EXPORT_SYMBOL_GPL(__iscsi_put_task
);
538 void iscsi_put_task(struct iscsi_task
*task
)
540 struct iscsi_session
*session
= task
->conn
->session
;
542 spin_lock_bh(&session
->lock
);
543 __iscsi_put_task(task
);
544 spin_unlock_bh(&session
->lock
);
546 EXPORT_SYMBOL_GPL(iscsi_put_task
);
549 * iscsi_complete_task - finish a task
550 * @task: iscsi cmd task
551 * @state: state to complete task with
553 * Must be called with session lock.
555 static void iscsi_complete_task(struct iscsi_task
*task
, int state
)
557 struct iscsi_conn
*conn
= task
->conn
;
559 ISCSI_DBG_SESSION(conn
->session
,
560 "complete task itt 0x%x state %d sc %p\n",
561 task
->itt
, task
->state
, task
->sc
);
562 if (task
->state
== ISCSI_TASK_COMPLETED
||
563 task
->state
== ISCSI_TASK_ABRT_TMF
||
564 task
->state
== ISCSI_TASK_ABRT_SESS_RECOV
||
565 task
->state
== ISCSI_TASK_REQUEUE_SCSIQ
)
567 WARN_ON_ONCE(task
->state
== ISCSI_TASK_FREE
);
570 if (!list_empty(&task
->running
))
571 list_del_init(&task
->running
);
573 if (conn
->task
== task
)
576 if (conn
->ping_task
== task
)
577 conn
->ping_task
= NULL
;
579 /* release get from queueing */
580 __iscsi_put_task(task
);
584 * iscsi_complete_scsi_task - finish scsi task normally
585 * @task: iscsi task for scsi cmd
586 * @exp_cmdsn: expected cmd sn in cpu format
587 * @max_cmdsn: max cmd sn in cpu format
589 * This is used when drivers do not need or cannot perform
590 * lower level pdu processing.
592 * Called with session lock
594 void iscsi_complete_scsi_task(struct iscsi_task
*task
,
595 uint32_t exp_cmdsn
, uint32_t max_cmdsn
)
597 struct iscsi_conn
*conn
= task
->conn
;
599 ISCSI_DBG_SESSION(conn
->session
, "[itt 0x%x]\n", task
->itt
);
601 conn
->last_recv
= jiffies
;
602 __iscsi_update_cmdsn(conn
->session
, exp_cmdsn
, max_cmdsn
);
603 iscsi_complete_task(task
, ISCSI_TASK_COMPLETED
);
605 EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task
);
609 * session lock must be held and if not called for a task that is
610 * still pending or from the xmit thread, then xmit thread must
613 static void fail_scsi_task(struct iscsi_task
*task
, int err
)
615 struct iscsi_conn
*conn
= task
->conn
;
616 struct scsi_cmnd
*sc
;
620 * if a command completes and we get a successful tmf response
621 * we will hit this because the scsi eh abort code does not take
628 if (task
->state
== ISCSI_TASK_PENDING
) {
630 * cmd never made it to the xmit thread, so we should not count
631 * the cmd in the sequencing
633 conn
->session
->queued_cmdsn
--;
634 /* it was never sent so just complete like normal */
635 state
= ISCSI_TASK_COMPLETED
;
636 } else if (err
== DID_TRANSPORT_DISRUPTED
)
637 state
= ISCSI_TASK_ABRT_SESS_RECOV
;
639 state
= ISCSI_TASK_ABRT_TMF
;
641 sc
->result
= err
<< 16;
642 if (!scsi_bidi_cmnd(sc
))
643 scsi_set_resid(sc
, scsi_bufflen(sc
));
645 scsi_out(sc
)->resid
= scsi_out(sc
)->length
;
646 scsi_in(sc
)->resid
= scsi_in(sc
)->length
;
649 iscsi_complete_task(task
, state
);
652 static int iscsi_prep_mgmt_task(struct iscsi_conn
*conn
,
653 struct iscsi_task
*task
)
655 struct iscsi_session
*session
= conn
->session
;
656 struct iscsi_hdr
*hdr
= task
->hdr
;
657 struct iscsi_nopout
*nop
= (struct iscsi_nopout
*)hdr
;
658 uint8_t opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
;
660 if (conn
->session
->state
== ISCSI_STATE_LOGGING_OUT
)
663 if (opcode
!= ISCSI_OP_LOGIN
&& opcode
!= ISCSI_OP_TEXT
)
664 nop
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
666 * pre-format CmdSN for outgoing PDU.
668 nop
->cmdsn
= cpu_to_be32(session
->cmdsn
);
669 if (hdr
->itt
!= RESERVED_ITT
) {
671 * TODO: We always use immediate for normal session pdus.
672 * If we start to send tmfs or nops as non-immediate then
673 * we should start checking the cmdsn numbers for mgmt tasks.
675 * During discovery sessions iscsid sends TEXT as non immediate,
676 * but we always only send one PDU at a time.
678 if (conn
->c_stage
== ISCSI_CONN_STARTED
&&
679 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
680 session
->queued_cmdsn
++;
685 if (session
->tt
->init_task
&& session
->tt
->init_task(task
))
688 if ((hdr
->opcode
& ISCSI_OPCODE_MASK
) == ISCSI_OP_LOGOUT
)
689 session
->state
= ISCSI_STATE_LOGGING_OUT
;
691 task
->state
= ISCSI_TASK_RUNNING
;
692 ISCSI_DBG_SESSION(session
, "mgmtpdu [op 0x%x hdr->itt 0x%x "
693 "datalen %d]\n", hdr
->opcode
& ISCSI_OPCODE_MASK
,
694 hdr
->itt
, task
->data_count
);
698 static struct iscsi_task
*
699 __iscsi_conn_send_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
700 char *data
, uint32_t data_size
)
702 struct iscsi_session
*session
= conn
->session
;
703 struct iscsi_host
*ihost
= shost_priv(session
->host
);
704 uint8_t opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
;
705 struct iscsi_task
*task
;
708 if (session
->state
== ISCSI_STATE_TERMINATE
)
711 if (opcode
== ISCSI_OP_LOGIN
|| opcode
== ISCSI_OP_TEXT
) {
713 * Login and Text are sent serially, in
714 * request-followed-by-response sequence.
715 * Same task can be used. Same ITT must be used.
716 * Note that login_task is preallocated at conn_create().
718 if (conn
->login_task
->state
!= ISCSI_TASK_FREE
) {
719 iscsi_conn_printk(KERN_ERR
, conn
, "Login/Text in "
720 "progress. Cannot start new task.\n");
724 task
= conn
->login_task
;
726 if (session
->state
!= ISCSI_STATE_LOGGED_IN
)
729 BUG_ON(conn
->c_stage
== ISCSI_CONN_INITIAL_STAGE
);
730 BUG_ON(conn
->c_stage
== ISCSI_CONN_STOPPED
);
732 if (!kfifo_out(&session
->cmdpool
.queue
,
733 (void*)&task
, sizeof(void*)))
737 * released in complete pdu for task we expect a response for, and
738 * released by the lld when it has transmitted the task for
739 * pdus we do not expect a response for.
741 atomic_set(&task
->refcount
, 1);
744 INIT_LIST_HEAD(&task
->running
);
745 task
->state
= ISCSI_TASK_PENDING
;
748 memcpy(task
->data
, data
, data_size
);
749 task
->data_count
= data_size
;
751 task
->data_count
= 0;
753 if (conn
->session
->tt
->alloc_pdu
) {
754 if (conn
->session
->tt
->alloc_pdu(task
, hdr
->opcode
)) {
755 iscsi_conn_printk(KERN_ERR
, conn
, "Could not allocate "
756 "pdu for mgmt task.\n");
761 itt
= task
->hdr
->itt
;
762 task
->hdr_len
= sizeof(struct iscsi_hdr
);
763 memcpy(task
->hdr
, hdr
, sizeof(struct iscsi_hdr
));
765 if (hdr
->itt
!= RESERVED_ITT
) {
766 if (session
->tt
->parse_pdu_itt
)
767 task
->hdr
->itt
= itt
;
769 task
->hdr
->itt
= build_itt(task
->itt
,
770 task
->conn
->session
->age
);
774 if (iscsi_prep_mgmt_task(conn
, task
))
777 if (session
->tt
->xmit_task(task
))
780 list_add_tail(&task
->running
, &conn
->mgmtqueue
);
781 iscsi_conn_queue_work(conn
);
787 __iscsi_put_task(task
);
791 int iscsi_conn_send_pdu(struct iscsi_cls_conn
*cls_conn
, struct iscsi_hdr
*hdr
,
792 char *data
, uint32_t data_size
)
794 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
795 struct iscsi_session
*session
= conn
->session
;
798 spin_lock_bh(&session
->lock
);
799 if (!__iscsi_conn_send_pdu(conn
, hdr
, data
, data_size
))
801 spin_unlock_bh(&session
->lock
);
804 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu
);
807 * iscsi_cmd_rsp - SCSI Command Response processing
808 * @conn: iscsi connection
810 * @task: scsi command task
811 * @data: cmd data buffer
812 * @datalen: len of buffer
814 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
815 * then completes the command and task.
817 static void iscsi_scsi_cmd_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
818 struct iscsi_task
*task
, char *data
,
821 struct iscsi_scsi_rsp
*rhdr
= (struct iscsi_scsi_rsp
*)hdr
;
822 struct iscsi_session
*session
= conn
->session
;
823 struct scsi_cmnd
*sc
= task
->sc
;
825 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)rhdr
);
826 conn
->exp_statsn
= be32_to_cpu(rhdr
->statsn
) + 1;
828 sc
->result
= (DID_OK
<< 16) | rhdr
->cmd_status
;
830 if (task
->protected) {
835 * Transports that didn't implement check_protection
836 * callback but still published T10-PI support to scsi-mid
837 * deserve this BUG_ON.
839 BUG_ON(!session
->tt
->check_protection
);
841 ascq
= session
->tt
->check_protection(task
, §or
);
843 sc
->result
= DRIVER_SENSE
<< 24 |
844 SAM_STAT_CHECK_CONDITION
;
845 scsi_build_sense_buffer(1, sc
->sense_buffer
,
846 ILLEGAL_REQUEST
, 0x10, ascq
);
847 sc
->sense_buffer
[7] = 0xc; /* Additional sense length */
848 sc
->sense_buffer
[8] = 0; /* Information desc type */
849 sc
->sense_buffer
[9] = 0xa; /* Additional desc length */
850 sc
->sense_buffer
[10] = 0x80; /* Validity bit */
852 put_unaligned_be64(sector
, &sc
->sense_buffer
[12]);
857 if (rhdr
->response
!= ISCSI_STATUS_CMD_COMPLETED
) {
858 sc
->result
= DID_ERROR
<< 16;
862 if (rhdr
->cmd_status
== SAM_STAT_CHECK_CONDITION
) {
867 iscsi_conn_printk(KERN_ERR
, conn
,
868 "Got CHECK_CONDITION but invalid data "
869 "buffer size of %d\n", datalen
);
870 sc
->result
= DID_BAD_TARGET
<< 16;
874 senselen
= get_unaligned_be16(data
);
875 if (datalen
< senselen
)
876 goto invalid_datalen
;
878 memcpy(sc
->sense_buffer
, data
+ 2,
879 min_t(uint16_t, senselen
, SCSI_SENSE_BUFFERSIZE
));
880 ISCSI_DBG_SESSION(session
, "copied %d bytes of sense\n",
881 min_t(uint16_t, senselen
,
882 SCSI_SENSE_BUFFERSIZE
));
885 if (rhdr
->flags
& (ISCSI_FLAG_CMD_BIDI_UNDERFLOW
|
886 ISCSI_FLAG_CMD_BIDI_OVERFLOW
)) {
887 int res_count
= be32_to_cpu(rhdr
->bi_residual_count
);
889 if (scsi_bidi_cmnd(sc
) && res_count
> 0 &&
890 (rhdr
->flags
& ISCSI_FLAG_CMD_BIDI_OVERFLOW
||
891 res_count
<= scsi_in(sc
)->length
))
892 scsi_in(sc
)->resid
= res_count
;
894 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
897 if (rhdr
->flags
& (ISCSI_FLAG_CMD_UNDERFLOW
|
898 ISCSI_FLAG_CMD_OVERFLOW
)) {
899 int res_count
= be32_to_cpu(rhdr
->residual_count
);
902 (rhdr
->flags
& ISCSI_FLAG_CMD_OVERFLOW
||
903 res_count
<= scsi_bufflen(sc
)))
904 /* write side for bidi or uni-io set_resid */
905 scsi_set_resid(sc
, res_count
);
907 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
910 ISCSI_DBG_SESSION(session
, "cmd rsp done [sc %p res %d itt 0x%x]\n",
911 sc
, sc
->result
, task
->itt
);
912 conn
->scsirsp_pdus_cnt
++;
913 iscsi_complete_task(task
, ISCSI_TASK_COMPLETED
);
917 * iscsi_data_in_rsp - SCSI Data-In Response processing
918 * @conn: iscsi connection
920 * @task: scsi command task
923 iscsi_data_in_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
924 struct iscsi_task
*task
)
926 struct iscsi_data_rsp
*rhdr
= (struct iscsi_data_rsp
*)hdr
;
927 struct scsi_cmnd
*sc
= task
->sc
;
929 if (!(rhdr
->flags
& ISCSI_FLAG_DATA_STATUS
))
932 iscsi_update_cmdsn(conn
->session
, (struct iscsi_nopin
*)hdr
);
933 sc
->result
= (DID_OK
<< 16) | rhdr
->cmd_status
;
934 conn
->exp_statsn
= be32_to_cpu(rhdr
->statsn
) + 1;
935 if (rhdr
->flags
& (ISCSI_FLAG_DATA_UNDERFLOW
|
936 ISCSI_FLAG_DATA_OVERFLOW
)) {
937 int res_count
= be32_to_cpu(rhdr
->residual_count
);
940 (rhdr
->flags
& ISCSI_FLAG_CMD_OVERFLOW
||
941 res_count
<= scsi_in(sc
)->length
))
942 scsi_in(sc
)->resid
= res_count
;
944 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
947 ISCSI_DBG_SESSION(conn
->session
, "data in with status done "
948 "[sc %p res %d itt 0x%x]\n",
949 sc
, sc
->result
, task
->itt
);
950 conn
->scsirsp_pdus_cnt
++;
951 iscsi_complete_task(task
, ISCSI_TASK_COMPLETED
);
954 static void iscsi_tmf_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
)
956 struct iscsi_tm_rsp
*tmf
= (struct iscsi_tm_rsp
*)hdr
;
958 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
959 conn
->tmfrsp_pdus_cnt
++;
961 if (conn
->tmf_state
!= TMF_QUEUED
)
964 if (tmf
->response
== ISCSI_TMF_RSP_COMPLETE
)
965 conn
->tmf_state
= TMF_SUCCESS
;
966 else if (tmf
->response
== ISCSI_TMF_RSP_NO_TASK
)
967 conn
->tmf_state
= TMF_NOT_FOUND
;
969 conn
->tmf_state
= TMF_FAILED
;
970 wake_up(&conn
->ehwait
);
973 static void iscsi_send_nopout(struct iscsi_conn
*conn
, struct iscsi_nopin
*rhdr
)
975 struct iscsi_nopout hdr
;
976 struct iscsi_task
*task
;
978 if (!rhdr
&& conn
->ping_task
)
981 memset(&hdr
, 0, sizeof(struct iscsi_nopout
));
982 hdr
.opcode
= ISCSI_OP_NOOP_OUT
| ISCSI_OP_IMMEDIATE
;
983 hdr
.flags
= ISCSI_FLAG_CMD_FINAL
;
988 hdr
.itt
= RESERVED_ITT
;
990 hdr
.ttt
= RESERVED_ITT
;
992 task
= __iscsi_conn_send_pdu(conn
, (struct iscsi_hdr
*)&hdr
, NULL
, 0);
994 iscsi_conn_printk(KERN_ERR
, conn
, "Could not send nopout\n");
996 /* only track our nops */
997 conn
->ping_task
= task
;
998 conn
->last_ping
= jiffies
;
1002 static int iscsi_nop_out_rsp(struct iscsi_task
*task
,
1003 struct iscsi_nopin
*nop
, char *data
, int datalen
)
1005 struct iscsi_conn
*conn
= task
->conn
;
1008 if (conn
->ping_task
!= task
) {
1010 * If this is not in response to one of our
1011 * nops then it must be from userspace.
1013 if (iscsi_recv_pdu(conn
->cls_conn
, (struct iscsi_hdr
*)nop
,
1015 rc
= ISCSI_ERR_CONN_FAILED
;
1017 mod_timer(&conn
->transport_timer
, jiffies
+ conn
->recv_timeout
);
1018 iscsi_complete_task(task
, ISCSI_TASK_COMPLETED
);
1022 static int iscsi_handle_reject(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
1023 char *data
, int datalen
)
1025 struct iscsi_reject
*reject
= (struct iscsi_reject
*)hdr
;
1026 struct iscsi_hdr rejected_pdu
;
1029 conn
->exp_statsn
= be32_to_cpu(reject
->statsn
) + 1;
1031 if (ntoh24(reject
->dlength
) > datalen
||
1032 ntoh24(reject
->dlength
) < sizeof(struct iscsi_hdr
)) {
1033 iscsi_conn_printk(KERN_ERR
, conn
, "Cannot handle rejected "
1034 "pdu. Invalid data length (pdu dlength "
1035 "%u, datalen %d\n", ntoh24(reject
->dlength
),
1037 return ISCSI_ERR_PROTO
;
1039 memcpy(&rejected_pdu
, data
, sizeof(struct iscsi_hdr
));
1040 opcode
= rejected_pdu
.opcode
& ISCSI_OPCODE_MASK
;
1042 switch (reject
->reason
) {
1043 case ISCSI_REASON_DATA_DIGEST_ERROR
:
1044 iscsi_conn_printk(KERN_ERR
, conn
,
1045 "pdu (op 0x%x itt 0x%x) rejected "
1046 "due to DataDigest error.\n",
1047 rejected_pdu
.itt
, opcode
);
1049 case ISCSI_REASON_IMM_CMD_REJECT
:
1050 iscsi_conn_printk(KERN_ERR
, conn
,
1051 "pdu (op 0x%x itt 0x%x) rejected. Too many "
1052 "immediate commands.\n",
1053 rejected_pdu
.itt
, opcode
);
1055 * We only send one TMF at a time so if the target could not
1056 * handle it, then it should get fixed (RFC mandates that
1057 * a target can handle one immediate TMF per conn).
1059 * For nops-outs, we could have sent more than one if
1060 * the target is sending us lots of nop-ins
1062 if (opcode
!= ISCSI_OP_NOOP_OUT
)
1065 if (rejected_pdu
.itt
== cpu_to_be32(ISCSI_RESERVED_TAG
))
1067 * nop-out in response to target's nop-out rejected.
1070 iscsi_send_nopout(conn
,
1071 (struct iscsi_nopin
*)&rejected_pdu
);
1073 struct iscsi_task
*task
;
1075 * Our nop as ping got dropped. We know the target
1076 * and transport are ok so just clean up
1078 task
= iscsi_itt_to_task(conn
, rejected_pdu
.itt
);
1080 iscsi_conn_printk(KERN_ERR
, conn
,
1081 "Invalid pdu reject. Could "
1082 "not lookup rejected task.\n");
1083 rc
= ISCSI_ERR_BAD_ITT
;
1085 rc
= iscsi_nop_out_rsp(task
,
1086 (struct iscsi_nopin
*)&rejected_pdu
,
1091 iscsi_conn_printk(KERN_ERR
, conn
,
1092 "pdu (op 0x%x itt 0x%x) rejected. Reason "
1093 "code 0x%x\n", rejected_pdu
.itt
,
1094 rejected_pdu
.opcode
, reject
->reason
);
1101 * iscsi_itt_to_task - look up task by itt
1102 * @conn: iscsi connection
1105 * This should be used for mgmt tasks like login and nops, or if
1106 * the LDD's itt space does not include the session age.
1108 * The session lock must be held.
1110 struct iscsi_task
*iscsi_itt_to_task(struct iscsi_conn
*conn
, itt_t itt
)
1112 struct iscsi_session
*session
= conn
->session
;
1115 if (itt
== RESERVED_ITT
)
1118 if (session
->tt
->parse_pdu_itt
)
1119 session
->tt
->parse_pdu_itt(conn
, itt
, &i
, NULL
);
1122 if (i
>= session
->cmds_max
)
1125 return session
->cmds
[i
];
1127 EXPORT_SYMBOL_GPL(iscsi_itt_to_task
);
1130 * __iscsi_complete_pdu - complete pdu
1132 * @hdr: iscsi header
1133 * @data: data buffer
1134 * @datalen: len of data buffer
1136 * Completes pdu processing by freeing any resources allocated at
1137 * queuecommand or send generic. session lock must be held and verify
1138 * itt must have been called.
1140 int __iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
1141 char *data
, int datalen
)
1143 struct iscsi_session
*session
= conn
->session
;
1144 int opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
, rc
= 0;
1145 struct iscsi_task
*task
;
1148 conn
->last_recv
= jiffies
;
1149 rc
= iscsi_verify_itt(conn
, hdr
->itt
);
1153 if (hdr
->itt
!= RESERVED_ITT
)
1154 itt
= get_itt(hdr
->itt
);
1158 ISCSI_DBG_SESSION(session
, "[op 0x%x cid %d itt 0x%x len %d]\n",
1159 opcode
, conn
->id
, itt
, datalen
);
1162 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
1165 case ISCSI_OP_NOOP_IN
:
1167 rc
= ISCSI_ERR_PROTO
;
1171 if (hdr
->ttt
== cpu_to_be32(ISCSI_RESERVED_TAG
))
1174 iscsi_send_nopout(conn
, (struct iscsi_nopin
*)hdr
);
1176 case ISCSI_OP_REJECT
:
1177 rc
= iscsi_handle_reject(conn
, hdr
, data
, datalen
);
1179 case ISCSI_OP_ASYNC_EVENT
:
1180 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
1181 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
1182 rc
= ISCSI_ERR_CONN_FAILED
;
1185 rc
= ISCSI_ERR_BAD_OPCODE
;
1192 case ISCSI_OP_SCSI_CMD_RSP
:
1193 case ISCSI_OP_SCSI_DATA_IN
:
1194 task
= iscsi_itt_to_ctask(conn
, hdr
->itt
);
1196 return ISCSI_ERR_BAD_ITT
;
1197 task
->last_xfer
= jiffies
;
1201 * LLD handles R2Ts if they need to.
1204 case ISCSI_OP_LOGOUT_RSP
:
1205 case ISCSI_OP_LOGIN_RSP
:
1206 case ISCSI_OP_TEXT_RSP
:
1207 case ISCSI_OP_SCSI_TMFUNC_RSP
:
1208 case ISCSI_OP_NOOP_IN
:
1209 task
= iscsi_itt_to_task(conn
, hdr
->itt
);
1211 return ISCSI_ERR_BAD_ITT
;
1214 return ISCSI_ERR_BAD_OPCODE
;
1218 case ISCSI_OP_SCSI_CMD_RSP
:
1219 iscsi_scsi_cmd_rsp(conn
, hdr
, task
, data
, datalen
);
1221 case ISCSI_OP_SCSI_DATA_IN
:
1222 iscsi_data_in_rsp(conn
, hdr
, task
);
1224 case ISCSI_OP_LOGOUT_RSP
:
1225 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
1227 rc
= ISCSI_ERR_PROTO
;
1230 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
1232 case ISCSI_OP_LOGIN_RSP
:
1233 case ISCSI_OP_TEXT_RSP
:
1234 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
1236 * login related PDU's exp_statsn is handled in
1240 case ISCSI_OP_SCSI_TMFUNC_RSP
:
1241 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
1243 rc
= ISCSI_ERR_PROTO
;
1247 iscsi_tmf_rsp(conn
, hdr
);
1248 iscsi_complete_task(task
, ISCSI_TASK_COMPLETED
);
1250 case ISCSI_OP_NOOP_IN
:
1251 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
1252 if (hdr
->ttt
!= cpu_to_be32(ISCSI_RESERVED_TAG
) || datalen
) {
1253 rc
= ISCSI_ERR_PROTO
;
1256 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
1258 rc
= iscsi_nop_out_rsp(task
, (struct iscsi_nopin
*)hdr
,
1262 rc
= ISCSI_ERR_BAD_OPCODE
;
1269 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
1270 rc
= ISCSI_ERR_CONN_FAILED
;
1271 iscsi_complete_task(task
, ISCSI_TASK_COMPLETED
);
1274 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu
);
1276 int iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
1277 char *data
, int datalen
)
1281 spin_lock(&conn
->session
->lock
);
1282 rc
= __iscsi_complete_pdu(conn
, hdr
, data
, datalen
);
1283 spin_unlock(&conn
->session
->lock
);
1286 EXPORT_SYMBOL_GPL(iscsi_complete_pdu
);
1288 int iscsi_verify_itt(struct iscsi_conn
*conn
, itt_t itt
)
1290 struct iscsi_session
*session
= conn
->session
;
1293 if (itt
== RESERVED_ITT
)
1296 if (session
->tt
->parse_pdu_itt
)
1297 session
->tt
->parse_pdu_itt(conn
, itt
, &i
, &age
);
1300 age
= ((__force u32
)itt
>> ISCSI_AGE_SHIFT
) & ISCSI_AGE_MASK
;
1303 if (age
!= session
->age
) {
1304 iscsi_conn_printk(KERN_ERR
, conn
,
1305 "received itt %x expected session age (%x)\n",
1306 (__force u32
)itt
, session
->age
);
1307 return ISCSI_ERR_BAD_ITT
;
1310 if (i
>= session
->cmds_max
) {
1311 iscsi_conn_printk(KERN_ERR
, conn
,
1312 "received invalid itt index %u (max cmds "
1313 "%u.\n", i
, session
->cmds_max
);
1314 return ISCSI_ERR_BAD_ITT
;
1318 EXPORT_SYMBOL_GPL(iscsi_verify_itt
);
1321 * iscsi_itt_to_ctask - look up ctask by itt
1322 * @conn: iscsi connection
1325 * This should be used for cmd tasks.
1327 * The session lock must be held.
1329 struct iscsi_task
*iscsi_itt_to_ctask(struct iscsi_conn
*conn
, itt_t itt
)
1331 struct iscsi_task
*task
;
1333 if (iscsi_verify_itt(conn
, itt
))
1336 task
= iscsi_itt_to_task(conn
, itt
);
1337 if (!task
|| !task
->sc
)
1340 if (task
->sc
->SCp
.phase
!= conn
->session
->age
) {
1341 iscsi_session_printk(KERN_ERR
, conn
->session
,
1342 "task's session age %d, expected %d\n",
1343 task
->sc
->SCp
.phase
, conn
->session
->age
);
1349 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask
);
1351 void iscsi_session_failure(struct iscsi_session
*session
,
1354 struct iscsi_conn
*conn
;
1357 spin_lock_bh(&session
->lock
);
1358 conn
= session
->leadconn
;
1359 if (session
->state
== ISCSI_STATE_TERMINATE
|| !conn
) {
1360 spin_unlock_bh(&session
->lock
);
1364 dev
= get_device(&conn
->cls_conn
->dev
);
1365 spin_unlock_bh(&session
->lock
);
1369 * if the host is being removed bypass the connection
1370 * recovery initialization because we are going to kill
1373 if (err
== ISCSI_ERR_INVALID_HOST
)
1374 iscsi_conn_error_event(conn
->cls_conn
, err
);
1376 iscsi_conn_failure(conn
, err
);
1379 EXPORT_SYMBOL_GPL(iscsi_session_failure
);
1381 void iscsi_conn_failure(struct iscsi_conn
*conn
, enum iscsi_err err
)
1383 struct iscsi_session
*session
= conn
->session
;
1385 spin_lock_bh(&session
->lock
);
1386 if (session
->state
== ISCSI_STATE_FAILED
) {
1387 spin_unlock_bh(&session
->lock
);
1391 if (conn
->stop_stage
== 0)
1392 session
->state
= ISCSI_STATE_FAILED
;
1393 spin_unlock_bh(&session
->lock
);
1395 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1396 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
1397 iscsi_conn_error_event(conn
->cls_conn
, err
);
1399 EXPORT_SYMBOL_GPL(iscsi_conn_failure
);
1401 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn
*conn
)
1403 struct iscsi_session
*session
= conn
->session
;
1406 * Check for iSCSI window and take care of CmdSN wrap-around
1408 if (!iscsi_sna_lte(session
->queued_cmdsn
, session
->max_cmdsn
)) {
1409 ISCSI_DBG_SESSION(session
, "iSCSI CmdSN closed. ExpCmdSn "
1410 "%u MaxCmdSN %u CmdSN %u/%u\n",
1411 session
->exp_cmdsn
, session
->max_cmdsn
,
1412 session
->cmdsn
, session
->queued_cmdsn
);
1418 static int iscsi_xmit_task(struct iscsi_conn
*conn
)
1420 struct iscsi_task
*task
= conn
->task
;
1423 if (test_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
))
1426 __iscsi_get_task(task
);
1427 spin_unlock_bh(&conn
->session
->lock
);
1428 rc
= conn
->session
->tt
->xmit_task(task
);
1429 spin_lock_bh(&conn
->session
->lock
);
1431 /* done with this task */
1432 task
->last_xfer
= jiffies
;
1435 __iscsi_put_task(task
);
1440 * iscsi_requeue_task - requeue task to run from session workqueue
1441 * @task: task to requeue
1443 * LLDs that need to run a task from the session workqueue should call
1444 * this. The session lock must be held. This should only be called
1445 * by software drivers.
1447 void iscsi_requeue_task(struct iscsi_task
*task
)
1449 struct iscsi_conn
*conn
= task
->conn
;
1452 * this may be on the requeue list already if the xmit_task callout
1453 * is handling the r2ts while we are adding new ones
1455 if (list_empty(&task
->running
))
1456 list_add_tail(&task
->running
, &conn
->requeue
);
1457 iscsi_conn_queue_work(conn
);
1459 EXPORT_SYMBOL_GPL(iscsi_requeue_task
);
1462 * iscsi_data_xmit - xmit any command into the scheduled connection
1463 * @conn: iscsi connection
1466 * The function can return -EAGAIN in which case the caller must
1467 * re-schedule it again later or recover. '0' return code means
1470 static int iscsi_data_xmit(struct iscsi_conn
*conn
)
1472 struct iscsi_task
*task
;
1475 spin_lock_bh(&conn
->session
->lock
);
1476 if (test_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
)) {
1477 ISCSI_DBG_SESSION(conn
->session
, "Tx suspended!\n");
1478 spin_unlock_bh(&conn
->session
->lock
);
1483 rc
= iscsi_xmit_task(conn
);
1489 * process mgmt pdus like nops before commands since we should
1490 * only have one nop-out as a ping from us and targets should not
1491 * overflow us with nop-ins
1494 while (!list_empty(&conn
->mgmtqueue
)) {
1495 conn
->task
= list_entry(conn
->mgmtqueue
.next
,
1496 struct iscsi_task
, running
);
1497 list_del_init(&conn
->task
->running
);
1498 if (iscsi_prep_mgmt_task(conn
, conn
->task
)) {
1499 __iscsi_put_task(conn
->task
);
1503 rc
= iscsi_xmit_task(conn
);
1508 /* process pending command queue */
1509 while (!list_empty(&conn
->cmdqueue
)) {
1510 conn
->task
= list_entry(conn
->cmdqueue
.next
, struct iscsi_task
,
1512 list_del_init(&conn
->task
->running
);
1513 if (conn
->session
->state
== ISCSI_STATE_LOGGING_OUT
) {
1514 fail_scsi_task(conn
->task
, DID_IMM_RETRY
);
1517 rc
= iscsi_prep_scsi_cmd_pdu(conn
->task
);
1519 if (rc
== -ENOMEM
|| rc
== -EACCES
) {
1520 list_add_tail(&conn
->task
->running
,
1525 fail_scsi_task(conn
->task
, DID_ABORT
);
1528 rc
= iscsi_xmit_task(conn
);
1532 * we could continuously get new task requests so
1533 * we need to check the mgmt queue for nops that need to
1534 * be sent to aviod starvation
1536 if (!list_empty(&conn
->mgmtqueue
))
1540 while (!list_empty(&conn
->requeue
)) {
1542 * we always do fastlogout - conn stop code will clean up.
1544 if (conn
->session
->state
== ISCSI_STATE_LOGGING_OUT
)
1547 task
= list_entry(conn
->requeue
.next
, struct iscsi_task
,
1549 if (iscsi_check_tmf_restrictions(task
, ISCSI_OP_SCSI_DATA_OUT
))
1553 list_del_init(&conn
->task
->running
);
1554 conn
->task
->state
= ISCSI_TASK_RUNNING
;
1555 rc
= iscsi_xmit_task(conn
);
1558 if (!list_empty(&conn
->mgmtqueue
))
1561 spin_unlock_bh(&conn
->session
->lock
);
1565 spin_unlock_bh(&conn
->session
->lock
);
1569 static void iscsi_xmitworker(struct work_struct
*work
)
1571 struct iscsi_conn
*conn
=
1572 container_of(work
, struct iscsi_conn
, xmitwork
);
1575 * serialize Xmit worker on a per-connection basis.
1578 rc
= iscsi_data_xmit(conn
);
1579 } while (rc
>= 0 || rc
== -EAGAIN
);
1582 static inline struct iscsi_task
*iscsi_alloc_task(struct iscsi_conn
*conn
,
1583 struct scsi_cmnd
*sc
)
1585 struct iscsi_task
*task
;
1587 if (!kfifo_out(&conn
->session
->cmdpool
.queue
,
1588 (void *) &task
, sizeof(void *)))
1591 sc
->SCp
.phase
= conn
->session
->age
;
1592 sc
->SCp
.ptr
= (char *) task
;
1594 atomic_set(&task
->refcount
, 1);
1595 task
->state
= ISCSI_TASK_PENDING
;
1598 task
->have_checked_conn
= false;
1599 task
->last_timeout
= jiffies
;
1600 task
->last_xfer
= jiffies
;
1601 task
->protected = false;
1602 INIT_LIST_HEAD(&task
->running
);
1607 FAILURE_BAD_HOST
= 1,
1608 FAILURE_SESSION_FAILED
,
1609 FAILURE_SESSION_FREED
,
1610 FAILURE_WINDOW_CLOSED
,
1612 FAILURE_SESSION_TERMINATE
,
1613 FAILURE_SESSION_IN_RECOVERY
,
1614 FAILURE_SESSION_RECOVERY_TIMEOUT
,
1615 FAILURE_SESSION_LOGGING_OUT
,
1616 FAILURE_SESSION_NOT_READY
,
1619 int iscsi_queuecommand(struct Scsi_Host
*host
, struct scsi_cmnd
*sc
)
1621 struct iscsi_cls_session
*cls_session
;
1622 struct iscsi_host
*ihost
;
1624 struct iscsi_session
*session
;
1625 struct iscsi_conn
*conn
;
1626 struct iscsi_task
*task
= NULL
;
1631 ihost
= shost_priv(host
);
1633 cls_session
= starget_to_session(scsi_target(sc
->device
));
1634 session
= cls_session
->dd_data
;
1635 spin_lock_bh(&session
->lock
);
1637 reason
= iscsi_session_chkready(cls_session
);
1639 sc
->result
= reason
;
1643 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
1645 * to handle the race between when we set the recovery state
1646 * and block the session we requeue here (commands could
1647 * be entering our queuecommand while a block is starting
1648 * up because the block code is not locked)
1650 switch (session
->state
) {
1651 case ISCSI_STATE_FAILED
:
1652 case ISCSI_STATE_IN_RECOVERY
:
1653 reason
= FAILURE_SESSION_IN_RECOVERY
;
1654 sc
->result
= DID_IMM_RETRY
<< 16;
1656 case ISCSI_STATE_LOGGING_OUT
:
1657 reason
= FAILURE_SESSION_LOGGING_OUT
;
1658 sc
->result
= DID_IMM_RETRY
<< 16;
1660 case ISCSI_STATE_RECOVERY_FAILED
:
1661 reason
= FAILURE_SESSION_RECOVERY_TIMEOUT
;
1662 sc
->result
= DID_TRANSPORT_FAILFAST
<< 16;
1664 case ISCSI_STATE_TERMINATE
:
1665 reason
= FAILURE_SESSION_TERMINATE
;
1666 sc
->result
= DID_NO_CONNECT
<< 16;
1669 reason
= FAILURE_SESSION_FREED
;
1670 sc
->result
= DID_NO_CONNECT
<< 16;
1675 conn
= session
->leadconn
;
1677 reason
= FAILURE_SESSION_FREED
;
1678 sc
->result
= DID_NO_CONNECT
<< 16;
1682 if (test_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
)) {
1683 reason
= FAILURE_SESSION_IN_RECOVERY
;
1684 sc
->result
= DID_REQUEUE
;
1688 if (iscsi_check_cmdsn_window_closed(conn
)) {
1689 reason
= FAILURE_WINDOW_CLOSED
;
1693 task
= iscsi_alloc_task(conn
, sc
);
1695 reason
= FAILURE_OOM
;
1699 if (!ihost
->workq
) {
1700 reason
= iscsi_prep_scsi_cmd_pdu(task
);
1702 if (reason
== -ENOMEM
|| reason
== -EACCES
) {
1703 reason
= FAILURE_OOM
;
1706 sc
->result
= DID_ABORT
<< 16;
1710 if (session
->tt
->xmit_task(task
)) {
1712 reason
= FAILURE_SESSION_NOT_READY
;
1716 list_add_tail(&task
->running
, &conn
->cmdqueue
);
1717 iscsi_conn_queue_work(conn
);
1720 session
->queued_cmdsn
++;
1721 spin_unlock_bh(&session
->lock
);
1725 iscsi_complete_task(task
, ISCSI_TASK_REQUEUE_SCSIQ
);
1727 spin_unlock_bh(&session
->lock
);
1728 ISCSI_DBG_SESSION(session
, "cmd 0x%x rejected (%d)\n",
1729 sc
->cmnd
[0], reason
);
1730 return SCSI_MLQUEUE_TARGET_BUSY
;
1733 iscsi_complete_task(task
, ISCSI_TASK_REQUEUE_SCSIQ
);
1735 spin_unlock_bh(&session
->lock
);
1736 ISCSI_DBG_SESSION(session
, "iscsi: cmd 0x%x is not queued (%d)\n",
1737 sc
->cmnd
[0], reason
);
1738 if (!scsi_bidi_cmnd(sc
))
1739 scsi_set_resid(sc
, scsi_bufflen(sc
));
1741 scsi_out(sc
)->resid
= scsi_out(sc
)->length
;
1742 scsi_in(sc
)->resid
= scsi_in(sc
)->length
;
1747 EXPORT_SYMBOL_GPL(iscsi_queuecommand
);
1749 int iscsi_change_queue_depth(struct scsi_device
*sdev
, int depth
, int reason
)
1752 case SCSI_QDEPTH_DEFAULT
:
1753 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), depth
);
1755 case SCSI_QDEPTH_QFULL
:
1756 scsi_track_queue_full(sdev
, depth
);
1758 case SCSI_QDEPTH_RAMP_UP
:
1759 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), depth
);
1764 return sdev
->queue_depth
;
1766 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth
);
1768 int iscsi_target_alloc(struct scsi_target
*starget
)
1770 struct iscsi_cls_session
*cls_session
= starget_to_session(starget
);
1771 struct iscsi_session
*session
= cls_session
->dd_data
;
1773 starget
->can_queue
= session
->scsi_cmds_max
;
1776 EXPORT_SYMBOL_GPL(iscsi_target_alloc
);
1778 static void iscsi_tmf_timedout(unsigned long data
)
1780 struct iscsi_conn
*conn
= (struct iscsi_conn
*)data
;
1781 struct iscsi_session
*session
= conn
->session
;
1783 spin_lock(&session
->lock
);
1784 if (conn
->tmf_state
== TMF_QUEUED
) {
1785 conn
->tmf_state
= TMF_TIMEDOUT
;
1786 ISCSI_DBG_EH(session
, "tmf timedout\n");
1787 /* unblock eh_abort() */
1788 wake_up(&conn
->ehwait
);
1790 spin_unlock(&session
->lock
);
1793 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn
*conn
,
1794 struct iscsi_tm
*hdr
, int age
,
1797 struct iscsi_session
*session
= conn
->session
;
1798 struct iscsi_task
*task
;
1800 task
= __iscsi_conn_send_pdu(conn
, (struct iscsi_hdr
*)hdr
,
1803 spin_unlock_bh(&session
->lock
);
1804 iscsi_conn_printk(KERN_ERR
, conn
, "Could not send TMF.\n");
1805 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1806 spin_lock_bh(&session
->lock
);
1809 conn
->tmfcmd_pdus_cnt
++;
1810 conn
->tmf_timer
.expires
= timeout
* HZ
+ jiffies
;
1811 conn
->tmf_timer
.function
= iscsi_tmf_timedout
;
1812 conn
->tmf_timer
.data
= (unsigned long)conn
;
1813 add_timer(&conn
->tmf_timer
);
1814 ISCSI_DBG_EH(session
, "tmf set timeout\n");
1816 spin_unlock_bh(&session
->lock
);
1817 mutex_unlock(&session
->eh_mutex
);
1820 * block eh thread until:
1824 * 3) session is terminated or restarted or userspace has
1825 * given up on recovery
1827 wait_event_interruptible(conn
->ehwait
, age
!= session
->age
||
1828 session
->state
!= ISCSI_STATE_LOGGED_IN
||
1829 conn
->tmf_state
!= TMF_QUEUED
);
1830 if (signal_pending(current
))
1831 flush_signals(current
);
1832 del_timer_sync(&conn
->tmf_timer
);
1834 mutex_lock(&session
->eh_mutex
);
1835 spin_lock_bh(&session
->lock
);
1836 /* if the session drops it will clean up the task */
1837 if (age
!= session
->age
||
1838 session
->state
!= ISCSI_STATE_LOGGED_IN
)
1844 * Fail commands. session lock held and recv side suspended and xmit
1847 static void fail_scsi_tasks(struct iscsi_conn
*conn
, unsigned lun
,
1850 struct iscsi_task
*task
;
1853 for (i
= 0; i
< conn
->session
->cmds_max
; i
++) {
1854 task
= conn
->session
->cmds
[i
];
1855 if (!task
->sc
|| task
->state
== ISCSI_TASK_FREE
)
1858 if (lun
!= -1 && lun
!= task
->sc
->device
->lun
)
1861 ISCSI_DBG_SESSION(conn
->session
,
1862 "failing sc %p itt 0x%x state %d\n",
1863 task
->sc
, task
->itt
, task
->state
);
1864 fail_scsi_task(task
, error
);
1869 * iscsi_suspend_queue - suspend iscsi_queuecommand
1870 * @conn: iscsi conn to stop queueing IO on
1872 * This grabs the session lock to make sure no one is in
1873 * xmit_task/queuecommand, and then sets suspend to prevent
1874 * new commands from being queued. This only needs to be called
1875 * by offload drivers that need to sync a path like ep disconnect
1876 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1877 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1879 void iscsi_suspend_queue(struct iscsi_conn
*conn
)
1881 spin_lock_bh(&conn
->session
->lock
);
1882 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1883 spin_unlock_bh(&conn
->session
->lock
);
1885 EXPORT_SYMBOL_GPL(iscsi_suspend_queue
);
1888 * iscsi_suspend_tx - suspend iscsi_data_xmit
1889 * @conn: iscsi conn tp stop processing IO on.
1891 * This function sets the suspend bit to prevent iscsi_data_xmit
1892 * from sending new IO, and if work is queued on the xmit thread
1893 * it will wait for it to be completed.
1895 void iscsi_suspend_tx(struct iscsi_conn
*conn
)
1897 struct Scsi_Host
*shost
= conn
->session
->host
;
1898 struct iscsi_host
*ihost
= shost_priv(shost
);
1900 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1902 flush_workqueue(ihost
->workq
);
1904 EXPORT_SYMBOL_GPL(iscsi_suspend_tx
);
1906 static void iscsi_start_tx(struct iscsi_conn
*conn
)
1908 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1909 iscsi_conn_queue_work(conn
);
1913 * We want to make sure a ping is in flight. It has timed out.
1914 * And we are not busy processing a pdu that is making
1915 * progress but got started before the ping and is taking a while
1916 * to complete so the ping is just stuck behind it in a queue.
1918 static int iscsi_has_ping_timed_out(struct iscsi_conn
*conn
)
1920 if (conn
->ping_task
&&
1921 time_before_eq(conn
->last_recv
+ (conn
->recv_timeout
* HZ
) +
1922 (conn
->ping_timeout
* HZ
), jiffies
))
1928 static enum blk_eh_timer_return
iscsi_eh_cmd_timed_out(struct scsi_cmnd
*sc
)
1930 enum blk_eh_timer_return rc
= BLK_EH_NOT_HANDLED
;
1931 struct iscsi_task
*task
= NULL
, *running_task
;
1932 struct iscsi_cls_session
*cls_session
;
1933 struct iscsi_session
*session
;
1934 struct iscsi_conn
*conn
;
1937 cls_session
= starget_to_session(scsi_target(sc
->device
));
1938 session
= cls_session
->dd_data
;
1940 ISCSI_DBG_EH(session
, "scsi cmd %p timedout\n", sc
);
1942 spin_lock(&session
->lock
);
1943 task
= (struct iscsi_task
*)sc
->SCp
.ptr
;
1946 * Raced with completion. Blk layer has taken ownership
1947 * so let timeout code complete it now.
1949 rc
= BLK_EH_HANDLED
;
1953 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
1955 * We are probably in the middle of iscsi recovery so let
1956 * that complete and handle the error.
1958 rc
= BLK_EH_RESET_TIMER
;
1962 conn
= session
->leadconn
;
1964 /* In the middle of shuting down */
1965 rc
= BLK_EH_RESET_TIMER
;
1970 * If we have sent (at least queued to the network layer) a pdu or
1971 * recvd one for the task since the last timeout ask for
1972 * more time. If on the next timeout we have not made progress
1973 * we can check if it is the task or connection when we send the
1976 if (time_after(task
->last_xfer
, task
->last_timeout
)) {
1977 ISCSI_DBG_EH(session
, "Command making progress. Asking "
1978 "scsi-ml for more time to complete. "
1979 "Last data xfer at %lu. Last timeout was at "
1980 "%lu\n.", task
->last_xfer
, task
->last_timeout
);
1981 task
->have_checked_conn
= false;
1982 rc
= BLK_EH_RESET_TIMER
;
1986 if (!conn
->recv_timeout
&& !conn
->ping_timeout
)
1989 * if the ping timedout then we are in the middle of cleaning up
1990 * and can let the iscsi eh handle it
1992 if (iscsi_has_ping_timed_out(conn
)) {
1993 rc
= BLK_EH_RESET_TIMER
;
1997 for (i
= 0; i
< conn
->session
->cmds_max
; i
++) {
1998 running_task
= conn
->session
->cmds
[i
];
1999 if (!running_task
->sc
|| running_task
== task
||
2000 running_task
->state
!= ISCSI_TASK_RUNNING
)
2004 * Only check if cmds started before this one have made
2005 * progress, or this could never fail
2007 if (time_after(running_task
->sc
->jiffies_at_alloc
,
2008 task
->sc
->jiffies_at_alloc
))
2011 if (time_after(running_task
->last_xfer
, task
->last_timeout
)) {
2013 * This task has not made progress, but a task
2014 * started before us has transferred data since
2015 * we started/last-checked. We could be queueing
2016 * too many tasks or the LU is bad.
2018 * If the device is bad the cmds ahead of us on
2019 * other devs will complete, and this loop will
2020 * eventually fail starting the scsi eh.
2022 ISCSI_DBG_EH(session
, "Command has not made progress "
2023 "but commands ahead of it have. "
2024 "Asking scsi-ml for more time to "
2025 "complete. Our last xfer vs running task "
2026 "last xfer %lu/%lu. Last check %lu.\n",
2027 task
->last_xfer
, running_task
->last_xfer
,
2028 task
->last_timeout
);
2029 rc
= BLK_EH_RESET_TIMER
;
2034 /* Assumes nop timeout is shorter than scsi cmd timeout */
2035 if (task
->have_checked_conn
)
2039 * Checking the transport already or nop from a cmd timeout still
2042 if (conn
->ping_task
) {
2043 task
->have_checked_conn
= true;
2044 rc
= BLK_EH_RESET_TIMER
;
2048 /* Make sure there is a transport check done */
2049 iscsi_send_nopout(conn
, NULL
);
2050 task
->have_checked_conn
= true;
2051 rc
= BLK_EH_RESET_TIMER
;
2055 task
->last_timeout
= jiffies
;
2056 spin_unlock(&session
->lock
);
2057 ISCSI_DBG_EH(session
, "return %s\n", rc
== BLK_EH_RESET_TIMER
?
2058 "timer reset" : "nh");
2062 static void iscsi_check_transport_timeouts(unsigned long data
)
2064 struct iscsi_conn
*conn
= (struct iscsi_conn
*)data
;
2065 struct iscsi_session
*session
= conn
->session
;
2066 unsigned long recv_timeout
, next_timeout
= 0, last_recv
;
2068 spin_lock(&session
->lock
);
2069 if (session
->state
!= ISCSI_STATE_LOGGED_IN
)
2072 recv_timeout
= conn
->recv_timeout
;
2077 last_recv
= conn
->last_recv
;
2079 if (iscsi_has_ping_timed_out(conn
)) {
2080 iscsi_conn_printk(KERN_ERR
, conn
, "ping timeout of %d secs "
2081 "expired, recv timeout %d, last rx %lu, "
2082 "last ping %lu, now %lu\n",
2083 conn
->ping_timeout
, conn
->recv_timeout
,
2084 last_recv
, conn
->last_ping
, jiffies
);
2085 spin_unlock(&session
->lock
);
2086 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
2090 if (time_before_eq(last_recv
+ recv_timeout
, jiffies
)) {
2091 /* send a ping to try to provoke some traffic */
2092 ISCSI_DBG_CONN(conn
, "Sending nopout as ping\n");
2093 iscsi_send_nopout(conn
, NULL
);
2094 next_timeout
= conn
->last_ping
+ (conn
->ping_timeout
* HZ
);
2096 next_timeout
= last_recv
+ recv_timeout
;
2098 ISCSI_DBG_CONN(conn
, "Setting next tmo %lu\n", next_timeout
);
2099 mod_timer(&conn
->transport_timer
, next_timeout
);
2101 spin_unlock(&session
->lock
);
2104 static void iscsi_prep_abort_task_pdu(struct iscsi_task
*task
,
2105 struct iscsi_tm
*hdr
)
2107 memset(hdr
, 0, sizeof(*hdr
));
2108 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
2109 hdr
->flags
= ISCSI_TM_FUNC_ABORT_TASK
& ISCSI_FLAG_TM_FUNC_MASK
;
2110 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2111 hdr
->lun
= task
->lun
;
2112 hdr
->rtt
= task
->hdr_itt
;
2113 hdr
->refcmdsn
= task
->cmdsn
;
2116 int iscsi_eh_abort(struct scsi_cmnd
*sc
)
2118 struct iscsi_cls_session
*cls_session
;
2119 struct iscsi_session
*session
;
2120 struct iscsi_conn
*conn
;
2121 struct iscsi_task
*task
;
2122 struct iscsi_tm
*hdr
;
2125 cls_session
= starget_to_session(scsi_target(sc
->device
));
2126 session
= cls_session
->dd_data
;
2128 ISCSI_DBG_EH(session
, "aborting sc %p\n", sc
);
2130 mutex_lock(&session
->eh_mutex
);
2131 spin_lock_bh(&session
->lock
);
2133 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2137 ISCSI_DBG_EH(session
, "sc never reached iscsi layer or "
2139 spin_unlock_bh(&session
->lock
);
2140 mutex_unlock(&session
->eh_mutex
);
2145 * If we are not logged in or we have started a new session
2146 * then let the host reset code handle this
2148 if (!session
->leadconn
|| session
->state
!= ISCSI_STATE_LOGGED_IN
||
2149 sc
->SCp
.phase
!= session
->age
) {
2150 spin_unlock_bh(&session
->lock
);
2151 mutex_unlock(&session
->eh_mutex
);
2152 ISCSI_DBG_EH(session
, "failing abort due to dropped "
2157 conn
= session
->leadconn
;
2158 conn
->eh_abort_cnt
++;
2161 task
= (struct iscsi_task
*)sc
->SCp
.ptr
;
2162 ISCSI_DBG_EH(session
, "aborting [sc %p itt 0x%x]\n",
2165 /* task completed before time out */
2167 ISCSI_DBG_EH(session
, "sc completed while abort in progress\n");
2171 if (task
->state
== ISCSI_TASK_PENDING
) {
2172 fail_scsi_task(task
, DID_ABORT
);
2176 /* only have one tmf outstanding at a time */
2177 if (conn
->tmf_state
!= TMF_INITIAL
)
2179 conn
->tmf_state
= TMF_QUEUED
;
2182 iscsi_prep_abort_task_pdu(task
, hdr
);
2184 if (iscsi_exec_task_mgmt_fn(conn
, hdr
, age
, session
->abort_timeout
)) {
2189 switch (conn
->tmf_state
) {
2191 spin_unlock_bh(&session
->lock
);
2193 * stop tx side incase the target had sent a abort rsp but
2194 * the initiator was still writing out data.
2196 iscsi_suspend_tx(conn
);
2198 * we do not stop the recv side because targets have been
2199 * good and have never sent us a successful tmf response
2200 * then sent more data for the cmd.
2202 spin_lock_bh(&session
->lock
);
2203 fail_scsi_task(task
, DID_ABORT
);
2204 conn
->tmf_state
= TMF_INITIAL
;
2205 memset(hdr
, 0, sizeof(*hdr
));
2206 spin_unlock_bh(&session
->lock
);
2207 iscsi_start_tx(conn
);
2208 goto success_unlocked
;
2210 spin_unlock_bh(&session
->lock
);
2211 iscsi_conn_failure(conn
, ISCSI_ERR_SCSI_EH_SESSION_RST
);
2212 goto failed_unlocked
;
2215 conn
->tmf_state
= TMF_INITIAL
;
2216 memset(hdr
, 0, sizeof(*hdr
));
2217 /* task completed before tmf abort response */
2218 ISCSI_DBG_EH(session
, "sc completed while abort in "
2224 conn
->tmf_state
= TMF_INITIAL
;
2229 spin_unlock_bh(&session
->lock
);
2231 ISCSI_DBG_EH(session
, "abort success [sc %p itt 0x%x]\n",
2233 mutex_unlock(&session
->eh_mutex
);
2237 spin_unlock_bh(&session
->lock
);
2239 ISCSI_DBG_EH(session
, "abort failed [sc %p itt 0x%x]\n", sc
,
2240 task
? task
->itt
: 0);
2241 mutex_unlock(&session
->eh_mutex
);
2244 EXPORT_SYMBOL_GPL(iscsi_eh_abort
);
2246 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd
*sc
, struct iscsi_tm
*hdr
)
2248 memset(hdr
, 0, sizeof(*hdr
));
2249 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
2250 hdr
->flags
= ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
& ISCSI_FLAG_TM_FUNC_MASK
;
2251 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2252 int_to_scsilun(sc
->device
->lun
, &hdr
->lun
);
2253 hdr
->rtt
= RESERVED_ITT
;
2256 int iscsi_eh_device_reset(struct scsi_cmnd
*sc
)
2258 struct iscsi_cls_session
*cls_session
;
2259 struct iscsi_session
*session
;
2260 struct iscsi_conn
*conn
;
2261 struct iscsi_tm
*hdr
;
2264 cls_session
= starget_to_session(scsi_target(sc
->device
));
2265 session
= cls_session
->dd_data
;
2267 ISCSI_DBG_EH(session
, "LU Reset [sc %p lun %u]\n", sc
, sc
->device
->lun
);
2269 mutex_lock(&session
->eh_mutex
);
2270 spin_lock_bh(&session
->lock
);
2272 * Just check if we are not logged in. We cannot check for
2273 * the phase because the reset could come from a ioctl.
2275 if (!session
->leadconn
|| session
->state
!= ISCSI_STATE_LOGGED_IN
)
2277 conn
= session
->leadconn
;
2279 /* only have one tmf outstanding at a time */
2280 if (conn
->tmf_state
!= TMF_INITIAL
)
2282 conn
->tmf_state
= TMF_QUEUED
;
2285 iscsi_prep_lun_reset_pdu(sc
, hdr
);
2287 if (iscsi_exec_task_mgmt_fn(conn
, hdr
, session
->age
,
2288 session
->lu_reset_timeout
)) {
2293 switch (conn
->tmf_state
) {
2297 spin_unlock_bh(&session
->lock
);
2298 iscsi_conn_failure(conn
, ISCSI_ERR_SCSI_EH_SESSION_RST
);
2301 conn
->tmf_state
= TMF_INITIAL
;
2306 spin_unlock_bh(&session
->lock
);
2308 iscsi_suspend_tx(conn
);
2310 spin_lock_bh(&session
->lock
);
2311 memset(hdr
, 0, sizeof(*hdr
));
2312 fail_scsi_tasks(conn
, sc
->device
->lun
, DID_ERROR
);
2313 conn
->tmf_state
= TMF_INITIAL
;
2314 spin_unlock_bh(&session
->lock
);
2316 iscsi_start_tx(conn
);
2320 spin_unlock_bh(&session
->lock
);
2322 ISCSI_DBG_EH(session
, "dev reset result = %s\n",
2323 rc
== SUCCESS
? "SUCCESS" : "FAILED");
2324 mutex_unlock(&session
->eh_mutex
);
2327 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset
);
2329 void iscsi_session_recovery_timedout(struct iscsi_cls_session
*cls_session
)
2331 struct iscsi_session
*session
= cls_session
->dd_data
;
2333 spin_lock_bh(&session
->lock
);
2334 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
2335 session
->state
= ISCSI_STATE_RECOVERY_FAILED
;
2336 if (session
->leadconn
)
2337 wake_up(&session
->leadconn
->ehwait
);
2339 spin_unlock_bh(&session
->lock
);
2341 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout
);
2344 * iscsi_eh_session_reset - drop session and attempt relogin
2347 * This function will wait for a relogin, session termination from
2348 * userspace, or a recovery/replacement timeout.
2350 int iscsi_eh_session_reset(struct scsi_cmnd
*sc
)
2352 struct iscsi_cls_session
*cls_session
;
2353 struct iscsi_session
*session
;
2354 struct iscsi_conn
*conn
;
2356 cls_session
= starget_to_session(scsi_target(sc
->device
));
2357 session
= cls_session
->dd_data
;
2358 conn
= session
->leadconn
;
2360 mutex_lock(&session
->eh_mutex
);
2361 spin_lock_bh(&session
->lock
);
2362 if (session
->state
== ISCSI_STATE_TERMINATE
) {
2364 ISCSI_DBG_EH(session
,
2365 "failing session reset: Could not log back into "
2366 "%s, %s [age %d]\n", session
->targetname
,
2367 conn
->persistent_address
, session
->age
);
2368 spin_unlock_bh(&session
->lock
);
2369 mutex_unlock(&session
->eh_mutex
);
2373 spin_unlock_bh(&session
->lock
);
2374 mutex_unlock(&session
->eh_mutex
);
2376 * we drop the lock here but the leadconn cannot be destoyed while
2377 * we are in the scsi eh
2379 iscsi_conn_failure(conn
, ISCSI_ERR_SCSI_EH_SESSION_RST
);
2381 ISCSI_DBG_EH(session
, "wait for relogin\n");
2382 wait_event_interruptible(conn
->ehwait
,
2383 session
->state
== ISCSI_STATE_TERMINATE
||
2384 session
->state
== ISCSI_STATE_LOGGED_IN
||
2385 session
->state
== ISCSI_STATE_RECOVERY_FAILED
);
2386 if (signal_pending(current
))
2387 flush_signals(current
);
2389 mutex_lock(&session
->eh_mutex
);
2390 spin_lock_bh(&session
->lock
);
2391 if (session
->state
== ISCSI_STATE_LOGGED_IN
) {
2392 ISCSI_DBG_EH(session
,
2393 "session reset succeeded for %s,%s\n",
2394 session
->targetname
, conn
->persistent_address
);
2397 spin_unlock_bh(&session
->lock
);
2398 mutex_unlock(&session
->eh_mutex
);
2401 EXPORT_SYMBOL_GPL(iscsi_eh_session_reset
);
2403 static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd
*sc
, struct iscsi_tm
*hdr
)
2405 memset(hdr
, 0, sizeof(*hdr
));
2406 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
2407 hdr
->flags
= ISCSI_TM_FUNC_TARGET_WARM_RESET
& ISCSI_FLAG_TM_FUNC_MASK
;
2408 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2409 hdr
->rtt
= RESERVED_ITT
;
2413 * iscsi_eh_target_reset - reset target
2416 * This will attempt to send a warm target reset.
2418 int iscsi_eh_target_reset(struct scsi_cmnd
*sc
)
2420 struct iscsi_cls_session
*cls_session
;
2421 struct iscsi_session
*session
;
2422 struct iscsi_conn
*conn
;
2423 struct iscsi_tm
*hdr
;
2426 cls_session
= starget_to_session(scsi_target(sc
->device
));
2427 session
= cls_session
->dd_data
;
2429 ISCSI_DBG_EH(session
, "tgt Reset [sc %p tgt %s]\n", sc
,
2430 session
->targetname
);
2432 mutex_lock(&session
->eh_mutex
);
2433 spin_lock_bh(&session
->lock
);
2435 * Just check if we are not logged in. We cannot check for
2436 * the phase because the reset could come from a ioctl.
2438 if (!session
->leadconn
|| session
->state
!= ISCSI_STATE_LOGGED_IN
)
2440 conn
= session
->leadconn
;
2442 /* only have one tmf outstanding at a time */
2443 if (conn
->tmf_state
!= TMF_INITIAL
)
2445 conn
->tmf_state
= TMF_QUEUED
;
2448 iscsi_prep_tgt_reset_pdu(sc
, hdr
);
2450 if (iscsi_exec_task_mgmt_fn(conn
, hdr
, session
->age
,
2451 session
->tgt_reset_timeout
)) {
2456 switch (conn
->tmf_state
) {
2460 spin_unlock_bh(&session
->lock
);
2461 iscsi_conn_failure(conn
, ISCSI_ERR_SCSI_EH_SESSION_RST
);
2464 conn
->tmf_state
= TMF_INITIAL
;
2469 spin_unlock_bh(&session
->lock
);
2471 iscsi_suspend_tx(conn
);
2473 spin_lock_bh(&session
->lock
);
2474 memset(hdr
, 0, sizeof(*hdr
));
2475 fail_scsi_tasks(conn
, -1, DID_ERROR
);
2476 conn
->tmf_state
= TMF_INITIAL
;
2477 spin_unlock_bh(&session
->lock
);
2479 iscsi_start_tx(conn
);
2483 spin_unlock_bh(&session
->lock
);
2485 ISCSI_DBG_EH(session
, "tgt %s reset result = %s\n", session
->targetname
,
2486 rc
== SUCCESS
? "SUCCESS" : "FAILED");
2487 mutex_unlock(&session
->eh_mutex
);
2490 EXPORT_SYMBOL_GPL(iscsi_eh_target_reset
);
2493 * iscsi_eh_recover_target - reset target and possibly the session
2496 * This will attempt to send a warm target reset. If that fails,
2497 * we will escalate to ERL0 session recovery.
2499 int iscsi_eh_recover_target(struct scsi_cmnd
*sc
)
2503 rc
= iscsi_eh_target_reset(sc
);
2505 rc
= iscsi_eh_session_reset(sc
);
2508 EXPORT_SYMBOL_GPL(iscsi_eh_recover_target
);
2511 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2512 * should be accessed via kfifo_{get,put} on q->queue.
2513 * Optionally, the caller can obtain the array of object pointers
2514 * by passing in a non-NULL @items pointer
2517 iscsi_pool_init(struct iscsi_pool
*q
, int max
, void ***items
, int item_size
)
2519 int i
, num_arrays
= 1;
2521 memset(q
, 0, sizeof(*q
));
2525 /* If the user passed an items pointer, he wants a copy of
2529 q
->pool
= kzalloc(num_arrays
* max
* sizeof(void*), GFP_KERNEL
);
2530 if (q
->pool
== NULL
)
2533 kfifo_init(&q
->queue
, (void*)q
->pool
, max
* sizeof(void*));
2535 for (i
= 0; i
< max
; i
++) {
2536 q
->pool
[i
] = kzalloc(item_size
, GFP_KERNEL
);
2537 if (q
->pool
[i
] == NULL
) {
2541 kfifo_in(&q
->queue
, (void*)&q
->pool
[i
], sizeof(void*));
2545 *items
= q
->pool
+ max
;
2546 memcpy(*items
, q
->pool
, max
* sizeof(void *));
2555 EXPORT_SYMBOL_GPL(iscsi_pool_init
);
2557 void iscsi_pool_free(struct iscsi_pool
*q
)
2561 for (i
= 0; i
< q
->max
; i
++)
2565 EXPORT_SYMBOL_GPL(iscsi_pool_free
);
2568 * iscsi_host_add - add host to system
2570 * @pdev: parent device
2572 * This should be called by partial offload and software iscsi drivers
2573 * to add a host to the system.
2575 int iscsi_host_add(struct Scsi_Host
*shost
, struct device
*pdev
)
2577 if (!shost
->can_queue
)
2578 shost
->can_queue
= ISCSI_DEF_XMIT_CMDS_MAX
;
2580 if (!shost
->cmd_per_lun
)
2581 shost
->cmd_per_lun
= ISCSI_DEF_CMD_PER_LUN
;
2583 if (!shost
->transportt
->eh_timed_out
)
2584 shost
->transportt
->eh_timed_out
= iscsi_eh_cmd_timed_out
;
2585 return scsi_add_host(shost
, pdev
);
2587 EXPORT_SYMBOL_GPL(iscsi_host_add
);
2590 * iscsi_host_alloc - allocate a host and driver data
2591 * @sht: scsi host template
2592 * @dd_data_size: driver host data size
2593 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2595 * This should be called by partial offload and software iscsi drivers.
2596 * To access the driver specific memory use the iscsi_host_priv() macro.
2598 struct Scsi_Host
*iscsi_host_alloc(struct scsi_host_template
*sht
,
2599 int dd_data_size
, bool xmit_can_sleep
)
2601 struct Scsi_Host
*shost
;
2602 struct iscsi_host
*ihost
;
2604 shost
= scsi_host_alloc(sht
, sizeof(struct iscsi_host
) + dd_data_size
);
2607 ihost
= shost_priv(shost
);
2609 if (xmit_can_sleep
) {
2610 snprintf(ihost
->workq_name
, sizeof(ihost
->workq_name
),
2611 "iscsi_q_%d", shost
->host_no
);
2612 ihost
->workq
= create_singlethread_workqueue(ihost
->workq_name
);
2617 spin_lock_init(&ihost
->lock
);
2618 ihost
->state
= ISCSI_HOST_SETUP
;
2619 ihost
->num_sessions
= 0;
2620 init_waitqueue_head(&ihost
->session_removal_wq
);
2624 scsi_host_put(shost
);
2627 EXPORT_SYMBOL_GPL(iscsi_host_alloc
);
2629 static void iscsi_notify_host_removed(struct iscsi_cls_session
*cls_session
)
2631 iscsi_session_failure(cls_session
->dd_data
, ISCSI_ERR_INVALID_HOST
);
2635 * iscsi_host_remove - remove host and sessions
2638 * If there are any sessions left, this will initiate the removal and wait
2639 * for the completion.
2641 void iscsi_host_remove(struct Scsi_Host
*shost
)
2643 struct iscsi_host
*ihost
= shost_priv(shost
);
2644 unsigned long flags
;
2646 spin_lock_irqsave(&ihost
->lock
, flags
);
2647 ihost
->state
= ISCSI_HOST_REMOVED
;
2648 spin_unlock_irqrestore(&ihost
->lock
, flags
);
2650 iscsi_host_for_each_session(shost
, iscsi_notify_host_removed
);
2651 wait_event_interruptible(ihost
->session_removal_wq
,
2652 ihost
->num_sessions
== 0);
2653 if (signal_pending(current
))
2654 flush_signals(current
);
2656 scsi_remove_host(shost
);
2658 destroy_workqueue(ihost
->workq
);
2660 EXPORT_SYMBOL_GPL(iscsi_host_remove
);
2662 void iscsi_host_free(struct Scsi_Host
*shost
)
2664 struct iscsi_host
*ihost
= shost_priv(shost
);
2666 kfree(ihost
->netdev
);
2667 kfree(ihost
->hwaddress
);
2668 kfree(ihost
->initiatorname
);
2669 scsi_host_put(shost
);
2671 EXPORT_SYMBOL_GPL(iscsi_host_free
);
2673 static void iscsi_host_dec_session_cnt(struct Scsi_Host
*shost
)
2675 struct iscsi_host
*ihost
= shost_priv(shost
);
2676 unsigned long flags
;
2678 shost
= scsi_host_get(shost
);
2680 printk(KERN_ERR
"Invalid state. Cannot notify host removal "
2681 "of session teardown event because host already "
2686 spin_lock_irqsave(&ihost
->lock
, flags
);
2687 ihost
->num_sessions
--;
2688 if (ihost
->num_sessions
== 0)
2689 wake_up(&ihost
->session_removal_wq
);
2690 spin_unlock_irqrestore(&ihost
->lock
, flags
);
2691 scsi_host_put(shost
);
2695 * iscsi_session_setup - create iscsi cls session and host and session
2696 * @iscsit: iscsi transport template
2698 * @cmds_max: session can queue
2699 * @cmd_task_size: LLD task private data size
2700 * @initial_cmdsn: initial CmdSN
2702 * This can be used by software iscsi_transports that allocate
2703 * a session per scsi host.
2705 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2706 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2707 * for nop handling and login/logout requests.
2709 struct iscsi_cls_session
*
2710 iscsi_session_setup(struct iscsi_transport
*iscsit
, struct Scsi_Host
*shost
,
2711 uint16_t cmds_max
, int dd_size
, int cmd_task_size
,
2712 uint32_t initial_cmdsn
, unsigned int id
)
2714 struct iscsi_host
*ihost
= shost_priv(shost
);
2715 struct iscsi_session
*session
;
2716 struct iscsi_cls_session
*cls_session
;
2717 int cmd_i
, scsi_cmds
, total_cmds
= cmds_max
;
2718 unsigned long flags
;
2720 spin_lock_irqsave(&ihost
->lock
, flags
);
2721 if (ihost
->state
== ISCSI_HOST_REMOVED
) {
2722 spin_unlock_irqrestore(&ihost
->lock
, flags
);
2725 ihost
->num_sessions
++;
2726 spin_unlock_irqrestore(&ihost
->lock
, flags
);
2729 total_cmds
= ISCSI_DEF_XMIT_CMDS_MAX
;
2731 * The iscsi layer needs some tasks for nop handling and tmfs,
2732 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2733 * + 1 command for scsi IO.
2735 if (total_cmds
< ISCSI_TOTAL_CMDS_MIN
) {
2736 printk(KERN_ERR
"iscsi: invalid can_queue of %d. can_queue "
2737 "must be a power of two that is at least %d.\n",
2738 total_cmds
, ISCSI_TOTAL_CMDS_MIN
);
2739 goto dec_session_count
;
2742 if (total_cmds
> ISCSI_TOTAL_CMDS_MAX
) {
2743 printk(KERN_ERR
"iscsi: invalid can_queue of %d. can_queue "
2744 "must be a power of 2 less than or equal to %d.\n",
2745 cmds_max
, ISCSI_TOTAL_CMDS_MAX
);
2746 total_cmds
= ISCSI_TOTAL_CMDS_MAX
;
2749 if (!is_power_of_2(total_cmds
)) {
2750 printk(KERN_ERR
"iscsi: invalid can_queue of %d. can_queue "
2751 "must be a power of 2.\n", total_cmds
);
2752 total_cmds
= rounddown_pow_of_two(total_cmds
);
2753 if (total_cmds
< ISCSI_TOTAL_CMDS_MIN
)
2755 printk(KERN_INFO
"iscsi: Rounding can_queue to %d.\n",
2758 scsi_cmds
= total_cmds
- ISCSI_MGMT_CMDS_MAX
;
2760 cls_session
= iscsi_alloc_session(shost
, iscsit
,
2761 sizeof(struct iscsi_session
) +
2764 goto dec_session_count
;
2765 session
= cls_session
->dd_data
;
2766 session
->cls_session
= cls_session
;
2767 session
->host
= shost
;
2768 session
->state
= ISCSI_STATE_FREE
;
2769 session
->fast_abort
= 1;
2770 session
->tgt_reset_timeout
= 30;
2771 session
->lu_reset_timeout
= 15;
2772 session
->abort_timeout
= 10;
2773 session
->scsi_cmds_max
= scsi_cmds
;
2774 session
->cmds_max
= total_cmds
;
2775 session
->queued_cmdsn
= session
->cmdsn
= initial_cmdsn
;
2776 session
->exp_cmdsn
= initial_cmdsn
+ 1;
2777 session
->max_cmdsn
= initial_cmdsn
+ 1;
2778 session
->max_r2t
= 1;
2779 session
->tt
= iscsit
;
2780 session
->dd_data
= cls_session
->dd_data
+ sizeof(*session
);
2781 mutex_init(&session
->eh_mutex
);
2782 spin_lock_init(&session
->lock
);
2784 /* initialize SCSI PDU commands pool */
2785 if (iscsi_pool_init(&session
->cmdpool
, session
->cmds_max
,
2786 (void***)&session
->cmds
,
2787 cmd_task_size
+ sizeof(struct iscsi_task
)))
2788 goto cmdpool_alloc_fail
;
2790 /* pre-format cmds pool with ITT */
2791 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
2792 struct iscsi_task
*task
= session
->cmds
[cmd_i
];
2795 task
->dd_data
= &task
[1];
2797 task
->state
= ISCSI_TASK_FREE
;
2798 INIT_LIST_HEAD(&task
->running
);
2801 if (!try_module_get(iscsit
->owner
))
2802 goto module_get_fail
;
2804 if (iscsi_add_session(cls_session
, id
))
2805 goto cls_session_fail
;
2810 module_put(iscsit
->owner
);
2812 iscsi_pool_free(&session
->cmdpool
);
2814 iscsi_free_session(cls_session
);
2816 iscsi_host_dec_session_cnt(shost
);
2819 EXPORT_SYMBOL_GPL(iscsi_session_setup
);
2822 * iscsi_session_teardown - destroy session, host, and cls_session
2823 * @cls_session: iscsi session
2825 * The driver must have called iscsi_remove_session before
2828 void iscsi_session_teardown(struct iscsi_cls_session
*cls_session
)
2830 struct iscsi_session
*session
= cls_session
->dd_data
;
2831 struct module
*owner
= cls_session
->transport
->owner
;
2832 struct Scsi_Host
*shost
= session
->host
;
2834 iscsi_pool_free(&session
->cmdpool
);
2836 kfree(session
->password
);
2837 kfree(session
->password_in
);
2838 kfree(session
->username
);
2839 kfree(session
->username_in
);
2840 kfree(session
->targetname
);
2841 kfree(session
->targetalias
);
2842 kfree(session
->initiatorname
);
2843 kfree(session
->boot_root
);
2844 kfree(session
->boot_nic
);
2845 kfree(session
->boot_target
);
2846 kfree(session
->ifacename
);
2847 kfree(session
->portal_type
);
2848 kfree(session
->discovery_parent_type
);
2850 iscsi_destroy_session(cls_session
);
2851 iscsi_host_dec_session_cnt(shost
);
2854 EXPORT_SYMBOL_GPL(iscsi_session_teardown
);
2857 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2858 * @cls_session: iscsi_cls_session
2859 * @dd_size: private driver data size
2862 struct iscsi_cls_conn
*
2863 iscsi_conn_setup(struct iscsi_cls_session
*cls_session
, int dd_size
,
2866 struct iscsi_session
*session
= cls_session
->dd_data
;
2867 struct iscsi_conn
*conn
;
2868 struct iscsi_cls_conn
*cls_conn
;
2871 cls_conn
= iscsi_create_conn(cls_session
, sizeof(*conn
) + dd_size
,
2875 conn
= cls_conn
->dd_data
;
2876 memset(conn
, 0, sizeof(*conn
) + dd_size
);
2878 conn
->dd_data
= cls_conn
->dd_data
+ sizeof(*conn
);
2879 conn
->session
= session
;
2880 conn
->cls_conn
= cls_conn
;
2881 conn
->c_stage
= ISCSI_CONN_INITIAL_STAGE
;
2882 conn
->id
= conn_idx
;
2883 conn
->exp_statsn
= 0;
2884 conn
->tmf_state
= TMF_INITIAL
;
2886 init_timer(&conn
->transport_timer
);
2887 conn
->transport_timer
.data
= (unsigned long)conn
;
2888 conn
->transport_timer
.function
= iscsi_check_transport_timeouts
;
2890 INIT_LIST_HEAD(&conn
->mgmtqueue
);
2891 INIT_LIST_HEAD(&conn
->cmdqueue
);
2892 INIT_LIST_HEAD(&conn
->requeue
);
2893 INIT_WORK(&conn
->xmitwork
, iscsi_xmitworker
);
2895 /* allocate login_task used for the login/text sequences */
2896 spin_lock_bh(&session
->lock
);
2897 if (!kfifo_out(&session
->cmdpool
.queue
,
2898 (void*)&conn
->login_task
,
2900 spin_unlock_bh(&session
->lock
);
2901 goto login_task_alloc_fail
;
2903 spin_unlock_bh(&session
->lock
);
2905 data
= (char *) __get_free_pages(GFP_KERNEL
,
2906 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN
));
2908 goto login_task_data_alloc_fail
;
2909 conn
->login_task
->data
= conn
->data
= data
;
2911 init_timer(&conn
->tmf_timer
);
2912 init_waitqueue_head(&conn
->ehwait
);
2916 login_task_data_alloc_fail
:
2917 kfifo_in(&session
->cmdpool
.queue
, (void*)&conn
->login_task
,
2919 login_task_alloc_fail
:
2920 iscsi_destroy_conn(cls_conn
);
2923 EXPORT_SYMBOL_GPL(iscsi_conn_setup
);
2926 * iscsi_conn_teardown - teardown iscsi connection
2927 * cls_conn: iscsi class connection
2929 * TODO: we may need to make this into a two step process
2930 * like scsi-mls remove + put host
2932 void iscsi_conn_teardown(struct iscsi_cls_conn
*cls_conn
)
2934 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2935 struct iscsi_session
*session
= conn
->session
;
2936 unsigned long flags
;
2938 del_timer_sync(&conn
->transport_timer
);
2940 spin_lock_bh(&session
->lock
);
2941 conn
->c_stage
= ISCSI_CONN_CLEANUP_WAIT
;
2942 if (session
->leadconn
== conn
) {
2944 * leading connection? then give up on recovery.
2946 session
->state
= ISCSI_STATE_TERMINATE
;
2947 wake_up(&conn
->ehwait
);
2949 spin_unlock_bh(&session
->lock
);
2952 * Block until all in-progress commands for this connection
2956 spin_lock_irqsave(session
->host
->host_lock
, flags
);
2957 if (!session
->host
->host_busy
) { /* OK for ERL == 0 */
2958 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
2961 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
2962 msleep_interruptible(500);
2963 iscsi_conn_printk(KERN_INFO
, conn
, "iscsi conn_destroy(): "
2964 "host_busy %d host_failed %d\n",
2965 session
->host
->host_busy
,
2966 session
->host
->host_failed
);
2968 * force eh_abort() to unblock
2970 wake_up(&conn
->ehwait
);
2973 /* flush queued up work because we free the connection below */
2974 iscsi_suspend_tx(conn
);
2976 spin_lock_bh(&session
->lock
);
2977 free_pages((unsigned long) conn
->data
,
2978 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN
));
2979 kfree(conn
->persistent_address
);
2980 kfree(conn
->local_ipaddr
);
2981 kfifo_in(&session
->cmdpool
.queue
, (void*)&conn
->login_task
,
2983 if (session
->leadconn
== conn
)
2984 session
->leadconn
= NULL
;
2985 spin_unlock_bh(&session
->lock
);
2987 iscsi_destroy_conn(cls_conn
);
2989 EXPORT_SYMBOL_GPL(iscsi_conn_teardown
);
2991 int iscsi_conn_start(struct iscsi_cls_conn
*cls_conn
)
2993 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2994 struct iscsi_session
*session
= conn
->session
;
2997 iscsi_conn_printk(KERN_ERR
, conn
,
2998 "can't start unbound connection\n");
3002 if ((session
->imm_data_en
|| !session
->initial_r2t_en
) &&
3003 session
->first_burst
> session
->max_burst
) {
3004 iscsi_conn_printk(KERN_INFO
, conn
, "invalid burst lengths: "
3005 "first_burst %d max_burst %d\n",
3006 session
->first_burst
, session
->max_burst
);
3010 if (conn
->ping_timeout
&& !conn
->recv_timeout
) {
3011 iscsi_conn_printk(KERN_ERR
, conn
, "invalid recv timeout of "
3012 "zero. Using 5 seconds\n.");
3013 conn
->recv_timeout
= 5;
3016 if (conn
->recv_timeout
&& !conn
->ping_timeout
) {
3017 iscsi_conn_printk(KERN_ERR
, conn
, "invalid ping timeout of "
3018 "zero. Using 5 seconds.\n");
3019 conn
->ping_timeout
= 5;
3022 spin_lock_bh(&session
->lock
);
3023 conn
->c_stage
= ISCSI_CONN_STARTED
;
3024 session
->state
= ISCSI_STATE_LOGGED_IN
;
3025 session
->queued_cmdsn
= session
->cmdsn
;
3027 conn
->last_recv
= jiffies
;
3028 conn
->last_ping
= jiffies
;
3029 if (conn
->recv_timeout
&& conn
->ping_timeout
)
3030 mod_timer(&conn
->transport_timer
,
3031 jiffies
+ (conn
->recv_timeout
* HZ
));
3033 switch(conn
->stop_stage
) {
3034 case STOP_CONN_RECOVER
:
3036 * unblock eh_abort() if it is blocked. re-try all
3037 * commands after successful recovery
3039 conn
->stop_stage
= 0;
3040 conn
->tmf_state
= TMF_INITIAL
;
3042 if (session
->age
== 16)
3045 case STOP_CONN_TERM
:
3046 conn
->stop_stage
= 0;
3051 spin_unlock_bh(&session
->lock
);
3053 iscsi_unblock_session(session
->cls_session
);
3054 wake_up(&conn
->ehwait
);
3057 EXPORT_SYMBOL_GPL(iscsi_conn_start
);
3060 fail_mgmt_tasks(struct iscsi_session
*session
, struct iscsi_conn
*conn
)
3062 struct iscsi_task
*task
;
3065 for (i
= 0; i
< conn
->session
->cmds_max
; i
++) {
3066 task
= conn
->session
->cmds
[i
];
3070 if (task
->state
== ISCSI_TASK_FREE
)
3073 ISCSI_DBG_SESSION(conn
->session
,
3074 "failing mgmt itt 0x%x state %d\n",
3075 task
->itt
, task
->state
);
3076 state
= ISCSI_TASK_ABRT_SESS_RECOV
;
3077 if (task
->state
== ISCSI_TASK_PENDING
)
3078 state
= ISCSI_TASK_COMPLETED
;
3079 iscsi_complete_task(task
, state
);
3084 static void iscsi_start_session_recovery(struct iscsi_session
*session
,
3085 struct iscsi_conn
*conn
, int flag
)
3089 mutex_lock(&session
->eh_mutex
);
3090 spin_lock_bh(&session
->lock
);
3091 if (conn
->stop_stage
== STOP_CONN_TERM
) {
3092 spin_unlock_bh(&session
->lock
);
3093 mutex_unlock(&session
->eh_mutex
);
3098 * When this is called for the in_login state, we only want to clean
3099 * up the login task and connection. We do not need to block and set
3100 * the recovery state again
3102 if (flag
== STOP_CONN_TERM
)
3103 session
->state
= ISCSI_STATE_TERMINATE
;
3104 else if (conn
->stop_stage
!= STOP_CONN_RECOVER
)
3105 session
->state
= ISCSI_STATE_IN_RECOVERY
;
3107 old_stop_stage
= conn
->stop_stage
;
3108 conn
->stop_stage
= flag
;
3109 spin_unlock_bh(&session
->lock
);
3111 del_timer_sync(&conn
->transport_timer
);
3112 iscsi_suspend_tx(conn
);
3114 spin_lock_bh(&session
->lock
);
3115 conn
->c_stage
= ISCSI_CONN_STOPPED
;
3116 spin_unlock_bh(&session
->lock
);
3119 * for connection level recovery we should not calculate
3120 * header digest. conn->hdr_size used for optimization
3121 * in hdr_extract() and will be re-negotiated at
3124 if (flag
== STOP_CONN_RECOVER
) {
3125 conn
->hdrdgst_en
= 0;
3126 conn
->datadgst_en
= 0;
3127 if (session
->state
== ISCSI_STATE_IN_RECOVERY
&&
3128 old_stop_stage
!= STOP_CONN_RECOVER
) {
3129 ISCSI_DBG_SESSION(session
, "blocking session\n");
3130 iscsi_block_session(session
->cls_session
);
3137 spin_lock_bh(&session
->lock
);
3138 fail_scsi_tasks(conn
, -1, DID_TRANSPORT_DISRUPTED
);
3139 fail_mgmt_tasks(session
, conn
);
3140 memset(&conn
->tmhdr
, 0, sizeof(conn
->tmhdr
));
3141 spin_unlock_bh(&session
->lock
);
3142 mutex_unlock(&session
->eh_mutex
);
3145 void iscsi_conn_stop(struct iscsi_cls_conn
*cls_conn
, int flag
)
3147 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
3148 struct iscsi_session
*session
= conn
->session
;
3151 case STOP_CONN_RECOVER
:
3152 case STOP_CONN_TERM
:
3153 iscsi_start_session_recovery(session
, conn
, flag
);
3156 iscsi_conn_printk(KERN_ERR
, conn
,
3157 "invalid stop flag %d\n", flag
);
3160 EXPORT_SYMBOL_GPL(iscsi_conn_stop
);
3162 int iscsi_conn_bind(struct iscsi_cls_session
*cls_session
,
3163 struct iscsi_cls_conn
*cls_conn
, int is_leading
)
3165 struct iscsi_session
*session
= cls_session
->dd_data
;
3166 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
3168 spin_lock_bh(&session
->lock
);
3170 session
->leadconn
= conn
;
3171 spin_unlock_bh(&session
->lock
);
3174 * Unblock xmitworker(), Login Phase will pass through.
3176 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
3177 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
3180 EXPORT_SYMBOL_GPL(iscsi_conn_bind
);
3182 int iscsi_switch_str_param(char **param
, char *new_val_buf
)
3187 if (!strcmp(*param
, new_val_buf
))
3191 new_val
= kstrdup(new_val_buf
, GFP_NOIO
);
3199 EXPORT_SYMBOL_GPL(iscsi_switch_str_param
);
3201 int iscsi_set_param(struct iscsi_cls_conn
*cls_conn
,
3202 enum iscsi_param param
, char *buf
, int buflen
)
3204 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
3205 struct iscsi_session
*session
= conn
->session
;
3209 case ISCSI_PARAM_FAST_ABORT
:
3210 sscanf(buf
, "%d", &session
->fast_abort
);
3212 case ISCSI_PARAM_ABORT_TMO
:
3213 sscanf(buf
, "%d", &session
->abort_timeout
);
3215 case ISCSI_PARAM_LU_RESET_TMO
:
3216 sscanf(buf
, "%d", &session
->lu_reset_timeout
);
3218 case ISCSI_PARAM_TGT_RESET_TMO
:
3219 sscanf(buf
, "%d", &session
->tgt_reset_timeout
);
3221 case ISCSI_PARAM_PING_TMO
:
3222 sscanf(buf
, "%d", &conn
->ping_timeout
);
3224 case ISCSI_PARAM_RECV_TMO
:
3225 sscanf(buf
, "%d", &conn
->recv_timeout
);
3227 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
3228 sscanf(buf
, "%d", &conn
->max_recv_dlength
);
3230 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
3231 sscanf(buf
, "%d", &conn
->max_xmit_dlength
);
3233 case ISCSI_PARAM_HDRDGST_EN
:
3234 sscanf(buf
, "%d", &conn
->hdrdgst_en
);
3236 case ISCSI_PARAM_DATADGST_EN
:
3237 sscanf(buf
, "%d", &conn
->datadgst_en
);
3239 case ISCSI_PARAM_INITIAL_R2T_EN
:
3240 sscanf(buf
, "%d", &session
->initial_r2t_en
);
3242 case ISCSI_PARAM_MAX_R2T
:
3243 sscanf(buf
, "%hu", &session
->max_r2t
);
3245 case ISCSI_PARAM_IMM_DATA_EN
:
3246 sscanf(buf
, "%d", &session
->imm_data_en
);
3248 case ISCSI_PARAM_FIRST_BURST
:
3249 sscanf(buf
, "%d", &session
->first_burst
);
3251 case ISCSI_PARAM_MAX_BURST
:
3252 sscanf(buf
, "%d", &session
->max_burst
);
3254 case ISCSI_PARAM_PDU_INORDER_EN
:
3255 sscanf(buf
, "%d", &session
->pdu_inorder_en
);
3257 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
3258 sscanf(buf
, "%d", &session
->dataseq_inorder_en
);
3260 case ISCSI_PARAM_ERL
:
3261 sscanf(buf
, "%d", &session
->erl
);
3263 case ISCSI_PARAM_EXP_STATSN
:
3264 sscanf(buf
, "%u", &conn
->exp_statsn
);
3266 case ISCSI_PARAM_USERNAME
:
3267 return iscsi_switch_str_param(&session
->username
, buf
);
3268 case ISCSI_PARAM_USERNAME_IN
:
3269 return iscsi_switch_str_param(&session
->username_in
, buf
);
3270 case ISCSI_PARAM_PASSWORD
:
3271 return iscsi_switch_str_param(&session
->password
, buf
);
3272 case ISCSI_PARAM_PASSWORD_IN
:
3273 return iscsi_switch_str_param(&session
->password_in
, buf
);
3274 case ISCSI_PARAM_TARGET_NAME
:
3275 return iscsi_switch_str_param(&session
->targetname
, buf
);
3276 case ISCSI_PARAM_TARGET_ALIAS
:
3277 return iscsi_switch_str_param(&session
->targetalias
, buf
);
3278 case ISCSI_PARAM_TPGT
:
3279 sscanf(buf
, "%d", &session
->tpgt
);
3281 case ISCSI_PARAM_PERSISTENT_PORT
:
3282 sscanf(buf
, "%d", &conn
->persistent_port
);
3284 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
3285 return iscsi_switch_str_param(&conn
->persistent_address
, buf
);
3286 case ISCSI_PARAM_IFACE_NAME
:
3287 return iscsi_switch_str_param(&session
->ifacename
, buf
);
3288 case ISCSI_PARAM_INITIATOR_NAME
:
3289 return iscsi_switch_str_param(&session
->initiatorname
, buf
);
3290 case ISCSI_PARAM_BOOT_ROOT
:
3291 return iscsi_switch_str_param(&session
->boot_root
, buf
);
3292 case ISCSI_PARAM_BOOT_NIC
:
3293 return iscsi_switch_str_param(&session
->boot_nic
, buf
);
3294 case ISCSI_PARAM_BOOT_TARGET
:
3295 return iscsi_switch_str_param(&session
->boot_target
, buf
);
3296 case ISCSI_PARAM_PORTAL_TYPE
:
3297 return iscsi_switch_str_param(&session
->portal_type
, buf
);
3298 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE
:
3299 return iscsi_switch_str_param(&session
->discovery_parent_type
,
3301 case ISCSI_PARAM_DISCOVERY_SESS
:
3302 sscanf(buf
, "%d", &val
);
3303 session
->discovery_sess
= !!val
;
3305 case ISCSI_PARAM_LOCAL_IPADDR
:
3306 return iscsi_switch_str_param(&conn
->local_ipaddr
, buf
);
3313 EXPORT_SYMBOL_GPL(iscsi_set_param
);
3315 int iscsi_session_get_param(struct iscsi_cls_session
*cls_session
,
3316 enum iscsi_param param
, char *buf
)
3318 struct iscsi_session
*session
= cls_session
->dd_data
;
3322 case ISCSI_PARAM_FAST_ABORT
:
3323 len
= sprintf(buf
, "%d\n", session
->fast_abort
);
3325 case ISCSI_PARAM_ABORT_TMO
:
3326 len
= sprintf(buf
, "%d\n", session
->abort_timeout
);
3328 case ISCSI_PARAM_LU_RESET_TMO
:
3329 len
= sprintf(buf
, "%d\n", session
->lu_reset_timeout
);
3331 case ISCSI_PARAM_TGT_RESET_TMO
:
3332 len
= sprintf(buf
, "%d\n", session
->tgt_reset_timeout
);
3334 case ISCSI_PARAM_INITIAL_R2T_EN
:
3335 len
= sprintf(buf
, "%d\n", session
->initial_r2t_en
);
3337 case ISCSI_PARAM_MAX_R2T
:
3338 len
= sprintf(buf
, "%hu\n", session
->max_r2t
);
3340 case ISCSI_PARAM_IMM_DATA_EN
:
3341 len
= sprintf(buf
, "%d\n", session
->imm_data_en
);
3343 case ISCSI_PARAM_FIRST_BURST
:
3344 len
= sprintf(buf
, "%u\n", session
->first_burst
);
3346 case ISCSI_PARAM_MAX_BURST
:
3347 len
= sprintf(buf
, "%u\n", session
->max_burst
);
3349 case ISCSI_PARAM_PDU_INORDER_EN
:
3350 len
= sprintf(buf
, "%d\n", session
->pdu_inorder_en
);
3352 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
3353 len
= sprintf(buf
, "%d\n", session
->dataseq_inorder_en
);
3355 case ISCSI_PARAM_DEF_TASKMGMT_TMO
:
3356 len
= sprintf(buf
, "%d\n", session
->def_taskmgmt_tmo
);
3358 case ISCSI_PARAM_ERL
:
3359 len
= sprintf(buf
, "%d\n", session
->erl
);
3361 case ISCSI_PARAM_TARGET_NAME
:
3362 len
= sprintf(buf
, "%s\n", session
->targetname
);
3364 case ISCSI_PARAM_TARGET_ALIAS
:
3365 len
= sprintf(buf
, "%s\n", session
->targetalias
);
3367 case ISCSI_PARAM_TPGT
:
3368 len
= sprintf(buf
, "%d\n", session
->tpgt
);
3370 case ISCSI_PARAM_USERNAME
:
3371 len
= sprintf(buf
, "%s\n", session
->username
);
3373 case ISCSI_PARAM_USERNAME_IN
:
3374 len
= sprintf(buf
, "%s\n", session
->username_in
);
3376 case ISCSI_PARAM_PASSWORD
:
3377 len
= sprintf(buf
, "%s\n", session
->password
);
3379 case ISCSI_PARAM_PASSWORD_IN
:
3380 len
= sprintf(buf
, "%s\n", session
->password_in
);
3382 case ISCSI_PARAM_IFACE_NAME
:
3383 len
= sprintf(buf
, "%s\n", session
->ifacename
);
3385 case ISCSI_PARAM_INITIATOR_NAME
:
3386 len
= sprintf(buf
, "%s\n", session
->initiatorname
);
3388 case ISCSI_PARAM_BOOT_ROOT
:
3389 len
= sprintf(buf
, "%s\n", session
->boot_root
);
3391 case ISCSI_PARAM_BOOT_NIC
:
3392 len
= sprintf(buf
, "%s\n", session
->boot_nic
);
3394 case ISCSI_PARAM_BOOT_TARGET
:
3395 len
= sprintf(buf
, "%s\n", session
->boot_target
);
3397 case ISCSI_PARAM_AUTO_SND_TGT_DISABLE
:
3398 len
= sprintf(buf
, "%u\n", session
->auto_snd_tgt_disable
);
3400 case ISCSI_PARAM_DISCOVERY_SESS
:
3401 len
= sprintf(buf
, "%u\n", session
->discovery_sess
);
3403 case ISCSI_PARAM_PORTAL_TYPE
:
3404 len
= sprintf(buf
, "%s\n", session
->portal_type
);
3406 case ISCSI_PARAM_CHAP_AUTH_EN
:
3407 len
= sprintf(buf
, "%u\n", session
->chap_auth_en
);
3409 case ISCSI_PARAM_DISCOVERY_LOGOUT_EN
:
3410 len
= sprintf(buf
, "%u\n", session
->discovery_logout_en
);
3412 case ISCSI_PARAM_BIDI_CHAP_EN
:
3413 len
= sprintf(buf
, "%u\n", session
->bidi_chap_en
);
3415 case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL
:
3416 len
= sprintf(buf
, "%u\n", session
->discovery_auth_optional
);
3418 case ISCSI_PARAM_DEF_TIME2WAIT
:
3419 len
= sprintf(buf
, "%d\n", session
->time2wait
);
3421 case ISCSI_PARAM_DEF_TIME2RETAIN
:
3422 len
= sprintf(buf
, "%d\n", session
->time2retain
);
3424 case ISCSI_PARAM_TSID
:
3425 len
= sprintf(buf
, "%u\n", session
->tsid
);
3427 case ISCSI_PARAM_ISID
:
3428 len
= sprintf(buf
, "%02x%02x%02x%02x%02x%02x\n",
3429 session
->isid
[0], session
->isid
[1],
3430 session
->isid
[2], session
->isid
[3],
3431 session
->isid
[4], session
->isid
[5]);
3433 case ISCSI_PARAM_DISCOVERY_PARENT_IDX
:
3434 len
= sprintf(buf
, "%u\n", session
->discovery_parent_idx
);
3436 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE
:
3437 if (session
->discovery_parent_type
)
3438 len
= sprintf(buf
, "%s\n",
3439 session
->discovery_parent_type
);
3441 len
= sprintf(buf
, "\n");
3449 EXPORT_SYMBOL_GPL(iscsi_session_get_param
);
3451 int iscsi_conn_get_addr_param(struct sockaddr_storage
*addr
,
3452 enum iscsi_param param
, char *buf
)
3454 struct sockaddr_in6
*sin6
= NULL
;
3455 struct sockaddr_in
*sin
= NULL
;
3458 switch (addr
->ss_family
) {
3460 sin
= (struct sockaddr_in
*)addr
;
3463 sin6
= (struct sockaddr_in6
*)addr
;
3470 case ISCSI_PARAM_CONN_ADDRESS
:
3471 case ISCSI_HOST_PARAM_IPADDRESS
:
3473 len
= sprintf(buf
, "%pI4\n", &sin
->sin_addr
.s_addr
);
3475 len
= sprintf(buf
, "%pI6\n", &sin6
->sin6_addr
);
3477 case ISCSI_PARAM_CONN_PORT
:
3479 len
= sprintf(buf
, "%hu\n", be16_to_cpu(sin
->sin_port
));
3481 len
= sprintf(buf
, "%hu\n",
3482 be16_to_cpu(sin6
->sin6_port
));
3490 EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param
);
3492 int iscsi_conn_get_param(struct iscsi_cls_conn
*cls_conn
,
3493 enum iscsi_param param
, char *buf
)
3495 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
3499 case ISCSI_PARAM_PING_TMO
:
3500 len
= sprintf(buf
, "%u\n", conn
->ping_timeout
);
3502 case ISCSI_PARAM_RECV_TMO
:
3503 len
= sprintf(buf
, "%u\n", conn
->recv_timeout
);
3505 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
3506 len
= sprintf(buf
, "%u\n", conn
->max_recv_dlength
);
3508 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
3509 len
= sprintf(buf
, "%u\n", conn
->max_xmit_dlength
);
3511 case ISCSI_PARAM_HDRDGST_EN
:
3512 len
= sprintf(buf
, "%d\n", conn
->hdrdgst_en
);
3514 case ISCSI_PARAM_DATADGST_EN
:
3515 len
= sprintf(buf
, "%d\n", conn
->datadgst_en
);
3517 case ISCSI_PARAM_IFMARKER_EN
:
3518 len
= sprintf(buf
, "%d\n", conn
->ifmarker_en
);
3520 case ISCSI_PARAM_OFMARKER_EN
:
3521 len
= sprintf(buf
, "%d\n", conn
->ofmarker_en
);
3523 case ISCSI_PARAM_EXP_STATSN
:
3524 len
= sprintf(buf
, "%u\n", conn
->exp_statsn
);
3526 case ISCSI_PARAM_PERSISTENT_PORT
:
3527 len
= sprintf(buf
, "%d\n", conn
->persistent_port
);
3529 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
3530 len
= sprintf(buf
, "%s\n", conn
->persistent_address
);
3532 case ISCSI_PARAM_STATSN
:
3533 len
= sprintf(buf
, "%u\n", conn
->statsn
);
3535 case ISCSI_PARAM_MAX_SEGMENT_SIZE
:
3536 len
= sprintf(buf
, "%u\n", conn
->max_segment_size
);
3538 case ISCSI_PARAM_KEEPALIVE_TMO
:
3539 len
= sprintf(buf
, "%u\n", conn
->keepalive_tmo
);
3541 case ISCSI_PARAM_LOCAL_PORT
:
3542 len
= sprintf(buf
, "%u\n", conn
->local_port
);
3544 case ISCSI_PARAM_TCP_TIMESTAMP_STAT
:
3545 len
= sprintf(buf
, "%u\n", conn
->tcp_timestamp_stat
);
3547 case ISCSI_PARAM_TCP_NAGLE_DISABLE
:
3548 len
= sprintf(buf
, "%u\n", conn
->tcp_nagle_disable
);
3550 case ISCSI_PARAM_TCP_WSF_DISABLE
:
3551 len
= sprintf(buf
, "%u\n", conn
->tcp_wsf_disable
);
3553 case ISCSI_PARAM_TCP_TIMER_SCALE
:
3554 len
= sprintf(buf
, "%u\n", conn
->tcp_timer_scale
);
3556 case ISCSI_PARAM_TCP_TIMESTAMP_EN
:
3557 len
= sprintf(buf
, "%u\n", conn
->tcp_timestamp_en
);
3559 case ISCSI_PARAM_IP_FRAGMENT_DISABLE
:
3560 len
= sprintf(buf
, "%u\n", conn
->fragment_disable
);
3562 case ISCSI_PARAM_IPV4_TOS
:
3563 len
= sprintf(buf
, "%u\n", conn
->ipv4_tos
);
3565 case ISCSI_PARAM_IPV6_TC
:
3566 len
= sprintf(buf
, "%u\n", conn
->ipv6_traffic_class
);
3568 case ISCSI_PARAM_IPV6_FLOW_LABEL
:
3569 len
= sprintf(buf
, "%u\n", conn
->ipv6_flow_label
);
3571 case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6
:
3572 len
= sprintf(buf
, "%u\n", conn
->is_fw_assigned_ipv6
);
3574 case ISCSI_PARAM_TCP_XMIT_WSF
:
3575 len
= sprintf(buf
, "%u\n", conn
->tcp_xmit_wsf
);
3577 case ISCSI_PARAM_TCP_RECV_WSF
:
3578 len
= sprintf(buf
, "%u\n", conn
->tcp_recv_wsf
);
3580 case ISCSI_PARAM_LOCAL_IPADDR
:
3581 len
= sprintf(buf
, "%s\n", conn
->local_ipaddr
);
3589 EXPORT_SYMBOL_GPL(iscsi_conn_get_param
);
3591 int iscsi_host_get_param(struct Scsi_Host
*shost
, enum iscsi_host_param param
,
3594 struct iscsi_host
*ihost
= shost_priv(shost
);
3598 case ISCSI_HOST_PARAM_NETDEV_NAME
:
3599 len
= sprintf(buf
, "%s\n", ihost
->netdev
);
3601 case ISCSI_HOST_PARAM_HWADDRESS
:
3602 len
= sprintf(buf
, "%s\n", ihost
->hwaddress
);
3604 case ISCSI_HOST_PARAM_INITIATOR_NAME
:
3605 len
= sprintf(buf
, "%s\n", ihost
->initiatorname
);
3613 EXPORT_SYMBOL_GPL(iscsi_host_get_param
);
3615 int iscsi_host_set_param(struct Scsi_Host
*shost
, enum iscsi_host_param param
,
3616 char *buf
, int buflen
)
3618 struct iscsi_host
*ihost
= shost_priv(shost
);
3621 case ISCSI_HOST_PARAM_NETDEV_NAME
:
3622 return iscsi_switch_str_param(&ihost
->netdev
, buf
);
3623 case ISCSI_HOST_PARAM_HWADDRESS
:
3624 return iscsi_switch_str_param(&ihost
->hwaddress
, buf
);
3625 case ISCSI_HOST_PARAM_INITIATOR_NAME
:
3626 return iscsi_switch_str_param(&ihost
->initiatorname
, buf
);
3633 EXPORT_SYMBOL_GPL(iscsi_host_set_param
);
3635 MODULE_AUTHOR("Mike Christie");
3636 MODULE_DESCRIPTION("iSCSI library functions");
3637 MODULE_LICENSE("GPL");