1 /*******************************************************************************
2 * This file contains the iSCSI Target specific utility functions.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
19 #include <linux/list.h>
20 #include <linux/sched/signal.h>
21 #include <net/ipv6.h> /* ipv6_addr_equal() */
22 #include <scsi/scsi_tcq.h>
23 #include <scsi/iscsi_proto.h>
24 #include <target/target_core_base.h>
25 #include <target/target_core_fabric.h>
26 #include <target/iscsi/iscsi_transport.h>
28 #include <target/iscsi/iscsi_target_core.h>
29 #include "iscsi_target_parameters.h"
30 #include "iscsi_target_seq_pdu_list.h"
31 #include "iscsi_target_datain_values.h"
32 #include "iscsi_target_erl0.h"
33 #include "iscsi_target_erl1.h"
34 #include "iscsi_target_erl2.h"
35 #include "iscsi_target_tpg.h"
36 #include "iscsi_target_util.h"
37 #include "iscsi_target.h"
39 #define PRINT_BUFF(buff, len) \
43 pr_debug("%d:\n", __LINE__); \
44 for (zzz = 0; zzz < len; zzz++) { \
45 if (zzz % 16 == 0) { \
48 pr_debug("%4i: ", zzz); \
50 pr_debug("%02x ", (unsigned char) (buff)[zzz]); \
56 extern struct list_head g_tiqn_list;
57 extern spinlock_t tiqn_lock;
60 * Called with cmd->r2t_lock held.
62 int iscsit_add_r2t_to_list(
63 struct iscsi_cmd *cmd,
69 struct iscsi_r2t *r2t;
71 r2t = kmem_cache_zalloc(lio_r2t_cache, GFP_ATOMIC);
73 pr_err("Unable to allocate memory for struct iscsi_r2t.\n");
76 INIT_LIST_HEAD(&r2t->r2t_list);
78 r2t->recovery_r2t = recovery;
79 r2t->r2t_sn = (!r2t_sn) ? cmd->r2t_sn++ : r2t_sn;
81 r2t->xfer_len = xfer_len;
82 list_add_tail(&r2t->r2t_list, &cmd->cmd_r2t_list);
83 spin_unlock_bh(&cmd->r2t_lock);
85 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T);
87 spin_lock_bh(&cmd->r2t_lock);
91 struct iscsi_r2t *iscsit_get_r2t_for_eos(
92 struct iscsi_cmd *cmd,
96 struct iscsi_r2t *r2t;
98 spin_lock_bh(&cmd->r2t_lock);
99 list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
100 if ((r2t->offset <= offset) &&
101 (r2t->offset + r2t->xfer_len) >= (offset + length)) {
102 spin_unlock_bh(&cmd->r2t_lock);
106 spin_unlock_bh(&cmd->r2t_lock);
108 pr_err("Unable to locate R2T for Offset: %u, Length:"
109 " %u\n", offset, length);
113 struct iscsi_r2t *iscsit_get_r2t_from_list(struct iscsi_cmd *cmd)
115 struct iscsi_r2t *r2t;
117 spin_lock_bh(&cmd->r2t_lock);
118 list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
119 if (!r2t->sent_r2t) {
120 spin_unlock_bh(&cmd->r2t_lock);
124 spin_unlock_bh(&cmd->r2t_lock);
126 pr_err("Unable to locate next R2T to send for ITT:"
127 " 0x%08x.\n", cmd->init_task_tag);
132 * Called with cmd->r2t_lock held.
134 void iscsit_free_r2t(struct iscsi_r2t *r2t, struct iscsi_cmd *cmd)
136 list_del(&r2t->r2t_list);
137 kmem_cache_free(lio_r2t_cache, r2t);
140 void iscsit_free_r2ts_from_list(struct iscsi_cmd *cmd)
142 struct iscsi_r2t *r2t, *r2t_tmp;
144 spin_lock_bh(&cmd->r2t_lock);
145 list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list)
146 iscsit_free_r2t(r2t, cmd);
147 spin_unlock_bh(&cmd->r2t_lock);
150 static int iscsit_wait_for_tag(struct se_session *se_sess, int state, int *cpup)
153 DEFINE_SBQ_WAIT(wait);
154 struct sbq_wait_state *ws;
155 struct sbitmap_queue *sbq;
157 if (state == TASK_RUNNING)
160 sbq = &se_sess->sess_tag_pool;
163 sbitmap_prepare_to_wait(sbq, ws, &wait, state);
164 if (signal_pending_state(state, current))
166 tag = sbitmap_queue_get(sbq, cpup);
172 sbitmap_finish_wait(sbq, ws, &wait);
177 * May be called from software interrupt (timer) context for allocating
180 struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, int state)
182 struct iscsi_cmd *cmd;
183 struct se_session *se_sess = conn->sess->se_sess;
186 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
188 tag = iscsit_wait_for_tag(se_sess, state, &cpu);
192 size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
193 cmd = (struct iscsi_cmd *)(se_sess->sess_cmd_map + (tag * size));
194 memset(cmd, 0, size);
196 cmd->se_cmd.map_tag = tag;
197 cmd->se_cmd.map_cpu = cpu;
199 cmd->data_direction = DMA_NONE;
200 INIT_LIST_HEAD(&cmd->i_conn_node);
201 INIT_LIST_HEAD(&cmd->datain_list);
202 INIT_LIST_HEAD(&cmd->cmd_r2t_list);
203 spin_lock_init(&cmd->datain_lock);
204 spin_lock_init(&cmd->dataout_timeout_lock);
205 spin_lock_init(&cmd->istate_lock);
206 spin_lock_init(&cmd->error_lock);
207 spin_lock_init(&cmd->r2t_lock);
208 timer_setup(&cmd->dataout_timer, iscsit_handle_dataout_timeout, 0);
212 EXPORT_SYMBOL(iscsit_allocate_cmd);
214 struct iscsi_seq *iscsit_get_seq_holder_for_datain(
215 struct iscsi_cmd *cmd,
220 for (i = 0; i < cmd->seq_count; i++)
221 if (cmd->seq_list[i].seq_send_order == seq_send_order)
222 return &cmd->seq_list[i];
227 struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsi_cmd *cmd)
231 if (!cmd->seq_list) {
232 pr_err("struct iscsi_cmd->seq_list is NULL!\n");
236 for (i = 0; i < cmd->seq_count; i++) {
237 if (cmd->seq_list[i].type != SEQTYPE_NORMAL)
239 if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) {
240 cmd->seq_send_order++;
241 return &cmd->seq_list[i];
248 struct iscsi_r2t *iscsit_get_holder_for_r2tsn(
249 struct iscsi_cmd *cmd,
252 struct iscsi_r2t *r2t;
254 spin_lock_bh(&cmd->r2t_lock);
255 list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
256 if (r2t->r2t_sn == r2t_sn) {
257 spin_unlock_bh(&cmd->r2t_lock);
261 spin_unlock_bh(&cmd->r2t_lock);
266 static inline int iscsit_check_received_cmdsn(struct iscsi_session *sess, u32 cmdsn)
272 * This is the proper method of checking received CmdSN against
273 * ExpCmdSN and MaxCmdSN values, as well as accounting for out
274 * or order CmdSNs due to multiple connection sessions and/or
277 max_cmdsn = atomic_read(&sess->max_cmd_sn);
278 if (iscsi_sna_gt(cmdsn, max_cmdsn)) {
279 pr_err("Received CmdSN: 0x%08x is greater than"
280 " MaxCmdSN: 0x%08x, ignoring.\n", cmdsn, max_cmdsn);
281 ret = CMDSN_MAXCMDSN_OVERRUN;
283 } else if (cmdsn == sess->exp_cmd_sn) {
285 pr_debug("Received CmdSN matches ExpCmdSN,"
286 " incremented ExpCmdSN to: 0x%08x\n",
288 ret = CMDSN_NORMAL_OPERATION;
290 } else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) {
291 pr_debug("Received CmdSN: 0x%08x is greater"
292 " than ExpCmdSN: 0x%08x, not acknowledging.\n",
293 cmdsn, sess->exp_cmd_sn);
294 ret = CMDSN_HIGHER_THAN_EXP;
297 pr_err("Received CmdSN: 0x%08x is less than"
298 " ExpCmdSN: 0x%08x, ignoring.\n", cmdsn,
300 ret = CMDSN_LOWER_THAN_EXP;
307 * Commands may be received out of order if MC/S is in use.
308 * Ensure they are executed in CmdSN order.
310 int iscsit_sequence_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
311 unsigned char *buf, __be32 cmdsn)
315 u8 reason = ISCSI_REASON_BOOKMARK_NO_RESOURCES;
317 mutex_lock(&conn->sess->cmdsn_mutex);
319 cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, be32_to_cpu(cmdsn));
321 case CMDSN_NORMAL_OPERATION:
322 ret = iscsit_execute_cmd(cmd, 0);
323 if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list))
324 iscsit_execute_ooo_cmdsns(conn->sess);
327 ret = CMDSN_ERROR_CANNOT_RECOVER;
330 case CMDSN_HIGHER_THAN_EXP:
331 ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, be32_to_cpu(cmdsn));
334 ret = CMDSN_ERROR_CANNOT_RECOVER;
337 ret = CMDSN_HIGHER_THAN_EXP;
339 case CMDSN_LOWER_THAN_EXP:
340 case CMDSN_MAXCMDSN_OVERRUN:
342 cmd->i_state = ISTATE_REMOVE;
343 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
345 * Existing callers for iscsit_sequence_cmd() will silently
346 * ignore commands with CMDSN_LOWER_THAN_EXP, so force this
347 * return for CMDSN_MAXCMDSN_OVERRUN as well..
349 ret = CMDSN_LOWER_THAN_EXP;
352 mutex_unlock(&conn->sess->cmdsn_mutex);
355 iscsit_reject_cmd(cmd, reason, buf);
359 EXPORT_SYMBOL(iscsit_sequence_cmd);
361 int iscsit_check_unsolicited_dataout(struct iscsi_cmd *cmd, unsigned char *buf)
363 struct iscsi_conn *conn = cmd->conn;
364 struct se_cmd *se_cmd = &cmd->se_cmd;
365 struct iscsi_data *hdr = (struct iscsi_data *) buf;
366 u32 payload_length = ntoh24(hdr->dlength);
368 if (conn->sess->sess_ops->InitialR2T) {
369 pr_err("Received unexpected unsolicited data"
370 " while InitialR2T=Yes, protocol error.\n");
371 transport_send_check_condition_and_sense(se_cmd,
372 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
376 if ((cmd->first_burst_len + payload_length) >
377 conn->sess->sess_ops->FirstBurstLength) {
378 pr_err("Total %u bytes exceeds FirstBurstLength: %u"
379 " for this Unsolicited DataOut Burst.\n",
380 (cmd->first_burst_len + payload_length),
381 conn->sess->sess_ops->FirstBurstLength);
382 transport_send_check_condition_and_sense(se_cmd,
383 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
387 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))
390 if (((cmd->first_burst_len + payload_length) != cmd->se_cmd.data_length) &&
391 ((cmd->first_burst_len + payload_length) !=
392 conn->sess->sess_ops->FirstBurstLength)) {
393 pr_err("Unsolicited non-immediate data received %u"
394 " does not equal FirstBurstLength: %u, and does"
395 " not equal ExpXferLen %u.\n",
396 (cmd->first_burst_len + payload_length),
397 conn->sess->sess_ops->FirstBurstLength, cmd->se_cmd.data_length);
398 transport_send_check_condition_and_sense(se_cmd,
399 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
405 struct iscsi_cmd *iscsit_find_cmd_from_itt(
406 struct iscsi_conn *conn,
409 struct iscsi_cmd *cmd;
411 spin_lock_bh(&conn->cmd_lock);
412 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
413 if (cmd->init_task_tag == init_task_tag) {
414 spin_unlock_bh(&conn->cmd_lock);
418 spin_unlock_bh(&conn->cmd_lock);
420 pr_err("Unable to locate ITT: 0x%08x on CID: %hu",
421 init_task_tag, conn->cid);
424 EXPORT_SYMBOL(iscsit_find_cmd_from_itt);
426 struct iscsi_cmd *iscsit_find_cmd_from_itt_or_dump(
427 struct iscsi_conn *conn,
431 struct iscsi_cmd *cmd;
433 spin_lock_bh(&conn->cmd_lock);
434 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
435 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT)
437 if (cmd->init_task_tag == init_task_tag) {
438 spin_unlock_bh(&conn->cmd_lock);
442 spin_unlock_bh(&conn->cmd_lock);
444 pr_err("Unable to locate ITT: 0x%08x on CID: %hu,"
445 " dumping payload\n", init_task_tag, conn->cid);
447 iscsit_dump_data_payload(conn, length, 1);
451 EXPORT_SYMBOL(iscsit_find_cmd_from_itt_or_dump);
453 struct iscsi_cmd *iscsit_find_cmd_from_ttt(
454 struct iscsi_conn *conn,
457 struct iscsi_cmd *cmd = NULL;
459 spin_lock_bh(&conn->cmd_lock);
460 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
461 if (cmd->targ_xfer_tag == targ_xfer_tag) {
462 spin_unlock_bh(&conn->cmd_lock);
466 spin_unlock_bh(&conn->cmd_lock);
468 pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n",
469 targ_xfer_tag, conn->cid);
473 int iscsit_find_cmd_for_recovery(
474 struct iscsi_session *sess,
475 struct iscsi_cmd **cmd_ptr,
476 struct iscsi_conn_recovery **cr_ptr,
479 struct iscsi_cmd *cmd = NULL;
480 struct iscsi_conn_recovery *cr;
482 * Scan through the inactive connection recovery list's command list.
483 * If init_task_tag matches the command is still alligent.
485 spin_lock(&sess->cr_i_lock);
486 list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
487 spin_lock(&cr->conn_recovery_cmd_lock);
488 list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
489 if (cmd->init_task_tag == init_task_tag) {
490 spin_unlock(&cr->conn_recovery_cmd_lock);
491 spin_unlock(&sess->cr_i_lock);
498 spin_unlock(&cr->conn_recovery_cmd_lock);
500 spin_unlock(&sess->cr_i_lock);
502 * Scan through the active connection recovery list's command list.
503 * If init_task_tag matches the command is ready to be reassigned.
505 spin_lock(&sess->cr_a_lock);
506 list_for_each_entry(cr, &sess->cr_active_list, cr_list) {
507 spin_lock(&cr->conn_recovery_cmd_lock);
508 list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
509 if (cmd->init_task_tag == init_task_tag) {
510 spin_unlock(&cr->conn_recovery_cmd_lock);
511 spin_unlock(&sess->cr_a_lock);
518 spin_unlock(&cr->conn_recovery_cmd_lock);
520 spin_unlock(&sess->cr_a_lock);
525 void iscsit_add_cmd_to_immediate_queue(
526 struct iscsi_cmd *cmd,
527 struct iscsi_conn *conn,
530 struct iscsi_queue_req *qr;
532 qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
534 pr_err("Unable to allocate memory for"
535 " struct iscsi_queue_req\n");
538 INIT_LIST_HEAD(&qr->qr_list);
542 spin_lock_bh(&conn->immed_queue_lock);
543 list_add_tail(&qr->qr_list, &conn->immed_queue_list);
544 atomic_inc(&cmd->immed_queue_count);
545 atomic_set(&conn->check_immediate_queue, 1);
546 spin_unlock_bh(&conn->immed_queue_lock);
548 wake_up(&conn->queues_wq);
550 EXPORT_SYMBOL(iscsit_add_cmd_to_immediate_queue);
552 struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsi_conn *conn)
554 struct iscsi_queue_req *qr;
556 spin_lock_bh(&conn->immed_queue_lock);
557 if (list_empty(&conn->immed_queue_list)) {
558 spin_unlock_bh(&conn->immed_queue_lock);
561 qr = list_first_entry(&conn->immed_queue_list,
562 struct iscsi_queue_req, qr_list);
564 list_del(&qr->qr_list);
566 atomic_dec(&qr->cmd->immed_queue_count);
567 spin_unlock_bh(&conn->immed_queue_lock);
572 static void iscsit_remove_cmd_from_immediate_queue(
573 struct iscsi_cmd *cmd,
574 struct iscsi_conn *conn)
576 struct iscsi_queue_req *qr, *qr_tmp;
578 spin_lock_bh(&conn->immed_queue_lock);
579 if (!atomic_read(&cmd->immed_queue_count)) {
580 spin_unlock_bh(&conn->immed_queue_lock);
584 list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
588 atomic_dec(&qr->cmd->immed_queue_count);
589 list_del(&qr->qr_list);
590 kmem_cache_free(lio_qr_cache, qr);
592 spin_unlock_bh(&conn->immed_queue_lock);
594 if (atomic_read(&cmd->immed_queue_count)) {
595 pr_err("ITT: 0x%08x immed_queue_count: %d\n",
597 atomic_read(&cmd->immed_queue_count));
601 int iscsit_add_cmd_to_response_queue(
602 struct iscsi_cmd *cmd,
603 struct iscsi_conn *conn,
606 struct iscsi_queue_req *qr;
608 qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
610 pr_err("Unable to allocate memory for"
611 " struct iscsi_queue_req\n");
614 INIT_LIST_HEAD(&qr->qr_list);
618 spin_lock_bh(&conn->response_queue_lock);
619 list_add_tail(&qr->qr_list, &conn->response_queue_list);
620 atomic_inc(&cmd->response_queue_count);
621 spin_unlock_bh(&conn->response_queue_lock);
623 wake_up(&conn->queues_wq);
627 struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsi_conn *conn)
629 struct iscsi_queue_req *qr;
631 spin_lock_bh(&conn->response_queue_lock);
632 if (list_empty(&conn->response_queue_list)) {
633 spin_unlock_bh(&conn->response_queue_lock);
637 qr = list_first_entry(&conn->response_queue_list,
638 struct iscsi_queue_req, qr_list);
640 list_del(&qr->qr_list);
642 atomic_dec(&qr->cmd->response_queue_count);
643 spin_unlock_bh(&conn->response_queue_lock);
648 static void iscsit_remove_cmd_from_response_queue(
649 struct iscsi_cmd *cmd,
650 struct iscsi_conn *conn)
652 struct iscsi_queue_req *qr, *qr_tmp;
654 spin_lock_bh(&conn->response_queue_lock);
655 if (!atomic_read(&cmd->response_queue_count)) {
656 spin_unlock_bh(&conn->response_queue_lock);
660 list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
665 atomic_dec(&qr->cmd->response_queue_count);
666 list_del(&qr->qr_list);
667 kmem_cache_free(lio_qr_cache, qr);
669 spin_unlock_bh(&conn->response_queue_lock);
671 if (atomic_read(&cmd->response_queue_count)) {
672 pr_err("ITT: 0x%08x response_queue_count: %d\n",
674 atomic_read(&cmd->response_queue_count));
678 bool iscsit_conn_all_queues_empty(struct iscsi_conn *conn)
682 spin_lock_bh(&conn->immed_queue_lock);
683 empty = list_empty(&conn->immed_queue_list);
684 spin_unlock_bh(&conn->immed_queue_lock);
689 spin_lock_bh(&conn->response_queue_lock);
690 empty = list_empty(&conn->response_queue_list);
691 spin_unlock_bh(&conn->response_queue_lock);
696 void iscsit_free_queue_reqs_for_conn(struct iscsi_conn *conn)
698 struct iscsi_queue_req *qr, *qr_tmp;
700 spin_lock_bh(&conn->immed_queue_lock);
701 list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
702 list_del(&qr->qr_list);
704 atomic_dec(&qr->cmd->immed_queue_count);
706 kmem_cache_free(lio_qr_cache, qr);
708 spin_unlock_bh(&conn->immed_queue_lock);
710 spin_lock_bh(&conn->response_queue_lock);
711 list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
713 list_del(&qr->qr_list);
715 atomic_dec(&qr->cmd->response_queue_count);
717 kmem_cache_free(lio_qr_cache, qr);
719 spin_unlock_bh(&conn->response_queue_lock);
722 void iscsit_release_cmd(struct iscsi_cmd *cmd)
724 struct iscsi_session *sess;
725 struct se_cmd *se_cmd = &cmd->se_cmd;
727 WARN_ON(!list_empty(&cmd->i_conn_node));
730 sess = cmd->conn->sess;
734 BUG_ON(!sess || !sess->se_sess);
737 kfree(cmd->pdu_list);
738 kfree(cmd->seq_list);
740 kfree(cmd->iov_data);
741 kfree(cmd->text_in_ptr);
743 target_free_tag(sess->se_sess, se_cmd);
745 EXPORT_SYMBOL(iscsit_release_cmd);
747 void __iscsit_free_cmd(struct iscsi_cmd *cmd, bool check_queues)
749 struct iscsi_conn *conn = cmd->conn;
751 WARN_ON(!list_empty(&cmd->i_conn_node));
753 if (cmd->data_direction == DMA_TO_DEVICE) {
754 iscsit_stop_dataout_timer(cmd);
755 iscsit_free_r2ts_from_list(cmd);
757 if (cmd->data_direction == DMA_FROM_DEVICE)
758 iscsit_free_all_datain_reqs(cmd);
760 if (conn && check_queues) {
761 iscsit_remove_cmd_from_immediate_queue(cmd, conn);
762 iscsit_remove_cmd_from_response_queue(cmd, conn);
765 if (conn && conn->conn_transport->iscsit_release_cmd)
766 conn->conn_transport->iscsit_release_cmd(conn, cmd);
769 void iscsit_free_cmd(struct iscsi_cmd *cmd, bool shutdown)
771 struct se_cmd *se_cmd = cmd->se_cmd.se_tfo ? &cmd->se_cmd : NULL;
774 __iscsit_free_cmd(cmd, shutdown);
776 rc = transport_generic_free_cmd(se_cmd, shutdown);
777 if (!rc && shutdown && se_cmd->se_sess) {
778 __iscsit_free_cmd(cmd, shutdown);
779 target_put_sess_cmd(se_cmd);
782 iscsit_release_cmd(cmd);
785 EXPORT_SYMBOL(iscsit_free_cmd);
787 int iscsit_check_session_usage_count(struct iscsi_session *sess)
789 spin_lock_bh(&sess->session_usage_lock);
790 if (sess->session_usage_count != 0) {
791 sess->session_waiting_on_uc = 1;
792 spin_unlock_bh(&sess->session_usage_lock);
796 wait_for_completion(&sess->session_waiting_on_uc_comp);
799 spin_unlock_bh(&sess->session_usage_lock);
804 void iscsit_dec_session_usage_count(struct iscsi_session *sess)
806 spin_lock_bh(&sess->session_usage_lock);
807 sess->session_usage_count--;
809 if (!sess->session_usage_count && sess->session_waiting_on_uc)
810 complete(&sess->session_waiting_on_uc_comp);
812 spin_unlock_bh(&sess->session_usage_lock);
815 void iscsit_inc_session_usage_count(struct iscsi_session *sess)
817 spin_lock_bh(&sess->session_usage_lock);
818 sess->session_usage_count++;
819 spin_unlock_bh(&sess->session_usage_lock);
822 struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
824 struct iscsi_conn *conn;
826 spin_lock_bh(&sess->conn_lock);
827 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
828 if ((conn->cid == cid) &&
829 (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
830 iscsit_inc_conn_usage_count(conn);
831 spin_unlock_bh(&sess->conn_lock);
835 spin_unlock_bh(&sess->conn_lock);
840 struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
842 struct iscsi_conn *conn;
844 spin_lock_bh(&sess->conn_lock);
845 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
846 if (conn->cid == cid) {
847 iscsit_inc_conn_usage_count(conn);
848 spin_lock(&conn->state_lock);
849 atomic_set(&conn->connection_wait_rcfr, 1);
850 spin_unlock(&conn->state_lock);
851 spin_unlock_bh(&sess->conn_lock);
855 spin_unlock_bh(&sess->conn_lock);
860 void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
862 spin_lock_bh(&conn->conn_usage_lock);
863 if (conn->conn_usage_count != 0) {
864 conn->conn_waiting_on_uc = 1;
865 spin_unlock_bh(&conn->conn_usage_lock);
867 wait_for_completion(&conn->conn_waiting_on_uc_comp);
870 spin_unlock_bh(&conn->conn_usage_lock);
873 void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
875 spin_lock_bh(&conn->conn_usage_lock);
876 conn->conn_usage_count--;
878 if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
879 complete(&conn->conn_waiting_on_uc_comp);
881 spin_unlock_bh(&conn->conn_usage_lock);
884 void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
886 spin_lock_bh(&conn->conn_usage_lock);
887 conn->conn_usage_count++;
888 spin_unlock_bh(&conn->conn_usage_lock);
891 static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
894 struct iscsi_cmd *cmd;
896 cmd = iscsit_allocate_cmd(conn, TASK_RUNNING);
900 cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
901 state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
902 ISTATE_SEND_NOPIN_NO_RESPONSE;
903 cmd->init_task_tag = RESERVED_ITT;
904 cmd->targ_xfer_tag = (want_response) ?
905 session_get_next_ttt(conn->sess) : 0xFFFFFFFF;
906 spin_lock_bh(&conn->cmd_lock);
907 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
908 spin_unlock_bh(&conn->cmd_lock);
911 iscsit_start_nopin_response_timer(conn);
912 iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
917 void iscsit_handle_nopin_response_timeout(struct timer_list *t)
919 struct iscsi_conn *conn = from_timer(conn, t, nopin_response_timer);
920 struct iscsi_session *sess = conn->sess;
922 iscsit_inc_conn_usage_count(conn);
924 spin_lock_bh(&conn->nopin_timer_lock);
925 if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
926 spin_unlock_bh(&conn->nopin_timer_lock);
927 iscsit_dec_conn_usage_count(conn);
931 pr_err("Did not receive response to NOPIN on CID: %hu, failing"
932 " connection for I_T Nexus %s,i,0x%6phN,%s,t,0x%02x\n",
933 conn->cid, sess->sess_ops->InitiatorName, sess->isid,
934 sess->tpg->tpg_tiqn->tiqn, (u32)sess->tpg->tpgt);
935 conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
936 spin_unlock_bh(&conn->nopin_timer_lock);
938 iscsit_fill_cxn_timeout_err_stats(sess);
939 iscsit_cause_connection_reinstatement(conn, 0);
940 iscsit_dec_conn_usage_count(conn);
943 void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
945 struct iscsi_session *sess = conn->sess;
946 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
948 spin_lock_bh(&conn->nopin_timer_lock);
949 if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
950 spin_unlock_bh(&conn->nopin_timer_lock);
954 mod_timer(&conn->nopin_response_timer,
955 (get_jiffies_64() + na->nopin_response_timeout * HZ));
956 spin_unlock_bh(&conn->nopin_timer_lock);
960 * Called with conn->nopin_timer_lock held.
962 void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
964 struct iscsi_session *sess = conn->sess;
965 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
967 spin_lock_bh(&conn->nopin_timer_lock);
968 if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
969 spin_unlock_bh(&conn->nopin_timer_lock);
973 conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
974 conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
975 mod_timer(&conn->nopin_response_timer,
976 jiffies + na->nopin_response_timeout * HZ);
978 pr_debug("Started NOPIN Response Timer on CID: %d to %u"
979 " seconds\n", conn->cid, na->nopin_response_timeout);
980 spin_unlock_bh(&conn->nopin_timer_lock);
983 void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
985 spin_lock_bh(&conn->nopin_timer_lock);
986 if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
987 spin_unlock_bh(&conn->nopin_timer_lock);
990 conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
991 spin_unlock_bh(&conn->nopin_timer_lock);
993 del_timer_sync(&conn->nopin_response_timer);
995 spin_lock_bh(&conn->nopin_timer_lock);
996 conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
997 spin_unlock_bh(&conn->nopin_timer_lock);
1000 void iscsit_handle_nopin_timeout(struct timer_list *t)
1002 struct iscsi_conn *conn = from_timer(conn, t, nopin_timer);
1004 iscsit_inc_conn_usage_count(conn);
1006 spin_lock_bh(&conn->nopin_timer_lock);
1007 if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
1008 spin_unlock_bh(&conn->nopin_timer_lock);
1009 iscsit_dec_conn_usage_count(conn);
1012 conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1013 spin_unlock_bh(&conn->nopin_timer_lock);
1015 iscsit_add_nopin(conn, 1);
1016 iscsit_dec_conn_usage_count(conn);
1020 * Called with conn->nopin_timer_lock held.
1022 void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
1024 struct iscsi_session *sess = conn->sess;
1025 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1027 * NOPIN timeout is disabled.
1029 if (!na->nopin_timeout)
1032 if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
1035 conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1036 conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
1037 mod_timer(&conn->nopin_timer, jiffies + na->nopin_timeout * HZ);
1039 pr_debug("Started NOPIN Timer on CID: %d at %u second"
1040 " interval\n", conn->cid, na->nopin_timeout);
1043 void iscsit_start_nopin_timer(struct iscsi_conn *conn)
1045 spin_lock_bh(&conn->nopin_timer_lock);
1046 __iscsit_start_nopin_timer(conn);
1047 spin_unlock_bh(&conn->nopin_timer_lock);
1050 void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
1052 spin_lock_bh(&conn->nopin_timer_lock);
1053 if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
1054 spin_unlock_bh(&conn->nopin_timer_lock);
1057 conn->nopin_timer_flags |= ISCSI_TF_STOP;
1058 spin_unlock_bh(&conn->nopin_timer_lock);
1060 del_timer_sync(&conn->nopin_timer);
1062 spin_lock_bh(&conn->nopin_timer_lock);
1063 conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1064 spin_unlock_bh(&conn->nopin_timer_lock);
1067 int iscsit_send_tx_data(
1068 struct iscsi_cmd *cmd,
1069 struct iscsi_conn *conn,
1072 int tx_sent, tx_size;
1077 tx_size = cmd->tx_size;
1080 iov = &cmd->iov_data[0];
1081 iov_count = cmd->iov_data_count;
1083 iov = &cmd->iov_misc[0];
1084 iov_count = cmd->iov_misc_count;
1087 tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
1088 if (tx_size != tx_sent) {
1089 if (tx_sent == -EAGAIN) {
1090 pr_err("tx_data() returned -EAGAIN\n");
1100 int iscsit_fe_sendpage_sg(
1101 struct iscsi_cmd *cmd,
1102 struct iscsi_conn *conn)
1104 struct scatterlist *sg = cmd->first_data_sg;
1106 u32 tx_hdr_size, data_len;
1107 u32 offset = cmd->first_data_sg_off;
1108 int tx_sent, iov_off;
1111 tx_hdr_size = ISCSI_HDR_LEN;
1112 if (conn->conn_ops->HeaderDigest)
1113 tx_hdr_size += ISCSI_CRC_LEN;
1115 iov.iov_base = cmd->pdu;
1116 iov.iov_len = tx_hdr_size;
1118 tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
1119 if (tx_hdr_size != tx_sent) {
1120 if (tx_sent == -EAGAIN) {
1121 pr_err("tx_data() returned -EAGAIN\n");
1127 data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
1129 * Set iov_off used by padding and data digest tx_data() calls below
1130 * in order to determine proper offset into cmd->iov_data[]
1132 if (conn->conn_ops->DataDigest) {
1133 data_len -= ISCSI_CRC_LEN;
1135 iov_off = (cmd->iov_data_count - 2);
1137 iov_off = (cmd->iov_data_count - 1);
1139 iov_off = (cmd->iov_data_count - 1);
1142 * Perform sendpage() for each page in the scatterlist
1145 u32 space = (sg->length - offset);
1146 u32 sub_len = min_t(u32, data_len, space);
1148 tx_sent = conn->sock->ops->sendpage(conn->sock,
1149 sg_page(sg), sg->offset + offset, sub_len, 0);
1150 if (tx_sent != sub_len) {
1151 if (tx_sent == -EAGAIN) {
1152 pr_err("tcp_sendpage() returned"
1157 pr_err("tcp_sendpage() failure: %d\n",
1162 data_len -= sub_len;
1169 struct kvec *iov_p = &cmd->iov_data[iov_off++];
1171 tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
1172 if (cmd->padding != tx_sent) {
1173 if (tx_sent == -EAGAIN) {
1174 pr_err("tx_data() returned -EAGAIN\n");
1182 if (conn->conn_ops->DataDigest) {
1183 struct kvec *iov_d = &cmd->iov_data[iov_off];
1185 tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
1186 if (ISCSI_CRC_LEN != tx_sent) {
1187 if (tx_sent == -EAGAIN) {
1188 pr_err("tx_data() returned -EAGAIN\n");
1199 * This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
1200 * back to the Initiator when an expection condition occurs with the
1201 * errors set in status_class and status_detail.
1203 * Parameters: iSCSI Connection, Status Class, Status Detail.
1204 * Returns: 0 on success, -1 on error.
1206 int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
1208 struct iscsi_login_rsp *hdr;
1209 struct iscsi_login *login = conn->conn_login;
1211 login->login_failed = 1;
1212 iscsit_collect_login_stats(conn, status_class, status_detail);
1214 memset(&login->rsp[0], 0, ISCSI_HDR_LEN);
1216 hdr = (struct iscsi_login_rsp *)&login->rsp[0];
1217 hdr->opcode = ISCSI_OP_LOGIN_RSP;
1218 hdr->status_class = status_class;
1219 hdr->status_detail = status_detail;
1220 hdr->itt = conn->login_itt;
1222 return conn->conn_transport->iscsit_put_login_tx(conn, login, 0);
1225 void iscsit_print_session_params(struct iscsi_session *sess)
1227 struct iscsi_conn *conn;
1229 pr_debug("-----------------------------[Session Params for"
1230 " SID: %u]-----------------------------\n", sess->sid);
1231 spin_lock_bh(&sess->conn_lock);
1232 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
1233 iscsi_dump_conn_ops(conn->conn_ops);
1234 spin_unlock_bh(&sess->conn_lock);
1236 iscsi_dump_sess_ops(sess->sess_ops);
1239 static int iscsit_do_rx_data(
1240 struct iscsi_conn *conn,
1241 struct iscsi_data_count *count)
1243 int data = count->data_length, rx_loop = 0, total_rx = 0;
1246 if (!conn || !conn->sock || !conn->conn_ops)
1249 memset(&msg, 0, sizeof(struct msghdr));
1250 iov_iter_kvec(&msg.msg_iter, READ, count->iov, count->iov_count, data);
1252 while (msg_data_left(&msg)) {
1253 rx_loop = sock_recvmsg(conn->sock, &msg, MSG_WAITALL);
1255 pr_debug("rx_loop: %d total_rx: %d\n",
1259 total_rx += rx_loop;
1260 pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
1261 rx_loop, total_rx, data);
1268 struct iscsi_conn *conn,
1273 struct iscsi_data_count c;
1275 if (!conn || !conn->sock || !conn->conn_ops)
1278 memset(&c, 0, sizeof(struct iscsi_data_count));
1280 c.iov_count = iov_count;
1281 c.data_length = data;
1282 c.type = ISCSI_RX_DATA;
1284 return iscsit_do_rx_data(conn, &c);
1288 struct iscsi_conn *conn,
1296 if (!conn || !conn->sock || !conn->conn_ops)
1300 pr_err("Data length is: %d\n", data);
1304 memset(&msg, 0, sizeof(struct msghdr));
1306 iov_iter_kvec(&msg.msg_iter, WRITE, iov, iov_count, data);
1308 while (msg_data_left(&msg)) {
1309 int tx_loop = sock_sendmsg(conn->sock, &msg);
1311 pr_debug("tx_loop: %d total_tx %d\n",
1315 total_tx += tx_loop;
1316 pr_debug("tx_loop: %d, total_tx: %d, data: %d\n",
1317 tx_loop, total_tx, data);
1323 void iscsit_collect_login_stats(
1324 struct iscsi_conn *conn,
1328 struct iscsi_param *intrname = NULL;
1329 struct iscsi_tiqn *tiqn;
1330 struct iscsi_login_stats *ls;
1332 tiqn = iscsit_snmp_get_tiqn(conn);
1336 ls = &tiqn->login_stats;
1338 spin_lock(&ls->lock);
1339 if (status_class == ISCSI_STATUS_CLS_SUCCESS)
1341 else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
1343 ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
1344 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1345 (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
1346 ls->authenticate_fails++;
1347 ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHENTICATE;
1348 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1349 (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
1350 ls->authorize_fails++;
1351 ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
1352 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1353 (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
1354 ls->negotiate_fails++;
1355 ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
1358 ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
1361 /* Save initiator name, ip address and time, if it is a failed login */
1362 if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
1363 if (conn->param_list)
1364 intrname = iscsi_find_param_from_key(INITIATORNAME,
1366 strlcpy(ls->last_intr_fail_name,
1367 (intrname ? intrname->value : "Unknown"),
1368 sizeof(ls->last_intr_fail_name));
1370 ls->last_intr_fail_ip_family = conn->login_family;
1372 ls->last_intr_fail_sockaddr = conn->login_sockaddr;
1373 ls->last_fail_time = get_jiffies_64();
1376 spin_unlock(&ls->lock);
1379 struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn)
1381 struct iscsi_portal_group *tpg;
1393 return tpg->tpg_tiqn;
1396 void iscsit_fill_cxn_timeout_err_stats(struct iscsi_session *sess)
1398 struct iscsi_portal_group *tpg = sess->tpg;
1399 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
1404 spin_lock_bh(&tiqn->sess_err_stats.lock);
1405 strlcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
1406 sess->sess_ops->InitiatorName,
1407 sizeof(tiqn->sess_err_stats.last_sess_fail_rem_name));
1408 tiqn->sess_err_stats.last_sess_failure_type =
1409 ISCSI_SESS_ERR_CXN_TIMEOUT;
1410 tiqn->sess_err_stats.cxn_timeout_errors++;
1411 atomic_long_inc(&sess->conn_timeout_errors);
1412 spin_unlock_bh(&tiqn->sess_err_stats.lock);