| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1 | /******************************************************************************* | 
 | 2 |  * This file contains main functions related to the iSCSI Target Core Driver. | 
 | 3 |  * | 
 | 4 |  * \u00a9 Copyright 2007-2011 RisingTide Systems LLC. | 
 | 5 |  * | 
 | 6 |  * Licensed to the Linux Foundation under the General Public License (GPL) version 2. | 
 | 7 |  * | 
 | 8 |  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org> | 
 | 9 |  * | 
 | 10 |  * This program is free software; you can redistribute it and/or modify | 
 | 11 |  * it under the terms of the GNU General Public License as published by | 
 | 12 |  * the Free Software Foundation; either version 2 of the License, or | 
 | 13 |  * (at your option) any later version. | 
 | 14 |  * | 
 | 15 |  * This program is distributed in the hope that it will be useful, | 
 | 16 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 17 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 18 |  * GNU General Public License for more details. | 
 | 19 |  ******************************************************************************/ | 
 | 20 |  | 
 | 21 | #include <linux/string.h> | 
 | 22 | #include <linux/kthread.h> | 
 | 23 | #include <linux/crypto.h> | 
 | 24 | #include <linux/completion.h> | 
| Paul Gortmaker | 827509e | 2011-08-30 14:20:44 -0400 | [diff] [blame] | 25 | #include <linux/module.h> | 
| Al Viro | 4040153 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 26 | #include <linux/idr.h> | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 27 | #include <asm/unaligned.h> | 
 | 28 | #include <scsi/scsi_device.h> | 
 | 29 | #include <scsi/iscsi_proto.h> | 
| Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 30 | #include <scsi/scsi_tcq.h> | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 31 | #include <target/target_core_base.h> | 
| Christoph Hellwig | c4795fb | 2011-11-16 09:46:48 -0500 | [diff] [blame] | 32 | #include <target/target_core_fabric.h> | 
| Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 33 | #include <target/target_core_configfs.h> | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 34 |  | 
 | 35 | #include "iscsi_target_core.h" | 
 | 36 | #include "iscsi_target_parameters.h" | 
 | 37 | #include "iscsi_target_seq_pdu_list.h" | 
 | 38 | #include "iscsi_target_tq.h" | 
 | 39 | #include "iscsi_target_configfs.h" | 
 | 40 | #include "iscsi_target_datain_values.h" | 
 | 41 | #include "iscsi_target_erl0.h" | 
 | 42 | #include "iscsi_target_erl1.h" | 
 | 43 | #include "iscsi_target_erl2.h" | 
 | 44 | #include "iscsi_target_login.h" | 
 | 45 | #include "iscsi_target_tmr.h" | 
 | 46 | #include "iscsi_target_tpg.h" | 
 | 47 | #include "iscsi_target_util.h" | 
 | 48 | #include "iscsi_target.h" | 
 | 49 | #include "iscsi_target_device.h" | 
 | 50 | #include "iscsi_target_stat.h" | 
 | 51 |  | 
 | 52 | static LIST_HEAD(g_tiqn_list); | 
 | 53 | static LIST_HEAD(g_np_list); | 
 | 54 | static DEFINE_SPINLOCK(tiqn_lock); | 
 | 55 | static DEFINE_SPINLOCK(np_lock); | 
 | 56 |  | 
 | 57 | static struct idr tiqn_idr; | 
 | 58 | struct idr sess_idr; | 
 | 59 | struct mutex auth_id_lock; | 
 | 60 | spinlock_t sess_idr_lock; | 
 | 61 |  | 
 | 62 | struct iscsit_global *iscsit_global; | 
 | 63 |  | 
 | 64 | struct kmem_cache *lio_cmd_cache; | 
 | 65 | struct kmem_cache *lio_qr_cache; | 
 | 66 | struct kmem_cache *lio_dr_cache; | 
 | 67 | struct kmem_cache *lio_ooo_cache; | 
 | 68 | struct kmem_cache *lio_r2t_cache; | 
 | 69 |  | 
 | 70 | static int iscsit_handle_immediate_data(struct iscsi_cmd *, | 
 | 71 | 			unsigned char *buf, u32); | 
 | 72 | static int iscsit_logout_post_handler(struct iscsi_cmd *, struct iscsi_conn *); | 
 | 73 |  | 
 | 74 | struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf) | 
 | 75 | { | 
 | 76 | 	struct iscsi_tiqn *tiqn = NULL; | 
 | 77 |  | 
 | 78 | 	spin_lock(&tiqn_lock); | 
 | 79 | 	list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) { | 
 | 80 | 		if (!strcmp(tiqn->tiqn, buf)) { | 
 | 81 |  | 
 | 82 | 			spin_lock(&tiqn->tiqn_state_lock); | 
 | 83 | 			if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) { | 
 | 84 | 				tiqn->tiqn_access_count++; | 
 | 85 | 				spin_unlock(&tiqn->tiqn_state_lock); | 
 | 86 | 				spin_unlock(&tiqn_lock); | 
 | 87 | 				return tiqn; | 
 | 88 | 			} | 
 | 89 | 			spin_unlock(&tiqn->tiqn_state_lock); | 
 | 90 | 		} | 
 | 91 | 	} | 
 | 92 | 	spin_unlock(&tiqn_lock); | 
 | 93 |  | 
 | 94 | 	return NULL; | 
 | 95 | } | 
 | 96 |  | 
 | 97 | static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn) | 
 | 98 | { | 
 | 99 | 	spin_lock(&tiqn->tiqn_state_lock); | 
 | 100 | 	if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) { | 
 | 101 | 		tiqn->tiqn_state = TIQN_STATE_SHUTDOWN; | 
 | 102 | 		spin_unlock(&tiqn->tiqn_state_lock); | 
 | 103 | 		return 0; | 
 | 104 | 	} | 
 | 105 | 	spin_unlock(&tiqn->tiqn_state_lock); | 
 | 106 |  | 
 | 107 | 	return -1; | 
 | 108 | } | 
 | 109 |  | 
 | 110 | void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn) | 
 | 111 | { | 
 | 112 | 	spin_lock(&tiqn->tiqn_state_lock); | 
 | 113 | 	tiqn->tiqn_access_count--; | 
 | 114 | 	spin_unlock(&tiqn->tiqn_state_lock); | 
 | 115 | } | 
 | 116 |  | 
 | 117 | /* | 
 | 118 |  * Note that IQN formatting is expected to be done in userspace, and | 
 | 119 |  * no explict IQN format checks are done here. | 
 | 120 |  */ | 
 | 121 | struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf) | 
 | 122 | { | 
 | 123 | 	struct iscsi_tiqn *tiqn = NULL; | 
 | 124 | 	int ret; | 
 | 125 |  | 
| Dan Carpenter | 8f50c7f | 2011-07-27 14:11:43 +0300 | [diff] [blame] | 126 | 	if (strlen(buf) >= ISCSI_IQN_LEN) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 127 | 		pr_err("Target IQN exceeds %d bytes\n", | 
 | 128 | 				ISCSI_IQN_LEN); | 
 | 129 | 		return ERR_PTR(-EINVAL); | 
 | 130 | 	} | 
 | 131 |  | 
 | 132 | 	tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL); | 
 | 133 | 	if (!tiqn) { | 
 | 134 | 		pr_err("Unable to allocate struct iscsi_tiqn\n"); | 
 | 135 | 		return ERR_PTR(-ENOMEM); | 
 | 136 | 	} | 
 | 137 |  | 
 | 138 | 	sprintf(tiqn->tiqn, "%s", buf); | 
 | 139 | 	INIT_LIST_HEAD(&tiqn->tiqn_list); | 
 | 140 | 	INIT_LIST_HEAD(&tiqn->tiqn_tpg_list); | 
 | 141 | 	spin_lock_init(&tiqn->tiqn_state_lock); | 
 | 142 | 	spin_lock_init(&tiqn->tiqn_tpg_lock); | 
 | 143 | 	spin_lock_init(&tiqn->sess_err_stats.lock); | 
 | 144 | 	spin_lock_init(&tiqn->login_stats.lock); | 
 | 145 | 	spin_lock_init(&tiqn->logout_stats.lock); | 
 | 146 |  | 
 | 147 | 	if (!idr_pre_get(&tiqn_idr, GFP_KERNEL)) { | 
 | 148 | 		pr_err("idr_pre_get() for tiqn_idr failed\n"); | 
 | 149 | 		kfree(tiqn); | 
 | 150 | 		return ERR_PTR(-ENOMEM); | 
 | 151 | 	} | 
 | 152 | 	tiqn->tiqn_state = TIQN_STATE_ACTIVE; | 
 | 153 |  | 
 | 154 | 	spin_lock(&tiqn_lock); | 
 | 155 | 	ret = idr_get_new(&tiqn_idr, NULL, &tiqn->tiqn_index); | 
 | 156 | 	if (ret < 0) { | 
 | 157 | 		pr_err("idr_get_new() failed for tiqn->tiqn_index\n"); | 
 | 158 | 		spin_unlock(&tiqn_lock); | 
 | 159 | 		kfree(tiqn); | 
 | 160 | 		return ERR_PTR(ret); | 
 | 161 | 	} | 
 | 162 | 	list_add_tail(&tiqn->tiqn_list, &g_tiqn_list); | 
 | 163 | 	spin_unlock(&tiqn_lock); | 
 | 164 |  | 
 | 165 | 	pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn); | 
 | 166 |  | 
 | 167 | 	return tiqn; | 
 | 168 |  | 
 | 169 | } | 
 | 170 |  | 
 | 171 | static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn) | 
 | 172 | { | 
 | 173 | 	/* | 
 | 174 | 	 * Wait for accesses to said struct iscsi_tiqn to end. | 
 | 175 | 	 */ | 
 | 176 | 	spin_lock(&tiqn->tiqn_state_lock); | 
 | 177 | 	while (tiqn->tiqn_access_count != 0) { | 
 | 178 | 		spin_unlock(&tiqn->tiqn_state_lock); | 
 | 179 | 		msleep(10); | 
 | 180 | 		spin_lock(&tiqn->tiqn_state_lock); | 
 | 181 | 	} | 
 | 182 | 	spin_unlock(&tiqn->tiqn_state_lock); | 
 | 183 | } | 
 | 184 |  | 
 | 185 | void iscsit_del_tiqn(struct iscsi_tiqn *tiqn) | 
 | 186 | { | 
 | 187 | 	/* | 
 | 188 | 	 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN | 
 | 189 | 	 * while holding tiqn->tiqn_state_lock.  This means that all subsequent | 
 | 190 | 	 * attempts to access this struct iscsi_tiqn will fail from both transport | 
 | 191 | 	 * fabric and control code paths. | 
 | 192 | 	 */ | 
 | 193 | 	if (iscsit_set_tiqn_shutdown(tiqn) < 0) { | 
 | 194 | 		pr_err("iscsit_set_tiqn_shutdown() failed\n"); | 
 | 195 | 		return; | 
 | 196 | 	} | 
 | 197 |  | 
 | 198 | 	iscsit_wait_for_tiqn(tiqn); | 
 | 199 |  | 
 | 200 | 	spin_lock(&tiqn_lock); | 
 | 201 | 	list_del(&tiqn->tiqn_list); | 
 | 202 | 	idr_remove(&tiqn_idr, tiqn->tiqn_index); | 
 | 203 | 	spin_unlock(&tiqn_lock); | 
 | 204 |  | 
 | 205 | 	pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n", | 
 | 206 | 			tiqn->tiqn); | 
 | 207 | 	kfree(tiqn); | 
 | 208 | } | 
 | 209 |  | 
 | 210 | int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg) | 
 | 211 | { | 
 | 212 | 	int ret; | 
 | 213 | 	/* | 
 | 214 | 	 * Determine if the network portal is accepting storage traffic. | 
 | 215 | 	 */ | 
 | 216 | 	spin_lock_bh(&np->np_thread_lock); | 
 | 217 | 	if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) { | 
 | 218 | 		spin_unlock_bh(&np->np_thread_lock); | 
 | 219 | 		return -1; | 
 | 220 | 	} | 
 | 221 | 	if (np->np_login_tpg) { | 
 | 222 | 		pr_err("np->np_login_tpg() is not NULL!\n"); | 
 | 223 | 		spin_unlock_bh(&np->np_thread_lock); | 
 | 224 | 		return -1; | 
 | 225 | 	} | 
 | 226 | 	spin_unlock_bh(&np->np_thread_lock); | 
 | 227 | 	/* | 
 | 228 | 	 * Determine if the portal group is accepting storage traffic. | 
 | 229 | 	 */ | 
 | 230 | 	spin_lock_bh(&tpg->tpg_state_lock); | 
 | 231 | 	if (tpg->tpg_state != TPG_STATE_ACTIVE) { | 
 | 232 | 		spin_unlock_bh(&tpg->tpg_state_lock); | 
 | 233 | 		return -1; | 
 | 234 | 	} | 
 | 235 | 	spin_unlock_bh(&tpg->tpg_state_lock); | 
 | 236 |  | 
 | 237 | 	/* | 
 | 238 | 	 * Here we serialize access across the TIQN+TPG Tuple. | 
 | 239 | 	 */ | 
 | 240 | 	ret = mutex_lock_interruptible(&tpg->np_login_lock); | 
 | 241 | 	if ((ret != 0) || signal_pending(current)) | 
 | 242 | 		return -1; | 
 | 243 |  | 
 | 244 | 	spin_lock_bh(&np->np_thread_lock); | 
 | 245 | 	np->np_login_tpg = tpg; | 
 | 246 | 	spin_unlock_bh(&np->np_thread_lock); | 
 | 247 |  | 
 | 248 | 	return 0; | 
 | 249 | } | 
 | 250 |  | 
 | 251 | int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg) | 
 | 252 | { | 
 | 253 | 	struct iscsi_tiqn *tiqn = tpg->tpg_tiqn; | 
 | 254 |  | 
 | 255 | 	spin_lock_bh(&np->np_thread_lock); | 
 | 256 | 	np->np_login_tpg = NULL; | 
 | 257 | 	spin_unlock_bh(&np->np_thread_lock); | 
 | 258 |  | 
 | 259 | 	mutex_unlock(&tpg->np_login_lock); | 
 | 260 |  | 
 | 261 | 	if (tiqn) | 
 | 262 | 		iscsit_put_tiqn_for_login(tiqn); | 
 | 263 |  | 
 | 264 | 	return 0; | 
 | 265 | } | 
 | 266 |  | 
 | 267 | static struct iscsi_np *iscsit_get_np( | 
 | 268 | 	struct __kernel_sockaddr_storage *sockaddr, | 
 | 269 | 	int network_transport) | 
 | 270 | { | 
 | 271 | 	struct sockaddr_in *sock_in, *sock_in_e; | 
 | 272 | 	struct sockaddr_in6 *sock_in6, *sock_in6_e; | 
 | 273 | 	struct iscsi_np *np; | 
 | 274 | 	int ip_match = 0; | 
 | 275 | 	u16 port; | 
 | 276 |  | 
 | 277 | 	spin_lock_bh(&np_lock); | 
 | 278 | 	list_for_each_entry(np, &g_np_list, np_list) { | 
 | 279 | 		spin_lock(&np->np_thread_lock); | 
 | 280 | 		if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) { | 
 | 281 | 			spin_unlock(&np->np_thread_lock); | 
 | 282 | 			continue; | 
 | 283 | 		} | 
 | 284 |  | 
 | 285 | 		if (sockaddr->ss_family == AF_INET6) { | 
 | 286 | 			sock_in6 = (struct sockaddr_in6 *)sockaddr; | 
 | 287 | 			sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr; | 
 | 288 |  | 
| Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 289 | 			if (!memcmp(&sock_in6->sin6_addr.in6_u, | 
 | 290 | 				    &sock_in6_e->sin6_addr.in6_u, | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 291 | 				    sizeof(struct in6_addr))) | 
 | 292 | 				ip_match = 1; | 
 | 293 |  | 
 | 294 | 			port = ntohs(sock_in6->sin6_port); | 
 | 295 | 		} else { | 
 | 296 | 			sock_in = (struct sockaddr_in *)sockaddr; | 
 | 297 | 			sock_in_e = (struct sockaddr_in *)&np->np_sockaddr; | 
 | 298 |  | 
 | 299 | 			if (sock_in->sin_addr.s_addr == | 
 | 300 | 			    sock_in_e->sin_addr.s_addr) | 
 | 301 | 				ip_match = 1; | 
 | 302 |  | 
 | 303 | 			port = ntohs(sock_in->sin_port); | 
 | 304 | 		} | 
 | 305 |  | 
 | 306 | 		if ((ip_match == 1) && (np->np_port == port) && | 
 | 307 | 		    (np->np_network_transport == network_transport)) { | 
 | 308 | 			/* | 
 | 309 | 			 * Increment the np_exports reference count now to | 
 | 310 | 			 * prevent iscsit_del_np() below from being called | 
 | 311 | 			 * while iscsi_tpg_add_network_portal() is called. | 
 | 312 | 			 */ | 
 | 313 | 			np->np_exports++; | 
 | 314 | 			spin_unlock(&np->np_thread_lock); | 
 | 315 | 			spin_unlock_bh(&np_lock); | 
 | 316 | 			return np; | 
 | 317 | 		} | 
 | 318 | 		spin_unlock(&np->np_thread_lock); | 
 | 319 | 	} | 
 | 320 | 	spin_unlock_bh(&np_lock); | 
 | 321 |  | 
 | 322 | 	return NULL; | 
 | 323 | } | 
 | 324 |  | 
 | 325 | struct iscsi_np *iscsit_add_np( | 
 | 326 | 	struct __kernel_sockaddr_storage *sockaddr, | 
 | 327 | 	char *ip_str, | 
 | 328 | 	int network_transport) | 
 | 329 | { | 
 | 330 | 	struct sockaddr_in *sock_in; | 
 | 331 | 	struct sockaddr_in6 *sock_in6; | 
 | 332 | 	struct iscsi_np *np; | 
 | 333 | 	int ret; | 
 | 334 | 	/* | 
 | 335 | 	 * Locate the existing struct iscsi_np if already active.. | 
 | 336 | 	 */ | 
 | 337 | 	np = iscsit_get_np(sockaddr, network_transport); | 
 | 338 | 	if (np) | 
 | 339 | 		return np; | 
 | 340 |  | 
 | 341 | 	np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL); | 
 | 342 | 	if (!np) { | 
 | 343 | 		pr_err("Unable to allocate memory for struct iscsi_np\n"); | 
 | 344 | 		return ERR_PTR(-ENOMEM); | 
 | 345 | 	} | 
 | 346 |  | 
 | 347 | 	np->np_flags |= NPF_IP_NETWORK; | 
 | 348 | 	if (sockaddr->ss_family == AF_INET6) { | 
 | 349 | 		sock_in6 = (struct sockaddr_in6 *)sockaddr; | 
 | 350 | 		snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str); | 
 | 351 | 		np->np_port = ntohs(sock_in6->sin6_port); | 
 | 352 | 	} else { | 
 | 353 | 		sock_in = (struct sockaddr_in *)sockaddr; | 
 | 354 | 		sprintf(np->np_ip, "%s", ip_str); | 
 | 355 | 		np->np_port = ntohs(sock_in->sin_port); | 
 | 356 | 	} | 
 | 357 |  | 
 | 358 | 	np->np_network_transport = network_transport; | 
 | 359 | 	spin_lock_init(&np->np_thread_lock); | 
 | 360 | 	init_completion(&np->np_restart_comp); | 
 | 361 | 	INIT_LIST_HEAD(&np->np_list); | 
 | 362 |  | 
 | 363 | 	ret = iscsi_target_setup_login_socket(np, sockaddr); | 
 | 364 | 	if (ret != 0) { | 
 | 365 | 		kfree(np); | 
 | 366 | 		return ERR_PTR(ret); | 
 | 367 | 	} | 
 | 368 |  | 
 | 369 | 	np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np"); | 
 | 370 | 	if (IS_ERR(np->np_thread)) { | 
 | 371 | 		pr_err("Unable to create kthread: iscsi_np\n"); | 
 | 372 | 		ret = PTR_ERR(np->np_thread); | 
 | 373 | 		kfree(np); | 
 | 374 | 		return ERR_PTR(ret); | 
 | 375 | 	} | 
 | 376 | 	/* | 
 | 377 | 	 * Increment the np_exports reference count now to prevent | 
 | 378 | 	 * iscsit_del_np() below from being run while a new call to | 
 | 379 | 	 * iscsi_tpg_add_network_portal() for a matching iscsi_np is | 
 | 380 | 	 * active.  We don't need to hold np->np_thread_lock at this | 
 | 381 | 	 * point because iscsi_np has not been added to g_np_list yet. | 
 | 382 | 	 */ | 
 | 383 | 	np->np_exports = 1; | 
 | 384 |  | 
 | 385 | 	spin_lock_bh(&np_lock); | 
 | 386 | 	list_add_tail(&np->np_list, &g_np_list); | 
 | 387 | 	spin_unlock_bh(&np_lock); | 
 | 388 |  | 
 | 389 | 	pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n", | 
 | 390 | 		np->np_ip, np->np_port, (np->np_network_transport == ISCSI_TCP) ? | 
 | 391 | 		"TCP" : "SCTP"); | 
 | 392 |  | 
 | 393 | 	return np; | 
 | 394 | } | 
 | 395 |  | 
 | 396 | int iscsit_reset_np_thread( | 
 | 397 | 	struct iscsi_np *np, | 
 | 398 | 	struct iscsi_tpg_np *tpg_np, | 
 | 399 | 	struct iscsi_portal_group *tpg) | 
 | 400 | { | 
 | 401 | 	spin_lock_bh(&np->np_thread_lock); | 
 | 402 | 	if (tpg && tpg_np) { | 
 | 403 | 		/* | 
 | 404 | 		 * The reset operation need only be performed when the | 
 | 405 | 		 * passed struct iscsi_portal_group has a login in progress | 
 | 406 | 		 * to one of the network portals. | 
 | 407 | 		 */ | 
 | 408 | 		if (tpg_np->tpg_np->np_login_tpg != tpg) { | 
 | 409 | 			spin_unlock_bh(&np->np_thread_lock); | 
 | 410 | 			return 0; | 
 | 411 | 		} | 
 | 412 | 	} | 
 | 413 | 	if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) { | 
 | 414 | 		spin_unlock_bh(&np->np_thread_lock); | 
 | 415 | 		return 0; | 
 | 416 | 	} | 
 | 417 | 	np->np_thread_state = ISCSI_NP_THREAD_RESET; | 
 | 418 |  | 
 | 419 | 	if (np->np_thread) { | 
 | 420 | 		spin_unlock_bh(&np->np_thread_lock); | 
 | 421 | 		send_sig(SIGINT, np->np_thread, 1); | 
 | 422 | 		wait_for_completion(&np->np_restart_comp); | 
 | 423 | 		spin_lock_bh(&np->np_thread_lock); | 
 | 424 | 	} | 
 | 425 | 	spin_unlock_bh(&np->np_thread_lock); | 
 | 426 |  | 
 | 427 | 	return 0; | 
 | 428 | } | 
 | 429 |  | 
 | 430 | int iscsit_del_np_comm(struct iscsi_np *np) | 
 | 431 | { | 
| Al Viro | bf6932f | 2012-07-21 08:55:18 +0100 | [diff] [blame] | 432 | 	if (np->np_socket) | 
 | 433 | 		sock_release(np->np_socket); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 434 | 	return 0; | 
 | 435 | } | 
 | 436 |  | 
 | 437 | int iscsit_del_np(struct iscsi_np *np) | 
 | 438 | { | 
 | 439 | 	spin_lock_bh(&np->np_thread_lock); | 
 | 440 | 	np->np_exports--; | 
 | 441 | 	if (np->np_exports) { | 
 | 442 | 		spin_unlock_bh(&np->np_thread_lock); | 
 | 443 | 		return 0; | 
 | 444 | 	} | 
 | 445 | 	np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN; | 
 | 446 | 	spin_unlock_bh(&np->np_thread_lock); | 
 | 447 |  | 
 | 448 | 	if (np->np_thread) { | 
 | 449 | 		/* | 
 | 450 | 		 * We need to send the signal to wakeup Linux/Net | 
 | 451 | 		 * which may be sleeping in sock_accept().. | 
 | 452 | 		 */ | 
 | 453 | 		send_sig(SIGINT, np->np_thread, 1); | 
 | 454 | 		kthread_stop(np->np_thread); | 
 | 455 | 	} | 
 | 456 | 	iscsit_del_np_comm(np); | 
 | 457 |  | 
 | 458 | 	spin_lock_bh(&np_lock); | 
 | 459 | 	list_del(&np->np_list); | 
 | 460 | 	spin_unlock_bh(&np_lock); | 
 | 461 |  | 
 | 462 | 	pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n", | 
 | 463 | 		np->np_ip, np->np_port, (np->np_network_transport == ISCSI_TCP) ? | 
 | 464 | 		"TCP" : "SCTP"); | 
 | 465 |  | 
 | 466 | 	kfree(np); | 
 | 467 | 	return 0; | 
 | 468 | } | 
 | 469 |  | 
 | 470 | static int __init iscsi_target_init_module(void) | 
 | 471 | { | 
 | 472 | 	int ret = 0; | 
 | 473 |  | 
 | 474 | 	pr_debug("iSCSI-Target "ISCSIT_VERSION"\n"); | 
 | 475 |  | 
 | 476 | 	iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL); | 
 | 477 | 	if (!iscsit_global) { | 
 | 478 | 		pr_err("Unable to allocate memory for iscsit_global\n"); | 
 | 479 | 		return -1; | 
 | 480 | 	} | 
 | 481 | 	mutex_init(&auth_id_lock); | 
 | 482 | 	spin_lock_init(&sess_idr_lock); | 
 | 483 | 	idr_init(&tiqn_idr); | 
 | 484 | 	idr_init(&sess_idr); | 
 | 485 |  | 
 | 486 | 	ret = iscsi_target_register_configfs(); | 
 | 487 | 	if (ret < 0) | 
 | 488 | 		goto out; | 
 | 489 |  | 
 | 490 | 	ret = iscsi_thread_set_init(); | 
 | 491 | 	if (ret < 0) | 
 | 492 | 		goto configfs_out; | 
 | 493 |  | 
 | 494 | 	if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) != | 
 | 495 | 			TARGET_THREAD_SET_COUNT) { | 
 | 496 | 		pr_err("iscsi_allocate_thread_sets() returned" | 
 | 497 | 			" unexpected value!\n"); | 
 | 498 | 		goto ts_out1; | 
 | 499 | 	} | 
 | 500 |  | 
 | 501 | 	lio_cmd_cache = kmem_cache_create("lio_cmd_cache", | 
 | 502 | 			sizeof(struct iscsi_cmd), __alignof__(struct iscsi_cmd), | 
 | 503 | 			0, NULL); | 
 | 504 | 	if (!lio_cmd_cache) { | 
 | 505 | 		pr_err("Unable to kmem_cache_create() for" | 
 | 506 | 				" lio_cmd_cache\n"); | 
 | 507 | 		goto ts_out2; | 
 | 508 | 	} | 
 | 509 |  | 
 | 510 | 	lio_qr_cache = kmem_cache_create("lio_qr_cache", | 
 | 511 | 			sizeof(struct iscsi_queue_req), | 
 | 512 | 			__alignof__(struct iscsi_queue_req), 0, NULL); | 
 | 513 | 	if (!lio_qr_cache) { | 
 | 514 | 		pr_err("nable to kmem_cache_create() for" | 
 | 515 | 				" lio_qr_cache\n"); | 
 | 516 | 		goto cmd_out; | 
 | 517 | 	} | 
 | 518 |  | 
 | 519 | 	lio_dr_cache = kmem_cache_create("lio_dr_cache", | 
 | 520 | 			sizeof(struct iscsi_datain_req), | 
 | 521 | 			__alignof__(struct iscsi_datain_req), 0, NULL); | 
 | 522 | 	if (!lio_dr_cache) { | 
 | 523 | 		pr_err("Unable to kmem_cache_create() for" | 
 | 524 | 				" lio_dr_cache\n"); | 
 | 525 | 		goto qr_out; | 
 | 526 | 	} | 
 | 527 |  | 
 | 528 | 	lio_ooo_cache = kmem_cache_create("lio_ooo_cache", | 
 | 529 | 			sizeof(struct iscsi_ooo_cmdsn), | 
 | 530 | 			__alignof__(struct iscsi_ooo_cmdsn), 0, NULL); | 
 | 531 | 	if (!lio_ooo_cache) { | 
 | 532 | 		pr_err("Unable to kmem_cache_create() for" | 
 | 533 | 				" lio_ooo_cache\n"); | 
 | 534 | 		goto dr_out; | 
 | 535 | 	} | 
 | 536 |  | 
 | 537 | 	lio_r2t_cache = kmem_cache_create("lio_r2t_cache", | 
 | 538 | 			sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t), | 
 | 539 | 			0, NULL); | 
 | 540 | 	if (!lio_r2t_cache) { | 
 | 541 | 		pr_err("Unable to kmem_cache_create() for" | 
 | 542 | 				" lio_r2t_cache\n"); | 
 | 543 | 		goto ooo_out; | 
 | 544 | 	} | 
 | 545 |  | 
 | 546 | 	if (iscsit_load_discovery_tpg() < 0) | 
 | 547 | 		goto r2t_out; | 
 | 548 |  | 
 | 549 | 	return ret; | 
 | 550 | r2t_out: | 
 | 551 | 	kmem_cache_destroy(lio_r2t_cache); | 
 | 552 | ooo_out: | 
 | 553 | 	kmem_cache_destroy(lio_ooo_cache); | 
 | 554 | dr_out: | 
 | 555 | 	kmem_cache_destroy(lio_dr_cache); | 
 | 556 | qr_out: | 
 | 557 | 	kmem_cache_destroy(lio_qr_cache); | 
 | 558 | cmd_out: | 
 | 559 | 	kmem_cache_destroy(lio_cmd_cache); | 
 | 560 | ts_out2: | 
 | 561 | 	iscsi_deallocate_thread_sets(); | 
 | 562 | ts_out1: | 
 | 563 | 	iscsi_thread_set_free(); | 
 | 564 | configfs_out: | 
 | 565 | 	iscsi_target_deregister_configfs(); | 
 | 566 | out: | 
 | 567 | 	kfree(iscsit_global); | 
 | 568 | 	return -ENOMEM; | 
 | 569 | } | 
 | 570 |  | 
 | 571 | static void __exit iscsi_target_cleanup_module(void) | 
 | 572 | { | 
 | 573 | 	iscsi_deallocate_thread_sets(); | 
 | 574 | 	iscsi_thread_set_free(); | 
 | 575 | 	iscsit_release_discovery_tpg(); | 
 | 576 | 	kmem_cache_destroy(lio_cmd_cache); | 
 | 577 | 	kmem_cache_destroy(lio_qr_cache); | 
 | 578 | 	kmem_cache_destroy(lio_dr_cache); | 
 | 579 | 	kmem_cache_destroy(lio_ooo_cache); | 
 | 580 | 	kmem_cache_destroy(lio_r2t_cache); | 
 | 581 |  | 
 | 582 | 	iscsi_target_deregister_configfs(); | 
 | 583 |  | 
 | 584 | 	kfree(iscsit_global); | 
 | 585 | } | 
 | 586 |  | 
| Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 587 | static int iscsit_add_reject( | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 588 | 	u8 reason, | 
 | 589 | 	int fail_conn, | 
 | 590 | 	unsigned char *buf, | 
 | 591 | 	struct iscsi_conn *conn) | 
 | 592 | { | 
 | 593 | 	struct iscsi_cmd *cmd; | 
 | 594 | 	struct iscsi_reject *hdr; | 
 | 595 | 	int ret; | 
 | 596 |  | 
 | 597 | 	cmd = iscsit_allocate_cmd(conn, GFP_KERNEL); | 
 | 598 | 	if (!cmd) | 
 | 599 | 		return -1; | 
 | 600 |  | 
 | 601 | 	cmd->iscsi_opcode = ISCSI_OP_REJECT; | 
 | 602 | 	if (fail_conn) | 
 | 603 | 		cmd->cmd_flags |= ICF_REJECT_FAIL_CONN; | 
 | 604 |  | 
 | 605 | 	hdr	= (struct iscsi_reject *) cmd->pdu; | 
 | 606 | 	hdr->reason = reason; | 
 | 607 |  | 
| Thomas Meyer | 1c3d579 | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 608 | 	cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 609 | 	if (!cmd->buf_ptr) { | 
 | 610 | 		pr_err("Unable to allocate memory for cmd->buf_ptr\n"); | 
 | 611 | 		iscsit_release_cmd(cmd); | 
 | 612 | 		return -1; | 
 | 613 | 	} | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 614 |  | 
 | 615 | 	spin_lock_bh(&conn->cmd_lock); | 
| Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 616 | 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 617 | 	spin_unlock_bh(&conn->cmd_lock); | 
 | 618 |  | 
 | 619 | 	cmd->i_state = ISTATE_SEND_REJECT; | 
 | 620 | 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); | 
 | 621 |  | 
 | 622 | 	ret = wait_for_completion_interruptible(&cmd->reject_comp); | 
 | 623 | 	if (ret != 0) | 
 | 624 | 		return -1; | 
 | 625 |  | 
 | 626 | 	return (!fail_conn) ? 0 : -1; | 
 | 627 | } | 
 | 628 |  | 
 | 629 | int iscsit_add_reject_from_cmd( | 
 | 630 | 	u8 reason, | 
 | 631 | 	int fail_conn, | 
 | 632 | 	int add_to_conn, | 
 | 633 | 	unsigned char *buf, | 
 | 634 | 	struct iscsi_cmd *cmd) | 
 | 635 | { | 
 | 636 | 	struct iscsi_conn *conn; | 
 | 637 | 	struct iscsi_reject *hdr; | 
 | 638 | 	int ret; | 
 | 639 |  | 
 | 640 | 	if (!cmd->conn) { | 
 | 641 | 		pr_err("cmd->conn is NULL for ITT: 0x%08x\n", | 
 | 642 | 				cmd->init_task_tag); | 
 | 643 | 		return -1; | 
 | 644 | 	} | 
 | 645 | 	conn = cmd->conn; | 
 | 646 |  | 
 | 647 | 	cmd->iscsi_opcode = ISCSI_OP_REJECT; | 
 | 648 | 	if (fail_conn) | 
 | 649 | 		cmd->cmd_flags |= ICF_REJECT_FAIL_CONN; | 
 | 650 |  | 
 | 651 | 	hdr	= (struct iscsi_reject *) cmd->pdu; | 
 | 652 | 	hdr->reason = reason; | 
 | 653 |  | 
| Thomas Meyer | 1c3d579 | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 654 | 	cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 655 | 	if (!cmd->buf_ptr) { | 
 | 656 | 		pr_err("Unable to allocate memory for cmd->buf_ptr\n"); | 
 | 657 | 		iscsit_release_cmd(cmd); | 
 | 658 | 		return -1; | 
 | 659 | 	} | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 660 |  | 
 | 661 | 	if (add_to_conn) { | 
 | 662 | 		spin_lock_bh(&conn->cmd_lock); | 
| Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 663 | 		list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 664 | 		spin_unlock_bh(&conn->cmd_lock); | 
 | 665 | 	} | 
 | 666 |  | 
 | 667 | 	cmd->i_state = ISTATE_SEND_REJECT; | 
 | 668 | 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); | 
 | 669 |  | 
 | 670 | 	ret = wait_for_completion_interruptible(&cmd->reject_comp); | 
 | 671 | 	if (ret != 0) | 
 | 672 | 		return -1; | 
 | 673 |  | 
 | 674 | 	return (!fail_conn) ? 0 : -1; | 
 | 675 | } | 
 | 676 |  | 
 | 677 | /* | 
 | 678 |  * Map some portion of the allocated scatterlist to an iovec, suitable for | 
| Andy Grover | bfb79ea | 2012-04-03 15:51:29 -0700 | [diff] [blame] | 679 |  * kernel sockets to copy data in/out. | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 680 |  */ | 
 | 681 | static int iscsit_map_iovec( | 
 | 682 | 	struct iscsi_cmd *cmd, | 
 | 683 | 	struct kvec *iov, | 
 | 684 | 	u32 data_offset, | 
 | 685 | 	u32 data_length) | 
 | 686 | { | 
 | 687 | 	u32 i = 0; | 
 | 688 | 	struct scatterlist *sg; | 
 | 689 | 	unsigned int page_off; | 
 | 690 |  | 
 | 691 | 	/* | 
| Andy Grover | bfb79ea | 2012-04-03 15:51:29 -0700 | [diff] [blame] | 692 | 	 * We know each entry in t_data_sg contains a page. | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 693 | 	 */ | 
| Andy Grover | bfb79ea | 2012-04-03 15:51:29 -0700 | [diff] [blame] | 694 | 	sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE]; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 695 | 	page_off = (data_offset % PAGE_SIZE); | 
 | 696 |  | 
 | 697 | 	cmd->first_data_sg = sg; | 
 | 698 | 	cmd->first_data_sg_off = page_off; | 
 | 699 |  | 
 | 700 | 	while (data_length) { | 
 | 701 | 		u32 cur_len = min_t(u32, data_length, sg->length - page_off); | 
 | 702 |  | 
 | 703 | 		iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off; | 
 | 704 | 		iov[i].iov_len = cur_len; | 
 | 705 |  | 
 | 706 | 		data_length -= cur_len; | 
 | 707 | 		page_off = 0; | 
 | 708 | 		sg = sg_next(sg); | 
 | 709 | 		i++; | 
 | 710 | 	} | 
 | 711 |  | 
 | 712 | 	cmd->kmapped_nents = i; | 
 | 713 |  | 
 | 714 | 	return i; | 
 | 715 | } | 
 | 716 |  | 
 | 717 | static void iscsit_unmap_iovec(struct iscsi_cmd *cmd) | 
 | 718 | { | 
 | 719 | 	u32 i; | 
 | 720 | 	struct scatterlist *sg; | 
 | 721 |  | 
 | 722 | 	sg = cmd->first_data_sg; | 
 | 723 |  | 
 | 724 | 	for (i = 0; i < cmd->kmapped_nents; i++) | 
 | 725 | 		kunmap(sg_page(&sg[i])); | 
 | 726 | } | 
 | 727 |  | 
 | 728 | static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn) | 
 | 729 | { | 
 | 730 | 	struct iscsi_cmd *cmd; | 
 | 731 |  | 
 | 732 | 	conn->exp_statsn = exp_statsn; | 
 | 733 |  | 
 | 734 | 	spin_lock_bh(&conn->cmd_lock); | 
| Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 735 | 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 736 | 		spin_lock(&cmd->istate_lock); | 
 | 737 | 		if ((cmd->i_state == ISTATE_SENT_STATUS) && | 
 | 738 | 		    (cmd->stat_sn < exp_statsn)) { | 
 | 739 | 			cmd->i_state = ISTATE_REMOVE; | 
 | 740 | 			spin_unlock(&cmd->istate_lock); | 
 | 741 | 			iscsit_add_cmd_to_immediate_queue(cmd, conn, | 
 | 742 | 						cmd->i_state); | 
 | 743 | 			continue; | 
 | 744 | 		} | 
 | 745 | 		spin_unlock(&cmd->istate_lock); | 
 | 746 | 	} | 
 | 747 | 	spin_unlock_bh(&conn->cmd_lock); | 
 | 748 | } | 
 | 749 |  | 
 | 750 | static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd) | 
 | 751 | { | 
| Nicholas Bellinger | f80e8ed | 2012-05-20 17:10:29 -0700 | [diff] [blame] | 752 | 	u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE)); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 753 |  | 
| Christoph Hellwig | c0427f1 | 2011-10-12 11:06:56 -0400 | [diff] [blame] | 754 | 	iov_count += ISCSI_IOV_DATA_BUFFER; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 755 |  | 
 | 756 | 	cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL); | 
 | 757 | 	if (!cmd->iov_data) { | 
 | 758 | 		pr_err("Unable to allocate cmd->iov_data\n"); | 
 | 759 | 		return -ENOMEM; | 
 | 760 | 	} | 
 | 761 |  | 
 | 762 | 	cmd->orig_iov_data_count = iov_count; | 
 | 763 | 	return 0; | 
 | 764 | } | 
 | 765 |  | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 766 | static int iscsit_handle_scsi_cmd( | 
 | 767 | 	struct iscsi_conn *conn, | 
 | 768 | 	unsigned char *buf) | 
 | 769 | { | 
 | 770 | 	int	data_direction, cmdsn_ret = 0, immed_ret, ret, transport_ret; | 
 | 771 | 	int	dump_immediate_data = 0, send_check_condition = 0, payload_length; | 
 | 772 | 	struct iscsi_cmd	*cmd = NULL; | 
 | 773 | 	struct iscsi_scsi_req *hdr; | 
| Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 774 | 	int iscsi_task_attr; | 
 | 775 | 	int sam_task_attr; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 776 |  | 
 | 777 | 	spin_lock_bh(&conn->sess->session_stats_lock); | 
 | 778 | 	conn->sess->cmd_pdus++; | 
 | 779 | 	if (conn->sess->se_sess->se_node_acl) { | 
 | 780 | 		spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock); | 
 | 781 | 		conn->sess->se_sess->se_node_acl->num_cmds++; | 
 | 782 | 		spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock); | 
 | 783 | 	} | 
 | 784 | 	spin_unlock_bh(&conn->sess->session_stats_lock); | 
 | 785 |  | 
 | 786 | 	hdr			= (struct iscsi_scsi_req *) buf; | 
 | 787 | 	payload_length		= ntoh24(hdr->dlength); | 
 | 788 | 	hdr->itt		= be32_to_cpu(hdr->itt); | 
 | 789 | 	hdr->data_length	= be32_to_cpu(hdr->data_length); | 
 | 790 | 	hdr->cmdsn		= be32_to_cpu(hdr->cmdsn); | 
 | 791 | 	hdr->exp_statsn		= be32_to_cpu(hdr->exp_statsn); | 
 | 792 |  | 
 | 793 | 	/* FIXME; Add checks for AdditionalHeaderSegment */ | 
 | 794 |  | 
 | 795 | 	if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) && | 
 | 796 | 	    !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) { | 
 | 797 | 		pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL" | 
 | 798 | 				" not set. Bad iSCSI Initiator.\n"); | 
 | 799 | 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1, | 
 | 800 | 				buf, conn); | 
 | 801 | 	} | 
 | 802 |  | 
 | 803 | 	if (((hdr->flags & ISCSI_FLAG_CMD_READ) || | 
 | 804 | 	     (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) { | 
 | 805 | 		/* | 
 | 806 | 		 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2) | 
 | 807 | 		 * that adds support for RESERVE/RELEASE.  There is a bug | 
 | 808 | 		 * add with this new functionality that sets R/W bits when | 
 | 809 | 		 * neither CDB carries any READ or WRITE datapayloads. | 
 | 810 | 		 */ | 
 | 811 | 		if ((hdr->cdb[0] == 0x16) || (hdr->cdb[0] == 0x17)) { | 
 | 812 | 			hdr->flags &= ~ISCSI_FLAG_CMD_READ; | 
 | 813 | 			hdr->flags &= ~ISCSI_FLAG_CMD_WRITE; | 
 | 814 | 			goto done; | 
 | 815 | 		} | 
 | 816 |  | 
 | 817 | 		pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE" | 
 | 818 | 			" set when Expected Data Transfer Length is 0 for" | 
 | 819 | 			" CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr->cdb[0]); | 
 | 820 | 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1, | 
 | 821 | 				buf, conn); | 
 | 822 | 	} | 
 | 823 | done: | 
 | 824 |  | 
 | 825 | 	if (!(hdr->flags & ISCSI_FLAG_CMD_READ) && | 
 | 826 | 	    !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) { | 
 | 827 | 		pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE" | 
 | 828 | 			" MUST be set if Expected Data Transfer Length is not 0." | 
 | 829 | 			" Bad iSCSI Initiator\n"); | 
 | 830 | 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1, | 
 | 831 | 				buf, conn); | 
 | 832 | 	} | 
 | 833 |  | 
 | 834 | 	if ((hdr->flags & ISCSI_FLAG_CMD_READ) && | 
 | 835 | 	    (hdr->flags & ISCSI_FLAG_CMD_WRITE)) { | 
 | 836 | 		pr_err("Bidirectional operations not supported!\n"); | 
 | 837 | 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1, | 
 | 838 | 				buf, conn); | 
 | 839 | 	} | 
 | 840 |  | 
 | 841 | 	if (hdr->opcode & ISCSI_OP_IMMEDIATE) { | 
 | 842 | 		pr_err("Illegally set Immediate Bit in iSCSI Initiator" | 
 | 843 | 				" Scsi Command PDU.\n"); | 
 | 844 | 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1, | 
 | 845 | 				buf, conn); | 
 | 846 | 	} | 
 | 847 |  | 
 | 848 | 	if (payload_length && !conn->sess->sess_ops->ImmediateData) { | 
 | 849 | 		pr_err("ImmediateData=No but DataSegmentLength=%u," | 
 | 850 | 			" protocol error.\n", payload_length); | 
 | 851 | 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1, | 
 | 852 | 				buf, conn); | 
 | 853 | 	} | 
 | 854 |  | 
 | 855 | 	if ((hdr->data_length == payload_length) && | 
 | 856 | 	    (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) { | 
 | 857 | 		pr_err("Expected Data Transfer Length and Length of" | 
 | 858 | 			" Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL" | 
 | 859 | 			" bit is not set protocol error\n"); | 
 | 860 | 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1, | 
 | 861 | 				buf, conn); | 
 | 862 | 	} | 
 | 863 |  | 
 | 864 | 	if (payload_length > hdr->data_length) { | 
 | 865 | 		pr_err("DataSegmentLength: %u is greater than" | 
 | 866 | 			" EDTL: %u, protocol error.\n", payload_length, | 
 | 867 | 				hdr->data_length); | 
 | 868 | 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1, | 
 | 869 | 				buf, conn); | 
 | 870 | 	} | 
 | 871 |  | 
| Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 872 | 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 873 | 		pr_err("DataSegmentLength: %u is greater than" | 
| Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 874 | 			" MaxXmitDataSegmentLength: %u, protocol error.\n", | 
 | 875 | 			payload_length, conn->conn_ops->MaxXmitDataSegmentLength); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 876 | 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1, | 
 | 877 | 				buf, conn); | 
 | 878 | 	} | 
 | 879 |  | 
 | 880 | 	if (payload_length > conn->sess->sess_ops->FirstBurstLength) { | 
 | 881 | 		pr_err("DataSegmentLength: %u is greater than" | 
 | 882 | 			" FirstBurstLength: %u, protocol error.\n", | 
 | 883 | 			payload_length, conn->sess->sess_ops->FirstBurstLength); | 
 | 884 | 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1, | 
 | 885 | 					buf, conn); | 
 | 886 | 	} | 
 | 887 |  | 
 | 888 | 	data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE : | 
 | 889 | 			 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE : | 
 | 890 | 			  DMA_NONE; | 
 | 891 |  | 
| Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 892 | 	cmd = iscsit_allocate_cmd(conn, GFP_KERNEL); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 893 | 	if (!cmd) | 
 | 894 | 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1, | 
| Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 895 | 					 buf, conn); | 
 | 896 |  | 
 | 897 | 	cmd->data_direction = data_direction; | 
| Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 898 | 	iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK; | 
 | 899 | 	/* | 
 | 900 | 	 * Figure out the SAM Task Attribute for the incoming SCSI CDB | 
 | 901 | 	 */ | 
 | 902 | 	if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) || | 
 | 903 | 	    (iscsi_task_attr == ISCSI_ATTR_SIMPLE)) | 
 | 904 | 		sam_task_attr = MSG_SIMPLE_TAG; | 
 | 905 | 	else if (iscsi_task_attr == ISCSI_ATTR_ORDERED) | 
 | 906 | 		sam_task_attr = MSG_ORDERED_TAG; | 
 | 907 | 	else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE) | 
 | 908 | 		sam_task_attr = MSG_HEAD_TAG; | 
 | 909 | 	else if (iscsi_task_attr == ISCSI_ATTR_ACA) | 
 | 910 | 		sam_task_attr = MSG_ACA_TAG; | 
 | 911 | 	else { | 
 | 912 | 		pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using" | 
 | 913 | 			" MSG_SIMPLE_TAG\n", iscsi_task_attr); | 
 | 914 | 		sam_task_attr = MSG_SIMPLE_TAG; | 
 | 915 | 	} | 
 | 916 |  | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 917 | 	cmd->iscsi_opcode	= ISCSI_OP_SCSI_CMD; | 
 | 918 | 	cmd->i_state		= ISTATE_NEW_CMD; | 
 | 919 | 	cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); | 
 | 920 | 	cmd->immediate_data	= (payload_length) ? 1 : 0; | 
 | 921 | 	cmd->unsolicited_data	= ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) && | 
 | 922 | 				     (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0); | 
 | 923 | 	if (cmd->unsolicited_data) | 
 | 924 | 		cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA; | 
 | 925 |  | 
 | 926 | 	conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt; | 
 | 927 | 	if (hdr->flags & ISCSI_FLAG_CMD_READ) { | 
 | 928 | 		spin_lock_bh(&conn->sess->ttt_lock); | 
 | 929 | 		cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++; | 
 | 930 | 		if (cmd->targ_xfer_tag == 0xFFFFFFFF) | 
 | 931 | 			cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++; | 
 | 932 | 		spin_unlock_bh(&conn->sess->ttt_lock); | 
 | 933 | 	} else if (hdr->flags & ISCSI_FLAG_CMD_WRITE) | 
 | 934 | 		cmd->targ_xfer_tag = 0xFFFFFFFF; | 
 | 935 | 	cmd->cmd_sn		= hdr->cmdsn; | 
 | 936 | 	cmd->exp_stat_sn	= hdr->exp_statsn; | 
 | 937 | 	cmd->first_burst_len	= payload_length; | 
 | 938 |  | 
 | 939 | 	if (cmd->data_direction == DMA_FROM_DEVICE) { | 
 | 940 | 		struct iscsi_datain_req *dr; | 
 | 941 |  | 
 | 942 | 		dr = iscsit_allocate_datain_req(); | 
 | 943 | 		if (!dr) | 
 | 944 | 			return iscsit_add_reject_from_cmd( | 
 | 945 | 					ISCSI_REASON_BOOKMARK_NO_RESOURCES, | 
 | 946 | 					1, 1, buf, cmd); | 
 | 947 |  | 
 | 948 | 		iscsit_attach_datain_req(cmd, dr); | 
 | 949 | 	} | 
 | 950 |  | 
 | 951 | 	/* | 
| Andy Grover | 065ca1e | 2012-04-03 15:51:23 -0700 | [diff] [blame] | 952 | 	 * Initialize struct se_cmd descriptor from target_core_mod infrastructure | 
 | 953 | 	 */ | 
 | 954 | 	transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops, | 
| Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 955 | 			conn->sess->se_sess, hdr->data_length, cmd->data_direction, | 
| Roland Dreier | 9c58b7d | 2012-08-15 14:35:25 -0700 | [diff] [blame] | 956 | 			sam_task_attr, cmd->sense_buffer + 2); | 
| Andy Grover | 065ca1e | 2012-04-03 15:51:23 -0700 | [diff] [blame] | 957 |  | 
 | 958 | 	pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x," | 
 | 959 | 		" ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt, | 
 | 960 | 		hdr->cmdsn, hdr->data_length, payload_length, conn->cid); | 
 | 961 |  | 
 | 962 | 	/* | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 963 | 	 * The CDB is going to an se_device_t. | 
 | 964 | 	 */ | 
| Andy Grover | 4f26998 | 2012-01-19 13:39:14 -0800 | [diff] [blame] | 965 | 	ret = transport_lookup_cmd_lun(&cmd->se_cmd, | 
 | 966 | 				       scsilun_to_int(&hdr->lun)); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 967 | 	if (ret < 0) { | 
 | 968 | 		if (cmd->se_cmd.scsi_sense_reason == TCM_NON_EXISTENT_LUN) { | 
 | 969 | 			pr_debug("Responding to non-acl'ed," | 
 | 970 | 				" non-existent or non-exported iSCSI LUN:" | 
 | 971 | 				" 0x%016Lx\n", get_unaligned_le64(&hdr->lun)); | 
 | 972 | 		} | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 973 | 		send_check_condition = 1; | 
 | 974 | 		goto attach_cmd; | 
 | 975 | 	} | 
| Andy Grover | a12f41f | 2012-04-03 15:51:20 -0700 | [diff] [blame] | 976 |  | 
 | 977 | 	transport_ret = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 978 | 	if (transport_ret == -ENOMEM) { | 
 | 979 | 		return iscsit_add_reject_from_cmd( | 
 | 980 | 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, | 
 | 981 | 				1, 1, buf, cmd); | 
| Nicholas Bellinger | 00fdc6b | 2012-03-13 18:20:11 -0700 | [diff] [blame] | 982 | 	} else if (transport_ret < 0) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 983 | 		/* | 
 | 984 | 		 * Unsupported SAM Opcode.  CHECK_CONDITION will be sent | 
 | 985 | 		 * in iscsit_execute_cmd() during the CmdSN OOO Execution | 
 | 986 | 		 * Mechinism. | 
 | 987 | 		 */ | 
 | 988 | 		send_check_condition = 1; | 
 | 989 | 	} else { | 
| Andy Grover | 4334e49 | 2012-04-03 15:51:25 -0700 | [diff] [blame] | 990 | 		if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 991 | 			return iscsit_add_reject_from_cmd( | 
 | 992 | 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, | 
 | 993 | 				1, 1, buf, cmd); | 
 | 994 | 	} | 
 | 995 |  | 
 | 996 | attach_cmd: | 
 | 997 | 	spin_lock_bh(&conn->cmd_lock); | 
| Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 998 | 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 999 | 	spin_unlock_bh(&conn->cmd_lock); | 
 | 1000 | 	/* | 
 | 1001 | 	 * Check if we need to delay processing because of ALUA | 
 | 1002 | 	 * Active/NonOptimized primary access state.. | 
 | 1003 | 	 */ | 
 | 1004 | 	core_alua_check_nonop_delay(&cmd->se_cmd); | 
| Andy Grover | bfb79ea | 2012-04-03 15:51:29 -0700 | [diff] [blame] | 1005 |  | 
 | 1006 | 	ret = iscsit_allocate_iovecs(cmd); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1007 | 	if (ret < 0) | 
 | 1008 | 		return iscsit_add_reject_from_cmd( | 
 | 1009 | 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, | 
| Nicholas Bellinger | cd931ee | 2012-01-16 17:11:54 -0800 | [diff] [blame] | 1010 | 				1, 0, buf, cmd); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1011 | 	/* | 
 | 1012 | 	 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if | 
 | 1013 | 	 * the Immediate Bit is not set, and no Immediate | 
 | 1014 | 	 * Data is attached. | 
 | 1015 | 	 * | 
 | 1016 | 	 * A PDU/CmdSN carrying Immediate Data can only | 
 | 1017 | 	 * be processed after the DataCRC has passed. | 
 | 1018 | 	 * If the DataCRC fails, the CmdSN MUST NOT | 
 | 1019 | 	 * be acknowledged. (See below) | 
 | 1020 | 	 */ | 
 | 1021 | 	if (!cmd->immediate_data) { | 
 | 1022 | 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn); | 
| Nicholas Bellinger | 7e32da5 | 2011-10-28 13:32:35 -0700 | [diff] [blame] | 1023 | 		if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) | 
 | 1024 | 			return 0; | 
 | 1025 | 		else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1026 | 			return iscsit_add_reject_from_cmd( | 
 | 1027 | 				ISCSI_REASON_PROTOCOL_ERROR, | 
 | 1028 | 				1, 0, buf, cmd); | 
 | 1029 | 	} | 
 | 1030 |  | 
 | 1031 | 	iscsit_ack_from_expstatsn(conn, hdr->exp_statsn); | 
 | 1032 |  | 
 | 1033 | 	/* | 
 | 1034 | 	 * If no Immediate Data is attached, it's OK to return now. | 
 | 1035 | 	 */ | 
 | 1036 | 	if (!cmd->immediate_data) { | 
 | 1037 | 		if (send_check_condition) | 
 | 1038 | 			return 0; | 
 | 1039 |  | 
 | 1040 | 		if (cmd->unsolicited_data) { | 
 | 1041 | 			iscsit_set_dataout_sequence_values(cmd); | 
 | 1042 |  | 
 | 1043 | 			spin_lock_bh(&cmd->dataout_timeout_lock); | 
 | 1044 | 			iscsit_start_dataout_timer(cmd, cmd->conn); | 
 | 1045 | 			spin_unlock_bh(&cmd->dataout_timeout_lock); | 
 | 1046 | 		} | 
 | 1047 |  | 
 | 1048 | 		return 0; | 
 | 1049 | 	} | 
 | 1050 |  | 
 | 1051 | 	/* | 
 | 1052 | 	 * Early CHECK_CONDITIONs never make it to the transport processing | 
 | 1053 | 	 * thread.  They are processed in CmdSN order by | 
 | 1054 | 	 * iscsit_check_received_cmdsn() below. | 
 | 1055 | 	 */ | 
 | 1056 | 	if (send_check_condition) { | 
 | 1057 | 		immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION; | 
 | 1058 | 		dump_immediate_data = 1; | 
 | 1059 | 		goto after_immediate_data; | 
 | 1060 | 	} | 
 | 1061 | 	/* | 
 | 1062 | 	 * Call directly into transport_generic_new_cmd() to perform | 
 | 1063 | 	 * the backend memory allocation. | 
 | 1064 | 	 */ | 
 | 1065 | 	ret = transport_generic_new_cmd(&cmd->se_cmd); | 
| Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1066 | 	if (ret < 0) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1067 | 		immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION; | 
 | 1068 | 		dump_immediate_data = 1; | 
 | 1069 | 		goto after_immediate_data; | 
 | 1070 | 	} | 
 | 1071 |  | 
 | 1072 | 	immed_ret = iscsit_handle_immediate_data(cmd, buf, payload_length); | 
 | 1073 | after_immediate_data: | 
 | 1074 | 	if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) { | 
 | 1075 | 		/* | 
 | 1076 | 		 * A PDU/CmdSN carrying Immediate Data passed | 
 | 1077 | 		 * DataCRC, check against ExpCmdSN/MaxCmdSN if | 
 | 1078 | 		 * Immediate Bit is not set. | 
 | 1079 | 		 */ | 
 | 1080 | 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn); | 
 | 1081 | 		/* | 
 | 1082 | 		 * Special case for Unsupported SAM WRITE Opcodes | 
 | 1083 | 		 * and ImmediateData=Yes. | 
 | 1084 | 		 */ | 
 | 1085 | 		if (dump_immediate_data) { | 
 | 1086 | 			if (iscsit_dump_data_payload(conn, payload_length, 1) < 0) | 
 | 1087 | 				return -1; | 
 | 1088 | 		} else if (cmd->unsolicited_data) { | 
 | 1089 | 			iscsit_set_dataout_sequence_values(cmd); | 
 | 1090 |  | 
 | 1091 | 			spin_lock_bh(&cmd->dataout_timeout_lock); | 
 | 1092 | 			iscsit_start_dataout_timer(cmd, cmd->conn); | 
 | 1093 | 			spin_unlock_bh(&cmd->dataout_timeout_lock); | 
 | 1094 | 		} | 
 | 1095 |  | 
 | 1096 | 		if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) | 
 | 1097 | 			return iscsit_add_reject_from_cmd( | 
 | 1098 | 				ISCSI_REASON_PROTOCOL_ERROR, | 
 | 1099 | 				1, 0, buf, cmd); | 
 | 1100 |  | 
 | 1101 | 	} else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) { | 
 | 1102 | 		/* | 
 | 1103 | 		 * Immediate Data failed DataCRC and ERL>=1, | 
 | 1104 | 		 * silently drop this PDU and let the initiator | 
 | 1105 | 		 * plug the CmdSN gap. | 
 | 1106 | 		 * | 
 | 1107 | 		 * FIXME: Send Unsolicited NOPIN with reserved | 
 | 1108 | 		 * TTT here to help the initiator figure out | 
 | 1109 | 		 * the missing CmdSN, although they should be | 
 | 1110 | 		 * intelligent enough to determine the missing | 
 | 1111 | 		 * CmdSN and issue a retry to plug the sequence. | 
 | 1112 | 		 */ | 
 | 1113 | 		cmd->i_state = ISTATE_REMOVE; | 
 | 1114 | 		iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state); | 
 | 1115 | 	} else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */ | 
 | 1116 | 		return -1; | 
 | 1117 |  | 
 | 1118 | 	return 0; | 
 | 1119 | } | 
 | 1120 |  | 
 | 1121 | static u32 iscsit_do_crypto_hash_sg( | 
 | 1122 | 	struct hash_desc *hash, | 
 | 1123 | 	struct iscsi_cmd *cmd, | 
 | 1124 | 	u32 data_offset, | 
 | 1125 | 	u32 data_length, | 
 | 1126 | 	u32 padding, | 
 | 1127 | 	u8 *pad_bytes) | 
 | 1128 | { | 
 | 1129 | 	u32 data_crc; | 
 | 1130 | 	u32 i; | 
 | 1131 | 	struct scatterlist *sg; | 
 | 1132 | 	unsigned int page_off; | 
 | 1133 |  | 
 | 1134 | 	crypto_hash_init(hash); | 
 | 1135 |  | 
 | 1136 | 	sg = cmd->first_data_sg; | 
 | 1137 | 	page_off = cmd->first_data_sg_off; | 
 | 1138 |  | 
 | 1139 | 	i = 0; | 
 | 1140 | 	while (data_length) { | 
 | 1141 | 		u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off)); | 
 | 1142 |  | 
 | 1143 | 		crypto_hash_update(hash, &sg[i], cur_len); | 
 | 1144 |  | 
 | 1145 | 		data_length -= cur_len; | 
 | 1146 | 		page_off = 0; | 
 | 1147 | 		i++; | 
 | 1148 | 	} | 
 | 1149 |  | 
 | 1150 | 	if (padding) { | 
 | 1151 | 		struct scatterlist pad_sg; | 
 | 1152 |  | 
 | 1153 | 		sg_init_one(&pad_sg, pad_bytes, padding); | 
 | 1154 | 		crypto_hash_update(hash, &pad_sg, padding); | 
 | 1155 | 	} | 
 | 1156 | 	crypto_hash_final(hash, (u8 *) &data_crc); | 
 | 1157 |  | 
 | 1158 | 	return data_crc; | 
 | 1159 | } | 
 | 1160 |  | 
 | 1161 | static void iscsit_do_crypto_hash_buf( | 
 | 1162 | 	struct hash_desc *hash, | 
 | 1163 | 	unsigned char *buf, | 
 | 1164 | 	u32 payload_length, | 
 | 1165 | 	u32 padding, | 
 | 1166 | 	u8 *pad_bytes, | 
 | 1167 | 	u8 *data_crc) | 
 | 1168 | { | 
 | 1169 | 	struct scatterlist sg; | 
 | 1170 |  | 
 | 1171 | 	crypto_hash_init(hash); | 
 | 1172 |  | 
| Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 1173 | 	sg_init_one(&sg, buf, payload_length); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1174 | 	crypto_hash_update(hash, &sg, payload_length); | 
 | 1175 |  | 
 | 1176 | 	if (padding) { | 
 | 1177 | 		sg_init_one(&sg, pad_bytes, padding); | 
 | 1178 | 		crypto_hash_update(hash, &sg, padding); | 
 | 1179 | 	} | 
 | 1180 | 	crypto_hash_final(hash, data_crc); | 
 | 1181 | } | 
 | 1182 |  | 
 | 1183 | static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf) | 
 | 1184 | { | 
 | 1185 | 	int iov_ret, ooo_cmdsn = 0, ret; | 
 | 1186 | 	u8 data_crc_failed = 0; | 
 | 1187 | 	u32 checksum, iov_count = 0, padding = 0, rx_got = 0; | 
 | 1188 | 	u32 rx_size = 0, payload_length; | 
 | 1189 | 	struct iscsi_cmd *cmd = NULL; | 
 | 1190 | 	struct se_cmd *se_cmd; | 
 | 1191 | 	struct iscsi_data *hdr; | 
 | 1192 | 	struct kvec *iov; | 
 | 1193 | 	unsigned long flags; | 
 | 1194 |  | 
 | 1195 | 	hdr			= (struct iscsi_data *) buf; | 
 | 1196 | 	payload_length		= ntoh24(hdr->dlength); | 
 | 1197 | 	hdr->itt		= be32_to_cpu(hdr->itt); | 
 | 1198 | 	hdr->ttt		= be32_to_cpu(hdr->ttt); | 
 | 1199 | 	hdr->exp_statsn		= be32_to_cpu(hdr->exp_statsn); | 
 | 1200 | 	hdr->datasn		= be32_to_cpu(hdr->datasn); | 
 | 1201 | 	hdr->offset		= be32_to_cpu(hdr->offset); | 
 | 1202 |  | 
 | 1203 | 	if (!payload_length) { | 
 | 1204 | 		pr_err("DataOUT payload is ZERO, protocol error.\n"); | 
 | 1205 | 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1, | 
 | 1206 | 					buf, conn); | 
 | 1207 | 	} | 
 | 1208 |  | 
 | 1209 | 	/* iSCSI write */ | 
 | 1210 | 	spin_lock_bh(&conn->sess->session_stats_lock); | 
 | 1211 | 	conn->sess->rx_data_octets += payload_length; | 
 | 1212 | 	if (conn->sess->se_sess->se_node_acl) { | 
 | 1213 | 		spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock); | 
 | 1214 | 		conn->sess->se_sess->se_node_acl->write_bytes += payload_length; | 
 | 1215 | 		spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock); | 
 | 1216 | 	} | 
 | 1217 | 	spin_unlock_bh(&conn->sess->session_stats_lock); | 
 | 1218 |  | 
| Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 1219 | 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1220 | 		pr_err("DataSegmentLength: %u is greater than" | 
| Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 1221 | 			" MaxXmitDataSegmentLength: %u\n", payload_length, | 
 | 1222 | 			conn->conn_ops->MaxXmitDataSegmentLength); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1223 | 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1, | 
 | 1224 | 					buf, conn); | 
 | 1225 | 	} | 
 | 1226 |  | 
 | 1227 | 	cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, | 
 | 1228 | 			payload_length); | 
 | 1229 | 	if (!cmd) | 
 | 1230 | 		return 0; | 
 | 1231 |  | 
 | 1232 | 	pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x," | 
 | 1233 | 		" DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n", | 
 | 1234 | 		hdr->itt, hdr->ttt, hdr->datasn, hdr->offset, | 
 | 1235 | 		payload_length, conn->cid); | 
 | 1236 |  | 
 | 1237 | 	if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) { | 
 | 1238 | 		pr_err("Command ITT: 0x%08x received DataOUT after" | 
 | 1239 | 			" last DataOUT received, dumping payload\n", | 
 | 1240 | 			cmd->init_task_tag); | 
 | 1241 | 		return iscsit_dump_data_payload(conn, payload_length, 1); | 
 | 1242 | 	} | 
 | 1243 |  | 
 | 1244 | 	if (cmd->data_direction != DMA_TO_DEVICE) { | 
 | 1245 | 		pr_err("Command ITT: 0x%08x received DataOUT for a" | 
 | 1246 | 			" NON-WRITE command.\n", cmd->init_task_tag); | 
 | 1247 | 		return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR, | 
 | 1248 | 				1, 0, buf, cmd); | 
 | 1249 | 	} | 
 | 1250 | 	se_cmd = &cmd->se_cmd; | 
 | 1251 | 	iscsit_mod_dataout_timer(cmd); | 
 | 1252 |  | 
| Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 1253 | 	if ((hdr->offset + payload_length) > cmd->se_cmd.data_length) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1254 | 		pr_err("DataOut Offset: %u, Length %u greater than" | 
 | 1255 | 			" iSCSI Command EDTL %u, protocol error.\n", | 
| Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 1256 | 			hdr->offset, payload_length, cmd->se_cmd.data_length); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1257 | 		return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID, | 
 | 1258 | 				1, 0, buf, cmd); | 
 | 1259 | 	} | 
 | 1260 |  | 
 | 1261 | 	if (cmd->unsolicited_data) { | 
 | 1262 | 		int dump_unsolicited_data = 0; | 
 | 1263 |  | 
 | 1264 | 		if (conn->sess->sess_ops->InitialR2T) { | 
 | 1265 | 			pr_err("Received unexpected unsolicited data" | 
 | 1266 | 				" while InitialR2T=Yes, protocol error.\n"); | 
 | 1267 | 			transport_send_check_condition_and_sense(&cmd->se_cmd, | 
 | 1268 | 					TCM_UNEXPECTED_UNSOLICITED_DATA, 0); | 
 | 1269 | 			return -1; | 
 | 1270 | 		} | 
 | 1271 | 		/* | 
 | 1272 | 		 * Special case for dealing with Unsolicited DataOUT | 
 | 1273 | 		 * and Unsupported SAM WRITE Opcodes and SE resource allocation | 
 | 1274 | 		 * failures; | 
 | 1275 | 		 */ | 
 | 1276 |  | 
 | 1277 | 		/* Something's amiss if we're not in WRITE_PENDING state... */ | 
 | 1278 | 		spin_lock_irqsave(&se_cmd->t_state_lock, flags); | 
 | 1279 | 		WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING); | 
 | 1280 | 		spin_unlock_irqrestore(&se_cmd->t_state_lock, flags); | 
 | 1281 |  | 
 | 1282 | 		spin_lock_irqsave(&se_cmd->t_state_lock, flags); | 
 | 1283 | 		if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) || | 
| Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1284 | 		     (se_cmd->se_cmd_flags & SCF_SCSI_CDB_EXCEPTION)) | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1285 | 			dump_unsolicited_data = 1; | 
 | 1286 | 		spin_unlock_irqrestore(&se_cmd->t_state_lock, flags); | 
 | 1287 |  | 
 | 1288 | 		if (dump_unsolicited_data) { | 
 | 1289 | 			/* | 
 | 1290 | 			 * Check if a delayed TASK_ABORTED status needs to | 
 | 1291 | 			 * be sent now if the ISCSI_FLAG_CMD_FINAL has been | 
 | 1292 | 			 * received with the unsolicitied data out. | 
 | 1293 | 			 */ | 
 | 1294 | 			if (hdr->flags & ISCSI_FLAG_CMD_FINAL) | 
 | 1295 | 				iscsit_stop_dataout_timer(cmd); | 
 | 1296 |  | 
 | 1297 | 			transport_check_aborted_status(se_cmd, | 
 | 1298 | 					(hdr->flags & ISCSI_FLAG_CMD_FINAL)); | 
 | 1299 | 			return iscsit_dump_data_payload(conn, payload_length, 1); | 
 | 1300 | 		} | 
 | 1301 | 	} else { | 
 | 1302 | 		/* | 
 | 1303 | 		 * For the normal solicited data path: | 
 | 1304 | 		 * | 
 | 1305 | 		 * Check for a delayed TASK_ABORTED status and dump any | 
 | 1306 | 		 * incoming data out payload if one exists.  Also, when the | 
 | 1307 | 		 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current | 
 | 1308 | 		 * data out sequence, we decrement outstanding_r2ts.  Once | 
 | 1309 | 		 * outstanding_r2ts reaches zero, go ahead and send the delayed | 
 | 1310 | 		 * TASK_ABORTED status. | 
 | 1311 | 		 */ | 
| Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 1312 | 		if (se_cmd->transport_state & CMD_T_ABORTED) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1313 | 			if (hdr->flags & ISCSI_FLAG_CMD_FINAL) | 
 | 1314 | 				if (--cmd->outstanding_r2ts < 1) { | 
 | 1315 | 					iscsit_stop_dataout_timer(cmd); | 
 | 1316 | 					transport_check_aborted_status( | 
 | 1317 | 							se_cmd, 1); | 
 | 1318 | 				} | 
 | 1319 |  | 
 | 1320 | 			return iscsit_dump_data_payload(conn, payload_length, 1); | 
 | 1321 | 		} | 
 | 1322 | 	} | 
 | 1323 | 	/* | 
 | 1324 | 	 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and | 
 | 1325 | 	 * within-command recovery checks before receiving the payload. | 
 | 1326 | 	 */ | 
 | 1327 | 	ret = iscsit_check_pre_dataout(cmd, buf); | 
 | 1328 | 	if (ret == DATAOUT_WITHIN_COMMAND_RECOVERY) | 
 | 1329 | 		return 0; | 
 | 1330 | 	else if (ret == DATAOUT_CANNOT_RECOVER) | 
 | 1331 | 		return -1; | 
 | 1332 |  | 
 | 1333 | 	rx_size += payload_length; | 
 | 1334 | 	iov = &cmd->iov_data[0]; | 
 | 1335 |  | 
 | 1336 | 	iov_ret = iscsit_map_iovec(cmd, iov, hdr->offset, payload_length); | 
 | 1337 | 	if (iov_ret < 0) | 
 | 1338 | 		return -1; | 
 | 1339 |  | 
 | 1340 | 	iov_count += iov_ret; | 
 | 1341 |  | 
 | 1342 | 	padding = ((-payload_length) & 3); | 
 | 1343 | 	if (padding != 0) { | 
 | 1344 | 		iov[iov_count].iov_base	= cmd->pad_bytes; | 
 | 1345 | 		iov[iov_count++].iov_len = padding; | 
 | 1346 | 		rx_size += padding; | 
 | 1347 | 		pr_debug("Receiving %u padding bytes.\n", padding); | 
 | 1348 | 	} | 
 | 1349 |  | 
 | 1350 | 	if (conn->conn_ops->DataDigest) { | 
 | 1351 | 		iov[iov_count].iov_base = &checksum; | 
 | 1352 | 		iov[iov_count++].iov_len = ISCSI_CRC_LEN; | 
 | 1353 | 		rx_size += ISCSI_CRC_LEN; | 
 | 1354 | 	} | 
 | 1355 |  | 
 | 1356 | 	rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size); | 
 | 1357 |  | 
 | 1358 | 	iscsit_unmap_iovec(cmd); | 
 | 1359 |  | 
 | 1360 | 	if (rx_got != rx_size) | 
 | 1361 | 		return -1; | 
 | 1362 |  | 
 | 1363 | 	if (conn->conn_ops->DataDigest) { | 
 | 1364 | 		u32 data_crc; | 
 | 1365 |  | 
 | 1366 | 		data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd, | 
 | 1367 | 						    hdr->offset, payload_length, padding, | 
 | 1368 | 						    cmd->pad_bytes); | 
 | 1369 |  | 
 | 1370 | 		if (checksum != data_crc) { | 
 | 1371 | 			pr_err("ITT: 0x%08x, Offset: %u, Length: %u," | 
 | 1372 | 				" DataSN: 0x%08x, CRC32C DataDigest 0x%08x" | 
 | 1373 | 				" does not match computed 0x%08x\n", | 
 | 1374 | 				hdr->itt, hdr->offset, payload_length, | 
 | 1375 | 				hdr->datasn, checksum, data_crc); | 
 | 1376 | 			data_crc_failed = 1; | 
 | 1377 | 		} else { | 
 | 1378 | 			pr_debug("Got CRC32C DataDigest 0x%08x for" | 
 | 1379 | 				" %u bytes of Data Out\n", checksum, | 
 | 1380 | 				payload_length); | 
 | 1381 | 		} | 
 | 1382 | 	} | 
 | 1383 | 	/* | 
 | 1384 | 	 * Increment post receive data and CRC values or perform | 
 | 1385 | 	 * within-command recovery. | 
 | 1386 | 	 */ | 
 | 1387 | 	ret = iscsit_check_post_dataout(cmd, buf, data_crc_failed); | 
 | 1388 | 	if ((ret == DATAOUT_NORMAL) || (ret == DATAOUT_WITHIN_COMMAND_RECOVERY)) | 
 | 1389 | 		return 0; | 
 | 1390 | 	else if (ret == DATAOUT_SEND_R2T) { | 
 | 1391 | 		iscsit_set_dataout_sequence_values(cmd); | 
| Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 1392 | 		iscsit_build_r2ts_for_cmd(cmd, conn, false); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1393 | 	} else if (ret == DATAOUT_SEND_TO_TRANSPORT) { | 
 | 1394 | 		/* | 
 | 1395 | 		 * Handle extra special case for out of order | 
 | 1396 | 		 * Unsolicited Data Out. | 
 | 1397 | 		 */ | 
 | 1398 | 		spin_lock_bh(&cmd->istate_lock); | 
 | 1399 | 		ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN); | 
 | 1400 | 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT; | 
 | 1401 | 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT; | 
 | 1402 | 		spin_unlock_bh(&cmd->istate_lock); | 
 | 1403 |  | 
 | 1404 | 		iscsit_stop_dataout_timer(cmd); | 
| Christoph Hellwig | 67441b6 | 2012-07-08 15:58:42 -0400 | [diff] [blame] | 1405 | 		if (ooo_cmdsn) | 
 | 1406 | 			return 0; | 
 | 1407 | 		target_execute_cmd(&cmd->se_cmd); | 
 | 1408 | 		return 0; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1409 | 	} else /* DATAOUT_CANNOT_RECOVER */ | 
 | 1410 | 		return -1; | 
 | 1411 |  | 
 | 1412 | 	return 0; | 
 | 1413 | } | 
 | 1414 |  | 
 | 1415 | static int iscsit_handle_nop_out( | 
 | 1416 | 	struct iscsi_conn *conn, | 
 | 1417 | 	unsigned char *buf) | 
 | 1418 | { | 
 | 1419 | 	unsigned char *ping_data = NULL; | 
 | 1420 | 	int cmdsn_ret, niov = 0, ret = 0, rx_got, rx_size; | 
 | 1421 | 	u32 checksum, data_crc, padding = 0, payload_length; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1422 | 	struct iscsi_cmd *cmd = NULL; | 
 | 1423 | 	struct kvec *iov = NULL; | 
 | 1424 | 	struct iscsi_nopout *hdr; | 
 | 1425 |  | 
 | 1426 | 	hdr			= (struct iscsi_nopout *) buf; | 
 | 1427 | 	payload_length		= ntoh24(hdr->dlength); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1428 | 	hdr->itt		= be32_to_cpu(hdr->itt); | 
 | 1429 | 	hdr->ttt		= be32_to_cpu(hdr->ttt); | 
 | 1430 | 	hdr->cmdsn		= be32_to_cpu(hdr->cmdsn); | 
 | 1431 | 	hdr->exp_statsn		= be32_to_cpu(hdr->exp_statsn); | 
 | 1432 |  | 
 | 1433 | 	if ((hdr->itt == 0xFFFFFFFF) && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { | 
 | 1434 | 		pr_err("NOPOUT ITT is reserved, but Immediate Bit is" | 
 | 1435 | 			" not set, protocol error.\n"); | 
 | 1436 | 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1, | 
 | 1437 | 					buf, conn); | 
 | 1438 | 	} | 
 | 1439 |  | 
| Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 1440 | 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1441 | 		pr_err("NOPOUT Ping Data DataSegmentLength: %u is" | 
| Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 1442 | 			" greater than MaxXmitDataSegmentLength: %u, protocol" | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1443 | 			" error.\n", payload_length, | 
| Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 1444 | 			conn->conn_ops->MaxXmitDataSegmentLength); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1445 | 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1, | 
 | 1446 | 					buf, conn); | 
 | 1447 | 	} | 
 | 1448 |  | 
 | 1449 | 	pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%09x," | 
 | 1450 | 		" CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n", | 
 | 1451 | 		(hdr->itt == 0xFFFFFFFF) ? "Response" : "Request", | 
 | 1452 | 		hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn, | 
 | 1453 | 		payload_length); | 
 | 1454 | 	/* | 
 | 1455 | 	 * This is not a response to a Unsolicited NopIN, which means | 
 | 1456 | 	 * it can either be a NOPOUT ping request (with a valid ITT), | 
 | 1457 | 	 * or a NOPOUT not requesting a NOPIN (with a reserved ITT). | 
 | 1458 | 	 * Either way, make sure we allocate an struct iscsi_cmd, as both | 
 | 1459 | 	 * can contain ping data. | 
 | 1460 | 	 */ | 
 | 1461 | 	if (hdr->ttt == 0xFFFFFFFF) { | 
 | 1462 | 		cmd = iscsit_allocate_cmd(conn, GFP_KERNEL); | 
 | 1463 | 		if (!cmd) | 
 | 1464 | 			return iscsit_add_reject( | 
 | 1465 | 					ISCSI_REASON_BOOKMARK_NO_RESOURCES, | 
 | 1466 | 					1, buf, conn); | 
 | 1467 |  | 
 | 1468 | 		cmd->iscsi_opcode	= ISCSI_OP_NOOP_OUT; | 
 | 1469 | 		cmd->i_state		= ISTATE_SEND_NOPIN; | 
 | 1470 | 		cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? | 
 | 1471 | 						1 : 0); | 
 | 1472 | 		conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt; | 
 | 1473 | 		cmd->targ_xfer_tag	= 0xFFFFFFFF; | 
 | 1474 | 		cmd->cmd_sn		= hdr->cmdsn; | 
 | 1475 | 		cmd->exp_stat_sn	= hdr->exp_statsn; | 
 | 1476 | 		cmd->data_direction	= DMA_NONE; | 
 | 1477 | 	} | 
 | 1478 |  | 
 | 1479 | 	if (payload_length && (hdr->ttt == 0xFFFFFFFF)) { | 
 | 1480 | 		rx_size = payload_length; | 
 | 1481 | 		ping_data = kzalloc(payload_length + 1, GFP_KERNEL); | 
 | 1482 | 		if (!ping_data) { | 
 | 1483 | 			pr_err("Unable to allocate memory for" | 
 | 1484 | 				" NOPOUT ping data.\n"); | 
 | 1485 | 			ret = -1; | 
 | 1486 | 			goto out; | 
 | 1487 | 		} | 
 | 1488 |  | 
 | 1489 | 		iov = &cmd->iov_misc[0]; | 
 | 1490 | 		iov[niov].iov_base	= ping_data; | 
 | 1491 | 		iov[niov++].iov_len	= payload_length; | 
 | 1492 |  | 
 | 1493 | 		padding = ((-payload_length) & 3); | 
 | 1494 | 		if (padding != 0) { | 
 | 1495 | 			pr_debug("Receiving %u additional bytes" | 
 | 1496 | 				" for padding.\n", padding); | 
 | 1497 | 			iov[niov].iov_base	= &cmd->pad_bytes; | 
 | 1498 | 			iov[niov++].iov_len	= padding; | 
 | 1499 | 			rx_size += padding; | 
 | 1500 | 		} | 
 | 1501 | 		if (conn->conn_ops->DataDigest) { | 
 | 1502 | 			iov[niov].iov_base	= &checksum; | 
 | 1503 | 			iov[niov++].iov_len	= ISCSI_CRC_LEN; | 
 | 1504 | 			rx_size += ISCSI_CRC_LEN; | 
 | 1505 | 		} | 
 | 1506 |  | 
 | 1507 | 		rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size); | 
 | 1508 | 		if (rx_got != rx_size) { | 
 | 1509 | 			ret = -1; | 
 | 1510 | 			goto out; | 
 | 1511 | 		} | 
 | 1512 |  | 
 | 1513 | 		if (conn->conn_ops->DataDigest) { | 
 | 1514 | 			iscsit_do_crypto_hash_buf(&conn->conn_rx_hash, | 
 | 1515 | 					ping_data, payload_length, | 
 | 1516 | 					padding, cmd->pad_bytes, | 
 | 1517 | 					(u8 *)&data_crc); | 
 | 1518 |  | 
 | 1519 | 			if (checksum != data_crc) { | 
 | 1520 | 				pr_err("Ping data CRC32C DataDigest" | 
 | 1521 | 				" 0x%08x does not match computed 0x%08x\n", | 
 | 1522 | 					checksum, data_crc); | 
 | 1523 | 				if (!conn->sess->sess_ops->ErrorRecoveryLevel) { | 
 | 1524 | 					pr_err("Unable to recover from" | 
 | 1525 | 					" NOPOUT Ping DataCRC failure while in" | 
 | 1526 | 						" ERL=0.\n"); | 
 | 1527 | 					ret = -1; | 
 | 1528 | 					goto out; | 
 | 1529 | 				} else { | 
 | 1530 | 					/* | 
 | 1531 | 					 * Silently drop this PDU and let the | 
 | 1532 | 					 * initiator plug the CmdSN gap. | 
 | 1533 | 					 */ | 
 | 1534 | 					pr_debug("Dropping NOPOUT" | 
 | 1535 | 					" Command CmdSN: 0x%08x due to" | 
 | 1536 | 					" DataCRC error.\n", hdr->cmdsn); | 
 | 1537 | 					ret = 0; | 
 | 1538 | 					goto out; | 
 | 1539 | 				} | 
 | 1540 | 			} else { | 
 | 1541 | 				pr_debug("Got CRC32C DataDigest" | 
 | 1542 | 				" 0x%08x for %u bytes of ping data.\n", | 
 | 1543 | 					checksum, payload_length); | 
 | 1544 | 			} | 
 | 1545 | 		} | 
 | 1546 |  | 
 | 1547 | 		ping_data[payload_length] = '\0'; | 
 | 1548 | 		/* | 
 | 1549 | 		 * Attach ping data to struct iscsi_cmd->buf_ptr. | 
 | 1550 | 		 */ | 
| Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 1551 | 		cmd->buf_ptr = ping_data; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1552 | 		cmd->buf_ptr_size = payload_length; | 
 | 1553 |  | 
 | 1554 | 		pr_debug("Got %u bytes of NOPOUT ping" | 
 | 1555 | 			" data.\n", payload_length); | 
 | 1556 | 		pr_debug("Ping Data: \"%s\"\n", ping_data); | 
 | 1557 | 	} | 
 | 1558 |  | 
 | 1559 | 	if (hdr->itt != 0xFFFFFFFF) { | 
 | 1560 | 		if (!cmd) { | 
 | 1561 | 			pr_err("Checking CmdSN for NOPOUT," | 
 | 1562 | 				" but cmd is NULL!\n"); | 
 | 1563 | 			return -1; | 
 | 1564 | 		} | 
 | 1565 | 		/* | 
 | 1566 | 		 * Initiator is expecting a NopIN ping reply, | 
 | 1567 | 		 */ | 
 | 1568 | 		spin_lock_bh(&conn->cmd_lock); | 
| Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 1569 | 		list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1570 | 		spin_unlock_bh(&conn->cmd_lock); | 
 | 1571 |  | 
 | 1572 | 		iscsit_ack_from_expstatsn(conn, hdr->exp_statsn); | 
 | 1573 |  | 
 | 1574 | 		if (hdr->opcode & ISCSI_OP_IMMEDIATE) { | 
 | 1575 | 			iscsit_add_cmd_to_response_queue(cmd, conn, | 
 | 1576 | 					cmd->i_state); | 
 | 1577 | 			return 0; | 
 | 1578 | 		} | 
 | 1579 |  | 
 | 1580 | 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn); | 
 | 1581 | 		if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) { | 
 | 1582 | 			ret = 0; | 
 | 1583 | 			goto ping_out; | 
 | 1584 | 		} | 
 | 1585 | 		if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) | 
 | 1586 | 			return iscsit_add_reject_from_cmd( | 
 | 1587 | 					ISCSI_REASON_PROTOCOL_ERROR, | 
 | 1588 | 					1, 0, buf, cmd); | 
 | 1589 |  | 
 | 1590 | 		return 0; | 
 | 1591 | 	} | 
 | 1592 |  | 
 | 1593 | 	if (hdr->ttt != 0xFFFFFFFF) { | 
 | 1594 | 		/* | 
 | 1595 | 		 * This was a response to a unsolicited NOPIN ping. | 
 | 1596 | 		 */ | 
 | 1597 | 		cmd = iscsit_find_cmd_from_ttt(conn, hdr->ttt); | 
 | 1598 | 		if (!cmd) | 
 | 1599 | 			return -1; | 
 | 1600 |  | 
 | 1601 | 		iscsit_stop_nopin_response_timer(conn); | 
 | 1602 |  | 
 | 1603 | 		cmd->i_state = ISTATE_REMOVE; | 
 | 1604 | 		iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state); | 
 | 1605 | 		iscsit_start_nopin_timer(conn); | 
 | 1606 | 	} else { | 
 | 1607 | 		/* | 
 | 1608 | 		 * Initiator is not expecting a NOPIN is response. | 
 | 1609 | 		 * Just ignore for now. | 
 | 1610 | 		 * | 
 | 1611 | 		 * iSCSI v19-91 10.18 | 
 | 1612 | 		 * "A NOP-OUT may also be used to confirm a changed | 
 | 1613 | 		 *  ExpStatSN if another PDU will not be available | 
 | 1614 | 		 *  for a long time." | 
 | 1615 | 		 */ | 
 | 1616 | 		ret = 0; | 
 | 1617 | 		goto out; | 
 | 1618 | 	} | 
 | 1619 |  | 
 | 1620 | 	return 0; | 
 | 1621 | out: | 
 | 1622 | 	if (cmd) | 
 | 1623 | 		iscsit_release_cmd(cmd); | 
 | 1624 | ping_out: | 
 | 1625 | 	kfree(ping_data); | 
 | 1626 | 	return ret; | 
 | 1627 | } | 
 | 1628 |  | 
 | 1629 | static int iscsit_handle_task_mgt_cmd( | 
 | 1630 | 	struct iscsi_conn *conn, | 
 | 1631 | 	unsigned char *buf) | 
 | 1632 | { | 
 | 1633 | 	struct iscsi_cmd *cmd; | 
 | 1634 | 	struct se_tmr_req *se_tmr; | 
 | 1635 | 	struct iscsi_tmr_req *tmr_req; | 
 | 1636 | 	struct iscsi_tm *hdr; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1637 | 	int out_of_order_cmdsn = 0; | 
 | 1638 | 	int ret; | 
 | 1639 | 	u8 function; | 
 | 1640 |  | 
 | 1641 | 	hdr			= (struct iscsi_tm *) buf; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1642 | 	hdr->itt		= be32_to_cpu(hdr->itt); | 
 | 1643 | 	hdr->rtt		= be32_to_cpu(hdr->rtt); | 
 | 1644 | 	hdr->cmdsn		= be32_to_cpu(hdr->cmdsn); | 
 | 1645 | 	hdr->exp_statsn		= be32_to_cpu(hdr->exp_statsn); | 
 | 1646 | 	hdr->refcmdsn		= be32_to_cpu(hdr->refcmdsn); | 
 | 1647 | 	hdr->exp_datasn		= be32_to_cpu(hdr->exp_datasn); | 
 | 1648 | 	hdr->flags &= ~ISCSI_FLAG_CMD_FINAL; | 
 | 1649 | 	function = hdr->flags; | 
 | 1650 |  | 
 | 1651 | 	pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:" | 
 | 1652 | 		" 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:" | 
 | 1653 | 		" 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function, | 
 | 1654 | 		hdr->rtt, hdr->refcmdsn, conn->cid); | 
 | 1655 |  | 
 | 1656 | 	if ((function != ISCSI_TM_FUNC_ABORT_TASK) && | 
 | 1657 | 	    ((function != ISCSI_TM_FUNC_TASK_REASSIGN) && | 
 | 1658 | 	     (hdr->rtt != ISCSI_RESERVED_TAG))) { | 
 | 1659 | 		pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n"); | 
 | 1660 | 		hdr->rtt = ISCSI_RESERVED_TAG; | 
 | 1661 | 	} | 
 | 1662 |  | 
 | 1663 | 	if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) && | 
 | 1664 | 			!(hdr->opcode & ISCSI_OP_IMMEDIATE)) { | 
 | 1665 | 		pr_err("Task Management Request TASK_REASSIGN not" | 
 | 1666 | 			" issued as immediate command, bad iSCSI Initiator" | 
 | 1667 | 				"implementation\n"); | 
 | 1668 | 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1, | 
 | 1669 | 					buf, conn); | 
 | 1670 | 	} | 
 | 1671 | 	if ((function != ISCSI_TM_FUNC_ABORT_TASK) && | 
 | 1672 | 	    (hdr->refcmdsn != ISCSI_RESERVED_TAG)) | 
 | 1673 | 		hdr->refcmdsn = ISCSI_RESERVED_TAG; | 
 | 1674 |  | 
| Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 1675 | 	cmd = iscsit_allocate_cmd(conn, GFP_KERNEL); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1676 | 	if (!cmd) | 
 | 1677 | 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, | 
| Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 1678 | 					 1, buf, conn); | 
 | 1679 |  | 
 | 1680 | 	cmd->data_direction = DMA_NONE; | 
 | 1681 |  | 
 | 1682 | 	cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL); | 
 | 1683 | 	if (!cmd->tmr_req) { | 
 | 1684 | 		pr_err("Unable to allocate memory for" | 
 | 1685 | 			" Task Management command!\n"); | 
 | 1686 | 		return iscsit_add_reject_from_cmd( | 
 | 1687 | 			ISCSI_REASON_BOOKMARK_NO_RESOURCES, | 
 | 1688 | 			1, 1, buf, cmd); | 
 | 1689 | 	} | 
 | 1690 |  | 
 | 1691 | 	/* | 
 | 1692 | 	 * TASK_REASSIGN for ERL=2 / connection stays inside of | 
 | 1693 | 	 * LIO-Target $FABRIC_MOD | 
 | 1694 | 	 */ | 
 | 1695 | 	if (function != ISCSI_TM_FUNC_TASK_REASSIGN) { | 
 | 1696 |  | 
 | 1697 | 		u8 tcm_function; | 
 | 1698 | 		int ret; | 
 | 1699 |  | 
 | 1700 | 		transport_init_se_cmd(&cmd->se_cmd, | 
 | 1701 | 				      &lio_target_fabric_configfs->tf_ops, | 
 | 1702 | 				      conn->sess->se_sess, 0, DMA_NONE, | 
| Roland Dreier | 9c58b7d | 2012-08-15 14:35:25 -0700 | [diff] [blame] | 1703 | 				      MSG_SIMPLE_TAG, cmd->sense_buffer + 2); | 
| Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 1704 |  | 
 | 1705 | 		switch (function) { | 
 | 1706 | 		case ISCSI_TM_FUNC_ABORT_TASK: | 
 | 1707 | 			tcm_function = TMR_ABORT_TASK; | 
 | 1708 | 			break; | 
 | 1709 | 		case ISCSI_TM_FUNC_ABORT_TASK_SET: | 
 | 1710 | 			tcm_function = TMR_ABORT_TASK_SET; | 
 | 1711 | 			break; | 
 | 1712 | 		case ISCSI_TM_FUNC_CLEAR_ACA: | 
 | 1713 | 			tcm_function = TMR_CLEAR_ACA; | 
 | 1714 | 			break; | 
 | 1715 | 		case ISCSI_TM_FUNC_CLEAR_TASK_SET: | 
 | 1716 | 			tcm_function = TMR_CLEAR_TASK_SET; | 
 | 1717 | 			break; | 
 | 1718 | 		case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET: | 
 | 1719 | 			tcm_function = TMR_LUN_RESET; | 
 | 1720 | 			break; | 
 | 1721 | 		case ISCSI_TM_FUNC_TARGET_WARM_RESET: | 
 | 1722 | 			tcm_function = TMR_TARGET_WARM_RESET; | 
 | 1723 | 			break; | 
 | 1724 | 		case ISCSI_TM_FUNC_TARGET_COLD_RESET: | 
 | 1725 | 			tcm_function = TMR_TARGET_COLD_RESET; | 
 | 1726 | 			break; | 
 | 1727 | 		default: | 
 | 1728 | 			pr_err("Unknown iSCSI TMR Function:" | 
 | 1729 | 			       " 0x%02x\n", function); | 
 | 1730 | 			return iscsit_add_reject_from_cmd( | 
 | 1731 | 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, | 
 | 1732 | 				1, 1, buf, cmd); | 
 | 1733 | 		} | 
 | 1734 |  | 
 | 1735 | 		ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, | 
 | 1736 | 					 tcm_function, GFP_KERNEL); | 
 | 1737 | 		if (ret < 0) | 
 | 1738 | 			return iscsit_add_reject_from_cmd( | 
 | 1739 | 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, | 
 | 1740 | 				1, 1, buf, cmd); | 
 | 1741 |  | 
 | 1742 | 		cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req; | 
 | 1743 | 	} | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1744 |  | 
 | 1745 | 	cmd->iscsi_opcode	= ISCSI_OP_SCSI_TMFUNC; | 
 | 1746 | 	cmd->i_state		= ISTATE_SEND_TASKMGTRSP; | 
 | 1747 | 	cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); | 
 | 1748 | 	cmd->init_task_tag	= hdr->itt; | 
 | 1749 | 	cmd->targ_xfer_tag	= 0xFFFFFFFF; | 
 | 1750 | 	cmd->cmd_sn		= hdr->cmdsn; | 
 | 1751 | 	cmd->exp_stat_sn	= hdr->exp_statsn; | 
 | 1752 | 	se_tmr			= cmd->se_cmd.se_tmr_req; | 
 | 1753 | 	tmr_req			= cmd->tmr_req; | 
 | 1754 | 	/* | 
 | 1755 | 	 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN | 
 | 1756 | 	 */ | 
 | 1757 | 	if (function != ISCSI_TM_FUNC_TASK_REASSIGN) { | 
| Andy Grover | 4f26998 | 2012-01-19 13:39:14 -0800 | [diff] [blame] | 1758 | 		ret = transport_lookup_tmr_lun(&cmd->se_cmd, | 
 | 1759 | 					       scsilun_to_int(&hdr->lun)); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1760 | 		if (ret < 0) { | 
 | 1761 | 			cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; | 
 | 1762 | 			se_tmr->response = ISCSI_TMF_RSP_NO_LUN; | 
 | 1763 | 			goto attach; | 
 | 1764 | 		} | 
 | 1765 | 	} | 
 | 1766 |  | 
 | 1767 | 	switch (function) { | 
 | 1768 | 	case ISCSI_TM_FUNC_ABORT_TASK: | 
 | 1769 | 		se_tmr->response = iscsit_tmr_abort_task(cmd, buf); | 
 | 1770 | 		if (se_tmr->response != ISCSI_TMF_RSP_COMPLETE) { | 
 | 1771 | 			cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; | 
 | 1772 | 			goto attach; | 
 | 1773 | 		} | 
 | 1774 | 		break; | 
 | 1775 | 	case ISCSI_TM_FUNC_ABORT_TASK_SET: | 
 | 1776 | 	case ISCSI_TM_FUNC_CLEAR_ACA: | 
 | 1777 | 	case ISCSI_TM_FUNC_CLEAR_TASK_SET: | 
 | 1778 | 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET: | 
 | 1779 | 		break; | 
 | 1780 | 	case ISCSI_TM_FUNC_TARGET_WARM_RESET: | 
 | 1781 | 		if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) { | 
 | 1782 | 			cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; | 
 | 1783 | 			se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED; | 
 | 1784 | 			goto attach; | 
 | 1785 | 		} | 
 | 1786 | 		break; | 
 | 1787 | 	case ISCSI_TM_FUNC_TARGET_COLD_RESET: | 
 | 1788 | 		if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) { | 
 | 1789 | 			cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; | 
 | 1790 | 			se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED; | 
 | 1791 | 			goto attach; | 
 | 1792 | 		} | 
 | 1793 | 		break; | 
 | 1794 | 	case ISCSI_TM_FUNC_TASK_REASSIGN: | 
 | 1795 | 		se_tmr->response = iscsit_tmr_task_reassign(cmd, buf); | 
 | 1796 | 		/* | 
 | 1797 | 		 * Perform sanity checks on the ExpDataSN only if the | 
 | 1798 | 		 * TASK_REASSIGN was successful. | 
 | 1799 | 		 */ | 
 | 1800 | 		if (se_tmr->response != ISCSI_TMF_RSP_COMPLETE) | 
 | 1801 | 			break; | 
 | 1802 |  | 
 | 1803 | 		if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0) | 
 | 1804 | 			return iscsit_add_reject_from_cmd( | 
 | 1805 | 					ISCSI_REASON_BOOKMARK_INVALID, 1, 1, | 
 | 1806 | 					buf, cmd); | 
 | 1807 | 		break; | 
 | 1808 | 	default: | 
 | 1809 | 		pr_err("Unknown TMR function: 0x%02x, protocol" | 
 | 1810 | 			" error.\n", function); | 
 | 1811 | 		cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; | 
 | 1812 | 		se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED; | 
 | 1813 | 		goto attach; | 
 | 1814 | 	} | 
 | 1815 |  | 
 | 1816 | 	if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) && | 
 | 1817 | 	    (se_tmr->response == ISCSI_TMF_RSP_COMPLETE)) | 
 | 1818 | 		se_tmr->call_transport = 1; | 
 | 1819 | attach: | 
 | 1820 | 	spin_lock_bh(&conn->cmd_lock); | 
| Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 1821 | 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1822 | 	spin_unlock_bh(&conn->cmd_lock); | 
 | 1823 |  | 
 | 1824 | 	if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) { | 
 | 1825 | 		int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn); | 
 | 1826 | 		if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP) | 
 | 1827 | 			out_of_order_cmdsn = 1; | 
| Nicholas Bellinger | 5a4c866 | 2011-10-28 13:37:19 -0700 | [diff] [blame] | 1828 | 		else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1829 | 			return 0; | 
| Nicholas Bellinger | 5a4c866 | 2011-10-28 13:37:19 -0700 | [diff] [blame] | 1830 | 		else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1831 | 			return iscsit_add_reject_from_cmd( | 
 | 1832 | 					ISCSI_REASON_PROTOCOL_ERROR, | 
 | 1833 | 					1, 0, buf, cmd); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1834 | 	} | 
 | 1835 | 	iscsit_ack_from_expstatsn(conn, hdr->exp_statsn); | 
 | 1836 |  | 
| Nicholas Bellinger | 5a4c866 | 2011-10-28 13:37:19 -0700 | [diff] [blame] | 1837 | 	if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE)) | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1838 | 		return 0; | 
 | 1839 | 	/* | 
 | 1840 | 	 * Found the referenced task, send to transport for processing. | 
 | 1841 | 	 */ | 
 | 1842 | 	if (se_tmr->call_transport) | 
 | 1843 | 		return transport_generic_handle_tmr(&cmd->se_cmd); | 
 | 1844 |  | 
 | 1845 | 	/* | 
 | 1846 | 	 * Could not find the referenced LUN, task, or Task Management | 
 | 1847 | 	 * command not authorized or supported.  Change state and | 
 | 1848 | 	 * let the tx_thread send the response. | 
 | 1849 | 	 * | 
 | 1850 | 	 * For connection recovery, this is also the default action for | 
 | 1851 | 	 * TMR TASK_REASSIGN. | 
 | 1852 | 	 */ | 
 | 1853 | 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); | 
 | 1854 | 	return 0; | 
 | 1855 | } | 
 | 1856 |  | 
 | 1857 | /* #warning FIXME: Support Text Command parameters besides SendTargets */ | 
 | 1858 | static int iscsit_handle_text_cmd( | 
 | 1859 | 	struct iscsi_conn *conn, | 
 | 1860 | 	unsigned char *buf) | 
 | 1861 | { | 
 | 1862 | 	char *text_ptr, *text_in; | 
 | 1863 | 	int cmdsn_ret, niov = 0, rx_got, rx_size; | 
 | 1864 | 	u32 checksum = 0, data_crc = 0, payload_length; | 
| Nicholas Bellinger | 76f1928 | 2011-07-27 12:16:22 -0700 | [diff] [blame] | 1865 | 	u32 padding = 0, pad_bytes = 0, text_length = 0; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1866 | 	struct iscsi_cmd *cmd; | 
 | 1867 | 	struct kvec iov[3]; | 
 | 1868 | 	struct iscsi_text *hdr; | 
 | 1869 |  | 
 | 1870 | 	hdr			= (struct iscsi_text *) buf; | 
 | 1871 | 	payload_length		= ntoh24(hdr->dlength); | 
 | 1872 | 	hdr->itt		= be32_to_cpu(hdr->itt); | 
 | 1873 | 	hdr->ttt		= be32_to_cpu(hdr->ttt); | 
 | 1874 | 	hdr->cmdsn		= be32_to_cpu(hdr->cmdsn); | 
 | 1875 | 	hdr->exp_statsn		= be32_to_cpu(hdr->exp_statsn); | 
 | 1876 |  | 
| Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 1877 | 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1878 | 		pr_err("Unable to accept text parameter length: %u" | 
| Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 1879 | 			"greater than MaxXmitDataSegmentLength %u.\n", | 
 | 1880 | 		       payload_length, conn->conn_ops->MaxXmitDataSegmentLength); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1881 | 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1, | 
 | 1882 | 					buf, conn); | 
 | 1883 | 	} | 
 | 1884 |  | 
 | 1885 | 	pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x," | 
 | 1886 | 		" ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn, | 
 | 1887 | 		hdr->exp_statsn, payload_length); | 
 | 1888 |  | 
 | 1889 | 	rx_size = text_length = payload_length; | 
 | 1890 | 	if (text_length) { | 
 | 1891 | 		text_in = kzalloc(text_length, GFP_KERNEL); | 
 | 1892 | 		if (!text_in) { | 
 | 1893 | 			pr_err("Unable to allocate memory for" | 
 | 1894 | 				" incoming text parameters\n"); | 
 | 1895 | 			return -1; | 
 | 1896 | 		} | 
 | 1897 |  | 
 | 1898 | 		memset(iov, 0, 3 * sizeof(struct kvec)); | 
 | 1899 | 		iov[niov].iov_base	= text_in; | 
 | 1900 | 		iov[niov++].iov_len	= text_length; | 
 | 1901 |  | 
 | 1902 | 		padding = ((-payload_length) & 3); | 
 | 1903 | 		if (padding != 0) { | 
| Nicholas Bellinger | 76f1928 | 2011-07-27 12:16:22 -0700 | [diff] [blame] | 1904 | 			iov[niov].iov_base = &pad_bytes; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1905 | 			iov[niov++].iov_len  = padding; | 
 | 1906 | 			rx_size += padding; | 
 | 1907 | 			pr_debug("Receiving %u additional bytes" | 
 | 1908 | 					" for padding.\n", padding); | 
 | 1909 | 		} | 
 | 1910 | 		if (conn->conn_ops->DataDigest) { | 
 | 1911 | 			iov[niov].iov_base	= &checksum; | 
 | 1912 | 			iov[niov++].iov_len	= ISCSI_CRC_LEN; | 
 | 1913 | 			rx_size += ISCSI_CRC_LEN; | 
 | 1914 | 		} | 
 | 1915 |  | 
 | 1916 | 		rx_got = rx_data(conn, &iov[0], niov, rx_size); | 
 | 1917 | 		if (rx_got != rx_size) { | 
 | 1918 | 			kfree(text_in); | 
 | 1919 | 			return -1; | 
 | 1920 | 		} | 
 | 1921 |  | 
 | 1922 | 		if (conn->conn_ops->DataDigest) { | 
 | 1923 | 			iscsit_do_crypto_hash_buf(&conn->conn_rx_hash, | 
 | 1924 | 					text_in, text_length, | 
| Nicholas Bellinger | 76f1928 | 2011-07-27 12:16:22 -0700 | [diff] [blame] | 1925 | 					padding, (u8 *)&pad_bytes, | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1926 | 					(u8 *)&data_crc); | 
 | 1927 |  | 
 | 1928 | 			if (checksum != data_crc) { | 
 | 1929 | 				pr_err("Text data CRC32C DataDigest" | 
 | 1930 | 					" 0x%08x does not match computed" | 
 | 1931 | 					" 0x%08x\n", checksum, data_crc); | 
 | 1932 | 				if (!conn->sess->sess_ops->ErrorRecoveryLevel) { | 
 | 1933 | 					pr_err("Unable to recover from" | 
 | 1934 | 					" Text Data digest failure while in" | 
 | 1935 | 						" ERL=0.\n"); | 
 | 1936 | 					kfree(text_in); | 
 | 1937 | 					return -1; | 
 | 1938 | 				} else { | 
 | 1939 | 					/* | 
 | 1940 | 					 * Silently drop this PDU and let the | 
 | 1941 | 					 * initiator plug the CmdSN gap. | 
 | 1942 | 					 */ | 
 | 1943 | 					pr_debug("Dropping Text" | 
 | 1944 | 					" Command CmdSN: 0x%08x due to" | 
 | 1945 | 					" DataCRC error.\n", hdr->cmdsn); | 
 | 1946 | 					kfree(text_in); | 
 | 1947 | 					return 0; | 
 | 1948 | 				} | 
 | 1949 | 			} else { | 
 | 1950 | 				pr_debug("Got CRC32C DataDigest" | 
 | 1951 | 					" 0x%08x for %u bytes of text data.\n", | 
 | 1952 | 						checksum, text_length); | 
 | 1953 | 			} | 
 | 1954 | 		} | 
 | 1955 | 		text_in[text_length - 1] = '\0'; | 
 | 1956 | 		pr_debug("Successfully read %d bytes of text" | 
 | 1957 | 				" data.\n", text_length); | 
 | 1958 |  | 
 | 1959 | 		if (strncmp("SendTargets", text_in, 11) != 0) { | 
 | 1960 | 			pr_err("Received Text Data that is not" | 
 | 1961 | 				" SendTargets, cannot continue.\n"); | 
 | 1962 | 			kfree(text_in); | 
 | 1963 | 			return -1; | 
 | 1964 | 		} | 
 | 1965 | 		text_ptr = strchr(text_in, '='); | 
 | 1966 | 		if (!text_ptr) { | 
 | 1967 | 			pr_err("No \"=\" separator found in Text Data," | 
 | 1968 | 				"  cannot continue.\n"); | 
 | 1969 | 			kfree(text_in); | 
 | 1970 | 			return -1; | 
 | 1971 | 		} | 
 | 1972 | 		if (strncmp("=All", text_ptr, 4) != 0) { | 
 | 1973 | 			pr_err("Unable to locate All value for" | 
 | 1974 | 				" SendTargets key,  cannot continue.\n"); | 
 | 1975 | 			kfree(text_in); | 
 | 1976 | 			return -1; | 
 | 1977 | 		} | 
 | 1978 | /*#warning Support SendTargets=(iSCSI Target Name/Nothing) values. */ | 
 | 1979 | 		kfree(text_in); | 
 | 1980 | 	} | 
 | 1981 |  | 
 | 1982 | 	cmd = iscsit_allocate_cmd(conn, GFP_KERNEL); | 
 | 1983 | 	if (!cmd) | 
 | 1984 | 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, | 
 | 1985 | 					1, buf, conn); | 
 | 1986 |  | 
 | 1987 | 	cmd->iscsi_opcode	= ISCSI_OP_TEXT; | 
 | 1988 | 	cmd->i_state		= ISTATE_SEND_TEXTRSP; | 
 | 1989 | 	cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); | 
 | 1990 | 	conn->sess->init_task_tag = cmd->init_task_tag	= hdr->itt; | 
 | 1991 | 	cmd->targ_xfer_tag	= 0xFFFFFFFF; | 
 | 1992 | 	cmd->cmd_sn		= hdr->cmdsn; | 
 | 1993 | 	cmd->exp_stat_sn	= hdr->exp_statsn; | 
 | 1994 | 	cmd->data_direction	= DMA_NONE; | 
 | 1995 |  | 
 | 1996 | 	spin_lock_bh(&conn->cmd_lock); | 
| Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 1997 | 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1998 | 	spin_unlock_bh(&conn->cmd_lock); | 
 | 1999 |  | 
 | 2000 | 	iscsit_ack_from_expstatsn(conn, hdr->exp_statsn); | 
 | 2001 |  | 
 | 2002 | 	if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) { | 
 | 2003 | 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn); | 
 | 2004 | 		if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) | 
 | 2005 | 			return iscsit_add_reject_from_cmd( | 
 | 2006 | 					ISCSI_REASON_PROTOCOL_ERROR, | 
 | 2007 | 					1, 0, buf, cmd); | 
 | 2008 |  | 
 | 2009 | 		return 0; | 
 | 2010 | 	} | 
 | 2011 |  | 
 | 2012 | 	return iscsit_execute_cmd(cmd, 0); | 
 | 2013 | } | 
 | 2014 |  | 
 | 2015 | int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn) | 
 | 2016 | { | 
 | 2017 | 	struct iscsi_conn *conn_p; | 
 | 2018 | 	struct iscsi_session *sess = conn->sess; | 
 | 2019 |  | 
 | 2020 | 	pr_debug("Received logout request CLOSESESSION on CID: %hu" | 
 | 2021 | 		" for SID: %u.\n", conn->cid, conn->sess->sid); | 
 | 2022 |  | 
 | 2023 | 	atomic_set(&sess->session_logout, 1); | 
 | 2024 | 	atomic_set(&conn->conn_logout_remove, 1); | 
 | 2025 | 	conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION; | 
 | 2026 |  | 
 | 2027 | 	iscsit_inc_conn_usage_count(conn); | 
 | 2028 | 	iscsit_inc_session_usage_count(sess); | 
 | 2029 |  | 
 | 2030 | 	spin_lock_bh(&sess->conn_lock); | 
 | 2031 | 	list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) { | 
 | 2032 | 		if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN) | 
 | 2033 | 			continue; | 
 | 2034 |  | 
 | 2035 | 		pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n"); | 
 | 2036 | 		conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT; | 
 | 2037 | 	} | 
 | 2038 | 	spin_unlock_bh(&sess->conn_lock); | 
 | 2039 |  | 
 | 2040 | 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); | 
 | 2041 |  | 
 | 2042 | 	return 0; | 
 | 2043 | } | 
 | 2044 |  | 
 | 2045 | int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn) | 
 | 2046 | { | 
 | 2047 | 	struct iscsi_conn *l_conn; | 
 | 2048 | 	struct iscsi_session *sess = conn->sess; | 
 | 2049 |  | 
 | 2050 | 	pr_debug("Received logout request CLOSECONNECTION for CID:" | 
 | 2051 | 		" %hu on CID: %hu.\n", cmd->logout_cid, conn->cid); | 
 | 2052 |  | 
 | 2053 | 	/* | 
 | 2054 | 	 * A Logout Request with a CLOSECONNECTION reason code for a CID | 
 | 2055 | 	 * can arrive on a connection with a differing CID. | 
 | 2056 | 	 */ | 
 | 2057 | 	if (conn->cid == cmd->logout_cid) { | 
 | 2058 | 		spin_lock_bh(&conn->state_lock); | 
 | 2059 | 		pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n"); | 
 | 2060 | 		conn->conn_state = TARG_CONN_STATE_IN_LOGOUT; | 
 | 2061 |  | 
 | 2062 | 		atomic_set(&conn->conn_logout_remove, 1); | 
 | 2063 | 		conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION; | 
 | 2064 | 		iscsit_inc_conn_usage_count(conn); | 
 | 2065 |  | 
 | 2066 | 		spin_unlock_bh(&conn->state_lock); | 
 | 2067 | 	} else { | 
 | 2068 | 		/* | 
 | 2069 | 		 * Handle all different cid CLOSECONNECTION requests in | 
 | 2070 | 		 * iscsit_logout_post_handler_diffcid() as to give enough | 
 | 2071 | 		 * time for any non immediate command's CmdSN to be | 
 | 2072 | 		 * acknowledged on the connection in question. | 
 | 2073 | 		 * | 
 | 2074 | 		 * Here we simply make sure the CID is still around. | 
 | 2075 | 		 */ | 
 | 2076 | 		l_conn = iscsit_get_conn_from_cid(sess, | 
 | 2077 | 				cmd->logout_cid); | 
 | 2078 | 		if (!l_conn) { | 
 | 2079 | 			cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND; | 
 | 2080 | 			iscsit_add_cmd_to_response_queue(cmd, conn, | 
 | 2081 | 					cmd->i_state); | 
 | 2082 | 			return 0; | 
 | 2083 | 		} | 
 | 2084 |  | 
 | 2085 | 		iscsit_dec_conn_usage_count(l_conn); | 
 | 2086 | 	} | 
 | 2087 |  | 
 | 2088 | 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); | 
 | 2089 |  | 
 | 2090 | 	return 0; | 
 | 2091 | } | 
 | 2092 |  | 
 | 2093 | int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn) | 
 | 2094 | { | 
 | 2095 | 	struct iscsi_session *sess = conn->sess; | 
 | 2096 |  | 
 | 2097 | 	pr_debug("Received explicit REMOVECONNFORRECOVERY logout for" | 
 | 2098 | 		" CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid); | 
 | 2099 |  | 
 | 2100 | 	if (sess->sess_ops->ErrorRecoveryLevel != 2) { | 
 | 2101 | 		pr_err("Received Logout Request REMOVECONNFORRECOVERY" | 
 | 2102 | 			" while ERL!=2.\n"); | 
 | 2103 | 		cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED; | 
 | 2104 | 		iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); | 
 | 2105 | 		return 0; | 
 | 2106 | 	} | 
 | 2107 |  | 
 | 2108 | 	if (conn->cid == cmd->logout_cid) { | 
 | 2109 | 		pr_err("Received Logout Request REMOVECONNFORRECOVERY" | 
 | 2110 | 			" with CID: %hu on CID: %hu, implementation error.\n", | 
 | 2111 | 				cmd->logout_cid, conn->cid); | 
 | 2112 | 		cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED; | 
 | 2113 | 		iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); | 
 | 2114 | 		return 0; | 
 | 2115 | 	} | 
 | 2116 |  | 
 | 2117 | 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); | 
 | 2118 |  | 
 | 2119 | 	return 0; | 
 | 2120 | } | 
 | 2121 |  | 
 | 2122 | static int iscsit_handle_logout_cmd( | 
 | 2123 | 	struct iscsi_conn *conn, | 
 | 2124 | 	unsigned char *buf) | 
 | 2125 | { | 
 | 2126 | 	int cmdsn_ret, logout_remove = 0; | 
 | 2127 | 	u8 reason_code = 0; | 
 | 2128 | 	struct iscsi_cmd *cmd; | 
 | 2129 | 	struct iscsi_logout *hdr; | 
 | 2130 | 	struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn); | 
 | 2131 |  | 
 | 2132 | 	hdr			= (struct iscsi_logout *) buf; | 
 | 2133 | 	reason_code		= (hdr->flags & 0x7f); | 
 | 2134 | 	hdr->itt		= be32_to_cpu(hdr->itt); | 
 | 2135 | 	hdr->cid		= be16_to_cpu(hdr->cid); | 
 | 2136 | 	hdr->cmdsn		= be32_to_cpu(hdr->cmdsn); | 
 | 2137 | 	hdr->exp_statsn	= be32_to_cpu(hdr->exp_statsn); | 
 | 2138 |  | 
 | 2139 | 	if (tiqn) { | 
 | 2140 | 		spin_lock(&tiqn->logout_stats.lock); | 
 | 2141 | 		if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) | 
 | 2142 | 			tiqn->logout_stats.normal_logouts++; | 
 | 2143 | 		else | 
 | 2144 | 			tiqn->logout_stats.abnormal_logouts++; | 
 | 2145 | 		spin_unlock(&tiqn->logout_stats.lock); | 
 | 2146 | 	} | 
 | 2147 |  | 
 | 2148 | 	pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x" | 
 | 2149 | 		" ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n", | 
 | 2150 | 		hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code, | 
 | 2151 | 		hdr->cid, conn->cid); | 
 | 2152 |  | 
 | 2153 | 	if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) { | 
 | 2154 | 		pr_err("Received logout request on connection that" | 
 | 2155 | 			" is not in logged in state, ignoring request.\n"); | 
 | 2156 | 		return 0; | 
 | 2157 | 	} | 
 | 2158 |  | 
 | 2159 | 	cmd = iscsit_allocate_cmd(conn, GFP_KERNEL); | 
 | 2160 | 	if (!cmd) | 
 | 2161 | 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1, | 
 | 2162 | 					buf, conn); | 
 | 2163 |  | 
 | 2164 | 	cmd->iscsi_opcode       = ISCSI_OP_LOGOUT; | 
 | 2165 | 	cmd->i_state            = ISTATE_SEND_LOGOUTRSP; | 
 | 2166 | 	cmd->immediate_cmd      = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); | 
 | 2167 | 	conn->sess->init_task_tag = cmd->init_task_tag  = hdr->itt; | 
 | 2168 | 	cmd->targ_xfer_tag      = 0xFFFFFFFF; | 
 | 2169 | 	cmd->cmd_sn             = hdr->cmdsn; | 
 | 2170 | 	cmd->exp_stat_sn        = hdr->exp_statsn; | 
 | 2171 | 	cmd->logout_cid         = hdr->cid; | 
 | 2172 | 	cmd->logout_reason      = reason_code; | 
 | 2173 | 	cmd->data_direction     = DMA_NONE; | 
 | 2174 |  | 
 | 2175 | 	/* | 
 | 2176 | 	 * We need to sleep in these cases (by returning 1) until the Logout | 
 | 2177 | 	 * Response gets sent in the tx thread. | 
 | 2178 | 	 */ | 
 | 2179 | 	if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) || | 
 | 2180 | 	   ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) && | 
 | 2181 | 	    (hdr->cid == conn->cid))) | 
 | 2182 | 		logout_remove = 1; | 
 | 2183 |  | 
 | 2184 | 	spin_lock_bh(&conn->cmd_lock); | 
| Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 2185 | 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2186 | 	spin_unlock_bh(&conn->cmd_lock); | 
 | 2187 |  | 
 | 2188 | 	if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY) | 
 | 2189 | 		iscsit_ack_from_expstatsn(conn, hdr->exp_statsn); | 
 | 2190 |  | 
 | 2191 | 	/* | 
 | 2192 | 	 * Immediate commands are executed, well, immediately. | 
 | 2193 | 	 * Non-Immediate Logout Commands are executed in CmdSN order. | 
 | 2194 | 	 */ | 
| Andy Grover | c6037cc | 2012-04-03 15:51:02 -0700 | [diff] [blame] | 2195 | 	if (cmd->immediate_cmd) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2196 | 		int ret = iscsit_execute_cmd(cmd, 0); | 
 | 2197 |  | 
 | 2198 | 		if (ret < 0) | 
 | 2199 | 			return ret; | 
 | 2200 | 	} else { | 
 | 2201 | 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn); | 
 | 2202 | 		if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) { | 
 | 2203 | 			logout_remove = 0; | 
 | 2204 | 		} else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) { | 
 | 2205 | 			return iscsit_add_reject_from_cmd( | 
 | 2206 | 				ISCSI_REASON_PROTOCOL_ERROR, | 
 | 2207 | 				1, 0, buf, cmd); | 
 | 2208 | 		} | 
 | 2209 | 	} | 
 | 2210 |  | 
 | 2211 | 	return logout_remove; | 
 | 2212 | } | 
 | 2213 |  | 
 | 2214 | static int iscsit_handle_snack( | 
 | 2215 | 	struct iscsi_conn *conn, | 
 | 2216 | 	unsigned char *buf) | 
 | 2217 | { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2218 | 	struct iscsi_snack *hdr; | 
 | 2219 |  | 
 | 2220 | 	hdr			= (struct iscsi_snack *) buf; | 
 | 2221 | 	hdr->flags		&= ~ISCSI_FLAG_CMD_FINAL; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2222 | 	hdr->itt		= be32_to_cpu(hdr->itt); | 
 | 2223 | 	hdr->ttt		= be32_to_cpu(hdr->ttt); | 
 | 2224 | 	hdr->exp_statsn		= be32_to_cpu(hdr->exp_statsn); | 
 | 2225 | 	hdr->begrun		= be32_to_cpu(hdr->begrun); | 
 | 2226 | 	hdr->runlength		= be32_to_cpu(hdr->runlength); | 
 | 2227 |  | 
 | 2228 | 	pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:" | 
 | 2229 | 		" 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x," | 
 | 2230 | 		" CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags, | 
 | 2231 | 			hdr->begrun, hdr->runlength, conn->cid); | 
 | 2232 |  | 
 | 2233 | 	if (!conn->sess->sess_ops->ErrorRecoveryLevel) { | 
 | 2234 | 		pr_err("Initiator sent SNACK request while in" | 
 | 2235 | 			" ErrorRecoveryLevel=0.\n"); | 
 | 2236 | 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1, | 
 | 2237 | 					buf, conn); | 
 | 2238 | 	} | 
 | 2239 | 	/* | 
 | 2240 | 	 * SNACK_DATA and SNACK_R2T are both 0,  so check which function to | 
 | 2241 | 	 * call from inside iscsi_send_recovery_datain_or_r2t(). | 
 | 2242 | 	 */ | 
 | 2243 | 	switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) { | 
 | 2244 | 	case 0: | 
 | 2245 | 		return iscsit_handle_recovery_datain_or_r2t(conn, buf, | 
 | 2246 | 			hdr->itt, hdr->ttt, hdr->begrun, hdr->runlength); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2247 | 	case ISCSI_FLAG_SNACK_TYPE_STATUS: | 
 | 2248 | 		return iscsit_handle_status_snack(conn, hdr->itt, hdr->ttt, | 
 | 2249 | 			hdr->begrun, hdr->runlength); | 
 | 2250 | 	case ISCSI_FLAG_SNACK_TYPE_DATA_ACK: | 
 | 2251 | 		return iscsit_handle_data_ack(conn, hdr->ttt, hdr->begrun, | 
 | 2252 | 			hdr->runlength); | 
 | 2253 | 	case ISCSI_FLAG_SNACK_TYPE_RDATA: | 
 | 2254 | 		/* FIXME: Support R-Data SNACK */ | 
 | 2255 | 		pr_err("R-Data SNACK Not Supported.\n"); | 
 | 2256 | 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1, | 
 | 2257 | 					buf, conn); | 
 | 2258 | 	default: | 
 | 2259 | 		pr_err("Unknown SNACK type 0x%02x, protocol" | 
 | 2260 | 			" error.\n", hdr->flags & 0x0f); | 
 | 2261 | 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1, | 
 | 2262 | 					buf, conn); | 
 | 2263 | 	} | 
 | 2264 |  | 
 | 2265 | 	return 0; | 
 | 2266 | } | 
 | 2267 |  | 
 | 2268 | static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn) | 
 | 2269 | { | 
 | 2270 | 	if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) || | 
 | 2271 | 	    (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) { | 
 | 2272 | 		wait_for_completion_interruptible_timeout( | 
 | 2273 | 					&conn->rx_half_close_comp, | 
 | 2274 | 					ISCSI_RX_THREAD_TCP_TIMEOUT * HZ); | 
 | 2275 | 	} | 
 | 2276 | } | 
 | 2277 |  | 
 | 2278 | static int iscsit_handle_immediate_data( | 
 | 2279 | 	struct iscsi_cmd *cmd, | 
 | 2280 | 	unsigned char *buf, | 
 | 2281 | 	u32 length) | 
 | 2282 | { | 
 | 2283 | 	int iov_ret, rx_got = 0, rx_size = 0; | 
 | 2284 | 	u32 checksum, iov_count = 0, padding = 0; | 
 | 2285 | 	struct iscsi_conn *conn = cmd->conn; | 
 | 2286 | 	struct kvec *iov; | 
 | 2287 |  | 
 | 2288 | 	iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length); | 
 | 2289 | 	if (iov_ret < 0) | 
 | 2290 | 		return IMMEDIATE_DATA_CANNOT_RECOVER; | 
 | 2291 |  | 
 | 2292 | 	rx_size = length; | 
 | 2293 | 	iov_count = iov_ret; | 
 | 2294 | 	iov = &cmd->iov_data[0]; | 
 | 2295 |  | 
 | 2296 | 	padding = ((-length) & 3); | 
 | 2297 | 	if (padding != 0) { | 
 | 2298 | 		iov[iov_count].iov_base	= cmd->pad_bytes; | 
 | 2299 | 		iov[iov_count++].iov_len = padding; | 
 | 2300 | 		rx_size += padding; | 
 | 2301 | 	} | 
 | 2302 |  | 
 | 2303 | 	if (conn->conn_ops->DataDigest) { | 
 | 2304 | 		iov[iov_count].iov_base		= &checksum; | 
 | 2305 | 		iov[iov_count++].iov_len	= ISCSI_CRC_LEN; | 
 | 2306 | 		rx_size += ISCSI_CRC_LEN; | 
 | 2307 | 	} | 
 | 2308 |  | 
 | 2309 | 	rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size); | 
 | 2310 |  | 
 | 2311 | 	iscsit_unmap_iovec(cmd); | 
 | 2312 |  | 
 | 2313 | 	if (rx_got != rx_size) { | 
 | 2314 | 		iscsit_rx_thread_wait_for_tcp(conn); | 
 | 2315 | 		return IMMEDIATE_DATA_CANNOT_RECOVER; | 
 | 2316 | 	} | 
 | 2317 |  | 
 | 2318 | 	if (conn->conn_ops->DataDigest) { | 
 | 2319 | 		u32 data_crc; | 
 | 2320 |  | 
 | 2321 | 		data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd, | 
 | 2322 | 						    cmd->write_data_done, length, padding, | 
 | 2323 | 						    cmd->pad_bytes); | 
 | 2324 |  | 
 | 2325 | 		if (checksum != data_crc) { | 
 | 2326 | 			pr_err("ImmediateData CRC32C DataDigest 0x%08x" | 
 | 2327 | 				" does not match computed 0x%08x\n", checksum, | 
 | 2328 | 				data_crc); | 
 | 2329 |  | 
 | 2330 | 			if (!conn->sess->sess_ops->ErrorRecoveryLevel) { | 
 | 2331 | 				pr_err("Unable to recover from" | 
 | 2332 | 					" Immediate Data digest failure while" | 
 | 2333 | 					" in ERL=0.\n"); | 
 | 2334 | 				iscsit_add_reject_from_cmd( | 
 | 2335 | 						ISCSI_REASON_DATA_DIGEST_ERROR, | 
 | 2336 | 						1, 0, buf, cmd); | 
 | 2337 | 				return IMMEDIATE_DATA_CANNOT_RECOVER; | 
 | 2338 | 			} else { | 
 | 2339 | 				iscsit_add_reject_from_cmd( | 
 | 2340 | 						ISCSI_REASON_DATA_DIGEST_ERROR, | 
 | 2341 | 						0, 0, buf, cmd); | 
 | 2342 | 				return IMMEDIATE_DATA_ERL1_CRC_FAILURE; | 
 | 2343 | 			} | 
 | 2344 | 		} else { | 
 | 2345 | 			pr_debug("Got CRC32C DataDigest 0x%08x for" | 
 | 2346 | 				" %u bytes of Immediate Data\n", checksum, | 
 | 2347 | 				length); | 
 | 2348 | 		} | 
 | 2349 | 	} | 
 | 2350 |  | 
 | 2351 | 	cmd->write_data_done += length; | 
 | 2352 |  | 
| Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 2353 | 	if (cmd->write_data_done == cmd->se_cmd.data_length) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2354 | 		spin_lock_bh(&cmd->istate_lock); | 
 | 2355 | 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT; | 
 | 2356 | 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT; | 
 | 2357 | 		spin_unlock_bh(&cmd->istate_lock); | 
 | 2358 | 	} | 
 | 2359 |  | 
 | 2360 | 	return IMMEDIATE_DATA_NORMAL_OPERATION; | 
 | 2361 | } | 
 | 2362 |  | 
 | 2363 | /* | 
 | 2364 |  *	Called with sess->conn_lock held. | 
 | 2365 |  */ | 
 | 2366 | /* #warning iscsi_build_conn_drop_async_message() only sends out on connections | 
 | 2367 | 	with active network interface */ | 
 | 2368 | static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn) | 
 | 2369 | { | 
 | 2370 | 	struct iscsi_cmd *cmd; | 
 | 2371 | 	struct iscsi_conn *conn_p; | 
 | 2372 |  | 
 | 2373 | 	/* | 
 | 2374 | 	 * Only send a Asynchronous Message on connections whos network | 
 | 2375 | 	 * interface is still functional. | 
 | 2376 | 	 */ | 
 | 2377 | 	list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) { | 
 | 2378 | 		if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) { | 
 | 2379 | 			iscsit_inc_conn_usage_count(conn_p); | 
 | 2380 | 			break; | 
 | 2381 | 		} | 
 | 2382 | 	} | 
 | 2383 |  | 
 | 2384 | 	if (!conn_p) | 
 | 2385 | 		return; | 
 | 2386 |  | 
 | 2387 | 	cmd = iscsit_allocate_cmd(conn_p, GFP_KERNEL); | 
 | 2388 | 	if (!cmd) { | 
 | 2389 | 		iscsit_dec_conn_usage_count(conn_p); | 
 | 2390 | 		return; | 
 | 2391 | 	} | 
 | 2392 |  | 
 | 2393 | 	cmd->logout_cid = conn->cid; | 
 | 2394 | 	cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT; | 
 | 2395 | 	cmd->i_state = ISTATE_SEND_ASYNCMSG; | 
 | 2396 |  | 
 | 2397 | 	spin_lock_bh(&conn_p->cmd_lock); | 
| Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 2398 | 	list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2399 | 	spin_unlock_bh(&conn_p->cmd_lock); | 
 | 2400 |  | 
 | 2401 | 	iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state); | 
 | 2402 | 	iscsit_dec_conn_usage_count(conn_p); | 
 | 2403 | } | 
 | 2404 |  | 
 | 2405 | static int iscsit_send_conn_drop_async_message( | 
 | 2406 | 	struct iscsi_cmd *cmd, | 
 | 2407 | 	struct iscsi_conn *conn) | 
 | 2408 | { | 
 | 2409 | 	struct iscsi_async *hdr; | 
 | 2410 |  | 
 | 2411 | 	cmd->tx_size = ISCSI_HDR_LEN; | 
 | 2412 | 	cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT; | 
 | 2413 |  | 
 | 2414 | 	hdr			= (struct iscsi_async *) cmd->pdu; | 
 | 2415 | 	hdr->opcode		= ISCSI_OP_ASYNC_EVENT; | 
 | 2416 | 	hdr->flags		= ISCSI_FLAG_CMD_FINAL; | 
 | 2417 | 	cmd->init_task_tag	= 0xFFFFFFFF; | 
 | 2418 | 	cmd->targ_xfer_tag	= 0xFFFFFFFF; | 
 | 2419 | 	put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]); | 
 | 2420 | 	cmd->stat_sn		= conn->stat_sn++; | 
 | 2421 | 	hdr->statsn		= cpu_to_be32(cmd->stat_sn); | 
 | 2422 | 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn); | 
 | 2423 | 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn); | 
 | 2424 | 	hdr->async_event	= ISCSI_ASYNC_MSG_DROPPING_CONNECTION; | 
 | 2425 | 	hdr->param1		= cpu_to_be16(cmd->logout_cid); | 
 | 2426 | 	hdr->param2		= cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait); | 
 | 2427 | 	hdr->param3		= cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain); | 
 | 2428 |  | 
 | 2429 | 	if (conn->conn_ops->HeaderDigest) { | 
 | 2430 | 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; | 
 | 2431 |  | 
 | 2432 | 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, | 
 | 2433 | 				(unsigned char *)hdr, ISCSI_HDR_LEN, | 
 | 2434 | 				0, NULL, (u8 *)header_digest); | 
 | 2435 |  | 
 | 2436 | 		cmd->tx_size += ISCSI_CRC_LEN; | 
 | 2437 | 		pr_debug("Attaching CRC32C HeaderDigest to" | 
 | 2438 | 			" Async Message 0x%08x\n", *header_digest); | 
 | 2439 | 	} | 
 | 2440 |  | 
 | 2441 | 	cmd->iov_misc[0].iov_base	= cmd->pdu; | 
 | 2442 | 	cmd->iov_misc[0].iov_len	= cmd->tx_size; | 
 | 2443 | 	cmd->iov_misc_count		= 1; | 
 | 2444 |  | 
 | 2445 | 	pr_debug("Sending Connection Dropped Async Message StatSN:" | 
 | 2446 | 		" 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn, | 
 | 2447 | 			cmd->logout_cid, conn->cid); | 
 | 2448 | 	return 0; | 
 | 2449 | } | 
 | 2450 |  | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2451 | static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn) | 
 | 2452 | { | 
 | 2453 | 	if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) || | 
 | 2454 | 	    (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) { | 
 | 2455 | 		wait_for_completion_interruptible_timeout( | 
 | 2456 | 					&conn->tx_half_close_comp, | 
 | 2457 | 					ISCSI_TX_THREAD_TCP_TIMEOUT * HZ); | 
 | 2458 | 	} | 
 | 2459 | } | 
 | 2460 |  | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2461 | static int iscsit_send_data_in( | 
 | 2462 | 	struct iscsi_cmd *cmd, | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2463 | 	struct iscsi_conn *conn) | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2464 | { | 
 | 2465 | 	int iov_ret = 0, set_statsn = 0; | 
 | 2466 | 	u32 iov_count = 0, tx_size = 0; | 
 | 2467 | 	struct iscsi_datain datain; | 
 | 2468 | 	struct iscsi_datain_req *dr; | 
 | 2469 | 	struct iscsi_data_rsp *hdr; | 
 | 2470 | 	struct kvec *iov; | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2471 | 	int eodr = 0; | 
 | 2472 | 	int ret; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2473 |  | 
 | 2474 | 	memset(&datain, 0, sizeof(struct iscsi_datain)); | 
 | 2475 | 	dr = iscsit_get_datain_values(cmd, &datain); | 
 | 2476 | 	if (!dr) { | 
 | 2477 | 		pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n", | 
 | 2478 | 				cmd->init_task_tag); | 
 | 2479 | 		return -1; | 
 | 2480 | 	} | 
 | 2481 |  | 
 | 2482 | 	/* | 
 | 2483 | 	 * Be paranoid and double check the logic for now. | 
 | 2484 | 	 */ | 
| Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 2485 | 	if ((datain.offset + datain.length) > cmd->se_cmd.data_length) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2486 | 		pr_err("Command ITT: 0x%08x, datain.offset: %u and" | 
 | 2487 | 			" datain.length: %u exceeds cmd->data_length: %u\n", | 
 | 2488 | 			cmd->init_task_tag, datain.offset, datain.length, | 
| Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 2489 | 				cmd->se_cmd.data_length); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2490 | 		return -1; | 
 | 2491 | 	} | 
 | 2492 |  | 
 | 2493 | 	spin_lock_bh(&conn->sess->session_stats_lock); | 
 | 2494 | 	conn->sess->tx_data_octets += datain.length; | 
 | 2495 | 	if (conn->sess->se_sess->se_node_acl) { | 
 | 2496 | 		spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock); | 
 | 2497 | 		conn->sess->se_sess->se_node_acl->read_bytes += datain.length; | 
 | 2498 | 		spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock); | 
 | 2499 | 	} | 
 | 2500 | 	spin_unlock_bh(&conn->sess->session_stats_lock); | 
 | 2501 | 	/* | 
 | 2502 | 	 * Special case for successfully execution w/ both DATAIN | 
 | 2503 | 	 * and Sense Data. | 
 | 2504 | 	 */ | 
 | 2505 | 	if ((datain.flags & ISCSI_FLAG_DATA_STATUS) && | 
 | 2506 | 	    (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)) | 
 | 2507 | 		datain.flags &= ~ISCSI_FLAG_DATA_STATUS; | 
 | 2508 | 	else { | 
 | 2509 | 		if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) || | 
 | 2510 | 		    (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) { | 
 | 2511 | 			iscsit_increment_maxcmdsn(cmd, conn->sess); | 
 | 2512 | 			cmd->stat_sn = conn->stat_sn++; | 
 | 2513 | 			set_statsn = 1; | 
 | 2514 | 		} else if (dr->dr_complete == | 
 | 2515 | 				DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY) | 
 | 2516 | 			set_statsn = 1; | 
 | 2517 | 	} | 
 | 2518 |  | 
 | 2519 | 	hdr	= (struct iscsi_data_rsp *) cmd->pdu; | 
 | 2520 | 	memset(hdr, 0, ISCSI_HDR_LEN); | 
 | 2521 | 	hdr->opcode		= ISCSI_OP_SCSI_DATA_IN; | 
 | 2522 | 	hdr->flags		= datain.flags; | 
 | 2523 | 	if (hdr->flags & ISCSI_FLAG_DATA_STATUS) { | 
 | 2524 | 		if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) { | 
 | 2525 | 			hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW; | 
| Nicholas Bellinger | 7e46cf0 | 2011-11-15 23:59:00 -0800 | [diff] [blame] | 2526 | 			hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2527 | 		} else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) { | 
 | 2528 | 			hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW; | 
| Nicholas Bellinger | 7e46cf0 | 2011-11-15 23:59:00 -0800 | [diff] [blame] | 2529 | 			hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2530 | 		} | 
 | 2531 | 	} | 
 | 2532 | 	hton24(hdr->dlength, datain.length); | 
 | 2533 | 	if (hdr->flags & ISCSI_FLAG_DATA_ACK) | 
 | 2534 | 		int_to_scsilun(cmd->se_cmd.orig_fe_lun, | 
 | 2535 | 				(struct scsi_lun *)&hdr->lun); | 
 | 2536 | 	else | 
 | 2537 | 		put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun); | 
 | 2538 |  | 
 | 2539 | 	hdr->itt		= cpu_to_be32(cmd->init_task_tag); | 
 | 2540 | 	hdr->ttt		= (hdr->flags & ISCSI_FLAG_DATA_ACK) ? | 
 | 2541 | 				   cpu_to_be32(cmd->targ_xfer_tag) : | 
 | 2542 | 				   0xFFFFFFFF; | 
 | 2543 | 	hdr->statsn		= (set_statsn) ? cpu_to_be32(cmd->stat_sn) : | 
 | 2544 | 						0xFFFFFFFF; | 
 | 2545 | 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn); | 
 | 2546 | 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn); | 
 | 2547 | 	hdr->datasn		= cpu_to_be32(datain.data_sn); | 
 | 2548 | 	hdr->offset		= cpu_to_be32(datain.offset); | 
 | 2549 |  | 
 | 2550 | 	iov = &cmd->iov_data[0]; | 
 | 2551 | 	iov[iov_count].iov_base	= cmd->pdu; | 
 | 2552 | 	iov[iov_count++].iov_len	= ISCSI_HDR_LEN; | 
 | 2553 | 	tx_size += ISCSI_HDR_LEN; | 
 | 2554 |  | 
 | 2555 | 	if (conn->conn_ops->HeaderDigest) { | 
 | 2556 | 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; | 
 | 2557 |  | 
 | 2558 | 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, | 
 | 2559 | 				(unsigned char *)hdr, ISCSI_HDR_LEN, | 
 | 2560 | 				0, NULL, (u8 *)header_digest); | 
 | 2561 |  | 
 | 2562 | 		iov[0].iov_len += ISCSI_CRC_LEN; | 
 | 2563 | 		tx_size += ISCSI_CRC_LEN; | 
 | 2564 |  | 
 | 2565 | 		pr_debug("Attaching CRC32 HeaderDigest" | 
 | 2566 | 			" for DataIN PDU 0x%08x\n", *header_digest); | 
 | 2567 | 	} | 
 | 2568 |  | 
 | 2569 | 	iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1], datain.offset, datain.length); | 
 | 2570 | 	if (iov_ret < 0) | 
 | 2571 | 		return -1; | 
 | 2572 |  | 
 | 2573 | 	iov_count += iov_ret; | 
 | 2574 | 	tx_size += datain.length; | 
 | 2575 |  | 
 | 2576 | 	cmd->padding = ((-datain.length) & 3); | 
 | 2577 | 	if (cmd->padding) { | 
 | 2578 | 		iov[iov_count].iov_base		= cmd->pad_bytes; | 
 | 2579 | 		iov[iov_count++].iov_len	= cmd->padding; | 
 | 2580 | 		tx_size += cmd->padding; | 
 | 2581 |  | 
 | 2582 | 		pr_debug("Attaching %u padding bytes\n", | 
 | 2583 | 				cmd->padding); | 
 | 2584 | 	} | 
 | 2585 | 	if (conn->conn_ops->DataDigest) { | 
 | 2586 | 		cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd, | 
 | 2587 | 			 datain.offset, datain.length, cmd->padding, cmd->pad_bytes); | 
 | 2588 |  | 
 | 2589 | 		iov[iov_count].iov_base	= &cmd->data_crc; | 
 | 2590 | 		iov[iov_count++].iov_len = ISCSI_CRC_LEN; | 
 | 2591 | 		tx_size += ISCSI_CRC_LEN; | 
 | 2592 |  | 
 | 2593 | 		pr_debug("Attached CRC32C DataDigest %d bytes, crc" | 
 | 2594 | 			" 0x%08x\n", datain.length+cmd->padding, cmd->data_crc); | 
 | 2595 | 	} | 
 | 2596 |  | 
 | 2597 | 	cmd->iov_data_count = iov_count; | 
 | 2598 | 	cmd->tx_size = tx_size; | 
 | 2599 |  | 
 | 2600 | 	pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x," | 
 | 2601 | 		" DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n", | 
 | 2602 | 		cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn), | 
 | 2603 | 		ntohl(hdr->offset), datain.length, conn->cid); | 
 | 2604 |  | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2605 | 	/* sendpage is preferred but can't insert markers */ | 
 | 2606 | 	if (!conn->conn_ops->IFMarker) | 
 | 2607 | 		ret = iscsit_fe_sendpage_sg(cmd, conn); | 
 | 2608 | 	else | 
 | 2609 | 		ret = iscsit_send_tx_data(cmd, conn, 0); | 
 | 2610 |  | 
 | 2611 | 	iscsit_unmap_iovec(cmd); | 
 | 2612 |  | 
 | 2613 | 	if (ret < 0) { | 
 | 2614 | 		iscsit_tx_thread_wait_for_tcp(conn); | 
 | 2615 | 		return ret; | 
 | 2616 | 	} | 
 | 2617 |  | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2618 | 	if (dr->dr_complete) { | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2619 | 		eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ? | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2620 | 				2 : 1; | 
 | 2621 | 		iscsit_free_datain_req(cmd, dr); | 
 | 2622 | 	} | 
 | 2623 |  | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2624 | 	return eodr; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2625 | } | 
 | 2626 |  | 
 | 2627 | static int iscsit_send_logout_response( | 
 | 2628 | 	struct iscsi_cmd *cmd, | 
 | 2629 | 	struct iscsi_conn *conn) | 
 | 2630 | { | 
 | 2631 | 	int niov = 0, tx_size; | 
 | 2632 | 	struct iscsi_conn *logout_conn = NULL; | 
 | 2633 | 	struct iscsi_conn_recovery *cr = NULL; | 
 | 2634 | 	struct iscsi_session *sess = conn->sess; | 
 | 2635 | 	struct kvec *iov; | 
 | 2636 | 	struct iscsi_logout_rsp *hdr; | 
 | 2637 | 	/* | 
 | 2638 | 	 * The actual shutting down of Sessions and/or Connections | 
 | 2639 | 	 * for CLOSESESSION and CLOSECONNECTION Logout Requests | 
 | 2640 | 	 * is done in scsi_logout_post_handler(). | 
 | 2641 | 	 */ | 
 | 2642 | 	switch (cmd->logout_reason) { | 
 | 2643 | 	case ISCSI_LOGOUT_REASON_CLOSE_SESSION: | 
 | 2644 | 		pr_debug("iSCSI session logout successful, setting" | 
 | 2645 | 			" logout response to ISCSI_LOGOUT_SUCCESS.\n"); | 
 | 2646 | 		cmd->logout_response = ISCSI_LOGOUT_SUCCESS; | 
 | 2647 | 		break; | 
 | 2648 | 	case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION: | 
 | 2649 | 		if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND) | 
 | 2650 | 			break; | 
 | 2651 | 		/* | 
 | 2652 | 		 * For CLOSECONNECTION logout requests carrying | 
 | 2653 | 		 * a matching logout CID -> local CID, the reference | 
 | 2654 | 		 * for the local CID will have been incremented in | 
 | 2655 | 		 * iscsi_logout_closeconnection(). | 
 | 2656 | 		 * | 
 | 2657 | 		 * For CLOSECONNECTION logout requests carrying | 
 | 2658 | 		 * a different CID than the connection it arrived | 
 | 2659 | 		 * on, the connection responding to cmd->logout_cid | 
 | 2660 | 		 * is stopped in iscsit_logout_post_handler_diffcid(). | 
 | 2661 | 		 */ | 
 | 2662 |  | 
 | 2663 | 		pr_debug("iSCSI CID: %hu logout on CID: %hu" | 
 | 2664 | 			" successful.\n", cmd->logout_cid, conn->cid); | 
 | 2665 | 		cmd->logout_response = ISCSI_LOGOUT_SUCCESS; | 
 | 2666 | 		break; | 
 | 2667 | 	case ISCSI_LOGOUT_REASON_RECOVERY: | 
 | 2668 | 		if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) || | 
 | 2669 | 		    (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED)) | 
 | 2670 | 			break; | 
 | 2671 | 		/* | 
 | 2672 | 		 * If the connection is still active from our point of view | 
 | 2673 | 		 * force connection recovery to occur. | 
 | 2674 | 		 */ | 
 | 2675 | 		logout_conn = iscsit_get_conn_from_cid_rcfr(sess, | 
 | 2676 | 				cmd->logout_cid); | 
