1 /*******************************************************************************
2 * This file contains the iSCSI Target specific utility functions.
4 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
6 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 ******************************************************************************/
21 #include <linux/list.h>
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/target_core_configfs.h>
28 #include "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_tq.h"
37 #include "iscsi_target_util.h"
38 #include "iscsi_target.h"
40 #define PRINT_BUFF(buff, len) \
44 pr_debug("%d:\n", __LINE__); \
45 for (zzz = 0; zzz < len; zzz++) { \
46 if (zzz % 16 == 0) { \
49 pr_debug("%4i: ", zzz); \
51 pr_debug("%02x ", (unsigned char) (buff)[zzz]); \
57 extern struct list_head g_tiqn_list;
58 extern spinlock_t tiqn_lock;
61 * Called with cmd->r2t_lock held.
63 int iscsit_add_r2t_to_list(
64 struct iscsi_cmd *cmd,
70 struct iscsi_r2t *r2t;
72 r2t = kmem_cache_zalloc(lio_r2t_cache, GFP_ATOMIC);
74 pr_err("Unable to allocate memory for struct iscsi_r2t.\n");
77 INIT_LIST_HEAD(&r2t->r2t_list);
79 r2t->recovery_r2t = recovery;
80 r2t->r2t_sn = (!r2t_sn) ? cmd->r2t_sn++ : r2t_sn;
82 r2t->xfer_len = xfer_len;
83 list_add_tail(&r2t->r2t_list, &cmd->cmd_r2t_list);
84 spin_unlock_bh(&cmd->r2t_lock);
86 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T);
88 spin_lock_bh(&cmd->r2t_lock);
92 struct iscsi_r2t *iscsit_get_r2t_for_eos(
93 struct iscsi_cmd *cmd,
97 struct iscsi_r2t *r2t;
99 spin_lock_bh(&cmd->r2t_lock);
100 list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
101 if ((r2t->offset <= offset) &&
102 (r2t->offset + r2t->xfer_len) >= (offset + length)) {
103 spin_unlock_bh(&cmd->r2t_lock);
107 spin_unlock_bh(&cmd->r2t_lock);
109 pr_err("Unable to locate R2T for Offset: %u, Length:"
110 " %u\n", offset, length);
114 struct iscsi_r2t *iscsit_get_r2t_from_list(struct iscsi_cmd *cmd)
116 struct iscsi_r2t *r2t;
118 spin_lock_bh(&cmd->r2t_lock);
119 list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
120 if (!r2t->sent_r2t) {
121 spin_unlock_bh(&cmd->r2t_lock);
125 spin_unlock_bh(&cmd->r2t_lock);
127 pr_err("Unable to locate next R2T to send for ITT:"
128 " 0x%08x.\n", cmd->init_task_tag);
133 * Called with cmd->r2t_lock held.
135 void iscsit_free_r2t(struct iscsi_r2t *r2t, struct iscsi_cmd *cmd)
137 list_del(&r2t->r2t_list);
138 kmem_cache_free(lio_r2t_cache, r2t);
141 void iscsit_free_r2ts_from_list(struct iscsi_cmd *cmd)
143 struct iscsi_r2t *r2t, *r2t_tmp;
145 spin_lock_bh(&cmd->r2t_lock);
146 list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list)
147 iscsit_free_r2t(r2t, cmd);
148 spin_unlock_bh(&cmd->r2t_lock);
152 * May be called from software interrupt (timer) context for allocating
155 struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, gfp_t gfp_mask)
157 struct iscsi_cmd *cmd;
159 cmd = kmem_cache_zalloc(lio_cmd_cache, gfp_mask);
161 pr_err("Unable to allocate memory for struct iscsi_cmd.\n");
166 INIT_LIST_HEAD(&cmd->i_list);
167 INIT_LIST_HEAD(&cmd->datain_list);
168 INIT_LIST_HEAD(&cmd->cmd_r2t_list);
169 init_completion(&cmd->reject_comp);
170 spin_lock_init(&cmd->datain_lock);
171 spin_lock_init(&cmd->dataout_timeout_lock);
172 spin_lock_init(&cmd->istate_lock);
173 spin_lock_init(&cmd->error_lock);
174 spin_lock_init(&cmd->r2t_lock);
180 * Called from iscsi_handle_scsi_cmd()
182 struct iscsi_cmd *iscsit_allocate_se_cmd(
183 struct iscsi_conn *conn,
188 struct iscsi_cmd *cmd;
189 struct se_cmd *se_cmd;
192 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
196 cmd->data_direction = data_direction;
197 cmd->data_length = data_length;
199 * Figure out the SAM Task Attribute for the incoming SCSI CDB
201 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
202 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
203 sam_task_attr = MSG_SIMPLE_TAG;
204 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
205 sam_task_attr = MSG_ORDERED_TAG;
206 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
207 sam_task_attr = MSG_HEAD_TAG;
208 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
209 sam_task_attr = MSG_ACA_TAG;
211 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
212 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
213 sam_task_attr = MSG_SIMPLE_TAG;
216 se_cmd = &cmd->se_cmd;
218 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
220 transport_init_se_cmd(se_cmd, &lio_target_fabric_configfs->tf_ops,
221 conn->sess->se_sess, data_length, data_direction,
222 sam_task_attr, &cmd->sense_buffer[0]);
226 struct iscsi_cmd *iscsit_allocate_se_cmd_for_tmr(
227 struct iscsi_conn *conn,
230 struct iscsi_cmd *cmd;
231 struct se_cmd *se_cmd;
235 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
239 cmd->data_direction = DMA_NONE;
241 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
243 pr_err("Unable to allocate memory for"
244 " Task Management command!\n");
248 * TASK_REASSIGN for ERL=2 / connection stays inside of
249 * LIO-Target $FABRIC_MOD
251 if (function == ISCSI_TM_FUNC_TASK_REASSIGN)
254 se_cmd = &cmd->se_cmd;
256 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
258 transport_init_se_cmd(se_cmd, &lio_target_fabric_configfs->tf_ops,
259 conn->sess->se_sess, 0, DMA_NONE,
260 MSG_SIMPLE_TAG, &cmd->sense_buffer[0]);
263 case ISCSI_TM_FUNC_ABORT_TASK:
264 tcm_function = TMR_ABORT_TASK;
266 case ISCSI_TM_FUNC_ABORT_TASK_SET:
267 tcm_function = TMR_ABORT_TASK_SET;
269 case ISCSI_TM_FUNC_CLEAR_ACA:
270 tcm_function = TMR_CLEAR_ACA;
272 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
273 tcm_function = TMR_CLEAR_TASK_SET;
275 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
276 tcm_function = TMR_LUN_RESET;
278 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
279 tcm_function = TMR_TARGET_WARM_RESET;
281 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
282 tcm_function = TMR_TARGET_COLD_RESET;
285 pr_err("Unknown iSCSI TMR Function:"
286 " 0x%02x\n", function);
290 rc = core_tmr_alloc_req(se_cmd, cmd->tmr_req, tcm_function, GFP_KERNEL);
294 cmd->tmr_req->se_tmr_req = se_cmd->se_tmr_req;
298 iscsit_release_cmd(cmd);
302 int iscsit_decide_list_to_build(
303 struct iscsi_cmd *cmd,
304 u32 immediate_data_length)
306 struct iscsi_build_list bl;
307 struct iscsi_conn *conn = cmd->conn;
308 struct iscsi_session *sess = conn->sess;
309 struct iscsi_node_attrib *na;
311 if (sess->sess_ops->DataSequenceInOrder &&
312 sess->sess_ops->DataPDUInOrder)
315 if (cmd->data_direction == DMA_NONE)
318 na = iscsit_tpg_get_node_attrib(sess);
319 memset(&bl, 0, sizeof(struct iscsi_build_list));
321 if (cmd->data_direction == DMA_FROM_DEVICE) {
322 bl.data_direction = ISCSI_PDU_READ;
323 bl.type = PDULIST_NORMAL;
324 if (na->random_datain_pdu_offsets)
325 bl.randomize |= RANDOM_DATAIN_PDU_OFFSETS;
326 if (na->random_datain_seq_offsets)
327 bl.randomize |= RANDOM_DATAIN_SEQ_OFFSETS;
329 bl.data_direction = ISCSI_PDU_WRITE;
330 bl.immediate_data_length = immediate_data_length;
331 if (na->random_r2t_offsets)
332 bl.randomize |= RANDOM_R2T_OFFSETS;
334 if (!cmd->immediate_data && !cmd->unsolicited_data)
335 bl.type = PDULIST_NORMAL;
336 else if (cmd->immediate_data && !cmd->unsolicited_data)
337 bl.type = PDULIST_IMMEDIATE;
338 else if (!cmd->immediate_data && cmd->unsolicited_data)
339 bl.type = PDULIST_UNSOLICITED;
340 else if (cmd->immediate_data && cmd->unsolicited_data)
341 bl.type = PDULIST_IMMEDIATE_AND_UNSOLICITED;
344 return iscsit_do_build_list(cmd, &bl);
347 struct iscsi_seq *iscsit_get_seq_holder_for_datain(
348 struct iscsi_cmd *cmd,
353 for (i = 0; i < cmd->seq_count; i++)
354 if (cmd->seq_list[i].seq_send_order == seq_send_order)
355 return &cmd->seq_list[i];
360 struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsi_cmd *cmd)
364 if (!cmd->seq_list) {
365 pr_err("struct iscsi_cmd->seq_list is NULL!\n");
369 for (i = 0; i < cmd->seq_count; i++) {
370 if (cmd->seq_list[i].type != SEQTYPE_NORMAL)
372 if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) {
373 cmd->seq_send_order++;
374 return &cmd->seq_list[i];
381 struct iscsi_r2t *iscsit_get_holder_for_r2tsn(
382 struct iscsi_cmd *cmd,
385 struct iscsi_r2t *r2t;
387 spin_lock_bh(&cmd->r2t_lock);
388 list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
389 if (r2t->r2t_sn == r2t_sn) {
390 spin_unlock_bh(&cmd->r2t_lock);
394 spin_unlock_bh(&cmd->r2t_lock);
399 static inline int iscsit_check_received_cmdsn(struct iscsi_session *sess, u32 cmdsn)
404 * This is the proper method of checking received CmdSN against
405 * ExpCmdSN and MaxCmdSN values, as well as accounting for out
406 * or order CmdSNs due to multiple connection sessions and/or
409 if (iscsi_sna_gt(cmdsn, sess->max_cmd_sn)) {
410 pr_err("Received CmdSN: 0x%08x is greater than"
411 " MaxCmdSN: 0x%08x, protocol error.\n", cmdsn,
413 ret = CMDSN_ERROR_CANNOT_RECOVER;
415 } else if (cmdsn == sess->exp_cmd_sn) {
417 pr_debug("Received CmdSN matches ExpCmdSN,"
418 " incremented ExpCmdSN to: 0x%08x\n",
420 ret = CMDSN_NORMAL_OPERATION;
422 } else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) {
423 pr_debug("Received CmdSN: 0x%08x is greater"
424 " than ExpCmdSN: 0x%08x, not acknowledging.\n",
425 cmdsn, sess->exp_cmd_sn);
426 ret = CMDSN_HIGHER_THAN_EXP;
429 pr_err("Received CmdSN: 0x%08x is less than"
430 " ExpCmdSN: 0x%08x, ignoring.\n", cmdsn,
432 ret = CMDSN_LOWER_THAN_EXP;
439 * Commands may be received out of order if MC/S is in use.
440 * Ensure they are executed in CmdSN order.
442 int iscsit_sequence_cmd(
443 struct iscsi_conn *conn,
444 struct iscsi_cmd *cmd,
450 mutex_lock(&conn->sess->cmdsn_mutex);
452 cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, cmdsn);
454 case CMDSN_NORMAL_OPERATION:
455 ret = iscsit_execute_cmd(cmd, 0);
456 if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list))
457 iscsit_execute_ooo_cmdsns(conn->sess);
459 case CMDSN_HIGHER_THAN_EXP:
460 ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, cmdsn);
462 case CMDSN_LOWER_THAN_EXP:
463 cmd->i_state = ISTATE_REMOVE;
464 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
471 mutex_unlock(&conn->sess->cmdsn_mutex);
476 int iscsit_check_unsolicited_dataout(struct iscsi_cmd *cmd, unsigned char *buf)
478 struct iscsi_conn *conn = cmd->conn;
479 struct se_cmd *se_cmd = &cmd->se_cmd;
480 struct iscsi_data *hdr = (struct iscsi_data *) buf;
481 u32 payload_length = ntoh24(hdr->dlength);
483 if (conn->sess->sess_ops->InitialR2T) {
484 pr_err("Received unexpected unsolicited data"
485 " while InitialR2T=Yes, protocol error.\n");
486 transport_send_check_condition_and_sense(se_cmd,
487 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
491 if ((cmd->first_burst_len + payload_length) >
492 conn->sess->sess_ops->FirstBurstLength) {
493 pr_err("Total %u bytes exceeds FirstBurstLength: %u"
494 " for this Unsolicited DataOut Burst.\n",
495 (cmd->first_burst_len + payload_length),
496 conn->sess->sess_ops->FirstBurstLength);
497 transport_send_check_condition_and_sense(se_cmd,
498 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
502 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))
505 if (((cmd->first_burst_len + payload_length) != cmd->data_length) &&
506 ((cmd->first_burst_len + payload_length) !=
507 conn->sess->sess_ops->FirstBurstLength)) {
508 pr_err("Unsolicited non-immediate data received %u"
509 " does not equal FirstBurstLength: %u, and does"
510 " not equal ExpXferLen %u.\n",
511 (cmd->first_burst_len + payload_length),
512 conn->sess->sess_ops->FirstBurstLength, cmd->data_length);
513 transport_send_check_condition_and_sense(se_cmd,
514 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
520 struct iscsi_cmd *iscsit_find_cmd_from_itt(
521 struct iscsi_conn *conn,
524 struct iscsi_cmd *cmd;
526 spin_lock_bh(&conn->cmd_lock);
527 list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
528 if (cmd->init_task_tag == init_task_tag) {
529 spin_unlock_bh(&conn->cmd_lock);
533 spin_unlock_bh(&conn->cmd_lock);
535 pr_err("Unable to locate ITT: 0x%08x on CID: %hu",
536 init_task_tag, conn->cid);
540 struct iscsi_cmd *iscsit_find_cmd_from_itt_or_dump(
541 struct iscsi_conn *conn,
545 struct iscsi_cmd *cmd;
547 spin_lock_bh(&conn->cmd_lock);
548 list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
549 if (cmd->init_task_tag == init_task_tag) {
550 spin_unlock_bh(&conn->cmd_lock);
554 spin_unlock_bh(&conn->cmd_lock);
556 pr_err("Unable to locate ITT: 0x%08x on CID: %hu,"
557 " dumping payload\n", init_task_tag, conn->cid);
559 iscsit_dump_data_payload(conn, length, 1);
564 struct iscsi_cmd *iscsit_find_cmd_from_ttt(
565 struct iscsi_conn *conn,
568 struct iscsi_cmd *cmd = NULL;
570 spin_lock_bh(&conn->cmd_lock);
571 list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
572 if (cmd->targ_xfer_tag == targ_xfer_tag) {
573 spin_unlock_bh(&conn->cmd_lock);
577 spin_unlock_bh(&conn->cmd_lock);
579 pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n",
580 targ_xfer_tag, conn->cid);
584 int iscsit_find_cmd_for_recovery(
585 struct iscsi_session *sess,
586 struct iscsi_cmd **cmd_ptr,
587 struct iscsi_conn_recovery **cr_ptr,
590 struct iscsi_cmd *cmd = NULL;
591 struct iscsi_conn_recovery *cr;
593 * Scan through the inactive connection recovery list's command list.
594 * If init_task_tag matches the command is still alligent.
596 spin_lock(&sess->cr_i_lock);
597 list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
598 spin_lock(&cr->conn_recovery_cmd_lock);
599 list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_list) {
600 if (cmd->init_task_tag == init_task_tag) {
601 spin_unlock(&cr->conn_recovery_cmd_lock);
602 spin_unlock(&sess->cr_i_lock);
609 spin_unlock(&cr->conn_recovery_cmd_lock);
611 spin_unlock(&sess->cr_i_lock);
613 * Scan through the active connection recovery list's command list.
614 * If init_task_tag matches the command is ready to be reassigned.
616 spin_lock(&sess->cr_a_lock);
617 list_for_each_entry(cr, &sess->cr_active_list, cr_list) {
618 spin_lock(&cr->conn_recovery_cmd_lock);
619 list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_list) {
620 if (cmd->init_task_tag == init_task_tag) {
621 spin_unlock(&cr->conn_recovery_cmd_lock);
622 spin_unlock(&sess->cr_a_lock);
629 spin_unlock(&cr->conn_recovery_cmd_lock);
631 spin_unlock(&sess->cr_a_lock);
636 void iscsit_add_cmd_to_immediate_queue(
637 struct iscsi_cmd *cmd,
638 struct iscsi_conn *conn,
641 struct iscsi_queue_req *qr;
643 qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
645 pr_err("Unable to allocate memory for"
646 " struct iscsi_queue_req\n");
649 INIT_LIST_HEAD(&qr->qr_list);
653 spin_lock_bh(&conn->immed_queue_lock);
654 list_add_tail(&qr->qr_list, &conn->immed_queue_list);
655 atomic_inc(&cmd->immed_queue_count);
656 atomic_set(&conn->check_immediate_queue, 1);
657 spin_unlock_bh(&conn->immed_queue_lock);
659 wake_up_process(conn->thread_set->tx_thread);
662 struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsi_conn *conn)
664 struct iscsi_queue_req *qr;
666 spin_lock_bh(&conn->immed_queue_lock);
667 if (list_empty(&conn->immed_queue_list)) {
668 spin_unlock_bh(&conn->immed_queue_lock);
671 list_for_each_entry(qr, &conn->immed_queue_list, qr_list)
674 list_del(&qr->qr_list);
676 atomic_dec(&qr->cmd->immed_queue_count);
677 spin_unlock_bh(&conn->immed_queue_lock);
682 static void iscsit_remove_cmd_from_immediate_queue(
683 struct iscsi_cmd *cmd,
684 struct iscsi_conn *conn)
686 struct iscsi_queue_req *qr, *qr_tmp;
688 spin_lock_bh(&conn->immed_queue_lock);
689 if (!atomic_read(&cmd->immed_queue_count)) {
690 spin_unlock_bh(&conn->immed_queue_lock);
694 list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
698 atomic_dec(&qr->cmd->immed_queue_count);
699 list_del(&qr->qr_list);
700 kmem_cache_free(lio_qr_cache, qr);
702 spin_unlock_bh(&conn->immed_queue_lock);
704 if (atomic_read(&cmd->immed_queue_count)) {
705 pr_err("ITT: 0x%08x immed_queue_count: %d\n",
707 atomic_read(&cmd->immed_queue_count));
711 void iscsit_add_cmd_to_response_queue(
712 struct iscsi_cmd *cmd,
713 struct iscsi_conn *conn,
716 struct iscsi_queue_req *qr;
718 qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
720 pr_err("Unable to allocate memory for"
721 " struct iscsi_queue_req\n");
724 INIT_LIST_HEAD(&qr->qr_list);
728 spin_lock_bh(&conn->response_queue_lock);
729 list_add_tail(&qr->qr_list, &conn->response_queue_list);
730 atomic_inc(&cmd->response_queue_count);
731 spin_unlock_bh(&conn->response_queue_lock);
733 wake_up_process(conn->thread_set->tx_thread);
736 struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsi_conn *conn)
738 struct iscsi_queue_req *qr;
740 spin_lock_bh(&conn->response_queue_lock);
741 if (list_empty(&conn->response_queue_list)) {
742 spin_unlock_bh(&conn->response_queue_lock);
746 list_for_each_entry(qr, &conn->response_queue_list, qr_list)
749 list_del(&qr->qr_list);
751 atomic_dec(&qr->cmd->response_queue_count);
752 spin_unlock_bh(&conn->response_queue_lock);
757 static void iscsit_remove_cmd_from_response_queue(
758 struct iscsi_cmd *cmd,
759 struct iscsi_conn *conn)
761 struct iscsi_queue_req *qr, *qr_tmp;
763 spin_lock_bh(&conn->response_queue_lock);
764 if (!atomic_read(&cmd->response_queue_count)) {
765 spin_unlock_bh(&conn->response_queue_lock);
769 list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
774 atomic_dec(&qr->cmd->response_queue_count);
775 list_del(&qr->qr_list);
776 kmem_cache_free(lio_qr_cache, qr);
778 spin_unlock_bh(&conn->response_queue_lock);
780 if (atomic_read(&cmd->response_queue_count)) {
781 pr_err("ITT: 0x%08x response_queue_count: %d\n",
783 atomic_read(&cmd->response_queue_count));
787 void iscsit_free_queue_reqs_for_conn(struct iscsi_conn *conn)
789 struct iscsi_queue_req *qr, *qr_tmp;
791 spin_lock_bh(&conn->immed_queue_lock);
792 list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
793 list_del(&qr->qr_list);
795 atomic_dec(&qr->cmd->immed_queue_count);
797 kmem_cache_free(lio_qr_cache, qr);
799 spin_unlock_bh(&conn->immed_queue_lock);
801 spin_lock_bh(&conn->response_queue_lock);
802 list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
804 list_del(&qr->qr_list);
806 atomic_dec(&qr->cmd->response_queue_count);
808 kmem_cache_free(lio_qr_cache, qr);
810 spin_unlock_bh(&conn->response_queue_lock);
813 void iscsit_release_cmd(struct iscsi_cmd *cmd)
815 struct iscsi_conn *conn = cmd->conn;
818 iscsit_free_r2ts_from_list(cmd);
819 iscsit_free_all_datain_reqs(cmd);
822 kfree(cmd->pdu_list);
823 kfree(cmd->seq_list);
825 kfree(cmd->iov_data);
827 for (i = 0; i < cmd->t_mem_sg_nents; i++)
828 __free_page(sg_page(&cmd->t_mem_sg[i]));
830 kfree(cmd->t_mem_sg);
833 iscsit_remove_cmd_from_immediate_queue(cmd, conn);
834 iscsit_remove_cmd_from_response_queue(cmd, conn);
837 kmem_cache_free(lio_cmd_cache, cmd);
840 void iscsit_free_cmd(struct iscsi_cmd *cmd)
843 * Determine if a struct se_cmd is assoicated with
844 * this struct iscsi_cmd.
846 switch (cmd->iscsi_opcode) {
847 case ISCSI_OP_SCSI_CMD:
848 case ISCSI_OP_SCSI_TMFUNC:
849 transport_generic_free_cmd(&cmd->se_cmd, 1);
851 case ISCSI_OP_REJECT:
853 * Handle special case for REJECT when iscsi_add_reject*() has
854 * overwritten the original iscsi_opcode assignment, and the
855 * associated cmd->se_cmd needs to be released.
857 if (cmd->se_cmd.se_tfo != NULL) {
858 transport_generic_free_cmd(&cmd->se_cmd, 1);
863 iscsit_release_cmd(cmd);
868 int iscsit_check_session_usage_count(struct iscsi_session *sess)
870 spin_lock_bh(&sess->session_usage_lock);
871 if (sess->session_usage_count != 0) {
872 sess->session_waiting_on_uc = 1;
873 spin_unlock_bh(&sess->session_usage_lock);
877 wait_for_completion(&sess->session_waiting_on_uc_comp);
880 spin_unlock_bh(&sess->session_usage_lock);
885 void iscsit_dec_session_usage_count(struct iscsi_session *sess)
887 spin_lock_bh(&sess->session_usage_lock);
888 sess->session_usage_count--;
890 if (!sess->session_usage_count && sess->session_waiting_on_uc)
891 complete(&sess->session_waiting_on_uc_comp);
893 spin_unlock_bh(&sess->session_usage_lock);
896 void iscsit_inc_session_usage_count(struct iscsi_session *sess)
898 spin_lock_bh(&sess->session_usage_lock);
899 sess->session_usage_count++;
900 spin_unlock_bh(&sess->session_usage_lock);
904 * Setup conn->if_marker and conn->of_marker values based upon
905 * the initial marker-less interval. (see iSCSI v19 A.2)
907 int iscsit_set_sync_and_steering_values(struct iscsi_conn *conn)
909 int login_ifmarker_count = 0, login_ofmarker_count = 0, next_marker = 0;
911 * IFMarkInt and OFMarkInt are negotiated as 32-bit words.
913 u32 IFMarkInt = (conn->conn_ops->IFMarkInt * 4);
914 u32 OFMarkInt = (conn->conn_ops->OFMarkInt * 4);
916 if (conn->conn_ops->OFMarker) {
918 * Account for the first Login Command received not
919 * via iscsi_recv_msg().
921 conn->of_marker += ISCSI_HDR_LEN;
922 if (conn->of_marker <= OFMarkInt) {
923 conn->of_marker = (OFMarkInt - conn->of_marker);
925 login_ofmarker_count = (conn->of_marker / OFMarkInt);
926 next_marker = (OFMarkInt * (login_ofmarker_count + 1)) +
927 (login_ofmarker_count * MARKER_SIZE);
928 conn->of_marker = (next_marker - conn->of_marker);
930 conn->of_marker_offset = 0;
931 pr_debug("Setting OFMarker value to %u based on Initial"
932 " Markerless Interval.\n", conn->of_marker);
935 if (conn->conn_ops->IFMarker) {
936 if (conn->if_marker <= IFMarkInt) {
937 conn->if_marker = (IFMarkInt - conn->if_marker);
939 login_ifmarker_count = (conn->if_marker / IFMarkInt);
940 next_marker = (IFMarkInt * (login_ifmarker_count + 1)) +
941 (login_ifmarker_count * MARKER_SIZE);
942 conn->if_marker = (next_marker - conn->if_marker);
944 pr_debug("Setting IFMarker value to %u based on Initial"
945 " Markerless Interval.\n", conn->if_marker);
951 struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
953 struct iscsi_conn *conn;
955 spin_lock_bh(&sess->conn_lock);
956 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
957 if ((conn->cid == cid) &&
958 (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
959 iscsit_inc_conn_usage_count(conn);
960 spin_unlock_bh(&sess->conn_lock);
964 spin_unlock_bh(&sess->conn_lock);
969 struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
971 struct iscsi_conn *conn;
973 spin_lock_bh(&sess->conn_lock);
974 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
975 if (conn->cid == cid) {
976 iscsit_inc_conn_usage_count(conn);
977 spin_lock(&conn->state_lock);
978 atomic_set(&conn->connection_wait_rcfr, 1);
979 spin_unlock(&conn->state_lock);
980 spin_unlock_bh(&sess->conn_lock);
984 spin_unlock_bh(&sess->conn_lock);
989 void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
991 spin_lock_bh(&conn->conn_usage_lock);
992 if (conn->conn_usage_count != 0) {
993 conn->conn_waiting_on_uc = 1;
994 spin_unlock_bh(&conn->conn_usage_lock);
996 wait_for_completion(&conn->conn_waiting_on_uc_comp);
999 spin_unlock_bh(&conn->conn_usage_lock);
1002 void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
1004 spin_lock_bh(&conn->conn_usage_lock);
1005 conn->conn_usage_count--;
1007 if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
1008 complete(&conn->conn_waiting_on_uc_comp);
1010 spin_unlock_bh(&conn->conn_usage_lock);
1013 void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
1015 spin_lock_bh(&conn->conn_usage_lock);
1016 conn->conn_usage_count++;
1017 spin_unlock_bh(&conn->conn_usage_lock);
1020 static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
1023 struct iscsi_cmd *cmd;
1025 cmd = iscsit_allocate_cmd(conn, GFP_ATOMIC);
1029 cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
1030 state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
1031 ISTATE_SEND_NOPIN_NO_RESPONSE;
1032 cmd->init_task_tag = 0xFFFFFFFF;
1033 spin_lock_bh(&conn->sess->ttt_lock);
1034 cmd->targ_xfer_tag = (want_response) ? conn->sess->targ_xfer_tag++ :
1036 if (want_response && (cmd->targ_xfer_tag == 0xFFFFFFFF))
1037 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
1038 spin_unlock_bh(&conn->sess->ttt_lock);
1040 spin_lock_bh(&conn->cmd_lock);
1041 list_add_tail(&cmd->i_list, &conn->conn_cmd_list);
1042 spin_unlock_bh(&conn->cmd_lock);
1045 iscsit_start_nopin_response_timer(conn);
1046 iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
1051 static void iscsit_handle_nopin_response_timeout(unsigned long data)
1053 struct iscsi_conn *conn = (struct iscsi_conn *) data;
1055 iscsit_inc_conn_usage_count(conn);
1057 spin_lock_bh(&conn->nopin_timer_lock);
1058 if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
1059 spin_unlock_bh(&conn->nopin_timer_lock);
1060 iscsit_dec_conn_usage_count(conn);
1064 pr_debug("Did not receive response to NOPIN on CID: %hu on"
1065 " SID: %u, failing connection.\n", conn->cid,
1067 conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
1068 spin_unlock_bh(&conn->nopin_timer_lock);
1071 struct iscsi_portal_group *tpg = conn->sess->tpg;
1072 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
1075 spin_lock_bh(&tiqn->sess_err_stats.lock);
1076 strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
1077 conn->sess->sess_ops->InitiatorName);
1078 tiqn->sess_err_stats.last_sess_failure_type =
1079 ISCSI_SESS_ERR_CXN_TIMEOUT;
1080 tiqn->sess_err_stats.cxn_timeout_errors++;
1081 conn->sess->conn_timeout_errors++;
1082 spin_unlock_bh(&tiqn->sess_err_stats.lock);
1086 iscsit_cause_connection_reinstatement(conn, 0);
1087 iscsit_dec_conn_usage_count(conn);
1090 void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
1092 struct iscsi_session *sess = conn->sess;
1093 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1095 spin_lock_bh(&conn->nopin_timer_lock);
1096 if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
1097 spin_unlock_bh(&conn->nopin_timer_lock);
1101 mod_timer(&conn->nopin_response_timer,
1102 (get_jiffies_64() + na->nopin_response_timeout * HZ));
1103 spin_unlock_bh(&conn->nopin_timer_lock);
1107 * Called with conn->nopin_timer_lock held.
1109 void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
1111 struct iscsi_session *sess = conn->sess;
1112 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1114 spin_lock_bh(&conn->nopin_timer_lock);
1115 if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
1116 spin_unlock_bh(&conn->nopin_timer_lock);
1120 init_timer(&conn->nopin_response_timer);
1121 conn->nopin_response_timer.expires =
1122 (get_jiffies_64() + na->nopin_response_timeout * HZ);
1123 conn->nopin_response_timer.data = (unsigned long)conn;
1124 conn->nopin_response_timer.function = iscsit_handle_nopin_response_timeout;
1125 conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
1126 conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
1127 add_timer(&conn->nopin_response_timer);
1129 pr_debug("Started NOPIN Response Timer on CID: %d to %u"
1130 " seconds\n", conn->cid, na->nopin_response_timeout);
1131 spin_unlock_bh(&conn->nopin_timer_lock);
1134 void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
1136 spin_lock_bh(&conn->nopin_timer_lock);
1137 if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
1138 spin_unlock_bh(&conn->nopin_timer_lock);
1141 conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
1142 spin_unlock_bh(&conn->nopin_timer_lock);
1144 del_timer_sync(&conn->nopin_response_timer);
1146 spin_lock_bh(&conn->nopin_timer_lock);
1147 conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
1148 spin_unlock_bh(&conn->nopin_timer_lock);
1151 static void iscsit_handle_nopin_timeout(unsigned long data)
1153 struct iscsi_conn *conn = (struct iscsi_conn *) data;
1155 iscsit_inc_conn_usage_count(conn);
1157 spin_lock_bh(&conn->nopin_timer_lock);
1158 if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
1159 spin_unlock_bh(&conn->nopin_timer_lock);
1160 iscsit_dec_conn_usage_count(conn);
1163 conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1164 spin_unlock_bh(&conn->nopin_timer_lock);
1166 iscsit_add_nopin(conn, 1);
1167 iscsit_dec_conn_usage_count(conn);
1171 * Called with conn->nopin_timer_lock held.
1173 void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
1175 struct iscsi_session *sess = conn->sess;
1176 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1178 * NOPIN timeout is disabled.
1180 if (!na->nopin_timeout)
1183 if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
1186 init_timer(&conn->nopin_timer);
1187 conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
1188 conn->nopin_timer.data = (unsigned long)conn;
1189 conn->nopin_timer.function = iscsit_handle_nopin_timeout;
1190 conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1191 conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
1192 add_timer(&conn->nopin_timer);
1194 pr_debug("Started NOPIN Timer on CID: %d at %u second"
1195 " interval\n", conn->cid, na->nopin_timeout);
1198 void iscsit_start_nopin_timer(struct iscsi_conn *conn)
1200 struct iscsi_session *sess = conn->sess;
1201 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1203 * NOPIN timeout is disabled..
1205 if (!na->nopin_timeout)
1208 spin_lock_bh(&conn->nopin_timer_lock);
1209 if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) {
1210 spin_unlock_bh(&conn->nopin_timer_lock);
1214 init_timer(&conn->nopin_timer);
1215 conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
1216 conn->nopin_timer.data = (unsigned long)conn;
1217 conn->nopin_timer.function = iscsit_handle_nopin_timeout;
1218 conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1219 conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
1220 add_timer(&conn->nopin_timer);
1222 pr_debug("Started NOPIN Timer on CID: %d at %u second"
1223 " interval\n", conn->cid, na->nopin_timeout);
1224 spin_unlock_bh(&conn->nopin_timer_lock);
1227 void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
1229 spin_lock_bh(&conn->nopin_timer_lock);
1230 if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
1231 spin_unlock_bh(&conn->nopin_timer_lock);
1234 conn->nopin_timer_flags |= ISCSI_TF_STOP;
1235 spin_unlock_bh(&conn->nopin_timer_lock);
1237 del_timer_sync(&conn->nopin_timer);
1239 spin_lock_bh(&conn->nopin_timer_lock);
1240 conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1241 spin_unlock_bh(&conn->nopin_timer_lock);
1244 int iscsit_send_tx_data(
1245 struct iscsi_cmd *cmd,
1246 struct iscsi_conn *conn,
1249 int tx_sent, tx_size;
1254 tx_size = cmd->tx_size;
1257 iov = &cmd->iov_data[0];
1258 iov_count = cmd->iov_data_count;
1260 iov = &cmd->iov_misc[0];
1261 iov_count = cmd->iov_misc_count;
1264 tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
1265 if (tx_size != tx_sent) {
1266 if (tx_sent == -EAGAIN) {
1267 pr_err("tx_data() returned -EAGAIN\n");
1277 int iscsit_fe_sendpage_sg(
1278 struct iscsi_cmd *cmd,
1279 struct iscsi_conn *conn)
1281 struct scatterlist *sg = cmd->first_data_sg;
1283 u32 tx_hdr_size, data_len;
1284 u32 offset = cmd->first_data_sg_off;
1285 int tx_sent, iov_off;
1288 tx_hdr_size = ISCSI_HDR_LEN;
1289 if (conn->conn_ops->HeaderDigest)
1290 tx_hdr_size += ISCSI_CRC_LEN;
1292 iov.iov_base = cmd->pdu;
1293 iov.iov_len = tx_hdr_size;
1295 tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
1296 if (tx_hdr_size != tx_sent) {
1297 if (tx_sent == -EAGAIN) {
1298 pr_err("tx_data() returned -EAGAIN\n");
1304 data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
1306 * Set iov_off used by padding and data digest tx_data() calls below
1307 * in order to determine proper offset into cmd->iov_data[]
1309 if (conn->conn_ops->DataDigest) {
1310 data_len -= ISCSI_CRC_LEN;
1312 iov_off = (cmd->iov_data_count - 2);
1314 iov_off = (cmd->iov_data_count - 1);
1316 iov_off = (cmd->iov_data_count - 1);
1319 * Perform sendpage() for each page in the scatterlist
1322 u32 space = (sg->length - offset);
1323 u32 sub_len = min_t(u32, data_len, space);
1325 tx_sent = conn->sock->ops->sendpage(conn->sock,
1326 sg_page(sg), sg->offset + offset, sub_len, 0);
1327 if (tx_sent != sub_len) {
1328 if (tx_sent == -EAGAIN) {
1329 pr_err("tcp_sendpage() returned"
1334 pr_err("tcp_sendpage() failure: %d\n",
1339 data_len -= sub_len;
1346 struct kvec *iov_p = &cmd->iov_data[iov_off++];
1348 tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
1349 if (cmd->padding != tx_sent) {
1350 if (tx_sent == -EAGAIN) {
1351 pr_err("tx_data() returned -EAGAIN\n");
1359 if (conn->conn_ops->DataDigest) {
1360 struct kvec *iov_d = &cmd->iov_data[iov_off];
1362 tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
1363 if (ISCSI_CRC_LEN != tx_sent) {
1364 if (tx_sent == -EAGAIN) {
1365 pr_err("tx_data() returned -EAGAIN\n");
1376 * This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
1377 * back to the Initiator when an expection condition occurs with the
1378 * errors set in status_class and status_detail.
1380 * Parameters: iSCSI Connection, Status Class, Status Detail.
1381 * Returns: 0 on success, -1 on error.
1383 int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
1385 u8 iscsi_hdr[ISCSI_HDR_LEN];
1388 struct iscsi_login_rsp *hdr;
1390 iscsit_collect_login_stats(conn, status_class, status_detail);
1392 memset(&iov, 0, sizeof(struct kvec));
1393 memset(&iscsi_hdr, 0x0, ISCSI_HDR_LEN);
1395 hdr = (struct iscsi_login_rsp *)&iscsi_hdr;
1396 hdr->opcode = ISCSI_OP_LOGIN_RSP;
1397 hdr->status_class = status_class;
1398 hdr->status_detail = status_detail;
1399 hdr->itt = cpu_to_be32(conn->login_itt);
1401 iov.iov_base = &iscsi_hdr;
1402 iov.iov_len = ISCSI_HDR_LEN;
1404 PRINT_BUFF(iscsi_hdr, ISCSI_HDR_LEN);
1406 err = tx_data(conn, &iov, 1, ISCSI_HDR_LEN);
1407 if (err != ISCSI_HDR_LEN) {
1408 pr_err("tx_data returned less than expected\n");
1415 void iscsit_print_session_params(struct iscsi_session *sess)
1417 struct iscsi_conn *conn;
1419 pr_debug("-----------------------------[Session Params for"
1420 " SID: %u]-----------------------------\n", sess->sid);
1421 spin_lock_bh(&sess->conn_lock);
1422 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
1423 iscsi_dump_conn_ops(conn->conn_ops);
1424 spin_unlock_bh(&sess->conn_lock);
1426 iscsi_dump_sess_ops(sess->sess_ops);
1429 static int iscsit_do_rx_data(
1430 struct iscsi_conn *conn,
1431 struct iscsi_data_count *count)
1433 int data = count->data_length, rx_loop = 0, total_rx = 0, iov_len;
1437 if (!conn || !conn->sock || !conn->conn_ops)
1440 memset(&msg, 0, sizeof(struct msghdr));
1443 iov_len = count->iov_count;
1445 while (total_rx < data) {
1446 rx_loop = kernel_recvmsg(conn->sock, &msg, iov_p, iov_len,
1447 (data - total_rx), MSG_WAITALL);
1449 pr_debug("rx_loop: %d total_rx: %d\n",
1453 total_rx += rx_loop;
1454 pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
1455 rx_loop, total_rx, data);
1461 static int iscsit_do_tx_data(
1462 struct iscsi_conn *conn,
1463 struct iscsi_data_count *count)
1465 int data = count->data_length, total_tx = 0, tx_loop = 0, iov_len;
1469 if (!conn || !conn->sock || !conn->conn_ops)
1473 pr_err("Data length is: %d\n", data);
1477 memset(&msg, 0, sizeof(struct msghdr));
1480 iov_len = count->iov_count;
1482 while (total_tx < data) {
1483 tx_loop = kernel_sendmsg(conn->sock, &msg, iov_p, iov_len,
1486 pr_debug("tx_loop: %d total_tx %d\n",
1490 total_tx += tx_loop;
1491 pr_debug("tx_loop: %d, total_tx: %d, data: %d\n",
1492 tx_loop, total_tx, data);
1499 struct iscsi_conn *conn,
1504 struct iscsi_data_count c;
1506 if (!conn || !conn->sock || !conn->conn_ops)
1509 memset(&c, 0, sizeof(struct iscsi_data_count));
1511 c.iov_count = iov_count;
1512 c.data_length = data;
1513 c.type = ISCSI_RX_DATA;
1515 return iscsit_do_rx_data(conn, &c);
1519 struct iscsi_conn *conn,
1524 struct iscsi_data_count c;
1526 if (!conn || !conn->sock || !conn->conn_ops)
1529 memset(&c, 0, sizeof(struct iscsi_data_count));
1531 c.iov_count = iov_count;
1532 c.data_length = data;
1533 c.type = ISCSI_TX_DATA;
1535 return iscsit_do_tx_data(conn, &c);
1538 void iscsit_collect_login_stats(
1539 struct iscsi_conn *conn,
1543 struct iscsi_param *intrname = NULL;
1544 struct iscsi_tiqn *tiqn;
1545 struct iscsi_login_stats *ls;
1547 tiqn = iscsit_snmp_get_tiqn(conn);
1551 ls = &tiqn->login_stats;
1553 spin_lock(&ls->lock);
1554 if (!strcmp(conn->login_ip, ls->last_intr_fail_ip_addr) &&
1555 ((get_jiffies_64() - ls->last_fail_time) < 10)) {
1556 /* We already have the failure info for this login */
1557 spin_unlock(&ls->lock);
1561 if (status_class == ISCSI_STATUS_CLS_SUCCESS)
1563 else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
1565 ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
1566 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1567 (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
1568 ls->authenticate_fails++;
1569 ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHENTICATE;
1570 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1571 (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
1572 ls->authorize_fails++;
1573 ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
1574 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1575 (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
1576 ls->negotiate_fails++;
1577 ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
1580 ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
1583 /* Save initiator name, ip address and time, if it is a failed login */
1584 if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
1585 if (conn->param_list)
1586 intrname = iscsi_find_param_from_key(INITIATORNAME,
1588 strcpy(ls->last_intr_fail_name,
1589 (intrname ? intrname->value : "Unknown"));
1591 ls->last_intr_fail_ip_family = conn->sock->sk->sk_family;
1592 snprintf(ls->last_intr_fail_ip_addr, IPV6_ADDRESS_SPACE,
1593 "%s", conn->login_ip);
1594 ls->last_fail_time = get_jiffies_64();
1597 spin_unlock(&ls->lock);
1600 struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn)
1602 struct iscsi_portal_group *tpg;
1604 if (!conn || !conn->sess)
1607 tpg = conn->sess->tpg;
1614 return tpg->tpg_tiqn;