1 /******************************************************************************* 2 * This file contains the iSCSI Target specific utility functions. 3 * 4 * (c) Copyright 2007-2013 Datera, Inc. 5 * 6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org> 7 * 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. 12 * 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 ******************************************************************************/ 18 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> 27 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" 38 39 #define PRINT_BUFF(buff, len) \ 40 { \ 41 int zzz; \ 42 \ 43 pr_debug("%d:\n", __LINE__); \ 44 for (zzz = 0; zzz < len; zzz++) { \ 45 if (zzz % 16 == 0) { \ 46 if (zzz) \ 47 pr_debug("\n"); \ 48 pr_debug("%4i: ", zzz); \ 49 } \ 50 pr_debug("%02x ", (unsigned char) (buff)[zzz]); \ 51 } \ 52 if ((len + 1) % 16) \ 53 pr_debug("\n"); \ 54 } 55 56 extern struct list_head g_tiqn_list; 57 extern spinlock_t tiqn_lock; 58 59 int iscsit_add_r2t_to_list( 60 struct iscsi_cmd *cmd, 61 u32 offset, 62 u32 xfer_len, 63 int recovery, 64 u32 r2t_sn) 65 { 66 struct iscsi_r2t *r2t; 67 68 lockdep_assert_held(&cmd->r2t_lock); 69 70 WARN_ON_ONCE((s32)xfer_len < 0); 71 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; 76 } 77 INIT_LIST_HEAD(&r2t->r2t_list); 78 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); 85 86 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T); 87 88 spin_lock_bh(&cmd->r2t_lock); 89 return 0; 90 } 91 92 struct iscsi_r2t *iscsit_get_r2t_for_eos( 93 struct iscsi_cmd *cmd, 94 u32 offset, 95 u32 length) 96 { 97 struct iscsi_r2t *r2t; 98 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; 105 } 106 } 107 spin_unlock_bh(&cmd->r2t_lock); 108 109 pr_err("Unable to locate R2T for Offset: %u, Length:" 110 " %u\n", offset, length); 111 return NULL; 112 } 113 114 struct iscsi_r2t *iscsit_get_r2t_from_list(struct iscsi_cmd *cmd) 115 { 116 struct iscsi_r2t *r2t; 117 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; 123 } 124 } 125 spin_unlock_bh(&cmd->r2t_lock); 126 127 pr_err("Unable to locate next R2T to send for ITT:" 128 " 0x%08x.\n", cmd->init_task_tag); 129 return NULL; 130 } 131 132 void iscsit_free_r2t(struct iscsi_r2t *r2t, struct iscsi_cmd *cmd) 133 { 134 lockdep_assert_held(&cmd->r2t_lock); 135 136 list_del(&r2t->r2t_list); 137 kmem_cache_free(lio_r2t_cache, r2t); 138 } 139 140 void iscsit_free_r2ts_from_list(struct iscsi_cmd *cmd) 141 { 142 struct iscsi_r2t *r2t, *r2t_tmp; 143 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); 148 } 149 150 static int iscsit_wait_for_tag(struct se_session *se_sess, int state, int *cpup) 151 { 152 int tag = -1; 153 DEFINE_SBQ_WAIT(wait); 154 struct sbq_wait_state *ws; 155 struct sbitmap_queue *sbq; 156 157 if (state == TASK_RUNNING) 158 return tag; 159 160 sbq = &se_sess->sess_tag_pool; 161 ws = &sbq->ws[0]; 162 for (;;) { 163 sbitmap_prepare_to_wait(sbq, ws, &wait, state); 164 if (signal_pending_state(state, current)) 165 break; 166 tag = sbitmap_queue_get(sbq, cpup); 167 if (tag >= 0) 168 break; 169 schedule(); 170 } 171 172 sbitmap_finish_wait(sbq, ws, &wait); 173 return tag; 174 } 175 176 /* 177 * May be called from software interrupt (timer) context for allocating 178 * iSCSI NopINs. 179 */ 180 struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, int state) 181 { 182 struct iscsi_cmd *cmd; 183 struct se_session *se_sess = conn->sess->se_sess; 184 int size, tag, cpu; 185 186 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu); 187 if (tag < 0) 188 tag = iscsit_wait_for_tag(se_sess, state, &cpu); 189 if (tag < 0) 190 return NULL; 191 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); 195 196 cmd->se_cmd.map_tag = tag; 197 cmd->se_cmd.map_cpu = cpu; 198 cmd->conn = conn; 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); 209 210 return cmd; 211 } 212 EXPORT_SYMBOL(iscsit_allocate_cmd); 213 214 struct iscsi_seq *iscsit_get_seq_holder_for_datain( 215 struct iscsi_cmd *cmd, 216 u32 seq_send_order) 217 { 218 u32 i; 219 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]; 223 224 return NULL; 225 } 226 227 struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsi_cmd *cmd) 228 { 229 u32 i; 230 231 if (!cmd->seq_list) { 232 pr_err("struct iscsi_cmd->seq_list is NULL!\n"); 233 return NULL; 234 } 235 236 for (i = 0; i < cmd->seq_count; i++) { 237 if (cmd->seq_list[i].type != SEQTYPE_NORMAL) 238 continue; 239 if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) { 240 cmd->seq_send_order++; 241 return &cmd->seq_list[i]; 242 } 243 } 244 245 return NULL; 246 } 247 248 struct iscsi_r2t *iscsit_get_holder_for_r2tsn( 249 struct iscsi_cmd *cmd, 250 u32 r2t_sn) 251 { 252 struct iscsi_r2t *r2t; 253 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); 258 return r2t; 259 } 260 } 261 spin_unlock_bh(&cmd->r2t_lock); 262 263 return NULL; 264 } 265 266 static inline int iscsit_check_received_cmdsn(struct iscsi_session *sess, u32 cmdsn) 267 { 268 u32 max_cmdsn; 269 int ret; 270 271 /* 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 275 * CRC failures. 276 */ 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; 282 283 } else if (cmdsn == sess->exp_cmd_sn) { 284 sess->exp_cmd_sn++; 285 pr_debug("Received CmdSN matches ExpCmdSN," 286 " incremented ExpCmdSN to: 0x%08x\n", 287 sess->exp_cmd_sn); 288 ret = CMDSN_NORMAL_OPERATION; 289 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; 295 296 } else { 297 pr_err("Received CmdSN: 0x%08x is less than" 298 " ExpCmdSN: 0x%08x, ignoring.\n", cmdsn, 299 sess->exp_cmd_sn); 300 ret = CMDSN_LOWER_THAN_EXP; 301 } 302 303 return ret; 304 } 305 306 /* 307 * Commands may be received out of order if MC/S is in use. 308 * Ensure they are executed in CmdSN order. 309 */ 310 int iscsit_sequence_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 311 unsigned char *buf, __be32 cmdsn) 312 { 313 int ret, cmdsn_ret; 314 bool reject = false; 315 u8 reason = ISCSI_REASON_BOOKMARK_NO_RESOURCES; 316 317 mutex_lock(&conn->sess->cmdsn_mutex); 318 319 cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, be32_to_cpu(cmdsn)); 320 switch (cmdsn_ret) { 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); 325 else if (ret < 0) { 326 reject = true; 327 ret = CMDSN_ERROR_CANNOT_RECOVER; 328 } 329 break; 330 case CMDSN_HIGHER_THAN_EXP: 331 ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, be32_to_cpu(cmdsn)); 332 if (ret < 0) { 333 reject = true; 334 ret = CMDSN_ERROR_CANNOT_RECOVER; 335 break; 336 } 337 ret = CMDSN_HIGHER_THAN_EXP; 338 break; 339 case CMDSN_LOWER_THAN_EXP: 340 case CMDSN_MAXCMDSN_OVERRUN: 341 default: 342 cmd->i_state = ISTATE_REMOVE; 343 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state); 344 /* 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.. 348 */ 349 ret = CMDSN_LOWER_THAN_EXP; 350 break; 351 } 352 mutex_unlock(&conn->sess->cmdsn_mutex); 353 354 if (reject) 355 iscsit_reject_cmd(cmd, reason, buf); 356 357 return ret; 358 } 359 EXPORT_SYMBOL(iscsit_sequence_cmd); 360 361 int iscsit_check_unsolicited_dataout(struct iscsi_cmd *cmd, unsigned char *buf) 362 { 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); 367 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); 373 return -1; 374 } 375 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); 384 return -1; 385 } 386 387 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) 388 return 0; 389 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); 400 return -1; 401 } 402 return 0; 403 } 404 405 struct iscsi_cmd *iscsit_find_cmd_from_itt( 406 struct iscsi_conn *conn, 407 itt_t init_task_tag) 408 { 409 struct iscsi_cmd *cmd; 410 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); 415 return cmd; 416 } 417 } 418 spin_unlock_bh(&conn->cmd_lock); 419 420 pr_err("Unable to locate ITT: 0x%08x on CID: %hu", 421 init_task_tag, conn->cid); 422 return NULL; 423 } 424 EXPORT_SYMBOL(iscsit_find_cmd_from_itt); 425 426 struct iscsi_cmd *iscsit_find_cmd_from_itt_or_dump( 427 struct iscsi_conn *conn, 428 itt_t init_task_tag, 429 u32 length) 430 { 431 struct iscsi_cmd *cmd; 432 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) 436 continue; 437 if (cmd->init_task_tag == init_task_tag) { 438 spin_unlock_bh(&conn->cmd_lock); 439 return cmd; 440 } 441 } 442 spin_unlock_bh(&conn->cmd_lock); 443 444 pr_err("Unable to locate ITT: 0x%08x on CID: %hu," 445 " dumping payload\n", init_task_tag, conn->cid); 446 if (length) 447 iscsit_dump_data_payload(conn, length, 1); 448 449 return NULL; 450 } 451 EXPORT_SYMBOL(iscsit_find_cmd_from_itt_or_dump); 452 453 struct iscsi_cmd *iscsit_find_cmd_from_ttt( 454 struct iscsi_conn *conn, 455 u32 targ_xfer_tag) 456 { 457 struct iscsi_cmd *cmd = NULL; 458 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); 463 return cmd; 464 } 465 } 466 spin_unlock_bh(&conn->cmd_lock); 467 468 pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n", 469 targ_xfer_tag, conn->cid); 470 return NULL; 471 } 472 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, 477 itt_t init_task_tag) 478 { 479 struct iscsi_cmd *cmd = NULL; 480 struct iscsi_conn_recovery *cr; 481 /* 482 * Scan through the inactive connection recovery list's command list. 483 * If init_task_tag matches the command is still alligent. 484 */ 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); 492 493 *cr_ptr = cr; 494 *cmd_ptr = cmd; 495 return -2; 496 } 497 } 498 spin_unlock(&cr->conn_recovery_cmd_lock); 499 } 500 spin_unlock(&sess->cr_i_lock); 501 /* 502 * Scan through the active connection recovery list's command list. 503 * If init_task_tag matches the command is ready to be reassigned. 504 */ 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); 512 513 *cr_ptr = cr; 514 *cmd_ptr = cmd; 515 return 0; 516 } 517 } 518 spin_unlock(&cr->conn_recovery_cmd_lock); 519 } 520 spin_unlock(&sess->cr_a_lock); 521 522 return -1; 523 } 524 525 void iscsit_add_cmd_to_immediate_queue( 526 struct iscsi_cmd *cmd, 527 struct iscsi_conn *conn, 528 u8 state) 529 { 530 struct iscsi_queue_req *qr; 531 532 qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC); 533 if (!qr) { 534 pr_err("Unable to allocate memory for" 535 " struct iscsi_queue_req\n"); 536 return; 537 } 538 INIT_LIST_HEAD(&qr->qr_list); 539 qr->cmd = cmd; 540 qr->state = state; 541 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); 547 548 wake_up(&conn->queues_wq); 549 } 550 EXPORT_SYMBOL(iscsit_add_cmd_to_immediate_queue); 551 552 struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsi_conn *conn) 553 { 554 struct iscsi_queue_req *qr; 555 556 spin_lock_bh(&conn->immed_queue_lock); 557 if (list_empty(&conn->immed_queue_list)) { 558 spin_unlock_bh(&conn->immed_queue_lock); 559 return NULL; 560 } 561 qr = list_first_entry(&conn->immed_queue_list, 562 struct iscsi_queue_req, qr_list); 563 564 list_del(&qr->qr_list); 565 if (qr->cmd) 566 atomic_dec(&qr->cmd->immed_queue_count); 567 spin_unlock_bh(&conn->immed_queue_lock); 568 569 return qr; 570 } 571 572 static void iscsit_remove_cmd_from_immediate_queue( 573 struct iscsi_cmd *cmd, 574 struct iscsi_conn *conn) 575 { 576 struct iscsi_queue_req *qr, *qr_tmp; 577 578 spin_lock_bh(&conn->immed_queue_lock); 579 if (!atomic_read(&cmd->immed_queue_count)) { 580 spin_unlock_bh(&conn->immed_queue_lock); 581 return; 582 } 583 584 list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) { 585 if (qr->cmd != cmd) 586 continue; 587 588 atomic_dec(&qr->cmd->immed_queue_count); 589 list_del(&qr->qr_list); 590 kmem_cache_free(lio_qr_cache, qr); 591 } 592 spin_unlock_bh(&conn->immed_queue_lock); 593 594 if (atomic_read(&cmd->immed_queue_count)) { 595 pr_err("ITT: 0x%08x immed_queue_count: %d\n", 596 cmd->init_task_tag, 597 atomic_read(&cmd->immed_queue_count)); 598 } 599 } 600 601 int iscsit_add_cmd_to_response_queue( 602 struct iscsi_cmd *cmd, 603 struct iscsi_conn *conn, 604 u8 state) 605 { 606 struct iscsi_queue_req *qr; 607 608 qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC); 609 if (!qr) { 610 pr_err("Unable to allocate memory for" 611 " struct iscsi_queue_req\n"); 612 return -ENOMEM; 613 } 614 INIT_LIST_HEAD(&qr->qr_list); 615 qr->cmd = cmd; 616 qr->state = state; 617 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); 622 623 wake_up(&conn->queues_wq); 624 return 0; 625 } 626 627 struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsi_conn *conn) 628 { 629 struct iscsi_queue_req *qr; 630 631 spin_lock_bh(&conn->response_queue_lock); 632 if (list_empty(&conn->response_queue_list)) { 633 spin_unlock_bh(&conn->response_queue_lock); 634 return NULL; 635 } 636 637 qr = list_first_entry(&conn->response_queue_list, 638 struct iscsi_queue_req, qr_list); 639 640 list_del(&qr->qr_list); 641 if (qr->cmd) 642 atomic_dec(&qr->cmd->response_queue_count); 643 spin_unlock_bh(&conn->response_queue_lock); 644 645 return qr; 646 } 647 648 static void iscsit_remove_cmd_from_response_queue( 649 struct iscsi_cmd *cmd, 650 struct iscsi_conn *conn) 651 { 652 struct iscsi_queue_req *qr, *qr_tmp; 653 654 spin_lock_bh(&conn->response_queue_lock); 655 if (!atomic_read(&cmd->response_queue_count)) { 656 spin_unlock_bh(&conn->response_queue_lock); 657 return; 658 } 659 660 list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list, 661 qr_list) { 662 if (qr->cmd != cmd) 663 continue; 664 665 atomic_dec(&qr->cmd->response_queue_count); 666 list_del(&qr->qr_list); 667 kmem_cache_free(lio_qr_cache, qr); 668 } 669 spin_unlock_bh(&conn->response_queue_lock); 670 671 if (atomic_read(&cmd->response_queue_count)) { 672 pr_err("ITT: 0x%08x response_queue_count: %d\n", 673 cmd->init_task_tag, 674 atomic_read(&cmd->response_queue_count)); 675 } 676 } 677 678 bool iscsit_conn_all_queues_empty(struct iscsi_conn *conn) 679 { 680 bool empty; 681 682 spin_lock_bh(&conn->immed_queue_lock); 683 empty = list_empty(&conn->immed_queue_list); 684 spin_unlock_bh(&conn->immed_queue_lock); 685 686 if (!empty) 687 return empty; 688 689 spin_lock_bh(&conn->response_queue_lock); 690 empty = list_empty(&conn->response_queue_list); 691 spin_unlock_bh(&conn->response_queue_lock); 692 693 return empty; 694 } 695 696 void iscsit_free_queue_reqs_for_conn(struct iscsi_conn *conn) 697 { 698 struct iscsi_queue_req *qr, *qr_tmp; 699 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); 703 if (qr->cmd) 704 atomic_dec(&qr->cmd->immed_queue_count); 705 706 kmem_cache_free(lio_qr_cache, qr); 707 } 708 spin_unlock_bh(&conn->immed_queue_lock); 709 710 spin_lock_bh(&conn->response_queue_lock); 711 list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list, 712 qr_list) { 713 list_del(&qr->qr_list); 714 if (qr->cmd) 715 atomic_dec(&qr->cmd->response_queue_count); 716 717 kmem_cache_free(lio_qr_cache, qr); 718 } 719 spin_unlock_bh(&conn->response_queue_lock); 720 } 721 722 void iscsit_release_cmd(struct iscsi_cmd *cmd) 723 { 724 struct iscsi_session *sess; 725 struct se_cmd *se_cmd = &cmd->se_cmd; 726 727 WARN_ON(!list_empty(&cmd->i_conn_node)); 728 729 if (cmd->conn) 730 sess = cmd->conn->sess; 731 else 732 sess = cmd->sess; 733 734 BUG_ON(!sess || !sess->se_sess); 735 736 kfree(cmd->buf_ptr); 737 kfree(cmd->pdu_list); 738 kfree(cmd->seq_list); 739 kfree(cmd->tmr_req); 740 kfree(cmd->overflow_buf); 741 kfree(cmd->iov_data); 742 kfree(cmd->text_in_ptr); 743 744 target_free_tag(sess->se_sess, se_cmd); 745 } 746 EXPORT_SYMBOL(iscsit_release_cmd); 747 748 void __iscsit_free_cmd(struct iscsi_cmd *cmd, bool check_queues) 749 { 750 struct iscsi_conn *conn = cmd->conn; 751 752 WARN_ON(!list_empty(&cmd->i_conn_node)); 753 754 if (cmd->data_direction == DMA_TO_DEVICE) { 755 iscsit_stop_dataout_timer(cmd); 756 iscsit_free_r2ts_from_list(cmd); 757 } 758 if (cmd->data_direction == DMA_FROM_DEVICE) 759 iscsit_free_all_datain_reqs(cmd); 760 761 if (conn && check_queues) { 762 iscsit_remove_cmd_from_immediate_queue(cmd, conn); 763 iscsit_remove_cmd_from_response_queue(cmd, conn); 764 } 765 766 if (conn && conn->conn_transport->iscsit_unmap_cmd) 767 conn->conn_transport->iscsit_unmap_cmd(conn, cmd); 768 } 769 770 void iscsit_free_cmd(struct iscsi_cmd *cmd, bool shutdown) 771 { 772 struct se_cmd *se_cmd = cmd->se_cmd.se_tfo ? &cmd->se_cmd : NULL; 773 int rc; 774 775 WARN_ON(!list_empty(&cmd->i_conn_node)); 776 777 __iscsit_free_cmd(cmd, shutdown); 778 if (se_cmd) { 779 rc = transport_generic_free_cmd(se_cmd, shutdown); 780 if (!rc && shutdown && se_cmd->se_sess) { 781 __iscsit_free_cmd(cmd, shutdown); 782 target_put_sess_cmd(se_cmd); 783 } 784 } else { 785 iscsit_release_cmd(cmd); 786 } 787 } 788 EXPORT_SYMBOL(iscsit_free_cmd); 789 790 int iscsit_check_session_usage_count(struct iscsi_session *sess) 791 { 792 spin_lock_bh(&sess->session_usage_lock); 793 if (sess->session_usage_count != 0) { 794 sess->session_waiting_on_uc = 1; 795 spin_unlock_bh(&sess->session_usage_lock); 796 if (in_interrupt()) 797 return 2; 798 799 wait_for_completion(&sess->session_waiting_on_uc_comp); 800 return 1; 801 } 802 spin_unlock_bh(&sess->session_usage_lock); 803 804 return 0; 805 } 806 807 void iscsit_dec_session_usage_count(struct iscsi_session *sess) 808 { 809 spin_lock_bh(&sess->session_usage_lock); 810 sess->session_usage_count--; 811 812 if (!sess->session_usage_count && sess->session_waiting_on_uc) 813 complete(&sess->session_waiting_on_uc_comp); 814 815 spin_unlock_bh(&sess->session_usage_lock); 816 } 817 818 void iscsit_inc_session_usage_count(struct iscsi_session *sess) 819 { 820 spin_lock_bh(&sess->session_usage_lock); 821 sess->session_usage_count++; 822 spin_unlock_bh(&sess->session_usage_lock); 823 } 824 825 struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid) 826 { 827 struct iscsi_conn *conn; 828 829 spin_lock_bh(&sess->conn_lock); 830 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) { 831 if ((conn->cid == cid) && 832 (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) { 833 iscsit_inc_conn_usage_count(conn); 834 spin_unlock_bh(&sess->conn_lock); 835 return conn; 836 } 837 } 838 spin_unlock_bh(&sess->conn_lock); 839 840 return NULL; 841 } 842 843 struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid) 844 { 845 struct iscsi_conn *conn; 846 847 spin_lock_bh(&sess->conn_lock); 848 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) { 849 if (conn->cid == cid) { 850 iscsit_inc_conn_usage_count(conn); 851 spin_lock(&conn->state_lock); 852 atomic_set(&conn->connection_wait_rcfr, 1); 853 spin_unlock(&conn->state_lock); 854 spin_unlock_bh(&sess->conn_lock); 855 return conn; 856 } 857 } 858 spin_unlock_bh(&sess->conn_lock); 859 860 return NULL; 861 } 862 863 void iscsit_check_conn_usage_count(struct iscsi_conn *conn) 864 { 865 spin_lock_bh(&conn->conn_usage_lock); 866 if (conn->conn_usage_count != 0) { 867 conn->conn_waiting_on_uc = 1; 868 spin_unlock_bh(&conn->conn_usage_lock); 869 870 wait_for_completion(&conn->conn_waiting_on_uc_comp); 871 return; 872 } 873 spin_unlock_bh(&conn->conn_usage_lock); 874 } 875 876 void iscsit_dec_conn_usage_count(struct iscsi_conn *conn) 877 { 878 spin_lock_bh(&conn->conn_usage_lock); 879 conn->conn_usage_count--; 880 881 if (!conn->conn_usage_count && conn->conn_waiting_on_uc) 882 complete(&conn->conn_waiting_on_uc_comp); 883 884 spin_unlock_bh(&conn->conn_usage_lock); 885 } 886 887 void iscsit_inc_conn_usage_count(struct iscsi_conn *conn) 888 { 889 spin_lock_bh(&conn->conn_usage_lock); 890 conn->conn_usage_count++; 891 spin_unlock_bh(&conn->conn_usage_lock); 892 } 893 894 static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response) 895 { 896 u8 state; 897 struct iscsi_cmd *cmd; 898 899 cmd = iscsit_allocate_cmd(conn, TASK_RUNNING); 900 if (!cmd) 901 return -1; 902 903 cmd->iscsi_opcode = ISCSI_OP_NOOP_IN; 904 state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE : 905 ISTATE_SEND_NOPIN_NO_RESPONSE; 906 cmd->init_task_tag = RESERVED_ITT; 907 cmd->targ_xfer_tag = (want_response) ? 908 session_get_next_ttt(conn->sess) : 0xFFFFFFFF; 909 spin_lock_bh(&conn->cmd_lock); 910 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 911 spin_unlock_bh(&conn->cmd_lock); 912 913 if (want_response) 914 iscsit_start_nopin_response_timer(conn); 915 iscsit_add_cmd_to_immediate_queue(cmd, conn, state); 916 917 return 0; 918 } 919 920 void iscsit_handle_nopin_response_timeout(struct timer_list *t) 921 { 922 struct iscsi_conn *conn = from_timer(conn, t, nopin_response_timer); 923 struct iscsi_session *sess = conn->sess; 924 925 iscsit_inc_conn_usage_count(conn); 926 927 spin_lock_bh(&conn->nopin_timer_lock); 928 if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) { 929 spin_unlock_bh(&conn->nopin_timer_lock); 930 iscsit_dec_conn_usage_count(conn); 931 return; 932 } 933 934 pr_err("Did not receive response to NOPIN on CID: %hu, failing" 935 " connection for I_T Nexus %s,i,0x%6phN,%s,t,0x%02x\n", 936 conn->cid, sess->sess_ops->InitiatorName, sess->isid, 937 sess->tpg->tpg_tiqn->tiqn, (u32)sess->tpg->tpgt); 938 conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING; 939 spin_unlock_bh(&conn->nopin_timer_lock); 940 941 iscsit_fill_cxn_timeout_err_stats(sess); 942 iscsit_cause_connection_reinstatement(conn, 0); 943 iscsit_dec_conn_usage_count(conn); 944 } 945 946 void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn) 947 { 948 struct iscsi_session *sess = conn->sess; 949 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess); 950 951 spin_lock_bh(&conn->nopin_timer_lock); 952 if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) { 953 spin_unlock_bh(&conn->nopin_timer_lock); 954 return; 955 } 956 957 mod_timer(&conn->nopin_response_timer, 958 (get_jiffies_64() + na->nopin_response_timeout * HZ)); 959 spin_unlock_bh(&conn->nopin_timer_lock); 960 } 961 962 void iscsit_start_nopin_response_timer(struct iscsi_conn *conn) 963 { 964 struct iscsi_session *sess = conn->sess; 965 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess); 966 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); 970 return; 971 } 972 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); 977 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); 981 } 982 983 void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn) 984 { 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); 988 return; 989 } 990 conn->nopin_response_timer_flags |= ISCSI_TF_STOP; 991 spin_unlock_bh(&conn->nopin_timer_lock); 992 993 del_timer_sync(&conn->nopin_response_timer); 994 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); 998 } 999 1000 void iscsit_handle_nopin_timeout(struct timer_list *t) 1001 { 1002 struct iscsi_conn *conn = from_timer(conn, t, nopin_timer); 1003 1004 iscsit_inc_conn_usage_count(conn); 1005 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); 1010 return; 1011 } 1012 conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING; 1013 spin_unlock_bh(&conn->nopin_timer_lock); 1014 1015 iscsit_add_nopin(conn, 1); 1016 iscsit_dec_conn_usage_count(conn); 1017 } 1018 1019 void __iscsit_start_nopin_timer(struct iscsi_conn *conn) 1020 { 1021 struct iscsi_session *sess = conn->sess; 1022 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess); 1023 1024 lockdep_assert_held(&conn->nopin_timer_lock); 1025 1026 /* 1027 * NOPIN timeout is disabled. 1028 */ 1029 if (!na->nopin_timeout) 1030 return; 1031 1032 if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) 1033 return; 1034 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); 1038 1039 pr_debug("Started NOPIN Timer on CID: %d at %u second" 1040 " interval\n", conn->cid, na->nopin_timeout); 1041 } 1042 1043 void iscsit_start_nopin_timer(struct iscsi_conn *conn) 1044 { 1045 spin_lock_bh(&conn->nopin_timer_lock); 1046 __iscsit_start_nopin_timer(conn); 1047 spin_unlock_bh(&conn->nopin_timer_lock); 1048 } 1049 1050 void iscsit_stop_nopin_timer(struct iscsi_conn *conn) 1051 { 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); 1055 return; 1056 } 1057 conn->nopin_timer_flags |= ISCSI_TF_STOP; 1058 spin_unlock_bh(&conn->nopin_timer_lock); 1059 1060 del_timer_sync(&conn->nopin_timer); 1061 1062 spin_lock_bh(&conn->nopin_timer_lock); 1063 conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING; 1064 spin_unlock_bh(&conn->nopin_timer_lock); 1065 } 1066 1067 int iscsit_send_tx_data( 1068 struct iscsi_cmd *cmd, 1069 struct iscsi_conn *conn, 1070 int use_misc) 1071 { 1072 int tx_sent, tx_size; 1073 u32 iov_count; 1074 struct kvec *iov; 1075 1076 send_data: 1077 tx_size = cmd->tx_size; 1078 1079 if (!use_misc) { 1080 iov = &cmd->iov_data[0]; 1081 iov_count = cmd->iov_data_count; 1082 } else { 1083 iov = &cmd->iov_misc[0]; 1084 iov_count = cmd->iov_misc_count; 1085 } 1086 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"); 1091 goto send_data; 1092 } else 1093 return -1; 1094 } 1095 cmd->tx_size = 0; 1096 1097 return 0; 1098 } 1099 1100 int iscsit_fe_sendpage_sg( 1101 struct iscsi_cmd *cmd, 1102 struct iscsi_conn *conn) 1103 { 1104 struct scatterlist *sg = cmd->first_data_sg; 1105 struct kvec iov; 1106 u32 tx_hdr_size, data_len; 1107 u32 offset = cmd->first_data_sg_off; 1108 int tx_sent, iov_off; 1109 1110 send_hdr: 1111 tx_hdr_size = ISCSI_HDR_LEN; 1112 if (conn->conn_ops->HeaderDigest) 1113 tx_hdr_size += ISCSI_CRC_LEN; 1114 1115 iov.iov_base = cmd->pdu; 1116 iov.iov_len = tx_hdr_size; 1117 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"); 1122 goto send_hdr; 1123 } 1124 return -1; 1125 } 1126 1127 data_len = cmd->tx_size - tx_hdr_size - cmd->padding; 1128 /* 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[] 1131 */ 1132 if (conn->conn_ops->DataDigest) { 1133 data_len -= ISCSI_CRC_LEN; 1134 if (cmd->padding) 1135 iov_off = (cmd->iov_data_count - 2); 1136 else 1137 iov_off = (cmd->iov_data_count - 1); 1138 } else { 1139 iov_off = (cmd->iov_data_count - 1); 1140 } 1141 /* 1142 * Perform sendpage() for each page in the scatterlist 1143 */ 1144 while (data_len) { 1145 u32 space = (sg->length - offset); 1146 u32 sub_len = min_t(u32, data_len, space); 1147 send_pg: 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" 1153 " -EAGAIN\n"); 1154 goto send_pg; 1155 } 1156 1157 pr_err("tcp_sendpage() failure: %d\n", 1158 tx_sent); 1159 return -1; 1160 } 1161 1162 data_len -= sub_len; 1163 offset = 0; 1164 sg = sg_next(sg); 1165 } 1166 1167 send_padding: 1168 if (cmd->padding) { 1169 struct kvec *iov_p = &cmd->iov_data[iov_off++]; 1170 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"); 1175 goto send_padding; 1176 } 1177 return -1; 1178 } 1179 } 1180 1181 send_datacrc: 1182 if (conn->conn_ops->DataDigest) { 1183 struct kvec *iov_d = &cmd->iov_data[iov_off]; 1184 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"); 1189 goto send_datacrc; 1190 } 1191 return -1; 1192 } 1193 } 1194 1195 return 0; 1196 } 1197 1198 /* 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. 1202 * 1203 * Parameters: iSCSI Connection, Status Class, Status Detail. 1204 * Returns: 0 on success, -1 on error. 1205 */ 1206 int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail) 1207 { 1208 struct iscsi_login_rsp *hdr; 1209 struct iscsi_login *login = conn->conn_login; 1210 1211 login->login_failed = 1; 1212 iscsit_collect_login_stats(conn, status_class, status_detail); 1213 1214 memset(&login->rsp[0], 0, ISCSI_HDR_LEN); 1215 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; 1221 1222 return conn->conn_transport->iscsit_put_login_tx(conn, login, 0); 1223 } 1224 1225 void iscsit_print_session_params(struct iscsi_session *sess) 1226 { 1227 struct iscsi_conn *conn; 1228 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); 1235 1236 iscsi_dump_sess_ops(sess->sess_ops); 1237 } 1238 1239 static int iscsit_do_rx_data( 1240 struct iscsi_conn *conn, 1241 struct iscsi_data_count *count) 1242 { 1243 int data = count->data_length, rx_loop = 0, total_rx = 0; 1244 struct msghdr msg; 1245 1246 if (!conn || !conn->sock || !conn->conn_ops) 1247 return -1; 1248 1249 memset(&msg, 0, sizeof(struct msghdr)); 1250 iov_iter_kvec(&msg.msg_iter, READ, count->iov, count->iov_count, data); 1251 1252 while (msg_data_left(&msg)) { 1253 rx_loop = sock_recvmsg(conn->sock, &msg, MSG_WAITALL); 1254 if (rx_loop <= 0) { 1255 pr_debug("rx_loop: %d total_rx: %d\n", 1256 rx_loop, total_rx); 1257 return rx_loop; 1258 } 1259 total_rx += rx_loop; 1260 pr_debug("rx_loop: %d, total_rx: %d, data: %d\n", 1261 rx_loop, total_rx, data); 1262 } 1263 1264 return total_rx; 1265 } 1266 1267 int rx_data( 1268 struct iscsi_conn *conn, 1269 struct kvec *iov, 1270 int iov_count, 1271 int data) 1272 { 1273 struct iscsi_data_count c; 1274 1275 if (!conn || !conn->sock || !conn->conn_ops) 1276 return -1; 1277 1278 memset(&c, 0, sizeof(struct iscsi_data_count)); 1279 c.iov = iov; 1280 c.iov_count = iov_count; 1281 c.data_length = data; 1282 c.type = ISCSI_RX_DATA; 1283 1284 return iscsit_do_rx_data(conn, &c); 1285 } 1286 1287 int tx_data( 1288 struct iscsi_conn *conn, 1289 struct kvec *iov, 1290 int iov_count, 1291 int data) 1292 { 1293 struct msghdr msg; 1294 int total_tx = 0; 1295 1296 if (!conn || !conn->sock || !conn->conn_ops) 1297 return -1; 1298 1299 if (data <= 0) { 1300 pr_err("Data length is: %d\n", data); 1301 return -1; 1302 } 1303 1304 memset(&msg, 0, sizeof(struct msghdr)); 1305 1306 iov_iter_kvec(&msg.msg_iter, WRITE, iov, iov_count, data); 1307 1308 while (msg_data_left(&msg)) { 1309 int tx_loop = sock_sendmsg(conn->sock, &msg); 1310 if (tx_loop <= 0) { 1311 pr_debug("tx_loop: %d total_tx %d\n", 1312 tx_loop, total_tx); 1313 return tx_loop; 1314 } 1315 total_tx += tx_loop; 1316 pr_debug("tx_loop: %d, total_tx: %d, data: %d\n", 1317 tx_loop, total_tx, data); 1318 } 1319 1320 return total_tx; 1321 } 1322 1323 void iscsit_collect_login_stats( 1324 struct iscsi_conn *conn, 1325 u8 status_class, 1326 u8 status_detail) 1327 { 1328 struct iscsi_param *intrname = NULL; 1329 struct iscsi_tiqn *tiqn; 1330 struct iscsi_login_stats *ls; 1331 1332 tiqn = iscsit_snmp_get_tiqn(conn); 1333 if (!tiqn) 1334 return; 1335 1336 ls = &tiqn->login_stats; 1337 1338 spin_lock(&ls->lock); 1339 if (status_class == ISCSI_STATUS_CLS_SUCCESS) 1340 ls->accepts++; 1341 else if (status_class == ISCSI_STATUS_CLS_REDIRECT) { 1342 ls->redirects++; 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; 1356 } else { 1357 ls->other_fails++; 1358 ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER; 1359 } 1360 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, 1365 conn->param_list); 1366 strlcpy(ls->last_intr_fail_name, 1367 (intrname ? intrname->value : "Unknown"), 1368 sizeof(ls->last_intr_fail_name)); 1369 1370 ls->last_intr_fail_ip_family = conn->login_family; 1371 1372 ls->last_intr_fail_sockaddr = conn->login_sockaddr; 1373 ls->last_fail_time = get_jiffies_64(); 1374 } 1375 1376 spin_unlock(&ls->lock); 1377 } 1378 1379 struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn) 1380 { 1381 struct iscsi_portal_group *tpg; 1382 1383 if (!conn) 1384 return NULL; 1385 1386 tpg = conn->tpg; 1387 if (!tpg) 1388 return NULL; 1389 1390 if (!tpg->tpg_tiqn) 1391 return NULL; 1392 1393 return tpg->tpg_tiqn; 1394 } 1395 1396 void iscsit_fill_cxn_timeout_err_stats(struct iscsi_session *sess) 1397 { 1398 struct iscsi_portal_group *tpg = sess->tpg; 1399 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn; 1400 1401 if (!tiqn) 1402 return; 1403 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); 1413 } 1414