mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target_util.c
blobc5c98559f7f61f825c5767a816a9bf697b041703
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/percpu_ida.h>
21 #include <scsi/scsi_tcq.h>
22 #include <scsi/iscsi_proto.h>
23 #include <target/target_core_base.h>
24 #include <target/target_core_fabric.h>
25 #include <target/target_core_configfs.h>
26 #include <target/iscsi/iscsi_transport.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) \
41 { \
42 int zzz; \
44 pr_debug("%d:\n", __LINE__); \
45 for (zzz = 0; zzz < len; zzz++) { \
46 if (zzz % 16 == 0) { \
47 if (zzz) \
48 pr_debug("\n"); \
49 pr_debug("%4i: ", zzz); \
50 } \
51 pr_debug("%02x ", (unsigned char) (buff)[zzz]); \
52 } \
53 if ((len + 1) % 16) \
54 pr_debug("\n"); \
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,
65 u32 offset,
66 u32 xfer_len,
67 int recovery,
68 u32 r2t_sn)
70 struct iscsi_r2t *r2t;
72 r2t = kmem_cache_zalloc(lio_r2t_cache, GFP_ATOMIC);
73 if (!r2t) {
74 pr_err("Unable to allocate memory for struct iscsi_r2t.\n");
75 return -1;
77 INIT_LIST_HEAD(&r2t->r2t_list);
79 r2t->recovery_r2t = recovery;
80 r2t->r2t_sn = (!r2t_sn) ? cmd->r2t_sn++ : r2t_sn;
81 r2t->offset = offset;
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);
89 return 0;
92 struct iscsi_r2t *iscsit_get_r2t_for_eos(
93 struct iscsi_cmd *cmd,
94 u32 offset,
95 u32 length)
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);
104 return r2t;
107 spin_unlock_bh(&cmd->r2t_lock);
109 pr_err("Unable to locate R2T for Offset: %u, Length:"
110 " %u\n", offset, length);
111 return NULL;
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);
122 return r2t;
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);
129 return NULL;
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
153 * iSCSI NopINs.
155 struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, gfp_t gfp_mask)
157 struct iscsi_cmd *cmd;
158 struct se_session *se_sess = conn->sess->se_sess;
159 int size, tag, state = (gfp_mask & __GFP_WAIT) ? TASK_INTERRUPTIBLE :
160 TASK_RUNNING;
162 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, state);
163 if (tag < 0)
164 return NULL;
166 size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
167 cmd = (struct iscsi_cmd *)(se_sess->sess_cmd_map + (tag * size));
168 memset(cmd, 0, size);
170 cmd->se_cmd.map_tag = tag;
171 cmd->conn = conn;
172 INIT_LIST_HEAD(&cmd->i_conn_node);
173 INIT_LIST_HEAD(&cmd->datain_list);
174 INIT_LIST_HEAD(&cmd->cmd_r2t_list);
175 spin_lock_init(&cmd->datain_lock);
176 spin_lock_init(&cmd->dataout_timeout_lock);
177 spin_lock_init(&cmd->istate_lock);
178 spin_lock_init(&cmd->error_lock);
179 spin_lock_init(&cmd->r2t_lock);
181 return cmd;
183 EXPORT_SYMBOL(iscsit_allocate_cmd);
185 struct iscsi_seq *iscsit_get_seq_holder_for_datain(
186 struct iscsi_cmd *cmd,
187 u32 seq_send_order)
189 u32 i;
191 for (i = 0; i < cmd->seq_count; i++)
192 if (cmd->seq_list[i].seq_send_order == seq_send_order)
193 return &cmd->seq_list[i];
195 return NULL;
198 struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsi_cmd *cmd)
200 u32 i;
202 if (!cmd->seq_list) {
203 pr_err("struct iscsi_cmd->seq_list is NULL!\n");
204 return NULL;
207 for (i = 0; i < cmd->seq_count; i++) {
208 if (cmd->seq_list[i].type != SEQTYPE_NORMAL)
209 continue;
210 if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) {
211 cmd->seq_send_order++;
212 return &cmd->seq_list[i];
216 return NULL;
219 struct iscsi_r2t *iscsit_get_holder_for_r2tsn(
220 struct iscsi_cmd *cmd,
221 u32 r2t_sn)
223 struct iscsi_r2t *r2t;
225 spin_lock_bh(&cmd->r2t_lock);
226 list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
227 if (r2t->r2t_sn == r2t_sn) {
228 spin_unlock_bh(&cmd->r2t_lock);
229 return r2t;
232 spin_unlock_bh(&cmd->r2t_lock);
234 return NULL;
237 static inline int iscsit_check_received_cmdsn(struct iscsi_session *sess, u32 cmdsn)
239 int ret;
242 * This is the proper method of checking received CmdSN against
243 * ExpCmdSN and MaxCmdSN values, as well as accounting for out
244 * or order CmdSNs due to multiple connection sessions and/or
245 * CRC failures.
247 if (iscsi_sna_gt(cmdsn, sess->max_cmd_sn)) {
248 pr_err("Received CmdSN: 0x%08x is greater than"
249 " MaxCmdSN: 0x%08x, protocol error.\n", cmdsn,
250 sess->max_cmd_sn);
251 ret = CMDSN_ERROR_CANNOT_RECOVER;
253 } else if (cmdsn == sess->exp_cmd_sn) {
254 sess->exp_cmd_sn++;
255 pr_debug("Received CmdSN matches ExpCmdSN,"
256 " incremented ExpCmdSN to: 0x%08x\n",
257 sess->exp_cmd_sn);
258 ret = CMDSN_NORMAL_OPERATION;
260 } else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) {
261 pr_debug("Received CmdSN: 0x%08x is greater"
262 " than ExpCmdSN: 0x%08x, not acknowledging.\n",
263 cmdsn, sess->exp_cmd_sn);
264 ret = CMDSN_HIGHER_THAN_EXP;
266 } else {
267 pr_err("Received CmdSN: 0x%08x is less than"
268 " ExpCmdSN: 0x%08x, ignoring.\n", cmdsn,
269 sess->exp_cmd_sn);
270 ret = CMDSN_LOWER_THAN_EXP;
273 return ret;
277 * Commands may be received out of order if MC/S is in use.
278 * Ensure they are executed in CmdSN order.
280 int iscsit_sequence_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
281 unsigned char *buf, __be32 cmdsn)
283 int ret, cmdsn_ret;
284 bool reject = false;
285 u8 reason = ISCSI_REASON_BOOKMARK_NO_RESOURCES;
287 mutex_lock(&conn->sess->cmdsn_mutex);
289 cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, be32_to_cpu(cmdsn));
290 switch (cmdsn_ret) {
291 case CMDSN_NORMAL_OPERATION:
292 ret = iscsit_execute_cmd(cmd, 0);
293 if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list))
294 iscsit_execute_ooo_cmdsns(conn->sess);
295 else if (ret < 0) {
296 reject = true;
297 ret = CMDSN_ERROR_CANNOT_RECOVER;
299 break;
300 case CMDSN_HIGHER_THAN_EXP:
301 ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, be32_to_cpu(cmdsn));
302 if (ret < 0) {
303 reject = true;
304 ret = CMDSN_ERROR_CANNOT_RECOVER;
305 break;
307 ret = CMDSN_HIGHER_THAN_EXP;
308 break;
309 case CMDSN_LOWER_THAN_EXP:
310 cmd->i_state = ISTATE_REMOVE;
311 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
312 ret = cmdsn_ret;
313 break;
314 default:
315 reason = ISCSI_REASON_PROTOCOL_ERROR;
316 reject = true;
317 ret = cmdsn_ret;
318 break;
320 mutex_unlock(&conn->sess->cmdsn_mutex);
322 if (reject)
323 iscsit_reject_cmd(cmd, reason, buf);
325 return ret;
327 EXPORT_SYMBOL(iscsit_sequence_cmd);
329 int iscsit_check_unsolicited_dataout(struct iscsi_cmd *cmd, unsigned char *buf)
331 struct iscsi_conn *conn = cmd->conn;
332 struct se_cmd *se_cmd = &cmd->se_cmd;
333 struct iscsi_data *hdr = (struct iscsi_data *) buf;
334 u32 payload_length = ntoh24(hdr->dlength);
336 if (conn->sess->sess_ops->InitialR2T) {
337 pr_err("Received unexpected unsolicited data"
338 " while InitialR2T=Yes, protocol error.\n");
339 transport_send_check_condition_and_sense(se_cmd,
340 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
341 return -1;
344 if ((cmd->first_burst_len + payload_length) >
345 conn->sess->sess_ops->FirstBurstLength) {
346 pr_err("Total %u bytes exceeds FirstBurstLength: %u"
347 " for this Unsolicited DataOut Burst.\n",
348 (cmd->first_burst_len + payload_length),
349 conn->sess->sess_ops->FirstBurstLength);
350 transport_send_check_condition_and_sense(se_cmd,
351 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
352 return -1;
355 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))
356 return 0;
358 if (((cmd->first_burst_len + payload_length) != cmd->se_cmd.data_length) &&
359 ((cmd->first_burst_len + payload_length) !=
360 conn->sess->sess_ops->FirstBurstLength)) {
361 pr_err("Unsolicited non-immediate data received %u"
362 " does not equal FirstBurstLength: %u, and does"
363 " not equal ExpXferLen %u.\n",
364 (cmd->first_burst_len + payload_length),
365 conn->sess->sess_ops->FirstBurstLength, cmd->se_cmd.data_length);
366 transport_send_check_condition_and_sense(se_cmd,
367 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
368 return -1;
370 return 0;
373 struct iscsi_cmd *iscsit_find_cmd_from_itt(
374 struct iscsi_conn *conn,
375 itt_t init_task_tag)
377 struct iscsi_cmd *cmd;
379 spin_lock_bh(&conn->cmd_lock);
380 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
381 if (cmd->init_task_tag == init_task_tag) {
382 spin_unlock_bh(&conn->cmd_lock);
383 return cmd;
386 spin_unlock_bh(&conn->cmd_lock);
388 pr_err("Unable to locate ITT: 0x%08x on CID: %hu",
389 init_task_tag, conn->cid);
390 return NULL;
393 struct iscsi_cmd *iscsit_find_cmd_from_itt_or_dump(
394 struct iscsi_conn *conn,
395 itt_t init_task_tag,
396 u32 length)
398 struct iscsi_cmd *cmd;
400 spin_lock_bh(&conn->cmd_lock);
401 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
402 if (cmd->init_task_tag == init_task_tag) {
403 spin_unlock_bh(&conn->cmd_lock);
404 return cmd;
407 spin_unlock_bh(&conn->cmd_lock);
409 pr_err("Unable to locate ITT: 0x%08x on CID: %hu,"
410 " dumping payload\n", init_task_tag, conn->cid);
411 if (length)
412 iscsit_dump_data_payload(conn, length, 1);
414 return NULL;
417 struct iscsi_cmd *iscsit_find_cmd_from_ttt(
418 struct iscsi_conn *conn,
419 u32 targ_xfer_tag)
421 struct iscsi_cmd *cmd = NULL;
423 spin_lock_bh(&conn->cmd_lock);
424 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
425 if (cmd->targ_xfer_tag == targ_xfer_tag) {
426 spin_unlock_bh(&conn->cmd_lock);
427 return cmd;
430 spin_unlock_bh(&conn->cmd_lock);
432 pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n",
433 targ_xfer_tag, conn->cid);
434 return NULL;
437 int iscsit_find_cmd_for_recovery(
438 struct iscsi_session *sess,
439 struct iscsi_cmd **cmd_ptr,
440 struct iscsi_conn_recovery **cr_ptr,
441 itt_t init_task_tag)
443 struct iscsi_cmd *cmd = NULL;
444 struct iscsi_conn_recovery *cr;
446 * Scan through the inactive connection recovery list's command list.
447 * If init_task_tag matches the command is still alligent.
449 spin_lock(&sess->cr_i_lock);
450 list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
451 spin_lock(&cr->conn_recovery_cmd_lock);
452 list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
453 if (cmd->init_task_tag == init_task_tag) {
454 spin_unlock(&cr->conn_recovery_cmd_lock);
455 spin_unlock(&sess->cr_i_lock);
457 *cr_ptr = cr;
458 *cmd_ptr = cmd;
459 return -2;
462 spin_unlock(&cr->conn_recovery_cmd_lock);
464 spin_unlock(&sess->cr_i_lock);
466 * Scan through the active connection recovery list's command list.
467 * If init_task_tag matches the command is ready to be reassigned.
469 spin_lock(&sess->cr_a_lock);
470 list_for_each_entry(cr, &sess->cr_active_list, cr_list) {
471 spin_lock(&cr->conn_recovery_cmd_lock);
472 list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
473 if (cmd->init_task_tag == init_task_tag) {
474 spin_unlock(&cr->conn_recovery_cmd_lock);
475 spin_unlock(&sess->cr_a_lock);
477 *cr_ptr = cr;
478 *cmd_ptr = cmd;
479 return 0;
482 spin_unlock(&cr->conn_recovery_cmd_lock);
484 spin_unlock(&sess->cr_a_lock);
486 return -1;
489 void iscsit_add_cmd_to_immediate_queue(
490 struct iscsi_cmd *cmd,
491 struct iscsi_conn *conn,
492 u8 state)
494 struct iscsi_queue_req *qr;
496 qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
497 if (!qr) {
498 pr_err("Unable to allocate memory for"
499 " struct iscsi_queue_req\n");
500 return;
502 INIT_LIST_HEAD(&qr->qr_list);
503 qr->cmd = cmd;
504 qr->state = state;
506 spin_lock_bh(&conn->immed_queue_lock);
507 list_add_tail(&qr->qr_list, &conn->immed_queue_list);
508 atomic_inc(&cmd->immed_queue_count);
509 atomic_set(&conn->check_immediate_queue, 1);
510 spin_unlock_bh(&conn->immed_queue_lock);
512 wake_up(&conn->queues_wq);
515 struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsi_conn *conn)
517 struct iscsi_queue_req *qr;
519 spin_lock_bh(&conn->immed_queue_lock);
520 if (list_empty(&conn->immed_queue_list)) {
521 spin_unlock_bh(&conn->immed_queue_lock);
522 return NULL;
524 qr = list_first_entry(&conn->immed_queue_list,
525 struct iscsi_queue_req, qr_list);
527 list_del(&qr->qr_list);
528 if (qr->cmd)
529 atomic_dec(&qr->cmd->immed_queue_count);
530 spin_unlock_bh(&conn->immed_queue_lock);
532 return qr;
535 static void iscsit_remove_cmd_from_immediate_queue(
536 struct iscsi_cmd *cmd,
537 struct iscsi_conn *conn)
539 struct iscsi_queue_req *qr, *qr_tmp;
541 spin_lock_bh(&conn->immed_queue_lock);
542 if (!atomic_read(&cmd->immed_queue_count)) {
543 spin_unlock_bh(&conn->immed_queue_lock);
544 return;
547 list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
548 if (qr->cmd != cmd)
549 continue;
551 atomic_dec(&qr->cmd->immed_queue_count);
552 list_del(&qr->qr_list);
553 kmem_cache_free(lio_qr_cache, qr);
555 spin_unlock_bh(&conn->immed_queue_lock);
557 if (atomic_read(&cmd->immed_queue_count)) {
558 pr_err("ITT: 0x%08x immed_queue_count: %d\n",
559 cmd->init_task_tag,
560 atomic_read(&cmd->immed_queue_count));
564 void iscsit_add_cmd_to_response_queue(
565 struct iscsi_cmd *cmd,
566 struct iscsi_conn *conn,
567 u8 state)
569 struct iscsi_queue_req *qr;
571 qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
572 if (!qr) {
573 pr_err("Unable to allocate memory for"
574 " struct iscsi_queue_req\n");
575 return;
577 INIT_LIST_HEAD(&qr->qr_list);
578 qr->cmd = cmd;
579 qr->state = state;
581 spin_lock_bh(&conn->response_queue_lock);
582 list_add_tail(&qr->qr_list, &conn->response_queue_list);
583 atomic_inc(&cmd->response_queue_count);
584 spin_unlock_bh(&conn->response_queue_lock);
586 wake_up(&conn->queues_wq);
589 struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsi_conn *conn)
591 struct iscsi_queue_req *qr;
593 spin_lock_bh(&conn->response_queue_lock);
594 if (list_empty(&conn->response_queue_list)) {
595 spin_unlock_bh(&conn->response_queue_lock);
596 return NULL;
599 qr = list_first_entry(&conn->response_queue_list,
600 struct iscsi_queue_req, qr_list);
602 list_del(&qr->qr_list);
603 if (qr->cmd)
604 atomic_dec(&qr->cmd->response_queue_count);
605 spin_unlock_bh(&conn->response_queue_lock);
607 return qr;
610 static void iscsit_remove_cmd_from_response_queue(
611 struct iscsi_cmd *cmd,
612 struct iscsi_conn *conn)
614 struct iscsi_queue_req *qr, *qr_tmp;
616 spin_lock_bh(&conn->response_queue_lock);
617 if (!atomic_read(&cmd->response_queue_count)) {
618 spin_unlock_bh(&conn->response_queue_lock);
619 return;
622 list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
623 qr_list) {
624 if (qr->cmd != cmd)
625 continue;
627 atomic_dec(&qr->cmd->response_queue_count);
628 list_del(&qr->qr_list);
629 kmem_cache_free(lio_qr_cache, qr);
631 spin_unlock_bh(&conn->response_queue_lock);
633 if (atomic_read(&cmd->response_queue_count)) {
634 pr_err("ITT: 0x%08x response_queue_count: %d\n",
635 cmd->init_task_tag,
636 atomic_read(&cmd->response_queue_count));
640 bool iscsit_conn_all_queues_empty(struct iscsi_conn *conn)
642 bool empty;
644 spin_lock_bh(&conn->immed_queue_lock);
645 empty = list_empty(&conn->immed_queue_list);
646 spin_unlock_bh(&conn->immed_queue_lock);
648 if (!empty)
649 return empty;
651 spin_lock_bh(&conn->response_queue_lock);
652 empty = list_empty(&conn->response_queue_list);
653 spin_unlock_bh(&conn->response_queue_lock);
655 return empty;
658 void iscsit_free_queue_reqs_for_conn(struct iscsi_conn *conn)
660 struct iscsi_queue_req *qr, *qr_tmp;
662 spin_lock_bh(&conn->immed_queue_lock);
663 list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
664 list_del(&qr->qr_list);
665 if (qr->cmd)
666 atomic_dec(&qr->cmd->immed_queue_count);
668 kmem_cache_free(lio_qr_cache, qr);
670 spin_unlock_bh(&conn->immed_queue_lock);
672 spin_lock_bh(&conn->response_queue_lock);
673 list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
674 qr_list) {
675 list_del(&qr->qr_list);
676 if (qr->cmd)
677 atomic_dec(&qr->cmd->response_queue_count);
679 kmem_cache_free(lio_qr_cache, qr);
681 spin_unlock_bh(&conn->response_queue_lock);
684 void iscsit_release_cmd(struct iscsi_cmd *cmd)
686 struct iscsi_session *sess;
687 struct se_cmd *se_cmd = &cmd->se_cmd;
689 if (cmd->conn)
690 sess = cmd->conn->sess;
691 else
692 sess = cmd->sess;
694 BUG_ON(!sess || !sess->se_sess);
696 kfree(cmd->buf_ptr);
697 kfree(cmd->pdu_list);
698 kfree(cmd->seq_list);
699 kfree(cmd->tmr_req);
700 kfree(cmd->iov_data);
701 kfree(cmd->text_in_ptr);
703 percpu_ida_free(&sess->se_sess->sess_tag_pool, se_cmd->map_tag);
705 EXPORT_SYMBOL(iscsit_release_cmd);
707 static void __iscsit_free_cmd(struct iscsi_cmd *cmd, bool scsi_cmd,
708 bool check_queues)
710 struct iscsi_conn *conn = cmd->conn;
712 if (scsi_cmd) {
713 if (cmd->data_direction == DMA_TO_DEVICE) {
714 iscsit_stop_dataout_timer(cmd);
715 iscsit_free_r2ts_from_list(cmd);
717 if (cmd->data_direction == DMA_FROM_DEVICE)
718 iscsit_free_all_datain_reqs(cmd);
721 if (conn && check_queues) {
722 iscsit_remove_cmd_from_immediate_queue(cmd, conn);
723 iscsit_remove_cmd_from_response_queue(cmd, conn);
727 void iscsit_free_cmd(struct iscsi_cmd *cmd, bool shutdown)
729 struct se_cmd *se_cmd = NULL;
730 int rc;
732 * Determine if a struct se_cmd is associated with
733 * this struct iscsi_cmd.
735 switch (cmd->iscsi_opcode) {
736 case ISCSI_OP_SCSI_CMD:
737 se_cmd = &cmd->se_cmd;
738 __iscsit_free_cmd(cmd, true, shutdown);
740 * Fallthrough
742 case ISCSI_OP_SCSI_TMFUNC:
743 rc = transport_generic_free_cmd(&cmd->se_cmd, shutdown);
744 if (!rc && shutdown && se_cmd && se_cmd->se_sess) {
745 __iscsit_free_cmd(cmd, true, shutdown);
746 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
748 break;
749 case ISCSI_OP_REJECT:
751 * Handle special case for REJECT when iscsi_add_reject*() has
752 * overwritten the original iscsi_opcode assignment, and the
753 * associated cmd->se_cmd needs to be released.
755 if (cmd->se_cmd.se_tfo != NULL) {
756 se_cmd = &cmd->se_cmd;
757 __iscsit_free_cmd(cmd, true, shutdown);
759 rc = transport_generic_free_cmd(&cmd->se_cmd, shutdown);
760 if (!rc && shutdown && se_cmd->se_sess) {
761 __iscsit_free_cmd(cmd, true, shutdown);
762 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
764 break;
766 /* Fall-through */
767 default:
768 __iscsit_free_cmd(cmd, false, shutdown);
769 iscsit_release_cmd(cmd);
770 break;
774 int iscsit_check_session_usage_count(struct iscsi_session *sess)
776 spin_lock_bh(&sess->session_usage_lock);
777 if (sess->session_usage_count != 0) {
778 sess->session_waiting_on_uc = 1;
779 spin_unlock_bh(&sess->session_usage_lock);
780 if (in_interrupt())
781 return 2;
783 wait_for_completion(&sess->session_waiting_on_uc_comp);
784 return 1;
786 spin_unlock_bh(&sess->session_usage_lock);
788 return 0;
791 void iscsit_dec_session_usage_count(struct iscsi_session *sess)
793 spin_lock_bh(&sess->session_usage_lock);
794 sess->session_usage_count--;
796 if (!sess->session_usage_count && sess->session_waiting_on_uc)
797 complete(&sess->session_waiting_on_uc_comp);
799 spin_unlock_bh(&sess->session_usage_lock);
802 void iscsit_inc_session_usage_count(struct iscsi_session *sess)
804 spin_lock_bh(&sess->session_usage_lock);
805 sess->session_usage_count++;
806 spin_unlock_bh(&sess->session_usage_lock);
810 * Setup conn->if_marker and conn->of_marker values based upon
811 * the initial marker-less interval. (see iSCSI v19 A.2)
813 int iscsit_set_sync_and_steering_values(struct iscsi_conn *conn)
815 int login_ifmarker_count = 0, login_ofmarker_count = 0, next_marker = 0;
817 * IFMarkInt and OFMarkInt are negotiated as 32-bit words.
819 u32 IFMarkInt = (conn->conn_ops->IFMarkInt * 4);
820 u32 OFMarkInt = (conn->conn_ops->OFMarkInt * 4);
822 if (conn->conn_ops->OFMarker) {
824 * Account for the first Login Command received not
825 * via iscsi_recv_msg().
827 conn->of_marker += ISCSI_HDR_LEN;
828 if (conn->of_marker <= OFMarkInt) {
829 conn->of_marker = (OFMarkInt - conn->of_marker);
830 } else {
831 login_ofmarker_count = (conn->of_marker / OFMarkInt);
832 next_marker = (OFMarkInt * (login_ofmarker_count + 1)) +
833 (login_ofmarker_count * MARKER_SIZE);
834 conn->of_marker = (next_marker - conn->of_marker);
836 conn->of_marker_offset = 0;
837 pr_debug("Setting OFMarker value to %u based on Initial"
838 " Markerless Interval.\n", conn->of_marker);
841 if (conn->conn_ops->IFMarker) {
842 if (conn->if_marker <= IFMarkInt) {
843 conn->if_marker = (IFMarkInt - conn->if_marker);
844 } else {
845 login_ifmarker_count = (conn->if_marker / IFMarkInt);
846 next_marker = (IFMarkInt * (login_ifmarker_count + 1)) +
847 (login_ifmarker_count * MARKER_SIZE);
848 conn->if_marker = (next_marker - conn->if_marker);
850 pr_debug("Setting IFMarker value to %u based on Initial"
851 " Markerless Interval.\n", conn->if_marker);
854 return 0;
857 struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
859 struct iscsi_conn *conn;
861 spin_lock_bh(&sess->conn_lock);
862 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
863 if ((conn->cid == cid) &&
864 (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
865 iscsit_inc_conn_usage_count(conn);
866 spin_unlock_bh(&sess->conn_lock);
867 return conn;
870 spin_unlock_bh(&sess->conn_lock);
872 return NULL;
875 struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
877 struct iscsi_conn *conn;
879 spin_lock_bh(&sess->conn_lock);
880 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
881 if (conn->cid == cid) {
882 iscsit_inc_conn_usage_count(conn);
883 spin_lock(&conn->state_lock);
884 atomic_set(&conn->connection_wait_rcfr, 1);
885 spin_unlock(&conn->state_lock);
886 spin_unlock_bh(&sess->conn_lock);
887 return conn;
890 spin_unlock_bh(&sess->conn_lock);
892 return NULL;
895 void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
897 spin_lock_bh(&conn->conn_usage_lock);
898 if (conn->conn_usage_count != 0) {
899 conn->conn_waiting_on_uc = 1;
900 spin_unlock_bh(&conn->conn_usage_lock);
902 wait_for_completion(&conn->conn_waiting_on_uc_comp);
903 return;
905 spin_unlock_bh(&conn->conn_usage_lock);
908 void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
910 spin_lock_bh(&conn->conn_usage_lock);
911 conn->conn_usage_count--;
913 if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
914 complete(&conn->conn_waiting_on_uc_comp);
916 spin_unlock_bh(&conn->conn_usage_lock);
919 void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
921 spin_lock_bh(&conn->conn_usage_lock);
922 conn->conn_usage_count++;
923 spin_unlock_bh(&conn->conn_usage_lock);
926 static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
928 u8 state;
929 struct iscsi_cmd *cmd;
931 cmd = iscsit_allocate_cmd(conn, GFP_ATOMIC);
932 if (!cmd)
933 return -1;
935 cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
936 state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
937 ISTATE_SEND_NOPIN_NO_RESPONSE;
938 cmd->init_task_tag = RESERVED_ITT;
939 spin_lock_bh(&conn->sess->ttt_lock);
940 cmd->targ_xfer_tag = (want_response) ? conn->sess->targ_xfer_tag++ :
941 0xFFFFFFFF;
942 if (want_response && (cmd->targ_xfer_tag == 0xFFFFFFFF))
943 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
944 spin_unlock_bh(&conn->sess->ttt_lock);
946 spin_lock_bh(&conn->cmd_lock);
947 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
948 spin_unlock_bh(&conn->cmd_lock);
950 if (want_response)
951 iscsit_start_nopin_response_timer(conn);
952 iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
954 return 0;
957 static void iscsit_handle_nopin_response_timeout(unsigned long data)
959 struct iscsi_conn *conn = (struct iscsi_conn *) data;
961 iscsit_inc_conn_usage_count(conn);
963 spin_lock_bh(&conn->nopin_timer_lock);
964 if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
965 spin_unlock_bh(&conn->nopin_timer_lock);
966 iscsit_dec_conn_usage_count(conn);
967 return;
970 pr_debug("Did not receive response to NOPIN on CID: %hu on"
971 " SID: %u, failing connection.\n", conn->cid,
972 conn->sess->sid);
973 conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
974 spin_unlock_bh(&conn->nopin_timer_lock);
977 struct iscsi_portal_group *tpg = conn->sess->tpg;
978 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
980 if (tiqn) {
981 spin_lock_bh(&tiqn->sess_err_stats.lock);
982 strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
983 conn->sess->sess_ops->InitiatorName);
984 tiqn->sess_err_stats.last_sess_failure_type =
985 ISCSI_SESS_ERR_CXN_TIMEOUT;
986 tiqn->sess_err_stats.cxn_timeout_errors++;
987 conn->sess->conn_timeout_errors++;
988 spin_unlock_bh(&tiqn->sess_err_stats.lock);
992 iscsit_cause_connection_reinstatement(conn, 0);
993 iscsit_dec_conn_usage_count(conn);
996 void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
998 struct iscsi_session *sess = conn->sess;
999 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1001 spin_lock_bh(&conn->nopin_timer_lock);
1002 if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
1003 spin_unlock_bh(&conn->nopin_timer_lock);
1004 return;
1007 mod_timer(&conn->nopin_response_timer,
1008 (get_jiffies_64() + na->nopin_response_timeout * HZ));
1009 spin_unlock_bh(&conn->nopin_timer_lock);
1013 * Called with conn->nopin_timer_lock held.
1015 void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
1017 struct iscsi_session *sess = conn->sess;
1018 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1020 spin_lock_bh(&conn->nopin_timer_lock);
1021 if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
1022 spin_unlock_bh(&conn->nopin_timer_lock);
1023 return;
1026 init_timer(&conn->nopin_response_timer);
1027 conn->nopin_response_timer.expires =
1028 (get_jiffies_64() + na->nopin_response_timeout * HZ);
1029 conn->nopin_response_timer.data = (unsigned long)conn;
1030 conn->nopin_response_timer.function = iscsit_handle_nopin_response_timeout;
1031 conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
1032 conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
1033 add_timer(&conn->nopin_response_timer);
1035 pr_debug("Started NOPIN Response Timer on CID: %d to %u"
1036 " seconds\n", conn->cid, na->nopin_response_timeout);
1037 spin_unlock_bh(&conn->nopin_timer_lock);
1040 void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
1042 spin_lock_bh(&conn->nopin_timer_lock);
1043 if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
1044 spin_unlock_bh(&conn->nopin_timer_lock);
1045 return;
1047 conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
1048 spin_unlock_bh(&conn->nopin_timer_lock);
1050 del_timer_sync(&conn->nopin_response_timer);
1052 spin_lock_bh(&conn->nopin_timer_lock);
1053 conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
1054 spin_unlock_bh(&conn->nopin_timer_lock);
1057 static void iscsit_handle_nopin_timeout(unsigned long data)
1059 struct iscsi_conn *conn = (struct iscsi_conn *) data;
1061 iscsit_inc_conn_usage_count(conn);
1063 spin_lock_bh(&conn->nopin_timer_lock);
1064 if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
1065 spin_unlock_bh(&conn->nopin_timer_lock);
1066 iscsit_dec_conn_usage_count(conn);
1067 return;
1069 conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1070 spin_unlock_bh(&conn->nopin_timer_lock);
1072 iscsit_add_nopin(conn, 1);
1073 iscsit_dec_conn_usage_count(conn);
1077 * Called with conn->nopin_timer_lock held.
1079 void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
1081 struct iscsi_session *sess = conn->sess;
1082 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1084 * NOPIN timeout is disabled.
1086 if (!na->nopin_timeout)
1087 return;
1089 if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
1090 return;
1092 init_timer(&conn->nopin_timer);
1093 conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
1094 conn->nopin_timer.data = (unsigned long)conn;
1095 conn->nopin_timer.function = iscsit_handle_nopin_timeout;
1096 conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1097 conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
1098 add_timer(&conn->nopin_timer);
1100 pr_debug("Started NOPIN Timer on CID: %d at %u second"
1101 " interval\n", conn->cid, na->nopin_timeout);
1104 void iscsit_start_nopin_timer(struct iscsi_conn *conn)
1106 struct iscsi_session *sess = conn->sess;
1107 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1109 * NOPIN timeout is disabled..
1111 if (!na->nopin_timeout)
1112 return;
1114 spin_lock_bh(&conn->nopin_timer_lock);
1115 if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) {
1116 spin_unlock_bh(&conn->nopin_timer_lock);
1117 return;
1120 init_timer(&conn->nopin_timer);
1121 conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
1122 conn->nopin_timer.data = (unsigned long)conn;
1123 conn->nopin_timer.function = iscsit_handle_nopin_timeout;
1124 conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1125 conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
1126 add_timer(&conn->nopin_timer);
1128 pr_debug("Started NOPIN Timer on CID: %d at %u second"
1129 " interval\n", conn->cid, na->nopin_timeout);
1130 spin_unlock_bh(&conn->nopin_timer_lock);
1133 void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
1135 spin_lock_bh(&conn->nopin_timer_lock);
1136 if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
1137 spin_unlock_bh(&conn->nopin_timer_lock);
1138 return;
1140 conn->nopin_timer_flags |= ISCSI_TF_STOP;
1141 spin_unlock_bh(&conn->nopin_timer_lock);
1143 del_timer_sync(&conn->nopin_timer);
1145 spin_lock_bh(&conn->nopin_timer_lock);
1146 conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1147 spin_unlock_bh(&conn->nopin_timer_lock);
1150 int iscsit_send_tx_data(
1151 struct iscsi_cmd *cmd,
1152 struct iscsi_conn *conn,
1153 int use_misc)
1155 int tx_sent, tx_size;
1156 u32 iov_count;
1157 struct kvec *iov;
1159 send_data:
1160 tx_size = cmd->tx_size;
1162 if (!use_misc) {
1163 iov = &cmd->iov_data[0];
1164 iov_count = cmd->iov_data_count;
1165 } else {
1166 iov = &cmd->iov_misc[0];
1167 iov_count = cmd->iov_misc_count;
1170 tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
1171 if (tx_size != tx_sent) {
1172 if (tx_sent == -EAGAIN) {
1173 pr_err("tx_data() returned -EAGAIN\n");
1174 goto send_data;
1175 } else
1176 return -1;
1178 cmd->tx_size = 0;
1180 return 0;
1183 int iscsit_fe_sendpage_sg(
1184 struct iscsi_cmd *cmd,
1185 struct iscsi_conn *conn)
1187 struct scatterlist *sg = cmd->first_data_sg;
1188 struct kvec iov;
1189 u32 tx_hdr_size, data_len;
1190 u32 offset = cmd->first_data_sg_off;
1191 int tx_sent, iov_off;
1193 send_hdr:
1194 tx_hdr_size = ISCSI_HDR_LEN;
1195 if (conn->conn_ops->HeaderDigest)
1196 tx_hdr_size += ISCSI_CRC_LEN;
1198 iov.iov_base = cmd->pdu;
1199 iov.iov_len = tx_hdr_size;
1201 tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
1202 if (tx_hdr_size != tx_sent) {
1203 if (tx_sent == -EAGAIN) {
1204 pr_err("tx_data() returned -EAGAIN\n");
1205 goto send_hdr;
1207 return -1;
1210 data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
1212 * Set iov_off used by padding and data digest tx_data() calls below
1213 * in order to determine proper offset into cmd->iov_data[]
1215 if (conn->conn_ops->DataDigest) {
1216 data_len -= ISCSI_CRC_LEN;
1217 if (cmd->padding)
1218 iov_off = (cmd->iov_data_count - 2);
1219 else
1220 iov_off = (cmd->iov_data_count - 1);
1221 } else {
1222 iov_off = (cmd->iov_data_count - 1);
1225 * Perform sendpage() for each page in the scatterlist
1227 while (data_len) {
1228 u32 space = (sg->length - offset);
1229 u32 sub_len = min_t(u32, data_len, space);
1230 send_pg:
1231 tx_sent = conn->sock->ops->sendpage(conn->sock,
1232 sg_page(sg), sg->offset + offset, sub_len, 0);
1233 if (tx_sent != sub_len) {
1234 if (tx_sent == -EAGAIN) {
1235 pr_err("tcp_sendpage() returned"
1236 " -EAGAIN\n");
1237 goto send_pg;
1240 pr_err("tcp_sendpage() failure: %d\n",
1241 tx_sent);
1242 return -1;
1245 data_len -= sub_len;
1246 offset = 0;
1247 sg = sg_next(sg);
1250 send_padding:
1251 if (cmd->padding) {
1252 struct kvec *iov_p = &cmd->iov_data[iov_off++];
1254 tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
1255 if (cmd->padding != tx_sent) {
1256 if (tx_sent == -EAGAIN) {
1257 pr_err("tx_data() returned -EAGAIN\n");
1258 goto send_padding;
1260 return -1;
1264 send_datacrc:
1265 if (conn->conn_ops->DataDigest) {
1266 struct kvec *iov_d = &cmd->iov_data[iov_off];
1268 tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
1269 if (ISCSI_CRC_LEN != tx_sent) {
1270 if (tx_sent == -EAGAIN) {
1271 pr_err("tx_data() returned -EAGAIN\n");
1272 goto send_datacrc;
1274 return -1;
1278 return 0;
1282 * This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
1283 * back to the Initiator when an expection condition occurs with the
1284 * errors set in status_class and status_detail.
1286 * Parameters: iSCSI Connection, Status Class, Status Detail.
1287 * Returns: 0 on success, -1 on error.
1289 int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
1291 struct iscsi_login_rsp *hdr;
1292 struct iscsi_login *login = conn->conn_login;
1294 login->login_failed = 1;
1295 iscsit_collect_login_stats(conn, status_class, status_detail);
1297 memset(&login->rsp[0], 0, ISCSI_HDR_LEN);
1299 hdr = (struct iscsi_login_rsp *)&login->rsp[0];
1300 hdr->opcode = ISCSI_OP_LOGIN_RSP;
1301 hdr->status_class = status_class;
1302 hdr->status_detail = status_detail;
1303 hdr->itt = conn->login_itt;
1305 return conn->conn_transport->iscsit_put_login_tx(conn, login, 0);
1308 void iscsit_print_session_params(struct iscsi_session *sess)
1310 struct iscsi_conn *conn;
1312 pr_debug("-----------------------------[Session Params for"
1313 " SID: %u]-----------------------------\n", sess->sid);
1314 spin_lock_bh(&sess->conn_lock);
1315 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
1316 iscsi_dump_conn_ops(conn->conn_ops);
1317 spin_unlock_bh(&sess->conn_lock);
1319 iscsi_dump_sess_ops(sess->sess_ops);
1322 static int iscsit_do_rx_data(
1323 struct iscsi_conn *conn,
1324 struct iscsi_data_count *count)
1326 int data = count->data_length, rx_loop = 0, total_rx = 0, iov_len;
1327 struct kvec *iov_p;
1328 struct msghdr msg;
1330 if (!conn || !conn->sock || !conn->conn_ops)
1331 return -1;
1333 memset(&msg, 0, sizeof(struct msghdr));
1335 iov_p = count->iov;
1336 iov_len = count->iov_count;
1338 while (total_rx < data) {
1339 rx_loop = kernel_recvmsg(conn->sock, &msg, iov_p, iov_len,
1340 (data - total_rx), MSG_WAITALL);
1341 if (rx_loop <= 0) {
1342 pr_debug("rx_loop: %d total_rx: %d\n",
1343 rx_loop, total_rx);
1344 return rx_loop;
1346 total_rx += rx_loop;
1347 pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
1348 rx_loop, total_rx, data);
1351 return total_rx;
1354 static int iscsit_do_tx_data(
1355 struct iscsi_conn *conn,
1356 struct iscsi_data_count *count)
1358 int ret, iov_len;
1359 struct kvec *iov_p;
1360 struct msghdr msg;
1362 if (!conn || !conn->sock || !conn->conn_ops)
1363 return -1;
1365 if (count->data_length <= 0) {
1366 pr_err("Data length is: %d\n", count->data_length);
1367 return -1;
1370 memset(&msg, 0, sizeof(struct msghdr));
1372 iov_p = count->iov;
1373 iov_len = count->iov_count;
1375 ret = kernel_sendmsg(conn->sock, &msg, iov_p, iov_len,
1376 count->data_length);
1377 if (ret != count->data_length) {
1378 pr_err("Unexpected ret: %d send data %d\n",
1379 ret, count->data_length);
1380 return -EPIPE;
1382 pr_debug("ret: %d, sent data: %d\n", ret, count->data_length);
1384 return ret;
1387 int rx_data(
1388 struct iscsi_conn *conn,
1389 struct kvec *iov,
1390 int iov_count,
1391 int data)
1393 struct iscsi_data_count c;
1395 if (!conn || !conn->sock || !conn->conn_ops)
1396 return -1;
1398 memset(&c, 0, sizeof(struct iscsi_data_count));
1399 c.iov = iov;
1400 c.iov_count = iov_count;
1401 c.data_length = data;
1402 c.type = ISCSI_RX_DATA;
1404 return iscsit_do_rx_data(conn, &c);
1407 int tx_data(
1408 struct iscsi_conn *conn,
1409 struct kvec *iov,
1410 int iov_count,
1411 int data)
1413 struct iscsi_data_count c;
1415 if (!conn || !conn->sock || !conn->conn_ops)
1416 return -1;
1418 memset(&c, 0, sizeof(struct iscsi_data_count));
1419 c.iov = iov;
1420 c.iov_count = iov_count;
1421 c.data_length = data;
1422 c.type = ISCSI_TX_DATA;
1424 return iscsit_do_tx_data(conn, &c);
1427 void iscsit_collect_login_stats(
1428 struct iscsi_conn *conn,
1429 u8 status_class,
1430 u8 status_detail)
1432 struct iscsi_param *intrname = NULL;
1433 struct iscsi_tiqn *tiqn;
1434 struct iscsi_login_stats *ls;
1436 tiqn = iscsit_snmp_get_tiqn(conn);
1437 if (!tiqn)
1438 return;
1440 ls = &tiqn->login_stats;
1442 spin_lock(&ls->lock);
1443 if (!strcmp(conn->login_ip, ls->last_intr_fail_ip_addr) &&
1444 ((get_jiffies_64() - ls->last_fail_time) < 10)) {
1445 /* We already have the failure info for this login */
1446 spin_unlock(&ls->lock);
1447 return;
1450 if (status_class == ISCSI_STATUS_CLS_SUCCESS)
1451 ls->accepts++;
1452 else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
1453 ls->redirects++;
1454 ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
1455 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1456 (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
1457 ls->authenticate_fails++;
1458 ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHENTICATE;
1459 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1460 (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
1461 ls->authorize_fails++;
1462 ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
1463 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1464 (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
1465 ls->negotiate_fails++;
1466 ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
1467 } else {
1468 ls->other_fails++;
1469 ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
1472 /* Save initiator name, ip address and time, if it is a failed login */
1473 if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
1474 if (conn->param_list)
1475 intrname = iscsi_find_param_from_key(INITIATORNAME,
1476 conn->param_list);
1477 strcpy(ls->last_intr_fail_name,
1478 (intrname ? intrname->value : "Unknown"));
1480 ls->last_intr_fail_ip_family = conn->login_family;
1482 snprintf(ls->last_intr_fail_ip_addr, IPV6_ADDRESS_SPACE,
1483 "%s", conn->login_ip);
1484 ls->last_fail_time = get_jiffies_64();
1487 spin_unlock(&ls->lock);
1490 struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn)
1492 struct iscsi_portal_group *tpg;
1494 if (!conn || !conn->sess)
1495 return NULL;
1497 tpg = conn->sess->tpg;
1498 if (!tpg)
1499 return NULL;
1501 if (!tpg->tpg_tiqn)
1502 return NULL;
1504 return tpg->tpg_tiqn;