| Andy Grover | ee1b1b9 | 2012-07-12 17:34:54 -0700 | [diff] [blame] | 2677 | 		if (logout_conn) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2678 | 			iscsit_connection_reinstatement_rcfr(logout_conn); | 
 | 2679 | 			iscsit_dec_conn_usage_count(logout_conn); | 
 | 2680 | 		} | 
 | 2681 |  | 
 | 2682 | 		cr = iscsit_get_inactive_connection_recovery_entry( | 
 | 2683 | 				conn->sess, cmd->logout_cid); | 
 | 2684 | 		if (!cr) { | 
 | 2685 | 			pr_err("Unable to locate CID: %hu for" | 
 | 2686 | 			" REMOVECONNFORRECOVERY Logout Request.\n", | 
 | 2687 | 				cmd->logout_cid); | 
 | 2688 | 			cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND; | 
 | 2689 | 			break; | 
 | 2690 | 		} | 
 | 2691 |  | 
 | 2692 | 		iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn); | 
 | 2693 |  | 
 | 2694 | 		pr_debug("iSCSI REMOVECONNFORRECOVERY logout" | 
 | 2695 | 			" for recovery for CID: %hu on CID: %hu successful.\n", | 
 | 2696 | 				cmd->logout_cid, conn->cid); | 
 | 2697 | 		cmd->logout_response = ISCSI_LOGOUT_SUCCESS; | 
 | 2698 | 		break; | 
 | 2699 | 	default: | 
 | 2700 | 		pr_err("Unknown cmd->logout_reason: 0x%02x\n", | 
 | 2701 | 				cmd->logout_reason); | 
 | 2702 | 		return -1; | 
 | 2703 | 	} | 
 | 2704 |  | 
 | 2705 | 	tx_size = ISCSI_HDR_LEN; | 
 | 2706 | 	hdr			= (struct iscsi_logout_rsp *)cmd->pdu; | 
 | 2707 | 	memset(hdr, 0, ISCSI_HDR_LEN); | 
 | 2708 | 	hdr->opcode		= ISCSI_OP_LOGOUT_RSP; | 
 | 2709 | 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL; | 
 | 2710 | 	hdr->response		= cmd->logout_response; | 
 | 2711 | 	hdr->itt		= cpu_to_be32(cmd->init_task_tag); | 
 | 2712 | 	cmd->stat_sn		= conn->stat_sn++; | 
 | 2713 | 	hdr->statsn		= cpu_to_be32(cmd->stat_sn); | 
 | 2714 |  | 
 | 2715 | 	iscsit_increment_maxcmdsn(cmd, conn->sess); | 
 | 2716 | 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn); | 
 | 2717 | 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn); | 
 | 2718 |  | 
 | 2719 | 	iov = &cmd->iov_misc[0]; | 
 | 2720 | 	iov[niov].iov_base	= cmd->pdu; | 
 | 2721 | 	iov[niov++].iov_len	= ISCSI_HDR_LEN; | 
 | 2722 |  | 
 | 2723 | 	if (conn->conn_ops->HeaderDigest) { | 
 | 2724 | 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; | 
 | 2725 |  | 
 | 2726 | 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, | 
 | 2727 | 				(unsigned char *)hdr, ISCSI_HDR_LEN, | 
 | 2728 | 				0, NULL, (u8 *)header_digest); | 
 | 2729 |  | 
 | 2730 | 		iov[0].iov_len += ISCSI_CRC_LEN; | 
 | 2731 | 		tx_size += ISCSI_CRC_LEN; | 
 | 2732 | 		pr_debug("Attaching CRC32C HeaderDigest to" | 
 | 2733 | 			" Logout Response 0x%08x\n", *header_digest); | 
 | 2734 | 	} | 
 | 2735 | 	cmd->iov_misc_count = niov; | 
 | 2736 | 	cmd->tx_size = tx_size; | 
 | 2737 |  | 
 | 2738 | 	pr_debug("Sending Logout Response ITT: 0x%08x StatSN:" | 
 | 2739 | 		" 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n", | 
 | 2740 | 		cmd->init_task_tag, cmd->stat_sn, hdr->response, | 
 | 2741 | 		cmd->logout_cid, conn->cid); | 
 | 2742 |  | 
 | 2743 | 	return 0; | 
 | 2744 | } | 
 | 2745 |  | 
 | 2746 | /* | 
 | 2747 |  *	Unsolicited NOPIN, either requesting a response or not. | 
 | 2748 |  */ | 
 | 2749 | static int iscsit_send_unsolicited_nopin( | 
 | 2750 | 	struct iscsi_cmd *cmd, | 
 | 2751 | 	struct iscsi_conn *conn, | 
 | 2752 | 	int want_response) | 
 | 2753 | { | 
 | 2754 | 	int tx_size = ISCSI_HDR_LEN; | 
 | 2755 | 	struct iscsi_nopin *hdr; | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2756 | 	int ret; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2757 |  | 
 | 2758 | 	hdr			= (struct iscsi_nopin *) cmd->pdu; | 
 | 2759 | 	memset(hdr, 0, ISCSI_HDR_LEN); | 
 | 2760 | 	hdr->opcode		= ISCSI_OP_NOOP_IN; | 
 | 2761 | 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL; | 
 | 2762 | 	hdr->itt		= cpu_to_be32(cmd->init_task_tag); | 
 | 2763 | 	hdr->ttt		= cpu_to_be32(cmd->targ_xfer_tag); | 
 | 2764 | 	cmd->stat_sn		= conn->stat_sn; | 
 | 2765 | 	hdr->statsn		= cpu_to_be32(cmd->stat_sn); | 
 | 2766 | 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn); | 
 | 2767 | 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn); | 
 | 2768 |  | 
 | 2769 | 	if (conn->conn_ops->HeaderDigest) { | 
 | 2770 | 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; | 
 | 2771 |  | 
 | 2772 | 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, | 
 | 2773 | 				(unsigned char *)hdr, ISCSI_HDR_LEN, | 
 | 2774 | 				0, NULL, (u8 *)header_digest); | 
 | 2775 |  | 
 | 2776 | 		tx_size += ISCSI_CRC_LEN; | 
 | 2777 | 		pr_debug("Attaching CRC32C HeaderDigest to" | 
 | 2778 | 			" NopIN 0x%08x\n", *header_digest); | 
 | 2779 | 	} | 
 | 2780 |  | 
 | 2781 | 	cmd->iov_misc[0].iov_base	= cmd->pdu; | 
 | 2782 | 	cmd->iov_misc[0].iov_len	= tx_size; | 
 | 2783 | 	cmd->iov_misc_count	= 1; | 
 | 2784 | 	cmd->tx_size		= tx_size; | 
 | 2785 |  | 
 | 2786 | 	pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:" | 
 | 2787 | 		" 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid); | 
 | 2788 |  | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2789 | 	ret = iscsit_send_tx_data(cmd, conn, 1); | 
 | 2790 | 	if (ret < 0) { | 
 | 2791 | 		iscsit_tx_thread_wait_for_tcp(conn); | 
 | 2792 | 		return ret; | 
 | 2793 | 	} | 
 | 2794 |  | 
 | 2795 | 	spin_lock_bh(&cmd->istate_lock); | 
 | 2796 | 	cmd->i_state = want_response ? | 
 | 2797 | 		ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS; | 
 | 2798 | 	spin_unlock_bh(&cmd->istate_lock); | 
 | 2799 |  | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2800 | 	return 0; | 
 | 2801 | } | 
 | 2802 |  | 
 | 2803 | static int iscsit_send_nopin_response( | 
 | 2804 | 	struct iscsi_cmd *cmd, | 
 | 2805 | 	struct iscsi_conn *conn) | 
 | 2806 | { | 
 | 2807 | 	int niov = 0, tx_size; | 
 | 2808 | 	u32 padding = 0; | 
 | 2809 | 	struct kvec *iov; | 
 | 2810 | 	struct iscsi_nopin *hdr; | 
 | 2811 |  | 
 | 2812 | 	tx_size = ISCSI_HDR_LEN; | 
 | 2813 | 	hdr			= (struct iscsi_nopin *) cmd->pdu; | 
 | 2814 | 	memset(hdr, 0, ISCSI_HDR_LEN); | 
 | 2815 | 	hdr->opcode		= ISCSI_OP_NOOP_IN; | 
 | 2816 | 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL; | 
 | 2817 | 	hton24(hdr->dlength, cmd->buf_ptr_size); | 
 | 2818 | 	put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun); | 
 | 2819 | 	hdr->itt		= cpu_to_be32(cmd->init_task_tag); | 
 | 2820 | 	hdr->ttt		= cpu_to_be32(cmd->targ_xfer_tag); | 
 | 2821 | 	cmd->stat_sn		= conn->stat_sn++; | 
 | 2822 | 	hdr->statsn		= cpu_to_be32(cmd->stat_sn); | 
 | 2823 |  | 
 | 2824 | 	iscsit_increment_maxcmdsn(cmd, conn->sess); | 
 | 2825 | 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn); | 
 | 2826 | 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn); | 
 | 2827 |  | 
 | 2828 | 	iov = &cmd->iov_misc[0]; | 
 | 2829 | 	iov[niov].iov_base	= cmd->pdu; | 
 | 2830 | 	iov[niov++].iov_len	= ISCSI_HDR_LEN; | 
 | 2831 |  | 
 | 2832 | 	if (conn->conn_ops->HeaderDigest) { | 
 | 2833 | 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; | 
 | 2834 |  | 
 | 2835 | 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, | 
 | 2836 | 				(unsigned char *)hdr, ISCSI_HDR_LEN, | 
 | 2837 | 				0, NULL, (u8 *)header_digest); | 
 | 2838 |  | 
 | 2839 | 		iov[0].iov_len += ISCSI_CRC_LEN; | 
 | 2840 | 		tx_size += ISCSI_CRC_LEN; | 
 | 2841 | 		pr_debug("Attaching CRC32C HeaderDigest" | 
 | 2842 | 			" to NopIn 0x%08x\n", *header_digest); | 
 | 2843 | 	} | 
 | 2844 |  | 
 | 2845 | 	/* | 
 | 2846 | 	 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr. | 
 | 2847 | 	 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size. | 
 | 2848 | 	 */ | 
 | 2849 | 	if (cmd->buf_ptr_size) { | 
 | 2850 | 		iov[niov].iov_base	= cmd->buf_ptr; | 
 | 2851 | 		iov[niov++].iov_len	= cmd->buf_ptr_size; | 
 | 2852 | 		tx_size += cmd->buf_ptr_size; | 
 | 2853 |  | 
 | 2854 | 		pr_debug("Echoing back %u bytes of ping" | 
 | 2855 | 			" data.\n", cmd->buf_ptr_size); | 
 | 2856 |  | 
 | 2857 | 		padding = ((-cmd->buf_ptr_size) & 3); | 
 | 2858 | 		if (padding != 0) { | 
 | 2859 | 			iov[niov].iov_base = &cmd->pad_bytes; | 
 | 2860 | 			iov[niov++].iov_len = padding; | 
 | 2861 | 			tx_size += padding; | 
 | 2862 | 			pr_debug("Attaching %u additional" | 
 | 2863 | 				" padding bytes.\n", padding); | 
 | 2864 | 		} | 
 | 2865 | 		if (conn->conn_ops->DataDigest) { | 
 | 2866 | 			iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, | 
 | 2867 | 				cmd->buf_ptr, cmd->buf_ptr_size, | 
 | 2868 | 				padding, (u8 *)&cmd->pad_bytes, | 
 | 2869 | 				(u8 *)&cmd->data_crc); | 
 | 2870 |  | 
 | 2871 | 			iov[niov].iov_base = &cmd->data_crc; | 
 | 2872 | 			iov[niov++].iov_len = ISCSI_CRC_LEN; | 
 | 2873 | 			tx_size += ISCSI_CRC_LEN; | 
 | 2874 | 			pr_debug("Attached DataDigest for %u" | 
 | 2875 | 				" bytes of ping data, CRC 0x%08x\n", | 
 | 2876 | 				cmd->buf_ptr_size, cmd->data_crc); | 
 | 2877 | 		} | 
 | 2878 | 	} | 
 | 2879 |  | 
 | 2880 | 	cmd->iov_misc_count = niov; | 
 | 2881 | 	cmd->tx_size = tx_size; | 
 | 2882 |  | 
 | 2883 | 	pr_debug("Sending NOPIN Response ITT: 0x%08x, TTT:" | 
 | 2884 | 		" 0x%08x, StatSN: 0x%08x, Length %u\n", cmd->init_task_tag, | 
 | 2885 | 		cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size); | 
 | 2886 |  | 
 | 2887 | 	return 0; | 
 | 2888 | } | 
 | 2889 |  | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2890 | static int iscsit_send_r2t( | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2891 | 	struct iscsi_cmd *cmd, | 
 | 2892 | 	struct iscsi_conn *conn) | 
 | 2893 | { | 
 | 2894 | 	int tx_size = 0; | 
 | 2895 | 	struct iscsi_r2t *r2t; | 
 | 2896 | 	struct iscsi_r2t_rsp *hdr; | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2897 | 	int ret; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2898 |  | 
 | 2899 | 	r2t = iscsit_get_r2t_from_list(cmd); | 
 | 2900 | 	if (!r2t) | 
 | 2901 | 		return -1; | 
 | 2902 |  | 
 | 2903 | 	hdr			= (struct iscsi_r2t_rsp *) cmd->pdu; | 
 | 2904 | 	memset(hdr, 0, ISCSI_HDR_LEN); | 
 | 2905 | 	hdr->opcode		= ISCSI_OP_R2T; | 
 | 2906 | 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL; | 
 | 2907 | 	int_to_scsilun(cmd->se_cmd.orig_fe_lun, | 
 | 2908 | 			(struct scsi_lun *)&hdr->lun); | 
 | 2909 | 	hdr->itt		= cpu_to_be32(cmd->init_task_tag); | 
 | 2910 | 	spin_lock_bh(&conn->sess->ttt_lock); | 
 | 2911 | 	r2t->targ_xfer_tag	= conn->sess->targ_xfer_tag++; | 
 | 2912 | 	if (r2t->targ_xfer_tag == 0xFFFFFFFF) | 
 | 2913 | 		r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++; | 
 | 2914 | 	spin_unlock_bh(&conn->sess->ttt_lock); | 
 | 2915 | 	hdr->ttt		= cpu_to_be32(r2t->targ_xfer_tag); | 
 | 2916 | 	hdr->statsn		= cpu_to_be32(conn->stat_sn); | 
 | 2917 | 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn); | 
 | 2918 | 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn); | 
 | 2919 | 	hdr->r2tsn		= cpu_to_be32(r2t->r2t_sn); | 
 | 2920 | 	hdr->data_offset	= cpu_to_be32(r2t->offset); | 
 | 2921 | 	hdr->data_length	= cpu_to_be32(r2t->xfer_len); | 
 | 2922 |  | 
 | 2923 | 	cmd->iov_misc[0].iov_base	= cmd->pdu; | 
 | 2924 | 	cmd->iov_misc[0].iov_len	= ISCSI_HDR_LEN; | 
 | 2925 | 	tx_size += ISCSI_HDR_LEN; | 
 | 2926 |  | 
 | 2927 | 	if (conn->conn_ops->HeaderDigest) { | 
 | 2928 | 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; | 
 | 2929 |  | 
 | 2930 | 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, | 
 | 2931 | 				(unsigned char *)hdr, ISCSI_HDR_LEN, | 
 | 2932 | 				0, NULL, (u8 *)header_digest); | 
 | 2933 |  | 
 | 2934 | 		cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN; | 
 | 2935 | 		tx_size += ISCSI_CRC_LEN; | 
 | 2936 | 		pr_debug("Attaching CRC32 HeaderDigest for R2T" | 
 | 2937 | 			" PDU 0x%08x\n", *header_digest); | 
 | 2938 | 	} | 
 | 2939 |  | 
 | 2940 | 	pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:" | 
 | 2941 | 		" 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n", | 
 | 2942 | 		(!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag, | 
 | 2943 | 		r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn, | 
 | 2944 | 			r2t->offset, r2t->xfer_len, conn->cid); | 
 | 2945 |  | 
 | 2946 | 	cmd->iov_misc_count = 1; | 
 | 2947 | 	cmd->tx_size = tx_size; | 
 | 2948 |  | 
 | 2949 | 	spin_lock_bh(&cmd->r2t_lock); | 
 | 2950 | 	r2t->sent_r2t = 1; | 
 | 2951 | 	spin_unlock_bh(&cmd->r2t_lock); | 
 | 2952 |  | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2953 | 	ret = iscsit_send_tx_data(cmd, conn, 1); | 
 | 2954 | 	if (ret < 0) { | 
 | 2955 | 		iscsit_tx_thread_wait_for_tcp(conn); | 
 | 2956 | 		return ret; | 
 | 2957 | 	} | 
 | 2958 |  | 
 | 2959 | 	spin_lock_bh(&cmd->dataout_timeout_lock); | 
 | 2960 | 	iscsit_start_dataout_timer(cmd, conn); | 
 | 2961 | 	spin_unlock_bh(&cmd->dataout_timeout_lock); | 
 | 2962 |  | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2963 | 	return 0; | 
 | 2964 | } | 
 | 2965 |  | 
 | 2966 | /* | 
| Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 2967 |  *	@recovery: If called from iscsi_task_reassign_complete_write() for | 
 | 2968 |  *		connection recovery. | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2969 |  */ | 
 | 2970 | int iscsit_build_r2ts_for_cmd( | 
 | 2971 | 	struct iscsi_cmd *cmd, | 
 | 2972 | 	struct iscsi_conn *conn, | 
| Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 2973 | 	bool recovery) | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2974 | { | 
 | 2975 | 	int first_r2t = 1; | 
 | 2976 | 	u32 offset = 0, xfer_len = 0; | 
 | 2977 |  | 
 | 2978 | 	spin_lock_bh(&cmd->r2t_lock); | 
 | 2979 | 	if (cmd->cmd_flags & ICF_SENT_LAST_R2T) { | 
 | 2980 | 		spin_unlock_bh(&cmd->r2t_lock); | 
 | 2981 | 		return 0; | 
 | 2982 | 	} | 
 | 2983 |  | 
| Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 2984 | 	if (conn->sess->sess_ops->DataSequenceInOrder && | 
 | 2985 | 	    !recovery) | 
| Andy Grover | c6037cc | 2012-04-03 15:51:02 -0700 | [diff] [blame] | 2986 | 		cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2987 |  | 
 | 2988 | 	while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) { | 
 | 2989 | 		if (conn->sess->sess_ops->DataSequenceInOrder) { | 
 | 2990 | 			offset = cmd->r2t_offset; | 
 | 2991 |  | 
| Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 2992 | 			if (first_r2t && recovery) { | 
 | 2993 | 				int new_data_end = offset + | 
 | 2994 | 					conn->sess->sess_ops->MaxBurstLength - | 
 | 2995 | 					cmd->next_burst_len; | 
 | 2996 |  | 
| Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 2997 | 				if (new_data_end > cmd->se_cmd.data_length) | 
 | 2998 | 					xfer_len = cmd->se_cmd.data_length - offset; | 
| Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 2999 | 				else | 
 | 3000 | 					xfer_len = | 
 | 3001 | 						conn->sess->sess_ops->MaxBurstLength - | 
 | 3002 | 						cmd->next_burst_len; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3003 | 			} else { | 
| Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3004 | 				int new_data_end = offset + | 
 | 3005 | 					conn->sess->sess_ops->MaxBurstLength; | 
 | 3006 |  | 
| Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 3007 | 				if (new_data_end > cmd->se_cmd.data_length) | 
 | 3008 | 					xfer_len = cmd->se_cmd.data_length - offset; | 
| Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3009 | 				else | 
 | 3010 | 					xfer_len = conn->sess->sess_ops->MaxBurstLength; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3011 | 			} | 
 | 3012 | 			cmd->r2t_offset += xfer_len; | 
 | 3013 |  | 
| Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 3014 | 			if (cmd->r2t_offset == cmd->se_cmd.data_length) | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3015 | 				cmd->cmd_flags |= ICF_SENT_LAST_R2T; | 
 | 3016 | 		} else { | 
 | 3017 | 			struct iscsi_seq *seq; | 
 | 3018 |  | 
 | 3019 | 			seq = iscsit_get_seq_holder_for_r2t(cmd); | 
 | 3020 | 			if (!seq) { | 
 | 3021 | 				spin_unlock_bh(&cmd->r2t_lock); | 
 | 3022 | 				return -1; | 
 | 3023 | 			} | 
 | 3024 |  | 
 | 3025 | 			offset = seq->offset; | 
 | 3026 | 			xfer_len = seq->xfer_len; | 
 | 3027 |  | 
 | 3028 | 			if (cmd->seq_send_order == cmd->seq_count) | 
 | 3029 | 				cmd->cmd_flags |= ICF_SENT_LAST_R2T; | 
 | 3030 | 		} | 
 | 3031 | 		cmd->outstanding_r2ts++; | 
 | 3032 | 		first_r2t = 0; | 
 | 3033 |  | 
 | 3034 | 		if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) { | 
 | 3035 | 			spin_unlock_bh(&cmd->r2t_lock); | 
 | 3036 | 			return -1; | 
 | 3037 | 		} | 
 | 3038 |  | 
 | 3039 | 		if (cmd->cmd_flags & ICF_SENT_LAST_R2T) | 
 | 3040 | 			break; | 
 | 3041 | 	} | 
 | 3042 | 	spin_unlock_bh(&cmd->r2t_lock); | 
 | 3043 |  | 
 | 3044 | 	return 0; | 
 | 3045 | } | 
 | 3046 |  | 
 | 3047 | static int iscsit_send_status( | 
 | 3048 | 	struct iscsi_cmd *cmd, | 
 | 3049 | 	struct iscsi_conn *conn) | 
 | 3050 | { | 
 | 3051 | 	u8 iov_count = 0, recovery; | 
 | 3052 | 	u32 padding = 0, tx_size = 0; | 
 | 3053 | 	struct iscsi_scsi_rsp *hdr; | 
 | 3054 | 	struct kvec *iov; | 
 | 3055 |  | 
 | 3056 | 	recovery = (cmd->i_state != ISTATE_SEND_STATUS); | 
 | 3057 | 	if (!recovery) | 
 | 3058 | 		cmd->stat_sn = conn->stat_sn++; | 
 | 3059 |  | 
 | 3060 | 	spin_lock_bh(&conn->sess->session_stats_lock); | 
 | 3061 | 	conn->sess->rsp_pdus++; | 
 | 3062 | 	spin_unlock_bh(&conn->sess->session_stats_lock); | 
 | 3063 |  | 
 | 3064 | 	hdr			= (struct iscsi_scsi_rsp *) cmd->pdu; | 
 | 3065 | 	memset(hdr, 0, ISCSI_HDR_LEN); | 
 | 3066 | 	hdr->opcode		= ISCSI_OP_SCSI_CMD_RSP; | 
 | 3067 | 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL; | 
 | 3068 | 	if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) { | 
 | 3069 | 		hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW; | 
| Nicholas Bellinger | 7e46cf0 | 2011-11-15 23:59:00 -0800 | [diff] [blame] | 3070 | 		hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3071 | 	} else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) { | 
 | 3072 | 		hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW; | 
| Nicholas Bellinger | 7e46cf0 | 2011-11-15 23:59:00 -0800 | [diff] [blame] | 3073 | 		hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3074 | 	} | 
 | 3075 | 	hdr->response		= cmd->iscsi_response; | 
 | 3076 | 	hdr->cmd_status		= cmd->se_cmd.scsi_status; | 
 | 3077 | 	hdr->itt		= cpu_to_be32(cmd->init_task_tag); | 
 | 3078 | 	hdr->statsn		= cpu_to_be32(cmd->stat_sn); | 
 | 3079 |  | 
 | 3080 | 	iscsit_increment_maxcmdsn(cmd, conn->sess); | 
 | 3081 | 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn); | 
 | 3082 | 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn); | 
 | 3083 |  | 
 | 3084 | 	iov = &cmd->iov_misc[0]; | 
 | 3085 | 	iov[iov_count].iov_base	= cmd->pdu; | 
 | 3086 | 	iov[iov_count++].iov_len = ISCSI_HDR_LEN; | 
 | 3087 | 	tx_size += ISCSI_HDR_LEN; | 
 | 3088 |  | 
 | 3089 | 	/* | 
 | 3090 | 	 * Attach SENSE DATA payload to iSCSI Response PDU | 
 | 3091 | 	 */ | 
 | 3092 | 	if (cmd->se_cmd.sense_buffer && | 
 | 3093 | 	   ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) || | 
 | 3094 | 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) { | 
| Roland Dreier | 9c58b7d | 2012-08-15 14:35:25 -0700 | [diff] [blame] | 3095 | 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer); | 
 | 3096 | 		cmd->se_cmd.scsi_sense_length += sizeof (__be16); | 
 | 3097 |  | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3098 | 		padding		= -(cmd->se_cmd.scsi_sense_length) & 3; | 
 | 3099 | 		hton24(hdr->dlength, cmd->se_cmd.scsi_sense_length); | 
| Roland Dreier | 9c58b7d | 2012-08-15 14:35:25 -0700 | [diff] [blame] | 3100 | 		iov[iov_count].iov_base	= cmd->sense_buffer; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3101 | 		iov[iov_count++].iov_len = | 
 | 3102 | 				(cmd->se_cmd.scsi_sense_length + padding); | 
 | 3103 | 		tx_size += cmd->se_cmd.scsi_sense_length; | 
 | 3104 |  | 
 | 3105 | 		if (padding) { | 
| Roland Dreier | 9c58b7d | 2012-08-15 14:35:25 -0700 | [diff] [blame] | 3106 | 			memset(cmd->sense_buffer + | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3107 | 				cmd->se_cmd.scsi_sense_length, 0, padding); | 
 | 3108 | 			tx_size += padding; | 
 | 3109 | 			pr_debug("Adding %u bytes of padding to" | 
 | 3110 | 				" SENSE.\n", padding); | 
 | 3111 | 		} | 
 | 3112 |  | 
 | 3113 | 		if (conn->conn_ops->DataDigest) { | 
 | 3114 | 			iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, | 
| Roland Dreier | 9c58b7d | 2012-08-15 14:35:25 -0700 | [diff] [blame] | 3115 | 				cmd->sense_buffer, | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3116 | 				(cmd->se_cmd.scsi_sense_length + padding), | 
 | 3117 | 				0, NULL, (u8 *)&cmd->data_crc); | 
 | 3118 |  | 
 | 3119 | 			iov[iov_count].iov_base    = &cmd->data_crc; | 
 | 3120 | 			iov[iov_count++].iov_len     = ISCSI_CRC_LEN; | 
 | 3121 | 			tx_size += ISCSI_CRC_LEN; | 
 | 3122 |  | 
 | 3123 | 			pr_debug("Attaching CRC32 DataDigest for" | 
 | 3124 | 				" SENSE, %u bytes CRC 0x%08x\n", | 
 | 3125 | 				(cmd->se_cmd.scsi_sense_length + padding), | 
 | 3126 | 				cmd->data_crc); | 
 | 3127 | 		} | 
 | 3128 |  | 
 | 3129 | 		pr_debug("Attaching SENSE DATA: %u bytes to iSCSI" | 
 | 3130 | 				" Response PDU\n", | 
 | 3131 | 				cmd->se_cmd.scsi_sense_length); | 
 | 3132 | 	} | 
 | 3133 |  | 
 | 3134 | 	if (conn->conn_ops->HeaderDigest) { | 
 | 3135 | 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; | 
 | 3136 |  | 
 | 3137 | 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, | 
 | 3138 | 				(unsigned char *)hdr, ISCSI_HDR_LEN, | 
 | 3139 | 				0, NULL, (u8 *)header_digest); | 
 | 3140 |  | 
 | 3141 | 		iov[0].iov_len += ISCSI_CRC_LEN; | 
 | 3142 | 		tx_size += ISCSI_CRC_LEN; | 
 | 3143 | 		pr_debug("Attaching CRC32 HeaderDigest for Response" | 
 | 3144 | 				" PDU 0x%08x\n", *header_digest); | 
 | 3145 | 	} | 
 | 3146 |  | 
 | 3147 | 	cmd->iov_misc_count = iov_count; | 
 | 3148 | 	cmd->tx_size = tx_size; | 
 | 3149 |  | 
 | 3150 | 	pr_debug("Built %sSCSI Response, ITT: 0x%08x, StatSN: 0x%08x," | 
 | 3151 | 		" Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n", | 
 | 3152 | 		(!recovery) ? "" : "Recovery ", cmd->init_task_tag, | 
 | 3153 | 		cmd->stat_sn, 0x00, cmd->se_cmd.scsi_status, conn->cid); | 
 | 3154 |  | 
 | 3155 | 	return 0; | 
 | 3156 | } | 
 | 3157 |  | 
 | 3158 | static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr) | 
 | 3159 | { | 
 | 3160 | 	switch (se_tmr->response) { | 
 | 3161 | 	case TMR_FUNCTION_COMPLETE: | 
 | 3162 | 		return ISCSI_TMF_RSP_COMPLETE; | 
 | 3163 | 	case TMR_TASK_DOES_NOT_EXIST: | 
 | 3164 | 		return ISCSI_TMF_RSP_NO_TASK; | 
 | 3165 | 	case TMR_LUN_DOES_NOT_EXIST: | 
 | 3166 | 		return ISCSI_TMF_RSP_NO_LUN; | 
 | 3167 | 	case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED: | 
 | 3168 | 		return ISCSI_TMF_RSP_NOT_SUPPORTED; | 
 | 3169 | 	case TMR_FUNCTION_AUTHORIZATION_FAILED: | 
 | 3170 | 		return ISCSI_TMF_RSP_AUTH_FAILED; | 
 | 3171 | 	case TMR_FUNCTION_REJECTED: | 
 | 3172 | 	default: | 
 | 3173 | 		return ISCSI_TMF_RSP_REJECTED; | 
 | 3174 | 	} | 
 | 3175 | } | 
 | 3176 |  | 
 | 3177 | static int iscsit_send_task_mgt_rsp( | 
 | 3178 | 	struct iscsi_cmd *cmd, | 
 | 3179 | 	struct iscsi_conn *conn) | 
 | 3180 | { | 
 | 3181 | 	struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req; | 
 | 3182 | 	struct iscsi_tm_rsp *hdr; | 
 | 3183 | 	u32 tx_size = 0; | 
 | 3184 |  | 
 | 3185 | 	hdr			= (struct iscsi_tm_rsp *) cmd->pdu; | 
 | 3186 | 	memset(hdr, 0, ISCSI_HDR_LEN); | 
 | 3187 | 	hdr->opcode		= ISCSI_OP_SCSI_TMFUNC_RSP; | 
| Nicholas Bellinger | 7ae0b10 | 2011-11-27 22:25:14 -0800 | [diff] [blame] | 3188 | 	hdr->flags		= ISCSI_FLAG_CMD_FINAL; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3189 | 	hdr->response		= iscsit_convert_tcm_tmr_rsp(se_tmr); | 
 | 3190 | 	hdr->itt		= cpu_to_be32(cmd->init_task_tag); | 
 | 3191 | 	cmd->stat_sn		= conn->stat_sn++; | 
 | 3192 | 	hdr->statsn		= cpu_to_be32(cmd->stat_sn); | 
 | 3193 |  | 
 | 3194 | 	iscsit_increment_maxcmdsn(cmd, conn->sess); | 
 | 3195 | 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn); | 
 | 3196 | 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn); | 
 | 3197 |  | 
 | 3198 | 	cmd->iov_misc[0].iov_base	= cmd->pdu; | 
 | 3199 | 	cmd->iov_misc[0].iov_len	= ISCSI_HDR_LEN; | 
 | 3200 | 	tx_size += ISCSI_HDR_LEN; | 
 | 3201 |  | 
 | 3202 | 	if (conn->conn_ops->HeaderDigest) { | 
 | 3203 | 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; | 
 | 3204 |  | 
 | 3205 | 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, | 
 | 3206 | 				(unsigned char *)hdr, ISCSI_HDR_LEN, | 
 | 3207 | 				0, NULL, (u8 *)header_digest); | 
 | 3208 |  | 
 | 3209 | 		cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN; | 
 | 3210 | 		tx_size += ISCSI_CRC_LEN; | 
 | 3211 | 		pr_debug("Attaching CRC32 HeaderDigest for Task" | 
 | 3212 | 			" Mgmt Response PDU 0x%08x\n", *header_digest); | 
 | 3213 | 	} | 
 | 3214 |  | 
 | 3215 | 	cmd->iov_misc_count = 1; | 
 | 3216 | 	cmd->tx_size = tx_size; | 
 | 3217 |  | 
 | 3218 | 	pr_debug("Built Task Management Response ITT: 0x%08x," | 
 | 3219 | 		" StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n", | 
 | 3220 | 		cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid); | 
 | 3221 |  | 
 | 3222 | 	return 0; | 
 | 3223 | } | 
 | 3224 |  | 
| Nicholas Bellinger | 2f9bc89 | 2012-01-16 23:33:48 -0800 | [diff] [blame] | 3225 | static bool iscsit_check_inaddr_any(struct iscsi_np *np) | 
 | 3226 | { | 
 | 3227 | 	bool ret = false; | 
 | 3228 |  | 
 | 3229 | 	if (np->np_sockaddr.ss_family == AF_INET6) { | 
 | 3230 | 		const struct sockaddr_in6 sin6 = { | 
 | 3231 | 			.sin6_addr = IN6ADDR_ANY_INIT }; | 
 | 3232 | 		struct sockaddr_in6 *sock_in6 = | 
 | 3233 | 			 (struct sockaddr_in6 *)&np->np_sockaddr; | 
 | 3234 |  | 
 | 3235 | 		if (!memcmp(sock_in6->sin6_addr.s6_addr, | 
 | 3236 | 				sin6.sin6_addr.s6_addr, 16)) | 
 | 3237 | 			ret = true; | 
 | 3238 | 	} else { | 
 | 3239 | 		struct sockaddr_in * sock_in = | 
 | 3240 | 			(struct sockaddr_in *)&np->np_sockaddr; | 
 | 3241 |  | 
 | 3242 | 		if (sock_in->sin_addr.s_addr == INADDR_ANY) | 
 | 3243 | 			ret = true; | 
 | 3244 | 	} | 
 | 3245 |  | 
 | 3246 | 	return ret; | 
 | 3247 | } | 
 | 3248 |  | 
| Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3249 | #define SENDTARGETS_BUF_LIMIT 32768U | 
 | 3250 |  | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3251 | static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd) | 
 | 3252 | { | 
 | 3253 | 	char *payload = NULL; | 
 | 3254 | 	struct iscsi_conn *conn = cmd->conn; | 
 | 3255 | 	struct iscsi_portal_group *tpg; | 
 | 3256 | 	struct iscsi_tiqn *tiqn; | 
 | 3257 | 	struct iscsi_tpg_np *tpg_np; | 
 | 3258 | 	int buffer_len, end_of_buf = 0, len = 0, payload_len = 0; | 
| Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3259 | 	unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */ | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3260 |  | 
| Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3261 | 	buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength, | 
 | 3262 | 			 SENDTARGETS_BUF_LIMIT); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3263 |  | 
 | 3264 | 	payload = kzalloc(buffer_len, GFP_KERNEL); | 
 | 3265 | 	if (!payload) { | 
 | 3266 | 		pr_err("Unable to allocate memory for sendtargets" | 
 | 3267 | 				" response.\n"); | 
 | 3268 | 		return -ENOMEM; | 
 | 3269 | 	} | 
 | 3270 |  | 
 | 3271 | 	spin_lock(&tiqn_lock); | 
 | 3272 | 	list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) { | 
 | 3273 | 		len = sprintf(buf, "TargetName=%s", tiqn->tiqn); | 
 | 3274 | 		len += 1; | 
 | 3275 |  | 
 | 3276 | 		if ((len + payload_len) > buffer_len) { | 
 | 3277 | 			spin_unlock(&tiqn->tiqn_tpg_lock); | 
 | 3278 | 			end_of_buf = 1; | 
 | 3279 | 			goto eob; | 
 | 3280 | 		} | 
| Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 3281 | 		memcpy(payload + payload_len, buf, len); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3282 | 		payload_len += len; | 
 | 3283 |  | 
 | 3284 | 		spin_lock(&tiqn->tiqn_tpg_lock); | 
 | 3285 | 		list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) { | 
 | 3286 |  | 
 | 3287 | 			spin_lock(&tpg->tpg_state_lock); | 
 | 3288 | 			if ((tpg->tpg_state == TPG_STATE_FREE) || | 
 | 3289 | 			    (tpg->tpg_state == TPG_STATE_INACTIVE)) { | 
 | 3290 | 				spin_unlock(&tpg->tpg_state_lock); | 
 | 3291 | 				continue; | 
 | 3292 | 			} | 
 | 3293 | 			spin_unlock(&tpg->tpg_state_lock); | 
 | 3294 |  | 
 | 3295 | 			spin_lock(&tpg->tpg_np_lock); | 
 | 3296 | 			list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, | 
 | 3297 | 						tpg_np_list) { | 
| Nicholas Bellinger | 2f9bc89 | 2012-01-16 23:33:48 -0800 | [diff] [blame] | 3298 | 				struct iscsi_np *np = tpg_np->tpg_np; | 
 | 3299 | 				bool inaddr_any = iscsit_check_inaddr_any(np); | 
 | 3300 |  | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3301 | 				len = sprintf(buf, "TargetAddress=" | 
 | 3302 | 					"%s%s%s:%hu,%hu", | 
| Nicholas Bellinger | 2f9bc89 | 2012-01-16 23:33:48 -0800 | [diff] [blame] | 3303 | 					(np->np_sockaddr.ss_family == AF_INET6) ? | 
 | 3304 | 					"[" : "", (inaddr_any == false) ? | 
 | 3305 | 						np->np_ip : conn->local_ip, | 
 | 3306 | 					(np->np_sockaddr.ss_family == AF_INET6) ? | 
 | 3307 | 					"]" : "", (inaddr_any == false) ? | 
 | 3308 | 						np->np_port : conn->local_port, | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3309 | 					tpg->tpgt); | 
 | 3310 | 				len += 1; | 
 | 3311 |  | 
 | 3312 | 				if ((len + payload_len) > buffer_len) { | 
 | 3313 | 					spin_unlock(&tpg->tpg_np_lock); | 
 | 3314 | 					spin_unlock(&tiqn->tiqn_tpg_lock); | 
 | 3315 | 					end_of_buf = 1; | 
 | 3316 | 					goto eob; | 
 | 3317 | 				} | 
| Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 3318 | 				memcpy(payload + payload_len, buf, len); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3319 | 				payload_len += len; | 
 | 3320 | 			} | 
 | 3321 | 			spin_unlock(&tpg->tpg_np_lock); | 
 | 3322 | 		} | 
 | 3323 | 		spin_unlock(&tiqn->tiqn_tpg_lock); | 
 | 3324 | eob: | 
 | 3325 | 		if (end_of_buf) | 
 | 3326 | 			break; | 
 | 3327 | 	} | 
 | 3328 | 	spin_unlock(&tiqn_lock); | 
 | 3329 |  | 
 | 3330 | 	cmd->buf_ptr = payload; | 
 | 3331 |  | 
 | 3332 | 	return payload_len; | 
 | 3333 | } | 
 | 3334 |  | 
 | 3335 | /* | 
 | 3336 |  *	FIXME: Add support for F_BIT and C_BIT when the length is longer than | 
 | 3337 |  *	MaxRecvDataSegmentLength. | 
 | 3338 |  */ | 
 | 3339 | static int iscsit_send_text_rsp( | 
 | 3340 | 	struct iscsi_cmd *cmd, | 
 | 3341 | 	struct iscsi_conn *conn) | 
 | 3342 | { | 
 | 3343 | 	struct iscsi_text_rsp *hdr; | 
 | 3344 | 	struct kvec *iov; | 
 | 3345 | 	u32 padding = 0, tx_size = 0; | 
 | 3346 | 	int text_length, iov_count = 0; | 
 | 3347 |  | 
 | 3348 | 	text_length = iscsit_build_sendtargets_response(cmd); | 
 | 3349 | 	if (text_length < 0) | 
 | 3350 | 		return text_length; | 
 | 3351 |  | 
 | 3352 | 	padding = ((-text_length) & 3); | 
 | 3353 | 	if (padding != 0) { | 
 | 3354 | 		memset(cmd->buf_ptr + text_length, 0, padding); | 
 | 3355 | 		pr_debug("Attaching %u additional bytes for" | 
 | 3356 | 			" padding.\n", padding); | 
 | 3357 | 	} | 
 | 3358 |  | 
 | 3359 | 	hdr			= (struct iscsi_text_rsp *) cmd->pdu; | 
 | 3360 | 	memset(hdr, 0, ISCSI_HDR_LEN); | 
 | 3361 | 	hdr->opcode		= ISCSI_OP_TEXT_RSP; | 
 | 3362 | 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL; | 
 | 3363 | 	hton24(hdr->dlength, text_length); | 
 | 3364 | 	hdr->itt		= cpu_to_be32(cmd->init_task_tag); | 
 | 3365 | 	hdr->ttt		= cpu_to_be32(cmd->targ_xfer_tag); | 
 | 3366 | 	cmd->stat_sn		= conn->stat_sn++; | 
 | 3367 | 	hdr->statsn		= cpu_to_be32(cmd->stat_sn); | 
 | 3368 |  | 
 | 3369 | 	iscsit_increment_maxcmdsn(cmd, conn->sess); | 
 | 3370 | 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn); | 
 | 3371 | 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn); | 
 | 3372 |  | 
 | 3373 | 	iov = &cmd->iov_misc[0]; | 
 | 3374 |  | 
 | 3375 | 	iov[iov_count].iov_base = cmd->pdu; | 
 | 3376 | 	iov[iov_count++].iov_len = ISCSI_HDR_LEN; | 
 | 3377 | 	iov[iov_count].iov_base	= cmd->buf_ptr; | 
 | 3378 | 	iov[iov_count++].iov_len = text_length + padding; | 
 | 3379 |  | 
 | 3380 | 	tx_size += (ISCSI_HDR_LEN + text_length + padding); | 
 | 3381 |  | 
 | 3382 | 	if (conn->conn_ops->HeaderDigest) { | 
 | 3383 | 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; | 
 | 3384 |  | 
 | 3385 | 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, | 
 | 3386 | 				(unsigned char *)hdr, ISCSI_HDR_LEN, | 
 | 3387 | 				0, NULL, (u8 *)header_digest); | 
 | 3388 |  | 
 | 3389 | 		iov[0].iov_len += ISCSI_CRC_LEN; | 
 | 3390 | 		tx_size += ISCSI_CRC_LEN; | 
 | 3391 | 		pr_debug("Attaching CRC32 HeaderDigest for" | 
 | 3392 | 			" Text Response PDU 0x%08x\n", *header_digest); | 
 | 3393 | 	} | 
 | 3394 |  | 
 | 3395 | 	if (conn->conn_ops->DataDigest) { | 
 | 3396 | 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, | 
 | 3397 | 				cmd->buf_ptr, (text_length + padding), | 
 | 3398 | 				0, NULL, (u8 *)&cmd->data_crc); | 
 | 3399 |  | 
 | 3400 | 		iov[iov_count].iov_base	= &cmd->data_crc; | 
 | 3401 | 		iov[iov_count++].iov_len = ISCSI_CRC_LEN; | 
 | 3402 | 		tx_size	+= ISCSI_CRC_LEN; | 
 | 3403 |  | 
 | 3404 | 		pr_debug("Attaching DataDigest for %u bytes of text" | 
 | 3405 | 			" data, CRC 0x%08x\n", (text_length + padding), | 
 | 3406 | 			cmd->data_crc); | 
 | 3407 | 	} | 
 | 3408 |  | 
 | 3409 | 	cmd->iov_misc_count = iov_count; | 
 | 3410 | 	cmd->tx_size = tx_size; | 
 | 3411 |  | 
 | 3412 | 	pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x," | 
 | 3413 | 		" Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn, | 
 | 3414 | 			text_length, conn->cid); | 
 | 3415 | 	return 0; | 
 | 3416 | } | 
 | 3417 |  | 
 | 3418 | static int iscsit_send_reject( | 
 | 3419 | 	struct iscsi_cmd *cmd, | 
 | 3420 | 	struct iscsi_conn *conn) | 
 | 3421 | { | 
 | 3422 | 	u32 iov_count = 0, tx_size = 0; | 
 | 3423 | 	struct iscsi_reject *hdr; | 
 | 3424 | 	struct kvec *iov; | 
 | 3425 |  | 
 | 3426 | 	hdr			= (struct iscsi_reject *) cmd->pdu; | 
 | 3427 | 	hdr->opcode		= ISCSI_OP_REJECT; | 
 | 3428 | 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL; | 
 | 3429 | 	hton24(hdr->dlength, ISCSI_HDR_LEN); | 
| Nicholas Bellinger | f25590f | 2012-09-22 17:21:06 -0700 | [diff] [blame] | 3430 | 	hdr->ffffffff		= 0xffffffff; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3431 | 	cmd->stat_sn		= conn->stat_sn++; | 
 | 3432 | 	hdr->statsn		= cpu_to_be32(cmd->stat_sn); | 
 | 3433 | 	hdr->exp_cmdsn	= cpu_to_be32(conn->sess->exp_cmd_sn); | 
 | 3434 | 	hdr->max_cmdsn	= cpu_to_be32(conn->sess->max_cmd_sn); | 
 | 3435 |  | 
 | 3436 | 	iov = &cmd->iov_misc[0]; | 
 | 3437 |  | 
 | 3438 | 	iov[iov_count].iov_base = cmd->pdu; | 
 | 3439 | 	iov[iov_count++].iov_len = ISCSI_HDR_LEN; | 
 | 3440 | 	iov[iov_count].iov_base = cmd->buf_ptr; | 
 | 3441 | 	iov[iov_count++].iov_len = ISCSI_HDR_LEN; | 
 | 3442 |  | 
 | 3443 | 	tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN); | 
 | 3444 |  | 
 | 3445 | 	if (conn->conn_ops->HeaderDigest) { | 
 | 3446 | 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; | 
 | 3447 |  | 
 | 3448 | 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, | 
 | 3449 | 				(unsigned char *)hdr, ISCSI_HDR_LEN, | 
 | 3450 | 				0, NULL, (u8 *)header_digest); | 
 | 3451 |  | 
 | 3452 | 		iov[0].iov_len += ISCSI_CRC_LEN; | 
 | 3453 | 		tx_size += ISCSI_CRC_LEN; | 
 | 3454 | 		pr_debug("Attaching CRC32 HeaderDigest for" | 
 | 3455 | 			" REJECT PDU 0x%08x\n", *header_digest); | 
 | 3456 | 	} | 
 | 3457 |  | 
 | 3458 | 	if (conn->conn_ops->DataDigest) { | 
 | 3459 | 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, | 
 | 3460 | 				(unsigned char *)cmd->buf_ptr, ISCSI_HDR_LEN, | 
 | 3461 | 				0, NULL, (u8 *)&cmd->data_crc); | 
 | 3462 |  | 
 | 3463 | 		iov[iov_count].iov_base = &cmd->data_crc; | 
 | 3464 | 		iov[iov_count++].iov_len  = ISCSI_CRC_LEN; | 
 | 3465 | 		tx_size += ISCSI_CRC_LEN; | 
 | 3466 | 		pr_debug("Attaching CRC32 DataDigest for REJECT" | 
 | 3467 | 				" PDU 0x%08x\n", cmd->data_crc); | 
 | 3468 | 	} | 
 | 3469 |  | 
 | 3470 | 	cmd->iov_misc_count = iov_count; | 
 | 3471 | 	cmd->tx_size = tx_size; | 
 | 3472 |  | 
 | 3473 | 	pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x," | 
 | 3474 | 		" CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid); | 
 | 3475 |  | 
 | 3476 | 	return 0; | 
 | 3477 | } | 
 | 3478 |  | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3479 | void iscsit_thread_get_cpumask(struct iscsi_conn *conn) | 
 | 3480 | { | 
 | 3481 | 	struct iscsi_thread_set *ts = conn->thread_set; | 
 | 3482 | 	int ord, cpu; | 
 | 3483 | 	/* | 
 | 3484 | 	 * thread_id is assigned from iscsit_global->ts_bitmap from | 
 | 3485 | 	 * within iscsi_thread_set.c:iscsi_allocate_thread_sets() | 
 | 3486 | 	 * | 
 | 3487 | 	 * Here we use thread_id to determine which CPU that this | 
 | 3488 | 	 * iSCSI connection's iscsi_thread_set will be scheduled to | 
 | 3489 | 	 * execute upon. | 
 | 3490 | 	 */ | 
 | 3491 | 	ord = ts->thread_id % cpumask_weight(cpu_online_mask); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3492 | 	for_each_online_cpu(cpu) { | 
 | 3493 | 		if (ord-- == 0) { | 
 | 3494 | 			cpumask_set_cpu(cpu, conn->conn_cpumask); | 
 | 3495 | 			return; | 
 | 3496 | 		} | 
 | 3497 | 	} | 
 | 3498 | 	/* | 
 | 3499 | 	 * This should never be reached.. | 
 | 3500 | 	 */ | 
 | 3501 | 	dump_stack(); | 
 | 3502 | 	cpumask_setall(conn->conn_cpumask); | 
 | 3503 | } | 
 | 3504 |  | 
 | 3505 | static inline void iscsit_thread_check_cpumask( | 
 | 3506 | 	struct iscsi_conn *conn, | 
 | 3507 | 	struct task_struct *p, | 
 | 3508 | 	int mode) | 
 | 3509 | { | 
 | 3510 | 	char buf[128]; | 
 | 3511 | 	/* | 
 | 3512 | 	 * mode == 1 signals iscsi_target_tx_thread() usage. | 
 | 3513 | 	 * mode == 0 signals iscsi_target_rx_thread() usage. | 
 | 3514 | 	 */ | 
 | 3515 | 	if (mode == 1) { | 
 | 3516 | 		if (!conn->conn_tx_reset_cpumask) | 
 | 3517 | 			return; | 
 | 3518 | 		conn->conn_tx_reset_cpumask = 0; | 
 | 3519 | 	} else { | 
 | 3520 | 		if (!conn->conn_rx_reset_cpumask) | 
 | 3521 | 			return; | 
 | 3522 | 		conn->conn_rx_reset_cpumask = 0; | 
 | 3523 | 	} | 
 | 3524 | 	/* | 
 | 3525 | 	 * Update the CPU mask for this single kthread so that | 
 | 3526 | 	 * both TX and RX kthreads are scheduled to run on the | 
 | 3527 | 	 * same CPU. | 
 | 3528 | 	 */ | 
 | 3529 | 	memset(buf, 0, 128); | 
 | 3530 | 	cpumask_scnprintf(buf, 128, conn->conn_cpumask); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3531 | 	set_cpus_allowed_ptr(p, conn->conn_cpumask); | 
 | 3532 | } | 
 | 3533 |  | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3534 | static int handle_immediate_queue(struct iscsi_conn *conn) | 
 | 3535 | { | 
 | 3536 | 	struct iscsi_queue_req *qr; | 
 | 3537 | 	struct iscsi_cmd *cmd; | 
 | 3538 | 	u8 state; | 
 | 3539 | 	int ret; | 
 | 3540 |  | 
 | 3541 | 	while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) { | 
 | 3542 | 		atomic_set(&conn->check_immediate_queue, 0); | 
 | 3543 | 		cmd = qr->cmd; | 
 | 3544 | 		state = qr->state; | 
 | 3545 | 		kmem_cache_free(lio_qr_cache, qr); | 
 | 3546 |  | 
 | 3547 | 		switch (state) { | 
 | 3548 | 		case ISTATE_SEND_R2T: | 
 | 3549 | 			ret = iscsit_send_r2t(cmd, conn); | 
 | 3550 | 			if (ret < 0) | 
 | 3551 | 				goto err; | 
 | 3552 | 			break; | 
 | 3553 | 		case ISTATE_REMOVE: | 
 | 3554 | 			if (cmd->data_direction == DMA_TO_DEVICE) | 
 | 3555 | 				iscsit_stop_dataout_timer(cmd); | 
 | 3556 |  | 
 | 3557 | 			spin_lock_bh(&conn->cmd_lock); | 
 | 3558 | 			list_del(&cmd->i_conn_node); | 
 | 3559 | 			spin_unlock_bh(&conn->cmd_lock); | 
 | 3560 |  | 
 | 3561 | 			iscsit_free_cmd(cmd); | 
 | 3562 | 			continue; | 
 | 3563 | 		case ISTATE_SEND_NOPIN_WANT_RESPONSE: | 
 | 3564 | 			iscsit_mod_nopin_response_timer(conn); | 
 | 3565 | 			ret = iscsit_send_unsolicited_nopin(cmd, | 
 | 3566 | 							    conn, 1); | 
 | 3567 | 			if (ret < 0) | 
 | 3568 | 				goto err; | 
 | 3569 | 			break; | 
 | 3570 | 		case ISTATE_SEND_NOPIN_NO_RESPONSE: | 
 | 3571 | 			ret = iscsit_send_unsolicited_nopin(cmd, | 
 | 3572 | 							    conn, 0); | 
 | 3573 | 			if (ret < 0) | 
 | 3574 | 				goto err; | 
 | 3575 | 			break; | 
 | 3576 | 		default: | 
 | 3577 | 			pr_err("Unknown Opcode: 0x%02x ITT:" | 
 | 3578 | 			       " 0x%08x, i_state: %d on CID: %hu\n", | 
 | 3579 | 			       cmd->iscsi_opcode, cmd->init_task_tag, state, | 
 | 3580 | 			       conn->cid); | 
 | 3581 | 			goto err; | 
 | 3582 | 		} | 
 | 3583 | 	} | 
 | 3584 |  | 
 | 3585 | 	return 0; | 
 | 3586 |  | 
 | 3587 | err: | 
 | 3588 | 	return -1; | 
 | 3589 | } | 
 | 3590 |  | 
 | 3591 | static int handle_response_queue(struct iscsi_conn *conn) | 
 | 3592 | { | 
 | 3593 | 	struct iscsi_queue_req *qr; | 
 | 3594 | 	struct iscsi_cmd *cmd; | 
 | 3595 | 	u8 state; | 
 | 3596 | 	int ret; | 
 | 3597 |  | 
 | 3598 | 	while ((qr = iscsit_get_cmd_from_response_queue(conn))) { | 
 | 3599 | 		cmd = qr->cmd; | 
 | 3600 | 		state = qr->state; | 
 | 3601 | 		kmem_cache_free(lio_qr_cache, qr); | 
 | 3602 |  | 
 | 3603 | check_rsp_state: | 
 | 3604 | 		switch (state) { | 
 | 3605 | 		case ISTATE_SEND_DATAIN: | 
 | 3606 | 			ret = iscsit_send_data_in(cmd, conn); | 
 | 3607 | 			if (ret < 0) | 
 | 3608 | 				goto err; | 
 | 3609 | 			else if (!ret) | 
 | 3610 | 				/* more drs */ | 
 | 3611 | 				goto check_rsp_state; | 
 | 3612 | 			else if (ret == 1) { | 
 | 3613 | 				/* all done */ | 
 | 3614 | 				spin_lock_bh(&cmd->istate_lock); | 
 | 3615 | 				cmd->i_state = ISTATE_SENT_STATUS; | 
 | 3616 | 				spin_unlock_bh(&cmd->istate_lock); | 
 | 3617 | 				continue; | 
 | 3618 | 			} else if (ret == 2) { | 
 | 3619 | 				/* Still must send status, | 
 | 3620 | 				   SCF_TRANSPORT_TASK_SENSE was set */ | 
 | 3621 | 				spin_lock_bh(&cmd->istate_lock); | 
 | 3622 | 				cmd->i_state = ISTATE_SEND_STATUS; | 
 | 3623 | 				spin_unlock_bh(&cmd->istate_lock); | 
 | 3624 | 				state = ISTATE_SEND_STATUS; | 
 | 3625 | 				goto check_rsp_state; | 
 | 3626 | 			} | 
 | 3627 |  | 
 | 3628 | 			break; | 
 | 3629 | 		case ISTATE_SEND_STATUS: | 
 | 3630 | 		case ISTATE_SEND_STATUS_RECOVERY: | 
 | 3631 | 			ret = iscsit_send_status(cmd, conn); | 
 | 3632 | 			break; | 
 | 3633 | 		case ISTATE_SEND_LOGOUTRSP: | 
 | 3634 | 			ret = iscsit_send_logout_response(cmd, conn); | 
 | 3635 | 			break; | 
 | 3636 | 		case ISTATE_SEND_ASYNCMSG: | 
 | 3637 | 			ret = iscsit_send_conn_drop_async_message( | 
 | 3638 | 				cmd, conn); | 
 | 3639 | 			break; | 
 | 3640 | 		case ISTATE_SEND_NOPIN: | 
 | 3641 | 			ret = iscsit_send_nopin_response(cmd, conn); | 
 | 3642 | 			break; | 
 | 3643 | 		case ISTATE_SEND_REJECT: | 
 | 3644 | 			ret = iscsit_send_reject(cmd, conn); | 
 | 3645 | 			break; | 
 | 3646 | 		case ISTATE_SEND_TASKMGTRSP: | 
 | 3647 | 			ret = iscsit_send_task_mgt_rsp(cmd, conn); | 
 | 3648 | 			if (ret != 0) | 
 | 3649 | 				break; | 
 | 3650 | 			ret = iscsit_tmr_post_handler(cmd, conn); | 
 | 3651 | 			if (ret != 0) | 
 | 3652 | 				iscsit_fall_back_to_erl0(conn->sess); | 
 | 3653 | 			break; | 
 | 3654 | 		case ISTATE_SEND_TEXTRSP: | 
 | 3655 | 			ret = iscsit_send_text_rsp(cmd, conn); | 
 | 3656 | 			break; | 
 | 3657 | 		default: | 
 | 3658 | 			pr_err("Unknown Opcode: 0x%02x ITT:" | 
 | 3659 | 			       " 0x%08x, i_state: %d on CID: %hu\n", | 
 | 3660 | 			       cmd->iscsi_opcode, cmd->init_task_tag, | 
 | 3661 | 			       state, conn->cid); | 
 | 3662 | 			goto err; | 
 | 3663 | 		} | 
 | 3664 | 		if (ret < 0) | 
 | 3665 | 			goto err; | 
 | 3666 |  | 
 | 3667 | 		if (iscsit_send_tx_data(cmd, conn, 1) < 0) { | 
 | 3668 | 			iscsit_tx_thread_wait_for_tcp(conn); | 
 | 3669 | 			iscsit_unmap_iovec(cmd); | 
 | 3670 | 			goto err; | 
 | 3671 | 		} | 
 | 3672 | 		iscsit_unmap_iovec(cmd); | 
 | 3673 |  | 
 | 3674 | 		switch (state) { | 
 | 3675 | 		case ISTATE_SEND_LOGOUTRSP: | 
 | 3676 | 			if (!iscsit_logout_post_handler(cmd, conn)) | 
 | 3677 | 				goto restart; | 
 | 3678 | 			/* fall through */ | 
 | 3679 | 		case ISTATE_SEND_STATUS: | 
 | 3680 | 		case ISTATE_SEND_ASYNCMSG: | 
 | 3681 | 		case ISTATE_SEND_NOPIN: | 
 | 3682 | 		case ISTATE_SEND_STATUS_RECOVERY: | 
 | 3683 | 		case ISTATE_SEND_TEXTRSP: | 
 | 3684 | 		case ISTATE_SEND_TASKMGTRSP: | 
 | 3685 | 			spin_lock_bh(&cmd->istate_lock); | 
 | 3686 | 			cmd->i_state = ISTATE_SENT_STATUS; | 
 | 3687 | 			spin_unlock_bh(&cmd->istate_lock); | 
 | 3688 | 			break; | 
 | 3689 | 		case ISTATE_SEND_REJECT: | 
 | 3690 | 			if (cmd->cmd_flags & ICF_REJECT_FAIL_CONN) { | 
 | 3691 | 				cmd->cmd_flags &= ~ICF_REJECT_FAIL_CONN; | 
 | 3692 | 				complete(&cmd->reject_comp); | 
 | 3693 | 				goto err; | 
 | 3694 | 			} | 
 | 3695 | 			complete(&cmd->reject_comp); | 
 | 3696 | 			break; | 
 | 3697 | 		default: | 
 | 3698 | 			pr_err("Unknown Opcode: 0x%02x ITT:" | 
 | 3699 | 			       " 0x%08x, i_state: %d on CID: %hu\n", | 
 | 3700 | 			       cmd->iscsi_opcode, cmd->init_task_tag, | 
 | 3701 | 			       cmd->i_state, conn->cid); | 
 | 3702 | 			goto err; | 
 | 3703 | 		} | 
 | 3704 |  | 
 | 3705 | 		if (atomic_read(&conn->check_immediate_queue)) | 
 | 3706 | 			break; | 
 | 3707 | 	} | 
 | 3708 |  | 
 | 3709 | 	return 0; | 
 | 3710 |  | 
 | 3711 | err: | 
 | 3712 | 	return -1; | 
 | 3713 | restart: | 
 | 3714 | 	return -EAGAIN; | 
 | 3715 | } | 
 | 3716 |  | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3717 | int iscsi_target_tx_thread(void *arg) | 
 | 3718 | { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3719 | 	int ret = 0; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3720 | 	struct iscsi_conn *conn; | 
| Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 3721 | 	struct iscsi_thread_set *ts = arg; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3722 | 	/* | 
 | 3723 | 	 * Allow ourselves to be interrupted by SIGINT so that a | 
 | 3724 | 	 * connection recovery / failure event can be triggered externally. | 
 | 3725 | 	 */ | 
 | 3726 | 	allow_signal(SIGINT); | 
 | 3727 |  | 
 | 3728 | restart: | 
 | 3729 | 	conn = iscsi_tx_thread_pre_handler(ts); | 
 | 3730 | 	if (!conn) | 
 | 3731 | 		goto out; | 
 | 3732 |  | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3733 | 	ret = 0; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3734 |  | 
 | 3735 | 	while (!kthread_should_stop()) { | 
 | 3736 | 		/* | 
 | 3737 | 		 * Ensure that both TX and RX per connection kthreads | 
 | 3738 | 		 * are scheduled to run on the same CPU. | 
 | 3739 | 		 */ | 
 | 3740 | 		iscsit_thread_check_cpumask(conn, current, 1); | 
 | 3741 |  | 
 | 3742 | 		schedule_timeout_interruptible(MAX_SCHEDULE_TIMEOUT); | 
 | 3743 |  | 
 | 3744 | 		if ((ts->status == ISCSI_THREAD_SET_RESET) || | 
 | 3745 | 		     signal_pending(current)) | 
 | 3746 | 			goto transport_err; | 
 | 3747 |  | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3748 | 		ret = handle_immediate_queue(conn); | 
 | 3749 | 		if (ret < 0) | 
 | 3750 | 			goto transport_err; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3751 |  | 
| Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3752 | 		ret = handle_response_queue(conn); | 
 | 3753 | 		if (ret == -EAGAIN) | 
 | 3754 | 			goto restart; | 
 | 3755 | 		else if (ret < 0) | 
 | 3756 | 			goto transport_err; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3757 | 	} | 
 | 3758 |  | 
 | 3759 | transport_err: | 
 | 3760 | 	iscsit_take_action_for_connection_exit(conn); | 
 | 3761 | 	goto restart; | 
 | 3762 | out: | 
 | 3763 | 	return 0; | 
 | 3764 | } | 
 | 3765 |  | 
 | 3766 | int iscsi_target_rx_thread(void *arg) | 
 | 3767 | { | 
 | 3768 | 	int ret; | 
 | 3769 | 	u8 buffer[ISCSI_HDR_LEN], opcode; | 
 | 3770 | 	u32 checksum = 0, digest = 0; | 
 | 3771 | 	struct iscsi_conn *conn = NULL; | 
| Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 3772 | 	struct iscsi_thread_set *ts = arg; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3773 | 	struct kvec iov; | 
 | 3774 | 	/* | 
 | 3775 | 	 * Allow ourselves to be interrupted by SIGINT so that a | 
 | 3776 | 	 * connection recovery / failure event can be triggered externally. | 
 | 3777 | 	 */ | 
 | 3778 | 	allow_signal(SIGINT); | 
 | 3779 |  | 
 | 3780 | restart: | 
 | 3781 | 	conn = iscsi_rx_thread_pre_handler(ts); | 
 | 3782 | 	if (!conn) | 
 | 3783 | 		goto out; | 
 | 3784 |  | 
 | 3785 | 	while (!kthread_should_stop()) { | 
 | 3786 | 		/* | 
 | 3787 | 		 * Ensure that both TX and RX per connection kthreads | 
 | 3788 | 		 * are scheduled to run on the same CPU. | 
 | 3789 | 		 */ | 
 | 3790 | 		iscsit_thread_check_cpumask(conn, current, 0); | 
 | 3791 |  | 
 | 3792 | 		memset(buffer, 0, ISCSI_HDR_LEN); | 
 | 3793 | 		memset(&iov, 0, sizeof(struct kvec)); | 
 | 3794 |  | 
 | 3795 | 		iov.iov_base	= buffer; | 
 | 3796 | 		iov.iov_len	= ISCSI_HDR_LEN; | 
 | 3797 |  | 
 | 3798 | 		ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN); | 
 | 3799 | 		if (ret != ISCSI_HDR_LEN) { | 
 | 3800 | 			iscsit_rx_thread_wait_for_tcp(conn); | 
 | 3801 | 			goto transport_err; | 
 | 3802 | 		} | 
 | 3803 |  | 
 | 3804 | 		/* | 
 | 3805 | 		 * Set conn->bad_hdr for use with REJECT PDUs. | 
 | 3806 | 		 */ | 
 | 3807 | 		memcpy(&conn->bad_hdr, &buffer, ISCSI_HDR_LEN); | 
 | 3808 |  | 
 | 3809 | 		if (conn->conn_ops->HeaderDigest) { | 
 | 3810 | 			iov.iov_base	= &digest; | 
 | 3811 | 			iov.iov_len	= ISCSI_CRC_LEN; | 
 | 3812 |  | 
 | 3813 | 			ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN); | 
 | 3814 | 			if (ret != ISCSI_CRC_LEN) { | 
 | 3815 | 				iscsit_rx_thread_wait_for_tcp(conn); | 
 | 3816 | 				goto transport_err; | 
 | 3817 | 			} | 
 | 3818 |  | 
 | 3819 | 			iscsit_do_crypto_hash_buf(&conn->conn_rx_hash, | 
 | 3820 | 					buffer, ISCSI_HDR_LEN, | 
 | 3821 | 					0, NULL, (u8 *)&checksum); | 
 | 3822 |  | 
 | 3823 | 			if (digest != checksum) { | 
 | 3824 | 				pr_err("HeaderDigest CRC32C failed," | 
 | 3825 | 					" received 0x%08x, computed 0x%08x\n", | 
 | 3826 | 					digest, checksum); | 
 | 3827 | 				/* | 
 | 3828 | 				 * Set the PDU to 0xff so it will intentionally | 
 | 3829 | 				 * hit default in the switch below. | 
 | 3830 | 				 */ | 
 | 3831 | 				memset(buffer, 0xff, ISCSI_HDR_LEN); | 
 | 3832 | 				spin_lock_bh(&conn->sess->session_stats_lock); | 
 | 3833 | 				conn->sess->conn_digest_errors++; | 
 | 3834 | 				spin_unlock_bh(&conn->sess->session_stats_lock); | 
 | 3835 | 			} else { | 
 | 3836 | 				pr_debug("Got HeaderDigest CRC32C" | 
 | 3837 | 						" 0x%08x\n", checksum); | 
 | 3838 | 			} | 
 | 3839 | 		} | 
 | 3840 |  | 
 | 3841 | 		if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT) | 
 | 3842 | 			goto transport_err; | 
 | 3843 |  | 
 | 3844 | 		opcode = buffer[0] & ISCSI_OPCODE_MASK; | 
 | 3845 |  | 
 | 3846 | 		if (conn->sess->sess_ops->SessionType && | 
 | 3847 | 		   ((!(opcode & ISCSI_OP_TEXT)) || | 
 | 3848 | 		    (!(opcode & ISCSI_OP_LOGOUT)))) { | 
 | 3849 | 			pr_err("Received illegal iSCSI Opcode: 0x%02x" | 
 | 3850 | 			" while in Discovery Session, rejecting.\n", opcode); | 
 | 3851 | 			iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1, | 
 | 3852 | 					buffer, conn); | 
 | 3853 | 			goto transport_err; | 
 | 3854 | 		} | 
 | 3855 |  | 
 | 3856 | 		switch (opcode) { | 
 | 3857 | 		case ISCSI_OP_SCSI_CMD: | 
 | 3858 | 			if (iscsit_handle_scsi_cmd(conn, buffer) < 0) | 
 | 3859 | 				goto transport_err; | 
 | 3860 | 			break; | 
 | 3861 | 		case ISCSI_OP_SCSI_DATA_OUT: | 
 | 3862 | 			if (iscsit_handle_data_out(conn, buffer) < 0) | 
 | 3863 | 				goto transport_err; | 
 | 3864 | 			break; | 
 | 3865 | 		case ISCSI_OP_NOOP_OUT: | 
 | 3866 | 			if (iscsit_handle_nop_out(conn, buffer) < 0) | 
 | 3867 | 				goto transport_err; | 
 | 3868 | 			break; | 
 | 3869 | 		case ISCSI_OP_SCSI_TMFUNC: | 
 | 3870 | 			if (iscsit_handle_task_mgt_cmd(conn, buffer) < 0) | 
 | 3871 | 				goto transport_err; | 
 | 3872 | 			break; | 
 | 3873 | 		case ISCSI_OP_TEXT: | 
 | 3874 | 			if (iscsit_handle_text_cmd(conn, buffer) < 0) | 
 | 3875 | 				goto transport_err; | 
 | 3876 | 			break; | 
 | 3877 | 		case ISCSI_OP_LOGOUT: | 
 | 3878 | 			ret = iscsit_handle_logout_cmd(conn, buffer); | 
 | 3879 | 			if (ret > 0) { | 
 | 3880 | 				wait_for_completion_timeout(&conn->conn_logout_comp, | 
 | 3881 | 						SECONDS_FOR_LOGOUT_COMP * HZ); | 
 | 3882 | 				goto transport_err; | 
 | 3883 | 			} else if (ret < 0) | 
 | 3884 | 				goto transport_err; | 
 | 3885 | 			break; | 
 | 3886 | 		case ISCSI_OP_SNACK: | 
 | 3887 | 			if (iscsit_handle_snack(conn, buffer) < 0) | 
 | 3888 | 				goto transport_err; | 
 | 3889 | 			break; | 
 | 3890 | 		default: | 
 | 3891 | 			pr_err("Got unknown iSCSI OpCode: 0x%02x\n", | 
 | 3892 | 					opcode); | 
 | 3893 | 			if (!conn->sess->sess_ops->ErrorRecoveryLevel) { | 
 | 3894 | 				pr_err("Cannot recover from unknown" | 
 | 3895 | 				" opcode while ERL=0, closing iSCSI connection" | 
 | 3896 | 				".\n"); | 
 | 3897 | 				goto transport_err; | 
 | 3898 | 			} | 
 | 3899 | 			if (!conn->conn_ops->OFMarker) { | 
 | 3900 | 				pr_err("Unable to recover from unknown" | 
 | 3901 | 				" opcode while OFMarker=No, closing iSCSI" | 
 | 3902 | 					" connection.\n"); | 
 | 3903 | 				goto transport_err; | 
 | 3904 | 			} | 
 | 3905 | 			if (iscsit_recover_from_unknown_opcode(conn) < 0) { | 
 | 3906 | 				pr_err("Unable to recover from unknown" | 
 | 3907 | 					" opcode, closing iSCSI connection.\n"); | 
 | 3908 | 				goto transport_err; | 
 | 3909 | 			} | 
 | 3910 | 			break; | 
 | 3911 | 		} | 
 | 3912 | 	} | 
 | 3913 |  | 
 | 3914 | transport_err: | 
 | 3915 | 	if (!signal_pending(current)) | 
 | 3916 | 		atomic_set(&conn->transport_failed, 1); | 
 | 3917 | 	iscsit_take_action_for_connection_exit(conn); | 
 | 3918 | 	goto restart; | 
 | 3919 | out: | 
 | 3920 | 	return 0; | 
 | 3921 | } | 
 | 3922 |  | 
 | 3923 | static void iscsit_release_commands_from_conn(struct iscsi_conn *conn) | 
 | 3924 | { | 
 | 3925 | 	struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL; | 
 | 3926 | 	struct iscsi_session *sess = conn->sess; | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3927 | 	/* | 
 | 3928 | 	 * We expect this function to only ever be called from either RX or TX | 
 | 3929 | 	 * thread context via iscsit_close_connection() once the other context | 
 | 3930 | 	 * has been reset -> returned sleeping pre-handler state. | 
 | 3931 | 	 */ | 
 | 3932 | 	spin_lock_bh(&conn->cmd_lock); | 
| Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 3933 | 	list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3934 |  | 
| Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 3935 | 		list_del(&cmd->i_conn_node); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3936 | 		spin_unlock_bh(&conn->cmd_lock); | 
 | 3937 |  | 
 | 3938 | 		iscsit_increment_maxcmdsn(cmd, sess); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3939 |  | 
| Nicholas Bellinger | d270190 | 2011-10-09 01:48:14 -0700 | [diff] [blame] | 3940 | 		iscsit_free_cmd(cmd); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3941 |  | 
 | 3942 | 		spin_lock_bh(&conn->cmd_lock); | 
 | 3943 | 	} | 
 | 3944 | 	spin_unlock_bh(&conn->cmd_lock); | 
 | 3945 | } | 
 | 3946 |  | 
 | 3947 | static void iscsit_stop_timers_for_cmds( | 
 | 3948 | 	struct iscsi_conn *conn) | 
 | 3949 | { | 
 | 3950 | 	struct iscsi_cmd *cmd; | 
 | 3951 |  | 
 | 3952 | 	spin_lock_bh(&conn->cmd_lock); | 
| Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 3953 | 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) { | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3954 | 		if (cmd->data_direction == DMA_TO_DEVICE) | 
 | 3955 | 			iscsit_stop_dataout_timer(cmd); | 
 | 3956 | 	} | 
 | 3957 | 	spin_unlock_bh(&conn->cmd_lock); | 
 | 3958 | } | 
 | 3959 |  | 
 | 3960 | int iscsit_close_connection( | 
 | 3961 | 	struct iscsi_conn *conn) | 
 | 3962 | { | 
 | 3963 | 	int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT); | 
 | 3964 | 	struct iscsi_session	*sess = conn->sess; | 
 | 3965 |  | 
 | 3966 | 	pr_debug("Closing iSCSI connection CID %hu on SID:" | 
 | 3967 | 		" %u\n", conn->cid, sess->sid); | 
 | 3968 | 	/* | 
 | 3969 | 	 * Always up conn_logout_comp just in case the RX Thread is sleeping | 
 | 3970 | 	 * and the logout response never got sent because the connection | 
 | 3971 | 	 * failed. | 
 | 3972 | 	 */ | 
 | 3973 | 	complete(&conn->conn_logout_comp); | 
 | 3974 |  | 
 | 3975 | 	iscsi_release_thread_set(conn); | 
 | 3976 |  | 
 | 3977 | 	iscsit_stop_timers_for_cmds(conn); | 
 | 3978 | 	iscsit_stop_nopin_response_timer(conn); | 
 | 3979 | 	iscsit_stop_nopin_timer(conn); | 
 | 3980 | 	iscsit_free_queue_reqs_for_conn(conn); | 
 | 3981 |  | 
 | 3982 | 	/* | 
 | 3983 | 	 * During Connection recovery drop unacknowledged out of order | 
 | 3984 | 	 * commands for this connection, and prepare the other commands | 
 | 3985 | 	 * for realligence. | 
 | 3986 | 	 * | 
 | 3987 | 	 * During normal operation clear the out of order commands (but | 
 | 3988 | 	 * do not free the struct iscsi_ooo_cmdsn's) and release all | 
 | 3989 | 	 * struct iscsi_cmds. | 
 | 3990 | 	 */ | 
 | 3991 | 	if (atomic_read(&conn->connection_recovery)) { | 
 | 3992 | 		iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn); | 
 | 3993 | 		iscsit_prepare_cmds_for_realligance(conn); | 
 | 3994 | 	} else { | 
 | 3995 | 		iscsit_clear_ooo_cmdsns_for_conn(conn); | 
 | 3996 | 		iscsit_release_commands_from_conn(conn); | 
 | 3997 | 	} | 
 | 3998 |  | 
 | 3999 | 	/* | 
 | 4000 | 	 * Handle decrementing session or connection usage count if | 
 | 4001 | 	 * a logout response was not able to be sent because the | 
 | 4002 | 	 * connection failed.  Fall back to Session Recovery here. | 
 | 4003 | 	 */ | 
 | 4004 | 	if (atomic_read(&conn->conn_logout_remove)) { | 
 | 4005 | 		if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) { | 
 | 4006 | 			iscsit_dec_conn_usage_count(conn); | 
 | 4007 | 			iscsit_dec_session_usage_count(sess); | 
 | 4008 | 		} | 
 | 4009 | 		if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) | 
 | 4010 | 			iscsit_dec_conn_usage_count(conn); | 
 | 4011 |  | 
 | 4012 | 		atomic_set(&conn->conn_logout_remove, 0); | 
 | 4013 | 		atomic_set(&sess->session_reinstatement, 0); | 
 | 4014 | 		atomic_set(&sess->session_fall_back_to_erl0, 1); | 
 | 4015 | 	} | 
 | 4016 |  | 
 | 4017 | 	spin_lock_bh(&sess->conn_lock); | 
 | 4018 | 	list_del(&conn->conn_list); | 
 | 4019 |  | 
 | 4020 | 	/* | 
 | 4021 | 	 * Attempt to let the Initiator know this connection failed by | 
 | 4022 | 	 * sending an Connection Dropped Async Message on another | 
 | 4023 | 	 * active connection. | 
 | 4024 | 	 */ | 
 | 4025 | 	if (atomic_read(&conn->connection_recovery)) | 
 | 4026 | 		iscsit_build_conn_drop_async_message(conn); | 
 | 4027 |  | 
 | 4028 | 	spin_unlock_bh(&sess->conn_lock); | 
 | 4029 |  | 
 | 4030 | 	/* | 
 | 4031 | 	 * If connection reinstatement is being performed on this connection, | 
 | 4032 | 	 * up the connection reinstatement semaphore that is being blocked on | 
 | 4033 | 	 * in iscsit_cause_connection_reinstatement(). | 
 | 4034 | 	 */ | 
 | 4035 | 	spin_lock_bh(&conn->state_lock); | 
 | 4036 | 	if (atomic_read(&conn->sleep_on_conn_wait_comp)) { | 
 | 4037 | 		spin_unlock_bh(&conn->state_lock); | 
 | 4038 | 		complete(&conn->conn_wait_comp); | 
 | 4039 | 		wait_for_completion(&conn->conn_post_wait_comp); | 
 | 4040 | 		spin_lock_bh(&conn->state_lock); | 
 | 4041 | 	} | 
 | 4042 |  | 
 | 4043 | 	/* | 
 | 4044 | 	 * If connection reinstatement is being performed on this connection | 
 | 4045 | 	 * by receiving a REMOVECONNFORRECOVERY logout request, up the | 
 | 4046 | 	 * connection wait rcfr semaphore that is being blocked on | 
 | 4047 | 	 * an iscsit_connection_reinstatement_rcfr(). | 
 | 4048 | 	 */ | 
 | 4049 | 	if (atomic_read(&conn->connection_wait_rcfr)) { | 
 | 4050 | 		spin_unlock_bh(&conn->state_lock); | 
 | 4051 | 		complete(&conn->conn_wait_rcfr_comp); | 
 | 4052 | 		wait_for_completion(&conn->conn_post_wait_comp); | 
 | 4053 | 		spin_lock_bh(&conn->state_lock); | 
 | 4054 | 	} | 
 | 4055 | 	atomic_set(&conn->connection_reinstatement, 1); | 
 | 4056 | 	spin_unlock_bh(&conn->state_lock); | 
 | 4057 |  | 
 | 4058 | 	/* | 
 | 4059 | 	 * If any other processes are accessing this connection pointer we | 
 | 4060 | 	 * must wait until they have completed. | 
 | 4061 | 	 */ | 
 | 4062 | 	iscsit_check_conn_usage_count(conn); | 
 | 4063 |  | 
 | 4064 | 	if (conn->conn_rx_hash.tfm) | 
 | 4065 | 		crypto_free_hash(conn->conn_rx_hash.tfm); | 
 | 4066 | 	if (conn->conn_tx_hash.tfm) | 
 | 4067 | 		crypto_free_hash(conn->conn_tx_hash.tfm); | 
 | 4068 |  | 
 | 4069 | 	if (conn->conn_cpumask) | 
 | 4070 | 		free_cpumask_var(conn->conn_cpumask); | 
 | 4071 |  | 
 | 4072 | 	kfree(conn->conn_ops); | 
 | 4073 | 	conn->conn_ops = NULL; | 
 | 4074 |  | 
