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 <asm/unaligned.h>
31 #include <scsi/scsi_cmnd.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_eh.h>
34 #include <scsi/scsi_tcq.h>
35 #include <scsi/scsi_host.h>
36 #include <scsi/scsi.h>
37 #include <scsi/iscsi_proto.h>
38 #include <scsi/scsi_transport.h>
39 #include <scsi/scsi_transport_iscsi.h>
40 #include <scsi/libiscsi.h>
42 static int iscsi_dbg_lib_conn;
43 module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
45 MODULE_PARM_DESC(debug_libiscsi_conn,
46 "Turn on debugging for connections in libiscsi module. "
47 "Set to 1 to turn on, and zero to turn off. Default is off.");
49 static int iscsi_dbg_lib_session;
50 module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
52 MODULE_PARM_DESC(debug_libiscsi_session,
53 "Turn on debugging for sessions in libiscsi module. "
54 "Set to 1 to turn on, and zero to turn off. Default is off.");
56 static int iscsi_dbg_lib_eh;
57 module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
59 MODULE_PARM_DESC(debug_libiscsi_eh,
60 "Turn on debugging for error handling in libiscsi module. "
61 "Set to 1 to turn on, and zero to turn off. Default is off.");
63 #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
65 if (iscsi_dbg_lib_conn) \
66 iscsi_conn_printk(KERN_INFO, _conn, \
71 #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
73 if (iscsi_dbg_lib_session) \
74 iscsi_session_printk(KERN_INFO, _session, \
79 #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
81 if (iscsi_dbg_lib_eh) \
82 iscsi_session_printk(KERN_INFO, _session, \
87 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
88 #define SNA32_CHECK 2147483648UL
90 static int iscsi_sna_lt(u32 n1, u32 n2)
92 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
93 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
96 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
97 static int iscsi_sna_lte(u32 n1, u32 n2)
99 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
100 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
103 inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
105 struct Scsi_Host *shost = conn->session->host;
106 struct iscsi_host *ihost = shost_priv(shost);
109 queue_work(ihost->workq, &conn->xmitwork);
111 EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
113 static void __iscsi_update_cmdsn(struct iscsi_session *session,
114 uint32_t exp_cmdsn, uint32_t max_cmdsn)
117 * standard specifies this check for when to update expected and
118 * max sequence numbers
120 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
123 if (exp_cmdsn != session->exp_cmdsn &&
124 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
125 session->exp_cmdsn = exp_cmdsn;
127 if (max_cmdsn != session->max_cmdsn &&
128 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
129 session->max_cmdsn = max_cmdsn;
131 * if the window closed with IO queued, then kick the
134 if (!list_empty(&session->leadconn->cmdqueue) ||
135 !list_empty(&session->leadconn->mgmtqueue))
136 iscsi_conn_queue_work(session->leadconn);
140 void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
142 __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
143 be32_to_cpu(hdr->max_cmdsn));
145 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
148 * iscsi_prep_data_out_pdu - initialize Data-Out
149 * @task: scsi command task
151 * @hdr: iscsi data in pdu
154 * Initialize Data-Out within this R2T sequence and finds
155 * proper data_offset within this SCSI command.
157 * This function is called with connection lock taken.
159 void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
160 struct iscsi_data *hdr)
162 struct iscsi_conn *conn = task->conn;
163 unsigned int left = r2t->data_length - r2t->sent;
165 task->hdr_len = sizeof(struct iscsi_data);
167 memset(hdr, 0, sizeof(struct iscsi_data));
169 hdr->datasn = cpu_to_be32(r2t->datasn);
171 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
172 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
173 hdr->itt = task->hdr_itt;
174 hdr->exp_statsn = r2t->exp_statsn;
175 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
176 if (left > conn->max_xmit_dlength) {
177 hton24(hdr->dlength, conn->max_xmit_dlength);
178 r2t->data_count = conn->max_xmit_dlength;
181 hton24(hdr->dlength, left);
182 r2t->data_count = left;
183 hdr->flags = ISCSI_FLAG_CMD_FINAL;
185 conn->dataout_pdus_cnt++;
187 EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
189 static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
191 unsigned exp_len = task->hdr_len + len;
193 if (exp_len > task->hdr_max) {
198 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
199 task->hdr_len = exp_len;
204 * make an extended cdb AHS
206 static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
208 struct scsi_cmnd *cmd = task->sc;
209 unsigned rlen, pad_len;
210 unsigned short ahslength;
211 struct iscsi_ecdb_ahdr *ecdb_ahdr;
214 ecdb_ahdr = iscsi_next_hdr(task);
215 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
217 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
218 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
220 pad_len = iscsi_padding(rlen);
222 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
223 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
228 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
230 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
231 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
232 ecdb_ahdr->reserved = 0;
233 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
235 ISCSI_DBG_SESSION(task->conn->session,
236 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
237 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
238 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
243 static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
245 struct scsi_cmnd *sc = task->sc;
246 struct iscsi_rlength_ahdr *rlen_ahdr;
249 rlen_ahdr = iscsi_next_hdr(task);
250 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
254 rlen_ahdr->ahslength =
255 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
256 sizeof(rlen_ahdr->reserved));
257 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
258 rlen_ahdr->reserved = 0;
259 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
261 ISCSI_DBG_SESSION(task->conn->session,
262 "bidi-in rlen_ahdr->read_length(%d) "
263 "rlen_ahdr->ahslength(%d)\n",
264 be32_to_cpu(rlen_ahdr->read_length),
265 be16_to_cpu(rlen_ahdr->ahslength));
270 * iscsi_check_tmf_restrictions - check if a task is affected by TMF
272 * @opcode: opcode to check for
274 * During TMF a task has to be checked if it's affected.
275 * All unrelated I/O can be passed through, but I/O to the
276 * affected LUN should be restricted.
277 * If 'fast_abort' is set we won't be sending any I/O to the
279 * Otherwise the target is waiting for all TTTs to be completed,
280 * so we have to send all outstanding Data-Out PDUs to the target.
282 static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
284 struct iscsi_conn *conn = task->conn;
285 struct iscsi_tm *tmf = &conn->tmhdr;
286 unsigned int hdr_lun;
288 if (conn->tmf_state == TMF_INITIAL)
291 if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
294 switch (ISCSI_TM_FUNC_VALUE(tmf)) {
295 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
297 * Allow PDUs for unrelated LUNs
299 hdr_lun = scsilun_to_int((struct scsi_lun *)tmf->lun);
300 if (hdr_lun != task->sc->device->lun)
303 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
305 * Fail all SCSI cmd PDUs
307 if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
308 iscsi_conn_printk(KERN_INFO, conn,
309 "task [op %x/%x itt "
312 task->hdr->opcode, opcode,
313 task->itt, task->hdr_itt);
317 * And also all data-out PDUs in response to R2T
318 * if fast_abort is set.
320 if (conn->session->fast_abort) {
321 iscsi_conn_printk(KERN_INFO, conn,
322 "task [op %x/%x itt "
323 "0x%x/0x%x] fast abort.\n",
324 task->hdr->opcode, opcode,
325 task->itt, task->hdr_itt);
329 case ISCSI_TM_FUNC_ABORT_TASK:
331 * the caller has already checked if the task
332 * they want to abort was in the pending queue so if
333 * we are here the cmd pdu has gone out already, and
334 * we will only hit this for data-outs
336 if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
337 task->hdr_itt == tmf->rtt) {
338 ISCSI_DBG_SESSION(conn->session,
339 "Preventing task %x/%x from sending "
340 "data-out due to abort task in "
341 "progress\n", task->itt,
352 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
355 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
356 * fields like dlength or final based on how much data it sends
358 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
360 struct iscsi_conn *conn = task->conn;
361 struct iscsi_session *session = conn->session;
362 struct scsi_cmnd *sc = task->sc;
363 struct iscsi_cmd *hdr;
364 unsigned hdrlength, cmd_len;
368 rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
372 if (conn->session->tt->alloc_pdu) {
373 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
377 hdr = (struct iscsi_cmd *) task->hdr;
379 memset(hdr, 0, sizeof(*hdr));
381 if (session->tt->parse_pdu_itt)
382 hdr->itt = task->hdr_itt = itt;
384 hdr->itt = task->hdr_itt = build_itt(task->itt,
385 task->conn->session->age);
387 rc = iscsi_add_hdr(task, sizeof(*hdr));
390 hdr->opcode = ISCSI_OP_SCSI_CMD;
391 hdr->flags = ISCSI_ATTR_SIMPLE;
392 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
393 memcpy(task->lun, hdr->lun, sizeof(task->lun));
394 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
395 cmd_len = sc->cmd_len;
396 if (cmd_len < ISCSI_CDB_SIZE)
397 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
398 else if (cmd_len > ISCSI_CDB_SIZE) {
399 rc = iscsi_prep_ecdb_ahs(task);
402 cmd_len = ISCSI_CDB_SIZE;
404 memcpy(hdr->cdb, sc->cmnd, cmd_len);
407 if (scsi_bidi_cmnd(sc)) {
408 hdr->flags |= ISCSI_FLAG_CMD_READ;
409 rc = iscsi_prep_bidi_ahs(task);
413 if (sc->sc_data_direction == DMA_TO_DEVICE) {
414 unsigned out_len = scsi_out(sc)->length;
415 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
417 hdr->data_length = cpu_to_be32(out_len);
418 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
422 * imm_count bytes to be sent right after
425 * unsol_count bytes(as Data-Out) to be sent
426 * without R2T ack right after
429 * r2t data_length bytes to be sent via R2T ack's
431 * pad_count bytes to be sent as zero-padding
433 memset(r2t, 0, sizeof(*r2t));
435 if (session->imm_data_en) {
436 if (out_len >= session->first_burst)
437 task->imm_count = min(session->first_burst,
438 conn->max_xmit_dlength);
440 task->imm_count = min(out_len,
441 conn->max_xmit_dlength);
442 hton24(hdr->dlength, task->imm_count);
444 zero_data(hdr->dlength);
446 if (!session->initial_r2t_en) {
447 r2t->data_length = min(session->first_burst, out_len) -
449 r2t->data_offset = task->imm_count;
450 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
451 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
454 if (!task->unsol_r2t.data_length)
455 /* No unsolicit Data-Out's */
456 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
458 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
459 zero_data(hdr->dlength);
460 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
462 if (sc->sc_data_direction == DMA_FROM_DEVICE)
463 hdr->flags |= ISCSI_FLAG_CMD_READ;
466 /* calculate size of additional header segments (AHSs) */
467 hdrlength = task->hdr_len - sizeof(*hdr);
469 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
470 hdrlength /= ISCSI_PAD_LEN;
472 WARN_ON(hdrlength >= 256);
473 hdr->hlength = hdrlength & 0xFF;
474 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
476 if (session->tt->init_task && session->tt->init_task(task))
479 task->state = ISCSI_TASK_RUNNING;
482 conn->scsicmd_pdus_cnt++;
483 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
484 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
485 scsi_bidi_cmnd(sc) ? "bidirectional" :
486 sc->sc_data_direction == DMA_TO_DEVICE ?
487 "write" : "read", conn->id, sc, sc->cmnd[0],
488 task->itt, scsi_bufflen(sc),
489 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
491 session->max_cmdsn - session->exp_cmdsn + 1);
496 * iscsi_free_task - free a task
497 * @task: iscsi cmd task
499 * Must be called with session lock.
500 * This function returns the scsi command to scsi-ml or cleans
501 * up mgmt tasks then returns the task to the pool.
503 static void iscsi_free_task(struct iscsi_task *task)
505 struct iscsi_conn *conn = task->conn;
506 struct iscsi_session *session = conn->session;
507 struct scsi_cmnd *sc = task->sc;
509 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
510 task->itt, task->state, task->sc);
512 session->tt->cleanup_task(task);
513 task->state = ISCSI_TASK_FREE;
516 * login task is preallocated so do not free
518 if (conn->login_task == task)
521 kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
525 /* SCSI eh reuses commands to verify us */
528 * queue command may call this to free the task, but
529 * not have setup the sc callback
536 void __iscsi_get_task(struct iscsi_task *task)
538 atomic_inc(&task->refcount);
540 EXPORT_SYMBOL_GPL(__iscsi_get_task);
542 static void __iscsi_put_task(struct iscsi_task *task)
544 if (atomic_dec_and_test(&task->refcount))
545 iscsi_free_task(task);
548 void iscsi_put_task(struct iscsi_task *task)
550 struct iscsi_session *session = task->conn->session;
552 spin_lock_bh(&session->lock);
553 __iscsi_put_task(task);
554 spin_unlock_bh(&session->lock);
556 EXPORT_SYMBOL_GPL(iscsi_put_task);
559 * iscsi_complete_task - finish a task
560 * @task: iscsi cmd task
561 * @state: state to complete task with
563 * Must be called with session lock.
565 static void iscsi_complete_task(struct iscsi_task *task, int state)
567 struct iscsi_conn *conn = task->conn;
569 ISCSI_DBG_SESSION(conn->session,
570 "complete task itt 0x%x state %d sc %p\n",
571 task->itt, task->state, task->sc);
572 if (task->state == ISCSI_TASK_COMPLETED ||
573 task->state == ISCSI_TASK_ABRT_TMF ||
574 task->state == ISCSI_TASK_ABRT_SESS_RECOV)
576 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
579 if (!list_empty(&task->running))
580 list_del_init(&task->running);
582 if (conn->task == task)
585 if (conn->ping_task == task)
586 conn->ping_task = NULL;
588 /* release get from queueing */
589 __iscsi_put_task(task);
593 * iscsi_complete_scsi_task - finish scsi task normally
594 * @task: iscsi task for scsi cmd
595 * @exp_cmdsn: expected cmd sn in cpu format
596 * @max_cmdsn: max cmd sn in cpu format
598 * This is used when drivers do not need or cannot perform
599 * lower level pdu processing.
601 * Called with session lock
603 void iscsi_complete_scsi_task(struct iscsi_task *task,
604 uint32_t exp_cmdsn, uint32_t max_cmdsn)
606 struct iscsi_conn *conn = task->conn;
608 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
610 conn->last_recv = jiffies;
611 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
612 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
614 EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
618 * session lock must be held and if not called for a task that is
619 * still pending or from the xmit thread, then xmit thread must
622 static void fail_scsi_task(struct iscsi_task *task, int err)
624 struct iscsi_conn *conn = task->conn;
625 struct scsi_cmnd *sc;
629 * if a command completes and we get a successful tmf response
630 * we will hit this because the scsi eh abort code does not take
637 if (task->state == ISCSI_TASK_PENDING) {
639 * cmd never made it to the xmit thread, so we should not count
640 * the cmd in the sequencing
642 conn->session->queued_cmdsn--;
643 /* it was never sent so just complete like normal */
644 state = ISCSI_TASK_COMPLETED;
645 } else if (err == DID_TRANSPORT_DISRUPTED)
646 state = ISCSI_TASK_ABRT_SESS_RECOV;
648 state = ISCSI_TASK_ABRT_TMF;
650 sc->result = err << 16;
651 if (!scsi_bidi_cmnd(sc))
652 scsi_set_resid(sc, scsi_bufflen(sc));
654 scsi_out(sc)->resid = scsi_out(sc)->length;
655 scsi_in(sc)->resid = scsi_in(sc)->length;
658 iscsi_complete_task(task, state);
661 static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
662 struct iscsi_task *task)
664 struct iscsi_session *session = conn->session;
665 struct iscsi_hdr *hdr = task->hdr;
666 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
667 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
669 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
672 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
673 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
675 * pre-format CmdSN for outgoing PDU.
677 nop->cmdsn = cpu_to_be32(session->cmdsn);
678 if (hdr->itt != RESERVED_ITT) {
680 * TODO: We always use immediate for normal session pdus.
681 * If we start to send tmfs or nops as non-immediate then
682 * we should start checking the cmdsn numbers for mgmt tasks.
684 * During discovery sessions iscsid sends TEXT as non immediate,
685 * but we always only send one PDU at a time.
687 if (conn->c_stage == ISCSI_CONN_STARTED &&
688 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
689 session->queued_cmdsn++;
694 if (session->tt->init_task && session->tt->init_task(task))
697 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
698 session->state = ISCSI_STATE_LOGGING_OUT;
700 task->state = ISCSI_TASK_RUNNING;
701 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
702 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
703 hdr->itt, task->data_count);
707 static struct iscsi_task *
708 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
709 char *data, uint32_t data_size)
711 struct iscsi_session *session = conn->session;
712 struct iscsi_host *ihost = shost_priv(session->host);
713 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
714 struct iscsi_task *task;
717 if (session->state == ISCSI_STATE_TERMINATE)
720 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
722 * Login and Text are sent serially, in
723 * request-followed-by-response sequence.
724 * Same task can be used. Same ITT must be used.
725 * Note that login_task is preallocated at conn_create().
727 if (conn->login_task->state != ISCSI_TASK_FREE) {
728 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
729 "progress. Cannot start new task.\n");
733 task = conn->login_task;
735 if (session->state != ISCSI_STATE_LOGGED_IN)
738 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
739 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
741 if (!kfifo_out(&session->cmdpool.queue,
742 (void*)&task, sizeof(void*)))
746 * released in complete pdu for task we expect a response for, and
747 * released by the lld when it has transmitted the task for
748 * pdus we do not expect a response for.
750 atomic_set(&task->refcount, 1);
753 INIT_LIST_HEAD(&task->running);
754 task->state = ISCSI_TASK_PENDING;
757 memcpy(task->data, data, data_size);
758 task->data_count = data_size;
760 task->data_count = 0;
762 if (conn->session->tt->alloc_pdu) {
763 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
764 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
765 "pdu for mgmt task.\n");
770 itt = task->hdr->itt;
771 task->hdr_len = sizeof(struct iscsi_hdr);
772 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
774 if (hdr->itt != RESERVED_ITT) {
775 if (session->tt->parse_pdu_itt)
776 task->hdr->itt = itt;
778 task->hdr->itt = build_itt(task->itt,
779 task->conn->session->age);
783 if (iscsi_prep_mgmt_task(conn, task))
786 if (session->tt->xmit_task(task))
789 list_add_tail(&task->running, &conn->mgmtqueue);
790 iscsi_conn_queue_work(conn);
796 __iscsi_put_task(task);
800 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
801 char *data, uint32_t data_size)
803 struct iscsi_conn *conn = cls_conn->dd_data;
804 struct iscsi_session *session = conn->session;
807 spin_lock_bh(&session->lock);
808 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
810 spin_unlock_bh(&session->lock);
813 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
816 * iscsi_cmd_rsp - SCSI Command Response processing
817 * @conn: iscsi connection
819 * @task: scsi command task
820 * @data: cmd data buffer
821 * @datalen: len of buffer
823 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
824 * then completes the command and task.
826 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
827 struct iscsi_task *task, char *data,
830 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
831 struct iscsi_session *session = conn->session;
832 struct scsi_cmnd *sc = task->sc;
834 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
835 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
837 sc->result = (DID_OK << 16) | rhdr->cmd_status;
839 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
840 sc->result = DID_ERROR << 16;
844 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
849 iscsi_conn_printk(KERN_ERR, conn,
850 "Got CHECK_CONDITION but invalid data "
851 "buffer size of %d\n", datalen);
852 sc->result = DID_BAD_TARGET << 16;
856 senselen = get_unaligned_be16(data);
857 if (datalen < senselen)
858 goto invalid_datalen;
860 memcpy(sc->sense_buffer, data + 2,
861 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
862 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
863 min_t(uint16_t, senselen,
864 SCSI_SENSE_BUFFERSIZE));
867 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
868 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
869 int res_count = be32_to_cpu(rhdr->bi_residual_count);
871 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
872 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
873 res_count <= scsi_in(sc)->length))
874 scsi_in(sc)->resid = res_count;
876 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
879 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
880 ISCSI_FLAG_CMD_OVERFLOW)) {
881 int res_count = be32_to_cpu(rhdr->residual_count);
884 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
885 res_count <= scsi_bufflen(sc)))
886 /* write side for bidi or uni-io set_resid */
887 scsi_set_resid(sc, res_count);
889 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
892 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
893 sc, sc->result, task->itt);
894 conn->scsirsp_pdus_cnt++;
895 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
899 * iscsi_data_in_rsp - SCSI Data-In Response processing
900 * @conn: iscsi connection
902 * @task: scsi command task
905 iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
906 struct iscsi_task *task)
908 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
909 struct scsi_cmnd *sc = task->sc;
911 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
914 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
915 sc->result = (DID_OK << 16) | rhdr->cmd_status;
916 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
917 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
918 ISCSI_FLAG_DATA_OVERFLOW)) {
919 int res_count = be32_to_cpu(rhdr->residual_count);
922 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
923 res_count <= scsi_in(sc)->length))
924 scsi_in(sc)->resid = res_count;
926 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
929 ISCSI_DBG_SESSION(conn->session, "data in with status done "
930 "[sc %p res %d itt 0x%x]\n",
931 sc, sc->result, task->itt);
932 conn->scsirsp_pdus_cnt++;
933 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
936 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
938 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
940 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
941 conn->tmfrsp_pdus_cnt++;
943 if (conn->tmf_state != TMF_QUEUED)
946 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
947 conn->tmf_state = TMF_SUCCESS;
948 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
949 conn->tmf_state = TMF_NOT_FOUND;
951 conn->tmf_state = TMF_FAILED;
952 wake_up(&conn->ehwait);
955 static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
957 struct iscsi_nopout hdr;
958 struct iscsi_task *task;
960 if (!rhdr && conn->ping_task)
963 memset(&hdr, 0, sizeof(struct iscsi_nopout));
964 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
965 hdr.flags = ISCSI_FLAG_CMD_FINAL;
968 memcpy(hdr.lun, rhdr->lun, 8);
970 hdr.itt = RESERVED_ITT;
972 hdr.ttt = RESERVED_ITT;
974 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
976 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
978 /* only track our nops */
979 conn->ping_task = task;
980 conn->last_ping = jiffies;
984 static int iscsi_nop_out_rsp(struct iscsi_task *task,
985 struct iscsi_nopin *nop, char *data, int datalen)
987 struct iscsi_conn *conn = task->conn;
990 if (conn->ping_task != task) {
992 * If this is not in response to one of our
993 * nops then it must be from userspace.
995 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
997 rc = ISCSI_ERR_CONN_FAILED;
999 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
1000 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1004 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1005 char *data, int datalen)
1007 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
1008 struct iscsi_hdr rejected_pdu;
1011 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
1013 if (ntoh24(reject->dlength) > datalen ||
1014 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
1015 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
1016 "pdu. Invalid data length (pdu dlength "
1017 "%u, datalen %d\n", ntoh24(reject->dlength),
1019 return ISCSI_ERR_PROTO;
1021 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
1022 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
1024 switch (reject->reason) {
1025 case ISCSI_REASON_DATA_DIGEST_ERROR:
1026 iscsi_conn_printk(KERN_ERR, conn,
1027 "pdu (op 0x%x itt 0x%x) rejected "
1028 "due to DataDigest error.\n",
1029 rejected_pdu.itt, opcode);
1031 case ISCSI_REASON_IMM_CMD_REJECT:
1032 iscsi_conn_printk(KERN_ERR, conn,
1033 "pdu (op 0x%x itt 0x%x) rejected. Too many "
1034 "immediate commands.\n",
1035 rejected_pdu.itt, opcode);
1037 * We only send one TMF at a time so if the target could not
1038 * handle it, then it should get fixed (RFC mandates that
1039 * a target can handle one immediate TMF per conn).
1041 * For nops-outs, we could have sent more than one if
1042 * the target is sending us lots of nop-ins
1044 if (opcode != ISCSI_OP_NOOP_OUT)
1047 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG))
1049 * nop-out in response to target's nop-out rejected.
1052 iscsi_send_nopout(conn,
1053 (struct iscsi_nopin*)&rejected_pdu);
1055 struct iscsi_task *task;
1057 * Our nop as ping got dropped. We know the target
1058 * and transport are ok so just clean up
1060 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
1062 iscsi_conn_printk(KERN_ERR, conn,
1063 "Invalid pdu reject. Could "
1064 "not lookup rejected task.\n");
1065 rc = ISCSI_ERR_BAD_ITT;
1067 rc = iscsi_nop_out_rsp(task,
1068 (struct iscsi_nopin*)&rejected_pdu,
1073 iscsi_conn_printk(KERN_ERR, conn,
1074 "pdu (op 0x%x itt 0x%x) rejected. Reason "
1075 "code 0x%x\n", rejected_pdu.itt,
1076 rejected_pdu.opcode, reject->reason);
1083 * iscsi_itt_to_task - look up task by itt
1084 * @conn: iscsi connection
1087 * This should be used for mgmt tasks like login and nops, or if
1088 * the LDD's itt space does not include the session age.
1090 * The session lock must be held.
1092 struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
1094 struct iscsi_session *session = conn->session;
1097 if (itt == RESERVED_ITT)
1100 if (session->tt->parse_pdu_itt)
1101 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1104 if (i >= session->cmds_max)
1107 return session->cmds[i];
1109 EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
1112 * __iscsi_complete_pdu - complete pdu
1114 * @hdr: iscsi header
1115 * @data: data buffer
1116 * @datalen: len of data buffer
1118 * Completes pdu processing by freeing any resources allocated at
1119 * queuecommand or send generic. session lock must be held and verify
1120 * itt must have been called.
1122 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1123 char *data, int datalen)
1125 struct iscsi_session *session = conn->session;
1126 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
1127 struct iscsi_task *task;
1130 conn->last_recv = jiffies;
1131 rc = iscsi_verify_itt(conn, hdr->itt);
1135 if (hdr->itt != RESERVED_ITT)
1136 itt = get_itt(hdr->itt);
1140 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1141 opcode, conn->id, itt, datalen);
1144 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1147 case ISCSI_OP_NOOP_IN:
1149 rc = ISCSI_ERR_PROTO;
1153 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
1156 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
1158 case ISCSI_OP_REJECT:
1159 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1161 case ISCSI_OP_ASYNC_EVENT:
1162 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1163 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1164 rc = ISCSI_ERR_CONN_FAILED;
1167 rc = ISCSI_ERR_BAD_OPCODE;
1174 case ISCSI_OP_SCSI_CMD_RSP:
1175 case ISCSI_OP_SCSI_DATA_IN:
1176 task = iscsi_itt_to_ctask(conn, hdr->itt);
1178 return ISCSI_ERR_BAD_ITT;
1179 task->last_xfer = jiffies;
1183 * LLD handles R2Ts if they need to.
1186 case ISCSI_OP_LOGOUT_RSP:
1187 case ISCSI_OP_LOGIN_RSP:
1188 case ISCSI_OP_TEXT_RSP:
1189 case ISCSI_OP_SCSI_TMFUNC_RSP:
1190 case ISCSI_OP_NOOP_IN:
1191 task = iscsi_itt_to_task(conn, hdr->itt);
1193 return ISCSI_ERR_BAD_ITT;
1196 return ISCSI_ERR_BAD_OPCODE;
1200 case ISCSI_OP_SCSI_CMD_RSP:
1201 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
1203 case ISCSI_OP_SCSI_DATA_IN:
1204 iscsi_data_in_rsp(conn, hdr, task);
1206 case ISCSI_OP_LOGOUT_RSP:
1207 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1209 rc = ISCSI_ERR_PROTO;
1212 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1214 case ISCSI_OP_LOGIN_RSP:
1215 case ISCSI_OP_TEXT_RSP:
1216 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1218 * login related PDU's exp_statsn is handled in
1222 case ISCSI_OP_SCSI_TMFUNC_RSP:
1223 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1225 rc = ISCSI_ERR_PROTO;
1229 iscsi_tmf_rsp(conn, hdr);
1230 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1232 case ISCSI_OP_NOOP_IN:
1233 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1234 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1235 rc = ISCSI_ERR_PROTO;
1238 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1240 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1244 rc = ISCSI_ERR_BAD_OPCODE;
1251 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1252 rc = ISCSI_ERR_CONN_FAILED;
1253 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1256 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
1258 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1259 char *data, int datalen)
1263 spin_lock(&conn->session->lock);
1264 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1265 spin_unlock(&conn->session->lock);
1268 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1270 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
1272 struct iscsi_session *session = conn->session;
1275 if (itt == RESERVED_ITT)
1278 if (session->tt->parse_pdu_itt)
1279 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1282 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1285 if (age != session->age) {
1286 iscsi_conn_printk(KERN_ERR, conn,
1287 "received itt %x expected session age (%x)\n",
1288 (__force u32)itt, session->age);
1289 return ISCSI_ERR_BAD_ITT;
1292 if (i >= session->cmds_max) {
1293 iscsi_conn_printk(KERN_ERR, conn,
1294 "received invalid itt index %u (max cmds "
1295 "%u.\n", i, session->cmds_max);
1296 return ISCSI_ERR_BAD_ITT;
1300 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1303 * iscsi_itt_to_ctask - look up ctask by itt
1304 * @conn: iscsi connection
1307 * This should be used for cmd tasks.
1309 * The session lock must be held.
1311 struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
1313 struct iscsi_task *task;
1315 if (iscsi_verify_itt(conn, itt))
1318 task = iscsi_itt_to_task(conn, itt);
1319 if (!task || !task->sc)
1322 if (task->sc->SCp.phase != conn->session->age) {
1323 iscsi_session_printk(KERN_ERR, conn->session,
1324 "task's session age %d, expected %d\n",
1325 task->sc->SCp.phase, conn->session->age);
1331 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1333 void iscsi_session_failure(struct iscsi_session *session,
1336 struct iscsi_conn *conn;
1338 unsigned long flags;
1340 spin_lock_irqsave(&session->lock, flags);
1341 conn = session->leadconn;
1342 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1343 spin_unlock_irqrestore(&session->lock, flags);
1347 dev = get_device(&conn->cls_conn->dev);
1348 spin_unlock_irqrestore(&session->lock, flags);
1352 * if the host is being removed bypass the connection
1353 * recovery initialization because we are going to kill
1356 if (err == ISCSI_ERR_INVALID_HOST)
1357 iscsi_conn_error_event(conn->cls_conn, err);
1359 iscsi_conn_failure(conn, err);
1362 EXPORT_SYMBOL_GPL(iscsi_session_failure);
1364 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1366 struct iscsi_session *session = conn->session;
1367 unsigned long flags;
1369 spin_lock_irqsave(&session->lock, flags);
1370 if (session->state == ISCSI_STATE_FAILED) {
1371 spin_unlock_irqrestore(&session->lock, flags);
1375 if (conn->stop_stage == 0)
1376 session->state = ISCSI_STATE_FAILED;
1377 spin_unlock_irqrestore(&session->lock, flags);
1379 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1380 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1381 iscsi_conn_error_event(conn->cls_conn, err);
1383 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1385 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1387 struct iscsi_session *session = conn->session;
1390 * Check for iSCSI window and take care of CmdSN wrap-around
1392 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1393 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1394 "%u MaxCmdSN %u CmdSN %u/%u\n",
1395 session->exp_cmdsn, session->max_cmdsn,
1396 session->cmdsn, session->queued_cmdsn);
1402 static int iscsi_xmit_task(struct iscsi_conn *conn)
1404 struct iscsi_task *task = conn->task;
1407 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1410 __iscsi_get_task(task);
1411 spin_unlock_bh(&conn->session->lock);
1412 rc = conn->session->tt->xmit_task(task);
1413 spin_lock_bh(&conn->session->lock);
1415 /* done with this task */
1416 task->last_xfer = jiffies;
1419 __iscsi_put_task(task);
1424 * iscsi_requeue_task - requeue task to run from session workqueue
1425 * @task: task to requeue
1427 * LLDs that need to run a task from the session workqueue should call
1428 * this. The session lock must be held. This should only be called
1429 * by software drivers.
1431 void iscsi_requeue_task(struct iscsi_task *task)
1433 struct iscsi_conn *conn = task->conn;
1436 * this may be on the requeue list already if the xmit_task callout
1437 * is handling the r2ts while we are adding new ones
1439 if (list_empty(&task->running))
1440 list_add_tail(&task->running, &conn->requeue);
1441 iscsi_conn_queue_work(conn);
1443 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1446 * iscsi_data_xmit - xmit any command into the scheduled connection
1447 * @conn: iscsi connection
1450 * The function can return -EAGAIN in which case the caller must
1451 * re-schedule it again later or recover. '0' return code means
1454 static int iscsi_data_xmit(struct iscsi_conn *conn)
1456 struct iscsi_task *task;
1459 spin_lock_bh(&conn->session->lock);
1460 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1461 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1462 spin_unlock_bh(&conn->session->lock);
1467 rc = iscsi_xmit_task(conn);
1473 * process mgmt pdus like nops before commands since we should
1474 * only have one nop-out as a ping from us and targets should not
1475 * overflow us with nop-ins
1478 while (!list_empty(&conn->mgmtqueue)) {
1479 conn->task = list_entry(conn->mgmtqueue.next,
1480 struct iscsi_task, running);
1481 list_del_init(&conn->task->running);
1482 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1483 __iscsi_put_task(conn->task);
1487 rc = iscsi_xmit_task(conn);
1492 /* process pending command queue */
1493 while (!list_empty(&conn->cmdqueue)) {
1494 conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1496 list_del_init(&conn->task->running);
1497 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1498 fail_scsi_task(conn->task, DID_IMM_RETRY);
1501 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1503 if (rc == -ENOMEM || rc == -EACCES) {
1504 list_add_tail(&conn->task->running,
1509 fail_scsi_task(conn->task, DID_ABORT);
1512 rc = iscsi_xmit_task(conn);
1516 * we could continuously get new task requests so
1517 * we need to check the mgmt queue for nops that need to
1518 * be sent to aviod starvation
1520 if (!list_empty(&conn->mgmtqueue))
1524 while (!list_empty(&conn->requeue)) {
1526 * we always do fastlogout - conn stop code will clean up.
1528 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1531 task = list_entry(conn->requeue.next, struct iscsi_task,
1533 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1537 list_del_init(&conn->task->running);
1538 conn->task->state = ISCSI_TASK_RUNNING;
1539 rc = iscsi_xmit_task(conn);
1542 if (!list_empty(&conn->mgmtqueue))
1545 spin_unlock_bh(&conn->session->lock);
1549 spin_unlock_bh(&conn->session->lock);
1553 static void iscsi_xmitworker(struct work_struct *work)
1555 struct iscsi_conn *conn =
1556 container_of(work, struct iscsi_conn, xmitwork);
1559 * serialize Xmit worker on a per-connection basis.
1562 rc = iscsi_data_xmit(conn);
1563 } while (rc >= 0 || rc == -EAGAIN);
1566 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1567 struct scsi_cmnd *sc)
1569 struct iscsi_task *task;
1571 if (!kfifo_out(&conn->session->cmdpool.queue,
1572 (void *) &task, sizeof(void *)))
1575 sc->SCp.phase = conn->session->age;
1576 sc->SCp.ptr = (char *) task;
1578 atomic_set(&task->refcount, 1);
1579 task->state = ISCSI_TASK_PENDING;
1582 task->have_checked_conn = false;
1583 task->last_timeout = jiffies;
1584 task->last_xfer = jiffies;
1585 INIT_LIST_HEAD(&task->running);
1590 FAILURE_BAD_HOST = 1,
1591 FAILURE_SESSION_FAILED,
1592 FAILURE_SESSION_FREED,
1593 FAILURE_WINDOW_CLOSED,
1595 FAILURE_SESSION_TERMINATE,
1596 FAILURE_SESSION_IN_RECOVERY,
1597 FAILURE_SESSION_RECOVERY_TIMEOUT,
1598 FAILURE_SESSION_LOGGING_OUT,
1599 FAILURE_SESSION_NOT_READY,
1602 static int iscsi_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1604 struct iscsi_cls_session *cls_session;
1605 struct Scsi_Host *host;
1606 struct iscsi_host *ihost;
1608 struct iscsi_session *session;
1609 struct iscsi_conn *conn;
1610 struct iscsi_task *task = NULL;
1612 sc->scsi_done = done;
1616 host = sc->device->host;
1617 ihost = shost_priv(host);
1618 spin_unlock(host->host_lock);
1620 cls_session = starget_to_session(scsi_target(sc->device));
1621 session = cls_session->dd_data;
1622 spin_lock(&session->lock);
1624 reason = iscsi_session_chkready(cls_session);
1626 sc->result = reason;
1630 if (session->state != ISCSI_STATE_LOGGED_IN) {
1632 * to handle the race between when we set the recovery state
1633 * and block the session we requeue here (commands could
1634 * be entering our queuecommand while a block is starting
1635 * up because the block code is not locked)
1637 switch (session->state) {
1638 case ISCSI_STATE_FAILED:
1639 case ISCSI_STATE_IN_RECOVERY:
1640 reason = FAILURE_SESSION_IN_RECOVERY;
1641 sc->result = DID_IMM_RETRY << 16;
1643 case ISCSI_STATE_LOGGING_OUT:
1644 reason = FAILURE_SESSION_LOGGING_OUT;
1645 sc->result = DID_IMM_RETRY << 16;
1647 case ISCSI_STATE_RECOVERY_FAILED:
1648 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1649 sc->result = DID_TRANSPORT_FAILFAST << 16;
1651 case ISCSI_STATE_TERMINATE:
1652 reason = FAILURE_SESSION_TERMINATE;
1653 sc->result = DID_NO_CONNECT << 16;
1656 reason = FAILURE_SESSION_FREED;
1657 sc->result = DID_NO_CONNECT << 16;
1662 conn = session->leadconn;
1664 reason = FAILURE_SESSION_FREED;
1665 sc->result = DID_NO_CONNECT << 16;
1669 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1670 reason = FAILURE_SESSION_IN_RECOVERY;
1671 sc->result = DID_REQUEUE;
1675 if (iscsi_check_cmdsn_window_closed(conn)) {
1676 reason = FAILURE_WINDOW_CLOSED;
1680 task = iscsi_alloc_task(conn, sc);
1682 reason = FAILURE_OOM;
1686 if (!ihost->workq) {
1687 reason = iscsi_prep_scsi_cmd_pdu(task);
1689 if (reason == -ENOMEM || reason == -EACCES) {
1690 reason = FAILURE_OOM;
1693 sc->result = DID_ABORT << 16;
1697 if (session->tt->xmit_task(task)) {
1699 reason = FAILURE_SESSION_NOT_READY;
1703 list_add_tail(&task->running, &conn->cmdqueue);
1704 iscsi_conn_queue_work(conn);
1707 session->queued_cmdsn++;
1708 spin_unlock(&session->lock);
1709 spin_lock(host->host_lock);
1713 sc->scsi_done = NULL;
1714 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1716 spin_unlock(&session->lock);
1717 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1718 sc->cmnd[0], reason);
1719 spin_lock(host->host_lock);
1720 return SCSI_MLQUEUE_TARGET_BUSY;
1723 sc->scsi_done = NULL;
1724 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1726 spin_unlock(&session->lock);
1727 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1728 sc->cmnd[0], reason);
1729 if (!scsi_bidi_cmnd(sc))
1730 scsi_set_resid(sc, scsi_bufflen(sc));
1732 scsi_out(sc)->resid = scsi_out(sc)->length;
1733 scsi_in(sc)->resid = scsi_in(sc)->length;
1736 spin_lock(host->host_lock);
1740 DEF_SCSI_QCMD(iscsi_queuecommand)
1741 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1743 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
1746 case SCSI_QDEPTH_DEFAULT:
1747 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1749 case SCSI_QDEPTH_QFULL:
1750 scsi_track_queue_full(sdev, depth);
1752 case SCSI_QDEPTH_RAMP_UP:
1753 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1758 return sdev->queue_depth;
1760 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1762 int iscsi_target_alloc(struct scsi_target *starget)
1764 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1765 struct iscsi_session *session = cls_session->dd_data;
1767 starget->can_queue = session->scsi_cmds_max;
1770 EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1772 static void iscsi_tmf_timedout(unsigned long data)
1774 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1775 struct iscsi_session *session = conn->session;
1777 spin_lock(&session->lock);
1778 if (conn->tmf_state == TMF_QUEUED) {
1779 conn->tmf_state = TMF_TIMEDOUT;
1780 ISCSI_DBG_EH(session, "tmf timedout\n");
1781 /* unblock eh_abort() */
1782 wake_up(&conn->ehwait);
1784 spin_unlock(&session->lock);
1787 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1788 struct iscsi_tm *hdr, int age,
1791 struct iscsi_session *session = conn->session;
1792 struct iscsi_task *task;
1794 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1797 spin_unlock_bh(&session->lock);
1798 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1799 spin_lock_bh(&session->lock);
1800 ISCSI_DBG_EH(session, "tmf exec failure\n");
1803 conn->tmfcmd_pdus_cnt++;
1804 conn->tmf_timer.expires = timeout * HZ + jiffies;
1805 conn->tmf_timer.function = iscsi_tmf_timedout;
1806 conn->tmf_timer.data = (unsigned long)conn;
1807 add_timer(&conn->tmf_timer);
1808 ISCSI_DBG_EH(session, "tmf set timeout\n");
1810 spin_unlock_bh(&session->lock);
1811 mutex_unlock(&session->eh_mutex);
1814 * block eh thread until:
1818 * 3) session is terminated or restarted or userspace has
1819 * given up on recovery
1821 wait_event_interruptible(conn->ehwait, age != session->age ||
1822 session->state != ISCSI_STATE_LOGGED_IN ||
1823 conn->tmf_state != TMF_QUEUED);
1824 if (signal_pending(current))
1825 flush_signals(current);
1826 del_timer_sync(&conn->tmf_timer);
1828 mutex_lock(&session->eh_mutex);
1829 spin_lock_bh(&session->lock);
1830 /* if the session drops it will clean up the task */
1831 if (age != session->age ||
1832 session->state != ISCSI_STATE_LOGGED_IN)
1838 * Fail commands. session lock held and recv side suspended and xmit
1841 static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1844 struct iscsi_task *task;
1847 for (i = 0; i < conn->session->cmds_max; i++) {
1848 task = conn->session->cmds[i];
1849 if (!task->sc || task->state == ISCSI_TASK_FREE)
1852 if (lun != -1 && lun != task->sc->device->lun)
1855 ISCSI_DBG_SESSION(conn->session,
1856 "failing sc %p itt 0x%x state %d\n",
1857 task->sc, task->itt, task->state);
1858 fail_scsi_task(task, error);
1863 * iscsi_suspend_queue - suspend iscsi_queuecommand
1864 * @conn: iscsi conn to stop queueing IO on
1866 * This grabs the session lock to make sure no one is in
1867 * xmit_task/queuecommand, and then sets suspend to prevent
1868 * new commands from being queued. This only needs to be called
1869 * by offload drivers that need to sync a path like ep disconnect
1870 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1871 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1873 void iscsi_suspend_queue(struct iscsi_conn *conn)
1875 spin_lock_bh(&conn->session->lock);
1876 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1877 spin_unlock_bh(&conn->session->lock);
1879 EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1882 * iscsi_suspend_tx - suspend iscsi_data_xmit
1883 * @conn: iscsi conn tp stop processing IO on.
1885 * This function sets the suspend bit to prevent iscsi_data_xmit
1886 * from sending new IO, and if work is queued on the xmit thread
1887 * it will wait for it to be completed.
1889 void iscsi_suspend_tx(struct iscsi_conn *conn)
1891 struct Scsi_Host *shost = conn->session->host;
1892 struct iscsi_host *ihost = shost_priv(shost);
1894 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1896 flush_workqueue(ihost->workq);
1898 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1900 static void iscsi_start_tx(struct iscsi_conn *conn)
1902 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1903 iscsi_conn_queue_work(conn);
1907 * We want to make sure a ping is in flight. It has timed out.
1908 * And we are not busy processing a pdu that is making
1909 * progress but got started before the ping and is taking a while
1910 * to complete so the ping is just stuck behind it in a queue.
1912 static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1914 if (conn->ping_task &&
1915 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1916 (conn->ping_timeout * HZ), jiffies))
1922 static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1924 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
1925 struct iscsi_task *task = NULL, *running_task;
1926 struct iscsi_cls_session *cls_session;
1927 struct iscsi_session *session;
1928 struct iscsi_conn *conn;
1931 cls_session = starget_to_session(scsi_target(sc->device));
1932 session = cls_session->dd_data;
1934 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1936 spin_lock(&session->lock);
1937 if (session->state != ISCSI_STATE_LOGGED_IN) {
1939 * We are probably in the middle of iscsi recovery so let
1940 * that complete and handle the error.
1942 rc = BLK_EH_RESET_TIMER;
1946 conn = session->leadconn;
1948 /* In the middle of shuting down */
1949 rc = BLK_EH_RESET_TIMER;
1953 task = (struct iscsi_task *)sc->SCp.ptr;
1956 * Raced with completion. Just reset timer, and let it
1959 rc = BLK_EH_RESET_TIMER;
1964 * If we have sent (at least queued to the network layer) a pdu or
1965 * recvd one for the task since the last timeout ask for
1966 * more time. If on the next timeout we have not made progress
1967 * we can check if it is the task or connection when we send the
1970 if (time_after(task->last_xfer, task->last_timeout)) {
1971 ISCSI_DBG_EH(session, "Command making progress. Asking "
1972 "scsi-ml for more time to complete. "
1973 "Last data xfer at %lu. Last timeout was at "
1974 "%lu\n.", task->last_xfer, task->last_timeout);
1975 task->have_checked_conn = false;
1976 rc = BLK_EH_RESET_TIMER;
1980 if (!conn->recv_timeout && !conn->ping_timeout)
1983 * if the ping timedout then we are in the middle of cleaning up
1984 * and can let the iscsi eh handle it
1986 if (iscsi_has_ping_timed_out(conn)) {
1987 rc = BLK_EH_RESET_TIMER;
1991 for (i = 0; i < conn->session->cmds_max; i++) {
1992 running_task = conn->session->cmds[i];
1993 if (!running_task->sc || running_task == task ||
1994 running_task->state != ISCSI_TASK_RUNNING)
1998 * Only check if cmds started before this one have made
1999 * progress, or this could never fail
2001 if (time_after(running_task->sc->jiffies_at_alloc,
2002 task->sc->jiffies_at_alloc))
2005 if (time_after(running_task->last_xfer, task->last_timeout)) {
2007 * This task has not made progress, but a task
2008 * started before us has transferred data since
2009 * we started/last-checked. We could be queueing
2010 * too many tasks or the LU is bad.
2012 * If the device is bad the cmds ahead of us on
2013 * other devs will complete, and this loop will
2014 * eventually fail starting the scsi eh.
2016 ISCSI_DBG_EH(session, "Command has not made progress "
2017 "but commands ahead of it have. "
2018 "Asking scsi-ml for more time to "
2019 "complete. Our last xfer vs running task "
2020 "last xfer %lu/%lu. Last check %lu.\n",
2021 task->last_xfer, running_task->last_xfer,
2022 task->last_timeout);
2023 rc = BLK_EH_RESET_TIMER;
2028 /* Assumes nop timeout is shorter than scsi cmd timeout */
2029 if (task->have_checked_conn)
2033 * Checking the transport already or nop from a cmd timeout still
2036 if (conn->ping_task) {
2037 task->have_checked_conn = true;
2038 rc = BLK_EH_RESET_TIMER;
2042 /* Make sure there is a transport check done */
2043 iscsi_send_nopout(conn, NULL);
2044 task->have_checked_conn = true;
2045 rc = BLK_EH_RESET_TIMER;
2049 task->last_timeout = jiffies;
2050 spin_unlock(&session->lock);
2051 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2052 "timer reset" : "nh");
2056 static void iscsi_check_transport_timeouts(unsigned long data)
2058 struct iscsi_conn *conn = (struct iscsi_conn *)data;
2059 struct iscsi_session *session = conn->session;
2060 unsigned long recv_timeout, next_timeout = 0, last_recv;
2062 spin_lock(&session->lock);
2063 if (session->state != ISCSI_STATE_LOGGED_IN)
2066 recv_timeout = conn->recv_timeout;
2071 last_recv = conn->last_recv;
2073 if (iscsi_has_ping_timed_out(conn)) {
2074 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
2075 "expired, recv timeout %d, last rx %lu, "
2076 "last ping %lu, now %lu\n",
2077 conn->ping_timeout, conn->recv_timeout,
2078 last_recv, conn->last_ping, jiffies);
2079 spin_unlock(&session->lock);
2080 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2084 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2085 /* send a ping to try to provoke some traffic */
2086 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
2087 iscsi_send_nopout(conn, NULL);
2088 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2090 next_timeout = last_recv + recv_timeout;
2092 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2093 mod_timer(&conn->transport_timer, next_timeout);
2095 spin_unlock(&session->lock);
2098 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2099 struct iscsi_tm *hdr)
2101 memset(hdr, 0, sizeof(*hdr));
2102 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2103 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2104 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2105 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
2106 hdr->rtt = task->hdr_itt;
2107 hdr->refcmdsn = task->cmdsn;
2110 int iscsi_eh_abort(struct scsi_cmnd *sc)
2112 struct iscsi_cls_session *cls_session;
2113 struct iscsi_session *session;
2114 struct iscsi_conn *conn;
2115 struct iscsi_task *task;
2116 struct iscsi_tm *hdr;
2119 cls_session = starget_to_session(scsi_target(sc->device));
2120 session = cls_session->dd_data;
2122 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
2124 mutex_lock(&session->eh_mutex);
2125 spin_lock_bh(&session->lock);
2127 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2131 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2133 spin_unlock_bh(&session->lock);
2134 mutex_unlock(&session->eh_mutex);
2139 * If we are not logged in or we have started a new session
2140 * then let the host reset code handle this
2142 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2143 sc->SCp.phase != session->age) {
2144 spin_unlock_bh(&session->lock);
2145 mutex_unlock(&session->eh_mutex);
2146 ISCSI_DBG_EH(session, "failing abort due to dropped "
2151 conn = session->leadconn;
2152 conn->eh_abort_cnt++;
2155 task = (struct iscsi_task *)sc->SCp.ptr;
2156 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2159 /* task completed before time out */
2161 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
2165 if (task->state == ISCSI_TASK_PENDING) {
2166 fail_scsi_task(task, DID_ABORT);
2170 /* only have one tmf outstanding at a time */
2171 if (conn->tmf_state != TMF_INITIAL)
2173 conn->tmf_state = TMF_QUEUED;
2176 iscsi_prep_abort_task_pdu(task, hdr);
2178 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
2183 switch (conn->tmf_state) {
2185 spin_unlock_bh(&session->lock);
2187 * stop tx side incase the target had sent a abort rsp but
2188 * the initiator was still writing out data.
2190 iscsi_suspend_tx(conn);
2192 * we do not stop the recv side because targets have been
2193 * good and have never sent us a successful tmf response
2194 * then sent more data for the cmd.
2196 spin_lock_bh(&session->lock);
2197 fail_scsi_task(task, DID_ABORT);
2198 conn->tmf_state = TMF_INITIAL;
2199 memset(hdr, 0, sizeof(*hdr));
2200 spin_unlock_bh(&session->lock);
2201 iscsi_start_tx(conn);
2202 goto success_unlocked;
2204 spin_unlock_bh(&session->lock);
2205 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2206 goto failed_unlocked;
2209 conn->tmf_state = TMF_INITIAL;
2210 memset(hdr, 0, sizeof(*hdr));
2211 /* task completed before tmf abort response */
2212 ISCSI_DBG_EH(session, "sc completed while abort in "
2218 conn->tmf_state = TMF_INITIAL;
2223 spin_unlock_bh(&session->lock);
2225 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2227 mutex_unlock(&session->eh_mutex);
2231 spin_unlock_bh(&session->lock);
2233 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2234 task ? task->itt : 0);
2235 mutex_unlock(&session->eh_mutex);
2238 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2240 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2242 memset(hdr, 0, sizeof(*hdr));
2243 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2244 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2245 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2246 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
2247 hdr->rtt = RESERVED_ITT;
2250 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2252 struct iscsi_cls_session *cls_session;
2253 struct iscsi_session *session;
2254 struct iscsi_conn *conn;
2255 struct iscsi_tm *hdr;
2258 cls_session = starget_to_session(scsi_target(sc->device));
2259 session = cls_session->dd_data;
2261 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
2263 mutex_lock(&session->eh_mutex);
2264 spin_lock_bh(&session->lock);
2266 * Just check if we are not logged in. We cannot check for
2267 * the phase because the reset could come from a ioctl.
2269 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2271 conn = session->leadconn;
2273 /* only have one tmf outstanding at a time */
2274 if (conn->tmf_state != TMF_INITIAL)
2276 conn->tmf_state = TMF_QUEUED;
2279 iscsi_prep_lun_reset_pdu(sc, hdr);
2281 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2282 session->lu_reset_timeout)) {
2287 switch (conn->tmf_state) {
2291 spin_unlock_bh(&session->lock);
2292 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2295 conn->tmf_state = TMF_INITIAL;
2300 spin_unlock_bh(&session->lock);
2302 iscsi_suspend_tx(conn);
2304 spin_lock_bh(&session->lock);
2305 memset(hdr, 0, sizeof(*hdr));
2306 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2307 conn->tmf_state = TMF_INITIAL;
2308 spin_unlock_bh(&session->lock);
2310 iscsi_start_tx(conn);
2314 spin_unlock_bh(&session->lock);
2316 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2317 rc == SUCCESS ? "SUCCESS" : "FAILED");
2318 mutex_unlock(&session->eh_mutex);
2321 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2323 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2325 struct iscsi_session *session = cls_session->dd_data;
2327 spin_lock_bh(&session->lock);
2328 if (session->state != ISCSI_STATE_LOGGED_IN) {
2329 session->state = ISCSI_STATE_RECOVERY_FAILED;
2330 if (session->leadconn)
2331 wake_up(&session->leadconn->ehwait);
2333 spin_unlock_bh(&session->lock);
2335 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
2338 * iscsi_eh_session_reset - drop session and attempt relogin
2341 * This function will wait for a relogin, session termination from
2342 * userspace, or a recovery/replacement timeout.
2344 int iscsi_eh_session_reset(struct scsi_cmnd *sc)
2346 struct iscsi_cls_session *cls_session;
2347 struct iscsi_session *session;
2348 struct iscsi_conn *conn;
2350 cls_session = starget_to_session(scsi_target(sc->device));
2351 session = cls_session->dd_data;
2352 conn = session->leadconn;
2354 mutex_lock(&session->eh_mutex);
2355 spin_lock_bh(&session->lock);
2356 if (session->state == ISCSI_STATE_TERMINATE) {
2358 ISCSI_DBG_EH(session,
2359 "failing session reset: Could not log back into "
2360 "%s, %s [age %d]\n", session->targetname,
2361 conn->persistent_address, session->age);
2362 spin_unlock_bh(&session->lock);
2363 mutex_unlock(&session->eh_mutex);
2367 spin_unlock_bh(&session->lock);
2368 mutex_unlock(&session->eh_mutex);
2370 * we drop the lock here but the leadconn cannot be destoyed while
2371 * we are in the scsi eh
2373 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2375 ISCSI_DBG_EH(session, "wait for relogin\n");
2376 wait_event_interruptible(conn->ehwait,
2377 session->state == ISCSI_STATE_TERMINATE ||
2378 session->state == ISCSI_STATE_LOGGED_IN ||
2379 session->state == ISCSI_STATE_RECOVERY_FAILED);
2380 if (signal_pending(current))
2381 flush_signals(current);
2383 mutex_lock(&session->eh_mutex);
2384 spin_lock_bh(&session->lock);
2385 if (session->state == ISCSI_STATE_LOGGED_IN) {
2386 ISCSI_DBG_EH(session,
2387 "session reset succeeded for %s,%s\n",
2388 session->targetname, conn->persistent_address);
2391 spin_unlock_bh(&session->lock);
2392 mutex_unlock(&session->eh_mutex);
2395 EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
2397 static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2399 memset(hdr, 0, sizeof(*hdr));
2400 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2401 hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2402 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2403 hdr->rtt = RESERVED_ITT;
2407 * iscsi_eh_target_reset - reset target
2410 * This will attempt to send a warm target reset.
2412 int iscsi_eh_target_reset(struct scsi_cmnd *sc)
2414 struct iscsi_cls_session *cls_session;
2415 struct iscsi_session *session;
2416 struct iscsi_conn *conn;
2417 struct iscsi_tm *hdr;
2420 cls_session = starget_to_session(scsi_target(sc->device));
2421 session = cls_session->dd_data;
2423 ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
2424 session->targetname);
2426 mutex_lock(&session->eh_mutex);
2427 spin_lock_bh(&session->lock);
2429 * Just check if we are not logged in. We cannot check for
2430 * the phase because the reset could come from a ioctl.
2432 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2434 conn = session->leadconn;
2436 /* only have one tmf outstanding at a time */
2437 if (conn->tmf_state != TMF_INITIAL)
2439 conn->tmf_state = TMF_QUEUED;
2442 iscsi_prep_tgt_reset_pdu(sc, hdr);
2444 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2445 session->tgt_reset_timeout)) {
2450 switch (conn->tmf_state) {
2454 spin_unlock_bh(&session->lock);
2455 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2458 conn->tmf_state = TMF_INITIAL;
2463 spin_unlock_bh(&session->lock);
2465 iscsi_suspend_tx(conn);
2467 spin_lock_bh(&session->lock);
2468 memset(hdr, 0, sizeof(*hdr));
2469 fail_scsi_tasks(conn, -1, DID_ERROR);
2470 conn->tmf_state = TMF_INITIAL;
2471 spin_unlock_bh(&session->lock);
2473 iscsi_start_tx(conn);
2477 spin_unlock_bh(&session->lock);
2479 ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
2480 rc == SUCCESS ? "SUCCESS" : "FAILED");
2481 mutex_unlock(&session->eh_mutex);
2484 EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
2487 * iscsi_eh_recover_target - reset target and possibly the session
2490 * This will attempt to send a warm target reset. If that fails,
2491 * we will escalate to ERL0 session recovery.
2493 int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2497 rc = iscsi_eh_target_reset(sc);
2499 rc = iscsi_eh_session_reset(sc);
2502 EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
2505 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2506 * should be accessed via kfifo_{get,put} on q->queue.
2507 * Optionally, the caller can obtain the array of object pointers
2508 * by passing in a non-NULL @items pointer
2511 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
2513 int i, num_arrays = 1;
2515 memset(q, 0, sizeof(*q));
2519 /* If the user passed an items pointer, he wants a copy of
2523 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2524 if (q->pool == NULL)
2527 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
2529 for (i = 0; i < max; i++) {
2530 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2531 if (q->pool[i] == NULL) {
2535 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
2539 *items = q->pool + max;
2540 memcpy(*items, q->pool, max * sizeof(void *));
2549 EXPORT_SYMBOL_GPL(iscsi_pool_init);
2551 void iscsi_pool_free(struct iscsi_pool *q)
2555 for (i = 0; i < q->max; i++)
2559 EXPORT_SYMBOL_GPL(iscsi_pool_free);
2562 * iscsi_host_add - add host to system
2564 * @pdev: parent device
2566 * This should be called by partial offload and software iscsi drivers
2567 * to add a host to the system.
2569 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2571 if (!shost->can_queue)
2572 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2574 if (!shost->cmd_per_lun)
2575 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2577 if (!shost->transportt->eh_timed_out)
2578 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
2579 return scsi_add_host(shost, pdev);
2581 EXPORT_SYMBOL_GPL(iscsi_host_add);
2584 * iscsi_host_alloc - allocate a host and driver data
2585 * @sht: scsi host template
2586 * @dd_data_size: driver host data size
2587 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2589 * This should be called by partial offload and software iscsi drivers.
2590 * To access the driver specific memory use the iscsi_host_priv() macro.
2592 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2593 int dd_data_size, bool xmit_can_sleep)
2595 struct Scsi_Host *shost;
2596 struct iscsi_host *ihost;
2598 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2601 ihost = shost_priv(shost);
2603 if (xmit_can_sleep) {
2604 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2605 "iscsi_q_%d", shost->host_no);
2606 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2611 spin_lock_init(&ihost->lock);
2612 ihost->state = ISCSI_HOST_SETUP;
2613 ihost->num_sessions = 0;
2614 init_waitqueue_head(&ihost->session_removal_wq);
2618 scsi_host_put(shost);
2621 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2623 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2625 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2629 * iscsi_host_remove - remove host and sessions
2632 * If there are any sessions left, this will initiate the removal and wait
2633 * for the completion.
2635 void iscsi_host_remove(struct Scsi_Host *shost)
2637 struct iscsi_host *ihost = shost_priv(shost);
2638 unsigned long flags;
2640 spin_lock_irqsave(&ihost->lock, flags);
2641 ihost->state = ISCSI_HOST_REMOVED;
2642 spin_unlock_irqrestore(&ihost->lock, flags);
2644 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2645 wait_event_interruptible(ihost->session_removal_wq,
2646 ihost->num_sessions == 0);
2647 if (signal_pending(current))
2648 flush_signals(current);
2650 scsi_remove_host(shost);
2652 destroy_workqueue(ihost->workq);
2654 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2656 void iscsi_host_free(struct Scsi_Host *shost)
2658 struct iscsi_host *ihost = shost_priv(shost);
2660 kfree(ihost->netdev);
2661 kfree(ihost->hwaddress);
2662 kfree(ihost->initiatorname);
2663 scsi_host_put(shost);
2665 EXPORT_SYMBOL_GPL(iscsi_host_free);
2667 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2669 struct iscsi_host *ihost = shost_priv(shost);
2670 unsigned long flags;
2672 shost = scsi_host_get(shost);
2674 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2675 "of session teardown event because host already "
2680 spin_lock_irqsave(&ihost->lock, flags);
2681 ihost->num_sessions--;
2682 if (ihost->num_sessions == 0)
2683 wake_up(&ihost->session_removal_wq);
2684 spin_unlock_irqrestore(&ihost->lock, flags);
2685 scsi_host_put(shost);
2689 * iscsi_session_setup - create iscsi cls session and host and session
2690 * @iscsit: iscsi transport template
2692 * @cmds_max: session can queue
2693 * @cmd_task_size: LLD task private data size
2694 * @initial_cmdsn: initial CmdSN
2696 * This can be used by software iscsi_transports that allocate
2697 * a session per scsi host.
2699 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2700 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2701 * for nop handling and login/logout requests.
2703 struct iscsi_cls_session *
2704 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2705 uint16_t cmds_max, int dd_size, int cmd_task_size,
2706 uint32_t initial_cmdsn, unsigned int id)
2708 struct iscsi_host *ihost = shost_priv(shost);
2709 struct iscsi_session *session;
2710 struct iscsi_cls_session *cls_session;
2711 int cmd_i, scsi_cmds, total_cmds = cmds_max;
2712 unsigned long flags;
2714 spin_lock_irqsave(&ihost->lock, flags);
2715 if (ihost->state == ISCSI_HOST_REMOVED) {
2716 spin_unlock_irqrestore(&ihost->lock, flags);
2719 ihost->num_sessions++;
2720 spin_unlock_irqrestore(&ihost->lock, flags);
2723 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2725 * The iscsi layer needs some tasks for nop handling and tmfs,
2726 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2727 * + 1 command for scsi IO.
2729 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2730 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2731 "must be a power of two that is at least %d.\n",
2732 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2733 goto dec_session_count;
2736 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2737 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2738 "must be a power of 2 less than or equal to %d.\n",
2739 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2740 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2743 if (!is_power_of_2(total_cmds)) {
2744 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2745 "must be a power of 2.\n", total_cmds);
2746 total_cmds = rounddown_pow_of_two(total_cmds);
2747 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2749 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2752 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2754 cls_session = iscsi_alloc_session(shost, iscsit,
2755 sizeof(struct iscsi_session) +
2758 goto dec_session_count;
2759 session = cls_session->dd_data;
2760 session->cls_session = cls_session;
2761 session->host = shost;
2762 session->state = ISCSI_STATE_FREE;
2763 session->fast_abort = 1;
2764 session->tgt_reset_timeout = 30;
2765 session->lu_reset_timeout = 15;
2766 session->abort_timeout = 10;
2767 session->scsi_cmds_max = scsi_cmds;
2768 session->cmds_max = total_cmds;
2769 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2770 session->exp_cmdsn = initial_cmdsn + 1;
2771 session->max_cmdsn = initial_cmdsn + 1;
2772 session->max_r2t = 1;
2773 session->tt = iscsit;
2774 session->dd_data = cls_session->dd_data + sizeof(*session);
2775 mutex_init(&session->eh_mutex);
2776 spin_lock_init(&session->lock);
2778 /* initialize SCSI PDU commands pool */
2779 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2780 (void***)&session->cmds,
2781 cmd_task_size + sizeof(struct iscsi_task)))
2782 goto cmdpool_alloc_fail;
2784 /* pre-format cmds pool with ITT */
2785 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2786 struct iscsi_task *task = session->cmds[cmd_i];
2789 task->dd_data = &task[1];
2791 task->state = ISCSI_TASK_FREE;
2792 INIT_LIST_HEAD(&task->running);
2795 if (!try_module_get(iscsit->owner))
2796 goto module_get_fail;
2798 if (iscsi_add_session(cls_session, id))
2799 goto cls_session_fail;
2804 module_put(iscsit->owner);
2806 iscsi_pool_free(&session->cmdpool);
2808 iscsi_free_session(cls_session);
2810 iscsi_host_dec_session_cnt(shost);
2813 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2816 * iscsi_session_teardown - destroy session, host, and cls_session
2817 * @cls_session: iscsi session
2819 * The driver must have called iscsi_remove_session before
2822 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2824 struct iscsi_session *session = cls_session->dd_data;
2825 struct module *owner = cls_session->transport->owner;
2826 struct Scsi_Host *shost = session->host;
2828 iscsi_pool_free(&session->cmdpool);
2830 kfree(session->password);
2831 kfree(session->password_in);
2832 kfree(session->username);
2833 kfree(session->username_in);
2834 kfree(session->targetname);
2835 kfree(session->initiatorname);
2836 kfree(session->ifacename);
2838 iscsi_destroy_session(cls_session);
2839 iscsi_host_dec_session_cnt(shost);
2842 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2845 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2846 * @cls_session: iscsi_cls_session
2847 * @dd_size: private driver data size
2850 struct iscsi_cls_conn *
2851 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2854 struct iscsi_session *session = cls_session->dd_data;
2855 struct iscsi_conn *conn;
2856 struct iscsi_cls_conn *cls_conn;
2859 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2863 conn = cls_conn->dd_data;
2864 memset(conn, 0, sizeof(*conn) + dd_size);
2866 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2867 conn->session = session;
2868 conn->cls_conn = cls_conn;
2869 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2870 conn->id = conn_idx;
2871 conn->exp_statsn = 0;
2872 conn->tmf_state = TMF_INITIAL;
2874 init_timer(&conn->transport_timer);
2875 conn->transport_timer.data = (unsigned long)conn;
2876 conn->transport_timer.function = iscsi_check_transport_timeouts;
2878 INIT_LIST_HEAD(&conn->mgmtqueue);
2879 INIT_LIST_HEAD(&conn->cmdqueue);
2880 INIT_LIST_HEAD(&conn->requeue);
2881 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2883 /* allocate login_task used for the login/text sequences */
2884 spin_lock_bh(&session->lock);
2885 if (!kfifo_out(&session->cmdpool.queue,
2886 (void*)&conn->login_task,
2888 spin_unlock_bh(&session->lock);
2889 goto login_task_alloc_fail;
2891 spin_unlock_bh(&session->lock);
2893 data = (char *) __get_free_pages(GFP_KERNEL,
2894 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2896 goto login_task_data_alloc_fail;
2897 conn->login_task->data = conn->data = data;
2899 init_timer(&conn->tmf_timer);
2900 init_waitqueue_head(&conn->ehwait);
2904 login_task_data_alloc_fail:
2905 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2907 login_task_alloc_fail:
2908 iscsi_destroy_conn(cls_conn);
2911 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2914 * iscsi_conn_teardown - teardown iscsi connection
2915 * cls_conn: iscsi class connection
2917 * TODO: we may need to make this into a two step process
2918 * like scsi-mls remove + put host
2920 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2922 struct iscsi_conn *conn = cls_conn->dd_data;
2923 struct iscsi_session *session = conn->session;
2924 unsigned long flags;
2926 del_timer_sync(&conn->transport_timer);
2928 spin_lock_bh(&session->lock);
2929 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2930 if (session->leadconn == conn) {
2932 * leading connection? then give up on recovery.
2934 session->state = ISCSI_STATE_TERMINATE;
2935 wake_up(&conn->ehwait);
2937 spin_unlock_bh(&session->lock);
2940 * Block until all in-progress commands for this connection
2944 spin_lock_irqsave(session->host->host_lock, flags);
2945 if (!session->host->host_busy) { /* OK for ERL == 0 */
2946 spin_unlock_irqrestore(session->host->host_lock, flags);
2949 spin_unlock_irqrestore(session->host->host_lock, flags);
2950 msleep_interruptible(500);
2951 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2952 "host_busy %d host_failed %d\n",
2953 session->host->host_busy,
2954 session->host->host_failed);
2956 * force eh_abort() to unblock
2958 wake_up(&conn->ehwait);
2961 /* flush queued up work because we free the connection below */
2962 iscsi_suspend_tx(conn);
2964 spin_lock_bh(&session->lock);
2965 free_pages((unsigned long) conn->data,
2966 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2967 kfree(conn->persistent_address);
2968 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2970 if (session->leadconn == conn)
2971 session->leadconn = NULL;
2972 spin_unlock_bh(&session->lock);
2974 iscsi_destroy_conn(cls_conn);
2976 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2978 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2980 struct iscsi_conn *conn = cls_conn->dd_data;
2981 struct iscsi_session *session = conn->session;
2984 iscsi_conn_printk(KERN_ERR, conn,
2985 "can't start unbound connection\n");
2989 if ((session->imm_data_en || !session->initial_r2t_en) &&
2990 session->first_burst > session->max_burst) {
2991 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2992 "first_burst %d max_burst %d\n",
2993 session->first_burst, session->max_burst);
2997 if (conn->ping_timeout && !conn->recv_timeout) {
2998 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2999 "zero. Using 5 seconds\n.");
3000 conn->recv_timeout = 5;
3003 if (conn->recv_timeout && !conn->ping_timeout) {
3004 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3005 "zero. Using 5 seconds.\n");
3006 conn->ping_timeout = 5;
3009 spin_lock_bh(&session->lock);
3010 conn->c_stage = ISCSI_CONN_STARTED;
3011 session->state = ISCSI_STATE_LOGGED_IN;
3012 session->queued_cmdsn = session->cmdsn;
3014 conn->last_recv = jiffies;
3015 conn->last_ping = jiffies;
3016 if (conn->recv_timeout && conn->ping_timeout)
3017 mod_timer(&conn->transport_timer,
3018 jiffies + (conn->recv_timeout * HZ));
3020 switch(conn->stop_stage) {
3021 case STOP_CONN_RECOVER:
3023 * unblock eh_abort() if it is blocked. re-try all
3024 * commands after successful recovery
3026 conn->stop_stage = 0;
3027 conn->tmf_state = TMF_INITIAL;
3029 if (session->age == 16)
3032 case STOP_CONN_TERM:
3033 conn->stop_stage = 0;
3038 spin_unlock_bh(&session->lock);
3040 iscsi_unblock_session(session->cls_session);
3041 wake_up(&conn->ehwait);
3044 EXPORT_SYMBOL_GPL(iscsi_conn_start);
3047 fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
3049 struct iscsi_task *task;
3052 for (i = 0; i < conn->session->cmds_max; i++) {
3053 task = conn->session->cmds[i];
3057 if (task->state == ISCSI_TASK_FREE)
3060 ISCSI_DBG_SESSION(conn->session,
3061 "failing mgmt itt 0x%x state %d\n",
3062 task->itt, task->state);
3063 state = ISCSI_TASK_ABRT_SESS_RECOV;
3064 if (task->state == ISCSI_TASK_PENDING)
3065 state = ISCSI_TASK_COMPLETED;
3066 iscsi_complete_task(task, state);
3071 static void iscsi_start_session_recovery(struct iscsi_session *session,
3072 struct iscsi_conn *conn, int flag)
3076 mutex_lock(&session->eh_mutex);
3077 spin_lock_bh(&session->lock);
3078 if (conn->stop_stage == STOP_CONN_TERM) {
3079 spin_unlock_bh(&session->lock);
3080 mutex_unlock(&session->eh_mutex);
3085 * When this is called for the in_login state, we only want to clean
3086 * up the login task and connection. We do not need to block and set
3087 * the recovery state again
3089 if (flag == STOP_CONN_TERM)
3090 session->state = ISCSI_STATE_TERMINATE;
3091 else if (conn->stop_stage != STOP_CONN_RECOVER)
3092 session->state = ISCSI_STATE_IN_RECOVERY;
3094 old_stop_stage = conn->stop_stage;
3095 conn->stop_stage = flag;
3096 spin_unlock_bh(&session->lock);
3098 del_timer_sync(&conn->transport_timer);
3099 iscsi_suspend_tx(conn);
3101 spin_lock_bh(&session->lock);
3102 conn->c_stage = ISCSI_CONN_STOPPED;
3103 spin_unlock_bh(&session->lock);
3106 * for connection level recovery we should not calculate
3107 * header digest. conn->hdr_size used for optimization
3108 * in hdr_extract() and will be re-negotiated at
3111 if (flag == STOP_CONN_RECOVER) {
3112 conn->hdrdgst_en = 0;
3113 conn->datadgst_en = 0;
3114 if (session->state == ISCSI_STATE_IN_RECOVERY &&
3115 old_stop_stage != STOP_CONN_RECOVER) {
3116 ISCSI_DBG_SESSION(session, "blocking session\n");
3117 iscsi_block_session(session->cls_session);
3124 spin_lock_bh(&session->lock);
3125 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
3126 fail_mgmt_tasks(session, conn);
3127 memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
3128 spin_unlock_bh(&session->lock);
3129 mutex_unlock(&session->eh_mutex);
3132 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3134 struct iscsi_conn *conn = cls_conn->dd_data;
3135 struct iscsi_session *session = conn->session;
3138 case STOP_CONN_RECOVER:
3139 case STOP_CONN_TERM:
3140 iscsi_start_session_recovery(session, conn, flag);
3143 iscsi_conn_printk(KERN_ERR, conn,
3144 "invalid stop flag %d\n", flag);
3147 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3149 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3150 struct iscsi_cls_conn *cls_conn, int is_leading)
3152 struct iscsi_session *session = cls_session->dd_data;
3153 struct iscsi_conn *conn = cls_conn->dd_data;
3155 spin_lock_bh(&session->lock);
3157 session->leadconn = conn;
3158 spin_unlock_bh(&session->lock);
3161 * Unblock xmitworker(), Login Phase will pass through.
3163 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
3164 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
3167 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3169 static int iscsi_switch_str_param(char **param, char *new_val_buf)
3174 if (!strcmp(*param, new_val_buf))
3178 new_val = kstrdup(new_val_buf, GFP_NOIO);
3187 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3188 enum iscsi_param param, char *buf, int buflen)
3190 struct iscsi_conn *conn = cls_conn->dd_data;
3191 struct iscsi_session *session = conn->session;
3195 case ISCSI_PARAM_FAST_ABORT:
3196 sscanf(buf, "%d", &session->fast_abort);
3198 case ISCSI_PARAM_ABORT_TMO:
3199 sscanf(buf, "%d", &session->abort_timeout);
3201 case ISCSI_PARAM_LU_RESET_TMO:
3202 sscanf(buf, "%d", &session->lu_reset_timeout);
3204 case ISCSI_PARAM_TGT_RESET_TMO:
3205 sscanf(buf, "%d", &session->tgt_reset_timeout);
3207 case ISCSI_PARAM_PING_TMO:
3208 sscanf(buf, "%d", &conn->ping_timeout);
3210 case ISCSI_PARAM_RECV_TMO:
3211 sscanf(buf, "%d", &conn->recv_timeout);
3213 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3214 sscanf(buf, "%d", &conn->max_recv_dlength);
3216 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3217 sscanf(buf, "%d", &conn->max_xmit_dlength);
3219 case ISCSI_PARAM_HDRDGST_EN:
3220 sscanf(buf, "%d", &conn->hdrdgst_en);
3222 case ISCSI_PARAM_DATADGST_EN:
3223 sscanf(buf, "%d", &conn->datadgst_en);
3225 case ISCSI_PARAM_INITIAL_R2T_EN:
3226 sscanf(buf, "%d", &session->initial_r2t_en);
3228 case ISCSI_PARAM_MAX_R2T:
3229 sscanf(buf, "%d", &session->max_r2t);
3231 case ISCSI_PARAM_IMM_DATA_EN:
3232 sscanf(buf, "%d", &session->imm_data_en);
3234 case ISCSI_PARAM_FIRST_BURST:
3235 sscanf(buf, "%d", &session->first_burst);
3237 case ISCSI_PARAM_MAX_BURST:
3238 sscanf(buf, "%d", &session->max_burst);
3240 case ISCSI_PARAM_PDU_INORDER_EN:
3241 sscanf(buf, "%d", &session->pdu_inorder_en);
3243 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3244 sscanf(buf, "%d", &session->dataseq_inorder_en);
3246 case ISCSI_PARAM_ERL:
3247 sscanf(buf, "%d", &session->erl);
3249 case ISCSI_PARAM_IFMARKER_EN:
3250 sscanf(buf, "%d", &value);
3253 case ISCSI_PARAM_OFMARKER_EN:
3254 sscanf(buf, "%d", &value);
3257 case ISCSI_PARAM_EXP_STATSN:
3258 sscanf(buf, "%u", &conn->exp_statsn);
3260 case ISCSI_PARAM_USERNAME:
3261 return iscsi_switch_str_param(&session->username, buf);
3262 case ISCSI_PARAM_USERNAME_IN:
3263 return iscsi_switch_str_param(&session->username_in, buf);
3264 case ISCSI_PARAM_PASSWORD:
3265 return iscsi_switch_str_param(&session->password, buf);
3266 case ISCSI_PARAM_PASSWORD_IN:
3267 return iscsi_switch_str_param(&session->password_in, buf);
3268 case ISCSI_PARAM_TARGET_NAME:
3269 return iscsi_switch_str_param(&session->targetname, buf);
3270 case ISCSI_PARAM_TPGT:
3271 sscanf(buf, "%d", &session->tpgt);
3273 case ISCSI_PARAM_PERSISTENT_PORT:
3274 sscanf(buf, "%d", &conn->persistent_port);
3276 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3277 return iscsi_switch_str_param(&conn->persistent_address, buf);
3278 case ISCSI_PARAM_IFACE_NAME:
3279 return iscsi_switch_str_param(&session->ifacename, buf);
3280 case ISCSI_PARAM_INITIATOR_NAME:
3281 return iscsi_switch_str_param(&session->initiatorname, buf);
3288 EXPORT_SYMBOL_GPL(iscsi_set_param);
3290 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3291 enum iscsi_param param, char *buf)
3293 struct iscsi_session *session = cls_session->dd_data;
3297 case ISCSI_PARAM_FAST_ABORT:
3298 len = sprintf(buf, "%d\n", session->fast_abort);
3300 case ISCSI_PARAM_ABORT_TMO:
3301 len = sprintf(buf, "%d\n", session->abort_timeout);
3303 case ISCSI_PARAM_LU_RESET_TMO:
3304 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
3306 case ISCSI_PARAM_TGT_RESET_TMO:
3307 len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
3309 case ISCSI_PARAM_INITIAL_R2T_EN:
3310 len = sprintf(buf, "%d\n", session->initial_r2t_en);
3312 case ISCSI_PARAM_MAX_R2T:
3313 len = sprintf(buf, "%hu\n", session->max_r2t);
3315 case ISCSI_PARAM_IMM_DATA_EN:
3316 len = sprintf(buf, "%d\n", session->imm_data_en);
3318 case ISCSI_PARAM_FIRST_BURST:
3319 len = sprintf(buf, "%u\n", session->first_burst);
3321 case ISCSI_PARAM_MAX_BURST:
3322 len = sprintf(buf, "%u\n", session->max_burst);
3324 case ISCSI_PARAM_PDU_INORDER_EN:
3325 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
3327 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3328 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
3330 case ISCSI_PARAM_ERL:
3331 len = sprintf(buf, "%d\n", session->erl);
3333 case ISCSI_PARAM_TARGET_NAME:
3334 len = sprintf(buf, "%s\n", session->targetname);
3336 case ISCSI_PARAM_TPGT:
3337 len = sprintf(buf, "%d\n", session->tpgt);
3339 case ISCSI_PARAM_USERNAME:
3340 len = sprintf(buf, "%s\n", session->username);
3342 case ISCSI_PARAM_USERNAME_IN:
3343 len = sprintf(buf, "%s\n", session->username_in);
3345 case ISCSI_PARAM_PASSWORD:
3346 len = sprintf(buf, "%s\n", session->password);
3348 case ISCSI_PARAM_PASSWORD_IN:
3349 len = sprintf(buf, "%s\n", session->password_in);
3351 case ISCSI_PARAM_IFACE_NAME:
3352 len = sprintf(buf, "%s\n", session->ifacename);
3354 case ISCSI_PARAM_INITIATOR_NAME:
3355 len = sprintf(buf, "%s\n", session->initiatorname);
3363 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3365 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3366 enum iscsi_param param, char *buf)
3368 struct iscsi_conn *conn = cls_conn->dd_data;
3372 case ISCSI_PARAM_PING_TMO:
3373 len = sprintf(buf, "%u\n", conn->ping_timeout);
3375 case ISCSI_PARAM_RECV_TMO:
3376 len = sprintf(buf, "%u\n", conn->recv_timeout);
3378 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3379 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3381 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3382 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3384 case ISCSI_PARAM_HDRDGST_EN:
3385 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3387 case ISCSI_PARAM_DATADGST_EN:
3388 len = sprintf(buf, "%d\n", conn->datadgst_en);
3390 case ISCSI_PARAM_IFMARKER_EN:
3391 len = sprintf(buf, "%d\n", conn->ifmarker_en);
3393 case ISCSI_PARAM_OFMARKER_EN:
3394 len = sprintf(buf, "%d\n", conn->ofmarker_en);
3396 case ISCSI_PARAM_EXP_STATSN:
3397 len = sprintf(buf, "%u\n", conn->exp_statsn);
3399 case ISCSI_PARAM_PERSISTENT_PORT:
3400 len = sprintf(buf, "%d\n", conn->persistent_port);
3402 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3403 len = sprintf(buf, "%s\n", conn->persistent_address);
3411 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3413 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3416 struct iscsi_host *ihost = shost_priv(shost);
3420 case ISCSI_HOST_PARAM_NETDEV_NAME:
3421 len = sprintf(buf, "%s\n", ihost->netdev);
3423 case ISCSI_HOST_PARAM_HWADDRESS:
3424 len = sprintf(buf, "%s\n", ihost->hwaddress);
3426 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3427 len = sprintf(buf, "%s\n", ihost->initiatorname);
3429 case ISCSI_HOST_PARAM_IPADDRESS:
3430 len = sprintf(buf, "%s\n", ihost->local_address);
3438 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3440 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3441 char *buf, int buflen)
3443 struct iscsi_host *ihost = shost_priv(shost);
3446 case ISCSI_HOST_PARAM_NETDEV_NAME:
3447 return iscsi_switch_str_param(&ihost->netdev, buf);
3448 case ISCSI_HOST_PARAM_HWADDRESS:
3449 return iscsi_switch_str_param(&ihost->hwaddress, buf);
3450 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3451 return iscsi_switch_str_param(&ihost->initiatorname, buf);
3458 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3460 MODULE_AUTHOR("Mike Christie");
3461 MODULE_DESCRIPTION("iSCSI library functions");
3462 MODULE_LICENSE("GPL");