4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/types.h>
25 #include <linux/kfifo.h>
26 #include <linux/delay.h>
27 #include <linux/log2.h>
28 #include <linux/slab.h>
29 #include <linux/sched/signal.h>
30 #include <linux/module.h>
31 #include <asm/unaligned.h>
33 #include <scsi/scsi_cmnd.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_eh.h>
36 #include <scsi/scsi_tcq.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi.h>
39 #include <scsi/iscsi_proto.h>
40 #include <scsi/scsi_transport.h>
41 #include <scsi/scsi_transport_iscsi.h>
42 #include <scsi/libiscsi.h>
43 #include <trace/events/iscsi.h>
45 static int iscsi_dbg_lib_conn;
46 module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
48 MODULE_PARM_DESC(debug_libiscsi_conn,
49 "Turn on debugging for connections in libiscsi module. "
50 "Set to 1 to turn on, and zero to turn off. Default is off.");
52 static int iscsi_dbg_lib_session;
53 module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
55 MODULE_PARM_DESC(debug_libiscsi_session,
56 "Turn on debugging for sessions in libiscsi module. "
57 "Set to 1 to turn on, and zero to turn off. Default is off.");
59 static int iscsi_dbg_lib_eh;
60 module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
62 MODULE_PARM_DESC(debug_libiscsi_eh,
63 "Turn on debugging for error handling in libiscsi module. "
64 "Set to 1 to turn on, and zero to turn off. Default is off.");
66 #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
68 if (iscsi_dbg_lib_conn) \
69 iscsi_conn_printk(KERN_INFO, _conn, \
72 iscsi_dbg_trace(trace_iscsi_dbg_conn, \
73 &(_conn)->cls_conn->dev, \
74 "%s " dbg_fmt, __func__, ##arg);\
77 #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
79 if (iscsi_dbg_lib_session) \
80 iscsi_session_printk(KERN_INFO, _session, \
83 iscsi_dbg_trace(trace_iscsi_dbg_session, \
84 &(_session)->cls_session->dev, \
85 "%s " dbg_fmt, __func__, ##arg); \
88 #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
90 if (iscsi_dbg_lib_eh) \
91 iscsi_session_printk(KERN_INFO, _session, \
94 iscsi_dbg_trace(trace_iscsi_dbg_eh, \
95 &(_session)->cls_session->dev, \
96 "%s " dbg_fmt, __func__, ##arg); \
99 inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
101 struct Scsi_Host *shost = conn->session->host;
102 struct iscsi_host *ihost = shost_priv(shost);
105 queue_work(ihost->workq, &conn->xmitwork);
107 EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
109 static void __iscsi_update_cmdsn(struct iscsi_session *session,
110 uint32_t exp_cmdsn, uint32_t max_cmdsn)
113 * standard specifies this check for when to update expected and
114 * max sequence numbers
116 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
119 if (exp_cmdsn != session->exp_cmdsn &&
120 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
121 session->exp_cmdsn = exp_cmdsn;
123 if (max_cmdsn != session->max_cmdsn &&
124 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn))
125 session->max_cmdsn = max_cmdsn;
128 void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
130 __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
131 be32_to_cpu(hdr->max_cmdsn));
133 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
136 * iscsi_prep_data_out_pdu - initialize Data-Out
137 * @task: scsi command task
139 * @hdr: iscsi data in pdu
142 * Initialize Data-Out within this R2T sequence and finds
143 * proper data_offset within this SCSI command.
145 * This function is called with connection lock taken.
147 void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
148 struct iscsi_data *hdr)
150 struct iscsi_conn *conn = task->conn;
151 unsigned int left = r2t->data_length - r2t->sent;
153 task->hdr_len = sizeof(struct iscsi_data);
155 memset(hdr, 0, sizeof(struct iscsi_data));
157 hdr->datasn = cpu_to_be32(r2t->datasn);
159 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
160 hdr->lun = task->lun;
161 hdr->itt = task->hdr_itt;
162 hdr->exp_statsn = r2t->exp_statsn;
163 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
164 if (left > conn->max_xmit_dlength) {
165 hton24(hdr->dlength, conn->max_xmit_dlength);
166 r2t->data_count = conn->max_xmit_dlength;
169 hton24(hdr->dlength, left);
170 r2t->data_count = left;
171 hdr->flags = ISCSI_FLAG_CMD_FINAL;
173 conn->dataout_pdus_cnt++;
175 EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
177 static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
179 unsigned exp_len = task->hdr_len + len;
181 if (exp_len > task->hdr_max) {
186 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
187 task->hdr_len = exp_len;
192 * make an extended cdb AHS
194 static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
196 struct scsi_cmnd *cmd = task->sc;
197 unsigned rlen, pad_len;
198 unsigned short ahslength;
199 struct iscsi_ecdb_ahdr *ecdb_ahdr;
202 ecdb_ahdr = iscsi_next_hdr(task);
203 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
205 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
206 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
208 pad_len = iscsi_padding(rlen);
210 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
211 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
216 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
218 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
219 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
220 ecdb_ahdr->reserved = 0;
221 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
223 ISCSI_DBG_SESSION(task->conn->session,
224 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
225 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
226 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
232 * iscsi_check_tmf_restrictions - check if a task is affected by TMF
234 * @opcode: opcode to check for
236 * During TMF a task has to be checked if it's affected.
237 * All unrelated I/O can be passed through, but I/O to the
238 * affected LUN should be restricted.
239 * If 'fast_abort' is set we won't be sending any I/O to the
241 * Otherwise the target is waiting for all TTTs to be completed,
242 * so we have to send all outstanding Data-Out PDUs to the target.
244 static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
246 struct iscsi_conn *conn = task->conn;
247 struct iscsi_tm *tmf = &conn->tmhdr;
250 if (conn->tmf_state == TMF_INITIAL)
253 if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
256 switch (ISCSI_TM_FUNC_VALUE(tmf)) {
257 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
259 * Allow PDUs for unrelated LUNs
261 hdr_lun = scsilun_to_int(&tmf->lun);
262 if (hdr_lun != task->sc->device->lun)
265 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
267 * Fail all SCSI cmd PDUs
269 if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
270 iscsi_conn_printk(KERN_INFO, conn,
279 * And also all data-out PDUs in response to R2T
280 * if fast_abort is set.
282 if (conn->session->fast_abort) {
283 iscsi_conn_printk(KERN_INFO, conn,
285 "0x%x/0x%x] fast abort.\n",
291 case ISCSI_TM_FUNC_ABORT_TASK:
293 * the caller has already checked if the task
294 * they want to abort was in the pending queue so if
295 * we are here the cmd pdu has gone out already, and
296 * we will only hit this for data-outs
298 if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
299 task->hdr_itt == tmf->rtt) {
300 ISCSI_DBG_SESSION(conn->session,
301 "Preventing task %x/%x from sending "
302 "data-out due to abort task in "
303 "progress\n", task->itt,
314 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
317 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
318 * fields like dlength or final based on how much data it sends
320 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
322 struct iscsi_conn *conn = task->conn;
323 struct iscsi_session *session = conn->session;
324 struct scsi_cmnd *sc = task->sc;
325 struct iscsi_scsi_req *hdr;
326 unsigned hdrlength, cmd_len, transfer_length;
330 rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
334 if (conn->session->tt->alloc_pdu) {
335 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
339 hdr = (struct iscsi_scsi_req *)task->hdr;
341 memset(hdr, 0, sizeof(*hdr));
343 if (session->tt->parse_pdu_itt)
344 hdr->itt = task->hdr_itt = itt;
346 hdr->itt = task->hdr_itt = build_itt(task->itt,
347 task->conn->session->age);
349 rc = iscsi_add_hdr(task, sizeof(*hdr));
352 hdr->opcode = ISCSI_OP_SCSI_CMD;
353 hdr->flags = ISCSI_ATTR_SIMPLE;
354 int_to_scsilun(sc->device->lun, &hdr->lun);
355 task->lun = hdr->lun;
356 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
357 cmd_len = sc->cmd_len;
358 if (cmd_len < ISCSI_CDB_SIZE)
359 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
360 else if (cmd_len > ISCSI_CDB_SIZE) {
361 rc = iscsi_prep_ecdb_ahs(task);
364 cmd_len = ISCSI_CDB_SIZE;
366 memcpy(hdr->cdb, sc->cmnd, cmd_len);
369 if (scsi_get_prot_op(sc) != SCSI_PROT_NORMAL)
370 task->protected = true;
372 transfer_length = scsi_transfer_length(sc);
373 hdr->data_length = cpu_to_be32(transfer_length);
374 if (sc->sc_data_direction == DMA_TO_DEVICE) {
375 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
377 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
381 * imm_count bytes to be sent right after
384 * unsol_count bytes(as Data-Out) to be sent
385 * without R2T ack right after
388 * r2t data_length bytes to be sent via R2T ack's
390 * pad_count bytes to be sent as zero-padding
392 memset(r2t, 0, sizeof(*r2t));
394 if (session->imm_data_en) {
395 if (transfer_length >= session->first_burst)
396 task->imm_count = min(session->first_burst,
397 conn->max_xmit_dlength);
399 task->imm_count = min(transfer_length,
400 conn->max_xmit_dlength);
401 hton24(hdr->dlength, task->imm_count);
403 zero_data(hdr->dlength);
405 if (!session->initial_r2t_en) {
406 r2t->data_length = min(session->first_burst,
409 r2t->data_offset = task->imm_count;
410 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
411 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
414 if (!task->unsol_r2t.data_length)
415 /* No unsolicit Data-Out's */
416 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
418 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
419 zero_data(hdr->dlength);
421 if (sc->sc_data_direction == DMA_FROM_DEVICE)
422 hdr->flags |= ISCSI_FLAG_CMD_READ;
425 /* calculate size of additional header segments (AHSs) */
426 hdrlength = task->hdr_len - sizeof(*hdr);
428 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
429 hdrlength /= ISCSI_PAD_LEN;
431 WARN_ON(hdrlength >= 256);
432 hdr->hlength = hdrlength & 0xFF;
433 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
435 if (session->tt->init_task && session->tt->init_task(task))
438 task->state = ISCSI_TASK_RUNNING;
441 conn->scsicmd_pdus_cnt++;
442 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
443 "itt 0x%x len %d cmdsn %d win %d]\n",
444 sc->sc_data_direction == DMA_TO_DEVICE ?
445 "write" : "read", conn->id, sc, sc->cmnd[0],
446 task->itt, transfer_length,
448 session->max_cmdsn - session->exp_cmdsn + 1);
453 * iscsi_free_task - free a task
454 * @task: iscsi cmd task
456 * Must be called with session back_lock.
457 * This function returns the scsi command to scsi-ml or cleans
458 * up mgmt tasks then returns the task to the pool.
460 static void iscsi_free_task(struct iscsi_task *task)
462 struct iscsi_conn *conn = task->conn;
463 struct iscsi_session *session = conn->session;
464 struct scsi_cmnd *sc = task->sc;
465 int oldstate = task->state;
467 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
468 task->itt, task->state, task->sc);
470 session->tt->cleanup_task(task);
471 task->state = ISCSI_TASK_FREE;
474 * login task is preallocated so do not free
476 if (conn->login_task == task)
479 kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
482 /* SCSI eh reuses commands to verify us */
485 * queue command may call this to free the task, so
486 * it will decide how to return sc to scsi-ml.
488 if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
493 void __iscsi_get_task(struct iscsi_task *task)
495 refcount_inc(&task->refcount);
497 EXPORT_SYMBOL_GPL(__iscsi_get_task);
499 void __iscsi_put_task(struct iscsi_task *task)
501 if (refcount_dec_and_test(&task->refcount))
502 iscsi_free_task(task);
504 EXPORT_SYMBOL_GPL(__iscsi_put_task);
506 void iscsi_put_task(struct iscsi_task *task)
508 struct iscsi_session *session = task->conn->session;
510 /* regular RX path uses back_lock */
511 spin_lock_bh(&session->back_lock);
512 __iscsi_put_task(task);
513 spin_unlock_bh(&session->back_lock);
515 EXPORT_SYMBOL_GPL(iscsi_put_task);
518 * iscsi_complete_task - finish a task
519 * @task: iscsi cmd task
520 * @state: state to complete task with
522 * Must be called with session back_lock.
524 static void iscsi_complete_task(struct iscsi_task *task, int state)
526 struct iscsi_conn *conn = task->conn;
528 ISCSI_DBG_SESSION(conn->session,
529 "complete task itt 0x%x state %d sc %p\n",
530 task->itt, task->state, task->sc);
531 if (task->state == ISCSI_TASK_COMPLETED ||
532 task->state == ISCSI_TASK_ABRT_TMF ||
533 task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
534 task->state == ISCSI_TASK_REQUEUE_SCSIQ)
536 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
539 spin_lock_bh(&conn->taskqueuelock);
540 if (!list_empty(&task->running)) {
541 pr_debug_once("%s while task on list", __func__);
542 list_del_init(&task->running);
544 spin_unlock_bh(&conn->taskqueuelock);
546 if (conn->task == task)
549 if (conn->ping_task == task)
550 conn->ping_task = NULL;
552 /* release get from queueing */
553 __iscsi_put_task(task);
557 * iscsi_complete_scsi_task - finish scsi task normally
558 * @task: iscsi task for scsi cmd
559 * @exp_cmdsn: expected cmd sn in cpu format
560 * @max_cmdsn: max cmd sn in cpu format
562 * This is used when drivers do not need or cannot perform
563 * lower level pdu processing.
565 * Called with session back_lock
567 void iscsi_complete_scsi_task(struct iscsi_task *task,
568 uint32_t exp_cmdsn, uint32_t max_cmdsn)
570 struct iscsi_conn *conn = task->conn;
572 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
574 conn->last_recv = jiffies;
575 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
576 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
578 EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
582 * session back_lock must be held and if not called for a task that is
583 * still pending or from the xmit thread, then xmit thread must
586 static void fail_scsi_task(struct iscsi_task *task, int err)
588 struct iscsi_conn *conn = task->conn;
589 struct scsi_cmnd *sc;
593 * if a command completes and we get a successful tmf response
594 * we will hit this because the scsi eh abort code does not take
601 if (task->state == ISCSI_TASK_PENDING) {
603 * cmd never made it to the xmit thread, so we should not count
604 * the cmd in the sequencing
606 conn->session->queued_cmdsn--;
607 /* it was never sent so just complete like normal */
608 state = ISCSI_TASK_COMPLETED;
609 } else if (err == DID_TRANSPORT_DISRUPTED)
610 state = ISCSI_TASK_ABRT_SESS_RECOV;
612 state = ISCSI_TASK_ABRT_TMF;
614 sc->result = err << 16;
615 scsi_set_resid(sc, scsi_bufflen(sc));
617 /* regular RX path uses back_lock */
618 spin_lock_bh(&conn->session->back_lock);
619 iscsi_complete_task(task, state);
620 spin_unlock_bh(&conn->session->back_lock);
623 static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
624 struct iscsi_task *task)
626 struct iscsi_session *session = conn->session;
627 struct iscsi_hdr *hdr = task->hdr;
628 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
629 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
631 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
634 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
635 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
637 * pre-format CmdSN for outgoing PDU.
639 nop->cmdsn = cpu_to_be32(session->cmdsn);
640 if (hdr->itt != RESERVED_ITT) {
642 * TODO: We always use immediate for normal session pdus.
643 * If we start to send tmfs or nops as non-immediate then
644 * we should start checking the cmdsn numbers for mgmt tasks.
646 * During discovery sessions iscsid sends TEXT as non immediate,
647 * but we always only send one PDU at a time.
649 if (conn->c_stage == ISCSI_CONN_STARTED &&
650 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
651 session->queued_cmdsn++;
656 if (session->tt->init_task && session->tt->init_task(task))
659 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
660 session->state = ISCSI_STATE_LOGGING_OUT;
662 task->state = ISCSI_TASK_RUNNING;
663 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
664 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
665 hdr->itt, task->data_count);
669 static struct iscsi_task *
670 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
671 char *data, uint32_t data_size)
673 struct iscsi_session *session = conn->session;
674 struct iscsi_host *ihost = shost_priv(session->host);
675 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
676 struct iscsi_task *task;
679 if (session->state == ISCSI_STATE_TERMINATE)
682 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
684 * Login and Text are sent serially, in
685 * request-followed-by-response sequence.
686 * Same task can be used. Same ITT must be used.
687 * Note that login_task is preallocated at conn_create().
689 if (conn->login_task->state != ISCSI_TASK_FREE) {
690 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
691 "progress. Cannot start new task.\n");
695 if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) {
696 iscsi_conn_printk(KERN_ERR, conn, "Invalid buffer len of %u for login task. Max len is %u\n", data_size, ISCSI_DEF_MAX_RECV_SEG_LEN);
700 task = conn->login_task;
702 if (session->state != ISCSI_STATE_LOGGED_IN)
705 if (data_size != 0) {
706 iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode);
710 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
711 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
713 if (!kfifo_out(&session->cmdpool.queue,
714 (void*)&task, sizeof(void*)))
718 * released in complete pdu for task we expect a response for, and
719 * released by the lld when it has transmitted the task for
720 * pdus we do not expect a response for.
722 refcount_set(&task->refcount, 1);
725 INIT_LIST_HEAD(&task->running);
726 task->state = ISCSI_TASK_PENDING;
729 memcpy(task->data, data, data_size);
730 task->data_count = data_size;
732 task->data_count = 0;
734 if (conn->session->tt->alloc_pdu) {
735 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
736 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
737 "pdu for mgmt task.\n");
742 itt = task->hdr->itt;
743 task->hdr_len = sizeof(struct iscsi_hdr);
744 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
746 if (hdr->itt != RESERVED_ITT) {
747 if (session->tt->parse_pdu_itt)
748 task->hdr->itt = itt;
750 task->hdr->itt = build_itt(task->itt,
751 task->conn->session->age);
755 if (iscsi_prep_mgmt_task(conn, task))
758 if (session->tt->xmit_task(task))
761 spin_lock_bh(&conn->taskqueuelock);
762 list_add_tail(&task->running, &conn->mgmtqueue);
763 spin_unlock_bh(&conn->taskqueuelock);
764 iscsi_conn_queue_work(conn);
770 /* regular RX path uses back_lock */
771 spin_lock(&session->back_lock);
772 __iscsi_put_task(task);
773 spin_unlock(&session->back_lock);
777 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
778 char *data, uint32_t data_size)
780 struct iscsi_conn *conn = cls_conn->dd_data;
781 struct iscsi_session *session = conn->session;
784 spin_lock_bh(&session->frwd_lock);
785 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
787 spin_unlock_bh(&session->frwd_lock);
790 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
793 * iscsi_cmd_rsp - SCSI Command Response processing
794 * @conn: iscsi connection
796 * @task: scsi command task
797 * @data: cmd data buffer
798 * @datalen: len of buffer
800 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
801 * then completes the command and task.
803 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
804 struct iscsi_task *task, char *data,
807 struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
808 struct iscsi_session *session = conn->session;
809 struct scsi_cmnd *sc = task->sc;
811 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
812 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
814 sc->result = (DID_OK << 16) | rhdr->cmd_status;
816 if (task->protected) {
821 * Transports that didn't implement check_protection
822 * callback but still published T10-PI support to scsi-mid
823 * deserve this BUG_ON.
825 BUG_ON(!session->tt->check_protection);
827 ascq = session->tt->check_protection(task, §or);
829 sc->result = DRIVER_SENSE << 24 |
830 SAM_STAT_CHECK_CONDITION;
831 scsi_build_sense_buffer(1, sc->sense_buffer,
832 ILLEGAL_REQUEST, 0x10, ascq);
833 scsi_set_sense_information(sc->sense_buffer,
834 SCSI_SENSE_BUFFERSIZE,
840 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
841 sc->result = DID_ERROR << 16;
845 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
850 iscsi_conn_printk(KERN_ERR, conn,
851 "Got CHECK_CONDITION but invalid data "
852 "buffer size of %d\n", datalen);
853 sc->result = DID_BAD_TARGET << 16;
857 senselen = get_unaligned_be16(data);
858 if (datalen < senselen)
859 goto invalid_datalen;
861 memcpy(sc->sense_buffer, data + 2,
862 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
863 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
864 min_t(uint16_t, senselen,
865 SCSI_SENSE_BUFFERSIZE));
868 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
869 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
870 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
873 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
874 ISCSI_FLAG_CMD_OVERFLOW)) {
875 int res_count = be32_to_cpu(rhdr->residual_count);
878 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
879 res_count <= scsi_bufflen(sc)))
880 /* write side for bidi or uni-io set_resid */
881 scsi_set_resid(sc, res_count);
883 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
886 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
887 sc, sc->result, task->itt);
888 conn->scsirsp_pdus_cnt++;
889 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
893 * iscsi_data_in_rsp - SCSI Data-In Response processing
894 * @conn: iscsi connection
896 * @task: scsi command task
899 iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
900 struct iscsi_task *task)
902 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
903 struct scsi_cmnd *sc = task->sc;
905 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
908 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
909 sc->result = (DID_OK << 16) | rhdr->cmd_status;
910 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
911 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
912 ISCSI_FLAG_DATA_OVERFLOW)) {
913 int res_count = be32_to_cpu(rhdr->residual_count);
916 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
917 res_count <= sc->sdb.length))
918 sc->sdb.resid = res_count;
920 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
923 ISCSI_DBG_SESSION(conn->session, "data in with status done "
924 "[sc %p res %d itt 0x%x]\n",
925 sc, sc->result, task->itt);
926 conn->scsirsp_pdus_cnt++;
927 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
930 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
932 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
934 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
935 conn->tmfrsp_pdus_cnt++;
937 if (conn->tmf_state != TMF_QUEUED)
940 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
941 conn->tmf_state = TMF_SUCCESS;
942 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
943 conn->tmf_state = TMF_NOT_FOUND;
945 conn->tmf_state = TMF_FAILED;
946 wake_up(&conn->ehwait);
949 static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
951 struct iscsi_nopout hdr;
952 struct iscsi_task *task;
954 if (!rhdr && conn->ping_task)
957 memset(&hdr, 0, sizeof(struct iscsi_nopout));
958 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
959 hdr.flags = ISCSI_FLAG_CMD_FINAL;
964 hdr.itt = RESERVED_ITT;
966 hdr.ttt = RESERVED_ITT;
968 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
970 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
973 /* only track our nops */
974 conn->ping_task = task;
975 conn->last_ping = jiffies;
981 static int iscsi_nop_out_rsp(struct iscsi_task *task,
982 struct iscsi_nopin *nop, char *data, int datalen)
984 struct iscsi_conn *conn = task->conn;
987 if (conn->ping_task != task) {
989 * If this is not in response to one of our
990 * nops then it must be from userspace.
992 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
994 rc = ISCSI_ERR_CONN_FAILED;
996 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
997 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1001 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1002 char *data, int datalen)
1004 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
1005 struct iscsi_hdr rejected_pdu;
1008 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
1010 if (ntoh24(reject->dlength) > datalen ||
1011 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
1012 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
1013 "pdu. Invalid data length (pdu dlength "
1014 "%u, datalen %d\n", ntoh24(reject->dlength),
1016 return ISCSI_ERR_PROTO;
1018 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
1019 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
1021 switch (reject->reason) {
1022 case ISCSI_REASON_DATA_DIGEST_ERROR:
1023 iscsi_conn_printk(KERN_ERR, conn,
1024 "pdu (op 0x%x itt 0x%x) rejected "
1025 "due to DataDigest error.\n",
1026 opcode, rejected_pdu.itt);
1028 case ISCSI_REASON_IMM_CMD_REJECT:
1029 iscsi_conn_printk(KERN_ERR, conn,
1030 "pdu (op 0x%x itt 0x%x) rejected. Too many "
1031 "immediate commands.\n",
1032 opcode, rejected_pdu.itt);
1034 * We only send one TMF at a time so if the target could not
1035 * handle it, then it should get fixed (RFC mandates that
1036 * a target can handle one immediate TMF per conn).
1038 * For nops-outs, we could have sent more than one if
1039 * the target is sending us lots of nop-ins
1041 if (opcode != ISCSI_OP_NOOP_OUT)
1044 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1046 * nop-out in response to target's nop-out rejected.
1049 /* In RX path we are under back lock */
1050 spin_unlock(&conn->session->back_lock);
1051 spin_lock(&conn->session->frwd_lock);
1052 iscsi_send_nopout(conn,
1053 (struct iscsi_nopin*)&rejected_pdu);
1054 spin_unlock(&conn->session->frwd_lock);
1055 spin_lock(&conn->session->back_lock);
1057 struct iscsi_task *task;
1059 * Our nop as ping got dropped. We know the target
1060 * and transport are ok so just clean up
1062 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
1064 iscsi_conn_printk(KERN_ERR, conn,
1065 "Invalid pdu reject. Could "
1066 "not lookup rejected task.\n");
1067 rc = ISCSI_ERR_BAD_ITT;
1069 rc = iscsi_nop_out_rsp(task,
1070 (struct iscsi_nopin*)&rejected_pdu,
1075 iscsi_conn_printk(KERN_ERR, conn,
1076 "pdu (op 0x%x itt 0x%x) rejected. Reason "
1077 "code 0x%x\n", rejected_pdu.opcode,
1078 rejected_pdu.itt, reject->reason);
1085 * iscsi_itt_to_task - look up task by itt
1086 * @conn: iscsi connection
1089 * This should be used for mgmt tasks like login and nops, or if
1090 * the LDD's itt space does not include the session age.
1092 * The session back_lock must be held.
1094 struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
1096 struct iscsi_session *session = conn->session;
1099 if (itt == RESERVED_ITT)
1102 if (session->tt->parse_pdu_itt)
1103 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1106 if (i >= session->cmds_max)
1109 return session->cmds[i];
1111 EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
1114 * __iscsi_complete_pdu - complete pdu
1116 * @hdr: iscsi header
1117 * @data: data buffer
1118 * @datalen: len of data buffer
1120 * Completes pdu processing by freeing any resources allocated at
1121 * queuecommand or send generic. session back_lock must be held and verify
1122 * itt must have been called.
1124 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1125 char *data, int datalen)
1127 struct iscsi_session *session = conn->session;
1128 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
1129 struct iscsi_task *task;
1132 conn->last_recv = jiffies;
1133 rc = iscsi_verify_itt(conn, hdr->itt);
1137 if (hdr->itt != RESERVED_ITT)
1138 itt = get_itt(hdr->itt);
1142 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1143 opcode, conn->id, itt, datalen);
1146 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1149 case ISCSI_OP_NOOP_IN:
1151 rc = ISCSI_ERR_PROTO;
1155 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
1158 /* In RX path we are under back lock */
1159 spin_unlock(&session->back_lock);
1160 spin_lock(&session->frwd_lock);
1161 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
1162 spin_unlock(&session->frwd_lock);
1163 spin_lock(&session->back_lock);
1165 case ISCSI_OP_REJECT:
1166 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1168 case ISCSI_OP_ASYNC_EVENT:
1169 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1170 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1171 rc = ISCSI_ERR_CONN_FAILED;
1174 rc = ISCSI_ERR_BAD_OPCODE;
1181 case ISCSI_OP_SCSI_CMD_RSP:
1182 case ISCSI_OP_SCSI_DATA_IN:
1183 task = iscsi_itt_to_ctask(conn, hdr->itt);
1185 return ISCSI_ERR_BAD_ITT;
1186 task->last_xfer = jiffies;
1190 * LLD handles R2Ts if they need to.
1193 case ISCSI_OP_LOGOUT_RSP:
1194 case ISCSI_OP_LOGIN_RSP:
1195 case ISCSI_OP_TEXT_RSP:
1196 case ISCSI_OP_SCSI_TMFUNC_RSP:
1197 case ISCSI_OP_NOOP_IN:
1198 task = iscsi_itt_to_task(conn, hdr->itt);
1200 return ISCSI_ERR_BAD_ITT;
1203 return ISCSI_ERR_BAD_OPCODE;
1207 case ISCSI_OP_SCSI_CMD_RSP:
1208 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
1210 case ISCSI_OP_SCSI_DATA_IN:
1211 iscsi_data_in_rsp(conn, hdr, task);
1213 case ISCSI_OP_LOGOUT_RSP:
1214 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1216 rc = ISCSI_ERR_PROTO;
1219 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1221 case ISCSI_OP_LOGIN_RSP:
1222 case ISCSI_OP_TEXT_RSP:
1223 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1225 * login related PDU's exp_statsn is handled in
1229 case ISCSI_OP_SCSI_TMFUNC_RSP:
1230 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1232 rc = ISCSI_ERR_PROTO;
1236 iscsi_tmf_rsp(conn, hdr);
1237 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1239 case ISCSI_OP_NOOP_IN:
1240 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1241 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1242 rc = ISCSI_ERR_PROTO;
1245 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1247 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1251 rc = ISCSI_ERR_BAD_OPCODE;
1258 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1259 rc = ISCSI_ERR_CONN_FAILED;
1260 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1263 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
1265 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1266 char *data, int datalen)
1270 spin_lock(&conn->session->back_lock);
1271 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1272 spin_unlock(&conn->session->back_lock);
1275 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1277 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
1279 struct iscsi_session *session = conn->session;
1282 if (itt == RESERVED_ITT)
1285 if (session->tt->parse_pdu_itt)
1286 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1289 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1292 if (age != session->age) {
1293 iscsi_conn_printk(KERN_ERR, conn,
1294 "received itt %x expected session age (%x)\n",
1295 (__force u32)itt, session->age);
1296 return ISCSI_ERR_BAD_ITT;
1299 if (i >= session->cmds_max) {
1300 iscsi_conn_printk(KERN_ERR, conn,
1301 "received invalid itt index %u (max cmds "
1302 "%u.\n", i, session->cmds_max);
1303 return ISCSI_ERR_BAD_ITT;
1307 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1310 * iscsi_itt_to_ctask - look up ctask by itt
1311 * @conn: iscsi connection
1314 * This should be used for cmd tasks.
1316 * The session back_lock must be held.
1318 struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
1320 struct iscsi_task *task;
1322 if (iscsi_verify_itt(conn, itt))
1325 task = iscsi_itt_to_task(conn, itt);
1326 if (!task || !task->sc)
1329 if (task->sc->SCp.phase != conn->session->age) {
1330 iscsi_session_printk(KERN_ERR, conn->session,
1331 "task's session age %d, expected %d\n",
1332 task->sc->SCp.phase, conn->session->age);
1338 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1340 void iscsi_session_failure(struct iscsi_session *session,
1343 struct iscsi_conn *conn;
1346 spin_lock_bh(&session->frwd_lock);
1347 conn = session->leadconn;
1348 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1349 spin_unlock_bh(&session->frwd_lock);
1353 dev = get_device(&conn->cls_conn->dev);
1354 spin_unlock_bh(&session->frwd_lock);
1358 * if the host is being removed bypass the connection
1359 * recovery initialization because we are going to kill
1362 if (err == ISCSI_ERR_INVALID_HOST)
1363 iscsi_conn_error_event(conn->cls_conn, err);
1365 iscsi_conn_failure(conn, err);
1368 EXPORT_SYMBOL_GPL(iscsi_session_failure);
1370 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1372 struct iscsi_session *session = conn->session;
1374 spin_lock_bh(&session->frwd_lock);
1375 if (session->state == ISCSI_STATE_FAILED) {
1376 spin_unlock_bh(&session->frwd_lock);
1380 if (conn->stop_stage == 0)
1381 session->state = ISCSI_STATE_FAILED;
1382 spin_unlock_bh(&session->frwd_lock);
1384 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1385 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1386 iscsi_conn_error_event(conn->cls_conn, err);
1388 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1390 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1392 struct iscsi_session *session = conn->session;
1395 * Check for iSCSI window and take care of CmdSN wrap-around
1397 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1398 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1399 "%u MaxCmdSN %u CmdSN %u/%u\n",
1400 session->exp_cmdsn, session->max_cmdsn,
1401 session->cmdsn, session->queued_cmdsn);
1407 static int iscsi_xmit_task(struct iscsi_conn *conn)
1409 struct iscsi_task *task = conn->task;
1412 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1415 __iscsi_get_task(task);
1416 spin_unlock_bh(&conn->session->frwd_lock);
1417 rc = conn->session->tt->xmit_task(task);
1418 spin_lock_bh(&conn->session->frwd_lock);
1420 /* done with this task */
1421 task->last_xfer = jiffies;
1424 /* regular RX path uses back_lock */
1425 spin_lock(&conn->session->back_lock);
1426 __iscsi_put_task(task);
1427 spin_unlock(&conn->session->back_lock);
1432 * iscsi_requeue_task - requeue task to run from session workqueue
1433 * @task: task to requeue
1435 * LLDs that need to run a task from the session workqueue should call
1436 * this. The session frwd_lock must be held. This should only be called
1437 * by software drivers.
1439 void iscsi_requeue_task(struct iscsi_task *task)
1441 struct iscsi_conn *conn = task->conn;
1444 * this may be on the requeue list already if the xmit_task callout
1445 * is handling the r2ts while we are adding new ones
1447 spin_lock_bh(&conn->taskqueuelock);
1448 if (list_empty(&task->running))
1449 list_add_tail(&task->running, &conn->requeue);
1450 spin_unlock_bh(&conn->taskqueuelock);
1451 iscsi_conn_queue_work(conn);
1453 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1456 * iscsi_data_xmit - xmit any command into the scheduled connection
1457 * @conn: iscsi connection
1460 * The function can return -EAGAIN in which case the caller must
1461 * re-schedule it again later or recover. '0' return code means
1464 static int iscsi_data_xmit(struct iscsi_conn *conn)
1466 struct iscsi_task *task;
1469 spin_lock_bh(&conn->session->frwd_lock);
1470 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1471 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1472 spin_unlock_bh(&conn->session->frwd_lock);
1477 rc = iscsi_xmit_task(conn);
1483 * process mgmt pdus like nops before commands since we should
1484 * only have one nop-out as a ping from us and targets should not
1485 * overflow us with nop-ins
1487 spin_lock_bh(&conn->taskqueuelock);
1489 while (!list_empty(&conn->mgmtqueue)) {
1490 conn->task = list_entry(conn->mgmtqueue.next,
1491 struct iscsi_task, running);
1492 list_del_init(&conn->task->running);
1493 spin_unlock_bh(&conn->taskqueuelock);
1494 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1495 /* regular RX path uses back_lock */
1496 spin_lock_bh(&conn->session->back_lock);
1497 __iscsi_put_task(conn->task);
1498 spin_unlock_bh(&conn->session->back_lock);
1500 spin_lock_bh(&conn->taskqueuelock);
1503 rc = iscsi_xmit_task(conn);
1506 spin_lock_bh(&conn->taskqueuelock);
1509 /* process pending command queue */
1510 while (!list_empty(&conn->cmdqueue)) {
1511 conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1513 list_del_init(&conn->task->running);
1514 spin_unlock_bh(&conn->taskqueuelock);
1515 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1516 fail_scsi_task(conn->task, DID_IMM_RETRY);
1517 spin_lock_bh(&conn->taskqueuelock);
1520 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1522 if (rc == -ENOMEM || rc == -EACCES) {
1523 spin_lock_bh(&conn->taskqueuelock);
1524 list_add_tail(&conn->task->running,
1527 spin_unlock_bh(&conn->taskqueuelock);
1530 fail_scsi_task(conn->task, DID_ABORT);
1531 spin_lock_bh(&conn->taskqueuelock);
1534 rc = iscsi_xmit_task(conn);
1538 * we could continuously get new task requests so
1539 * we need to check the mgmt queue for nops that need to
1540 * be sent to aviod starvation
1542 spin_lock_bh(&conn->taskqueuelock);
1543 if (!list_empty(&conn->mgmtqueue))
1547 while (!list_empty(&conn->requeue)) {
1549 * we always do fastlogout - conn stop code will clean up.
1551 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1554 task = list_entry(conn->requeue.next, struct iscsi_task,
1556 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1560 list_del_init(&conn->task->running);
1561 conn->task->state = ISCSI_TASK_RUNNING;
1562 spin_unlock_bh(&conn->taskqueuelock);
1563 rc = iscsi_xmit_task(conn);
1566 spin_lock_bh(&conn->taskqueuelock);
1567 if (!list_empty(&conn->mgmtqueue))
1570 spin_unlock_bh(&conn->taskqueuelock);
1571 spin_unlock_bh(&conn->session->frwd_lock);
1575 spin_unlock_bh(&conn->session->frwd_lock);
1579 static void iscsi_xmitworker(struct work_struct *work)
1581 struct iscsi_conn *conn =
1582 container_of(work, struct iscsi_conn, xmitwork);
1585 * serialize Xmit worker on a per-connection basis.
1588 rc = iscsi_data_xmit(conn);
1589 } while (rc >= 0 || rc == -EAGAIN);
1592 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1593 struct scsi_cmnd *sc)
1595 struct iscsi_task *task;
1597 if (!kfifo_out(&conn->session->cmdpool.queue,
1598 (void *) &task, sizeof(void *)))
1601 sc->SCp.phase = conn->session->age;
1602 sc->SCp.ptr = (char *) task;
1604 refcount_set(&task->refcount, 1);
1605 task->state = ISCSI_TASK_PENDING;
1608 task->have_checked_conn = false;
1609 task->last_timeout = jiffies;
1610 task->last_xfer = jiffies;
1611 task->protected = false;
1612 INIT_LIST_HEAD(&task->running);
1617 FAILURE_BAD_HOST = 1,
1618 FAILURE_SESSION_FAILED,
1619 FAILURE_SESSION_FREED,
1620 FAILURE_WINDOW_CLOSED,
1622 FAILURE_SESSION_TERMINATE,
1623 FAILURE_SESSION_IN_RECOVERY,
1624 FAILURE_SESSION_RECOVERY_TIMEOUT,
1625 FAILURE_SESSION_LOGGING_OUT,
1626 FAILURE_SESSION_NOT_READY,
1629 int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
1631 struct iscsi_cls_session *cls_session;
1632 struct iscsi_host *ihost;
1634 struct iscsi_session *session;
1635 struct iscsi_conn *conn;
1636 struct iscsi_task *task = NULL;
1641 ihost = shost_priv(host);
1643 cls_session = starget_to_session(scsi_target(sc->device));
1644 session = cls_session->dd_data;
1645 spin_lock_bh(&session->frwd_lock);
1647 reason = iscsi_session_chkready(cls_session);
1649 sc->result = reason;
1653 if (session->state != ISCSI_STATE_LOGGED_IN) {
1655 * to handle the race between when we set the recovery state
1656 * and block the session we requeue here (commands could
1657 * be entering our queuecommand while a block is starting
1658 * up because the block code is not locked)
1660 switch (session->state) {
1661 case ISCSI_STATE_FAILED:
1663 * cmds should fail during shutdown, if the session
1664 * state is bad, allowing completion to happen
1666 if (unlikely(system_state != SYSTEM_RUNNING)) {
1667 reason = FAILURE_SESSION_FAILED;
1668 sc->result = DID_NO_CONNECT << 16;
1672 case ISCSI_STATE_IN_RECOVERY:
1673 reason = FAILURE_SESSION_IN_RECOVERY;
1674 sc->result = DID_IMM_RETRY << 16;
1676 case ISCSI_STATE_LOGGING_OUT:
1677 reason = FAILURE_SESSION_LOGGING_OUT;
1678 sc->result = DID_IMM_RETRY << 16;
1680 case ISCSI_STATE_RECOVERY_FAILED:
1681 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1682 sc->result = DID_TRANSPORT_FAILFAST << 16;
1684 case ISCSI_STATE_TERMINATE:
1685 reason = FAILURE_SESSION_TERMINATE;
1686 sc->result = DID_NO_CONNECT << 16;
1689 reason = FAILURE_SESSION_FREED;
1690 sc->result = DID_NO_CONNECT << 16;
1695 conn = session->leadconn;
1697 reason = FAILURE_SESSION_FREED;
1698 sc->result = DID_NO_CONNECT << 16;
1702 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1703 reason = FAILURE_SESSION_IN_RECOVERY;
1704 sc->result = DID_REQUEUE << 16;
1708 if (iscsi_check_cmdsn_window_closed(conn)) {
1709 reason = FAILURE_WINDOW_CLOSED;
1713 task = iscsi_alloc_task(conn, sc);
1715 reason = FAILURE_OOM;
1719 if (!ihost->workq) {
1720 reason = iscsi_prep_scsi_cmd_pdu(task);
1722 if (reason == -ENOMEM || reason == -EACCES) {
1723 reason = FAILURE_OOM;
1726 sc->result = DID_ABORT << 16;
1730 if (session->tt->xmit_task(task)) {
1732 reason = FAILURE_SESSION_NOT_READY;
1736 spin_lock_bh(&conn->taskqueuelock);
1737 list_add_tail(&task->running, &conn->cmdqueue);
1738 spin_unlock_bh(&conn->taskqueuelock);
1739 iscsi_conn_queue_work(conn);
1742 session->queued_cmdsn++;
1743 spin_unlock_bh(&session->frwd_lock);
1747 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1749 spin_unlock_bh(&session->frwd_lock);
1750 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1751 sc->cmnd[0], reason);
1752 return SCSI_MLQUEUE_TARGET_BUSY;
1755 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1757 spin_unlock_bh(&session->frwd_lock);
1758 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1759 sc->cmnd[0], reason);
1760 scsi_set_resid(sc, scsi_bufflen(sc));
1764 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1766 int iscsi_target_alloc(struct scsi_target *starget)
1768 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1769 struct iscsi_session *session = cls_session->dd_data;
1771 starget->can_queue = session->scsi_cmds_max;
1774 EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1776 static void iscsi_tmf_timedout(struct timer_list *t)
1778 struct iscsi_conn *conn = from_timer(conn, t, tmf_timer);
1779 struct iscsi_session *session = conn->session;
1781 spin_lock(&session->frwd_lock);
1782 if (conn->tmf_state == TMF_QUEUED) {
1783 conn->tmf_state = TMF_TIMEDOUT;
1784 ISCSI_DBG_EH(session, "tmf timedout\n");
1785 /* unblock eh_abort() */
1786 wake_up(&conn->ehwait);
1788 spin_unlock(&session->frwd_lock);
1791 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1792 struct iscsi_tm *hdr, int age,
1794 __must_hold(&session->frwd_lock)
1796 struct iscsi_session *session = conn->session;
1797 struct iscsi_task *task;
1799 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1802 spin_unlock_bh(&session->frwd_lock);
1803 iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
1804 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1805 spin_lock_bh(&session->frwd_lock);
1808 conn->tmfcmd_pdus_cnt++;
1809 conn->tmf_timer.expires = timeout * HZ + jiffies;
1810 add_timer(&conn->tmf_timer);
1811 ISCSI_DBG_EH(session, "tmf set timeout\n");
1813 spin_unlock_bh(&session->frwd_lock);
1814 mutex_unlock(&session->eh_mutex);
1817 * block eh thread until:
1821 * 3) session is terminated or restarted or userspace has
1822 * given up on recovery
1824 wait_event_interruptible(conn->ehwait, age != session->age ||
1825 session->state != ISCSI_STATE_LOGGED_IN ||
1826 conn->tmf_state != TMF_QUEUED);
1827 if (signal_pending(current))
1828 flush_signals(current);
1829 del_timer_sync(&conn->tmf_timer);
1831 mutex_lock(&session->eh_mutex);
1832 spin_lock_bh(&session->frwd_lock);
1833 /* if the session drops it will clean up the task */
1834 if (age != session->age ||
1835 session->state != ISCSI_STATE_LOGGED_IN)
1841 * Fail commands. session lock held and recv side suspended and xmit
1844 static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
1846 struct iscsi_task *task;
1849 for (i = 0; i < conn->session->cmds_max; i++) {
1850 task = conn->session->cmds[i];
1851 if (!task->sc || task->state == ISCSI_TASK_FREE)
1854 if (lun != -1 && lun != task->sc->device->lun)
1857 ISCSI_DBG_SESSION(conn->session,
1858 "failing sc %p itt 0x%x state %d\n",
1859 task->sc, task->itt, task->state);
1860 fail_scsi_task(task, error);
1865 * iscsi_suspend_queue - suspend iscsi_queuecommand
1866 * @conn: iscsi conn to stop queueing IO on
1868 * This grabs the session frwd_lock to make sure no one is in
1869 * xmit_task/queuecommand, and then sets suspend to prevent
1870 * new commands from being queued. This only needs to be called
1871 * by offload drivers that need to sync a path like ep disconnect
1872 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1873 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1875 void iscsi_suspend_queue(struct iscsi_conn *conn)
1877 spin_lock_bh(&conn->session->frwd_lock);
1878 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1879 spin_unlock_bh(&conn->session->frwd_lock);
1881 EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1884 * iscsi_suspend_tx - suspend iscsi_data_xmit
1885 * @conn: iscsi conn tp stop processing IO on.
1887 * This function sets the suspend bit to prevent iscsi_data_xmit
1888 * from sending new IO, and if work is queued on the xmit thread
1889 * it will wait for it to be completed.
1891 void iscsi_suspend_tx(struct iscsi_conn *conn)
1893 struct Scsi_Host *shost = conn->session->host;
1894 struct iscsi_host *ihost = shost_priv(shost);
1896 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1898 flush_workqueue(ihost->workq);
1900 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1902 static void iscsi_start_tx(struct iscsi_conn *conn)
1904 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1905 iscsi_conn_queue_work(conn);
1909 * We want to make sure a ping is in flight. It has timed out.
1910 * And we are not busy processing a pdu that is making
1911 * progress but got started before the ping and is taking a while
1912 * to complete so the ping is just stuck behind it in a queue.
1914 static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1916 if (conn->ping_task &&
1917 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1918 (conn->ping_timeout * HZ), jiffies))
1924 enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1926 enum blk_eh_timer_return rc = BLK_EH_DONE;
1927 struct iscsi_task *task = NULL, *running_task;
1928 struct iscsi_cls_session *cls_session;
1929 struct iscsi_session *session;
1930 struct iscsi_conn *conn;
1933 cls_session = starget_to_session(scsi_target(sc->device));
1934 session = cls_session->dd_data;
1936 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1938 spin_lock(&session->frwd_lock);
1939 task = (struct iscsi_task *)sc->SCp.ptr;
1942 * Raced with completion. Blk layer has taken ownership
1943 * so let timeout code complete it now.
1949 if (session->state != ISCSI_STATE_LOGGED_IN) {
1951 * During shutdown, if session is prematurely disconnected,
1952 * recovery won't happen and there will be hung cmds. Not
1953 * handling cmds would trigger EH, also bad in this case.
1954 * Instead, handle cmd, allow completion to happen and let
1955 * upper layer to deal with the result.
1957 if (unlikely(system_state != SYSTEM_RUNNING)) {
1958 sc->result = DID_NO_CONNECT << 16;
1959 ISCSI_DBG_EH(session, "sc on shutdown, handled\n");
1964 * We are probably in the middle of iscsi recovery so let
1965 * that complete and handle the error.
1967 rc = BLK_EH_RESET_TIMER;
1971 conn = session->leadconn;
1973 /* In the middle of shuting down */
1974 rc = BLK_EH_RESET_TIMER;
1979 * If we have sent (at least queued to the network layer) a pdu or
1980 * recvd one for the task since the last timeout ask for
1981 * more time. If on the next timeout we have not made progress
1982 * we can check if it is the task or connection when we send the
1985 if (time_after(task->last_xfer, task->last_timeout)) {
1986 ISCSI_DBG_EH(session, "Command making progress. Asking "
1987 "scsi-ml for more time to complete. "
1988 "Last data xfer at %lu. Last timeout was at "
1989 "%lu\n.", task->last_xfer, task->last_timeout);
1990 task->have_checked_conn = false;
1991 rc = BLK_EH_RESET_TIMER;
1995 if (!conn->recv_timeout && !conn->ping_timeout)
1998 * if the ping timedout then we are in the middle of cleaning up
1999 * and can let the iscsi eh handle it
2001 if (iscsi_has_ping_timed_out(conn)) {
2002 rc = BLK_EH_RESET_TIMER;
2006 for (i = 0; i < conn->session->cmds_max; i++) {
2007 running_task = conn->session->cmds[i];
2008 if (!running_task->sc || running_task == task ||
2009 running_task->state != ISCSI_TASK_RUNNING)
2013 * Only check if cmds started before this one have made
2014 * progress, or this could never fail
2016 if (time_after(running_task->sc->jiffies_at_alloc,
2017 task->sc->jiffies_at_alloc))
2020 if (time_after(running_task->last_xfer, task->last_timeout)) {
2022 * This task has not made progress, but a task
2023 * started before us has transferred data since
2024 * we started/last-checked. We could be queueing
2025 * too many tasks or the LU is bad.
2027 * If the device is bad the cmds ahead of us on
2028 * other devs will complete, and this loop will
2029 * eventually fail starting the scsi eh.
2031 ISCSI_DBG_EH(session, "Command has not made progress "
2032 "but commands ahead of it have. "
2033 "Asking scsi-ml for more time to "
2034 "complete. Our last xfer vs running task "
2035 "last xfer %lu/%lu. Last check %lu.\n",
2036 task->last_xfer, running_task->last_xfer,
2037 task->last_timeout);
2038 rc = BLK_EH_RESET_TIMER;
2043 /* Assumes nop timeout is shorter than scsi cmd timeout */
2044 if (task->have_checked_conn)
2048 * Checking the transport already or nop from a cmd timeout still
2051 if (conn->ping_task) {
2052 task->have_checked_conn = true;
2053 rc = BLK_EH_RESET_TIMER;
2057 /* Make sure there is a transport check done */
2058 iscsi_send_nopout(conn, NULL);
2059 task->have_checked_conn = true;
2060 rc = BLK_EH_RESET_TIMER;
2064 task->last_timeout = jiffies;
2065 spin_unlock(&session->frwd_lock);
2066 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2067 "timer reset" : "shutdown or nh");
2070 EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out);
2072 static void iscsi_check_transport_timeouts(struct timer_list *t)
2074 struct iscsi_conn *conn = from_timer(conn, t, transport_timer);
2075 struct iscsi_session *session = conn->session;
2076 unsigned long recv_timeout, next_timeout = 0, last_recv;
2078 spin_lock(&session->frwd_lock);
2079 if (session->state != ISCSI_STATE_LOGGED_IN)
2082 recv_timeout = conn->recv_timeout;
2087 last_recv = conn->last_recv;
2089 if (iscsi_has_ping_timed_out(conn)) {
2090 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
2091 "expired, recv timeout %d, last rx %lu, "
2092 "last ping %lu, now %lu\n",
2093 conn->ping_timeout, conn->recv_timeout,
2094 last_recv, conn->last_ping, jiffies);
2095 spin_unlock(&session->frwd_lock);
2096 iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
2100 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2101 /* send a ping to try to provoke some traffic */
2102 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
2103 if (iscsi_send_nopout(conn, NULL))
2104 next_timeout = jiffies + (1 * HZ);
2106 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2108 next_timeout = last_recv + recv_timeout;
2110 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2111 mod_timer(&conn->transport_timer, next_timeout);
2113 spin_unlock(&session->frwd_lock);
2116 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2117 struct iscsi_tm *hdr)
2119 memset(hdr, 0, sizeof(*hdr));
2120 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2121 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2122 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2123 hdr->lun = task->lun;
2124 hdr->rtt = task->hdr_itt;
2125 hdr->refcmdsn = task->cmdsn;
2128 int iscsi_eh_abort(struct scsi_cmnd *sc)
2130 struct iscsi_cls_session *cls_session;
2131 struct iscsi_session *session;
2132 struct iscsi_conn *conn;
2133 struct iscsi_task *task;
2134 struct iscsi_tm *hdr;
2137 cls_session = starget_to_session(scsi_target(sc->device));
2138 session = cls_session->dd_data;
2140 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
2142 mutex_lock(&session->eh_mutex);
2143 spin_lock_bh(&session->frwd_lock);
2145 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2149 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2151 spin_unlock_bh(&session->frwd_lock);
2152 mutex_unlock(&session->eh_mutex);
2157 * If we are not logged in or we have started a new session
2158 * then let the host reset code handle this
2160 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2161 sc->SCp.phase != session->age) {
2162 spin_unlock_bh(&session->frwd_lock);
2163 mutex_unlock(&session->eh_mutex);
2164 ISCSI_DBG_EH(session, "failing abort due to dropped "
2169 conn = session->leadconn;
2170 conn->eh_abort_cnt++;
2173 task = (struct iscsi_task *)sc->SCp.ptr;
2174 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2177 /* task completed before time out */
2179 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
2183 if (task->state == ISCSI_TASK_PENDING) {
2184 fail_scsi_task(task, DID_ABORT);
2188 /* only have one tmf outstanding at a time */
2189 if (conn->tmf_state != TMF_INITIAL)
2191 conn->tmf_state = TMF_QUEUED;
2194 iscsi_prep_abort_task_pdu(task, hdr);
2196 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout))
2199 switch (conn->tmf_state) {
2201 spin_unlock_bh(&session->frwd_lock);
2203 * stop tx side incase the target had sent a abort rsp but
2204 * the initiator was still writing out data.
2206 iscsi_suspend_tx(conn);
2208 * we do not stop the recv side because targets have been
2209 * good and have never sent us a successful tmf response
2210 * then sent more data for the cmd.
2212 spin_lock_bh(&session->frwd_lock);
2213 fail_scsi_task(task, DID_ABORT);
2214 conn->tmf_state = TMF_INITIAL;
2215 memset(hdr, 0, sizeof(*hdr));
2216 spin_unlock_bh(&session->frwd_lock);
2217 iscsi_start_tx(conn);
2218 goto success_unlocked;
2220 spin_unlock_bh(&session->frwd_lock);
2221 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2222 goto failed_unlocked;
2225 conn->tmf_state = TMF_INITIAL;
2226 memset(hdr, 0, sizeof(*hdr));
2227 /* task completed before tmf abort response */
2228 ISCSI_DBG_EH(session, "sc completed while abort in "
2234 conn->tmf_state = TMF_INITIAL;
2239 spin_unlock_bh(&session->frwd_lock);
2241 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2243 mutex_unlock(&session->eh_mutex);
2247 spin_unlock_bh(&session->frwd_lock);
2249 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2250 task ? task->itt : 0);
2251 mutex_unlock(&session->eh_mutex);
2254 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2256 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2258 memset(hdr, 0, sizeof(*hdr));
2259 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2260 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2261 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2262 int_to_scsilun(sc->device->lun, &hdr->lun);
2263 hdr->rtt = RESERVED_ITT;
2266 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2268 struct iscsi_cls_session *cls_session;
2269 struct iscsi_session *session;
2270 struct iscsi_conn *conn;
2271 struct iscsi_tm *hdr;
2274 cls_session = starget_to_session(scsi_target(sc->device));
2275 session = cls_session->dd_data;
2277 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc,
2280 mutex_lock(&session->eh_mutex);
2281 spin_lock_bh(&session->frwd_lock);
2283 * Just check if we are not logged in. We cannot check for
2284 * the phase because the reset could come from a ioctl.
2286 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2288 conn = session->leadconn;
2290 /* only have one tmf outstanding at a time */
2291 if (conn->tmf_state != TMF_INITIAL)
2293 conn->tmf_state = TMF_QUEUED;
2296 iscsi_prep_lun_reset_pdu(sc, hdr);
2298 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2299 session->lu_reset_timeout)) {
2304 switch (conn->tmf_state) {
2308 spin_unlock_bh(&session->frwd_lock);
2309 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2312 conn->tmf_state = TMF_INITIAL;
2317 spin_unlock_bh(&session->frwd_lock);
2319 iscsi_suspend_tx(conn);
2321 spin_lock_bh(&session->frwd_lock);
2322 memset(hdr, 0, sizeof(*hdr));
2323 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2324 conn->tmf_state = TMF_INITIAL;
2325 spin_unlock_bh(&session->frwd_lock);
2327 iscsi_start_tx(conn);
2331 spin_unlock_bh(&session->frwd_lock);
2333 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2334 rc == SUCCESS ? "SUCCESS" : "FAILED");
2335 mutex_unlock(&session->eh_mutex);
2338 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2340 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2342 struct iscsi_session *session = cls_session->dd_data;
2344 spin_lock_bh(&session->frwd_lock);
2345 if (session->state != ISCSI_STATE_LOGGED_IN) {
2346 session->state = ISCSI_STATE_RECOVERY_FAILED;
2347 if (session->leadconn)
2348 wake_up(&session->leadconn->ehwait);
2350 spin_unlock_bh(&session->frwd_lock);
2352 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
2355 * iscsi_eh_session_reset - drop session and attempt relogin
2358 * This function will wait for a relogin, session termination from
2359 * userspace, or a recovery/replacement timeout.
2361 int iscsi_eh_session_reset(struct scsi_cmnd *sc)
2363 struct iscsi_cls_session *cls_session;
2364 struct iscsi_session *session;
2365 struct iscsi_conn *conn;
2367 cls_session = starget_to_session(scsi_target(sc->device));
2368 session = cls_session->dd_data;
2369 conn = session->leadconn;
2371 mutex_lock(&session->eh_mutex);
2372 spin_lock_bh(&session->frwd_lock);
2373 if (session->state == ISCSI_STATE_TERMINATE) {
2375 ISCSI_DBG_EH(session,
2376 "failing session reset: Could not log back into "
2377 "%s [age %d]\n", session->targetname,
2379 spin_unlock_bh(&session->frwd_lock);
2380 mutex_unlock(&session->eh_mutex);
2384 spin_unlock_bh(&session->frwd_lock);
2385 mutex_unlock(&session->eh_mutex);
2387 * we drop the lock here but the leadconn cannot be destoyed while
2388 * we are in the scsi eh
2390 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2392 ISCSI_DBG_EH(session, "wait for relogin\n");
2393 wait_event_interruptible(conn->ehwait,
2394 session->state == ISCSI_STATE_TERMINATE ||
2395 session->state == ISCSI_STATE_LOGGED_IN ||
2396 session->state == ISCSI_STATE_RECOVERY_FAILED);
2397 if (signal_pending(current))
2398 flush_signals(current);
2400 mutex_lock(&session->eh_mutex);
2401 spin_lock_bh(&session->frwd_lock);
2402 if (session->state == ISCSI_STATE_LOGGED_IN) {
2403 ISCSI_DBG_EH(session,
2404 "session reset succeeded for %s,%s\n",
2405 session->targetname, conn->persistent_address);
2408 spin_unlock_bh(&session->frwd_lock);
2409 mutex_unlock(&session->eh_mutex);
2412 EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
2414 static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2416 memset(hdr, 0, sizeof(*hdr));
2417 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2418 hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2419 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2420 hdr->rtt = RESERVED_ITT;
2424 * iscsi_eh_target_reset - reset target
2427 * This will attempt to send a warm target reset.
2429 static int iscsi_eh_target_reset(struct scsi_cmnd *sc)
2431 struct iscsi_cls_session *cls_session;
2432 struct iscsi_session *session;
2433 struct iscsi_conn *conn;
2434 struct iscsi_tm *hdr;
2437 cls_session = starget_to_session(scsi_target(sc->device));
2438 session = cls_session->dd_data;
2440 ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
2441 session->targetname);
2443 mutex_lock(&session->eh_mutex);
2444 spin_lock_bh(&session->frwd_lock);
2446 * Just check if we are not logged in. We cannot check for
2447 * the phase because the reset could come from a ioctl.
2449 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2451 conn = session->leadconn;
2453 /* only have one tmf outstanding at a time */
2454 if (conn->tmf_state != TMF_INITIAL)
2456 conn->tmf_state = TMF_QUEUED;
2459 iscsi_prep_tgt_reset_pdu(sc, hdr);
2461 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2462 session->tgt_reset_timeout)) {
2467 switch (conn->tmf_state) {
2471 spin_unlock_bh(&session->frwd_lock);
2472 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2475 conn->tmf_state = TMF_INITIAL;
2480 spin_unlock_bh(&session->frwd_lock);
2482 iscsi_suspend_tx(conn);
2484 spin_lock_bh(&session->frwd_lock);
2485 memset(hdr, 0, sizeof(*hdr));
2486 fail_scsi_tasks(conn, -1, DID_ERROR);
2487 conn->tmf_state = TMF_INITIAL;
2488 spin_unlock_bh(&session->frwd_lock);
2490 iscsi_start_tx(conn);
2494 spin_unlock_bh(&session->frwd_lock);
2496 ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
2497 rc == SUCCESS ? "SUCCESS" : "FAILED");
2498 mutex_unlock(&session->eh_mutex);
2503 * iscsi_eh_recover_target - reset target and possibly the session
2506 * This will attempt to send a warm target reset. If that fails,
2507 * we will escalate to ERL0 session recovery.
2509 int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2513 rc = iscsi_eh_target_reset(sc);
2515 rc = iscsi_eh_session_reset(sc);
2518 EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
2521 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2522 * should be accessed via kfifo_{get,put} on q->queue.
2523 * Optionally, the caller can obtain the array of object pointers
2524 * by passing in a non-NULL @items pointer
2527 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
2529 int i, num_arrays = 1;
2531 memset(q, 0, sizeof(*q));
2535 /* If the user passed an items pointer, he wants a copy of
2539 q->pool = kvcalloc(num_arrays * max, sizeof(void *), GFP_KERNEL);
2540 if (q->pool == NULL)
2543 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
2545 for (i = 0; i < max; i++) {
2546 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2547 if (q->pool[i] == NULL) {
2551 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
2555 *items = q->pool + max;
2556 memcpy(*items, q->pool, max * sizeof(void *));
2565 EXPORT_SYMBOL_GPL(iscsi_pool_init);
2567 void iscsi_pool_free(struct iscsi_pool *q)
2571 for (i = 0; i < q->max; i++)
2575 EXPORT_SYMBOL_GPL(iscsi_pool_free);
2578 * iscsi_host_add - add host to system
2580 * @pdev: parent device
2582 * This should be called by partial offload and software iscsi drivers
2583 * to add a host to the system.
2585 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2587 if (!shost->can_queue)
2588 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2590 if (!shost->cmd_per_lun)
2591 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2593 return scsi_add_host(shost, pdev);
2595 EXPORT_SYMBOL_GPL(iscsi_host_add);
2598 * iscsi_host_alloc - allocate a host and driver data
2599 * @sht: scsi host template
2600 * @dd_data_size: driver host data size
2601 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2603 * This should be called by partial offload and software iscsi drivers.
2604 * To access the driver specific memory use the iscsi_host_priv() macro.
2606 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2607 int dd_data_size, bool xmit_can_sleep)
2609 struct Scsi_Host *shost;
2610 struct iscsi_host *ihost;
2612 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2615 ihost = shost_priv(shost);
2617 if (xmit_can_sleep) {
2618 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2619 "iscsi_q_%d", shost->host_no);
2620 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2625 spin_lock_init(&ihost->lock);
2626 ihost->state = ISCSI_HOST_SETUP;
2627 ihost->num_sessions = 0;
2628 init_waitqueue_head(&ihost->session_removal_wq);
2632 scsi_host_put(shost);
2635 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2637 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2639 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2643 * iscsi_host_remove - remove host and sessions
2646 * If there are any sessions left, this will initiate the removal and wait
2647 * for the completion.
2649 void iscsi_host_remove(struct Scsi_Host *shost)
2651 struct iscsi_host *ihost = shost_priv(shost);
2652 unsigned long flags;
2654 spin_lock_irqsave(&ihost->lock, flags);
2655 ihost->state = ISCSI_HOST_REMOVED;
2656 spin_unlock_irqrestore(&ihost->lock, flags);
2658 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2659 wait_event_interruptible(ihost->session_removal_wq,
2660 ihost->num_sessions == 0);
2661 if (signal_pending(current))
2662 flush_signals(current);
2664 scsi_remove_host(shost);
2666 destroy_workqueue(ihost->workq);
2668 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2670 void iscsi_host_free(struct Scsi_Host *shost)
2672 struct iscsi_host *ihost = shost_priv(shost);
2674 kfree(ihost->netdev);
2675 kfree(ihost->hwaddress);
2676 kfree(ihost->initiatorname);
2677 scsi_host_put(shost);
2679 EXPORT_SYMBOL_GPL(iscsi_host_free);
2681 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2683 struct iscsi_host *ihost = shost_priv(shost);
2684 unsigned long flags;
2686 shost = scsi_host_get(shost);
2688 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2689 "of session teardown event because host already "
2694 spin_lock_irqsave(&ihost->lock, flags);
2695 ihost->num_sessions--;
2696 if (ihost->num_sessions == 0)
2697 wake_up(&ihost->session_removal_wq);
2698 spin_unlock_irqrestore(&ihost->lock, flags);
2699 scsi_host_put(shost);
2703 * iscsi_session_setup - create iscsi cls session and host and session
2704 * @iscsit: iscsi transport template
2706 * @cmds_max: session can queue
2707 * @dd_size: private driver data size, added to session allocation size
2708 * @cmd_task_size: LLD task private data size
2709 * @initial_cmdsn: initial CmdSN
2710 * @id: target ID to add to this session
2712 * This can be used by software iscsi_transports that allocate
2713 * a session per scsi host.
2715 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2716 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2717 * for nop handling and login/logout requests.
2719 struct iscsi_cls_session *
2720 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2721 uint16_t cmds_max, int dd_size, int cmd_task_size,
2722 uint32_t initial_cmdsn, unsigned int id)
2724 struct iscsi_host *ihost = shost_priv(shost);
2725 struct iscsi_session *session;
2726 struct iscsi_cls_session *cls_session;
2727 int cmd_i, scsi_cmds, total_cmds = cmds_max;
2728 unsigned long flags;
2730 spin_lock_irqsave(&ihost->lock, flags);
2731 if (ihost->state == ISCSI_HOST_REMOVED) {
2732 spin_unlock_irqrestore(&ihost->lock, flags);
2735 ihost->num_sessions++;
2736 spin_unlock_irqrestore(&ihost->lock, flags);
2739 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2741 * The iscsi layer needs some tasks for nop handling and tmfs,
2742 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2743 * + 1 command for scsi IO.
2745 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2746 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2747 "must be a power of two that is at least %d.\n",
2748 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2749 goto dec_session_count;
2752 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2753 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2754 "must be a power of 2 less than or equal to %d.\n",
2755 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2756 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2759 if (!is_power_of_2(total_cmds)) {
2760 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2761 "must be a power of 2.\n", total_cmds);
2762 total_cmds = rounddown_pow_of_two(total_cmds);
2763 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2765 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2768 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2770 cls_session = iscsi_alloc_session(shost, iscsit,
2771 sizeof(struct iscsi_session) +
2774 goto dec_session_count;
2775 session = cls_session->dd_data;
2776 session->cls_session = cls_session;
2777 session->host = shost;
2778 session->state = ISCSI_STATE_FREE;
2779 session->fast_abort = 1;
2780 session->tgt_reset_timeout = 30;
2781 session->lu_reset_timeout = 15;
2782 session->abort_timeout = 10;
2783 session->scsi_cmds_max = scsi_cmds;
2784 session->cmds_max = total_cmds;
2785 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2786 session->exp_cmdsn = initial_cmdsn + 1;
2787 session->max_cmdsn = initial_cmdsn + 1;
2788 session->max_r2t = 1;
2789 session->tt = iscsit;
2790 session->dd_data = cls_session->dd_data + sizeof(*session);
2792 mutex_init(&session->eh_mutex);
2793 spin_lock_init(&session->frwd_lock);
2794 spin_lock_init(&session->back_lock);
2796 /* initialize SCSI PDU commands pool */
2797 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2798 (void***)&session->cmds,
2799 cmd_task_size + sizeof(struct iscsi_task)))
2800 goto cmdpool_alloc_fail;
2802 /* pre-format cmds pool with ITT */
2803 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2804 struct iscsi_task *task = session->cmds[cmd_i];
2807 task->dd_data = &task[1];
2809 task->state = ISCSI_TASK_FREE;
2810 INIT_LIST_HEAD(&task->running);
2813 if (!try_module_get(iscsit->owner))
2814 goto module_get_fail;
2816 if (iscsi_add_session(cls_session, id))
2817 goto cls_session_fail;
2822 module_put(iscsit->owner);
2824 iscsi_pool_free(&session->cmdpool);
2826 iscsi_free_session(cls_session);
2828 iscsi_host_dec_session_cnt(shost);
2831 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2834 * iscsi_session_teardown - destroy session, host, and cls_session
2835 * @cls_session: iscsi session
2837 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2839 struct iscsi_session *session = cls_session->dd_data;
2840 struct module *owner = cls_session->transport->owner;
2841 struct Scsi_Host *shost = session->host;
2843 iscsi_pool_free(&session->cmdpool);
2845 iscsi_remove_session(cls_session);
2847 kfree(session->password);
2848 kfree(session->password_in);
2849 kfree(session->username);
2850 kfree(session->username_in);
2851 kfree(session->targetname);
2852 kfree(session->targetalias);
2853 kfree(session->initiatorname);
2854 kfree(session->boot_root);
2855 kfree(session->boot_nic);
2856 kfree(session->boot_target);
2857 kfree(session->ifacename);
2858 kfree(session->portal_type);
2859 kfree(session->discovery_parent_type);
2861 iscsi_free_session(cls_session);
2863 iscsi_host_dec_session_cnt(shost);
2866 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2869 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2870 * @cls_session: iscsi_cls_session
2871 * @dd_size: private driver data size
2874 struct iscsi_cls_conn *
2875 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2878 struct iscsi_session *session = cls_session->dd_data;
2879 struct iscsi_conn *conn;
2880 struct iscsi_cls_conn *cls_conn;
2883 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2887 conn = cls_conn->dd_data;
2888 memset(conn, 0, sizeof(*conn) + dd_size);
2890 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2891 conn->session = session;
2892 conn->cls_conn = cls_conn;
2893 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2894 conn->id = conn_idx;
2895 conn->exp_statsn = 0;
2896 conn->tmf_state = TMF_INITIAL;
2898 timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0);
2900 INIT_LIST_HEAD(&conn->mgmtqueue);
2901 INIT_LIST_HEAD(&conn->cmdqueue);
2902 INIT_LIST_HEAD(&conn->requeue);
2903 spin_lock_init(&conn->taskqueuelock);
2904 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2906 /* allocate login_task used for the login/text sequences */
2907 spin_lock_bh(&session->frwd_lock);
2908 if (!kfifo_out(&session->cmdpool.queue,
2909 (void*)&conn->login_task,
2911 spin_unlock_bh(&session->frwd_lock);
2912 goto login_task_alloc_fail;
2914 spin_unlock_bh(&session->frwd_lock);
2916 data = (char *) __get_free_pages(GFP_KERNEL,
2917 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2919 goto login_task_data_alloc_fail;
2920 conn->login_task->data = conn->data = data;
2922 timer_setup(&conn->tmf_timer, iscsi_tmf_timedout, 0);
2923 init_waitqueue_head(&conn->ehwait);
2927 login_task_data_alloc_fail:
2928 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2930 login_task_alloc_fail:
2931 iscsi_destroy_conn(cls_conn);
2934 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2937 * iscsi_conn_teardown - teardown iscsi connection
2938 * @cls_conn: iscsi class connection
2940 * TODO: we may need to make this into a two step process
2941 * like scsi-mls remove + put host
2943 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2945 struct iscsi_conn *conn = cls_conn->dd_data;
2946 struct iscsi_session *session = conn->session;
2948 del_timer_sync(&conn->transport_timer);
2950 mutex_lock(&session->eh_mutex);
2951 spin_lock_bh(&session->frwd_lock);
2952 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2953 if (session->leadconn == conn) {
2955 * leading connection? then give up on recovery.
2957 session->state = ISCSI_STATE_TERMINATE;
2958 wake_up(&conn->ehwait);
2960 spin_unlock_bh(&session->frwd_lock);
2962 /* flush queued up work because we free the connection below */
2963 iscsi_suspend_tx(conn);
2965 spin_lock_bh(&session->frwd_lock);
2966 free_pages((unsigned long) conn->data,
2967 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2968 kfree(conn->persistent_address);
2969 kfree(conn->local_ipaddr);
2970 /* regular RX path uses back_lock */
2971 spin_lock_bh(&session->back_lock);
2972 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2974 spin_unlock_bh(&session->back_lock);
2975 if (session->leadconn == conn)
2976 session->leadconn = NULL;
2977 spin_unlock_bh(&session->frwd_lock);
2978 mutex_unlock(&session->eh_mutex);
2980 iscsi_destroy_conn(cls_conn);
2982 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2984 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2986 struct iscsi_conn *conn = cls_conn->dd_data;
2987 struct iscsi_session *session = conn->session;
2990 iscsi_conn_printk(KERN_ERR, conn,
2991 "can't start unbound connection\n");
2995 if ((session->imm_data_en || !session->initial_r2t_en) &&
2996 session->first_burst > session->max_burst) {
2997 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2998 "first_burst %d max_burst %d\n",
2999 session->first_burst, session->max_burst);
3003 if (conn->ping_timeout && !conn->recv_timeout) {
3004 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
3005 "zero. Using 5 seconds\n.");
3006 conn->recv_timeout = 5;
3009 if (conn->recv_timeout && !conn->ping_timeout) {
3010 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3011 "zero. Using 5 seconds.\n");
3012 conn->ping_timeout = 5;
3015 spin_lock_bh(&session->frwd_lock);
3016 conn->c_stage = ISCSI_CONN_STARTED;
3017 session->state = ISCSI_STATE_LOGGED_IN;
3018 session->queued_cmdsn = session->cmdsn;
3020 conn->last_recv = jiffies;
3021 conn->last_ping = jiffies;
3022 if (conn->recv_timeout && conn->ping_timeout)
3023 mod_timer(&conn->transport_timer,
3024 jiffies + (conn->recv_timeout * HZ));
3026 switch(conn->stop_stage) {
3027 case STOP_CONN_RECOVER:
3029 * unblock eh_abort() if it is blocked. re-try all
3030 * commands after successful recovery
3032 conn->stop_stage = 0;
3033 conn->tmf_state = TMF_INITIAL;
3035 if (session->age == 16)
3038 case STOP_CONN_TERM:
3039 conn->stop_stage = 0;
3044 spin_unlock_bh(&session->frwd_lock);
3046 iscsi_unblock_session(session->cls_session);
3047 wake_up(&conn->ehwait);
3050 EXPORT_SYMBOL_GPL(iscsi_conn_start);
3053 fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
3055 struct iscsi_task *task;
3058 for (i = 0; i < conn->session->cmds_max; i++) {
3059 task = conn->session->cmds[i];
3063 if (task->state == ISCSI_TASK_FREE)
3066 ISCSI_DBG_SESSION(conn->session,
3067 "failing mgmt itt 0x%x state %d\n",
3068 task->itt, task->state);
3069 state = ISCSI_TASK_ABRT_SESS_RECOV;
3070 if (task->state == ISCSI_TASK_PENDING)
3071 state = ISCSI_TASK_COMPLETED;
3072 iscsi_complete_task(task, state);
3077 static void iscsi_start_session_recovery(struct iscsi_session *session,
3078 struct iscsi_conn *conn, int flag)
3082 mutex_lock(&session->eh_mutex);
3083 spin_lock_bh(&session->frwd_lock);
3084 if (conn->stop_stage == STOP_CONN_TERM) {
3085 spin_unlock_bh(&session->frwd_lock);
3086 mutex_unlock(&session->eh_mutex);
3091 * When this is called for the in_login state, we only want to clean
3092 * up the login task and connection. We do not need to block and set
3093 * the recovery state again
3095 if (flag == STOP_CONN_TERM)
3096 session->state = ISCSI_STATE_TERMINATE;
3097 else if (conn->stop_stage != STOP_CONN_RECOVER)
3098 session->state = ISCSI_STATE_IN_RECOVERY;
3100 old_stop_stage = conn->stop_stage;
3101 conn->stop_stage = flag;
3102 spin_unlock_bh(&session->frwd_lock);
3104 del_timer_sync(&conn->transport_timer);
3105 iscsi_suspend_tx(conn);
3107 spin_lock_bh(&session->frwd_lock);
3108 conn->c_stage = ISCSI_CONN_STOPPED;
3109 spin_unlock_bh(&session->frwd_lock);
3112 * for connection level recovery we should not calculate
3113 * header digest. conn->hdr_size used for optimization
3114 * in hdr_extract() and will be re-negotiated at
3117 if (flag == STOP_CONN_RECOVER) {
3118 conn->hdrdgst_en = 0;
3119 conn->datadgst_en = 0;
3120 if (session->state == ISCSI_STATE_IN_RECOVERY &&
3121 old_stop_stage != STOP_CONN_RECOVER) {
3122 ISCSI_DBG_SESSION(session, "blocking session\n");
3123 iscsi_block_session(session->cls_session);
3130 spin_lock_bh(&session->frwd_lock);
3131 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
3132 fail_mgmt_tasks(session, conn);
3133 memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
3134 spin_unlock_bh(&session->frwd_lock);
3135 mutex_unlock(&session->eh_mutex);
3138 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3140 struct iscsi_conn *conn = cls_conn->dd_data;
3141 struct iscsi_session *session = conn->session;
3144 case STOP_CONN_RECOVER:
3145 case STOP_CONN_TERM:
3146 iscsi_start_session_recovery(session, conn, flag);
3149 iscsi_conn_printk(KERN_ERR, conn,
3150 "invalid stop flag %d\n", flag);
3153 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3155 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3156 struct iscsi_cls_conn *cls_conn, int is_leading)
3158 struct iscsi_session *session = cls_session->dd_data;
3159 struct iscsi_conn *conn = cls_conn->dd_data;
3161 spin_lock_bh(&session->frwd_lock);
3163 session->leadconn = conn;
3164 spin_unlock_bh(&session->frwd_lock);
3167 * Unblock xmitworker(), Login Phase will pass through.
3169 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
3170 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
3173 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3175 int iscsi_switch_str_param(char **param, char *new_val_buf)
3180 if (!strcmp(*param, new_val_buf))
3184 new_val = kstrdup(new_val_buf, GFP_NOIO);
3192 EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
3194 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3195 enum iscsi_param param, char *buf, int buflen)
3197 struct iscsi_conn *conn = cls_conn->dd_data;
3198 struct iscsi_session *session = conn->session;
3202 case ISCSI_PARAM_FAST_ABORT:
3203 sscanf(buf, "%d", &session->fast_abort);
3205 case ISCSI_PARAM_ABORT_TMO:
3206 sscanf(buf, "%d", &session->abort_timeout);
3208 case ISCSI_PARAM_LU_RESET_TMO:
3209 sscanf(buf, "%d", &session->lu_reset_timeout);
3211 case ISCSI_PARAM_TGT_RESET_TMO:
3212 sscanf(buf, "%d", &session->tgt_reset_timeout);
3214 case ISCSI_PARAM_PING_TMO:
3215 sscanf(buf, "%d", &conn->ping_timeout);
3217 case ISCSI_PARAM_RECV_TMO:
3218 sscanf(buf, "%d", &conn->recv_timeout);
3220 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3221 sscanf(buf, "%d", &conn->max_recv_dlength);
3223 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3224 sscanf(buf, "%d", &conn->max_xmit_dlength);
3226 case ISCSI_PARAM_HDRDGST_EN:
3227 sscanf(buf, "%d", &conn->hdrdgst_en);
3229 case ISCSI_PARAM_DATADGST_EN:
3230 sscanf(buf, "%d", &conn->datadgst_en);
3232 case ISCSI_PARAM_INITIAL_R2T_EN:
3233 sscanf(buf, "%d", &session->initial_r2t_en);
3235 case ISCSI_PARAM_MAX_R2T:
3236 sscanf(buf, "%hu", &session->max_r2t);
3238 case ISCSI_PARAM_IMM_DATA_EN:
3239 sscanf(buf, "%d", &session->imm_data_en);
3241 case ISCSI_PARAM_FIRST_BURST:
3242 sscanf(buf, "%d", &session->first_burst);
3244 case ISCSI_PARAM_MAX_BURST:
3245 sscanf(buf, "%d", &session->max_burst);
3247 case ISCSI_PARAM_PDU_INORDER_EN:
3248 sscanf(buf, "%d", &session->pdu_inorder_en);
3250 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3251 sscanf(buf, "%d", &session->dataseq_inorder_en);
3253 case ISCSI_PARAM_ERL:
3254 sscanf(buf, "%d", &session->erl);
3256 case ISCSI_PARAM_EXP_STATSN:
3257 sscanf(buf, "%u", &conn->exp_statsn);
3259 case ISCSI_PARAM_USERNAME:
3260 return iscsi_switch_str_param(&session->username, buf);
3261 case ISCSI_PARAM_USERNAME_IN:
3262 return iscsi_switch_str_param(&session->username_in, buf);
3263 case ISCSI_PARAM_PASSWORD:
3264 return iscsi_switch_str_param(&session->password, buf);
3265 case ISCSI_PARAM_PASSWORD_IN:
3266 return iscsi_switch_str_param(&session->password_in, buf);
3267 case ISCSI_PARAM_TARGET_NAME:
3268 return iscsi_switch_str_param(&session->targetname, buf);
3269 case ISCSI_PARAM_TARGET_ALIAS:
3270 return iscsi_switch_str_param(&session->targetalias, buf);
3271 case ISCSI_PARAM_TPGT:
3272 sscanf(buf, "%d", &session->tpgt);
3274 case ISCSI_PARAM_PERSISTENT_PORT:
3275 sscanf(buf, "%d", &conn->persistent_port);
3277 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3278 return iscsi_switch_str_param(&conn->persistent_address, buf);
3279 case ISCSI_PARAM_IFACE_NAME:
3280 return iscsi_switch_str_param(&session->ifacename, buf);
3281 case ISCSI_PARAM_INITIATOR_NAME:
3282 return iscsi_switch_str_param(&session->initiatorname, buf);
3283 case ISCSI_PARAM_BOOT_ROOT:
3284 return iscsi_switch_str_param(&session->boot_root, buf);
3285 case ISCSI_PARAM_BOOT_NIC:
3286 return iscsi_switch_str_param(&session->boot_nic, buf);
3287 case ISCSI_PARAM_BOOT_TARGET:
3288 return iscsi_switch_str_param(&session->boot_target, buf);
3289 case ISCSI_PARAM_PORTAL_TYPE:
3290 return iscsi_switch_str_param(&session->portal_type, buf);
3291 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3292 return iscsi_switch_str_param(&session->discovery_parent_type,
3294 case ISCSI_PARAM_DISCOVERY_SESS:
3295 sscanf(buf, "%d", &val);
3296 session->discovery_sess = !!val;
3298 case ISCSI_PARAM_LOCAL_IPADDR:
3299 return iscsi_switch_str_param(&conn->local_ipaddr, buf);
3306 EXPORT_SYMBOL_GPL(iscsi_set_param);
3308 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3309 enum iscsi_param param, char *buf)
3311 struct iscsi_session *session = cls_session->dd_data;
3315 case ISCSI_PARAM_FAST_ABORT:
3316 len = sprintf(buf, "%d\n", session->fast_abort);
3318 case ISCSI_PARAM_ABORT_TMO:
3319 len = sprintf(buf, "%d\n", session->abort_timeout);
3321 case ISCSI_PARAM_LU_RESET_TMO:
3322 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
3324 case ISCSI_PARAM_TGT_RESET_TMO:
3325 len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
3327 case ISCSI_PARAM_INITIAL_R2T_EN:
3328 len = sprintf(buf, "%d\n", session->initial_r2t_en);
3330 case ISCSI_PARAM_MAX_R2T:
3331 len = sprintf(buf, "%hu\n", session->max_r2t);
3333 case ISCSI_PARAM_IMM_DATA_EN:
3334 len = sprintf(buf, "%d\n", session->imm_data_en);
3336 case ISCSI_PARAM_FIRST_BURST:
3337 len = sprintf(buf, "%u\n", session->first_burst);
3339 case ISCSI_PARAM_MAX_BURST:
3340 len = sprintf(buf, "%u\n", session->max_burst);
3342 case ISCSI_PARAM_PDU_INORDER_EN:
3343 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
3345 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3346 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
3348 case ISCSI_PARAM_DEF_TASKMGMT_TMO:
3349 len = sprintf(buf, "%d\n", session->def_taskmgmt_tmo);
3351 case ISCSI_PARAM_ERL:
3352 len = sprintf(buf, "%d\n", session->erl);
3354 case ISCSI_PARAM_TARGET_NAME:
3355 len = sprintf(buf, "%s\n", session->targetname);
3357 case ISCSI_PARAM_TARGET_ALIAS:
3358 len = sprintf(buf, "%s\n", session->targetalias);
3360 case ISCSI_PARAM_TPGT:
3361 len = sprintf(buf, "%d\n", session->tpgt);
3363 case ISCSI_PARAM_USERNAME:
3364 len = sprintf(buf, "%s\n", session->username);
3366 case ISCSI_PARAM_USERNAME_IN:
3367 len = sprintf(buf, "%s\n", session->username_in);
3369 case ISCSI_PARAM_PASSWORD:
3370 len = sprintf(buf, "%s\n", session->password);
3372 case ISCSI_PARAM_PASSWORD_IN:
3373 len = sprintf(buf, "%s\n", session->password_in);
3375 case ISCSI_PARAM_IFACE_NAME:
3376 len = sprintf(buf, "%s\n", session->ifacename);
3378 case ISCSI_PARAM_INITIATOR_NAME:
3379 len = sprintf(buf, "%s\n", session->initiatorname);
3381 case ISCSI_PARAM_BOOT_ROOT:
3382 len = sprintf(buf, "%s\n", session->boot_root);
3384 case ISCSI_PARAM_BOOT_NIC:
3385 len = sprintf(buf, "%s\n", session->boot_nic);
3387 case ISCSI_PARAM_BOOT_TARGET:
3388 len = sprintf(buf, "%s\n", session->boot_target);
3390 case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
3391 len = sprintf(buf, "%u\n", session->auto_snd_tgt_disable);
3393 case ISCSI_PARAM_DISCOVERY_SESS:
3394 len = sprintf(buf, "%u\n", session->discovery_sess);
3396 case ISCSI_PARAM_PORTAL_TYPE:
3397 len = sprintf(buf, "%s\n", session->portal_type);
3399 case ISCSI_PARAM_CHAP_AUTH_EN:
3400 len = sprintf(buf, "%u\n", session->chap_auth_en);
3402 case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
3403 len = sprintf(buf, "%u\n", session->discovery_logout_en);
3405 case ISCSI_PARAM_BIDI_CHAP_EN:
3406 len = sprintf(buf, "%u\n", session->bidi_chap_en);
3408 case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
3409 len = sprintf(buf, "%u\n", session->discovery_auth_optional);
3411 case ISCSI_PARAM_DEF_TIME2WAIT:
3412 len = sprintf(buf, "%d\n", session->time2wait);
3414 case ISCSI_PARAM_DEF_TIME2RETAIN:
3415 len = sprintf(buf, "%d\n", session->time2retain);
3417 case ISCSI_PARAM_TSID:
3418 len = sprintf(buf, "%u\n", session->tsid);
3420 case ISCSI_PARAM_ISID:
3421 len = sprintf(buf, "%02x%02x%02x%02x%02x%02x\n",
3422 session->isid[0], session->isid[1],
3423 session->isid[2], session->isid[3],
3424 session->isid[4], session->isid[5]);
3426 case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
3427 len = sprintf(buf, "%u\n", session->discovery_parent_idx);
3429 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3430 if (session->discovery_parent_type)
3431 len = sprintf(buf, "%s\n",
3432 session->discovery_parent_type);
3434 len = sprintf(buf, "\n");
3442 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3444 int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
3445 enum iscsi_param param, char *buf)
3447 struct sockaddr_in6 *sin6 = NULL;
3448 struct sockaddr_in *sin = NULL;
3451 switch (addr->ss_family) {
3453 sin = (struct sockaddr_in *)addr;
3456 sin6 = (struct sockaddr_in6 *)addr;
3463 case ISCSI_PARAM_CONN_ADDRESS:
3464 case ISCSI_HOST_PARAM_IPADDRESS:
3466 len = sprintf(buf, "%pI4\n", &sin->sin_addr.s_addr);
3468 len = sprintf(buf, "%pI6\n", &sin6->sin6_addr);
3470 case ISCSI_PARAM_CONN_PORT:
3471 case ISCSI_PARAM_LOCAL_PORT:
3473 len = sprintf(buf, "%hu\n", be16_to_cpu(sin->sin_port));
3475 len = sprintf(buf, "%hu\n",
3476 be16_to_cpu(sin6->sin6_port));
3484 EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
3486 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3487 enum iscsi_param param, char *buf)
3489 struct iscsi_conn *conn = cls_conn->dd_data;
3493 case ISCSI_PARAM_PING_TMO:
3494 len = sprintf(buf, "%u\n", conn->ping_timeout);
3496 case ISCSI_PARAM_RECV_TMO:
3497 len = sprintf(buf, "%u\n", conn->recv_timeout);
3499 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3500 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3502 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3503 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3505 case ISCSI_PARAM_HDRDGST_EN:
3506 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3508 case ISCSI_PARAM_DATADGST_EN:
3509 len = sprintf(buf, "%d\n", conn->datadgst_en);
3511 case ISCSI_PARAM_IFMARKER_EN:
3512 len = sprintf(buf, "%d\n", conn->ifmarker_en);
3514 case ISCSI_PARAM_OFMARKER_EN:
3515 len = sprintf(buf, "%d\n", conn->ofmarker_en);
3517 case ISCSI_PARAM_EXP_STATSN:
3518 len = sprintf(buf, "%u\n", conn->exp_statsn);
3520 case ISCSI_PARAM_PERSISTENT_PORT:
3521 len = sprintf(buf, "%d\n", conn->persistent_port);
3523 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3524 len = sprintf(buf, "%s\n", conn->persistent_address);
3526 case ISCSI_PARAM_STATSN:
3527 len = sprintf(buf, "%u\n", conn->statsn);
3529 case ISCSI_PARAM_MAX_SEGMENT_SIZE:
3530 len = sprintf(buf, "%u\n", conn->max_segment_size);
3532 case ISCSI_PARAM_KEEPALIVE_TMO:
3533 len = sprintf(buf, "%u\n", conn->keepalive_tmo);
3535 case ISCSI_PARAM_LOCAL_PORT:
3536 len = sprintf(buf, "%u\n", conn->local_port);
3538 case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
3539 len = sprintf(buf, "%u\n", conn->tcp_timestamp_stat);
3541 case ISCSI_PARAM_TCP_NAGLE_DISABLE:
3542 len = sprintf(buf, "%u\n", conn->tcp_nagle_disable);
3544 case ISCSI_PARAM_TCP_WSF_DISABLE:
3545 len = sprintf(buf, "%u\n", conn->tcp_wsf_disable);
3547 case ISCSI_PARAM_TCP_TIMER_SCALE:
3548 len = sprintf(buf, "%u\n", conn->tcp_timer_scale);
3550 case ISCSI_PARAM_TCP_TIMESTAMP_EN:
3551 len = sprintf(buf, "%u\n", conn->tcp_timestamp_en);
3553 case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
3554 len = sprintf(buf, "%u\n", conn->fragment_disable);
3556 case ISCSI_PARAM_IPV4_TOS:
3557 len = sprintf(buf, "%u\n", conn->ipv4_tos);
3559 case ISCSI_PARAM_IPV6_TC:
3560 len = sprintf(buf, "%u\n", conn->ipv6_traffic_class);
3562 case ISCSI_PARAM_IPV6_FLOW_LABEL:
3563 len = sprintf(buf, "%u\n", conn->ipv6_flow_label);
3565 case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
3566 len = sprintf(buf, "%u\n", conn->is_fw_assigned_ipv6);
3568 case ISCSI_PARAM_TCP_XMIT_WSF:
3569 len = sprintf(buf, "%u\n", conn->tcp_xmit_wsf);
3571 case ISCSI_PARAM_TCP_RECV_WSF:
3572 len = sprintf(buf, "%u\n", conn->tcp_recv_wsf);
3574 case ISCSI_PARAM_LOCAL_IPADDR:
3575 len = sprintf(buf, "%s\n", conn->local_ipaddr);
3583 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3585 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3588 struct iscsi_host *ihost = shost_priv(shost);
3592 case ISCSI_HOST_PARAM_NETDEV_NAME:
3593 len = sprintf(buf, "%s\n", ihost->netdev);
3595 case ISCSI_HOST_PARAM_HWADDRESS:
3596 len = sprintf(buf, "%s\n", ihost->hwaddress);
3598 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3599 len = sprintf(buf, "%s\n", ihost->initiatorname);
3607 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3609 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3610 char *buf, int buflen)
3612 struct iscsi_host *ihost = shost_priv(shost);
3615 case ISCSI_HOST_PARAM_NETDEV_NAME:
3616 return iscsi_switch_str_param(&ihost->netdev, buf);
3617 case ISCSI_HOST_PARAM_HWADDRESS:
3618 return iscsi_switch_str_param(&ihost->hwaddress, buf);
3619 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3620 return iscsi_switch_str_param(&ihost->initiatorname, buf);
3627 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3629 MODULE_AUTHOR("Mike Christie");
3630 MODULE_DESCRIPTION("iSCSI library functions");
3631 MODULE_LICENSE("GPL");