1e48354ceSNicholas Bellinger /******************************************************************************* 2e48354ceSNicholas Bellinger * This file contains main functions related to the iSCSI Target Core Driver. 3e48354ceSNicholas Bellinger * 44c76251eSNicholas Bellinger * (c) Copyright 2007-2013 Datera, Inc. 5e48354ceSNicholas Bellinger * 6e48354ceSNicholas Bellinger * Author: Nicholas A. Bellinger <nab@linux-iscsi.org> 7e48354ceSNicholas Bellinger * 8e48354ceSNicholas Bellinger * This program is free software; you can redistribute it and/or modify 9e48354ceSNicholas Bellinger * it under the terms of the GNU General Public License as published by 10e48354ceSNicholas Bellinger * the Free Software Foundation; either version 2 of the License, or 11e48354ceSNicholas Bellinger * (at your option) any later version. 12e48354ceSNicholas Bellinger * 13e48354ceSNicholas Bellinger * This program is distributed in the hope that it will be useful, 14e48354ceSNicholas Bellinger * but WITHOUT ANY WARRANTY; without even the implied warranty of 15e48354ceSNicholas Bellinger * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16e48354ceSNicholas Bellinger * GNU General Public License for more details. 17e48354ceSNicholas Bellinger ******************************************************************************/ 18e48354ceSNicholas Bellinger 1969110e3cSHerbert Xu #include <crypto/hash.h> 20e48354ceSNicholas Bellinger #include <linux/string.h> 21e48354ceSNicholas Bellinger #include <linux/kthread.h> 22e48354ceSNicholas Bellinger #include <linux/completion.h> 23827509e3SPaul Gortmaker #include <linux/module.h> 245538d294SDavid S. Miller #include <linux/vmalloc.h> 2540401530SAl Viro #include <linux/idr.h> 268dcf07beSBart Van Assche #include <linux/delay.h> 273f07c014SIngo Molnar #include <linux/sched/signal.h> 28e48354ceSNicholas Bellinger #include <asm/unaligned.h> 297bfca0cfSSagi Grimberg #include <linux/inet.h> 308dcf07beSBart Van Assche #include <net/ipv6.h> 31ba929992SBart Van Assche #include <scsi/scsi_proto.h> 32e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h> 33d28b1169SAndy Grover #include <scsi/scsi_tcq.h> 34e48354ceSNicholas Bellinger #include <target/target_core_base.h> 35c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h> 36e48354ceSNicholas Bellinger 3767f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h> 38e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h" 39e48354ceSNicholas Bellinger #include "iscsi_target_seq_pdu_list.h" 40e48354ceSNicholas Bellinger #include "iscsi_target_datain_values.h" 41e48354ceSNicholas Bellinger #include "iscsi_target_erl0.h" 42e48354ceSNicholas Bellinger #include "iscsi_target_erl1.h" 43e48354ceSNicholas Bellinger #include "iscsi_target_erl2.h" 44e48354ceSNicholas Bellinger #include "iscsi_target_login.h" 45e48354ceSNicholas Bellinger #include "iscsi_target_tmr.h" 46e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h" 47e48354ceSNicholas Bellinger #include "iscsi_target_util.h" 48e48354ceSNicholas Bellinger #include "iscsi_target.h" 49e48354ceSNicholas Bellinger #include "iscsi_target_device.h" 5067f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_stat.h> 51e48354ceSNicholas Bellinger 52baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h> 53baa4d64bSNicholas Bellinger 54e48354ceSNicholas Bellinger static LIST_HEAD(g_tiqn_list); 55e48354ceSNicholas Bellinger static LIST_HEAD(g_np_list); 56e48354ceSNicholas Bellinger static DEFINE_SPINLOCK(tiqn_lock); 57ee291e63SAndy Grover static DEFINE_MUTEX(np_lock); 58e48354ceSNicholas Bellinger 59e48354ceSNicholas Bellinger static struct idr tiqn_idr; 6031ff0ceeSMatthew Wilcox DEFINE_IDA(sess_ida); 61e48354ceSNicholas Bellinger struct mutex auth_id_lock; 62e48354ceSNicholas Bellinger 63e48354ceSNicholas Bellinger struct iscsit_global *iscsit_global; 64e48354ceSNicholas Bellinger 65e48354ceSNicholas Bellinger struct kmem_cache *lio_qr_cache; 66e48354ceSNicholas Bellinger struct kmem_cache *lio_dr_cache; 67e48354ceSNicholas Bellinger struct kmem_cache *lio_ooo_cache; 68e48354ceSNicholas Bellinger struct kmem_cache *lio_r2t_cache; 69e48354ceSNicholas Bellinger 70e48354ceSNicholas Bellinger static int iscsit_handle_immediate_data(struct iscsi_cmd *, 712ec5a8c1SNicholas Bellinger struct iscsi_scsi_req *, u32); 72e48354ceSNicholas Bellinger 73e48354ceSNicholas Bellinger struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf) 74e48354ceSNicholas Bellinger { 75e48354ceSNicholas Bellinger struct iscsi_tiqn *tiqn = NULL; 76e48354ceSNicholas Bellinger 77e48354ceSNicholas Bellinger spin_lock(&tiqn_lock); 78e48354ceSNicholas Bellinger list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) { 79e48354ceSNicholas Bellinger if (!strcmp(tiqn->tiqn, buf)) { 80e48354ceSNicholas Bellinger 81e48354ceSNicholas Bellinger spin_lock(&tiqn->tiqn_state_lock); 82e48354ceSNicholas Bellinger if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) { 83e48354ceSNicholas Bellinger tiqn->tiqn_access_count++; 84e48354ceSNicholas Bellinger spin_unlock(&tiqn->tiqn_state_lock); 85e48354ceSNicholas Bellinger spin_unlock(&tiqn_lock); 86e48354ceSNicholas Bellinger return tiqn; 87e48354ceSNicholas Bellinger } 88e48354ceSNicholas Bellinger spin_unlock(&tiqn->tiqn_state_lock); 89e48354ceSNicholas Bellinger } 90e48354ceSNicholas Bellinger } 91e48354ceSNicholas Bellinger spin_unlock(&tiqn_lock); 92e48354ceSNicholas Bellinger 93e48354ceSNicholas Bellinger return NULL; 94e48354ceSNicholas Bellinger } 95e48354ceSNicholas Bellinger 96e48354ceSNicholas Bellinger static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn) 97e48354ceSNicholas Bellinger { 98e48354ceSNicholas Bellinger spin_lock(&tiqn->tiqn_state_lock); 99e48354ceSNicholas Bellinger if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) { 100e48354ceSNicholas Bellinger tiqn->tiqn_state = TIQN_STATE_SHUTDOWN; 101e48354ceSNicholas Bellinger spin_unlock(&tiqn->tiqn_state_lock); 102e48354ceSNicholas Bellinger return 0; 103e48354ceSNicholas Bellinger } 104e48354ceSNicholas Bellinger spin_unlock(&tiqn->tiqn_state_lock); 105e48354ceSNicholas Bellinger 106e48354ceSNicholas Bellinger return -1; 107e48354ceSNicholas Bellinger } 108e48354ceSNicholas Bellinger 109e48354ceSNicholas Bellinger void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn) 110e48354ceSNicholas Bellinger { 111e48354ceSNicholas Bellinger spin_lock(&tiqn->tiqn_state_lock); 112e48354ceSNicholas Bellinger tiqn->tiqn_access_count--; 113e48354ceSNicholas Bellinger spin_unlock(&tiqn->tiqn_state_lock); 114e48354ceSNicholas Bellinger } 115e48354ceSNicholas Bellinger 116e48354ceSNicholas Bellinger /* 117e48354ceSNicholas Bellinger * Note that IQN formatting is expected to be done in userspace, and 118e48354ceSNicholas Bellinger * no explict IQN format checks are done here. 119e48354ceSNicholas Bellinger */ 120e48354ceSNicholas Bellinger struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf) 121e48354ceSNicholas Bellinger { 122e48354ceSNicholas Bellinger struct iscsi_tiqn *tiqn = NULL; 123e48354ceSNicholas Bellinger int ret; 124e48354ceSNicholas Bellinger 1258f50c7f5SDan Carpenter if (strlen(buf) >= ISCSI_IQN_LEN) { 126e48354ceSNicholas Bellinger pr_err("Target IQN exceeds %d bytes\n", 127e48354ceSNicholas Bellinger ISCSI_IQN_LEN); 128e48354ceSNicholas Bellinger return ERR_PTR(-EINVAL); 129e48354ceSNicholas Bellinger } 130e48354ceSNicholas Bellinger 1313829f381SMarkus Elfring tiqn = kzalloc(sizeof(*tiqn), GFP_KERNEL); 132c46e22f1SMarkus Elfring if (!tiqn) 133e48354ceSNicholas Bellinger return ERR_PTR(-ENOMEM); 134e48354ceSNicholas Bellinger 135e48354ceSNicholas Bellinger sprintf(tiqn->tiqn, "%s", buf); 136e48354ceSNicholas Bellinger INIT_LIST_HEAD(&tiqn->tiqn_list); 137e48354ceSNicholas Bellinger INIT_LIST_HEAD(&tiqn->tiqn_tpg_list); 138e48354ceSNicholas Bellinger spin_lock_init(&tiqn->tiqn_state_lock); 139e48354ceSNicholas Bellinger spin_lock_init(&tiqn->tiqn_tpg_lock); 140e48354ceSNicholas Bellinger spin_lock_init(&tiqn->sess_err_stats.lock); 141e48354ceSNicholas Bellinger spin_lock_init(&tiqn->login_stats.lock); 142e48354ceSNicholas Bellinger spin_lock_init(&tiqn->logout_stats.lock); 143e48354ceSNicholas Bellinger 144e48354ceSNicholas Bellinger tiqn->tiqn_state = TIQN_STATE_ACTIVE; 145e48354ceSNicholas Bellinger 146c9365bd0STejun Heo idr_preload(GFP_KERNEL); 147e48354ceSNicholas Bellinger spin_lock(&tiqn_lock); 148c9365bd0STejun Heo 149c9365bd0STejun Heo ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT); 150e48354ceSNicholas Bellinger if (ret < 0) { 151c9365bd0STejun Heo pr_err("idr_alloc() failed for tiqn->tiqn_index\n"); 152e48354ceSNicholas Bellinger spin_unlock(&tiqn_lock); 153c9365bd0STejun Heo idr_preload_end(); 154e48354ceSNicholas Bellinger kfree(tiqn); 155e48354ceSNicholas Bellinger return ERR_PTR(ret); 156e48354ceSNicholas Bellinger } 157c9365bd0STejun Heo tiqn->tiqn_index = ret; 158e48354ceSNicholas Bellinger list_add_tail(&tiqn->tiqn_list, &g_tiqn_list); 159c9365bd0STejun Heo 160e48354ceSNicholas Bellinger spin_unlock(&tiqn_lock); 161c9365bd0STejun Heo idr_preload_end(); 162e48354ceSNicholas Bellinger 163e48354ceSNicholas Bellinger pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn); 164e48354ceSNicholas Bellinger 165e48354ceSNicholas Bellinger return tiqn; 166e48354ceSNicholas Bellinger 167e48354ceSNicholas Bellinger } 168e48354ceSNicholas Bellinger 169e48354ceSNicholas Bellinger static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn) 170e48354ceSNicholas Bellinger { 171e48354ceSNicholas Bellinger /* 172e48354ceSNicholas Bellinger * Wait for accesses to said struct iscsi_tiqn to end. 173e48354ceSNicholas Bellinger */ 174e48354ceSNicholas Bellinger spin_lock(&tiqn->tiqn_state_lock); 175e48354ceSNicholas Bellinger while (tiqn->tiqn_access_count != 0) { 176e48354ceSNicholas Bellinger spin_unlock(&tiqn->tiqn_state_lock); 177e48354ceSNicholas Bellinger msleep(10); 178e48354ceSNicholas Bellinger spin_lock(&tiqn->tiqn_state_lock); 179e48354ceSNicholas Bellinger } 180e48354ceSNicholas Bellinger spin_unlock(&tiqn->tiqn_state_lock); 181e48354ceSNicholas Bellinger } 182e48354ceSNicholas Bellinger 183e48354ceSNicholas Bellinger void iscsit_del_tiqn(struct iscsi_tiqn *tiqn) 184e48354ceSNicholas Bellinger { 185e48354ceSNicholas Bellinger /* 186e48354ceSNicholas Bellinger * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN 187e48354ceSNicholas Bellinger * while holding tiqn->tiqn_state_lock. This means that all subsequent 188e48354ceSNicholas Bellinger * attempts to access this struct iscsi_tiqn will fail from both transport 189e48354ceSNicholas Bellinger * fabric and control code paths. 190e48354ceSNicholas Bellinger */ 191e48354ceSNicholas Bellinger if (iscsit_set_tiqn_shutdown(tiqn) < 0) { 192e48354ceSNicholas Bellinger pr_err("iscsit_set_tiqn_shutdown() failed\n"); 193e48354ceSNicholas Bellinger return; 194e48354ceSNicholas Bellinger } 195e48354ceSNicholas Bellinger 196e48354ceSNicholas Bellinger iscsit_wait_for_tiqn(tiqn); 197e48354ceSNicholas Bellinger 198e48354ceSNicholas Bellinger spin_lock(&tiqn_lock); 199e48354ceSNicholas Bellinger list_del(&tiqn->tiqn_list); 200e48354ceSNicholas Bellinger idr_remove(&tiqn_idr, tiqn->tiqn_index); 201e48354ceSNicholas Bellinger spin_unlock(&tiqn_lock); 202e48354ceSNicholas Bellinger 203e48354ceSNicholas Bellinger pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n", 204e48354ceSNicholas Bellinger tiqn->tiqn); 205e48354ceSNicholas Bellinger kfree(tiqn); 206e48354ceSNicholas Bellinger } 207e48354ceSNicholas Bellinger 208e48354ceSNicholas Bellinger int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg) 209e48354ceSNicholas Bellinger { 210e48354ceSNicholas Bellinger int ret; 211e48354ceSNicholas Bellinger /* 212e48354ceSNicholas Bellinger * Determine if the network portal is accepting storage traffic. 213e48354ceSNicholas Bellinger */ 214e48354ceSNicholas Bellinger spin_lock_bh(&np->np_thread_lock); 215e48354ceSNicholas Bellinger if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) { 216e48354ceSNicholas Bellinger spin_unlock_bh(&np->np_thread_lock); 217e48354ceSNicholas Bellinger return -1; 218e48354ceSNicholas Bellinger } 219e48354ceSNicholas Bellinger spin_unlock_bh(&np->np_thread_lock); 220e48354ceSNicholas Bellinger /* 221e48354ceSNicholas Bellinger * Determine if the portal group is accepting storage traffic. 222e48354ceSNicholas Bellinger */ 223e48354ceSNicholas Bellinger spin_lock_bh(&tpg->tpg_state_lock); 224e48354ceSNicholas Bellinger if (tpg->tpg_state != TPG_STATE_ACTIVE) { 225e48354ceSNicholas Bellinger spin_unlock_bh(&tpg->tpg_state_lock); 226e48354ceSNicholas Bellinger return -1; 227e48354ceSNicholas Bellinger } 228e48354ceSNicholas Bellinger spin_unlock_bh(&tpg->tpg_state_lock); 229e48354ceSNicholas Bellinger 230e48354ceSNicholas Bellinger /* 231e48354ceSNicholas Bellinger * Here we serialize access across the TIQN+TPG Tuple. 232e48354ceSNicholas Bellinger */ 233a91eb7d9SNicholas Bellinger ret = down_interruptible(&tpg->np_login_sem); 234ee7619f2SNicholas Bellinger if (ret != 0) 235e48354ceSNicholas Bellinger return -1; 236e48354ceSNicholas Bellinger 237a91eb7d9SNicholas Bellinger spin_lock_bh(&tpg->tpg_state_lock); 238a91eb7d9SNicholas Bellinger if (tpg->tpg_state != TPG_STATE_ACTIVE) { 239a91eb7d9SNicholas Bellinger spin_unlock_bh(&tpg->tpg_state_lock); 240a91eb7d9SNicholas Bellinger up(&tpg->np_login_sem); 241a91eb7d9SNicholas Bellinger return -1; 242a91eb7d9SNicholas Bellinger } 243a91eb7d9SNicholas Bellinger spin_unlock_bh(&tpg->tpg_state_lock); 244e48354ceSNicholas Bellinger 245e48354ceSNicholas Bellinger return 0; 246e48354ceSNicholas Bellinger } 247e48354ceSNicholas Bellinger 248a91eb7d9SNicholas Bellinger void iscsit_login_kref_put(struct kref *kref) 249a91eb7d9SNicholas Bellinger { 250a91eb7d9SNicholas Bellinger struct iscsi_tpg_np *tpg_np = container_of(kref, 251a91eb7d9SNicholas Bellinger struct iscsi_tpg_np, tpg_np_kref); 252a91eb7d9SNicholas Bellinger 253a91eb7d9SNicholas Bellinger complete(&tpg_np->tpg_np_comp); 254a91eb7d9SNicholas Bellinger } 255a91eb7d9SNicholas Bellinger 256a91eb7d9SNicholas Bellinger int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg, 257a91eb7d9SNicholas Bellinger struct iscsi_tpg_np *tpg_np) 258e48354ceSNicholas Bellinger { 259e48354ceSNicholas Bellinger struct iscsi_tiqn *tiqn = tpg->tpg_tiqn; 260e48354ceSNicholas Bellinger 261a91eb7d9SNicholas Bellinger up(&tpg->np_login_sem); 262e48354ceSNicholas Bellinger 263a91eb7d9SNicholas Bellinger if (tpg_np) 264a91eb7d9SNicholas Bellinger kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put); 265e48354ceSNicholas Bellinger 266e48354ceSNicholas Bellinger if (tiqn) 267e48354ceSNicholas Bellinger iscsit_put_tiqn_for_login(tiqn); 268e48354ceSNicholas Bellinger 269e48354ceSNicholas Bellinger return 0; 270e48354ceSNicholas Bellinger } 271e48354ceSNicholas Bellinger 27205b96892SNicholas Bellinger bool iscsit_check_np_match( 27313a3cf08SAndy Grover struct sockaddr_storage *sockaddr, 27405b96892SNicholas Bellinger struct iscsi_np *np, 275e48354ceSNicholas Bellinger int network_transport) 276e48354ceSNicholas Bellinger { 277e48354ceSNicholas Bellinger struct sockaddr_in *sock_in, *sock_in_e; 278e48354ceSNicholas Bellinger struct sockaddr_in6 *sock_in6, *sock_in6_e; 27905b96892SNicholas Bellinger bool ip_match = false; 28069d75574SAndy Grover u16 port, port_e; 281e48354ceSNicholas Bellinger 28205b96892SNicholas Bellinger if (sockaddr->ss_family == AF_INET6) { 28305b96892SNicholas Bellinger sock_in6 = (struct sockaddr_in6 *)sockaddr; 28405b96892SNicholas Bellinger sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr; 28505b96892SNicholas Bellinger 28605b96892SNicholas Bellinger if (!memcmp(&sock_in6->sin6_addr.in6_u, 28705b96892SNicholas Bellinger &sock_in6_e->sin6_addr.in6_u, 28805b96892SNicholas Bellinger sizeof(struct in6_addr))) 28905b96892SNicholas Bellinger ip_match = true; 29005b96892SNicholas Bellinger 29105b96892SNicholas Bellinger port = ntohs(sock_in6->sin6_port); 29269d75574SAndy Grover port_e = ntohs(sock_in6_e->sin6_port); 29305b96892SNicholas Bellinger } else { 29405b96892SNicholas Bellinger sock_in = (struct sockaddr_in *)sockaddr; 29505b96892SNicholas Bellinger sock_in_e = (struct sockaddr_in *)&np->np_sockaddr; 29605b96892SNicholas Bellinger 29705b96892SNicholas Bellinger if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr) 29805b96892SNicholas Bellinger ip_match = true; 29905b96892SNicholas Bellinger 30005b96892SNicholas Bellinger port = ntohs(sock_in->sin_port); 30169d75574SAndy Grover port_e = ntohs(sock_in_e->sin_port); 30205b96892SNicholas Bellinger } 30305b96892SNicholas Bellinger 30469d75574SAndy Grover if (ip_match && (port_e == port) && 30505b96892SNicholas Bellinger (np->np_network_transport == network_transport)) 30605b96892SNicholas Bellinger return true; 30705b96892SNicholas Bellinger 30805b96892SNicholas Bellinger return false; 30905b96892SNicholas Bellinger } 31005b96892SNicholas Bellinger 311ee291e63SAndy Grover /* 312ee291e63SAndy Grover * Called with mutex np_lock held 313ee291e63SAndy Grover */ 31405b96892SNicholas Bellinger static struct iscsi_np *iscsit_get_np( 31513a3cf08SAndy Grover struct sockaddr_storage *sockaddr, 31605b96892SNicholas Bellinger int network_transport) 31705b96892SNicholas Bellinger { 31805b96892SNicholas Bellinger struct iscsi_np *np; 31905b96892SNicholas Bellinger bool match; 32005b96892SNicholas Bellinger 321e48354ceSNicholas Bellinger list_for_each_entry(np, &g_np_list, np_list) { 322ee291e63SAndy Grover spin_lock_bh(&np->np_thread_lock); 323e48354ceSNicholas Bellinger if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) { 324ee291e63SAndy Grover spin_unlock_bh(&np->np_thread_lock); 325e48354ceSNicholas Bellinger continue; 326e48354ceSNicholas Bellinger } 327e48354ceSNicholas Bellinger 32805b96892SNicholas Bellinger match = iscsit_check_np_match(sockaddr, np, network_transport); 3290bcc297eSChristophe Vu-Brugier if (match) { 330e48354ceSNicholas Bellinger /* 331e48354ceSNicholas Bellinger * Increment the np_exports reference count now to 332e48354ceSNicholas Bellinger * prevent iscsit_del_np() below from being called 333e48354ceSNicholas Bellinger * while iscsi_tpg_add_network_portal() is called. 334e48354ceSNicholas Bellinger */ 335e48354ceSNicholas Bellinger np->np_exports++; 336ee291e63SAndy Grover spin_unlock_bh(&np->np_thread_lock); 337e48354ceSNicholas Bellinger return np; 338e48354ceSNicholas Bellinger } 339ee291e63SAndy Grover spin_unlock_bh(&np->np_thread_lock); 340e48354ceSNicholas Bellinger } 341e48354ceSNicholas Bellinger 342e48354ceSNicholas Bellinger return NULL; 343e48354ceSNicholas Bellinger } 344e48354ceSNicholas Bellinger 345e48354ceSNicholas Bellinger struct iscsi_np *iscsit_add_np( 34613a3cf08SAndy Grover struct sockaddr_storage *sockaddr, 347e48354ceSNicholas Bellinger int network_transport) 348e48354ceSNicholas Bellinger { 349e48354ceSNicholas Bellinger struct iscsi_np *np; 350e48354ceSNicholas Bellinger int ret; 351ee291e63SAndy Grover 352ee291e63SAndy Grover mutex_lock(&np_lock); 353ee291e63SAndy Grover 354e48354ceSNicholas Bellinger /* 355e48354ceSNicholas Bellinger * Locate the existing struct iscsi_np if already active.. 356e48354ceSNicholas Bellinger */ 357e48354ceSNicholas Bellinger np = iscsit_get_np(sockaddr, network_transport); 358ee291e63SAndy Grover if (np) { 359ee291e63SAndy Grover mutex_unlock(&np_lock); 360e48354ceSNicholas Bellinger return np; 361ee291e63SAndy Grover } 362e48354ceSNicholas Bellinger 3633829f381SMarkus Elfring np = kzalloc(sizeof(*np), GFP_KERNEL); 364e48354ceSNicholas Bellinger if (!np) { 365ee291e63SAndy Grover mutex_unlock(&np_lock); 366e48354ceSNicholas Bellinger return ERR_PTR(-ENOMEM); 367e48354ceSNicholas Bellinger } 368e48354ceSNicholas Bellinger 369e48354ceSNicholas Bellinger np->np_flags |= NPF_IP_NETWORK; 370e48354ceSNicholas Bellinger np->np_network_transport = network_transport; 371e48354ceSNicholas Bellinger spin_lock_init(&np->np_thread_lock); 372e48354ceSNicholas Bellinger init_completion(&np->np_restart_comp); 373e48354ceSNicholas Bellinger INIT_LIST_HEAD(&np->np_list); 374e48354ceSNicholas Bellinger 375f7c9564aSKees Cook timer_setup(&np->np_login_timer, iscsi_handle_login_thread_timeout, 0); 3768a47aa9dSBart Van Assche 377e48354ceSNicholas Bellinger ret = iscsi_target_setup_login_socket(np, sockaddr); 378e48354ceSNicholas Bellinger if (ret != 0) { 379e48354ceSNicholas Bellinger kfree(np); 380ee291e63SAndy Grover mutex_unlock(&np_lock); 381e48354ceSNicholas Bellinger return ERR_PTR(ret); 382e48354ceSNicholas Bellinger } 383e48354ceSNicholas Bellinger 384e48354ceSNicholas Bellinger np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np"); 385e48354ceSNicholas Bellinger if (IS_ERR(np->np_thread)) { 386e48354ceSNicholas Bellinger pr_err("Unable to create kthread: iscsi_np\n"); 387e48354ceSNicholas Bellinger ret = PTR_ERR(np->np_thread); 388e48354ceSNicholas Bellinger kfree(np); 389ee291e63SAndy Grover mutex_unlock(&np_lock); 390e48354ceSNicholas Bellinger return ERR_PTR(ret); 391e48354ceSNicholas Bellinger } 392e48354ceSNicholas Bellinger /* 393e48354ceSNicholas Bellinger * Increment the np_exports reference count now to prevent 394e48354ceSNicholas Bellinger * iscsit_del_np() below from being run while a new call to 395e48354ceSNicholas Bellinger * iscsi_tpg_add_network_portal() for a matching iscsi_np is 396e48354ceSNicholas Bellinger * active. We don't need to hold np->np_thread_lock at this 397e48354ceSNicholas Bellinger * point because iscsi_np has not been added to g_np_list yet. 398e48354ceSNicholas Bellinger */ 399e48354ceSNicholas Bellinger np->np_exports = 1; 400ee291e63SAndy Grover np->np_thread_state = ISCSI_NP_THREAD_ACTIVE; 401e48354ceSNicholas Bellinger 402e48354ceSNicholas Bellinger list_add_tail(&np->np_list, &g_np_list); 403ee291e63SAndy Grover mutex_unlock(&np_lock); 404e48354ceSNicholas Bellinger 40569d75574SAndy Grover pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n", 40669d75574SAndy Grover &np->np_sockaddr, np->np_transport->name); 407e48354ceSNicholas Bellinger 408e48354ceSNicholas Bellinger return np; 409e48354ceSNicholas Bellinger } 410e48354ceSNicholas Bellinger 411e48354ceSNicholas Bellinger int iscsit_reset_np_thread( 412e48354ceSNicholas Bellinger struct iscsi_np *np, 413e48354ceSNicholas Bellinger struct iscsi_tpg_np *tpg_np, 414a91eb7d9SNicholas Bellinger struct iscsi_portal_group *tpg, 415a91eb7d9SNicholas Bellinger bool shutdown) 416e48354ceSNicholas Bellinger { 417e48354ceSNicholas Bellinger spin_lock_bh(&np->np_thread_lock); 418e48354ceSNicholas Bellinger if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) { 419e48354ceSNicholas Bellinger spin_unlock_bh(&np->np_thread_lock); 420e48354ceSNicholas Bellinger return 0; 421e48354ceSNicholas Bellinger } 422e48354ceSNicholas Bellinger np->np_thread_state = ISCSI_NP_THREAD_RESET; 423978d13d6SNicholas Bellinger atomic_inc(&np->np_reset_count); 424e48354ceSNicholas Bellinger 425e48354ceSNicholas Bellinger if (np->np_thread) { 426e48354ceSNicholas Bellinger spin_unlock_bh(&np->np_thread_lock); 427e48354ceSNicholas Bellinger send_sig(SIGINT, np->np_thread, 1); 428e48354ceSNicholas Bellinger wait_for_completion(&np->np_restart_comp); 429e48354ceSNicholas Bellinger spin_lock_bh(&np->np_thread_lock); 430e48354ceSNicholas Bellinger } 431e48354ceSNicholas Bellinger spin_unlock_bh(&np->np_thread_lock); 432e48354ceSNicholas Bellinger 433a91eb7d9SNicholas Bellinger if (tpg_np && shutdown) { 434a91eb7d9SNicholas Bellinger kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put); 435a91eb7d9SNicholas Bellinger 436a91eb7d9SNicholas Bellinger wait_for_completion(&tpg_np->tpg_np_comp); 437a91eb7d9SNicholas Bellinger } 438a91eb7d9SNicholas Bellinger 439e48354ceSNicholas Bellinger return 0; 440e48354ceSNicholas Bellinger } 441e48354ceSNicholas Bellinger 442baa4d64bSNicholas Bellinger static void iscsit_free_np(struct iscsi_np *np) 443e48354ceSNicholas Bellinger { 444bf6932f4SAl Viro if (np->np_socket) 445e48354ceSNicholas Bellinger sock_release(np->np_socket); 446e48354ceSNicholas Bellinger } 447e48354ceSNicholas Bellinger 448e48354ceSNicholas Bellinger int iscsit_del_np(struct iscsi_np *np) 449e48354ceSNicholas Bellinger { 450e48354ceSNicholas Bellinger spin_lock_bh(&np->np_thread_lock); 451e48354ceSNicholas Bellinger np->np_exports--; 452e48354ceSNicholas Bellinger if (np->np_exports) { 4532363d196SNicholas Bellinger np->enabled = true; 454e48354ceSNicholas Bellinger spin_unlock_bh(&np->np_thread_lock); 455e48354ceSNicholas Bellinger return 0; 456e48354ceSNicholas Bellinger } 457e48354ceSNicholas Bellinger np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN; 458e48354ceSNicholas Bellinger spin_unlock_bh(&np->np_thread_lock); 459e48354ceSNicholas Bellinger 460e48354ceSNicholas Bellinger if (np->np_thread) { 461e48354ceSNicholas Bellinger /* 462e48354ceSNicholas Bellinger * We need to send the signal to wakeup Linux/Net 463e48354ceSNicholas Bellinger * which may be sleeping in sock_accept().. 464e48354ceSNicholas Bellinger */ 465e48354ceSNicholas Bellinger send_sig(SIGINT, np->np_thread, 1); 466e48354ceSNicholas Bellinger kthread_stop(np->np_thread); 467db6077fdSNicholas Bellinger np->np_thread = NULL; 468e48354ceSNicholas Bellinger } 469baa4d64bSNicholas Bellinger 470baa4d64bSNicholas Bellinger np->np_transport->iscsit_free_np(np); 471e48354ceSNicholas Bellinger 472ee291e63SAndy Grover mutex_lock(&np_lock); 473e48354ceSNicholas Bellinger list_del(&np->np_list); 474ee291e63SAndy Grover mutex_unlock(&np_lock); 475e48354ceSNicholas Bellinger 47669d75574SAndy Grover pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n", 47769d75574SAndy Grover &np->np_sockaddr, np->np_transport->name); 478e48354ceSNicholas Bellinger 479baa4d64bSNicholas Bellinger iscsit_put_transport(np->np_transport); 480e48354ceSNicholas Bellinger kfree(np); 481e48354ceSNicholas Bellinger return 0; 482e48354ceSNicholas Bellinger } 483e48354ceSNicholas Bellinger 484e8205ccaSVarun Prakash static void iscsit_get_rx_pdu(struct iscsi_conn *); 4852ec5a8c1SNicholas Bellinger 486d2faaefbSVarun Prakash int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd) 4872ec5a8c1SNicholas Bellinger { 488a4467018SNicholas Bellinger return iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state); 4892ec5a8c1SNicholas Bellinger } 490d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_queue_rsp); 4912ec5a8c1SNicholas Bellinger 492d2faaefbSVarun Prakash void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd) 493131e6abcSNicholas Bellinger { 494131e6abcSNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 495064cdd2dSNicholas Bellinger if (!list_empty(&cmd->i_conn_node) && 496064cdd2dSNicholas Bellinger !(cmd->se_cmd.transport_state & CMD_T_FABRIC_STOP)) 497131e6abcSNicholas Bellinger list_del_init(&cmd->i_conn_node); 498131e6abcSNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 499131e6abcSNicholas Bellinger 5004412a671SBart Van Assche __iscsit_free_cmd(cmd, true); 501131e6abcSNicholas Bellinger } 502d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_aborted_task); 503131e6abcSNicholas Bellinger 5042854bb23SVarun Prakash static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *, 505e1dfb21fSBart Van Assche u32, u32, const void *, void *); 5062854bb23SVarun Prakash static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *); 5072854bb23SVarun Prakash 5082854bb23SVarun Prakash static int 5092854bb23SVarun Prakash iscsit_xmit_nondatain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 5102854bb23SVarun Prakash const void *data_buf, u32 data_buf_len) 5112854bb23SVarun Prakash { 5122854bb23SVarun Prakash struct iscsi_hdr *hdr = (struct iscsi_hdr *)cmd->pdu; 5132854bb23SVarun Prakash struct kvec *iov; 5142854bb23SVarun Prakash u32 niov = 0, tx_size = ISCSI_HDR_LEN; 5152854bb23SVarun Prakash int ret; 5162854bb23SVarun Prakash 5172854bb23SVarun Prakash iov = &cmd->iov_misc[0]; 5182854bb23SVarun Prakash iov[niov].iov_base = cmd->pdu; 5192854bb23SVarun Prakash iov[niov++].iov_len = ISCSI_HDR_LEN; 5202854bb23SVarun Prakash 5212854bb23SVarun Prakash if (conn->conn_ops->HeaderDigest) { 5222854bb23SVarun Prakash u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; 5232854bb23SVarun Prakash 5242854bb23SVarun Prakash iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr, 5252854bb23SVarun Prakash ISCSI_HDR_LEN, 0, NULL, 526e1dfb21fSBart Van Assche header_digest); 5272854bb23SVarun Prakash 5282854bb23SVarun Prakash iov[0].iov_len += ISCSI_CRC_LEN; 5292854bb23SVarun Prakash tx_size += ISCSI_CRC_LEN; 5302854bb23SVarun Prakash pr_debug("Attaching CRC32C HeaderDigest" 5312854bb23SVarun Prakash " to opcode 0x%x 0x%08x\n", 5322854bb23SVarun Prakash hdr->opcode, *header_digest); 5332854bb23SVarun Prakash } 5342854bb23SVarun Prakash 5352854bb23SVarun Prakash if (data_buf_len) { 5362854bb23SVarun Prakash u32 padding = ((-data_buf_len) & 3); 5372854bb23SVarun Prakash 5382854bb23SVarun Prakash iov[niov].iov_base = (void *)data_buf; 5392854bb23SVarun Prakash iov[niov++].iov_len = data_buf_len; 5402854bb23SVarun Prakash tx_size += data_buf_len; 5412854bb23SVarun Prakash 5422854bb23SVarun Prakash if (padding != 0) { 5432854bb23SVarun Prakash iov[niov].iov_base = &cmd->pad_bytes; 5442854bb23SVarun Prakash iov[niov++].iov_len = padding; 5452854bb23SVarun Prakash tx_size += padding; 5462854bb23SVarun Prakash pr_debug("Attaching %u additional" 5472854bb23SVarun Prakash " padding bytes.\n", padding); 5482854bb23SVarun Prakash } 5492854bb23SVarun Prakash 5502854bb23SVarun Prakash if (conn->conn_ops->DataDigest) { 5512854bb23SVarun Prakash iscsit_do_crypto_hash_buf(conn->conn_tx_hash, 5522854bb23SVarun Prakash data_buf, data_buf_len, 553e1dfb21fSBart Van Assche padding, &cmd->pad_bytes, 554e1dfb21fSBart Van Assche &cmd->data_crc); 5552854bb23SVarun Prakash 5562854bb23SVarun Prakash iov[niov].iov_base = &cmd->data_crc; 5572854bb23SVarun Prakash iov[niov++].iov_len = ISCSI_CRC_LEN; 5582854bb23SVarun Prakash tx_size += ISCSI_CRC_LEN; 5592854bb23SVarun Prakash pr_debug("Attached DataDigest for %u" 5602854bb23SVarun Prakash " bytes opcode 0x%x, CRC 0x%08x\n", 5612854bb23SVarun Prakash data_buf_len, hdr->opcode, cmd->data_crc); 5622854bb23SVarun Prakash } 5632854bb23SVarun Prakash } 5642854bb23SVarun Prakash 5652854bb23SVarun Prakash cmd->iov_misc_count = niov; 5662854bb23SVarun Prakash cmd->tx_size = tx_size; 5672854bb23SVarun Prakash 5682854bb23SVarun Prakash ret = iscsit_send_tx_data(cmd, conn, 1); 5692854bb23SVarun Prakash if (ret < 0) { 5702854bb23SVarun Prakash iscsit_tx_thread_wait_for_tcp(conn); 5712854bb23SVarun Prakash return ret; 5722854bb23SVarun Prakash } 5732854bb23SVarun Prakash 5742854bb23SVarun Prakash return 0; 5752854bb23SVarun Prakash } 5762854bb23SVarun Prakash 5772854bb23SVarun Prakash static int iscsit_map_iovec(struct iscsi_cmd *, struct kvec *, u32, u32); 5782854bb23SVarun Prakash static void iscsit_unmap_iovec(struct iscsi_cmd *); 5792854bb23SVarun Prakash static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsi_cmd *, 5802854bb23SVarun Prakash u32, u32, u32, u8 *); 5812854bb23SVarun Prakash static int 5822854bb23SVarun Prakash iscsit_xmit_datain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 5832854bb23SVarun Prakash const struct iscsi_datain *datain) 5842854bb23SVarun Prakash { 5852854bb23SVarun Prakash struct kvec *iov; 5862854bb23SVarun Prakash u32 iov_count = 0, tx_size = 0; 5872854bb23SVarun Prakash int ret, iov_ret; 5882854bb23SVarun Prakash 5892854bb23SVarun Prakash iov = &cmd->iov_data[0]; 5902854bb23SVarun Prakash iov[iov_count].iov_base = cmd->pdu; 5912854bb23SVarun Prakash iov[iov_count++].iov_len = ISCSI_HDR_LEN; 5922854bb23SVarun Prakash tx_size += ISCSI_HDR_LEN; 5932854bb23SVarun Prakash 5942854bb23SVarun Prakash if (conn->conn_ops->HeaderDigest) { 5952854bb23SVarun Prakash u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; 5962854bb23SVarun Prakash 5972854bb23SVarun Prakash iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu, 5982854bb23SVarun Prakash ISCSI_HDR_LEN, 0, NULL, 599e1dfb21fSBart Van Assche header_digest); 6002854bb23SVarun Prakash 6012854bb23SVarun Prakash iov[0].iov_len += ISCSI_CRC_LEN; 6022854bb23SVarun Prakash tx_size += ISCSI_CRC_LEN; 6032854bb23SVarun Prakash 6042854bb23SVarun Prakash pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n", 6052854bb23SVarun Prakash *header_digest); 6062854bb23SVarun Prakash } 6072854bb23SVarun Prakash 6082854bb23SVarun Prakash iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1], 6092854bb23SVarun Prakash datain->offset, datain->length); 6102854bb23SVarun Prakash if (iov_ret < 0) 6112854bb23SVarun Prakash return -1; 6122854bb23SVarun Prakash 6132854bb23SVarun Prakash iov_count += iov_ret; 6142854bb23SVarun Prakash tx_size += datain->length; 6152854bb23SVarun Prakash 6162854bb23SVarun Prakash cmd->padding = ((-datain->length) & 3); 6172854bb23SVarun Prakash if (cmd->padding) { 6182854bb23SVarun Prakash iov[iov_count].iov_base = cmd->pad_bytes; 6192854bb23SVarun Prakash iov[iov_count++].iov_len = cmd->padding; 6202854bb23SVarun Prakash tx_size += cmd->padding; 6212854bb23SVarun Prakash 6222854bb23SVarun Prakash pr_debug("Attaching %u padding bytes\n", cmd->padding); 6232854bb23SVarun Prakash } 6242854bb23SVarun Prakash 6252854bb23SVarun Prakash if (conn->conn_ops->DataDigest) { 6262854bb23SVarun Prakash cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash, 6272854bb23SVarun Prakash cmd, datain->offset, 6282854bb23SVarun Prakash datain->length, 6292854bb23SVarun Prakash cmd->padding, 6302854bb23SVarun Prakash cmd->pad_bytes); 6312854bb23SVarun Prakash 6322854bb23SVarun Prakash iov[iov_count].iov_base = &cmd->data_crc; 6332854bb23SVarun Prakash iov[iov_count++].iov_len = ISCSI_CRC_LEN; 6342854bb23SVarun Prakash tx_size += ISCSI_CRC_LEN; 6352854bb23SVarun Prakash 6362854bb23SVarun Prakash pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n", 6372854bb23SVarun Prakash datain->length + cmd->padding, cmd->data_crc); 6382854bb23SVarun Prakash } 6392854bb23SVarun Prakash 6402854bb23SVarun Prakash cmd->iov_data_count = iov_count; 6412854bb23SVarun Prakash cmd->tx_size = tx_size; 6422854bb23SVarun Prakash 6432854bb23SVarun Prakash ret = iscsit_fe_sendpage_sg(cmd, conn); 6442854bb23SVarun Prakash 6452854bb23SVarun Prakash iscsit_unmap_iovec(cmd); 6462854bb23SVarun Prakash 6472854bb23SVarun Prakash if (ret < 0) { 6482854bb23SVarun Prakash iscsit_tx_thread_wait_for_tcp(conn); 6492854bb23SVarun Prakash return ret; 6502854bb23SVarun Prakash } 6512854bb23SVarun Prakash 6522854bb23SVarun Prakash return 0; 6532854bb23SVarun Prakash } 6542854bb23SVarun Prakash 6552854bb23SVarun Prakash static int iscsit_xmit_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 6562854bb23SVarun Prakash struct iscsi_datain_req *dr, const void *buf, 6572854bb23SVarun Prakash u32 buf_len) 6582854bb23SVarun Prakash { 6592854bb23SVarun Prakash if (dr) 6602854bb23SVarun Prakash return iscsit_xmit_datain_pdu(conn, cmd, buf); 6612854bb23SVarun Prakash else 6622854bb23SVarun Prakash return iscsit_xmit_nondatain_pdu(conn, cmd, buf, buf_len); 6632854bb23SVarun Prakash } 6642854bb23SVarun Prakash 665e70beee7SNicholas Bellinger static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn) 666e70beee7SNicholas Bellinger { 667e70beee7SNicholas Bellinger return TARGET_PROT_NORMAL; 668e70beee7SNicholas Bellinger } 669e70beee7SNicholas Bellinger 670baa4d64bSNicholas Bellinger static struct iscsit_transport iscsi_target_transport = { 671baa4d64bSNicholas Bellinger .name = "iSCSI/TCP", 672baa4d64bSNicholas Bellinger .transport_type = ISCSI_TCP, 673bd027d85SNicholas Bellinger .rdma_shutdown = false, 674baa4d64bSNicholas Bellinger .owner = NULL, 675baa4d64bSNicholas Bellinger .iscsit_setup_np = iscsit_setup_np, 676baa4d64bSNicholas Bellinger .iscsit_accept_np = iscsit_accept_np, 677baa4d64bSNicholas Bellinger .iscsit_free_np = iscsit_free_np, 678baa4d64bSNicholas Bellinger .iscsit_get_login_rx = iscsit_get_login_rx, 679baa4d64bSNicholas Bellinger .iscsit_put_login_tx = iscsit_put_login_tx, 6803e1c81a9SNicholas Bellinger .iscsit_get_dataout = iscsit_build_r2ts_for_cmd, 6812ec5a8c1SNicholas Bellinger .iscsit_immediate_queue = iscsit_immediate_queue, 6822ec5a8c1SNicholas Bellinger .iscsit_response_queue = iscsit_response_queue, 6832ec5a8c1SNicholas Bellinger .iscsit_queue_data_in = iscsit_queue_rsp, 6842ec5a8c1SNicholas Bellinger .iscsit_queue_status = iscsit_queue_rsp, 685131e6abcSNicholas Bellinger .iscsit_aborted_task = iscsit_aborted_task, 6862854bb23SVarun Prakash .iscsit_xmit_pdu = iscsit_xmit_pdu, 687e8205ccaSVarun Prakash .iscsit_get_rx_pdu = iscsit_get_rx_pdu, 688e70beee7SNicholas Bellinger .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops, 689baa4d64bSNicholas Bellinger }; 690baa4d64bSNicholas Bellinger 691e48354ceSNicholas Bellinger static int __init iscsi_target_init_module(void) 692e48354ceSNicholas Bellinger { 69388dcd2daSNicholas Bellinger int ret = 0, size; 694e48354ceSNicholas Bellinger 695e48354ceSNicholas Bellinger pr_debug("iSCSI-Target "ISCSIT_VERSION"\n"); 6963829f381SMarkus Elfring iscsit_global = kzalloc(sizeof(*iscsit_global), GFP_KERNEL); 697c46e22f1SMarkus Elfring if (!iscsit_global) 698e48354ceSNicholas Bellinger return -1; 699c46e22f1SMarkus Elfring 70088dcd2daSNicholas Bellinger spin_lock_init(&iscsit_global->ts_bitmap_lock); 701e48354ceSNicholas Bellinger mutex_init(&auth_id_lock); 702e48354ceSNicholas Bellinger idr_init(&tiqn_idr); 703e48354ceSNicholas Bellinger 7049ac8928eSChristoph Hellwig ret = target_register_template(&iscsi_ops); 7059ac8928eSChristoph Hellwig if (ret) 706e48354ceSNicholas Bellinger goto out; 707e48354ceSNicholas Bellinger 70888dcd2daSNicholas Bellinger size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long); 70988dcd2daSNicholas Bellinger iscsit_global->ts_bitmap = vzalloc(size); 710c46e22f1SMarkus Elfring if (!iscsit_global->ts_bitmap) 711e48354ceSNicholas Bellinger goto configfs_out; 712e48354ceSNicholas Bellinger 713e48354ceSNicholas Bellinger lio_qr_cache = kmem_cache_create("lio_qr_cache", 714e48354ceSNicholas Bellinger sizeof(struct iscsi_queue_req), 715e48354ceSNicholas Bellinger __alignof__(struct iscsi_queue_req), 0, NULL); 716e48354ceSNicholas Bellinger if (!lio_qr_cache) { 717e48354ceSNicholas Bellinger pr_err("nable to kmem_cache_create() for" 718e48354ceSNicholas Bellinger " lio_qr_cache\n"); 71988dcd2daSNicholas Bellinger goto bitmap_out; 720e48354ceSNicholas Bellinger } 721e48354ceSNicholas Bellinger 722e48354ceSNicholas Bellinger lio_dr_cache = kmem_cache_create("lio_dr_cache", 723e48354ceSNicholas Bellinger sizeof(struct iscsi_datain_req), 724e48354ceSNicholas Bellinger __alignof__(struct iscsi_datain_req), 0, NULL); 725e48354ceSNicholas Bellinger if (!lio_dr_cache) { 726e48354ceSNicholas Bellinger pr_err("Unable to kmem_cache_create() for" 727e48354ceSNicholas Bellinger " lio_dr_cache\n"); 728e48354ceSNicholas Bellinger goto qr_out; 729e48354ceSNicholas Bellinger } 730e48354ceSNicholas Bellinger 731e48354ceSNicholas Bellinger lio_ooo_cache = kmem_cache_create("lio_ooo_cache", 732e48354ceSNicholas Bellinger sizeof(struct iscsi_ooo_cmdsn), 733e48354ceSNicholas Bellinger __alignof__(struct iscsi_ooo_cmdsn), 0, NULL); 734e48354ceSNicholas Bellinger if (!lio_ooo_cache) { 735e48354ceSNicholas Bellinger pr_err("Unable to kmem_cache_create() for" 736e48354ceSNicholas Bellinger " lio_ooo_cache\n"); 737e48354ceSNicholas Bellinger goto dr_out; 738e48354ceSNicholas Bellinger } 739e48354ceSNicholas Bellinger 740e48354ceSNicholas Bellinger lio_r2t_cache = kmem_cache_create("lio_r2t_cache", 741e48354ceSNicholas Bellinger sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t), 742e48354ceSNicholas Bellinger 0, NULL); 743e48354ceSNicholas Bellinger if (!lio_r2t_cache) { 744e48354ceSNicholas Bellinger pr_err("Unable to kmem_cache_create() for" 745e48354ceSNicholas Bellinger " lio_r2t_cache\n"); 746e48354ceSNicholas Bellinger goto ooo_out; 747e48354ceSNicholas Bellinger } 748e48354ceSNicholas Bellinger 749baa4d64bSNicholas Bellinger iscsit_register_transport(&iscsi_target_transport); 750baa4d64bSNicholas Bellinger 751e48354ceSNicholas Bellinger if (iscsit_load_discovery_tpg() < 0) 752e48354ceSNicholas Bellinger goto r2t_out; 753e48354ceSNicholas Bellinger 754e48354ceSNicholas Bellinger return ret; 755e48354ceSNicholas Bellinger r2t_out: 7567f2c53bbSLino Sanfilippo iscsit_unregister_transport(&iscsi_target_transport); 757e48354ceSNicholas Bellinger kmem_cache_destroy(lio_r2t_cache); 758e48354ceSNicholas Bellinger ooo_out: 759e48354ceSNicholas Bellinger kmem_cache_destroy(lio_ooo_cache); 760e48354ceSNicholas Bellinger dr_out: 761e48354ceSNicholas Bellinger kmem_cache_destroy(lio_dr_cache); 762e48354ceSNicholas Bellinger qr_out: 763e48354ceSNicholas Bellinger kmem_cache_destroy(lio_qr_cache); 76488dcd2daSNicholas Bellinger bitmap_out: 76588dcd2daSNicholas Bellinger vfree(iscsit_global->ts_bitmap); 766e48354ceSNicholas Bellinger configfs_out: 7679ac8928eSChristoph Hellwig /* XXX: this probably wants it to be it's own unwind step.. */ 7689ac8928eSChristoph Hellwig if (iscsit_global->discovery_tpg) 7699ac8928eSChristoph Hellwig iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1); 7709ac8928eSChristoph Hellwig target_unregister_template(&iscsi_ops); 771e48354ceSNicholas Bellinger out: 772e48354ceSNicholas Bellinger kfree(iscsit_global); 773e48354ceSNicholas Bellinger return -ENOMEM; 774e48354ceSNicholas Bellinger } 775e48354ceSNicholas Bellinger 776e48354ceSNicholas Bellinger static void __exit iscsi_target_cleanup_module(void) 777e48354ceSNicholas Bellinger { 778e48354ceSNicholas Bellinger iscsit_release_discovery_tpg(); 779baa4d64bSNicholas Bellinger iscsit_unregister_transport(&iscsi_target_transport); 780e48354ceSNicholas Bellinger kmem_cache_destroy(lio_qr_cache); 781e48354ceSNicholas Bellinger kmem_cache_destroy(lio_dr_cache); 782e48354ceSNicholas Bellinger kmem_cache_destroy(lio_ooo_cache); 783e48354ceSNicholas Bellinger kmem_cache_destroy(lio_r2t_cache); 784e48354ceSNicholas Bellinger 7859ac8928eSChristoph Hellwig /* 7869ac8928eSChristoph Hellwig * Shutdown discovery sessions and disable discovery TPG 7879ac8928eSChristoph Hellwig */ 7889ac8928eSChristoph Hellwig if (iscsit_global->discovery_tpg) 7899ac8928eSChristoph Hellwig iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1); 790e48354ceSNicholas Bellinger 7919ac8928eSChristoph Hellwig target_unregister_template(&iscsi_ops); 792e48354ceSNicholas Bellinger 79388dcd2daSNicholas Bellinger vfree(iscsit_global->ts_bitmap); 794e48354ceSNicholas Bellinger kfree(iscsit_global); 795e48354ceSNicholas Bellinger } 796e48354ceSNicholas Bellinger 797d2faaefbSVarun Prakash int iscsit_add_reject( 798ba159914SNicholas Bellinger struct iscsi_conn *conn, 799e48354ceSNicholas Bellinger u8 reason, 800ba159914SNicholas Bellinger unsigned char *buf) 801e48354ceSNicholas Bellinger { 802e48354ceSNicholas Bellinger struct iscsi_cmd *cmd; 803e48354ceSNicholas Bellinger 804676687c6SNicholas Bellinger cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); 805e48354ceSNicholas Bellinger if (!cmd) 806e48354ceSNicholas Bellinger return -1; 807e48354ceSNicholas Bellinger 808e48354ceSNicholas Bellinger cmd->iscsi_opcode = ISCSI_OP_REJECT; 809ba159914SNicholas Bellinger cmd->reject_reason = reason; 810e48354ceSNicholas Bellinger 8111c3d5794SThomas Meyer cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL); 812e48354ceSNicholas Bellinger if (!cmd->buf_ptr) { 813e48354ceSNicholas Bellinger pr_err("Unable to allocate memory for cmd->buf_ptr\n"); 814aafc9d15SNicholas Bellinger iscsit_free_cmd(cmd, false); 815e48354ceSNicholas Bellinger return -1; 816e48354ceSNicholas Bellinger } 817e48354ceSNicholas Bellinger 818e48354ceSNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 8192fbb471eSAndy Grover list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 820e48354ceSNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 821e48354ceSNicholas Bellinger 822e48354ceSNicholas Bellinger cmd->i_state = ISTATE_SEND_REJECT; 823e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 824e48354ceSNicholas Bellinger 825e48354ceSNicholas Bellinger return -1; 826e48354ceSNicholas Bellinger } 827d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_add_reject); 828e48354ceSNicholas Bellinger 829ba159914SNicholas Bellinger static int iscsit_add_reject_from_cmd( 830ba159914SNicholas Bellinger struct iscsi_cmd *cmd, 831e48354ceSNicholas Bellinger u8 reason, 832ba159914SNicholas Bellinger bool add_to_conn, 833ba159914SNicholas Bellinger unsigned char *buf) 834e48354ceSNicholas Bellinger { 835e48354ceSNicholas Bellinger struct iscsi_conn *conn; 836cfe2b621SBart Van Assche const bool do_put = cmd->se_cmd.se_tfo != NULL; 837e48354ceSNicholas Bellinger 838e48354ceSNicholas Bellinger if (!cmd->conn) { 839e48354ceSNicholas Bellinger pr_err("cmd->conn is NULL for ITT: 0x%08x\n", 840e48354ceSNicholas Bellinger cmd->init_task_tag); 841e48354ceSNicholas Bellinger return -1; 842e48354ceSNicholas Bellinger } 843e48354ceSNicholas Bellinger conn = cmd->conn; 844e48354ceSNicholas Bellinger 845e48354ceSNicholas Bellinger cmd->iscsi_opcode = ISCSI_OP_REJECT; 846ba159914SNicholas Bellinger cmd->reject_reason = reason; 847e48354ceSNicholas Bellinger 8481c3d5794SThomas Meyer cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL); 849e48354ceSNicholas Bellinger if (!cmd->buf_ptr) { 850e48354ceSNicholas Bellinger pr_err("Unable to allocate memory for cmd->buf_ptr\n"); 851aafc9d15SNicholas Bellinger iscsit_free_cmd(cmd, false); 852e48354ceSNicholas Bellinger return -1; 853e48354ceSNicholas Bellinger } 854e48354ceSNicholas Bellinger 855e48354ceSNicholas Bellinger if (add_to_conn) { 856e48354ceSNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 8572fbb471eSAndy Grover list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 858e48354ceSNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 859e48354ceSNicholas Bellinger } 860e48354ceSNicholas Bellinger 861e48354ceSNicholas Bellinger cmd->i_state = ISTATE_SEND_REJECT; 862e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 8633e1c81a9SNicholas Bellinger /* 8643e1c81a9SNicholas Bellinger * Perform the kref_put now if se_cmd has already been setup by 8653e1c81a9SNicholas Bellinger * scsit_setup_scsi_cmd() 8663e1c81a9SNicholas Bellinger */ 867cfe2b621SBart Van Assche if (do_put) { 8683e1c81a9SNicholas Bellinger pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n"); 869afc16604SBart Van Assche target_put_sess_cmd(&cmd->se_cmd); 8703e1c81a9SNicholas Bellinger } 871e48354ceSNicholas Bellinger return -1; 872e48354ceSNicholas Bellinger } 873ba159914SNicholas Bellinger 874ba159914SNicholas Bellinger static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason, 875ba159914SNicholas Bellinger unsigned char *buf) 876ba159914SNicholas Bellinger { 877ba159914SNicholas Bellinger return iscsit_add_reject_from_cmd(cmd, reason, true, buf); 878ba159914SNicholas Bellinger } 879ba159914SNicholas Bellinger 880ba159914SNicholas Bellinger int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf) 881ba159914SNicholas Bellinger { 882ba159914SNicholas Bellinger return iscsit_add_reject_from_cmd(cmd, reason, false, buf); 883ba159914SNicholas Bellinger } 884d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_reject_cmd); 885e48354ceSNicholas Bellinger 886e48354ceSNicholas Bellinger /* 887e48354ceSNicholas Bellinger * Map some portion of the allocated scatterlist to an iovec, suitable for 888bfb79eacSAndy Grover * kernel sockets to copy data in/out. 889e48354ceSNicholas Bellinger */ 890e48354ceSNicholas Bellinger static int iscsit_map_iovec( 891e48354ceSNicholas Bellinger struct iscsi_cmd *cmd, 892e48354ceSNicholas Bellinger struct kvec *iov, 893e48354ceSNicholas Bellinger u32 data_offset, 894e48354ceSNicholas Bellinger u32 data_length) 895e48354ceSNicholas Bellinger { 896e48354ceSNicholas Bellinger u32 i = 0; 897e48354ceSNicholas Bellinger struct scatterlist *sg; 898e48354ceSNicholas Bellinger unsigned int page_off; 899e48354ceSNicholas Bellinger 900e48354ceSNicholas Bellinger /* 901bfb79eacSAndy Grover * We know each entry in t_data_sg contains a page. 902e48354ceSNicholas Bellinger */ 9032b16509cSImran Haider u32 ent = data_offset / PAGE_SIZE; 9042b16509cSImran Haider 9052b16509cSImran Haider if (ent >= cmd->se_cmd.t_data_nents) { 9062b16509cSImran Haider pr_err("Initial page entry out-of-bounds\n"); 9072b16509cSImran Haider return -1; 9082b16509cSImran Haider } 9092b16509cSImran Haider 9102b16509cSImran Haider sg = &cmd->se_cmd.t_data_sg[ent]; 911e48354ceSNicholas Bellinger page_off = (data_offset % PAGE_SIZE); 912e48354ceSNicholas Bellinger 913e48354ceSNicholas Bellinger cmd->first_data_sg = sg; 914e48354ceSNicholas Bellinger cmd->first_data_sg_off = page_off; 915e48354ceSNicholas Bellinger 916e48354ceSNicholas Bellinger while (data_length) { 917e48354ceSNicholas Bellinger u32 cur_len = min_t(u32, data_length, sg->length - page_off); 918e48354ceSNicholas Bellinger 919e48354ceSNicholas Bellinger iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off; 920e48354ceSNicholas Bellinger iov[i].iov_len = cur_len; 921e48354ceSNicholas Bellinger 922e48354ceSNicholas Bellinger data_length -= cur_len; 923e48354ceSNicholas Bellinger page_off = 0; 924e48354ceSNicholas Bellinger sg = sg_next(sg); 925e48354ceSNicholas Bellinger i++; 926e48354ceSNicholas Bellinger } 927e48354ceSNicholas Bellinger 928e48354ceSNicholas Bellinger cmd->kmapped_nents = i; 929e48354ceSNicholas Bellinger 930e48354ceSNicholas Bellinger return i; 931e48354ceSNicholas Bellinger } 932e48354ceSNicholas Bellinger 933e48354ceSNicholas Bellinger static void iscsit_unmap_iovec(struct iscsi_cmd *cmd) 934e48354ceSNicholas Bellinger { 935e48354ceSNicholas Bellinger u32 i; 936e48354ceSNicholas Bellinger struct scatterlist *sg; 937e48354ceSNicholas Bellinger 938e48354ceSNicholas Bellinger sg = cmd->first_data_sg; 939e48354ceSNicholas Bellinger 940e48354ceSNicholas Bellinger for (i = 0; i < cmd->kmapped_nents; i++) 941e48354ceSNicholas Bellinger kunmap(sg_page(&sg[i])); 942e48354ceSNicholas Bellinger } 943e48354ceSNicholas Bellinger 944e48354ceSNicholas Bellinger static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn) 945e48354ceSNicholas Bellinger { 946f56cbbb4SNicholas Bellinger LIST_HEAD(ack_list); 947f56cbbb4SNicholas Bellinger struct iscsi_cmd *cmd, *cmd_p; 948e48354ceSNicholas Bellinger 949e48354ceSNicholas Bellinger conn->exp_statsn = exp_statsn; 950e48354ceSNicholas Bellinger 9513e1c81a9SNicholas Bellinger if (conn->sess->sess_ops->RDMAExtensions) 9523e1c81a9SNicholas Bellinger return; 9533e1c81a9SNicholas Bellinger 954e48354ceSNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 955f56cbbb4SNicholas Bellinger list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) { 956e48354ceSNicholas Bellinger spin_lock(&cmd->istate_lock); 957e48354ceSNicholas Bellinger if ((cmd->i_state == ISTATE_SENT_STATUS) && 95864c13330SSteve Hodgson iscsi_sna_lt(cmd->stat_sn, exp_statsn)) { 959e48354ceSNicholas Bellinger cmd->i_state = ISTATE_REMOVE; 960e48354ceSNicholas Bellinger spin_unlock(&cmd->istate_lock); 961f56cbbb4SNicholas Bellinger list_move_tail(&cmd->i_conn_node, &ack_list); 962e48354ceSNicholas Bellinger continue; 963e48354ceSNicholas Bellinger } 964e48354ceSNicholas Bellinger spin_unlock(&cmd->istate_lock); 965e48354ceSNicholas Bellinger } 966e48354ceSNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 967f56cbbb4SNicholas Bellinger 968f56cbbb4SNicholas Bellinger list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) { 9695159d763SNicholas Bellinger list_del_init(&cmd->i_conn_node); 970f56cbbb4SNicholas Bellinger iscsit_free_cmd(cmd, false); 971f56cbbb4SNicholas Bellinger } 972e48354ceSNicholas Bellinger } 973e48354ceSNicholas Bellinger 974e48354ceSNicholas Bellinger static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd) 975e48354ceSNicholas Bellinger { 976f80e8ed3SNicholas Bellinger u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE)); 977e48354ceSNicholas Bellinger 978c0427f15SChristoph Hellwig iov_count += ISCSI_IOV_DATA_BUFFER; 979f1725110SMarkus Elfring cmd->iov_data = kcalloc(iov_count, sizeof(*cmd->iov_data), GFP_KERNEL); 980c46e22f1SMarkus Elfring if (!cmd->iov_data) 981e48354ceSNicholas Bellinger return -ENOMEM; 982e48354ceSNicholas Bellinger 983e48354ceSNicholas Bellinger cmd->orig_iov_data_count = iov_count; 984e48354ceSNicholas Bellinger return 0; 985e48354ceSNicholas Bellinger } 986e48354ceSNicholas Bellinger 9873e1c81a9SNicholas Bellinger int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 988e48354ceSNicholas Bellinger unsigned char *buf) 989e48354ceSNicholas Bellinger { 9903e1c81a9SNicholas Bellinger int data_direction, payload_length; 991e48354ceSNicholas Bellinger struct iscsi_scsi_req *hdr; 992d28b1169SAndy Grover int iscsi_task_attr; 993d28b1169SAndy Grover int sam_task_attr; 994e48354ceSNicholas Bellinger 99504f3b31bSNicholas Bellinger atomic_long_inc(&conn->sess->cmd_pdus); 996e48354ceSNicholas Bellinger 997e48354ceSNicholas Bellinger hdr = (struct iscsi_scsi_req *) buf; 998e48354ceSNicholas Bellinger payload_length = ntoh24(hdr->dlength); 999e48354ceSNicholas Bellinger 1000e48354ceSNicholas Bellinger /* FIXME; Add checks for AdditionalHeaderSegment */ 1001e48354ceSNicholas Bellinger 1002e48354ceSNicholas Bellinger if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) && 1003e48354ceSNicholas Bellinger !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) { 1004e48354ceSNicholas Bellinger pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL" 1005e48354ceSNicholas Bellinger " not set. Bad iSCSI Initiator.\n"); 1006ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 1007ba159914SNicholas Bellinger ISCSI_REASON_BOOKMARK_INVALID, buf); 1008e48354ceSNicholas Bellinger } 1009e48354ceSNicholas Bellinger 1010e48354ceSNicholas Bellinger if (((hdr->flags & ISCSI_FLAG_CMD_READ) || 1011e48354ceSNicholas Bellinger (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) { 1012e48354ceSNicholas Bellinger /* 10134454b66cSNicholas Bellinger * From RFC-3720 Section 10.3.1: 10144454b66cSNicholas Bellinger * 10154454b66cSNicholas Bellinger * "Either or both of R and W MAY be 1 when either the 10164454b66cSNicholas Bellinger * Expected Data Transfer Length and/or Bidirectional Read 10174454b66cSNicholas Bellinger * Expected Data Transfer Length are 0" 10184454b66cSNicholas Bellinger * 10194454b66cSNicholas Bellinger * For this case, go ahead and clear the unnecssary bits 10204454b66cSNicholas Bellinger * to avoid any confusion with ->data_direction. 1021e48354ceSNicholas Bellinger */ 1022e48354ceSNicholas Bellinger hdr->flags &= ~ISCSI_FLAG_CMD_READ; 1023e48354ceSNicholas Bellinger hdr->flags &= ~ISCSI_FLAG_CMD_WRITE; 1024e48354ceSNicholas Bellinger 10254454b66cSNicholas Bellinger pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE" 1026e48354ceSNicholas Bellinger " set when Expected Data Transfer Length is 0 for" 10274454b66cSNicholas Bellinger " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]); 1028e48354ceSNicholas Bellinger } 1029e48354ceSNicholas Bellinger 1030e48354ceSNicholas Bellinger if (!(hdr->flags & ISCSI_FLAG_CMD_READ) && 1031e48354ceSNicholas Bellinger !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) { 1032e48354ceSNicholas Bellinger pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE" 1033e48354ceSNicholas Bellinger " MUST be set if Expected Data Transfer Length is not 0." 1034e48354ceSNicholas Bellinger " Bad iSCSI Initiator\n"); 1035ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 1036ba159914SNicholas Bellinger ISCSI_REASON_BOOKMARK_INVALID, buf); 1037e48354ceSNicholas Bellinger } 1038e48354ceSNicholas Bellinger 1039e48354ceSNicholas Bellinger if ((hdr->flags & ISCSI_FLAG_CMD_READ) && 1040e48354ceSNicholas Bellinger (hdr->flags & ISCSI_FLAG_CMD_WRITE)) { 1041e48354ceSNicholas Bellinger pr_err("Bidirectional operations not supported!\n"); 1042ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 1043ba159914SNicholas Bellinger ISCSI_REASON_BOOKMARK_INVALID, buf); 1044e48354ceSNicholas Bellinger } 1045e48354ceSNicholas Bellinger 1046e48354ceSNicholas Bellinger if (hdr->opcode & ISCSI_OP_IMMEDIATE) { 1047e48354ceSNicholas Bellinger pr_err("Illegally set Immediate Bit in iSCSI Initiator" 1048e48354ceSNicholas Bellinger " Scsi Command PDU.\n"); 1049ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 1050ba159914SNicholas Bellinger ISCSI_REASON_BOOKMARK_INVALID, buf); 1051e48354ceSNicholas Bellinger } 1052e48354ceSNicholas Bellinger 1053e48354ceSNicholas Bellinger if (payload_length && !conn->sess->sess_ops->ImmediateData) { 1054e48354ceSNicholas Bellinger pr_err("ImmediateData=No but DataSegmentLength=%u," 1055e48354ceSNicholas Bellinger " protocol error.\n", payload_length); 1056ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 1057ba159914SNicholas Bellinger ISCSI_REASON_PROTOCOL_ERROR, buf); 1058e48354ceSNicholas Bellinger } 1059e48354ceSNicholas Bellinger 106050e5c87dSChristoph Hellwig if ((be32_to_cpu(hdr->data_length) == payload_length) && 1061e48354ceSNicholas Bellinger (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) { 1062e48354ceSNicholas Bellinger pr_err("Expected Data Transfer Length and Length of" 1063e48354ceSNicholas Bellinger " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL" 1064e48354ceSNicholas Bellinger " bit is not set protocol error\n"); 1065ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 1066ba159914SNicholas Bellinger ISCSI_REASON_PROTOCOL_ERROR, buf); 1067e48354ceSNicholas Bellinger } 1068e48354ceSNicholas Bellinger 106950e5c87dSChristoph Hellwig if (payload_length > be32_to_cpu(hdr->data_length)) { 1070e48354ceSNicholas Bellinger pr_err("DataSegmentLength: %u is greater than" 1071e48354ceSNicholas Bellinger " EDTL: %u, protocol error.\n", payload_length, 1072e48354ceSNicholas Bellinger hdr->data_length); 1073ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 1074ba159914SNicholas Bellinger ISCSI_REASON_PROTOCOL_ERROR, buf); 1075e48354ceSNicholas Bellinger } 1076e48354ceSNicholas Bellinger 107721f5aa7eSNicholas Bellinger if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { 1078e48354ceSNicholas Bellinger pr_err("DataSegmentLength: %u is greater than" 107921f5aa7eSNicholas Bellinger " MaxXmitDataSegmentLength: %u, protocol error.\n", 108021f5aa7eSNicholas Bellinger payload_length, conn->conn_ops->MaxXmitDataSegmentLength); 1081ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 1082ba159914SNicholas Bellinger ISCSI_REASON_PROTOCOL_ERROR, buf); 1083e48354ceSNicholas Bellinger } 1084e48354ceSNicholas Bellinger 1085e48354ceSNicholas Bellinger if (payload_length > conn->sess->sess_ops->FirstBurstLength) { 1086e48354ceSNicholas Bellinger pr_err("DataSegmentLength: %u is greater than" 1087e48354ceSNicholas Bellinger " FirstBurstLength: %u, protocol error.\n", 1088e48354ceSNicholas Bellinger payload_length, conn->sess->sess_ops->FirstBurstLength); 1089ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 1090ba159914SNicholas Bellinger ISCSI_REASON_BOOKMARK_INVALID, buf); 1091e48354ceSNicholas Bellinger } 1092e48354ceSNicholas Bellinger 1093e48354ceSNicholas Bellinger data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE : 1094e48354ceSNicholas Bellinger (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE : 1095e48354ceSNicholas Bellinger DMA_NONE; 1096e48354ceSNicholas Bellinger 1097d28b1169SAndy Grover cmd->data_direction = data_direction; 1098d28b1169SAndy Grover iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK; 1099d28b1169SAndy Grover /* 1100d28b1169SAndy Grover * Figure out the SAM Task Attribute for the incoming SCSI CDB 1101d28b1169SAndy Grover */ 1102d28b1169SAndy Grover if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) || 1103d28b1169SAndy Grover (iscsi_task_attr == ISCSI_ATTR_SIMPLE)) 110468d81f40SChristoph Hellwig sam_task_attr = TCM_SIMPLE_TAG; 1105d28b1169SAndy Grover else if (iscsi_task_attr == ISCSI_ATTR_ORDERED) 110668d81f40SChristoph Hellwig sam_task_attr = TCM_ORDERED_TAG; 1107d28b1169SAndy Grover else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE) 110868d81f40SChristoph Hellwig sam_task_attr = TCM_HEAD_TAG; 1109d28b1169SAndy Grover else if (iscsi_task_attr == ISCSI_ATTR_ACA) 111068d81f40SChristoph Hellwig sam_task_attr = TCM_ACA_TAG; 1111d28b1169SAndy Grover else { 1112d28b1169SAndy Grover pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using" 111368d81f40SChristoph Hellwig " TCM_SIMPLE_TAG\n", iscsi_task_attr); 111468d81f40SChristoph Hellwig sam_task_attr = TCM_SIMPLE_TAG; 1115d28b1169SAndy Grover } 1116d28b1169SAndy Grover 1117e48354ceSNicholas Bellinger cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD; 1118e48354ceSNicholas Bellinger cmd->i_state = ISTATE_NEW_CMD; 1119e48354ceSNicholas Bellinger cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); 1120e48354ceSNicholas Bellinger cmd->immediate_data = (payload_length) ? 1 : 0; 1121e48354ceSNicholas Bellinger cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) && 1122e48354ceSNicholas Bellinger (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0); 1123e48354ceSNicholas Bellinger if (cmd->unsolicited_data) 1124e48354ceSNicholas Bellinger cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA; 1125e48354ceSNicholas Bellinger 1126e48354ceSNicholas Bellinger conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt; 11279547308bSAlexei Potashnik if (hdr->flags & ISCSI_FLAG_CMD_READ) 1128c1e34b64SSagi Grimberg cmd->targ_xfer_tag = session_get_next_ttt(conn->sess); 11299547308bSAlexei Potashnik else 1130e48354ceSNicholas Bellinger cmd->targ_xfer_tag = 0xFFFFFFFF; 113150e5c87dSChristoph Hellwig cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); 113250e5c87dSChristoph Hellwig cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); 1133e48354ceSNicholas Bellinger cmd->first_burst_len = payload_length; 1134e48354ceSNicholas Bellinger 11353e1c81a9SNicholas Bellinger if (!conn->sess->sess_ops->RDMAExtensions && 11363e1c81a9SNicholas Bellinger cmd->data_direction == DMA_FROM_DEVICE) { 1137e48354ceSNicholas Bellinger struct iscsi_datain_req *dr; 1138e48354ceSNicholas Bellinger 1139e48354ceSNicholas Bellinger dr = iscsit_allocate_datain_req(); 1140e48354ceSNicholas Bellinger if (!dr) 1141ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 1142ba159914SNicholas Bellinger ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); 1143e48354ceSNicholas Bellinger 1144e48354ceSNicholas Bellinger iscsit_attach_datain_req(cmd, dr); 1145e48354ceSNicholas Bellinger } 1146e48354ceSNicholas Bellinger 1147e48354ceSNicholas Bellinger /* 1148065ca1e4SAndy Grover * Initialize struct se_cmd descriptor from target_core_mod infrastructure 1149065ca1e4SAndy Grover */ 11509ac8928eSChristoph Hellwig transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops, 115150e5c87dSChristoph Hellwig conn->sess->se_sess, be32_to_cpu(hdr->data_length), 115250e5c87dSChristoph Hellwig cmd->data_direction, sam_task_attr, 115350e5c87dSChristoph Hellwig cmd->sense_buffer + 2); 1154065ca1e4SAndy Grover 1155065ca1e4SAndy Grover pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x," 1156065ca1e4SAndy Grover " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt, 11573e1c81a9SNicholas Bellinger hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length, 11583e1c81a9SNicholas Bellinger conn->cid); 11593e1c81a9SNicholas Bellinger 1160afc16604SBart Van Assche target_get_sess_cmd(&cmd->se_cmd, true); 1161065ca1e4SAndy Grover 1162de103c93SChristoph Hellwig cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd, 11634f26998aSAndy Grover scsilun_to_int(&hdr->lun)); 1164de103c93SChristoph Hellwig if (cmd->sense_reason) 1165e48354ceSNicholas Bellinger goto attach_cmd; 1166a12f41f8SAndy Grover 1167649ee054SBart Van Assche /* only used for printks or comparing with ->ref_task_tag */ 1168649ee054SBart Van Assche cmd->se_cmd.tag = (__force u32)cmd->init_task_tag; 1169de103c93SChristoph Hellwig cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb); 1170de103c93SChristoph Hellwig if (cmd->sense_reason) { 1171de103c93SChristoph Hellwig if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) { 1172ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 1173ba159914SNicholas Bellinger ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); 1174de103c93SChristoph Hellwig } 1175de103c93SChristoph Hellwig 1176de103c93SChristoph Hellwig goto attach_cmd; 1177de103c93SChristoph Hellwig } 1178de103c93SChristoph Hellwig 1179de103c93SChristoph Hellwig if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) { 1180ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 1181ba159914SNicholas Bellinger ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); 1182e48354ceSNicholas Bellinger } 1183e48354ceSNicholas Bellinger 1184e48354ceSNicholas Bellinger attach_cmd: 1185e48354ceSNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 11862fbb471eSAndy Grover list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 1187e48354ceSNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 1188e48354ceSNicholas Bellinger /* 1189e48354ceSNicholas Bellinger * Check if we need to delay processing because of ALUA 1190e48354ceSNicholas Bellinger * Active/NonOptimized primary access state.. 1191e48354ceSNicholas Bellinger */ 1192e48354ceSNicholas Bellinger core_alua_check_nonop_delay(&cmd->se_cmd); 1193bfb79eacSAndy Grover 11943e1c81a9SNicholas Bellinger return 0; 1195de103c93SChristoph Hellwig } 11963e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_setup_scsi_cmd); 1197de103c93SChristoph Hellwig 11983e1c81a9SNicholas Bellinger void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd) 11993e1c81a9SNicholas Bellinger { 12003e1c81a9SNicholas Bellinger iscsit_set_dataout_sequence_values(cmd); 12013e1c81a9SNicholas Bellinger 12023e1c81a9SNicholas Bellinger spin_lock_bh(&cmd->dataout_timeout_lock); 12033e1c81a9SNicholas Bellinger iscsit_start_dataout_timer(cmd, cmd->conn); 12043e1c81a9SNicholas Bellinger spin_unlock_bh(&cmd->dataout_timeout_lock); 12053e1c81a9SNicholas Bellinger } 12063e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout); 12073e1c81a9SNicholas Bellinger 12083e1c81a9SNicholas Bellinger int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 12093e1c81a9SNicholas Bellinger struct iscsi_scsi_req *hdr) 12103e1c81a9SNicholas Bellinger { 12113e1c81a9SNicholas Bellinger int cmdsn_ret = 0; 1212e48354ceSNicholas Bellinger /* 1213e48354ceSNicholas Bellinger * Check the CmdSN against ExpCmdSN/MaxCmdSN here if 1214e48354ceSNicholas Bellinger * the Immediate Bit is not set, and no Immediate 1215e48354ceSNicholas Bellinger * Data is attached. 1216e48354ceSNicholas Bellinger * 1217e48354ceSNicholas Bellinger * A PDU/CmdSN carrying Immediate Data can only 1218e48354ceSNicholas Bellinger * be processed after the DataCRC has passed. 1219e48354ceSNicholas Bellinger * If the DataCRC fails, the CmdSN MUST NOT 1220e48354ceSNicholas Bellinger * be acknowledged. (See below) 1221e48354ceSNicholas Bellinger */ 1222e48354ceSNicholas Bellinger if (!cmd->immediate_data) { 1223561bf158SNicholas Bellinger cmdsn_ret = iscsit_sequence_cmd(conn, cmd, 1224561bf158SNicholas Bellinger (unsigned char *)hdr, hdr->cmdsn); 1225561bf158SNicholas Bellinger if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) 1226561bf158SNicholas Bellinger return -1; 1227561bf158SNicholas Bellinger else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) { 1228afc16604SBart Van Assche target_put_sess_cmd(&cmd->se_cmd); 12293e1c81a9SNicholas Bellinger return 0; 12303e1c81a9SNicholas Bellinger } 1231e48354ceSNicholas Bellinger } 1232e48354ceSNicholas Bellinger 123350e5c87dSChristoph Hellwig iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); 1234e48354ceSNicholas Bellinger 1235e48354ceSNicholas Bellinger /* 1236e48354ceSNicholas Bellinger * If no Immediate Data is attached, it's OK to return now. 1237e48354ceSNicholas Bellinger */ 1238e48354ceSNicholas Bellinger if (!cmd->immediate_data) { 12393e1c81a9SNicholas Bellinger if (!cmd->sense_reason && cmd->unsolicited_data) 12403e1c81a9SNicholas Bellinger iscsit_set_unsoliticed_dataout(cmd); 12413e1c81a9SNicholas Bellinger if (!cmd->sense_reason) 12423e1c81a9SNicholas Bellinger return 0; 1243e48354ceSNicholas Bellinger 1244afc16604SBart Van Assche target_put_sess_cmd(&cmd->se_cmd); 1245e48354ceSNicholas Bellinger return 0; 1246e48354ceSNicholas Bellinger } 1247e48354ceSNicholas Bellinger 1248e48354ceSNicholas Bellinger /* 12493e1c81a9SNicholas Bellinger * Early CHECK_CONDITIONs with ImmediateData never make it to command 12503e1c81a9SNicholas Bellinger * execution. These exceptions are processed in CmdSN order using 12513e1c81a9SNicholas Bellinger * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below. 1252e48354ceSNicholas Bellinger */ 12538fa4011eSBart Van Assche if (cmd->sense_reason) 12543e1c81a9SNicholas Bellinger return 1; 1255e48354ceSNicholas Bellinger /* 1256e48354ceSNicholas Bellinger * Call directly into transport_generic_new_cmd() to perform 1257e48354ceSNicholas Bellinger * the backend memory allocation. 1258e48354ceSNicholas Bellinger */ 1259de103c93SChristoph Hellwig cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd); 1260561bf158SNicholas Bellinger if (cmd->sense_reason) 12613e1c81a9SNicholas Bellinger return 1; 1262e48354ceSNicholas Bellinger 12633e1c81a9SNicholas Bellinger return 0; 12643e1c81a9SNicholas Bellinger } 12653e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_process_scsi_cmd); 12663e1c81a9SNicholas Bellinger 12673e1c81a9SNicholas Bellinger static int 12683e1c81a9SNicholas Bellinger iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr, 12693e1c81a9SNicholas Bellinger bool dump_payload) 12703e1c81a9SNicholas Bellinger { 12713e1c81a9SNicholas Bellinger int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION; 12723e1c81a9SNicholas Bellinger /* 12733e1c81a9SNicholas Bellinger * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes. 12743e1c81a9SNicholas Bellinger */ 12750bcc297eSChristophe Vu-Brugier if (dump_payload) 12763e1c81a9SNicholas Bellinger goto after_immediate_data; 1277abb85a9bSNicholas Bellinger /* 1278abb85a9bSNicholas Bellinger * Check for underflow case where both EDTL and immediate data payload 1279abb85a9bSNicholas Bellinger * exceeds what is presented by CDB's TRANSFER LENGTH, and what has 1280abb85a9bSNicholas Bellinger * already been set in target_cmd_size_check() as se_cmd->data_length. 1281abb85a9bSNicholas Bellinger * 1282abb85a9bSNicholas Bellinger * For this special case, fail the command and dump the immediate data 1283abb85a9bSNicholas Bellinger * payload. 1284abb85a9bSNicholas Bellinger */ 1285abb85a9bSNicholas Bellinger if (cmd->first_burst_len > cmd->se_cmd.data_length) { 1286abb85a9bSNicholas Bellinger cmd->sense_reason = TCM_INVALID_CDB_FIELD; 1287abb85a9bSNicholas Bellinger goto after_immediate_data; 1288abb85a9bSNicholas Bellinger } 12893e1c81a9SNicholas Bellinger 12903e1c81a9SNicholas Bellinger immed_ret = iscsit_handle_immediate_data(cmd, hdr, 12913e1c81a9SNicholas Bellinger cmd->first_burst_len); 1292e48354ceSNicholas Bellinger after_immediate_data: 1293e48354ceSNicholas Bellinger if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) { 1294e48354ceSNicholas Bellinger /* 1295e48354ceSNicholas Bellinger * A PDU/CmdSN carrying Immediate Data passed 1296e48354ceSNicholas Bellinger * DataCRC, check against ExpCmdSN/MaxCmdSN if 1297e48354ceSNicholas Bellinger * Immediate Bit is not set. 1298e48354ceSNicholas Bellinger */ 1299561bf158SNicholas Bellinger cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd, 1300561bf158SNicholas Bellinger (unsigned char *)hdr, hdr->cmdsn); 13019d86a2beSNicholas Bellinger if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) 1302561bf158SNicholas Bellinger return -1; 1303e48354ceSNicholas Bellinger 13049d86a2beSNicholas Bellinger if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) { 1305561bf158SNicholas Bellinger int rc; 1306561bf158SNicholas Bellinger 1307561bf158SNicholas Bellinger rc = iscsit_dump_data_payload(cmd->conn, 1308561bf158SNicholas Bellinger cmd->first_burst_len, 1); 1309afc16604SBart Van Assche target_put_sess_cmd(&cmd->se_cmd); 1310561bf158SNicholas Bellinger return rc; 13113e1c81a9SNicholas Bellinger } else if (cmd->unsolicited_data) 13123e1c81a9SNicholas Bellinger iscsit_set_unsoliticed_dataout(cmd); 1313e48354ceSNicholas Bellinger 1314e48354ceSNicholas Bellinger } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) { 1315e48354ceSNicholas Bellinger /* 1316e48354ceSNicholas Bellinger * Immediate Data failed DataCRC and ERL>=1, 1317e48354ceSNicholas Bellinger * silently drop this PDU and let the initiator 1318e48354ceSNicholas Bellinger * plug the CmdSN gap. 1319e48354ceSNicholas Bellinger * 1320e48354ceSNicholas Bellinger * FIXME: Send Unsolicited NOPIN with reserved 1321e48354ceSNicholas Bellinger * TTT here to help the initiator figure out 1322e48354ceSNicholas Bellinger * the missing CmdSN, although they should be 1323e48354ceSNicholas Bellinger * intelligent enough to determine the missing 1324e48354ceSNicholas Bellinger * CmdSN and issue a retry to plug the sequence. 1325e48354ceSNicholas Bellinger */ 1326e48354ceSNicholas Bellinger cmd->i_state = ISTATE_REMOVE; 13273e1c81a9SNicholas Bellinger iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state); 1328e48354ceSNicholas Bellinger } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */ 1329e48354ceSNicholas Bellinger return -1; 1330e48354ceSNicholas Bellinger 1331e48354ceSNicholas Bellinger return 0; 1332e48354ceSNicholas Bellinger } 1333e48354ceSNicholas Bellinger 13343e1c81a9SNicholas Bellinger static int 13353e1c81a9SNicholas Bellinger iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 13363e1c81a9SNicholas Bellinger unsigned char *buf) 13373e1c81a9SNicholas Bellinger { 13383e1c81a9SNicholas Bellinger struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf; 13393e1c81a9SNicholas Bellinger int rc, immed_data; 13403e1c81a9SNicholas Bellinger bool dump_payload = false; 13413e1c81a9SNicholas Bellinger 13423e1c81a9SNicholas Bellinger rc = iscsit_setup_scsi_cmd(conn, cmd, buf); 13433e1c81a9SNicholas Bellinger if (rc < 0) 1344561bf158SNicholas Bellinger return 0; 13453e1c81a9SNicholas Bellinger /* 13463e1c81a9SNicholas Bellinger * Allocation iovecs needed for struct socket operations for 13473e1c81a9SNicholas Bellinger * traditional iSCSI block I/O. 13483e1c81a9SNicholas Bellinger */ 13493e1c81a9SNicholas Bellinger if (iscsit_allocate_iovecs(cmd) < 0) { 1350b815fc12SMike Christie return iscsit_reject_cmd(cmd, 1351ba159914SNicholas Bellinger ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); 13523e1c81a9SNicholas Bellinger } 13533e1c81a9SNicholas Bellinger immed_data = cmd->immediate_data; 13543e1c81a9SNicholas Bellinger 13553e1c81a9SNicholas Bellinger rc = iscsit_process_scsi_cmd(conn, cmd, hdr); 13563e1c81a9SNicholas Bellinger if (rc < 0) 13573e1c81a9SNicholas Bellinger return rc; 13583e1c81a9SNicholas Bellinger else if (rc > 0) 13593e1c81a9SNicholas Bellinger dump_payload = true; 13603e1c81a9SNicholas Bellinger 13613e1c81a9SNicholas Bellinger if (!immed_data) 13623e1c81a9SNicholas Bellinger return 0; 13633e1c81a9SNicholas Bellinger 13643e1c81a9SNicholas Bellinger return iscsit_get_immediate_data(cmd, hdr, dump_payload); 13653e1c81a9SNicholas Bellinger } 13663e1c81a9SNicholas Bellinger 1367e48354ceSNicholas Bellinger static u32 iscsit_do_crypto_hash_sg( 136869110e3cSHerbert Xu struct ahash_request *hash, 1369e48354ceSNicholas Bellinger struct iscsi_cmd *cmd, 1370e48354ceSNicholas Bellinger u32 data_offset, 1371e48354ceSNicholas Bellinger u32 data_length, 1372e48354ceSNicholas Bellinger u32 padding, 1373e48354ceSNicholas Bellinger u8 *pad_bytes) 1374e48354ceSNicholas Bellinger { 1375e48354ceSNicholas Bellinger u32 data_crc; 1376e48354ceSNicholas Bellinger struct scatterlist *sg; 1377e48354ceSNicholas Bellinger unsigned int page_off; 1378e48354ceSNicholas Bellinger 137969110e3cSHerbert Xu crypto_ahash_init(hash); 1380e48354ceSNicholas Bellinger 1381e48354ceSNicholas Bellinger sg = cmd->first_data_sg; 1382e48354ceSNicholas Bellinger page_off = cmd->first_data_sg_off; 1383e48354ceSNicholas Bellinger 1384e48354ceSNicholas Bellinger while (data_length) { 1385aa75679cSAlexei Potashnik u32 cur_len = min_t(u32, data_length, (sg->length - page_off)); 1386e48354ceSNicholas Bellinger 138769110e3cSHerbert Xu ahash_request_set_crypt(hash, sg, NULL, cur_len); 138869110e3cSHerbert Xu crypto_ahash_update(hash); 1389e48354ceSNicholas Bellinger 1390e48354ceSNicholas Bellinger data_length -= cur_len; 1391e48354ceSNicholas Bellinger page_off = 0; 1392aa75679cSAlexei Potashnik /* iscsit_map_iovec has already checked for invalid sg pointers */ 1393aa75679cSAlexei Potashnik sg = sg_next(sg); 1394e48354ceSNicholas Bellinger } 1395e48354ceSNicholas Bellinger 1396e48354ceSNicholas Bellinger if (padding) { 1397e48354ceSNicholas Bellinger struct scatterlist pad_sg; 1398e48354ceSNicholas Bellinger 1399e48354ceSNicholas Bellinger sg_init_one(&pad_sg, pad_bytes, padding); 140069110e3cSHerbert Xu ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc, 140169110e3cSHerbert Xu padding); 140269110e3cSHerbert Xu crypto_ahash_finup(hash); 140369110e3cSHerbert Xu } else { 140469110e3cSHerbert Xu ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0); 140569110e3cSHerbert Xu crypto_ahash_final(hash); 1406e48354ceSNicholas Bellinger } 1407e48354ceSNicholas Bellinger 1408e48354ceSNicholas Bellinger return data_crc; 1409e48354ceSNicholas Bellinger } 1410e48354ceSNicholas Bellinger 1411e1dfb21fSBart Van Assche static void iscsit_do_crypto_hash_buf(struct ahash_request *hash, 1412e1dfb21fSBart Van Assche const void *buf, u32 payload_length, u32 padding, 1413e1dfb21fSBart Van Assche const void *pad_bytes, void *data_crc) 1414e48354ceSNicholas Bellinger { 141569110e3cSHerbert Xu struct scatterlist sg[2]; 1416e48354ceSNicholas Bellinger 141769110e3cSHerbert Xu sg_init_table(sg, ARRAY_SIZE(sg)); 141869110e3cSHerbert Xu sg_set_buf(sg, buf, payload_length); 141969110e3cSHerbert Xu sg_set_buf(sg + 1, pad_bytes, padding); 1420e48354ceSNicholas Bellinger 142169110e3cSHerbert Xu ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding); 1422e48354ceSNicholas Bellinger 142369110e3cSHerbert Xu crypto_ahash_digest(hash); 1424e48354ceSNicholas Bellinger } 1425e48354ceSNicholas Bellinger 14263e1c81a9SNicholas Bellinger int 14279a584bf9SVarun Prakash __iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf, 14289a584bf9SVarun Prakash struct iscsi_cmd *cmd, u32 payload_length, 14299a584bf9SVarun Prakash bool *success) 1430e48354ceSNicholas Bellinger { 14319a584bf9SVarun Prakash struct iscsi_data *hdr = buf; 1432e48354ceSNicholas Bellinger struct se_cmd *se_cmd; 14333e1c81a9SNicholas Bellinger int rc; 1434e48354ceSNicholas Bellinger 1435e48354ceSNicholas Bellinger /* iSCSI write */ 143604f3b31bSNicholas Bellinger atomic_long_add(payload_length, &conn->sess->rx_data_octets); 1437e48354ceSNicholas Bellinger 1438e48354ceSNicholas Bellinger pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x," 1439e48354ceSNicholas Bellinger " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n", 14403e1c81a9SNicholas Bellinger hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset), 1441e48354ceSNicholas Bellinger payload_length, conn->cid); 1442e48354ceSNicholas Bellinger 1443e48354ceSNicholas Bellinger if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) { 1444e48354ceSNicholas Bellinger pr_err("Command ITT: 0x%08x received DataOUT after" 1445e48354ceSNicholas Bellinger " last DataOUT received, dumping payload\n", 1446e48354ceSNicholas Bellinger cmd->init_task_tag); 1447e48354ceSNicholas Bellinger return iscsit_dump_data_payload(conn, payload_length, 1); 1448e48354ceSNicholas Bellinger } 1449e48354ceSNicholas Bellinger 1450e48354ceSNicholas Bellinger if (cmd->data_direction != DMA_TO_DEVICE) { 1451e48354ceSNicholas Bellinger pr_err("Command ITT: 0x%08x received DataOUT for a" 1452e48354ceSNicholas Bellinger " NON-WRITE command.\n", cmd->init_task_tag); 145397c99b47SNicholas Bellinger return iscsit_dump_data_payload(conn, payload_length, 1); 1454e48354ceSNicholas Bellinger } 1455e48354ceSNicholas Bellinger se_cmd = &cmd->se_cmd; 1456e48354ceSNicholas Bellinger iscsit_mod_dataout_timer(cmd); 1457e48354ceSNicholas Bellinger 145850e5c87dSChristoph Hellwig if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) { 1459de3493aeSBart Van Assche pr_err("DataOut Offset: %u, Length %u greater than iSCSI Command EDTL %u, protocol error.\n", 1460de3493aeSBart Van Assche be32_to_cpu(hdr->offset), payload_length, 1461de3493aeSBart Van Assche cmd->se_cmd.data_length); 1462ba159914SNicholas Bellinger return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf); 1463e48354ceSNicholas Bellinger } 1464e48354ceSNicholas Bellinger 1465e48354ceSNicholas Bellinger if (cmd->unsolicited_data) { 1466e48354ceSNicholas Bellinger int dump_unsolicited_data = 0; 1467e48354ceSNicholas Bellinger 1468e48354ceSNicholas Bellinger if (conn->sess->sess_ops->InitialR2T) { 1469e48354ceSNicholas Bellinger pr_err("Received unexpected unsolicited data" 1470e48354ceSNicholas Bellinger " while InitialR2T=Yes, protocol error.\n"); 1471e48354ceSNicholas Bellinger transport_send_check_condition_and_sense(&cmd->se_cmd, 1472e48354ceSNicholas Bellinger TCM_UNEXPECTED_UNSOLICITED_DATA, 0); 1473e48354ceSNicholas Bellinger return -1; 1474e48354ceSNicholas Bellinger } 1475e48354ceSNicholas Bellinger /* 1476e48354ceSNicholas Bellinger * Special case for dealing with Unsolicited DataOUT 1477e48354ceSNicholas Bellinger * and Unsupported SAM WRITE Opcodes and SE resource allocation 1478e48354ceSNicholas Bellinger * failures; 1479e48354ceSNicholas Bellinger */ 1480e48354ceSNicholas Bellinger 1481e48354ceSNicholas Bellinger /* Something's amiss if we're not in WRITE_PENDING state... */ 1482e48354ceSNicholas Bellinger WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING); 1483de103c93SChristoph Hellwig if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE)) 1484e48354ceSNicholas Bellinger dump_unsolicited_data = 1; 1485e48354ceSNicholas Bellinger 1486e48354ceSNicholas Bellinger if (dump_unsolicited_data) { 1487e48354ceSNicholas Bellinger /* 1488e48354ceSNicholas Bellinger * Check if a delayed TASK_ABORTED status needs to 1489e48354ceSNicholas Bellinger * be sent now if the ISCSI_FLAG_CMD_FINAL has been 14905a342521SBart Van Assche * received with the unsolicited data out. 1491e48354ceSNicholas Bellinger */ 1492e48354ceSNicholas Bellinger if (hdr->flags & ISCSI_FLAG_CMD_FINAL) 1493e48354ceSNicholas Bellinger iscsit_stop_dataout_timer(cmd); 1494e48354ceSNicholas Bellinger 1495e48354ceSNicholas Bellinger transport_check_aborted_status(se_cmd, 1496e48354ceSNicholas Bellinger (hdr->flags & ISCSI_FLAG_CMD_FINAL)); 1497e48354ceSNicholas Bellinger return iscsit_dump_data_payload(conn, payload_length, 1); 1498e48354ceSNicholas Bellinger } 1499e48354ceSNicholas Bellinger } else { 1500e48354ceSNicholas Bellinger /* 1501e48354ceSNicholas Bellinger * For the normal solicited data path: 1502e48354ceSNicholas Bellinger * 1503e48354ceSNicholas Bellinger * Check for a delayed TASK_ABORTED status and dump any 1504e48354ceSNicholas Bellinger * incoming data out payload if one exists. Also, when the 1505e48354ceSNicholas Bellinger * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current 1506e48354ceSNicholas Bellinger * data out sequence, we decrement outstanding_r2ts. Once 1507e48354ceSNicholas Bellinger * outstanding_r2ts reaches zero, go ahead and send the delayed 1508e48354ceSNicholas Bellinger * TASK_ABORTED status. 1509e48354ceSNicholas Bellinger */ 15107d680f3bSChristoph Hellwig if (se_cmd->transport_state & CMD_T_ABORTED) { 1511e48354ceSNicholas Bellinger if (hdr->flags & ISCSI_FLAG_CMD_FINAL) 1512e48354ceSNicholas Bellinger if (--cmd->outstanding_r2ts < 1) { 1513e48354ceSNicholas Bellinger iscsit_stop_dataout_timer(cmd); 1514e48354ceSNicholas Bellinger transport_check_aborted_status( 1515e48354ceSNicholas Bellinger se_cmd, 1); 1516e48354ceSNicholas Bellinger } 1517e48354ceSNicholas Bellinger 1518e48354ceSNicholas Bellinger return iscsit_dump_data_payload(conn, payload_length, 1); 1519e48354ceSNicholas Bellinger } 1520e48354ceSNicholas Bellinger } 1521e48354ceSNicholas Bellinger /* 15220d5efb8aSBart Van Assche * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and 1523e48354ceSNicholas Bellinger * within-command recovery checks before receiving the payload. 1524e48354ceSNicholas Bellinger */ 15253e1c81a9SNicholas Bellinger rc = iscsit_check_pre_dataout(cmd, buf); 15263e1c81a9SNicholas Bellinger if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY) 1527e48354ceSNicholas Bellinger return 0; 15283e1c81a9SNicholas Bellinger else if (rc == DATAOUT_CANNOT_RECOVER) 1529e48354ceSNicholas Bellinger return -1; 15309a584bf9SVarun Prakash *success = true; 15313e1c81a9SNicholas Bellinger return 0; 15323e1c81a9SNicholas Bellinger } 15339a584bf9SVarun Prakash EXPORT_SYMBOL(__iscsit_check_dataout_hdr); 15349a584bf9SVarun Prakash 15359a584bf9SVarun Prakash int 15369a584bf9SVarun Prakash iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf, 15379a584bf9SVarun Prakash struct iscsi_cmd **out_cmd) 15389a584bf9SVarun Prakash { 15399a584bf9SVarun Prakash struct iscsi_data *hdr = buf; 15409a584bf9SVarun Prakash struct iscsi_cmd *cmd; 15419a584bf9SVarun Prakash u32 payload_length = ntoh24(hdr->dlength); 15429a584bf9SVarun Prakash int rc; 15439a584bf9SVarun Prakash bool success = false; 15449a584bf9SVarun Prakash 15459a584bf9SVarun Prakash if (!payload_length) { 15469a584bf9SVarun Prakash pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n"); 15479a584bf9SVarun Prakash return 0; 15489a584bf9SVarun Prakash } 15499a584bf9SVarun Prakash 15509a584bf9SVarun Prakash if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { 15519a584bf9SVarun Prakash pr_err_ratelimited("DataSegmentLength: %u is greater than" 15529a584bf9SVarun Prakash " MaxXmitDataSegmentLength: %u\n", payload_length, 15539a584bf9SVarun Prakash conn->conn_ops->MaxXmitDataSegmentLength); 15549a584bf9SVarun Prakash return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, buf); 15559a584bf9SVarun Prakash } 15569a584bf9SVarun Prakash 15579a584bf9SVarun Prakash cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, payload_length); 15589a584bf9SVarun Prakash if (!cmd) 15599a584bf9SVarun Prakash return 0; 15609a584bf9SVarun Prakash 15619a584bf9SVarun Prakash rc = __iscsit_check_dataout_hdr(conn, buf, cmd, payload_length, &success); 15629a584bf9SVarun Prakash 15639a584bf9SVarun Prakash if (success) 15649a584bf9SVarun Prakash *out_cmd = cmd; 15659a584bf9SVarun Prakash 15669a584bf9SVarun Prakash return rc; 15679a584bf9SVarun Prakash } 15683e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_check_dataout_hdr); 15693e1c81a9SNicholas Bellinger 15703e1c81a9SNicholas Bellinger static int 15713e1c81a9SNicholas Bellinger iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 15723e1c81a9SNicholas Bellinger struct iscsi_data *hdr) 15733e1c81a9SNicholas Bellinger { 15743e1c81a9SNicholas Bellinger struct kvec *iov; 15753e1c81a9SNicholas Bellinger u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0; 15763e1c81a9SNicholas Bellinger u32 payload_length = ntoh24(hdr->dlength); 15773e1c81a9SNicholas Bellinger int iov_ret, data_crc_failed = 0; 15783e1c81a9SNicholas Bellinger 1579e48354ceSNicholas Bellinger rx_size += payload_length; 1580e48354ceSNicholas Bellinger iov = &cmd->iov_data[0]; 1581e48354ceSNicholas Bellinger 158250e5c87dSChristoph Hellwig iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset), 158350e5c87dSChristoph Hellwig payload_length); 1584e48354ceSNicholas Bellinger if (iov_ret < 0) 1585e48354ceSNicholas Bellinger return -1; 1586e48354ceSNicholas Bellinger 1587e48354ceSNicholas Bellinger iov_count += iov_ret; 1588e48354ceSNicholas Bellinger 1589e48354ceSNicholas Bellinger padding = ((-payload_length) & 3); 1590e48354ceSNicholas Bellinger if (padding != 0) { 1591e48354ceSNicholas Bellinger iov[iov_count].iov_base = cmd->pad_bytes; 1592e48354ceSNicholas Bellinger iov[iov_count++].iov_len = padding; 1593e48354ceSNicholas Bellinger rx_size += padding; 1594e48354ceSNicholas Bellinger pr_debug("Receiving %u padding bytes.\n", padding); 1595e48354ceSNicholas Bellinger } 1596e48354ceSNicholas Bellinger 1597e48354ceSNicholas Bellinger if (conn->conn_ops->DataDigest) { 1598e48354ceSNicholas Bellinger iov[iov_count].iov_base = &checksum; 1599e48354ceSNicholas Bellinger iov[iov_count++].iov_len = ISCSI_CRC_LEN; 1600e48354ceSNicholas Bellinger rx_size += ISCSI_CRC_LEN; 1601e48354ceSNicholas Bellinger } 1602e48354ceSNicholas Bellinger 1603e48354ceSNicholas Bellinger rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size); 1604e48354ceSNicholas Bellinger 1605e48354ceSNicholas Bellinger iscsit_unmap_iovec(cmd); 1606e48354ceSNicholas Bellinger 1607e48354ceSNicholas Bellinger if (rx_got != rx_size) 1608e48354ceSNicholas Bellinger return -1; 1609e48354ceSNicholas Bellinger 1610e48354ceSNicholas Bellinger if (conn->conn_ops->DataDigest) { 1611e48354ceSNicholas Bellinger u32 data_crc; 1612e48354ceSNicholas Bellinger 161369110e3cSHerbert Xu data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd, 161450e5c87dSChristoph Hellwig be32_to_cpu(hdr->offset), 161550e5c87dSChristoph Hellwig payload_length, padding, 1616e48354ceSNicholas Bellinger cmd->pad_bytes); 1617e48354ceSNicholas Bellinger 1618e48354ceSNicholas Bellinger if (checksum != data_crc) { 1619e48354ceSNicholas Bellinger pr_err("ITT: 0x%08x, Offset: %u, Length: %u," 1620e48354ceSNicholas Bellinger " DataSN: 0x%08x, CRC32C DataDigest 0x%08x" 1621e48354ceSNicholas Bellinger " does not match computed 0x%08x\n", 1622e48354ceSNicholas Bellinger hdr->itt, hdr->offset, payload_length, 1623e48354ceSNicholas Bellinger hdr->datasn, checksum, data_crc); 1624e48354ceSNicholas Bellinger data_crc_failed = 1; 1625e48354ceSNicholas Bellinger } else { 1626e48354ceSNicholas Bellinger pr_debug("Got CRC32C DataDigest 0x%08x for" 1627e48354ceSNicholas Bellinger " %u bytes of Data Out\n", checksum, 1628e48354ceSNicholas Bellinger payload_length); 1629e48354ceSNicholas Bellinger } 1630e48354ceSNicholas Bellinger } 16313e1c81a9SNicholas Bellinger 16323e1c81a9SNicholas Bellinger return data_crc_failed; 16333e1c81a9SNicholas Bellinger } 16343e1c81a9SNicholas Bellinger 16353e1c81a9SNicholas Bellinger int 16363e1c81a9SNicholas Bellinger iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr, 16373e1c81a9SNicholas Bellinger bool data_crc_failed) 16383e1c81a9SNicholas Bellinger { 16393e1c81a9SNicholas Bellinger struct iscsi_conn *conn = cmd->conn; 16403e1c81a9SNicholas Bellinger int rc, ooo_cmdsn; 1641e48354ceSNicholas Bellinger /* 1642e48354ceSNicholas Bellinger * Increment post receive data and CRC values or perform 1643e48354ceSNicholas Bellinger * within-command recovery. 1644e48354ceSNicholas Bellinger */ 16453e1c81a9SNicholas Bellinger rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed); 16463e1c81a9SNicholas Bellinger if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)) 1647e48354ceSNicholas Bellinger return 0; 16483e1c81a9SNicholas Bellinger else if (rc == DATAOUT_SEND_R2T) { 1649e48354ceSNicholas Bellinger iscsit_set_dataout_sequence_values(cmd); 16503e1c81a9SNicholas Bellinger conn->conn_transport->iscsit_get_dataout(conn, cmd, false); 16513e1c81a9SNicholas Bellinger } else if (rc == DATAOUT_SEND_TO_TRANSPORT) { 1652e48354ceSNicholas Bellinger /* 1653e48354ceSNicholas Bellinger * Handle extra special case for out of order 1654e48354ceSNicholas Bellinger * Unsolicited Data Out. 1655e48354ceSNicholas Bellinger */ 1656e48354ceSNicholas Bellinger spin_lock_bh(&cmd->istate_lock); 1657e48354ceSNicholas Bellinger ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN); 1658e48354ceSNicholas Bellinger cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT; 1659e48354ceSNicholas Bellinger cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT; 1660e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock); 1661e48354ceSNicholas Bellinger 1662e48354ceSNicholas Bellinger iscsit_stop_dataout_timer(cmd); 166367441b68SChristoph Hellwig if (ooo_cmdsn) 166467441b68SChristoph Hellwig return 0; 166567441b68SChristoph Hellwig target_execute_cmd(&cmd->se_cmd); 166667441b68SChristoph Hellwig return 0; 1667e48354ceSNicholas Bellinger } else /* DATAOUT_CANNOT_RECOVER */ 1668e48354ceSNicholas Bellinger return -1; 1669e48354ceSNicholas Bellinger 1670e48354ceSNicholas Bellinger return 0; 1671e48354ceSNicholas Bellinger } 16723e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_check_dataout_payload); 1673e48354ceSNicholas Bellinger 16743e1c81a9SNicholas Bellinger static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf) 16753e1c81a9SNicholas Bellinger { 1676dbcbc95cSNicholas Bellinger struct iscsi_cmd *cmd = NULL; 16773e1c81a9SNicholas Bellinger struct iscsi_data *hdr = (struct iscsi_data *)buf; 16783e1c81a9SNicholas Bellinger int rc; 16793e1c81a9SNicholas Bellinger bool data_crc_failed = false; 16803e1c81a9SNicholas Bellinger 16813e1c81a9SNicholas Bellinger rc = iscsit_check_dataout_hdr(conn, buf, &cmd); 16823e1c81a9SNicholas Bellinger if (rc < 0) 1683561bf158SNicholas Bellinger return 0; 16843e1c81a9SNicholas Bellinger else if (!cmd) 16853e1c81a9SNicholas Bellinger return 0; 16863e1c81a9SNicholas Bellinger 16873e1c81a9SNicholas Bellinger rc = iscsit_get_dataout(conn, cmd, hdr); 16883e1c81a9SNicholas Bellinger if (rc < 0) 16893e1c81a9SNicholas Bellinger return rc; 16903e1c81a9SNicholas Bellinger else if (rc > 0) 16913e1c81a9SNicholas Bellinger data_crc_failed = true; 16923e1c81a9SNicholas Bellinger 16933e1c81a9SNicholas Bellinger return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed); 16943e1c81a9SNicholas Bellinger } 16953e1c81a9SNicholas Bellinger 1696778de368SNicholas Bellinger int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 1697778de368SNicholas Bellinger struct iscsi_nopout *hdr) 1698e48354ceSNicholas Bellinger { 1699778de368SNicholas Bellinger u32 payload_length = ntoh24(hdr->dlength); 1700e48354ceSNicholas Bellinger 1701a3662605SArshad Hussain if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) { 1702a3662605SArshad Hussain pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n"); 1703a3662605SArshad Hussain if (!cmd) 1704a3662605SArshad Hussain return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 1705a3662605SArshad Hussain (unsigned char *)hdr); 1706a3662605SArshad Hussain 1707a3662605SArshad Hussain return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, 1708a3662605SArshad Hussain (unsigned char *)hdr); 1709a3662605SArshad Hussain } 1710a3662605SArshad Hussain 171166c7db68SChristoph Hellwig if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { 1712e48354ceSNicholas Bellinger pr_err("NOPOUT ITT is reserved, but Immediate Bit is" 1713e48354ceSNicholas Bellinger " not set, protocol error.\n"); 171428aaa950SNicholas Bellinger if (!cmd) 171528aaa950SNicholas Bellinger return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 171628aaa950SNicholas Bellinger (unsigned char *)hdr); 171728aaa950SNicholas Bellinger 1718ba159914SNicholas Bellinger return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, 1719ba159914SNicholas Bellinger (unsigned char *)hdr); 1720e48354ceSNicholas Bellinger } 1721e48354ceSNicholas Bellinger 172221f5aa7eSNicholas Bellinger if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { 1723e48354ceSNicholas Bellinger pr_err("NOPOUT Ping Data DataSegmentLength: %u is" 172421f5aa7eSNicholas Bellinger " greater than MaxXmitDataSegmentLength: %u, protocol" 1725e48354ceSNicholas Bellinger " error.\n", payload_length, 172621f5aa7eSNicholas Bellinger conn->conn_ops->MaxXmitDataSegmentLength); 172728aaa950SNicholas Bellinger if (!cmd) 172828aaa950SNicholas Bellinger return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 172928aaa950SNicholas Bellinger (unsigned char *)hdr); 173028aaa950SNicholas Bellinger 1731ba159914SNicholas Bellinger return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, 1732ba159914SNicholas Bellinger (unsigned char *)hdr); 1733e48354ceSNicholas Bellinger } 1734e48354ceSNicholas Bellinger 17353e1c81a9SNicholas Bellinger pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x," 1736e48354ceSNicholas Bellinger " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n", 173766c7db68SChristoph Hellwig hdr->itt == RESERVED_ITT ? "Response" : "Request", 1738e48354ceSNicholas Bellinger hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn, 1739e48354ceSNicholas Bellinger payload_length); 1740e48354ceSNicholas Bellinger /* 1741e48354ceSNicholas Bellinger * This is not a response to a Unsolicited NopIN, which means 1742e48354ceSNicholas Bellinger * it can either be a NOPOUT ping request (with a valid ITT), 1743e48354ceSNicholas Bellinger * or a NOPOUT not requesting a NOPIN (with a reserved ITT). 1744e48354ceSNicholas Bellinger * Either way, make sure we allocate an struct iscsi_cmd, as both 1745e48354ceSNicholas Bellinger * can contain ping data. 1746e48354ceSNicholas Bellinger */ 174750e5c87dSChristoph Hellwig if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) { 1748e48354ceSNicholas Bellinger cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT; 1749e48354ceSNicholas Bellinger cmd->i_state = ISTATE_SEND_NOPIN; 1750e48354ceSNicholas Bellinger cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1751e48354ceSNicholas Bellinger 1 : 0); 1752e48354ceSNicholas Bellinger conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt; 1753e48354ceSNicholas Bellinger cmd->targ_xfer_tag = 0xFFFFFFFF; 175450e5c87dSChristoph Hellwig cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); 175550e5c87dSChristoph Hellwig cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); 1756e48354ceSNicholas Bellinger cmd->data_direction = DMA_NONE; 1757e48354ceSNicholas Bellinger } 1758e48354ceSNicholas Bellinger 1759778de368SNicholas Bellinger return 0; 1760778de368SNicholas Bellinger } 1761778de368SNicholas Bellinger EXPORT_SYMBOL(iscsit_setup_nop_out); 1762778de368SNicholas Bellinger 1763778de368SNicholas Bellinger int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 1764778de368SNicholas Bellinger struct iscsi_nopout *hdr) 1765778de368SNicholas Bellinger { 1766778de368SNicholas Bellinger struct iscsi_cmd *cmd_p = NULL; 1767778de368SNicholas Bellinger int cmdsn_ret = 0; 1768778de368SNicholas Bellinger /* 1769778de368SNicholas Bellinger * Initiator is expecting a NopIN ping reply.. 1770778de368SNicholas Bellinger */ 1771778de368SNicholas Bellinger if (hdr->itt != RESERVED_ITT) { 17727cbfcc95SNicholas Bellinger if (!cmd) 17737cbfcc95SNicholas Bellinger return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 17747cbfcc95SNicholas Bellinger (unsigned char *)hdr); 1775778de368SNicholas Bellinger 1776778de368SNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 1777778de368SNicholas Bellinger list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 1778778de368SNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 1779778de368SNicholas Bellinger 1780778de368SNicholas Bellinger iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); 1781778de368SNicholas Bellinger 1782778de368SNicholas Bellinger if (hdr->opcode & ISCSI_OP_IMMEDIATE) { 1783778de368SNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, conn, 1784778de368SNicholas Bellinger cmd->i_state); 1785778de368SNicholas Bellinger return 0; 1786778de368SNicholas Bellinger } 1787778de368SNicholas Bellinger 1788561bf158SNicholas Bellinger cmdsn_ret = iscsit_sequence_cmd(conn, cmd, 1789561bf158SNicholas Bellinger (unsigned char *)hdr, hdr->cmdsn); 1790778de368SNicholas Bellinger if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) 1791778de368SNicholas Bellinger return 0; 1792778de368SNicholas Bellinger if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) 1793ba159914SNicholas Bellinger return -1; 1794778de368SNicholas Bellinger 1795778de368SNicholas Bellinger return 0; 1796778de368SNicholas Bellinger } 1797778de368SNicholas Bellinger /* 1798778de368SNicholas Bellinger * This was a response to a unsolicited NOPIN ping. 1799778de368SNicholas Bellinger */ 1800778de368SNicholas Bellinger if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) { 1801778de368SNicholas Bellinger cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt)); 1802778de368SNicholas Bellinger if (!cmd_p) 1803778de368SNicholas Bellinger return -EINVAL; 1804778de368SNicholas Bellinger 1805778de368SNicholas Bellinger iscsit_stop_nopin_response_timer(conn); 1806778de368SNicholas Bellinger 1807778de368SNicholas Bellinger cmd_p->i_state = ISTATE_REMOVE; 1808778de368SNicholas Bellinger iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state); 1809778de368SNicholas Bellinger 1810778de368SNicholas Bellinger iscsit_start_nopin_timer(conn); 1811778de368SNicholas Bellinger return 0; 1812778de368SNicholas Bellinger } 1813778de368SNicholas Bellinger /* 1814778de368SNicholas Bellinger * Otherwise, initiator is not expecting a NOPIN is response. 1815778de368SNicholas Bellinger * Just ignore for now. 1816778de368SNicholas Bellinger */ 18171a40f0a3SVarun Prakash 18181a40f0a3SVarun Prakash if (cmd) 18191a40f0a3SVarun Prakash iscsit_free_cmd(cmd, false); 18201a40f0a3SVarun Prakash 1821778de368SNicholas Bellinger return 0; 1822778de368SNicholas Bellinger } 1823778de368SNicholas Bellinger EXPORT_SYMBOL(iscsit_process_nop_out); 1824778de368SNicholas Bellinger 1825778de368SNicholas Bellinger static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 1826778de368SNicholas Bellinger unsigned char *buf) 1827778de368SNicholas Bellinger { 1828778de368SNicholas Bellinger unsigned char *ping_data = NULL; 1829778de368SNicholas Bellinger struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf; 1830778de368SNicholas Bellinger struct kvec *iov = NULL; 1831778de368SNicholas Bellinger u32 payload_length = ntoh24(hdr->dlength); 1832778de368SNicholas Bellinger int ret; 1833778de368SNicholas Bellinger 1834778de368SNicholas Bellinger ret = iscsit_setup_nop_out(conn, cmd, hdr); 1835778de368SNicholas Bellinger if (ret < 0) 1836561bf158SNicholas Bellinger return 0; 1837778de368SNicholas Bellinger /* 1838778de368SNicholas Bellinger * Handle NOP-OUT payload for traditional iSCSI sockets 1839778de368SNicholas Bellinger */ 184050e5c87dSChristoph Hellwig if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) { 1841778de368SNicholas Bellinger u32 checksum, data_crc, padding = 0; 1842778de368SNicholas Bellinger int niov = 0, rx_got, rx_size = payload_length; 1843778de368SNicholas Bellinger 1844e48354ceSNicholas Bellinger ping_data = kzalloc(payload_length + 1, GFP_KERNEL); 1845e48354ceSNicholas Bellinger if (!ping_data) { 1846e48354ceSNicholas Bellinger ret = -1; 1847e48354ceSNicholas Bellinger goto out; 1848e48354ceSNicholas Bellinger } 1849e48354ceSNicholas Bellinger 1850e48354ceSNicholas Bellinger iov = &cmd->iov_misc[0]; 1851e48354ceSNicholas Bellinger iov[niov].iov_base = ping_data; 1852e48354ceSNicholas Bellinger iov[niov++].iov_len = payload_length; 1853e48354ceSNicholas Bellinger 1854e48354ceSNicholas Bellinger padding = ((-payload_length) & 3); 1855e48354ceSNicholas Bellinger if (padding != 0) { 1856e48354ceSNicholas Bellinger pr_debug("Receiving %u additional bytes" 1857e48354ceSNicholas Bellinger " for padding.\n", padding); 1858e48354ceSNicholas Bellinger iov[niov].iov_base = &cmd->pad_bytes; 1859e48354ceSNicholas Bellinger iov[niov++].iov_len = padding; 1860e48354ceSNicholas Bellinger rx_size += padding; 1861e48354ceSNicholas Bellinger } 1862e48354ceSNicholas Bellinger if (conn->conn_ops->DataDigest) { 1863e48354ceSNicholas Bellinger iov[niov].iov_base = &checksum; 1864e48354ceSNicholas Bellinger iov[niov++].iov_len = ISCSI_CRC_LEN; 1865e48354ceSNicholas Bellinger rx_size += ISCSI_CRC_LEN; 1866e48354ceSNicholas Bellinger } 1867e48354ceSNicholas Bellinger 1868e48354ceSNicholas Bellinger rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size); 1869e48354ceSNicholas Bellinger if (rx_got != rx_size) { 1870e48354ceSNicholas Bellinger ret = -1; 1871e48354ceSNicholas Bellinger goto out; 1872e48354ceSNicholas Bellinger } 1873e48354ceSNicholas Bellinger 1874e48354ceSNicholas Bellinger if (conn->conn_ops->DataDigest) { 1875e1dfb21fSBart Van Assche iscsit_do_crypto_hash_buf(conn->conn_rx_hash, ping_data, 1876e1dfb21fSBart Van Assche payload_length, padding, 1877e1dfb21fSBart Van Assche cmd->pad_bytes, &data_crc); 1878e48354ceSNicholas Bellinger 1879e48354ceSNicholas Bellinger if (checksum != data_crc) { 1880e48354ceSNicholas Bellinger pr_err("Ping data CRC32C DataDigest" 1881e48354ceSNicholas Bellinger " 0x%08x does not match computed 0x%08x\n", 1882e48354ceSNicholas Bellinger checksum, data_crc); 1883e48354ceSNicholas Bellinger if (!conn->sess->sess_ops->ErrorRecoveryLevel) { 1884e48354ceSNicholas Bellinger pr_err("Unable to recover from" 1885e48354ceSNicholas Bellinger " NOPOUT Ping DataCRC failure while in" 1886e48354ceSNicholas Bellinger " ERL=0.\n"); 1887e48354ceSNicholas Bellinger ret = -1; 1888e48354ceSNicholas Bellinger goto out; 1889e48354ceSNicholas Bellinger } else { 1890e48354ceSNicholas Bellinger /* 1891e48354ceSNicholas Bellinger * Silently drop this PDU and let the 1892e48354ceSNicholas Bellinger * initiator plug the CmdSN gap. 1893e48354ceSNicholas Bellinger */ 1894e48354ceSNicholas Bellinger pr_debug("Dropping NOPOUT" 1895e48354ceSNicholas Bellinger " Command CmdSN: 0x%08x due to" 1896e48354ceSNicholas Bellinger " DataCRC error.\n", hdr->cmdsn); 1897e48354ceSNicholas Bellinger ret = 0; 1898e48354ceSNicholas Bellinger goto out; 1899e48354ceSNicholas Bellinger } 1900e48354ceSNicholas Bellinger } else { 1901e48354ceSNicholas Bellinger pr_debug("Got CRC32C DataDigest" 1902e48354ceSNicholas Bellinger " 0x%08x for %u bytes of ping data.\n", 1903e48354ceSNicholas Bellinger checksum, payload_length); 1904e48354ceSNicholas Bellinger } 1905e48354ceSNicholas Bellinger } 1906e48354ceSNicholas Bellinger 1907e48354ceSNicholas Bellinger ping_data[payload_length] = '\0'; 1908e48354ceSNicholas Bellinger /* 1909e48354ceSNicholas Bellinger * Attach ping data to struct iscsi_cmd->buf_ptr. 1910e48354ceSNicholas Bellinger */ 19118359cf43SJörn Engel cmd->buf_ptr = ping_data; 1912e48354ceSNicholas Bellinger cmd->buf_ptr_size = payload_length; 1913e48354ceSNicholas Bellinger 1914e48354ceSNicholas Bellinger pr_debug("Got %u bytes of NOPOUT ping" 1915e48354ceSNicholas Bellinger " data.\n", payload_length); 1916e48354ceSNicholas Bellinger pr_debug("Ping Data: \"%s\"\n", ping_data); 1917e48354ceSNicholas Bellinger } 1918e48354ceSNicholas Bellinger 1919778de368SNicholas Bellinger return iscsit_process_nop_out(conn, cmd, hdr); 1920e48354ceSNicholas Bellinger out: 1921e48354ceSNicholas Bellinger if (cmd) 1922aafc9d15SNicholas Bellinger iscsit_free_cmd(cmd, false); 1923778de368SNicholas Bellinger 1924e48354ceSNicholas Bellinger kfree(ping_data); 1925e48354ceSNicholas Bellinger return ret; 1926e48354ceSNicholas Bellinger } 1927e48354ceSNicholas Bellinger 1928e381fe9eSBart Van Assche static enum tcm_tmreq_table iscsit_convert_tmf(u8 iscsi_tmf) 1929e381fe9eSBart Van Assche { 1930e381fe9eSBart Van Assche switch (iscsi_tmf) { 1931e381fe9eSBart Van Assche case ISCSI_TM_FUNC_ABORT_TASK: 1932e381fe9eSBart Van Assche return TMR_ABORT_TASK; 1933e381fe9eSBart Van Assche case ISCSI_TM_FUNC_ABORT_TASK_SET: 1934e381fe9eSBart Van Assche return TMR_ABORT_TASK_SET; 1935e381fe9eSBart Van Assche case ISCSI_TM_FUNC_CLEAR_ACA: 1936e381fe9eSBart Van Assche return TMR_CLEAR_ACA; 1937e381fe9eSBart Van Assche case ISCSI_TM_FUNC_CLEAR_TASK_SET: 1938e381fe9eSBart Van Assche return TMR_CLEAR_TASK_SET; 1939e381fe9eSBart Van Assche case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET: 1940e381fe9eSBart Van Assche return TMR_LUN_RESET; 1941e381fe9eSBart Van Assche case ISCSI_TM_FUNC_TARGET_WARM_RESET: 1942e381fe9eSBart Van Assche return TMR_TARGET_WARM_RESET; 1943e381fe9eSBart Van Assche case ISCSI_TM_FUNC_TARGET_COLD_RESET: 1944e381fe9eSBart Van Assche return TMR_TARGET_COLD_RESET; 1945e381fe9eSBart Van Assche default: 1946e381fe9eSBart Van Assche return TMR_UNKNOWN; 1947e381fe9eSBart Van Assche } 1948e381fe9eSBart Van Assche } 1949e381fe9eSBart Van Assche 19503e1c81a9SNicholas Bellinger int 19513e1c81a9SNicholas Bellinger iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 1952e48354ceSNicholas Bellinger unsigned char *buf) 1953e48354ceSNicholas Bellinger { 1954e48354ceSNicholas Bellinger struct se_tmr_req *se_tmr; 1955e48354ceSNicholas Bellinger struct iscsi_tmr_req *tmr_req; 1956e48354ceSNicholas Bellinger struct iscsi_tm *hdr; 1957186a9647SNicholas Bellinger int out_of_order_cmdsn = 0, ret; 195859b6986dSBart Van Assche u8 function, tcm_function = TMR_UNKNOWN; 1959e48354ceSNicholas Bellinger 1960e48354ceSNicholas Bellinger hdr = (struct iscsi_tm *) buf; 1961e48354ceSNicholas Bellinger hdr->flags &= ~ISCSI_FLAG_CMD_FINAL; 1962e48354ceSNicholas Bellinger function = hdr->flags; 1963e48354ceSNicholas Bellinger 1964e48354ceSNicholas Bellinger pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:" 1965e48354ceSNicholas Bellinger " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:" 1966e48354ceSNicholas Bellinger " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function, 1967e48354ceSNicholas Bellinger hdr->rtt, hdr->refcmdsn, conn->cid); 1968e48354ceSNicholas Bellinger 1969e48354ceSNicholas Bellinger if ((function != ISCSI_TM_FUNC_ABORT_TASK) && 1970e48354ceSNicholas Bellinger ((function != ISCSI_TM_FUNC_TASK_REASSIGN) && 197166c7db68SChristoph Hellwig hdr->rtt != RESERVED_ITT)) { 1972e48354ceSNicholas Bellinger pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n"); 197366c7db68SChristoph Hellwig hdr->rtt = RESERVED_ITT; 1974e48354ceSNicholas Bellinger } 1975e48354ceSNicholas Bellinger 1976e48354ceSNicholas Bellinger if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) && 1977e48354ceSNicholas Bellinger !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { 1978e48354ceSNicholas Bellinger pr_err("Task Management Request TASK_REASSIGN not" 1979e48354ceSNicholas Bellinger " issued as immediate command, bad iSCSI Initiator" 1980e48354ceSNicholas Bellinger "implementation\n"); 1981ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 1982ba159914SNicholas Bellinger ISCSI_REASON_PROTOCOL_ERROR, buf); 1983e48354ceSNicholas Bellinger } 1984e48354ceSNicholas Bellinger if ((function != ISCSI_TM_FUNC_ABORT_TASK) && 198550e5c87dSChristoph Hellwig be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG) 198650e5c87dSChristoph Hellwig hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG); 1987e48354ceSNicholas Bellinger 1988d28b1169SAndy Grover cmd->data_direction = DMA_NONE; 19893829f381SMarkus Elfring cmd->tmr_req = kzalloc(sizeof(*cmd->tmr_req), GFP_KERNEL); 1990ae072726SNicholas Bellinger if (!cmd->tmr_req) { 1991ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 1992d28b1169SAndy Grover ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1993ba159914SNicholas Bellinger buf); 1994ae072726SNicholas Bellinger } 1995ae072726SNicholas Bellinger 1996ae072726SNicholas Bellinger transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops, 1997ae072726SNicholas Bellinger conn->sess->se_sess, 0, DMA_NONE, 1998ae072726SNicholas Bellinger TCM_SIMPLE_TAG, cmd->sense_buffer + 2); 1999ae072726SNicholas Bellinger 2000ae072726SNicholas Bellinger target_get_sess_cmd(&cmd->se_cmd, true); 2001d28b1169SAndy Grover 2002d28b1169SAndy Grover /* 2003d28b1169SAndy Grover * TASK_REASSIGN for ERL=2 / connection stays inside of 2004d28b1169SAndy Grover * LIO-Target $FABRIC_MOD 2005d28b1169SAndy Grover */ 2006d28b1169SAndy Grover if (function != ISCSI_TM_FUNC_TASK_REASSIGN) { 2007e381fe9eSBart Van Assche tcm_function = iscsit_convert_tmf(function); 2008e381fe9eSBart Van Assche if (tcm_function == TMR_UNKNOWN) { 2009d28b1169SAndy Grover pr_err("Unknown iSCSI TMR Function:" 2010d28b1169SAndy Grover " 0x%02x\n", function); 2011ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 2012ba159914SNicholas Bellinger ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); 2013d28b1169SAndy Grover } 201459b6986dSBart Van Assche } 201559b6986dSBart Van Assche ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, tcm_function, 201659b6986dSBart Van Assche GFP_KERNEL); 2017d28b1169SAndy Grover if (ret < 0) 2018ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 2019ba159914SNicholas Bellinger ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); 2020d28b1169SAndy Grover 2021d28b1169SAndy Grover cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req; 2022d28b1169SAndy Grover 2023e48354ceSNicholas Bellinger cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC; 2024e48354ceSNicholas Bellinger cmd->i_state = ISTATE_SEND_TASKMGTRSP; 2025e48354ceSNicholas Bellinger cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); 2026e48354ceSNicholas Bellinger cmd->init_task_tag = hdr->itt; 2027e48354ceSNicholas Bellinger cmd->targ_xfer_tag = 0xFFFFFFFF; 202850e5c87dSChristoph Hellwig cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); 202950e5c87dSChristoph Hellwig cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); 2030e48354ceSNicholas Bellinger se_tmr = cmd->se_cmd.se_tmr_req; 2031e48354ceSNicholas Bellinger tmr_req = cmd->tmr_req; 2032e48354ceSNicholas Bellinger /* 2033e48354ceSNicholas Bellinger * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN 2034e48354ceSNicholas Bellinger */ 2035e48354ceSNicholas Bellinger if (function != ISCSI_TM_FUNC_TASK_REASSIGN) { 20364f26998aSAndy Grover ret = transport_lookup_tmr_lun(&cmd->se_cmd, 20374f26998aSAndy Grover scsilun_to_int(&hdr->lun)); 2038e48354ceSNicholas Bellinger if (ret < 0) { 2039e48354ceSNicholas Bellinger se_tmr->response = ISCSI_TMF_RSP_NO_LUN; 2040e48354ceSNicholas Bellinger goto attach; 2041e48354ceSNicholas Bellinger } 2042e48354ceSNicholas Bellinger } 2043e48354ceSNicholas Bellinger 2044e48354ceSNicholas Bellinger switch (function) { 2045e48354ceSNicholas Bellinger case ISCSI_TM_FUNC_ABORT_TASK: 2046e48354ceSNicholas Bellinger se_tmr->response = iscsit_tmr_abort_task(cmd, buf); 2047de103c93SChristoph Hellwig if (se_tmr->response) 2048e48354ceSNicholas Bellinger goto attach; 2049e48354ceSNicholas Bellinger break; 2050e48354ceSNicholas Bellinger case ISCSI_TM_FUNC_ABORT_TASK_SET: 2051e48354ceSNicholas Bellinger case ISCSI_TM_FUNC_CLEAR_ACA: 2052e48354ceSNicholas Bellinger case ISCSI_TM_FUNC_CLEAR_TASK_SET: 2053e48354ceSNicholas Bellinger case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET: 2054e48354ceSNicholas Bellinger break; 2055e48354ceSNicholas Bellinger case ISCSI_TM_FUNC_TARGET_WARM_RESET: 2056e48354ceSNicholas Bellinger if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) { 2057e48354ceSNicholas Bellinger se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED; 2058e48354ceSNicholas Bellinger goto attach; 2059e48354ceSNicholas Bellinger } 2060e48354ceSNicholas Bellinger break; 2061e48354ceSNicholas Bellinger case ISCSI_TM_FUNC_TARGET_COLD_RESET: 2062e48354ceSNicholas Bellinger if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) { 2063e48354ceSNicholas Bellinger se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED; 2064e48354ceSNicholas Bellinger goto attach; 2065e48354ceSNicholas Bellinger } 2066e48354ceSNicholas Bellinger break; 2067e48354ceSNicholas Bellinger case ISCSI_TM_FUNC_TASK_REASSIGN: 2068e48354ceSNicholas Bellinger se_tmr->response = iscsit_tmr_task_reassign(cmd, buf); 2069e48354ceSNicholas Bellinger /* 2070e48354ceSNicholas Bellinger * Perform sanity checks on the ExpDataSN only if the 2071e48354ceSNicholas Bellinger * TASK_REASSIGN was successful. 2072e48354ceSNicholas Bellinger */ 2073de103c93SChristoph Hellwig if (se_tmr->response) 2074e48354ceSNicholas Bellinger break; 2075e48354ceSNicholas Bellinger 2076e48354ceSNicholas Bellinger if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0) 2077ba159914SNicholas Bellinger return iscsit_add_reject_cmd(cmd, 2078ba159914SNicholas Bellinger ISCSI_REASON_BOOKMARK_INVALID, buf); 2079e48354ceSNicholas Bellinger break; 2080e48354ceSNicholas Bellinger default: 2081e48354ceSNicholas Bellinger pr_err("Unknown TMR function: 0x%02x, protocol" 2082e48354ceSNicholas Bellinger " error.\n", function); 2083e48354ceSNicholas Bellinger se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED; 2084e48354ceSNicholas Bellinger goto attach; 2085e48354ceSNicholas Bellinger } 2086e48354ceSNicholas Bellinger 2087e48354ceSNicholas Bellinger if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) && 2088e48354ceSNicholas Bellinger (se_tmr->response == ISCSI_TMF_RSP_COMPLETE)) 2089e48354ceSNicholas Bellinger se_tmr->call_transport = 1; 2090e48354ceSNicholas Bellinger attach: 2091e48354ceSNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 20922fbb471eSAndy Grover list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 2093e48354ceSNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 2094e48354ceSNicholas Bellinger 2095e48354ceSNicholas Bellinger if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) { 2096561bf158SNicholas Bellinger int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn); 20973fc9fb13SNicholas Bellinger if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP) { 2098e48354ceSNicholas Bellinger out_of_order_cmdsn = 1; 20993fc9fb13SNicholas Bellinger } else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) { 21003fc9fb13SNicholas Bellinger target_put_sess_cmd(&cmd->se_cmd); 2101e48354ceSNicholas Bellinger return 0; 21023fc9fb13SNicholas Bellinger } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) { 2103ba159914SNicholas Bellinger return -1; 2104e48354ceSNicholas Bellinger } 21053fc9fb13SNicholas Bellinger } 210650e5c87dSChristoph Hellwig iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); 2107e48354ceSNicholas Bellinger 21085a4c8666SNicholas Bellinger if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE)) 2109e48354ceSNicholas Bellinger return 0; 2110e48354ceSNicholas Bellinger /* 2111e48354ceSNicholas Bellinger * Found the referenced task, send to transport for processing. 2112e48354ceSNicholas Bellinger */ 2113e48354ceSNicholas Bellinger if (se_tmr->call_transport) 2114e48354ceSNicholas Bellinger return transport_generic_handle_tmr(&cmd->se_cmd); 2115e48354ceSNicholas Bellinger 2116e48354ceSNicholas Bellinger /* 2117e48354ceSNicholas Bellinger * Could not find the referenced LUN, task, or Task Management 2118e48354ceSNicholas Bellinger * command not authorized or supported. Change state and 2119e48354ceSNicholas Bellinger * let the tx_thread send the response. 2120e48354ceSNicholas Bellinger * 2121e48354ceSNicholas Bellinger * For connection recovery, this is also the default action for 2122e48354ceSNicholas Bellinger * TMR TASK_REASSIGN. 2123e48354ceSNicholas Bellinger */ 2124e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 2125ae072726SNicholas Bellinger target_put_sess_cmd(&cmd->se_cmd); 2126e48354ceSNicholas Bellinger return 0; 2127e48354ceSNicholas Bellinger } 21283e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd); 2129e48354ceSNicholas Bellinger 2130e48354ceSNicholas Bellinger /* #warning FIXME: Support Text Command parameters besides SendTargets */ 213164534aa7SNicholas Bellinger int 213264534aa7SNicholas Bellinger iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 213364534aa7SNicholas Bellinger struct iscsi_text *hdr) 2134e48354ceSNicholas Bellinger { 213564534aa7SNicholas Bellinger u32 payload_length = ntoh24(hdr->dlength); 2136e48354ceSNicholas Bellinger 213721f5aa7eSNicholas Bellinger if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { 2138e48354ceSNicholas Bellinger pr_err("Unable to accept text parameter length: %u" 213921f5aa7eSNicholas Bellinger "greater than MaxXmitDataSegmentLength %u.\n", 214021f5aa7eSNicholas Bellinger payload_length, conn->conn_ops->MaxXmitDataSegmentLength); 2141ba159914SNicholas Bellinger return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, 2142ba159914SNicholas Bellinger (unsigned char *)hdr); 2143e48354ceSNicholas Bellinger } 2144e48354ceSNicholas Bellinger 2145122f8afcSNicholas Bellinger if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) || 2146122f8afcSNicholas Bellinger (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) { 2147122f8afcSNicholas Bellinger pr_err("Multi sequence text commands currently not supported\n"); 2148122f8afcSNicholas Bellinger return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED, 2149122f8afcSNicholas Bellinger (unsigned char *)hdr); 2150122f8afcSNicholas Bellinger } 2151122f8afcSNicholas Bellinger 2152e48354ceSNicholas Bellinger pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x," 2153e48354ceSNicholas Bellinger " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn, 2154e48354ceSNicholas Bellinger hdr->exp_statsn, payload_length); 2155e48354ceSNicholas Bellinger 215664534aa7SNicholas Bellinger cmd->iscsi_opcode = ISCSI_OP_TEXT; 215764534aa7SNicholas Bellinger cmd->i_state = ISTATE_SEND_TEXTRSP; 215864534aa7SNicholas Bellinger cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); 215964534aa7SNicholas Bellinger conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt; 216064534aa7SNicholas Bellinger cmd->targ_xfer_tag = 0xFFFFFFFF; 216164534aa7SNicholas Bellinger cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); 216264534aa7SNicholas Bellinger cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); 216364534aa7SNicholas Bellinger cmd->data_direction = DMA_NONE; 2164ea8dc5b4SVarun Prakash kfree(cmd->text_in_ptr); 2165e4f4e801SSagi Grimberg cmd->text_in_ptr = NULL; 216664534aa7SNicholas Bellinger 216764534aa7SNicholas Bellinger return 0; 216864534aa7SNicholas Bellinger } 216964534aa7SNicholas Bellinger EXPORT_SYMBOL(iscsit_setup_text_cmd); 217064534aa7SNicholas Bellinger 217164534aa7SNicholas Bellinger int 217264534aa7SNicholas Bellinger iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 217364534aa7SNicholas Bellinger struct iscsi_text *hdr) 217464534aa7SNicholas Bellinger { 21759864ca9dSNicholas Bellinger unsigned char *text_in = cmd->text_in_ptr, *text_ptr; 217664534aa7SNicholas Bellinger int cmdsn_ret; 217764534aa7SNicholas Bellinger 21789864ca9dSNicholas Bellinger if (!text_in) { 2179e4f4e801SSagi Grimberg cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt); 2180e4f4e801SSagi Grimberg if (cmd->targ_xfer_tag == 0xFFFFFFFF) { 21819864ca9dSNicholas Bellinger pr_err("Unable to locate text_in buffer for sendtargets" 21829864ca9dSNicholas Bellinger " discovery\n"); 21839864ca9dSNicholas Bellinger goto reject; 21849864ca9dSNicholas Bellinger } 2185e4f4e801SSagi Grimberg goto empty_sendtargets; 2186e4f4e801SSagi Grimberg } 21879864ca9dSNicholas Bellinger if (strncmp("SendTargets", text_in, 11) != 0) { 21889864ca9dSNicholas Bellinger pr_err("Received Text Data that is not" 21899864ca9dSNicholas Bellinger " SendTargets, cannot continue.\n"); 21909864ca9dSNicholas Bellinger goto reject; 21919864ca9dSNicholas Bellinger } 21929864ca9dSNicholas Bellinger text_ptr = strchr(text_in, '='); 21939864ca9dSNicholas Bellinger if (!text_ptr) { 21949864ca9dSNicholas Bellinger pr_err("No \"=\" separator found in Text Data," 21959864ca9dSNicholas Bellinger " cannot continue.\n"); 21969864ca9dSNicholas Bellinger goto reject; 21979864ca9dSNicholas Bellinger } 21989864ca9dSNicholas Bellinger if (!strncmp("=All", text_ptr, 4)) { 21998060b8ddSAndy Grover cmd->cmd_flags |= ICF_SENDTARGETS_ALL; 22006665889cSNicholas Bellinger } else if (!strncmp("=iqn.", text_ptr, 5) || 22016665889cSNicholas Bellinger !strncmp("=eui.", text_ptr, 5)) { 22028060b8ddSAndy Grover cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE; 22039864ca9dSNicholas Bellinger } else { 22049864ca9dSNicholas Bellinger pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr); 22059864ca9dSNicholas Bellinger goto reject; 22069864ca9dSNicholas Bellinger } 22079864ca9dSNicholas Bellinger 220864534aa7SNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 220964534aa7SNicholas Bellinger list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 221064534aa7SNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 221164534aa7SNicholas Bellinger 2212e4f4e801SSagi Grimberg empty_sendtargets: 221364534aa7SNicholas Bellinger iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); 221464534aa7SNicholas Bellinger 221564534aa7SNicholas Bellinger if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) { 2216561bf158SNicholas Bellinger cmdsn_ret = iscsit_sequence_cmd(conn, cmd, 2217561bf158SNicholas Bellinger (unsigned char *)hdr, hdr->cmdsn); 221864534aa7SNicholas Bellinger if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) 2219ba159914SNicholas Bellinger return -1; 2220ba159914SNicholas Bellinger 222164534aa7SNicholas Bellinger return 0; 222264534aa7SNicholas Bellinger } 222364534aa7SNicholas Bellinger 222464534aa7SNicholas Bellinger return iscsit_execute_cmd(cmd, 0); 22259864ca9dSNicholas Bellinger 22269864ca9dSNicholas Bellinger reject: 2227ba159914SNicholas Bellinger return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, 2228ba159914SNicholas Bellinger (unsigned char *)hdr); 222964534aa7SNicholas Bellinger } 223064534aa7SNicholas Bellinger EXPORT_SYMBOL(iscsit_process_text_cmd); 223164534aa7SNicholas Bellinger 223264534aa7SNicholas Bellinger static int 223364534aa7SNicholas Bellinger iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 223464534aa7SNicholas Bellinger unsigned char *buf) 223564534aa7SNicholas Bellinger { 223664534aa7SNicholas Bellinger struct iscsi_text *hdr = (struct iscsi_text *)buf; 223764534aa7SNicholas Bellinger char *text_in = NULL; 223864534aa7SNicholas Bellinger u32 payload_length = ntoh24(hdr->dlength); 223964534aa7SNicholas Bellinger int rx_size, rc; 224064534aa7SNicholas Bellinger 224164534aa7SNicholas Bellinger rc = iscsit_setup_text_cmd(conn, cmd, hdr); 224264534aa7SNicholas Bellinger if (rc < 0) 2243561bf158SNicholas Bellinger return 0; 224464534aa7SNicholas Bellinger 224564534aa7SNicholas Bellinger rx_size = payload_length; 224664534aa7SNicholas Bellinger if (payload_length) { 224764534aa7SNicholas Bellinger u32 checksum = 0, data_crc = 0; 224864534aa7SNicholas Bellinger u32 padding = 0, pad_bytes = 0; 224964534aa7SNicholas Bellinger int niov = 0, rx_got; 225064534aa7SNicholas Bellinger struct kvec iov[3]; 225164534aa7SNicholas Bellinger 225264534aa7SNicholas Bellinger text_in = kzalloc(payload_length, GFP_KERNEL); 2253c46e22f1SMarkus Elfring if (!text_in) 225464534aa7SNicholas Bellinger goto reject; 2255c46e22f1SMarkus Elfring 22569864ca9dSNicholas Bellinger cmd->text_in_ptr = text_in; 2257e48354ceSNicholas Bellinger 2258e48354ceSNicholas Bellinger memset(iov, 0, 3 * sizeof(struct kvec)); 2259e48354ceSNicholas Bellinger iov[niov].iov_base = text_in; 226064534aa7SNicholas Bellinger iov[niov++].iov_len = payload_length; 2261e48354ceSNicholas Bellinger 2262e48354ceSNicholas Bellinger padding = ((-payload_length) & 3); 2263e48354ceSNicholas Bellinger if (padding != 0) { 226476f1928eSNicholas Bellinger iov[niov].iov_base = &pad_bytes; 2265e48354ceSNicholas Bellinger iov[niov++].iov_len = padding; 2266e48354ceSNicholas Bellinger rx_size += padding; 2267e48354ceSNicholas Bellinger pr_debug("Receiving %u additional bytes" 2268e48354ceSNicholas Bellinger " for padding.\n", padding); 2269e48354ceSNicholas Bellinger } 2270e48354ceSNicholas Bellinger if (conn->conn_ops->DataDigest) { 2271e48354ceSNicholas Bellinger iov[niov].iov_base = &checksum; 2272e48354ceSNicholas Bellinger iov[niov++].iov_len = ISCSI_CRC_LEN; 2273e48354ceSNicholas Bellinger rx_size += ISCSI_CRC_LEN; 2274e48354ceSNicholas Bellinger } 2275e48354ceSNicholas Bellinger 2276e48354ceSNicholas Bellinger rx_got = rx_data(conn, &iov[0], niov, rx_size); 227764534aa7SNicholas Bellinger if (rx_got != rx_size) 227864534aa7SNicholas Bellinger goto reject; 2279e48354ceSNicholas Bellinger 2280e48354ceSNicholas Bellinger if (conn->conn_ops->DataDigest) { 2281e1dfb21fSBart Van Assche iscsit_do_crypto_hash_buf(conn->conn_rx_hash, text_in, 2282e1dfb21fSBart Van Assche payload_length, padding, 2283e1dfb21fSBart Van Assche &pad_bytes, &data_crc); 2284e48354ceSNicholas Bellinger 2285e48354ceSNicholas Bellinger if (checksum != data_crc) { 2286e48354ceSNicholas Bellinger pr_err("Text data CRC32C DataDigest" 2287e48354ceSNicholas Bellinger " 0x%08x does not match computed" 2288e48354ceSNicholas Bellinger " 0x%08x\n", checksum, data_crc); 2289e48354ceSNicholas Bellinger if (!conn->sess->sess_ops->ErrorRecoveryLevel) { 2290e48354ceSNicholas Bellinger pr_err("Unable to recover from" 2291e48354ceSNicholas Bellinger " Text Data digest failure while in" 2292e48354ceSNicholas Bellinger " ERL=0.\n"); 229364534aa7SNicholas Bellinger goto reject; 2294e48354ceSNicholas Bellinger } else { 2295e48354ceSNicholas Bellinger /* 2296e48354ceSNicholas Bellinger * Silently drop this PDU and let the 2297e48354ceSNicholas Bellinger * initiator plug the CmdSN gap. 2298e48354ceSNicholas Bellinger */ 2299e48354ceSNicholas Bellinger pr_debug("Dropping Text" 2300e48354ceSNicholas Bellinger " Command CmdSN: 0x%08x due to" 2301e48354ceSNicholas Bellinger " DataCRC error.\n", hdr->cmdsn); 2302e48354ceSNicholas Bellinger kfree(text_in); 2303e48354ceSNicholas Bellinger return 0; 2304e48354ceSNicholas Bellinger } 2305e48354ceSNicholas Bellinger } else { 2306e48354ceSNicholas Bellinger pr_debug("Got CRC32C DataDigest" 2307e48354ceSNicholas Bellinger " 0x%08x for %u bytes of text data.\n", 230864534aa7SNicholas Bellinger checksum, payload_length); 2309e48354ceSNicholas Bellinger } 2310e48354ceSNicholas Bellinger } 231164534aa7SNicholas Bellinger text_in[payload_length - 1] = '\0'; 2312e48354ceSNicholas Bellinger pr_debug("Successfully read %d bytes of text" 231364534aa7SNicholas Bellinger " data.\n", payload_length); 2314e48354ceSNicholas Bellinger } 2315e48354ceSNicholas Bellinger 231664534aa7SNicholas Bellinger return iscsit_process_text_cmd(conn, cmd, hdr); 2317e48354ceSNicholas Bellinger 231864534aa7SNicholas Bellinger reject: 23199864ca9dSNicholas Bellinger kfree(cmd->text_in_ptr); 23209864ca9dSNicholas Bellinger cmd->text_in_ptr = NULL; 2321ba159914SNicholas Bellinger return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf); 2322e48354ceSNicholas Bellinger } 2323e48354ceSNicholas Bellinger 2324e48354ceSNicholas Bellinger int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 2325e48354ceSNicholas Bellinger { 2326e48354ceSNicholas Bellinger struct iscsi_conn *conn_p; 2327e48354ceSNicholas Bellinger struct iscsi_session *sess = conn->sess; 2328e48354ceSNicholas Bellinger 2329e48354ceSNicholas Bellinger pr_debug("Received logout request CLOSESESSION on CID: %hu" 2330e48354ceSNicholas Bellinger " for SID: %u.\n", conn->cid, conn->sess->sid); 2331e48354ceSNicholas Bellinger 2332e48354ceSNicholas Bellinger atomic_set(&sess->session_logout, 1); 2333e48354ceSNicholas Bellinger atomic_set(&conn->conn_logout_remove, 1); 2334e48354ceSNicholas Bellinger conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION; 2335e48354ceSNicholas Bellinger 2336e48354ceSNicholas Bellinger iscsit_inc_conn_usage_count(conn); 2337e48354ceSNicholas Bellinger iscsit_inc_session_usage_count(sess); 2338e48354ceSNicholas Bellinger 2339e48354ceSNicholas Bellinger spin_lock_bh(&sess->conn_lock); 2340e48354ceSNicholas Bellinger list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) { 2341e48354ceSNicholas Bellinger if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN) 2342e48354ceSNicholas Bellinger continue; 2343e48354ceSNicholas Bellinger 2344e48354ceSNicholas Bellinger pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n"); 2345e48354ceSNicholas Bellinger conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT; 2346e48354ceSNicholas Bellinger } 2347e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 2348e48354ceSNicholas Bellinger 2349e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 2350e48354ceSNicholas Bellinger 2351e48354ceSNicholas Bellinger return 0; 2352e48354ceSNicholas Bellinger } 2353e48354ceSNicholas Bellinger 2354e48354ceSNicholas Bellinger int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 2355e48354ceSNicholas Bellinger { 2356e48354ceSNicholas Bellinger struct iscsi_conn *l_conn; 2357e48354ceSNicholas Bellinger struct iscsi_session *sess = conn->sess; 2358e48354ceSNicholas Bellinger 2359e48354ceSNicholas Bellinger pr_debug("Received logout request CLOSECONNECTION for CID:" 2360e48354ceSNicholas Bellinger " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid); 2361e48354ceSNicholas Bellinger 2362e48354ceSNicholas Bellinger /* 2363e48354ceSNicholas Bellinger * A Logout Request with a CLOSECONNECTION reason code for a CID 2364e48354ceSNicholas Bellinger * can arrive on a connection with a differing CID. 2365e48354ceSNicholas Bellinger */ 2366e48354ceSNicholas Bellinger if (conn->cid == cmd->logout_cid) { 2367e48354ceSNicholas Bellinger spin_lock_bh(&conn->state_lock); 2368e48354ceSNicholas Bellinger pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n"); 2369e48354ceSNicholas Bellinger conn->conn_state = TARG_CONN_STATE_IN_LOGOUT; 2370e48354ceSNicholas Bellinger 2371e48354ceSNicholas Bellinger atomic_set(&conn->conn_logout_remove, 1); 2372e48354ceSNicholas Bellinger conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION; 2373e48354ceSNicholas Bellinger iscsit_inc_conn_usage_count(conn); 2374e48354ceSNicholas Bellinger 2375e48354ceSNicholas Bellinger spin_unlock_bh(&conn->state_lock); 2376e48354ceSNicholas Bellinger } else { 2377e48354ceSNicholas Bellinger /* 2378e48354ceSNicholas Bellinger * Handle all different cid CLOSECONNECTION requests in 2379e48354ceSNicholas Bellinger * iscsit_logout_post_handler_diffcid() as to give enough 2380e48354ceSNicholas Bellinger * time for any non immediate command's CmdSN to be 2381e48354ceSNicholas Bellinger * acknowledged on the connection in question. 2382e48354ceSNicholas Bellinger * 2383e48354ceSNicholas Bellinger * Here we simply make sure the CID is still around. 2384e48354ceSNicholas Bellinger */ 2385e48354ceSNicholas Bellinger l_conn = iscsit_get_conn_from_cid(sess, 2386e48354ceSNicholas Bellinger cmd->logout_cid); 2387e48354ceSNicholas Bellinger if (!l_conn) { 2388e48354ceSNicholas Bellinger cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND; 2389e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, conn, 2390e48354ceSNicholas Bellinger cmd->i_state); 2391e48354ceSNicholas Bellinger return 0; 2392e48354ceSNicholas Bellinger } 2393e48354ceSNicholas Bellinger 2394e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(l_conn); 2395e48354ceSNicholas Bellinger } 2396e48354ceSNicholas Bellinger 2397e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 2398e48354ceSNicholas Bellinger 2399e48354ceSNicholas Bellinger return 0; 2400e48354ceSNicholas Bellinger } 2401e48354ceSNicholas Bellinger 2402e48354ceSNicholas Bellinger int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 2403e48354ceSNicholas Bellinger { 2404e48354ceSNicholas Bellinger struct iscsi_session *sess = conn->sess; 2405e48354ceSNicholas Bellinger 2406e48354ceSNicholas Bellinger pr_debug("Received explicit REMOVECONNFORRECOVERY logout for" 2407e48354ceSNicholas Bellinger " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid); 2408e48354ceSNicholas Bellinger 2409e48354ceSNicholas Bellinger if (sess->sess_ops->ErrorRecoveryLevel != 2) { 2410e48354ceSNicholas Bellinger pr_err("Received Logout Request REMOVECONNFORRECOVERY" 2411e48354ceSNicholas Bellinger " while ERL!=2.\n"); 2412e48354ceSNicholas Bellinger cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED; 2413e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 2414e48354ceSNicholas Bellinger return 0; 2415e48354ceSNicholas Bellinger } 2416e48354ceSNicholas Bellinger 2417e48354ceSNicholas Bellinger if (conn->cid == cmd->logout_cid) { 2418e48354ceSNicholas Bellinger pr_err("Received Logout Request REMOVECONNFORRECOVERY" 2419e48354ceSNicholas Bellinger " with CID: %hu on CID: %hu, implementation error.\n", 2420e48354ceSNicholas Bellinger cmd->logout_cid, conn->cid); 2421e48354ceSNicholas Bellinger cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED; 2422e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 2423e48354ceSNicholas Bellinger return 0; 2424e48354ceSNicholas Bellinger } 2425e48354ceSNicholas Bellinger 2426e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 2427e48354ceSNicholas Bellinger 2428e48354ceSNicholas Bellinger return 0; 2429e48354ceSNicholas Bellinger } 2430e48354ceSNicholas Bellinger 24313e1c81a9SNicholas Bellinger int 24323e1c81a9SNicholas Bellinger iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 2433e48354ceSNicholas Bellinger unsigned char *buf) 2434e48354ceSNicholas Bellinger { 2435e48354ceSNicholas Bellinger int cmdsn_ret, logout_remove = 0; 2436e48354ceSNicholas Bellinger u8 reason_code = 0; 2437e48354ceSNicholas Bellinger struct iscsi_logout *hdr; 2438e48354ceSNicholas Bellinger struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn); 2439e48354ceSNicholas Bellinger 2440e48354ceSNicholas Bellinger hdr = (struct iscsi_logout *) buf; 2441e48354ceSNicholas Bellinger reason_code = (hdr->flags & 0x7f); 2442e48354ceSNicholas Bellinger 2443e48354ceSNicholas Bellinger if (tiqn) { 2444e48354ceSNicholas Bellinger spin_lock(&tiqn->logout_stats.lock); 2445e48354ceSNicholas Bellinger if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) 2446e48354ceSNicholas Bellinger tiqn->logout_stats.normal_logouts++; 2447e48354ceSNicholas Bellinger else 2448e48354ceSNicholas Bellinger tiqn->logout_stats.abnormal_logouts++; 2449e48354ceSNicholas Bellinger spin_unlock(&tiqn->logout_stats.lock); 2450e48354ceSNicholas Bellinger } 2451e48354ceSNicholas Bellinger 2452e48354ceSNicholas Bellinger pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x" 2453e48354ceSNicholas Bellinger " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n", 2454e48354ceSNicholas Bellinger hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code, 2455e48354ceSNicholas Bellinger hdr->cid, conn->cid); 2456e48354ceSNicholas Bellinger 2457e48354ceSNicholas Bellinger if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) { 2458e48354ceSNicholas Bellinger pr_err("Received logout request on connection that" 2459e48354ceSNicholas Bellinger " is not in logged in state, ignoring request.\n"); 2460aafc9d15SNicholas Bellinger iscsit_free_cmd(cmd, false); 2461e48354ceSNicholas Bellinger return 0; 2462e48354ceSNicholas Bellinger } 2463e48354ceSNicholas Bellinger 2464e48354ceSNicholas Bellinger cmd->iscsi_opcode = ISCSI_OP_LOGOUT; 2465e48354ceSNicholas Bellinger cmd->i_state = ISTATE_SEND_LOGOUTRSP; 2466e48354ceSNicholas Bellinger cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); 2467e48354ceSNicholas Bellinger conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt; 2468e48354ceSNicholas Bellinger cmd->targ_xfer_tag = 0xFFFFFFFF; 246950e5c87dSChristoph Hellwig cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); 247050e5c87dSChristoph Hellwig cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); 247150e5c87dSChristoph Hellwig cmd->logout_cid = be16_to_cpu(hdr->cid); 2472e48354ceSNicholas Bellinger cmd->logout_reason = reason_code; 2473e48354ceSNicholas Bellinger cmd->data_direction = DMA_NONE; 2474e48354ceSNicholas Bellinger 2475e48354ceSNicholas Bellinger /* 2476e48354ceSNicholas Bellinger * We need to sleep in these cases (by returning 1) until the Logout 2477e48354ceSNicholas Bellinger * Response gets sent in the tx thread. 2478e48354ceSNicholas Bellinger */ 2479e48354ceSNicholas Bellinger if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) || 2480e48354ceSNicholas Bellinger ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) && 248150e5c87dSChristoph Hellwig be16_to_cpu(hdr->cid) == conn->cid)) 2482e48354ceSNicholas Bellinger logout_remove = 1; 2483e48354ceSNicholas Bellinger 2484e48354ceSNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 24852fbb471eSAndy Grover list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 2486e48354ceSNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 2487e48354ceSNicholas Bellinger 2488e48354ceSNicholas Bellinger if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY) 248950e5c87dSChristoph Hellwig iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); 2490e48354ceSNicholas Bellinger 2491e48354ceSNicholas Bellinger /* 2492e48354ceSNicholas Bellinger * Immediate commands are executed, well, immediately. 2493e48354ceSNicholas Bellinger * Non-Immediate Logout Commands are executed in CmdSN order. 2494e48354ceSNicholas Bellinger */ 2495c6037cc5SAndy Grover if (cmd->immediate_cmd) { 2496e48354ceSNicholas Bellinger int ret = iscsit_execute_cmd(cmd, 0); 2497e48354ceSNicholas Bellinger 2498e48354ceSNicholas Bellinger if (ret < 0) 2499e48354ceSNicholas Bellinger return ret; 2500e48354ceSNicholas Bellinger } else { 2501561bf158SNicholas Bellinger cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn); 2502ba159914SNicholas Bellinger if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) 2503e48354ceSNicholas Bellinger logout_remove = 0; 2504ba159914SNicholas Bellinger else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) 2505ba159914SNicholas Bellinger return -1; 2506e48354ceSNicholas Bellinger } 2507e48354ceSNicholas Bellinger 2508e48354ceSNicholas Bellinger return logout_remove; 2509e48354ceSNicholas Bellinger } 25103e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_handle_logout_cmd); 2511e48354ceSNicholas Bellinger 2512d2faaefbSVarun Prakash int iscsit_handle_snack( 2513e48354ceSNicholas Bellinger struct iscsi_conn *conn, 2514e48354ceSNicholas Bellinger unsigned char *buf) 2515e48354ceSNicholas Bellinger { 2516e48354ceSNicholas Bellinger struct iscsi_snack *hdr; 2517e48354ceSNicholas Bellinger 2518e48354ceSNicholas Bellinger hdr = (struct iscsi_snack *) buf; 2519e48354ceSNicholas Bellinger hdr->flags &= ~ISCSI_FLAG_CMD_FINAL; 2520e48354ceSNicholas Bellinger 2521e48354ceSNicholas Bellinger pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:" 2522e48354ceSNicholas Bellinger " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x," 2523e48354ceSNicholas Bellinger " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags, 2524e48354ceSNicholas Bellinger hdr->begrun, hdr->runlength, conn->cid); 2525e48354ceSNicholas Bellinger 2526e48354ceSNicholas Bellinger if (!conn->sess->sess_ops->ErrorRecoveryLevel) { 2527e48354ceSNicholas Bellinger pr_err("Initiator sent SNACK request while in" 2528e48354ceSNicholas Bellinger " ErrorRecoveryLevel=0.\n"); 2529ba159914SNicholas Bellinger return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 2530ba159914SNicholas Bellinger buf); 2531e48354ceSNicholas Bellinger } 2532e48354ceSNicholas Bellinger /* 2533e48354ceSNicholas Bellinger * SNACK_DATA and SNACK_R2T are both 0, so check which function to 2534e48354ceSNicholas Bellinger * call from inside iscsi_send_recovery_datain_or_r2t(). 2535e48354ceSNicholas Bellinger */ 2536e48354ceSNicholas Bellinger switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) { 2537e48354ceSNicholas Bellinger case 0: 2538e48354ceSNicholas Bellinger return iscsit_handle_recovery_datain_or_r2t(conn, buf, 253950e5c87dSChristoph Hellwig hdr->itt, 254050e5c87dSChristoph Hellwig be32_to_cpu(hdr->ttt), 254150e5c87dSChristoph Hellwig be32_to_cpu(hdr->begrun), 254250e5c87dSChristoph Hellwig be32_to_cpu(hdr->runlength)); 2543e48354ceSNicholas Bellinger case ISCSI_FLAG_SNACK_TYPE_STATUS: 254450e5c87dSChristoph Hellwig return iscsit_handle_status_snack(conn, hdr->itt, 254550e5c87dSChristoph Hellwig be32_to_cpu(hdr->ttt), 254650e5c87dSChristoph Hellwig be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength)); 2547e48354ceSNicholas Bellinger case ISCSI_FLAG_SNACK_TYPE_DATA_ACK: 254850e5c87dSChristoph Hellwig return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt), 254950e5c87dSChristoph Hellwig be32_to_cpu(hdr->begrun), 255050e5c87dSChristoph Hellwig be32_to_cpu(hdr->runlength)); 2551e48354ceSNicholas Bellinger case ISCSI_FLAG_SNACK_TYPE_RDATA: 2552e48354ceSNicholas Bellinger /* FIXME: Support R-Data SNACK */ 2553e48354ceSNicholas Bellinger pr_err("R-Data SNACK Not Supported.\n"); 2554ba159914SNicholas Bellinger return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 2555ba159914SNicholas Bellinger buf); 2556e48354ceSNicholas Bellinger default: 2557e48354ceSNicholas Bellinger pr_err("Unknown SNACK type 0x%02x, protocol" 2558e48354ceSNicholas Bellinger " error.\n", hdr->flags & 0x0f); 2559ba159914SNicholas Bellinger return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 2560ba159914SNicholas Bellinger buf); 2561e48354ceSNicholas Bellinger } 2562e48354ceSNicholas Bellinger 2563e48354ceSNicholas Bellinger return 0; 2564e48354ceSNicholas Bellinger } 2565d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_handle_snack); 2566e48354ceSNicholas Bellinger 2567e48354ceSNicholas Bellinger static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn) 2568e48354ceSNicholas Bellinger { 2569e48354ceSNicholas Bellinger if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) || 2570e48354ceSNicholas Bellinger (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) { 2571e48354ceSNicholas Bellinger wait_for_completion_interruptible_timeout( 2572e48354ceSNicholas Bellinger &conn->rx_half_close_comp, 2573e48354ceSNicholas Bellinger ISCSI_RX_THREAD_TCP_TIMEOUT * HZ); 2574e48354ceSNicholas Bellinger } 2575e48354ceSNicholas Bellinger } 2576e48354ceSNicholas Bellinger 2577e48354ceSNicholas Bellinger static int iscsit_handle_immediate_data( 2578e48354ceSNicholas Bellinger struct iscsi_cmd *cmd, 25793e1c81a9SNicholas Bellinger struct iscsi_scsi_req *hdr, 2580e48354ceSNicholas Bellinger u32 length) 2581e48354ceSNicholas Bellinger { 2582e48354ceSNicholas Bellinger int iov_ret, rx_got = 0, rx_size = 0; 2583e48354ceSNicholas Bellinger u32 checksum, iov_count = 0, padding = 0; 2584e48354ceSNicholas Bellinger struct iscsi_conn *conn = cmd->conn; 2585e48354ceSNicholas Bellinger struct kvec *iov; 2586e48354ceSNicholas Bellinger 2587e48354ceSNicholas Bellinger iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length); 2588e48354ceSNicholas Bellinger if (iov_ret < 0) 2589e48354ceSNicholas Bellinger return IMMEDIATE_DATA_CANNOT_RECOVER; 2590e48354ceSNicholas Bellinger 2591e48354ceSNicholas Bellinger rx_size = length; 2592e48354ceSNicholas Bellinger iov_count = iov_ret; 2593e48354ceSNicholas Bellinger iov = &cmd->iov_data[0]; 2594e48354ceSNicholas Bellinger 2595e48354ceSNicholas Bellinger padding = ((-length) & 3); 2596e48354ceSNicholas Bellinger if (padding != 0) { 2597e48354ceSNicholas Bellinger iov[iov_count].iov_base = cmd->pad_bytes; 2598e48354ceSNicholas Bellinger iov[iov_count++].iov_len = padding; 2599e48354ceSNicholas Bellinger rx_size += padding; 2600e48354ceSNicholas Bellinger } 2601e48354ceSNicholas Bellinger 2602e48354ceSNicholas Bellinger if (conn->conn_ops->DataDigest) { 2603e48354ceSNicholas Bellinger iov[iov_count].iov_base = &checksum; 2604e48354ceSNicholas Bellinger iov[iov_count++].iov_len = ISCSI_CRC_LEN; 2605e48354ceSNicholas Bellinger rx_size += ISCSI_CRC_LEN; 2606e48354ceSNicholas Bellinger } 2607e48354ceSNicholas Bellinger 2608e48354ceSNicholas Bellinger rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size); 2609e48354ceSNicholas Bellinger 2610e48354ceSNicholas Bellinger iscsit_unmap_iovec(cmd); 2611e48354ceSNicholas Bellinger 2612e48354ceSNicholas Bellinger if (rx_got != rx_size) { 2613e48354ceSNicholas Bellinger iscsit_rx_thread_wait_for_tcp(conn); 2614e48354ceSNicholas Bellinger return IMMEDIATE_DATA_CANNOT_RECOVER; 2615e48354ceSNicholas Bellinger } 2616e48354ceSNicholas Bellinger 2617e48354ceSNicholas Bellinger if (conn->conn_ops->DataDigest) { 2618e48354ceSNicholas Bellinger u32 data_crc; 2619e48354ceSNicholas Bellinger 262069110e3cSHerbert Xu data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd, 2621e48354ceSNicholas Bellinger cmd->write_data_done, length, padding, 2622e48354ceSNicholas Bellinger cmd->pad_bytes); 2623e48354ceSNicholas Bellinger 2624e48354ceSNicholas Bellinger if (checksum != data_crc) { 2625e48354ceSNicholas Bellinger pr_err("ImmediateData CRC32C DataDigest 0x%08x" 2626e48354ceSNicholas Bellinger " does not match computed 0x%08x\n", checksum, 2627e48354ceSNicholas Bellinger data_crc); 2628e48354ceSNicholas Bellinger 2629e48354ceSNicholas Bellinger if (!conn->sess->sess_ops->ErrorRecoveryLevel) { 2630e48354ceSNicholas Bellinger pr_err("Unable to recover from" 2631e48354ceSNicholas Bellinger " Immediate Data digest failure while" 2632e48354ceSNicholas Bellinger " in ERL=0.\n"); 2633ba159914SNicholas Bellinger iscsit_reject_cmd(cmd, 2634e48354ceSNicholas Bellinger ISCSI_REASON_DATA_DIGEST_ERROR, 2635ba159914SNicholas Bellinger (unsigned char *)hdr); 2636e48354ceSNicholas Bellinger return IMMEDIATE_DATA_CANNOT_RECOVER; 2637e48354ceSNicholas Bellinger } else { 2638ba159914SNicholas Bellinger iscsit_reject_cmd(cmd, 2639e48354ceSNicholas Bellinger ISCSI_REASON_DATA_DIGEST_ERROR, 2640ba159914SNicholas Bellinger (unsigned char *)hdr); 2641e48354ceSNicholas Bellinger return IMMEDIATE_DATA_ERL1_CRC_FAILURE; 2642e48354ceSNicholas Bellinger } 2643e48354ceSNicholas Bellinger } else { 2644e48354ceSNicholas Bellinger pr_debug("Got CRC32C DataDigest 0x%08x for" 2645e48354ceSNicholas Bellinger " %u bytes of Immediate Data\n", checksum, 2646e48354ceSNicholas Bellinger length); 2647e48354ceSNicholas Bellinger } 2648e48354ceSNicholas Bellinger } 2649e48354ceSNicholas Bellinger 2650e48354ceSNicholas Bellinger cmd->write_data_done += length; 2651e48354ceSNicholas Bellinger 2652ebf1d95cSAndy Grover if (cmd->write_data_done == cmd->se_cmd.data_length) { 2653e48354ceSNicholas Bellinger spin_lock_bh(&cmd->istate_lock); 2654e48354ceSNicholas Bellinger cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT; 2655e48354ceSNicholas Bellinger cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT; 2656e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock); 2657e48354ceSNicholas Bellinger } 2658e48354ceSNicholas Bellinger 2659e48354ceSNicholas Bellinger return IMMEDIATE_DATA_NORMAL_OPERATION; 2660e48354ceSNicholas Bellinger } 2661e48354ceSNicholas Bellinger 2662e48354ceSNicholas Bellinger /* 2663e48354ceSNicholas Bellinger * Called with sess->conn_lock held. 2664e48354ceSNicholas Bellinger */ 2665e48354ceSNicholas Bellinger /* #warning iscsi_build_conn_drop_async_message() only sends out on connections 2666e48354ceSNicholas Bellinger with active network interface */ 2667e48354ceSNicholas Bellinger static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn) 2668e48354ceSNicholas Bellinger { 2669e48354ceSNicholas Bellinger struct iscsi_cmd *cmd; 2670e48354ceSNicholas Bellinger struct iscsi_conn *conn_p; 2671d444edc6SNicholas Bellinger bool found = false; 2672e48354ceSNicholas Bellinger 2673e48354ceSNicholas Bellinger /* 2674e48354ceSNicholas Bellinger * Only send a Asynchronous Message on connections whos network 2675e48354ceSNicholas Bellinger * interface is still functional. 2676e48354ceSNicholas Bellinger */ 2677e48354ceSNicholas Bellinger list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) { 2678e48354ceSNicholas Bellinger if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) { 2679e48354ceSNicholas Bellinger iscsit_inc_conn_usage_count(conn_p); 2680d444edc6SNicholas Bellinger found = true; 2681e48354ceSNicholas Bellinger break; 2682e48354ceSNicholas Bellinger } 2683e48354ceSNicholas Bellinger } 2684e48354ceSNicholas Bellinger 2685d444edc6SNicholas Bellinger if (!found) 2686e48354ceSNicholas Bellinger return; 2687e48354ceSNicholas Bellinger 2688676687c6SNicholas Bellinger cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING); 2689e48354ceSNicholas Bellinger if (!cmd) { 2690e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(conn_p); 2691e48354ceSNicholas Bellinger return; 2692e48354ceSNicholas Bellinger } 2693e48354ceSNicholas Bellinger 2694e48354ceSNicholas Bellinger cmd->logout_cid = conn->cid; 2695e48354ceSNicholas Bellinger cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT; 2696e48354ceSNicholas Bellinger cmd->i_state = ISTATE_SEND_ASYNCMSG; 2697e48354ceSNicholas Bellinger 2698e48354ceSNicholas Bellinger spin_lock_bh(&conn_p->cmd_lock); 26992fbb471eSAndy Grover list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list); 2700e48354ceSNicholas Bellinger spin_unlock_bh(&conn_p->cmd_lock); 2701e48354ceSNicholas Bellinger 2702e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state); 2703e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(conn_p); 2704e48354ceSNicholas Bellinger } 2705e48354ceSNicholas Bellinger 2706e48354ceSNicholas Bellinger static int iscsit_send_conn_drop_async_message( 2707e48354ceSNicholas Bellinger struct iscsi_cmd *cmd, 2708e48354ceSNicholas Bellinger struct iscsi_conn *conn) 2709e48354ceSNicholas Bellinger { 2710e48354ceSNicholas Bellinger struct iscsi_async *hdr; 2711e48354ceSNicholas Bellinger 2712e48354ceSNicholas Bellinger cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT; 2713e48354ceSNicholas Bellinger 2714e48354ceSNicholas Bellinger hdr = (struct iscsi_async *) cmd->pdu; 2715e48354ceSNicholas Bellinger hdr->opcode = ISCSI_OP_ASYNC_EVENT; 2716e48354ceSNicholas Bellinger hdr->flags = ISCSI_FLAG_CMD_FINAL; 271766c7db68SChristoph Hellwig cmd->init_task_tag = RESERVED_ITT; 2718e48354ceSNicholas Bellinger cmd->targ_xfer_tag = 0xFFFFFFFF; 2719e48354ceSNicholas Bellinger put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]); 2720e48354ceSNicholas Bellinger cmd->stat_sn = conn->stat_sn++; 2721e48354ceSNicholas Bellinger hdr->statsn = cpu_to_be32(cmd->stat_sn); 2722e48354ceSNicholas Bellinger hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 2723109e2381SRoland Dreier hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn)); 2724e48354ceSNicholas Bellinger hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION; 2725e48354ceSNicholas Bellinger hdr->param1 = cpu_to_be16(cmd->logout_cid); 2726e48354ceSNicholas Bellinger hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait); 2727e48354ceSNicholas Bellinger hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain); 2728e48354ceSNicholas Bellinger 2729e48354ceSNicholas Bellinger pr_debug("Sending Connection Dropped Async Message StatSN:" 2730e48354ceSNicholas Bellinger " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn, 2731e48354ceSNicholas Bellinger cmd->logout_cid, conn->cid); 27322854bb23SVarun Prakash 27332854bb23SVarun Prakash return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0); 2734e48354ceSNicholas Bellinger } 2735e48354ceSNicholas Bellinger 27366f3c0e69SAndy Grover static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn) 27376f3c0e69SAndy Grover { 27386f3c0e69SAndy Grover if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) || 27396f3c0e69SAndy Grover (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) { 27406f3c0e69SAndy Grover wait_for_completion_interruptible_timeout( 27416f3c0e69SAndy Grover &conn->tx_half_close_comp, 27426f3c0e69SAndy Grover ISCSI_TX_THREAD_TCP_TIMEOUT * HZ); 27436f3c0e69SAndy Grover } 27446f3c0e69SAndy Grover } 27456f3c0e69SAndy Grover 2746d2faaefbSVarun Prakash void 27472ec5a8c1SNicholas Bellinger iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 27482ec5a8c1SNicholas Bellinger struct iscsi_datain *datain, struct iscsi_data_rsp *hdr, 27492ec5a8c1SNicholas Bellinger bool set_statsn) 2750e48354ceSNicholas Bellinger { 27512ec5a8c1SNicholas Bellinger hdr->opcode = ISCSI_OP_SCSI_DATA_IN; 27522ec5a8c1SNicholas Bellinger hdr->flags = datain->flags; 27532ec5a8c1SNicholas Bellinger if (hdr->flags & ISCSI_FLAG_DATA_STATUS) { 27542ec5a8c1SNicholas Bellinger if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) { 27552ec5a8c1SNicholas Bellinger hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW; 27562ec5a8c1SNicholas Bellinger hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); 27572ec5a8c1SNicholas Bellinger } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) { 27582ec5a8c1SNicholas Bellinger hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW; 27592ec5a8c1SNicholas Bellinger hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); 27602ec5a8c1SNicholas Bellinger } 27612ec5a8c1SNicholas Bellinger } 27622ec5a8c1SNicholas Bellinger hton24(hdr->dlength, datain->length); 27632ec5a8c1SNicholas Bellinger if (hdr->flags & ISCSI_FLAG_DATA_ACK) 27642ec5a8c1SNicholas Bellinger int_to_scsilun(cmd->se_cmd.orig_fe_lun, 27652ec5a8c1SNicholas Bellinger (struct scsi_lun *)&hdr->lun); 27662ec5a8c1SNicholas Bellinger else 27672ec5a8c1SNicholas Bellinger put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun); 27682ec5a8c1SNicholas Bellinger 27692ec5a8c1SNicholas Bellinger hdr->itt = cmd->init_task_tag; 27702ec5a8c1SNicholas Bellinger 27712ec5a8c1SNicholas Bellinger if (hdr->flags & ISCSI_FLAG_DATA_ACK) 27722ec5a8c1SNicholas Bellinger hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag); 27732ec5a8c1SNicholas Bellinger else 27742ec5a8c1SNicholas Bellinger hdr->ttt = cpu_to_be32(0xFFFFFFFF); 27752ec5a8c1SNicholas Bellinger if (set_statsn) 27762ec5a8c1SNicholas Bellinger hdr->statsn = cpu_to_be32(cmd->stat_sn); 27772ec5a8c1SNicholas Bellinger else 27782ec5a8c1SNicholas Bellinger hdr->statsn = cpu_to_be32(0xFFFFFFFF); 27792ec5a8c1SNicholas Bellinger 27802ec5a8c1SNicholas Bellinger hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 2781109e2381SRoland Dreier hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn)); 27822ec5a8c1SNicholas Bellinger hdr->datasn = cpu_to_be32(datain->data_sn); 27832ec5a8c1SNicholas Bellinger hdr->offset = cpu_to_be32(datain->offset); 27842ec5a8c1SNicholas Bellinger 27852ec5a8c1SNicholas Bellinger pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x," 27862ec5a8c1SNicholas Bellinger " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n", 27872ec5a8c1SNicholas Bellinger cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn), 27882ec5a8c1SNicholas Bellinger ntohl(hdr->offset), datain->length, conn->cid); 27892ec5a8c1SNicholas Bellinger } 2790d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_build_datain_pdu); 27912ec5a8c1SNicholas Bellinger 27922ec5a8c1SNicholas Bellinger static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 27932ec5a8c1SNicholas Bellinger { 27942ec5a8c1SNicholas Bellinger struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0]; 2795e48354ceSNicholas Bellinger struct iscsi_datain datain; 2796e48354ceSNicholas Bellinger struct iscsi_datain_req *dr; 27972854bb23SVarun Prakash int eodr = 0, ret; 27982ec5a8c1SNicholas Bellinger bool set_statsn = false; 2799e48354ceSNicholas Bellinger 2800e48354ceSNicholas Bellinger memset(&datain, 0, sizeof(struct iscsi_datain)); 2801e48354ceSNicholas Bellinger dr = iscsit_get_datain_values(cmd, &datain); 2802e48354ceSNicholas Bellinger if (!dr) { 2803e48354ceSNicholas Bellinger pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n", 2804e48354ceSNicholas Bellinger cmd->init_task_tag); 2805e48354ceSNicholas Bellinger return -1; 2806e48354ceSNicholas Bellinger } 2807e48354ceSNicholas Bellinger /* 2808e48354ceSNicholas Bellinger * Be paranoid and double check the logic for now. 2809e48354ceSNicholas Bellinger */ 2810ebf1d95cSAndy Grover if ((datain.offset + datain.length) > cmd->se_cmd.data_length) { 2811e48354ceSNicholas Bellinger pr_err("Command ITT: 0x%08x, datain.offset: %u and" 2812e48354ceSNicholas Bellinger " datain.length: %u exceeds cmd->data_length: %u\n", 2813e48354ceSNicholas Bellinger cmd->init_task_tag, datain.offset, datain.length, 2814ebf1d95cSAndy Grover cmd->se_cmd.data_length); 2815e48354ceSNicholas Bellinger return -1; 2816e48354ceSNicholas Bellinger } 2817e48354ceSNicholas Bellinger 281804f3b31bSNicholas Bellinger atomic_long_add(datain.length, &conn->sess->tx_data_octets); 2819e48354ceSNicholas Bellinger /* 2820e48354ceSNicholas Bellinger * Special case for successfully execution w/ both DATAIN 2821e48354ceSNicholas Bellinger * and Sense Data. 2822e48354ceSNicholas Bellinger */ 2823e48354ceSNicholas Bellinger if ((datain.flags & ISCSI_FLAG_DATA_STATUS) && 2824e48354ceSNicholas Bellinger (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)) 2825e48354ceSNicholas Bellinger datain.flags &= ~ISCSI_FLAG_DATA_STATUS; 2826e48354ceSNicholas Bellinger else { 2827e48354ceSNicholas Bellinger if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) || 2828e48354ceSNicholas Bellinger (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) { 2829e48354ceSNicholas Bellinger iscsit_increment_maxcmdsn(cmd, conn->sess); 2830e48354ceSNicholas Bellinger cmd->stat_sn = conn->stat_sn++; 28312ec5a8c1SNicholas Bellinger set_statsn = true; 2832e48354ceSNicholas Bellinger } else if (dr->dr_complete == 2833e48354ceSNicholas Bellinger DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY) 28342ec5a8c1SNicholas Bellinger set_statsn = true; 2835e48354ceSNicholas Bellinger } 2836e48354ceSNicholas Bellinger 28372ec5a8c1SNicholas Bellinger iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn); 2838e48354ceSNicholas Bellinger 28392854bb23SVarun Prakash ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0); 28402854bb23SVarun Prakash if (ret < 0) 28416f3c0e69SAndy Grover return ret; 28426f3c0e69SAndy Grover 2843e48354ceSNicholas Bellinger if (dr->dr_complete) { 28446f3c0e69SAndy Grover eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ? 2845e48354ceSNicholas Bellinger 2 : 1; 2846e48354ceSNicholas Bellinger iscsit_free_datain_req(cmd, dr); 2847e48354ceSNicholas Bellinger } 2848e48354ceSNicholas Bellinger 28496f3c0e69SAndy Grover return eodr; 2850e48354ceSNicholas Bellinger } 2851e48354ceSNicholas Bellinger 28522ec5a8c1SNicholas Bellinger int 28532ec5a8c1SNicholas Bellinger iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 28542ec5a8c1SNicholas Bellinger struct iscsi_logout_rsp *hdr) 2855e48354ceSNicholas Bellinger { 2856e48354ceSNicholas Bellinger struct iscsi_conn *logout_conn = NULL; 2857e48354ceSNicholas Bellinger struct iscsi_conn_recovery *cr = NULL; 2858e48354ceSNicholas Bellinger struct iscsi_session *sess = conn->sess; 2859e48354ceSNicholas Bellinger /* 2860e48354ceSNicholas Bellinger * The actual shutting down of Sessions and/or Connections 2861e48354ceSNicholas Bellinger * for CLOSESESSION and CLOSECONNECTION Logout Requests 2862e48354ceSNicholas Bellinger * is done in scsi_logout_post_handler(). 2863e48354ceSNicholas Bellinger */ 2864e48354ceSNicholas Bellinger switch (cmd->logout_reason) { 2865e48354ceSNicholas Bellinger case ISCSI_LOGOUT_REASON_CLOSE_SESSION: 2866e48354ceSNicholas Bellinger pr_debug("iSCSI session logout successful, setting" 2867e48354ceSNicholas Bellinger " logout response to ISCSI_LOGOUT_SUCCESS.\n"); 2868e48354ceSNicholas Bellinger cmd->logout_response = ISCSI_LOGOUT_SUCCESS; 2869e48354ceSNicholas Bellinger break; 2870e48354ceSNicholas Bellinger case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION: 2871e48354ceSNicholas Bellinger if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND) 2872e48354ceSNicholas Bellinger break; 2873e48354ceSNicholas Bellinger /* 2874e48354ceSNicholas Bellinger * For CLOSECONNECTION logout requests carrying 2875e48354ceSNicholas Bellinger * a matching logout CID -> local CID, the reference 2876e48354ceSNicholas Bellinger * for the local CID will have been incremented in 2877e48354ceSNicholas Bellinger * iscsi_logout_closeconnection(). 2878e48354ceSNicholas Bellinger * 2879e48354ceSNicholas Bellinger * For CLOSECONNECTION logout requests carrying 2880e48354ceSNicholas Bellinger * a different CID than the connection it arrived 2881e48354ceSNicholas Bellinger * on, the connection responding to cmd->logout_cid 2882e48354ceSNicholas Bellinger * is stopped in iscsit_logout_post_handler_diffcid(). 2883e48354ceSNicholas Bellinger */ 2884e48354ceSNicholas Bellinger 2885e48354ceSNicholas Bellinger pr_debug("iSCSI CID: %hu logout on CID: %hu" 2886e48354ceSNicholas Bellinger " successful.\n", cmd->logout_cid, conn->cid); 2887e48354ceSNicholas Bellinger cmd->logout_response = ISCSI_LOGOUT_SUCCESS; 2888e48354ceSNicholas Bellinger break; 2889e48354ceSNicholas Bellinger case ISCSI_LOGOUT_REASON_RECOVERY: 2890e48354ceSNicholas Bellinger if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) || 2891e48354ceSNicholas Bellinger (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED)) 2892e48354ceSNicholas Bellinger break; 2893e48354ceSNicholas Bellinger /* 2894e48354ceSNicholas Bellinger * If the connection is still active from our point of view 2895e48354ceSNicholas Bellinger * force connection recovery to occur. 2896e48354ceSNicholas Bellinger */ 2897e48354ceSNicholas Bellinger logout_conn = iscsit_get_conn_from_cid_rcfr(sess, 2898e48354ceSNicholas Bellinger cmd->logout_cid); 2899ee1b1b9cSAndy Grover if (logout_conn) { 2900e48354ceSNicholas Bellinger iscsit_connection_reinstatement_rcfr(logout_conn); 2901e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(logout_conn); 2902e48354ceSNicholas Bellinger } 2903e48354ceSNicholas Bellinger 2904e48354ceSNicholas Bellinger cr = iscsit_get_inactive_connection_recovery_entry( 2905e48354ceSNicholas Bellinger conn->sess, cmd->logout_cid); 2906e48354ceSNicholas Bellinger if (!cr) { 2907e48354ceSNicholas Bellinger pr_err("Unable to locate CID: %hu for" 2908e48354ceSNicholas Bellinger " REMOVECONNFORRECOVERY Logout Request.\n", 2909e48354ceSNicholas Bellinger cmd->logout_cid); 2910e48354ceSNicholas Bellinger cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND; 2911e48354ceSNicholas Bellinger break; 2912e48354ceSNicholas Bellinger } 2913e48354ceSNicholas Bellinger 2914e48354ceSNicholas Bellinger iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn); 2915e48354ceSNicholas Bellinger 2916e48354ceSNicholas Bellinger pr_debug("iSCSI REMOVECONNFORRECOVERY logout" 2917e48354ceSNicholas Bellinger " for recovery for CID: %hu on CID: %hu successful.\n", 2918e48354ceSNicholas Bellinger cmd->logout_cid, conn->cid); 2919e48354ceSNicholas Bellinger cmd->logout_response = ISCSI_LOGOUT_SUCCESS; 2920e48354ceSNicholas Bellinger break; 2921e48354ceSNicholas Bellinger default: 2922e48354ceSNicholas Bellinger pr_err("Unknown cmd->logout_reason: 0x%02x\n", 2923e48354ceSNicholas Bellinger cmd->logout_reason); 2924e48354ceSNicholas Bellinger return -1; 2925e48354ceSNicholas Bellinger } 2926e48354ceSNicholas Bellinger 2927e48354ceSNicholas Bellinger hdr->opcode = ISCSI_OP_LOGOUT_RSP; 2928e48354ceSNicholas Bellinger hdr->flags |= ISCSI_FLAG_CMD_FINAL; 2929e48354ceSNicholas Bellinger hdr->response = cmd->logout_response; 293066c7db68SChristoph Hellwig hdr->itt = cmd->init_task_tag; 2931e48354ceSNicholas Bellinger cmd->stat_sn = conn->stat_sn++; 2932e48354ceSNicholas Bellinger hdr->statsn = cpu_to_be32(cmd->stat_sn); 2933e48354ceSNicholas Bellinger 2934e48354ceSNicholas Bellinger iscsit_increment_maxcmdsn(cmd, conn->sess); 2935e48354ceSNicholas Bellinger hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 2936109e2381SRoland Dreier hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn)); 2937e48354ceSNicholas Bellinger 29382ec5a8c1SNicholas Bellinger pr_debug("Built Logout Response ITT: 0x%08x StatSN:" 29392ec5a8c1SNicholas Bellinger " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n", 29402ec5a8c1SNicholas Bellinger cmd->init_task_tag, cmd->stat_sn, hdr->response, 29412ec5a8c1SNicholas Bellinger cmd->logout_cid, conn->cid); 29422ec5a8c1SNicholas Bellinger 29432ec5a8c1SNicholas Bellinger return 0; 29442ec5a8c1SNicholas Bellinger } 29452ec5a8c1SNicholas Bellinger EXPORT_SYMBOL(iscsit_build_logout_rsp); 29462ec5a8c1SNicholas Bellinger 29472ec5a8c1SNicholas Bellinger static int 29482ec5a8c1SNicholas Bellinger iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 29492ec5a8c1SNicholas Bellinger { 29502854bb23SVarun Prakash int rc; 29512ec5a8c1SNicholas Bellinger 29522ec5a8c1SNicholas Bellinger rc = iscsit_build_logout_rsp(cmd, conn, 29532ec5a8c1SNicholas Bellinger (struct iscsi_logout_rsp *)&cmd->pdu[0]); 29542ec5a8c1SNicholas Bellinger if (rc < 0) 29552ec5a8c1SNicholas Bellinger return rc; 29562ec5a8c1SNicholas Bellinger 29572854bb23SVarun Prakash return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0); 2958e48354ceSNicholas Bellinger } 2959e48354ceSNicholas Bellinger 29602ec5a8c1SNicholas Bellinger void 29612ec5a8c1SNicholas Bellinger iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 29622ec5a8c1SNicholas Bellinger struct iscsi_nopin *hdr, bool nopout_response) 29632ec5a8c1SNicholas Bellinger { 29642ec5a8c1SNicholas Bellinger hdr->opcode = ISCSI_OP_NOOP_IN; 29652ec5a8c1SNicholas Bellinger hdr->flags |= ISCSI_FLAG_CMD_FINAL; 29662ec5a8c1SNicholas Bellinger hton24(hdr->dlength, cmd->buf_ptr_size); 29672ec5a8c1SNicholas Bellinger if (nopout_response) 29682ec5a8c1SNicholas Bellinger put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun); 29692ec5a8c1SNicholas Bellinger hdr->itt = cmd->init_task_tag; 29702ec5a8c1SNicholas Bellinger hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag); 29712ec5a8c1SNicholas Bellinger cmd->stat_sn = (nopout_response) ? conn->stat_sn++ : 29722ec5a8c1SNicholas Bellinger conn->stat_sn; 29732ec5a8c1SNicholas Bellinger hdr->statsn = cpu_to_be32(cmd->stat_sn); 29742ec5a8c1SNicholas Bellinger 29752ec5a8c1SNicholas Bellinger if (nopout_response) 29762ec5a8c1SNicholas Bellinger iscsit_increment_maxcmdsn(cmd, conn->sess); 29772ec5a8c1SNicholas Bellinger 29782ec5a8c1SNicholas Bellinger hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 2979109e2381SRoland Dreier hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn)); 29802ec5a8c1SNicholas Bellinger 29812ec5a8c1SNicholas Bellinger pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x," 29822ec5a8c1SNicholas Bellinger " StatSN: 0x%08x, Length %u\n", (nopout_response) ? 29833fc6a642SColin Ian King "Solicited" : "Unsolicited", cmd->init_task_tag, 29842ec5a8c1SNicholas Bellinger cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size); 29852ec5a8c1SNicholas Bellinger } 29862ec5a8c1SNicholas Bellinger EXPORT_SYMBOL(iscsit_build_nopin_rsp); 29872ec5a8c1SNicholas Bellinger 2988e48354ceSNicholas Bellinger /* 2989e48354ceSNicholas Bellinger * Unsolicited NOPIN, either requesting a response or not. 2990e48354ceSNicholas Bellinger */ 2991e48354ceSNicholas Bellinger static int iscsit_send_unsolicited_nopin( 2992e48354ceSNicholas Bellinger struct iscsi_cmd *cmd, 2993e48354ceSNicholas Bellinger struct iscsi_conn *conn, 2994e48354ceSNicholas Bellinger int want_response) 2995e48354ceSNicholas Bellinger { 29962ec5a8c1SNicholas Bellinger struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0]; 29972854bb23SVarun Prakash int ret; 2998e48354ceSNicholas Bellinger 29992ec5a8c1SNicholas Bellinger iscsit_build_nopin_rsp(cmd, conn, hdr, false); 3000e48354ceSNicholas Bellinger 3001e48354ceSNicholas Bellinger pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:" 3002e48354ceSNicholas Bellinger " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid); 3003e48354ceSNicholas Bellinger 30042854bb23SVarun Prakash ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0); 30052854bb23SVarun Prakash if (ret < 0) 30066f3c0e69SAndy Grover return ret; 30076f3c0e69SAndy Grover 30086f3c0e69SAndy Grover spin_lock_bh(&cmd->istate_lock); 30096f3c0e69SAndy Grover cmd->i_state = want_response ? 30106f3c0e69SAndy Grover ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS; 30116f3c0e69SAndy Grover spin_unlock_bh(&cmd->istate_lock); 30126f3c0e69SAndy Grover 3013e48354ceSNicholas Bellinger return 0; 3014e48354ceSNicholas Bellinger } 3015e48354ceSNicholas Bellinger 30162ec5a8c1SNicholas Bellinger static int 30172ec5a8c1SNicholas Bellinger iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 3018e48354ceSNicholas Bellinger { 30192ec5a8c1SNicholas Bellinger struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0]; 30202ec5a8c1SNicholas Bellinger 30212ec5a8c1SNicholas Bellinger iscsit_build_nopin_rsp(cmd, conn, hdr, true); 3022e48354ceSNicholas Bellinger 3023e48354ceSNicholas Bellinger /* 3024e48354ceSNicholas Bellinger * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr. 3025e48354ceSNicholas Bellinger * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size. 3026e48354ceSNicholas Bellinger */ 30272854bb23SVarun Prakash pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size); 3028e48354ceSNicholas Bellinger 30292854bb23SVarun Prakash return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, 30302854bb23SVarun Prakash cmd->buf_ptr, 30312854bb23SVarun Prakash cmd->buf_ptr_size); 3032e48354ceSNicholas Bellinger } 3033e48354ceSNicholas Bellinger 30346f3c0e69SAndy Grover static int iscsit_send_r2t( 3035e48354ceSNicholas Bellinger struct iscsi_cmd *cmd, 3036e48354ceSNicholas Bellinger struct iscsi_conn *conn) 3037e48354ceSNicholas Bellinger { 3038e48354ceSNicholas Bellinger struct iscsi_r2t *r2t; 3039e48354ceSNicholas Bellinger struct iscsi_r2t_rsp *hdr; 30406f3c0e69SAndy Grover int ret; 3041e48354ceSNicholas Bellinger 3042e48354ceSNicholas Bellinger r2t = iscsit_get_r2t_from_list(cmd); 3043e48354ceSNicholas Bellinger if (!r2t) 3044e48354ceSNicholas Bellinger return -1; 3045e48354ceSNicholas Bellinger 3046e48354ceSNicholas Bellinger hdr = (struct iscsi_r2t_rsp *) cmd->pdu; 3047e48354ceSNicholas Bellinger memset(hdr, 0, ISCSI_HDR_LEN); 3048e48354ceSNicholas Bellinger hdr->opcode = ISCSI_OP_R2T; 3049e48354ceSNicholas Bellinger hdr->flags |= ISCSI_FLAG_CMD_FINAL; 3050e48354ceSNicholas Bellinger int_to_scsilun(cmd->se_cmd.orig_fe_lun, 3051e48354ceSNicholas Bellinger (struct scsi_lun *)&hdr->lun); 305266c7db68SChristoph Hellwig hdr->itt = cmd->init_task_tag; 30538567270dSVarun Prakash if (conn->conn_transport->iscsit_get_r2t_ttt) 30548567270dSVarun Prakash conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t); 30558567270dSVarun Prakash else 3056c1e34b64SSagi Grimberg r2t->targ_xfer_tag = session_get_next_ttt(conn->sess); 3057e48354ceSNicholas Bellinger hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag); 3058e48354ceSNicholas Bellinger hdr->statsn = cpu_to_be32(conn->stat_sn); 3059e48354ceSNicholas Bellinger hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 3060109e2381SRoland Dreier hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn)); 3061e48354ceSNicholas Bellinger hdr->r2tsn = cpu_to_be32(r2t->r2t_sn); 3062e48354ceSNicholas Bellinger hdr->data_offset = cpu_to_be32(r2t->offset); 3063e48354ceSNicholas Bellinger hdr->data_length = cpu_to_be32(r2t->xfer_len); 3064e48354ceSNicholas Bellinger 3065e48354ceSNicholas Bellinger pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:" 3066e48354ceSNicholas Bellinger " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n", 3067e48354ceSNicholas Bellinger (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag, 3068e48354ceSNicholas Bellinger r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn, 3069e48354ceSNicholas Bellinger r2t->offset, r2t->xfer_len, conn->cid); 3070e48354ceSNicholas Bellinger 3071e48354ceSNicholas Bellinger spin_lock_bh(&cmd->r2t_lock); 3072e48354ceSNicholas Bellinger r2t->sent_r2t = 1; 3073e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->r2t_lock); 3074e48354ceSNicholas Bellinger 30752854bb23SVarun Prakash ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0); 30766f3c0e69SAndy Grover if (ret < 0) { 30776f3c0e69SAndy Grover return ret; 30786f3c0e69SAndy Grover } 30796f3c0e69SAndy Grover 30806f3c0e69SAndy Grover spin_lock_bh(&cmd->dataout_timeout_lock); 30816f3c0e69SAndy Grover iscsit_start_dataout_timer(cmd, conn); 30826f3c0e69SAndy Grover spin_unlock_bh(&cmd->dataout_timeout_lock); 30836f3c0e69SAndy Grover 3084e48354ceSNicholas Bellinger return 0; 3085e48354ceSNicholas Bellinger } 3086e48354ceSNicholas Bellinger 3087e48354ceSNicholas Bellinger /* 30888b1e1244SAndy Grover * @recovery: If called from iscsi_task_reassign_complete_write() for 3089e48354ceSNicholas Bellinger * connection recovery. 3090e48354ceSNicholas Bellinger */ 3091e48354ceSNicholas Bellinger int iscsit_build_r2ts_for_cmd( 3092e48354ceSNicholas Bellinger struct iscsi_conn *conn, 30933e1c81a9SNicholas Bellinger struct iscsi_cmd *cmd, 30948b1e1244SAndy Grover bool recovery) 3095e48354ceSNicholas Bellinger { 3096e48354ceSNicholas Bellinger int first_r2t = 1; 3097e48354ceSNicholas Bellinger u32 offset = 0, xfer_len = 0; 3098e48354ceSNicholas Bellinger 3099e48354ceSNicholas Bellinger spin_lock_bh(&cmd->r2t_lock); 3100e48354ceSNicholas Bellinger if (cmd->cmd_flags & ICF_SENT_LAST_R2T) { 3101e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->r2t_lock); 3102e48354ceSNicholas Bellinger return 0; 3103e48354ceSNicholas Bellinger } 3104e48354ceSNicholas Bellinger 31058b1e1244SAndy Grover if (conn->sess->sess_ops->DataSequenceInOrder && 31068b1e1244SAndy Grover !recovery) 3107c6037cc5SAndy Grover cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done); 3108e48354ceSNicholas Bellinger 3109e48354ceSNicholas Bellinger while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) { 3110e48354ceSNicholas Bellinger if (conn->sess->sess_ops->DataSequenceInOrder) { 3111e48354ceSNicholas Bellinger offset = cmd->r2t_offset; 3112e48354ceSNicholas Bellinger 31138b1e1244SAndy Grover if (first_r2t && recovery) { 31148b1e1244SAndy Grover int new_data_end = offset + 31158b1e1244SAndy Grover conn->sess->sess_ops->MaxBurstLength - 31168b1e1244SAndy Grover cmd->next_burst_len; 31178b1e1244SAndy Grover 3118ebf1d95cSAndy Grover if (new_data_end > cmd->se_cmd.data_length) 3119ebf1d95cSAndy Grover xfer_len = cmd->se_cmd.data_length - offset; 31208b1e1244SAndy Grover else 31218b1e1244SAndy Grover xfer_len = 31228b1e1244SAndy Grover conn->sess->sess_ops->MaxBurstLength - 31238b1e1244SAndy Grover cmd->next_burst_len; 3124e48354ceSNicholas Bellinger } else { 31258b1e1244SAndy Grover int new_data_end = offset + 3126e48354ceSNicholas Bellinger conn->sess->sess_ops->MaxBurstLength; 31278b1e1244SAndy Grover 3128ebf1d95cSAndy Grover if (new_data_end > cmd->se_cmd.data_length) 3129ebf1d95cSAndy Grover xfer_len = cmd->se_cmd.data_length - offset; 31308b1e1244SAndy Grover else 31318b1e1244SAndy Grover xfer_len = conn->sess->sess_ops->MaxBurstLength; 3132e48354ceSNicholas Bellinger } 3133e48354ceSNicholas Bellinger cmd->r2t_offset += xfer_len; 3134e48354ceSNicholas Bellinger 3135ebf1d95cSAndy Grover if (cmd->r2t_offset == cmd->se_cmd.data_length) 3136e48354ceSNicholas Bellinger cmd->cmd_flags |= ICF_SENT_LAST_R2T; 3137e48354ceSNicholas Bellinger } else { 3138e48354ceSNicholas Bellinger struct iscsi_seq *seq; 3139e48354ceSNicholas Bellinger 3140e48354ceSNicholas Bellinger seq = iscsit_get_seq_holder_for_r2t(cmd); 3141e48354ceSNicholas Bellinger if (!seq) { 3142e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->r2t_lock); 3143e48354ceSNicholas Bellinger return -1; 3144e48354ceSNicholas Bellinger } 3145e48354ceSNicholas Bellinger 3146e48354ceSNicholas Bellinger offset = seq->offset; 3147e48354ceSNicholas Bellinger xfer_len = seq->xfer_len; 3148e48354ceSNicholas Bellinger 3149e48354ceSNicholas Bellinger if (cmd->seq_send_order == cmd->seq_count) 3150e48354ceSNicholas Bellinger cmd->cmd_flags |= ICF_SENT_LAST_R2T; 3151e48354ceSNicholas Bellinger } 3152e48354ceSNicholas Bellinger cmd->outstanding_r2ts++; 3153e48354ceSNicholas Bellinger first_r2t = 0; 3154e48354ceSNicholas Bellinger 3155e48354ceSNicholas Bellinger if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) { 3156e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->r2t_lock); 3157e48354ceSNicholas Bellinger return -1; 3158e48354ceSNicholas Bellinger } 3159e48354ceSNicholas Bellinger 3160e48354ceSNicholas Bellinger if (cmd->cmd_flags & ICF_SENT_LAST_R2T) 3161e48354ceSNicholas Bellinger break; 3162e48354ceSNicholas Bellinger } 3163e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->r2t_lock); 3164e48354ceSNicholas Bellinger 3165e48354ceSNicholas Bellinger return 0; 3166e48354ceSNicholas Bellinger } 3167d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd); 3168e48354ceSNicholas Bellinger 31692ec5a8c1SNicholas Bellinger void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 31702ec5a8c1SNicholas Bellinger bool inc_stat_sn, struct iscsi_scsi_rsp *hdr) 3171e48354ceSNicholas Bellinger { 31722ec5a8c1SNicholas Bellinger if (inc_stat_sn) 3173e48354ceSNicholas Bellinger cmd->stat_sn = conn->stat_sn++; 3174e48354ceSNicholas Bellinger 317504f3b31bSNicholas Bellinger atomic_long_inc(&conn->sess->rsp_pdus); 3176e48354ceSNicholas Bellinger 3177e48354ceSNicholas Bellinger memset(hdr, 0, ISCSI_HDR_LEN); 3178e48354ceSNicholas Bellinger hdr->opcode = ISCSI_OP_SCSI_CMD_RSP; 3179e48354ceSNicholas Bellinger hdr->flags |= ISCSI_FLAG_CMD_FINAL; 3180e48354ceSNicholas Bellinger if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) { 3181e48354ceSNicholas Bellinger hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW; 31827e46cf02SNicholas Bellinger hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); 3183e48354ceSNicholas Bellinger } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) { 3184e48354ceSNicholas Bellinger hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW; 31857e46cf02SNicholas Bellinger hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); 3186e48354ceSNicholas Bellinger } 3187e48354ceSNicholas Bellinger hdr->response = cmd->iscsi_response; 3188e48354ceSNicholas Bellinger hdr->cmd_status = cmd->se_cmd.scsi_status; 318966c7db68SChristoph Hellwig hdr->itt = cmd->init_task_tag; 3190e48354ceSNicholas Bellinger hdr->statsn = cpu_to_be32(cmd->stat_sn); 3191e48354ceSNicholas Bellinger 3192e48354ceSNicholas Bellinger iscsit_increment_maxcmdsn(cmd, conn->sess); 3193e48354ceSNicholas Bellinger hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 3194109e2381SRoland Dreier hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn)); 3195e48354ceSNicholas Bellinger 31962ec5a8c1SNicholas Bellinger pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x," 31972ec5a8c1SNicholas Bellinger " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n", 31982ec5a8c1SNicholas Bellinger cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status, 31992ec5a8c1SNicholas Bellinger cmd->se_cmd.scsi_status, conn->cid); 32002ec5a8c1SNicholas Bellinger } 32012ec5a8c1SNicholas Bellinger EXPORT_SYMBOL(iscsit_build_rsp_pdu); 32022ec5a8c1SNicholas Bellinger 32032ec5a8c1SNicholas Bellinger static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 32042ec5a8c1SNicholas Bellinger { 32052ec5a8c1SNicholas Bellinger struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0]; 32062ec5a8c1SNicholas Bellinger bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS); 32072854bb23SVarun Prakash void *data_buf = NULL; 32082854bb23SVarun Prakash u32 padding = 0, data_buf_len = 0; 32092ec5a8c1SNicholas Bellinger 32102ec5a8c1SNicholas Bellinger iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr); 32112ec5a8c1SNicholas Bellinger 3212e48354ceSNicholas Bellinger /* 3213e48354ceSNicholas Bellinger * Attach SENSE DATA payload to iSCSI Response PDU 3214e48354ceSNicholas Bellinger */ 3215e48354ceSNicholas Bellinger if (cmd->se_cmd.sense_buffer && 3216e48354ceSNicholas Bellinger ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) || 3217e48354ceSNicholas Bellinger (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) { 32189c58b7ddSRoland Dreier put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer); 32199c58b7ddSRoland Dreier cmd->se_cmd.scsi_sense_length += sizeof (__be16); 32209c58b7ddSRoland Dreier 3221e48354ceSNicholas Bellinger padding = -(cmd->se_cmd.scsi_sense_length) & 3; 322250e5c87dSChristoph Hellwig hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length); 32232854bb23SVarun Prakash data_buf = cmd->sense_buffer; 32242854bb23SVarun Prakash data_buf_len = cmd->se_cmd.scsi_sense_length + padding; 3225e48354ceSNicholas Bellinger 3226e48354ceSNicholas Bellinger if (padding) { 32279c58b7ddSRoland Dreier memset(cmd->sense_buffer + 3228e48354ceSNicholas Bellinger cmd->se_cmd.scsi_sense_length, 0, padding); 3229e48354ceSNicholas Bellinger pr_debug("Adding %u bytes of padding to" 3230e48354ceSNicholas Bellinger " SENSE.\n", padding); 3231e48354ceSNicholas Bellinger } 3232e48354ceSNicholas Bellinger 3233e48354ceSNicholas Bellinger pr_debug("Attaching SENSE DATA: %u bytes to iSCSI" 3234e48354ceSNicholas Bellinger " Response PDU\n", 3235e48354ceSNicholas Bellinger cmd->se_cmd.scsi_sense_length); 3236e48354ceSNicholas Bellinger } 3237e48354ceSNicholas Bellinger 32382854bb23SVarun Prakash return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf, 32392854bb23SVarun Prakash data_buf_len); 3240e48354ceSNicholas Bellinger } 3241e48354ceSNicholas Bellinger 3242e48354ceSNicholas Bellinger static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr) 3243e48354ceSNicholas Bellinger { 3244e48354ceSNicholas Bellinger switch (se_tmr->response) { 3245e48354ceSNicholas Bellinger case TMR_FUNCTION_COMPLETE: 3246e48354ceSNicholas Bellinger return ISCSI_TMF_RSP_COMPLETE; 3247e48354ceSNicholas Bellinger case TMR_TASK_DOES_NOT_EXIST: 3248e48354ceSNicholas Bellinger return ISCSI_TMF_RSP_NO_TASK; 3249e48354ceSNicholas Bellinger case TMR_LUN_DOES_NOT_EXIST: 3250e48354ceSNicholas Bellinger return ISCSI_TMF_RSP_NO_LUN; 3251e48354ceSNicholas Bellinger case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED: 3252e48354ceSNicholas Bellinger return ISCSI_TMF_RSP_NOT_SUPPORTED; 3253e48354ceSNicholas Bellinger case TMR_FUNCTION_REJECTED: 3254e48354ceSNicholas Bellinger default: 3255e48354ceSNicholas Bellinger return ISCSI_TMF_RSP_REJECTED; 3256e48354ceSNicholas Bellinger } 3257e48354ceSNicholas Bellinger } 3258e48354ceSNicholas Bellinger 32592ec5a8c1SNicholas Bellinger void 32602ec5a8c1SNicholas Bellinger iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 32612ec5a8c1SNicholas Bellinger struct iscsi_tm_rsp *hdr) 3262e48354ceSNicholas Bellinger { 3263e48354ceSNicholas Bellinger struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req; 3264e48354ceSNicholas Bellinger 3265e48354ceSNicholas Bellinger hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP; 32667ae0b103SNicholas Bellinger hdr->flags = ISCSI_FLAG_CMD_FINAL; 3267e48354ceSNicholas Bellinger hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr); 326866c7db68SChristoph Hellwig hdr->itt = cmd->init_task_tag; 3269e48354ceSNicholas Bellinger cmd->stat_sn = conn->stat_sn++; 3270e48354ceSNicholas Bellinger hdr->statsn = cpu_to_be32(cmd->stat_sn); 3271e48354ceSNicholas Bellinger 3272e48354ceSNicholas Bellinger iscsit_increment_maxcmdsn(cmd, conn->sess); 3273e48354ceSNicholas Bellinger hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 3274109e2381SRoland Dreier hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn)); 3275e48354ceSNicholas Bellinger 32762ec5a8c1SNicholas Bellinger pr_debug("Built Task Management Response ITT: 0x%08x," 32772ec5a8c1SNicholas Bellinger " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n", 32782ec5a8c1SNicholas Bellinger cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid); 32792ec5a8c1SNicholas Bellinger } 32802ec5a8c1SNicholas Bellinger EXPORT_SYMBOL(iscsit_build_task_mgt_rsp); 32812ec5a8c1SNicholas Bellinger 32822ec5a8c1SNicholas Bellinger static int 32832ec5a8c1SNicholas Bellinger iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 32842ec5a8c1SNicholas Bellinger { 32852ec5a8c1SNicholas Bellinger struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0]; 32862ec5a8c1SNicholas Bellinger 32872ec5a8c1SNicholas Bellinger iscsit_build_task_mgt_rsp(cmd, conn, hdr); 32882ec5a8c1SNicholas Bellinger 32892854bb23SVarun Prakash return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0); 3290e48354ceSNicholas Bellinger } 3291e48354ceSNicholas Bellinger 32928b1e1244SAndy Grover #define SENDTARGETS_BUF_LIMIT 32768U 32938b1e1244SAndy Grover 329422c7aaa5SSagi Grimberg static int 329522c7aaa5SSagi Grimberg iscsit_build_sendtargets_response(struct iscsi_cmd *cmd, 3296e4f4e801SSagi Grimberg enum iscsit_transport_type network_transport, 3297e4f4e801SSagi Grimberg int skip_bytes, bool *completed) 3298e48354ceSNicholas Bellinger { 3299e48354ceSNicholas Bellinger char *payload = NULL; 3300e48354ceSNicholas Bellinger struct iscsi_conn *conn = cmd->conn; 3301e48354ceSNicholas Bellinger struct iscsi_portal_group *tpg; 3302e48354ceSNicholas Bellinger struct iscsi_tiqn *tiqn; 3303e48354ceSNicholas Bellinger struct iscsi_tpg_np *tpg_np; 3304e48354ceSNicholas Bellinger int buffer_len, end_of_buf = 0, len = 0, payload_len = 0; 33052dd1d53fSThomas Glanzmann int target_name_printed; 33068b1e1244SAndy Grover unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */ 33076665889cSNicholas Bellinger unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL; 3308a6415cddSDavid Disseldorp bool active; 3309e48354ceSNicholas Bellinger 3310be7dcfb6SSagi Grimberg buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength, 33118b1e1244SAndy Grover SENDTARGETS_BUF_LIMIT); 3312e48354ceSNicholas Bellinger 3313e48354ceSNicholas Bellinger payload = kzalloc(buffer_len, GFP_KERNEL); 3314c46e22f1SMarkus Elfring if (!payload) 3315e48354ceSNicholas Bellinger return -ENOMEM; 3316c46e22f1SMarkus Elfring 33176665889cSNicholas Bellinger /* 33188060b8ddSAndy Grover * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE 33196665889cSNicholas Bellinger * explicit case.. 33206665889cSNicholas Bellinger */ 33218060b8ddSAndy Grover if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) { 33226665889cSNicholas Bellinger text_ptr = strchr(text_in, '='); 33236665889cSNicholas Bellinger if (!text_ptr) { 33246665889cSNicholas Bellinger pr_err("Unable to locate '=' string in text_in:" 33256665889cSNicholas Bellinger " %s\n", text_in); 33264f45d320SDan Carpenter kfree(payload); 33276665889cSNicholas Bellinger return -EINVAL; 33286665889cSNicholas Bellinger } 33296665889cSNicholas Bellinger /* 33306665889cSNicholas Bellinger * Skip over '=' character.. 33316665889cSNicholas Bellinger */ 33326665889cSNicholas Bellinger text_ptr += 1; 33336665889cSNicholas Bellinger } 3334e48354ceSNicholas Bellinger 3335e48354ceSNicholas Bellinger spin_lock(&tiqn_lock); 3336e48354ceSNicholas Bellinger list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) { 33378060b8ddSAndy Grover if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) && 33386665889cSNicholas Bellinger strcmp(tiqn->tiqn, text_ptr)) { 33396665889cSNicholas Bellinger continue; 33406665889cSNicholas Bellinger } 33416665889cSNicholas Bellinger 33422dd1d53fSThomas Glanzmann target_name_printed = 0; 3343e48354ceSNicholas Bellinger 3344e48354ceSNicholas Bellinger spin_lock(&tiqn->tiqn_tpg_lock); 3345e48354ceSNicholas Bellinger list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) { 3346e48354ceSNicholas Bellinger 33472dd1d53fSThomas Glanzmann /* If demo_mode_discovery=0 and generate_node_acls=0 33482dd1d53fSThomas Glanzmann * (demo mode dislabed) do not return 33492dd1d53fSThomas Glanzmann * TargetName+TargetAddress unless a NodeACL exists. 33502dd1d53fSThomas Glanzmann */ 33512dd1d53fSThomas Glanzmann 33522dd1d53fSThomas Glanzmann if ((tpg->tpg_attrib.generate_node_acls == 0) && 33532dd1d53fSThomas Glanzmann (tpg->tpg_attrib.demo_mode_discovery == 0) && 335421aaa23bSNicholas Bellinger (!target_tpg_has_node_acl(&tpg->tpg_se_tpg, 33552dd1d53fSThomas Glanzmann cmd->conn->sess->sess_ops->InitiatorName))) { 33562dd1d53fSThomas Glanzmann continue; 33572dd1d53fSThomas Glanzmann } 33582dd1d53fSThomas Glanzmann 3359e48354ceSNicholas Bellinger spin_lock(&tpg->tpg_state_lock); 3360a6415cddSDavid Disseldorp active = (tpg->tpg_state == TPG_STATE_ACTIVE); 3361e48354ceSNicholas Bellinger spin_unlock(&tpg->tpg_state_lock); 3362a6415cddSDavid Disseldorp 3363a6415cddSDavid Disseldorp if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets) 3364e48354ceSNicholas Bellinger continue; 3365e48354ceSNicholas Bellinger 3366e48354ceSNicholas Bellinger spin_lock(&tpg->tpg_np_lock); 3367e48354ceSNicholas Bellinger list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, 3368e48354ceSNicholas Bellinger tpg_np_list) { 33692f9bc894SNicholas Bellinger struct iscsi_np *np = tpg_np->tpg_np; 337013a3cf08SAndy Grover struct sockaddr_storage *sockaddr; 33712f9bc894SNicholas Bellinger 337222c7aaa5SSagi Grimberg if (np->np_network_transport != network_transport) 337322c7aaa5SSagi Grimberg continue; 337422c7aaa5SSagi Grimberg 33752dd1d53fSThomas Glanzmann if (!target_name_printed) { 33762dd1d53fSThomas Glanzmann len = sprintf(buf, "TargetName=%s", 33772dd1d53fSThomas Glanzmann tiqn->tiqn); 33782dd1d53fSThomas Glanzmann len += 1; 33792dd1d53fSThomas Glanzmann 33802dd1d53fSThomas Glanzmann if ((len + payload_len) > buffer_len) { 33812dd1d53fSThomas Glanzmann spin_unlock(&tpg->tpg_np_lock); 33822dd1d53fSThomas Glanzmann spin_unlock(&tiqn->tiqn_tpg_lock); 33832dd1d53fSThomas Glanzmann end_of_buf = 1; 33842dd1d53fSThomas Glanzmann goto eob; 33852dd1d53fSThomas Glanzmann } 3386e4f4e801SSagi Grimberg 3387e4f4e801SSagi Grimberg if (skip_bytes && len <= skip_bytes) { 3388e4f4e801SSagi Grimberg skip_bytes -= len; 3389e4f4e801SSagi Grimberg } else { 33902dd1d53fSThomas Glanzmann memcpy(payload + payload_len, buf, len); 33912dd1d53fSThomas Glanzmann payload_len += len; 33922dd1d53fSThomas Glanzmann target_name_printed = 1; 3393e4f4e801SSagi Grimberg if (len > skip_bytes) 3394e4f4e801SSagi Grimberg skip_bytes = 0; 3395e4f4e801SSagi Grimberg } 33962dd1d53fSThomas Glanzmann } 33972dd1d53fSThomas Glanzmann 33987bfca0cfSSagi Grimberg if (inet_addr_is_any((struct sockaddr *)&np->np_sockaddr)) 339969d75574SAndy Grover sockaddr = &conn->local_sockaddr; 34001997e625SAndy Grover else 340169d75574SAndy Grover sockaddr = &np->np_sockaddr; 34021997e625SAndy Grover 340376c28f1fSAndy Grover len = sprintf(buf, "TargetAddress=" 340476c28f1fSAndy Grover "%pISpc,%hu", 340569d75574SAndy Grover sockaddr, 3406e48354ceSNicholas Bellinger tpg->tpgt); 3407e48354ceSNicholas Bellinger len += 1; 3408e48354ceSNicholas Bellinger 3409e48354ceSNicholas Bellinger if ((len + payload_len) > buffer_len) { 3410e48354ceSNicholas Bellinger spin_unlock(&tpg->tpg_np_lock); 3411e48354ceSNicholas Bellinger spin_unlock(&tiqn->tiqn_tpg_lock); 3412e48354ceSNicholas Bellinger end_of_buf = 1; 3413e48354ceSNicholas Bellinger goto eob; 3414e48354ceSNicholas Bellinger } 3415e4f4e801SSagi Grimberg 3416e4f4e801SSagi Grimberg if (skip_bytes && len <= skip_bytes) { 3417e4f4e801SSagi Grimberg skip_bytes -= len; 3418e4f4e801SSagi Grimberg } else { 34198359cf43SJörn Engel memcpy(payload + payload_len, buf, len); 3420e48354ceSNicholas Bellinger payload_len += len; 3421e4f4e801SSagi Grimberg if (len > skip_bytes) 3422e4f4e801SSagi Grimberg skip_bytes = 0; 3423e4f4e801SSagi Grimberg } 3424e48354ceSNicholas Bellinger } 3425e48354ceSNicholas Bellinger spin_unlock(&tpg->tpg_np_lock); 3426e48354ceSNicholas Bellinger } 3427e48354ceSNicholas Bellinger spin_unlock(&tiqn->tiqn_tpg_lock); 3428e48354ceSNicholas Bellinger eob: 3429e4f4e801SSagi Grimberg if (end_of_buf) { 3430e4f4e801SSagi Grimberg *completed = false; 3431e48354ceSNicholas Bellinger break; 3432e4f4e801SSagi Grimberg } 34336665889cSNicholas Bellinger 34348060b8ddSAndy Grover if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) 34356665889cSNicholas Bellinger break; 3436e48354ceSNicholas Bellinger } 3437e48354ceSNicholas Bellinger spin_unlock(&tiqn_lock); 3438e48354ceSNicholas Bellinger 3439e48354ceSNicholas Bellinger cmd->buf_ptr = payload; 3440e48354ceSNicholas Bellinger 3441e48354ceSNicholas Bellinger return payload_len; 3442e48354ceSNicholas Bellinger } 3443e48354ceSNicholas Bellinger 3444889c8a68SNicholas Bellinger int 3445889c8a68SNicholas Bellinger iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 344622c7aaa5SSagi Grimberg struct iscsi_text_rsp *hdr, 344722c7aaa5SSagi Grimberg enum iscsit_transport_type network_transport) 3448e48354ceSNicholas Bellinger { 3449889c8a68SNicholas Bellinger int text_length, padding; 3450e4f4e801SSagi Grimberg bool completed = true; 3451e48354ceSNicholas Bellinger 3452e4f4e801SSagi Grimberg text_length = iscsit_build_sendtargets_response(cmd, network_transport, 3453e4f4e801SSagi Grimberg cmd->read_data_done, 3454e4f4e801SSagi Grimberg &completed); 3455e48354ceSNicholas Bellinger if (text_length < 0) 3456e48354ceSNicholas Bellinger return text_length; 3457e48354ceSNicholas Bellinger 3458e4f4e801SSagi Grimberg if (completed) { 3459310d40a9SVarun Prakash hdr->flags = ISCSI_FLAG_CMD_FINAL; 3460e4f4e801SSagi Grimberg } else { 3461310d40a9SVarun Prakash hdr->flags = ISCSI_FLAG_TEXT_CONTINUE; 3462e4f4e801SSagi Grimberg cmd->read_data_done += text_length; 3463e4f4e801SSagi Grimberg if (cmd->targ_xfer_tag == 0xFFFFFFFF) 3464e4f4e801SSagi Grimberg cmd->targ_xfer_tag = session_get_next_ttt(conn->sess); 3465e4f4e801SSagi Grimberg } 3466e4f4e801SSagi Grimberg hdr->opcode = ISCSI_OP_TEXT_RSP; 3467889c8a68SNicholas Bellinger padding = ((-text_length) & 3); 3468e48354ceSNicholas Bellinger hton24(hdr->dlength, text_length); 346966c7db68SChristoph Hellwig hdr->itt = cmd->init_task_tag; 3470e48354ceSNicholas Bellinger hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag); 3471e48354ceSNicholas Bellinger cmd->stat_sn = conn->stat_sn++; 3472e48354ceSNicholas Bellinger hdr->statsn = cpu_to_be32(cmd->stat_sn); 3473e48354ceSNicholas Bellinger 3474e48354ceSNicholas Bellinger iscsit_increment_maxcmdsn(cmd, conn->sess); 3475e4f4e801SSagi Grimberg /* 3476e4f4e801SSagi Grimberg * Reset maxcmdsn_inc in multi-part text payload exchanges to 3477e4f4e801SSagi Grimberg * correctly increment MaxCmdSN for each response answering a 3478e4f4e801SSagi Grimberg * non immediate text request with a valid CmdSN. 3479e4f4e801SSagi Grimberg */ 3480e4f4e801SSagi Grimberg cmd->maxcmdsn_inc = 0; 3481e48354ceSNicholas Bellinger hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 3482109e2381SRoland Dreier hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn)); 3483e48354ceSNicholas Bellinger 3484e4f4e801SSagi Grimberg pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x," 3485e4f4e801SSagi Grimberg " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag, 3486e4f4e801SSagi Grimberg cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid, 3487e4f4e801SSagi Grimberg !!(hdr->flags & ISCSI_FLAG_CMD_FINAL), 3488e4f4e801SSagi Grimberg !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)); 3489e48354ceSNicholas Bellinger 3490889c8a68SNicholas Bellinger return text_length + padding; 3491889c8a68SNicholas Bellinger } 3492889c8a68SNicholas Bellinger EXPORT_SYMBOL(iscsit_build_text_rsp); 3493889c8a68SNicholas Bellinger 3494889c8a68SNicholas Bellinger static int iscsit_send_text_rsp( 3495889c8a68SNicholas Bellinger struct iscsi_cmd *cmd, 3496889c8a68SNicholas Bellinger struct iscsi_conn *conn) 3497889c8a68SNicholas Bellinger { 3498889c8a68SNicholas Bellinger struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu; 34992854bb23SVarun Prakash int text_length; 3500889c8a68SNicholas Bellinger 3501864e504aSVarun Prakash text_length = iscsit_build_text_rsp(cmd, conn, hdr, 3502864e504aSVarun Prakash conn->conn_transport->transport_type); 35032854bb23SVarun Prakash if (text_length < 0) 35042854bb23SVarun Prakash return text_length; 3505889c8a68SNicholas Bellinger 35062854bb23SVarun Prakash return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, 35072854bb23SVarun Prakash cmd->buf_ptr, 35082854bb23SVarun Prakash text_length); 3509e48354ceSNicholas Bellinger } 3510e48354ceSNicholas Bellinger 35112ec5a8c1SNicholas Bellinger void 35122ec5a8c1SNicholas Bellinger iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 35132ec5a8c1SNicholas Bellinger struct iscsi_reject *hdr) 3514e48354ceSNicholas Bellinger { 3515e48354ceSNicholas Bellinger hdr->opcode = ISCSI_OP_REJECT; 3516ba159914SNicholas Bellinger hdr->reason = cmd->reject_reason; 3517e48354ceSNicholas Bellinger hdr->flags |= ISCSI_FLAG_CMD_FINAL; 3518e48354ceSNicholas Bellinger hton24(hdr->dlength, ISCSI_HDR_LEN); 351950e5c87dSChristoph Hellwig hdr->ffffffff = cpu_to_be32(0xffffffff); 3520e48354ceSNicholas Bellinger cmd->stat_sn = conn->stat_sn++; 3521e48354ceSNicholas Bellinger hdr->statsn = cpu_to_be32(cmd->stat_sn); 3522e48354ceSNicholas Bellinger hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 3523109e2381SRoland Dreier hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn)); 3524e48354ceSNicholas Bellinger 35252ec5a8c1SNicholas Bellinger } 35262ec5a8c1SNicholas Bellinger EXPORT_SYMBOL(iscsit_build_reject); 3527e48354ceSNicholas Bellinger 35282ec5a8c1SNicholas Bellinger static int iscsit_send_reject( 35292ec5a8c1SNicholas Bellinger struct iscsi_cmd *cmd, 35302ec5a8c1SNicholas Bellinger struct iscsi_conn *conn) 35312ec5a8c1SNicholas Bellinger { 3532bfbdb31dSNicholas Bellinger struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0]; 35332ec5a8c1SNicholas Bellinger 3534bfbdb31dSNicholas Bellinger iscsit_build_reject(cmd, conn, hdr); 35352ec5a8c1SNicholas Bellinger 3536e48354ceSNicholas Bellinger pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x," 3537e48354ceSNicholas Bellinger " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid); 3538e48354ceSNicholas Bellinger 35392854bb23SVarun Prakash return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, 35402854bb23SVarun Prakash cmd->buf_ptr, 35412854bb23SVarun Prakash ISCSI_HDR_LEN); 3542e48354ceSNicholas Bellinger } 3543e48354ceSNicholas Bellinger 3544e48354ceSNicholas Bellinger void iscsit_thread_get_cpumask(struct iscsi_conn *conn) 3545e48354ceSNicholas Bellinger { 3546e48354ceSNicholas Bellinger int ord, cpu; 3547e48354ceSNicholas Bellinger /* 354888dcd2daSNicholas Bellinger * bitmap_id is assigned from iscsit_global->ts_bitmap from 354988dcd2daSNicholas Bellinger * within iscsit_start_kthreads() 3550e48354ceSNicholas Bellinger * 355188dcd2daSNicholas Bellinger * Here we use bitmap_id to determine which CPU that this 355288dcd2daSNicholas Bellinger * iSCSI connection's RX/TX threads will be scheduled to 3553e48354ceSNicholas Bellinger * execute upon. 3554e48354ceSNicholas Bellinger */ 355588dcd2daSNicholas Bellinger ord = conn->bitmap_id % cpumask_weight(cpu_online_mask); 3556e48354ceSNicholas Bellinger for_each_online_cpu(cpu) { 3557e48354ceSNicholas Bellinger if (ord-- == 0) { 3558e48354ceSNicholas Bellinger cpumask_set_cpu(cpu, conn->conn_cpumask); 3559e48354ceSNicholas Bellinger return; 3560e48354ceSNicholas Bellinger } 3561e48354ceSNicholas Bellinger } 3562e48354ceSNicholas Bellinger /* 3563e48354ceSNicholas Bellinger * This should never be reached.. 3564e48354ceSNicholas Bellinger */ 3565e48354ceSNicholas Bellinger dump_stack(); 3566e48354ceSNicholas Bellinger cpumask_setall(conn->conn_cpumask); 3567e48354ceSNicholas Bellinger } 3568e48354ceSNicholas Bellinger 3569d2faaefbSVarun Prakash int 35702ec5a8c1SNicholas Bellinger iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state) 3571e48354ceSNicholas Bellinger { 35722ec5a8c1SNicholas Bellinger int ret; 35732ec5a8c1SNicholas Bellinger 35742ec5a8c1SNicholas Bellinger switch (state) { 35752ec5a8c1SNicholas Bellinger case ISTATE_SEND_R2T: 35762ec5a8c1SNicholas Bellinger ret = iscsit_send_r2t(cmd, conn); 35772ec5a8c1SNicholas Bellinger if (ret < 0) 35782ec5a8c1SNicholas Bellinger goto err; 35792ec5a8c1SNicholas Bellinger break; 35802ec5a8c1SNicholas Bellinger case ISTATE_REMOVE: 35812ec5a8c1SNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 35825159d763SNicholas Bellinger list_del_init(&cmd->i_conn_node); 35832ec5a8c1SNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 35842ec5a8c1SNicholas Bellinger 3585aafc9d15SNicholas Bellinger iscsit_free_cmd(cmd, false); 35862ec5a8c1SNicholas Bellinger break; 35872ec5a8c1SNicholas Bellinger case ISTATE_SEND_NOPIN_WANT_RESPONSE: 35882ec5a8c1SNicholas Bellinger iscsit_mod_nopin_response_timer(conn); 35892ec5a8c1SNicholas Bellinger ret = iscsit_send_unsolicited_nopin(cmd, conn, 1); 35902ec5a8c1SNicholas Bellinger if (ret < 0) 35912ec5a8c1SNicholas Bellinger goto err; 35922ec5a8c1SNicholas Bellinger break; 35932ec5a8c1SNicholas Bellinger case ISTATE_SEND_NOPIN_NO_RESPONSE: 35942ec5a8c1SNicholas Bellinger ret = iscsit_send_unsolicited_nopin(cmd, conn, 0); 35952ec5a8c1SNicholas Bellinger if (ret < 0) 35962ec5a8c1SNicholas Bellinger goto err; 35972ec5a8c1SNicholas Bellinger break; 35982ec5a8c1SNicholas Bellinger default: 35992ec5a8c1SNicholas Bellinger pr_err("Unknown Opcode: 0x%02x ITT:" 36002ec5a8c1SNicholas Bellinger " 0x%08x, i_state: %d on CID: %hu\n", 36012ec5a8c1SNicholas Bellinger cmd->iscsi_opcode, cmd->init_task_tag, state, 36022ec5a8c1SNicholas Bellinger conn->cid); 36032ec5a8c1SNicholas Bellinger goto err; 36042ec5a8c1SNicholas Bellinger } 36052ec5a8c1SNicholas Bellinger 36062ec5a8c1SNicholas Bellinger return 0; 36072ec5a8c1SNicholas Bellinger 36082ec5a8c1SNicholas Bellinger err: 36092ec5a8c1SNicholas Bellinger return -1; 36102ec5a8c1SNicholas Bellinger } 3611d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_immediate_queue); 36122ec5a8c1SNicholas Bellinger 36132ec5a8c1SNicholas Bellinger static int 36142ec5a8c1SNicholas Bellinger iscsit_handle_immediate_queue(struct iscsi_conn *conn) 36152ec5a8c1SNicholas Bellinger { 36162ec5a8c1SNicholas Bellinger struct iscsit_transport *t = conn->conn_transport; 36176f3c0e69SAndy Grover struct iscsi_queue_req *qr; 36186f3c0e69SAndy Grover struct iscsi_cmd *cmd; 3619e48354ceSNicholas Bellinger u8 state; 36206f3c0e69SAndy Grover int ret; 3621e48354ceSNicholas Bellinger 3622c6037cc5SAndy Grover while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) { 3623e48354ceSNicholas Bellinger atomic_set(&conn->check_immediate_queue, 0); 3624e48354ceSNicholas Bellinger cmd = qr->cmd; 3625e48354ceSNicholas Bellinger state = qr->state; 3626e48354ceSNicholas Bellinger kmem_cache_free(lio_qr_cache, qr); 3627e48354ceSNicholas Bellinger 36282ec5a8c1SNicholas Bellinger ret = t->iscsit_immediate_queue(conn, cmd, state); 36296f3c0e69SAndy Grover if (ret < 0) 36302ec5a8c1SNicholas Bellinger return ret; 3631e48354ceSNicholas Bellinger } 3632e48354ceSNicholas Bellinger 36336f3c0e69SAndy Grover return 0; 3634e48354ceSNicholas Bellinger } 36356f3c0e69SAndy Grover 3636d2faaefbSVarun Prakash int 36372ec5a8c1SNicholas Bellinger iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state) 36386f3c0e69SAndy Grover { 36396f3c0e69SAndy Grover int ret; 3640e48354ceSNicholas Bellinger 3641e48354ceSNicholas Bellinger check_rsp_state: 3642e48354ceSNicholas Bellinger switch (state) { 3643e48354ceSNicholas Bellinger case ISTATE_SEND_DATAIN: 36442ec5a8c1SNicholas Bellinger ret = iscsit_send_datain(cmd, conn); 36456f3c0e69SAndy Grover if (ret < 0) 36466f3c0e69SAndy Grover goto err; 36476f3c0e69SAndy Grover else if (!ret) 36486f3c0e69SAndy Grover /* more drs */ 36496f3c0e69SAndy Grover goto check_rsp_state; 36506f3c0e69SAndy Grover else if (ret == 1) { 36516f3c0e69SAndy Grover /* all done */ 36526f3c0e69SAndy Grover spin_lock_bh(&cmd->istate_lock); 36536f3c0e69SAndy Grover cmd->i_state = ISTATE_SENT_STATUS; 3654e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock); 3655fd3a9025SNicholas Bellinger 3656fd3a9025SNicholas Bellinger if (atomic_read(&conn->check_immediate_queue)) 3657fd3a9025SNicholas Bellinger return 1; 3658fd3a9025SNicholas Bellinger 36592ec5a8c1SNicholas Bellinger return 0; 36606f3c0e69SAndy Grover } else if (ret == 2) { 36616f3c0e69SAndy Grover /* Still must send status, 36626f3c0e69SAndy Grover SCF_TRANSPORT_TASK_SENSE was set */ 36636f3c0e69SAndy Grover spin_lock_bh(&cmd->istate_lock); 36646f3c0e69SAndy Grover cmd->i_state = ISTATE_SEND_STATUS; 36656f3c0e69SAndy Grover spin_unlock_bh(&cmd->istate_lock); 36666f3c0e69SAndy Grover state = ISTATE_SEND_STATUS; 36676f3c0e69SAndy Grover goto check_rsp_state; 36686f3c0e69SAndy Grover } 36696f3c0e69SAndy Grover 3670e48354ceSNicholas Bellinger break; 3671e48354ceSNicholas Bellinger case ISTATE_SEND_STATUS: 3672e48354ceSNicholas Bellinger case ISTATE_SEND_STATUS_RECOVERY: 36732ec5a8c1SNicholas Bellinger ret = iscsit_send_response(cmd, conn); 3674e48354ceSNicholas Bellinger break; 3675e48354ceSNicholas Bellinger case ISTATE_SEND_LOGOUTRSP: 36762ec5a8c1SNicholas Bellinger ret = iscsit_send_logout(cmd, conn); 3677e48354ceSNicholas Bellinger break; 3678e48354ceSNicholas Bellinger case ISTATE_SEND_ASYNCMSG: 3679e48354ceSNicholas Bellinger ret = iscsit_send_conn_drop_async_message( 3680e48354ceSNicholas Bellinger cmd, conn); 3681e48354ceSNicholas Bellinger break; 3682e48354ceSNicholas Bellinger case ISTATE_SEND_NOPIN: 36832ec5a8c1SNicholas Bellinger ret = iscsit_send_nopin(cmd, conn); 3684e48354ceSNicholas Bellinger break; 3685e48354ceSNicholas Bellinger case ISTATE_SEND_REJECT: 3686e48354ceSNicholas Bellinger ret = iscsit_send_reject(cmd, conn); 3687e48354ceSNicholas Bellinger break; 3688e48354ceSNicholas Bellinger case ISTATE_SEND_TASKMGTRSP: 3689e48354ceSNicholas Bellinger ret = iscsit_send_task_mgt_rsp(cmd, conn); 3690e48354ceSNicholas Bellinger if (ret != 0) 3691e48354ceSNicholas Bellinger break; 3692e48354ceSNicholas Bellinger ret = iscsit_tmr_post_handler(cmd, conn); 3693e48354ceSNicholas Bellinger if (ret != 0) 3694e48354ceSNicholas Bellinger iscsit_fall_back_to_erl0(conn->sess); 3695e48354ceSNicholas Bellinger break; 3696e48354ceSNicholas Bellinger case ISTATE_SEND_TEXTRSP: 3697e48354ceSNicholas Bellinger ret = iscsit_send_text_rsp(cmd, conn); 3698e48354ceSNicholas Bellinger break; 3699e48354ceSNicholas Bellinger default: 3700e48354ceSNicholas Bellinger pr_err("Unknown Opcode: 0x%02x ITT:" 3701e48354ceSNicholas Bellinger " 0x%08x, i_state: %d on CID: %hu\n", 3702e48354ceSNicholas Bellinger cmd->iscsi_opcode, cmd->init_task_tag, 3703e48354ceSNicholas Bellinger state, conn->cid); 37046f3c0e69SAndy Grover goto err; 3705e48354ceSNicholas Bellinger } 3706c6037cc5SAndy Grover if (ret < 0) 37076f3c0e69SAndy Grover goto err; 3708e48354ceSNicholas Bellinger 3709e48354ceSNicholas Bellinger switch (state) { 37106f3c0e69SAndy Grover case ISTATE_SEND_LOGOUTRSP: 37116f3c0e69SAndy Grover if (!iscsit_logout_post_handler(cmd, conn)) 371288dcd2daSNicholas Bellinger return -ECONNRESET; 37136f3c0e69SAndy Grover /* fall through */ 3714e48354ceSNicholas Bellinger case ISTATE_SEND_STATUS: 3715e48354ceSNicholas Bellinger case ISTATE_SEND_ASYNCMSG: 3716e48354ceSNicholas Bellinger case ISTATE_SEND_NOPIN: 3717e48354ceSNicholas Bellinger case ISTATE_SEND_STATUS_RECOVERY: 3718e48354ceSNicholas Bellinger case ISTATE_SEND_TEXTRSP: 37196f3c0e69SAndy Grover case ISTATE_SEND_TASKMGTRSP: 3720ba159914SNicholas Bellinger case ISTATE_SEND_REJECT: 37216f3c0e69SAndy Grover spin_lock_bh(&cmd->istate_lock); 37226f3c0e69SAndy Grover cmd->i_state = ISTATE_SENT_STATUS; 37236f3c0e69SAndy Grover spin_unlock_bh(&cmd->istate_lock); 3724e48354ceSNicholas Bellinger break; 3725e48354ceSNicholas Bellinger default: 3726e48354ceSNicholas Bellinger pr_err("Unknown Opcode: 0x%02x ITT:" 3727e48354ceSNicholas Bellinger " 0x%08x, i_state: %d on CID: %hu\n", 3728e48354ceSNicholas Bellinger cmd->iscsi_opcode, cmd->init_task_tag, 3729e48354ceSNicholas Bellinger cmd->i_state, conn->cid); 37306f3c0e69SAndy Grover goto err; 3731e48354ceSNicholas Bellinger } 3732e48354ceSNicholas Bellinger 3733e48354ceSNicholas Bellinger if (atomic_read(&conn->check_immediate_queue)) 3734fd3a9025SNicholas Bellinger return 1; 37356f3c0e69SAndy Grover 37366f3c0e69SAndy Grover return 0; 37376f3c0e69SAndy Grover 37386f3c0e69SAndy Grover err: 37396f3c0e69SAndy Grover return -1; 37406f3c0e69SAndy Grover } 3741d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_response_queue); 37426f3c0e69SAndy Grover 37432ec5a8c1SNicholas Bellinger static int iscsit_handle_response_queue(struct iscsi_conn *conn) 37442ec5a8c1SNicholas Bellinger { 37452ec5a8c1SNicholas Bellinger struct iscsit_transport *t = conn->conn_transport; 37462ec5a8c1SNicholas Bellinger struct iscsi_queue_req *qr; 37472ec5a8c1SNicholas Bellinger struct iscsi_cmd *cmd; 37482ec5a8c1SNicholas Bellinger u8 state; 37492ec5a8c1SNicholas Bellinger int ret; 37502ec5a8c1SNicholas Bellinger 37512ec5a8c1SNicholas Bellinger while ((qr = iscsit_get_cmd_from_response_queue(conn))) { 37522ec5a8c1SNicholas Bellinger cmd = qr->cmd; 37532ec5a8c1SNicholas Bellinger state = qr->state; 37542ec5a8c1SNicholas Bellinger kmem_cache_free(lio_qr_cache, qr); 37552ec5a8c1SNicholas Bellinger 37562ec5a8c1SNicholas Bellinger ret = t->iscsit_response_queue(conn, cmd, state); 37572ec5a8c1SNicholas Bellinger if (ret == 1 || ret < 0) 37582ec5a8c1SNicholas Bellinger return ret; 37592ec5a8c1SNicholas Bellinger } 37602ec5a8c1SNicholas Bellinger 37612ec5a8c1SNicholas Bellinger return 0; 37622ec5a8c1SNicholas Bellinger } 37632ec5a8c1SNicholas Bellinger 37646f3c0e69SAndy Grover int iscsi_target_tx_thread(void *arg) 37656f3c0e69SAndy Grover { 37666f3c0e69SAndy Grover int ret = 0; 376788dcd2daSNicholas Bellinger struct iscsi_conn *conn = arg; 37685e0cf5e6SJiang Yi bool conn_freed = false; 37695e0cf5e6SJiang Yi 37706f3c0e69SAndy Grover /* 37716f3c0e69SAndy Grover * Allow ourselves to be interrupted by SIGINT so that a 37726f3c0e69SAndy Grover * connection recovery / failure event can be triggered externally. 37736f3c0e69SAndy Grover */ 37746f3c0e69SAndy Grover allow_signal(SIGINT); 37756f3c0e69SAndy Grover 37766f3c0e69SAndy Grover while (!kthread_should_stop()) { 37776f3c0e69SAndy Grover /* 37786f3c0e69SAndy Grover * Ensure that both TX and RX per connection kthreads 37796f3c0e69SAndy Grover * are scheduled to run on the same CPU. 37806f3c0e69SAndy Grover */ 37816f3c0e69SAndy Grover iscsit_thread_check_cpumask(conn, current, 1); 37826f3c0e69SAndy Grover 3783d5627acbSRoland Dreier wait_event_interruptible(conn->queues_wq, 378488dcd2daSNicholas Bellinger !iscsit_conn_all_queues_empty(conn)); 37856f3c0e69SAndy Grover 378688dcd2daSNicholas Bellinger if (signal_pending(current)) 37876f3c0e69SAndy Grover goto transport_err; 37886f3c0e69SAndy Grover 3789fd3a9025SNicholas Bellinger get_immediate: 37902ec5a8c1SNicholas Bellinger ret = iscsit_handle_immediate_queue(conn); 37916f3c0e69SAndy Grover if (ret < 0) 37926f3c0e69SAndy Grover goto transport_err; 37936f3c0e69SAndy Grover 37942ec5a8c1SNicholas Bellinger ret = iscsit_handle_response_queue(conn); 37955e0cf5e6SJiang Yi if (ret == 1) { 3796fd3a9025SNicholas Bellinger goto get_immediate; 37975e0cf5e6SJiang Yi } else if (ret == -ECONNRESET) { 37985e0cf5e6SJiang Yi conn_freed = true; 379988dcd2daSNicholas Bellinger goto out; 38005e0cf5e6SJiang Yi } else if (ret < 0) { 38016f3c0e69SAndy Grover goto transport_err; 3802e48354ceSNicholas Bellinger } 38035e0cf5e6SJiang Yi } 3804e48354ceSNicholas Bellinger 3805e48354ceSNicholas Bellinger transport_err: 3806e5419865SNicholas Bellinger /* 3807e5419865SNicholas Bellinger * Avoid the normal connection failure code-path if this connection 3808e5419865SNicholas Bellinger * is still within LOGIN mode, and iscsi_np process context is 3809e5419865SNicholas Bellinger * responsible for cleaning up the early connection failure. 3810e5419865SNicholas Bellinger */ 3811e5419865SNicholas Bellinger if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN) 38125e0cf5e6SJiang Yi iscsit_take_action_for_connection_exit(conn, &conn_freed); 3813e48354ceSNicholas Bellinger out: 38145e0cf5e6SJiang Yi if (!conn_freed) { 38155e0cf5e6SJiang Yi while (!kthread_should_stop()) { 38165e0cf5e6SJiang Yi msleep(100); 38175e0cf5e6SJiang Yi } 38185e0cf5e6SJiang Yi } 3819e48354ceSNicholas Bellinger return 0; 3820e48354ceSNicholas Bellinger } 3821e48354ceSNicholas Bellinger 38223e1c81a9SNicholas Bellinger static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf) 38233e1c81a9SNicholas Bellinger { 38243e1c81a9SNicholas Bellinger struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf; 38253e1c81a9SNicholas Bellinger struct iscsi_cmd *cmd; 38263e1c81a9SNicholas Bellinger int ret = 0; 38273e1c81a9SNicholas Bellinger 38283e1c81a9SNicholas Bellinger switch (hdr->opcode & ISCSI_OPCODE_MASK) { 38293e1c81a9SNicholas Bellinger case ISCSI_OP_SCSI_CMD: 3830676687c6SNicholas Bellinger cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); 38313e1c81a9SNicholas Bellinger if (!cmd) 3832ba159914SNicholas Bellinger goto reject; 38333e1c81a9SNicholas Bellinger 38343e1c81a9SNicholas Bellinger ret = iscsit_handle_scsi_cmd(conn, cmd, buf); 38353e1c81a9SNicholas Bellinger break; 38363e1c81a9SNicholas Bellinger case ISCSI_OP_SCSI_DATA_OUT: 38373e1c81a9SNicholas Bellinger ret = iscsit_handle_data_out(conn, buf); 38383e1c81a9SNicholas Bellinger break; 38393e1c81a9SNicholas Bellinger case ISCSI_OP_NOOP_OUT: 38403e1c81a9SNicholas Bellinger cmd = NULL; 38413e1c81a9SNicholas Bellinger if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) { 3842676687c6SNicholas Bellinger cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); 38433e1c81a9SNicholas Bellinger if (!cmd) 3844ba159914SNicholas Bellinger goto reject; 38453e1c81a9SNicholas Bellinger } 38463e1c81a9SNicholas Bellinger ret = iscsit_handle_nop_out(conn, cmd, buf); 38473e1c81a9SNicholas Bellinger break; 38483e1c81a9SNicholas Bellinger case ISCSI_OP_SCSI_TMFUNC: 3849676687c6SNicholas Bellinger cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); 38503e1c81a9SNicholas Bellinger if (!cmd) 3851ba159914SNicholas Bellinger goto reject; 38523e1c81a9SNicholas Bellinger 38533e1c81a9SNicholas Bellinger ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf); 38543e1c81a9SNicholas Bellinger break; 38553e1c81a9SNicholas Bellinger case ISCSI_OP_TEXT: 3856e4f4e801SSagi Grimberg if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) { 3857e4f4e801SSagi Grimberg cmd = iscsit_find_cmd_from_itt(conn, hdr->itt); 3858e4f4e801SSagi Grimberg if (!cmd) 3859e4f4e801SSagi Grimberg goto reject; 3860e4f4e801SSagi Grimberg } else { 3861676687c6SNicholas Bellinger cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); 386264534aa7SNicholas Bellinger if (!cmd) 3863ba159914SNicholas Bellinger goto reject; 3864e4f4e801SSagi Grimberg } 386564534aa7SNicholas Bellinger 386664534aa7SNicholas Bellinger ret = iscsit_handle_text_cmd(conn, cmd, buf); 38673e1c81a9SNicholas Bellinger break; 38683e1c81a9SNicholas Bellinger case ISCSI_OP_LOGOUT: 3869676687c6SNicholas Bellinger cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); 38703e1c81a9SNicholas Bellinger if (!cmd) 3871ba159914SNicholas Bellinger goto reject; 38723e1c81a9SNicholas Bellinger 38733e1c81a9SNicholas Bellinger ret = iscsit_handle_logout_cmd(conn, cmd, buf); 38743e1c81a9SNicholas Bellinger if (ret > 0) 38753e1c81a9SNicholas Bellinger wait_for_completion_timeout(&conn->conn_logout_comp, 38763e1c81a9SNicholas Bellinger SECONDS_FOR_LOGOUT_COMP * HZ); 38773e1c81a9SNicholas Bellinger break; 38783e1c81a9SNicholas Bellinger case ISCSI_OP_SNACK: 38793e1c81a9SNicholas Bellinger ret = iscsit_handle_snack(conn, buf); 38803e1c81a9SNicholas Bellinger break; 38813e1c81a9SNicholas Bellinger default: 38823e1c81a9SNicholas Bellinger pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode); 38833e1c81a9SNicholas Bellinger if (!conn->sess->sess_ops->ErrorRecoveryLevel) { 38843e1c81a9SNicholas Bellinger pr_err("Cannot recover from unknown" 38853e1c81a9SNicholas Bellinger " opcode while ERL=0, closing iSCSI connection.\n"); 38863e1c81a9SNicholas Bellinger return -1; 38873e1c81a9SNicholas Bellinger } 3888c04a6091SChristophe Vu-Brugier pr_err("Unable to recover from unknown opcode while OFMarker=No," 3889c04a6091SChristophe Vu-Brugier " closing iSCSI connection.\n"); 3890c04a6091SChristophe Vu-Brugier ret = -1; 38913e1c81a9SNicholas Bellinger break; 38923e1c81a9SNicholas Bellinger } 38933e1c81a9SNicholas Bellinger 38943e1c81a9SNicholas Bellinger return ret; 3895ba159914SNicholas Bellinger reject: 3896ba159914SNicholas Bellinger return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); 38973e1c81a9SNicholas Bellinger } 38983e1c81a9SNicholas Bellinger 3899ca82c2bdSNicholas Bellinger static bool iscsi_target_check_conn_state(struct iscsi_conn *conn) 3900ca82c2bdSNicholas Bellinger { 3901ca82c2bdSNicholas Bellinger bool ret; 3902ca82c2bdSNicholas Bellinger 3903ca82c2bdSNicholas Bellinger spin_lock_bh(&conn->state_lock); 3904ca82c2bdSNicholas Bellinger ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN); 3905ca82c2bdSNicholas Bellinger spin_unlock_bh(&conn->state_lock); 3906ca82c2bdSNicholas Bellinger 3907ca82c2bdSNicholas Bellinger return ret; 3908ca82c2bdSNicholas Bellinger } 3909ca82c2bdSNicholas Bellinger 3910e8205ccaSVarun Prakash static void iscsit_get_rx_pdu(struct iscsi_conn *conn) 3911e48354ceSNicholas Bellinger { 3912e8205ccaSVarun Prakash int ret; 3913e48354ceSNicholas Bellinger u8 buffer[ISCSI_HDR_LEN], opcode; 3914e48354ceSNicholas Bellinger u32 checksum = 0, digest = 0; 3915e48354ceSNicholas Bellinger struct kvec iov; 39163e1c81a9SNicholas Bellinger 3917e48354ceSNicholas Bellinger while (!kthread_should_stop()) { 3918e48354ceSNicholas Bellinger /* 3919e48354ceSNicholas Bellinger * Ensure that both TX and RX per connection kthreads 3920e48354ceSNicholas Bellinger * are scheduled to run on the same CPU. 3921e48354ceSNicholas Bellinger */ 3922e48354ceSNicholas Bellinger iscsit_thread_check_cpumask(conn, current, 0); 3923e48354ceSNicholas Bellinger 3924e48354ceSNicholas Bellinger memset(buffer, 0, ISCSI_HDR_LEN); 3925e48354ceSNicholas Bellinger memset(&iov, 0, sizeof(struct kvec)); 3926e48354ceSNicholas Bellinger 3927e48354ceSNicholas Bellinger iov.iov_base = buffer; 3928e48354ceSNicholas Bellinger iov.iov_len = ISCSI_HDR_LEN; 3929e48354ceSNicholas Bellinger 3930e48354ceSNicholas Bellinger ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN); 3931e48354ceSNicholas Bellinger if (ret != ISCSI_HDR_LEN) { 3932e48354ceSNicholas Bellinger iscsit_rx_thread_wait_for_tcp(conn); 3933e8205ccaSVarun Prakash return; 3934e48354ceSNicholas Bellinger } 3935e48354ceSNicholas Bellinger 3936e48354ceSNicholas Bellinger if (conn->conn_ops->HeaderDigest) { 3937e48354ceSNicholas Bellinger iov.iov_base = &digest; 3938e48354ceSNicholas Bellinger iov.iov_len = ISCSI_CRC_LEN; 3939e48354ceSNicholas Bellinger 3940e48354ceSNicholas Bellinger ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN); 3941e48354ceSNicholas Bellinger if (ret != ISCSI_CRC_LEN) { 3942e48354ceSNicholas Bellinger iscsit_rx_thread_wait_for_tcp(conn); 3943e8205ccaSVarun Prakash return; 3944e48354ceSNicholas Bellinger } 3945e48354ceSNicholas Bellinger 3946e1dfb21fSBart Van Assche iscsit_do_crypto_hash_buf(conn->conn_rx_hash, buffer, 3947e1dfb21fSBart Van Assche ISCSI_HDR_LEN, 0, NULL, 3948e1dfb21fSBart Van Assche &checksum); 3949e48354ceSNicholas Bellinger 3950e48354ceSNicholas Bellinger if (digest != checksum) { 3951e48354ceSNicholas Bellinger pr_err("HeaderDigest CRC32C failed," 3952e48354ceSNicholas Bellinger " received 0x%08x, computed 0x%08x\n", 3953e48354ceSNicholas Bellinger digest, checksum); 3954e48354ceSNicholas Bellinger /* 3955e48354ceSNicholas Bellinger * Set the PDU to 0xff so it will intentionally 3956e48354ceSNicholas Bellinger * hit default in the switch below. 3957e48354ceSNicholas Bellinger */ 3958e48354ceSNicholas Bellinger memset(buffer, 0xff, ISCSI_HDR_LEN); 395904f3b31bSNicholas Bellinger atomic_long_inc(&conn->sess->conn_digest_errors); 3960e48354ceSNicholas Bellinger } else { 3961e48354ceSNicholas Bellinger pr_debug("Got HeaderDigest CRC32C" 3962e48354ceSNicholas Bellinger " 0x%08x\n", checksum); 3963e48354ceSNicholas Bellinger } 3964e48354ceSNicholas Bellinger } 3965e48354ceSNicholas Bellinger 3966e48354ceSNicholas Bellinger if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT) 3967e8205ccaSVarun Prakash return; 3968e48354ceSNicholas Bellinger 3969e48354ceSNicholas Bellinger opcode = buffer[0] & ISCSI_OPCODE_MASK; 3970e48354ceSNicholas Bellinger 3971e48354ceSNicholas Bellinger if (conn->sess->sess_ops->SessionType && 3972e48354ceSNicholas Bellinger ((!(opcode & ISCSI_OP_TEXT)) || 3973e48354ceSNicholas Bellinger (!(opcode & ISCSI_OP_LOGOUT)))) { 3974e48354ceSNicholas Bellinger pr_err("Received illegal iSCSI Opcode: 0x%02x" 3975e48354ceSNicholas Bellinger " while in Discovery Session, rejecting.\n", opcode); 3976ba159914SNicholas Bellinger iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 3977ba159914SNicholas Bellinger buffer); 3978e8205ccaSVarun Prakash return; 3979e48354ceSNicholas Bellinger } 3980e48354ceSNicholas Bellinger 39813e1c81a9SNicholas Bellinger ret = iscsi_target_rx_opcode(conn, buffer); 39823e1c81a9SNicholas Bellinger if (ret < 0) 3983e8205ccaSVarun Prakash return; 3984e8205ccaSVarun Prakash } 3985e48354ceSNicholas Bellinger } 3986e48354ceSNicholas Bellinger 3987e8205ccaSVarun Prakash int iscsi_target_rx_thread(void *arg) 3988e8205ccaSVarun Prakash { 3989e8205ccaSVarun Prakash int rc; 3990e8205ccaSVarun Prakash struct iscsi_conn *conn = arg; 39915e0cf5e6SJiang Yi bool conn_freed = false; 3992e8205ccaSVarun Prakash 3993e8205ccaSVarun Prakash /* 3994e8205ccaSVarun Prakash * Allow ourselves to be interrupted by SIGINT so that a 3995e8205ccaSVarun Prakash * connection recovery / failure event can be triggered externally. 3996e8205ccaSVarun Prakash */ 3997e8205ccaSVarun Prakash allow_signal(SIGINT); 3998e8205ccaSVarun Prakash /* 3999e8205ccaSVarun Prakash * Wait for iscsi_post_login_handler() to complete before allowing 4000e8205ccaSVarun Prakash * incoming iscsi/tcp socket I/O, and/or failing the connection. 4001e8205ccaSVarun Prakash */ 4002e8205ccaSVarun Prakash rc = wait_for_completion_interruptible(&conn->rx_login_comp); 4003e8205ccaSVarun Prakash if (rc < 0 || iscsi_target_check_conn_state(conn)) 40045e0cf5e6SJiang Yi goto out; 4005e8205ccaSVarun Prakash 4006e8205ccaSVarun Prakash if (!conn->conn_transport->iscsit_get_rx_pdu) 4007e8205ccaSVarun Prakash return 0; 4008e8205ccaSVarun Prakash 4009e8205ccaSVarun Prakash conn->conn_transport->iscsit_get_rx_pdu(conn); 4010e8205ccaSVarun Prakash 4011e48354ceSNicholas Bellinger if (!signal_pending(current)) 4012e48354ceSNicholas Bellinger atomic_set(&conn->transport_failed, 1); 40135e0cf5e6SJiang Yi iscsit_take_action_for_connection_exit(conn, &conn_freed); 40145e0cf5e6SJiang Yi 40155e0cf5e6SJiang Yi out: 40165e0cf5e6SJiang Yi if (!conn_freed) { 40175e0cf5e6SJiang Yi while (!kthread_should_stop()) { 40185e0cf5e6SJiang Yi msleep(100); 40195e0cf5e6SJiang Yi } 40205e0cf5e6SJiang Yi } 40215e0cf5e6SJiang Yi 4022e48354ceSNicholas Bellinger return 0; 4023e48354ceSNicholas Bellinger } 4024e48354ceSNicholas Bellinger 4025e48354ceSNicholas Bellinger static void iscsit_release_commands_from_conn(struct iscsi_conn *conn) 4026e48354ceSNicholas Bellinger { 4027064cdd2dSNicholas Bellinger LIST_HEAD(tmp_list); 4028e48354ceSNicholas Bellinger struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL; 4029e48354ceSNicholas Bellinger struct iscsi_session *sess = conn->sess; 4030e48354ceSNicholas Bellinger /* 4031e48354ceSNicholas Bellinger * We expect this function to only ever be called from either RX or TX 4032e48354ceSNicholas Bellinger * thread context via iscsit_close_connection() once the other context 4033e48354ceSNicholas Bellinger * has been reset -> returned sleeping pre-handler state. 4034e48354ceSNicholas Bellinger */ 4035e48354ceSNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 4036064cdd2dSNicholas Bellinger list_splice_init(&conn->conn_cmd_list, &tmp_list); 4037e48354ceSNicholas Bellinger 4038064cdd2dSNicholas Bellinger list_for_each_entry(cmd, &tmp_list, i_conn_node) { 4039064cdd2dSNicholas Bellinger struct se_cmd *se_cmd = &cmd->se_cmd; 4040e48354ceSNicholas Bellinger 4041064cdd2dSNicholas Bellinger if (se_cmd->se_tfo != NULL) { 4042064cdd2dSNicholas Bellinger spin_lock(&se_cmd->t_state_lock); 4043064cdd2dSNicholas Bellinger se_cmd->transport_state |= CMD_T_FABRIC_STOP; 4044064cdd2dSNicholas Bellinger spin_unlock(&se_cmd->t_state_lock); 4045064cdd2dSNicholas Bellinger } 4046e48354ceSNicholas Bellinger } 4047e48354ceSNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 4048064cdd2dSNicholas Bellinger 4049064cdd2dSNicholas Bellinger list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) { 4050064cdd2dSNicholas Bellinger list_del_init(&cmd->i_conn_node); 4051064cdd2dSNicholas Bellinger 4052064cdd2dSNicholas Bellinger iscsit_increment_maxcmdsn(cmd, sess); 4053064cdd2dSNicholas Bellinger iscsit_free_cmd(cmd, true); 4054064cdd2dSNicholas Bellinger 4055064cdd2dSNicholas Bellinger } 4056e48354ceSNicholas Bellinger } 4057e48354ceSNicholas Bellinger 4058e48354ceSNicholas Bellinger static void iscsit_stop_timers_for_cmds( 4059e48354ceSNicholas Bellinger struct iscsi_conn *conn) 4060e48354ceSNicholas Bellinger { 4061e48354ceSNicholas Bellinger struct iscsi_cmd *cmd; 4062e48354ceSNicholas Bellinger 4063e48354ceSNicholas Bellinger spin_lock_bh(&conn->cmd_lock); 40642fbb471eSAndy Grover list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) { 4065e48354ceSNicholas Bellinger if (cmd->data_direction == DMA_TO_DEVICE) 4066e48354ceSNicholas Bellinger iscsit_stop_dataout_timer(cmd); 4067e48354ceSNicholas Bellinger } 4068e48354ceSNicholas Bellinger spin_unlock_bh(&conn->cmd_lock); 4069e48354ceSNicholas Bellinger } 4070e48354ceSNicholas Bellinger 4071e48354ceSNicholas Bellinger int iscsit_close_connection( 4072e48354ceSNicholas Bellinger struct iscsi_conn *conn) 4073e48354ceSNicholas Bellinger { 4074e48354ceSNicholas Bellinger int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT); 4075e48354ceSNicholas Bellinger struct iscsi_session *sess = conn->sess; 4076e48354ceSNicholas Bellinger 4077e48354ceSNicholas Bellinger pr_debug("Closing iSCSI connection CID %hu on SID:" 4078e48354ceSNicholas Bellinger " %u\n", conn->cid, sess->sid); 4079e48354ceSNicholas Bellinger /* 4080b4869ee9SVarun Prakash * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD 4081b4869ee9SVarun Prakash * case just in case the RX Thread in iscsi_target_rx_opcode() is 4082b4869ee9SVarun Prakash * sleeping and the logout response never got sent because the 4083b4869ee9SVarun Prakash * connection failed. 4084f068fbc8SNicholas Bellinger * 4085f068fbc8SNicholas Bellinger * However for iser-target, isert_wait4logout() is using conn_logout_comp 4086f068fbc8SNicholas Bellinger * to signal logout response TX interrupt completion. Go ahead and skip 4087f068fbc8SNicholas Bellinger * this for iser since isert_rx_opcode() does not wait on logout failure, 4088f068fbc8SNicholas Bellinger * and to avoid iscsi_conn pointer dereference in iser-target code. 4089e48354ceSNicholas Bellinger */ 4090bd027d85SNicholas Bellinger if (!conn->conn_transport->rdma_shutdown) 4091e48354ceSNicholas Bellinger complete(&conn->conn_logout_comp); 4092e48354ceSNicholas Bellinger 409388dcd2daSNicholas Bellinger if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) { 409488dcd2daSNicholas Bellinger if (conn->tx_thread && 409588dcd2daSNicholas Bellinger cmpxchg(&conn->tx_thread_active, true, false)) { 409688dcd2daSNicholas Bellinger send_sig(SIGINT, conn->tx_thread, 1); 409788dcd2daSNicholas Bellinger kthread_stop(conn->tx_thread); 409888dcd2daSNicholas Bellinger } 409988dcd2daSNicholas Bellinger } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) { 410088dcd2daSNicholas Bellinger if (conn->rx_thread && 410188dcd2daSNicholas Bellinger cmpxchg(&conn->rx_thread_active, true, false)) { 410288dcd2daSNicholas Bellinger send_sig(SIGINT, conn->rx_thread, 1); 410388dcd2daSNicholas Bellinger kthread_stop(conn->rx_thread); 410488dcd2daSNicholas Bellinger } 410588dcd2daSNicholas Bellinger } 410688dcd2daSNicholas Bellinger 410788dcd2daSNicholas Bellinger spin_lock(&iscsit_global->ts_bitmap_lock); 410888dcd2daSNicholas Bellinger bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id, 410988dcd2daSNicholas Bellinger get_order(1)); 411088dcd2daSNicholas Bellinger spin_unlock(&iscsit_global->ts_bitmap_lock); 4111e48354ceSNicholas Bellinger 4112e48354ceSNicholas Bellinger iscsit_stop_timers_for_cmds(conn); 4113e48354ceSNicholas Bellinger iscsit_stop_nopin_response_timer(conn); 4114e48354ceSNicholas Bellinger iscsit_stop_nopin_timer(conn); 4115defd8848SNicholas Bellinger 4116defd8848SNicholas Bellinger if (conn->conn_transport->iscsit_wait_conn) 4117defd8848SNicholas Bellinger conn->conn_transport->iscsit_wait_conn(conn); 4118defd8848SNicholas Bellinger 4119e48354ceSNicholas Bellinger /* 4120e48354ceSNicholas Bellinger * During Connection recovery drop unacknowledged out of order 4121e48354ceSNicholas Bellinger * commands for this connection, and prepare the other commands 412253c561dcSBart Van Assche * for reallegiance. 4123e48354ceSNicholas Bellinger * 4124e48354ceSNicholas Bellinger * During normal operation clear the out of order commands (but 4125e48354ceSNicholas Bellinger * do not free the struct iscsi_ooo_cmdsn's) and release all 4126e48354ceSNicholas Bellinger * struct iscsi_cmds. 4127e48354ceSNicholas Bellinger */ 4128e48354ceSNicholas Bellinger if (atomic_read(&conn->connection_recovery)) { 4129e48354ceSNicholas Bellinger iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn); 413053c561dcSBart Van Assche iscsit_prepare_cmds_for_reallegiance(conn); 4131e48354ceSNicholas Bellinger } else { 4132e48354ceSNicholas Bellinger iscsit_clear_ooo_cmdsns_for_conn(conn); 4133e48354ceSNicholas Bellinger iscsit_release_commands_from_conn(conn); 4134e48354ceSNicholas Bellinger } 4135bbc05048SNicholas Bellinger iscsit_free_queue_reqs_for_conn(conn); 4136e48354ceSNicholas Bellinger 4137e48354ceSNicholas Bellinger /* 4138e48354ceSNicholas Bellinger * Handle decrementing session or connection usage count if 4139e48354ceSNicholas Bellinger * a logout response was not able to be sent because the 4140e48354ceSNicholas Bellinger * connection failed. Fall back to Session Recovery here. 4141e48354ceSNicholas Bellinger */ 4142e48354ceSNicholas Bellinger if (atomic_read(&conn->conn_logout_remove)) { 4143e48354ceSNicholas Bellinger if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) { 4144e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(conn); 4145e48354ceSNicholas Bellinger iscsit_dec_session_usage_count(sess); 4146e48354ceSNicholas Bellinger } 4147e48354ceSNicholas Bellinger if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) 4148e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(conn); 4149e48354ceSNicholas Bellinger 4150e48354ceSNicholas Bellinger atomic_set(&conn->conn_logout_remove, 0); 4151e48354ceSNicholas Bellinger atomic_set(&sess->session_reinstatement, 0); 4152e48354ceSNicholas Bellinger atomic_set(&sess->session_fall_back_to_erl0, 1); 4153e48354ceSNicholas Bellinger } 4154e48354ceSNicholas Bellinger 4155e48354ceSNicholas Bellinger spin_lock_bh(&sess->conn_lock); 4156e48354ceSNicholas Bellinger list_del(&conn->conn_list); 4157e48354ceSNicholas Bellinger 4158e48354ceSNicholas Bellinger /* 4159e48354ceSNicholas Bellinger * Attempt to let the Initiator know this connection failed by 4160e48354ceSNicholas Bellinger * sending an Connection Dropped Async Message on another 4161e48354ceSNicholas Bellinger * active connection. 4162e48354ceSNicholas Bellinger */ 4163e48354ceSNicholas Bellinger if (atomic_read(&conn->connection_recovery)) 4164e48354ceSNicholas Bellinger iscsit_build_conn_drop_async_message(conn); 4165e48354ceSNicholas Bellinger 4166e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4167e48354ceSNicholas Bellinger 4168e48354ceSNicholas Bellinger /* 4169e48354ceSNicholas Bellinger * If connection reinstatement is being performed on this connection, 4170e48354ceSNicholas Bellinger * up the connection reinstatement semaphore that is being blocked on 4171e48354ceSNicholas Bellinger * in iscsit_cause_connection_reinstatement(). 4172e48354ceSNicholas Bellinger */ 4173e48354ceSNicholas Bellinger spin_lock_bh(&conn->state_lock); 4174e48354ceSNicholas Bellinger if (atomic_read(&conn->sleep_on_conn_wait_comp)) { 4175e48354ceSNicholas Bellinger spin_unlock_bh(&conn->state_lock); 4176e48354ceSNicholas Bellinger complete(&conn->conn_wait_comp); 4177e48354ceSNicholas Bellinger wait_for_completion(&conn->conn_post_wait_comp); 4178e48354ceSNicholas Bellinger spin_lock_bh(&conn->state_lock); 4179e48354ceSNicholas Bellinger } 4180e48354ceSNicholas Bellinger 4181e48354ceSNicholas Bellinger /* 4182e48354ceSNicholas Bellinger * If connection reinstatement is being performed on this connection 4183e48354ceSNicholas Bellinger * by receiving a REMOVECONNFORRECOVERY logout request, up the 4184e48354ceSNicholas Bellinger * connection wait rcfr semaphore that is being blocked on 4185e48354ceSNicholas Bellinger * an iscsit_connection_reinstatement_rcfr(). 4186e48354ceSNicholas Bellinger */ 4187e48354ceSNicholas Bellinger if (atomic_read(&conn->connection_wait_rcfr)) { 4188e48354ceSNicholas Bellinger spin_unlock_bh(&conn->state_lock); 4189e48354ceSNicholas Bellinger complete(&conn->conn_wait_rcfr_comp); 4190e48354ceSNicholas Bellinger wait_for_completion(&conn->conn_post_wait_comp); 4191e48354ceSNicholas Bellinger spin_lock_bh(&conn->state_lock); 4192e48354ceSNicholas Bellinger } 4193e48354ceSNicholas Bellinger atomic_set(&conn->connection_reinstatement, 1); 4194e48354ceSNicholas Bellinger spin_unlock_bh(&conn->state_lock); 4195e48354ceSNicholas Bellinger 4196e48354ceSNicholas Bellinger /* 4197e48354ceSNicholas Bellinger * If any other processes are accessing this connection pointer we 4198e48354ceSNicholas Bellinger * must wait until they have completed. 4199e48354ceSNicholas Bellinger */ 4200e48354ceSNicholas Bellinger iscsit_check_conn_usage_count(conn); 4201e48354ceSNicholas Bellinger 420269110e3cSHerbert Xu ahash_request_free(conn->conn_tx_hash); 420369110e3cSHerbert Xu if (conn->conn_rx_hash) { 420469110e3cSHerbert Xu struct crypto_ahash *tfm; 420569110e3cSHerbert Xu 420669110e3cSHerbert Xu tfm = crypto_ahash_reqtfm(conn->conn_rx_hash); 420769110e3cSHerbert Xu ahash_request_free(conn->conn_rx_hash); 420869110e3cSHerbert Xu crypto_free_ahash(tfm); 420969110e3cSHerbert Xu } 4210e48354ceSNicholas Bellinger 4211e48354ceSNicholas Bellinger free_cpumask_var(conn->conn_cpumask); 4212e48354ceSNicholas Bellinger 4213e48354ceSNicholas Bellinger kfree(conn->conn_ops); 4214e48354ceSNicholas Bellinger conn->conn_ops = NULL; 4215e48354ceSNicholas Bellinger 4216bf6932f4SAl Viro if (conn->sock) 4217e48354ceSNicholas Bellinger sock_release(conn->sock); 4218baa4d64bSNicholas Bellinger 4219baa4d64bSNicholas Bellinger if (conn->conn_transport->iscsit_free_conn) 4220baa4d64bSNicholas Bellinger conn->conn_transport->iscsit_free_conn(conn); 4221baa4d64bSNicholas Bellinger 4222baa4d64bSNicholas Bellinger iscsit_put_transport(conn->conn_transport); 4223baa4d64bSNicholas Bellinger 4224e48354ceSNicholas Bellinger pr_debug("Moving to TARG_CONN_STATE_FREE.\n"); 4225e48354ceSNicholas Bellinger conn->conn_state = TARG_CONN_STATE_FREE; 4226e48354ceSNicholas Bellinger kfree(conn); 4227e48354ceSNicholas Bellinger 4228e48354ceSNicholas Bellinger spin_lock_bh(&sess->conn_lock); 4229e48354ceSNicholas Bellinger atomic_dec(&sess->nconn); 4230e48354ceSNicholas Bellinger pr_debug("Decremented iSCSI connection count to %hu from node:" 4231e48354ceSNicholas Bellinger " %s\n", atomic_read(&sess->nconn), 4232e48354ceSNicholas Bellinger sess->sess_ops->InitiatorName); 4233e48354ceSNicholas Bellinger /* 4234e48354ceSNicholas Bellinger * Make sure that if one connection fails in an non ERL=2 iSCSI 4235e48354ceSNicholas Bellinger * Session that they all fail. 4236e48354ceSNicholas Bellinger */ 4237e48354ceSNicholas Bellinger if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout && 4238e48354ceSNicholas Bellinger !atomic_read(&sess->session_logout)) 4239e48354ceSNicholas Bellinger atomic_set(&sess->session_fall_back_to_erl0, 1); 4240e48354ceSNicholas Bellinger 4241e48354ceSNicholas Bellinger /* 4242e48354ceSNicholas Bellinger * If this was not the last connection in the session, and we are 4243e48354ceSNicholas Bellinger * performing session reinstatement or falling back to ERL=0, call 4244e48354ceSNicholas Bellinger * iscsit_stop_session() without sleeping to shutdown the other 4245e48354ceSNicholas Bellinger * active connections. 4246e48354ceSNicholas Bellinger */ 4247e48354ceSNicholas Bellinger if (atomic_read(&sess->nconn)) { 4248e48354ceSNicholas Bellinger if (!atomic_read(&sess->session_reinstatement) && 4249e48354ceSNicholas Bellinger !atomic_read(&sess->session_fall_back_to_erl0)) { 4250e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4251e48354ceSNicholas Bellinger return 0; 4252e48354ceSNicholas Bellinger } 4253e48354ceSNicholas Bellinger if (!atomic_read(&sess->session_stop_active)) { 4254e48354ceSNicholas Bellinger atomic_set(&sess->session_stop_active, 1); 4255e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4256e48354ceSNicholas Bellinger iscsit_stop_session(sess, 0, 0); 4257e48354ceSNicholas Bellinger return 0; 4258e48354ceSNicholas Bellinger } 4259e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4260e48354ceSNicholas Bellinger return 0; 4261e48354ceSNicholas Bellinger } 4262e48354ceSNicholas Bellinger 4263e48354ceSNicholas Bellinger /* 4264e48354ceSNicholas Bellinger * If this was the last connection in the session and one of the 4265e48354ceSNicholas Bellinger * following is occurring: 4266e48354ceSNicholas Bellinger * 4267e48354ceSNicholas Bellinger * Session Reinstatement is not being performed, and are falling back 4268e48354ceSNicholas Bellinger * to ERL=0 call iscsit_close_session(). 4269e48354ceSNicholas Bellinger * 4270e48354ceSNicholas Bellinger * Session Logout was requested. iscsit_close_session() will be called 4271e48354ceSNicholas Bellinger * elsewhere. 4272e48354ceSNicholas Bellinger * 4273e48354ceSNicholas Bellinger * Session Continuation is not being performed, start the Time2Retain 4274e48354ceSNicholas Bellinger * handler and check if sleep_on_sess_wait_sem is active. 4275e48354ceSNicholas Bellinger */ 4276e48354ceSNicholas Bellinger if (!atomic_read(&sess->session_reinstatement) && 4277e48354ceSNicholas Bellinger atomic_read(&sess->session_fall_back_to_erl0)) { 4278e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 427944f33d0fSChristoph Hellwig iscsit_close_session(sess); 4280e48354ceSNicholas Bellinger 4281e48354ceSNicholas Bellinger return 0; 4282e48354ceSNicholas Bellinger } else if (atomic_read(&sess->session_logout)) { 4283e48354ceSNicholas Bellinger pr_debug("Moving to TARG_SESS_STATE_FREE.\n"); 4284e48354ceSNicholas Bellinger sess->session_state = TARG_SESS_STATE_FREE; 4285e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4286e48354ceSNicholas Bellinger 4287e48354ceSNicholas Bellinger if (atomic_read(&sess->sleep_on_sess_wait_comp)) 4288e48354ceSNicholas Bellinger complete(&sess->session_wait_comp); 4289e48354ceSNicholas Bellinger 4290e48354ceSNicholas Bellinger return 0; 4291e48354ceSNicholas Bellinger } else { 4292e48354ceSNicholas Bellinger pr_debug("Moving to TARG_SESS_STATE_FAILED.\n"); 4293e48354ceSNicholas Bellinger sess->session_state = TARG_SESS_STATE_FAILED; 4294e48354ceSNicholas Bellinger 4295e48354ceSNicholas Bellinger if (!atomic_read(&sess->session_continuation)) { 4296e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4297e48354ceSNicholas Bellinger iscsit_start_time2retain_handler(sess); 4298e48354ceSNicholas Bellinger } else 4299e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4300e48354ceSNicholas Bellinger 4301e48354ceSNicholas Bellinger if (atomic_read(&sess->sleep_on_sess_wait_comp)) 4302e48354ceSNicholas Bellinger complete(&sess->session_wait_comp); 4303e48354ceSNicholas Bellinger 4304e48354ceSNicholas Bellinger return 0; 4305e48354ceSNicholas Bellinger } 4306e48354ceSNicholas Bellinger } 4307e48354ceSNicholas Bellinger 430844f33d0fSChristoph Hellwig /* 430944f33d0fSChristoph Hellwig * If the iSCSI Session for the iSCSI Initiator Node exists, 431044f33d0fSChristoph Hellwig * forcefully shutdown the iSCSI NEXUS. 431144f33d0fSChristoph Hellwig */ 4312e48354ceSNicholas Bellinger int iscsit_close_session(struct iscsi_session *sess) 4313e48354ceSNicholas Bellinger { 431460bfcf8eSAndy Grover struct iscsi_portal_group *tpg = sess->tpg; 4315e48354ceSNicholas Bellinger struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; 4316e48354ceSNicholas Bellinger 4317e48354ceSNicholas Bellinger if (atomic_read(&sess->nconn)) { 4318e48354ceSNicholas Bellinger pr_err("%d connection(s) still exist for iSCSI session" 4319e48354ceSNicholas Bellinger " to %s\n", atomic_read(&sess->nconn), 4320e48354ceSNicholas Bellinger sess->sess_ops->InitiatorName); 4321e48354ceSNicholas Bellinger BUG(); 4322e48354ceSNicholas Bellinger } 4323e48354ceSNicholas Bellinger 4324e48354ceSNicholas Bellinger spin_lock_bh(&se_tpg->session_lock); 4325e48354ceSNicholas Bellinger atomic_set(&sess->session_logout, 1); 4326e48354ceSNicholas Bellinger atomic_set(&sess->session_reinstatement, 1); 4327e48354ceSNicholas Bellinger iscsit_stop_time2retain_timer(sess); 4328e48354ceSNicholas Bellinger spin_unlock_bh(&se_tpg->session_lock); 4329e48354ceSNicholas Bellinger 4330e48354ceSNicholas Bellinger /* 4331e48354ceSNicholas Bellinger * transport_deregister_session_configfs() will clear the 4332e48354ceSNicholas Bellinger * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context 4333e48354ceSNicholas Bellinger * can be setting it again with __transport_register_session() in 4334e48354ceSNicholas Bellinger * iscsi_post_login_handler() again after the iscsit_stop_session() 4335e48354ceSNicholas Bellinger * completes in iscsi_np context. 4336e48354ceSNicholas Bellinger */ 4337e48354ceSNicholas Bellinger transport_deregister_session_configfs(sess->se_sess); 4338e48354ceSNicholas Bellinger 4339e48354ceSNicholas Bellinger /* 4340e48354ceSNicholas Bellinger * If any other processes are accessing this session pointer we must 4341e48354ceSNicholas Bellinger * wait until they have completed. If we are in an interrupt (the 4342e48354ceSNicholas Bellinger * time2retain handler) and contain and active session usage count we 4343e48354ceSNicholas Bellinger * restart the timer and exit. 4344e48354ceSNicholas Bellinger */ 4345e48354ceSNicholas Bellinger if (!in_interrupt()) { 4346e48354ceSNicholas Bellinger if (iscsit_check_session_usage_count(sess) == 1) 4347e48354ceSNicholas Bellinger iscsit_stop_session(sess, 1, 1); 4348e48354ceSNicholas Bellinger } else { 4349e48354ceSNicholas Bellinger if (iscsit_check_session_usage_count(sess) == 2) { 4350e48354ceSNicholas Bellinger atomic_set(&sess->session_logout, 0); 4351e48354ceSNicholas Bellinger iscsit_start_time2retain_handler(sess); 4352e48354ceSNicholas Bellinger return 0; 4353e48354ceSNicholas Bellinger } 4354e48354ceSNicholas Bellinger } 4355e48354ceSNicholas Bellinger 4356e48354ceSNicholas Bellinger transport_deregister_session(sess->se_sess); 4357e48354ceSNicholas Bellinger 4358e48354ceSNicholas Bellinger if (sess->sess_ops->ErrorRecoveryLevel == 2) 4359e48354ceSNicholas Bellinger iscsit_free_connection_recovery_entires(sess); 4360e48354ceSNicholas Bellinger 4361e48354ceSNicholas Bellinger iscsit_free_all_ooo_cmdsns(sess); 4362e48354ceSNicholas Bellinger 4363e48354ceSNicholas Bellinger spin_lock_bh(&se_tpg->session_lock); 4364e48354ceSNicholas Bellinger pr_debug("Moving to TARG_SESS_STATE_FREE.\n"); 4365e48354ceSNicholas Bellinger sess->session_state = TARG_SESS_STATE_FREE; 4366e48354ceSNicholas Bellinger pr_debug("Released iSCSI session from node: %s\n", 4367e48354ceSNicholas Bellinger sess->sess_ops->InitiatorName); 4368e48354ceSNicholas Bellinger tpg->nsessions--; 4369e48354ceSNicholas Bellinger if (tpg->tpg_tiqn) 4370e48354ceSNicholas Bellinger tpg->tpg_tiqn->tiqn_nsessions--; 4371e48354ceSNicholas Bellinger 4372e48354ceSNicholas Bellinger pr_debug("Decremented number of active iSCSI Sessions on" 4373e48354ceSNicholas Bellinger " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions); 4374e48354ceSNicholas Bellinger 437531ff0ceeSMatthew Wilcox ida_free(&sess_ida, sess->session_index); 4376e48354ceSNicholas Bellinger kfree(sess->sess_ops); 4377e48354ceSNicholas Bellinger sess->sess_ops = NULL; 4378e48354ceSNicholas Bellinger spin_unlock_bh(&se_tpg->session_lock); 4379e48354ceSNicholas Bellinger 4380e48354ceSNicholas Bellinger kfree(sess); 4381e48354ceSNicholas Bellinger return 0; 4382e48354ceSNicholas Bellinger } 4383e48354ceSNicholas Bellinger 4384e48354ceSNicholas Bellinger static void iscsit_logout_post_handler_closesession( 4385e48354ceSNicholas Bellinger struct iscsi_conn *conn) 4386e48354ceSNicholas Bellinger { 4387e48354ceSNicholas Bellinger struct iscsi_session *sess = conn->sess; 4388007d038bSNicholas Bellinger int sleep = 1; 4389007d038bSNicholas Bellinger /* 4390007d038bSNicholas Bellinger * Traditional iscsi/tcp will invoke this logic from TX thread 4391007d038bSNicholas Bellinger * context during session logout, so clear tx_thread_active and 4392007d038bSNicholas Bellinger * sleep if iscsit_close_connection() has not already occured. 4393007d038bSNicholas Bellinger * 4394007d038bSNicholas Bellinger * Since iser-target invokes this logic from it's own workqueue, 4395007d038bSNicholas Bellinger * always sleep waiting for RX/TX thread shutdown to complete 4396007d038bSNicholas Bellinger * within iscsit_close_connection(). 4397007d038bSNicholas Bellinger */ 4398105fa2f4SNicholas Bellinger if (!conn->conn_transport->rdma_shutdown) { 4399007d038bSNicholas Bellinger sleep = cmpxchg(&conn->tx_thread_active, true, false); 4400105fa2f4SNicholas Bellinger if (!sleep) 4401105fa2f4SNicholas Bellinger return; 4402105fa2f4SNicholas Bellinger } 4403e48354ceSNicholas Bellinger 4404e48354ceSNicholas Bellinger atomic_set(&conn->conn_logout_remove, 0); 4405e48354ceSNicholas Bellinger complete(&conn->conn_logout_comp); 4406e48354ceSNicholas Bellinger 4407e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(conn); 440888dcd2daSNicholas Bellinger iscsit_stop_session(sess, sleep, sleep); 4409e48354ceSNicholas Bellinger iscsit_dec_session_usage_count(sess); 441044f33d0fSChristoph Hellwig iscsit_close_session(sess); 4411e48354ceSNicholas Bellinger } 4412e48354ceSNicholas Bellinger 4413e48354ceSNicholas Bellinger static void iscsit_logout_post_handler_samecid( 4414e48354ceSNicholas Bellinger struct iscsi_conn *conn) 4415e48354ceSNicholas Bellinger { 4416007d038bSNicholas Bellinger int sleep = 1; 4417007d038bSNicholas Bellinger 4418105fa2f4SNicholas Bellinger if (!conn->conn_transport->rdma_shutdown) { 4419007d038bSNicholas Bellinger sleep = cmpxchg(&conn->tx_thread_active, true, false); 4420105fa2f4SNicholas Bellinger if (!sleep) 4421105fa2f4SNicholas Bellinger return; 4422105fa2f4SNicholas Bellinger } 4423e48354ceSNicholas Bellinger 4424e48354ceSNicholas Bellinger atomic_set(&conn->conn_logout_remove, 0); 4425e48354ceSNicholas Bellinger complete(&conn->conn_logout_comp); 4426e48354ceSNicholas Bellinger 442788dcd2daSNicholas Bellinger iscsit_cause_connection_reinstatement(conn, sleep); 4428e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(conn); 4429e48354ceSNicholas Bellinger } 4430e48354ceSNicholas Bellinger 4431e48354ceSNicholas Bellinger static void iscsit_logout_post_handler_diffcid( 4432e48354ceSNicholas Bellinger struct iscsi_conn *conn, 4433e48354ceSNicholas Bellinger u16 cid) 4434e48354ceSNicholas Bellinger { 4435e48354ceSNicholas Bellinger struct iscsi_conn *l_conn; 4436e48354ceSNicholas Bellinger struct iscsi_session *sess = conn->sess; 4437b53b0d99SNicholas Bellinger bool conn_found = false; 4438e48354ceSNicholas Bellinger 4439e48354ceSNicholas Bellinger if (!sess) 4440e48354ceSNicholas Bellinger return; 4441e48354ceSNicholas Bellinger 4442e48354ceSNicholas Bellinger spin_lock_bh(&sess->conn_lock); 4443e48354ceSNicholas Bellinger list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) { 4444e48354ceSNicholas Bellinger if (l_conn->cid == cid) { 4445e48354ceSNicholas Bellinger iscsit_inc_conn_usage_count(l_conn); 4446b53b0d99SNicholas Bellinger conn_found = true; 4447e48354ceSNicholas Bellinger break; 4448e48354ceSNicholas Bellinger } 4449e48354ceSNicholas Bellinger } 4450e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4451e48354ceSNicholas Bellinger 4452b53b0d99SNicholas Bellinger if (!conn_found) 4453e48354ceSNicholas Bellinger return; 4454e48354ceSNicholas Bellinger 4455e48354ceSNicholas Bellinger if (l_conn->sock) 4456e48354ceSNicholas Bellinger l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN); 4457e48354ceSNicholas Bellinger 4458e48354ceSNicholas Bellinger spin_lock_bh(&l_conn->state_lock); 4459e48354ceSNicholas Bellinger pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n"); 4460e48354ceSNicholas Bellinger l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT; 4461e48354ceSNicholas Bellinger spin_unlock_bh(&l_conn->state_lock); 4462e48354ceSNicholas Bellinger 4463e48354ceSNicholas Bellinger iscsit_cause_connection_reinstatement(l_conn, 1); 4464e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(l_conn); 4465e48354ceSNicholas Bellinger } 4466e48354ceSNicholas Bellinger 4467e48354ceSNicholas Bellinger /* 4468e48354ceSNicholas Bellinger * Return of 0 causes the TX thread to restart. 4469e48354ceSNicholas Bellinger */ 44702ec5a8c1SNicholas Bellinger int iscsit_logout_post_handler( 4471e48354ceSNicholas Bellinger struct iscsi_cmd *cmd, 4472e48354ceSNicholas Bellinger struct iscsi_conn *conn) 4473e48354ceSNicholas Bellinger { 4474e48354ceSNicholas Bellinger int ret = 0; 4475e48354ceSNicholas Bellinger 4476e48354ceSNicholas Bellinger switch (cmd->logout_reason) { 4477e48354ceSNicholas Bellinger case ISCSI_LOGOUT_REASON_CLOSE_SESSION: 4478e48354ceSNicholas Bellinger switch (cmd->logout_response) { 4479e48354ceSNicholas Bellinger case ISCSI_LOGOUT_SUCCESS: 4480e48354ceSNicholas Bellinger case ISCSI_LOGOUT_CLEANUP_FAILED: 4481e48354ceSNicholas Bellinger default: 4482e48354ceSNicholas Bellinger iscsit_logout_post_handler_closesession(conn); 4483e48354ceSNicholas Bellinger break; 4484e48354ceSNicholas Bellinger } 4485e48354ceSNicholas Bellinger ret = 0; 4486e48354ceSNicholas Bellinger break; 4487e48354ceSNicholas Bellinger case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION: 4488e48354ceSNicholas Bellinger if (conn->cid == cmd->logout_cid) { 4489e48354ceSNicholas Bellinger switch (cmd->logout_response) { 4490e48354ceSNicholas Bellinger case ISCSI_LOGOUT_SUCCESS: 4491e48354ceSNicholas Bellinger case ISCSI_LOGOUT_CLEANUP_FAILED: 4492e48354ceSNicholas Bellinger default: 4493e48354ceSNicholas Bellinger iscsit_logout_post_handler_samecid(conn); 4494e48354ceSNicholas Bellinger break; 4495e48354ceSNicholas Bellinger } 4496e48354ceSNicholas Bellinger ret = 0; 4497e48354ceSNicholas Bellinger } else { 4498e48354ceSNicholas Bellinger switch (cmd->logout_response) { 4499e48354ceSNicholas Bellinger case ISCSI_LOGOUT_SUCCESS: 4500e48354ceSNicholas Bellinger iscsit_logout_post_handler_diffcid(conn, 4501e48354ceSNicholas Bellinger cmd->logout_cid); 4502e48354ceSNicholas Bellinger break; 4503e48354ceSNicholas Bellinger case ISCSI_LOGOUT_CID_NOT_FOUND: 4504e48354ceSNicholas Bellinger case ISCSI_LOGOUT_CLEANUP_FAILED: 4505e48354ceSNicholas Bellinger default: 4506e48354ceSNicholas Bellinger break; 4507e48354ceSNicholas Bellinger } 4508e48354ceSNicholas Bellinger ret = 1; 4509e48354ceSNicholas Bellinger } 4510e48354ceSNicholas Bellinger break; 4511e48354ceSNicholas Bellinger case ISCSI_LOGOUT_REASON_RECOVERY: 4512e48354ceSNicholas Bellinger switch (cmd->logout_response) { 4513e48354ceSNicholas Bellinger case ISCSI_LOGOUT_SUCCESS: 4514e48354ceSNicholas Bellinger case ISCSI_LOGOUT_CID_NOT_FOUND: 4515e48354ceSNicholas Bellinger case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED: 4516e48354ceSNicholas Bellinger case ISCSI_LOGOUT_CLEANUP_FAILED: 4517e48354ceSNicholas Bellinger default: 4518e48354ceSNicholas Bellinger break; 4519e48354ceSNicholas Bellinger } 4520e48354ceSNicholas Bellinger ret = 1; 4521e48354ceSNicholas Bellinger break; 4522e48354ceSNicholas Bellinger default: 4523e48354ceSNicholas Bellinger break; 4524e48354ceSNicholas Bellinger 4525e48354ceSNicholas Bellinger } 4526e48354ceSNicholas Bellinger return ret; 4527e48354ceSNicholas Bellinger } 45282ec5a8c1SNicholas Bellinger EXPORT_SYMBOL(iscsit_logout_post_handler); 4529e48354ceSNicholas Bellinger 4530e48354ceSNicholas Bellinger void iscsit_fail_session(struct iscsi_session *sess) 4531e48354ceSNicholas Bellinger { 4532e48354ceSNicholas Bellinger struct iscsi_conn *conn; 4533e48354ceSNicholas Bellinger 4534e48354ceSNicholas Bellinger spin_lock_bh(&sess->conn_lock); 4535e48354ceSNicholas Bellinger list_for_each_entry(conn, &sess->sess_conn_list, conn_list) { 4536e48354ceSNicholas Bellinger pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n"); 4537e48354ceSNicholas Bellinger conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT; 4538e48354ceSNicholas Bellinger } 4539e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4540e48354ceSNicholas Bellinger 4541e48354ceSNicholas Bellinger pr_debug("Moving to TARG_SESS_STATE_FAILED.\n"); 4542e48354ceSNicholas Bellinger sess->session_state = TARG_SESS_STATE_FAILED; 4543e48354ceSNicholas Bellinger } 4544e48354ceSNicholas Bellinger 4545e48354ceSNicholas Bellinger int iscsit_free_session(struct iscsi_session *sess) 4546e48354ceSNicholas Bellinger { 4547e48354ceSNicholas Bellinger u16 conn_count = atomic_read(&sess->nconn); 4548e48354ceSNicholas Bellinger struct iscsi_conn *conn, *conn_tmp = NULL; 4549e48354ceSNicholas Bellinger int is_last; 4550e48354ceSNicholas Bellinger 4551e48354ceSNicholas Bellinger spin_lock_bh(&sess->conn_lock); 4552e48354ceSNicholas Bellinger atomic_set(&sess->sleep_on_sess_wait_comp, 1); 4553e48354ceSNicholas Bellinger 4554e48354ceSNicholas Bellinger list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list, 4555e48354ceSNicholas Bellinger conn_list) { 4556e48354ceSNicholas Bellinger if (conn_count == 0) 4557e48354ceSNicholas Bellinger break; 4558e48354ceSNicholas Bellinger 4559e48354ceSNicholas Bellinger if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) { 4560e48354ceSNicholas Bellinger is_last = 1; 4561e48354ceSNicholas Bellinger } else { 4562e48354ceSNicholas Bellinger iscsit_inc_conn_usage_count(conn_tmp); 4563e48354ceSNicholas Bellinger is_last = 0; 4564e48354ceSNicholas Bellinger } 4565e48354ceSNicholas Bellinger iscsit_inc_conn_usage_count(conn); 4566e48354ceSNicholas Bellinger 4567e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4568e48354ceSNicholas Bellinger iscsit_cause_connection_reinstatement(conn, 1); 4569e48354ceSNicholas Bellinger spin_lock_bh(&sess->conn_lock); 4570e48354ceSNicholas Bellinger 4571e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(conn); 4572e48354ceSNicholas Bellinger if (is_last == 0) 4573e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(conn_tmp); 4574e48354ceSNicholas Bellinger 4575e48354ceSNicholas Bellinger conn_count--; 4576e48354ceSNicholas Bellinger } 4577e48354ceSNicholas Bellinger 4578e48354ceSNicholas Bellinger if (atomic_read(&sess->nconn)) { 4579e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4580e48354ceSNicholas Bellinger wait_for_completion(&sess->session_wait_comp); 4581e48354ceSNicholas Bellinger } else 4582e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4583e48354ceSNicholas Bellinger 458444f33d0fSChristoph Hellwig iscsit_close_session(sess); 4585e48354ceSNicholas Bellinger return 0; 4586e48354ceSNicholas Bellinger } 4587e48354ceSNicholas Bellinger 4588e48354ceSNicholas Bellinger void iscsit_stop_session( 4589e48354ceSNicholas Bellinger struct iscsi_session *sess, 4590e48354ceSNicholas Bellinger int session_sleep, 4591e48354ceSNicholas Bellinger int connection_sleep) 4592e48354ceSNicholas Bellinger { 4593e48354ceSNicholas Bellinger u16 conn_count = atomic_read(&sess->nconn); 4594e48354ceSNicholas Bellinger struct iscsi_conn *conn, *conn_tmp = NULL; 4595e48354ceSNicholas Bellinger int is_last; 4596e48354ceSNicholas Bellinger 4597e48354ceSNicholas Bellinger spin_lock_bh(&sess->conn_lock); 4598e48354ceSNicholas Bellinger if (session_sleep) 4599e48354ceSNicholas Bellinger atomic_set(&sess->sleep_on_sess_wait_comp, 1); 4600e48354ceSNicholas Bellinger 4601e48354ceSNicholas Bellinger if (connection_sleep) { 4602e48354ceSNicholas Bellinger list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list, 4603e48354ceSNicholas Bellinger conn_list) { 4604e48354ceSNicholas Bellinger if (conn_count == 0) 4605e48354ceSNicholas Bellinger break; 4606e48354ceSNicholas Bellinger 4607e48354ceSNicholas Bellinger if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) { 4608e48354ceSNicholas Bellinger is_last = 1; 4609e48354ceSNicholas Bellinger } else { 4610e48354ceSNicholas Bellinger iscsit_inc_conn_usage_count(conn_tmp); 4611e48354ceSNicholas Bellinger is_last = 0; 4612e48354ceSNicholas Bellinger } 4613e48354ceSNicholas Bellinger iscsit_inc_conn_usage_count(conn); 4614e48354ceSNicholas Bellinger 4615e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4616e48354ceSNicholas Bellinger iscsit_cause_connection_reinstatement(conn, 1); 4617e48354ceSNicholas Bellinger spin_lock_bh(&sess->conn_lock); 4618e48354ceSNicholas Bellinger 4619e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(conn); 4620e48354ceSNicholas Bellinger if (is_last == 0) 4621e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(conn_tmp); 4622e48354ceSNicholas Bellinger conn_count--; 4623e48354ceSNicholas Bellinger } 4624e48354ceSNicholas Bellinger } else { 4625e48354ceSNicholas Bellinger list_for_each_entry(conn, &sess->sess_conn_list, conn_list) 4626e48354ceSNicholas Bellinger iscsit_cause_connection_reinstatement(conn, 0); 4627e48354ceSNicholas Bellinger } 4628e48354ceSNicholas Bellinger 4629e48354ceSNicholas Bellinger if (session_sleep && atomic_read(&sess->nconn)) { 4630e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4631e48354ceSNicholas Bellinger wait_for_completion(&sess->session_wait_comp); 4632e48354ceSNicholas Bellinger } else 4633e48354ceSNicholas Bellinger spin_unlock_bh(&sess->conn_lock); 4634e48354ceSNicholas Bellinger } 4635e48354ceSNicholas Bellinger 4636e48354ceSNicholas Bellinger int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force) 4637e48354ceSNicholas Bellinger { 4638e48354ceSNicholas Bellinger struct iscsi_session *sess; 4639e48354ceSNicholas Bellinger struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; 4640e48354ceSNicholas Bellinger struct se_session *se_sess, *se_sess_tmp; 4641417c20a9SNicholas Bellinger LIST_HEAD(free_list); 4642e48354ceSNicholas Bellinger int session_count = 0; 4643e48354ceSNicholas Bellinger 4644e48354ceSNicholas Bellinger spin_lock_bh(&se_tpg->session_lock); 4645e48354ceSNicholas Bellinger if (tpg->nsessions && !force) { 4646e48354ceSNicholas Bellinger spin_unlock_bh(&se_tpg->session_lock); 4647e48354ceSNicholas Bellinger return -1; 4648e48354ceSNicholas Bellinger } 4649e48354ceSNicholas Bellinger 4650e48354ceSNicholas Bellinger list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list, 4651e48354ceSNicholas Bellinger sess_list) { 4652e48354ceSNicholas Bellinger sess = (struct iscsi_session *)se_sess->fabric_sess_ptr; 4653e48354ceSNicholas Bellinger 4654e48354ceSNicholas Bellinger spin_lock(&sess->conn_lock); 4655e48354ceSNicholas Bellinger if (atomic_read(&sess->session_fall_back_to_erl0) || 4656e48354ceSNicholas Bellinger atomic_read(&sess->session_logout) || 4657e48354ceSNicholas Bellinger (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) { 4658e48354ceSNicholas Bellinger spin_unlock(&sess->conn_lock); 4659e48354ceSNicholas Bellinger continue; 4660e48354ceSNicholas Bellinger } 4661e48354ceSNicholas Bellinger atomic_set(&sess->session_reinstatement, 1); 4662197b806aSNicholas Bellinger atomic_set(&sess->session_fall_back_to_erl0, 1); 4663e48354ceSNicholas Bellinger spin_unlock(&sess->conn_lock); 4664e48354ceSNicholas Bellinger 4665417c20a9SNicholas Bellinger list_move_tail(&se_sess->sess_list, &free_list); 4666e48354ceSNicholas Bellinger } 4667e48354ceSNicholas Bellinger spin_unlock_bh(&se_tpg->session_lock); 4668e48354ceSNicholas Bellinger 4669417c20a9SNicholas Bellinger list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) { 4670417c20a9SNicholas Bellinger sess = (struct iscsi_session *)se_sess->fabric_sess_ptr; 4671417c20a9SNicholas Bellinger 4672417c20a9SNicholas Bellinger iscsit_free_session(sess); 4673417c20a9SNicholas Bellinger session_count++; 4674417c20a9SNicholas Bellinger } 4675417c20a9SNicholas Bellinger 4676e48354ceSNicholas Bellinger pr_debug("Released %d iSCSI Session(s) from Target Portal" 4677e48354ceSNicholas Bellinger " Group: %hu\n", session_count, tpg->tpgt); 4678e48354ceSNicholas Bellinger return 0; 4679e48354ceSNicholas Bellinger } 4680e48354ceSNicholas Bellinger 4681e48354ceSNicholas Bellinger MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure"); 4682e48354ceSNicholas Bellinger MODULE_VERSION("4.1.x"); 4683e48354ceSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org"); 4684e48354ceSNicholas Bellinger MODULE_LICENSE("GPL"); 4685e48354ceSNicholas Bellinger 4686e48354ceSNicholas Bellinger module_init(iscsi_target_init_module); 4687e48354ceSNicholas Bellinger module_exit(iscsi_target_cleanup_module); 4688