1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630 |
- /*******************************************************************************
- * This file contains the iSCSI Target specific utility functions.
- *
- * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
- *
- * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
- *
- * 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 <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 "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, gfp_t gfp_mask)
- {
- struct iscsi_cmd *cmd;
- cmd = kmem_cache_zalloc(lio_cmd_cache, gfp_mask);
- if (!cmd) {
- pr_err("Unable to allocate memory for struct iscsi_cmd.\n");
- return NULL;
- }
- cmd->conn = conn;
- INIT_LIST_HEAD(&cmd->i_list);
- INIT_LIST_HEAD(&cmd->datain_list);
- INIT_LIST_HEAD(&cmd->cmd_r2t_list);
- init_completion(&cmd->reject_comp);
- 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;
- }
- /*
- * Called from iscsi_handle_scsi_cmd()
- */
- struct iscsi_cmd *iscsit_allocate_se_cmd(
- struct iscsi_conn *conn,
- u32 data_length,
- int data_direction,
- int iscsi_task_attr)
- {
- struct iscsi_cmd *cmd;
- struct se_cmd *se_cmd;
- int sam_task_attr;
- cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
- if (!cmd)
- return NULL;
- cmd->data_direction = data_direction;
- cmd->data_length = data_length;
- /*
- * Figure out the SAM Task Attribute for the incoming SCSI CDB
- */
- if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
- (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
- sam_task_attr = MSG_SIMPLE_TAG;
- else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
- sam_task_attr = MSG_ORDERED_TAG;
- else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
- sam_task_attr = MSG_HEAD_TAG;
- else if (iscsi_task_attr == ISCSI_ATTR_ACA)
- sam_task_attr = MSG_ACA_TAG;
- else {
- pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
- " MSG_SIMPLE_TAG\n", iscsi_task_attr);
- sam_task_attr = MSG_SIMPLE_TAG;
- }
- se_cmd = &cmd->se_cmd;
- /*
- * Initialize struct se_cmd descriptor from target_core_mod infrastructure
- */
- transport_init_se_cmd(se_cmd, &lio_target_fabric_configfs->tf_ops,
- conn->sess->se_sess, data_length, data_direction,
- sam_task_attr, &cmd->sense_buffer[0]);
- return cmd;
- }
- struct iscsi_cmd *iscsit_allocate_se_cmd_for_tmr(
- struct iscsi_conn *conn,
- u8 function)
- {
- struct iscsi_cmd *cmd;
- struct se_cmd *se_cmd;
- int rc;
- u8 tcm_function;
- cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
- if (!cmd)
- return NULL;
- cmd->data_direction = DMA_NONE;
- cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
- if (!cmd->tmr_req) {
- pr_err("Unable to allocate memory for"
- " Task Management command!\n");
- goto out;
- }
- /*
- * TASK_REASSIGN for ERL=2 / connection stays inside of
- * LIO-Target $FABRIC_MOD
- */
- if (function == ISCSI_TM_FUNC_TASK_REASSIGN)
- return cmd;
- se_cmd = &cmd->se_cmd;
- /*
- * Initialize struct se_cmd descriptor from target_core_mod infrastructure
- */
- transport_init_se_cmd(se_cmd, &lio_target_fabric_configfs->tf_ops,
- conn->sess->se_sess, 0, DMA_NONE,
- MSG_SIMPLE_TAG, &cmd->sense_buffer[0]);
- switch (function) {
- case ISCSI_TM_FUNC_ABORT_TASK:
- tcm_function = TMR_ABORT_TASK;
- break;
- case ISCSI_TM_FUNC_ABORT_TASK_SET:
- tcm_function = TMR_ABORT_TASK_SET;
- break;
- case ISCSI_TM_FUNC_CLEAR_ACA:
- tcm_function = TMR_CLEAR_ACA;
- break;
- case ISCSI_TM_FUNC_CLEAR_TASK_SET:
- tcm_function = TMR_CLEAR_TASK_SET;
- break;
- case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
- tcm_function = TMR_LUN_RESET;
- break;
- case ISCSI_TM_FUNC_TARGET_WARM_RESET:
- tcm_function = TMR_TARGET_WARM_RESET;
- break;
- case ISCSI_TM_FUNC_TARGET_COLD_RESET:
- tcm_function = TMR_TARGET_COLD_RESET;
- break;
- default:
- pr_err("Unknown iSCSI TMR Function:"
- " 0x%02x\n", function);
- goto out;
- }
- rc = core_tmr_alloc_req(se_cmd, cmd->tmr_req, tcm_function, GFP_KERNEL);
- if (rc < 0)
- goto out;
- cmd->tmr_req->se_tmr_req = se_cmd->se_tmr_req;
- return cmd;
- out:
- iscsit_release_cmd(cmd);
- return NULL;
- }
- int iscsit_decide_list_to_build(
- struct iscsi_cmd *cmd,
- u32 immediate_data_length)
- {
- struct iscsi_build_list bl;
- struct iscsi_conn *conn = cmd->conn;
- struct iscsi_session *sess = conn->sess;
- struct iscsi_node_attrib *na;
- if (sess->sess_ops->DataSequenceInOrder &&
- sess->sess_ops->DataPDUInOrder)
- return 0;
- if (cmd->data_direction == DMA_NONE)
- return 0;
- na = iscsit_tpg_get_node_attrib(sess);
- memset(&bl, 0, sizeof(struct iscsi_build_list));
- if (cmd->data_direction == DMA_FROM_DEVICE) {
- bl.data_direction = ISCSI_PDU_READ;
- bl.type = PDULIST_NORMAL;
- if (na->random_datain_pdu_offsets)
- bl.randomize |= RANDOM_DATAIN_PDU_OFFSETS;
- if (na->random_datain_seq_offsets)
- bl.randomize |= RANDOM_DATAIN_SEQ_OFFSETS;
- } else {
- bl.data_direction = ISCSI_PDU_WRITE;
- bl.immediate_data_length = immediate_data_length;
- if (na->random_r2t_offsets)
- bl.randomize |= RANDOM_R2T_OFFSETS;
- if (!cmd->immediate_data && !cmd->unsolicited_data)
- bl.type = PDULIST_NORMAL;
- else if (cmd->immediate_data && !cmd->unsolicited_data)
- bl.type = PDULIST_IMMEDIATE;
- else if (!cmd->immediate_data && cmd->unsolicited_data)
- bl.type = PDULIST_UNSOLICITED;
- else if (cmd->immediate_data && cmd->unsolicited_data)
- bl.type = PDULIST_IMMEDIATE_AND_UNSOLICITED;
- }
- return iscsit_do_build_list(cmd, &bl);
- }
- 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, protocol error.\n", cmdsn,
- sess->max_cmd_sn);
- ret = CMDSN_ERROR_CANNOT_RECOVER;
- } 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,
- u32 cmdsn)
- {
- int ret;
- int cmdsn_ret;
- mutex_lock(&conn->sess->cmdsn_mutex);
- cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, cmdsn);
- switch (cmdsn_ret) {
- case CMDSN_NORMAL_OPERATION:
- ret = iscsit_execute_cmd(cmd, 0);
- if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list))
- iscsit_execute_ooo_cmdsns(conn->sess);
- break;
- case CMDSN_HIGHER_THAN_EXP:
- ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, cmdsn);
- break;
- case CMDSN_LOWER_THAN_EXP:
- cmd->i_state = ISTATE_REMOVE;
- iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
- ret = cmdsn_ret;
- break;
- default:
- ret = cmdsn_ret;
- break;
- }
- mutex_unlock(&conn->sess->cmdsn_mutex);
- return ret;
- }
- int iscsit_check_unsolicited_dataout(struct iscsi_cmd *cmd, unsigned char *buf)
- {
- struct iscsi_conn *conn = cmd->conn;
- struct se_cmd *se_cmd = &cmd->se_cmd;
- struct iscsi_data *hdr = (struct iscsi_data *) buf;
- u32 payload_length = ntoh24(hdr->dlength);
- if (conn->sess->sess_ops->InitialR2T) {
- pr_err("Received unexpected unsolicited data"
- " while InitialR2T=Yes, protocol error.\n");
- transport_send_check_condition_and_sense(se_cmd,
- TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
- return -1;
- }
- if ((cmd->first_burst_len + payload_length) >
- conn->sess->sess_ops->FirstBurstLength) {
- pr_err("Total %u bytes exceeds FirstBurstLength: %u"
- " for this Unsolicited DataOut Burst.\n",
- (cmd->first_burst_len + payload_length),
- conn->sess->sess_ops->FirstBurstLength);
- transport_send_check_condition_and_sense(se_cmd,
- TCM_INCORRECT_AMOUNT_OF_DATA, 0);
- return -1;
- }
- if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))
- return 0;
- if (((cmd->first_burst_len + payload_length) != cmd->data_length) &&
- ((cmd->first_burst_len + payload_length) !=
- conn->sess->sess_ops->FirstBurstLength)) {
- pr_err("Unsolicited non-immediate data received %u"
- " does not equal FirstBurstLength: %u, and does"
- " not equal ExpXferLen %u.\n",
- (cmd->first_burst_len + payload_length),
- conn->sess->sess_ops->FirstBurstLength, cmd->data_length);
- transport_send_check_condition_and_sense(se_cmd,
- TCM_INCORRECT_AMOUNT_OF_DATA, 0);
- return -1;
- }
- return 0;
- }
- struct iscsi_cmd *iscsit_find_cmd_from_itt(
- struct iscsi_conn *conn,
- u32 init_task_tag)
- {
- struct iscsi_cmd *cmd;
- spin_lock_bh(&conn->cmd_lock);
- list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
- if (cmd->init_task_tag == init_task_tag) {
- spin_unlock_bh(&conn->cmd_lock);
- return cmd;
- }
- }
- spin_unlock_bh(&conn->cmd_lock);
- pr_err("Unable to locate ITT: 0x%08x on CID: %hu",
- init_task_tag, conn->cid);
- return NULL;
- }
- struct iscsi_cmd *iscsit_find_cmd_from_itt_or_dump(
- struct iscsi_conn *conn,
- u32 init_task_tag,
- u32 length)
- {
- struct iscsi_cmd *cmd;
- spin_lock_bh(&conn->cmd_lock);
- list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
- if (cmd->init_task_tag == init_task_tag) {
- spin_unlock_bh(&conn->cmd_lock);
- return cmd;
- }
- }
- spin_unlock_bh(&conn->cmd_lock);
- pr_err("Unable to locate ITT: 0x%08x on CID: %hu,"
- " dumping payload\n", init_task_tag, conn->cid);
- if (length)
- iscsit_dump_data_payload(conn, length, 1);
- return NULL;
- }
- struct iscsi_cmd *iscsit_find_cmd_from_ttt(
- struct iscsi_conn *conn,
- u32 targ_xfer_tag)
- {
- struct iscsi_cmd *cmd = NULL;
- spin_lock_bh(&conn->cmd_lock);
- list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
- if (cmd->targ_xfer_tag == targ_xfer_tag) {
- spin_unlock_bh(&conn->cmd_lock);
- return cmd;
- }
- }
- spin_unlock_bh(&conn->cmd_lock);
- pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n",
- targ_xfer_tag, conn->cid);
- return NULL;
- }
- int iscsit_find_cmd_for_recovery(
- struct iscsi_session *sess,
- struct iscsi_cmd **cmd_ptr,
- struct iscsi_conn_recovery **cr_ptr,
- u32 init_task_tag)
- {
- struct iscsi_cmd *cmd = NULL;
- struct iscsi_conn_recovery *cr;
- /*
- * Scan through the inactive connection recovery list's command list.
- * If init_task_tag matches the command is still alligent.
- */
- spin_lock(&sess->cr_i_lock);
- list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
- spin_lock(&cr->conn_recovery_cmd_lock);
- list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_list) {
- if (cmd->init_task_tag == init_task_tag) {
- spin_unlock(&cr->conn_recovery_cmd_lock);
- spin_unlock(&sess->cr_i_lock);
- *cr_ptr = cr;
- *cmd_ptr = cmd;
- return -2;
- }
- }
- spin_unlock(&cr->conn_recovery_cmd_lock);
- }
- spin_unlock(&sess->cr_i_lock);
- /*
- * Scan through the active connection recovery list's command list.
- * If init_task_tag matches the command is ready to be reassigned.
- */
- spin_lock(&sess->cr_a_lock);
- list_for_each_entry(cr, &sess->cr_active_list, cr_list) {
- spin_lock(&cr->conn_recovery_cmd_lock);
- list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_list) {
- if (cmd->init_task_tag == init_task_tag) {
- spin_unlock(&cr->conn_recovery_cmd_lock);
- spin_unlock(&sess->cr_a_lock);
- *cr_ptr = cr;
- *cmd_ptr = cmd;
- return 0;
- }
- }
- spin_unlock(&cr->conn_recovery_cmd_lock);
- }
- spin_unlock(&sess->cr_a_lock);
- return -1;
- }
- void iscsit_add_cmd_to_immediate_queue(
- struct iscsi_cmd *cmd,
- struct iscsi_conn *conn,
- u8 state)
- {
- struct iscsi_queue_req *qr;
- qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
- if (!qr) {
- pr_err("Unable to allocate memory for"
- " struct iscsi_queue_req\n");
- return;
- }
- INIT_LIST_HEAD(&qr->qr_list);
- qr->cmd = cmd;
- qr->state = state;
- spin_lock_bh(&conn->immed_queue_lock);
- list_add_tail(&qr->qr_list, &conn->immed_queue_list);
- atomic_inc(&cmd->immed_queue_count);
- atomic_set(&conn->check_immediate_queue, 1);
- spin_unlock_bh(&conn->immed_queue_lock);
- wake_up(&conn->queues_wq);
- }
- struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsi_conn *conn)
- {
- struct iscsi_queue_req *qr;
- spin_lock_bh(&conn->immed_queue_lock);
- if (list_empty(&conn->immed_queue_list)) {
- spin_unlock_bh(&conn->immed_queue_lock);
- return NULL;
- }
- list_for_each_entry(qr, &conn->immed_queue_list, qr_list)
- break;
- list_del(&qr->qr_list);
- if (qr->cmd)
- atomic_dec(&qr->cmd->immed_queue_count);
- spin_unlock_bh(&conn->immed_queue_lock);
- return qr;
- }
- static void iscsit_remove_cmd_from_immediate_queue(
- struct iscsi_cmd *cmd,
- struct iscsi_conn *conn)
- {
- struct iscsi_queue_req *qr, *qr_tmp;
- spin_lock_bh(&conn->immed_queue_lock);
- if (!atomic_read(&cmd->immed_queue_count)) {
- spin_unlock_bh(&conn->immed_queue_lock);
- return;
- }
- list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
- if (qr->cmd != cmd)
- continue;
- atomic_dec(&qr->cmd->immed_queue_count);
- list_del(&qr->qr_list);
- kmem_cache_free(lio_qr_cache, qr);
- }
- spin_unlock_bh(&conn->immed_queue_lock);
- if (atomic_read(&cmd->immed_queue_count)) {
- pr_err("ITT: 0x%08x immed_queue_count: %d\n",
- cmd->init_task_tag,
- atomic_read(&cmd->immed_queue_count));
- }
- }
- void iscsit_add_cmd_to_response_queue(
- struct iscsi_cmd *cmd,
- struct iscsi_conn *conn,
- u8 state)
- {
- struct iscsi_queue_req *qr;
- qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
- if (!qr) {
- pr_err("Unable to allocate memory for"
- " struct iscsi_queue_req\n");
- return;
- }
- INIT_LIST_HEAD(&qr->qr_list);
- qr->cmd = cmd;
- qr->state = state;
- spin_lock_bh(&conn->response_queue_lock);
- list_add_tail(&qr->qr_list, &conn->response_queue_list);
- atomic_inc(&cmd->response_queue_count);
- spin_unlock_bh(&conn->response_queue_lock);
- wake_up(&conn->queues_wq);
- }
- struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsi_conn *conn)
- {
- struct iscsi_queue_req *qr;
- spin_lock_bh(&conn->response_queue_lock);
- if (list_empty(&conn->response_queue_list)) {
- spin_unlock_bh(&conn->response_queue_lock);
- return NULL;
- }
- list_for_each_entry(qr, &conn->response_queue_list, qr_list)
- break;
- list_del(&qr->qr_list);
- if (qr->cmd)
- atomic_dec(&qr->cmd->response_queue_count);
- spin_unlock_bh(&conn->response_queue_lock);
- return qr;
- }
- static void iscsit_remove_cmd_from_response_queue(
- struct iscsi_cmd *cmd,
- struct iscsi_conn *conn)
- {
- struct iscsi_queue_req *qr, *qr_tmp;
- spin_lock_bh(&conn->response_queue_lock);
- if (!atomic_read(&cmd->response_queue_count)) {
- spin_unlock_bh(&conn->response_queue_lock);
- return;
- }
- list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
- qr_list) {
- if (qr->cmd != cmd)
- continue;
- atomic_dec(&qr->cmd->response_queue_count);
- list_del(&qr->qr_list);
- kmem_cache_free(lio_qr_cache, qr);
- }
- spin_unlock_bh(&conn->response_queue_lock);
- if (atomic_read(&cmd->response_queue_count)) {
- pr_err("ITT: 0x%08x response_queue_count: %d\n",
- cmd->init_task_tag,
- atomic_read(&cmd->response_queue_count));
- }
- }
- bool iscsit_conn_all_queues_empty(struct iscsi_conn *conn)
- {
- bool empty;
- spin_lock_bh(&conn->immed_queue_lock);
- empty = list_empty(&conn->immed_queue_list);
- spin_unlock_bh(&conn->immed_queue_lock);
- if (!empty)
- return empty;
- spin_lock_bh(&conn->response_queue_lock);
- empty = list_empty(&conn->response_queue_list);
- spin_unlock_bh(&conn->response_queue_lock);
- return empty;
- }
- void iscsit_free_queue_reqs_for_conn(struct iscsi_conn *conn)
- {
- struct iscsi_queue_req *qr, *qr_tmp;
- spin_lock_bh(&conn->immed_queue_lock);
- list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
- list_del(&qr->qr_list);
- if (qr->cmd)
- atomic_dec(&qr->cmd->immed_queue_count);
- kmem_cache_free(lio_qr_cache, qr);
- }
- spin_unlock_bh(&conn->immed_queue_lock);
- spin_lock_bh(&conn->response_queue_lock);
- list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
- qr_list) {
- list_del(&qr->qr_list);
- if (qr->cmd)
- atomic_dec(&qr->cmd->response_queue_count);
- kmem_cache_free(lio_qr_cache, qr);
- }
- spin_unlock_bh(&conn->response_queue_lock);
- }
- void iscsit_release_cmd(struct iscsi_cmd *cmd)
- {
- struct iscsi_conn *conn = cmd->conn;
- int i;
- iscsit_free_r2ts_from_list(cmd);
- iscsit_free_all_datain_reqs(cmd);
- kfree(cmd->buf_ptr);
- kfree(cmd->pdu_list);
- kfree(cmd->seq_list);
- kfree(cmd->tmr_req);
- kfree(cmd->iov_data);
- for (i = 0; i < cmd->t_mem_sg_nents; i++)
- __free_page(sg_page(&cmd->t_mem_sg[i]));
- kfree(cmd->t_mem_sg);
- if (conn) {
- iscsit_remove_cmd_from_immediate_queue(cmd, conn);
- iscsit_remove_cmd_from_response_queue(cmd, conn);
- }
- kmem_cache_free(lio_cmd_cache, cmd);
- }
- void iscsit_free_cmd(struct iscsi_cmd *cmd)
- {
- /*
- * Determine if a struct se_cmd is assoicated with
- * this struct iscsi_cmd.
- */
- switch (cmd->iscsi_opcode) {
- case ISCSI_OP_SCSI_CMD:
- case ISCSI_OP_SCSI_TMFUNC:
- transport_generic_free_cmd(&cmd->se_cmd, 1);
- break;
- case ISCSI_OP_REJECT:
- /*
- * Handle special case for REJECT when iscsi_add_reject*() has
- * overwritten the original iscsi_opcode assignment, and the
- * associated cmd->se_cmd needs to be released.
- */
- if (cmd->se_cmd.se_tfo != NULL) {
- transport_generic_free_cmd(&cmd->se_cmd, 1);
- break;
- }
- /* Fall-through */
- default:
- iscsit_release_cmd(cmd);
- break;
- }
- }
- int iscsit_check_session_usage_count(struct iscsi_session *sess)
- {
- spin_lock_bh(&sess->session_usage_lock);
- if (sess->session_usage_count != 0) {
- sess->session_waiting_on_uc = 1;
- spin_unlock_bh(&sess->session_usage_lock);
- if (in_interrupt())
- return 2;
- wait_for_completion(&sess->session_waiting_on_uc_comp);
- return 1;
- }
- spin_unlock_bh(&sess->session_usage_lock);
- return 0;
- }
- void iscsit_dec_session_usage_count(struct iscsi_session *sess)
- {
- spin_lock_bh(&sess->session_usage_lock);
- sess->session_usage_count--;
- if (!sess->session_usage_count && sess->session_waiting_on_uc)
- complete(&sess->session_waiting_on_uc_comp);
- spin_unlock_bh(&sess->session_usage_lock);
- }
- void iscsit_inc_session_usage_count(struct iscsi_session *sess)
- {
- spin_lock_bh(&sess->session_usage_lock);
- sess->session_usage_count++;
- spin_unlock_bh(&sess->session_usage_lock);
- }
- /*
- * Setup conn->if_marker and conn->of_marker values based upon
- * the initial marker-less interval. (see iSCSI v19 A.2)
- */
- int iscsit_set_sync_and_steering_values(struct iscsi_conn *conn)
- {
- int login_ifmarker_count = 0, login_ofmarker_count = 0, next_marker = 0;
- /*
- * IFMarkInt and OFMarkInt are negotiated as 32-bit words.
- */
- u32 IFMarkInt = (conn->conn_ops->IFMarkInt * 4);
- u32 OFMarkInt = (conn->conn_ops->OFMarkInt * 4);
- if (conn->conn_ops->OFMarker) {
- /*
- * Account for the first Login Command received not
- * via iscsi_recv_msg().
- */
- conn->of_marker += ISCSI_HDR_LEN;
- if (conn->of_marker <= OFMarkInt) {
- conn->of_marker = (OFMarkInt - conn->of_marker);
- } else {
- login_ofmarker_count = (conn->of_marker / OFMarkInt);
- next_marker = (OFMarkInt * (login_ofmarker_count + 1)) +
- (login_ofmarker_count * MARKER_SIZE);
- conn->of_marker = (next_marker - conn->of_marker);
- }
- conn->of_marker_offset = 0;
- pr_debug("Setting OFMarker value to %u based on Initial"
- " Markerless Interval.\n", conn->of_marker);
- }
- if (conn->conn_ops->IFMarker) {
- if (conn->if_marker <= IFMarkInt) {
- conn->if_marker = (IFMarkInt - conn->if_marker);
- } else {
- login_ifmarker_count = (conn->if_marker / IFMarkInt);
- next_marker = (IFMarkInt * (login_ifmarker_count + 1)) +
- (login_ifmarker_count * MARKER_SIZE);
- conn->if_marker = (next_marker - conn->if_marker);
- }
- pr_debug("Setting IFMarker value to %u based on Initial"
- " Markerless Interval.\n", conn->if_marker);
- }
- return 0;
- }
- struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
- {
- struct iscsi_conn *conn;
- spin_lock_bh(&sess->conn_lock);
- list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
- if ((conn->cid == cid) &&
- (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
- iscsit_inc_conn_usage_count(conn);
- spin_unlock_bh(&sess->conn_lock);
- return conn;
- }
- }
- spin_unlock_bh(&sess->conn_lock);
- return NULL;
- }
- struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
- {
- struct iscsi_conn *conn;
- spin_lock_bh(&sess->conn_lock);
- list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
- if (conn->cid == cid) {
- iscsit_inc_conn_usage_count(conn);
- spin_lock(&conn->state_lock);
- atomic_set(&conn->connection_wait_rcfr, 1);
- spin_unlock(&conn->state_lock);
- spin_unlock_bh(&sess->conn_lock);
- return conn;
- }
- }
- spin_unlock_bh(&sess->conn_lock);
- return NULL;
- }
- void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
- {
- spin_lock_bh(&conn->conn_usage_lock);
- if (conn->conn_usage_count != 0) {
- conn->conn_waiting_on_uc = 1;
- spin_unlock_bh(&conn->conn_usage_lock);
- wait_for_completion(&conn->conn_waiting_on_uc_comp);
- return;
- }
- spin_unlock_bh(&conn->conn_usage_lock);
- }
- void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
- {
- spin_lock_bh(&conn->conn_usage_lock);
- conn->conn_usage_count--;
- if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
- complete(&conn->conn_waiting_on_uc_comp);
- spin_unlock_bh(&conn->conn_usage_lock);
- }
- void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
- {
- spin_lock_bh(&conn->conn_usage_lock);
- conn->conn_usage_count++;
- spin_unlock_bh(&conn->conn_usage_lock);
- }
- static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
- {
- u8 state;
- struct iscsi_cmd *cmd;
- cmd = iscsit_allocate_cmd(conn, GFP_ATOMIC);
- if (!cmd)
- return -1;
- cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
- state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
- ISTATE_SEND_NOPIN_NO_RESPONSE;
- cmd->init_task_tag = 0xFFFFFFFF;
- spin_lock_bh(&conn->sess->ttt_lock);
- cmd->targ_xfer_tag = (want_response) ? conn->sess->targ_xfer_tag++ :
- 0xFFFFFFFF;
- if (want_response && (cmd->targ_xfer_tag == 0xFFFFFFFF))
- cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
- spin_unlock_bh(&conn->sess->ttt_lock);
- spin_lock_bh(&conn->cmd_lock);
- list_add_tail(&cmd->i_list, &conn->conn_cmd_list);
- spin_unlock_bh(&conn->cmd_lock);
- if (want_response)
- iscsit_start_nopin_response_timer(conn);
- iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
- return 0;
- }
- static void iscsit_handle_nopin_response_timeout(unsigned long data)
- {
- struct iscsi_conn *conn = (struct iscsi_conn *) data;
- iscsit_inc_conn_usage_count(conn);
- spin_lock_bh(&conn->nopin_timer_lock);
- if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
- spin_unlock_bh(&conn->nopin_timer_lock);
- iscsit_dec_conn_usage_count(conn);
- return;
- }
- pr_debug("Did not receive response to NOPIN on CID: %hu on"
- " SID: %u, failing connection.\n", conn->cid,
- conn->sess->sid);
- conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
- spin_unlock_bh(&conn->nopin_timer_lock);
- {
- struct iscsi_portal_group *tpg = conn->sess->tpg;
- struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
- if (tiqn) {
- spin_lock_bh(&tiqn->sess_err_stats.lock);
- strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
- conn->sess->sess_ops->InitiatorName);
- tiqn->sess_err_stats.last_sess_failure_type =
- ISCSI_SESS_ERR_CXN_TIMEOUT;
- tiqn->sess_err_stats.cxn_timeout_errors++;
- conn->sess->conn_timeout_errors++;
- spin_unlock_bh(&tiqn->sess_err_stats.lock);
- }
- }
- iscsit_cause_connection_reinstatement(conn, 0);
- iscsit_dec_conn_usage_count(conn);
- }
- void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
- {
- struct iscsi_session *sess = conn->sess;
- struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
- spin_lock_bh(&conn->nopin_timer_lock);
- if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
- spin_unlock_bh(&conn->nopin_timer_lock);
- return;
- }
- mod_timer(&conn->nopin_response_timer,
- (get_jiffies_64() + na->nopin_response_timeout * HZ));
- spin_unlock_bh(&conn->nopin_timer_lock);
- }
- /*
- * Called with conn->nopin_timer_lock held.
- */
- void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
- {
- struct iscsi_session *sess = conn->sess;
- struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
- spin_lock_bh(&conn->nopin_timer_lock);
- if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
- spin_unlock_bh(&conn->nopin_timer_lock);
- return;
- }
- init_timer(&conn->nopin_response_timer);
- conn->nopin_response_timer.expires =
- (get_jiffies_64() + na->nopin_response_timeout * HZ);
- conn->nopin_response_timer.data = (unsigned long)conn;
- conn->nopin_response_timer.function = iscsit_handle_nopin_response_timeout;
- conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
- conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
- add_timer(&conn->nopin_response_timer);
- pr_debug("Started NOPIN Response Timer on CID: %d to %u"
- " seconds\n", conn->cid, na->nopin_response_timeout);
- spin_unlock_bh(&conn->nopin_timer_lock);
- }
- void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
- {
- spin_lock_bh(&conn->nopin_timer_lock);
- if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
- spin_unlock_bh(&conn->nopin_timer_lock);
- return;
- }
- conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
- spin_unlock_bh(&conn->nopin_timer_lock);
- del_timer_sync(&conn->nopin_response_timer);
- spin_lock_bh(&conn->nopin_timer_lock);
- conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
- spin_unlock_bh(&conn->nopin_timer_lock);
- }
- static void iscsit_handle_nopin_timeout(unsigned long data)
- {
- struct iscsi_conn *conn = (struct iscsi_conn *) data;
- iscsit_inc_conn_usage_count(conn);
- spin_lock_bh(&conn->nopin_timer_lock);
- if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
- spin_unlock_bh(&conn->nopin_timer_lock);
- iscsit_dec_conn_usage_count(conn);
- return;
- }
- conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
- spin_unlock_bh(&conn->nopin_timer_lock);
- iscsit_add_nopin(conn, 1);
- iscsit_dec_conn_usage_count(conn);
- }
- /*
- * Called with conn->nopin_timer_lock held.
- */
- void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
- {
- struct iscsi_session *sess = conn->sess;
- struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
- /*
- * NOPIN timeout is disabled.
- */
- if (!na->nopin_timeout)
- return;
- if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
- return;
- init_timer(&conn->nopin_timer);
- conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
- conn->nopin_timer.data = (unsigned long)conn;
- conn->nopin_timer.function = iscsit_handle_nopin_timeout;
- conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
- conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
- add_timer(&conn->nopin_timer);
- pr_debug("Started NOPIN Timer on CID: %d at %u second"
- " interval\n", conn->cid, na->nopin_timeout);
- }
- void iscsit_start_nopin_timer(struct iscsi_conn *conn)
- {
- struct iscsi_session *sess = conn->sess;
- struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
- /*
- * NOPIN timeout is disabled..
- */
- if (!na->nopin_timeout)
- return;
- spin_lock_bh(&conn->nopin_timer_lock);
- if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) {
- spin_unlock_bh(&conn->nopin_timer_lock);
- return;
- }
- init_timer(&conn->nopin_timer);
- conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
- conn->nopin_timer.data = (unsigned long)conn;
- conn->nopin_timer.function = iscsit_handle_nopin_timeout;
- conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
- conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
- add_timer(&conn->nopin_timer);
- pr_debug("Started NOPIN Timer on CID: %d at %u second"
- " interval\n", conn->cid, na->nopin_timeout);
- spin_unlock_bh(&conn->nopin_timer_lock);
- }
- void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
- {
- spin_lock_bh(&conn->nopin_timer_lock);
- if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
- spin_unlock_bh(&conn->nopin_timer_lock);
- return;
- }
- conn->nopin_timer_flags |= ISCSI_TF_STOP;
- spin_unlock_bh(&conn->nopin_timer_lock);
- del_timer_sync(&conn->nopin_timer);
- spin_lock_bh(&conn->nopin_timer_lock);
- conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
- spin_unlock_bh(&conn->nopin_timer_lock);
- }
- int iscsit_send_tx_data(
- struct iscsi_cmd *cmd,
- struct iscsi_conn *conn,
- int use_misc)
- {
- int tx_sent, tx_size;
- u32 iov_count;
- struct kvec *iov;
- send_data:
- tx_size = cmd->tx_size;
- if (!use_misc) {
- iov = &cmd->iov_data[0];
- iov_count = cmd->iov_data_count;
- } else {
- iov = &cmd->iov_misc[0];
- iov_count = cmd->iov_misc_count;
- }
- tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
- if (tx_size != tx_sent) {
- if (tx_sent == -EAGAIN) {
- pr_err("tx_data() returned -EAGAIN\n");
- goto send_data;
- } else
- return -1;
- }
- cmd->tx_size = 0;
- return 0;
- }
- int iscsit_fe_sendpage_sg(
- struct iscsi_cmd *cmd,
- struct iscsi_conn *conn)
- {
- struct scatterlist *sg = cmd->first_data_sg;
- struct kvec iov;
- u32 tx_hdr_size, data_len;
- u32 offset = cmd->first_data_sg_off;
- int tx_sent, iov_off;
- send_hdr:
- tx_hdr_size = ISCSI_HDR_LEN;
- if (conn->conn_ops->HeaderDigest)
- tx_hdr_size += ISCSI_CRC_LEN;
- iov.iov_base = cmd->pdu;
- iov.iov_len = tx_hdr_size;
- tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
- if (tx_hdr_size != tx_sent) {
- if (tx_sent == -EAGAIN) {
- pr_err("tx_data() returned -EAGAIN\n");
- goto send_hdr;
- }
- return -1;
- }
- data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
- /*
- * Set iov_off used by padding and data digest tx_data() calls below
- * in order to determine proper offset into cmd->iov_data[]
- */
- if (conn->conn_ops->DataDigest) {
- data_len -= ISCSI_CRC_LEN;
- if (cmd->padding)
- iov_off = (cmd->iov_data_count - 2);
- else
- iov_off = (cmd->iov_data_count - 1);
- } else {
- iov_off = (cmd->iov_data_count - 1);
- }
- /*
- * Perform sendpage() for each page in the scatterlist
- */
- while (data_len) {
- u32 space = (sg->length - offset);
- u32 sub_len = min_t(u32, data_len, space);
- send_pg:
- tx_sent = conn->sock->ops->sendpage(conn->sock,
- sg_page(sg), sg->offset + offset, sub_len, 0);
- if (tx_sent != sub_len) {
- if (tx_sent == -EAGAIN) {
- pr_err("tcp_sendpage() returned"
- " -EAGAIN\n");
- goto send_pg;
- }
- pr_err("tcp_sendpage() failure: %d\n",
- tx_sent);
- return -1;
- }
- data_len -= sub_len;
- offset = 0;
- sg = sg_next(sg);
- }
- send_padding:
- if (cmd->padding) {
- struct kvec *iov_p = &cmd->iov_data[iov_off++];
- tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
- if (cmd->padding != tx_sent) {
- if (tx_sent == -EAGAIN) {
- pr_err("tx_data() returned -EAGAIN\n");
- goto send_padding;
- }
- return -1;
- }
- }
- send_datacrc:
- if (conn->conn_ops->DataDigest) {
- struct kvec *iov_d = &cmd->iov_data[iov_off];
- tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
- if (ISCSI_CRC_LEN != tx_sent) {
- if (tx_sent == -EAGAIN) {
- pr_err("tx_data() returned -EAGAIN\n");
- goto send_datacrc;
- }
- return -1;
- }
- }
- return 0;
- }
- /*
- * This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
- * back to the Initiator when an expection condition occurs with the
- * errors set in status_class and status_detail.
- *
- * Parameters: iSCSI Connection, Status Class, Status Detail.
- * Returns: 0 on success, -1 on error.
- */
- int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
- {
- u8 iscsi_hdr[ISCSI_HDR_LEN];
- int err;
- struct kvec iov;
- struct iscsi_login_rsp *hdr;
- iscsit_collect_login_stats(conn, status_class, status_detail);
- memset(&iov, 0, sizeof(struct kvec));
- memset(&iscsi_hdr, 0x0, ISCSI_HDR_LEN);
- hdr = (struct iscsi_login_rsp *)&iscsi_hdr;
- hdr->opcode = ISCSI_OP_LOGIN_RSP;
- hdr->status_class = status_class;
- hdr->status_detail = status_detail;
- hdr->itt = cpu_to_be32(conn->login_itt);
- iov.iov_base = &iscsi_hdr;
- iov.iov_len = ISCSI_HDR_LEN;
- PRINT_BUFF(iscsi_hdr, ISCSI_HDR_LEN);
- err = tx_data(conn, &iov, 1, ISCSI_HDR_LEN);
- if (err != ISCSI_HDR_LEN) {
- pr_err("tx_data returned less than expected\n");
- return -1;
- }
- return 0;
- }
- void iscsit_print_session_params(struct iscsi_session *sess)
- {
- struct iscsi_conn *conn;
- pr_debug("-----------------------------[Session Params for"
- " SID: %u]-----------------------------\n", sess->sid);
- spin_lock_bh(&sess->conn_lock);
- list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
- iscsi_dump_conn_ops(conn->conn_ops);
- spin_unlock_bh(&sess->conn_lock);
- iscsi_dump_sess_ops(sess->sess_ops);
- }
- static int iscsit_do_rx_data(
- struct iscsi_conn *conn,
- struct iscsi_data_count *count)
- {
- int data = count->data_length, rx_loop = 0, total_rx = 0, iov_len;
- struct kvec *iov_p;
- struct msghdr msg;
- if (!conn || !conn->sock || !conn->conn_ops)
- return -1;
- memset(&msg, 0, sizeof(struct msghdr));
- iov_p = count->iov;
- iov_len = count->iov_count;
- while (total_rx < data) {
- rx_loop = kernel_recvmsg(conn->sock, &msg, iov_p, iov_len,
- (data - total_rx), MSG_WAITALL);
- if (rx_loop <= 0) {
- pr_debug("rx_loop: %d total_rx: %d\n",
- rx_loop, total_rx);
- return rx_loop;
- }
- total_rx += rx_loop;
- pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
- rx_loop, total_rx, data);
- }
- return total_rx;
- }
- static int iscsit_do_tx_data(
- struct iscsi_conn *conn,
- struct iscsi_data_count *count)
- {
- int ret, iov_len;
- struct kvec *iov_p;
- struct msghdr msg;
- if (!conn || !conn->sock || !conn->conn_ops)
- return -1;
- if (count->data_length <= 0) {
- pr_err("Data length is: %d\n", count->data_length);
- return -1;
- }
- memset(&msg, 0, sizeof(struct msghdr));
- iov_p = count->iov;
- iov_len = count->iov_count;
- ret = kernel_sendmsg(conn->sock, &msg, iov_p, iov_len,
- count->data_length);
- if (ret != count->data_length) {
- pr_err("Unexpected ret: %d send data %d\n",
- ret, count->data_length);
- return -EPIPE;
- }
- pr_debug("ret: %d, sent data: %d\n", ret, count->data_length);
- return ret;
- }
- int rx_data(
- struct iscsi_conn *conn,
- struct kvec *iov,
- int iov_count,
- int data)
- {
- struct iscsi_data_count c;
- if (!conn || !conn->sock || !conn->conn_ops)
- return -1;
- memset(&c, 0, sizeof(struct iscsi_data_count));
- c.iov = iov;
- c.iov_count = iov_count;
- c.data_length = data;
- c.type = ISCSI_RX_DATA;
- return iscsit_do_rx_data(conn, &c);
- }
- int tx_data(
- struct iscsi_conn *conn,
- struct kvec *iov,
- int iov_count,
- int data)
- {
- struct iscsi_data_count c;
- if (!conn || !conn->sock || !conn->conn_ops)
- return -1;
- memset(&c, 0, sizeof(struct iscsi_data_count));
- c.iov = iov;
- c.iov_count = iov_count;
- c.data_length = data;
- c.type = ISCSI_TX_DATA;
- return iscsit_do_tx_data(conn, &c);
- }
- void iscsit_collect_login_stats(
- struct iscsi_conn *conn,
- u8 status_class,
- u8 status_detail)
- {
- struct iscsi_param *intrname = NULL;
- struct iscsi_tiqn *tiqn;
- struct iscsi_login_stats *ls;
- tiqn = iscsit_snmp_get_tiqn(conn);
- if (!tiqn)
- return;
- ls = &tiqn->login_stats;
- spin_lock(&ls->lock);
- if (!strcmp(conn->login_ip, ls->last_intr_fail_ip_addr) &&
- ((get_jiffies_64() - ls->last_fail_time) < 10)) {
- /* We already have the failure info for this login */
- spin_unlock(&ls->lock);
- return;
- }
- if (status_class == ISCSI_STATUS_CLS_SUCCESS)
- ls->accepts++;
- else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
- ls->redirects++;
- ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
- } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
- (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
- ls->authenticate_fails++;
- ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHENTICATE;
- } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
- (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
- ls->authorize_fails++;
- ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
- } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
- (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
- ls->negotiate_fails++;
- ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
- } else {
- ls->other_fails++;
- ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
- }
- /* Save initiator name, ip address and time, if it is a failed login */
- if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
- if (conn->param_list)
- intrname = iscsi_find_param_from_key(INITIATORNAME,
- conn->param_list);
- strcpy(ls->last_intr_fail_name,
- (intrname ? intrname->value : "Unknown"));
- ls->last_intr_fail_ip_family = conn->sock->sk->sk_family;
- snprintf(ls->last_intr_fail_ip_addr, IPV6_ADDRESS_SPACE,
- "%s", conn->login_ip);
- ls->last_fail_time = get_jiffies_64();
- }
- spin_unlock(&ls->lock);
- }
- struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn)
- {
- struct iscsi_portal_group *tpg;
- if (!conn || !conn->sess)
- return NULL;
- tpg = conn->sess->tpg;
- if (!tpg)
- return NULL;
- if (!tpg->tpg_tiqn)
- return NULL;
- return tpg->tpg_tiqn;
- }
|