4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/types.h>
25 #include <linux/kfifo.h>
26 #include <linux/delay.h>
27 #include <linux/log2.h>
28 #include <asm/unaligned.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi.h>
36 #include <scsi/iscsi_proto.h>
37 #include <scsi/scsi_transport.h>
38 #include <scsi/scsi_transport_iscsi.h>
39 #include <scsi/libiscsi.h>
41 static int iscsi_dbg_lib_conn;
42 module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
44 MODULE_PARM_DESC(debug_libiscsi_conn,
45 "Turn on debugging for connections in libiscsi module. "
46 "Set to 1 to turn on, and zero to turn off. Default is off.");
48 static int iscsi_dbg_lib_session;
49 module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
51 MODULE_PARM_DESC(debug_libiscsi_session,
52 "Turn on debugging for sessions in libiscsi module. "
53 "Set to 1 to turn on, and zero to turn off. Default is off.");
55 static int iscsi_dbg_lib_eh;
56 module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
58 MODULE_PARM_DESC(debug_libiscsi_eh,
59 "Turn on debugging for error handling in libiscsi module. "
60 "Set to 1 to turn on, and zero to turn off. Default is off.");
62 #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
64 if (iscsi_dbg_lib_conn) \
65 iscsi_conn_printk(KERN_INFO, _conn, \
70 #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
72 if (iscsi_dbg_lib_session) \
73 iscsi_session_printk(KERN_INFO, _session, \
78 #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
80 if (iscsi_dbg_lib_eh) \
81 iscsi_session_printk(KERN_INFO, _session, \
86 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
87 #define SNA32_CHECK 2147483648UL
89 static int iscsi_sna_lt(u32 n1, u32 n2)
91 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
92 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
95 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
96 static int iscsi_sna_lte(u32 n1, u32 n2)
98 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
99 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
102 inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
104 struct Scsi_Host *shost = conn->session->host;
105 struct iscsi_host *ihost = shost_priv(shost);
108 queue_work(ihost->workq, &conn->xmitwork);
110 EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
112 static void __iscsi_update_cmdsn(struct iscsi_session *session,
113 uint32_t exp_cmdsn, uint32_t max_cmdsn)
116 * standard specifies this check for when to update expected and
117 * max sequence numbers
119 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
122 if (exp_cmdsn != session->exp_cmdsn &&
123 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
124 session->exp_cmdsn = exp_cmdsn;
126 if (max_cmdsn != session->max_cmdsn &&
127 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
128 session->max_cmdsn = max_cmdsn;
130 * if the window closed with IO queued, then kick the
133 if (!list_empty(&session->leadconn->cmdqueue) ||
134 !list_empty(&session->leadconn->mgmtqueue))
135 iscsi_conn_queue_work(session->leadconn);
139 void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
141 __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
142 be32_to_cpu(hdr->max_cmdsn));
144 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
147 * iscsi_prep_data_out_pdu - initialize Data-Out
148 * @task: scsi command task
150 * @hdr: iscsi data in pdu
153 * Initialize Data-Out within this R2T sequence and finds
154 * proper data_offset within this SCSI command.
156 * This function is called with connection lock taken.
158 void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
159 struct iscsi_data *hdr)
161 struct iscsi_conn *conn = task->conn;
162 unsigned int left = r2t->data_length - r2t->sent;
164 task->hdr_len = sizeof(struct iscsi_data);
166 memset(hdr, 0, sizeof(struct iscsi_data));
168 hdr->datasn = cpu_to_be32(r2t->datasn);
170 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
171 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
172 hdr->itt = task->hdr_itt;
173 hdr->exp_statsn = r2t->exp_statsn;
174 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
175 if (left > conn->max_xmit_dlength) {
176 hton24(hdr->dlength, conn->max_xmit_dlength);
177 r2t->data_count = conn->max_xmit_dlength;
180 hton24(hdr->dlength, left);
181 r2t->data_count = left;
182 hdr->flags = ISCSI_FLAG_CMD_FINAL;
184 conn->dataout_pdus_cnt++;
186 EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
188 static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
190 unsigned exp_len = task->hdr_len + len;
192 if (exp_len > task->hdr_max) {
197 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
198 task->hdr_len = exp_len;
203 * make an extended cdb AHS
205 static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
207 struct scsi_cmnd *cmd = task->sc;
208 unsigned rlen, pad_len;
209 unsigned short ahslength;
210 struct iscsi_ecdb_ahdr *ecdb_ahdr;
213 ecdb_ahdr = iscsi_next_hdr(task);
214 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
216 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
217 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
219 pad_len = iscsi_padding(rlen);
221 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
222 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
227 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
229 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
230 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
231 ecdb_ahdr->reserved = 0;
232 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
234 ISCSI_DBG_SESSION(task->conn->session,
235 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
236 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
237 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
242 static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
244 struct scsi_cmnd *sc = task->sc;
245 struct iscsi_rlength_ahdr *rlen_ahdr;
248 rlen_ahdr = iscsi_next_hdr(task);
249 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
253 rlen_ahdr->ahslength =
254 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
255 sizeof(rlen_ahdr->reserved));
256 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
257 rlen_ahdr->reserved = 0;
258 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
260 ISCSI_DBG_SESSION(task->conn->session,
261 "bidi-in rlen_ahdr->read_length(%d) "
262 "rlen_ahdr->ahslength(%d)\n",
263 be32_to_cpu(rlen_ahdr->read_length),
264 be16_to_cpu(rlen_ahdr->ahslength));
269 * iscsi_check_tmf_restrictions - check if a task is affected by TMF
271 * @opcode: opcode to check for
273 * During TMF a task has to be checked if it's affected.
274 * All unrelated I/O can be passed through, but I/O to the
275 * affected LUN should be restricted.
276 * If 'fast_abort' is set we won't be sending any I/O to the
278 * Otherwise the target is waiting for all TTTs to be completed,
279 * so we have to send all outstanding Data-Out PDUs to the target.
281 static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
283 struct iscsi_conn *conn = task->conn;
284 struct iscsi_tm *tmf = &conn->tmhdr;
285 unsigned int hdr_lun;
287 if (conn->tmf_state == TMF_INITIAL)
290 if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
293 switch (ISCSI_TM_FUNC_VALUE(tmf)) {
294 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
296 * Allow PDUs for unrelated LUNs
298 hdr_lun = scsilun_to_int((struct scsi_lun *)tmf->lun);
299 if (hdr_lun != task->sc->device->lun)
302 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
304 * Fail all SCSI cmd PDUs
306 if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
307 iscsi_conn_printk(KERN_INFO, conn,
308 "task [op %x/%x itt "
311 task->hdr->opcode, opcode,
312 task->itt, task->hdr_itt);
316 * And also all data-out PDUs in response to R2T
317 * if fast_abort is set.
319 if (conn->session->fast_abort) {
320 iscsi_conn_printk(KERN_INFO, conn,
321 "task [op %x/%x itt "
322 "0x%x/0x%x] fast abort.\n",
323 task->hdr->opcode, opcode,
324 task->itt, task->hdr_itt);
328 case ISCSI_TM_FUNC_ABORT_TASK:
330 * the caller has already checked if the task
331 * they want to abort was in the pending queue so if
332 * we are here the cmd pdu has gone out already, and
333 * we will only hit this for data-outs
335 if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
336 task->hdr_itt == tmf->rtt) {
337 ISCSI_DBG_SESSION(conn->session,
338 "Preventing task %x/%x from sending "
339 "data-out due to abort task in "
340 "progress\n", task->itt,
351 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
354 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
355 * fields like dlength or final based on how much data it sends
357 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
359 struct iscsi_conn *conn = task->conn;
360 struct iscsi_session *session = conn->session;
361 struct scsi_cmnd *sc = task->sc;
362 struct iscsi_cmd *hdr;
363 unsigned hdrlength, cmd_len;
367 rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
371 if (conn->session->tt->alloc_pdu) {
372 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
376 hdr = (struct iscsi_cmd *) task->hdr;
378 memset(hdr, 0, sizeof(*hdr));
380 if (session->tt->parse_pdu_itt)
381 hdr->itt = task->hdr_itt = itt;
383 hdr->itt = task->hdr_itt = build_itt(task->itt,
384 task->conn->session->age);
386 rc = iscsi_add_hdr(task, sizeof(*hdr));
389 hdr->opcode = ISCSI_OP_SCSI_CMD;
390 hdr->flags = ISCSI_ATTR_SIMPLE;
391 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
392 memcpy(task->lun, hdr->lun, sizeof(task->lun));
393 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
394 cmd_len = sc->cmd_len;
395 if (cmd_len < ISCSI_CDB_SIZE)
396 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
397 else if (cmd_len > ISCSI_CDB_SIZE) {
398 rc = iscsi_prep_ecdb_ahs(task);
401 cmd_len = ISCSI_CDB_SIZE;
403 memcpy(hdr->cdb, sc->cmnd, cmd_len);
406 if (scsi_bidi_cmnd(sc)) {
407 hdr->flags |= ISCSI_FLAG_CMD_READ;
408 rc = iscsi_prep_bidi_ahs(task);
412 if (sc->sc_data_direction == DMA_TO_DEVICE) {
413 unsigned out_len = scsi_out(sc)->length;
414 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
416 hdr->data_length = cpu_to_be32(out_len);
417 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
421 * imm_count bytes to be sent right after
424 * unsol_count bytes(as Data-Out) to be sent
425 * without R2T ack right after
428 * r2t data_length bytes to be sent via R2T ack's
430 * pad_count bytes to be sent as zero-padding
432 memset(r2t, 0, sizeof(*r2t));
434 if (session->imm_data_en) {
435 if (out_len >= session->first_burst)
436 task->imm_count = min(session->first_burst,
437 conn->max_xmit_dlength);
439 task->imm_count = min(out_len,
440 conn->max_xmit_dlength);
441 hton24(hdr->dlength, task->imm_count);
443 zero_data(hdr->dlength);
445 if (!session->initial_r2t_en) {
446 r2t->data_length = min(session->first_burst, out_len) -
448 r2t->data_offset = task->imm_count;
449 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
450 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
453 if (!task->unsol_r2t.data_length)
454 /* No unsolicit Data-Out's */
455 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
457 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
458 zero_data(hdr->dlength);
459 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
461 if (sc->sc_data_direction == DMA_FROM_DEVICE)
462 hdr->flags |= ISCSI_FLAG_CMD_READ;
465 /* calculate size of additional header segments (AHSs) */
466 hdrlength = task->hdr_len - sizeof(*hdr);
468 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
469 hdrlength /= ISCSI_PAD_LEN;
471 WARN_ON(hdrlength >= 256);
472 hdr->hlength = hdrlength & 0xFF;
474 if (session->tt->init_task && session->tt->init_task(task))
477 task->state = ISCSI_TASK_RUNNING;
478 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
481 conn->scsicmd_pdus_cnt++;
482 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
483 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
484 scsi_bidi_cmnd(sc) ? "bidirectional" :
485 sc->sc_data_direction == DMA_TO_DEVICE ?
486 "write" : "read", conn->id, sc, sc->cmnd[0],
487 task->itt, scsi_bufflen(sc),
488 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
490 session->max_cmdsn - session->exp_cmdsn + 1);
495 * iscsi_free_task - free a task
496 * @task: iscsi cmd task
498 * Must be called with session lock.
499 * This function returns the scsi command to scsi-ml or cleans
500 * up mgmt tasks then returns the task to the pool.
502 static void iscsi_free_task(struct iscsi_task *task)
504 struct iscsi_conn *conn = task->conn;
505 struct iscsi_session *session = conn->session;
506 struct scsi_cmnd *sc = task->sc;
508 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
509 task->itt, task->state, task->sc);
511 session->tt->cleanup_task(task);
512 task->state = ISCSI_TASK_FREE;
515 * login task is preallocated so do not free
517 if (conn->login_task == task)
520 kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
524 /* SCSI eh reuses commands to verify us */
527 * queue command may call this to free the task, but
528 * not have setup the sc callback
535 void __iscsi_get_task(struct iscsi_task *task)
537 atomic_inc(&task->refcount);
539 EXPORT_SYMBOL_GPL(__iscsi_get_task);
541 static void __iscsi_put_task(struct iscsi_task *task)
543 if (atomic_dec_and_test(&task->refcount))
544 iscsi_free_task(task);
547 void iscsi_put_task(struct iscsi_task *task)
549 struct iscsi_session *session = task->conn->session;
551 spin_lock_bh(&session->lock);
552 __iscsi_put_task(task);
553 spin_unlock_bh(&session->lock);
555 EXPORT_SYMBOL_GPL(iscsi_put_task);
558 * iscsi_complete_task - finish a task
559 * @task: iscsi cmd task
560 * @state: state to complete task with
562 * Must be called with session lock.
564 static void iscsi_complete_task(struct iscsi_task *task, int state)
566 struct iscsi_conn *conn = task->conn;
568 ISCSI_DBG_SESSION(conn->session,
569 "complete task itt 0x%x state %d sc %p\n",
570 task->itt, task->state, task->sc);
571 if (task->state == ISCSI_TASK_COMPLETED ||
572 task->state == ISCSI_TASK_ABRT_TMF ||
573 task->state == ISCSI_TASK_ABRT_SESS_RECOV)
575 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
578 if (!list_empty(&task->running))
579 list_del_init(&task->running);
581 if (conn->task == task)
584 if (conn->ping_task == task)
585 conn->ping_task = NULL;
587 /* release get from queueing */
588 __iscsi_put_task(task);
592 * iscsi_complete_scsi_task - finish scsi task normally
593 * @task: iscsi task for scsi cmd
594 * @exp_cmdsn: expected cmd sn in cpu format
595 * @max_cmdsn: max cmd sn in cpu format
597 * This is used when drivers do not need or cannot perform
598 * lower level pdu processing.
600 * Called with session lock
602 void iscsi_complete_scsi_task(struct iscsi_task *task,
603 uint32_t exp_cmdsn, uint32_t max_cmdsn)
605 struct iscsi_conn *conn = task->conn;
607 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
609 conn->last_recv = jiffies;
610 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
611 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
613 EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
617 * session lock must be held and if not called for a task that is
618 * still pending or from the xmit thread, then xmit thread must
621 static void fail_scsi_task(struct iscsi_task *task, int err)
623 struct iscsi_conn *conn = task->conn;
624 struct scsi_cmnd *sc;
628 * if a command completes and we get a successful tmf response
629 * we will hit this because the scsi eh abort code does not take
636 if (task->state == ISCSI_TASK_PENDING) {
638 * cmd never made it to the xmit thread, so we should not count
639 * the cmd in the sequencing
641 conn->session->queued_cmdsn--;
642 /* it was never sent so just complete like normal */
643 state = ISCSI_TASK_COMPLETED;
644 } else if (err == DID_TRANSPORT_DISRUPTED)
645 state = ISCSI_TASK_ABRT_SESS_RECOV;
647 state = ISCSI_TASK_ABRT_TMF;
649 sc->result = err << 16;
650 if (!scsi_bidi_cmnd(sc))
651 scsi_set_resid(sc, scsi_bufflen(sc));
653 scsi_out(sc)->resid = scsi_out(sc)->length;
654 scsi_in(sc)->resid = scsi_in(sc)->length;
657 iscsi_complete_task(task, state);
660 static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
661 struct iscsi_task *task)
663 struct iscsi_session *session = conn->session;
664 struct iscsi_hdr *hdr = task->hdr;
665 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
666 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
668 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
671 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
672 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
674 * pre-format CmdSN for outgoing PDU.
676 nop->cmdsn = cpu_to_be32(session->cmdsn);
677 if (hdr->itt != RESERVED_ITT) {
679 * TODO: We always use immediate for normal session pdus.
680 * If we start to send tmfs or nops as non-immediate then
681 * we should start checking the cmdsn numbers for mgmt tasks.
683 * During discovery sessions iscsid sends TEXT as non immediate,
684 * but we always only send one PDU at a time.
686 if (conn->c_stage == ISCSI_CONN_STARTED &&
687 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
688 session->queued_cmdsn++;
693 if (session->tt->init_task && session->tt->init_task(task))
696 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
697 session->state = ISCSI_STATE_LOGGING_OUT;
699 task->state = ISCSI_TASK_RUNNING;
700 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
701 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
702 hdr->itt, task->data_count);
706 static struct iscsi_task *
707 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
708 char *data, uint32_t data_size)
710 struct iscsi_session *session = conn->session;
711 struct iscsi_host *ihost = shost_priv(session->host);
712 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
713 struct iscsi_task *task;
716 if (session->state == ISCSI_STATE_TERMINATE)
719 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
721 * Login and Text are sent serially, in
722 * request-followed-by-response sequence.
723 * Same task can be used. Same ITT must be used.
724 * Note that login_task is preallocated at conn_create().
726 if (conn->login_task->state != ISCSI_TASK_FREE) {
727 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
728 "progress. Cannot start new task.\n");
732 task = conn->login_task;
734 if (session->state != ISCSI_STATE_LOGGED_IN)
737 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
738 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
740 if (!kfifo_out(&session->cmdpool.queue,
741 (void*)&task, sizeof(void*)))
745 * released in complete pdu for task we expect a response for, and
746 * released by the lld when it has transmitted the task for
747 * pdus we do not expect a response for.
749 atomic_set(&task->refcount, 1);
752 INIT_LIST_HEAD(&task->running);
753 task->state = ISCSI_TASK_PENDING;
756 memcpy(task->data, data, data_size);
757 task->data_count = data_size;
759 task->data_count = 0;
761 if (conn->session->tt->alloc_pdu) {
762 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
763 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
764 "pdu for mgmt task.\n");
769 itt = task->hdr->itt;
770 task->hdr_len = sizeof(struct iscsi_hdr);
771 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
773 if (hdr->itt != RESERVED_ITT) {
774 if (session->tt->parse_pdu_itt)
775 task->hdr->itt = itt;
777 task->hdr->itt = build_itt(task->itt,
778 task->conn->session->age);
782 if (iscsi_prep_mgmt_task(conn, task))
785 if (session->tt->xmit_task(task))
788 list_add_tail(&task->running, &conn->mgmtqueue);
789 iscsi_conn_queue_work(conn);
795 __iscsi_put_task(task);
799 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
800 char *data, uint32_t data_size)
802 struct iscsi_conn *conn = cls_conn->dd_data;
803 struct iscsi_session *session = conn->session;
806 spin_lock_bh(&session->lock);
807 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
809 spin_unlock_bh(&session->lock);
812 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
815 * iscsi_cmd_rsp - SCSI Command Response processing
816 * @conn: iscsi connection
818 * @task: scsi command task
819 * @data: cmd data buffer
820 * @datalen: len of buffer
822 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
823 * then completes the command and task.
825 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
826 struct iscsi_task *task, char *data,
829 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
830 struct iscsi_session *session = conn->session;
831 struct scsi_cmnd *sc = task->sc;
833 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
834 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
836 sc->result = (DID_OK << 16) | rhdr->cmd_status;
838 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
839 sc->result = DID_ERROR << 16;
843 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
848 iscsi_conn_printk(KERN_ERR, conn,
849 "Got CHECK_CONDITION but invalid data "
850 "buffer size of %d\n", datalen);
851 sc->result = DID_BAD_TARGET << 16;
855 senselen = get_unaligned_be16(data);
856 if (datalen < senselen)
857 goto invalid_datalen;
859 memcpy(sc->sense_buffer, data + 2,
860 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
861 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
862 min_t(uint16_t, senselen,
863 SCSI_SENSE_BUFFERSIZE));
866 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
867 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
868 int res_count = be32_to_cpu(rhdr->bi_residual_count);
870 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
871 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
872 res_count <= scsi_in(sc)->length))
873 scsi_in(sc)->resid = res_count;
875 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
878 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
879 ISCSI_FLAG_CMD_OVERFLOW)) {
880 int res_count = be32_to_cpu(rhdr->residual_count);
883 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
884 res_count <= scsi_bufflen(sc)))
885 /* write side for bidi or uni-io set_resid */
886 scsi_set_resid(sc, res_count);
888 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
891 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
892 sc, sc->result, task->itt);
893 conn->scsirsp_pdus_cnt++;
894 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
898 * iscsi_data_in_rsp - SCSI Data-In Response processing
899 * @conn: iscsi connection
901 * @task: scsi command task
904 iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
905 struct iscsi_task *task)
907 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
908 struct scsi_cmnd *sc = task->sc;
910 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
913 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
914 sc->result = (DID_OK << 16) | rhdr->cmd_status;
915 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
916 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
917 ISCSI_FLAG_DATA_OVERFLOW)) {
918 int res_count = be32_to_cpu(rhdr->residual_count);
921 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
922 res_count <= scsi_in(sc)->length))
923 scsi_in(sc)->resid = res_count;
925 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
928 ISCSI_DBG_SESSION(conn->session, "data in with status done "
929 "[sc %p res %d itt 0x%x]\n",
930 sc, sc->result, task->itt);
931 conn->scsirsp_pdus_cnt++;
932 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
935 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
937 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
939 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
940 conn->tmfrsp_pdus_cnt++;
942 if (conn->tmf_state != TMF_QUEUED)
945 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
946 conn->tmf_state = TMF_SUCCESS;
947 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
948 conn->tmf_state = TMF_NOT_FOUND;
950 conn->tmf_state = TMF_FAILED;
951 wake_up(&conn->ehwait);
954 static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
956 struct iscsi_nopout hdr;
957 struct iscsi_task *task;
959 if (!rhdr && conn->ping_task)
962 memset(&hdr, 0, sizeof(struct iscsi_nopout));
963 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
964 hdr.flags = ISCSI_FLAG_CMD_FINAL;
967 memcpy(hdr.lun, rhdr->lun, 8);
969 hdr.itt = RESERVED_ITT;
971 hdr.ttt = RESERVED_ITT;
973 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
975 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
977 /* only track our nops */
978 conn->ping_task = task;
979 conn->last_ping = jiffies;
983 static int iscsi_nop_out_rsp(struct iscsi_task *task,
984 struct iscsi_nopin *nop, char *data, int datalen)
986 struct iscsi_conn *conn = task->conn;
989 if (conn->ping_task != task) {
991 * If this is not in response to one of our
992 * nops then it must be from userspace.
994 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
996 rc = ISCSI_ERR_CONN_FAILED;
998 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
999 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1003 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1004 char *data, int datalen)
1006 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
1007 struct iscsi_hdr rejected_pdu;
1010 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
1012 if (ntoh24(reject->dlength) > datalen ||
1013 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
1014 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
1015 "pdu. Invalid data length (pdu dlength "
1016 "%u, datalen %d\n", ntoh24(reject->dlength),
1018 return ISCSI_ERR_PROTO;
1020 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
1021 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
1023 switch (reject->reason) {
1024 case ISCSI_REASON_DATA_DIGEST_ERROR:
1025 iscsi_conn_printk(KERN_ERR, conn,
1026 "pdu (op 0x%x itt 0x%x) rejected "
1027 "due to DataDigest error.\n",
1028 rejected_pdu.itt, opcode);
1030 case ISCSI_REASON_IMM_CMD_REJECT:
1031 iscsi_conn_printk(KERN_ERR, conn,
1032 "pdu (op 0x%x itt 0x%x) rejected. Too many "
1033 "immediate commands.\n",
1034 rejected_pdu.itt, opcode);
1036 * We only send one TMF at a time so if the target could not
1037 * handle it, then it should get fixed (RFC mandates that
1038 * a target can handle one immediate TMF per conn).
1040 * For nops-outs, we could have sent more than one if
1041 * the target is sending us lots of nop-ins
1043 if (opcode != ISCSI_OP_NOOP_OUT)
1046 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG))
1048 * nop-out in response to target's nop-out rejected.
1051 iscsi_send_nopout(conn,
1052 (struct iscsi_nopin*)&rejected_pdu);
1054 struct iscsi_task *task;
1056 * Our nop as ping got dropped. We know the target
1057 * and transport are ok so just clean up
1059 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
1061 iscsi_conn_printk(KERN_ERR, conn,
1062 "Invalid pdu reject. Could "
1063 "not lookup rejected task.\n");
1064 rc = ISCSI_ERR_BAD_ITT;
1066 rc = iscsi_nop_out_rsp(task,
1067 (struct iscsi_nopin*)&rejected_pdu,
1072 iscsi_conn_printk(KERN_ERR, conn,
1073 "pdu (op 0x%x itt 0x%x) rejected. Reason "
1074 "code 0x%x\n", rejected_pdu.itt,
1075 rejected_pdu.opcode, reject->reason);
1082 * iscsi_itt_to_task - look up task by itt
1083 * @conn: iscsi connection
1086 * This should be used for mgmt tasks like login and nops, or if
1087 * the LDD's itt space does not include the session age.
1089 * The session lock must be held.
1091 struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
1093 struct iscsi_session *session = conn->session;
1096 if (itt == RESERVED_ITT)
1099 if (session->tt->parse_pdu_itt)
1100 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1103 if (i >= session->cmds_max)
1106 return session->cmds[i];
1108 EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
1111 * __iscsi_complete_pdu - complete pdu
1113 * @hdr: iscsi header
1114 * @data: data buffer
1115 * @datalen: len of data buffer
1117 * Completes pdu processing by freeing any resources allocated at
1118 * queuecommand or send generic. session lock must be held and verify
1119 * itt must have been called.
1121 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1122 char *data, int datalen)
1124 struct iscsi_session *session = conn->session;
1125 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
1126 struct iscsi_task *task;
1129 conn->last_recv = jiffies;
1130 rc = iscsi_verify_itt(conn, hdr->itt);
1134 if (hdr->itt != RESERVED_ITT)
1135 itt = get_itt(hdr->itt);
1139 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1140 opcode, conn->id, itt, datalen);
1143 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1146 case ISCSI_OP_NOOP_IN:
1148 rc = ISCSI_ERR_PROTO;
1152 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
1155 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
1157 case ISCSI_OP_REJECT:
1158 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1160 case ISCSI_OP_ASYNC_EVENT:
1161 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1162 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1163 rc = ISCSI_ERR_CONN_FAILED;
1166 rc = ISCSI_ERR_BAD_OPCODE;
1173 case ISCSI_OP_SCSI_CMD_RSP:
1174 case ISCSI_OP_SCSI_DATA_IN:
1175 task = iscsi_itt_to_ctask(conn, hdr->itt);
1177 return ISCSI_ERR_BAD_ITT;
1178 task->last_xfer = jiffies;
1182 * LLD handles R2Ts if they need to.
1185 case ISCSI_OP_LOGOUT_RSP:
1186 case ISCSI_OP_LOGIN_RSP:
1187 case ISCSI_OP_TEXT_RSP:
1188 case ISCSI_OP_SCSI_TMFUNC_RSP:
1189 case ISCSI_OP_NOOP_IN:
1190 task = iscsi_itt_to_task(conn, hdr->itt);
1192 return ISCSI_ERR_BAD_ITT;
1195 return ISCSI_ERR_BAD_OPCODE;
1199 case ISCSI_OP_SCSI_CMD_RSP:
1200 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
1202 case ISCSI_OP_SCSI_DATA_IN:
1203 iscsi_data_in_rsp(conn, hdr, task);
1205 case ISCSI_OP_LOGOUT_RSP:
1206 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1208 rc = ISCSI_ERR_PROTO;
1211 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1213 case ISCSI_OP_LOGIN_RSP:
1214 case ISCSI_OP_TEXT_RSP:
1215 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1217 * login related PDU's exp_statsn is handled in
1221 case ISCSI_OP_SCSI_TMFUNC_RSP:
1222 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1224 rc = ISCSI_ERR_PROTO;
1228 iscsi_tmf_rsp(conn, hdr);
1229 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1231 case ISCSI_OP_NOOP_IN:
1232 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1233 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1234 rc = ISCSI_ERR_PROTO;
1237 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1239 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1243 rc = ISCSI_ERR_BAD_OPCODE;
1250 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1251 rc = ISCSI_ERR_CONN_FAILED;
1252 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1255 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
1257 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1258 char *data, int datalen)
1262 spin_lock(&conn->session->lock);
1263 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1264 spin_unlock(&conn->session->lock);
1267 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1269 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
1271 struct iscsi_session *session = conn->session;
1274 if (itt == RESERVED_ITT)
1277 if (session->tt->parse_pdu_itt)
1278 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1281 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1284 if (age != session->age) {
1285 iscsi_conn_printk(KERN_ERR, conn,
1286 "received itt %x expected session age (%x)\n",
1287 (__force u32)itt, session->age);
1288 return ISCSI_ERR_BAD_ITT;
1291 if (i >= session->cmds_max) {
1292 iscsi_conn_printk(KERN_ERR, conn,
1293 "received invalid itt index %u (max cmds "
1294 "%u.\n", i, session->cmds_max);
1295 return ISCSI_ERR_BAD_ITT;
1299 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1302 * iscsi_itt_to_ctask - look up ctask by itt
1303 * @conn: iscsi connection
1306 * This should be used for cmd tasks.
1308 * The session lock must be held.
1310 struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
1312 struct iscsi_task *task;
1314 if (iscsi_verify_itt(conn, itt))
1317 task = iscsi_itt_to_task(conn, itt);
1318 if (!task || !task->sc)
1321 if (task->sc->SCp.phase != conn->session->age) {
1322 iscsi_session_printk(KERN_ERR, conn->session,
1323 "task's session age %d, expected %d\n",
1324 task->sc->SCp.phase, conn->session->age);
1330 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1332 void iscsi_session_failure(struct iscsi_session *session,
1335 struct iscsi_conn *conn;
1337 unsigned long flags;
1339 spin_lock_irqsave(&session->lock, flags);
1340 conn = session->leadconn;
1341 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1342 spin_unlock_irqrestore(&session->lock, flags);
1346 dev = get_device(&conn->cls_conn->dev);
1347 spin_unlock_irqrestore(&session->lock, flags);
1351 * if the host is being removed bypass the connection
1352 * recovery initialization because we are going to kill
1355 if (err == ISCSI_ERR_INVALID_HOST)
1356 iscsi_conn_error_event(conn->cls_conn, err);
1358 iscsi_conn_failure(conn, err);
1361 EXPORT_SYMBOL_GPL(iscsi_session_failure);
1363 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1365 struct iscsi_session *session = conn->session;
1366 unsigned long flags;
1368 spin_lock_irqsave(&session->lock, flags);
1369 if (session->state == ISCSI_STATE_FAILED) {
1370 spin_unlock_irqrestore(&session->lock, flags);
1374 if (conn->stop_stage == 0)
1375 session->state = ISCSI_STATE_FAILED;
1376 spin_unlock_irqrestore(&session->lock, flags);
1378 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1379 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1380 iscsi_conn_error_event(conn->cls_conn, err);
1382 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1384 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1386 struct iscsi_session *session = conn->session;
1389 * Check for iSCSI window and take care of CmdSN wrap-around
1391 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1392 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1393 "%u MaxCmdSN %u CmdSN %u/%u\n",
1394 session->exp_cmdsn, session->max_cmdsn,
1395 session->cmdsn, session->queued_cmdsn);
1401 static int iscsi_xmit_task(struct iscsi_conn *conn)
1403 struct iscsi_task *task = conn->task;
1406 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1409 __iscsi_get_task(task);
1410 spin_unlock_bh(&conn->session->lock);
1411 rc = conn->session->tt->xmit_task(task);
1412 spin_lock_bh(&conn->session->lock);
1414 /* done with this task */
1415 task->last_xfer = jiffies;
1418 __iscsi_put_task(task);
1423 * iscsi_requeue_task - requeue task to run from session workqueue
1424 * @task: task to requeue
1426 * LLDs that need to run a task from the session workqueue should call
1427 * this. The session lock must be held. This should only be called
1428 * by software drivers.
1430 void iscsi_requeue_task(struct iscsi_task *task)
1432 struct iscsi_conn *conn = task->conn;
1435 * this may be on the requeue list already if the xmit_task callout
1436 * is handling the r2ts while we are adding new ones
1438 if (list_empty(&task->running))
1439 list_add_tail(&task->running, &conn->requeue);
1440 iscsi_conn_queue_work(conn);
1442 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1445 * iscsi_data_xmit - xmit any command into the scheduled connection
1446 * @conn: iscsi connection
1449 * The function can return -EAGAIN in which case the caller must
1450 * re-schedule it again later or recover. '0' return code means
1453 static int iscsi_data_xmit(struct iscsi_conn *conn)
1455 struct iscsi_task *task;
1458 spin_lock_bh(&conn->session->lock);
1459 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1460 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1461 spin_unlock_bh(&conn->session->lock);
1466 rc = iscsi_xmit_task(conn);
1472 * process mgmt pdus like nops before commands since we should
1473 * only have one nop-out as a ping from us and targets should not
1474 * overflow us with nop-ins
1477 while (!list_empty(&conn->mgmtqueue)) {
1478 conn->task = list_entry(conn->mgmtqueue.next,
1479 struct iscsi_task, running);
1480 list_del_init(&conn->task->running);
1481 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1482 __iscsi_put_task(conn->task);
1486 rc = iscsi_xmit_task(conn);
1491 /* process pending command queue */
1492 while (!list_empty(&conn->cmdqueue)) {
1493 conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1495 list_del_init(&conn->task->running);
1496 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1497 fail_scsi_task(conn->task, DID_IMM_RETRY);
1500 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1502 if (rc == -ENOMEM || rc == -EACCES) {
1503 list_add_tail(&conn->task->running,
1508 fail_scsi_task(conn->task, DID_ABORT);
1511 rc = iscsi_xmit_task(conn);
1515 * we could continuously get new task requests so
1516 * we need to check the mgmt queue for nops that need to
1517 * be sent to aviod starvation
1519 if (!list_empty(&conn->mgmtqueue))
1523 while (!list_empty(&conn->requeue)) {
1525 * we always do fastlogout - conn stop code will clean up.
1527 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1530 task = list_entry(conn->requeue.next, struct iscsi_task,
1532 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1536 list_del_init(&conn->task->running);
1537 conn->task->state = ISCSI_TASK_RUNNING;
1538 rc = iscsi_xmit_task(conn);
1541 if (!list_empty(&conn->mgmtqueue))
1544 spin_unlock_bh(&conn->session->lock);
1548 spin_unlock_bh(&conn->session->lock);
1552 static void iscsi_xmitworker(struct work_struct *work)
1554 struct iscsi_conn *conn =
1555 container_of(work, struct iscsi_conn, xmitwork);
1558 * serialize Xmit worker on a per-connection basis.
1561 rc = iscsi_data_xmit(conn);
1562 } while (rc >= 0 || rc == -EAGAIN);
1565 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1566 struct scsi_cmnd *sc)
1568 struct iscsi_task *task;
1570 if (!kfifo_out(&conn->session->cmdpool.queue,
1571 (void *) &task, sizeof(void *)))
1574 sc->SCp.phase = conn->session->age;
1575 sc->SCp.ptr = (char *) task;
1577 atomic_set(&task->refcount, 1);
1578 task->state = ISCSI_TASK_PENDING;
1581 task->have_checked_conn = false;
1582 task->last_timeout = jiffies;
1583 task->last_xfer = jiffies;
1584 INIT_LIST_HEAD(&task->running);
1589 FAILURE_BAD_HOST = 1,
1590 FAILURE_SESSION_FAILED,
1591 FAILURE_SESSION_FREED,
1592 FAILURE_WINDOW_CLOSED,
1594 FAILURE_SESSION_TERMINATE,
1595 FAILURE_SESSION_IN_RECOVERY,
1596 FAILURE_SESSION_RECOVERY_TIMEOUT,
1597 FAILURE_SESSION_LOGGING_OUT,
1598 FAILURE_SESSION_NOT_READY,
1601 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1603 struct iscsi_cls_session *cls_session;
1604 struct Scsi_Host *host;
1605 struct iscsi_host *ihost;
1607 struct iscsi_session *session;
1608 struct iscsi_conn *conn;
1609 struct iscsi_task *task = NULL;
1611 sc->scsi_done = done;
1615 host = sc->device->host;
1616 ihost = shost_priv(host);
1617 spin_unlock(host->host_lock);
1619 cls_session = starget_to_session(scsi_target(sc->device));
1620 session = cls_session->dd_data;
1621 spin_lock(&session->lock);
1623 reason = iscsi_session_chkready(cls_session);
1625 sc->result = reason;
1629 if (session->state != ISCSI_STATE_LOGGED_IN) {
1631 * to handle the race between when we set the recovery state
1632 * and block the session we requeue here (commands could
1633 * be entering our queuecommand while a block is starting
1634 * up because the block code is not locked)
1636 switch (session->state) {
1637 case ISCSI_STATE_FAILED:
1638 case ISCSI_STATE_IN_RECOVERY:
1639 reason = FAILURE_SESSION_IN_RECOVERY;
1640 sc->result = DID_IMM_RETRY << 16;
1642 case ISCSI_STATE_LOGGING_OUT:
1643 reason = FAILURE_SESSION_LOGGING_OUT;
1644 sc->result = DID_IMM_RETRY << 16;
1646 case ISCSI_STATE_RECOVERY_FAILED:
1647 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1648 sc->result = DID_TRANSPORT_FAILFAST << 16;
1650 case ISCSI_STATE_TERMINATE:
1651 reason = FAILURE_SESSION_TERMINATE;
1652 sc->result = DID_NO_CONNECT << 16;
1655 reason = FAILURE_SESSION_FREED;
1656 sc->result = DID_NO_CONNECT << 16;
1661 conn = session->leadconn;
1663 reason = FAILURE_SESSION_FREED;
1664 sc->result = DID_NO_CONNECT << 16;
1668 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1669 reason = FAILURE_SESSION_IN_RECOVERY;
1670 sc->result = DID_REQUEUE;
1674 if (iscsi_check_cmdsn_window_closed(conn)) {
1675 reason = FAILURE_WINDOW_CLOSED;
1679 task = iscsi_alloc_task(conn, sc);
1681 reason = FAILURE_OOM;
1685 if (!ihost->workq) {
1686 reason = iscsi_prep_scsi_cmd_pdu(task);
1688 if (reason == -ENOMEM || reason == -EACCES) {
1689 reason = FAILURE_OOM;
1692 sc->result = DID_ABORT << 16;
1696 if (session->tt->xmit_task(task)) {
1698 reason = FAILURE_SESSION_NOT_READY;
1702 list_add_tail(&task->running, &conn->cmdqueue);
1703 iscsi_conn_queue_work(conn);
1706 session->queued_cmdsn++;
1707 spin_unlock(&session->lock);
1708 spin_lock(host->host_lock);
1712 sc->scsi_done = NULL;
1713 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1715 spin_unlock(&session->lock);
1716 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1717 sc->cmnd[0], reason);
1718 spin_lock(host->host_lock);
1719 return SCSI_MLQUEUE_TARGET_BUSY;
1722 sc->scsi_done = NULL;
1723 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1725 spin_unlock(&session->lock);
1726 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1727 sc->cmnd[0], reason);
1728 if (!scsi_bidi_cmnd(sc))
1729 scsi_set_resid(sc, scsi_bufflen(sc));
1731 scsi_out(sc)->resid = scsi_out(sc)->length;
1732 scsi_in(sc)->resid = scsi_in(sc)->length;
1735 spin_lock(host->host_lock);
1738 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1740 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
1743 case SCSI_QDEPTH_DEFAULT:
1744 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1746 case SCSI_QDEPTH_QFULL:
1747 scsi_track_queue_full(sdev, depth);
1749 case SCSI_QDEPTH_RAMP_UP:
1750 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1755 return sdev->queue_depth;
1757 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1759 int iscsi_target_alloc(struct scsi_target *starget)
1761 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1762 struct iscsi_session *session = cls_session->dd_data;
1764 starget->can_queue = session->scsi_cmds_max;
1767 EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1769 static void iscsi_tmf_timedout(unsigned long data)
1771 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1772 struct iscsi_session *session = conn->session;
1774 spin_lock(&session->lock);
1775 if (conn->tmf_state == TMF_QUEUED) {
1776 conn->tmf_state = TMF_TIMEDOUT;
1777 ISCSI_DBG_EH(session, "tmf timedout\n");
1778 /* unblock eh_abort() */
1779 wake_up(&conn->ehwait);
1781 spin_unlock(&session->lock);
1784 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1785 struct iscsi_tm *hdr, int age,
1788 struct iscsi_session *session = conn->session;
1789 struct iscsi_task *task;
1791 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1794 spin_unlock_bh(&session->lock);
1795 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1796 spin_lock_bh(&session->lock);
1797 ISCSI_DBG_EH(session, "tmf exec failure\n");
1800 conn->tmfcmd_pdus_cnt++;
1801 conn->tmf_timer.expires = timeout * HZ + jiffies;
1802 conn->tmf_timer.function = iscsi_tmf_timedout;
1803 conn->tmf_timer.data = (unsigned long)conn;
1804 add_timer(&conn->tmf_timer);
1805 ISCSI_DBG_EH(session, "tmf set timeout\n");
1807 spin_unlock_bh(&session->lock);
1808 mutex_unlock(&session->eh_mutex);
1811 * block eh thread until:
1815 * 3) session is terminated or restarted or userspace has
1816 * given up on recovery
1818 wait_event_interruptible(conn->ehwait, age != session->age ||
1819 session->state != ISCSI_STATE_LOGGED_IN ||
1820 conn->tmf_state != TMF_QUEUED);
1821 if (signal_pending(current))
1822 flush_signals(current);
1823 del_timer_sync(&conn->tmf_timer);
1825 mutex_lock(&session->eh_mutex);
1826 spin_lock_bh(&session->lock);
1827 /* if the session drops it will clean up the task */
1828 if (age != session->age ||
1829 session->state != ISCSI_STATE_LOGGED_IN)
1835 * Fail commands. session lock held and recv side suspended and xmit
1838 static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1841 struct iscsi_task *task;
1844 for (i = 0; i < conn->session->cmds_max; i++) {
1845 task = conn->session->cmds[i];
1846 if (!task->sc || task->state == ISCSI_TASK_FREE)
1849 if (lun != -1 && lun != task->sc->device->lun)
1852 ISCSI_DBG_SESSION(conn->session,
1853 "failing sc %p itt 0x%x state %d\n",
1854 task->sc, task->itt, task->state);
1855 fail_scsi_task(task, error);
1860 * iscsi_suspend_queue - suspend iscsi_queuecommand
1861 * @conn: iscsi conn to stop queueing IO on
1863 * This grabs the session lock to make sure no one is in
1864 * xmit_task/queuecommand, and then sets suspend to prevent
1865 * new commands from being queued. This only needs to be called
1866 * by offload drivers that need to sync a path like ep disconnect
1867 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1868 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1870 void iscsi_suspend_queue(struct iscsi_conn *conn)
1872 spin_lock_bh(&conn->session->lock);
1873 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1874 spin_unlock_bh(&conn->session->lock);
1876 EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1879 * iscsi_suspend_tx - suspend iscsi_data_xmit
1880 * @conn: iscsi conn tp stop processing IO on.
1882 * This function sets the suspend bit to prevent iscsi_data_xmit
1883 * from sending new IO, and if work is queued on the xmit thread
1884 * it will wait for it to be completed.
1886 void iscsi_suspend_tx(struct iscsi_conn *conn)
1888 struct Scsi_Host *shost = conn->session->host;
1889 struct iscsi_host *ihost = shost_priv(shost);
1891 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1893 flush_workqueue(ihost->workq);
1895 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1897 static void iscsi_start_tx(struct iscsi_conn *conn)
1899 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1900 iscsi_conn_queue_work(conn);
1904 * We want to make sure a ping is in flight. It has timed out.
1905 * And we are not busy processing a pdu that is making
1906 * progress but got started before the ping and is taking a while
1907 * to complete so the ping is just stuck behind it in a queue.
1909 static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1911 if (conn->ping_task &&
1912 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1913 (conn->ping_timeout * HZ), jiffies))
1919 static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1921 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
1922 struct iscsi_task *task = NULL;
1923 struct iscsi_cls_session *cls_session;
1924 struct iscsi_session *session;
1925 struct iscsi_conn *conn;
1927 cls_session = starget_to_session(scsi_target(sc->device));
1928 session = cls_session->dd_data;
1930 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1932 spin_lock(&session->lock);
1933 if (session->state != ISCSI_STATE_LOGGED_IN) {
1935 * We are probably in the middle of iscsi recovery so let
1936 * that complete and handle the error.
1938 rc = BLK_EH_RESET_TIMER;
1942 conn = session->leadconn;
1944 /* In the middle of shuting down */
1945 rc = BLK_EH_RESET_TIMER;
1949 task = (struct iscsi_task *)sc->SCp.ptr;
1953 * If we have sent (at least queued to the network layer) a pdu or
1954 * recvd one for the task since the last timeout ask for
1955 * more time. If on the next timeout we have not made progress
1956 * we can check if it is the task or connection when we send the
1959 if (time_after_eq(task->last_xfer, task->last_timeout)) {
1960 ISCSI_DBG_EH(session, "Command making progress. Asking "
1961 "scsi-ml for more time to complete. "
1962 "Last data recv at %lu. Last timeout was at "
1963 "%lu\n.", task->last_xfer, task->last_timeout);
1964 task->have_checked_conn = false;
1965 rc = BLK_EH_RESET_TIMER;
1969 if (!conn->recv_timeout && !conn->ping_timeout)
1972 * if the ping timedout then we are in the middle of cleaning up
1973 * and can let the iscsi eh handle it
1975 if (iscsi_has_ping_timed_out(conn)) {
1976 rc = BLK_EH_RESET_TIMER;
1980 /* Assumes nop timeout is shorter than scsi cmd timeout */
1981 if (task->have_checked_conn)
1985 * Checking the transport already or nop from a cmd timeout still
1988 if (conn->ping_task) {
1989 task->have_checked_conn = true;
1990 rc = BLK_EH_RESET_TIMER;
1994 /* Make sure there is a transport check done */
1995 iscsi_send_nopout(conn, NULL);
1996 task->have_checked_conn = true;
1997 rc = BLK_EH_RESET_TIMER;
2001 task->last_timeout = jiffies;
2002 spin_unlock(&session->lock);
2003 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2004 "timer reset" : "nh");
2008 static void iscsi_check_transport_timeouts(unsigned long data)
2010 struct iscsi_conn *conn = (struct iscsi_conn *)data;
2011 struct iscsi_session *session = conn->session;
2012 unsigned long recv_timeout, next_timeout = 0, last_recv;
2014 spin_lock(&session->lock);
2015 if (session->state != ISCSI_STATE_LOGGED_IN)
2018 recv_timeout = conn->recv_timeout;
2023 last_recv = conn->last_recv;
2025 if (iscsi_has_ping_timed_out(conn)) {
2026 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
2027 "expired, recv timeout %d, last rx %lu, "
2028 "last ping %lu, now %lu\n",
2029 conn->ping_timeout, conn->recv_timeout,
2030 last_recv, conn->last_ping, jiffies);
2031 spin_unlock(&session->lock);
2032 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2036 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2037 /* send a ping to try to provoke some traffic */
2038 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
2039 iscsi_send_nopout(conn, NULL);
2040 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2042 next_timeout = last_recv + recv_timeout;
2044 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2045 mod_timer(&conn->transport_timer, next_timeout);
2047 spin_unlock(&session->lock);
2050 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2051 struct iscsi_tm *hdr)
2053 memset(hdr, 0, sizeof(*hdr));
2054 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2055 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2056 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2057 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
2058 hdr->rtt = task->hdr_itt;
2059 hdr->refcmdsn = task->cmdsn;
2062 int iscsi_eh_abort(struct scsi_cmnd *sc)
2064 struct iscsi_cls_session *cls_session;
2065 struct iscsi_session *session;
2066 struct iscsi_conn *conn;
2067 struct iscsi_task *task;
2068 struct iscsi_tm *hdr;
2071 cls_session = starget_to_session(scsi_target(sc->device));
2072 session = cls_session->dd_data;
2074 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
2076 mutex_lock(&session->eh_mutex);
2077 spin_lock_bh(&session->lock);
2079 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2083 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2085 spin_unlock_bh(&session->lock);
2086 mutex_unlock(&session->eh_mutex);
2091 * If we are not logged in or we have started a new session
2092 * then let the host reset code handle this
2094 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2095 sc->SCp.phase != session->age) {
2096 spin_unlock_bh(&session->lock);
2097 mutex_unlock(&session->eh_mutex);
2098 ISCSI_DBG_EH(session, "failing abort due to dropped "
2103 conn = session->leadconn;
2104 conn->eh_abort_cnt++;
2107 task = (struct iscsi_task *)sc->SCp.ptr;
2108 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2111 /* task completed before time out */
2113 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
2117 if (task->state == ISCSI_TASK_PENDING) {
2118 fail_scsi_task(task, DID_ABORT);
2122 /* only have one tmf outstanding at a time */
2123 if (conn->tmf_state != TMF_INITIAL)
2125 conn->tmf_state = TMF_QUEUED;
2128 iscsi_prep_abort_task_pdu(task, hdr);
2130 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
2135 switch (conn->tmf_state) {
2137 spin_unlock_bh(&session->lock);
2139 * stop tx side incase the target had sent a abort rsp but
2140 * the initiator was still writing out data.
2142 iscsi_suspend_tx(conn);
2144 * we do not stop the recv side because targets have been
2145 * good and have never sent us a successful tmf response
2146 * then sent more data for the cmd.
2148 spin_lock_bh(&session->lock);
2149 fail_scsi_task(task, DID_ABORT);
2150 conn->tmf_state = TMF_INITIAL;
2151 memset(hdr, 0, sizeof(*hdr));
2152 spin_unlock_bh(&session->lock);
2153 iscsi_start_tx(conn);
2154 goto success_unlocked;
2156 spin_unlock_bh(&session->lock);
2157 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2158 goto failed_unlocked;
2161 conn->tmf_state = TMF_INITIAL;
2162 memset(hdr, 0, sizeof(*hdr));
2163 /* task completed before tmf abort response */
2164 ISCSI_DBG_EH(session, "sc completed while abort in "
2170 conn->tmf_state = TMF_INITIAL;
2175 spin_unlock_bh(&session->lock);
2177 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2179 mutex_unlock(&session->eh_mutex);
2183 spin_unlock_bh(&session->lock);
2185 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2186 task ? task->itt : 0);
2187 mutex_unlock(&session->eh_mutex);
2190 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2192 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2194 memset(hdr, 0, sizeof(*hdr));
2195 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2196 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2197 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2198 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
2199 hdr->rtt = RESERVED_ITT;
2202 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2204 struct iscsi_cls_session *cls_session;
2205 struct iscsi_session *session;
2206 struct iscsi_conn *conn;
2207 struct iscsi_tm *hdr;
2210 cls_session = starget_to_session(scsi_target(sc->device));
2211 session = cls_session->dd_data;
2213 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
2215 mutex_lock(&session->eh_mutex);
2216 spin_lock_bh(&session->lock);
2218 * Just check if we are not logged in. We cannot check for
2219 * the phase because the reset could come from a ioctl.
2221 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2223 conn = session->leadconn;
2225 /* only have one tmf outstanding at a time */
2226 if (conn->tmf_state != TMF_INITIAL)
2228 conn->tmf_state = TMF_QUEUED;
2231 iscsi_prep_lun_reset_pdu(sc, hdr);
2233 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2234 session->lu_reset_timeout)) {
2239 switch (conn->tmf_state) {
2243 spin_unlock_bh(&session->lock);
2244 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2247 conn->tmf_state = TMF_INITIAL;
2252 spin_unlock_bh(&session->lock);
2254 iscsi_suspend_tx(conn);
2256 spin_lock_bh(&session->lock);
2257 memset(hdr, 0, sizeof(*hdr));
2258 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2259 conn->tmf_state = TMF_INITIAL;
2260 spin_unlock_bh(&session->lock);
2262 iscsi_start_tx(conn);
2266 spin_unlock_bh(&session->lock);
2268 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2269 rc == SUCCESS ? "SUCCESS" : "FAILED");
2270 mutex_unlock(&session->eh_mutex);
2273 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2275 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2277 struct iscsi_session *session = cls_session->dd_data;
2279 spin_lock_bh(&session->lock);
2280 if (session->state != ISCSI_STATE_LOGGED_IN) {
2281 session->state = ISCSI_STATE_RECOVERY_FAILED;
2282 if (session->leadconn)
2283 wake_up(&session->leadconn->ehwait);
2285 spin_unlock_bh(&session->lock);
2287 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
2290 * iscsi_eh_session_reset - drop session and attempt relogin
2293 * This function will wait for a relogin, session termination from
2294 * userspace, or a recovery/replacement timeout.
2296 static int iscsi_eh_session_reset(struct scsi_cmnd *sc)
2298 struct iscsi_cls_session *cls_session;
2299 struct iscsi_session *session;
2300 struct iscsi_conn *conn;
2302 cls_session = starget_to_session(scsi_target(sc->device));
2303 session = cls_session->dd_data;
2304 conn = session->leadconn;
2306 mutex_lock(&session->eh_mutex);
2307 spin_lock_bh(&session->lock);
2308 if (session->state == ISCSI_STATE_TERMINATE) {
2310 ISCSI_DBG_EH(session,
2311 "failing session reset: Could not log back into "
2312 "%s, %s [age %d]\n", session->targetname,
2313 conn->persistent_address, session->age);
2314 spin_unlock_bh(&session->lock);
2315 mutex_unlock(&session->eh_mutex);
2319 spin_unlock_bh(&session->lock);
2320 mutex_unlock(&session->eh_mutex);
2322 * we drop the lock here but the leadconn cannot be destoyed while
2323 * we are in the scsi eh
2325 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2327 ISCSI_DBG_EH(session, "wait for relogin\n");
2328 wait_event_interruptible(conn->ehwait,
2329 session->state == ISCSI_STATE_TERMINATE ||
2330 session->state == ISCSI_STATE_LOGGED_IN ||
2331 session->state == ISCSI_STATE_RECOVERY_FAILED);
2332 if (signal_pending(current))
2333 flush_signals(current);
2335 mutex_lock(&session->eh_mutex);
2336 spin_lock_bh(&session->lock);
2337 if (session->state == ISCSI_STATE_LOGGED_IN) {
2338 ISCSI_DBG_EH(session,
2339 "session reset succeeded for %s,%s\n",
2340 session->targetname, conn->persistent_address);
2343 spin_unlock_bh(&session->lock);
2344 mutex_unlock(&session->eh_mutex);
2348 static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2350 memset(hdr, 0, sizeof(*hdr));
2351 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2352 hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2353 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2354 hdr->rtt = RESERVED_ITT;
2358 * iscsi_eh_target_reset - reset target
2361 * This will attempt to send a warm target reset. If that fails
2362 * then we will drop the session and attempt ERL0 recovery.
2364 int iscsi_eh_target_reset(struct scsi_cmnd *sc)
2366 struct iscsi_cls_session *cls_session;
2367 struct iscsi_session *session;
2368 struct iscsi_conn *conn;
2369 struct iscsi_tm *hdr;
2372 cls_session = starget_to_session(scsi_target(sc->device));
2373 session = cls_session->dd_data;
2375 ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
2376 session->targetname);
2378 mutex_lock(&session->eh_mutex);
2379 spin_lock_bh(&session->lock);
2381 * Just check if we are not logged in. We cannot check for
2382 * the phase because the reset could come from a ioctl.
2384 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2386 conn = session->leadconn;
2388 /* only have one tmf outstanding at a time */
2389 if (conn->tmf_state != TMF_INITIAL)
2391 conn->tmf_state = TMF_QUEUED;
2394 iscsi_prep_tgt_reset_pdu(sc, hdr);
2396 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2397 session->tgt_reset_timeout)) {
2402 switch (conn->tmf_state) {
2406 spin_unlock_bh(&session->lock);
2407 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2410 conn->tmf_state = TMF_INITIAL;
2415 spin_unlock_bh(&session->lock);
2417 iscsi_suspend_tx(conn);
2419 spin_lock_bh(&session->lock);
2420 memset(hdr, 0, sizeof(*hdr));
2421 fail_scsi_tasks(conn, -1, DID_ERROR);
2422 conn->tmf_state = TMF_INITIAL;
2423 spin_unlock_bh(&session->lock);
2425 iscsi_start_tx(conn);
2429 spin_unlock_bh(&session->lock);
2431 ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
2432 rc == SUCCESS ? "SUCCESS" : "FAILED");
2433 mutex_unlock(&session->eh_mutex);
2436 rc = iscsi_eh_session_reset(sc);
2439 EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
2442 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2443 * should be accessed via kfifo_{get,put} on q->queue.
2444 * Optionally, the caller can obtain the array of object pointers
2445 * by passing in a non-NULL @items pointer
2448 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
2450 int i, num_arrays = 1;
2452 memset(q, 0, sizeof(*q));
2456 /* If the user passed an items pointer, he wants a copy of
2460 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2461 if (q->pool == NULL)
2464 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
2466 for (i = 0; i < max; i++) {
2467 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2468 if (q->pool[i] == NULL) {
2472 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
2476 *items = q->pool + max;
2477 memcpy(*items, q->pool, max * sizeof(void *));
2486 EXPORT_SYMBOL_GPL(iscsi_pool_init);
2488 void iscsi_pool_free(struct iscsi_pool *q)
2492 for (i = 0; i < q->max; i++)
2496 EXPORT_SYMBOL_GPL(iscsi_pool_free);
2499 * iscsi_host_add - add host to system
2501 * @pdev: parent device
2503 * This should be called by partial offload and software iscsi drivers
2504 * to add a host to the system.
2506 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2508 if (!shost->can_queue)
2509 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2511 if (!shost->cmd_per_lun)
2512 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2514 if (!shost->transportt->eh_timed_out)
2515 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
2516 return scsi_add_host(shost, pdev);
2518 EXPORT_SYMBOL_GPL(iscsi_host_add);
2521 * iscsi_host_alloc - allocate a host and driver data
2522 * @sht: scsi host template
2523 * @dd_data_size: driver host data size
2524 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2526 * This should be called by partial offload and software iscsi drivers.
2527 * To access the driver specific memory use the iscsi_host_priv() macro.
2529 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2530 int dd_data_size, bool xmit_can_sleep)
2532 struct Scsi_Host *shost;
2533 struct iscsi_host *ihost;
2535 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2538 ihost = shost_priv(shost);
2540 if (xmit_can_sleep) {
2541 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2542 "iscsi_q_%d", shost->host_no);
2543 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2548 spin_lock_init(&ihost->lock);
2549 ihost->state = ISCSI_HOST_SETUP;
2550 ihost->num_sessions = 0;
2551 init_waitqueue_head(&ihost->session_removal_wq);
2555 scsi_host_put(shost);
2558 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2560 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2562 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2566 * iscsi_host_remove - remove host and sessions
2569 * If there are any sessions left, this will initiate the removal and wait
2570 * for the completion.
2572 void iscsi_host_remove(struct Scsi_Host *shost)
2574 struct iscsi_host *ihost = shost_priv(shost);
2575 unsigned long flags;
2577 spin_lock_irqsave(&ihost->lock, flags);
2578 ihost->state = ISCSI_HOST_REMOVED;
2579 spin_unlock_irqrestore(&ihost->lock, flags);
2581 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2582 wait_event_interruptible(ihost->session_removal_wq,
2583 ihost->num_sessions == 0);
2584 if (signal_pending(current))
2585 flush_signals(current);
2587 scsi_remove_host(shost);
2589 destroy_workqueue(ihost->workq);
2591 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2593 void iscsi_host_free(struct Scsi_Host *shost)
2595 struct iscsi_host *ihost = shost_priv(shost);
2597 kfree(ihost->netdev);
2598 kfree(ihost->hwaddress);
2599 kfree(ihost->initiatorname);
2600 scsi_host_put(shost);
2602 EXPORT_SYMBOL_GPL(iscsi_host_free);
2604 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2606 struct iscsi_host *ihost = shost_priv(shost);
2607 unsigned long flags;
2609 shost = scsi_host_get(shost);
2611 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2612 "of session teardown event because host already "
2617 spin_lock_irqsave(&ihost->lock, flags);
2618 ihost->num_sessions--;
2619 if (ihost->num_sessions == 0)
2620 wake_up(&ihost->session_removal_wq);
2621 spin_unlock_irqrestore(&ihost->lock, flags);
2622 scsi_host_put(shost);
2626 * iscsi_session_setup - create iscsi cls session and host and session
2627 * @iscsit: iscsi transport template
2629 * @cmds_max: session can queue
2630 * @cmd_task_size: LLD task private data size
2631 * @initial_cmdsn: initial CmdSN
2633 * This can be used by software iscsi_transports that allocate
2634 * a session per scsi host.
2636 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2637 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2638 * for nop handling and login/logout requests.
2640 struct iscsi_cls_session *
2641 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2642 uint16_t cmds_max, int dd_size, int cmd_task_size,
2643 uint32_t initial_cmdsn, unsigned int id)
2645 struct iscsi_host *ihost = shost_priv(shost);
2646 struct iscsi_session *session;
2647 struct iscsi_cls_session *cls_session;
2648 int cmd_i, scsi_cmds, total_cmds = cmds_max;
2649 unsigned long flags;
2651 spin_lock_irqsave(&ihost->lock, flags);
2652 if (ihost->state == ISCSI_HOST_REMOVED) {
2653 spin_unlock_irqrestore(&ihost->lock, flags);
2656 ihost->num_sessions++;
2657 spin_unlock_irqrestore(&ihost->lock, flags);
2660 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2662 * The iscsi layer needs some tasks for nop handling and tmfs,
2663 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2664 * + 1 command for scsi IO.
2666 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2667 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2668 "must be a power of two that is at least %d.\n",
2669 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2670 goto dec_session_count;
2673 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2674 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2675 "must be a power of 2 less than or equal to %d.\n",
2676 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2677 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2680 if (!is_power_of_2(total_cmds)) {
2681 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2682 "must be a power of 2.\n", total_cmds);
2683 total_cmds = rounddown_pow_of_two(total_cmds);
2684 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2686 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2689 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2691 cls_session = iscsi_alloc_session(shost, iscsit,
2692 sizeof(struct iscsi_session) +
2695 goto dec_session_count;
2696 session = cls_session->dd_data;
2697 session->cls_session = cls_session;
2698 session->host = shost;
2699 session->state = ISCSI_STATE_FREE;
2700 session->fast_abort = 1;
2701 session->tgt_reset_timeout = 30;
2702 session->lu_reset_timeout = 15;
2703 session->abort_timeout = 10;
2704 session->scsi_cmds_max = scsi_cmds;
2705 session->cmds_max = total_cmds;
2706 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2707 session->exp_cmdsn = initial_cmdsn + 1;
2708 session->max_cmdsn = initial_cmdsn + 1;
2709 session->max_r2t = 1;
2710 session->tt = iscsit;
2711 session->dd_data = cls_session->dd_data + sizeof(*session);
2712 mutex_init(&session->eh_mutex);
2713 spin_lock_init(&session->lock);
2715 /* initialize SCSI PDU commands pool */
2716 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2717 (void***)&session->cmds,
2718 cmd_task_size + sizeof(struct iscsi_task)))
2719 goto cmdpool_alloc_fail;
2721 /* pre-format cmds pool with ITT */
2722 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2723 struct iscsi_task *task = session->cmds[cmd_i];
2726 task->dd_data = &task[1];
2728 task->state = ISCSI_TASK_FREE;
2729 INIT_LIST_HEAD(&task->running);
2732 if (!try_module_get(iscsit->owner))
2733 goto module_get_fail;
2735 if (iscsi_add_session(cls_session, id))
2736 goto cls_session_fail;
2741 module_put(iscsit->owner);
2743 iscsi_pool_free(&session->cmdpool);
2745 iscsi_free_session(cls_session);
2747 iscsi_host_dec_session_cnt(shost);
2750 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2753 * iscsi_session_teardown - destroy session, host, and cls_session
2754 * @cls_session: iscsi session
2756 * The driver must have called iscsi_remove_session before
2759 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2761 struct iscsi_session *session = cls_session->dd_data;
2762 struct module *owner = cls_session->transport->owner;
2763 struct Scsi_Host *shost = session->host;
2765 iscsi_pool_free(&session->cmdpool);
2767 kfree(session->password);
2768 kfree(session->password_in);
2769 kfree(session->username);
2770 kfree(session->username_in);
2771 kfree(session->targetname);
2772 kfree(session->initiatorname);
2773 kfree(session->ifacename);
2775 iscsi_destroy_session(cls_session);
2776 iscsi_host_dec_session_cnt(shost);
2779 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2782 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2783 * @cls_session: iscsi_cls_session
2784 * @dd_size: private driver data size
2787 struct iscsi_cls_conn *
2788 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2791 struct iscsi_session *session = cls_session->dd_data;
2792 struct iscsi_conn *conn;
2793 struct iscsi_cls_conn *cls_conn;
2796 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2800 conn = cls_conn->dd_data;
2801 memset(conn, 0, sizeof(*conn) + dd_size);
2803 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2804 conn->session = session;
2805 conn->cls_conn = cls_conn;
2806 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2807 conn->id = conn_idx;
2808 conn->exp_statsn = 0;
2809 conn->tmf_state = TMF_INITIAL;
2811 init_timer(&conn->transport_timer);
2812 conn->transport_timer.data = (unsigned long)conn;
2813 conn->transport_timer.function = iscsi_check_transport_timeouts;
2815 INIT_LIST_HEAD(&conn->mgmtqueue);
2816 INIT_LIST_HEAD(&conn->cmdqueue);
2817 INIT_LIST_HEAD(&conn->requeue);
2818 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2820 /* allocate login_task used for the login/text sequences */
2821 spin_lock_bh(&session->lock);
2822 if (!kfifo_out(&session->cmdpool.queue,
2823 (void*)&conn->login_task,
2825 spin_unlock_bh(&session->lock);
2826 goto login_task_alloc_fail;
2828 spin_unlock_bh(&session->lock);
2830 data = (char *) __get_free_pages(GFP_KERNEL,
2831 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2833 goto login_task_data_alloc_fail;
2834 conn->login_task->data = conn->data = data;
2836 init_timer(&conn->tmf_timer);
2837 init_waitqueue_head(&conn->ehwait);
2841 login_task_data_alloc_fail:
2842 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2844 login_task_alloc_fail:
2845 iscsi_destroy_conn(cls_conn);
2848 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2851 * iscsi_conn_teardown - teardown iscsi connection
2852 * cls_conn: iscsi class connection
2854 * TODO: we may need to make this into a two step process
2855 * like scsi-mls remove + put host
2857 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2859 struct iscsi_conn *conn = cls_conn->dd_data;
2860 struct iscsi_session *session = conn->session;
2861 unsigned long flags;
2863 del_timer_sync(&conn->transport_timer);
2865 spin_lock_bh(&session->lock);
2866 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2867 if (session->leadconn == conn) {
2869 * leading connection? then give up on recovery.
2871 session->state = ISCSI_STATE_TERMINATE;
2872 wake_up(&conn->ehwait);
2874 spin_unlock_bh(&session->lock);
2877 * Block until all in-progress commands for this connection
2881 spin_lock_irqsave(session->host->host_lock, flags);
2882 if (!session->host->host_busy) { /* OK for ERL == 0 */
2883 spin_unlock_irqrestore(session->host->host_lock, flags);
2886 spin_unlock_irqrestore(session->host->host_lock, flags);
2887 msleep_interruptible(500);
2888 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2889 "host_busy %d host_failed %d\n",
2890 session->host->host_busy,
2891 session->host->host_failed);
2893 * force eh_abort() to unblock
2895 wake_up(&conn->ehwait);
2898 /* flush queued up work because we free the connection below */
2899 iscsi_suspend_tx(conn);
2901 spin_lock_bh(&session->lock);
2902 free_pages((unsigned long) conn->data,
2903 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2904 kfree(conn->persistent_address);
2905 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2907 if (session->leadconn == conn)
2908 session->leadconn = NULL;
2909 spin_unlock_bh(&session->lock);
2911 iscsi_destroy_conn(cls_conn);
2913 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2915 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2917 struct iscsi_conn *conn = cls_conn->dd_data;
2918 struct iscsi_session *session = conn->session;
2921 iscsi_conn_printk(KERN_ERR, conn,
2922 "can't start unbound connection\n");
2926 if ((session->imm_data_en || !session->initial_r2t_en) &&
2927 session->first_burst > session->max_burst) {
2928 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2929 "first_burst %d max_burst %d\n",
2930 session->first_burst, session->max_burst);
2934 if (conn->ping_timeout && !conn->recv_timeout) {
2935 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2936 "zero. Using 5 seconds\n.");
2937 conn->recv_timeout = 5;
2940 if (conn->recv_timeout && !conn->ping_timeout) {
2941 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2942 "zero. Using 5 seconds.\n");
2943 conn->ping_timeout = 5;
2946 spin_lock_bh(&session->lock);
2947 conn->c_stage = ISCSI_CONN_STARTED;
2948 session->state = ISCSI_STATE_LOGGED_IN;
2949 session->queued_cmdsn = session->cmdsn;
2951 conn->last_recv = jiffies;
2952 conn->last_ping = jiffies;
2953 if (conn->recv_timeout && conn->ping_timeout)
2954 mod_timer(&conn->transport_timer,
2955 jiffies + (conn->recv_timeout * HZ));
2957 switch(conn->stop_stage) {
2958 case STOP_CONN_RECOVER:
2960 * unblock eh_abort() if it is blocked. re-try all
2961 * commands after successful recovery
2963 conn->stop_stage = 0;
2964 conn->tmf_state = TMF_INITIAL;
2966 if (session->age == 16)
2969 case STOP_CONN_TERM:
2970 conn->stop_stage = 0;
2975 spin_unlock_bh(&session->lock);
2977 iscsi_unblock_session(session->cls_session);
2978 wake_up(&conn->ehwait);
2981 EXPORT_SYMBOL_GPL(iscsi_conn_start);
2984 fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
2986 struct iscsi_task *task;
2989 for (i = 0; i < conn->session->cmds_max; i++) {
2990 task = conn->session->cmds[i];
2994 if (task->state == ISCSI_TASK_FREE)
2997 ISCSI_DBG_SESSION(conn->session,
2998 "failing mgmt itt 0x%x state %d\n",
2999 task->itt, task->state);
3000 state = ISCSI_TASK_ABRT_SESS_RECOV;
3001 if (task->state == ISCSI_TASK_PENDING)
3002 state = ISCSI_TASK_COMPLETED;
3003 iscsi_complete_task(task, state);
3008 static void iscsi_start_session_recovery(struct iscsi_session *session,
3009 struct iscsi_conn *conn, int flag)
3013 mutex_lock(&session->eh_mutex);
3014 spin_lock_bh(&session->lock);
3015 if (conn->stop_stage == STOP_CONN_TERM) {
3016 spin_unlock_bh(&session->lock);
3017 mutex_unlock(&session->eh_mutex);
3022 * When this is called for the in_login state, we only want to clean
3023 * up the login task and connection. We do not need to block and set
3024 * the recovery state again
3026 if (flag == STOP_CONN_TERM)
3027 session->state = ISCSI_STATE_TERMINATE;
3028 else if (conn->stop_stage != STOP_CONN_RECOVER)
3029 session->state = ISCSI_STATE_IN_RECOVERY;
3030 spin_unlock_bh(&session->lock);
3032 del_timer_sync(&conn->transport_timer);
3033 iscsi_suspend_tx(conn);
3035 spin_lock_bh(&session->lock);
3036 old_stop_stage = conn->stop_stage;
3037 conn->stop_stage = flag;
3038 conn->c_stage = ISCSI_CONN_STOPPED;
3039 spin_unlock_bh(&session->lock);
3042 * for connection level recovery we should not calculate
3043 * header digest. conn->hdr_size used for optimization
3044 * in hdr_extract() and will be re-negotiated at
3047 if (flag == STOP_CONN_RECOVER) {
3048 conn->hdrdgst_en = 0;
3049 conn->datadgst_en = 0;
3050 if (session->state == ISCSI_STATE_IN_RECOVERY &&
3051 old_stop_stage != STOP_CONN_RECOVER) {
3052 ISCSI_DBG_SESSION(session, "blocking session\n");
3053 iscsi_block_session(session->cls_session);
3060 spin_lock_bh(&session->lock);
3061 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
3062 fail_mgmt_tasks(session, conn);
3063 memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
3064 spin_unlock_bh(&session->lock);
3065 mutex_unlock(&session->eh_mutex);
3068 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3070 struct iscsi_conn *conn = cls_conn->dd_data;
3071 struct iscsi_session *session = conn->session;
3074 case STOP_CONN_RECOVER:
3075 case STOP_CONN_TERM:
3076 iscsi_start_session_recovery(session, conn, flag);
3079 iscsi_conn_printk(KERN_ERR, conn,
3080 "invalid stop flag %d\n", flag);
3083 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3085 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3086 struct iscsi_cls_conn *cls_conn, int is_leading)
3088 struct iscsi_session *session = cls_session->dd_data;
3089 struct iscsi_conn *conn = cls_conn->dd_data;
3091 spin_lock_bh(&session->lock);
3093 session->leadconn = conn;
3094 spin_unlock_bh(&session->lock);
3097 * Unblock xmitworker(), Login Phase will pass through.
3099 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
3100 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
3103 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3105 static int iscsi_switch_str_param(char **param, char *new_val_buf)
3110 if (!strcmp(*param, new_val_buf))
3114 new_val = kstrdup(new_val_buf, GFP_NOIO);
3123 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3124 enum iscsi_param param, char *buf, int buflen)
3126 struct iscsi_conn *conn = cls_conn->dd_data;
3127 struct iscsi_session *session = conn->session;
3131 case ISCSI_PARAM_FAST_ABORT:
3132 sscanf(buf, "%d", &session->fast_abort);
3134 case ISCSI_PARAM_ABORT_TMO:
3135 sscanf(buf, "%d", &session->abort_timeout);
3137 case ISCSI_PARAM_LU_RESET_TMO:
3138 sscanf(buf, "%d", &session->lu_reset_timeout);
3140 case ISCSI_PARAM_TGT_RESET_TMO:
3141 sscanf(buf, "%d", &session->tgt_reset_timeout);
3143 case ISCSI_PARAM_PING_TMO:
3144 sscanf(buf, "%d", &conn->ping_timeout);
3146 case ISCSI_PARAM_RECV_TMO:
3147 sscanf(buf, "%d", &conn->recv_timeout);
3149 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3150 sscanf(buf, "%d", &conn->max_recv_dlength);
3152 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3153 sscanf(buf, "%d", &conn->max_xmit_dlength);
3155 case ISCSI_PARAM_HDRDGST_EN:
3156 sscanf(buf, "%d", &conn->hdrdgst_en);
3158 case ISCSI_PARAM_DATADGST_EN:
3159 sscanf(buf, "%d", &conn->datadgst_en);
3161 case ISCSI_PARAM_INITIAL_R2T_EN:
3162 sscanf(buf, "%d", &session->initial_r2t_en);
3164 case ISCSI_PARAM_MAX_R2T:
3165 sscanf(buf, "%d", &session->max_r2t);
3167 case ISCSI_PARAM_IMM_DATA_EN:
3168 sscanf(buf, "%d", &session->imm_data_en);
3170 case ISCSI_PARAM_FIRST_BURST:
3171 sscanf(buf, "%d", &session->first_burst);
3173 case ISCSI_PARAM_MAX_BURST:
3174 sscanf(buf, "%d", &session->max_burst);
3176 case ISCSI_PARAM_PDU_INORDER_EN:
3177 sscanf(buf, "%d", &session->pdu_inorder_en);
3179 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3180 sscanf(buf, "%d", &session->dataseq_inorder_en);
3182 case ISCSI_PARAM_ERL:
3183 sscanf(buf, "%d", &session->erl);
3185 case ISCSI_PARAM_IFMARKER_EN:
3186 sscanf(buf, "%d", &value);
3189 case ISCSI_PARAM_OFMARKER_EN:
3190 sscanf(buf, "%d", &value);
3193 case ISCSI_PARAM_EXP_STATSN:
3194 sscanf(buf, "%u", &conn->exp_statsn);
3196 case ISCSI_PARAM_USERNAME:
3197 return iscsi_switch_str_param(&session->username, buf);
3198 case ISCSI_PARAM_USERNAME_IN:
3199 return iscsi_switch_str_param(&session->username_in, buf);
3200 case ISCSI_PARAM_PASSWORD:
3201 return iscsi_switch_str_param(&session->password, buf);
3202 case ISCSI_PARAM_PASSWORD_IN:
3203 return iscsi_switch_str_param(&session->password_in, buf);
3204 case ISCSI_PARAM_TARGET_NAME:
3205 return iscsi_switch_str_param(&session->targetname, buf);
3206 case ISCSI_PARAM_TPGT:
3207 sscanf(buf, "%d", &session->tpgt);
3209 case ISCSI_PARAM_PERSISTENT_PORT:
3210 sscanf(buf, "%d", &conn->persistent_port);
3212 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3213 return iscsi_switch_str_param(&conn->persistent_address, buf);
3214 case ISCSI_PARAM_IFACE_NAME:
3215 return iscsi_switch_str_param(&session->ifacename, buf);
3216 case ISCSI_PARAM_INITIATOR_NAME:
3217 return iscsi_switch_str_param(&session->initiatorname, buf);
3224 EXPORT_SYMBOL_GPL(iscsi_set_param);
3226 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3227 enum iscsi_param param, char *buf)
3229 struct iscsi_session *session = cls_session->dd_data;
3233 case ISCSI_PARAM_FAST_ABORT:
3234 len = sprintf(buf, "%d\n", session->fast_abort);
3236 case ISCSI_PARAM_ABORT_TMO:
3237 len = sprintf(buf, "%d\n", session->abort_timeout);
3239 case ISCSI_PARAM_LU_RESET_TMO:
3240 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
3242 case ISCSI_PARAM_TGT_RESET_TMO:
3243 len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
3245 case ISCSI_PARAM_INITIAL_R2T_EN:
3246 len = sprintf(buf, "%d\n", session->initial_r2t_en);
3248 case ISCSI_PARAM_MAX_R2T:
3249 len = sprintf(buf, "%hu\n", session->max_r2t);
3251 case ISCSI_PARAM_IMM_DATA_EN:
3252 len = sprintf(buf, "%d\n", session->imm_data_en);
3254 case ISCSI_PARAM_FIRST_BURST:
3255 len = sprintf(buf, "%u\n", session->first_burst);
3257 case ISCSI_PARAM_MAX_BURST:
3258 len = sprintf(buf, "%u\n", session->max_burst);
3260 case ISCSI_PARAM_PDU_INORDER_EN:
3261 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
3263 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3264 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
3266 case ISCSI_PARAM_ERL:
3267 len = sprintf(buf, "%d\n", session->erl);
3269 case ISCSI_PARAM_TARGET_NAME:
3270 len = sprintf(buf, "%s\n", session->targetname);
3272 case ISCSI_PARAM_TPGT:
3273 len = sprintf(buf, "%d\n", session->tpgt);
3275 case ISCSI_PARAM_USERNAME:
3276 len = sprintf(buf, "%s\n", session->username);
3278 case ISCSI_PARAM_USERNAME_IN:
3279 len = sprintf(buf, "%s\n", session->username_in);
3281 case ISCSI_PARAM_PASSWORD:
3282 len = sprintf(buf, "%s\n", session->password);
3284 case ISCSI_PARAM_PASSWORD_IN:
3285 len = sprintf(buf, "%s\n", session->password_in);
3287 case ISCSI_PARAM_IFACE_NAME:
3288 len = sprintf(buf, "%s\n", session->ifacename);
3290 case ISCSI_PARAM_INITIATOR_NAME:
3291 len = sprintf(buf, "%s\n", session->initiatorname);
3299 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3301 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3302 enum iscsi_param param, char *buf)
3304 struct iscsi_conn *conn = cls_conn->dd_data;
3308 case ISCSI_PARAM_PING_TMO:
3309 len = sprintf(buf, "%u\n", conn->ping_timeout);
3311 case ISCSI_PARAM_RECV_TMO:
3312 len = sprintf(buf, "%u\n", conn->recv_timeout);
3314 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3315 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3317 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3318 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3320 case ISCSI_PARAM_HDRDGST_EN:
3321 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3323 case ISCSI_PARAM_DATADGST_EN:
3324 len = sprintf(buf, "%d\n", conn->datadgst_en);
3326 case ISCSI_PARAM_IFMARKER_EN:
3327 len = sprintf(buf, "%d\n", conn->ifmarker_en);
3329 case ISCSI_PARAM_OFMARKER_EN:
3330 len = sprintf(buf, "%d\n", conn->ofmarker_en);
3332 case ISCSI_PARAM_EXP_STATSN:
3333 len = sprintf(buf, "%u\n", conn->exp_statsn);
3335 case ISCSI_PARAM_PERSISTENT_PORT:
3336 len = sprintf(buf, "%d\n", conn->persistent_port);
3338 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3339 len = sprintf(buf, "%s\n", conn->persistent_address);
3347 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3349 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3352 struct iscsi_host *ihost = shost_priv(shost);
3356 case ISCSI_HOST_PARAM_NETDEV_NAME:
3357 len = sprintf(buf, "%s\n", ihost->netdev);
3359 case ISCSI_HOST_PARAM_HWADDRESS:
3360 len = sprintf(buf, "%s\n", ihost->hwaddress);
3362 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3363 len = sprintf(buf, "%s\n", ihost->initiatorname);
3365 case ISCSI_HOST_PARAM_IPADDRESS:
3366 len = sprintf(buf, "%s\n", ihost->local_address);
3374 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3376 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3377 char *buf, int buflen)
3379 struct iscsi_host *ihost = shost_priv(shost);
3382 case ISCSI_HOST_PARAM_NETDEV_NAME:
3383 return iscsi_switch_str_param(&ihost->netdev, buf);
3384 case ISCSI_HOST_PARAM_HWADDRESS:
3385 return iscsi_switch_str_param(&ihost->hwaddress, buf);
3386 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3387 return iscsi_switch_str_param(&ihost->initiatorname, buf);
3394 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3396 MODULE_AUTHOR("Mike Christie");
3397 MODULE_DESCRIPTION("iSCSI library functions");
3398 MODULE_LICENSE("GPL");