/*******************************************************************************
* This file contains the iSCSI Target specific utility functions.
*
* (c) Copyright 2007-2013 Datera, Inc.
*
* Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
******************************************************************************/
#include <linux/list.h>
#include <linux/percpu_ida.h>
#include <scsi/scsi_tcq.h>
#include <scsi/iscsi_proto.h>
#include <target/target_core_base.h>
#include <target/target_core_fabric.h>
#include <target/target_core_configfs.h>
#include <target/iscsi/iscsi_transport.h>
#include "iscsi_target_core.h"
#include "iscsi_target_parameters.h"
#include "iscsi_target_seq_pdu_list.h"
#include "iscsi_target_datain_values.h"
#include "iscsi_target_erl0.h"
#include "iscsi_target_erl1.h"
#include "iscsi_target_erl2.h"
#include "iscsi_target_tpg.h"
#include "iscsi_target_tq.h"
#include "iscsi_target_util.h"
#include "iscsi_target.h"
#define PRINT_BUFF(buff, len) \
{ \
int zzz; \
\
pr_debug("%d:\n", __LINE__); \
for (zzz = 0; zzz < len; zzz++) { \
if (zzz % 16 == 0) { \
if (zzz) \
pr_debug("\n"); \
pr_debug("%4i: ", zzz); \
} \
pr_debug("%02x ", (unsigned char) (buff)[zzz]); \
} \
if ((len + 1) % 16) \
pr_debug("\n"); \
}
extern struct list_head g_tiqn_list;
extern spinlock_t tiqn_lock;
/*
* Called with cmd->r2t_lock held.
*/
int iscsit_add_r2t_to_list(
struct iscsi_cmd *cmd,
u32 offset,
u32 xfer_len,
int recovery,
u32 r2t_sn)
{
struct iscsi_r2t *r2t;
r2t = kmem_cache_zalloc(lio_r2t_cache, GFP_ATOMIC);
if (!r2t) {
pr_err("Unable to allocate memory for struct iscsi_r2t.\n");
return -1;
}
INIT_LIST_HEAD(&r2t->r2t_list);
r2t->recovery_r2t = recovery;
r2t->r2t_sn = (!r2t_sn) ? cmd->r2t_sn++ : r2t_sn;
r2t->offset = offset;
r2t->xfer_len = xfer_len;
list_add_tail(&r2t->r2t_list, &cmd->cmd_r2t_list);
spin_unlock_bh(&cmd->r2t_lock);
iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T);
spin_lock_bh(&cmd->r2t_lock);
return 0;
}
struct iscsi_r2t *iscsit_get_r2t_for_eos(
struct iscsi_cmd *cmd,
u32 offset,
u32 length)
{
struct iscsi_r2t *r2t;
spin_lock_bh(&cmd->r2t_lock);
list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
if ((r2t->offset <= offset) &&
(r2t->offset + r2t->xfer_len) >= (offset + length)) {
spin_unlock_bh(&cmd->r2t_lock);
return r2t;
}
}
spin_unlock_bh(&cmd->r2t_lock);
pr_err("Unable to locate R2T for Offset: %u, Length:"
" %u\n", offset, length);
return NULL;
}
struct iscsi_r2t *iscsit_get_r2t_from_list(struct iscsi_cmd *cmd)
{
struct iscsi_r2t *r2t;
spin_lock_bh(&cmd->r2t_lock);
list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
if (!r2t->sent_r2t) {
spin_unlock_bh(&cmd->r2t_lock);
return r2t;
}
}
spin_unlock_bh(&cmd->r2t_lock);
pr_err("Unable to locate next R2T to send for ITT:"
" 0x%08x.\n", cmd->init_task_tag);
return NULL;
}
/*
* Called with cmd->r2t_lock held.
*/
void iscsit_free_r2t(struct iscsi_r2t *r2t, struct iscsi_cmd *cmd)
{
list_del(&r2t->r2t_list);
kmem_cache_free(lio_r2t_cache, r2t);
}
void iscsit_free_r2ts_from_list(struct iscsi_cmd *cmd)
{
struct iscsi_r2t *r2t, *r2t_tmp;
spin_lock_bh(&cmd->r2t_lock);
list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list)
iscsit_free_r2t(r2t, cmd);
spin_unlock_bh(&cmd->r2t_lock);
}
/*
* May be called from software interrupt (timer) context for allocating
* iSCSI NopINs.
*/
struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, int state)
{
struct iscsi_cmd *cmd;
struct se_session *se_sess = conn->sess->se_sess;
int size, tag;
tag = percpu_ida_alloc(&se_sess->sess_tag_pool, state);
if (tag < 0)
return NULL;
size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
cmd = (struct iscsi_cmd *)(se_sess->sess_cmd_map + (tag * size));
memset(cmd, 0, size);
cmd->se_cmd.map_tag = tag;
cmd->conn = conn;
INIT_LIST_HEAD(&cmd->i_conn_node);
INIT_LIST_HEAD(&cmd->datain_list);
INIT_LIST_HEAD(&cmd->cmd_r2t_list);
spin_lock_init(&cmd->datain_lock);
spin_lock_init(&cmd->dataout_timeout_lock);
spin_lock_init(&cmd->istate_lock);
spin_lock_init(&cmd->error_lock);
spin_lock_init(&cmd->r2t_lock);
return cmd;
}
EXPORT_SYMBOL(iscsit_allocate_cmd);
struct iscsi_seq *iscsit_get_seq_holder_for_datain(
struct iscsi_cmd *cmd,
u32 seq_send_order)
{
u32 i;
for (i = 0; i < cmd->seq_count; i++)
if (cmd->seq_list[i].seq_send_order == seq_send_order)
return &cmd->seq_list[i];
return NULL;
}
struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsi_cmd *cmd)
{
u32 i;
if (!cmd->seq_list) {
pr_err("struct iscsi_cmd->seq_list is NULL!\n");
return NULL;
}
for (i = 0; i < cmd->seq_count; i++) {
if (cmd->seq_list[i].type != SEQTYPE_NORMAL)
continue;
if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) {
cmd->seq_send_order++;
return &cmd->seq_list[i];
}
}
return NULL;
}
struct iscsi_r2t *iscsit_get_holder_for_r2tsn(
struct iscsi_cmd *cmd,
u32 r2t_sn)
{
struct iscsi_r2t *r2t;
spin_lock_bh(&cmd->r2t_lock);
list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
if (r2t->r2t_sn == r2t_sn) {
spin_unlock_bh(&cmd->r2t_lock);
return r2t;
}
}
spin_unlock_bh(&cmd->r2t_lock);
return NULL;
}
static inline int iscsit_check_received_cmdsn(struct iscsi_session *sess, u32 cmdsn)
{
int ret;
/*
* This is the proper method of checking received CmdSN against
* ExpCmdSN and MaxCmdSN values, as well as accounting for out
* or order CmdSNs due to multiple connection sessions and/or
* CRC failures.
*/
if (iscsi_sna_gt(cmdsn, sess->max_cmd_sn)) {
pr_err("Received CmdSN: 0x%08x is greater than"
" MaxCmdSN: 0x%08x, ignoring.\n", cmdsn,
sess->max_cmd_sn);
ret = CMDSN_MAXCMDSN_OVERRUN;
} else if (cmdsn == sess->exp_cmd_sn) {
sess->exp_cmd_sn++;
pr_debug("Received CmdSN matches ExpCmdSN,"
" incremented ExpCmdSN to: 0x%08x\n",
sess->exp_cmd_sn);
ret = CMDSN_NORMAL_OPERATION;
} else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) {
pr_debug("Received CmdSN: 0x%08x is greater"
" than ExpCmdSN: 0x%08x, not acknowledging.\n",
cmdsn, sess->exp_cmd_sn);
ret = CMDSN_HIGHER_THAN_EXP;
} else {
pr_err("Received CmdSN: 0x%08x is less than"
" ExpCmdSN: 0x%08x, ignoring.\n", cmdsn,
sess->exp_cmd_sn);
ret = CMDSN_LOWER_THAN_EXP;
}
return ret;
}
/*
* Commands may be received out of order if MC/S is in use.
* Ensure they are executed in CmdSN order.
*/
int iscsit_sequence_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
unsigned char *buf, __be32 cmdsn)
{
int ret, cmdsn_ret;
bool reject = false;
u8 reason = ISCSI_REASON_BOOKMARK_NO_RESOURCES;
mutex_lock(&conn->sess->cmdsn_mutex);
cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, be32_to_cpu(cmdsn));
switch (cmdsn_ret) {
case CMDSN_NORMAL_OPERATION:
ret = iscsit_execute_cmd(cmd, 0);
if ((ret >
|