4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/types.h>
25 #include <linux/kfifo.h>
26 #include <linux/delay.h>
27 #include <linux/log2.h>
28 #include <asm/unaligned.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi.h>
36 #include <scsi/iscsi_proto.h>
37 #include <scsi/scsi_transport.h>
38 #include <scsi/scsi_transport_iscsi.h>
39 #include <scsi/libiscsi.h>
41 static int iscsi_dbg_lib_conn
;
42 module_param_named(debug_libiscsi_conn
, iscsi_dbg_lib_conn
, int,
44 MODULE_PARM_DESC(debug_libiscsi_conn
,
45 "Turn on debugging for connections in libiscsi module. "
46 "Set to 1 to turn on, and zero to turn off. Default is off.");
48 static int iscsi_dbg_lib_session
;
49 module_param_named(debug_libiscsi_session
, iscsi_dbg_lib_session
, int,
51 MODULE_PARM_DESC(debug_libiscsi_session
,
52 "Turn on debugging for sessions in libiscsi module. "
53 "Set to 1 to turn on, and zero to turn off. Default is off.");
55 static int iscsi_dbg_lib_eh
;
56 module_param_named(debug_libiscsi_eh
, iscsi_dbg_lib_eh
, int,
58 MODULE_PARM_DESC(debug_libiscsi_eh
,
59 "Turn on debugging for error handling in libiscsi module. "
60 "Set to 1 to turn on, and zero to turn off. Default is off.");
62 #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
64 if (iscsi_dbg_lib_conn) \
65 iscsi_conn_printk(KERN_INFO, _conn, \
70 #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
72 if (iscsi_dbg_lib_session) \
73 iscsi_session_printk(KERN_INFO, _session, \
78 #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
80 if (iscsi_dbg_lib_eh) \
81 iscsi_session_printk(KERN_INFO, _session, \
86 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
87 #define SNA32_CHECK 2147483648UL
89 static int iscsi_sna_lt(u32 n1
, u32 n2
)
91 return n1
!= n2
&& ((n1
< n2
&& (n2
- n1
< SNA32_CHECK
)) ||
92 (n1
> n2
&& (n2
- n1
< SNA32_CHECK
)));
95 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
96 static int iscsi_sna_lte(u32 n1
, u32 n2
)
98 return n1
== n2
|| ((n1
< n2
&& (n2
- n1
< SNA32_CHECK
)) ||
99 (n1
> n2
&& (n2
- n1
< SNA32_CHECK
)));
102 inline void iscsi_conn_queue_work(struct iscsi_conn
*conn
)
104 struct Scsi_Host
*shost
= conn
->session
->host
;
105 struct iscsi_host
*ihost
= shost_priv(shost
);
108 queue_work(ihost
->workq
, &conn
->xmitwork
);
110 EXPORT_SYMBOL_GPL(iscsi_conn_queue_work
);
113 iscsi_update_cmdsn(struct iscsi_session
*session
, struct iscsi_nopin
*hdr
)
115 uint32_t max_cmdsn
= be32_to_cpu(hdr
->max_cmdsn
);
116 uint32_t exp_cmdsn
= be32_to_cpu(hdr
->exp_cmdsn
);
119 * standard specifies this check for when to update expected and
120 * max sequence numbers
122 if (iscsi_sna_lt(max_cmdsn
, exp_cmdsn
- 1))
125 if (exp_cmdsn
!= session
->exp_cmdsn
&&
126 !iscsi_sna_lt(exp_cmdsn
, session
->exp_cmdsn
))
127 session
->exp_cmdsn
= exp_cmdsn
;
129 if (max_cmdsn
!= session
->max_cmdsn
&&
130 !iscsi_sna_lt(max_cmdsn
, session
->max_cmdsn
)) {
131 session
->max_cmdsn
= max_cmdsn
;
133 * if the window closed with IO queued, then kick the
136 if (!list_empty(&session
->leadconn
->cmdqueue
) ||
137 !list_empty(&session
->leadconn
->mgmtqueue
))
138 iscsi_conn_queue_work(session
->leadconn
);
141 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn
);
144 * iscsi_prep_data_out_pdu - initialize Data-Out
145 * @task: scsi command task
147 * @hdr: iscsi data in pdu
150 * Initialize Data-Out within this R2T sequence and finds
151 * proper data_offset within this SCSI command.
153 * This function is called with connection lock taken.
155 void iscsi_prep_data_out_pdu(struct iscsi_task
*task
, struct iscsi_r2t_info
*r2t
,
156 struct iscsi_data
*hdr
)
158 struct iscsi_conn
*conn
= task
->conn
;
159 unsigned int left
= r2t
->data_length
- r2t
->sent
;
161 task
->hdr_len
= sizeof(struct iscsi_data
);
163 memset(hdr
, 0, sizeof(struct iscsi_data
));
165 hdr
->datasn
= cpu_to_be32(r2t
->datasn
);
167 hdr
->opcode
= ISCSI_OP_SCSI_DATA_OUT
;
168 memcpy(hdr
->lun
, task
->lun
, sizeof(hdr
->lun
));
169 hdr
->itt
= task
->hdr_itt
;
170 hdr
->exp_statsn
= r2t
->exp_statsn
;
171 hdr
->offset
= cpu_to_be32(r2t
->data_offset
+ r2t
->sent
);
172 if (left
> conn
->max_xmit_dlength
) {
173 hton24(hdr
->dlength
, conn
->max_xmit_dlength
);
174 r2t
->data_count
= conn
->max_xmit_dlength
;
177 hton24(hdr
->dlength
, left
);
178 r2t
->data_count
= left
;
179 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
181 conn
->dataout_pdus_cnt
++;
183 EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu
);
185 static int iscsi_add_hdr(struct iscsi_task
*task
, unsigned len
)
187 unsigned exp_len
= task
->hdr_len
+ len
;
189 if (exp_len
> task
->hdr_max
) {
194 WARN_ON(len
& (ISCSI_PAD_LEN
- 1)); /* caller must pad the AHS */
195 task
->hdr_len
= exp_len
;
200 * make an extended cdb AHS
202 static int iscsi_prep_ecdb_ahs(struct iscsi_task
*task
)
204 struct scsi_cmnd
*cmd
= task
->sc
;
205 unsigned rlen
, pad_len
;
206 unsigned short ahslength
;
207 struct iscsi_ecdb_ahdr
*ecdb_ahdr
;
210 ecdb_ahdr
= iscsi_next_hdr(task
);
211 rlen
= cmd
->cmd_len
- ISCSI_CDB_SIZE
;
213 BUG_ON(rlen
> sizeof(ecdb_ahdr
->ecdb
));
214 ahslength
= rlen
+ sizeof(ecdb_ahdr
->reserved
);
216 pad_len
= iscsi_padding(rlen
);
218 rc
= iscsi_add_hdr(task
, sizeof(ecdb_ahdr
->ahslength
) +
219 sizeof(ecdb_ahdr
->ahstype
) + ahslength
+ pad_len
);
224 memset(&ecdb_ahdr
->ecdb
[rlen
], 0, pad_len
);
226 ecdb_ahdr
->ahslength
= cpu_to_be16(ahslength
);
227 ecdb_ahdr
->ahstype
= ISCSI_AHSTYPE_CDB
;
228 ecdb_ahdr
->reserved
= 0;
229 memcpy(ecdb_ahdr
->ecdb
, cmd
->cmnd
+ ISCSI_CDB_SIZE
, rlen
);
231 ISCSI_DBG_SESSION(task
->conn
->session
,
232 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
233 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
234 "%u\n", cmd
->cmd_len
, rlen
, pad_len
, ahslength
,
239 static int iscsi_prep_bidi_ahs(struct iscsi_task
*task
)
241 struct scsi_cmnd
*sc
= task
->sc
;
242 struct iscsi_rlength_ahdr
*rlen_ahdr
;
245 rlen_ahdr
= iscsi_next_hdr(task
);
246 rc
= iscsi_add_hdr(task
, sizeof(*rlen_ahdr
));
250 rlen_ahdr
->ahslength
=
251 cpu_to_be16(sizeof(rlen_ahdr
->read_length
) +
252 sizeof(rlen_ahdr
->reserved
));
253 rlen_ahdr
->ahstype
= ISCSI_AHSTYPE_RLENGTH
;
254 rlen_ahdr
->reserved
= 0;
255 rlen_ahdr
->read_length
= cpu_to_be32(scsi_in(sc
)->length
);
257 ISCSI_DBG_SESSION(task
->conn
->session
,
258 "bidi-in rlen_ahdr->read_length(%d) "
259 "rlen_ahdr->ahslength(%d)\n",
260 be32_to_cpu(rlen_ahdr
->read_length
),
261 be16_to_cpu(rlen_ahdr
->ahslength
));
266 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
269 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
270 * fields like dlength or final based on how much data it sends
272 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task
*task
)
274 struct iscsi_conn
*conn
= task
->conn
;
275 struct iscsi_session
*session
= conn
->session
;
276 struct scsi_cmnd
*sc
= task
->sc
;
277 struct iscsi_cmd
*hdr
;
278 unsigned hdrlength
, cmd_len
;
282 if (conn
->session
->tt
->alloc_pdu
) {
283 rc
= conn
->session
->tt
->alloc_pdu(task
, ISCSI_OP_SCSI_CMD
);
287 hdr
= (struct iscsi_cmd
*) task
->hdr
;
289 memset(hdr
, 0, sizeof(*hdr
));
291 if (session
->tt
->parse_pdu_itt
)
292 hdr
->itt
= task
->hdr_itt
= itt
;
294 hdr
->itt
= task
->hdr_itt
= build_itt(task
->itt
,
295 task
->conn
->session
->age
);
297 rc
= iscsi_add_hdr(task
, sizeof(*hdr
));
300 hdr
->opcode
= ISCSI_OP_SCSI_CMD
;
301 hdr
->flags
= ISCSI_ATTR_SIMPLE
;
302 int_to_scsilun(sc
->device
->lun
, (struct scsi_lun
*)hdr
->lun
);
303 memcpy(task
->lun
, hdr
->lun
, sizeof(task
->lun
));
304 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
305 cmd_len
= sc
->cmd_len
;
306 if (cmd_len
< ISCSI_CDB_SIZE
)
307 memset(&hdr
->cdb
[cmd_len
], 0, ISCSI_CDB_SIZE
- cmd_len
);
308 else if (cmd_len
> ISCSI_CDB_SIZE
) {
309 rc
= iscsi_prep_ecdb_ahs(task
);
312 cmd_len
= ISCSI_CDB_SIZE
;
314 memcpy(hdr
->cdb
, sc
->cmnd
, cmd_len
);
317 if (scsi_bidi_cmnd(sc
)) {
318 hdr
->flags
|= ISCSI_FLAG_CMD_READ
;
319 rc
= iscsi_prep_bidi_ahs(task
);
323 if (sc
->sc_data_direction
== DMA_TO_DEVICE
) {
324 unsigned out_len
= scsi_out(sc
)->length
;
325 struct iscsi_r2t_info
*r2t
= &task
->unsol_r2t
;
327 hdr
->data_length
= cpu_to_be32(out_len
);
328 hdr
->flags
|= ISCSI_FLAG_CMD_WRITE
;
332 * imm_count bytes to be sent right after
335 * unsol_count bytes(as Data-Out) to be sent
336 * without R2T ack right after
339 * r2t data_length bytes to be sent via R2T ack's
341 * pad_count bytes to be sent as zero-padding
343 memset(r2t
, 0, sizeof(*r2t
));
345 if (session
->imm_data_en
) {
346 if (out_len
>= session
->first_burst
)
347 task
->imm_count
= min(session
->first_burst
,
348 conn
->max_xmit_dlength
);
350 task
->imm_count
= min(out_len
,
351 conn
->max_xmit_dlength
);
352 hton24(hdr
->dlength
, task
->imm_count
);
354 zero_data(hdr
->dlength
);
356 if (!session
->initial_r2t_en
) {
357 r2t
->data_length
= min(session
->first_burst
, out_len
) -
359 r2t
->data_offset
= task
->imm_count
;
360 r2t
->ttt
= cpu_to_be32(ISCSI_RESERVED_TAG
);
361 r2t
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
364 if (!task
->unsol_r2t
.data_length
)
365 /* No unsolicit Data-Out's */
366 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
368 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
369 zero_data(hdr
->dlength
);
370 hdr
->data_length
= cpu_to_be32(scsi_in(sc
)->length
);
372 if (sc
->sc_data_direction
== DMA_FROM_DEVICE
)
373 hdr
->flags
|= ISCSI_FLAG_CMD_READ
;
376 /* calculate size of additional header segments (AHSs) */
377 hdrlength
= task
->hdr_len
- sizeof(*hdr
);
379 WARN_ON(hdrlength
& (ISCSI_PAD_LEN
-1));
380 hdrlength
/= ISCSI_PAD_LEN
;
382 WARN_ON(hdrlength
>= 256);
383 hdr
->hlength
= hdrlength
& 0xFF;
385 if (session
->tt
->init_task
&& session
->tt
->init_task(task
))
388 task
->state
= ISCSI_TASK_RUNNING
;
389 hdr
->cmdsn
= task
->cmdsn
= cpu_to_be32(session
->cmdsn
);
392 conn
->scsicmd_pdus_cnt
++;
393 ISCSI_DBG_SESSION(session
, "iscsi prep [%s cid %d sc %p cdb 0x%x "
394 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
395 scsi_bidi_cmnd(sc
) ? "bidirectional" :
396 sc
->sc_data_direction
== DMA_TO_DEVICE
?
397 "write" : "read", conn
->id
, sc
, sc
->cmnd
[0],
398 task
->itt
, scsi_bufflen(sc
),
399 scsi_bidi_cmnd(sc
) ? scsi_in(sc
)->length
: 0,
401 session
->max_cmdsn
- session
->exp_cmdsn
+ 1);
406 * iscsi_free_task - free a task
407 * @task: iscsi cmd task
409 * Must be called with session lock.
410 * This function returns the scsi command to scsi-ml or cleans
411 * up mgmt tasks then returns the task to the pool.
413 static void iscsi_free_task(struct iscsi_task
*task
)
415 struct iscsi_conn
*conn
= task
->conn
;
416 struct iscsi_session
*session
= conn
->session
;
417 struct scsi_cmnd
*sc
= task
->sc
;
419 ISCSI_DBG_SESSION(session
, "freeing task itt 0x%x state %d sc %p\n",
420 task
->itt
, task
->state
, task
->sc
);
422 session
->tt
->cleanup_task(task
);
423 task
->state
= ISCSI_TASK_FREE
;
426 * login task is preallocated so do not free
428 if (conn
->login_task
== task
)
431 __kfifo_put(session
->cmdpool
.queue
, (void*)&task
, sizeof(void*));
435 /* SCSI eh reuses commands to verify us */
438 * queue command may call this to free the task, but
439 * not have setup the sc callback
446 void __iscsi_get_task(struct iscsi_task
*task
)
448 atomic_inc(&task
->refcount
);
450 EXPORT_SYMBOL_GPL(__iscsi_get_task
);
452 static void __iscsi_put_task(struct iscsi_task
*task
)
454 if (atomic_dec_and_test(&task
->refcount
))
455 iscsi_free_task(task
);
458 void iscsi_put_task(struct iscsi_task
*task
)
460 struct iscsi_session
*session
= task
->conn
->session
;
462 spin_lock_bh(&session
->lock
);
463 __iscsi_put_task(task
);
464 spin_unlock_bh(&session
->lock
);
466 EXPORT_SYMBOL_GPL(iscsi_put_task
);
469 * iscsi_complete_task - finish a task
470 * @task: iscsi cmd task
471 * @state: state to complete task with
473 * Must be called with session lock.
475 static void iscsi_complete_task(struct iscsi_task
*task
, int state
)
477 struct iscsi_conn
*conn
= task
->conn
;
479 ISCSI_DBG_SESSION(conn
->session
,
480 "complete task itt 0x%x state %d sc %p\n",
481 task
->itt
, task
->state
, task
->sc
);
482 if (task
->state
== ISCSI_TASK_COMPLETED
||
483 task
->state
== ISCSI_TASK_ABRT_TMF
||
484 task
->state
== ISCSI_TASK_ABRT_SESS_RECOV
)
486 WARN_ON_ONCE(task
->state
== ISCSI_TASK_FREE
);
489 if (!list_empty(&task
->running
))
490 list_del_init(&task
->running
);
492 if (conn
->task
== task
)
495 if (conn
->ping_task
== task
)
496 conn
->ping_task
= NULL
;
498 /* release get from queueing */
499 __iscsi_put_task(task
);
503 * session lock must be held and if not called for a task that is
504 * still pending or from the xmit thread, then xmit thread must
507 static void fail_scsi_task(struct iscsi_task
*task
, int err
)
509 struct iscsi_conn
*conn
= task
->conn
;
510 struct scsi_cmnd
*sc
;
514 * if a command completes and we get a successful tmf response
515 * we will hit this because the scsi eh abort code does not take
522 if (task
->state
== ISCSI_TASK_PENDING
) {
524 * cmd never made it to the xmit thread, so we should not count
525 * the cmd in the sequencing
527 conn
->session
->queued_cmdsn
--;
528 /* it was never sent so just complete like normal */
529 state
= ISCSI_TASK_COMPLETED
;
530 } else if (err
== DID_TRANSPORT_DISRUPTED
)
531 state
= ISCSI_TASK_ABRT_SESS_RECOV
;
533 state
= ISCSI_TASK_ABRT_TMF
;
535 sc
->result
= err
<< 16;
536 if (!scsi_bidi_cmnd(sc
))
537 scsi_set_resid(sc
, scsi_bufflen(sc
));
539 scsi_out(sc
)->resid
= scsi_out(sc
)->length
;
540 scsi_in(sc
)->resid
= scsi_in(sc
)->length
;
543 iscsi_complete_task(task
, state
);
546 static int iscsi_prep_mgmt_task(struct iscsi_conn
*conn
,
547 struct iscsi_task
*task
)
549 struct iscsi_session
*session
= conn
->session
;
550 struct iscsi_hdr
*hdr
= task
->hdr
;
551 struct iscsi_nopout
*nop
= (struct iscsi_nopout
*)hdr
;
553 if (conn
->session
->state
== ISCSI_STATE_LOGGING_OUT
)
556 if (hdr
->opcode
!= (ISCSI_OP_LOGIN
| ISCSI_OP_IMMEDIATE
) &&
557 hdr
->opcode
!= (ISCSI_OP_TEXT
| ISCSI_OP_IMMEDIATE
))
558 nop
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
560 * pre-format CmdSN for outgoing PDU.
562 nop
->cmdsn
= cpu_to_be32(session
->cmdsn
);
563 if (hdr
->itt
!= RESERVED_ITT
) {
565 * TODO: We always use immediate, so we never hit this.
566 * If we start to send tmfs or nops as non-immediate then
567 * we should start checking the cmdsn numbers for mgmt tasks.
569 if (conn
->c_stage
== ISCSI_CONN_STARTED
&&
570 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
571 session
->queued_cmdsn
++;
576 if (session
->tt
->init_task
&& session
->tt
->init_task(task
))
579 if ((hdr
->opcode
& ISCSI_OPCODE_MASK
) == ISCSI_OP_LOGOUT
)
580 session
->state
= ISCSI_STATE_LOGGING_OUT
;
582 task
->state
= ISCSI_TASK_RUNNING
;
583 ISCSI_DBG_SESSION(session
, "mgmtpdu [op 0x%x hdr->itt 0x%x "
584 "datalen %d]\n", hdr
->opcode
& ISCSI_OPCODE_MASK
,
585 hdr
->itt
, task
->data_count
);
589 static struct iscsi_task
*
590 __iscsi_conn_send_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
591 char *data
, uint32_t data_size
)
593 struct iscsi_session
*session
= conn
->session
;
594 struct iscsi_host
*ihost
= shost_priv(session
->host
);
595 struct iscsi_task
*task
;
598 if (session
->state
== ISCSI_STATE_TERMINATE
)
601 if (hdr
->opcode
== (ISCSI_OP_LOGIN
| ISCSI_OP_IMMEDIATE
) ||
602 hdr
->opcode
== (ISCSI_OP_TEXT
| ISCSI_OP_IMMEDIATE
))
604 * Login and Text are sent serially, in
605 * request-followed-by-response sequence.
606 * Same task can be used. Same ITT must be used.
607 * Note that login_task is preallocated at conn_create().
609 task
= conn
->login_task
;
611 if (session
->state
!= ISCSI_STATE_LOGGED_IN
)
614 BUG_ON(conn
->c_stage
== ISCSI_CONN_INITIAL_STAGE
);
615 BUG_ON(conn
->c_stage
== ISCSI_CONN_STOPPED
);
617 if (!__kfifo_get(session
->cmdpool
.queue
,
618 (void*)&task
, sizeof(void*)))
622 * released in complete pdu for task we expect a response for, and
623 * released by the lld when it has transmitted the task for
624 * pdus we do not expect a response for.
626 atomic_set(&task
->refcount
, 1);
629 INIT_LIST_HEAD(&task
->running
);
630 task
->state
= ISCSI_TASK_PENDING
;
633 memcpy(task
->data
, data
, data_size
);
634 task
->data_count
= data_size
;
636 task
->data_count
= 0;
638 if (conn
->session
->tt
->alloc_pdu
) {
639 if (conn
->session
->tt
->alloc_pdu(task
, hdr
->opcode
)) {
640 iscsi_conn_printk(KERN_ERR
, conn
, "Could not allocate "
641 "pdu for mgmt task.\n");
646 itt
= task
->hdr
->itt
;
647 task
->hdr_len
= sizeof(struct iscsi_hdr
);
648 memcpy(task
->hdr
, hdr
, sizeof(struct iscsi_hdr
));
650 if (hdr
->itt
!= RESERVED_ITT
) {
651 if (session
->tt
->parse_pdu_itt
)
652 task
->hdr
->itt
= itt
;
654 task
->hdr
->itt
= build_itt(task
->itt
,
655 task
->conn
->session
->age
);
659 if (iscsi_prep_mgmt_task(conn
, task
))
662 if (session
->tt
->xmit_task(task
))
665 list_add_tail(&task
->running
, &conn
->mgmtqueue
);
666 iscsi_conn_queue_work(conn
);
672 __iscsi_put_task(task
);
676 int iscsi_conn_send_pdu(struct iscsi_cls_conn
*cls_conn
, struct iscsi_hdr
*hdr
,
677 char *data
, uint32_t data_size
)
679 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
680 struct iscsi_session
*session
= conn
->session
;
683 spin_lock_bh(&session
->lock
);
684 if (!__iscsi_conn_send_pdu(conn
, hdr
, data
, data_size
))
686 spin_unlock_bh(&session
->lock
);
689 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu
);
692 * iscsi_cmd_rsp - SCSI Command Response processing
693 * @conn: iscsi connection
695 * @task: scsi command task
696 * @data: cmd data buffer
697 * @datalen: len of buffer
699 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
700 * then completes the command and task.
702 static void iscsi_scsi_cmd_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
703 struct iscsi_task
*task
, char *data
,
706 struct iscsi_cmd_rsp
*rhdr
= (struct iscsi_cmd_rsp
*)hdr
;
707 struct iscsi_session
*session
= conn
->session
;
708 struct scsi_cmnd
*sc
= task
->sc
;
710 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)rhdr
);
711 conn
->exp_statsn
= be32_to_cpu(rhdr
->statsn
) + 1;
713 sc
->result
= (DID_OK
<< 16) | rhdr
->cmd_status
;
715 if (rhdr
->response
!= ISCSI_STATUS_CMD_COMPLETED
) {
716 sc
->result
= DID_ERROR
<< 16;
720 if (rhdr
->cmd_status
== SAM_STAT_CHECK_CONDITION
) {
725 iscsi_conn_printk(KERN_ERR
, conn
,
726 "Got CHECK_CONDITION but invalid data "
727 "buffer size of %d\n", datalen
);
728 sc
->result
= DID_BAD_TARGET
<< 16;
732 senselen
= get_unaligned_be16(data
);
733 if (datalen
< senselen
)
734 goto invalid_datalen
;
736 memcpy(sc
->sense_buffer
, data
+ 2,
737 min_t(uint16_t, senselen
, SCSI_SENSE_BUFFERSIZE
));
738 ISCSI_DBG_SESSION(session
, "copied %d bytes of sense\n",
739 min_t(uint16_t, senselen
,
740 SCSI_SENSE_BUFFERSIZE
));
743 if (rhdr
->flags
& (ISCSI_FLAG_CMD_BIDI_UNDERFLOW
|
744 ISCSI_FLAG_CMD_BIDI_OVERFLOW
)) {
745 int res_count
= be32_to_cpu(rhdr
->bi_residual_count
);
747 if (scsi_bidi_cmnd(sc
) && res_count
> 0 &&
748 (rhdr
->flags
& ISCSI_FLAG_CMD_BIDI_OVERFLOW
||
749 res_count
<= scsi_in(sc
)->length
))
750 scsi_in(sc
)->resid
= res_count
;
752 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
755 if (rhdr
->flags
& (ISCSI_FLAG_CMD_UNDERFLOW
|
756 ISCSI_FLAG_CMD_OVERFLOW
)) {
757 int res_count
= be32_to_cpu(rhdr
->residual_count
);
760 (rhdr
->flags
& ISCSI_FLAG_CMD_OVERFLOW
||
761 res_count
<= scsi_bufflen(sc
)))
762 /* write side for bidi or uni-io set_resid */
763 scsi_set_resid(sc
, res_count
);
765 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
768 ISCSI_DBG_SESSION(session
, "cmd rsp done [sc %p res %d itt 0x%x]\n",
769 sc
, sc
->result
, task
->itt
);
770 conn
->scsirsp_pdus_cnt
++;
771 iscsi_complete_task(task
, ISCSI_TASK_COMPLETED
);
775 * iscsi_data_in_rsp - SCSI Data-In Response processing
776 * @conn: iscsi connection
778 * @task: scsi command task
781 iscsi_data_in_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
782 struct iscsi_task
*task
)
784 struct iscsi_data_rsp
*rhdr
= (struct iscsi_data_rsp
*)hdr
;
785 struct scsi_cmnd
*sc
= task
->sc
;
787 if (!(rhdr
->flags
& ISCSI_FLAG_DATA_STATUS
))
790 iscsi_update_cmdsn(conn
->session
, (struct iscsi_nopin
*)hdr
);
791 sc
->result
= (DID_OK
<< 16) | rhdr
->cmd_status
;
792 conn
->exp_statsn
= be32_to_cpu(rhdr
->statsn
) + 1;
793 if (rhdr
->flags
& (ISCSI_FLAG_DATA_UNDERFLOW
|
794 ISCSI_FLAG_DATA_OVERFLOW
)) {
795 int res_count
= be32_to_cpu(rhdr
->residual_count
);
798 (rhdr
->flags
& ISCSI_FLAG_CMD_OVERFLOW
||
799 res_count
<= scsi_in(sc
)->length
))
800 scsi_in(sc
)->resid
= res_count
;
802 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
805 ISCSI_DBG_SESSION(conn
->session
, "data in with status done "
806 "[sc %p res %d itt 0x%x]\n",
807 sc
, sc
->result
, task
->itt
);
808 conn
->scsirsp_pdus_cnt
++;
809 iscsi_complete_task(task
, ISCSI_TASK_COMPLETED
);
812 static void iscsi_tmf_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
)
814 struct iscsi_tm_rsp
*tmf
= (struct iscsi_tm_rsp
*)hdr
;
816 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
817 conn
->tmfrsp_pdus_cnt
++;
819 if (conn
->tmf_state
!= TMF_QUEUED
)
822 if (tmf
->response
== ISCSI_TMF_RSP_COMPLETE
)
823 conn
->tmf_state
= TMF_SUCCESS
;
824 else if (tmf
->response
== ISCSI_TMF_RSP_NO_TASK
)
825 conn
->tmf_state
= TMF_NOT_FOUND
;
827 conn
->tmf_state
= TMF_FAILED
;
828 wake_up(&conn
->ehwait
);
831 static void iscsi_send_nopout(struct iscsi_conn
*conn
, struct iscsi_nopin
*rhdr
)
833 struct iscsi_nopout hdr
;
834 struct iscsi_task
*task
;
836 if (!rhdr
&& conn
->ping_task
)
839 memset(&hdr
, 0, sizeof(struct iscsi_nopout
));
840 hdr
.opcode
= ISCSI_OP_NOOP_OUT
| ISCSI_OP_IMMEDIATE
;
841 hdr
.flags
= ISCSI_FLAG_CMD_FINAL
;
844 memcpy(hdr
.lun
, rhdr
->lun
, 8);
846 hdr
.itt
= RESERVED_ITT
;
848 hdr
.ttt
= RESERVED_ITT
;
850 task
= __iscsi_conn_send_pdu(conn
, (struct iscsi_hdr
*)&hdr
, NULL
, 0);
852 iscsi_conn_printk(KERN_ERR
, conn
, "Could not send nopout\n");
854 /* only track our nops */
855 conn
->ping_task
= task
;
856 conn
->last_ping
= jiffies
;
860 static int iscsi_nop_out_rsp(struct iscsi_task
*task
,
861 struct iscsi_nopin
*nop
, char *data
, int datalen
)
863 struct iscsi_conn
*conn
= task
->conn
;
866 if (conn
->ping_task
!= task
) {
868 * If this is not in response to one of our
869 * nops then it must be from userspace.
871 if (iscsi_recv_pdu(conn
->cls_conn
, (struct iscsi_hdr
*)nop
,
873 rc
= ISCSI_ERR_CONN_FAILED
;
875 mod_timer(&conn
->transport_timer
, jiffies
+ conn
->recv_timeout
);
876 iscsi_complete_task(task
, ISCSI_TASK_COMPLETED
);
880 static int iscsi_handle_reject(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
881 char *data
, int datalen
)
883 struct iscsi_reject
*reject
= (struct iscsi_reject
*)hdr
;
884 struct iscsi_hdr rejected_pdu
;
887 conn
->exp_statsn
= be32_to_cpu(reject
->statsn
) + 1;
889 if (ntoh24(reject
->dlength
) > datalen
||
890 ntoh24(reject
->dlength
) < sizeof(struct iscsi_hdr
)) {
891 iscsi_conn_printk(KERN_ERR
, conn
, "Cannot handle rejected "
892 "pdu. Invalid data length (pdu dlength "
893 "%u, datalen %d\n", ntoh24(reject
->dlength
),
895 return ISCSI_ERR_PROTO
;
897 memcpy(&rejected_pdu
, data
, sizeof(struct iscsi_hdr
));
898 opcode
= rejected_pdu
.opcode
& ISCSI_OPCODE_MASK
;
900 switch (reject
->reason
) {
901 case ISCSI_REASON_DATA_DIGEST_ERROR
:
902 iscsi_conn_printk(KERN_ERR
, conn
,
903 "pdu (op 0x%x itt 0x%x) rejected "
904 "due to DataDigest error.\n",
905 rejected_pdu
.itt
, opcode
);
907 case ISCSI_REASON_IMM_CMD_REJECT
:
908 iscsi_conn_printk(KERN_ERR
, conn
,
909 "pdu (op 0x%x itt 0x%x) rejected. Too many "
910 "immediate commands.\n",
911 rejected_pdu
.itt
, opcode
);
913 * We only send one TMF at a time so if the target could not
914 * handle it, then it should get fixed (RFC mandates that
915 * a target can handle one immediate TMF per conn).
917 * For nops-outs, we could have sent more than one if
918 * the target is sending us lots of nop-ins
920 if (opcode
!= ISCSI_OP_NOOP_OUT
)
923 if (rejected_pdu
.itt
== cpu_to_be32(ISCSI_RESERVED_TAG
))
925 * nop-out in response to target's nop-out rejected.
928 iscsi_send_nopout(conn
,
929 (struct iscsi_nopin
*)&rejected_pdu
);
931 struct iscsi_task
*task
;
933 * Our nop as ping got dropped. We know the target
934 * and transport are ok so just clean up
936 task
= iscsi_itt_to_task(conn
, rejected_pdu
.itt
);
938 iscsi_conn_printk(KERN_ERR
, conn
,
939 "Invalid pdu reject. Could "
940 "not lookup rejected task.\n");
941 rc
= ISCSI_ERR_BAD_ITT
;
943 rc
= iscsi_nop_out_rsp(task
,
944 (struct iscsi_nopin
*)&rejected_pdu
,
949 iscsi_conn_printk(KERN_ERR
, conn
,
950 "pdu (op 0x%x itt 0x%x) rejected. Reason "
951 "code 0x%x\n", rejected_pdu
.itt
,
952 rejected_pdu
.opcode
, reject
->reason
);
959 * iscsi_itt_to_task - look up task by itt
960 * @conn: iscsi connection
963 * This should be used for mgmt tasks like login and nops, or if
964 * the LDD's itt space does not include the session age.
966 * The session lock must be held.
968 struct iscsi_task
*iscsi_itt_to_task(struct iscsi_conn
*conn
, itt_t itt
)
970 struct iscsi_session
*session
= conn
->session
;
973 if (itt
== RESERVED_ITT
)
976 if (session
->tt
->parse_pdu_itt
)
977 session
->tt
->parse_pdu_itt(conn
, itt
, &i
, NULL
);
980 if (i
>= session
->cmds_max
)
983 return session
->cmds
[i
];
985 EXPORT_SYMBOL_GPL(iscsi_itt_to_task
);
988 * __iscsi_complete_pdu - complete pdu
992 * @datalen: len of data buffer
994 * Completes pdu processing by freeing any resources allocated at
995 * queuecommand or send generic. session lock must be held and verify
996 * itt must have been called.
998 int __iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
999 char *data
, int datalen
)
1001 struct iscsi_session
*session
= conn
->session
;
1002 int opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
, rc
= 0;
1003 struct iscsi_task
*task
;
1006 conn
->last_recv
= jiffies
;
1007 rc
= iscsi_verify_itt(conn
, hdr
->itt
);
1011 if (hdr
->itt
!= RESERVED_ITT
)
1012 itt
= get_itt(hdr
->itt
);
1016 ISCSI_DBG_SESSION(session
, "[op 0x%x cid %d itt 0x%x len %d]\n",
1017 opcode
, conn
->id
, itt
, datalen
);
1020 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
1023 case ISCSI_OP_NOOP_IN
:
1025 rc
= ISCSI_ERR_PROTO
;
1029 if (hdr
->ttt
== cpu_to_be32(ISCSI_RESERVED_TAG
))
1032 iscsi_send_nopout(conn
, (struct iscsi_nopin
*)hdr
);
1034 case ISCSI_OP_REJECT
:
1035 rc
= iscsi_handle_reject(conn
, hdr
, data
, datalen
);
1037 case ISCSI_OP_ASYNC_EVENT
:
1038 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
1039 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
1040 rc
= ISCSI_ERR_CONN_FAILED
;
1043 rc
= ISCSI_ERR_BAD_OPCODE
;
1050 case ISCSI_OP_SCSI_CMD_RSP
:
1051 case ISCSI_OP_SCSI_DATA_IN
:
1052 task
= iscsi_itt_to_ctask(conn
, hdr
->itt
);
1054 return ISCSI_ERR_BAD_ITT
;
1055 task
->last_xfer
= jiffies
;
1059 * LLD handles R2Ts if they need to.
1062 case ISCSI_OP_LOGOUT_RSP
:
1063 case ISCSI_OP_LOGIN_RSP
:
1064 case ISCSI_OP_TEXT_RSP
:
1065 case ISCSI_OP_SCSI_TMFUNC_RSP
:
1066 case ISCSI_OP_NOOP_IN
:
1067 task
= iscsi_itt_to_task(conn
, hdr
->itt
);
1069 return ISCSI_ERR_BAD_ITT
;
1072 return ISCSI_ERR_BAD_OPCODE
;
1076 case ISCSI_OP_SCSI_CMD_RSP
:
1077 iscsi_scsi_cmd_rsp(conn
, hdr
, task
, data
, datalen
);
1079 case ISCSI_OP_SCSI_DATA_IN
:
1080 iscsi_data_in_rsp(conn
, hdr
, task
);
1082 case ISCSI_OP_LOGOUT_RSP
:
1083 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
1085 rc
= ISCSI_ERR_PROTO
;
1088 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
1090 case ISCSI_OP_LOGIN_RSP
:
1091 case ISCSI_OP_TEXT_RSP
:
1092 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
1094 * login related PDU's exp_statsn is handled in
1098 case ISCSI_OP_SCSI_TMFUNC_RSP
:
1099 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
1101 rc
= ISCSI_ERR_PROTO
;
1105 iscsi_tmf_rsp(conn
, hdr
);
1106 iscsi_complete_task(task
, ISCSI_TASK_COMPLETED
);
1108 case ISCSI_OP_NOOP_IN
:
1109 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
1110 if (hdr
->ttt
!= cpu_to_be32(ISCSI_RESERVED_TAG
) || datalen
) {
1111 rc
= ISCSI_ERR_PROTO
;
1114 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
1116 rc
= iscsi_nop_out_rsp(task
, (struct iscsi_nopin
*)hdr
,
1120 rc
= ISCSI_ERR_BAD_OPCODE
;
1127 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
1128 rc
= ISCSI_ERR_CONN_FAILED
;
1129 iscsi_complete_task(task
, ISCSI_TASK_COMPLETED
);
1132 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu
);
1134 int iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
1135 char *data
, int datalen
)
1139 spin_lock(&conn
->session
->lock
);
1140 rc
= __iscsi_complete_pdu(conn
, hdr
, data
, datalen
);
1141 spin_unlock(&conn
->session
->lock
);
1144 EXPORT_SYMBOL_GPL(iscsi_complete_pdu
);
1146 int iscsi_verify_itt(struct iscsi_conn
*conn
, itt_t itt
)
1148 struct iscsi_session
*session
= conn
->session
;
1151 if (itt
== RESERVED_ITT
)
1154 if (session
->tt
->parse_pdu_itt
)
1155 session
->tt
->parse_pdu_itt(conn
, itt
, &i
, &age
);
1158 age
= ((__force u32
)itt
>> ISCSI_AGE_SHIFT
) & ISCSI_AGE_MASK
;
1161 if (age
!= session
->age
) {
1162 iscsi_conn_printk(KERN_ERR
, conn
,
1163 "received itt %x expected session age (%x)\n",
1164 (__force u32
)itt
, session
->age
);
1165 return ISCSI_ERR_BAD_ITT
;
1168 if (i
>= session
->cmds_max
) {
1169 iscsi_conn_printk(KERN_ERR
, conn
,
1170 "received invalid itt index %u (max cmds "
1171 "%u.\n", i
, session
->cmds_max
);
1172 return ISCSI_ERR_BAD_ITT
;
1176 EXPORT_SYMBOL_GPL(iscsi_verify_itt
);
1179 * iscsi_itt_to_ctask - look up ctask by itt
1180 * @conn: iscsi connection
1183 * This should be used for cmd tasks.
1185 * The session lock must be held.
1187 struct iscsi_task
*iscsi_itt_to_ctask(struct iscsi_conn
*conn
, itt_t itt
)
1189 struct iscsi_task
*task
;
1191 if (iscsi_verify_itt(conn
, itt
))
1194 task
= iscsi_itt_to_task(conn
, itt
);
1195 if (!task
|| !task
->sc
)
1198 if (task
->sc
->SCp
.phase
!= conn
->session
->age
) {
1199 iscsi_session_printk(KERN_ERR
, conn
->session
,
1200 "task's session age %d, expected %d\n",
1201 task
->sc
->SCp
.phase
, conn
->session
->age
);
1207 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask
);
1209 void iscsi_session_failure(struct iscsi_session
*session
,
1212 struct iscsi_conn
*conn
;
1214 unsigned long flags
;
1216 spin_lock_irqsave(&session
->lock
, flags
);
1217 conn
= session
->leadconn
;
1218 if (session
->state
== ISCSI_STATE_TERMINATE
|| !conn
) {
1219 spin_unlock_irqrestore(&session
->lock
, flags
);
1223 dev
= get_device(&conn
->cls_conn
->dev
);
1224 spin_unlock_irqrestore(&session
->lock
, flags
);
1228 * if the host is being removed bypass the connection
1229 * recovery initialization because we are going to kill
1232 if (err
== ISCSI_ERR_INVALID_HOST
)
1233 iscsi_conn_error_event(conn
->cls_conn
, err
);
1235 iscsi_conn_failure(conn
, err
);
1238 EXPORT_SYMBOL_GPL(iscsi_session_failure
);
1240 void iscsi_conn_failure(struct iscsi_conn
*conn
, enum iscsi_err err
)
1242 struct iscsi_session
*session
= conn
->session
;
1243 unsigned long flags
;
1245 spin_lock_irqsave(&session
->lock
, flags
);
1246 if (session
->state
== ISCSI_STATE_FAILED
) {
1247 spin_unlock_irqrestore(&session
->lock
, flags
);
1251 if (conn
->stop_stage
== 0)
1252 session
->state
= ISCSI_STATE_FAILED
;
1253 spin_unlock_irqrestore(&session
->lock
, flags
);
1255 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1256 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
1257 iscsi_conn_error_event(conn
->cls_conn
, err
);
1259 EXPORT_SYMBOL_GPL(iscsi_conn_failure
);
1261 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn
*conn
)
1263 struct iscsi_session
*session
= conn
->session
;
1266 * Check for iSCSI window and take care of CmdSN wrap-around
1268 if (!iscsi_sna_lte(session
->queued_cmdsn
, session
->max_cmdsn
)) {
1269 ISCSI_DBG_SESSION(session
, "iSCSI CmdSN closed. ExpCmdSn "
1270 "%u MaxCmdSN %u CmdSN %u/%u\n",
1271 session
->exp_cmdsn
, session
->max_cmdsn
,
1272 session
->cmdsn
, session
->queued_cmdsn
);
1278 static int iscsi_xmit_task(struct iscsi_conn
*conn
)
1280 struct iscsi_task
*task
= conn
->task
;
1283 __iscsi_get_task(task
);
1284 spin_unlock_bh(&conn
->session
->lock
);
1285 rc
= conn
->session
->tt
->xmit_task(task
);
1286 spin_lock_bh(&conn
->session
->lock
);
1288 /* done with this task */
1289 task
->last_xfer
= jiffies
;
1292 __iscsi_put_task(task
);
1297 * iscsi_requeue_task - requeue task to run from session workqueue
1298 * @task: task to requeue
1300 * LLDs that need to run a task from the session workqueue should call
1301 * this. The session lock must be held. This should only be called
1302 * by software drivers.
1304 void iscsi_requeue_task(struct iscsi_task
*task
)
1306 struct iscsi_conn
*conn
= task
->conn
;
1309 * this may be on the requeue list already if the xmit_task callout
1310 * is handling the r2ts while we are adding new ones
1312 if (list_empty(&task
->running
))
1313 list_add_tail(&task
->running
, &conn
->requeue
);
1314 iscsi_conn_queue_work(conn
);
1316 EXPORT_SYMBOL_GPL(iscsi_requeue_task
);
1319 * iscsi_data_xmit - xmit any command into the scheduled connection
1320 * @conn: iscsi connection
1323 * The function can return -EAGAIN in which case the caller must
1324 * re-schedule it again later or recover. '0' return code means
1327 static int iscsi_data_xmit(struct iscsi_conn
*conn
)
1331 spin_lock_bh(&conn
->session
->lock
);
1332 if (unlikely(conn
->suspend_tx
)) {
1333 ISCSI_DBG_SESSION(conn
->session
, "Tx suspended!\n");
1334 spin_unlock_bh(&conn
->session
->lock
);
1339 rc
= iscsi_xmit_task(conn
);
1345 * process mgmt pdus like nops before commands since we should
1346 * only have one nop-out as a ping from us and targets should not
1347 * overflow us with nop-ins
1350 while (!list_empty(&conn
->mgmtqueue
)) {
1351 conn
->task
= list_entry(conn
->mgmtqueue
.next
,
1352 struct iscsi_task
, running
);
1353 list_del_init(&conn
->task
->running
);
1354 if (iscsi_prep_mgmt_task(conn
, conn
->task
)) {
1355 __iscsi_put_task(conn
->task
);
1359 rc
= iscsi_xmit_task(conn
);
1364 /* process pending command queue */
1365 while (!list_empty(&conn
->cmdqueue
)) {
1366 if (conn
->tmf_state
== TMF_QUEUED
)
1369 conn
->task
= list_entry(conn
->cmdqueue
.next
,
1370 struct iscsi_task
, running
);
1371 list_del_init(&conn
->task
->running
);
1372 if (conn
->session
->state
== ISCSI_STATE_LOGGING_OUT
) {
1373 fail_scsi_task(conn
->task
, DID_IMM_RETRY
);
1376 rc
= iscsi_prep_scsi_cmd_pdu(conn
->task
);
1378 if (rc
== -ENOMEM
) {
1379 list_add_tail(&conn
->task
->running
,
1384 fail_scsi_task(conn
->task
, DID_ABORT
);
1387 rc
= iscsi_xmit_task(conn
);
1391 * we could continuously get new task requests so
1392 * we need to check the mgmt queue for nops that need to
1393 * be sent to aviod starvation
1395 if (!list_empty(&conn
->mgmtqueue
))
1399 while (!list_empty(&conn
->requeue
)) {
1400 if (conn
->session
->fast_abort
&& conn
->tmf_state
!= TMF_INITIAL
)
1404 * we always do fastlogout - conn stop code will clean up.
1406 if (conn
->session
->state
== ISCSI_STATE_LOGGING_OUT
)
1409 conn
->task
= list_entry(conn
->requeue
.next
,
1410 struct iscsi_task
, running
);
1411 list_del_init(&conn
->task
->running
);
1412 conn
->task
->state
= ISCSI_TASK_RUNNING
;
1413 rc
= iscsi_xmit_task(conn
);
1416 if (!list_empty(&conn
->mgmtqueue
))
1419 spin_unlock_bh(&conn
->session
->lock
);
1423 if (unlikely(conn
->suspend_tx
))
1425 spin_unlock_bh(&conn
->session
->lock
);
1429 static void iscsi_xmitworker(struct work_struct
*work
)
1431 struct iscsi_conn
*conn
=
1432 container_of(work
, struct iscsi_conn
, xmitwork
);
1435 * serialize Xmit worker on a per-connection basis.
1438 rc
= iscsi_data_xmit(conn
);
1439 } while (rc
>= 0 || rc
== -EAGAIN
);
1442 static inline struct iscsi_task
*iscsi_alloc_task(struct iscsi_conn
*conn
,
1443 struct scsi_cmnd
*sc
)
1445 struct iscsi_task
*task
;
1447 if (!__kfifo_get(conn
->session
->cmdpool
.queue
,
1448 (void *) &task
, sizeof(void *)))
1451 sc
->SCp
.phase
= conn
->session
->age
;
1452 sc
->SCp
.ptr
= (char *) task
;
1454 atomic_set(&task
->refcount
, 1);
1455 task
->state
= ISCSI_TASK_PENDING
;
1458 task
->have_checked_conn
= false;
1459 task
->last_timeout
= jiffies
;
1460 task
->last_xfer
= jiffies
;
1461 INIT_LIST_HEAD(&task
->running
);
1466 FAILURE_BAD_HOST
= 1,
1467 FAILURE_SESSION_FAILED
,
1468 FAILURE_SESSION_FREED
,
1469 FAILURE_WINDOW_CLOSED
,
1471 FAILURE_SESSION_TERMINATE
,
1472 FAILURE_SESSION_IN_RECOVERY
,
1473 FAILURE_SESSION_RECOVERY_TIMEOUT
,
1474 FAILURE_SESSION_LOGGING_OUT
,
1475 FAILURE_SESSION_NOT_READY
,
1478 int iscsi_queuecommand(struct scsi_cmnd
*sc
, void (*done
)(struct scsi_cmnd
*))
1480 struct iscsi_cls_session
*cls_session
;
1481 struct Scsi_Host
*host
;
1482 struct iscsi_host
*ihost
;
1484 struct iscsi_session
*session
;
1485 struct iscsi_conn
*conn
;
1486 struct iscsi_task
*task
= NULL
;
1488 sc
->scsi_done
= done
;
1492 host
= sc
->device
->host
;
1493 ihost
= shost_priv(host
);
1494 spin_unlock(host
->host_lock
);
1496 cls_session
= starget_to_session(scsi_target(sc
->device
));
1497 session
= cls_session
->dd_data
;
1498 spin_lock(&session
->lock
);
1500 reason
= iscsi_session_chkready(cls_session
);
1502 sc
->result
= reason
;
1506 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
1508 * to handle the race between when we set the recovery state
1509 * and block the session we requeue here (commands could
1510 * be entering our queuecommand while a block is starting
1511 * up because the block code is not locked)
1513 switch (session
->state
) {
1514 case ISCSI_STATE_FAILED
:
1515 case ISCSI_STATE_IN_RECOVERY
:
1516 reason
= FAILURE_SESSION_IN_RECOVERY
;
1517 sc
->result
= DID_IMM_RETRY
<< 16;
1519 case ISCSI_STATE_LOGGING_OUT
:
1520 reason
= FAILURE_SESSION_LOGGING_OUT
;
1521 sc
->result
= DID_IMM_RETRY
<< 16;
1523 case ISCSI_STATE_RECOVERY_FAILED
:
1524 reason
= FAILURE_SESSION_RECOVERY_TIMEOUT
;
1525 sc
->result
= DID_TRANSPORT_FAILFAST
<< 16;
1527 case ISCSI_STATE_TERMINATE
:
1528 reason
= FAILURE_SESSION_TERMINATE
;
1529 sc
->result
= DID_NO_CONNECT
<< 16;
1532 reason
= FAILURE_SESSION_FREED
;
1533 sc
->result
= DID_NO_CONNECT
<< 16;
1538 conn
= session
->leadconn
;
1540 reason
= FAILURE_SESSION_FREED
;
1541 sc
->result
= DID_NO_CONNECT
<< 16;
1545 if (iscsi_check_cmdsn_window_closed(conn
)) {
1546 reason
= FAILURE_WINDOW_CLOSED
;
1550 task
= iscsi_alloc_task(conn
, sc
);
1552 reason
= FAILURE_OOM
;
1556 if (!ihost
->workq
) {
1557 reason
= iscsi_prep_scsi_cmd_pdu(task
);
1559 if (reason
== -ENOMEM
) {
1560 reason
= FAILURE_OOM
;
1563 sc
->result
= DID_ABORT
<< 16;
1567 if (session
->tt
->xmit_task(task
)) {
1569 reason
= FAILURE_SESSION_NOT_READY
;
1573 list_add_tail(&task
->running
, &conn
->cmdqueue
);
1574 iscsi_conn_queue_work(conn
);
1577 session
->queued_cmdsn
++;
1578 spin_unlock(&session
->lock
);
1579 spin_lock(host
->host_lock
);
1583 sc
->scsi_done
= NULL
;
1584 iscsi_complete_task(task
, ISCSI_TASK_COMPLETED
);
1586 spin_unlock(&session
->lock
);
1587 ISCSI_DBG_SESSION(session
, "cmd 0x%x rejected (%d)\n",
1588 sc
->cmnd
[0], reason
);
1589 spin_lock(host
->host_lock
);
1590 return SCSI_MLQUEUE_TARGET_BUSY
;
1593 sc
->scsi_done
= NULL
;
1594 iscsi_complete_task(task
, ISCSI_TASK_COMPLETED
);
1596 spin_unlock(&session
->lock
);
1597 ISCSI_DBG_SESSION(session
, "iscsi: cmd 0x%x is not queued (%d)\n",
1598 sc
->cmnd
[0], reason
);
1599 if (!scsi_bidi_cmnd(sc
))
1600 scsi_set_resid(sc
, scsi_bufflen(sc
));
1602 scsi_out(sc
)->resid
= scsi_out(sc
)->length
;
1603 scsi_in(sc
)->resid
= scsi_in(sc
)->length
;
1606 spin_lock(host
->host_lock
);
1609 EXPORT_SYMBOL_GPL(iscsi_queuecommand
);
1611 int iscsi_change_queue_depth(struct scsi_device
*sdev
, int depth
)
1613 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), depth
);
1614 return sdev
->queue_depth
;
1616 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth
);
1618 int iscsi_target_alloc(struct scsi_target
*starget
)
1620 struct iscsi_cls_session
*cls_session
= starget_to_session(starget
);
1621 struct iscsi_session
*session
= cls_session
->dd_data
;
1623 starget
->can_queue
= session
->scsi_cmds_max
;
1626 EXPORT_SYMBOL_GPL(iscsi_target_alloc
);
1628 void iscsi_session_recovery_timedout(struct iscsi_cls_session
*cls_session
)
1630 struct iscsi_session
*session
= cls_session
->dd_data
;
1632 spin_lock_bh(&session
->lock
);
1633 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
1634 session
->state
= ISCSI_STATE_RECOVERY_FAILED
;
1635 if (session
->leadconn
)
1636 wake_up(&session
->leadconn
->ehwait
);
1638 spin_unlock_bh(&session
->lock
);
1640 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout
);
1642 int iscsi_eh_target_reset(struct scsi_cmnd
*sc
)
1644 struct iscsi_cls_session
*cls_session
;
1645 struct iscsi_session
*session
;
1646 struct iscsi_conn
*conn
;
1648 cls_session
= starget_to_session(scsi_target(sc
->device
));
1649 session
= cls_session
->dd_data
;
1650 conn
= session
->leadconn
;
1652 mutex_lock(&session
->eh_mutex
);
1653 spin_lock_bh(&session
->lock
);
1654 if (session
->state
== ISCSI_STATE_TERMINATE
) {
1656 ISCSI_DBG_EH(session
,
1657 "failing target reset: Could not log back into "
1658 "target [age %d]\n",
1660 spin_unlock_bh(&session
->lock
);
1661 mutex_unlock(&session
->eh_mutex
);
1665 spin_unlock_bh(&session
->lock
);
1666 mutex_unlock(&session
->eh_mutex
);
1668 * we drop the lock here but the leadconn cannot be destoyed while
1669 * we are in the scsi eh
1671 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1673 ISCSI_DBG_EH(session
, "wait for relogin\n");
1674 wait_event_interruptible(conn
->ehwait
,
1675 session
->state
== ISCSI_STATE_TERMINATE
||
1676 session
->state
== ISCSI_STATE_LOGGED_IN
||
1677 session
->state
== ISCSI_STATE_RECOVERY_FAILED
);
1678 if (signal_pending(current
))
1679 flush_signals(current
);
1681 mutex_lock(&session
->eh_mutex
);
1682 spin_lock_bh(&session
->lock
);
1683 if (session
->state
== ISCSI_STATE_LOGGED_IN
) {
1684 ISCSI_DBG_EH(session
,
1685 "target reset succeeded\n");
1688 spin_unlock_bh(&session
->lock
);
1689 mutex_unlock(&session
->eh_mutex
);
1692 EXPORT_SYMBOL_GPL(iscsi_eh_target_reset
);
1694 static void iscsi_tmf_timedout(unsigned long data
)
1696 struct iscsi_conn
*conn
= (struct iscsi_conn
*)data
;
1697 struct iscsi_session
*session
= conn
->session
;
1699 spin_lock(&session
->lock
);
1700 if (conn
->tmf_state
== TMF_QUEUED
) {
1701 conn
->tmf_state
= TMF_TIMEDOUT
;
1702 ISCSI_DBG_EH(session
, "tmf timedout\n");
1703 /* unblock eh_abort() */
1704 wake_up(&conn
->ehwait
);
1706 spin_unlock(&session
->lock
);
1709 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn
*conn
,
1710 struct iscsi_tm
*hdr
, int age
,
1713 struct iscsi_session
*session
= conn
->session
;
1714 struct iscsi_task
*task
;
1716 task
= __iscsi_conn_send_pdu(conn
, (struct iscsi_hdr
*)hdr
,
1719 spin_unlock_bh(&session
->lock
);
1720 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1721 spin_lock_bh(&session
->lock
);
1722 ISCSI_DBG_EH(session
, "tmf exec failure\n");
1725 conn
->tmfcmd_pdus_cnt
++;
1726 conn
->tmf_timer
.expires
= timeout
* HZ
+ jiffies
;
1727 conn
->tmf_timer
.function
= iscsi_tmf_timedout
;
1728 conn
->tmf_timer
.data
= (unsigned long)conn
;
1729 add_timer(&conn
->tmf_timer
);
1730 ISCSI_DBG_EH(session
, "tmf set timeout\n");
1732 spin_unlock_bh(&session
->lock
);
1733 mutex_unlock(&session
->eh_mutex
);
1736 * block eh thread until:
1740 * 3) session is terminated or restarted or userspace has
1741 * given up on recovery
1743 wait_event_interruptible(conn
->ehwait
, age
!= session
->age
||
1744 session
->state
!= ISCSI_STATE_LOGGED_IN
||
1745 conn
->tmf_state
!= TMF_QUEUED
);
1746 if (signal_pending(current
))
1747 flush_signals(current
);
1748 del_timer_sync(&conn
->tmf_timer
);
1750 mutex_lock(&session
->eh_mutex
);
1751 spin_lock_bh(&session
->lock
);
1752 /* if the session drops it will clean up the task */
1753 if (age
!= session
->age
||
1754 session
->state
!= ISCSI_STATE_LOGGED_IN
)
1760 * Fail commands. session lock held and recv side suspended and xmit
1763 static void fail_scsi_tasks(struct iscsi_conn
*conn
, unsigned lun
,
1766 struct iscsi_task
*task
;
1769 for (i
= 0; i
< conn
->session
->cmds_max
; i
++) {
1770 task
= conn
->session
->cmds
[i
];
1771 if (!task
->sc
|| task
->state
== ISCSI_TASK_FREE
)
1774 if (lun
!= -1 && lun
!= task
->sc
->device
->lun
)
1777 ISCSI_DBG_SESSION(conn
->session
,
1778 "failing sc %p itt 0x%x state %d\n",
1779 task
->sc
, task
->itt
, task
->state
);
1780 fail_scsi_task(task
, error
);
1784 void iscsi_suspend_tx(struct iscsi_conn
*conn
)
1786 struct Scsi_Host
*shost
= conn
->session
->host
;
1787 struct iscsi_host
*ihost
= shost_priv(shost
);
1789 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1791 flush_workqueue(ihost
->workq
);
1793 EXPORT_SYMBOL_GPL(iscsi_suspend_tx
);
1795 static void iscsi_start_tx(struct iscsi_conn
*conn
)
1797 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1798 iscsi_conn_queue_work(conn
);
1802 * We want to make sure a ping is in flight. It has timed out.
1803 * And we are not busy processing a pdu that is making
1804 * progress but got started before the ping and is taking a while
1805 * to complete so the ping is just stuck behind it in a queue.
1807 static int iscsi_has_ping_timed_out(struct iscsi_conn
*conn
)
1809 if (conn
->ping_task
&&
1810 time_before_eq(conn
->last_recv
+ (conn
->recv_timeout
* HZ
) +
1811 (conn
->ping_timeout
* HZ
), jiffies
))
1817 static enum blk_eh_timer_return
iscsi_eh_cmd_timed_out(struct scsi_cmnd
*sc
)
1819 enum blk_eh_timer_return rc
= BLK_EH_NOT_HANDLED
;
1820 struct iscsi_task
*task
= NULL
;
1821 struct iscsi_cls_session
*cls_session
;
1822 struct iscsi_session
*session
;
1823 struct iscsi_conn
*conn
;
1825 cls_session
= starget_to_session(scsi_target(sc
->device
));
1826 session
= cls_session
->dd_data
;
1828 ISCSI_DBG_EH(session
, "scsi cmd %p timedout\n", sc
);
1830 spin_lock(&session
->lock
);
1831 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
1833 * We are probably in the middle of iscsi recovery so let
1834 * that complete and handle the error.
1836 rc
= BLK_EH_RESET_TIMER
;
1840 conn
= session
->leadconn
;
1842 /* In the middle of shuting down */
1843 rc
= BLK_EH_RESET_TIMER
;
1847 task
= (struct iscsi_task
*)sc
->SCp
.ptr
;
1851 * If we have sent (at least queued to the network layer) a pdu or
1852 * recvd one for the task since the last timeout ask for
1853 * more time. If on the next timeout we have not made progress
1854 * we can check if it is the task or connection when we send the
1857 if (time_after_eq(task
->last_xfer
, task
->last_timeout
)) {
1858 ISCSI_DBG_EH(session
, "Command making progress. Asking "
1859 "scsi-ml for more time to complete. "
1860 "Last data recv at %lu. Last timeout was at "
1861 "%lu\n.", task
->last_xfer
, task
->last_timeout
);
1862 task
->have_checked_conn
= false;
1863 rc
= BLK_EH_RESET_TIMER
;
1867 if (!conn
->recv_timeout
&& !conn
->ping_timeout
)
1870 * if the ping timedout then we are in the middle of cleaning up
1871 * and can let the iscsi eh handle it
1873 if (iscsi_has_ping_timed_out(conn
)) {
1874 rc
= BLK_EH_RESET_TIMER
;
1878 /* Assumes nop timeout is shorter than scsi cmd timeout */
1879 if (task
->have_checked_conn
)
1883 * Checking the transport already or nop from a cmd timeout still
1886 if (conn
->ping_task
) {
1887 task
->have_checked_conn
= true;
1888 rc
= BLK_EH_RESET_TIMER
;
1892 /* Make sure there is a transport check done */
1893 iscsi_send_nopout(conn
, NULL
);
1894 task
->have_checked_conn
= true;
1895 rc
= BLK_EH_RESET_TIMER
;
1899 task
->last_timeout
= jiffies
;
1900 spin_unlock(&session
->lock
);
1901 ISCSI_DBG_EH(session
, "return %s\n", rc
== BLK_EH_RESET_TIMER
?
1902 "timer reset" : "nh");
1906 static void iscsi_check_transport_timeouts(unsigned long data
)
1908 struct iscsi_conn
*conn
= (struct iscsi_conn
*)data
;
1909 struct iscsi_session
*session
= conn
->session
;
1910 unsigned long recv_timeout
, next_timeout
= 0, last_recv
;
1912 spin_lock(&session
->lock
);
1913 if (session
->state
!= ISCSI_STATE_LOGGED_IN
)
1916 recv_timeout
= conn
->recv_timeout
;
1921 last_recv
= conn
->last_recv
;
1923 if (iscsi_has_ping_timed_out(conn
)) {
1924 iscsi_conn_printk(KERN_ERR
, conn
, "ping timeout of %d secs "
1925 "expired, recv timeout %d, last rx %lu, "
1926 "last ping %lu, now %lu\n",
1927 conn
->ping_timeout
, conn
->recv_timeout
,
1928 last_recv
, conn
->last_ping
, jiffies
);
1929 spin_unlock(&session
->lock
);
1930 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1934 if (time_before_eq(last_recv
+ recv_timeout
, jiffies
)) {
1935 /* send a ping to try to provoke some traffic */
1936 ISCSI_DBG_CONN(conn
, "Sending nopout as ping\n");
1937 iscsi_send_nopout(conn
, NULL
);
1938 next_timeout
= conn
->last_ping
+ (conn
->ping_timeout
* HZ
);
1940 next_timeout
= last_recv
+ recv_timeout
;
1942 ISCSI_DBG_CONN(conn
, "Setting next tmo %lu\n", next_timeout
);
1943 mod_timer(&conn
->transport_timer
, next_timeout
);
1945 spin_unlock(&session
->lock
);
1948 static void iscsi_prep_abort_task_pdu(struct iscsi_task
*task
,
1949 struct iscsi_tm
*hdr
)
1951 memset(hdr
, 0, sizeof(*hdr
));
1952 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
1953 hdr
->flags
= ISCSI_TM_FUNC_ABORT_TASK
& ISCSI_FLAG_TM_FUNC_MASK
;
1954 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
1955 memcpy(hdr
->lun
, task
->lun
, sizeof(hdr
->lun
));
1956 hdr
->rtt
= task
->hdr_itt
;
1957 hdr
->refcmdsn
= task
->cmdsn
;
1960 int iscsi_eh_abort(struct scsi_cmnd
*sc
)
1962 struct iscsi_cls_session
*cls_session
;
1963 struct iscsi_session
*session
;
1964 struct iscsi_conn
*conn
;
1965 struct iscsi_task
*task
;
1966 struct iscsi_tm
*hdr
;
1969 cls_session
= starget_to_session(scsi_target(sc
->device
));
1970 session
= cls_session
->dd_data
;
1972 ISCSI_DBG_EH(session
, "aborting sc %p\n", sc
);
1974 mutex_lock(&session
->eh_mutex
);
1975 spin_lock_bh(&session
->lock
);
1977 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1981 ISCSI_DBG_EH(session
, "sc never reached iscsi layer or "
1983 spin_unlock_bh(&session
->lock
);
1984 mutex_unlock(&session
->eh_mutex
);
1989 * If we are not logged in or we have started a new session
1990 * then let the host reset code handle this
1992 if (!session
->leadconn
|| session
->state
!= ISCSI_STATE_LOGGED_IN
||
1993 sc
->SCp
.phase
!= session
->age
) {
1994 spin_unlock_bh(&session
->lock
);
1995 mutex_unlock(&session
->eh_mutex
);
1996 ISCSI_DBG_EH(session
, "failing abort due to dropped "
2001 conn
= session
->leadconn
;
2002 conn
->eh_abort_cnt
++;
2005 task
= (struct iscsi_task
*)sc
->SCp
.ptr
;
2006 ISCSI_DBG_EH(session
, "aborting [sc %p itt 0x%x]\n",
2009 /* task completed before time out */
2011 ISCSI_DBG_EH(session
, "sc completed while abort in progress\n");
2015 if (task
->state
== ISCSI_TASK_PENDING
) {
2016 fail_scsi_task(task
, DID_ABORT
);
2020 /* only have one tmf outstanding at a time */
2021 if (conn
->tmf_state
!= TMF_INITIAL
)
2023 conn
->tmf_state
= TMF_QUEUED
;
2026 iscsi_prep_abort_task_pdu(task
, hdr
);
2028 if (iscsi_exec_task_mgmt_fn(conn
, hdr
, age
, session
->abort_timeout
)) {
2033 switch (conn
->tmf_state
) {
2035 spin_unlock_bh(&session
->lock
);
2037 * stop tx side incase the target had sent a abort rsp but
2038 * the initiator was still writing out data.
2040 iscsi_suspend_tx(conn
);
2042 * we do not stop the recv side because targets have been
2043 * good and have never sent us a successful tmf response
2044 * then sent more data for the cmd.
2046 spin_lock_bh(&session
->lock
);
2047 fail_scsi_task(task
, DID_ABORT
);
2048 conn
->tmf_state
= TMF_INITIAL
;
2049 spin_unlock_bh(&session
->lock
);
2050 iscsi_start_tx(conn
);
2051 goto success_unlocked
;
2053 spin_unlock_bh(&session
->lock
);
2054 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
2055 goto failed_unlocked
;
2058 conn
->tmf_state
= TMF_INITIAL
;
2059 /* task completed before tmf abort response */
2060 ISCSI_DBG_EH(session
, "sc completed while abort in "
2066 conn
->tmf_state
= TMF_INITIAL
;
2071 spin_unlock_bh(&session
->lock
);
2073 ISCSI_DBG_EH(session
, "abort success [sc %p itt 0x%x]\n",
2075 mutex_unlock(&session
->eh_mutex
);
2079 spin_unlock_bh(&session
->lock
);
2081 ISCSI_DBG_EH(session
, "abort failed [sc %p itt 0x%x]\n", sc
,
2082 task
? task
->itt
: 0);
2083 mutex_unlock(&session
->eh_mutex
);
2086 EXPORT_SYMBOL_GPL(iscsi_eh_abort
);
2088 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd
*sc
, struct iscsi_tm
*hdr
)
2090 memset(hdr
, 0, sizeof(*hdr
));
2091 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
2092 hdr
->flags
= ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
& ISCSI_FLAG_TM_FUNC_MASK
;
2093 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2094 int_to_scsilun(sc
->device
->lun
, (struct scsi_lun
*)hdr
->lun
);
2095 hdr
->rtt
= RESERVED_ITT
;
2098 int iscsi_eh_device_reset(struct scsi_cmnd
*sc
)
2100 struct iscsi_cls_session
*cls_session
;
2101 struct iscsi_session
*session
;
2102 struct iscsi_conn
*conn
;
2103 struct iscsi_tm
*hdr
;
2106 cls_session
= starget_to_session(scsi_target(sc
->device
));
2107 session
= cls_session
->dd_data
;
2109 ISCSI_DBG_EH(session
, "LU Reset [sc %p lun %u]\n", sc
, sc
->device
->lun
);
2111 mutex_lock(&session
->eh_mutex
);
2112 spin_lock_bh(&session
->lock
);
2114 * Just check if we are not logged in. We cannot check for
2115 * the phase because the reset could come from a ioctl.
2117 if (!session
->leadconn
|| session
->state
!= ISCSI_STATE_LOGGED_IN
)
2119 conn
= session
->leadconn
;
2121 /* only have one tmf outstanding at a time */
2122 if (conn
->tmf_state
!= TMF_INITIAL
)
2124 conn
->tmf_state
= TMF_QUEUED
;
2127 iscsi_prep_lun_reset_pdu(sc
, hdr
);
2129 if (iscsi_exec_task_mgmt_fn(conn
, hdr
, session
->age
,
2130 session
->lu_reset_timeout
)) {
2135 switch (conn
->tmf_state
) {
2139 spin_unlock_bh(&session
->lock
);
2140 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
2143 conn
->tmf_state
= TMF_INITIAL
;
2148 spin_unlock_bh(&session
->lock
);
2150 iscsi_suspend_tx(conn
);
2152 spin_lock_bh(&session
->lock
);
2153 fail_scsi_tasks(conn
, sc
->device
->lun
, DID_ERROR
);
2154 conn
->tmf_state
= TMF_INITIAL
;
2155 spin_unlock_bh(&session
->lock
);
2157 iscsi_start_tx(conn
);
2161 spin_unlock_bh(&session
->lock
);
2163 ISCSI_DBG_EH(session
, "dev reset result = %s\n",
2164 rc
== SUCCESS
? "SUCCESS" : "FAILED");
2165 mutex_unlock(&session
->eh_mutex
);
2168 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset
);
2171 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2172 * should be accessed via kfifo_{get,put} on q->queue.
2173 * Optionally, the caller can obtain the array of object pointers
2174 * by passing in a non-NULL @items pointer
2177 iscsi_pool_init(struct iscsi_pool
*q
, int max
, void ***items
, int item_size
)
2179 int i
, num_arrays
= 1;
2181 memset(q
, 0, sizeof(*q
));
2185 /* If the user passed an items pointer, he wants a copy of
2189 q
->pool
= kzalloc(num_arrays
* max
* sizeof(void*), GFP_KERNEL
);
2190 if (q
->pool
== NULL
)
2193 q
->queue
= kfifo_init((void*)q
->pool
, max
* sizeof(void*),
2195 if (IS_ERR(q
->queue
)) {
2200 for (i
= 0; i
< max
; i
++) {
2201 q
->pool
[i
] = kzalloc(item_size
, GFP_KERNEL
);
2202 if (q
->pool
[i
] == NULL
) {
2206 __kfifo_put(q
->queue
, (void*)&q
->pool
[i
], sizeof(void*));
2210 *items
= q
->pool
+ max
;
2211 memcpy(*items
, q
->pool
, max
* sizeof(void *));
2220 EXPORT_SYMBOL_GPL(iscsi_pool_init
);
2222 void iscsi_pool_free(struct iscsi_pool
*q
)
2226 for (i
= 0; i
< q
->max
; i
++)
2231 EXPORT_SYMBOL_GPL(iscsi_pool_free
);
2234 * iscsi_host_add - add host to system
2236 * @pdev: parent device
2238 * This should be called by partial offload and software iscsi drivers
2239 * to add a host to the system.
2241 int iscsi_host_add(struct Scsi_Host
*shost
, struct device
*pdev
)
2243 if (!shost
->can_queue
)
2244 shost
->can_queue
= ISCSI_DEF_XMIT_CMDS_MAX
;
2246 if (!shost
->cmd_per_lun
)
2247 shost
->cmd_per_lun
= ISCSI_DEF_CMD_PER_LUN
;
2249 if (!shost
->transportt
->eh_timed_out
)
2250 shost
->transportt
->eh_timed_out
= iscsi_eh_cmd_timed_out
;
2251 return scsi_add_host(shost
, pdev
);
2253 EXPORT_SYMBOL_GPL(iscsi_host_add
);
2256 * iscsi_host_alloc - allocate a host and driver data
2257 * @sht: scsi host template
2258 * @dd_data_size: driver host data size
2259 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2261 * This should be called by partial offload and software iscsi drivers.
2262 * To access the driver specific memory use the iscsi_host_priv() macro.
2264 struct Scsi_Host
*iscsi_host_alloc(struct scsi_host_template
*sht
,
2265 int dd_data_size
, bool xmit_can_sleep
)
2267 struct Scsi_Host
*shost
;
2268 struct iscsi_host
*ihost
;
2270 shost
= scsi_host_alloc(sht
, sizeof(struct iscsi_host
) + dd_data_size
);
2273 ihost
= shost_priv(shost
);
2275 if (xmit_can_sleep
) {
2276 snprintf(ihost
->workq_name
, sizeof(ihost
->workq_name
),
2277 "iscsi_q_%d", shost
->host_no
);
2278 ihost
->workq
= create_singlethread_workqueue(ihost
->workq_name
);
2283 spin_lock_init(&ihost
->lock
);
2284 ihost
->state
= ISCSI_HOST_SETUP
;
2285 ihost
->num_sessions
= 0;
2286 init_waitqueue_head(&ihost
->session_removal_wq
);
2290 scsi_host_put(shost
);
2293 EXPORT_SYMBOL_GPL(iscsi_host_alloc
);
2295 static void iscsi_notify_host_removed(struct iscsi_cls_session
*cls_session
)
2297 iscsi_session_failure(cls_session
->dd_data
, ISCSI_ERR_INVALID_HOST
);
2301 * iscsi_host_remove - remove host and sessions
2304 * If there are any sessions left, this will initiate the removal and wait
2305 * for the completion.
2307 void iscsi_host_remove(struct Scsi_Host
*shost
)
2309 struct iscsi_host
*ihost
= shost_priv(shost
);
2310 unsigned long flags
;
2312 spin_lock_irqsave(&ihost
->lock
, flags
);
2313 ihost
->state
= ISCSI_HOST_REMOVED
;
2314 spin_unlock_irqrestore(&ihost
->lock
, flags
);
2316 iscsi_host_for_each_session(shost
, iscsi_notify_host_removed
);
2317 wait_event_interruptible(ihost
->session_removal_wq
,
2318 ihost
->num_sessions
== 0);
2319 if (signal_pending(current
))
2320 flush_signals(current
);
2322 scsi_remove_host(shost
);
2324 destroy_workqueue(ihost
->workq
);
2326 EXPORT_SYMBOL_GPL(iscsi_host_remove
);
2328 void iscsi_host_free(struct Scsi_Host
*shost
)
2330 struct iscsi_host
*ihost
= shost_priv(shost
);
2332 kfree(ihost
->netdev
);
2333 kfree(ihost
->hwaddress
);
2334 kfree(ihost
->initiatorname
);
2335 scsi_host_put(shost
);
2337 EXPORT_SYMBOL_GPL(iscsi_host_free
);
2339 static void iscsi_host_dec_session_cnt(struct Scsi_Host
*shost
)
2341 struct iscsi_host
*ihost
= shost_priv(shost
);
2342 unsigned long flags
;
2344 shost
= scsi_host_get(shost
);
2346 printk(KERN_ERR
"Invalid state. Cannot notify host removal "
2347 "of session teardown event because host already "
2352 spin_lock_irqsave(&ihost
->lock
, flags
);
2353 ihost
->num_sessions
--;
2354 if (ihost
->num_sessions
== 0)
2355 wake_up(&ihost
->session_removal_wq
);
2356 spin_unlock_irqrestore(&ihost
->lock
, flags
);
2357 scsi_host_put(shost
);
2361 * iscsi_session_setup - create iscsi cls session and host and session
2362 * @iscsit: iscsi transport template
2364 * @cmds_max: session can queue
2365 * @cmd_task_size: LLD task private data size
2366 * @initial_cmdsn: initial CmdSN
2368 * This can be used by software iscsi_transports that allocate
2369 * a session per scsi host.
2371 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2372 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2373 * for nop handling and login/logout requests.
2375 struct iscsi_cls_session
*
2376 iscsi_session_setup(struct iscsi_transport
*iscsit
, struct Scsi_Host
*shost
,
2377 uint16_t cmds_max
, int cmd_task_size
,
2378 uint32_t initial_cmdsn
, unsigned int id
)
2380 struct iscsi_host
*ihost
= shost_priv(shost
);
2381 struct iscsi_session
*session
;
2382 struct iscsi_cls_session
*cls_session
;
2383 int cmd_i
, scsi_cmds
, total_cmds
= cmds_max
;
2384 unsigned long flags
;
2386 spin_lock_irqsave(&ihost
->lock
, flags
);
2387 if (ihost
->state
== ISCSI_HOST_REMOVED
) {
2388 spin_unlock_irqrestore(&ihost
->lock
, flags
);
2391 ihost
->num_sessions
++;
2392 spin_unlock_irqrestore(&ihost
->lock
, flags
);
2395 total_cmds
= ISCSI_DEF_XMIT_CMDS_MAX
;
2397 * The iscsi layer needs some tasks for nop handling and tmfs,
2398 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2399 * + 1 command for scsi IO.
2401 if (total_cmds
< ISCSI_TOTAL_CMDS_MIN
) {
2402 printk(KERN_ERR
"iscsi: invalid can_queue of %d. can_queue "
2403 "must be a power of two that is at least %d.\n",
2404 total_cmds
, ISCSI_TOTAL_CMDS_MIN
);
2405 goto dec_session_count
;
2408 if (total_cmds
> ISCSI_TOTAL_CMDS_MAX
) {
2409 printk(KERN_ERR
"iscsi: invalid can_queue of %d. can_queue "
2410 "must be a power of 2 less than or equal to %d.\n",
2411 cmds_max
, ISCSI_TOTAL_CMDS_MAX
);
2412 total_cmds
= ISCSI_TOTAL_CMDS_MAX
;
2415 if (!is_power_of_2(total_cmds
)) {
2416 printk(KERN_ERR
"iscsi: invalid can_queue of %d. can_queue "
2417 "must be a power of 2.\n", total_cmds
);
2418 total_cmds
= rounddown_pow_of_two(total_cmds
);
2419 if (total_cmds
< ISCSI_TOTAL_CMDS_MIN
)
2421 printk(KERN_INFO
"iscsi: Rounding can_queue to %d.\n",
2424 scsi_cmds
= total_cmds
- ISCSI_MGMT_CMDS_MAX
;
2426 cls_session
= iscsi_alloc_session(shost
, iscsit
,
2427 sizeof(struct iscsi_session
));
2429 goto dec_session_count
;
2430 session
= cls_session
->dd_data
;
2431 session
->cls_session
= cls_session
;
2432 session
->host
= shost
;
2433 session
->state
= ISCSI_STATE_FREE
;
2434 session
->fast_abort
= 1;
2435 session
->lu_reset_timeout
= 15;
2436 session
->abort_timeout
= 10;
2437 session
->scsi_cmds_max
= scsi_cmds
;
2438 session
->cmds_max
= total_cmds
;
2439 session
->queued_cmdsn
= session
->cmdsn
= initial_cmdsn
;
2440 session
->exp_cmdsn
= initial_cmdsn
+ 1;
2441 session
->max_cmdsn
= initial_cmdsn
+ 1;
2442 session
->max_r2t
= 1;
2443 session
->tt
= iscsit
;
2444 mutex_init(&session
->eh_mutex
);
2445 spin_lock_init(&session
->lock
);
2447 /* initialize SCSI PDU commands pool */
2448 if (iscsi_pool_init(&session
->cmdpool
, session
->cmds_max
,
2449 (void***)&session
->cmds
,
2450 cmd_task_size
+ sizeof(struct iscsi_task
)))
2451 goto cmdpool_alloc_fail
;
2453 /* pre-format cmds pool with ITT */
2454 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
2455 struct iscsi_task
*task
= session
->cmds
[cmd_i
];
2458 task
->dd_data
= &task
[1];
2460 task
->state
= ISCSI_TASK_FREE
;
2461 INIT_LIST_HEAD(&task
->running
);
2464 if (!try_module_get(iscsit
->owner
))
2465 goto module_get_fail
;
2467 if (iscsi_add_session(cls_session
, id
))
2468 goto cls_session_fail
;
2473 module_put(iscsit
->owner
);
2475 iscsi_pool_free(&session
->cmdpool
);
2477 iscsi_free_session(cls_session
);
2479 iscsi_host_dec_session_cnt(shost
);
2482 EXPORT_SYMBOL_GPL(iscsi_session_setup
);
2485 * iscsi_session_teardown - destroy session, host, and cls_session
2486 * @cls_session: iscsi session
2488 * The driver must have called iscsi_remove_session before
2491 void iscsi_session_teardown(struct iscsi_cls_session
*cls_session
)
2493 struct iscsi_session
*session
= cls_session
->dd_data
;
2494 struct module
*owner
= cls_session
->transport
->owner
;
2495 struct Scsi_Host
*shost
= session
->host
;
2497 iscsi_pool_free(&session
->cmdpool
);
2499 kfree(session
->password
);
2500 kfree(session
->password_in
);
2501 kfree(session
->username
);
2502 kfree(session
->username_in
);
2503 kfree(session
->targetname
);
2504 kfree(session
->initiatorname
);
2505 kfree(session
->ifacename
);
2507 iscsi_destroy_session(cls_session
);
2508 iscsi_host_dec_session_cnt(shost
);
2511 EXPORT_SYMBOL_GPL(iscsi_session_teardown
);
2514 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2515 * @cls_session: iscsi_cls_session
2516 * @dd_size: private driver data size
2519 struct iscsi_cls_conn
*
2520 iscsi_conn_setup(struct iscsi_cls_session
*cls_session
, int dd_size
,
2523 struct iscsi_session
*session
= cls_session
->dd_data
;
2524 struct iscsi_conn
*conn
;
2525 struct iscsi_cls_conn
*cls_conn
;
2528 cls_conn
= iscsi_create_conn(cls_session
, sizeof(*conn
) + dd_size
,
2532 conn
= cls_conn
->dd_data
;
2533 memset(conn
, 0, sizeof(*conn
) + dd_size
);
2535 conn
->dd_data
= cls_conn
->dd_data
+ sizeof(*conn
);
2536 conn
->session
= session
;
2537 conn
->cls_conn
= cls_conn
;
2538 conn
->c_stage
= ISCSI_CONN_INITIAL_STAGE
;
2539 conn
->id
= conn_idx
;
2540 conn
->exp_statsn
= 0;
2541 conn
->tmf_state
= TMF_INITIAL
;
2543 init_timer(&conn
->transport_timer
);
2544 conn
->transport_timer
.data
= (unsigned long)conn
;
2545 conn
->transport_timer
.function
= iscsi_check_transport_timeouts
;
2547 INIT_LIST_HEAD(&conn
->mgmtqueue
);
2548 INIT_LIST_HEAD(&conn
->cmdqueue
);
2549 INIT_LIST_HEAD(&conn
->requeue
);
2550 INIT_WORK(&conn
->xmitwork
, iscsi_xmitworker
);
2552 /* allocate login_task used for the login/text sequences */
2553 spin_lock_bh(&session
->lock
);
2554 if (!__kfifo_get(session
->cmdpool
.queue
,
2555 (void*)&conn
->login_task
,
2557 spin_unlock_bh(&session
->lock
);
2558 goto login_task_alloc_fail
;
2560 spin_unlock_bh(&session
->lock
);
2562 data
= (char *) __get_free_pages(GFP_KERNEL
,
2563 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN
));
2565 goto login_task_data_alloc_fail
;
2566 conn
->login_task
->data
= conn
->data
= data
;
2568 init_timer(&conn
->tmf_timer
);
2569 init_waitqueue_head(&conn
->ehwait
);
2573 login_task_data_alloc_fail
:
2574 __kfifo_put(session
->cmdpool
.queue
, (void*)&conn
->login_task
,
2576 login_task_alloc_fail
:
2577 iscsi_destroy_conn(cls_conn
);
2580 EXPORT_SYMBOL_GPL(iscsi_conn_setup
);
2583 * iscsi_conn_teardown - teardown iscsi connection
2584 * cls_conn: iscsi class connection
2586 * TODO: we may need to make this into a two step process
2587 * like scsi-mls remove + put host
2589 void iscsi_conn_teardown(struct iscsi_cls_conn
*cls_conn
)
2591 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2592 struct iscsi_session
*session
= conn
->session
;
2593 unsigned long flags
;
2595 del_timer_sync(&conn
->transport_timer
);
2597 spin_lock_bh(&session
->lock
);
2598 conn
->c_stage
= ISCSI_CONN_CLEANUP_WAIT
;
2599 if (session
->leadconn
== conn
) {
2601 * leading connection? then give up on recovery.
2603 session
->state
= ISCSI_STATE_TERMINATE
;
2604 wake_up(&conn
->ehwait
);
2606 spin_unlock_bh(&session
->lock
);
2609 * Block until all in-progress commands for this connection
2613 spin_lock_irqsave(session
->host
->host_lock
, flags
);
2614 if (!session
->host
->host_busy
) { /* OK for ERL == 0 */
2615 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
2618 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
2619 msleep_interruptible(500);
2620 iscsi_conn_printk(KERN_INFO
, conn
, "iscsi conn_destroy(): "
2621 "host_busy %d host_failed %d\n",
2622 session
->host
->host_busy
,
2623 session
->host
->host_failed
);
2625 * force eh_abort() to unblock
2627 wake_up(&conn
->ehwait
);
2630 /* flush queued up work because we free the connection below */
2631 iscsi_suspend_tx(conn
);
2633 spin_lock_bh(&session
->lock
);
2634 free_pages((unsigned long) conn
->data
,
2635 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN
));
2636 kfree(conn
->persistent_address
);
2637 __kfifo_put(session
->cmdpool
.queue
, (void*)&conn
->login_task
,
2639 if (session
->leadconn
== conn
)
2640 session
->leadconn
= NULL
;
2641 spin_unlock_bh(&session
->lock
);
2643 iscsi_destroy_conn(cls_conn
);
2645 EXPORT_SYMBOL_GPL(iscsi_conn_teardown
);
2647 int iscsi_conn_start(struct iscsi_cls_conn
*cls_conn
)
2649 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2650 struct iscsi_session
*session
= conn
->session
;
2653 iscsi_conn_printk(KERN_ERR
, conn
,
2654 "can't start unbound connection\n");
2658 if ((session
->imm_data_en
|| !session
->initial_r2t_en
) &&
2659 session
->first_burst
> session
->max_burst
) {
2660 iscsi_conn_printk(KERN_INFO
, conn
, "invalid burst lengths: "
2661 "first_burst %d max_burst %d\n",
2662 session
->first_burst
, session
->max_burst
);
2666 if (conn
->ping_timeout
&& !conn
->recv_timeout
) {
2667 iscsi_conn_printk(KERN_ERR
, conn
, "invalid recv timeout of "
2668 "zero. Using 5 seconds\n.");
2669 conn
->recv_timeout
= 5;
2672 if (conn
->recv_timeout
&& !conn
->ping_timeout
) {
2673 iscsi_conn_printk(KERN_ERR
, conn
, "invalid ping timeout of "
2674 "zero. Using 5 seconds.\n");
2675 conn
->ping_timeout
= 5;
2678 spin_lock_bh(&session
->lock
);
2679 conn
->c_stage
= ISCSI_CONN_STARTED
;
2680 session
->state
= ISCSI_STATE_LOGGED_IN
;
2681 session
->queued_cmdsn
= session
->cmdsn
;
2683 conn
->last_recv
= jiffies
;
2684 conn
->last_ping
= jiffies
;
2685 if (conn
->recv_timeout
&& conn
->ping_timeout
)
2686 mod_timer(&conn
->transport_timer
,
2687 jiffies
+ (conn
->recv_timeout
* HZ
));
2689 switch(conn
->stop_stage
) {
2690 case STOP_CONN_RECOVER
:
2692 * unblock eh_abort() if it is blocked. re-try all
2693 * commands after successful recovery
2695 conn
->stop_stage
= 0;
2696 conn
->tmf_state
= TMF_INITIAL
;
2698 if (session
->age
== 16)
2701 case STOP_CONN_TERM
:
2702 conn
->stop_stage
= 0;
2707 spin_unlock_bh(&session
->lock
);
2709 iscsi_unblock_session(session
->cls_session
);
2710 wake_up(&conn
->ehwait
);
2713 EXPORT_SYMBOL_GPL(iscsi_conn_start
);
2716 fail_mgmt_tasks(struct iscsi_session
*session
, struct iscsi_conn
*conn
)
2718 struct iscsi_task
*task
;
2721 for (i
= 0; i
< conn
->session
->cmds_max
; i
++) {
2722 task
= conn
->session
->cmds
[i
];
2726 if (task
->state
== ISCSI_TASK_FREE
)
2729 ISCSI_DBG_SESSION(conn
->session
,
2730 "failing mgmt itt 0x%x state %d\n",
2731 task
->itt
, task
->state
);
2732 state
= ISCSI_TASK_ABRT_SESS_RECOV
;
2733 if (task
->state
== ISCSI_TASK_PENDING
)
2734 state
= ISCSI_TASK_COMPLETED
;
2735 iscsi_complete_task(task
, state
);
2740 static void iscsi_start_session_recovery(struct iscsi_session
*session
,
2741 struct iscsi_conn
*conn
, int flag
)
2745 mutex_lock(&session
->eh_mutex
);
2746 spin_lock_bh(&session
->lock
);
2747 if (conn
->stop_stage
== STOP_CONN_TERM
) {
2748 spin_unlock_bh(&session
->lock
);
2749 mutex_unlock(&session
->eh_mutex
);
2754 * When this is called for the in_login state, we only want to clean
2755 * up the login task and connection. We do not need to block and set
2756 * the recovery state again
2758 if (flag
== STOP_CONN_TERM
)
2759 session
->state
= ISCSI_STATE_TERMINATE
;
2760 else if (conn
->stop_stage
!= STOP_CONN_RECOVER
)
2761 session
->state
= ISCSI_STATE_IN_RECOVERY
;
2762 spin_unlock_bh(&session
->lock
);
2764 del_timer_sync(&conn
->transport_timer
);
2765 iscsi_suspend_tx(conn
);
2767 spin_lock_bh(&session
->lock
);
2768 old_stop_stage
= conn
->stop_stage
;
2769 conn
->stop_stage
= flag
;
2770 conn
->c_stage
= ISCSI_CONN_STOPPED
;
2771 spin_unlock_bh(&session
->lock
);
2774 * for connection level recovery we should not calculate
2775 * header digest. conn->hdr_size used for optimization
2776 * in hdr_extract() and will be re-negotiated at
2779 if (flag
== STOP_CONN_RECOVER
) {
2780 conn
->hdrdgst_en
= 0;
2781 conn
->datadgst_en
= 0;
2782 if (session
->state
== ISCSI_STATE_IN_RECOVERY
&&
2783 old_stop_stage
!= STOP_CONN_RECOVER
) {
2784 ISCSI_DBG_SESSION(session
, "blocking session\n");
2785 iscsi_block_session(session
->cls_session
);
2792 spin_lock_bh(&session
->lock
);
2793 fail_scsi_tasks(conn
, -1, DID_TRANSPORT_DISRUPTED
);
2794 fail_mgmt_tasks(session
, conn
);
2795 spin_unlock_bh(&session
->lock
);
2796 mutex_unlock(&session
->eh_mutex
);
2799 void iscsi_conn_stop(struct iscsi_cls_conn
*cls_conn
, int flag
)
2801 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2802 struct iscsi_session
*session
= conn
->session
;
2805 case STOP_CONN_RECOVER
:
2806 case STOP_CONN_TERM
:
2807 iscsi_start_session_recovery(session
, conn
, flag
);
2810 iscsi_conn_printk(KERN_ERR
, conn
,
2811 "invalid stop flag %d\n", flag
);
2814 EXPORT_SYMBOL_GPL(iscsi_conn_stop
);
2816 int iscsi_conn_bind(struct iscsi_cls_session
*cls_session
,
2817 struct iscsi_cls_conn
*cls_conn
, int is_leading
)
2819 struct iscsi_session
*session
= cls_session
->dd_data
;
2820 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2822 spin_lock_bh(&session
->lock
);
2824 session
->leadconn
= conn
;
2825 spin_unlock_bh(&session
->lock
);
2828 * Unblock xmitworker(), Login Phase will pass through.
2830 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
2831 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
2834 EXPORT_SYMBOL_GPL(iscsi_conn_bind
);
2836 static int iscsi_switch_str_param(char **param
, char *new_val_buf
)
2841 if (!strcmp(*param
, new_val_buf
))
2845 new_val
= kstrdup(new_val_buf
, GFP_NOIO
);
2854 int iscsi_set_param(struct iscsi_cls_conn
*cls_conn
,
2855 enum iscsi_param param
, char *buf
, int buflen
)
2857 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2858 struct iscsi_session
*session
= conn
->session
;
2862 case ISCSI_PARAM_FAST_ABORT
:
2863 sscanf(buf
, "%d", &session
->fast_abort
);
2865 case ISCSI_PARAM_ABORT_TMO
:
2866 sscanf(buf
, "%d", &session
->abort_timeout
);
2868 case ISCSI_PARAM_LU_RESET_TMO
:
2869 sscanf(buf
, "%d", &session
->lu_reset_timeout
);
2871 case ISCSI_PARAM_PING_TMO
:
2872 sscanf(buf
, "%d", &conn
->ping_timeout
);
2874 case ISCSI_PARAM_RECV_TMO
:
2875 sscanf(buf
, "%d", &conn
->recv_timeout
);
2877 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
2878 sscanf(buf
, "%d", &conn
->max_recv_dlength
);
2880 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
2881 sscanf(buf
, "%d", &conn
->max_xmit_dlength
);
2883 case ISCSI_PARAM_HDRDGST_EN
:
2884 sscanf(buf
, "%d", &conn
->hdrdgst_en
);
2886 case ISCSI_PARAM_DATADGST_EN
:
2887 sscanf(buf
, "%d", &conn
->datadgst_en
);
2889 case ISCSI_PARAM_INITIAL_R2T_EN
:
2890 sscanf(buf
, "%d", &session
->initial_r2t_en
);
2892 case ISCSI_PARAM_MAX_R2T
:
2893 sscanf(buf
, "%d", &session
->max_r2t
);
2895 case ISCSI_PARAM_IMM_DATA_EN
:
2896 sscanf(buf
, "%d", &session
->imm_data_en
);
2898 case ISCSI_PARAM_FIRST_BURST
:
2899 sscanf(buf
, "%d", &session
->first_burst
);
2901 case ISCSI_PARAM_MAX_BURST
:
2902 sscanf(buf
, "%d", &session
->max_burst
);
2904 case ISCSI_PARAM_PDU_INORDER_EN
:
2905 sscanf(buf
, "%d", &session
->pdu_inorder_en
);
2907 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
2908 sscanf(buf
, "%d", &session
->dataseq_inorder_en
);
2910 case ISCSI_PARAM_ERL
:
2911 sscanf(buf
, "%d", &session
->erl
);
2913 case ISCSI_PARAM_IFMARKER_EN
:
2914 sscanf(buf
, "%d", &value
);
2917 case ISCSI_PARAM_OFMARKER_EN
:
2918 sscanf(buf
, "%d", &value
);
2921 case ISCSI_PARAM_EXP_STATSN
:
2922 sscanf(buf
, "%u", &conn
->exp_statsn
);
2924 case ISCSI_PARAM_USERNAME
:
2925 return iscsi_switch_str_param(&session
->username
, buf
);
2926 case ISCSI_PARAM_USERNAME_IN
:
2927 return iscsi_switch_str_param(&session
->username_in
, buf
);
2928 case ISCSI_PARAM_PASSWORD
:
2929 return iscsi_switch_str_param(&session
->password
, buf
);
2930 case ISCSI_PARAM_PASSWORD_IN
:
2931 return iscsi_switch_str_param(&session
->password_in
, buf
);
2932 case ISCSI_PARAM_TARGET_NAME
:
2933 return iscsi_switch_str_param(&session
->targetname
, buf
);
2934 case ISCSI_PARAM_TPGT
:
2935 sscanf(buf
, "%d", &session
->tpgt
);
2937 case ISCSI_PARAM_PERSISTENT_PORT
:
2938 sscanf(buf
, "%d", &conn
->persistent_port
);
2940 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
2941 return iscsi_switch_str_param(&conn
->persistent_address
, buf
);
2942 case ISCSI_PARAM_IFACE_NAME
:
2943 return iscsi_switch_str_param(&session
->ifacename
, buf
);
2944 case ISCSI_PARAM_INITIATOR_NAME
:
2945 return iscsi_switch_str_param(&session
->initiatorname
, buf
);
2952 EXPORT_SYMBOL_GPL(iscsi_set_param
);
2954 int iscsi_session_get_param(struct iscsi_cls_session
*cls_session
,
2955 enum iscsi_param param
, char *buf
)
2957 struct iscsi_session
*session
= cls_session
->dd_data
;
2961 case ISCSI_PARAM_FAST_ABORT
:
2962 len
= sprintf(buf
, "%d\n", session
->fast_abort
);
2964 case ISCSI_PARAM_ABORT_TMO
:
2965 len
= sprintf(buf
, "%d\n", session
->abort_timeout
);
2967 case ISCSI_PARAM_LU_RESET_TMO
:
2968 len
= sprintf(buf
, "%d\n", session
->lu_reset_timeout
);
2970 case ISCSI_PARAM_INITIAL_R2T_EN
:
2971 len
= sprintf(buf
, "%d\n", session
->initial_r2t_en
);
2973 case ISCSI_PARAM_MAX_R2T
:
2974 len
= sprintf(buf
, "%hu\n", session
->max_r2t
);
2976 case ISCSI_PARAM_IMM_DATA_EN
:
2977 len
= sprintf(buf
, "%d\n", session
->imm_data_en
);
2979 case ISCSI_PARAM_FIRST_BURST
:
2980 len
= sprintf(buf
, "%u\n", session
->first_burst
);
2982 case ISCSI_PARAM_MAX_BURST
:
2983 len
= sprintf(buf
, "%u\n", session
->max_burst
);
2985 case ISCSI_PARAM_PDU_INORDER_EN
:
2986 len
= sprintf(buf
, "%d\n", session
->pdu_inorder_en
);
2988 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
2989 len
= sprintf(buf
, "%d\n", session
->dataseq_inorder_en
);
2991 case ISCSI_PARAM_ERL
:
2992 len
= sprintf(buf
, "%d\n", session
->erl
);
2994 case ISCSI_PARAM_TARGET_NAME
:
2995 len
= sprintf(buf
, "%s\n", session
->targetname
);
2997 case ISCSI_PARAM_TPGT
:
2998 len
= sprintf(buf
, "%d\n", session
->tpgt
);
3000 case ISCSI_PARAM_USERNAME
:
3001 len
= sprintf(buf
, "%s\n", session
->username
);
3003 case ISCSI_PARAM_USERNAME_IN
:
3004 len
= sprintf(buf
, "%s\n", session
->username_in
);
3006 case ISCSI_PARAM_PASSWORD
:
3007 len
= sprintf(buf
, "%s\n", session
->password
);
3009 case ISCSI_PARAM_PASSWORD_IN
:
3010 len
= sprintf(buf
, "%s\n", session
->password_in
);
3012 case ISCSI_PARAM_IFACE_NAME
:
3013 len
= sprintf(buf
, "%s\n", session
->ifacename
);
3015 case ISCSI_PARAM_INITIATOR_NAME
:
3016 len
= sprintf(buf
, "%s\n", session
->initiatorname
);
3024 EXPORT_SYMBOL_GPL(iscsi_session_get_param
);
3026 int iscsi_conn_get_param(struct iscsi_cls_conn
*cls_conn
,
3027 enum iscsi_param param
, char *buf
)
3029 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
3033 case ISCSI_PARAM_PING_TMO
:
3034 len
= sprintf(buf
, "%u\n", conn
->ping_timeout
);
3036 case ISCSI_PARAM_RECV_TMO
:
3037 len
= sprintf(buf
, "%u\n", conn
->recv_timeout
);
3039 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
3040 len
= sprintf(buf
, "%u\n", conn
->max_recv_dlength
);
3042 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
3043 len
= sprintf(buf
, "%u\n", conn
->max_xmit_dlength
);
3045 case ISCSI_PARAM_HDRDGST_EN
:
3046 len
= sprintf(buf
, "%d\n", conn
->hdrdgst_en
);
3048 case ISCSI_PARAM_DATADGST_EN
:
3049 len
= sprintf(buf
, "%d\n", conn
->datadgst_en
);
3051 case ISCSI_PARAM_IFMARKER_EN
:
3052 len
= sprintf(buf
, "%d\n", conn
->ifmarker_en
);
3054 case ISCSI_PARAM_OFMARKER_EN
:
3055 len
= sprintf(buf
, "%d\n", conn
->ofmarker_en
);
3057 case ISCSI_PARAM_EXP_STATSN
:
3058 len
= sprintf(buf
, "%u\n", conn
->exp_statsn
);
3060 case ISCSI_PARAM_PERSISTENT_PORT
:
3061 len
= sprintf(buf
, "%d\n", conn
->persistent_port
);
3063 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
3064 len
= sprintf(buf
, "%s\n", conn
->persistent_address
);
3072 EXPORT_SYMBOL_GPL(iscsi_conn_get_param
);
3074 int iscsi_host_get_param(struct Scsi_Host
*shost
, enum iscsi_host_param param
,
3077 struct iscsi_host
*ihost
= shost_priv(shost
);
3081 case ISCSI_HOST_PARAM_NETDEV_NAME
:
3082 len
= sprintf(buf
, "%s\n", ihost
->netdev
);
3084 case ISCSI_HOST_PARAM_HWADDRESS
:
3085 len
= sprintf(buf
, "%s\n", ihost
->hwaddress
);
3087 case ISCSI_HOST_PARAM_INITIATOR_NAME
:
3088 len
= sprintf(buf
, "%s\n", ihost
->initiatorname
);
3090 case ISCSI_HOST_PARAM_IPADDRESS
:
3091 len
= sprintf(buf
, "%s\n", ihost
->local_address
);
3099 EXPORT_SYMBOL_GPL(iscsi_host_get_param
);
3101 int iscsi_host_set_param(struct Scsi_Host
*shost
, enum iscsi_host_param param
,
3102 char *buf
, int buflen
)
3104 struct iscsi_host
*ihost
= shost_priv(shost
);
3107 case ISCSI_HOST_PARAM_NETDEV_NAME
:
3108 return iscsi_switch_str_param(&ihost
->netdev
, buf
);
3109 case ISCSI_HOST_PARAM_HWADDRESS
:
3110 return iscsi_switch_str_param(&ihost
->hwaddress
, buf
);
3111 case ISCSI_HOST_PARAM_INITIATOR_NAME
:
3112 return iscsi_switch_str_param(&ihost
->initiatorname
, buf
);
3119 EXPORT_SYMBOL_GPL(iscsi_host_set_param
);
3121 MODULE_AUTHOR("Mike Christie");
3122 MODULE_DESCRIPTION("iSCSI library functions");
3123 MODULE_LICENSE("GPL");