1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
6 * Copyright (C) 2004 - 2006 Mike Christie
7 * Copyright (C) 2004 - 2005 Dmitry Yusupov
8 * Copyright (C) 2004 - 2005 Alex Aizman
11 #include <linux/types.h>
12 #include <linux/kfifo.h>
13 #include <linux/delay.h>
14 #include <linux/log2.h>
15 #include <linux/slab.h>
16 #include <linux/sched/signal.h>
17 #include <linux/module.h>
18 #include <asm/unaligned.h>
20 #include <scsi/scsi_cmnd.h>
21 #include <scsi/scsi_device.h>
22 #include <scsi/scsi_eh.h>
23 #include <scsi/scsi_tcq.h>
24 #include <scsi/scsi_host.h>
25 #include <scsi/scsi.h>
26 #include <scsi/iscsi_proto.h>
27 #include <scsi/scsi_transport.h>
28 #include <scsi/scsi_transport_iscsi.h>
29 #include <scsi/libiscsi.h>
30 #include <trace/events/iscsi.h>
32 static int iscsi_dbg_lib_conn;
33 module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
35 MODULE_PARM_DESC(debug_libiscsi_conn,
36 "Turn on debugging for connections in libiscsi module. "
37 "Set to 1 to turn on, and zero to turn off. Default is off.");
39 static int iscsi_dbg_lib_session;
40 module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
42 MODULE_PARM_DESC(debug_libiscsi_session,
43 "Turn on debugging for sessions in libiscsi module. "
44 "Set to 1 to turn on, and zero to turn off. Default is off.");
46 static int iscsi_dbg_lib_eh;
47 module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
49 MODULE_PARM_DESC(debug_libiscsi_eh,
50 "Turn on debugging for error handling in libiscsi module. "
51 "Set to 1 to turn on, and zero to turn off. Default is off.");
53 #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
55 if (iscsi_dbg_lib_conn) \
56 iscsi_conn_printk(KERN_INFO, _conn, \
59 iscsi_dbg_trace(trace_iscsi_dbg_conn, \
60 &(_conn)->cls_conn->dev, \
61 "%s " dbg_fmt, __func__, ##arg);\
64 #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
66 if (iscsi_dbg_lib_session) \
67 iscsi_session_printk(KERN_INFO, _session, \
70 iscsi_dbg_trace(trace_iscsi_dbg_session, \
71 &(_session)->cls_session->dev, \
72 "%s " dbg_fmt, __func__, ##arg); \
75 #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
77 if (iscsi_dbg_lib_eh) \
78 iscsi_session_printk(KERN_INFO, _session, \
81 iscsi_dbg_trace(trace_iscsi_dbg_eh, \
82 &(_session)->cls_session->dev, \
83 "%s " dbg_fmt, __func__, ##arg); \
86 inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
88 struct Scsi_Host *shost = conn->session->host;
89 struct iscsi_host *ihost = shost_priv(shost);
92 queue_work(ihost->workq, &conn->xmitwork);
94 EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
96 static void __iscsi_update_cmdsn(struct iscsi_session *session,
97 uint32_t exp_cmdsn, uint32_t max_cmdsn)
100 * standard specifies this check for when to update expected and
101 * max sequence numbers
103 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
106 if (exp_cmdsn != session->exp_cmdsn &&
107 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
108 session->exp_cmdsn = exp_cmdsn;
110 if (max_cmdsn != session->max_cmdsn &&
111 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn))
112 session->max_cmdsn = max_cmdsn;
115 void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
117 __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
118 be32_to_cpu(hdr->max_cmdsn));
120 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
123 * iscsi_prep_data_out_pdu - initialize Data-Out
124 * @task: scsi command task
126 * @hdr: iscsi data in pdu
129 * Initialize Data-Out within this R2T sequence and finds
130 * proper data_offset within this SCSI command.
132 * This function is called with connection lock taken.
134 void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
135 struct iscsi_data *hdr)
137 struct iscsi_conn *conn = task->conn;
138 unsigned int left = r2t->data_length - r2t->sent;
140 task->hdr_len = sizeof(struct iscsi_data);
142 memset(hdr, 0, sizeof(struct iscsi_data));
144 hdr->datasn = cpu_to_be32(r2t->datasn);
146 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
147 hdr->lun = task->lun;
148 hdr->itt = task->hdr_itt;
149 hdr->exp_statsn = r2t->exp_statsn;
150 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
151 if (left > conn->max_xmit_dlength) {
152 hton24(hdr->dlength, conn->max_xmit_dlength);
153 r2t->data_count = conn->max_xmit_dlength;
156 hton24(hdr->dlength, left);
157 r2t->data_count = left;
158 hdr->flags = ISCSI_FLAG_CMD_FINAL;
160 conn->dataout_pdus_cnt++;
162 EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
164 static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
166 unsigned exp_len = task->hdr_len + len;
168 if (exp_len > task->hdr_max) {
173 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
174 task->hdr_len = exp_len;
179 * make an extended cdb AHS
181 static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
183 struct scsi_cmnd *cmd = task->sc;
184 unsigned rlen, pad_len;
185 unsigned short ahslength;
186 struct iscsi_ecdb_ahdr *ecdb_ahdr;
189 ecdb_ahdr = iscsi_next_hdr(task);
190 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
192 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
193 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
195 pad_len = iscsi_padding(rlen);
197 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
198 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
203 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
205 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
206 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
207 ecdb_ahdr->reserved = 0;
208 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
210 ISCSI_DBG_SESSION(task->conn->session,
211 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
212 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
213 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
219 * iscsi_check_tmf_restrictions - check if a task is affected by TMF
221 * @opcode: opcode to check for
223 * During TMF a task has to be checked if it's affected.
224 * All unrelated I/O can be passed through, but I/O to the
225 * affected LUN should be restricted.
226 * If 'fast_abort' is set we won't be sending any I/O to the
228 * Otherwise the target is waiting for all TTTs to be completed,
229 * so we have to send all outstanding Data-Out PDUs to the target.
231 static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
233 struct iscsi_conn *conn = task->conn;
234 struct iscsi_tm *tmf = &conn->tmhdr;
237 if (conn->tmf_state == TMF_INITIAL)
240 if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
243 switch (ISCSI_TM_FUNC_VALUE(tmf)) {
244 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
246 * Allow PDUs for unrelated LUNs
248 hdr_lun = scsilun_to_int(&tmf->lun);
249 if (hdr_lun != task->sc->device->lun)
252 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
254 * Fail all SCSI cmd PDUs
256 if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
257 iscsi_conn_printk(KERN_INFO, conn,
266 * And also all data-out PDUs in response to R2T
267 * if fast_abort is set.
269 if (conn->session->fast_abort) {
270 iscsi_conn_printk(KERN_INFO, conn,
272 "0x%x/0x%x] fast abort.\n",
278 case ISCSI_TM_FUNC_ABORT_TASK:
280 * the caller has already checked if the task
281 * they want to abort was in the pending queue so if
282 * we are here the cmd pdu has gone out already, and
283 * we will only hit this for data-outs
285 if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
286 task->hdr_itt == tmf->rtt) {
287 ISCSI_DBG_SESSION(conn->session,
288 "Preventing task %x/%x from sending "
289 "data-out due to abort task in "
290 "progress\n", task->itt,
301 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
304 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
305 * fields like dlength or final based on how much data it sends
307 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
309 struct iscsi_conn *conn = task->conn;
310 struct iscsi_session *session = conn->session;
311 struct scsi_cmnd *sc = task->sc;
312 struct iscsi_scsi_req *hdr;
313 unsigned hdrlength, cmd_len, transfer_length;
317 rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
321 if (conn->session->tt->alloc_pdu) {
322 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
326 hdr = (struct iscsi_scsi_req *)task->hdr;
328 memset(hdr, 0, sizeof(*hdr));
330 if (session->tt->parse_pdu_itt)
331 hdr->itt = task->hdr_itt = itt;
333 hdr->itt = task->hdr_itt = build_itt(task->itt,
334 task->conn->session->age);
336 rc = iscsi_add_hdr(task, sizeof(*hdr));
339 hdr->opcode = ISCSI_OP_SCSI_CMD;
340 hdr->flags = ISCSI_ATTR_SIMPLE;
341 int_to_scsilun(sc->device->lun, &hdr->lun);
342 task->lun = hdr->lun;
343 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
344 cmd_len = sc->cmd_len;
345 if (cmd_len < ISCSI_CDB_SIZE)
346 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
347 else if (cmd_len > ISCSI_CDB_SIZE) {
348 rc = iscsi_prep_ecdb_ahs(task);
351 cmd_len = ISCSI_CDB_SIZE;
353 memcpy(hdr->cdb, sc->cmnd, cmd_len);
356 if (scsi_get_prot_op(sc) != SCSI_PROT_NORMAL)
357 task->protected = true;
359 transfer_length = scsi_transfer_length(sc);
360 hdr->data_length = cpu_to_be32(transfer_length);
361 if (sc->sc_data_direction == DMA_TO_DEVICE) {
362 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
364 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
368 * imm_count bytes to be sent right after
371 * unsol_count bytes(as Data-Out) to be sent
372 * without R2T ack right after
375 * r2t data_length bytes to be sent via R2T ack's
377 * pad_count bytes to be sent as zero-padding
379 memset(r2t, 0, sizeof(*r2t));
381 if (session->imm_data_en) {
382 if (transfer_length >= session->first_burst)
383 task->imm_count = min(session->first_burst,
384 conn->max_xmit_dlength);
386 task->imm_count = min(transfer_length,
387 conn->max_xmit_dlength);
388 hton24(hdr->dlength, task->imm_count);
390 zero_data(hdr->dlength);
392 if (!session->initial_r2t_en) {
393 r2t->data_length = min(session->first_burst,
396 r2t->data_offset = task->imm_count;
397 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
398 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
401 if (!task->unsol_r2t.data_length)
402 /* No unsolicit Data-Out's */
403 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
405 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
406 zero_data(hdr->dlength);
408 if (sc->sc_data_direction == DMA_FROM_DEVICE)
409 hdr->flags |= ISCSI_FLAG_CMD_READ;
412 /* calculate size of additional header segments (AHSs) */
413 hdrlength = task->hdr_len - sizeof(*hdr);
415 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
416 hdrlength /= ISCSI_PAD_LEN;
418 WARN_ON(hdrlength >= 256);
419 hdr->hlength = hdrlength & 0xFF;
420 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
422 if (session->tt->init_task && session->tt->init_task(task))
425 task->state = ISCSI_TASK_RUNNING;
428 conn->scsicmd_pdus_cnt++;
429 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
430 "itt 0x%x len %d cmdsn %d win %d]\n",
431 sc->sc_data_direction == DMA_TO_DEVICE ?
432 "write" : "read", conn->id, sc, sc->cmnd[0],
433 task->itt, transfer_length,
435 session->max_cmdsn - session->exp_cmdsn + 1);
440 * iscsi_free_task - free a task
441 * @task: iscsi cmd task
443 * Must be called with session back_lock.
444 * This function returns the scsi command to scsi-ml or cleans
445 * up mgmt tasks then returns the task to the pool.
447 static void iscsi_free_task(struct iscsi_task *task)
449 struct iscsi_conn *conn = task->conn;
450 struct iscsi_session *session = conn->session;
451 struct scsi_cmnd *sc = task->sc;
452 int oldstate = task->state;
454 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
455 task->itt, task->state, task->sc);
457 session->tt->cleanup_task(task);
458 task->state = ISCSI_TASK_FREE;
461 * login task is preallocated so do not free
463 if (conn->login_task == task)
466 kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
469 /* SCSI eh reuses commands to verify us */
472 * queue command may call this to free the task, so
473 * it will decide how to return sc to scsi-ml.
475 if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
480 void __iscsi_get_task(struct iscsi_task *task)
482 refcount_inc(&task->refcount);
484 EXPORT_SYMBOL_GPL(__iscsi_get_task);
486 void __iscsi_put_task(struct iscsi_task *task)
488 if (refcount_dec_and_test(&task->refcount))
489 iscsi_free_task(task);
491 EXPORT_SYMBOL_GPL(__iscsi_put_task);
493 void iscsi_put_task(struct iscsi_task *task)
495 struct iscsi_session *session = task->conn->session;
497 /* regular RX path uses back_lock */
498 spin_lock_bh(&session->back_lock);
499 __iscsi_put_task(task);
500 spin_unlock_bh(&session->back_lock);
502 EXPORT_SYMBOL_GPL(iscsi_put_task);
505 * iscsi_complete_task - finish a task
506 * @task: iscsi cmd task
507 * @state: state to complete task with
509 * Must be called with session back_lock.
511 static void iscsi_complete_task(struct iscsi_task *task, int state)
513 struct iscsi_conn *conn = task->conn;
515 ISCSI_DBG_SESSION(conn->session,
516 "complete task itt 0x%x state %d sc %p\n",
517 task->itt, task->state, task->sc);
518 if (task->state == ISCSI_TASK_COMPLETED ||
519 task->state == ISCSI_TASK_ABRT_TMF ||
520 task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
521 task->state == ISCSI_TASK_REQUEUE_SCSIQ)
523 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
526 if (READ_ONCE(conn->ping_task) == task)
527 WRITE_ONCE(conn->ping_task, NULL);
529 /* release get from queueing */
530 __iscsi_put_task(task);
534 * iscsi_complete_scsi_task - finish scsi task normally
535 * @task: iscsi task for scsi cmd
536 * @exp_cmdsn: expected cmd sn in cpu format
537 * @max_cmdsn: max cmd sn in cpu format
539 * This is used when drivers do not need or cannot perform
540 * lower level pdu processing.
542 * Called with session back_lock
544 void iscsi_complete_scsi_task(struct iscsi_task *task,
545 uint32_t exp_cmdsn, uint32_t max_cmdsn)
547 struct iscsi_conn *conn = task->conn;
549 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
551 conn->last_recv = jiffies;
552 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
553 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
555 EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
558 * Must be called with back and frwd lock
560 static bool cleanup_queued_task(struct iscsi_task *task)
562 struct iscsi_conn *conn = task->conn;
563 bool early_complete = false;
565 /* Bad target might have completed task while it was still running */
566 if (task->state == ISCSI_TASK_COMPLETED)
567 early_complete = true;
569 if (!list_empty(&task->running)) {
570 list_del_init(&task->running);
572 * If it's on a list but still running, this could be from
573 * a bad target sending a rsp early, cleanup from a TMF, or
576 if (task->state == ISCSI_TASK_RUNNING ||
577 task->state == ISCSI_TASK_COMPLETED)
578 __iscsi_put_task(task);
581 if (conn->task == task) {
583 __iscsi_put_task(task);
586 return early_complete;
590 * session frwd lock must be held and if not called for a task that is still
591 * pending or from the xmit thread, then xmit thread must be suspended
593 static void fail_scsi_task(struct iscsi_task *task, int err)
595 struct iscsi_conn *conn = task->conn;
596 struct scsi_cmnd *sc;
599 spin_lock_bh(&conn->session->back_lock);
600 if (cleanup_queued_task(task)) {
601 spin_unlock_bh(&conn->session->back_lock);
605 if (task->state == ISCSI_TASK_PENDING) {
607 * cmd never made it to the xmit thread, so we should not count
608 * the cmd in the sequencing
610 conn->session->queued_cmdsn--;
611 /* it was never sent so just complete like normal */
612 state = ISCSI_TASK_COMPLETED;
613 } else if (err == DID_TRANSPORT_DISRUPTED)
614 state = ISCSI_TASK_ABRT_SESS_RECOV;
616 state = ISCSI_TASK_ABRT_TMF;
619 sc->result = err << 16;
620 scsi_set_resid(sc, scsi_bufflen(sc));
621 iscsi_complete_task(task, state);
622 spin_unlock_bh(&conn->session->back_lock);
625 static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
626 struct iscsi_task *task)
628 struct iscsi_session *session = conn->session;
629 struct iscsi_hdr *hdr = task->hdr;
630 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
631 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
633 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
636 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
637 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
639 * pre-format CmdSN for outgoing PDU.
641 nop->cmdsn = cpu_to_be32(session->cmdsn);
642 if (hdr->itt != RESERVED_ITT) {
644 * TODO: We always use immediate for normal session pdus.
645 * If we start to send tmfs or nops as non-immediate then
646 * we should start checking the cmdsn numbers for mgmt tasks.
648 * During discovery sessions iscsid sends TEXT as non immediate,
649 * but we always only send one PDU at a time.
651 if (conn->c_stage == ISCSI_CONN_STARTED &&
652 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
653 session->queued_cmdsn++;
658 if (session->tt->init_task && session->tt->init_task(task))
661 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
662 session->state = ISCSI_STATE_LOGGING_OUT;
664 task->state = ISCSI_TASK_RUNNING;
665 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
666 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
667 hdr->itt, task->data_count);
671 static struct iscsi_task *
672 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
673 char *data, uint32_t data_size)
675 struct iscsi_session *session = conn->session;
676 struct iscsi_host *ihost = shost_priv(session->host);
677 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
678 struct iscsi_task *task;
681 if (session->state == ISCSI_STATE_TERMINATE)
684 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
686 * Login and Text are sent serially, in
687 * request-followed-by-response sequence.
688 * Same task can be used. Same ITT must be used.
689 * Note that login_task is preallocated at conn_create().
691 if (conn->login_task->state != ISCSI_TASK_FREE) {
692 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
693 "progress. Cannot start new task.\n");
697 if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) {
698 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);
702 task = conn->login_task;
704 if (session->state != ISCSI_STATE_LOGGED_IN)
707 if (data_size != 0) {
708 iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode);
712 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
713 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
715 if (!kfifo_out(&session->cmdpool.queue,
716 (void*)&task, sizeof(void*)))
720 * released in complete pdu for task we expect a response for, and
721 * released by the lld when it has transmitted the task for
722 * pdus we do not expect a response for.
724 refcount_set(&task->refcount, 1);
727 INIT_LIST_HEAD(&task->running);
728 task->state = ISCSI_TASK_PENDING;
731 memcpy(task->data, data, data_size);
732 task->data_count = data_size;
734 task->data_count = 0;
736 if (conn->session->tt->alloc_pdu) {
737 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
738 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
739 "pdu for mgmt task.\n");
744 itt = task->hdr->itt;
745 task->hdr_len = sizeof(struct iscsi_hdr);
746 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
748 if (hdr->itt != RESERVED_ITT) {
749 if (session->tt->parse_pdu_itt)
750 task->hdr->itt = itt;
752 task->hdr->itt = build_itt(task->itt,
753 task->conn->session->age);
756 if (unlikely(READ_ONCE(conn->ping_task) == INVALID_SCSI_TASK))
757 WRITE_ONCE(conn->ping_task, task);
760 if (iscsi_prep_mgmt_task(conn, task))
763 if (session->tt->xmit_task(task))
766 list_add_tail(&task->running, &conn->mgmtqueue);
767 iscsi_conn_queue_work(conn);
773 /* regular RX path uses back_lock */
774 spin_lock(&session->back_lock);
775 __iscsi_put_task(task);
776 spin_unlock(&session->back_lock);
780 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
781 char *data, uint32_t data_size)
783 struct iscsi_conn *conn = cls_conn->dd_data;
784 struct iscsi_session *session = conn->session;
787 spin_lock_bh(&session->frwd_lock);
788 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
790 spin_unlock_bh(&session->frwd_lock);
793 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
796 * iscsi_scsi_cmd_rsp - SCSI Command Response processing
797 * @conn: iscsi connection
799 * @task: scsi command task
800 * @data: cmd data buffer
801 * @datalen: len of buffer
803 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
804 * then completes the command and task. called under back_lock
806 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
807 struct iscsi_task *task, char *data,
810 struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
811 struct iscsi_session *session = conn->session;
812 struct scsi_cmnd *sc = task->sc;
814 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
815 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
817 sc->result = (DID_OK << 16) | rhdr->cmd_status;
819 if (task->protected) {
824 * Transports that didn't implement check_protection
825 * callback but still published T10-PI support to scsi-mid
826 * deserve this BUG_ON.
828 BUG_ON(!session->tt->check_protection);
830 ascq = session->tt->check_protection(task, §or);
832 sc->result = DRIVER_SENSE << 24 |
833 SAM_STAT_CHECK_CONDITION;
834 scsi_build_sense_buffer(1, sc->sense_buffer,
835 ILLEGAL_REQUEST, 0x10, ascq);
836 scsi_set_sense_information(sc->sense_buffer,
837 SCSI_SENSE_BUFFERSIZE,
843 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
844 sc->result = DID_ERROR << 16;
848 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
853 iscsi_conn_printk(KERN_ERR, conn,
854 "Got CHECK_CONDITION but invalid data "
855 "buffer size of %d\n", datalen);
856 sc->result = DID_BAD_TARGET << 16;
860 senselen = get_unaligned_be16(data);
861 if (datalen < senselen)
862 goto invalid_datalen;
864 memcpy(sc->sense_buffer, data + 2,
865 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
866 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
867 min_t(uint16_t, senselen,
868 SCSI_SENSE_BUFFERSIZE));
871 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
872 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
873 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
876 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
877 ISCSI_FLAG_CMD_OVERFLOW)) {
878 int res_count = be32_to_cpu(rhdr->residual_count);
881 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
882 res_count <= scsi_bufflen(sc)))
883 /* write side for bidi or uni-io set_resid */
884 scsi_set_resid(sc, res_count);
886 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
889 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
890 sc, sc->result, task->itt);
891 conn->scsirsp_pdus_cnt++;
892 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
896 * iscsi_data_in_rsp - SCSI Data-In Response processing
897 * @conn: iscsi connection
899 * @task: scsi command task
901 * iscsi_data_in_rsp sets up the scsi_cmnd fields based on the data received
902 * then completes the command and task. called under back_lock
905 iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
906 struct iscsi_task *task)
908 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
909 struct scsi_cmnd *sc = task->sc;
911 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
914 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
915 sc->result = (DID_OK << 16) | rhdr->cmd_status;
916 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
917 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
918 ISCSI_FLAG_DATA_OVERFLOW)) {
919 int res_count = be32_to_cpu(rhdr->residual_count);
922 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
923 res_count <= sc->sdb.length))
924 scsi_set_resid(sc, res_count);
926 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
929 ISCSI_DBG_SESSION(conn->session, "data in with status done "
930 "[sc %p res %d itt 0x%x]\n",
931 sc, sc->result, task->itt);
932 conn->scsirsp_pdus_cnt++;
933 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
936 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
938 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
940 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
941 conn->tmfrsp_pdus_cnt++;
943 if (conn->tmf_state != TMF_QUEUED)
946 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
947 conn->tmf_state = TMF_SUCCESS;
948 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
949 conn->tmf_state = TMF_NOT_FOUND;
951 conn->tmf_state = TMF_FAILED;
952 wake_up(&conn->ehwait);
955 static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
957 struct iscsi_nopout hdr;
958 struct iscsi_task *task;
961 if (READ_ONCE(conn->ping_task))
963 WRITE_ONCE(conn->ping_task, INVALID_SCSI_TASK);
966 memset(&hdr, 0, sizeof(struct iscsi_nopout));
967 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
968 hdr.flags = ISCSI_FLAG_CMD_FINAL;
973 hdr.itt = RESERVED_ITT;
975 hdr.ttt = RESERVED_ITT;
977 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
980 WRITE_ONCE(conn->ping_task, NULL);
981 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
984 /* only track our nops */
985 conn->last_ping = jiffies;
992 * iscsi_nop_out_rsp - SCSI NOP Response processing
993 * @task: scsi command task
994 * @nop: the nop structure
995 * @data: where to put the data
996 * @datalen: length of data
998 * iscsi_nop_out_rsp handles nop response from use or
999 * from user space. called under back_lock
1001 static int iscsi_nop_out_rsp(struct iscsi_task *task,
1002 struct iscsi_nopin *nop, char *data, int datalen)
1004 struct iscsi_conn *conn = task->conn;
1007 if (READ_ONCE(conn->ping_task) != task) {
1009 * If this is not in response to one of our
1010 * nops then it must be from userspace.
1012 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
1014 rc = ISCSI_ERR_CONN_FAILED;
1016 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
1017 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1021 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1022 char *data, int datalen)
1024 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
1025 struct iscsi_hdr rejected_pdu;
1028 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
1030 if (ntoh24(reject->dlength) > datalen ||
1031 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
1032 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
1033 "pdu. Invalid data length (pdu dlength "
1034 "%u, datalen %d\n", ntoh24(reject->dlength),
1036 return ISCSI_ERR_PROTO;
1038 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
1039 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
1041 switch (reject->reason) {
1042 case ISCSI_REASON_DATA_DIGEST_ERROR:
1043 iscsi_conn_printk(KERN_ERR, conn,
1044 "pdu (op 0x%x itt 0x%x) rejected "
1045 "due to DataDigest error.\n",
1046 opcode, rejected_pdu.itt);
1048 case ISCSI_REASON_IMM_CMD_REJECT:
1049 iscsi_conn_printk(KERN_ERR, conn,
1050 "pdu (op 0x%x itt 0x%x) rejected. Too many "
1051 "immediate commands.\n",
1052 opcode, rejected_pdu.itt);
1054 * We only send one TMF at a time so if the target could not
1055 * handle it, then it should get fixed (RFC mandates that
1056 * a target can handle one immediate TMF per conn).
1058 * For nops-outs, we could have sent more than one if
1059 * the target is sending us lots of nop-ins
1061 if (opcode != ISCSI_OP_NOOP_OUT)
1064 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1066 * nop-out in response to target's nop-out rejected.
1069 /* In RX path we are under back lock */
1070 spin_unlock(&conn->session->back_lock);
1071 spin_lock(&conn->session->frwd_lock);
1072 iscsi_send_nopout(conn,
1073 (struct iscsi_nopin*)&rejected_pdu);
1074 spin_unlock(&conn->session->frwd_lock);
1075 spin_lock(&conn->session->back_lock);
1077 struct iscsi_task *task;
1079 * Our nop as ping got dropped. We know the target
1080 * and transport are ok so just clean up
1082 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
1084 iscsi_conn_printk(KERN_ERR, conn,
1085 "Invalid pdu reject. Could "
1086 "not lookup rejected task.\n");
1087 rc = ISCSI_ERR_BAD_ITT;
1089 rc = iscsi_nop_out_rsp(task,
1090 (struct iscsi_nopin*)&rejected_pdu,
1095 iscsi_conn_printk(KERN_ERR, conn,
1096 "pdu (op 0x%x itt 0x%x) rejected. Reason "
1097 "code 0x%x\n", rejected_pdu.opcode,
1098 rejected_pdu.itt, reject->reason);
1105 * iscsi_itt_to_task - look up task by itt
1106 * @conn: iscsi connection
1109 * This should be used for mgmt tasks like login and nops, or if
1110 * the LDD's itt space does not include the session age.
1112 * The session back_lock must be held.
1114 struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
1116 struct iscsi_session *session = conn->session;
1119 if (itt == RESERVED_ITT)
1122 if (session->tt->parse_pdu_itt)
1123 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1126 if (i >= session->cmds_max)
1129 return session->cmds[i];
1131 EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
1134 * __iscsi_complete_pdu - complete pdu
1136 * @hdr: iscsi header
1137 * @data: data buffer
1138 * @datalen: len of data buffer
1140 * Completes pdu processing by freeing any resources allocated at
1141 * queuecommand or send generic. session back_lock must be held and verify
1142 * itt must have been called.
1144 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1145 char *data, int datalen)
1147 struct iscsi_session *session = conn->session;
1148 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
1149 struct iscsi_task *task;
1152 conn->last_recv = jiffies;
1153 rc = iscsi_verify_itt(conn, hdr->itt);
1157 if (hdr->itt != RESERVED_ITT)
1158 itt = get_itt(hdr->itt);
1162 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1163 opcode, conn->id, itt, datalen);
1166 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1169 case ISCSI_OP_NOOP_IN:
1171 rc = ISCSI_ERR_PROTO;
1175 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
1178 /* In RX path we are under back lock */
1179 spin_unlock(&session->back_lock);
1180 spin_lock(&session->frwd_lock);
1181 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
1182 spin_unlock(&session->frwd_lock);
1183 spin_lock(&session->back_lock);
1185 case ISCSI_OP_REJECT:
1186 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1188 case ISCSI_OP_ASYNC_EVENT:
1189 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1190 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1191 rc = ISCSI_ERR_CONN_FAILED;
1194 rc = ISCSI_ERR_BAD_OPCODE;
1201 case ISCSI_OP_SCSI_CMD_RSP:
1202 case ISCSI_OP_SCSI_DATA_IN:
1203 task = iscsi_itt_to_ctask(conn, hdr->itt);
1205 return ISCSI_ERR_BAD_ITT;
1206 task->last_xfer = jiffies;
1210 * LLD handles R2Ts if they need to.
1213 case ISCSI_OP_LOGOUT_RSP:
1214 case ISCSI_OP_LOGIN_RSP:
1215 case ISCSI_OP_TEXT_RSP:
1216 case ISCSI_OP_SCSI_TMFUNC_RSP:
1217 case ISCSI_OP_NOOP_IN:
1218 task = iscsi_itt_to_task(conn, hdr->itt);
1220 return ISCSI_ERR_BAD_ITT;
1223 return ISCSI_ERR_BAD_OPCODE;
1227 case ISCSI_OP_SCSI_CMD_RSP:
1228 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
1230 case ISCSI_OP_SCSI_DATA_IN:
1231 iscsi_data_in_rsp(conn, hdr, task);
1233 case ISCSI_OP_LOGOUT_RSP:
1234 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1236 rc = ISCSI_ERR_PROTO;
1239 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1241 case ISCSI_OP_LOGIN_RSP:
1242 case ISCSI_OP_TEXT_RSP:
1243 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1245 * login related PDU's exp_statsn is handled in
1249 case ISCSI_OP_SCSI_TMFUNC_RSP:
1250 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1252 rc = ISCSI_ERR_PROTO;
1256 iscsi_tmf_rsp(conn, hdr);
1257 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1259 case ISCSI_OP_NOOP_IN:
1260 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1261 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1262 rc = ISCSI_ERR_PROTO;
1265 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1267 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1271 rc = ISCSI_ERR_BAD_OPCODE;
1278 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1279 rc = ISCSI_ERR_CONN_FAILED;
1280 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1283 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
1285 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1286 char *data, int datalen)
1290 spin_lock(&conn->session->back_lock);
1291 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1292 spin_unlock(&conn->session->back_lock);
1295 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1297 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
1299 struct iscsi_session *session = conn->session;
1302 if (itt == RESERVED_ITT)
1305 if (session->tt->parse_pdu_itt)
1306 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1309 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1312 if (age != session->age) {
1313 iscsi_conn_printk(KERN_ERR, conn,
1314 "received itt %x expected session age (%x)\n",
1315 (__force u32)itt, session->age);
1316 return ISCSI_ERR_BAD_ITT;
1319 if (i >= session->cmds_max) {
1320 iscsi_conn_printk(KERN_ERR, conn,
1321 "received invalid itt index %u (max cmds "
1322 "%u.\n", i, session->cmds_max);
1323 return ISCSI_ERR_BAD_ITT;
1327 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1330 * iscsi_itt_to_ctask - look up ctask by itt
1331 * @conn: iscsi connection
1334 * This should be used for cmd tasks.
1336 * The session back_lock must be held.
1338 struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
1340 struct iscsi_task *task;
1342 if (iscsi_verify_itt(conn, itt))
1345 task = iscsi_itt_to_task(conn, itt);
1346 if (!task || !task->sc)
1349 if (task->sc->SCp.phase != conn->session->age) {
1350 iscsi_session_printk(KERN_ERR, conn->session,
1351 "task's session age %d, expected %d\n",
1352 task->sc->SCp.phase, conn->session->age);
1358 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1360 void iscsi_session_failure(struct iscsi_session *session,
1363 struct iscsi_conn *conn;
1366 spin_lock_bh(&session->frwd_lock);
1367 conn = session->leadconn;
1368 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1369 spin_unlock_bh(&session->frwd_lock);
1373 dev = get_device(&conn->cls_conn->dev);
1374 spin_unlock_bh(&session->frwd_lock);
1378 * if the host is being removed bypass the connection
1379 * recovery initialization because we are going to kill
1382 if (err == ISCSI_ERR_INVALID_HOST)
1383 iscsi_conn_error_event(conn->cls_conn, err);
1385 iscsi_conn_failure(conn, err);
1388 EXPORT_SYMBOL_GPL(iscsi_session_failure);
1390 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1392 struct iscsi_session *session = conn->session;
1394 spin_lock_bh(&session->frwd_lock);
1395 if (session->state == ISCSI_STATE_FAILED) {
1396 spin_unlock_bh(&session->frwd_lock);
1400 if (conn->stop_stage == 0)
1401 session->state = ISCSI_STATE_FAILED;
1402 spin_unlock_bh(&session->frwd_lock);
1404 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1405 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1406 iscsi_conn_error_event(conn->cls_conn, err);
1408 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1410 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1412 struct iscsi_session *session = conn->session;
1415 * Check for iSCSI window and take care of CmdSN wrap-around
1417 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1418 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1419 "%u MaxCmdSN %u CmdSN %u/%u\n",
1420 session->exp_cmdsn, session->max_cmdsn,
1421 session->cmdsn, session->queued_cmdsn);
1427 static int iscsi_xmit_task(struct iscsi_conn *conn, struct iscsi_task *task,
1432 spin_lock_bh(&conn->session->back_lock);
1435 /* Take a ref so we can access it after xmit_task() */
1436 __iscsi_get_task(task);
1438 /* Already have a ref from when we failed to send it last call */
1443 * If this was a requeue for a R2T we have an extra ref on the task in
1444 * case a bad target sends a cmd rsp before we have handled the task.
1447 __iscsi_put_task(task);
1450 * Do this after dropping the extra ref because if this was a requeue
1451 * it's removed from that list and cleanup_queued_task would miss it.
1453 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1455 * Save the task and ref in case we weren't cleaning up this
1456 * task and get woken up again.
1459 spin_unlock_bh(&conn->session->back_lock);
1462 spin_unlock_bh(&conn->session->back_lock);
1464 spin_unlock_bh(&conn->session->frwd_lock);
1465 rc = conn->session->tt->xmit_task(task);
1466 spin_lock_bh(&conn->session->frwd_lock);
1468 /* done with this task */
1469 task->last_xfer = jiffies;
1471 /* regular RX path uses back_lock */
1472 spin_lock(&conn->session->back_lock);
1473 if (rc && task->state == ISCSI_TASK_RUNNING) {
1475 * get an extra ref that is released next time we access it
1476 * as conn->task above.
1478 __iscsi_get_task(task);
1482 __iscsi_put_task(task);
1483 spin_unlock(&conn->session->back_lock);
1488 * iscsi_requeue_task - requeue task to run from session workqueue
1489 * @task: task to requeue
1491 * Callers must have taken a ref to the task that is going to be requeued.
1493 void iscsi_requeue_task(struct iscsi_task *task)
1495 struct iscsi_conn *conn = task->conn;
1498 * this may be on the requeue list already if the xmit_task callout
1499 * is handling the r2ts while we are adding new ones
1501 spin_lock_bh(&conn->session->frwd_lock);
1502 if (list_empty(&task->running)) {
1503 list_add_tail(&task->running, &conn->requeue);
1506 * Don't need the extra ref since it's already requeued and
1509 iscsi_put_task(task);
1511 iscsi_conn_queue_work(conn);
1512 spin_unlock_bh(&conn->session->frwd_lock);
1514 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1517 * iscsi_data_xmit - xmit any command into the scheduled connection
1518 * @conn: iscsi connection
1521 * The function can return -EAGAIN in which case the caller must
1522 * re-schedule it again later or recover. '0' return code means
1525 static int iscsi_data_xmit(struct iscsi_conn *conn)
1527 struct iscsi_task *task;
1530 spin_lock_bh(&conn->session->frwd_lock);
1531 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1532 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1533 spin_unlock_bh(&conn->session->frwd_lock);
1538 rc = iscsi_xmit_task(conn, conn->task, false);
1544 * process mgmt pdus like nops before commands since we should
1545 * only have one nop-out as a ping from us and targets should not
1546 * overflow us with nop-ins
1549 while (!list_empty(&conn->mgmtqueue)) {
1550 task = list_entry(conn->mgmtqueue.next, struct iscsi_task,
1552 list_del_init(&task->running);
1553 if (iscsi_prep_mgmt_task(conn, task)) {
1554 /* regular RX path uses back_lock */
1555 spin_lock_bh(&conn->session->back_lock);
1556 __iscsi_put_task(task);
1557 spin_unlock_bh(&conn->session->back_lock);
1560 rc = iscsi_xmit_task(conn, task, false);
1565 /* process pending command queue */
1566 while (!list_empty(&conn->cmdqueue)) {
1567 task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1569 list_del_init(&task->running);
1570 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1571 fail_scsi_task(task, DID_IMM_RETRY);
1574 rc = iscsi_prep_scsi_cmd_pdu(task);
1576 if (rc == -ENOMEM || rc == -EACCES)
1577 fail_scsi_task(task, DID_IMM_RETRY);
1579 fail_scsi_task(task, DID_ABORT);
1582 rc = iscsi_xmit_task(conn, task, false);
1586 * we could continuously get new task requests so
1587 * we need to check the mgmt queue for nops that need to
1588 * be sent to aviod starvation
1590 if (!list_empty(&conn->mgmtqueue))
1594 while (!list_empty(&conn->requeue)) {
1596 * we always do fastlogout - conn stop code will clean up.
1598 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1601 task = list_entry(conn->requeue.next, struct iscsi_task,
1604 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1607 list_del_init(&task->running);
1608 rc = iscsi_xmit_task(conn, task, true);
1611 if (!list_empty(&conn->mgmtqueue))
1614 spin_unlock_bh(&conn->session->frwd_lock);
1618 spin_unlock_bh(&conn->session->frwd_lock);
1622 static void iscsi_xmitworker(struct work_struct *work)
1624 struct iscsi_conn *conn =
1625 container_of(work, struct iscsi_conn, xmitwork);
1628 * serialize Xmit worker on a per-connection basis.
1631 rc = iscsi_data_xmit(conn);
1632 } while (rc >= 0 || rc == -EAGAIN);
1635 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1636 struct scsi_cmnd *sc)
1638 struct iscsi_task *task;
1640 if (!kfifo_out(&conn->session->cmdpool.queue,
1641 (void *) &task, sizeof(void *)))
1644 sc->SCp.phase = conn->session->age;
1645 sc->SCp.ptr = (char *) task;
1647 refcount_set(&task->refcount, 1);
1648 task->state = ISCSI_TASK_PENDING;
1651 task->have_checked_conn = false;
1652 task->last_timeout = jiffies;
1653 task->last_xfer = jiffies;
1654 task->protected = false;
1655 INIT_LIST_HEAD(&task->running);
1660 FAILURE_BAD_HOST = 1,
1661 FAILURE_SESSION_FAILED,
1662 FAILURE_SESSION_FREED,
1663 FAILURE_WINDOW_CLOSED,
1665 FAILURE_SESSION_TERMINATE,
1666 FAILURE_SESSION_IN_RECOVERY,
1667 FAILURE_SESSION_RECOVERY_TIMEOUT,
1668 FAILURE_SESSION_LOGGING_OUT,
1669 FAILURE_SESSION_NOT_READY,
1672 int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
1674 struct iscsi_cls_session *cls_session;
1675 struct iscsi_host *ihost;
1677 struct iscsi_session *session;
1678 struct iscsi_conn *conn;
1679 struct iscsi_task *task = NULL;
1684 ihost = shost_priv(host);
1686 cls_session = starget_to_session(scsi_target(sc->device));
1687 session = cls_session->dd_data;
1688 spin_lock_bh(&session->frwd_lock);
1690 reason = iscsi_session_chkready(cls_session);
1692 sc->result = reason;
1696 if (session->state != ISCSI_STATE_LOGGED_IN) {
1698 * to handle the race between when we set the recovery state
1699 * and block the session we requeue here (commands could
1700 * be entering our queuecommand while a block is starting
1701 * up because the block code is not locked)
1703 switch (session->state) {
1704 case ISCSI_STATE_FAILED:
1706 * cmds should fail during shutdown, if the session
1707 * state is bad, allowing completion to happen
1709 if (unlikely(system_state != SYSTEM_RUNNING)) {
1710 reason = FAILURE_SESSION_FAILED;
1711 sc->result = DID_NO_CONNECT << 16;
1715 case ISCSI_STATE_IN_RECOVERY:
1716 reason = FAILURE_SESSION_IN_RECOVERY;
1717 sc->result = DID_IMM_RETRY << 16;
1719 case ISCSI_STATE_LOGGING_OUT:
1720 reason = FAILURE_SESSION_LOGGING_OUT;
1721 sc->result = DID_IMM_RETRY << 16;
1723 case ISCSI_STATE_RECOVERY_FAILED:
1724 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1725 sc->result = DID_TRANSPORT_FAILFAST << 16;
1727 case ISCSI_STATE_TERMINATE:
1728 reason = FAILURE_SESSION_TERMINATE;
1729 sc->result = DID_NO_CONNECT << 16;
1732 reason = FAILURE_SESSION_FREED;
1733 sc->result = DID_NO_CONNECT << 16;
1738 conn = session->leadconn;
1740 reason = FAILURE_SESSION_FREED;
1741 sc->result = DID_NO_CONNECT << 16;
1745 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1746 reason = FAILURE_SESSION_IN_RECOVERY;
1747 sc->result = DID_REQUEUE << 16;
1751 if (iscsi_check_cmdsn_window_closed(conn)) {
1752 reason = FAILURE_WINDOW_CLOSED;
1756 task = iscsi_alloc_task(conn, sc);
1758 reason = FAILURE_OOM;
1762 if (!ihost->workq) {
1763 reason = iscsi_prep_scsi_cmd_pdu(task);
1765 if (reason == -ENOMEM || reason == -EACCES) {
1766 reason = FAILURE_OOM;
1769 sc->result = DID_ABORT << 16;
1773 if (session->tt->xmit_task(task)) {
1775 reason = FAILURE_SESSION_NOT_READY;
1779 list_add_tail(&task->running, &conn->cmdqueue);
1780 iscsi_conn_queue_work(conn);
1783 session->queued_cmdsn++;
1784 spin_unlock_bh(&session->frwd_lock);
1788 spin_lock_bh(&session->back_lock);
1789 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1790 spin_unlock_bh(&session->back_lock);
1792 spin_unlock_bh(&session->frwd_lock);
1793 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1794 sc->cmnd[0], reason);
1795 return SCSI_MLQUEUE_TARGET_BUSY;
1798 spin_lock_bh(&session->back_lock);
1799 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1800 spin_unlock_bh(&session->back_lock);
1802 spin_unlock_bh(&session->frwd_lock);
1803 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1804 sc->cmnd[0], reason);
1805 scsi_set_resid(sc, scsi_bufflen(sc));
1809 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1811 int iscsi_target_alloc(struct scsi_target *starget)
1813 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1814 struct iscsi_session *session = cls_session->dd_data;
1816 starget->can_queue = session->scsi_cmds_max;
1819 EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1821 static void iscsi_tmf_timedout(struct timer_list *t)
1823 struct iscsi_conn *conn = from_timer(conn, t, tmf_timer);
1824 struct iscsi_session *session = conn->session;
1826 spin_lock(&session->frwd_lock);
1827 if (conn->tmf_state == TMF_QUEUED) {
1828 conn->tmf_state = TMF_TIMEDOUT;
1829 ISCSI_DBG_EH(session, "tmf timedout\n");
1830 /* unblock eh_abort() */
1831 wake_up(&conn->ehwait);
1833 spin_unlock(&session->frwd_lock);
1836 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1837 struct iscsi_tm *hdr, int age,
1839 __must_hold(&session->frwd_lock)
1841 struct iscsi_session *session = conn->session;
1842 struct iscsi_task *task;
1844 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1847 spin_unlock_bh(&session->frwd_lock);
1848 iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
1849 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1850 spin_lock_bh(&session->frwd_lock);
1853 conn->tmfcmd_pdus_cnt++;
1854 conn->tmf_timer.expires = timeout * HZ + jiffies;
1855 add_timer(&conn->tmf_timer);
1856 ISCSI_DBG_EH(session, "tmf set timeout\n");
1858 spin_unlock_bh(&session->frwd_lock);
1859 mutex_unlock(&session->eh_mutex);
1862 * block eh thread until:
1866 * 3) session is terminated or restarted or userspace has
1867 * given up on recovery
1869 wait_event_interruptible(conn->ehwait, age != session->age ||
1870 session->state != ISCSI_STATE_LOGGED_IN ||
1871 conn->tmf_state != TMF_QUEUED);
1872 if (signal_pending(current))
1873 flush_signals(current);
1874 del_timer_sync(&conn->tmf_timer);
1876 mutex_lock(&session->eh_mutex);
1877 spin_lock_bh(&session->frwd_lock);
1878 /* if the session drops it will clean up the task */
1879 if (age != session->age ||
1880 session->state != ISCSI_STATE_LOGGED_IN)
1886 * Fail commands. session frwd lock held and xmit thread flushed.
1888 static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
1890 struct iscsi_session *session = conn->session;
1891 struct iscsi_task *task;
1894 spin_lock_bh(&session->back_lock);
1895 for (i = 0; i < session->cmds_max; i++) {
1896 task = session->cmds[i];
1897 if (!task->sc || task->state == ISCSI_TASK_FREE)
1900 if (lun != -1 && lun != task->sc->device->lun)
1903 __iscsi_get_task(task);
1904 spin_unlock_bh(&session->back_lock);
1906 ISCSI_DBG_SESSION(session,
1907 "failing sc %p itt 0x%x state %d\n",
1908 task->sc, task->itt, task->state);
1909 fail_scsi_task(task, error);
1911 spin_unlock_bh(&session->frwd_lock);
1912 iscsi_put_task(task);
1913 spin_lock_bh(&session->frwd_lock);
1915 spin_lock_bh(&session->back_lock);
1918 spin_unlock_bh(&session->back_lock);
1922 * iscsi_suspend_queue - suspend iscsi_queuecommand
1923 * @conn: iscsi conn to stop queueing IO on
1925 * This grabs the session frwd_lock to make sure no one is in
1926 * xmit_task/queuecommand, and then sets suspend to prevent
1927 * new commands from being queued. This only needs to be called
1928 * by offload drivers that need to sync a path like ep disconnect
1929 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1930 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1932 void iscsi_suspend_queue(struct iscsi_conn *conn)
1934 spin_lock_bh(&conn->session->frwd_lock);
1935 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1936 spin_unlock_bh(&conn->session->frwd_lock);
1938 EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1941 * iscsi_suspend_tx - suspend iscsi_data_xmit
1942 * @conn: iscsi conn tp stop processing IO on.
1944 * This function sets the suspend bit to prevent iscsi_data_xmit
1945 * from sending new IO, and if work is queued on the xmit thread
1946 * it will wait for it to be completed.
1948 void iscsi_suspend_tx(struct iscsi_conn *conn)
1950 struct Scsi_Host *shost = conn->session->host;
1951 struct iscsi_host *ihost = shost_priv(shost);
1953 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1955 flush_workqueue(ihost->workq);
1957 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1959 static void iscsi_start_tx(struct iscsi_conn *conn)
1961 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1962 iscsi_conn_queue_work(conn);
1966 * We want to make sure a ping is in flight. It has timed out.
1967 * And we are not busy processing a pdu that is making
1968 * progress but got started before the ping and is taking a while
1969 * to complete so the ping is just stuck behind it in a queue.
1971 static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1973 if (READ_ONCE(conn->ping_task) &&
1974 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1975 (conn->ping_timeout * HZ), jiffies))
1981 enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1983 enum blk_eh_timer_return rc = BLK_EH_DONE;
1984 struct iscsi_task *task = NULL, *running_task;
1985 struct iscsi_cls_session *cls_session;
1986 struct iscsi_session *session;
1987 struct iscsi_conn *conn;
1990 cls_session = starget_to_session(scsi_target(sc->device));
1991 session = cls_session->dd_data;
1993 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1995 spin_lock_bh(&session->frwd_lock);
1996 spin_lock(&session->back_lock);
1997 task = (struct iscsi_task *)sc->SCp.ptr;
2000 * Raced with completion. Blk layer has taken ownership
2001 * so let timeout code complete it now.
2004 spin_unlock(&session->back_lock);
2007 __iscsi_get_task(task);
2008 spin_unlock(&session->back_lock);
2010 if (session->state != ISCSI_STATE_LOGGED_IN) {
2012 * During shutdown, if session is prematurely disconnected,
2013 * recovery won't happen and there will be hung cmds. Not
2014 * handling cmds would trigger EH, also bad in this case.
2015 * Instead, handle cmd, allow completion to happen and let
2016 * upper layer to deal with the result.
2018 if (unlikely(system_state != SYSTEM_RUNNING)) {
2019 sc->result = DID_NO_CONNECT << 16;
2020 ISCSI_DBG_EH(session, "sc on shutdown, handled\n");
2025 * We are probably in the middle of iscsi recovery so let
2026 * that complete and handle the error.
2028 rc = BLK_EH_RESET_TIMER;
2032 conn = session->leadconn;
2034 /* In the middle of shuting down */
2035 rc = BLK_EH_RESET_TIMER;
2040 * If we have sent (at least queued to the network layer) a pdu or
2041 * recvd one for the task since the last timeout ask for
2042 * more time. If on the next timeout we have not made progress
2043 * we can check if it is the task or connection when we send the
2046 if (time_after(task->last_xfer, task->last_timeout)) {
2047 ISCSI_DBG_EH(session, "Command making progress. Asking "
2048 "scsi-ml for more time to complete. "
2049 "Last data xfer at %lu. Last timeout was at "
2050 "%lu\n.", task->last_xfer, task->last_timeout);
2051 task->have_checked_conn = false;
2052 rc = BLK_EH_RESET_TIMER;
2056 if (!conn->recv_timeout && !conn->ping_timeout)
2059 * if the ping timedout then we are in the middle of cleaning up
2060 * and can let the iscsi eh handle it
2062 if (iscsi_has_ping_timed_out(conn)) {
2063 rc = BLK_EH_RESET_TIMER;
2067 spin_lock(&session->back_lock);
2068 for (i = 0; i < conn->session->cmds_max; i++) {
2069 running_task = conn->session->cmds[i];
2070 if (!running_task->sc || running_task == task ||
2071 running_task->state != ISCSI_TASK_RUNNING)
2075 * Only check if cmds started before this one have made
2076 * progress, or this could never fail
2078 if (time_after(running_task->sc->jiffies_at_alloc,
2079 task->sc->jiffies_at_alloc))
2082 if (time_after(running_task->last_xfer, task->last_timeout)) {
2084 * This task has not made progress, but a task
2085 * started before us has transferred data since
2086 * we started/last-checked. We could be queueing
2087 * too many tasks or the LU is bad.
2089 * If the device is bad the cmds ahead of us on
2090 * other devs will complete, and this loop will
2091 * eventually fail starting the scsi eh.
2093 ISCSI_DBG_EH(session, "Command has not made progress "
2094 "but commands ahead of it have. "
2095 "Asking scsi-ml for more time to "
2096 "complete. Our last xfer vs running task "
2097 "last xfer %lu/%lu. Last check %lu.\n",
2098 task->last_xfer, running_task->last_xfer,
2099 task->last_timeout);
2100 spin_unlock(&session->back_lock);
2101 rc = BLK_EH_RESET_TIMER;
2105 spin_unlock(&session->back_lock);
2107 /* Assumes nop timeout is shorter than scsi cmd timeout */
2108 if (task->have_checked_conn)
2112 * Checking the transport already or nop from a cmd timeout still
2115 if (READ_ONCE(conn->ping_task)) {
2116 task->have_checked_conn = true;
2117 rc = BLK_EH_RESET_TIMER;
2121 /* Make sure there is a transport check done */
2122 iscsi_send_nopout(conn, NULL);
2123 task->have_checked_conn = true;
2124 rc = BLK_EH_RESET_TIMER;
2127 spin_unlock_bh(&session->frwd_lock);
2130 task->last_timeout = jiffies;
2131 iscsi_put_task(task);
2133 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2134 "timer reset" : "shutdown or nh");
2137 EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out);
2139 static void iscsi_check_transport_timeouts(struct timer_list *t)
2141 struct iscsi_conn *conn = from_timer(conn, t, transport_timer);
2142 struct iscsi_session *session = conn->session;
2143 unsigned long recv_timeout, next_timeout = 0, last_recv;
2145 spin_lock(&session->frwd_lock);
2146 if (session->state != ISCSI_STATE_LOGGED_IN)
2149 recv_timeout = conn->recv_timeout;
2154 last_recv = conn->last_recv;
2156 if (iscsi_has_ping_timed_out(conn)) {
2157 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
2158 "expired, recv timeout %d, last rx %lu, "
2159 "last ping %lu, now %lu\n",
2160 conn->ping_timeout, conn->recv_timeout,
2161 last_recv, conn->last_ping, jiffies);
2162 spin_unlock(&session->frwd_lock);
2163 iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
2167 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2168 /* send a ping to try to provoke some traffic */
2169 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
2170 if (iscsi_send_nopout(conn, NULL))
2171 next_timeout = jiffies + (1 * HZ);
2173 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2175 next_timeout = last_recv + recv_timeout;
2177 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2178 mod_timer(&conn->transport_timer, next_timeout);
2180 spin_unlock(&session->frwd_lock);
2183 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2184 struct iscsi_tm *hdr)
2186 memset(hdr, 0, sizeof(*hdr));
2187 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2188 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2189 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2190 hdr->lun = task->lun;
2191 hdr->rtt = task->hdr_itt;
2192 hdr->refcmdsn = task->cmdsn;
2195 int iscsi_eh_abort(struct scsi_cmnd *sc)
2197 struct iscsi_cls_session *cls_session;
2198 struct iscsi_session *session;
2199 struct iscsi_conn *conn;
2200 struct iscsi_task *task;
2201 struct iscsi_tm *hdr;
2204 cls_session = starget_to_session(scsi_target(sc->device));
2205 session = cls_session->dd_data;
2207 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
2209 mutex_lock(&session->eh_mutex);
2210 spin_lock_bh(&session->frwd_lock);
2212 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2216 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2218 spin_unlock_bh(&session->frwd_lock);
2219 mutex_unlock(&session->eh_mutex);
2224 * If we are not logged in or we have started a new session
2225 * then let the host reset code handle this
2227 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2228 sc->SCp.phase != session->age) {
2229 spin_unlock_bh(&session->frwd_lock);
2230 mutex_unlock(&session->eh_mutex);
2231 ISCSI_DBG_EH(session, "failing abort due to dropped "
2236 conn = session->leadconn;
2237 conn->eh_abort_cnt++;
2240 spin_lock(&session->back_lock);
2241 task = (struct iscsi_task *)sc->SCp.ptr;
2242 if (!task || !task->sc) {
2243 /* task completed before time out */
2244 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
2246 spin_unlock(&session->back_lock);
2247 spin_unlock_bh(&session->frwd_lock);
2248 mutex_unlock(&session->eh_mutex);
2251 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n", sc, task->itt);
2252 __iscsi_get_task(task);
2253 spin_unlock(&session->back_lock);
2255 if (task->state == ISCSI_TASK_PENDING) {
2256 fail_scsi_task(task, DID_ABORT);
2260 /* only have one tmf outstanding at a time */
2261 if (conn->tmf_state != TMF_INITIAL)
2263 conn->tmf_state = TMF_QUEUED;
2266 iscsi_prep_abort_task_pdu(task, hdr);
2268 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout))
2271 switch (conn->tmf_state) {
2273 spin_unlock_bh(&session->frwd_lock);
2275 * stop tx side incase the target had sent a abort rsp but
2276 * the initiator was still writing out data.
2278 iscsi_suspend_tx(conn);
2280 * we do not stop the recv side because targets have been
2281 * good and have never sent us a successful tmf response
2282 * then sent more data for the cmd.
2284 spin_lock_bh(&session->frwd_lock);
2285 fail_scsi_task(task, DID_ABORT);
2286 conn->tmf_state = TMF_INITIAL;
2287 memset(hdr, 0, sizeof(*hdr));
2288 spin_unlock_bh(&session->frwd_lock);
2289 iscsi_start_tx(conn);
2290 goto success_unlocked;
2292 spin_unlock_bh(&session->frwd_lock);
2293 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2294 goto failed_unlocked;
2297 conn->tmf_state = TMF_INITIAL;
2298 memset(hdr, 0, sizeof(*hdr));
2299 /* task completed before tmf abort response */
2300 ISCSI_DBG_EH(session, "sc completed while abort in "
2306 conn->tmf_state = TMF_INITIAL;
2311 spin_unlock_bh(&session->frwd_lock);
2313 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2315 iscsi_put_task(task);
2316 mutex_unlock(&session->eh_mutex);
2320 spin_unlock_bh(&session->frwd_lock);
2322 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2323 task ? task->itt : 0);
2324 iscsi_put_task(task);
2325 mutex_unlock(&session->eh_mutex);
2328 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2330 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2332 memset(hdr, 0, sizeof(*hdr));
2333 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2334 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2335 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2336 int_to_scsilun(sc->device->lun, &hdr->lun);
2337 hdr->rtt = RESERVED_ITT;
2340 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2342 struct iscsi_cls_session *cls_session;
2343 struct iscsi_session *session;
2344 struct iscsi_conn *conn;
2345 struct iscsi_tm *hdr;
2348 cls_session = starget_to_session(scsi_target(sc->device));
2349 session = cls_session->dd_data;
2351 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc,
2354 mutex_lock(&session->eh_mutex);
2355 spin_lock_bh(&session->frwd_lock);
2357 * Just check if we are not logged in. We cannot check for
2358 * the phase because the reset could come from a ioctl.
2360 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2362 conn = session->leadconn;
2364 /* only have one tmf outstanding at a time */
2365 if (conn->tmf_state != TMF_INITIAL)
2367 conn->tmf_state = TMF_QUEUED;
2370 iscsi_prep_lun_reset_pdu(sc, hdr);
2372 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2373 session->lu_reset_timeout)) {
2378 switch (conn->tmf_state) {
2382 spin_unlock_bh(&session->frwd_lock);
2383 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2386 conn->tmf_state = TMF_INITIAL;
2391 spin_unlock_bh(&session->frwd_lock);
2393 iscsi_suspend_tx(conn);
2395 spin_lock_bh(&session->frwd_lock);
2396 memset(hdr, 0, sizeof(*hdr));
2397 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2398 conn->tmf_state = TMF_INITIAL;
2399 spin_unlock_bh(&session->frwd_lock);
2401 iscsi_start_tx(conn);
2405 spin_unlock_bh(&session->frwd_lock);
2407 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2408 rc == SUCCESS ? "SUCCESS" : "FAILED");
2409 mutex_unlock(&session->eh_mutex);
2412 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2414 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2416 struct iscsi_session *session = cls_session->dd_data;
2418 spin_lock_bh(&session->frwd_lock);
2419 if (session->state != ISCSI_STATE_LOGGED_IN) {
2420 session->state = ISCSI_STATE_RECOVERY_FAILED;
2421 if (session->leadconn)
2422 wake_up(&session->leadconn->ehwait);
2424 spin_unlock_bh(&session->frwd_lock);
2426 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
2429 * iscsi_eh_session_reset - drop session and attempt relogin
2432 * This function will wait for a relogin, session termination from
2433 * userspace, or a recovery/replacement timeout.
2435 int iscsi_eh_session_reset(struct scsi_cmnd *sc)
2437 struct iscsi_cls_session *cls_session;
2438 struct iscsi_session *session;
2439 struct iscsi_conn *conn;
2441 cls_session = starget_to_session(scsi_target(sc->device));
2442 session = cls_session->dd_data;
2443 conn = session->leadconn;
2445 mutex_lock(&session->eh_mutex);
2446 spin_lock_bh(&session->frwd_lock);
2447 if (session->state == ISCSI_STATE_TERMINATE) {
2449 ISCSI_DBG_EH(session,
2450 "failing session reset: Could not log back into "
2451 "%s [age %d]\n", session->targetname,
2453 spin_unlock_bh(&session->frwd_lock);
2454 mutex_unlock(&session->eh_mutex);
2458 spin_unlock_bh(&session->frwd_lock);
2459 mutex_unlock(&session->eh_mutex);
2461 * we drop the lock here but the leadconn cannot be destoyed while
2462 * we are in the scsi eh
2464 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2466 ISCSI_DBG_EH(session, "wait for relogin\n");
2467 wait_event_interruptible(conn->ehwait,
2468 session->state == ISCSI_STATE_TERMINATE ||
2469 session->state == ISCSI_STATE_LOGGED_IN ||
2470 session->state == ISCSI_STATE_RECOVERY_FAILED);
2471 if (signal_pending(current))
2472 flush_signals(current);
2474 mutex_lock(&session->eh_mutex);
2475 spin_lock_bh(&session->frwd_lock);
2476 if (session->state == ISCSI_STATE_LOGGED_IN) {
2477 ISCSI_DBG_EH(session,
2478 "session reset succeeded for %s,%s\n",
2479 session->targetname, conn->persistent_address);
2482 spin_unlock_bh(&session->frwd_lock);
2483 mutex_unlock(&session->eh_mutex);
2486 EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
2488 static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2490 memset(hdr, 0, sizeof(*hdr));
2491 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2492 hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2493 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2494 hdr->rtt = RESERVED_ITT;
2498 * iscsi_eh_target_reset - reset target
2501 * This will attempt to send a warm target reset.
2503 static int iscsi_eh_target_reset(struct scsi_cmnd *sc)
2505 struct iscsi_cls_session *cls_session;
2506 struct iscsi_session *session;
2507 struct iscsi_conn *conn;
2508 struct iscsi_tm *hdr;
2511 cls_session = starget_to_session(scsi_target(sc->device));
2512 session = cls_session->dd_data;
2514 ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
2515 session->targetname);
2517 mutex_lock(&session->eh_mutex);
2518 spin_lock_bh(&session->frwd_lock);
2520 * Just check if we are not logged in. We cannot check for
2521 * the phase because the reset could come from a ioctl.
2523 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2525 conn = session->leadconn;
2527 /* only have one tmf outstanding at a time */
2528 if (conn->tmf_state != TMF_INITIAL)
2530 conn->tmf_state = TMF_QUEUED;
2533 iscsi_prep_tgt_reset_pdu(sc, hdr);
2535 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2536 session->tgt_reset_timeout)) {
2541 switch (conn->tmf_state) {
2545 spin_unlock_bh(&session->frwd_lock);
2546 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2549 conn->tmf_state = TMF_INITIAL;
2554 spin_unlock_bh(&session->frwd_lock);
2556 iscsi_suspend_tx(conn);
2558 spin_lock_bh(&session->frwd_lock);
2559 memset(hdr, 0, sizeof(*hdr));
2560 fail_scsi_tasks(conn, -1, DID_ERROR);
2561 conn->tmf_state = TMF_INITIAL;
2562 spin_unlock_bh(&session->frwd_lock);
2564 iscsi_start_tx(conn);
2568 spin_unlock_bh(&session->frwd_lock);
2570 ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
2571 rc == SUCCESS ? "SUCCESS" : "FAILED");
2572 mutex_unlock(&session->eh_mutex);
2577 * iscsi_eh_recover_target - reset target and possibly the session
2580 * This will attempt to send a warm target reset. If that fails,
2581 * we will escalate to ERL0 session recovery.
2583 int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2587 rc = iscsi_eh_target_reset(sc);
2589 rc = iscsi_eh_session_reset(sc);
2592 EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
2595 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2596 * should be accessed via kfifo_{get,put} on q->queue.
2597 * Optionally, the caller can obtain the array of object pointers
2598 * by passing in a non-NULL @items pointer
2601 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
2603 int i, num_arrays = 1;
2605 memset(q, 0, sizeof(*q));
2609 /* If the user passed an items pointer, he wants a copy of
2613 q->pool = kvcalloc(num_arrays * max, sizeof(void *), GFP_KERNEL);
2614 if (q->pool == NULL)
2617 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
2619 for (i = 0; i < max; i++) {
2620 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2621 if (q->pool[i] == NULL) {
2625 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
2629 *items = q->pool + max;
2630 memcpy(*items, q->pool, max * sizeof(void *));
2639 EXPORT_SYMBOL_GPL(iscsi_pool_init);
2641 void iscsi_pool_free(struct iscsi_pool *q)
2645 for (i = 0; i < q->max; i++)
2649 EXPORT_SYMBOL_GPL(iscsi_pool_free);
2651 int iscsi_host_get_max_scsi_cmds(struct Scsi_Host *shost,
2652 uint16_t requested_cmds_max)
2654 int scsi_cmds, total_cmds = requested_cmds_max;
2658 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2660 * The iscsi layer needs some tasks for nop handling and tmfs,
2661 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2662 * + 1 command for scsi IO.
2664 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2665 printk(KERN_ERR "iscsi: invalid max cmds of %d. Must be a power of two that is at least %d.\n",
2666 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2670 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2671 printk(KERN_INFO "iscsi: invalid max cmds of %d. Must be a power of 2 less than or equal to %d. Using %d.\n",
2672 requested_cmds_max, ISCSI_TOTAL_CMDS_MAX,
2673 ISCSI_TOTAL_CMDS_MAX);
2674 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2677 if (!is_power_of_2(total_cmds)) {
2678 total_cmds = rounddown_pow_of_two(total_cmds);
2679 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2680 printk(KERN_ERR "iscsi: invalid max cmds of %d. Must be a power of 2 greater than %d.\n", requested_cmds_max, ISCSI_TOTAL_CMDS_MIN);
2684 printk(KERN_INFO "iscsi: invalid max cmds %d. Must be a power of 2. Rounding max cmds down to %d.\n",
2685 requested_cmds_max, total_cmds);
2688 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2689 if (shost->can_queue && scsi_cmds > shost->can_queue) {
2690 total_cmds = shost->can_queue;
2692 printk(KERN_INFO "iscsi: requested max cmds %u is higher than driver limit. Using driver limit %u\n",
2693 requested_cmds_max, shost->can_queue);
2699 EXPORT_SYMBOL_GPL(iscsi_host_get_max_scsi_cmds);
2702 * iscsi_host_add - add host to system
2704 * @pdev: parent device
2706 * This should be called by partial offload and software iscsi drivers
2707 * to add a host to the system.
2709 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2711 if (!shost->can_queue)
2712 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2714 if (!shost->cmd_per_lun)
2715 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2717 return scsi_add_host(shost, pdev);
2719 EXPORT_SYMBOL_GPL(iscsi_host_add);
2722 * iscsi_host_alloc - allocate a host and driver data
2723 * @sht: scsi host template
2724 * @dd_data_size: driver host data size
2725 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2727 * This should be called by partial offload and software iscsi drivers.
2728 * To access the driver specific memory use the iscsi_host_priv() macro.
2730 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2731 int dd_data_size, bool xmit_can_sleep)
2733 struct Scsi_Host *shost;
2734 struct iscsi_host *ihost;
2736 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2739 ihost = shost_priv(shost);
2741 if (xmit_can_sleep) {
2742 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2743 "iscsi_q_%d", shost->host_no);
2744 ihost->workq = alloc_workqueue("%s",
2745 WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND,
2746 1, ihost->workq_name);
2751 spin_lock_init(&ihost->lock);
2752 ihost->state = ISCSI_HOST_SETUP;
2753 ihost->num_sessions = 0;
2754 init_waitqueue_head(&ihost->session_removal_wq);
2758 scsi_host_put(shost);
2761 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2763 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2765 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2769 * iscsi_host_remove - remove host and sessions
2772 * If there are any sessions left, this will initiate the removal and wait
2773 * for the completion.
2775 void iscsi_host_remove(struct Scsi_Host *shost)
2777 struct iscsi_host *ihost = shost_priv(shost);
2778 unsigned long flags;
2780 spin_lock_irqsave(&ihost->lock, flags);
2781 ihost->state = ISCSI_HOST_REMOVED;
2782 spin_unlock_irqrestore(&ihost->lock, flags);
2784 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2785 wait_event_interruptible(ihost->session_removal_wq,
2786 ihost->num_sessions == 0);
2787 if (signal_pending(current))
2788 flush_signals(current);
2790 scsi_remove_host(shost);
2792 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2794 void iscsi_host_free(struct Scsi_Host *shost)
2796 struct iscsi_host *ihost = shost_priv(shost);
2799 destroy_workqueue(ihost->workq);
2801 kfree(ihost->netdev);
2802 kfree(ihost->hwaddress);
2803 kfree(ihost->initiatorname);
2804 scsi_host_put(shost);
2806 EXPORT_SYMBOL_GPL(iscsi_host_free);
2808 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2810 struct iscsi_host *ihost = shost_priv(shost);
2811 unsigned long flags;
2813 shost = scsi_host_get(shost);
2815 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2816 "of session teardown event because host already "
2821 spin_lock_irqsave(&ihost->lock, flags);
2822 ihost->num_sessions--;
2823 if (ihost->num_sessions == 0)
2824 wake_up(&ihost->session_removal_wq);
2825 spin_unlock_irqrestore(&ihost->lock, flags);
2826 scsi_host_put(shost);
2830 * iscsi_session_setup - create iscsi cls session and host and session
2831 * @iscsit: iscsi transport template
2833 * @cmds_max: session can queue
2834 * @dd_size: private driver data size, added to session allocation size
2835 * @cmd_task_size: LLD task private data size
2836 * @initial_cmdsn: initial CmdSN
2837 * @id: target ID to add to this session
2839 * This can be used by software iscsi_transports that allocate
2840 * a session per scsi host.
2842 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2843 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2844 * for nop handling and login/logout requests.
2846 struct iscsi_cls_session *
2847 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2848 uint16_t cmds_max, int dd_size, int cmd_task_size,
2849 uint32_t initial_cmdsn, unsigned int id)
2851 struct iscsi_host *ihost = shost_priv(shost);
2852 struct iscsi_session *session;
2853 struct iscsi_cls_session *cls_session;
2854 int cmd_i, scsi_cmds;
2855 unsigned long flags;
2857 spin_lock_irqsave(&ihost->lock, flags);
2858 if (ihost->state == ISCSI_HOST_REMOVED) {
2859 spin_unlock_irqrestore(&ihost->lock, flags);
2862 ihost->num_sessions++;
2863 spin_unlock_irqrestore(&ihost->lock, flags);
2865 scsi_cmds = iscsi_host_get_max_scsi_cmds(shost, cmds_max);
2867 goto dec_session_count;
2869 cls_session = iscsi_alloc_session(shost, iscsit,
2870 sizeof(struct iscsi_session) +
2873 goto dec_session_count;
2874 session = cls_session->dd_data;
2875 session->cls_session = cls_session;
2876 session->host = shost;
2877 session->state = ISCSI_STATE_FREE;
2878 session->fast_abort = 1;
2879 session->tgt_reset_timeout = 30;
2880 session->lu_reset_timeout = 15;
2881 session->abort_timeout = 10;
2882 session->scsi_cmds_max = scsi_cmds;
2883 session->cmds_max = scsi_cmds + ISCSI_MGMT_CMDS_MAX;
2884 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2885 session->exp_cmdsn = initial_cmdsn + 1;
2886 session->max_cmdsn = initial_cmdsn + 1;
2887 session->max_r2t = 1;
2888 session->tt = iscsit;
2889 session->dd_data = cls_session->dd_data + sizeof(*session);
2891 mutex_init(&session->eh_mutex);
2892 spin_lock_init(&session->frwd_lock);
2893 spin_lock_init(&session->back_lock);
2895 /* initialize SCSI PDU commands pool */
2896 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2897 (void***)&session->cmds,
2898 cmd_task_size + sizeof(struct iscsi_task)))
2899 goto cmdpool_alloc_fail;
2901 /* pre-format cmds pool with ITT */
2902 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2903 struct iscsi_task *task = session->cmds[cmd_i];
2906 task->dd_data = &task[1];
2908 task->state = ISCSI_TASK_FREE;
2909 INIT_LIST_HEAD(&task->running);
2912 if (!try_module_get(iscsit->owner))
2913 goto module_get_fail;
2915 if (iscsi_add_session(cls_session, id))
2916 goto cls_session_fail;
2921 module_put(iscsit->owner);
2923 iscsi_pool_free(&session->cmdpool);
2925 iscsi_free_session(cls_session);
2927 iscsi_host_dec_session_cnt(shost);
2930 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2933 * iscsi_session_teardown - destroy session, host, and cls_session
2934 * @cls_session: iscsi session
2936 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2938 struct iscsi_session *session = cls_session->dd_data;
2939 struct module *owner = cls_session->transport->owner;
2940 struct Scsi_Host *shost = session->host;
2942 iscsi_pool_free(&session->cmdpool);
2944 iscsi_remove_session(cls_session);
2946 kfree(session->password);
2947 kfree(session->password_in);
2948 kfree(session->username);
2949 kfree(session->username_in);
2950 kfree(session->targetname);
2951 kfree(session->targetalias);
2952 kfree(session->initiatorname);
2953 kfree(session->boot_root);
2954 kfree(session->boot_nic);
2955 kfree(session->boot_target);
2956 kfree(session->ifacename);
2957 kfree(session->portal_type);
2958 kfree(session->discovery_parent_type);
2960 iscsi_free_session(cls_session);
2962 iscsi_host_dec_session_cnt(shost);
2965 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2968 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2969 * @cls_session: iscsi_cls_session
2970 * @dd_size: private driver data size
2973 struct iscsi_cls_conn *
2974 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2977 struct iscsi_session *session = cls_session->dd_data;
2978 struct iscsi_conn *conn;
2979 struct iscsi_cls_conn *cls_conn;
2982 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2986 conn = cls_conn->dd_data;
2987 memset(conn, 0, sizeof(*conn) + dd_size);
2989 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2990 conn->session = session;
2991 conn->cls_conn = cls_conn;
2992 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2993 conn->id = conn_idx;
2994 conn->exp_statsn = 0;
2995 conn->tmf_state = TMF_INITIAL;
2997 timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0);
2999 INIT_LIST_HEAD(&conn->mgmtqueue);
3000 INIT_LIST_HEAD(&conn->cmdqueue);
3001 INIT_LIST_HEAD(&conn->requeue);
3002 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
3004 /* allocate login_task used for the login/text sequences */
3005 spin_lock_bh(&session->frwd_lock);
3006 if (!kfifo_out(&session->cmdpool.queue,
3007 (void*)&conn->login_task,
3009 spin_unlock_bh(&session->frwd_lock);
3010 goto login_task_alloc_fail;
3012 spin_unlock_bh(&session->frwd_lock);
3014 data = (char *) __get_free_pages(GFP_KERNEL,
3015 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
3017 goto login_task_data_alloc_fail;
3018 conn->login_task->data = conn->data = data;
3020 timer_setup(&conn->tmf_timer, iscsi_tmf_timedout, 0);
3021 init_waitqueue_head(&conn->ehwait);
3025 login_task_data_alloc_fail:
3026 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
3028 login_task_alloc_fail:
3029 iscsi_destroy_conn(cls_conn);
3032 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
3035 * iscsi_conn_teardown - teardown iscsi connection
3036 * @cls_conn: iscsi class connection
3038 * TODO: we may need to make this into a two step process
3039 * like scsi-mls remove + put host
3041 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
3043 struct iscsi_conn *conn = cls_conn->dd_data;
3044 struct iscsi_session *session = conn->session;
3046 del_timer_sync(&conn->transport_timer);
3048 mutex_lock(&session->eh_mutex);
3049 spin_lock_bh(&session->frwd_lock);
3050 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
3051 if (session->leadconn == conn) {
3053 * leading connection? then give up on recovery.
3055 session->state = ISCSI_STATE_TERMINATE;
3056 wake_up(&conn->ehwait);
3058 spin_unlock_bh(&session->frwd_lock);
3060 /* flush queued up work because we free the connection below */
3061 iscsi_suspend_tx(conn);
3063 spin_lock_bh(&session->frwd_lock);
3064 free_pages((unsigned long) conn->data,
3065 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
3066 kfree(conn->persistent_address);
3067 kfree(conn->local_ipaddr);
3068 /* regular RX path uses back_lock */
3069 spin_lock_bh(&session->back_lock);
3070 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
3072 spin_unlock_bh(&session->back_lock);
3073 if (session->leadconn == conn)
3074 session->leadconn = NULL;
3075 spin_unlock_bh(&session->frwd_lock);
3076 mutex_unlock(&session->eh_mutex);
3078 iscsi_destroy_conn(cls_conn);
3080 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
3082 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
3084 struct iscsi_conn *conn = cls_conn->dd_data;
3085 struct iscsi_session *session = conn->session;
3088 iscsi_conn_printk(KERN_ERR, conn,
3089 "can't start unbound connection\n");
3093 if ((session->imm_data_en || !session->initial_r2t_en) &&
3094 session->first_burst > session->max_burst) {
3095 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
3096 "first_burst %d max_burst %d\n",
3097 session->first_burst, session->max_burst);
3101 if (conn->ping_timeout && !conn->recv_timeout) {
3102 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
3103 "zero. Using 5 seconds\n.");
3104 conn->recv_timeout = 5;
3107 if (conn->recv_timeout && !conn->ping_timeout) {
3108 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3109 "zero. Using 5 seconds.\n");
3110 conn->ping_timeout = 5;
3113 spin_lock_bh(&session->frwd_lock);
3114 conn->c_stage = ISCSI_CONN_STARTED;
3115 session->state = ISCSI_STATE_LOGGED_IN;
3116 session->queued_cmdsn = session->cmdsn;
3118 conn->last_recv = jiffies;
3119 conn->last_ping = jiffies;
3120 if (conn->recv_timeout && conn->ping_timeout)
3121 mod_timer(&conn->transport_timer,
3122 jiffies + (conn->recv_timeout * HZ));
3124 switch(conn->stop_stage) {
3125 case STOP_CONN_RECOVER:
3127 * unblock eh_abort() if it is blocked. re-try all
3128 * commands after successful recovery
3130 conn->stop_stage = 0;
3131 conn->tmf_state = TMF_INITIAL;
3133 if (session->age == 16)
3136 case STOP_CONN_TERM:
3137 conn->stop_stage = 0;
3142 spin_unlock_bh(&session->frwd_lock);
3144 iscsi_unblock_session(session->cls_session);
3145 wake_up(&conn->ehwait);
3148 EXPORT_SYMBOL_GPL(iscsi_conn_start);
3151 fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
3153 struct iscsi_task *task;
3156 for (i = 0; i < conn->session->cmds_max; i++) {
3157 task = conn->session->cmds[i];
3161 if (task->state == ISCSI_TASK_FREE)
3164 ISCSI_DBG_SESSION(conn->session,
3165 "failing mgmt itt 0x%x state %d\n",
3166 task->itt, task->state);
3168 spin_lock_bh(&session->back_lock);
3169 if (cleanup_queued_task(task)) {
3170 spin_unlock_bh(&session->back_lock);
3174 state = ISCSI_TASK_ABRT_SESS_RECOV;
3175 if (task->state == ISCSI_TASK_PENDING)
3176 state = ISCSI_TASK_COMPLETED;
3177 iscsi_complete_task(task, state);
3178 spin_unlock_bh(&session->back_lock);
3182 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3184 struct iscsi_conn *conn = cls_conn->dd_data;
3185 struct iscsi_session *session = conn->session;
3188 mutex_lock(&session->eh_mutex);
3189 spin_lock_bh(&session->frwd_lock);
3190 if (conn->stop_stage == STOP_CONN_TERM) {
3191 spin_unlock_bh(&session->frwd_lock);
3192 mutex_unlock(&session->eh_mutex);
3197 * When this is called for the in_login state, we only want to clean
3198 * up the login task and connection. We do not need to block and set
3199 * the recovery state again
3201 if (flag == STOP_CONN_TERM)
3202 session->state = ISCSI_STATE_TERMINATE;
3203 else if (conn->stop_stage != STOP_CONN_RECOVER)
3204 session->state = ISCSI_STATE_IN_RECOVERY;
3206 old_stop_stage = conn->stop_stage;
3207 conn->stop_stage = flag;
3208 spin_unlock_bh(&session->frwd_lock);
3210 del_timer_sync(&conn->transport_timer);
3211 iscsi_suspend_tx(conn);
3213 spin_lock_bh(&session->frwd_lock);
3214 conn->c_stage = ISCSI_CONN_STOPPED;
3215 spin_unlock_bh(&session->frwd_lock);
3218 * for connection level recovery we should not calculate
3219 * header digest. conn->hdr_size used for optimization
3220 * in hdr_extract() and will be re-negotiated at
3223 if (flag == STOP_CONN_RECOVER) {
3224 conn->hdrdgst_en = 0;
3225 conn->datadgst_en = 0;
3226 if (session->state == ISCSI_STATE_IN_RECOVERY &&
3227 old_stop_stage != STOP_CONN_RECOVER) {
3228 ISCSI_DBG_SESSION(session, "blocking session\n");
3229 iscsi_block_session(session->cls_session);
3236 spin_lock_bh(&session->frwd_lock);
3237 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
3238 fail_mgmt_tasks(session, conn);
3239 memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
3240 spin_unlock_bh(&session->frwd_lock);
3241 mutex_unlock(&session->eh_mutex);
3243 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3245 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3246 struct iscsi_cls_conn *cls_conn, int is_leading)
3248 struct iscsi_session *session = cls_session->dd_data;
3249 struct iscsi_conn *conn = cls_conn->dd_data;
3251 spin_lock_bh(&session->frwd_lock);
3253 session->leadconn = conn;
3254 spin_unlock_bh(&session->frwd_lock);
3257 * The target could have reduced it's window size between logins, so
3258 * we have to reset max/exp cmdsn so we can see the new values.
3260 spin_lock_bh(&session->back_lock);
3261 session->max_cmdsn = session->exp_cmdsn = session->cmdsn + 1;
3262 spin_unlock_bh(&session->back_lock);
3264 * Unblock xmitworker(), Login Phase will pass through.
3266 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
3267 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
3270 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3272 int iscsi_switch_str_param(char **param, char *new_val_buf)
3277 if (!strcmp(*param, new_val_buf))
3281 new_val = kstrdup(new_val_buf, GFP_NOIO);
3289 EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
3291 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3292 enum iscsi_param param, char *buf, int buflen)
3294 struct iscsi_conn *conn = cls_conn->dd_data;
3295 struct iscsi_session *session = conn->session;
3299 case ISCSI_PARAM_FAST_ABORT:
3300 sscanf(buf, "%d", &session->fast_abort);
3302 case ISCSI_PARAM_ABORT_TMO:
3303 sscanf(buf, "%d", &session->abort_timeout);
3305 case ISCSI_PARAM_LU_RESET_TMO:
3306 sscanf(buf, "%d", &session->lu_reset_timeout);
3308 case ISCSI_PARAM_TGT_RESET_TMO:
3309 sscanf(buf, "%d", &session->tgt_reset_timeout);
3311 case ISCSI_PARAM_PING_TMO:
3312 sscanf(buf, "%d", &conn->ping_timeout);
3314 case ISCSI_PARAM_RECV_TMO:
3315 sscanf(buf, "%d", &conn->recv_timeout);
3317 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3318 sscanf(buf, "%d", &conn->max_recv_dlength);
3320 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3321 sscanf(buf, "%d", &conn->max_xmit_dlength);
3323 case ISCSI_PARAM_HDRDGST_EN:
3324 sscanf(buf, "%d", &conn->hdrdgst_en);
3326 case ISCSI_PARAM_DATADGST_EN:
3327 sscanf(buf, "%d", &conn->datadgst_en);
3329 case ISCSI_PARAM_INITIAL_R2T_EN:
3330 sscanf(buf, "%d", &session->initial_r2t_en);
3332 case ISCSI_PARAM_MAX_R2T:
3333 sscanf(buf, "%hu", &session->max_r2t);
3335 case ISCSI_PARAM_IMM_DATA_EN:
3336 sscanf(buf, "%d", &session->imm_data_en);
3338 case ISCSI_PARAM_FIRST_BURST:
3339 sscanf(buf, "%d", &session->first_burst);
3341 case ISCSI_PARAM_MAX_BURST:
3342 sscanf(buf, "%d", &session->max_burst);
3344 case ISCSI_PARAM_PDU_INORDER_EN:
3345 sscanf(buf, "%d", &session->pdu_inorder_en);
3347 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3348 sscanf(buf, "%d", &session->dataseq_inorder_en);
3350 case ISCSI_PARAM_ERL:
3351 sscanf(buf, "%d", &session->erl);
3353 case ISCSI_PARAM_EXP_STATSN:
3354 sscanf(buf, "%u", &conn->exp_statsn);
3356 case ISCSI_PARAM_USERNAME:
3357 return iscsi_switch_str_param(&session->username, buf);
3358 case ISCSI_PARAM_USERNAME_IN:
3359 return iscsi_switch_str_param(&session->username_in, buf);
3360 case ISCSI_PARAM_PASSWORD:
3361 return iscsi_switch_str_param(&session->password, buf);
3362 case ISCSI_PARAM_PASSWORD_IN:
3363 return iscsi_switch_str_param(&session->password_in, buf);
3364 case ISCSI_PARAM_TARGET_NAME:
3365 return iscsi_switch_str_param(&session->targetname, buf);
3366 case ISCSI_PARAM_TARGET_ALIAS:
3367 return iscsi_switch_str_param(&session->targetalias, buf);
3368 case ISCSI_PARAM_TPGT:
3369 sscanf(buf, "%d", &session->tpgt);
3371 case ISCSI_PARAM_PERSISTENT_PORT:
3372 sscanf(buf, "%d", &conn->persistent_port);
3374 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3375 return iscsi_switch_str_param(&conn->persistent_address, buf);
3376 case ISCSI_PARAM_IFACE_NAME:
3377 return iscsi_switch_str_param(&session->ifacename, buf);
3378 case ISCSI_PARAM_INITIATOR_NAME:
3379 return iscsi_switch_str_param(&session->initiatorname, buf);
3380 case ISCSI_PARAM_BOOT_ROOT:
3381 return iscsi_switch_str_param(&session->boot_root, buf);
3382 case ISCSI_PARAM_BOOT_NIC:
3383 return iscsi_switch_str_param(&session->boot_nic, buf);
3384 case ISCSI_PARAM_BOOT_TARGET:
3385 return iscsi_switch_str_param(&session->boot_target, buf);
3386 case ISCSI_PARAM_PORTAL_TYPE:
3387 return iscsi_switch_str_param(&session->portal_type, buf);
3388 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3389 return iscsi_switch_str_param(&session->discovery_parent_type,
3391 case ISCSI_PARAM_DISCOVERY_SESS:
3392 sscanf(buf, "%d", &val);
3393 session->discovery_sess = !!val;
3395 case ISCSI_PARAM_LOCAL_IPADDR:
3396 return iscsi_switch_str_param(&conn->local_ipaddr, buf);
3403 EXPORT_SYMBOL_GPL(iscsi_set_param);
3405 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3406 enum iscsi_param param, char *buf)
3408 struct iscsi_session *session = cls_session->dd_data;
3412 case ISCSI_PARAM_FAST_ABORT:
3413 len = sysfs_emit(buf, "%d\n", session->fast_abort);
3415 case ISCSI_PARAM_ABORT_TMO:
3416 len = sysfs_emit(buf, "%d\n", session->abort_timeout);
3418 case ISCSI_PARAM_LU_RESET_TMO:
3419 len = sysfs_emit(buf, "%d\n", session->lu_reset_timeout);
3421 case ISCSI_PARAM_TGT_RESET_TMO:
3422 len = sysfs_emit(buf, "%d\n", session->tgt_reset_timeout);
3424 case ISCSI_PARAM_INITIAL_R2T_EN:
3425 len = sysfs_emit(buf, "%d\n", session->initial_r2t_en);
3427 case ISCSI_PARAM_MAX_R2T:
3428 len = sysfs_emit(buf, "%hu\n", session->max_r2t);
3430 case ISCSI_PARAM_IMM_DATA_EN:
3431 len = sysfs_emit(buf, "%d\n", session->imm_data_en);
3433 case ISCSI_PARAM_FIRST_BURST:
3434 len = sysfs_emit(buf, "%u\n", session->first_burst);
3436 case ISCSI_PARAM_MAX_BURST:
3437 len = sysfs_emit(buf, "%u\n", session->max_burst);
3439 case ISCSI_PARAM_PDU_INORDER_EN:
3440 len = sysfs_emit(buf, "%d\n", session->pdu_inorder_en);
3442 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3443 len = sysfs_emit(buf, "%d\n", session->dataseq_inorder_en);
3445 case ISCSI_PARAM_DEF_TASKMGMT_TMO:
3446 len = sysfs_emit(buf, "%d\n", session->def_taskmgmt_tmo);
3448 case ISCSI_PARAM_ERL:
3449 len = sysfs_emit(buf, "%d\n", session->erl);
3451 case ISCSI_PARAM_TARGET_NAME:
3452 len = sysfs_emit(buf, "%s\n", session->targetname);
3454 case ISCSI_PARAM_TARGET_ALIAS:
3455 len = sysfs_emit(buf, "%s\n", session->targetalias);
3457 case ISCSI_PARAM_TPGT:
3458 len = sysfs_emit(buf, "%d\n", session->tpgt);
3460 case ISCSI_PARAM_USERNAME:
3461 len = sysfs_emit(buf, "%s\n", session->username);
3463 case ISCSI_PARAM_USERNAME_IN:
3464 len = sysfs_emit(buf, "%s\n", session->username_in);
3466 case ISCSI_PARAM_PASSWORD:
3467 len = sysfs_emit(buf, "%s\n", session->password);
3469 case ISCSI_PARAM_PASSWORD_IN:
3470 len = sysfs_emit(buf, "%s\n", session->password_in);
3472 case ISCSI_PARAM_IFACE_NAME:
3473 len = sysfs_emit(buf, "%s\n", session->ifacename);
3475 case ISCSI_PARAM_INITIATOR_NAME:
3476 len = sysfs_emit(buf, "%s\n", session->initiatorname);
3478 case ISCSI_PARAM_BOOT_ROOT:
3479 len = sysfs_emit(buf, "%s\n", session->boot_root);
3481 case ISCSI_PARAM_BOOT_NIC:
3482 len = sysfs_emit(buf, "%s\n", session->boot_nic);
3484 case ISCSI_PARAM_BOOT_TARGET:
3485 len = sysfs_emit(buf, "%s\n", session->boot_target);
3487 case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
3488 len = sysfs_emit(buf, "%u\n", session->auto_snd_tgt_disable);
3490 case ISCSI_PARAM_DISCOVERY_SESS:
3491 len = sysfs_emit(buf, "%u\n", session->discovery_sess);
3493 case ISCSI_PARAM_PORTAL_TYPE:
3494 len = sysfs_emit(buf, "%s\n", session->portal_type);
3496 case ISCSI_PARAM_CHAP_AUTH_EN:
3497 len = sysfs_emit(buf, "%u\n", session->chap_auth_en);
3499 case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
3500 len = sysfs_emit(buf, "%u\n", session->discovery_logout_en);
3502 case ISCSI_PARAM_BIDI_CHAP_EN:
3503 len = sysfs_emit(buf, "%u\n", session->bidi_chap_en);
3505 case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
3506 len = sysfs_emit(buf, "%u\n", session->discovery_auth_optional);
3508 case ISCSI_PARAM_DEF_TIME2WAIT:
3509 len = sysfs_emit(buf, "%d\n", session->time2wait);
3511 case ISCSI_PARAM_DEF_TIME2RETAIN:
3512 len = sysfs_emit(buf, "%d\n", session->time2retain);
3514 case ISCSI_PARAM_TSID:
3515 len = sysfs_emit(buf, "%u\n", session->tsid);
3517 case ISCSI_PARAM_ISID:
3518 len = sysfs_emit(buf, "%02x%02x%02x%02x%02x%02x\n",
3519 session->isid[0], session->isid[1],
3520 session->isid[2], session->isid[3],
3521 session->isid[4], session->isid[5]);
3523 case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
3524 len = sysfs_emit(buf, "%u\n", session->discovery_parent_idx);
3526 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3527 if (session->discovery_parent_type)
3528 len = sysfs_emit(buf, "%s\n",
3529 session->discovery_parent_type);
3531 len = sysfs_emit(buf, "\n");
3539 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3541 int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
3542 enum iscsi_param param, char *buf)
3544 struct sockaddr_in6 *sin6 = NULL;
3545 struct sockaddr_in *sin = NULL;
3548 switch (addr->ss_family) {
3550 sin = (struct sockaddr_in *)addr;
3553 sin6 = (struct sockaddr_in6 *)addr;
3560 case ISCSI_PARAM_CONN_ADDRESS:
3561 case ISCSI_HOST_PARAM_IPADDRESS:
3563 len = sysfs_emit(buf, "%pI4\n", &sin->sin_addr.s_addr);
3565 len = sysfs_emit(buf, "%pI6\n", &sin6->sin6_addr);
3567 case ISCSI_PARAM_CONN_PORT:
3568 case ISCSI_PARAM_LOCAL_PORT:
3570 len = sysfs_emit(buf, "%hu\n", be16_to_cpu(sin->sin_port));
3572 len = sysfs_emit(buf, "%hu\n",
3573 be16_to_cpu(sin6->sin6_port));
3581 EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
3583 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3584 enum iscsi_param param, char *buf)
3586 struct iscsi_conn *conn = cls_conn->dd_data;
3590 case ISCSI_PARAM_PING_TMO:
3591 len = sysfs_emit(buf, "%u\n", conn->ping_timeout);
3593 case ISCSI_PARAM_RECV_TMO:
3594 len = sysfs_emit(buf, "%u\n", conn->recv_timeout);
3596 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3597 len = sysfs_emit(buf, "%u\n", conn->max_recv_dlength);
3599 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3600 len = sysfs_emit(buf, "%u\n", conn->max_xmit_dlength);
3602 case ISCSI_PARAM_HDRDGST_EN:
3603 len = sysfs_emit(buf, "%d\n", conn->hdrdgst_en);
3605 case ISCSI_PARAM_DATADGST_EN:
3606 len = sysfs_emit(buf, "%d\n", conn->datadgst_en);
3608 case ISCSI_PARAM_IFMARKER_EN:
3609 len = sysfs_emit(buf, "%d\n", conn->ifmarker_en);
3611 case ISCSI_PARAM_OFMARKER_EN:
3612 len = sysfs_emit(buf, "%d\n", conn->ofmarker_en);
3614 case ISCSI_PARAM_EXP_STATSN:
3615 len = sysfs_emit(buf, "%u\n", conn->exp_statsn);
3617 case ISCSI_PARAM_PERSISTENT_PORT:
3618 len = sysfs_emit(buf, "%d\n", conn->persistent_port);
3620 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3621 len = sysfs_emit(buf, "%s\n", conn->persistent_address);
3623 case ISCSI_PARAM_STATSN:
3624 len = sysfs_emit(buf, "%u\n", conn->statsn);
3626 case ISCSI_PARAM_MAX_SEGMENT_SIZE:
3627 len = sysfs_emit(buf, "%u\n", conn->max_segment_size);
3629 case ISCSI_PARAM_KEEPALIVE_TMO:
3630 len = sysfs_emit(buf, "%u\n", conn->keepalive_tmo);
3632 case ISCSI_PARAM_LOCAL_PORT:
3633 len = sysfs_emit(buf, "%u\n", conn->local_port);
3635 case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
3636 len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_stat);
3638 case ISCSI_PARAM_TCP_NAGLE_DISABLE:
3639 len = sysfs_emit(buf, "%u\n", conn->tcp_nagle_disable);
3641 case ISCSI_PARAM_TCP_WSF_DISABLE:
3642 len = sysfs_emit(buf, "%u\n", conn->tcp_wsf_disable);
3644 case ISCSI_PARAM_TCP_TIMER_SCALE:
3645 len = sysfs_emit(buf, "%u\n", conn->tcp_timer_scale);
3647 case ISCSI_PARAM_TCP_TIMESTAMP_EN:
3648 len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_en);
3650 case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
3651 len = sysfs_emit(buf, "%u\n", conn->fragment_disable);
3653 case ISCSI_PARAM_IPV4_TOS:
3654 len = sysfs_emit(buf, "%u\n", conn->ipv4_tos);
3656 case ISCSI_PARAM_IPV6_TC:
3657 len = sysfs_emit(buf, "%u\n", conn->ipv6_traffic_class);
3659 case ISCSI_PARAM_IPV6_FLOW_LABEL:
3660 len = sysfs_emit(buf, "%u\n", conn->ipv6_flow_label);
3662 case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
3663 len = sysfs_emit(buf, "%u\n", conn->is_fw_assigned_ipv6);
3665 case ISCSI_PARAM_TCP_XMIT_WSF:
3666 len = sysfs_emit(buf, "%u\n", conn->tcp_xmit_wsf);
3668 case ISCSI_PARAM_TCP_RECV_WSF:
3669 len = sysfs_emit(buf, "%u\n", conn->tcp_recv_wsf);
3671 case ISCSI_PARAM_LOCAL_IPADDR:
3672 len = sysfs_emit(buf, "%s\n", conn->local_ipaddr);
3680 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3682 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3685 struct iscsi_host *ihost = shost_priv(shost);
3689 case ISCSI_HOST_PARAM_NETDEV_NAME:
3690 len = sysfs_emit(buf, "%s\n", ihost->netdev);
3692 case ISCSI_HOST_PARAM_HWADDRESS:
3693 len = sysfs_emit(buf, "%s\n", ihost->hwaddress);
3695 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3696 len = sysfs_emit(buf, "%s\n", ihost->initiatorname);
3704 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3706 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3707 char *buf, int buflen)
3709 struct iscsi_host *ihost = shost_priv(shost);
3712 case ISCSI_HOST_PARAM_NETDEV_NAME:
3713 return iscsi_switch_str_param(&ihost->netdev, buf);
3714 case ISCSI_HOST_PARAM_HWADDRESS:
3715 return iscsi_switch_str_param(&ihost->hwaddress, buf);
3716 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3717 return iscsi_switch_str_param(&ihost->initiatorname, buf);
3724 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3726 MODULE_AUTHOR("Mike Christie");
3727 MODULE_DESCRIPTION("iSCSI library functions");
3728 MODULE_LICENSE("GPL");