| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/******************************************************************************* |
| 3 |
* This file contains the iSCSI Target specific utility functions. |
| 4 |
* |
| 5 |
* (c) Copyright 2007-2013 Datera, Inc. |
| 6 |
* |
| 7 |
* Author: Nicholas A. Bellinger <[email protected]> |
| 8 |
* |
| 9 |
******************************************************************************/ |
| 10 |
|
| 11 |
#include <linux/list.h> |
| 12 |
#include <linux/sched/signal.h> |
| 13 |
#include <net/ipv6.h> /* ipv6_addr_equal() */ |
| 14 |
#include <scsi/scsi_tcq.h> |
| 15 |
#include <scsi/iscsi_proto.h> |
| 16 |
#include <target/target_core_base.h> |
| 17 |
#include <target/target_core_fabric.h> |
| 18 |
#include <target/iscsi/iscsi_transport.h> |
| 19 |
|
| 20 |
#include <target/iscsi/iscsi_target_core.h> |
| 21 |
#include "iscsi_target_parameters.h" |
| 22 |
#include "iscsi_target_seq_pdu_list.h" |
| 23 |
#include "iscsi_target_datain_values.h" |
| 24 |
#include "iscsi_target_erl0.h" |
| 25 |
#include "iscsi_target_erl1.h" |
| 26 |
#include "iscsi_target_erl2.h" |
| 27 |
#include "iscsi_target_tpg.h" |
| 28 |
#include "iscsi_target_util.h" |
| 29 |
#include "iscsi_target.h" |
| 30 |
|
| 31 |
extern struct list_head g_tiqn_list; |
| 32 |
extern spinlock_t tiqn_lock; |
| 33 |
|
| 34 |
int iscsit_add_r2t_to_list( |
| 35 |
struct iscsit_cmd *cmd, |
| 36 |
u32 offset, |
| 37 |
u32 xfer_len, |
| 38 |
int recovery, |
| 39 |
u32 r2t_sn) |
| 40 |
{ |
| 41 |
struct iscsi_r2t *r2t; |
| 42 |
|
| 43 |
lockdep_assert_held(&cmd->r2t_lock); |
| 44 |
|
| 45 |
WARN_ON_ONCE((s32)xfer_len < 0); |
| 46 |
|
| 47 |
r2t = kmem_cache_zalloc(lio_r2t_cache, GFP_ATOMIC); |
| 48 |
if (!r2t) { |
| 49 |
pr_err("Unable to allocate memory for struct iscsi_r2t.\n"); |
| 50 |
return -1; |
| 51 |
} |
| 52 |
INIT_LIST_HEAD(&r2t->r2t_list); |
| 53 |
|
| 54 |
r2t->recovery_r2t = recovery; |
| 55 |
r2t->r2t_sn = (!r2t_sn) ? cmd->r2t_sn++ : r2t_sn; |
| 56 |
r2t->offset = offset; |
| 57 |
r2t->xfer_len = xfer_len; |
| 58 |
list_add_tail(&r2t->r2t_list, &cmd->cmd_r2t_list); |
| 59 |
spin_unlock_bh(&cmd->r2t_lock); |
| 60 |
|
| 61 |
iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T); |
| 62 |
|
| 63 |
spin_lock_bh(&cmd->r2t_lock); |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
struct iscsi_r2t *iscsit_get_r2t_for_eos( |
| 68 |
struct iscsit_cmd *cmd, |
| 69 |
u32 offset, |
| 70 |
u32 length) |
| 71 |
{ |
| 72 |
struct iscsi_r2t *r2t; |
| 73 |
|
| 74 |
spin_lock_bh(&cmd->r2t_lock); |
| 75 |
list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) { |
| 76 |
if ((r2t->offset <= offset) && |
| 77 |
(r2t->offset + r2t->xfer_len) >= (offset + length)) { |
| 78 |
spin_unlock_bh(&cmd->r2t_lock); |
| 79 |
return r2t; |
| 80 |
} |
| 81 |
} |
| 82 |
spin_unlock_bh(&cmd->r2t_lock); |
| 83 |
|
| 84 |
pr_err("Unable to locate R2T for Offset: %u, Length:" |
| 85 |
" %u\n", offset, length); |
| 86 |
return NULL; |
| 87 |
} |
| 88 |
|
| 89 |
struct iscsi_r2t *iscsit_get_r2t_from_list(struct iscsit_cmd *cmd) |
| 90 |
{ |
| 91 |
struct iscsi_r2t *r2t; |
| 92 |
|
| 93 |
spin_lock_bh(&cmd->r2t_lock); |
| 94 |
list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) { |
| 95 |
if (!r2t->sent_r2t) { |
| 96 |
spin_unlock_bh(&cmd->r2t_lock); |
| 97 |
return r2t; |
| 98 |
} |
| 99 |
} |
| 100 |
spin_unlock_bh(&cmd->r2t_lock); |
| 101 |
|
| 102 |
pr_err("Unable to locate next R2T to send for ITT:" |
| 103 |
" 0x%08x.\n", cmd->init_task_tag); |
| 104 |
return NULL; |
| 105 |
} |
| 106 |
|
| 107 |
void iscsit_free_r2t(struct iscsi_r2t *r2t, struct iscsit_cmd *cmd) |
| 108 |
{ |
| 109 |
lockdep_assert_held(&cmd->r2t_lock); |
| 110 |
|
| 111 |
list_del(&r2t->r2t_list); |
| 112 |
kmem_cache_free(lio_r2t_cache, r2t); |
| 113 |
} |
| 114 |
|
| 115 |
void iscsit_free_r2ts_from_list(struct iscsit_cmd *cmd) |
| 116 |
{ |
| 117 |
struct iscsi_r2t *r2t, *r2t_tmp; |
| 118 |
|
| 119 |
spin_lock_bh(&cmd->r2t_lock); |
| 120 |
list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list) |
| 121 |
iscsit_free_r2t(r2t, cmd); |
| 122 |
spin_unlock_bh(&cmd->r2t_lock); |
| 123 |
} |
| 124 |
|
| 125 |
static int iscsit_wait_for_tag(struct se_session *se_sess, int state, int *cpup) |
| 126 |
{ |
| 127 |
int tag = -1; |
| 128 |
DEFINE_SBQ_WAIT(wait); |
| 129 |
struct sbq_wait_state *ws; |
| 130 |
struct sbitmap_queue *sbq; |
| 131 |
|
| 132 |
if (state == TASK_RUNNING) |
| 133 |
return tag; |
| 134 |
|
| 135 |
sbq = &se_sess->sess_tag_pool; |
| 136 |
ws = &sbq->ws[0]; |
| 137 |
for (;;) { |
| 138 |
sbitmap_prepare_to_wait(sbq, ws, &wait, state); |
| 139 |
if (signal_pending_state(state, current)) |
| 140 |
break; |
| 141 |
tag = sbitmap_queue_get(sbq, cpup); |
| 142 |
if (tag >= 0) |
| 143 |
break; |
| 144 |
schedule(); |
| 145 |
} |
| 146 |
|
| 147 |
sbitmap_finish_wait(sbq, ws, &wait); |
| 148 |
return tag; |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
* May be called from software interrupt (timer) context for allocating |
| 153 |
* iSCSI NopINs. |
| 154 |
*/ |
| 155 |
struct iscsit_cmd *iscsit_allocate_cmd(struct iscsit_conn *conn, int state) |
| 156 |
{ |
| 157 |
struct iscsit_cmd *cmd; |
| 158 |
struct se_session *se_sess = conn->sess->se_sess; |
| 159 |
int size, tag, cpu; |
| 160 |
|
| 161 |
tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu); |
| 162 |
if (tag < 0) |
| 163 |
tag = iscsit_wait_for_tag(se_sess, state, &cpu); |
| 164 |
if (tag < 0) |
| 165 |
return NULL; |
| 166 |
|
| 167 |
size = sizeof(struct iscsit_cmd) + conn->conn_transport->priv_size; |
| 168 |
cmd = (struct iscsit_cmd *)(se_sess->sess_cmd_map + (tag * size)); |
| 169 |
memset(cmd, 0, size); |
| 170 |
|
| 171 |
cmd->se_cmd.map_tag = tag; |
| 172 |
cmd->se_cmd.map_cpu = cpu; |
| 173 |
cmd->conn = conn; |
| 174 |
cmd->data_direction = DMA_NONE; |
| 175 |
INIT_LIST_HEAD(&cmd->i_conn_node); |
| 176 |
INIT_LIST_HEAD(&cmd->datain_list); |
| 177 |
INIT_LIST_HEAD(&cmd->cmd_r2t_list); |
| 178 |
spin_lock_init(&cmd->datain_lock); |
| 179 |
spin_lock_init(&cmd->dataout_timeout_lock); |
| 180 |
spin_lock_init(&cmd->istate_lock); |
| 181 |
spin_lock_init(&cmd->error_lock); |
| 182 |
spin_lock_init(&cmd->r2t_lock); |
| 183 |
timer_setup(&cmd->dataout_timer, iscsit_handle_dataout_timeout, 0); |
| 184 |
|
| 185 |
return cmd; |
| 186 |
} |
| 187 |
EXPORT_SYMBOL(iscsit_allocate_cmd); |
| 188 |
|
| 189 |
struct iscsi_seq *iscsit_get_seq_holder_for_datain( |
| 190 |
struct iscsit_cmd *cmd, |
| 191 |
u32 seq_send_order) |
| 192 |
{ |
| 193 |
u32 i; |
| 194 |
|
| 195 |
for (i = 0; i < cmd->seq_count; i++) |
| 196 |
if (cmd->seq_list[i].seq_send_order == seq_send_order) |
| 197 |
return &cmd->seq_list[i]; |
| 198 |
|
| 199 |
return NULL; |
| 200 |
} |
| 201 |
|
| 202 |
struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsit_cmd *cmd) |
| 203 |
{ |
| 204 |
u32 i; |
| 205 |
|
| 206 |
if (!cmd->seq_list) { |
| 207 |
pr_err("struct iscsit_cmd->seq_list is NULL!\n"); |
| 208 |
return NULL; |
| 209 |
} |
| 210 |
|
| 211 |
for (i = 0; i < cmd->seq_count; i++) { |
| 212 |
if (cmd->seq_list[i].type != SEQTYPE_NORMAL) |
| 213 |
continue; |
| 214 |
if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) { |
| 215 |
cmd->seq_send_order++; |
| 216 |
return &cmd->seq_list[i]; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
return NULL; |
| 221 |
} |
| 222 |
|
| 223 |
struct iscsi_r2t *iscsit_get_holder_for_r2tsn( |
| 224 |
struct iscsit_cmd *cmd, |
| 225 |
u32 r2t_sn) |
| 226 |
{ |
| 227 |
struct iscsi_r2t *r2t; |
| 228 |
|
| 229 |
spin_lock_bh(&cmd->r2t_lock); |
| 230 |
list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) { |
| 231 |
if (r2t->r2t_sn == r2t_sn) { |
| 232 |
spin_unlock_bh(&cmd->r2t_lock); |
| 233 |
return r2t; |
| 234 |
} |
| 235 |
} |
| 236 |
spin_unlock_bh(&cmd->r2t_lock); |
| 237 |
|
| 238 |
return NULL; |
| 239 |
} |
| 240 |
|
| 241 |
static inline int iscsit_check_received_cmdsn(struct iscsit_session *sess, u32 cmdsn) |
| 242 |
{ |
| 243 |
u32 max_cmdsn; |
| 244 |
int ret; |
| 245 |
|
| 246 |
/* |
| 247 |
* This is the proper method of checking received CmdSN against |
| 248 |
* ExpCmdSN and MaxCmdSN values, as well as accounting for out |
| 249 |
* or order CmdSNs due to multiple connection sessions and/or |
| 250 |
* CRC failures. |
| 251 |
*/ |
| 252 |
max_cmdsn = atomic_read(&sess->max_cmd_sn); |
| 253 |
if (iscsi_sna_gt(cmdsn, max_cmdsn)) { |
| 254 |
pr_err("Received CmdSN: 0x%08x is greater than" |
| 255 |
" MaxCmdSN: 0x%08x, ignoring.\n", cmdsn, max_cmdsn); |
| 256 |
ret = CMDSN_MAXCMDSN_OVERRUN; |
| 257 |
|
| 258 |
} else if (cmdsn == sess->exp_cmd_sn) { |
| 259 |
sess->exp_cmd_sn++; |
| 260 |
pr_debug("Received CmdSN matches ExpCmdSN," |
| 261 |
" incremented ExpCmdSN to: 0x%08x\n", |
| 262 |
sess->exp_cmd_sn); |
| 263 |
ret = CMDSN_NORMAL_OPERATION; |
| 264 |
|
| 265 |
} else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) { |
| 266 |
pr_debug("Received CmdSN: 0x%08x is greater" |
| 267 |
" than ExpCmdSN: 0x%08x, not acknowledging.\n", |
| 268 |
cmdsn, sess->exp_cmd_sn); |
| 269 |
ret = CMDSN_HIGHER_THAN_EXP; |
| 270 |
|
| 271 |
} else { |
| 272 |
pr_err("Received CmdSN: 0x%08x is less than" |
| 273 |
" ExpCmdSN: 0x%08x, ignoring.\n", cmdsn, |
| 274 |
sess->exp_cmd_sn); |
| 275 |
ret = CMDSN_LOWER_THAN_EXP; |
| 276 |
} |
| 277 |
|
| 278 |
return ret; |
| 279 |
} |
| 280 |
|
| 281 |
/* |
| 282 |
* Commands may be received out of order if MC/S is in use. |
| 283 |
* Ensure they are executed in CmdSN order. |
| 284 |
*/ |
| 285 |
int iscsit_sequence_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd, |
| 286 |
unsigned char *buf, __be32 cmdsn) |
| 287 |
{ |
| 288 |
int ret, cmdsn_ret; |
| 289 |
bool reject = false; |
| 290 |
u8 reason = ISCSI_REASON_BOOKMARK_NO_RESOURCES; |
| 291 |
|
| 292 |
mutex_lock(&conn->sess->cmdsn_mutex); |
| 293 |
|
| 294 |
cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, be32_to_cpu(cmdsn)); |
| 295 |
switch (cmdsn_ret) { |
| 296 |
case CMDSN_NORMAL_OPERATION: |
| 297 |
ret = iscsit_execute_cmd(cmd, 0); |
| 298 |
if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list)) |
| 299 |
iscsit_execute_ooo_cmdsns(conn->sess); |
| 300 |
else if (ret < 0) { |
| 301 |
reject = true; |
| 302 |
ret = CMDSN_ERROR_CANNOT_RECOVER; |
| 303 |
} |
| 304 |
break; |
| 305 |
case CMDSN_HIGHER_THAN_EXP: |
| 306 |
ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, be32_to_cpu(cmdsn)); |
| 307 |
if (ret < 0) { |
| 308 |
reject = true; |
| 309 |
ret = CMDSN_ERROR_CANNOT_RECOVER; |
| 310 |
break; |
| 311 |
} |
| 312 |
ret = CMDSN_HIGHER_THAN_EXP; |
| 313 |
break; |
| 314 |
case CMDSN_LOWER_THAN_EXP: |
| 315 |
case CMDSN_MAXCMDSN_OVERRUN: |
| 316 |
default: |
| 317 |
cmd->i_state = ISTATE_REMOVE; |
| 318 |
iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state); |
| 319 |
/* |
| 320 |
* Existing callers for iscsit_sequence_cmd() will silently |
| 321 |
* ignore commands with CMDSN_LOWER_THAN_EXP, so force this |
| 322 |
* return for CMDSN_MAXCMDSN_OVERRUN as well.. |
| 323 |
*/ |
| 324 |
ret = CMDSN_LOWER_THAN_EXP; |
| 325 |
break; |
| 326 |
} |
| 327 |
mutex_unlock(&conn->sess->cmdsn_mutex); |
| 328 |
|
| 329 |
if (reject) |
| 330 |
iscsit_reject_cmd(cmd, reason, buf); |
| 331 |
|
| 332 |
return ret; |
| 333 |
} |
| 334 |
EXPORT_SYMBOL(iscsit_sequence_cmd); |
| 335 |
|
| 336 |
struct iscsit_cmd *iscsit_find_cmd_from_itt( |
| 337 |
struct iscsit_conn *conn, |
| 338 |
itt_t init_task_tag) |
| 339 |
{ |
| 340 |
struct iscsit_cmd *cmd; |
| 341 |
|
| 342 |
spin_lock_bh(&conn->cmd_lock); |
| 343 |
list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) { |
| 344 |
if (cmd->init_task_tag == init_task_tag) { |
| 345 |
spin_unlock_bh(&conn->cmd_lock); |
| 346 |
return cmd; |
| 347 |
} |
| 348 |
} |
| 349 |
spin_unlock_bh(&conn->cmd_lock); |
| 350 |
|
| 351 |
pr_err("Unable to locate ITT: 0x%08x on CID: %hu", |
| 352 |
init_task_tag, conn->cid); |
| 353 |
return NULL; |
| 354 |
} |
| 355 |
EXPORT_SYMBOL(iscsit_find_cmd_from_itt); |
| 356 |
|
| 357 |
struct iscsit_cmd *iscsit_find_cmd_from_itt_or_dump( |
| 358 |
struct iscsit_conn *conn, |
| 359 |
itt_t init_task_tag, |
| 360 |
u32 length) |
| 361 |
{ |
| 362 |
struct iscsit_cmd *cmd; |
| 363 |
|
| 364 |
spin_lock_bh(&conn->cmd_lock); |
| 365 |
list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) { |
| 366 |
if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) |
| 367 |
continue; |
| 368 |
if (cmd->init_task_tag == init_task_tag) { |
| 369 |
spin_unlock_bh(&conn->cmd_lock); |
| 370 |
return cmd; |
| 371 |
} |
| 372 |
} |
| 373 |
spin_unlock_bh(&conn->cmd_lock); |
| 374 |
|
| 375 |
pr_err("Unable to locate ITT: 0x%08x on CID: %hu," |
| 376 |
" dumping payload\n", init_task_tag, conn->cid); |
| 377 |
if (length) |
| 378 |
iscsit_dump_data_payload(conn, length, 1); |
| 379 |
|
| 380 |
return NULL; |
| 381 |
} |
| 382 |
EXPORT_SYMBOL(iscsit_find_cmd_from_itt_or_dump); |
| 383 |
|
| 384 |
struct iscsit_cmd *iscsit_find_cmd_from_ttt( |
| 385 |
struct iscsit_conn *conn, |
| 386 |
u32 targ_xfer_tag) |
| 387 |
{ |
| 388 |
struct iscsit_cmd *cmd = NULL; |
| 389 |
|
| 390 |
spin_lock_bh(&conn->cmd_lock); |
| 391 |
list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) { |
| 392 |
if (cmd->targ_xfer_tag == targ_xfer_tag) { |
| 393 |
spin_unlock_bh(&conn->cmd_lock); |
| 394 |
return cmd; |
| 395 |
} |
| 396 |
} |
| 397 |
spin_unlock_bh(&conn->cmd_lock); |
| 398 |
|
| 399 |
pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n", |
| 400 |
targ_xfer_tag, conn->cid); |
| 401 |
return NULL; |
| 402 |
} |
| 403 |
|
| 404 |
int iscsit_find_cmd_for_recovery( |
| 405 |
struct iscsit_session *sess, |
| 406 |
struct iscsit_cmd **cmd_ptr, |
| 407 |
struct iscsi_conn_recovery **cr_ptr, |
| 408 |
itt_t init_task_tag) |
| 409 |
{ |
| 410 |
struct iscsit_cmd *cmd = NULL; |
| 411 |
struct iscsi_conn_recovery *cr; |
| 412 |
/* |
| 413 |
* Scan through the inactive connection recovery list's command list. |
| 414 |
* If init_task_tag matches the command is still alligent. |
| 415 |
*/ |
| 416 |
spin_lock(&sess->cr_i_lock); |
| 417 |
list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) { |
| 418 |
spin_lock(&cr->conn_recovery_cmd_lock); |
| 419 |
list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) { |
| 420 |
if (cmd->init_task_tag == init_task_tag) { |
| 421 |
spin_unlock(&cr->conn_recovery_cmd_lock); |
| 422 |
spin_unlock(&sess->cr_i_lock); |
| 423 |
|
| 424 |
*cr_ptr = cr; |
| 425 |
*cmd_ptr = cmd; |
| 426 |
return -2; |
| 427 |
} |
| 428 |
} |
| 429 |
spin_unlock(&cr->conn_recovery_cmd_lock); |
| 430 |
} |
| 431 |
spin_unlock(&sess->cr_i_lock); |
| 432 |
/* |
| 433 |
* Scan through the active connection recovery list's command list. |
| 434 |
* If init_task_tag matches the command is ready to be reassigned. |
| 435 |
*/ |
| 436 |
spin_lock(&sess->cr_a_lock); |
| 437 |
list_for_each_entry(cr, &sess->cr_active_list, cr_list) { |
| 438 |
spin_lock(&cr->conn_recovery_cmd_lock); |
| 439 |
list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) { |
| 440 |
if (cmd->init_task_tag == init_task_tag) { |
| 441 |
spin_unlock(&cr->conn_recovery_cmd_lock); |
| 442 |
spin_unlock(&sess->cr_a_lock); |
| 443 |
|
| 444 |
*cr_ptr = cr; |
| 445 |
*cmd_ptr = cmd; |
| 446 |
return 0; |
| 447 |
} |
| 448 |
} |
| 449 |
spin_unlock(&cr->conn_recovery_cmd_lock); |
| 450 |
} |
| 451 |
spin_unlock(&sess->cr_a_lock); |
| 452 |
|
| 453 |
return -1; |
| 454 |
} |
| 455 |
|
| 456 |
void iscsit_add_cmd_to_immediate_queue( |
| 457 |
struct iscsit_cmd *cmd, |
| 458 |
struct iscsit_conn *conn, |
| 459 |
u8 state) |
| 460 |
{ |
| 461 |
struct iscsi_queue_req *qr; |
| 462 |
|
| 463 |
qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC); |
| 464 |
if (!qr) { |
| 465 |
pr_err("Unable to allocate memory for" |
| 466 |
" struct iscsi_queue_req\n"); |
| 467 |
return; |
| 468 |
} |
| 469 |
INIT_LIST_HEAD(&qr->qr_list); |
| 470 |
qr->cmd = cmd; |
| 471 |
qr->state = state; |
| 472 |
|
| 473 |
spin_lock_bh(&conn->immed_queue_lock); |
| 474 |
list_add_tail(&qr->qr_list, &conn->immed_queue_list); |
| 475 |
atomic_inc(&cmd->immed_queue_count); |
| 476 |
atomic_set(&conn->check_immediate_queue, 1); |
| 477 |
spin_unlock_bh(&conn->immed_queue_lock); |
| 478 |
|
| 479 |
wake_up(&conn->queues_wq); |
| 480 |
} |
| 481 |
EXPORT_SYMBOL(iscsit_add_cmd_to_immediate_queue); |
| 482 |
|
| 483 |
struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsit_conn *conn) |
| 484 |
{ |
| 485 |
struct iscsi_queue_req *qr; |
| 486 |
|
| 487 |
spin_lock_bh(&conn->immed_queue_lock); |
| 488 |
if (list_empty(&conn->immed_queue_list)) { |
| 489 |
spin_unlock_bh(&conn->immed_queue_lock); |
| 490 |
return NULL; |
| 491 |
} |
| 492 |
qr = list_first_entry(&conn->immed_queue_list, |
| 493 |
struct iscsi_queue_req, qr_list); |
| 494 |
|
| 495 |
list_del(&qr->qr_list); |
| 496 |
if (qr->cmd) |
| 497 |
atomic_dec(&qr->cmd->immed_queue_count); |
| 498 |
spin_unlock_bh(&conn->immed_queue_lock); |
| 499 |
|
| 500 |
return qr; |
| 501 |
} |
| 502 |
|
| 503 |
static void iscsit_remove_cmd_from_immediate_queue( |
| 504 |
struct iscsit_cmd *cmd, |
| 505 |
struct iscsit_conn *conn) |
| 506 |
{ |
| 507 |
struct iscsi_queue_req *qr, *qr_tmp; |
| 508 |
|
| 509 |
spin_lock_bh(&conn->immed_queue_lock); |
| 510 |
if (!atomic_read(&cmd->immed_queue_count)) { |
| 511 |
spin_unlock_bh(&conn->immed_queue_lock); |
| 512 |
return; |
| 513 |
} |
| 514 |
|
| 515 |
list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) { |
| 516 |
if (qr->cmd != cmd) |
| 517 |
continue; |
| 518 |
|
| 519 |
atomic_dec(&qr->cmd->immed_queue_count); |
| 520 |
list_del(&qr->qr_list); |
| 521 |
kmem_cache_free(lio_qr_cache, qr); |
| 522 |
} |
| 523 |
spin_unlock_bh(&conn->immed_queue_lock); |
| 524 |
|
| 525 |
if (atomic_read(&cmd->immed_queue_count)) { |
| 526 |
pr_err("ITT: 0x%08x immed_queue_count: %d\n", |
| 527 |
cmd->init_task_tag, |
| 528 |
atomic_read(&cmd->immed_queue_count)); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
int iscsit_add_cmd_to_response_queue( |
| 533 |
struct iscsit_cmd *cmd, |
| 534 |
struct iscsit_conn *conn, |
| 535 |
u8 state) |
| 536 |
{ |
| 537 |
struct iscsi_queue_req *qr; |
| 538 |
|
| 539 |
qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC); |
| 540 |
if (!qr) { |
| 541 |
pr_err("Unable to allocate memory for" |
| 542 |
" struct iscsi_queue_req\n"); |
| 543 |
return -ENOMEM; |
| 544 |
} |
| 545 |
INIT_LIST_HEAD(&qr->qr_list); |
| 546 |
qr->cmd = cmd; |
| 547 |
qr->state = state; |
| 548 |
|
| 549 |
spin_lock_bh(&conn->response_queue_lock); |
| 550 |
list_add_tail(&qr->qr_list, &conn->response_queue_list); |
| 551 |
atomic_inc(&cmd->response_queue_count); |
| 552 |
spin_unlock_bh(&conn->response_queue_lock); |
| 553 |
|
| 554 |
wake_up(&conn->queues_wq); |
| 555 |
return 0; |
| 556 |
} |
| 557 |
|
| 558 |
struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsit_conn *conn) |
| 559 |
{ |
| 560 |
struct iscsi_queue_req *qr; |
| 561 |
|
| 562 |
spin_lock_bh(&conn->response_queue_lock); |
| 563 |
if (list_empty(&conn->response_queue_list)) { |
| 564 |
spin_unlock_bh(&conn->response_queue_lock); |
| 565 |
return NULL; |
| 566 |
} |
| 567 |
|
| 568 |
qr = list_first_entry(&conn->response_queue_list, |
| 569 |
struct iscsi_queue_req, qr_list); |
| 570 |
|
| 571 |
list_del(&qr->qr_list); |
| 572 |
if (qr->cmd) |
| 573 |
atomic_dec(&qr->cmd->response_queue_count); |
| 574 |
spin_unlock_bh(&conn->response_queue_lock); |
| 575 |
|
| 576 |
return qr; |
| 577 |
} |
| 578 |
|
| 579 |
static void iscsit_remove_cmd_from_response_queue( |
| 580 |
struct iscsit_cmd *cmd, |
| 581 |
struct iscsit_conn *conn) |
| 582 |
{ |
| 583 |
struct iscsi_queue_req *qr, *qr_tmp; |
| 584 |
|
| 585 |
spin_lock_bh(&conn->response_queue_lock); |
| 586 |
if (!atomic_read(&cmd->response_queue_count)) { |
| 587 |
spin_unlock_bh(&conn->response_queue_lock); |
| 588 |
return; |
| 589 |
} |
| 590 |
|
| 591 |
list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list, |
| 592 |
qr_list) { |
| 593 |
if (qr->cmd != cmd) |
| 594 |
continue; |
| 595 |
|
| 596 |
atomic_dec(&qr->cmd->response_queue_count); |
| 597 |
list_del(&qr->qr_list); |
| 598 |
kmem_cache_free(lio_qr_cache, qr); |
| 599 |
} |
| 600 |
spin_unlock_bh(&conn->response_queue_lock); |
| 601 |
|
| 602 |
if (atomic_read(&cmd->response_queue_count)) { |
| 603 |
pr_err("ITT: 0x%08x response_queue_count: %d\n", |
| 604 |
cmd->init_task_tag, |
| 605 |
atomic_read(&cmd->response_queue_count)); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
bool iscsit_conn_all_queues_empty(struct iscsit_conn *conn) |
| 610 |
{ |
| 611 |
bool empty; |
| 612 |
|
| 613 |
spin_lock_bh(&conn->immed_queue_lock); |
| 614 |
empty = list_empty(&conn->immed_queue_list); |
| 615 |
spin_unlock_bh(&conn->immed_queue_lock); |
| 616 |
|
| 617 |
if (!empty) |
| 618 |
return empty; |
| 619 |
|
| 620 |
spin_lock_bh(&conn->response_queue_lock); |
| 621 |
empty = list_empty(&conn->response_queue_list); |
| 622 |
spin_unlock_bh(&conn->response_queue_lock); |
| 623 |
|
| 624 |
return empty; |
| 625 |
} |
| 626 |
|
| 627 |
void iscsit_free_queue_reqs_for_conn(struct iscsit_conn *conn) |
| 628 |
{ |
| 629 |
struct iscsi_queue_req *qr, *qr_tmp; |
| 630 |
|
| 631 |
spin_lock_bh(&conn->immed_queue_lock); |
| 632 |
list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) { |
| 633 |
list_del(&qr->qr_list); |
| 634 |
if (qr->cmd) |
| 635 |
atomic_dec(&qr->cmd->immed_queue_count); |
| 636 |
|
| 637 |
kmem_cache_free(lio_qr_cache, qr); |
| 638 |
} |
| 639 |
spin_unlock_bh(&conn->immed_queue_lock); |
| 640 |
|
| 641 |
spin_lock_bh(&conn->response_queue_lock); |
| 642 |
list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list, |
| 643 |
qr_list) { |
| 644 |
list_del(&qr->qr_list); |
| 645 |
if (qr->cmd) |
| 646 |
atomic_dec(&qr->cmd->response_queue_count); |
| 647 |
|
| 648 |
kmem_cache_free(lio_qr_cache, qr); |
| 649 |
} |
| 650 |
spin_unlock_bh(&conn->response_queue_lock); |
| 651 |
} |
| 652 |
|
| 653 |
void iscsit_release_cmd(struct iscsit_cmd *cmd) |
| 654 |
{ |
| 655 |
struct iscsit_session *sess; |
| 656 |
struct se_cmd *se_cmd = &cmd->se_cmd; |
| 657 |
|
| 658 |
WARN_ON(!list_empty(&cmd->i_conn_node)); |
| 659 |
|
| 660 |
if (cmd->conn) |
| 661 |
sess = cmd->conn->sess; |
| 662 |
else |
| 663 |
sess = cmd->sess; |
| 664 |
|
| 665 |
BUG_ON(!sess || !sess->se_sess); |
| 666 |
|
| 667 |
kfree(cmd->buf_ptr); |
| 668 |
kfree(cmd->pdu_list); |
| 669 |
kfree(cmd->seq_list); |
| 670 |
kfree(cmd->tmr_req); |
| 671 |
kfree(cmd->overflow_buf); |
| 672 |
kfree(cmd->iov_data); |
| 673 |
kfree(cmd->text_in_ptr); |
| 674 |
|
| 675 |
target_free_tag(sess->se_sess, se_cmd); |
| 676 |
} |
| 677 |
EXPORT_SYMBOL(iscsit_release_cmd); |
| 678 |
|
| 679 |
void __iscsit_free_cmd(struct iscsit_cmd *cmd, bool check_queues) |
| 680 |
{ |
| 681 |
struct iscsit_conn *conn = cmd->conn; |
| 682 |
|
| 683 |
WARN_ON(!list_empty(&cmd->i_conn_node)); |
| 684 |
|
| 685 |
if (cmd->data_direction == DMA_TO_DEVICE) { |
| 686 |
iscsit_stop_dataout_timer(cmd); |
| 687 |
iscsit_free_r2ts_from_list(cmd); |
| 688 |
} |
| 689 |
if (cmd->data_direction == DMA_FROM_DEVICE) |
| 690 |
iscsit_free_all_datain_reqs(cmd); |
| 691 |
|
| 692 |
if (conn && check_queues) { |
| 693 |
iscsit_remove_cmd_from_immediate_queue(cmd, conn); |
| 694 |
iscsit_remove_cmd_from_response_queue(cmd, conn); |
| 695 |
} |
| 696 |
|
| 697 |
if (conn && conn->conn_transport->iscsit_unmap_cmd) |
| 698 |
conn->conn_transport->iscsit_unmap_cmd(conn, cmd); |
| 699 |
} |
| 700 |
|
| 701 |
void iscsit_free_cmd(struct iscsit_cmd *cmd, bool shutdown) |
| 702 |
{ |
| 703 |
struct se_cmd *se_cmd = cmd->se_cmd.se_tfo ? &cmd->se_cmd : NULL; |
| 704 |
int rc; |
| 705 |
|
| 706 |
WARN_ON(!list_empty(&cmd->i_conn_node)); |
| 707 |
|
| 708 |
__iscsit_free_cmd(cmd, shutdown); |
| 709 |
if (se_cmd) { |
| 710 |
rc = transport_generic_free_cmd(se_cmd, shutdown); |
| 711 |
if (!rc && shutdown && se_cmd->se_sess) { |
| 712 |
__iscsit_free_cmd(cmd, shutdown); |
| 713 |
target_put_sess_cmd(se_cmd); |
| 714 |
} |
| 715 |
} else { |
| 716 |
iscsit_release_cmd(cmd); |
| 717 |
} |
| 718 |
} |
| 719 |
EXPORT_SYMBOL(iscsit_free_cmd); |
| 720 |
|
| 721 |
bool iscsit_check_session_usage_count(struct iscsit_session *sess, |
| 722 |
bool can_sleep) |
| 723 |
{ |
| 724 |
spin_lock_bh(&sess->session_usage_lock); |
| 725 |
if (sess->session_usage_count != 0) { |
| 726 |
sess->session_waiting_on_uc = 1; |
| 727 |
spin_unlock_bh(&sess->session_usage_lock); |
| 728 |
if (!can_sleep) |
| 729 |
return true; |
| 730 |
|
| 731 |
wait_for_completion(&sess->session_waiting_on_uc_comp); |
| 732 |
return false; |
| 733 |
} |
| 734 |
spin_unlock_bh(&sess->session_usage_lock); |
| 735 |
|
| 736 |
return false; |
| 737 |
} |
| 738 |
|
| 739 |
void iscsit_dec_session_usage_count(struct iscsit_session *sess) |
| 740 |
{ |
| 741 |
spin_lock_bh(&sess->session_usage_lock); |
| 742 |
sess->session_usage_count--; |
| 743 |
|
| 744 |
if (!sess->session_usage_count && sess->session_waiting_on_uc) { |
| 745 |
spin_unlock_bh(&sess->session_usage_lock); |
| 746 |
complete(&sess->session_waiting_on_uc_comp); |
| 747 |
return; |
| 748 |
} |
| 749 |
|
| 750 |
spin_unlock_bh(&sess->session_usage_lock); |
| 751 |
} |
| 752 |
|
| 753 |
void iscsit_inc_session_usage_count(struct iscsit_session *sess) |
| 754 |
{ |
| 755 |
spin_lock_bh(&sess->session_usage_lock); |
| 756 |
sess->session_usage_count++; |
| 757 |
spin_unlock_bh(&sess->session_usage_lock); |
| 758 |
} |
| 759 |
|
| 760 |
struct iscsit_conn *iscsit_get_conn_from_cid(struct iscsit_session *sess, u16 cid) |
| 761 |
{ |
| 762 |
struct iscsit_conn *conn; |
| 763 |
|
| 764 |
spin_lock_bh(&sess->conn_lock); |
| 765 |
list_for_each_entry(conn, &sess->sess_conn_list, conn_list) { |
| 766 |
if ((conn->cid == cid) && |
| 767 |
(conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) { |
| 768 |
iscsit_inc_conn_usage_count(conn); |
| 769 |
spin_unlock_bh(&sess->conn_lock); |
| 770 |
return conn; |
| 771 |
} |
| 772 |
} |
| 773 |
spin_unlock_bh(&sess->conn_lock); |
| 774 |
|
| 775 |
return NULL; |
| 776 |
} |
| 777 |
|
| 778 |
struct iscsit_conn *iscsit_get_conn_from_cid_rcfr(struct iscsit_session *sess, u16 cid) |
| 779 |
{ |
| 780 |
struct iscsit_conn *conn; |
| 781 |
|
| 782 |
spin_lock_bh(&sess->conn_lock); |
| 783 |
list_for_each_entry(conn, &sess->sess_conn_list, conn_list) { |
| 784 |
if (conn->cid == cid) { |
| 785 |
iscsit_inc_conn_usage_count(conn); |
| 786 |
spin_lock(&conn->state_lock); |
| 787 |
atomic_set(&conn->connection_wait_rcfr, 1); |
| 788 |
spin_unlock(&conn->state_lock); |
| 789 |
spin_unlock_bh(&sess->conn_lock); |
| 790 |
return conn; |
| 791 |
} |
| 792 |
} |
| 793 |
spin_unlock_bh(&sess->conn_lock); |
| 794 |
|
| 795 |
return NULL; |
| 796 |
} |
| 797 |
|
| 798 |
void iscsit_check_conn_usage_count(struct iscsit_conn *conn) |
| 799 |
{ |
| 800 |
spin_lock_bh(&conn->conn_usage_lock); |
| 801 |
if (conn->conn_usage_count != 0) { |
| 802 |
conn->conn_waiting_on_uc = 1; |
| 803 |
spin_unlock_bh(&conn->conn_usage_lock); |
| 804 |
|
| 805 |
wait_for_completion(&conn->conn_waiting_on_uc_comp); |
| 806 |
return; |
| 807 |
} |
| 808 |
spin_unlock_bh(&conn->conn_usage_lock); |
| 809 |
} |
| 810 |
|
| 811 |
void iscsit_dec_conn_usage_count(struct iscsit_conn *conn) |
| 812 |
{ |
| 813 |
spin_lock_bh(&conn->conn_usage_lock); |
| 814 |
conn->conn_usage_count--; |
| 815 |
|
| 816 |
if (!conn->conn_usage_count && conn->conn_waiting_on_uc) { |
| 817 |
spin_unlock_bh(&conn->conn_usage_lock); |
| 818 |
complete(&conn->conn_waiting_on_uc_comp); |
| 819 |
return; |
| 820 |
} |
| 821 |
|
| 822 |
spin_unlock_bh(&conn->conn_usage_lock); |
| 823 |
} |
| 824 |
|
| 825 |
void iscsit_inc_conn_usage_count(struct iscsit_conn *conn) |
| 826 |
{ |
| 827 |
spin_lock_bh(&conn->conn_usage_lock); |
| 828 |
conn->conn_usage_count++; |
| 829 |
spin_unlock_bh(&conn->conn_usage_lock); |
| 830 |
} |
| 831 |
|
| 832 |
static int iscsit_add_nopin(struct iscsit_conn *conn, int want_response) |
| 833 |
{ |
| 834 |
u8 state; |
| 835 |
struct iscsit_cmd *cmd; |
| 836 |
|
| 837 |
cmd = iscsit_allocate_cmd(conn, TASK_RUNNING); |
| 838 |
if (!cmd) |
| 839 |
return -1; |
| 840 |
|
| 841 |
cmd->iscsi_opcode = ISCSI_OP_NOOP_IN; |
| 842 |
state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE : |
| 843 |
ISTATE_SEND_NOPIN_NO_RESPONSE; |
| 844 |
cmd->init_task_tag = RESERVED_ITT; |
| 845 |
cmd->targ_xfer_tag = (want_response) ? |
| 846 |
session_get_next_ttt(conn->sess) : 0xFFFFFFFF; |
| 847 |
spin_lock_bh(&conn->cmd_lock); |
| 848 |
list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); |
| 849 |
spin_unlock_bh(&conn->cmd_lock); |
| 850 |
|
| 851 |
if (want_response) |
| 852 |
iscsit_start_nopin_response_timer(conn); |
| 853 |
iscsit_add_cmd_to_immediate_queue(cmd, conn, state); |
| 854 |
|
| 855 |
return 0; |
| 856 |
} |
| 857 |
|
| 858 |
void iscsit_handle_nopin_response_timeout(struct timer_list *t) |
| 859 |
{ |
| 860 |
struct iscsit_conn *conn = timer_container_of(conn, t, |
| 861 |
nopin_response_timer); |
| 862 |
struct iscsit_session *sess = conn->sess; |
| 863 |
|
| 864 |
iscsit_inc_conn_usage_count(conn); |
| 865 |
|
| 866 |
spin_lock_bh(&conn->nopin_timer_lock); |
| 867 |
if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) { |
| 868 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 869 |
iscsit_dec_conn_usage_count(conn); |
| 870 |
return; |
| 871 |
} |
| 872 |
|
| 873 |
pr_err("Did not receive response to NOPIN on CID: %hu, failing" |
| 874 |
" connection for I_T Nexus %s,i,0x%6phN,%s,t,0x%02x\n", |
| 875 |
conn->cid, sess->sess_ops->InitiatorName, sess->isid, |
| 876 |
sess->tpg->tpg_tiqn->tiqn, (u32)sess->tpg->tpgt); |
| 877 |
conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING; |
| 878 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 879 |
|
| 880 |
iscsit_fill_cxn_timeout_err_stats(sess); |
| 881 |
iscsit_cause_connection_reinstatement(conn, 0); |
| 882 |
iscsit_dec_conn_usage_count(conn); |
| 883 |
} |
| 884 |
|
| 885 |
void iscsit_mod_nopin_response_timer(struct iscsit_conn *conn) |
| 886 |
{ |
| 887 |
struct iscsit_session *sess = conn->sess; |
| 888 |
struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess); |
| 889 |
|
| 890 |
spin_lock_bh(&conn->nopin_timer_lock); |
| 891 |
if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) { |
| 892 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 893 |
return; |
| 894 |
} |
| 895 |
|
| 896 |
mod_timer(&conn->nopin_response_timer, |
| 897 |
(get_jiffies_64() + na->nopin_response_timeout * HZ)); |
| 898 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 899 |
} |
| 900 |
|
| 901 |
void iscsit_start_nopin_response_timer(struct iscsit_conn *conn) |
| 902 |
{ |
| 903 |
struct iscsit_session *sess = conn->sess; |
| 904 |
struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess); |
| 905 |
|
| 906 |
spin_lock_bh(&conn->nopin_timer_lock); |
| 907 |
if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) { |
| 908 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 909 |
return; |
| 910 |
} |
| 911 |
|
| 912 |
conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP; |
| 913 |
conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING; |
| 914 |
mod_timer(&conn->nopin_response_timer, |
| 915 |
jiffies + na->nopin_response_timeout * HZ); |
| 916 |
|
| 917 |
pr_debug("Started NOPIN Response Timer on CID: %d to %u" |
| 918 |
" seconds\n", conn->cid, na->nopin_response_timeout); |
| 919 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 920 |
} |
| 921 |
|
| 922 |
void iscsit_stop_nopin_response_timer(struct iscsit_conn *conn) |
| 923 |
{ |
| 924 |
spin_lock_bh(&conn->nopin_timer_lock); |
| 925 |
if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) { |
| 926 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 927 |
return; |
| 928 |
} |
| 929 |
conn->nopin_response_timer_flags |= ISCSI_TF_STOP; |
| 930 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 931 |
|
| 932 |
timer_delete_sync(&conn->nopin_response_timer); |
| 933 |
|
| 934 |
spin_lock_bh(&conn->nopin_timer_lock); |
| 935 |
conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING; |
| 936 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 937 |
} |
| 938 |
|
| 939 |
void iscsit_handle_nopin_timeout(struct timer_list *t) |
| 940 |
{ |
| 941 |
struct iscsit_conn *conn = timer_container_of(conn, t, nopin_timer); |
| 942 |
|
| 943 |
iscsit_inc_conn_usage_count(conn); |
| 944 |
|
| 945 |
spin_lock_bh(&conn->nopin_timer_lock); |
| 946 |
if (conn->nopin_timer_flags & ISCSI_TF_STOP) { |
| 947 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 948 |
iscsit_dec_conn_usage_count(conn); |
| 949 |
return; |
| 950 |
} |
| 951 |
conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING; |
| 952 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 953 |
|
| 954 |
iscsit_add_nopin(conn, 1); |
| 955 |
iscsit_dec_conn_usage_count(conn); |
| 956 |
} |
| 957 |
|
| 958 |
void __iscsit_start_nopin_timer(struct iscsit_conn *conn) |
| 959 |
{ |
| 960 |
struct iscsit_session *sess = conn->sess; |
| 961 |
struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess); |
| 962 |
|
| 963 |
lockdep_assert_held(&conn->nopin_timer_lock); |
| 964 |
|
| 965 |
/* |
| 966 |
* NOPIN timeout is disabled. |
| 967 |
*/ |
| 968 |
if (!na->nopin_timeout) |
| 969 |
return; |
| 970 |
|
| 971 |
if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) |
| 972 |
return; |
| 973 |
|
| 974 |
conn->nopin_timer_flags &= ~ISCSI_TF_STOP; |
| 975 |
conn->nopin_timer_flags |= ISCSI_TF_RUNNING; |
| 976 |
mod_timer(&conn->nopin_timer, jiffies + na->nopin_timeout * HZ); |
| 977 |
|
| 978 |
pr_debug("Started NOPIN Timer on CID: %d at %u second" |
| 979 |
" interval\n", conn->cid, na->nopin_timeout); |
| 980 |
} |
| 981 |
|
| 982 |
void iscsit_start_nopin_timer(struct iscsit_conn *conn) |
| 983 |
{ |
| 984 |
spin_lock_bh(&conn->nopin_timer_lock); |
| 985 |
__iscsit_start_nopin_timer(conn); |
| 986 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 987 |
} |
| 988 |
|
| 989 |
void iscsit_stop_nopin_timer(struct iscsit_conn *conn) |
| 990 |
{ |
| 991 |
spin_lock_bh(&conn->nopin_timer_lock); |
| 992 |
if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) { |
| 993 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 994 |
return; |
| 995 |
} |
| 996 |
conn->nopin_timer_flags |= ISCSI_TF_STOP; |
| 997 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 998 |
|
| 999 |
timer_delete_sync(&conn->nopin_timer); |
| 1000 |
|
| 1001 |
spin_lock_bh(&conn->nopin_timer_lock); |
| 1002 |
conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING; |
| 1003 |
spin_unlock_bh(&conn->nopin_timer_lock); |
| 1004 |
} |
| 1005 |
|
| 1006 |
void iscsit_login_timeout(struct timer_list *t) |
| 1007 |
{ |
| 1008 |
struct iscsit_conn *conn = timer_container_of(conn, t, login_timer); |
| 1009 |
struct iscsi_login *login = conn->login; |
| 1010 |
|
| 1011 |
pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n"); |
| 1012 |
|
| 1013 |
spin_lock_bh(&conn->login_timer_lock); |
| 1014 |
login->login_failed = 1; |
| 1015 |
|
| 1016 |
if (conn->login_kworker) { |
| 1017 |
pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n", |
| 1018 |
conn->login_kworker->comm, conn->login_kworker->pid); |
| 1019 |
send_sig(SIGINT, conn->login_kworker, 1); |
| 1020 |
} else { |
| 1021 |
schedule_delayed_work(&conn->login_work, 0); |
| 1022 |
} |
| 1023 |
spin_unlock_bh(&conn->login_timer_lock); |
| 1024 |
} |
| 1025 |
|
| 1026 |
void iscsit_start_login_timer(struct iscsit_conn *conn, struct task_struct *kthr) |
| 1027 |
{ |
| 1028 |
pr_debug("Login timer started\n"); |
| 1029 |
|
| 1030 |
conn->login_kworker = kthr; |
| 1031 |
mod_timer(&conn->login_timer, jiffies + TA_LOGIN_TIMEOUT * HZ); |
| 1032 |
} |
| 1033 |
|
| 1034 |
int iscsit_set_login_timer_kworker(struct iscsit_conn *conn, struct task_struct *kthr) |
| 1035 |
{ |
| 1036 |
struct iscsi_login *login = conn->login; |
| 1037 |
int ret = 0; |
| 1038 |
|
| 1039 |
spin_lock_bh(&conn->login_timer_lock); |
| 1040 |
if (login->login_failed) { |
| 1041 |
/* The timer has already expired */ |
| 1042 |
ret = -1; |
| 1043 |
} else { |
| 1044 |
conn->login_kworker = kthr; |
| 1045 |
} |
| 1046 |
spin_unlock_bh(&conn->login_timer_lock); |
| 1047 |
|
| 1048 |
return ret; |
| 1049 |
} |
| 1050 |
|
| 1051 |
void iscsit_stop_login_timer(struct iscsit_conn *conn) |
| 1052 |
{ |
| 1053 |
pr_debug("Login timer stopped\n"); |
| 1054 |
timer_delete_sync(&conn->login_timer); |
| 1055 |
} |
| 1056 |
|
| 1057 |
int iscsit_send_tx_data( |
| 1058 |
struct iscsit_cmd *cmd, |
| 1059 |
struct iscsit_conn *conn, |
| 1060 |
int use_misc) |
| 1061 |
{ |
| 1062 |
int tx_sent, tx_size; |
| 1063 |
u32 iov_count; |
| 1064 |
struct kvec *iov; |
| 1065 |
|
| 1066 |
send_data: |
| 1067 |
tx_size = cmd->tx_size; |
| 1068 |
|
| 1069 |
if (!use_misc) { |
| 1070 |
iov = &cmd->iov_data[0]; |
| 1071 |
iov_count = cmd->iov_data_count; |
| 1072 |
} else { |
| 1073 |
iov = &cmd->iov_misc[0]; |
| 1074 |
iov_count = cmd->iov_misc_count; |
| 1075 |
} |
| 1076 |
|
| 1077 |
tx_sent = tx_data(conn, &iov[0], iov_count, tx_size); |
| 1078 |
if (tx_size != tx_sent) { |
| 1079 |
if (tx_sent == -EAGAIN) { |
| 1080 |
pr_err("tx_data() returned -EAGAIN\n"); |
| 1081 |
goto send_data; |
| 1082 |
} else |
| 1083 |
return -1; |
| 1084 |
} |
| 1085 |
cmd->tx_size = 0; |
| 1086 |
|
| 1087 |
return 0; |
| 1088 |
} |
| 1089 |
|
| 1090 |
int iscsit_fe_sendpage_sg( |
| 1091 |
struct iscsit_cmd *cmd, |
| 1092 |
struct iscsit_conn *conn) |
| 1093 |
{ |
| 1094 |
struct scatterlist *sg = cmd->first_data_sg; |
| 1095 |
struct bio_vec bvec; |
| 1096 |
struct msghdr msghdr = { .msg_flags = MSG_SPLICE_PAGES, }; |
| 1097 |
struct kvec iov; |
| 1098 |
u32 tx_hdr_size, data_len; |
| 1099 |
u32 offset = cmd->first_data_sg_off; |
| 1100 |
int tx_sent, iov_off; |
| 1101 |
|
| 1102 |
send_hdr: |
| 1103 |
tx_hdr_size = ISCSI_HDR_LEN; |
| 1104 |
if (conn->conn_ops->HeaderDigest) |
| 1105 |
tx_hdr_size += ISCSI_CRC_LEN; |
| 1106 |
|
| 1107 |
iov.iov_base = cmd->pdu; |
| 1108 |
iov.iov_len = tx_hdr_size; |
| 1109 |
|
| 1110 |
tx_sent = tx_data(conn, &iov, 1, tx_hdr_size); |
| 1111 |
if (tx_hdr_size != tx_sent) { |
| 1112 |
if (tx_sent == -EAGAIN) { |
| 1113 |
pr_err("tx_data() returned -EAGAIN\n"); |
| 1114 |
goto send_hdr; |
| 1115 |
} |
| 1116 |
return -1; |
| 1117 |
} |
| 1118 |
|
| 1119 |
data_len = cmd->tx_size - tx_hdr_size - cmd->padding; |
| 1120 |
/* |
| 1121 |
* Set iov_off used by padding and data digest tx_data() calls below |
| 1122 |
* in order to determine proper offset into cmd->iov_data[] |
| 1123 |
*/ |
| 1124 |
if (conn->conn_ops->DataDigest) { |
| 1125 |
data_len -= ISCSI_CRC_LEN; |
| 1126 |
if (cmd->padding) |
| 1127 |
iov_off = (cmd->iov_data_count - 2); |
| 1128 |
else |
| 1129 |
iov_off = (cmd->iov_data_count - 1); |
| 1130 |
} else { |
| 1131 |
iov_off = (cmd->iov_data_count - 1); |
| 1132 |
} |
| 1133 |
/* |
| 1134 |
* Perform sendpage() for each page in the scatterlist |
| 1135 |
*/ |
| 1136 |
while (data_len) { |
| 1137 |
u32 space = (sg->length - offset); |
| 1138 |
u32 sub_len = min_t(u32, data_len, space); |
| 1139 |
send_pg: |
| 1140 |
bvec_set_page(&bvec, sg_page(sg), sub_len, sg->offset + offset); |
| 1141 |
iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, sub_len); |
| 1142 |
|
| 1143 |
tx_sent = conn->sock->ops->sendmsg(conn->sock, &msghdr, |
| 1144 |
sub_len); |
| 1145 |
if (tx_sent != sub_len) { |
| 1146 |
if (tx_sent == -EAGAIN) { |
| 1147 |
pr_err("sendmsg/splice returned -EAGAIN\n"); |
| 1148 |
goto send_pg; |
| 1149 |
} |
| 1150 |
|
| 1151 |
pr_err("sendmsg/splice failure: %d\n", tx_sent); |
| 1152 |
return -1; |
| 1153 |
} |
| 1154 |
|
| 1155 |
data_len -= sub_len; |
| 1156 |
offset = 0; |
| 1157 |
sg = sg_next(sg); |
| 1158 |
} |
| 1159 |
|
| 1160 |
send_padding: |
| 1161 |
if (cmd->padding) { |
| 1162 |
struct kvec *iov_p = &cmd->iov_data[iov_off++]; |
| 1163 |
|
| 1164 |
tx_sent = tx_data(conn, iov_p, 1, cmd->padding); |
| 1165 |
if (cmd->padding != tx_sent) { |
| 1166 |
if (tx_sent == -EAGAIN) { |
| 1167 |
pr_err("tx_data() returned -EAGAIN\n"); |
| 1168 |
goto send_padding; |
| 1169 |
} |
| 1170 |
return -1; |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
send_datacrc: |
| 1175 |
if (conn->conn_ops->DataDigest) { |
| 1176 |
struct kvec *iov_d = &cmd->iov_data[iov_off]; |
| 1177 |
|
| 1178 |
tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN); |
| 1179 |
if (ISCSI_CRC_LEN != tx_sent) { |
| 1180 |
if (tx_sent == -EAGAIN) { |
| 1181 |
pr_err("tx_data() returned -EAGAIN\n"); |
| 1182 |
goto send_datacrc; |
| 1183 |
} |
| 1184 |
return -1; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
return 0; |
| 1189 |
} |
| 1190 |
|
| 1191 |
/* |
| 1192 |
* This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU |
| 1193 |
* back to the Initiator when an expection condition occurs with the |
| 1194 |
* errors set in status_class and status_detail. |
| 1195 |
* |
| 1196 |
* Parameters: iSCSI Connection, Status Class, Status Detail. |
| 1197 |
* Returns: 0 on success, -1 on error. |
| 1198 |
*/ |
| 1199 |
int iscsit_tx_login_rsp(struct iscsit_conn *conn, u8 status_class, u8 status_detail) |
| 1200 |
{ |
| 1201 |
struct iscsi_login_rsp *hdr; |
| 1202 |
struct iscsi_login *login = conn->conn_login; |
| 1203 |
|
| 1204 |
login->login_failed = 1; |
| 1205 |
iscsit_collect_login_stats(conn, status_class, status_detail); |
| 1206 |
|
| 1207 |
memset(&login->rsp[0], 0, ISCSI_HDR_LEN); |
| 1208 |
|
| 1209 |
hdr = (struct iscsi_login_rsp *)&login->rsp[0]; |
| 1210 |
hdr->opcode = ISCSI_OP_LOGIN_RSP; |
| 1211 |
hdr->status_class = status_class; |
| 1212 |
hdr->status_detail = status_detail; |
| 1213 |
hdr->itt = conn->login_itt; |
| 1214 |
|
| 1215 |
return conn->conn_transport->iscsit_put_login_tx(conn, login, 0); |
| 1216 |
} |
| 1217 |
|
| 1218 |
int rx_data( |
| 1219 |
struct iscsit_conn *conn, |
| 1220 |
struct kvec *iov, |
| 1221 |
int iov_count, |
| 1222 |
int data) |
| 1223 |
{ |
| 1224 |
int rx_loop = 0, total_rx = 0; |
| 1225 |
struct msghdr msg; |
| 1226 |
|
| 1227 |
if (!conn || !conn->sock || !conn->conn_ops) |
| 1228 |
return -1; |
| 1229 |
|
| 1230 |
memset(&msg, 0, sizeof(struct msghdr)); |
| 1231 |
iov_iter_kvec(&msg.msg_iter, ITER_DEST, iov, iov_count, data); |
| 1232 |
|
| 1233 |
while (msg_data_left(&msg)) { |
| 1234 |
rx_loop = sock_recvmsg(conn->sock, &msg, MSG_WAITALL); |
| 1235 |
if (rx_loop <= 0) { |
| 1236 |
pr_debug("rx_loop: %d total_rx: %d\n", |
| 1237 |
rx_loop, total_rx); |
| 1238 |
return rx_loop; |
| 1239 |
} |
| 1240 |
total_rx += rx_loop; |
| 1241 |
pr_debug("rx_loop: %d, total_rx: %d, data: %d\n", |
| 1242 |
rx_loop, total_rx, data); |
| 1243 |
} |
| 1244 |
|
| 1245 |
return total_rx; |
| 1246 |
} |
| 1247 |
|
| 1248 |
int tx_data( |
| 1249 |
struct iscsit_conn *conn, |
| 1250 |
struct kvec *iov, |
| 1251 |
int iov_count, |
| 1252 |
int data) |
| 1253 |
{ |
| 1254 |
struct msghdr msg; |
| 1255 |
int total_tx = 0; |
| 1256 |
|
| 1257 |
if (!conn || !conn->sock || !conn->conn_ops) |
| 1258 |
return -1; |
| 1259 |
|
| 1260 |
if (data <= 0) { |
| 1261 |
pr_err("Data length is: %d\n", data); |
| 1262 |
return -1; |
| 1263 |
} |
| 1264 |
|
| 1265 |
memset(&msg, 0, sizeof(struct msghdr)); |
| 1266 |
|
| 1267 |
iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, iov_count, data); |
| 1268 |
|
| 1269 |
while (msg_data_left(&msg)) { |
| 1270 |
int tx_loop = sock_sendmsg(conn->sock, &msg); |
| 1271 |
if (tx_loop <= 0) { |
| 1272 |
pr_debug("tx_loop: %d total_tx %d\n", |
| 1273 |
tx_loop, total_tx); |
| 1274 |
return tx_loop; |
| 1275 |
} |
| 1276 |
total_tx += tx_loop; |
| 1277 |
pr_debug("tx_loop: %d, total_tx: %d, data: %d\n", |
| 1278 |
tx_loop, total_tx, data); |
| 1279 |
} |
| 1280 |
|
| 1281 |
return total_tx; |
| 1282 |
} |
| 1283 |
|
| 1284 |
void iscsit_collect_login_stats( |
| 1285 |
struct iscsit_conn *conn, |
| 1286 |
u8 status_class, |
| 1287 |
u8 status_detail) |
| 1288 |
{ |
| 1289 |
struct iscsi_param *intrname = NULL; |
| 1290 |
struct iscsi_tiqn *tiqn; |
| 1291 |
struct iscsi_login_stats *ls; |
| 1292 |
|
| 1293 |
tiqn = iscsit_snmp_get_tiqn(conn); |
| 1294 |
if (!tiqn) |
| 1295 |
return; |
| 1296 |
|
| 1297 |
ls = &tiqn->login_stats; |
| 1298 |
|
| 1299 |
spin_lock(&ls->lock); |
| 1300 |
if (status_class == ISCSI_STATUS_CLS_SUCCESS) |
| 1301 |
ls->accepts++; |
| 1302 |
else if (status_class == ISCSI_STATUS_CLS_REDIRECT) { |
| 1303 |
ls->redirects++; |
| 1304 |
ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT; |
| 1305 |
} else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) && |
| 1306 |
(status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) { |
| 1307 |
ls->authenticate_fails++; |
| 1308 |
ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHENTICATE; |
| 1309 |
} else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) && |
| 1310 |
(status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) { |
| 1311 |
ls->authorize_fails++; |
| 1312 |
ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE; |
| 1313 |
} else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) && |
| 1314 |
(status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) { |
| 1315 |
ls->negotiate_fails++; |
| 1316 |
ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE; |
| 1317 |
} else { |
| 1318 |
ls->other_fails++; |
| 1319 |
ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER; |
| 1320 |
} |
| 1321 |
|
| 1322 |
/* Save initiator name, ip address and time, if it is a failed login */ |
| 1323 |
if (status_class != ISCSI_STATUS_CLS_SUCCESS) { |
| 1324 |
if (conn->param_list) |
| 1325 |
intrname = iscsi_find_param_from_key(INITIATORNAME, |
| 1326 |
conn->param_list); |
| 1327 |
strscpy(ls->last_intr_fail_name, |
| 1328 |
(intrname ? intrname->value : "Unknown"), |
| 1329 |
sizeof(ls->last_intr_fail_name)); |
| 1330 |
|
| 1331 |
ls->last_intr_fail_ip_family = conn->login_family; |
| 1332 |
|
| 1333 |
ls->last_intr_fail_sockaddr = conn->login_sockaddr; |
| 1334 |
ls->last_fail_time = get_jiffies_64(); |
| 1335 |
} |
| 1336 |
|
| 1337 |
spin_unlock(&ls->lock); |
| 1338 |
} |
| 1339 |
|
| 1340 |
struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsit_conn *conn) |
| 1341 |
{ |
| 1342 |
struct iscsi_portal_group *tpg; |
| 1343 |
|
| 1344 |
if (!conn) |
| 1345 |
return NULL; |
| 1346 |
|
| 1347 |
tpg = conn->tpg; |
| 1348 |
if (!tpg) |
| 1349 |
return NULL; |
| 1350 |
|
| 1351 |
if (!tpg->tpg_tiqn) |
| 1352 |
return NULL; |
| 1353 |
|
| 1354 |
return tpg->tpg_tiqn; |
| 1355 |
} |
| 1356 |
|
| 1357 |
void iscsit_fill_cxn_timeout_err_stats(struct iscsit_session *sess) |
| 1358 |
{ |
| 1359 |
struct iscsi_portal_group *tpg = sess->tpg; |
| 1360 |
struct iscsi_tiqn *tiqn = tpg->tpg_tiqn; |
| 1361 |
|
| 1362 |
if (!tiqn) |
| 1363 |
return; |
| 1364 |
|
| 1365 |
spin_lock_bh(&tiqn->sess_err_stats.lock); |
| 1366 |
strscpy(tiqn->sess_err_stats.last_sess_fail_rem_name, |
| 1367 |
sess->sess_ops->InitiatorName, |
| 1368 |
sizeof(tiqn->sess_err_stats.last_sess_fail_rem_name)); |
| 1369 |
tiqn->sess_err_stats.last_sess_failure_type = |
| 1370 |
ISCSI_SESS_ERR_CXN_TIMEOUT; |
| 1371 |
tiqn->sess_err_stats.cxn_timeout_errors++; |
| 1372 |
atomic_long_inc(&sess->conn_timeout_errors); |
| 1373 |
spin_unlock_bh(&tiqn->sess_err_stats.lock); |
| 1374 |
} |