| Al Viro | bf6932f | 2012-07-21 08:55:18 +0100 | [diff] [blame] | 4075 | 	if (conn->sock) | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4076 | 		sock_release(conn->sock); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4077 | 	conn->thread_set = NULL; | 
 | 4078 |  | 
 | 4079 | 	pr_debug("Moving to TARG_CONN_STATE_FREE.\n"); | 
 | 4080 | 	conn->conn_state = TARG_CONN_STATE_FREE; | 
 | 4081 | 	kfree(conn); | 
 | 4082 |  | 
 | 4083 | 	spin_lock_bh(&sess->conn_lock); | 
 | 4084 | 	atomic_dec(&sess->nconn); | 
 | 4085 | 	pr_debug("Decremented iSCSI connection count to %hu from node:" | 
 | 4086 | 		" %s\n", atomic_read(&sess->nconn), | 
 | 4087 | 		sess->sess_ops->InitiatorName); | 
 | 4088 | 	/* | 
 | 4089 | 	 * Make sure that if one connection fails in an non ERL=2 iSCSI | 
 | 4090 | 	 * Session that they all fail. | 
 | 4091 | 	 */ | 
 | 4092 | 	if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout && | 
 | 4093 | 	     !atomic_read(&sess->session_logout)) | 
 | 4094 | 		atomic_set(&sess->session_fall_back_to_erl0, 1); | 
 | 4095 |  | 
 | 4096 | 	/* | 
 | 4097 | 	 * If this was not the last connection in the session, and we are | 
 | 4098 | 	 * performing session reinstatement or falling back to ERL=0, call | 
 | 4099 | 	 * iscsit_stop_session() without sleeping to shutdown the other | 
 | 4100 | 	 * active connections. | 
 | 4101 | 	 */ | 
 | 4102 | 	if (atomic_read(&sess->nconn)) { | 
 | 4103 | 		if (!atomic_read(&sess->session_reinstatement) && | 
 | 4104 | 		    !atomic_read(&sess->session_fall_back_to_erl0)) { | 
 | 4105 | 			spin_unlock_bh(&sess->conn_lock); | 
 | 4106 | 			return 0; | 
 | 4107 | 		} | 
 | 4108 | 		if (!atomic_read(&sess->session_stop_active)) { | 
 | 4109 | 			atomic_set(&sess->session_stop_active, 1); | 
 | 4110 | 			spin_unlock_bh(&sess->conn_lock); | 
 | 4111 | 			iscsit_stop_session(sess, 0, 0); | 
 | 4112 | 			return 0; | 
 | 4113 | 		} | 
 | 4114 | 		spin_unlock_bh(&sess->conn_lock); | 
 | 4115 | 		return 0; | 
 | 4116 | 	} | 
 | 4117 |  | 
 | 4118 | 	/* | 
 | 4119 | 	 * If this was the last connection in the session and one of the | 
 | 4120 | 	 * following is occurring: | 
 | 4121 | 	 * | 
 | 4122 | 	 * Session Reinstatement is not being performed, and are falling back | 
 | 4123 | 	 * to ERL=0 call iscsit_close_session(). | 
 | 4124 | 	 * | 
 | 4125 | 	 * Session Logout was requested.  iscsit_close_session() will be called | 
 | 4126 | 	 * elsewhere. | 
 | 4127 | 	 * | 
 | 4128 | 	 * Session Continuation is not being performed, start the Time2Retain | 
 | 4129 | 	 * handler and check if sleep_on_sess_wait_sem is active. | 
 | 4130 | 	 */ | 
 | 4131 | 	if (!atomic_read(&sess->session_reinstatement) && | 
 | 4132 | 	     atomic_read(&sess->session_fall_back_to_erl0)) { | 
 | 4133 | 		spin_unlock_bh(&sess->conn_lock); | 
| Nicholas Bellinger | 99367f0 | 2012-02-27 01:43:32 -0800 | [diff] [blame] | 4134 | 		target_put_session(sess->se_sess); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4135 |  | 
 | 4136 | 		return 0; | 
 | 4137 | 	} else if (atomic_read(&sess->session_logout)) { | 
 | 4138 | 		pr_debug("Moving to TARG_SESS_STATE_FREE.\n"); | 
 | 4139 | 		sess->session_state = TARG_SESS_STATE_FREE; | 
 | 4140 | 		spin_unlock_bh(&sess->conn_lock); | 
 | 4141 |  | 
 | 4142 | 		if (atomic_read(&sess->sleep_on_sess_wait_comp)) | 
 | 4143 | 			complete(&sess->session_wait_comp); | 
 | 4144 |  | 
 | 4145 | 		return 0; | 
 | 4146 | 	} else { | 
 | 4147 | 		pr_debug("Moving to TARG_SESS_STATE_FAILED.\n"); | 
 | 4148 | 		sess->session_state = TARG_SESS_STATE_FAILED; | 
 | 4149 |  | 
 | 4150 | 		if (!atomic_read(&sess->session_continuation)) { | 
 | 4151 | 			spin_unlock_bh(&sess->conn_lock); | 
 | 4152 | 			iscsit_start_time2retain_handler(sess); | 
 | 4153 | 		} else | 
 | 4154 | 			spin_unlock_bh(&sess->conn_lock); | 
 | 4155 |  | 
 | 4156 | 		if (atomic_read(&sess->sleep_on_sess_wait_comp)) | 
 | 4157 | 			complete(&sess->session_wait_comp); | 
 | 4158 |  | 
 | 4159 | 		return 0; | 
 | 4160 | 	} | 
 | 4161 | 	spin_unlock_bh(&sess->conn_lock); | 
 | 4162 |  | 
 | 4163 | 	return 0; | 
 | 4164 | } | 
 | 4165 |  | 
 | 4166 | int iscsit_close_session(struct iscsi_session *sess) | 
 | 4167 | { | 
 | 4168 | 	struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess); | 
 | 4169 | 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; | 
 | 4170 |  | 
 | 4171 | 	if (atomic_read(&sess->nconn)) { | 
 | 4172 | 		pr_err("%d connection(s) still exist for iSCSI session" | 
 | 4173 | 			" to %s\n", atomic_read(&sess->nconn), | 
 | 4174 | 			sess->sess_ops->InitiatorName); | 
 | 4175 | 		BUG(); | 
 | 4176 | 	} | 
 | 4177 |  | 
 | 4178 | 	spin_lock_bh(&se_tpg->session_lock); | 
 | 4179 | 	atomic_set(&sess->session_logout, 1); | 
 | 4180 | 	atomic_set(&sess->session_reinstatement, 1); | 
 | 4181 | 	iscsit_stop_time2retain_timer(sess); | 
 | 4182 | 	spin_unlock_bh(&se_tpg->session_lock); | 
 | 4183 |  | 
 | 4184 | 	/* | 
 | 4185 | 	 * transport_deregister_session_configfs() will clear the | 
 | 4186 | 	 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context | 
 | 4187 | 	 * can be setting it again with __transport_register_session() in | 
 | 4188 | 	 * iscsi_post_login_handler() again after the iscsit_stop_session() | 
 | 4189 | 	 * completes in iscsi_np context. | 
 | 4190 | 	 */ | 
 | 4191 | 	transport_deregister_session_configfs(sess->se_sess); | 
 | 4192 |  | 
 | 4193 | 	/* | 
 | 4194 | 	 * If any other processes are accessing this session pointer we must | 
 | 4195 | 	 * wait until they have completed.  If we are in an interrupt (the | 
 | 4196 | 	 * time2retain handler) and contain and active session usage count we | 
 | 4197 | 	 * restart the timer and exit. | 
 | 4198 | 	 */ | 
 | 4199 | 	if (!in_interrupt()) { | 
 | 4200 | 		if (iscsit_check_session_usage_count(sess) == 1) | 
 | 4201 | 			iscsit_stop_session(sess, 1, 1); | 
 | 4202 | 	} else { | 
 | 4203 | 		if (iscsit_check_session_usage_count(sess) == 2) { | 
 | 4204 | 			atomic_set(&sess->session_logout, 0); | 
 | 4205 | 			iscsit_start_time2retain_handler(sess); | 
 | 4206 | 			return 0; | 
 | 4207 | 		} | 
 | 4208 | 	} | 
 | 4209 |  | 
 | 4210 | 	transport_deregister_session(sess->se_sess); | 
 | 4211 |  | 
 | 4212 | 	if (sess->sess_ops->ErrorRecoveryLevel == 2) | 
 | 4213 | 		iscsit_free_connection_recovery_entires(sess); | 
 | 4214 |  | 
 | 4215 | 	iscsit_free_all_ooo_cmdsns(sess); | 
 | 4216 |  | 
 | 4217 | 	spin_lock_bh(&se_tpg->session_lock); | 
 | 4218 | 	pr_debug("Moving to TARG_SESS_STATE_FREE.\n"); | 
 | 4219 | 	sess->session_state = TARG_SESS_STATE_FREE; | 
 | 4220 | 	pr_debug("Released iSCSI session from node: %s\n", | 
 | 4221 | 			sess->sess_ops->InitiatorName); | 
 | 4222 | 	tpg->nsessions--; | 
 | 4223 | 	if (tpg->tpg_tiqn) | 
 | 4224 | 		tpg->tpg_tiqn->tiqn_nsessions--; | 
 | 4225 |  | 
 | 4226 | 	pr_debug("Decremented number of active iSCSI Sessions on" | 
 | 4227 | 		" iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions); | 
 | 4228 |  | 
 | 4229 | 	spin_lock(&sess_idr_lock); | 
 | 4230 | 	idr_remove(&sess_idr, sess->session_index); | 
 | 4231 | 	spin_unlock(&sess_idr_lock); | 
 | 4232 |  | 
 | 4233 | 	kfree(sess->sess_ops); | 
 | 4234 | 	sess->sess_ops = NULL; | 
 | 4235 | 	spin_unlock_bh(&se_tpg->session_lock); | 
 | 4236 |  | 
 | 4237 | 	kfree(sess); | 
 | 4238 | 	return 0; | 
 | 4239 | } | 
 | 4240 |  | 
 | 4241 | static void iscsit_logout_post_handler_closesession( | 
 | 4242 | 	struct iscsi_conn *conn) | 
 | 4243 | { | 
 | 4244 | 	struct iscsi_session *sess = conn->sess; | 
 | 4245 |  | 
 | 4246 | 	iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD); | 
 | 4247 | 	iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD); | 
 | 4248 |  | 
 | 4249 | 	atomic_set(&conn->conn_logout_remove, 0); | 
 | 4250 | 	complete(&conn->conn_logout_comp); | 
 | 4251 |  | 
 | 4252 | 	iscsit_dec_conn_usage_count(conn); | 
 | 4253 | 	iscsit_stop_session(sess, 1, 1); | 
 | 4254 | 	iscsit_dec_session_usage_count(sess); | 
| Nicholas Bellinger | 99367f0 | 2012-02-27 01:43:32 -0800 | [diff] [blame] | 4255 | 	target_put_session(sess->se_sess); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4256 | } | 
 | 4257 |  | 
 | 4258 | static void iscsit_logout_post_handler_samecid( | 
 | 4259 | 	struct iscsi_conn *conn) | 
 | 4260 | { | 
 | 4261 | 	iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD); | 
 | 4262 | 	iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD); | 
 | 4263 |  | 
 | 4264 | 	atomic_set(&conn->conn_logout_remove, 0); | 
 | 4265 | 	complete(&conn->conn_logout_comp); | 
 | 4266 |  | 
 | 4267 | 	iscsit_cause_connection_reinstatement(conn, 1); | 
 | 4268 | 	iscsit_dec_conn_usage_count(conn); | 
 | 4269 | } | 
 | 4270 |  | 
 | 4271 | static void iscsit_logout_post_handler_diffcid( | 
 | 4272 | 	struct iscsi_conn *conn, | 
 | 4273 | 	u16 cid) | 
 | 4274 | { | 
 | 4275 | 	struct iscsi_conn *l_conn; | 
 | 4276 | 	struct iscsi_session *sess = conn->sess; | 
 | 4277 |  | 
 | 4278 | 	if (!sess) | 
 | 4279 | 		return; | 
 | 4280 |  | 
 | 4281 | 	spin_lock_bh(&sess->conn_lock); | 
 | 4282 | 	list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) { | 
 | 4283 | 		if (l_conn->cid == cid) { | 
 | 4284 | 			iscsit_inc_conn_usage_count(l_conn); | 
 | 4285 | 			break; | 
 | 4286 | 		} | 
 | 4287 | 	} | 
 | 4288 | 	spin_unlock_bh(&sess->conn_lock); | 
 | 4289 |  | 
 | 4290 | 	if (!l_conn) | 
 | 4291 | 		return; | 
 | 4292 |  | 
 | 4293 | 	if (l_conn->sock) | 
 | 4294 | 		l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN); | 
 | 4295 |  | 
 | 4296 | 	spin_lock_bh(&l_conn->state_lock); | 
 | 4297 | 	pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n"); | 
 | 4298 | 	l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT; | 
 | 4299 | 	spin_unlock_bh(&l_conn->state_lock); | 
 | 4300 |  | 
 | 4301 | 	iscsit_cause_connection_reinstatement(l_conn, 1); | 
 | 4302 | 	iscsit_dec_conn_usage_count(l_conn); | 
 | 4303 | } | 
 | 4304 |  | 
 | 4305 | /* | 
 | 4306 |  *	Return of 0 causes the TX thread to restart. | 
 | 4307 |  */ | 
 | 4308 | static int iscsit_logout_post_handler( | 
 | 4309 | 	struct iscsi_cmd *cmd, | 
 | 4310 | 	struct iscsi_conn *conn) | 
 | 4311 | { | 
 | 4312 | 	int ret = 0; | 
 | 4313 |  | 
 | 4314 | 	switch (cmd->logout_reason) { | 
 | 4315 | 	case ISCSI_LOGOUT_REASON_CLOSE_SESSION: | 
 | 4316 | 		switch (cmd->logout_response) { | 
 | 4317 | 		case ISCSI_LOGOUT_SUCCESS: | 
 | 4318 | 		case ISCSI_LOGOUT_CLEANUP_FAILED: | 
 | 4319 | 		default: | 
 | 4320 | 			iscsit_logout_post_handler_closesession(conn); | 
 | 4321 | 			break; | 
 | 4322 | 		} | 
 | 4323 | 		ret = 0; | 
 | 4324 | 		break; | 
 | 4325 | 	case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION: | 
 | 4326 | 		if (conn->cid == cmd->logout_cid) { | 
 | 4327 | 			switch (cmd->logout_response) { | 
 | 4328 | 			case ISCSI_LOGOUT_SUCCESS: | 
 | 4329 | 			case ISCSI_LOGOUT_CLEANUP_FAILED: | 
 | 4330 | 			default: | 
 | 4331 | 				iscsit_logout_post_handler_samecid(conn); | 
 | 4332 | 				break; | 
 | 4333 | 			} | 
 | 4334 | 			ret = 0; | 
 | 4335 | 		} else { | 
 | 4336 | 			switch (cmd->logout_response) { | 
 | 4337 | 			case ISCSI_LOGOUT_SUCCESS: | 
 | 4338 | 				iscsit_logout_post_handler_diffcid(conn, | 
 | 4339 | 					cmd->logout_cid); | 
 | 4340 | 				break; | 
 | 4341 | 			case ISCSI_LOGOUT_CID_NOT_FOUND: | 
 | 4342 | 			case ISCSI_LOGOUT_CLEANUP_FAILED: | 
 | 4343 | 			default: | 
 | 4344 | 				break; | 
 | 4345 | 			} | 
 | 4346 | 			ret = 1; | 
 | 4347 | 		} | 
 | 4348 | 		break; | 
 | 4349 | 	case ISCSI_LOGOUT_REASON_RECOVERY: | 
 | 4350 | 		switch (cmd->logout_response) { | 
 | 4351 | 		case ISCSI_LOGOUT_SUCCESS: | 
 | 4352 | 		case ISCSI_LOGOUT_CID_NOT_FOUND: | 
 | 4353 | 		case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED: | 
 | 4354 | 		case ISCSI_LOGOUT_CLEANUP_FAILED: | 
 | 4355 | 		default: | 
 | 4356 | 			break; | 
 | 4357 | 		} | 
 | 4358 | 		ret = 1; | 
 | 4359 | 		break; | 
 | 4360 | 	default: | 
 | 4361 | 		break; | 
 | 4362 |  | 
 | 4363 | 	} | 
 | 4364 | 	return ret; | 
 | 4365 | } | 
 | 4366 |  | 
 | 4367 | void iscsit_fail_session(struct iscsi_session *sess) | 
 | 4368 | { | 
 | 4369 | 	struct iscsi_conn *conn; | 
 | 4370 |  | 
 | 4371 | 	spin_lock_bh(&sess->conn_lock); | 
 | 4372 | 	list_for_each_entry(conn, &sess->sess_conn_list, conn_list) { | 
 | 4373 | 		pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n"); | 
 | 4374 | 		conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT; | 
 | 4375 | 	} | 
 | 4376 | 	spin_unlock_bh(&sess->conn_lock); | 
 | 4377 |  | 
 | 4378 | 	pr_debug("Moving to TARG_SESS_STATE_FAILED.\n"); | 
 | 4379 | 	sess->session_state = TARG_SESS_STATE_FAILED; | 
 | 4380 | } | 
 | 4381 |  | 
 | 4382 | int iscsit_free_session(struct iscsi_session *sess) | 
 | 4383 | { | 
 | 4384 | 	u16 conn_count = atomic_read(&sess->nconn); | 
 | 4385 | 	struct iscsi_conn *conn, *conn_tmp = NULL; | 
 | 4386 | 	int is_last; | 
 | 4387 |  | 
 | 4388 | 	spin_lock_bh(&sess->conn_lock); | 
 | 4389 | 	atomic_set(&sess->sleep_on_sess_wait_comp, 1); | 
 | 4390 |  | 
 | 4391 | 	list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list, | 
 | 4392 | 			conn_list) { | 
 | 4393 | 		if (conn_count == 0) | 
 | 4394 | 			break; | 
 | 4395 |  | 
 | 4396 | 		if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) { | 
 | 4397 | 			is_last = 1; | 
 | 4398 | 		} else { | 
 | 4399 | 			iscsit_inc_conn_usage_count(conn_tmp); | 
 | 4400 | 			is_last = 0; | 
 | 4401 | 		} | 
 | 4402 | 		iscsit_inc_conn_usage_count(conn); | 
 | 4403 |  | 
 | 4404 | 		spin_unlock_bh(&sess->conn_lock); | 
 | 4405 | 		iscsit_cause_connection_reinstatement(conn, 1); | 
 | 4406 | 		spin_lock_bh(&sess->conn_lock); | 
 | 4407 |  | 
 | 4408 | 		iscsit_dec_conn_usage_count(conn); | 
 | 4409 | 		if (is_last == 0) | 
 | 4410 | 			iscsit_dec_conn_usage_count(conn_tmp); | 
 | 4411 |  | 
 | 4412 | 		conn_count--; | 
 | 4413 | 	} | 
 | 4414 |  | 
 | 4415 | 	if (atomic_read(&sess->nconn)) { | 
 | 4416 | 		spin_unlock_bh(&sess->conn_lock); | 
 | 4417 | 		wait_for_completion(&sess->session_wait_comp); | 
 | 4418 | 	} else | 
 | 4419 | 		spin_unlock_bh(&sess->conn_lock); | 
 | 4420 |  | 
| Nicholas Bellinger | 99367f0 | 2012-02-27 01:43:32 -0800 | [diff] [blame] | 4421 | 	target_put_session(sess->se_sess); | 
| Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4422 | 	return 0; | 
 | 4423 | } | 
 | 4424 |  | 
 | 4425 | void iscsit_stop_session( | 
 | 4426 | 	struct iscsi_session *sess, | 
 | 4427 | 	int session_sleep, | 
 | 4428 | 	int connection_sleep) | 
 | 4429 | { | 
 | 4430 | 	u16 conn_count = atomic_read(&sess->nconn); | 
 | 4431 | 	struct iscsi_conn *conn, *conn_tmp = NULL; | 
 | 4432 | 	int is_last; | 
 | 4433 |  | 
 | 4434 | 	spin_lock_bh(&sess->conn_lock); | 
 | 4435 | 	if (session_sleep) | 
 | 4436 | 		atomic_set(&sess->sleep_on_sess_wait_comp, 1); | 
 | 4437 |  | 
 | 4438 | 	if (connection_sleep) { | 
 | 4439 | 		list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list, | 
 | 4440 | 				conn_list) { | 
 | 4441 | 			if (conn_count == 0) | 
 | 4442 | 				break; | 
 | 4443 |  | 
 | 4444 | 			if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) { | 
 | 4445 | 				is_last = 1; | 
 | 4446 | 			} else { | 
 | 4447 | 				iscsit_inc_conn_usage_count(conn_tmp); | 
 | 4448 | 				is_last = 0; | 
 | 4449 | 			} | 
 | 4450 | 			iscsit_inc_conn_usage_count(conn); | 
 | 4451 |  | 
 | 4452 | 			spin_unlock_bh(&sess->conn_lock); | 
 | 4453 | 			iscsit_cause_connection_reinstatement(conn, 1); | 
 | 4454 | 			spin_lock_bh(&sess->conn_lock); | 
 | 4455 |  | 
 | 4456 | 			iscsit_dec_conn_usage_count(conn); | 
 | 4457 | 			if (is_last == 0) | 
 | 4458 | 				iscsit_dec_conn_usage_count(conn_tmp); | 
 | 4459 | 			conn_count--; | 
 | 4460 | 		} | 
 | 4461 | 	} else { | 
 | 4462 | 		list_for_each_entry(conn, &sess->sess_conn_list, conn_list) | 
 | 4463 | 			iscsit_cause_connection_reinstatement(conn, 0); | 
 | 4464 | 	} | 
 | 4465 |  | 
 | 4466 | 	if (session_sleep && atomic_read(&sess->nconn)) { | 
 | 4467 | 		spin_unlock_bh(&sess->conn_lock); | 
 | 4468 | 		wait_for_completion(&sess->session_wait_comp); | 
 | 4469 | 	} else | 
 | 4470 | 		spin_unlock_bh(&sess->conn_lock); | 
 | 4471 | } | 
 | 4472 |  | 
 | 4473 | int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force) | 
 | 4474 | { | 
 | 4475 | 	struct iscsi_session *sess; | 
 | 4476 | 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; | 
 | 4477 | 	struct se_session *se_sess, *se_sess_tmp; | 
 | 4478 | 	int session_count = 0; | 
 | 4479 |  | 
 | 4480 | 	spin_lock_bh(&se_tpg->session_lock); | 
 | 4481 | 	if (tpg->nsessions && !force) { | 
 | 4482 | 		spin_unlock_bh(&se_tpg->session_lock); | 
 | 4483 | 		return -1; | 
 | 4484 | 	} | 
 | 4485 |  | 
 | 4486 | 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list, | 
 | 4487 | 			sess_list) { | 
 | 4488 | 		sess = (struct iscsi_session *)se_sess->fabric_sess_ptr; | 
 | 4489 |  | 
 | 4490 | 		spin_lock(&sess->conn_lock); | 
 | 4491 | 		if (atomic_read(&sess->session_fall_back_to_erl0) || | 
 | 4492 | 		    atomic_read(&sess->session_logout) || | 
 | 4493 | 		    (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) { | 
 | 4494 | 			spin_unlock(&sess->conn_lock); | 
 | 4495 | 			continue; | 
 | 4496 | 		} | 
 | 4497 | 		atomic_set(&sess->session_reinstatement, 1); | 
 | 4498 | 		spin_unlock(&sess->conn_lock); | 
 | 4499 | 		spin_unlock_bh(&se_tpg->session_lock); | 
 | 4500 |  | 
 | 4501 | 		iscsit_free_session(sess); | 
 | 4502 | 		spin_lock_bh(&se_tpg->session_lock); | 
 | 4503 |  | 
 | 4504 | 		session_count++; | 
 | 4505 | 	} | 
 | 4506 | 	spin_unlock_bh(&se_tpg->session_lock); | 
 | 4507 |  | 
 | 4508 | 	pr_debug("Released %d iSCSI Session(s) from Target Portal" | 
 | 4509 | 			" Group: %hu\n", session_count, tpg->tpgt); | 
 | 4510 | 	return 0; | 
 | 4511 | } | 
 | 4512 |  | 
 | 4513 | MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure"); | 
 | 4514 | MODULE_VERSION("4.1.x"); | 
 | 4515 | MODULE_AUTHOR("nab@Linux-iSCSI.org"); | 
 | 4516 | MODULE_LICENSE("GPL"); | 
 | 4517 |  | 
 | 4518 | module_init(iscsi_target_init_module); | 
 | 4519 | module_exit(iscsi_target_cleanup_module); |