21 #include <linux/string.h>
25 #include <linux/module.h>
27 #include <asm/unaligned.h>
57 static struct idr tiqn_idr;
70 static int iscsit_handle_immediate_data(
struct iscsi_cmd *,
127 pr_err(
"Target IQN exceeds %d bytes\n",
134 pr_err(
"Unable to allocate struct iscsi_tiqn\n");
148 pr_err(
"idr_pre_get() for tiqn_idr failed\n");
157 pr_err(
"idr_get_new() failed for tiqn->tiqn_index\n");
165 pr_debug(
"CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->
tiqn);
171 static void iscsit_wait_for_tiqn(
struct iscsi_tiqn *tiqn)
193 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
194 pr_err(
"iscsit_set_tiqn_shutdown() failed\n");
198 iscsit_wait_for_tiqn(tiqn);
205 pr_debug(
"CORE[0] - Deleted iSCSI Target IQN: %s\n",
222 pr_err(
"np->np_login_tpg() is not NULL!\n");
241 if ((ret != 0) || signal_pending(
current))
267 static struct iscsi_np *iscsit_get_np(
269 int network_transport)
277 spin_lock_bh(&np_lock);
306 if ((ip_match == 1) && (np->
np_port == port) &&
315 spin_unlock_bh(&np_lock);
320 spin_unlock_bh(&np_lock);
328 int network_transport)
337 np = iscsit_get_np(sockaddr, network_transport);
343 pr_err(
"Unable to allocate memory for struct iscsi_np\n");
371 pr_err(
"Unable to create kthread: iscsi_np\n");
385 spin_lock_bh(&np_lock);
387 spin_unlock_bh(&np_lock);
389 pr_debug(
"CORE[0] - Added Network Portal: %s:%hu on %s\n",
408 if (tpg_np->
tpg_np->np_login_tpg != tpg) {
430 static int iscsit_del_np_comm(
struct iscsi_np *np)
456 iscsit_del_np_comm(np);
458 spin_lock_bh(&np_lock);
460 spin_unlock_bh(&np_lock);
462 pr_debug(
"CORE[0] - Removed Network Portal: %s:%hu on %s\n",
470 static int __init iscsi_target_init_module(
void)
476 iscsit_global = kzalloc(
sizeof(
struct iscsit_global),
GFP_KERNEL);
477 if (!iscsit_global) {
478 pr_err(
"Unable to allocate memory for iscsit_global\n");
496 pr_err(
"iscsi_allocate_thread_sets() returned"
497 " unexpected value!\n");
504 if (!lio_cmd_cache) {
505 pr_err(
"Unable to kmem_cache_create() for"
514 pr_err(
"nable to kmem_cache_create() for"
523 pr_err(
"Unable to kmem_cache_create() for"
531 if (!lio_ooo_cache) {
532 pr_err(
"Unable to kmem_cache_create() for"
540 if (!lio_r2t_cache) {
541 pr_err(
"Unable to kmem_cache_create() for"
567 kfree(iscsit_global);
571 static void __exit iscsi_target_cleanup_module(
void)
584 kfree(iscsit_global);
587 static int iscsit_add_reject(
610 pr_err(
"Unable to allocate memory for cmd->buf_ptr\n");
626 return (!fail_conn) ? 0 : -1;
641 pr_err(
"cmd->conn is NULL for ITT: 0x%08x\n",
656 pr_err(
"Unable to allocate memory for cmd->buf_ptr\n");
674 return (!fail_conn) ? 0 : -1;
681 static int iscsit_map_iovec(
689 unsigned int page_off;
700 while (data_length) {
706 data_length -= cur_len;
717 static void iscsit_unmap_iovec(
struct iscsi_cmd *cmd)
728 static void iscsit_ack_from_expstatsn(
struct iscsi_conn *conn,
u32 exp_statsn)
750 static int iscsit_allocate_iovecs(
struct iscsi_cmd *cmd)
758 pr_err(
"Unable to allocate cmd->iov_data\n");
766 static int iscsit_handle_scsi_cmd(
771 int dump_immediate_data = 0, send_check_condition = 0,
payload_length;
777 spin_lock_bh(&conn->
sess->session_stats_lock);
778 conn->
sess->cmd_pdus++;
779 if (conn->
sess->se_sess->se_node_acl) {
780 spin_lock(&conn->
sess->se_sess->se_node_acl->stats_lock);
781 conn->
sess->se_sess->se_node_acl->num_cmds++;
782 spin_unlock(&conn->
sess->se_sess->se_node_acl->stats_lock);
784 spin_unlock_bh(&conn->
sess->session_stats_lock);
793 pr_err(
"ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
794 " not set. Bad iSCSI Initiator.\n");
807 if ((hdr->
cdb[0] == 0x16) || (hdr->
cdb[0] == 0x17)) {
813 pr_err(
"ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
814 " set when Expected Data Transfer Length is 0 for"
815 " CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr->
cdb[0]);
823 pr_err(
"ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
824 " MUST be set if Expected Data Transfer Length is not 0."
825 " Bad iSCSI Initiator\n");
832 pr_err(
"Bidirectional operations not supported!\n");
838 pr_err(
"Illegally set Immediate Bit in iSCSI Initiator"
839 " Scsi Command PDU.\n");
845 pr_err(
"ImmediateData=No but DataSegmentLength=%u,"
853 pr_err(
"Expected Data Transfer Length and Length of"
854 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
855 " bit is not set protocol error\n");
861 pr_err(
"DataSegmentLength: %u is greater than"
869 pr_err(
"DataSegmentLength: %u is greater than"
870 " MaxXmitDataSegmentLength: %u, protocol error.\n",
877 pr_err(
"DataSegmentLength: %u is greater than"
878 " FirstBurstLength: %u, protocol error.\n",
908 pr_debug(
"Unknown iSCSI Task Attribute: 0x%02x, using"
909 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
924 spin_lock_bh(&conn->
sess->ttt_lock);
928 spin_unlock_bh(&conn->
sess->ttt_lock);
955 pr_debug(
"Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
956 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->
itt,
966 pr_debug(
"Responding to non-acl'ed,"
967 " non-existent or non-exported iSCSI LUN:"
970 send_check_condition = 1;
975 if (transport_ret == -
ENOMEM) {
979 }
else if (transport_ret < 0) {
985 send_check_condition = 1;
1003 ret = iscsit_allocate_iovecs(cmd);
1034 if (send_check_condition)
1053 if (send_check_condition) {
1055 dump_immediate_data = 1;
1056 goto after_immediate_data;
1065 dump_immediate_data = 1;
1066 goto after_immediate_data;
1069 immed_ret = iscsit_handle_immediate_data(cmd, buf,
payload_length);
1070 after_immediate_data:
1082 if (dump_immediate_data) {
1118 static u32 iscsit_do_crypto_hash_sg(
1129 unsigned int page_off;
1131 crypto_hash_init(hash);
1137 while (data_length) {
1140 crypto_hash_update(hash, &sg[i], cur_len);
1142 data_length -= cur_len;
1151 crypto_hash_update(hash, &pad_sg, padding);
1153 crypto_hash_final(hash, (
u8 *) &data_crc);
1158 static void iscsit_do_crypto_hash_buf(
1168 crypto_hash_init(hash);
1171 crypto_hash_update(hash, &sg, payload_length);
1175 crypto_hash_update(hash, &sg, padding);
1177 crypto_hash_final(hash, data_crc);
1180 static int iscsit_handle_data_out(
struct iscsi_conn *conn,
unsigned char *buf)
1182 int iov_ret, ooo_cmdsn = 0,
ret;
1183 u8 data_crc_failed = 0;
1184 u32 checksum, iov_count = 0, padding = 0, rx_got = 0;
1190 unsigned long flags;
1195 if (!payload_length) {
1196 pr_err(
"DataOUT payload is ZERO, protocol error.\n");
1202 spin_lock_bh(&conn->
sess->session_stats_lock);
1204 if (conn->
sess->se_sess->se_node_acl) {
1205 spin_lock(&conn->
sess->se_sess->se_node_acl->stats_lock);
1207 spin_unlock(&conn->
sess->se_sess->se_node_acl->stats_lock);
1209 spin_unlock_bh(&conn->
sess->session_stats_lock);
1211 if (payload_length > conn->
conn_ops->MaxXmitDataSegmentLength) {
1212 pr_err(
"DataSegmentLength: %u is greater than"
1213 " MaxXmitDataSegmentLength: %u\n", payload_length,
1214 conn->
conn_ops->MaxXmitDataSegmentLength);
1224 pr_debug(
"Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1225 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1227 payload_length, conn->
cid);
1230 pr_err(
"Command ITT: 0x%08x received DataOUT after"
1231 " last DataOUT received, dumping payload\n",
1237 pr_err(
"Command ITT: 0x%08x received DataOUT for a"
1246 pr_err(
"DataOut Offset: %u, Length %u greater than"
1247 " iSCSI Command EDTL %u, protocol error.\n",
1254 int dump_unsolicited_data = 0;
1256 if (conn->
sess->sess_ops->InitialR2T) {
1257 pr_err(
"Received unexpected unsolicited data"
1258 " while InitialR2T=Yes, protocol error.\n");
1277 dump_unsolicited_data = 1;
1280 if (dump_unsolicited_data) {
1333 iov_count += iov_ret;
1340 pr_debug(
"Receiving %u padding bytes.\n", padding);
1351 iscsit_unmap_iovec(cmd);
1353 if (rx_got != rx_size)
1359 data_crc = iscsit_do_crypto_hash_sg(&conn->
conn_rx_hash, cmd,
1361 payload_length, padding,
1364 if (checksum != data_crc) {
1365 pr_err(
"ITT: 0x%08x, Offset: %u, Length: %u,"
1366 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1367 " does not match computed 0x%08x\n",
1369 hdr->
datasn, checksum, data_crc);
1370 data_crc_failed = 1;
1372 pr_debug(
"Got CRC32C DataDigest 0x%08x for"
1373 " %u bytes of Data Out\n", checksum,
1409 static int iscsit_handle_nop_out(
1413 unsigned char *ping_data =
NULL;
1414 int cmdsn_ret, niov = 0, ret = 0, rx_got, rx_size;
1424 pr_err(
"NOPOUT ITT is reserved, but Immediate Bit is"
1425 " not set, protocol error.\n");
1430 if (payload_length > conn->
conn_ops->MaxXmitDataSegmentLength) {
1431 pr_err(
"NOPOUT Ping Data DataSegmentLength: %u is"
1432 " greater than MaxXmitDataSegmentLength: %u, protocol"
1433 " error.\n", payload_length,
1434 conn->
conn_ops->MaxXmitDataSegmentLength);
1439 pr_debug(
"Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%09x,"
1440 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1454 return iscsit_add_reject(
1471 ping_data = kzalloc(payload_length + 1,
GFP_KERNEL);
1473 pr_err(
"Unable to allocate memory for"
1474 " NOPOUT ping data.\n");
1485 pr_debug(
"Receiving %u additional bytes"
1486 " for padding.\n", padding);
1498 if (rx_got != rx_size) {
1505 ping_data, payload_length,
1509 if (checksum != data_crc) {
1510 pr_err(
"Ping data CRC32C DataDigest"
1511 " 0x%08x does not match computed 0x%08x\n",
1512 checksum, data_crc);
1513 if (!conn->
sess->sess_ops->ErrorRecoveryLevel) {
1514 pr_err(
"Unable to recover from"
1515 " NOPOUT Ping DataCRC failure while in"
1525 " Command CmdSN: 0x%08x due to"
1526 " DataCRC error.\n", hdr->
cmdsn);
1532 " 0x%08x for %u bytes of ping data.\n",
1533 checksum, payload_length);
1544 pr_debug(
"Got %u bytes of NOPOUT ping"
1545 " data.\n", payload_length);
1546 pr_debug(
"Ping Data: \"%s\"\n", ping_data);
1551 pr_err(
"Checking CmdSN for NOPOUT,"
1552 " but cmd is NULL!\n");
1619 static int iscsit_handle_task_mgt_cmd(
1627 int out_of_order_cmdsn = 0;
1633 function = hdr->
flags;
1635 pr_debug(
"Got Task Management Request ITT: 0x%08x, CmdSN:"
1636 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1637 " 0x%08x, CID: %hu\n", hdr->
itt, hdr->
cmdsn,
function,
1643 pr_err(
"RefTaskTag should be set to 0xFFFFFFFF.\n");
1649 pr_err(
"Task Management Request TASK_REASSIGN not"
1650 " issued as immediate command, bad iSCSI Initiator"
1651 "implementation\n");
1668 pr_err(
"Unable to allocate memory for"
1669 " Task Management command!\n");
1712 pr_err(
"Unknown iSCSI TMR Function:"
1713 " 0x%02x\n",
function);
1736 se_tmr = cmd->
se_cmd.se_tmr_req;
1793 pr_err(
"Unknown TMR function: 0x%02x, protocol"
1794 " error.\n",
function);
1811 out_of_order_cmdsn = 1;
1842 static int iscsit_handle_text_cmd(
1846 char *text_ptr, *text_in;
1847 int cmdsn_ret, niov = 0, rx_got, rx_size;
1849 u32 padding = 0, pad_bytes = 0, text_length = 0;
1857 if (payload_length > conn->
conn_ops->MaxXmitDataSegmentLength) {
1858 pr_err(
"Unable to accept text parameter length: %u"
1859 "greater than MaxXmitDataSegmentLength %u.\n",
1860 payload_length, conn->
conn_ops->MaxXmitDataSegmentLength);
1865 pr_debug(
"Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1866 " ExpStatSN: 0x%08x, Length: %u\n", hdr->
itt, hdr->
cmdsn,
1873 pr_err(
"Unable to allocate memory for"
1874 " incoming text parameters\n");
1880 iov[niov++].
iov_len = text_length;
1887 pr_debug(
"Receiving %u additional bytes"
1888 " for padding.\n", padding);
1896 rx_got =
rx_data(conn, &iov[0], niov, rx_size);
1897 if (rx_got != rx_size) {
1904 text_in, text_length,
1905 padding, (
u8 *)&pad_bytes,
1908 if (checksum != data_crc) {
1909 pr_err(
"Text data CRC32C DataDigest"
1910 " 0x%08x does not match computed"
1911 " 0x%08x\n", checksum, data_crc);
1912 if (!conn->
sess->sess_ops->ErrorRecoveryLevel) {
1913 pr_err(
"Unable to recover from"
1914 " Text Data digest failure while in"
1924 " Command CmdSN: 0x%08x due to"
1925 " DataCRC error.\n", hdr->
cmdsn);
1931 " 0x%08x for %u bytes of text data.\n",
1932 checksum, text_length);
1935 text_in[text_length - 1] =
'\0';
1936 pr_debug(
"Successfully read %d bytes of text"
1937 " data.\n", text_length);
1939 if (
strncmp(
"SendTargets", text_in, 11) != 0) {
1940 pr_err(
"Received Text Data that is not"
1941 " SendTargets, cannot continue.\n");
1945 text_ptr =
strchr(text_in,
'=');
1947 pr_err(
"No \"=\" separator found in Text Data,"
1948 " cannot continue.\n");
1952 if (
strncmp(
"=All", text_ptr, 4) != 0) {
1953 pr_err(
"Unable to locate All value for"
1954 " SendTargets key, cannot continue.\n");
2000 pr_debug(
"Received logout request CLOSESESSION on CID: %hu"
2001 " for SID: %u.\n", conn->
cid, conn->
sess->sid);
2015 pr_debug(
"Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2030 pr_debug(
"Received logout request CLOSECONNECTION for CID:"
2039 pr_debug(
"Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2077 pr_debug(
"Received explicit REMOVECONNFORRECOVERY logout for"
2080 if (sess->
sess_ops->ErrorRecoveryLevel != 2) {
2081 pr_err(
"Received Logout Request REMOVECONNFORRECOVERY"
2082 " while ERL!=2.\n");
2089 pr_err(
"Received Logout Request REMOVECONNFORRECOVERY"
2090 " with CID: %hu on CID: %hu, implementation error.\n",
2102 static int iscsit_handle_logout_cmd(
2106 int cmdsn_ret, logout_remove = 0;
2113 reason_code = (hdr->
flags & 0x7f);
2124 pr_debug(
"Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2125 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2130 pr_err(
"Received logout request on connection that"
2131 " is not in logged in state, ignoring request.\n");
2187 return logout_remove;
2190 static int iscsit_handle_snack(
2199 pr_debug(
"Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2200 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2204 if (!conn->
sess->sess_ops->ErrorRecoveryLevel) {
2205 pr_err(
"Initiator sent SNACK request while in"
2206 " ErrorRecoveryLevel=0.\n");
2231 pr_err(
"R-Data SNACK Not Supported.\n");
2235 pr_err(
"Unknown SNACK type 0x%02x, protocol"
2236 " error.\n", hdr->
flags & 0x0f);
2244 static void iscsit_rx_thread_wait_for_tcp(
struct iscsi_conn *conn)
2254 static int iscsit_handle_immediate_data(
2259 int iov_ret, rx_got = 0, rx_size = 0;
2269 iov_count = iov_ret;
2272 padding = ((-
length) & 3);
2287 iscsit_unmap_iovec(cmd);
2289 if (rx_got != rx_size) {
2290 iscsit_rx_thread_wait_for_tcp(conn);
2297 data_crc = iscsit_do_crypto_hash_sg(&conn->
conn_rx_hash, cmd,
2301 if (checksum != data_crc) {
2302 pr_err(
"ImmediateData CRC32C DataDigest 0x%08x"
2303 " does not match computed 0x%08x\n", checksum,
2306 if (!conn->
sess->sess_ops->ErrorRecoveryLevel) {
2307 pr_err(
"Unable to recover from"
2308 " Immediate Data digest failure while"
2321 pr_debug(
"Got CRC32C DataDigest 0x%08x for"
2322 " %u bytes of Immediate Data\n", checksum,
2344 static void iscsit_build_conn_drop_async_message(
struct iscsi_conn *conn)
2381 static int iscsit_send_conn_drop_async_message(
2405 if (conn->
conn_ops->HeaderDigest) {
2410 0,
NULL, (
u8 *)header_digest);
2413 pr_debug(
"Attaching CRC32C HeaderDigest to"
2414 " Async Message 0x%08x\n", *header_digest);
2421 pr_debug(
"Sending Connection Dropped Async Message StatSN:"
2422 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->
stat_sn,
2427 static void iscsit_tx_thread_wait_for_tcp(
struct iscsi_conn *conn)
2437 static int iscsit_send_data_in(
2441 int iov_ret = 0, set_statsn = 0;
2442 u32 iov_count = 0, tx_size = 0;
2453 pr_err(
"iscsit_get_datain_values failed for ITT: 0x%08x\n",
2461 if ((datain.offset + datain.length) > cmd->
se_cmd.data_length) {
2462 pr_err(
"Command ITT: 0x%08x, datain.offset: %u and"
2463 " datain.length: %u exceeds cmd->data_length: %u\n",
2465 cmd->
se_cmd.data_length);
2469 spin_lock_bh(&conn->
sess->session_stats_lock);
2470 conn->
sess->tx_data_octets += datain.length;
2471 if (conn->
sess->se_sess->se_node_acl) {
2472 spin_lock(&conn->
sess->se_sess->se_node_acl->stats_lock);
2473 conn->
sess->se_sess->se_node_acl->read_bytes += datain.length;
2474 spin_unlock(&conn->
sess->se_sess->se_node_acl->stats_lock);
2476 spin_unlock_bh(&conn->
sess->session_stats_lock);
2498 hdr->
flags = datain.flags;
2536 if (conn->
conn_ops->HeaderDigest) {
2541 0,
NULL, (
u8 *)header_digest);
2546 pr_debug(
"Attaching CRC32 HeaderDigest"
2547 " for DataIN PDU 0x%08x\n", *header_digest);
2550 iov_ret = iscsit_map_iovec(cmd, &cmd->
iov_data[1], datain.offset, datain.length);
2554 iov_count += iov_ret;
2555 tx_size += datain.length;
2557 cmd->
padding = ((-datain.length) & 3);
2563 pr_debug(
"Attaching %u padding bytes\n",
2574 pr_debug(
"Attached CRC32C DataDigest %d bytes, crc"
2581 pr_debug(
"Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2582 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2592 iscsit_unmap_iovec(cmd);
2595 iscsit_tx_thread_wait_for_tcp(conn);
2608 static int iscsit_send_logout_response(
2612 int niov = 0, tx_size;
2625 pr_debug(
"iSCSI session logout successful, setting"
2626 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2644 pr_debug(
"iSCSI CID: %hu logout on CID: %hu"
2666 pr_err(
"Unable to locate CID: %hu for"
2667 " REMOVECONNFORRECOVERY Logout Request.\n",
2675 pr_debug(
"iSCSI REMOVECONNFORRECOVERY logout"
2676 " for recovery for CID: %hu on CID: %hu successful.\n",
2681 pr_err(
"Unknown cmd->logout_reason: 0x%02x\n",
2704 if (conn->
conn_ops->HeaderDigest) {
2709 0,
NULL, (
u8 *)header_digest);
2713 pr_debug(
"Attaching CRC32C HeaderDigest to"
2714 " Logout Response 0x%08x\n", *header_digest);
2719 pr_debug(
"Sending Logout Response ITT: 0x%08x StatSN:"
2720 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2730 static int iscsit_send_unsolicited_nopin(
2750 if (conn->
conn_ops->HeaderDigest) {
2755 0,
NULL, (
u8 *)header_digest);
2758 pr_debug(
"Attaching CRC32C HeaderDigest to"
2759 " NopIN 0x%08x\n", *header_digest);
2763 cmd->
iov_misc[0].iov_len = tx_size;
2767 pr_debug(
"Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2772 iscsit_tx_thread_wait_for_tcp(conn);
2777 cmd->
i_state = want_response ?
2784 static int iscsit_send_nopin_response(
2788 int niov = 0, tx_size;
2813 if (conn->
conn_ops->HeaderDigest) {
2818 0,
NULL, (
u8 *)header_digest);
2822 pr_debug(
"Attaching CRC32C HeaderDigest"
2823 " to NopIn 0x%08x\n", *header_digest);
2835 pr_debug(
"Echoing back %u bytes of ping"
2844 " padding bytes.\n", padding);
2855 pr_debug(
"Attached DataDigest for %u"
2856 " bytes of ping data, CRC 0x%08x\n",
2864 pr_debug(
"Sending NOPIN Response ITT: 0x%08x, TTT:"
2871 static int iscsit_send_r2t(
2891 spin_lock_bh(&conn->
sess->ttt_lock);
2895 spin_unlock_bh(&conn->
sess->ttt_lock);
2908 if (conn->
conn_ops->HeaderDigest) {
2913 0,
NULL, (
u8 *)header_digest);
2917 pr_debug(
"Attaching CRC32 HeaderDigest for R2T"
2918 " PDU 0x%08x\n", *header_digest);
2921 pr_debug(
"Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
2922 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
2936 iscsit_tx_thread_wait_for_tcp(conn);
2965 if (conn->
sess->sess_ops->DataSequenceInOrder &&
2970 if (conn->
sess->sess_ops->DataSequenceInOrder) {
2973 if (first_r2t && recovery) {
2974 int new_data_end = offset +
2975 conn->
sess->sess_ops->MaxBurstLength -
2978 if (new_data_end > cmd->
se_cmd.data_length)
2982 conn->
sess->sess_ops->MaxBurstLength -
2985 int new_data_end = offset +
2986 conn->
sess->sess_ops->MaxBurstLength;
2988 if (new_data_end > cmd->
se_cmd.data_length)
3028 static int iscsit_send_status(
3032 u8 iov_count = 0, recovery;
3033 u32 padding = 0, tx_size = 0;
3041 spin_lock_bh(&conn->
sess->session_stats_lock);
3042 conn->
sess->rsp_pdus++;
3043 spin_unlock_bh(&conn->
sess->session_stats_lock);
3073 if (cmd->
se_cmd.sense_buffer &&
3079 padding = -(cmd->
se_cmd.scsi_sense_length) & 3;
3084 tx_size += cmd->
se_cmd.scsi_sense_length;
3088 cmd->
se_cmd.scsi_sense_length, 0, padding);
3090 pr_debug(
"Adding %u bytes of padding to"
3091 " SENSE.\n", padding);
3097 (cmd->
se_cmd.scsi_sense_length + padding),
3104 pr_debug(
"Attaching CRC32 DataDigest for"
3105 " SENSE, %u bytes CRC 0x%08x\n",
3106 (cmd->
se_cmd.scsi_sense_length + padding),
3110 pr_debug(
"Attaching SENSE DATA: %u bytes to iSCSI"
3112 cmd->
se_cmd.scsi_sense_length);
3115 if (conn->
conn_ops->HeaderDigest) {
3120 0,
NULL, (
u8 *)header_digest);
3124 pr_debug(
"Attaching CRC32 HeaderDigest for Response"
3125 " PDU 0x%08x\n", *header_digest);
3131 pr_debug(
"Built %sSCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3132 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3139 static u8 iscsit_convert_tcm_tmr_rsp(
struct se_tmr_req *se_tmr)
3158 static int iscsit_send_task_mgt_rsp(
3170 hdr->
response = iscsit_convert_tcm_tmr_rsp(se_tmr);
3183 if (conn->
conn_ops->HeaderDigest) {
3188 0,
NULL, (
u8 *)header_digest);
3192 pr_debug(
"Attaching CRC32 HeaderDigest for Task"
3193 " Mgmt Response PDU 0x%08x\n", *header_digest);
3199 pr_debug(
"Built Task Management Response ITT: 0x%08x,"
3200 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3206 static bool iscsit_check_inaddr_any(
struct iscsi_np *np)
3230 #define SENDTARGETS_BUF_LIMIT 32768U
3232 static int iscsit_build_sendtargets_response(
struct iscsi_cmd *cmd)
3242 buffer_len =
max(conn->
conn_ops->MaxRecvDataSegmentLength,
3247 pr_err(
"Unable to allocate memory for sendtargets"
3279 bool inaddr_any = iscsit_check_inaddr_any(np);
3284 "[" :
"", (inaddr_any ==
false) ?
3287 "]" :
"", (inaddr_any ==
false) ?
3319 static int iscsit_send_text_rsp(
3325 u32 padding = 0, tx_size = 0;
3326 int text_length, iov_count = 0;
3328 text_length = iscsit_build_sendtargets_response(cmd);
3329 if (text_length < 0)
3332 padding = ((-text_length) & 3);
3335 pr_debug(
"Attaching %u additional bytes for"
3336 " padding.\n", padding);
3362 if (conn->
conn_ops->HeaderDigest) {
3367 0,
NULL, (
u8 *)header_digest);
3371 pr_debug(
"Attaching CRC32 HeaderDigest for"
3372 " Text Response PDU 0x%08x\n", *header_digest);
3377 cmd->
buf_ptr, (text_length + padding),
3384 pr_debug(
"Attaching DataDigest for %u bytes of text"
3385 " data, CRC 0x%08x\n", (text_length + padding),
3392 pr_debug(
"Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3394 text_length, conn->
cid);
3398 static int iscsit_send_reject(
3402 u32 iov_count = 0, tx_size = 0;
3425 if (conn->
conn_ops->HeaderDigest) {
3430 0,
NULL, (
u8 *)header_digest);
3434 pr_debug(
"Attaching CRC32 HeaderDigest for"
3435 " REJECT PDU 0x%08x\n", *header_digest);
3446 pr_debug(
"Attaching CRC32 DataDigest for REJECT"
3453 pr_debug(
"Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3485 static inline void iscsit_thread_check_cpumask(
3514 static int handle_immediate_queue(
struct iscsi_conn *conn)
3529 ret = iscsit_send_r2t(cmd, conn);
3545 ret = iscsit_send_unsolicited_nopin(cmd,
3551 ret = iscsit_send_unsolicited_nopin(cmd,
3557 pr_err(
"Unknown Opcode: 0x%02x ITT:"
3558 " 0x%08x, i_state: %d on CID: %hu\n",
3571 static int handle_response_queue(
struct iscsi_conn *conn)
3586 ret = iscsit_send_data_in(cmd, conn);
3591 goto check_rsp_state;
3592 else if (ret == 1) {
3598 }
else if (ret == 2) {
3605 goto check_rsp_state;
3611 ret = iscsit_send_status(cmd, conn);
3614 ret = iscsit_send_logout_response(cmd, conn);
3617 ret = iscsit_send_conn_drop_async_message(
3621 ret = iscsit_send_nopin_response(cmd, conn);
3624 ret = iscsit_send_reject(cmd, conn);
3627 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3635 ret = iscsit_send_text_rsp(cmd, conn);
3638 pr_err(
"Unknown Opcode: 0x%02x ITT:"
3639 " 0x%08x, i_state: %d on CID: %hu\n",
3648 iscsit_tx_thread_wait_for_tcp(conn);
3649 iscsit_unmap_iovec(cmd);
3652 iscsit_unmap_iovec(cmd);
3656 if (!iscsit_logout_post_handler(cmd, conn))
3678 pr_err(
"Unknown Opcode: 0x%02x ITT:"
3679 " 0x%08x, i_state: %d on CID: %hu\n",
3720 iscsit_thread_check_cpumask(conn,
current, 1);
3730 ret = handle_immediate_queue(conn);
3734 ret = handle_response_queue(conn);
3772 iscsit_thread_check_cpumask(conn,
current, 0);
3782 iscsit_rx_thread_wait_for_tcp(conn);
3791 if (conn->
conn_ops->HeaderDigest) {
3797 iscsit_rx_thread_wait_for_tcp(conn);
3803 0,
NULL, (
u8 *)&checksum);
3805 if (
digest != checksum) {
3806 pr_err(
"HeaderDigest CRC32C failed,"
3807 " received 0x%08x, computed 0x%08x\n",
3814 spin_lock_bh(&conn->
sess->session_stats_lock);
3815 conn->
sess->conn_digest_errors++;
3816 spin_unlock_bh(&conn->
sess->session_stats_lock);
3819 " 0x%08x\n", checksum);
3828 if (conn->
sess->sess_ops->SessionType &&
3831 pr_err(
"Received illegal iSCSI Opcode: 0x%02x"
3832 " while in Discovery Session, rejecting.\n", opcode);
3840 if (iscsit_handle_scsi_cmd(conn, buffer) < 0)
3844 if (iscsit_handle_data_out(conn, buffer) < 0)
3848 if (iscsit_handle_nop_out(conn, buffer) < 0)
3852 if (iscsit_handle_task_mgt_cmd(conn, buffer) < 0)
3856 if (iscsit_handle_text_cmd(conn, buffer) < 0)
3859 case ISCSI_OP_LOGOUT:
3860 ret = iscsit_handle_logout_cmd(conn, buffer);
3869 if (iscsit_handle_snack(conn, buffer) < 0)
3873 pr_err(
"Got unknown iSCSI OpCode: 0x%02x\n",
3875 if (!conn->
sess->sess_ops->ErrorRecoveryLevel) {
3876 pr_err(
"Cannot recover from unknown"
3877 " opcode while ERL=0, closing iSCSI connection"
3882 pr_err(
"Unable to recover from unknown"
3883 " opcode while OFMarker=No, closing iSCSI"
3888 pr_err(
"Unable to recover from unknown"
3889 " opcode, closing iSCSI connection.\n");
3905 static void iscsit_release_commands_from_conn(
struct iscsi_conn *conn)
3929 static void iscsit_stop_timers_for_cmds(
3948 pr_debug(
"Closing iSCSI connection CID %hu on SID:"
3949 " %u\n", conn->
cid, sess->
sid);
3959 iscsit_stop_timers_for_cmds(conn);
3978 iscsit_release_commands_from_conn(conn);
4008 iscsit_build_conn_drop_async_message(conn);
4061 pr_debug(
"Moving to TARG_CONN_STATE_FREE.\n");
4067 pr_debug(
"Decremented iSCSI connection count to %hu from node:"
4074 if ((sess->
sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4120 pr_debug(
"Moving to TARG_SESS_STATE_FREE.\n");
4129 pr_debug(
"Moving to TARG_SESS_STATE_FAILED.\n");
4154 pr_err(
"%d connection(s) still exist for iSCSI session"
4194 if (sess->
sess_ops->ErrorRecoveryLevel == 2)
4200 pr_debug(
"Moving to TARG_SESS_STATE_FREE.\n");
4202 pr_debug(
"Released iSCSI session from node: %s\n",
4208 pr_debug(
"Decremented number of active iSCSI Sessions on"
4211 spin_lock(&sess_idr_lock);
4213 spin_unlock(&sess_idr_lock);
4223 static void iscsit_logout_post_handler_closesession(
4240 static void iscsit_logout_post_handler_samecid(
4253 static void iscsit_logout_post_handler_diffcid(
4265 if (l_conn->
cid == cid) {
4279 pr_debug(
"Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4290 static int iscsit_logout_post_handler(
4302 iscsit_logout_post_handler_closesession(conn);
4313 iscsit_logout_post_handler_samecid(conn);
4320 iscsit_logout_post_handler_diffcid(conn,
4355 pr_debug(
"Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4360 pr_debug(
"Moving to TARG_SESS_STATE_FAILED.\n");
4375 if (conn_count == 0)
4410 int connection_sleep)
4420 if (connection_sleep) {
4423 if (conn_count == 0)
4460 int session_count = 0;
4490 pr_debug(
"Released %d iSCSI Session(s) from Target Portal"
4491 " Group: %hu\n", session_count, tpg->
tpgt);