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
9 * maintained by open-iscsi@googlegroups.com
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_session *session = task->conn->session;
234 struct iscsi_tm *tmf = &session->tmhdr;
237 if (session->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_session_printk(KERN_INFO, session,
258 "task [op %x itt 0x%x/0x%x] rejected.\n",
259 opcode, task->itt, task->hdr_itt);
263 * And also all data-out PDUs in response to R2T
264 * if fast_abort is set.
266 if (session->fast_abort) {
267 iscsi_session_printk(KERN_INFO, session,
268 "task [op %x itt 0x%x/0x%x] fast abort.\n",
269 opcode, task->itt, task->hdr_itt);
273 case ISCSI_TM_FUNC_ABORT_TASK:
275 * the caller has already checked if the task
276 * they want to abort was in the pending queue so if
277 * we are here the cmd pdu has gone out already, and
278 * we will only hit this for data-outs
280 if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
281 task->hdr_itt == tmf->rtt) {
282 ISCSI_DBG_SESSION(session,
283 "Preventing task %x/%x from sending "
284 "data-out due to abort task in "
285 "progress\n", task->itt,
296 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
299 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
300 * fields like dlength or final based on how much data it sends
302 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
304 struct iscsi_conn *conn = task->conn;
305 struct iscsi_session *session = conn->session;
306 struct scsi_cmnd *sc = task->sc;
307 struct iscsi_scsi_req *hdr;
308 unsigned hdrlength, cmd_len, transfer_length;
312 rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
316 if (conn->session->tt->alloc_pdu) {
317 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
321 hdr = (struct iscsi_scsi_req *)task->hdr;
323 memset(hdr, 0, sizeof(*hdr));
325 if (session->tt->parse_pdu_itt)
326 hdr->itt = task->hdr_itt = itt;
328 hdr->itt = task->hdr_itt = build_itt(task->itt,
329 task->conn->session->age);
331 rc = iscsi_add_hdr(task, sizeof(*hdr));
334 hdr->opcode = ISCSI_OP_SCSI_CMD;
335 hdr->flags = ISCSI_ATTR_SIMPLE;
336 int_to_scsilun(sc->device->lun, &hdr->lun);
337 task->lun = hdr->lun;
338 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
339 cmd_len = sc->cmd_len;
340 if (cmd_len < ISCSI_CDB_SIZE)
341 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
342 else if (cmd_len > ISCSI_CDB_SIZE) {
343 rc = iscsi_prep_ecdb_ahs(task);
346 cmd_len = ISCSI_CDB_SIZE;
348 memcpy(hdr->cdb, sc->cmnd, cmd_len);
351 if (scsi_get_prot_op(sc) != SCSI_PROT_NORMAL)
352 task->protected = true;
354 transfer_length = scsi_transfer_length(sc);
355 hdr->data_length = cpu_to_be32(transfer_length);
356 if (sc->sc_data_direction == DMA_TO_DEVICE) {
357 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
359 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
363 * imm_count bytes to be sent right after
366 * unsol_count bytes(as Data-Out) to be sent
367 * without R2T ack right after
370 * r2t data_length bytes to be sent via R2T ack's
372 * pad_count bytes to be sent as zero-padding
374 memset(r2t, 0, sizeof(*r2t));
376 if (session->imm_data_en) {
377 if (transfer_length >= session->first_burst)
378 task->imm_count = min(session->first_burst,
379 conn->max_xmit_dlength);
381 task->imm_count = min(transfer_length,
382 conn->max_xmit_dlength);
383 hton24(hdr->dlength, task->imm_count);
385 zero_data(hdr->dlength);
387 if (!session->initial_r2t_en) {
388 r2t->data_length = min(session->first_burst,
391 r2t->data_offset = task->imm_count;
392 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
393 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
396 if (!task->unsol_r2t.data_length)
397 /* No unsolicit Data-Out's */
398 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
400 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
401 zero_data(hdr->dlength);
403 if (sc->sc_data_direction == DMA_FROM_DEVICE)
404 hdr->flags |= ISCSI_FLAG_CMD_READ;
407 /* calculate size of additional header segments (AHSs) */
408 hdrlength = task->hdr_len - sizeof(*hdr);
410 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
411 hdrlength /= ISCSI_PAD_LEN;
413 WARN_ON(hdrlength >= 256);
414 hdr->hlength = hdrlength & 0xFF;
415 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
417 if (session->tt->init_task && session->tt->init_task(task))
420 task->state = ISCSI_TASK_RUNNING;
423 conn->scsicmd_pdus_cnt++;
424 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
425 "itt 0x%x len %d cmdsn %d win %d]\n",
426 sc->sc_data_direction == DMA_TO_DEVICE ?
427 "write" : "read", conn->id, sc, sc->cmnd[0],
428 task->itt, transfer_length,
430 session->max_cmdsn - session->exp_cmdsn + 1);
435 * iscsi_free_task - free a task
436 * @task: iscsi cmd task
438 * Must be called with session back_lock.
439 * This function returns the scsi command to scsi-ml or cleans
440 * up mgmt tasks then returns the task to the pool.
442 static void iscsi_free_task(struct iscsi_task *task)
444 struct iscsi_conn *conn = task->conn;
445 struct iscsi_session *session = conn->session;
446 struct scsi_cmnd *sc = task->sc;
447 int oldstate = task->state;
449 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
450 task->itt, task->state, task->sc);
452 session->tt->cleanup_task(task);
453 task->state = ISCSI_TASK_FREE;
456 * login task is preallocated so do not free
458 if (conn->login_task == task)
461 kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
464 /* SCSI eh reuses commands to verify us */
465 iscsi_cmd(sc)->task = NULL;
467 * queue command may call this to free the task, so
468 * it will decide how to return sc to scsi-ml.
470 if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
475 void __iscsi_get_task(struct iscsi_task *task)
477 refcount_inc(&task->refcount);
479 EXPORT_SYMBOL_GPL(__iscsi_get_task);
481 void __iscsi_put_task(struct iscsi_task *task)
483 if (refcount_dec_and_test(&task->refcount))
484 iscsi_free_task(task);
486 EXPORT_SYMBOL_GPL(__iscsi_put_task);
488 void iscsi_put_task(struct iscsi_task *task)
490 struct iscsi_session *session = task->conn->session;
492 /* regular RX path uses back_lock */
493 spin_lock_bh(&session->back_lock);
494 __iscsi_put_task(task);
495 spin_unlock_bh(&session->back_lock);
497 EXPORT_SYMBOL_GPL(iscsi_put_task);
500 * iscsi_complete_task - finish a task
501 * @task: iscsi cmd task
502 * @state: state to complete task with
504 * Must be called with session back_lock.
506 static void iscsi_complete_task(struct iscsi_task *task, int state)
508 struct iscsi_conn *conn = task->conn;
510 ISCSI_DBG_SESSION(conn->session,
511 "complete task itt 0x%x state %d sc %p\n",
512 task->itt, task->state, task->sc);
513 if (task->state == ISCSI_TASK_COMPLETED ||
514 task->state == ISCSI_TASK_ABRT_TMF ||
515 task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
516 task->state == ISCSI_TASK_REQUEUE_SCSIQ)
518 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
521 if (READ_ONCE(conn->ping_task) == task)
522 WRITE_ONCE(conn->ping_task, NULL);
524 /* release get from queueing */
525 __iscsi_put_task(task);
529 * iscsi_complete_scsi_task - finish scsi task normally
530 * @task: iscsi task for scsi cmd
531 * @exp_cmdsn: expected cmd sn in cpu format
532 * @max_cmdsn: max cmd sn in cpu format
534 * This is used when drivers do not need or cannot perform
535 * lower level pdu processing.
537 * Called with session back_lock
539 void iscsi_complete_scsi_task(struct iscsi_task *task,
540 uint32_t exp_cmdsn, uint32_t max_cmdsn)
542 struct iscsi_conn *conn = task->conn;
544 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
546 conn->last_recv = jiffies;
547 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
548 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
550 EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
553 * Must be called with back and frwd lock
555 static bool cleanup_queued_task(struct iscsi_task *task)
557 struct iscsi_conn *conn = task->conn;
558 bool early_complete = false;
560 /* Bad target might have completed task while it was still running */
561 if (task->state == ISCSI_TASK_COMPLETED)
562 early_complete = true;
564 if (!list_empty(&task->running)) {
565 list_del_init(&task->running);
567 * If it's on a list but still running, this could be from
568 * a bad target sending a rsp early, cleanup from a TMF, or
571 if (task->state == ISCSI_TASK_RUNNING ||
572 task->state == ISCSI_TASK_COMPLETED)
573 __iscsi_put_task(task);
576 if (conn->session->running_aborted_task == task) {
577 conn->session->running_aborted_task = NULL;
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 ||
682 !test_bit(ISCSI_CONN_FLAG_BOUND, &conn->flags))
685 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
687 * Login and Text are sent serially, in
688 * request-followed-by-response sequence.
689 * Same task can be used. Same ITT must be used.
690 * Note that login_task is preallocated at conn_create().
692 if (conn->login_task->state != ISCSI_TASK_FREE) {
693 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
694 "progress. Cannot start new task.\n");
698 if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) {
699 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);
703 task = conn->login_task;
705 if (session->state != ISCSI_STATE_LOGGED_IN)
708 if (data_size != 0) {
709 iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode);
713 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
714 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
716 if (!kfifo_out(&session->cmdpool.queue,
717 (void*)&task, sizeof(void*)))
721 * released in complete pdu for task we expect a response for, and
722 * released by the lld when it has transmitted the task for
723 * pdus we do not expect a response for.
725 refcount_set(&task->refcount, 1);
728 INIT_LIST_HEAD(&task->running);
729 task->state = ISCSI_TASK_PENDING;
732 memcpy(task->data, data, data_size);
733 task->data_count = data_size;
735 task->data_count = 0;
737 if (conn->session->tt->alloc_pdu) {
738 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
739 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
740 "pdu for mgmt task.\n");
745 itt = task->hdr->itt;
746 task->hdr_len = sizeof(struct iscsi_hdr);
747 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
749 if (hdr->itt != RESERVED_ITT) {
750 if (session->tt->parse_pdu_itt)
751 task->hdr->itt = itt;
753 task->hdr->itt = build_itt(task->itt,
754 task->conn->session->age);
757 if (unlikely(READ_ONCE(conn->ping_task) == INVALID_SCSI_TASK))
758 WRITE_ONCE(conn->ping_task, task);
761 if (iscsi_prep_mgmt_task(conn, task))
764 if (session->tt->xmit_task(task))
767 list_add_tail(&task->running, &conn->mgmtqueue);
768 iscsi_conn_queue_work(conn);
774 /* regular RX path uses back_lock */
775 spin_lock(&session->back_lock);
776 __iscsi_put_task(task);
777 spin_unlock(&session->back_lock);
781 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
782 char *data, uint32_t data_size)
784 struct iscsi_conn *conn = cls_conn->dd_data;
785 struct iscsi_session *session = conn->session;
788 spin_lock_bh(&session->frwd_lock);
789 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
791 spin_unlock_bh(&session->frwd_lock);
794 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
797 * iscsi_scsi_cmd_rsp - SCSI Command Response processing
798 * @conn: iscsi connection
800 * @task: scsi command task
801 * @data: cmd data buffer
802 * @datalen: len of buffer
804 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
805 * then completes the command and task. called under back_lock
807 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
808 struct iscsi_task *task, char *data,
811 struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
812 struct iscsi_session *session = conn->session;
813 struct scsi_cmnd *sc = task->sc;
815 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
816 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
818 sc->result = (DID_OK << 16) | rhdr->cmd_status;
820 if (task->protected) {
825 * Transports that didn't implement check_protection
826 * callback but still published T10-PI support to scsi-mid
827 * deserve this BUG_ON.
829 BUG_ON(!session->tt->check_protection);
831 ascq = session->tt->check_protection(task, §or);
833 scsi_build_sense(sc, 1, ILLEGAL_REQUEST, 0x10, ascq);
834 scsi_set_sense_information(sc->sense_buffer,
835 SCSI_SENSE_BUFFERSIZE,
841 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
842 sc->result = DID_ERROR << 16;
846 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
851 iscsi_conn_printk(KERN_ERR, conn,
852 "Got CHECK_CONDITION but invalid data "
853 "buffer size of %d\n", datalen);
854 sc->result = DID_BAD_TARGET << 16;
858 senselen = get_unaligned_be16(data);
859 if (datalen < senselen)
860 goto invalid_datalen;
862 memcpy(sc->sense_buffer, data + 2,
863 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
864 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
865 min_t(uint16_t, senselen,
866 SCSI_SENSE_BUFFERSIZE));
869 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
870 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
871 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
874 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
875 ISCSI_FLAG_CMD_OVERFLOW)) {
876 int res_count = be32_to_cpu(rhdr->residual_count);
879 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
880 res_count <= scsi_bufflen(sc)))
881 /* write side for bidi or uni-io set_resid */
882 scsi_set_resid(sc, res_count);
884 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
887 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
888 sc, sc->result, task->itt);
889 conn->scsirsp_pdus_cnt++;
890 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
894 * iscsi_data_in_rsp - SCSI Data-In Response processing
895 * @conn: iscsi connection
897 * @task: scsi command task
899 * iscsi_data_in_rsp sets up the scsi_cmnd fields based on the data received
900 * then completes the command and task. called under back_lock
903 iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
904 struct iscsi_task *task)
906 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
907 struct scsi_cmnd *sc = task->sc;
909 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
912 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
913 sc->result = (DID_OK << 16) | rhdr->cmd_status;
914 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
915 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
916 ISCSI_FLAG_DATA_OVERFLOW)) {
917 int res_count = be32_to_cpu(rhdr->residual_count);
920 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
921 res_count <= sc->sdb.length))
922 scsi_set_resid(sc, res_count);
924 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
927 ISCSI_DBG_SESSION(conn->session, "data in with status done "
928 "[sc %p res %d itt 0x%x]\n",
929 sc, sc->result, task->itt);
930 conn->scsirsp_pdus_cnt++;
931 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
934 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
936 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
937 struct iscsi_session *session = conn->session;
939 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
940 conn->tmfrsp_pdus_cnt++;
942 if (session->tmf_state != TMF_QUEUED)
945 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
946 session->tmf_state = TMF_SUCCESS;
947 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
948 session->tmf_state = TMF_NOT_FOUND;
950 session->tmf_state = TMF_FAILED;
951 wake_up(&session->ehwait);
954 static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
956 struct iscsi_nopout hdr;
957 struct iscsi_task *task;
960 if (READ_ONCE(conn->ping_task))
962 WRITE_ONCE(conn->ping_task, INVALID_SCSI_TASK);
965 memset(&hdr, 0, sizeof(struct iscsi_nopout));
966 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
967 hdr.flags = ISCSI_FLAG_CMD_FINAL;
972 hdr.itt = RESERVED_ITT;
974 hdr.ttt = RESERVED_ITT;
976 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
979 WRITE_ONCE(conn->ping_task, NULL);
980 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
983 /* only track our nops */
984 conn->last_ping = jiffies;
991 * iscsi_nop_out_rsp - SCSI NOP Response processing
992 * @task: scsi command task
993 * @nop: the nop structure
994 * @data: where to put the data
995 * @datalen: length of data
997 * iscsi_nop_out_rsp handles nop response from use or
998 * from user space. called under back_lock
1000 static int iscsi_nop_out_rsp(struct iscsi_task *task,
1001 struct iscsi_nopin *nop, char *data, int datalen)
1003 struct iscsi_conn *conn = task->conn;
1006 if (READ_ONCE(conn->ping_task) != task) {
1008 * If this is not in response to one of our
1009 * nops then it must be from userspace.
1011 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
1013 rc = ISCSI_ERR_CONN_FAILED;
1015 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
1016 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1020 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1021 char *data, int datalen)
1023 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
1024 struct iscsi_hdr rejected_pdu;
1027 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
1029 if (ntoh24(reject->dlength) > datalen ||
1030 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
1031 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
1032 "pdu. Invalid data length (pdu dlength "
1033 "%u, datalen %d\n", ntoh24(reject->dlength),
1035 return ISCSI_ERR_PROTO;
1037 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
1038 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
1040 switch (reject->reason) {
1041 case ISCSI_REASON_DATA_DIGEST_ERROR:
1042 iscsi_conn_printk(KERN_ERR, conn,
1043 "pdu (op 0x%x itt 0x%x) rejected "
1044 "due to DataDigest error.\n",
1045 opcode, rejected_pdu.itt);
1047 case ISCSI_REASON_IMM_CMD_REJECT:
1048 iscsi_conn_printk(KERN_ERR, conn,
1049 "pdu (op 0x%x itt 0x%x) rejected. Too many "
1050 "immediate commands.\n",
1051 opcode, rejected_pdu.itt);
1053 * We only send one TMF at a time so if the target could not
1054 * handle it, then it should get fixed (RFC mandates that
1055 * a target can handle one immediate TMF per conn).
1057 * For nops-outs, we could have sent more than one if
1058 * the target is sending us lots of nop-ins
1060 if (opcode != ISCSI_OP_NOOP_OUT)
1063 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1065 * nop-out in response to target's nop-out rejected.
1068 /* In RX path we are under back lock */
1069 spin_unlock(&conn->session->back_lock);
1070 spin_lock(&conn->session->frwd_lock);
1071 iscsi_send_nopout(conn,
1072 (struct iscsi_nopin*)&rejected_pdu);
1073 spin_unlock(&conn->session->frwd_lock);
1074 spin_lock(&conn->session->back_lock);
1076 struct iscsi_task *task;
1078 * Our nop as ping got dropped. We know the target
1079 * and transport are ok so just clean up
1081 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
1083 iscsi_conn_printk(KERN_ERR, conn,
1084 "Invalid pdu reject. Could "
1085 "not lookup rejected task.\n");
1086 rc = ISCSI_ERR_BAD_ITT;
1088 rc = iscsi_nop_out_rsp(task,
1089 (struct iscsi_nopin*)&rejected_pdu,
1094 iscsi_conn_printk(KERN_ERR, conn,
1095 "pdu (op 0x%x itt 0x%x) rejected. Reason "
1096 "code 0x%x\n", rejected_pdu.opcode,
1097 rejected_pdu.itt, reject->reason);
1104 * iscsi_itt_to_task - look up task by itt
1105 * @conn: iscsi connection
1108 * This should be used for mgmt tasks like login and nops, or if
1109 * the LDD's itt space does not include the session age.
1111 * The session back_lock must be held.
1113 struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
1115 struct iscsi_session *session = conn->session;
1118 if (itt == RESERVED_ITT)
1121 if (session->tt->parse_pdu_itt)
1122 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1125 if (i >= session->cmds_max)
1128 return session->cmds[i];
1130 EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
1133 * __iscsi_complete_pdu - complete pdu
1135 * @hdr: iscsi header
1136 * @data: data buffer
1137 * @datalen: len of data buffer
1139 * Completes pdu processing by freeing any resources allocated at
1140 * queuecommand or send generic. session back_lock must be held and verify
1141 * itt must have been called.
1143 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1144 char *data, int datalen)
1146 struct iscsi_session *session = conn->session;
1147 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
1148 struct iscsi_task *task;
1151 conn->last_recv = jiffies;
1152 rc = iscsi_verify_itt(conn, hdr->itt);
1156 if (hdr->itt != RESERVED_ITT)
1157 itt = get_itt(hdr->itt);
1161 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1162 opcode, conn->id, itt, datalen);
1165 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1168 case ISCSI_OP_NOOP_IN:
1170 rc = ISCSI_ERR_PROTO;
1174 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
1177 /* In RX path we are under back lock */
1178 spin_unlock(&session->back_lock);
1179 spin_lock(&session->frwd_lock);
1180 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
1181 spin_unlock(&session->frwd_lock);
1182 spin_lock(&session->back_lock);
1184 case ISCSI_OP_REJECT:
1185 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1187 case ISCSI_OP_ASYNC_EVENT:
1188 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1189 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1190 rc = ISCSI_ERR_CONN_FAILED;
1193 rc = ISCSI_ERR_BAD_OPCODE;
1200 case ISCSI_OP_SCSI_CMD_RSP:
1201 case ISCSI_OP_SCSI_DATA_IN:
1202 task = iscsi_itt_to_ctask(conn, hdr->itt);
1204 return ISCSI_ERR_BAD_ITT;
1205 task->last_xfer = jiffies;
1209 * LLD handles R2Ts if they need to.
1212 case ISCSI_OP_LOGOUT_RSP:
1213 case ISCSI_OP_LOGIN_RSP:
1214 case ISCSI_OP_TEXT_RSP:
1215 case ISCSI_OP_SCSI_TMFUNC_RSP:
1216 case ISCSI_OP_NOOP_IN:
1217 task = iscsi_itt_to_task(conn, hdr->itt);
1219 return ISCSI_ERR_BAD_ITT;
1222 return ISCSI_ERR_BAD_OPCODE;
1226 case ISCSI_OP_SCSI_CMD_RSP:
1227 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
1229 case ISCSI_OP_SCSI_DATA_IN:
1230 iscsi_data_in_rsp(conn, hdr, task);
1232 case ISCSI_OP_LOGOUT_RSP:
1233 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1235 rc = ISCSI_ERR_PROTO;
1238 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1240 case ISCSI_OP_LOGIN_RSP:
1241 case ISCSI_OP_TEXT_RSP:
1242 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1244 * login related PDU's exp_statsn is handled in
1248 case ISCSI_OP_SCSI_TMFUNC_RSP:
1249 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1251 rc = ISCSI_ERR_PROTO;
1255 iscsi_tmf_rsp(conn, hdr);
1256 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1258 case ISCSI_OP_NOOP_IN:
1259 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1260 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1261 rc = ISCSI_ERR_PROTO;
1264 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1266 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1270 rc = ISCSI_ERR_BAD_OPCODE;
1277 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1278 rc = ISCSI_ERR_CONN_FAILED;
1279 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1282 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
1284 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1285 char *data, int datalen)
1289 spin_lock(&conn->session->back_lock);
1290 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1291 spin_unlock(&conn->session->back_lock);
1294 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1296 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
1298 struct iscsi_session *session = conn->session;
1301 if (itt == RESERVED_ITT)
1304 if (session->tt->parse_pdu_itt)
1305 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1308 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1311 if (age != session->age) {
1312 iscsi_conn_printk(KERN_ERR, conn,
1313 "received itt %x expected session age (%x)\n",
1314 (__force u32)itt, session->age);
1315 return ISCSI_ERR_BAD_ITT;
1318 if (i >= session->cmds_max) {
1319 iscsi_conn_printk(KERN_ERR, conn,
1320 "received invalid itt index %u (max cmds "
1321 "%u.\n", i, session->cmds_max);
1322 return ISCSI_ERR_BAD_ITT;
1326 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1329 * iscsi_itt_to_ctask - look up ctask by itt
1330 * @conn: iscsi connection
1333 * This should be used for cmd tasks.
1335 * The session back_lock must be held.
1337 struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
1339 struct iscsi_task *task;
1341 if (iscsi_verify_itt(conn, itt))
1344 task = iscsi_itt_to_task(conn, itt);
1345 if (!task || !task->sc)
1348 if (iscsi_cmd(task->sc)->age != conn->session->age) {
1349 iscsi_session_printk(KERN_ERR, conn->session,
1350 "task's session age %d, expected %d\n",
1351 iscsi_cmd(task->sc)->age, conn->session->age);
1357 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1359 void iscsi_session_failure(struct iscsi_session *session,
1362 struct iscsi_conn *conn;
1364 spin_lock_bh(&session->frwd_lock);
1365 conn = session->leadconn;
1366 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1367 spin_unlock_bh(&session->frwd_lock);
1371 iscsi_get_conn(conn->cls_conn);
1372 spin_unlock_bh(&session->frwd_lock);
1374 * if the host is being removed bypass the connection
1375 * recovery initialization because we are going to kill
1378 if (err == ISCSI_ERR_INVALID_HOST)
1379 iscsi_conn_error_event(conn->cls_conn, err);
1381 iscsi_conn_failure(conn, err);
1382 iscsi_put_conn(conn->cls_conn);
1384 EXPORT_SYMBOL_GPL(iscsi_session_failure);
1386 static bool iscsi_set_conn_failed(struct iscsi_conn *conn)
1388 struct iscsi_session *session = conn->session;
1390 if (session->state == ISCSI_STATE_FAILED)
1393 if (conn->stop_stage == 0)
1394 session->state = ISCSI_STATE_FAILED;
1396 set_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags);
1397 set_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags);
1401 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1403 struct iscsi_session *session = conn->session;
1406 spin_lock_bh(&session->frwd_lock);
1407 needs_evt = iscsi_set_conn_failed(conn);
1408 spin_unlock_bh(&session->frwd_lock);
1411 iscsi_conn_error_event(conn->cls_conn, err);
1413 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1415 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1417 struct iscsi_session *session = conn->session;
1420 * Check for iSCSI window and take care of CmdSN wrap-around
1422 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1423 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1424 "%u MaxCmdSN %u CmdSN %u/%u\n",
1425 session->exp_cmdsn, session->max_cmdsn,
1426 session->cmdsn, session->queued_cmdsn);
1432 static int iscsi_xmit_task(struct iscsi_conn *conn, struct iscsi_task *task,
1437 spin_lock_bh(&conn->session->back_lock);
1440 /* Take a ref so we can access it after xmit_task() */
1441 __iscsi_get_task(task);
1443 /* Already have a ref from when we failed to send it last call */
1448 * If this was a requeue for a R2T we have an extra ref on the task in
1449 * case a bad target sends a cmd rsp before we have handled the task.
1452 __iscsi_put_task(task);
1455 * Do this after dropping the extra ref because if this was a requeue
1456 * it's removed from that list and cleanup_queued_task would miss it.
1458 if (test_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags)) {
1460 * Save the task and ref in case we weren't cleaning up this
1461 * task and get woken up again.
1464 spin_unlock_bh(&conn->session->back_lock);
1467 spin_unlock_bh(&conn->session->back_lock);
1469 spin_unlock_bh(&conn->session->frwd_lock);
1470 rc = conn->session->tt->xmit_task(task);
1471 spin_lock_bh(&conn->session->frwd_lock);
1473 /* done with this task */
1474 task->last_xfer = jiffies;
1476 /* regular RX path uses back_lock */
1477 spin_lock(&conn->session->back_lock);
1478 if (rc && task->state == ISCSI_TASK_RUNNING) {
1480 * get an extra ref that is released next time we access it
1481 * as conn->task above.
1483 __iscsi_get_task(task);
1487 __iscsi_put_task(task);
1488 spin_unlock(&conn->session->back_lock);
1493 * iscsi_requeue_task - requeue task to run from session workqueue
1494 * @task: task to requeue
1496 * Callers must have taken a ref to the task that is going to be requeued.
1498 void iscsi_requeue_task(struct iscsi_task *task)
1500 struct iscsi_conn *conn = task->conn;
1503 * this may be on the requeue list already if the xmit_task callout
1504 * is handling the r2ts while we are adding new ones
1506 spin_lock_bh(&conn->session->frwd_lock);
1507 if (list_empty(&task->running)) {
1508 list_add_tail(&task->running, &conn->requeue);
1511 * Don't need the extra ref since it's already requeued and
1514 iscsi_put_task(task);
1516 iscsi_conn_queue_work(conn);
1517 spin_unlock_bh(&conn->session->frwd_lock);
1519 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1522 * iscsi_data_xmit - xmit any command into the scheduled connection
1523 * @conn: iscsi connection
1526 * The function can return -EAGAIN in which case the caller must
1527 * re-schedule it again later or recover. '0' return code means
1530 static int iscsi_data_xmit(struct iscsi_conn *conn)
1532 struct iscsi_task *task;
1535 spin_lock_bh(&conn->session->frwd_lock);
1536 if (test_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags)) {
1537 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1538 spin_unlock_bh(&conn->session->frwd_lock);
1543 rc = iscsi_xmit_task(conn, conn->task, false);
1549 * process mgmt pdus like nops before commands since we should
1550 * only have one nop-out as a ping from us and targets should not
1551 * overflow us with nop-ins
1554 while (!list_empty(&conn->mgmtqueue)) {
1555 task = list_entry(conn->mgmtqueue.next, struct iscsi_task,
1557 list_del_init(&task->running);
1558 if (iscsi_prep_mgmt_task(conn, task)) {
1559 /* regular RX path uses back_lock */
1560 spin_lock_bh(&conn->session->back_lock);
1561 __iscsi_put_task(task);
1562 spin_unlock_bh(&conn->session->back_lock);
1565 rc = iscsi_xmit_task(conn, task, false);
1570 /* process pending command queue */
1571 while (!list_empty(&conn->cmdqueue)) {
1572 task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1574 list_del_init(&task->running);
1575 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1576 fail_scsi_task(task, DID_IMM_RETRY);
1579 rc = iscsi_prep_scsi_cmd_pdu(task);
1581 if (rc == -ENOMEM || rc == -EACCES)
1582 fail_scsi_task(task, DID_IMM_RETRY);
1584 fail_scsi_task(task, DID_ABORT);
1587 rc = iscsi_xmit_task(conn, task, false);
1591 * we could continuously get new task requests so
1592 * we need to check the mgmt queue for nops that need to
1593 * be sent to aviod starvation
1595 if (!list_empty(&conn->mgmtqueue))
1599 while (!list_empty(&conn->requeue)) {
1601 * we always do fastlogout - conn stop code will clean up.
1603 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1606 task = list_entry(conn->requeue.next, struct iscsi_task,
1609 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1612 list_del_init(&task->running);
1613 rc = iscsi_xmit_task(conn, task, true);
1616 if (!list_empty(&conn->mgmtqueue))
1619 spin_unlock_bh(&conn->session->frwd_lock);
1623 spin_unlock_bh(&conn->session->frwd_lock);
1627 static void iscsi_xmitworker(struct work_struct *work)
1629 struct iscsi_conn *conn =
1630 container_of(work, struct iscsi_conn, xmitwork);
1633 * serialize Xmit worker on a per-connection basis.
1636 rc = iscsi_data_xmit(conn);
1637 } while (rc >= 0 || rc == -EAGAIN);
1640 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1641 struct scsi_cmnd *sc)
1643 struct iscsi_task *task;
1645 if (!kfifo_out(&conn->session->cmdpool.queue,
1646 (void *) &task, sizeof(void *)))
1649 iscsi_cmd(sc)->age = conn->session->age;
1650 iscsi_cmd(sc)->task = task;
1652 refcount_set(&task->refcount, 1);
1653 task->state = ISCSI_TASK_PENDING;
1656 task->have_checked_conn = false;
1657 task->last_timeout = jiffies;
1658 task->last_xfer = jiffies;
1659 task->protected = false;
1660 INIT_LIST_HEAD(&task->running);
1665 FAILURE_BAD_HOST = 1,
1666 FAILURE_SESSION_FAILED,
1667 FAILURE_SESSION_FREED,
1668 FAILURE_WINDOW_CLOSED,
1670 FAILURE_SESSION_TERMINATE,
1671 FAILURE_SESSION_IN_RECOVERY,
1672 FAILURE_SESSION_RECOVERY_TIMEOUT,
1673 FAILURE_SESSION_LOGGING_OUT,
1674 FAILURE_SESSION_NOT_READY,
1677 int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
1679 struct iscsi_cls_session *cls_session;
1680 struct iscsi_host *ihost;
1682 struct iscsi_session *session;
1683 struct iscsi_conn *conn;
1684 struct iscsi_task *task = NULL;
1687 iscsi_cmd(sc)->task = NULL;
1689 ihost = shost_priv(host);
1691 cls_session = starget_to_session(scsi_target(sc->device));
1692 session = cls_session->dd_data;
1693 spin_lock_bh(&session->frwd_lock);
1695 reason = iscsi_session_chkready(cls_session);
1697 sc->result = reason;
1701 if (session->state != ISCSI_STATE_LOGGED_IN) {
1703 * to handle the race between when we set the recovery state
1704 * and block the session we requeue here (commands could
1705 * be entering our queuecommand while a block is starting
1706 * up because the block code is not locked)
1708 switch (session->state) {
1709 case ISCSI_STATE_FAILED:
1711 * cmds should fail during shutdown, if the session
1712 * state is bad, allowing completion to happen
1714 if (unlikely(system_state != SYSTEM_RUNNING)) {
1715 reason = FAILURE_SESSION_FAILED;
1716 sc->result = DID_NO_CONNECT << 16;
1720 case ISCSI_STATE_IN_RECOVERY:
1721 reason = FAILURE_SESSION_IN_RECOVERY;
1722 sc->result = DID_IMM_RETRY << 16;
1724 case ISCSI_STATE_LOGGING_OUT:
1725 reason = FAILURE_SESSION_LOGGING_OUT;
1726 sc->result = DID_IMM_RETRY << 16;
1728 case ISCSI_STATE_RECOVERY_FAILED:
1729 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1730 sc->result = DID_TRANSPORT_FAILFAST << 16;
1732 case ISCSI_STATE_TERMINATE:
1733 reason = FAILURE_SESSION_TERMINATE;
1734 sc->result = DID_NO_CONNECT << 16;
1737 reason = FAILURE_SESSION_FREED;
1738 sc->result = DID_NO_CONNECT << 16;
1743 conn = session->leadconn;
1745 reason = FAILURE_SESSION_FREED;
1746 sc->result = DID_NO_CONNECT << 16;
1750 if (test_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags)) {
1751 reason = FAILURE_SESSION_IN_RECOVERY;
1752 sc->result = DID_REQUEUE << 16;
1756 if (iscsi_check_cmdsn_window_closed(conn)) {
1757 reason = FAILURE_WINDOW_CLOSED;
1761 task = iscsi_alloc_task(conn, sc);
1763 reason = FAILURE_OOM;
1767 if (!ihost->workq) {
1768 reason = iscsi_prep_scsi_cmd_pdu(task);
1770 if (reason == -ENOMEM || reason == -EACCES) {
1771 reason = FAILURE_OOM;
1774 sc->result = DID_ABORT << 16;
1778 if (session->tt->xmit_task(task)) {
1780 reason = FAILURE_SESSION_NOT_READY;
1784 list_add_tail(&task->running, &conn->cmdqueue);
1785 iscsi_conn_queue_work(conn);
1788 session->queued_cmdsn++;
1789 spin_unlock_bh(&session->frwd_lock);
1793 spin_lock_bh(&session->back_lock);
1794 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1795 spin_unlock_bh(&session->back_lock);
1797 spin_unlock_bh(&session->frwd_lock);
1798 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1799 sc->cmnd[0], reason);
1800 return SCSI_MLQUEUE_TARGET_BUSY;
1803 spin_lock_bh(&session->back_lock);
1804 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1805 spin_unlock_bh(&session->back_lock);
1807 spin_unlock_bh(&session->frwd_lock);
1808 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1809 sc->cmnd[0], reason);
1810 scsi_set_resid(sc, scsi_bufflen(sc));
1814 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1816 int iscsi_target_alloc(struct scsi_target *starget)
1818 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1819 struct iscsi_session *session = cls_session->dd_data;
1821 starget->can_queue = session->scsi_cmds_max;
1824 EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1826 static void iscsi_tmf_timedout(struct timer_list *t)
1828 struct iscsi_session *session = from_timer(session, t, tmf_timer);
1830 spin_lock(&session->frwd_lock);
1831 if (session->tmf_state == TMF_QUEUED) {
1832 session->tmf_state = TMF_TIMEDOUT;
1833 ISCSI_DBG_EH(session, "tmf timedout\n");
1834 /* unblock eh_abort() */
1835 wake_up(&session->ehwait);
1837 spin_unlock(&session->frwd_lock);
1840 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1841 struct iscsi_tm *hdr, int age,
1843 __must_hold(&session->frwd_lock)
1845 struct iscsi_session *session = conn->session;
1846 struct iscsi_task *task;
1848 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1851 spin_unlock_bh(&session->frwd_lock);
1852 iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
1853 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1854 spin_lock_bh(&session->frwd_lock);
1857 conn->tmfcmd_pdus_cnt++;
1858 session->tmf_timer.expires = timeout * HZ + jiffies;
1859 add_timer(&session->tmf_timer);
1860 ISCSI_DBG_EH(session, "tmf set timeout\n");
1862 spin_unlock_bh(&session->frwd_lock);
1863 mutex_unlock(&session->eh_mutex);
1866 * block eh thread until:
1870 * 3) session is terminated or restarted or userspace has
1871 * given up on recovery
1873 wait_event_interruptible(session->ehwait, age != session->age ||
1874 session->state != ISCSI_STATE_LOGGED_IN ||
1875 session->tmf_state != TMF_QUEUED);
1876 if (signal_pending(current))
1877 flush_signals(current);
1878 del_timer_sync(&session->tmf_timer);
1880 mutex_lock(&session->eh_mutex);
1881 spin_lock_bh(&session->frwd_lock);
1882 /* if the session drops it will clean up the task */
1883 if (age != session->age ||
1884 session->state != ISCSI_STATE_LOGGED_IN)
1890 * Fail commands. session frwd lock held and xmit thread flushed.
1892 static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
1894 struct iscsi_session *session = conn->session;
1895 struct iscsi_task *task;
1898 spin_lock_bh(&session->back_lock);
1899 for (i = 0; i < session->cmds_max; i++) {
1900 task = session->cmds[i];
1901 if (!task->sc || task->state == ISCSI_TASK_FREE)
1904 if (lun != -1 && lun != task->sc->device->lun)
1907 __iscsi_get_task(task);
1908 spin_unlock_bh(&session->back_lock);
1910 ISCSI_DBG_SESSION(session,
1911 "failing sc %p itt 0x%x state %d\n",
1912 task->sc, task->itt, task->state);
1913 fail_scsi_task(task, error);
1915 spin_unlock_bh(&session->frwd_lock);
1916 iscsi_put_task(task);
1917 spin_lock_bh(&session->frwd_lock);
1919 spin_lock_bh(&session->back_lock);
1922 spin_unlock_bh(&session->back_lock);
1926 * iscsi_suspend_queue - suspend iscsi_queuecommand
1927 * @conn: iscsi conn to stop queueing IO on
1929 * This grabs the session frwd_lock to make sure no one is in
1930 * xmit_task/queuecommand, and then sets suspend to prevent
1931 * new commands from being queued. This only needs to be called
1932 * by offload drivers that need to sync a path like ep disconnect
1933 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1934 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1936 void iscsi_suspend_queue(struct iscsi_conn *conn)
1938 spin_lock_bh(&conn->session->frwd_lock);
1939 set_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags);
1940 spin_unlock_bh(&conn->session->frwd_lock);
1942 EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1945 * iscsi_suspend_tx - suspend iscsi_data_xmit
1946 * @conn: iscsi conn tp stop processing IO on.
1948 * This function sets the suspend bit to prevent iscsi_data_xmit
1949 * from sending new IO, and if work is queued on the xmit thread
1950 * it will wait for it to be completed.
1952 void iscsi_suspend_tx(struct iscsi_conn *conn)
1954 struct Scsi_Host *shost = conn->session->host;
1955 struct iscsi_host *ihost = shost_priv(shost);
1957 set_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags);
1959 flush_workqueue(ihost->workq);
1961 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1963 static void iscsi_start_tx(struct iscsi_conn *conn)
1965 clear_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags);
1966 iscsi_conn_queue_work(conn);
1970 * We want to make sure a ping is in flight. It has timed out.
1971 * And we are not busy processing a pdu that is making
1972 * progress but got started before the ping and is taking a while
1973 * to complete so the ping is just stuck behind it in a queue.
1975 static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1977 if (READ_ONCE(conn->ping_task) &&
1978 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1979 (conn->ping_timeout * HZ), jiffies))
1985 enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1987 enum blk_eh_timer_return rc = BLK_EH_DONE;
1988 struct iscsi_task *task = NULL, *running_task;
1989 struct iscsi_cls_session *cls_session;
1990 struct iscsi_session *session;
1991 struct iscsi_conn *conn;
1994 cls_session = starget_to_session(scsi_target(sc->device));
1995 session = cls_session->dd_data;
1997 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1999 spin_lock_bh(&session->frwd_lock);
2000 spin_lock(&session->back_lock);
2001 task = iscsi_cmd(sc)->task;
2004 * Raced with completion. Blk layer has taken ownership
2005 * so let timeout code complete it now.
2008 spin_unlock(&session->back_lock);
2011 __iscsi_get_task(task);
2012 spin_unlock(&session->back_lock);
2014 if (session->state != ISCSI_STATE_LOGGED_IN) {
2016 * During shutdown, if session is prematurely disconnected,
2017 * recovery won't happen and there will be hung cmds. Not
2018 * handling cmds would trigger EH, also bad in this case.
2019 * Instead, handle cmd, allow completion to happen and let
2020 * upper layer to deal with the result.
2022 if (unlikely(system_state != SYSTEM_RUNNING)) {
2023 sc->result = DID_NO_CONNECT << 16;
2024 ISCSI_DBG_EH(session, "sc on shutdown, handled\n");
2029 * We are probably in the middle of iscsi recovery so let
2030 * that complete and handle the error.
2032 rc = BLK_EH_RESET_TIMER;
2036 conn = session->leadconn;
2038 /* In the middle of shuting down */
2039 rc = BLK_EH_RESET_TIMER;
2044 * If we have sent (at least queued to the network layer) a pdu or
2045 * recvd one for the task since the last timeout ask for
2046 * more time. If on the next timeout we have not made progress
2047 * we can check if it is the task or connection when we send the
2050 if (time_after(task->last_xfer, task->last_timeout)) {
2051 ISCSI_DBG_EH(session, "Command making progress. Asking "
2052 "scsi-ml for more time to complete. "
2053 "Last data xfer at %lu. Last timeout was at "
2054 "%lu\n.", task->last_xfer, task->last_timeout);
2055 task->have_checked_conn = false;
2056 rc = BLK_EH_RESET_TIMER;
2060 if (!conn->recv_timeout && !conn->ping_timeout)
2063 * if the ping timedout then we are in the middle of cleaning up
2064 * and can let the iscsi eh handle it
2066 if (iscsi_has_ping_timed_out(conn)) {
2067 rc = BLK_EH_RESET_TIMER;
2071 spin_lock(&session->back_lock);
2072 for (i = 0; i < conn->session->cmds_max; i++) {
2073 running_task = conn->session->cmds[i];
2074 if (!running_task->sc || running_task == task ||
2075 running_task->state != ISCSI_TASK_RUNNING)
2079 * Only check if cmds started before this one have made
2080 * progress, or this could never fail
2082 if (time_after(running_task->sc->jiffies_at_alloc,
2083 task->sc->jiffies_at_alloc))
2086 if (time_after(running_task->last_xfer, task->last_timeout)) {
2088 * This task has not made progress, but a task
2089 * started before us has transferred data since
2090 * we started/last-checked. We could be queueing
2091 * too many tasks or the LU is bad.
2093 * If the device is bad the cmds ahead of us on
2094 * other devs will complete, and this loop will
2095 * eventually fail starting the scsi eh.
2097 ISCSI_DBG_EH(session, "Command has not made progress "
2098 "but commands ahead of it have. "
2099 "Asking scsi-ml for more time to "
2100 "complete. Our last xfer vs running task "
2101 "last xfer %lu/%lu. Last check %lu.\n",
2102 task->last_xfer, running_task->last_xfer,
2103 task->last_timeout);
2104 spin_unlock(&session->back_lock);
2105 rc = BLK_EH_RESET_TIMER;
2109 spin_unlock(&session->back_lock);
2111 /* Assumes nop timeout is shorter than scsi cmd timeout */
2112 if (task->have_checked_conn)
2116 * Checking the transport already or nop from a cmd timeout still
2119 if (READ_ONCE(conn->ping_task)) {
2120 task->have_checked_conn = true;
2121 rc = BLK_EH_RESET_TIMER;
2125 /* Make sure there is a transport check done */
2126 iscsi_send_nopout(conn, NULL);
2127 task->have_checked_conn = true;
2128 rc = BLK_EH_RESET_TIMER;
2131 spin_unlock_bh(&session->frwd_lock);
2134 task->last_timeout = jiffies;
2135 iscsi_put_task(task);
2137 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2138 "timer reset" : "shutdown or nh");
2141 EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out);
2143 static void iscsi_check_transport_timeouts(struct timer_list *t)
2145 struct iscsi_conn *conn = from_timer(conn, t, transport_timer);
2146 struct iscsi_session *session = conn->session;
2147 unsigned long recv_timeout, next_timeout = 0, last_recv;
2149 spin_lock(&session->frwd_lock);
2150 if (session->state != ISCSI_STATE_LOGGED_IN)
2153 recv_timeout = conn->recv_timeout;
2158 last_recv = conn->last_recv;
2160 if (iscsi_has_ping_timed_out(conn)) {
2161 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
2162 "expired, recv timeout %d, last rx %lu, "
2163 "last ping %lu, now %lu\n",
2164 conn->ping_timeout, conn->recv_timeout,
2165 last_recv, conn->last_ping, jiffies);
2166 spin_unlock(&session->frwd_lock);
2167 iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
2171 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2172 /* send a ping to try to provoke some traffic */
2173 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
2174 if (iscsi_send_nopout(conn, NULL))
2175 next_timeout = jiffies + (1 * HZ);
2177 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2179 next_timeout = last_recv + recv_timeout;
2181 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2182 mod_timer(&conn->transport_timer, next_timeout);
2184 spin_unlock(&session->frwd_lock);
2188 * iscsi_conn_unbind - prevent queueing to conn.
2189 * @cls_conn: iscsi conn ep is bound to.
2190 * @is_active: is the conn in use for boot or is this for EH/termination
2192 * This must be called by drivers implementing the ep_disconnect callout.
2193 * It disables queueing to the connection from libiscsi in preparation for
2194 * an ep_disconnect call.
2196 void iscsi_conn_unbind(struct iscsi_cls_conn *cls_conn, bool is_active)
2198 struct iscsi_session *session;
2199 struct iscsi_conn *conn;
2204 conn = cls_conn->dd_data;
2205 session = conn->session;
2207 * Wait for iscsi_eh calls to exit. We don't wait for the tmf to
2208 * complete or timeout. The caller just wants to know what's running
2209 * is everything that needs to be cleaned up, and no cmds will be
2212 mutex_lock(&session->eh_mutex);
2214 iscsi_suspend_queue(conn);
2215 iscsi_suspend_tx(conn);
2217 spin_lock_bh(&session->frwd_lock);
2218 clear_bit(ISCSI_CONN_FLAG_BOUND, &conn->flags);
2222 * if logout timed out before userspace could even send a PDU
2223 * the state might still be in ISCSI_STATE_LOGGED_IN and
2224 * allowing new cmds and TMFs.
2226 if (session->state == ISCSI_STATE_LOGGED_IN)
2227 iscsi_set_conn_failed(conn);
2229 spin_unlock_bh(&session->frwd_lock);
2230 mutex_unlock(&session->eh_mutex);
2232 EXPORT_SYMBOL_GPL(iscsi_conn_unbind);
2234 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2235 struct iscsi_tm *hdr)
2237 memset(hdr, 0, sizeof(*hdr));
2238 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2239 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2240 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2241 hdr->lun = task->lun;
2242 hdr->rtt = task->hdr_itt;
2243 hdr->refcmdsn = task->cmdsn;
2246 int iscsi_eh_abort(struct scsi_cmnd *sc)
2248 struct iscsi_cls_session *cls_session;
2249 struct iscsi_session *session;
2250 struct iscsi_conn *conn;
2251 struct iscsi_task *task;
2252 struct iscsi_tm *hdr;
2255 cls_session = starget_to_session(scsi_target(sc->device));
2256 session = cls_session->dd_data;
2258 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
2260 mutex_lock(&session->eh_mutex);
2261 spin_lock_bh(&session->frwd_lock);
2263 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2266 if (!iscsi_cmd(sc)->task) {
2267 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2269 spin_unlock_bh(&session->frwd_lock);
2270 mutex_unlock(&session->eh_mutex);
2275 * If we are not logged in or we have started a new session
2276 * then let the host reset code handle this
2278 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2279 iscsi_cmd(sc)->age != session->age) {
2280 spin_unlock_bh(&session->frwd_lock);
2281 mutex_unlock(&session->eh_mutex);
2282 ISCSI_DBG_EH(session, "failing abort due to dropped "
2287 spin_lock(&session->back_lock);
2288 task = iscsi_cmd(sc)->task;
2289 if (!task || !task->sc) {
2290 /* task completed before time out */
2291 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
2293 spin_unlock(&session->back_lock);
2294 spin_unlock_bh(&session->frwd_lock);
2295 mutex_unlock(&session->eh_mutex);
2299 conn = session->leadconn;
2300 iscsi_get_conn(conn->cls_conn);
2301 conn->eh_abort_cnt++;
2304 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n", sc, task->itt);
2305 __iscsi_get_task(task);
2306 spin_unlock(&session->back_lock);
2308 if (task->state == ISCSI_TASK_PENDING) {
2309 fail_scsi_task(task, DID_ABORT);
2313 /* only have one tmf outstanding at a time */
2314 if (session->tmf_state != TMF_INITIAL)
2316 session->tmf_state = TMF_QUEUED;
2318 hdr = &session->tmhdr;
2319 iscsi_prep_abort_task_pdu(task, hdr);
2321 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout))
2324 switch (session->tmf_state) {
2326 spin_unlock_bh(&session->frwd_lock);
2328 * stop tx side incase the target had sent a abort rsp but
2329 * the initiator was still writing out data.
2331 iscsi_suspend_tx(conn);
2333 * we do not stop the recv side because targets have been
2334 * good and have never sent us a successful tmf response
2335 * then sent more data for the cmd.
2337 spin_lock_bh(&session->frwd_lock);
2338 fail_scsi_task(task, DID_ABORT);
2339 session->tmf_state = TMF_INITIAL;
2340 memset(hdr, 0, sizeof(*hdr));
2341 spin_unlock_bh(&session->frwd_lock);
2342 iscsi_start_tx(conn);
2343 goto success_unlocked;
2345 session->running_aborted_task = task;
2346 spin_unlock_bh(&session->frwd_lock);
2347 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2348 goto failed_unlocked;
2350 if (iscsi_task_is_completed(task)) {
2351 session->tmf_state = TMF_INITIAL;
2352 memset(hdr, 0, sizeof(*hdr));
2353 /* task completed before tmf abort response */
2354 ISCSI_DBG_EH(session, "sc completed while abort in "
2360 session->tmf_state = TMF_INITIAL;
2365 spin_unlock_bh(&session->frwd_lock);
2367 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2369 iscsi_put_task(task);
2370 iscsi_put_conn(conn->cls_conn);
2371 mutex_unlock(&session->eh_mutex);
2375 spin_unlock_bh(&session->frwd_lock);
2377 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2378 task ? task->itt : 0);
2380 * The driver might be accessing the task so hold the ref. The conn
2381 * stop cleanup will drop the ref after ep_disconnect so we know the
2382 * driver's no longer touching the task.
2384 if (!session->running_aborted_task)
2385 iscsi_put_task(task);
2387 iscsi_put_conn(conn->cls_conn);
2388 mutex_unlock(&session->eh_mutex);
2391 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2393 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2395 memset(hdr, 0, sizeof(*hdr));
2396 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2397 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2398 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2399 int_to_scsilun(sc->device->lun, &hdr->lun);
2400 hdr->rtt = RESERVED_ITT;
2403 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2405 struct iscsi_cls_session *cls_session;
2406 struct iscsi_session *session;
2407 struct iscsi_conn *conn;
2408 struct iscsi_tm *hdr;
2411 cls_session = starget_to_session(scsi_target(sc->device));
2412 session = cls_session->dd_data;
2414 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc,
2417 mutex_lock(&session->eh_mutex);
2418 spin_lock_bh(&session->frwd_lock);
2420 * Just check if we are not logged in. We cannot check for
2421 * the phase because the reset could come from a ioctl.
2423 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2425 conn = session->leadconn;
2427 /* only have one tmf outstanding at a time */
2428 if (session->tmf_state != TMF_INITIAL)
2430 session->tmf_state = TMF_QUEUED;
2432 hdr = &session->tmhdr;
2433 iscsi_prep_lun_reset_pdu(sc, hdr);
2435 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2436 session->lu_reset_timeout)) {
2441 switch (session->tmf_state) {
2445 spin_unlock_bh(&session->frwd_lock);
2446 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2449 session->tmf_state = TMF_INITIAL;
2454 spin_unlock_bh(&session->frwd_lock);
2456 iscsi_suspend_tx(conn);
2458 spin_lock_bh(&session->frwd_lock);
2459 memset(hdr, 0, sizeof(*hdr));
2460 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2461 session->tmf_state = TMF_INITIAL;
2462 spin_unlock_bh(&session->frwd_lock);
2464 iscsi_start_tx(conn);
2468 spin_unlock_bh(&session->frwd_lock);
2470 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2471 rc == SUCCESS ? "SUCCESS" : "FAILED");
2472 mutex_unlock(&session->eh_mutex);
2475 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2477 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2479 struct iscsi_session *session = cls_session->dd_data;
2481 spin_lock_bh(&session->frwd_lock);
2482 if (session->state != ISCSI_STATE_LOGGED_IN) {
2483 session->state = ISCSI_STATE_RECOVERY_FAILED;
2484 wake_up(&session->ehwait);
2486 spin_unlock_bh(&session->frwd_lock);
2488 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
2491 * iscsi_eh_session_reset - drop session and attempt relogin
2494 * This function will wait for a relogin, session termination from
2495 * userspace, or a recovery/replacement timeout.
2497 int iscsi_eh_session_reset(struct scsi_cmnd *sc)
2499 struct iscsi_cls_session *cls_session;
2500 struct iscsi_session *session;
2501 struct iscsi_conn *conn;
2503 cls_session = starget_to_session(scsi_target(sc->device));
2504 session = cls_session->dd_data;
2506 mutex_lock(&session->eh_mutex);
2507 spin_lock_bh(&session->frwd_lock);
2508 if (session->state == ISCSI_STATE_TERMINATE) {
2510 ISCSI_DBG_EH(session,
2511 "failing session reset: Could not log back into "
2512 "%s [age %d]\n", session->targetname,
2514 spin_unlock_bh(&session->frwd_lock);
2515 mutex_unlock(&session->eh_mutex);
2519 conn = session->leadconn;
2520 iscsi_get_conn(conn->cls_conn);
2522 spin_unlock_bh(&session->frwd_lock);
2523 mutex_unlock(&session->eh_mutex);
2525 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2526 iscsi_put_conn(conn->cls_conn);
2528 ISCSI_DBG_EH(session, "wait for relogin\n");
2529 wait_event_interruptible(session->ehwait,
2530 session->state == ISCSI_STATE_TERMINATE ||
2531 session->state == ISCSI_STATE_LOGGED_IN ||
2532 session->state == ISCSI_STATE_RECOVERY_FAILED);
2533 if (signal_pending(current))
2534 flush_signals(current);
2536 mutex_lock(&session->eh_mutex);
2537 spin_lock_bh(&session->frwd_lock);
2538 if (session->state == ISCSI_STATE_LOGGED_IN) {
2539 ISCSI_DBG_EH(session,
2540 "session reset succeeded for %s,%s\n",
2541 session->targetname, conn->persistent_address);
2544 spin_unlock_bh(&session->frwd_lock);
2545 mutex_unlock(&session->eh_mutex);
2548 EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
2550 static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2552 memset(hdr, 0, sizeof(*hdr));
2553 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2554 hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2555 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2556 hdr->rtt = RESERVED_ITT;
2560 * iscsi_eh_target_reset - reset target
2563 * This will attempt to send a warm target reset.
2565 static int iscsi_eh_target_reset(struct scsi_cmnd *sc)
2567 struct iscsi_cls_session *cls_session;
2568 struct iscsi_session *session;
2569 struct iscsi_conn *conn;
2570 struct iscsi_tm *hdr;
2573 cls_session = starget_to_session(scsi_target(sc->device));
2574 session = cls_session->dd_data;
2576 ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
2577 session->targetname);
2579 mutex_lock(&session->eh_mutex);
2580 spin_lock_bh(&session->frwd_lock);
2582 * Just check if we are not logged in. We cannot check for
2583 * the phase because the reset could come from a ioctl.
2585 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2587 conn = session->leadconn;
2589 /* only have one tmf outstanding at a time */
2590 if (session->tmf_state != TMF_INITIAL)
2592 session->tmf_state = TMF_QUEUED;
2594 hdr = &session->tmhdr;
2595 iscsi_prep_tgt_reset_pdu(sc, hdr);
2597 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2598 session->tgt_reset_timeout)) {
2603 switch (session->tmf_state) {
2607 spin_unlock_bh(&session->frwd_lock);
2608 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2611 session->tmf_state = TMF_INITIAL;
2616 spin_unlock_bh(&session->frwd_lock);
2618 iscsi_suspend_tx(conn);
2620 spin_lock_bh(&session->frwd_lock);
2621 memset(hdr, 0, sizeof(*hdr));
2622 fail_scsi_tasks(conn, -1, DID_ERROR);
2623 session->tmf_state = TMF_INITIAL;
2624 spin_unlock_bh(&session->frwd_lock);
2626 iscsi_start_tx(conn);
2630 spin_unlock_bh(&session->frwd_lock);
2632 ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
2633 rc == SUCCESS ? "SUCCESS" : "FAILED");
2634 mutex_unlock(&session->eh_mutex);
2639 * iscsi_eh_recover_target - reset target and possibly the session
2642 * This will attempt to send a warm target reset. If that fails,
2643 * we will escalate to ERL0 session recovery.
2645 int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2649 rc = iscsi_eh_target_reset(sc);
2651 rc = iscsi_eh_session_reset(sc);
2654 EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
2657 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2658 * should be accessed via kfifo_{get,put} on q->queue.
2659 * Optionally, the caller can obtain the array of object pointers
2660 * by passing in a non-NULL @items pointer
2663 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
2665 int i, num_arrays = 1;
2667 memset(q, 0, sizeof(*q));
2671 /* If the user passed an items pointer, he wants a copy of
2675 q->pool = kvcalloc(num_arrays * max, sizeof(void *), GFP_KERNEL);
2676 if (q->pool == NULL)
2679 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
2681 for (i = 0; i < max; i++) {
2682 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2683 if (q->pool[i] == NULL) {
2687 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
2691 *items = q->pool + max;
2692 memcpy(*items, q->pool, max * sizeof(void *));
2701 EXPORT_SYMBOL_GPL(iscsi_pool_init);
2703 void iscsi_pool_free(struct iscsi_pool *q)
2707 for (i = 0; i < q->max; i++)
2711 EXPORT_SYMBOL_GPL(iscsi_pool_free);
2713 int iscsi_host_get_max_scsi_cmds(struct Scsi_Host *shost,
2714 uint16_t requested_cmds_max)
2716 int scsi_cmds, total_cmds = requested_cmds_max;
2720 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2722 * The iscsi layer needs some tasks for nop handling and tmfs,
2723 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2724 * + 1 command for scsi IO.
2726 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2727 printk(KERN_ERR "iscsi: invalid max cmds of %d. Must be a power of two that is at least %d.\n",
2728 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2732 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2733 printk(KERN_INFO "iscsi: invalid max cmds of %d. Must be a power of 2 less than or equal to %d. Using %d.\n",
2734 requested_cmds_max, ISCSI_TOTAL_CMDS_MAX,
2735 ISCSI_TOTAL_CMDS_MAX);
2736 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2739 if (!is_power_of_2(total_cmds)) {
2740 total_cmds = rounddown_pow_of_two(total_cmds);
2741 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2742 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);
2746 printk(KERN_INFO "iscsi: invalid max cmds %d. Must be a power of 2. Rounding max cmds down to %d.\n",
2747 requested_cmds_max, total_cmds);
2750 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2751 if (shost->can_queue && scsi_cmds > shost->can_queue) {
2752 total_cmds = shost->can_queue;
2754 printk(KERN_INFO "iscsi: requested max cmds %u is higher than driver limit. Using driver limit %u\n",
2755 requested_cmds_max, shost->can_queue);
2761 EXPORT_SYMBOL_GPL(iscsi_host_get_max_scsi_cmds);
2764 * iscsi_host_add - add host to system
2766 * @pdev: parent device
2768 * This should be called by partial offload and software iscsi drivers
2769 * to add a host to the system.
2771 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2773 if (!shost->can_queue)
2774 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2776 if (!shost->cmd_per_lun)
2777 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2779 return scsi_add_host(shost, pdev);
2781 EXPORT_SYMBOL_GPL(iscsi_host_add);
2784 * iscsi_host_alloc - allocate a host and driver data
2785 * @sht: scsi host template
2786 * @dd_data_size: driver host data size
2787 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2789 * This should be called by partial offload and software iscsi drivers.
2790 * To access the driver specific memory use the iscsi_host_priv() macro.
2792 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2793 int dd_data_size, bool xmit_can_sleep)
2795 struct Scsi_Host *shost;
2796 struct iscsi_host *ihost;
2798 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2801 ihost = shost_priv(shost);
2803 if (xmit_can_sleep) {
2804 ihost->workq = alloc_workqueue("iscsi_q_%d",
2805 WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND,
2811 spin_lock_init(&ihost->lock);
2812 ihost->state = ISCSI_HOST_SETUP;
2813 ihost->num_sessions = 0;
2814 init_waitqueue_head(&ihost->session_removal_wq);
2818 scsi_host_put(shost);
2821 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2823 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2825 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2829 * iscsi_host_remove - remove host and sessions
2832 * If there are any sessions left, this will initiate the removal and wait
2833 * for the completion.
2835 void iscsi_host_remove(struct Scsi_Host *shost)
2837 struct iscsi_host *ihost = shost_priv(shost);
2838 unsigned long flags;
2840 spin_lock_irqsave(&ihost->lock, flags);
2841 ihost->state = ISCSI_HOST_REMOVED;
2842 spin_unlock_irqrestore(&ihost->lock, flags);
2844 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2845 wait_event_interruptible(ihost->session_removal_wq,
2846 ihost->num_sessions == 0);
2847 if (signal_pending(current))
2848 flush_signals(current);
2850 scsi_remove_host(shost);
2852 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2854 void iscsi_host_free(struct Scsi_Host *shost)
2856 struct iscsi_host *ihost = shost_priv(shost);
2859 destroy_workqueue(ihost->workq);
2861 kfree(ihost->netdev);
2862 kfree(ihost->hwaddress);
2863 kfree(ihost->initiatorname);
2864 scsi_host_put(shost);
2866 EXPORT_SYMBOL_GPL(iscsi_host_free);
2868 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2870 struct iscsi_host *ihost = shost_priv(shost);
2871 unsigned long flags;
2873 shost = scsi_host_get(shost);
2875 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2876 "of session teardown event because host already "
2881 spin_lock_irqsave(&ihost->lock, flags);
2882 ihost->num_sessions--;
2883 if (ihost->num_sessions == 0)
2884 wake_up(&ihost->session_removal_wq);
2885 spin_unlock_irqrestore(&ihost->lock, flags);
2886 scsi_host_put(shost);
2890 * iscsi_session_setup - create iscsi cls session and host and session
2891 * @iscsit: iscsi transport template
2893 * @cmds_max: session can queue
2894 * @dd_size: private driver data size, added to session allocation size
2895 * @cmd_task_size: LLD task private data size
2896 * @initial_cmdsn: initial CmdSN
2897 * @id: target ID to add to this session
2899 * This can be used by software iscsi_transports that allocate
2900 * a session per scsi host.
2902 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2903 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2904 * for nop handling and login/logout requests.
2906 struct iscsi_cls_session *
2907 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2908 uint16_t cmds_max, int dd_size, int cmd_task_size,
2909 uint32_t initial_cmdsn, unsigned int id)
2911 struct iscsi_host *ihost = shost_priv(shost);
2912 struct iscsi_session *session;
2913 struct iscsi_cls_session *cls_session;
2914 int cmd_i, scsi_cmds;
2915 unsigned long flags;
2917 spin_lock_irqsave(&ihost->lock, flags);
2918 if (ihost->state == ISCSI_HOST_REMOVED) {
2919 spin_unlock_irqrestore(&ihost->lock, flags);
2922 ihost->num_sessions++;
2923 spin_unlock_irqrestore(&ihost->lock, flags);
2925 scsi_cmds = iscsi_host_get_max_scsi_cmds(shost, cmds_max);
2927 goto dec_session_count;
2929 cls_session = iscsi_alloc_session(shost, iscsit,
2930 sizeof(struct iscsi_session) +
2933 goto dec_session_count;
2934 session = cls_session->dd_data;
2935 session->cls_session = cls_session;
2936 session->host = shost;
2937 session->state = ISCSI_STATE_FREE;
2938 session->fast_abort = 1;
2939 session->tgt_reset_timeout = 30;
2940 session->lu_reset_timeout = 15;
2941 session->abort_timeout = 10;
2942 session->scsi_cmds_max = scsi_cmds;
2943 session->cmds_max = scsi_cmds + ISCSI_MGMT_CMDS_MAX;
2944 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2945 session->exp_cmdsn = initial_cmdsn + 1;
2946 session->max_cmdsn = initial_cmdsn + 1;
2947 session->max_r2t = 1;
2948 session->tt = iscsit;
2949 session->dd_data = cls_session->dd_data + sizeof(*session);
2951 session->tmf_state = TMF_INITIAL;
2952 timer_setup(&session->tmf_timer, iscsi_tmf_timedout, 0);
2953 mutex_init(&session->eh_mutex);
2954 init_waitqueue_head(&session->ehwait);
2956 spin_lock_init(&session->frwd_lock);
2957 spin_lock_init(&session->back_lock);
2959 /* initialize SCSI PDU commands pool */
2960 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2961 (void***)&session->cmds,
2962 cmd_task_size + sizeof(struct iscsi_task)))
2963 goto cmdpool_alloc_fail;
2965 /* pre-format cmds pool with ITT */
2966 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2967 struct iscsi_task *task = session->cmds[cmd_i];
2970 task->dd_data = &task[1];
2972 task->state = ISCSI_TASK_FREE;
2973 INIT_LIST_HEAD(&task->running);
2976 if (!try_module_get(iscsit->owner))
2977 goto module_get_fail;
2979 if (iscsi_add_session(cls_session, id))
2980 goto cls_session_fail;
2985 module_put(iscsit->owner);
2987 iscsi_pool_free(&session->cmdpool);
2989 iscsi_free_session(cls_session);
2991 iscsi_host_dec_session_cnt(shost);
2994 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2997 * iscsi_session_teardown - destroy session, host, and cls_session
2998 * @cls_session: iscsi session
3000 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
3002 struct iscsi_session *session = cls_session->dd_data;
3003 struct module *owner = cls_session->transport->owner;
3004 struct Scsi_Host *shost = session->host;
3006 iscsi_remove_session(cls_session);
3008 iscsi_pool_free(&session->cmdpool);
3009 kfree(session->password);
3010 kfree(session->password_in);
3011 kfree(session->username);
3012 kfree(session->username_in);
3013 kfree(session->targetname);
3014 kfree(session->targetalias);
3015 kfree(session->initiatorname);
3016 kfree(session->boot_root);
3017 kfree(session->boot_nic);
3018 kfree(session->boot_target);
3019 kfree(session->ifacename);
3020 kfree(session->portal_type);
3021 kfree(session->discovery_parent_type);
3023 iscsi_free_session(cls_session);
3025 iscsi_host_dec_session_cnt(shost);
3028 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
3031 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
3032 * @cls_session: iscsi_cls_session
3033 * @dd_size: private driver data size
3036 struct iscsi_cls_conn *
3037 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
3040 struct iscsi_session *session = cls_session->dd_data;
3041 struct iscsi_conn *conn;
3042 struct iscsi_cls_conn *cls_conn;
3046 cls_conn = iscsi_alloc_conn(cls_session, sizeof(*conn) + dd_size,
3050 conn = cls_conn->dd_data;
3052 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
3053 conn->session = session;
3054 conn->cls_conn = cls_conn;
3055 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
3056 conn->id = conn_idx;
3057 conn->exp_statsn = 0;
3059 timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0);
3061 INIT_LIST_HEAD(&conn->mgmtqueue);
3062 INIT_LIST_HEAD(&conn->cmdqueue);
3063 INIT_LIST_HEAD(&conn->requeue);
3064 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
3066 /* allocate login_task used for the login/text sequences */
3067 spin_lock_bh(&session->frwd_lock);
3068 if (!kfifo_out(&session->cmdpool.queue,
3069 (void*)&conn->login_task,
3071 spin_unlock_bh(&session->frwd_lock);
3072 goto login_task_alloc_fail;
3074 spin_unlock_bh(&session->frwd_lock);
3076 data = (char *) __get_free_pages(GFP_KERNEL,
3077 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
3079 goto login_task_data_alloc_fail;
3080 conn->login_task->data = conn->data = data;
3082 err = iscsi_add_conn(cls_conn);
3084 goto login_task_add_dev_fail;
3088 login_task_add_dev_fail:
3089 free_pages((unsigned long) conn->data,
3090 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
3092 login_task_data_alloc_fail:
3093 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
3095 login_task_alloc_fail:
3096 iscsi_put_conn(cls_conn);
3099 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
3102 * iscsi_conn_teardown - teardown iscsi connection
3103 * @cls_conn: iscsi class connection
3105 * TODO: we may need to make this into a two step process
3106 * like scsi-mls remove + put host
3108 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
3110 struct iscsi_conn *conn = cls_conn->dd_data;
3111 struct iscsi_session *session = conn->session;
3113 iscsi_remove_conn(cls_conn);
3115 del_timer_sync(&conn->transport_timer);
3117 mutex_lock(&session->eh_mutex);
3118 spin_lock_bh(&session->frwd_lock);
3119 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
3120 if (session->leadconn == conn) {
3122 * leading connection? then give up on recovery.
3124 session->state = ISCSI_STATE_TERMINATE;
3125 wake_up(&session->ehwait);
3127 spin_unlock_bh(&session->frwd_lock);
3129 /* flush queued up work because we free the connection below */
3130 iscsi_suspend_tx(conn);
3132 spin_lock_bh(&session->frwd_lock);
3133 free_pages((unsigned long) conn->data,
3134 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
3135 kfree(conn->persistent_address);
3136 kfree(conn->local_ipaddr);
3137 /* regular RX path uses back_lock */
3138 spin_lock_bh(&session->back_lock);
3139 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
3141 spin_unlock_bh(&session->back_lock);
3142 if (session->leadconn == conn)
3143 session->leadconn = NULL;
3144 spin_unlock_bh(&session->frwd_lock);
3145 mutex_unlock(&session->eh_mutex);
3147 iscsi_put_conn(cls_conn);
3149 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
3151 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
3153 struct iscsi_conn *conn = cls_conn->dd_data;
3154 struct iscsi_session *session = conn->session;
3157 iscsi_conn_printk(KERN_ERR, conn,
3158 "can't start unbound connection\n");
3162 if ((session->imm_data_en || !session->initial_r2t_en) &&
3163 session->first_burst > session->max_burst) {
3164 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
3165 "first_burst %d max_burst %d\n",
3166 session->first_burst, session->max_burst);
3170 if (conn->ping_timeout && !conn->recv_timeout) {
3171 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
3172 "zero. Using 5 seconds\n.");
3173 conn->recv_timeout = 5;
3176 if (conn->recv_timeout && !conn->ping_timeout) {
3177 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3178 "zero. Using 5 seconds.\n");
3179 conn->ping_timeout = 5;
3182 spin_lock_bh(&session->frwd_lock);
3183 conn->c_stage = ISCSI_CONN_STARTED;
3184 session->state = ISCSI_STATE_LOGGED_IN;
3185 session->queued_cmdsn = session->cmdsn;
3187 conn->last_recv = jiffies;
3188 conn->last_ping = jiffies;
3189 if (conn->recv_timeout && conn->ping_timeout)
3190 mod_timer(&conn->transport_timer,
3191 jiffies + (conn->recv_timeout * HZ));
3193 switch(conn->stop_stage) {
3194 case STOP_CONN_RECOVER:
3196 * unblock eh_abort() if it is blocked. re-try all
3197 * commands after successful recovery
3199 conn->stop_stage = 0;
3200 session->tmf_state = TMF_INITIAL;
3202 if (session->age == 16)
3205 case STOP_CONN_TERM:
3206 conn->stop_stage = 0;
3211 spin_unlock_bh(&session->frwd_lock);
3213 iscsi_unblock_session(session->cls_session);
3214 wake_up(&session->ehwait);
3217 EXPORT_SYMBOL_GPL(iscsi_conn_start);
3220 fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
3222 struct iscsi_task *task;
3225 for (i = 0; i < conn->session->cmds_max; i++) {
3226 task = conn->session->cmds[i];
3230 if (task->state == ISCSI_TASK_FREE)
3233 ISCSI_DBG_SESSION(conn->session,
3234 "failing mgmt itt 0x%x state %d\n",
3235 task->itt, task->state);
3237 spin_lock_bh(&session->back_lock);
3238 if (cleanup_queued_task(task)) {
3239 spin_unlock_bh(&session->back_lock);
3243 state = ISCSI_TASK_ABRT_SESS_RECOV;
3244 if (task->state == ISCSI_TASK_PENDING)
3245 state = ISCSI_TASK_COMPLETED;
3246 iscsi_complete_task(task, state);
3247 spin_unlock_bh(&session->back_lock);
3251 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3253 struct iscsi_conn *conn = cls_conn->dd_data;
3254 struct iscsi_session *session = conn->session;
3257 mutex_lock(&session->eh_mutex);
3258 spin_lock_bh(&session->frwd_lock);
3259 if (conn->stop_stage == STOP_CONN_TERM) {
3260 spin_unlock_bh(&session->frwd_lock);
3261 mutex_unlock(&session->eh_mutex);
3266 * When this is called for the in_login state, we only want to clean
3267 * up the login task and connection. We do not need to block and set
3268 * the recovery state again
3270 if (flag == STOP_CONN_TERM)
3271 session->state = ISCSI_STATE_TERMINATE;
3272 else if (conn->stop_stage != STOP_CONN_RECOVER)
3273 session->state = ISCSI_STATE_IN_RECOVERY;
3275 old_stop_stage = conn->stop_stage;
3276 conn->stop_stage = flag;
3277 spin_unlock_bh(&session->frwd_lock);
3279 del_timer_sync(&conn->transport_timer);
3280 iscsi_suspend_tx(conn);
3282 spin_lock_bh(&session->frwd_lock);
3283 conn->c_stage = ISCSI_CONN_STOPPED;
3284 spin_unlock_bh(&session->frwd_lock);
3287 * for connection level recovery we should not calculate
3288 * header digest. conn->hdr_size used for optimization
3289 * in hdr_extract() and will be re-negotiated at
3292 if (flag == STOP_CONN_RECOVER) {
3293 conn->hdrdgst_en = 0;
3294 conn->datadgst_en = 0;
3295 if (session->state == ISCSI_STATE_IN_RECOVERY &&
3296 old_stop_stage != STOP_CONN_RECOVER) {
3297 ISCSI_DBG_SESSION(session, "blocking session\n");
3298 iscsi_block_session(session->cls_session);
3305 spin_lock_bh(&session->frwd_lock);
3306 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
3307 fail_mgmt_tasks(session, conn);
3308 memset(&session->tmhdr, 0, sizeof(session->tmhdr));
3309 spin_unlock_bh(&session->frwd_lock);
3310 mutex_unlock(&session->eh_mutex);
3312 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3314 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3315 struct iscsi_cls_conn *cls_conn, int is_leading)
3317 struct iscsi_session *session = cls_session->dd_data;
3318 struct iscsi_conn *conn = cls_conn->dd_data;
3320 spin_lock_bh(&session->frwd_lock);
3322 session->leadconn = conn;
3324 set_bit(ISCSI_CONN_FLAG_BOUND, &conn->flags);
3325 spin_unlock_bh(&session->frwd_lock);
3328 * The target could have reduced it's window size between logins, so
3329 * we have to reset max/exp cmdsn so we can see the new values.
3331 spin_lock_bh(&session->back_lock);
3332 session->max_cmdsn = session->exp_cmdsn = session->cmdsn + 1;
3333 spin_unlock_bh(&session->back_lock);
3335 * Unblock xmitworker(), Login Phase will pass through.
3337 clear_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags);
3338 clear_bit(ISCSI_CONN_FLAG_SUSPEND_TX, &conn->flags);
3341 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3343 int iscsi_switch_str_param(char **param, char *new_val_buf)
3348 if (!strcmp(*param, new_val_buf))
3352 new_val = kstrdup(new_val_buf, GFP_NOIO);
3360 EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
3362 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3363 enum iscsi_param param, char *buf, int buflen)
3365 struct iscsi_conn *conn = cls_conn->dd_data;
3366 struct iscsi_session *session = conn->session;
3370 case ISCSI_PARAM_FAST_ABORT:
3371 sscanf(buf, "%d", &session->fast_abort);
3373 case ISCSI_PARAM_ABORT_TMO:
3374 sscanf(buf, "%d", &session->abort_timeout);
3376 case ISCSI_PARAM_LU_RESET_TMO:
3377 sscanf(buf, "%d", &session->lu_reset_timeout);
3379 case ISCSI_PARAM_TGT_RESET_TMO:
3380 sscanf(buf, "%d", &session->tgt_reset_timeout);
3382 case ISCSI_PARAM_PING_TMO:
3383 sscanf(buf, "%d", &conn->ping_timeout);
3385 case ISCSI_PARAM_RECV_TMO:
3386 sscanf(buf, "%d", &conn->recv_timeout);
3388 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3389 sscanf(buf, "%d", &conn->max_recv_dlength);
3391 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3392 sscanf(buf, "%d", &conn->max_xmit_dlength);
3394 case ISCSI_PARAM_HDRDGST_EN:
3395 sscanf(buf, "%d", &conn->hdrdgst_en);
3397 case ISCSI_PARAM_DATADGST_EN:
3398 sscanf(buf, "%d", &conn->datadgst_en);
3400 case ISCSI_PARAM_INITIAL_R2T_EN:
3401 sscanf(buf, "%d", &session->initial_r2t_en);
3403 case ISCSI_PARAM_MAX_R2T:
3404 sscanf(buf, "%hu", &session->max_r2t);
3406 case ISCSI_PARAM_IMM_DATA_EN:
3407 sscanf(buf, "%d", &session->imm_data_en);
3409 case ISCSI_PARAM_FIRST_BURST:
3410 sscanf(buf, "%d", &session->first_burst);
3412 case ISCSI_PARAM_MAX_BURST:
3413 sscanf(buf, "%d", &session->max_burst);
3415 case ISCSI_PARAM_PDU_INORDER_EN:
3416 sscanf(buf, "%d", &session->pdu_inorder_en);
3418 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3419 sscanf(buf, "%d", &session->dataseq_inorder_en);
3421 case ISCSI_PARAM_ERL:
3422 sscanf(buf, "%d", &session->erl);
3424 case ISCSI_PARAM_EXP_STATSN:
3425 sscanf(buf, "%u", &conn->exp_statsn);
3427 case ISCSI_PARAM_USERNAME:
3428 return iscsi_switch_str_param(&session->username, buf);
3429 case ISCSI_PARAM_USERNAME_IN:
3430 return iscsi_switch_str_param(&session->username_in, buf);
3431 case ISCSI_PARAM_PASSWORD:
3432 return iscsi_switch_str_param(&session->password, buf);
3433 case ISCSI_PARAM_PASSWORD_IN:
3434 return iscsi_switch_str_param(&session->password_in, buf);
3435 case ISCSI_PARAM_TARGET_NAME:
3436 return iscsi_switch_str_param(&session->targetname, buf);
3437 case ISCSI_PARAM_TARGET_ALIAS:
3438 return iscsi_switch_str_param(&session->targetalias, buf);
3439 case ISCSI_PARAM_TPGT:
3440 sscanf(buf, "%d", &session->tpgt);
3442 case ISCSI_PARAM_PERSISTENT_PORT:
3443 sscanf(buf, "%d", &conn->persistent_port);
3445 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3446 return iscsi_switch_str_param(&conn->persistent_address, buf);
3447 case ISCSI_PARAM_IFACE_NAME:
3448 return iscsi_switch_str_param(&session->ifacename, buf);
3449 case ISCSI_PARAM_INITIATOR_NAME:
3450 return iscsi_switch_str_param(&session->initiatorname, buf);
3451 case ISCSI_PARAM_BOOT_ROOT:
3452 return iscsi_switch_str_param(&session->boot_root, buf);
3453 case ISCSI_PARAM_BOOT_NIC:
3454 return iscsi_switch_str_param(&session->boot_nic, buf);
3455 case ISCSI_PARAM_BOOT_TARGET:
3456 return iscsi_switch_str_param(&session->boot_target, buf);
3457 case ISCSI_PARAM_PORTAL_TYPE:
3458 return iscsi_switch_str_param(&session->portal_type, buf);
3459 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3460 return iscsi_switch_str_param(&session->discovery_parent_type,
3462 case ISCSI_PARAM_DISCOVERY_SESS:
3463 sscanf(buf, "%d", &val);
3464 session->discovery_sess = !!val;
3466 case ISCSI_PARAM_LOCAL_IPADDR:
3467 return iscsi_switch_str_param(&conn->local_ipaddr, buf);
3474 EXPORT_SYMBOL_GPL(iscsi_set_param);
3476 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3477 enum iscsi_param param, char *buf)
3479 struct iscsi_session *session = cls_session->dd_data;
3483 case ISCSI_PARAM_FAST_ABORT:
3484 len = sysfs_emit(buf, "%d\n", session->fast_abort);
3486 case ISCSI_PARAM_ABORT_TMO:
3487 len = sysfs_emit(buf, "%d\n", session->abort_timeout);
3489 case ISCSI_PARAM_LU_RESET_TMO:
3490 len = sysfs_emit(buf, "%d\n", session->lu_reset_timeout);
3492 case ISCSI_PARAM_TGT_RESET_TMO:
3493 len = sysfs_emit(buf, "%d\n", session->tgt_reset_timeout);
3495 case ISCSI_PARAM_INITIAL_R2T_EN:
3496 len = sysfs_emit(buf, "%d\n", session->initial_r2t_en);
3498 case ISCSI_PARAM_MAX_R2T:
3499 len = sysfs_emit(buf, "%hu\n", session->max_r2t);
3501 case ISCSI_PARAM_IMM_DATA_EN:
3502 len = sysfs_emit(buf, "%d\n", session->imm_data_en);
3504 case ISCSI_PARAM_FIRST_BURST:
3505 len = sysfs_emit(buf, "%u\n", session->first_burst);
3507 case ISCSI_PARAM_MAX_BURST:
3508 len = sysfs_emit(buf, "%u\n", session->max_burst);
3510 case ISCSI_PARAM_PDU_INORDER_EN:
3511 len = sysfs_emit(buf, "%d\n", session->pdu_inorder_en);
3513 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3514 len = sysfs_emit(buf, "%d\n", session->dataseq_inorder_en);
3516 case ISCSI_PARAM_DEF_TASKMGMT_TMO:
3517 len = sysfs_emit(buf, "%d\n", session->def_taskmgmt_tmo);
3519 case ISCSI_PARAM_ERL:
3520 len = sysfs_emit(buf, "%d\n", session->erl);
3522 case ISCSI_PARAM_TARGET_NAME:
3523 len = sysfs_emit(buf, "%s\n", session->targetname);
3525 case ISCSI_PARAM_TARGET_ALIAS:
3526 len = sysfs_emit(buf, "%s\n", session->targetalias);
3528 case ISCSI_PARAM_TPGT:
3529 len = sysfs_emit(buf, "%d\n", session->tpgt);
3531 case ISCSI_PARAM_USERNAME:
3532 len = sysfs_emit(buf, "%s\n", session->username);
3534 case ISCSI_PARAM_USERNAME_IN:
3535 len = sysfs_emit(buf, "%s\n", session->username_in);
3537 case ISCSI_PARAM_PASSWORD:
3538 len = sysfs_emit(buf, "%s\n", session->password);
3540 case ISCSI_PARAM_PASSWORD_IN:
3541 len = sysfs_emit(buf, "%s\n", session->password_in);
3543 case ISCSI_PARAM_IFACE_NAME:
3544 len = sysfs_emit(buf, "%s\n", session->ifacename);
3546 case ISCSI_PARAM_INITIATOR_NAME:
3547 len = sysfs_emit(buf, "%s\n", session->initiatorname);
3549 case ISCSI_PARAM_BOOT_ROOT:
3550 len = sysfs_emit(buf, "%s\n", session->boot_root);
3552 case ISCSI_PARAM_BOOT_NIC:
3553 len = sysfs_emit(buf, "%s\n", session->boot_nic);
3555 case ISCSI_PARAM_BOOT_TARGET:
3556 len = sysfs_emit(buf, "%s\n", session->boot_target);
3558 case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
3559 len = sysfs_emit(buf, "%u\n", session->auto_snd_tgt_disable);
3561 case ISCSI_PARAM_DISCOVERY_SESS:
3562 len = sysfs_emit(buf, "%u\n", session->discovery_sess);
3564 case ISCSI_PARAM_PORTAL_TYPE:
3565 len = sysfs_emit(buf, "%s\n", session->portal_type);
3567 case ISCSI_PARAM_CHAP_AUTH_EN:
3568 len = sysfs_emit(buf, "%u\n", session->chap_auth_en);
3570 case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
3571 len = sysfs_emit(buf, "%u\n", session->discovery_logout_en);
3573 case ISCSI_PARAM_BIDI_CHAP_EN:
3574 len = sysfs_emit(buf, "%u\n", session->bidi_chap_en);
3576 case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
3577 len = sysfs_emit(buf, "%u\n", session->discovery_auth_optional);
3579 case ISCSI_PARAM_DEF_TIME2WAIT:
3580 len = sysfs_emit(buf, "%d\n", session->time2wait);
3582 case ISCSI_PARAM_DEF_TIME2RETAIN:
3583 len = sysfs_emit(buf, "%d\n", session->time2retain);
3585 case ISCSI_PARAM_TSID:
3586 len = sysfs_emit(buf, "%u\n", session->tsid);
3588 case ISCSI_PARAM_ISID:
3589 len = sysfs_emit(buf, "%02x%02x%02x%02x%02x%02x\n",
3590 session->isid[0], session->isid[1],
3591 session->isid[2], session->isid[3],
3592 session->isid[4], session->isid[5]);
3594 case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
3595 len = sysfs_emit(buf, "%u\n", session->discovery_parent_idx);
3597 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3598 if (session->discovery_parent_type)
3599 len = sysfs_emit(buf, "%s\n",
3600 session->discovery_parent_type);
3602 len = sysfs_emit(buf, "\n");
3610 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3612 int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
3613 enum iscsi_param param, char *buf)
3615 struct sockaddr_in6 *sin6 = NULL;
3616 struct sockaddr_in *sin = NULL;
3619 switch (addr->ss_family) {
3621 sin = (struct sockaddr_in *)addr;
3624 sin6 = (struct sockaddr_in6 *)addr;
3631 case ISCSI_PARAM_CONN_ADDRESS:
3632 case ISCSI_HOST_PARAM_IPADDRESS:
3634 len = sysfs_emit(buf, "%pI4\n", &sin->sin_addr.s_addr);
3636 len = sysfs_emit(buf, "%pI6\n", &sin6->sin6_addr);
3638 case ISCSI_PARAM_CONN_PORT:
3639 case ISCSI_PARAM_LOCAL_PORT:
3641 len = sysfs_emit(buf, "%hu\n", be16_to_cpu(sin->sin_port));
3643 len = sysfs_emit(buf, "%hu\n",
3644 be16_to_cpu(sin6->sin6_port));
3652 EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
3654 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3655 enum iscsi_param param, char *buf)
3657 struct iscsi_conn *conn = cls_conn->dd_data;
3661 case ISCSI_PARAM_PING_TMO:
3662 len = sysfs_emit(buf, "%u\n", conn->ping_timeout);
3664 case ISCSI_PARAM_RECV_TMO:
3665 len = sysfs_emit(buf, "%u\n", conn->recv_timeout);
3667 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3668 len = sysfs_emit(buf, "%u\n", conn->max_recv_dlength);
3670 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3671 len = sysfs_emit(buf, "%u\n", conn->max_xmit_dlength);
3673 case ISCSI_PARAM_HDRDGST_EN:
3674 len = sysfs_emit(buf, "%d\n", conn->hdrdgst_en);
3676 case ISCSI_PARAM_DATADGST_EN:
3677 len = sysfs_emit(buf, "%d\n", conn->datadgst_en);
3679 case ISCSI_PARAM_IFMARKER_EN:
3680 len = sysfs_emit(buf, "%d\n", conn->ifmarker_en);
3682 case ISCSI_PARAM_OFMARKER_EN:
3683 len = sysfs_emit(buf, "%d\n", conn->ofmarker_en);
3685 case ISCSI_PARAM_EXP_STATSN:
3686 len = sysfs_emit(buf, "%u\n", conn->exp_statsn);
3688 case ISCSI_PARAM_PERSISTENT_PORT:
3689 len = sysfs_emit(buf, "%d\n", conn->persistent_port);
3691 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3692 len = sysfs_emit(buf, "%s\n", conn->persistent_address);
3694 case ISCSI_PARAM_STATSN:
3695 len = sysfs_emit(buf, "%u\n", conn->statsn);
3697 case ISCSI_PARAM_MAX_SEGMENT_SIZE:
3698 len = sysfs_emit(buf, "%u\n", conn->max_segment_size);
3700 case ISCSI_PARAM_KEEPALIVE_TMO:
3701 len = sysfs_emit(buf, "%u\n", conn->keepalive_tmo);
3703 case ISCSI_PARAM_LOCAL_PORT:
3704 len = sysfs_emit(buf, "%u\n", conn->local_port);
3706 case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
3707 len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_stat);
3709 case ISCSI_PARAM_TCP_NAGLE_DISABLE:
3710 len = sysfs_emit(buf, "%u\n", conn->tcp_nagle_disable);
3712 case ISCSI_PARAM_TCP_WSF_DISABLE:
3713 len = sysfs_emit(buf, "%u\n", conn->tcp_wsf_disable);
3715 case ISCSI_PARAM_TCP_TIMER_SCALE:
3716 len = sysfs_emit(buf, "%u\n", conn->tcp_timer_scale);
3718 case ISCSI_PARAM_TCP_TIMESTAMP_EN:
3719 len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_en);
3721 case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
3722 len = sysfs_emit(buf, "%u\n", conn->fragment_disable);
3724 case ISCSI_PARAM_IPV4_TOS:
3725 len = sysfs_emit(buf, "%u\n", conn->ipv4_tos);
3727 case ISCSI_PARAM_IPV6_TC:
3728 len = sysfs_emit(buf, "%u\n", conn->ipv6_traffic_class);
3730 case ISCSI_PARAM_IPV6_FLOW_LABEL:
3731 len = sysfs_emit(buf, "%u\n", conn->ipv6_flow_label);
3733 case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
3734 len = sysfs_emit(buf, "%u\n", conn->is_fw_assigned_ipv6);
3736 case ISCSI_PARAM_TCP_XMIT_WSF:
3737 len = sysfs_emit(buf, "%u\n", conn->tcp_xmit_wsf);
3739 case ISCSI_PARAM_TCP_RECV_WSF:
3740 len = sysfs_emit(buf, "%u\n", conn->tcp_recv_wsf);
3742 case ISCSI_PARAM_LOCAL_IPADDR:
3743 len = sysfs_emit(buf, "%s\n", conn->local_ipaddr);
3751 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3753 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3756 struct iscsi_host *ihost = shost_priv(shost);
3760 case ISCSI_HOST_PARAM_NETDEV_NAME:
3761 len = sysfs_emit(buf, "%s\n", ihost->netdev);
3763 case ISCSI_HOST_PARAM_HWADDRESS:
3764 len = sysfs_emit(buf, "%s\n", ihost->hwaddress);
3766 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3767 len = sysfs_emit(buf, "%s\n", ihost->initiatorname);
3775 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3777 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3778 char *buf, int buflen)
3780 struct iscsi_host *ihost = shost_priv(shost);
3783 case ISCSI_HOST_PARAM_NETDEV_NAME:
3784 return iscsi_switch_str_param(&ihost->netdev, buf);
3785 case ISCSI_HOST_PARAM_HWADDRESS:
3786 return iscsi_switch_str_param(&ihost->hwaddress, buf);
3787 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3788 return iscsi_switch_str_param(&ihost->initiatorname, buf);
3795 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3797 MODULE_AUTHOR("Mike Christie");
3798 MODULE_DESCRIPTION("iSCSI library functions");
3799 MODULE_LICENSE("GPL");