target_core_transport.c 92.5 KB
Newer Older
1 2 3 4 5
/*******************************************************************************
 * Filename:  target_core_transport.c
 *
 * This file contains the Generic Target Engine Core.
 *
6
 * (c) Copyright 2002-2013 Datera, Inc.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * Nicholas A. Bellinger <nab@kernel.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ******************************************************************************/

#include <linux/net.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/kthread.h>
#include <linux/in.h>
#include <linux/cdrom.h>
35
#include <linux/module.h>
36
#include <linux/ratelimit.h>
37
#include <linux/vmalloc.h>
38 39 40
#include <asm/unaligned.h>
#include <net/sock.h>
#include <net/tcp.h>
41
#include <scsi/scsi_proto.h>
42
#include <scsi/scsi_common.h>
43 44

#include <target/target_core_base.h>
45 46
#include <target/target_core_backend.h>
#include <target/target_core_fabric.h>
47

Christoph Hellwig's avatar
Christoph Hellwig committed
48
#include "target_core_internal.h"
49 50 51 52
#include "target_core_alua.h"
#include "target_core_pr.h"
#include "target_core_ua.h"

53 54 55
#define CREATE_TRACE_POINTS
#include <trace/events/target.h>

56
static struct workqueue_struct *target_completion_wq;
57 58 59 60 61 62
static struct kmem_cache *se_sess_cache;
struct kmem_cache *se_ua_cache;
struct kmem_cache *t10_pr_reg_cache;
struct kmem_cache *t10_alua_lu_gp_cache;
struct kmem_cache *t10_alua_lu_gp_mem_cache;
struct kmem_cache *t10_alua_tg_pt_gp_cache;
63 64
struct kmem_cache *t10_alua_lba_map_cache;
struct kmem_cache *t10_alua_lba_map_mem_cache;
65 66

static void transport_complete_task_attr(struct se_cmd *cmd);
67
static int translate_sense_reason(struct se_cmd *cmd, sense_reason_t reason);
68
static void transport_handle_queue_full(struct se_cmd *cmd,
69
		struct se_device *dev, int err, bool write_pending);
70
static int transport_put_cmd(struct se_cmd *cmd);
71
static void target_complete_ok_work(struct work_struct *work);
72

73
int init_se_kmem_caches(void)
74 75 76 77
{
	se_sess_cache = kmem_cache_create("se_sess_cache",
			sizeof(struct se_session), __alignof__(struct se_session),
			0, NULL);
78 79
	if (!se_sess_cache) {
		pr_err("kmem_cache_create() for struct se_session"
80
				" failed\n");
81
		goto out;
82 83 84 85
	}
	se_ua_cache = kmem_cache_create("se_ua_cache",
			sizeof(struct se_ua), __alignof__(struct se_ua),
			0, NULL);
86 87
	if (!se_ua_cache) {
		pr_err("kmem_cache_create() for struct se_ua failed\n");
88
		goto out_free_sess_cache;
89 90 91 92
	}
	t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
			sizeof(struct t10_pr_registration),
			__alignof__(struct t10_pr_registration), 0, NULL);
93 94
	if (!t10_pr_reg_cache) {
		pr_err("kmem_cache_create() for struct t10_pr_registration"
95
				" failed\n");
96
		goto out_free_ua_cache;
97 98 99 100
	}
	t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
			sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
			0, NULL);
101 102
	if (!t10_alua_lu_gp_cache) {
		pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
103
				" failed\n");
104
		goto out_free_pr_reg_cache;
105 106 107 108
	}
	t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
			sizeof(struct t10_alua_lu_gp_member),
			__alignof__(struct t10_alua_lu_gp_member), 0, NULL);
109 110
	if (!t10_alua_lu_gp_mem_cache) {
		pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
111
				"cache failed\n");
112
		goto out_free_lu_gp_cache;
113 114 115 116
	}
	t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
			sizeof(struct t10_alua_tg_pt_gp),
			__alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
117 118
	if (!t10_alua_tg_pt_gp_cache) {
		pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
119
				"cache failed\n");
120
		goto out_free_lu_gp_mem_cache;
121
	}
122 123 124 125 126 127 128
	t10_alua_lba_map_cache = kmem_cache_create(
			"t10_alua_lba_map_cache",
			sizeof(struct t10_alua_lba_map),
			__alignof__(struct t10_alua_lba_map), 0, NULL);
	if (!t10_alua_lba_map_cache) {
		pr_err("kmem_cache_create() for t10_alua_lba_map_"
				"cache failed\n");
129
		goto out_free_tg_pt_gp_cache;
130 131 132 133 134 135 136 137 138 139
	}
	t10_alua_lba_map_mem_cache = kmem_cache_create(
			"t10_alua_lba_map_mem_cache",
			sizeof(struct t10_alua_lba_map_member),
			__alignof__(struct t10_alua_lba_map_member), 0, NULL);
	if (!t10_alua_lba_map_mem_cache) {
		pr_err("kmem_cache_create() for t10_alua_lba_map_mem_"
				"cache failed\n");
		goto out_free_lba_map_cache;
	}
140

141 142 143
	target_completion_wq = alloc_workqueue("target_completion",
					       WQ_MEM_RECLAIM, 0);
	if (!target_completion_wq)
144
		goto out_free_lba_map_mem_cache;
145

146
	return 0;
147

148 149 150 151
out_free_lba_map_mem_cache:
	kmem_cache_destroy(t10_alua_lba_map_mem_cache);
out_free_lba_map_cache:
	kmem_cache_destroy(t10_alua_lba_map_cache);
152 153 154 155 156 157 158 159 160 161 162 163
out_free_tg_pt_gp_cache:
	kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
out_free_lu_gp_mem_cache:
	kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
out_free_lu_gp_cache:
	kmem_cache_destroy(t10_alua_lu_gp_cache);
out_free_pr_reg_cache:
	kmem_cache_destroy(t10_pr_reg_cache);
out_free_ua_cache:
	kmem_cache_destroy(se_ua_cache);
out_free_sess_cache:
	kmem_cache_destroy(se_sess_cache);
164
out:
165
	return -ENOMEM;
166 167
}

168
void release_se_kmem_caches(void)
169
{
170
	destroy_workqueue(target_completion_wq);
171 172 173 174 175 176
	kmem_cache_destroy(se_sess_cache);
	kmem_cache_destroy(se_ua_cache);
	kmem_cache_destroy(t10_pr_reg_cache);
	kmem_cache_destroy(t10_alua_lu_gp_cache);
	kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
	kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
177 178
	kmem_cache_destroy(t10_alua_lba_map_cache);
	kmem_cache_destroy(t10_alua_lba_map_mem_cache);
179 180
}

181 182 183
/* This code ensures unique mib indexes are handed out. */
static DEFINE_SPINLOCK(scsi_mib_index_lock);
static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
184 185 186 187 188 189 190 191

/*
 * Allocate a new row index for the entry type specified
 */
u32 scsi_get_new_index(scsi_index_t type)
{
	u32 new_index;

192
	BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
193

194 195 196
	spin_lock(&scsi_mib_index_lock);
	new_index = ++scsi_mib_index[type];
	spin_unlock(&scsi_mib_index_lock);
197 198 199 200

	return new_index;
}

201
void transport_subsystem_check_init(void)
202 203
{
	int ret;
204
	static int sub_api_initialized;
205

206 207 208
	if (sub_api_initialized)
		return;

209 210
	ret = request_module("target_core_iblock");
	if (ret != 0)
211
		pr_err("Unable to load target_core_iblock\n");
212 213 214

	ret = request_module("target_core_file");
	if (ret != 0)
215
		pr_err("Unable to load target_core_file\n");
216 217 218

	ret = request_module("target_core_pscsi");
	if (ret != 0)
219
		pr_err("Unable to load target_core_pscsi\n");
220

221 222 223 224
	ret = request_module("target_core_user");
	if (ret != 0)
		pr_err("Unable to load target_core_user\n");

225
	sub_api_initialized = 1;
226 227
}

228
struct se_session *transport_init_session(enum target_prot_op sup_prot_ops)
229 230 231 232
{
	struct se_session *se_sess;

	se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
233 234
	if (!se_sess) {
		pr_err("Unable to allocate struct se_session from"
235 236 237 238 239
				" se_sess_cache\n");
		return ERR_PTR(-ENOMEM);
	}
	INIT_LIST_HEAD(&se_sess->sess_list);
	INIT_LIST_HEAD(&se_sess->sess_acl_list);
240
	INIT_LIST_HEAD(&se_sess->sess_cmd_list);
241
	INIT_LIST_HEAD(&se_sess->sess_wait_list);
242
	spin_lock_init(&se_sess->sess_cmd_lock);
243
	se_sess->sup_prot_ops = sup_prot_ops;
244 245 246 247 248

	return se_sess;
}
EXPORT_SYMBOL(transport_init_session);

249 250 251 252 253
int transport_alloc_session_tags(struct se_session *se_sess,
			         unsigned int tag_num, unsigned int tag_size)
{
	int rc;

254
	se_sess->sess_cmd_map = kzalloc(tag_num * tag_size,
255
					GFP_KERNEL | __GFP_NOWARN | __GFP_RETRY_MAYFAIL);
256
	if (!se_sess->sess_cmd_map) {
257 258 259 260 261
		se_sess->sess_cmd_map = vzalloc(tag_num * tag_size);
		if (!se_sess->sess_cmd_map) {
			pr_err("Unable to allocate se_sess->sess_cmd_map\n");
			return -ENOMEM;
		}
262 263 264 265 266 267
	}

	rc = percpu_ida_init(&se_sess->sess_tag_pool, tag_num);
	if (rc < 0) {
		pr_err("Unable to init se_sess->sess_tag_pool,"
			" tag_num: %u\n", tag_num);
268
		kvfree(se_sess->sess_cmd_map);
269 270 271 272 273 274 275 276 277
		se_sess->sess_cmd_map = NULL;
		return -ENOMEM;
	}

	return 0;
}
EXPORT_SYMBOL(transport_alloc_session_tags);

struct se_session *transport_init_session_tags(unsigned int tag_num,
278 279
					       unsigned int tag_size,
					       enum target_prot_op sup_prot_ops)
280 281 282 283
{
	struct se_session *se_sess;
	int rc;

284 285 286 287 288 289 290 291 292 293 294
	if (tag_num != 0 && !tag_size) {
		pr_err("init_session_tags called with percpu-ida tag_num:"
		       " %u, but zero tag_size\n", tag_num);
		return ERR_PTR(-EINVAL);
	}
	if (!tag_num && tag_size) {
		pr_err("init_session_tags called with percpu-ida tag_size:"
		       " %u, but zero tag_num\n", tag_size);
		return ERR_PTR(-EINVAL);
	}

295
	se_sess = transport_init_session(sup_prot_ops);
296 297 298 299 300 301 302 303 304 305 306 307 308
	if (IS_ERR(se_sess))
		return se_sess;

	rc = transport_alloc_session_tags(se_sess, tag_num, tag_size);
	if (rc < 0) {
		transport_free_session(se_sess);
		return ERR_PTR(-ENOMEM);
	}

	return se_sess;
}
EXPORT_SYMBOL(transport_init_session_tags);

309
/*
310
 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
311 312 313 314 315 316 317
 */
void __transport_register_session(
	struct se_portal_group *se_tpg,
	struct se_node_acl *se_nacl,
	struct se_session *se_sess,
	void *fabric_sess_ptr)
{
318
	const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo;
319 320 321 322 323 324 325 326 327 328 329
	unsigned char buf[PR_REG_ISID_LEN];

	se_sess->se_tpg = se_tpg;
	se_sess->fabric_sess_ptr = fabric_sess_ptr;
	/*
	 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
	 *
	 * Only set for struct se_session's that will actually be moving I/O.
	 * eg: *NOT* discovery sessions.
	 */
	if (se_nacl) {
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
		/*
		 *
		 * Determine if fabric allows for T10-PI feature bits exposed to
		 * initiators for device backends with !dev->dev_attrib.pi_prot_type.
		 *
		 * If so, then always save prot_type on a per se_node_acl node
		 * basis and re-instate the previous sess_prot_type to avoid
		 * disabling PI from below any previously initiator side
		 * registered LUNs.
		 */
		if (se_nacl->saved_prot_type)
			se_sess->sess_prot_type = se_nacl->saved_prot_type;
		else if (tfo->tpg_check_prot_fabric_only)
			se_sess->sess_prot_type = se_nacl->saved_prot_type =
					tfo->tpg_check_prot_fabric_only(se_tpg);
345 346 347 348
		/*
		 * If the fabric module supports an ISID based TransportID,
		 * save this value in binary from the fabric I_T Nexus now.
		 */
349
		if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
350
			memset(&buf[0], 0, PR_REG_ISID_LEN);
351
			se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
352 353 354
					&buf[0], PR_REG_ISID_LEN);
			se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
		}
355

356 357 358 359 360 361 362 363 364 365 366 367 368
		spin_lock_irq(&se_nacl->nacl_sess_lock);
		/*
		 * The se_nacl->nacl_sess pointer will be set to the
		 * last active I_T Nexus for each struct se_node_acl.
		 */
		se_nacl->nacl_sess = se_sess;

		list_add_tail(&se_sess->sess_acl_list,
			      &se_nacl->acl_sess_list);
		spin_unlock_irq(&se_nacl->nacl_sess_lock);
	}
	list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);

369
	pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
370
		se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
371 372 373 374 375 376 377 378 379
}
EXPORT_SYMBOL(__transport_register_session);

void transport_register_session(
	struct se_portal_group *se_tpg,
	struct se_node_acl *se_nacl,
	struct se_session *se_sess,
	void *fabric_sess_ptr)
{
380 381 382
	unsigned long flags;

	spin_lock_irqsave(&se_tpg->session_lock, flags);
383
	__transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
384
	spin_unlock_irqrestore(&se_tpg->session_lock, flags);
385 386 387
}
EXPORT_SYMBOL(transport_register_session);

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
struct se_session *
target_alloc_session(struct se_portal_group *tpg,
		     unsigned int tag_num, unsigned int tag_size,
		     enum target_prot_op prot_op,
		     const char *initiatorname, void *private,
		     int (*callback)(struct se_portal_group *,
				     struct se_session *, void *))
{
	struct se_session *sess;

	/*
	 * If the fabric driver is using percpu-ida based pre allocation
	 * of I/O descriptor tags, go ahead and perform that setup now..
	 */
	if (tag_num != 0)
		sess = transport_init_session_tags(tag_num, tag_size, prot_op);
	else
		sess = transport_init_session(prot_op);

	if (IS_ERR(sess))
		return sess;

	sess->se_node_acl = core_tpg_check_initiator_node_acl(tpg,
					(unsigned char *)initiatorname);
	if (!sess->se_node_acl) {
		transport_free_session(sess);
		return ERR_PTR(-EACCES);
	}
	/*
	 * Go ahead and perform any remaining fabric setup that is
	 * required before transport_register_session().
	 */
	if (callback != NULL) {
		int rc = callback(tpg, sess, private);
		if (rc) {
			transport_free_session(sess);
			return ERR_PTR(rc);
		}
	}

	transport_register_session(tpg, sess->se_node_acl, sess, private);
	return sess;
}
EXPORT_SYMBOL(target_alloc_session);

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
ssize_t target_show_dynamic_sessions(struct se_portal_group *se_tpg, char *page)
{
	struct se_session *se_sess;
	ssize_t len = 0;

	spin_lock_bh(&se_tpg->session_lock);
	list_for_each_entry(se_sess, &se_tpg->tpg_sess_list, sess_list) {
		if (!se_sess->se_node_acl)
			continue;
		if (!se_sess->se_node_acl->dynamic_node_acl)
			continue;
		if (strlen(se_sess->se_node_acl->initiatorname) + 1 + len > PAGE_SIZE)
			break;

		len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
				se_sess->se_node_acl->initiatorname);
		len += 1; /* Include NULL terminator */
	}
	spin_unlock_bh(&se_tpg->session_lock);

	return len;
}
EXPORT_SYMBOL(target_show_dynamic_sessions);

457 458 459 460
static void target_complete_nacl(struct kref *kref)
{
	struct se_node_acl *nacl = container_of(kref,
				struct se_node_acl, acl_kref);
461
	struct se_portal_group *se_tpg = nacl->se_tpg;
462

463 464 465 466 467 468
	if (!nacl->dynamic_stop) {
		complete(&nacl->acl_free_comp);
		return;
	}

	mutex_lock(&se_tpg->acl_node_mutex);
469
	list_del_init(&nacl->acl_list);
470 471 472 473 474
	mutex_unlock(&se_tpg->acl_node_mutex);

	core_tpg_wait_for_nacl_pr_ref(nacl);
	core_free_device_list_for_node(nacl, se_tpg);
	kfree(nacl);
475 476 477 478 479 480
}

void target_put_nacl(struct se_node_acl *nacl)
{
	kref_put(&nacl->acl_kref, target_complete_nacl);
}
481
EXPORT_SYMBOL(target_put_nacl);
482

483 484 485
void transport_deregister_session_configfs(struct se_session *se_sess)
{
	struct se_node_acl *se_nacl;
486
	unsigned long flags;
487 488 489 490
	/*
	 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
	 */
	se_nacl = se_sess->se_node_acl;
491
	if (se_nacl) {
492
		spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
Christoph Hellwig's avatar
Christoph Hellwig committed
493 494
		if (!list_empty(&se_sess->sess_acl_list))
			list_del_init(&se_sess->sess_acl_list);
495 496 497 498 499 500 501 502 503 504 505 506
		/*
		 * If the session list is empty, then clear the pointer.
		 * Otherwise, set the struct se_session pointer from the tail
		 * element of the per struct se_node_acl active session list.
		 */
		if (list_empty(&se_nacl->acl_sess_list))
			se_nacl->nacl_sess = NULL;
		else {
			se_nacl->nacl_sess = container_of(
					se_nacl->acl_sess_list.prev,
					struct se_session, sess_acl_list);
		}
507
		spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
508 509 510 511 512 513
	}
}
EXPORT_SYMBOL(transport_deregister_session_configfs);

void transport_free_session(struct se_session *se_sess)
{
514
	struct se_node_acl *se_nacl = se_sess->se_node_acl;
515

516 517 518 519 520
	/*
	 * Drop the se_node_acl->nacl_kref obtained from within
	 * core_tpg_get_initiator_node_acl().
	 */
	if (se_nacl) {
521 522 523 524
		struct se_portal_group *se_tpg = se_nacl->se_tpg;
		const struct target_core_fabric_ops *se_tfo = se_tpg->se_tpg_tfo;
		unsigned long flags;

525
		se_sess->se_node_acl = NULL;
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540

		/*
		 * Also determine if we need to drop the extra ->cmd_kref if
		 * it had been previously dynamically generated, and
		 * the endpoint is not caching dynamic ACLs.
		 */
		mutex_lock(&se_tpg->acl_node_mutex);
		if (se_nacl->dynamic_node_acl &&
		    !se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
			spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
			if (list_empty(&se_nacl->acl_sess_list))
				se_nacl->dynamic_stop = true;
			spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);

			if (se_nacl->dynamic_stop)
541
				list_del_init(&se_nacl->acl_list);
542 543 544 545 546 547
		}
		mutex_unlock(&se_tpg->acl_node_mutex);

		if (se_nacl->dynamic_stop)
			target_put_nacl(se_nacl);

548 549
		target_put_nacl(se_nacl);
	}
550 551
	if (se_sess->sess_cmd_map) {
		percpu_ida_destroy(&se_sess->sess_tag_pool);
552
		kvfree(se_sess->sess_cmd_map);
553
	}
554 555 556 557 558 559 560
	kmem_cache_free(se_sess_cache, se_sess);
}
EXPORT_SYMBOL(transport_free_session);

void transport_deregister_session(struct se_session *se_sess)
{
	struct se_portal_group *se_tpg = se_sess->se_tpg;
561
	unsigned long flags;
562

563
	if (!se_tpg) {
564 565 566 567
		transport_free_session(se_sess);
		return;
	}

568
	spin_lock_irqsave(&se_tpg->session_lock, flags);
569 570 571
	list_del(&se_sess->sess_list);
	se_sess->se_tpg = NULL;
	se_sess->fabric_sess_ptr = NULL;
572
	spin_unlock_irqrestore(&se_tpg->session_lock, flags);
573

574
	pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
575
		se_tpg->se_tpg_tfo->get_fabric_name());
576
	/*
577
	 * If last kref is dropping now for an explicit NodeACL, awake sleeping
578
	 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
579
	 * removal context from within transport_free_session() code.
580 581 582
	 *
	 * For dynamic ACL, target_put_nacl() uses target_complete_nacl()
	 * to release all remaining generate_node_acl=1 created ACL resources.
583 584
	 */

585
	transport_free_session(se_sess);
586 587 588
}
EXPORT_SYMBOL(transport_deregister_session);

589
static void target_remove_from_state_list(struct se_cmd *cmd)
590
{
591
	struct se_device *dev = cmd->se_dev;
592 593
	unsigned long flags;

594 595
	if (!dev)
		return;
596

597 598 599 600
	spin_lock_irqsave(&dev->execute_task_lock, flags);
	if (cmd->state_active) {
		list_del(&cmd->state_list);
		cmd->state_active = false;
601
	}
602
	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
603 604
}

605
static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
606 607 608
{
	unsigned long flags;

609
	target_remove_from_state_list(cmd);
610

611 612 613 614
	/*
	 * Clear struct se_cmd->se_lun before the handoff to FE.
	 */
	cmd->se_lun = NULL;
615

616
	spin_lock_irqsave(&cmd->t_state_lock, flags);
617 618
	/*
	 * Determine if frontend context caller is requesting the stopping of
619
	 * this command for frontend exceptions.
620
	 */
621
	if (cmd->transport_state & CMD_T_STOP) {
622 623
		pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08llx\n",
			__func__, __LINE__, cmd->tag);
624

625
		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
626

627
		complete_all(&cmd->t_transport_stop_comp);
628 629
		return 1;
	}
630
	cmd->transport_state &= ~CMD_T_ACTIVE;
631
	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
632

633 634 635 636 637 638 639
	/*
	 * Some fabric modules like tcm_loop can release their internally
	 * allocated I/O reference and struct se_cmd now.
	 *
	 * Fabric modules are expected to return '1' here if the se_cmd being
	 * passed is released at this point, or zero if not being released.
	 */
640
	return cmd->se_tfo->check_stop_free(cmd);
641 642 643 644
}

static void transport_lun_remove_cmd(struct se_cmd *cmd)
{
645
	struct se_lun *lun = cmd->se_lun;
646

647
	if (!lun)
648 649
		return;

650 651
	if (cmpxchg(&cmd->lun_ref_active, true, false))
		percpu_ref_put(&lun->lun_ref);
652 653
}

654
int transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
655
{
656
	bool ack_kref = (cmd->se_cmd_flags & SCF_ACK_KREF);
657
	int ret = 0;
658

659 660
	if (cmd->se_cmd_flags & SCF_SE_LUN_CMD)
		transport_lun_remove_cmd(cmd);
661 662 663 664 665 666
	/*
	 * Allow the fabric driver to unmap any resources before
	 * releasing the descriptor via TFO->release_cmd()
	 */
	if (remove)
		cmd->se_tfo->aborted_task(cmd);
667

668
	if (transport_cmd_check_stop_to_fabric(cmd))
669
		return 1;
670
	if (remove && ack_kref)
671 672 673
		ret = transport_put_cmd(cmd);

	return ret;
674 675
}

676 677 678 679
static void target_complete_failure_work(struct work_struct *work)
{
	struct se_cmd *cmd = container_of(work, struct se_cmd, work);

680 681
	transport_generic_request_failure(cmd,
			TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE);
682 683
}

684
/*
685 686
 * Used when asking transport to copy Sense Data from the underlying
 * Linux/SCSI struct scsi_cmnd
687
 */
688
static unsigned char *transport_get_sense_buffer(struct se_cmd *cmd)
689 690 691 692 693 694
{
	struct se_device *dev = cmd->se_dev;

	WARN_ON(!cmd->se_lun);

	if (!dev)
695
		return NULL;
696

697 698
	if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION)
		return NULL;
699

700
	cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
701

702
	pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n",
703
		dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
704
	return cmd->sense_buffer;
705 706
}

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
void transport_copy_sense_to_cmd(struct se_cmd *cmd, unsigned char *sense)
{
	unsigned char *cmd_sense_buf;
	unsigned long flags;

	spin_lock_irqsave(&cmd->t_state_lock, flags);
	cmd_sense_buf = transport_get_sense_buffer(cmd);
	if (!cmd_sense_buf) {
		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
		return;
	}

	cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
	memcpy(cmd_sense_buf, sense, cmd->scsi_sense_length);
	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
}
EXPORT_SYMBOL(transport_copy_sense_to_cmd);

725
void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
726
{
727
	struct se_device *dev = cmd->se_dev;
728
	int success;
729 730
	unsigned long flags;

731 732
	cmd->scsi_status = scsi_status;

733
	spin_lock_irqsave(&cmd->t_state_lock, flags);
734 735
	switch (cmd->scsi_status) {
	case SAM_STAT_CHECK_CONDITION:
736
		if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)
737
			success = 1;
738 739 740 741
		else
			success = 0;
		break;
	default:
742
		success = 1;
743
		break;
744 745
	}

746
	/*
747
	 * Check for case where an explicit ABORT_TASK has been received
748 749
	 * and transport_wait_for_tasks() will be waiting for completion..
	 */
750
	if (cmd->transport_state & CMD_T_ABORTED ||
751 752
	    cmd->transport_state & CMD_T_STOP) {
		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
753 754 755 756 757 758 759 760 761
		/*
		 * If COMPARE_AND_WRITE was stopped by __transport_wait_for_tasks(),
		 * release se_device->caw_sem obtained by sbc_compare_and_write()
		 * since target_complete_ok_work() or target_complete_failure_work()
		 * won't be called to invoke the normal CAW completion callbacks.
		 */
		if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) {
			up(&dev->caw_sem);
		}
762
		complete_all(&cmd->t_transport_stop_comp);
763
		return;
764
	} else if (!success) {
765
		INIT_WORK(&cmd->work, target_complete_failure_work);
766
	} else {
767
		INIT_WORK(&cmd->work, target_complete_ok_work);
768
	}
769 770

	cmd->t_state = TRANSPORT_COMPLETE;
771
	cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
772
	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
773

774
	if (cmd->se_cmd_flags & SCF_USE_CPUID)
775
		queue_work_on(cmd->cpuid, target_completion_wq, &cmd->work);
776 777
	else
		queue_work(target_completion_wq, &cmd->work);
778
}
779 780
EXPORT_SYMBOL(target_complete_cmd);

781 782
void target_complete_cmd_with_length(struct se_cmd *cmd, u8 scsi_status, int length)
{
783
	if (scsi_status == SAM_STAT_GOOD && length < cmd->data_length) {
784 785 786 787 788 789 790 791 792 793 794 795 796 797
		if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
			cmd->residual_count += cmd->data_length - length;
		} else {
			cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
			cmd->residual_count = cmd->data_length - length;
		}

		cmd->data_length = length;
	}

	target_complete_cmd(cmd, scsi_status);
}
EXPORT_SYMBOL(target_complete_cmd_with_length);

798
static void target_add_to_state_list(struct se_cmd *cmd)
799
{
800 801
	struct se_device *dev = cmd->se_dev;
	unsigned long flags;
802

803 804 805 806
	spin_lock_irqsave(&dev->execute_task_lock, flags);
	if (!cmd->state_active) {
		list_add_tail(&cmd->state_list, &dev->state_list);
		cmd->state_active = true;
807
	}
808
	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
809 810
}

811
/*
812
 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
813
 */
814 815
static void transport_write_pending_qf(struct se_cmd *cmd);
static void transport_complete_qf(struct se_cmd *cmd);
816

817
void target_qf_do_work(struct work_struct *work)
818 819 820
{
	struct se_device *dev = container_of(work, struct se_device,
					qf_work_queue);
821
	LIST_HEAD(qf_cmd_list);
822 823 824
	struct se_cmd *cmd, *cmd_tmp;

	spin_lock_irq(&dev->qf_cmd_lock);
825 826
	list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
	spin_unlock_irq(&dev->qf_cmd_lock);
827

828
	list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
829
		list_del(&cmd->se_qf_node);
830
		atomic_dec_mb(&dev->dev_qf_count);
831

832
		pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
833
			" context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
834
			(cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
835 836
			(cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
			: "UNKNOWN");
837

838 839
		if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP)
			transport_write_pending_qf(cmd);
840 841
		else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK ||
			 cmd->t_state == TRANSPORT_COMPLETE_QF_ERR)
842
			transport_complete_qf(cmd);
843 844 845
	}
}

846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
{
	switch (cmd->data_direction) {
	case DMA_NONE:
		return "NONE";
	case DMA_FROM_DEVICE:
		return "READ";
	case DMA_TO_DEVICE:
		return "WRITE";
	case DMA_BIDIRECTIONAL:
		return "BIDI";
	default:
		break;
	}

	return "UNKNOWN";
}

void transport_dump_dev_state(
	struct se_device *dev,
	char *b,
	int *bl)
{
	*bl += sprintf(b + *bl, "Status: ");
870
	if (dev->export_count)
871
		*bl += sprintf(b + *bl, "ACTIVATED");
872
	else
873 874
		*bl += sprintf(b + *bl, "DEACTIVATED");

875
	*bl += sprintf(b + *bl, "  Max Queue Depth: %d", dev->queue_depth);
876
	*bl += sprintf(b + *bl, "  SectorSize: %u  HwMaxSectors: %u\n",
877 878
		dev->dev_attrib.block_size,
		dev->dev_attrib.hw_max_sectors);
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
	*bl += sprintf(b + *bl, "        ");
}

void transport_dump_vpd_proto_id(
	struct t10_vpd *vpd,
	unsigned char *p_buf,
	int p_buf_len)
{
	unsigned char buf[VPD_TMP_BUF_SIZE];
	int len;

	memset(buf, 0, VPD_TMP_BUF_SIZE);
	len = sprintf(buf, "T10 VPD Protocol Identifier: ");

	switch (vpd->protocol_identifier) {
	case 0x00:
		sprintf(buf+len, "Fibre Channel\n");
		break;
	case 0x10:
		sprintf(buf+len, "Parallel SCSI\n");
		break;
	case 0x20:
		sprintf(buf+len, "SSA\n");
		break;
	case 0x30:
		sprintf(buf+len, "IEEE 1394\n");
		break;
	case 0x40:
		sprintf(buf+len, "SCSI Remote Direct Memory Access"
				" Protocol\n");
		break;
	case 0x50:
		sprintf(buf+len, "Internet SCSI (iSCSI)\n");
		break;
	case 0x60:
		sprintf(buf+len, "SAS Serial SCSI Protocol\n");
		break;
	case 0x70:
		sprintf(buf+len, "Automation/Drive Interface Transport"
				" Protocol\n");
		break;
	case 0x80:
		sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
		break;
	default:
		sprintf(buf+len, "Unknown 0x%02x\n",
				vpd->protocol_identifier);
		break;
	}

	if (p_buf)
		strncpy(p_buf, buf, p_buf_len);
	else
932
		pr_debug("%s", buf);
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
}

void
transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
{
	/*
	 * Check if the Protocol Identifier Valid (PIV) bit is set..
	 *
	 * from spc3r23.pdf section 7.5.1
	 */
	 if (page_83[1] & 0x80) {
		vpd->protocol_identifier = (page_83[0] & 0xf0);
		vpd->protocol_identifier_set = 1;
		transport_dump_vpd_proto_id(vpd, NULL, 0);
	}
}
EXPORT_SYMBOL(transport_set_vpd_proto_id);

int transport_dump_vpd_assoc(
	struct t10_vpd *vpd,
	unsigned char *p_buf,
	int p_buf_len)
{
	unsigned char buf[VPD_TMP_BUF_SIZE];
957 958
	int ret = 0;
	int len;
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974

	memset(buf, 0, VPD_TMP_BUF_SIZE);
	len = sprintf(buf, "T10 VPD Identifier Association: ");

	switch (vpd->association) {
	case 0x00:
		sprintf(buf+len, "addressed logical unit\n");
		break;
	case 0x10:
		sprintf(buf+len, "target port\n");
		break;
	case 0x20:
		sprintf(buf+len, "SCSI target device\n");
		break;
	default:
		sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
975
		ret = -EINVAL;
976 977 978 979 980 981
		break;
	}

	if (p_buf)
		strncpy(p_buf, buf, p_buf_len);
	else
982
		pr_debug("%s", buf);
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004

	return ret;
}

int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
{
	/*
	 * The VPD identification association..
	 *
	 * from spc3r23.pdf Section 7.6.3.1 Table 297
	 */
	vpd->association = (page_83[1] & 0x30);
	return transport_dump_vpd_assoc(vpd, NULL, 0);
}
EXPORT_SYMBOL(transport_set_vpd_assoc);

int transport_dump_vpd_ident_type(
	struct t10_vpd *vpd,
	unsigned char *p_buf,
	int p_buf_len)
{
	unsigned char buf[VPD_TMP_BUF_SIZE];
1005 1006
	int ret = 0;
	int len;
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032

	memset(buf, 0, VPD_TMP_BUF_SIZE);
	len = sprintf(buf, "T10 VPD Identifier Type: ");

	switch (vpd->device_identifier_type) {
	case 0x00:
		sprintf(buf+len, "Vendor specific\n");
		break;
	case 0x01:
		sprintf(buf+len, "T10 Vendor ID based\n");
		break;
	case 0x02:
		sprintf(buf+len, "EUI-64 based\n");
		break;
	case 0x03:
		sprintf(buf+len, "NAA\n");
		break;
	case 0x04:
		sprintf(buf+len, "Relative target port identifier\n");
		break;
	case 0x08:
		sprintf(buf+len, "SCSI name string\n");
		break;
	default:
		sprintf(buf+len, "Unsupported: 0x%02x\n",
				vpd->device_identifier_type);
1033
		ret = -EINVAL;
1034 1035 1036
		break;
	}

1037 1038 1039
	if (p_buf) {
		if (p_buf_len < strlen(buf)+1)
			return -EINVAL;
1040
		strncpy(p_buf, buf, p_buf_len);
1041
	} else {
1042
		pr_debug("%s", buf);
1043
	}
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071

	return ret;
}

int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
{
	/*
	 * The VPD identifier type..
	 *
	 * from spc3r23.pdf Section 7.6.3.1 Table 298
	 */
	vpd->device_identifier_type = (page_83[1] & 0x0f);
	return transport_dump_vpd_ident_type(vpd, NULL, 0);
}
EXPORT_SYMBOL(transport_set_vpd_ident_type);

int transport_dump_vpd_ident(
	struct t10_vpd *vpd,
	unsigned char *p_buf,
	int p_buf_len)
{
	unsigned char buf[VPD_TMP_BUF_SIZE];
	int ret = 0;

	memset(buf, 0, VPD_TMP_BUF_SIZE);

	switch (vpd->device_identifier_code_set) {
	case 0x01: /* Binary */
1072 1073
		snprintf(buf, sizeof(buf),
			"T10 VPD Binary Device Identifier: %s\n",
1074 1075 1076
			&vpd->device_identifier[0]);
		break;
	case 0x02: /* ASCII */
1077 1078
		snprintf(buf, sizeof(buf),
			"T10 VPD ASCII Device Identifier: %s\n",
1079 1080 1081
			&vpd->device_identifier[0]);
		break;
	case 0x03: /* UTF-8 */
1082 1083
		snprintf(buf, sizeof(buf),
			"T10 VPD UTF-8 Device Identifier: %s\n",
1084 1085 1086 1087 1088
			&vpd->device_identifier[0]);
		break;
	default:
		sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
			" 0x%02x", vpd->device_identifier_code_set);
1089
		ret = -EINVAL;
1090 1091 1092 1093 1094 1095
		break;
	}

	if (p_buf)
		strncpy(p_buf, buf, p_buf_len);
	else
1096
		pr_debug("%s", buf);
1097 1098 1099 1100 1101 1102 1103 1104

	return ret;
}

int
transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
{
	static const char hex_str[] = "0123456789abcdef";
1105
	int j = 0, i = 4; /* offset to start of the identifier */
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137

	/*
	 * The VPD Code Set (encoding)
	 *
	 * from spc3r23.pdf Section 7.6.3.1 Table 296
	 */
	vpd->device_identifier_code_set = (page_83[0] & 0x0f);
	switch (vpd->device_identifier_code_set) {
	case 0x01: /* Binary */
		vpd->device_identifier[j++] =
				hex_str[vpd->device_identifier_type];
		while (i < (4 + page_83[3])) {
			vpd->device_identifier[j++] =
				hex_str[(page_83[i] & 0xf0) >> 4];
			vpd->device_identifier[j++] =
				hex_str[page_83[i] & 0x0f];
			i++;
		}
		break;
	case 0x02: /* ASCII */
	case 0x03: /* UTF-8 */
		while (i < (4 + page_83[3]))
			vpd->device_identifier[j++] = page_83[i++];
		break;
	default:
		break;
	}

	return transport_dump_vpd_ident(vpd, NULL, 0);
}
EXPORT_SYMBOL(transport_set_vpd_ident);

1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
static sense_reason_t
target_check_max_data_sg_nents(struct se_cmd *cmd, struct se_device *dev,
			       unsigned int size)
{
	u32 mtl;

	if (!cmd->se_tfo->max_data_sg_nents)
		return TCM_NO_SENSE;
	/*
	 * Check if fabric enforced maximum SGL entries per I/O descriptor
	 * exceeds se_cmd->data_length.  If true, set SCF_UNDERFLOW_BIT +
	 * residual_count and reduce original cmd->data_length to maximum
	 * length based on single PAGE_SIZE entry scatter-lists.
	 */
	mtl = (cmd->se_tfo->max_data_sg_nents * PAGE_SIZE);
	if (cmd->data_length > mtl) {
		/*
		 * If an existing CDB overflow is present, calculate new residual
		 * based on CDB size minus fabric maximum transfer length.
		 *
		 * If an existing CDB underflow is present, calculate new residual
		 * based on original cmd->data_length minus fabric maximum transfer
		 * length.
		 *
		 * Otherwise, set the underflow residual based on cmd->data_length
		 * minus fabric maximum transfer length.
		 */
		if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
			cmd->residual_count = (size - mtl);
		} else if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
			u32 orig_dl = size + cmd->residual_count;
			cmd->residual_count = (orig_dl - mtl);
		} else {
			cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
			cmd->residual_count = (cmd->data_length - mtl);
		}
		cmd->data_length = mtl;
		/*
		 * Reset sbc_check_prot() calculated protection payload
		 * length based upon the new smaller MTL.
		 */
		if (cmd->prot_length) {
			u32 sectors = (mtl / dev->dev_attrib.block_size);
			cmd->prot_length = dev->prot_length * sectors;
		}
	}
	return TCM_NO_SENSE;
}

1187 1188
sense_reason_t
target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
1189 1190 1191 1192 1193 1194
{
	struct se_device *dev = cmd->se_dev;

	if (cmd->unknown_data_length) {
		cmd->data_length = size;
	} else if (size != cmd->data_length) {
1195
		pr_warn_ratelimited("TARGET_CORE[%s]: Expected Transfer Length:"
1196 1197 1198 1199
			" %u does not match SCSI CDB Length: %u for SAM Opcode:"
			" 0x%02x\n", cmd->se_tfo->get_fabric_name(),
				cmd->data_length, size, cmd->t_task_cdb[0]);

1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
		if (cmd->data_direction == DMA_TO_DEVICE) {
			if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
				pr_err_ratelimited("Rejecting underflow/overflow"
						   " for WRITE data CDB\n");
				return TCM_INVALID_CDB_FIELD;
			}
			/*
			 * Some fabric drivers like iscsi-target still expect to
			 * always reject overflow writes.  Reject this case until
			 * full fabric driver level support for overflow writes
			 * is introduced tree-wide.
			 */
			if (size > cmd->data_length) {
				pr_err_ratelimited("Rejecting overflow for"
						   " WRITE control CDB\n");
				return TCM_INVALID_CDB_FIELD;
			}
1217 1218 1219 1220 1221
		}
		/*
		 * Reject READ_* or WRITE_* with overflow/underflow for
		 * type SCF_SCSI_DATA_CDB.
		 */
1222
		if (dev->dev_attrib.block_size != 512)  {
1223 1224 1225 1226
			pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
				" CDB on non 512-byte sector setup subsystem"
				" plugin: %s\n", dev->transport->name);
			/* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
1227
			return TCM_INVALID_CDB_FIELD;
1228
		}
1229 1230 1231 1232 1233 1234
		/*
		 * For the overflow case keep the existing fabric provided
		 * ->data_length.  Otherwise for the underflow case, reset
		 * ->data_length to the smaller SCSI expected data transfer
		 * length.
		 */
1235 1236 1237 1238 1239 1240
		if (size > cmd->data_length) {
			cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
			cmd->residual_count = (size - cmd->data_length);
		} else {
			cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
			cmd->residual_count = (cmd->data_length - size);
1241
			cmd->data_length = size;
1242 1243 1244
		}
	}

1245
	return target_check_max_data_sg_nents(cmd, dev, size);
1246 1247 1248

}

1249 1250 1251
/*
 * Used by fabric modules containing a local struct se_cmd within their
 * fabric dependent per I/O descriptor.
1252 1253
 *
 * Preserves the value of @cmd->tag.
1254 1255 1256
 */
void transport_init_se_cmd(
	struct se_cmd *cmd,
1257
	const struct target_core_fabric_ops *tfo,
1258 1259 1260 1261 1262 1263
	struct se_session *se_sess,
	u32 data_length,
	int data_direction,
	int task_attr,
	unsigned char *sense_buffer)
{
1264
	INIT_LIST_HEAD(&cmd->se_delayed_node);
1265
	INIT_LIST_HEAD(&cmd->se_qf_node);
1266
	INIT_LIST_HEAD(&cmd->se_cmd_list);
1267
	INIT_LIST_HEAD(&cmd->state_list);
1268
	init_completion(&cmd->t_transport_stop_comp);
1269
	init_completion(&cmd->cmd_wait_comp);
1270
	spin_lock_init(&cmd->t_state_lock);
1271
	INIT_WORK(&cmd->work, NULL);
1272
	kref_init(&cmd->cmd_kref);
1273 1274 1275 1276 1277 1278 1279

	cmd->se_tfo = tfo;
	cmd->se_sess = se_sess;
	cmd->data_length = data_length;
	cmd->data_direction = data_direction;
	cmd->sam_task_attr = task_attr;
	cmd->sense_buffer = sense_buffer;
1280 1281

	cmd->state_active = false;
1282 1283 1284
}
EXPORT_SYMBOL(transport_init_se_cmd);

1285 1286
static sense_reason_t
transport_check_alloc_task_attr(struct se_cmd *cmd)
1287
{
1288 1289
	struct se_device *dev = cmd->se_dev;

1290 1291 1292 1293
	/*
	 * Check if SAM Task Attribute emulation is enabled for this
	 * struct se_device storage object
	 */
1294
	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1295 1296
		return 0;

1297
	if (cmd->sam_task_attr == TCM_ACA_TAG) {
Andy Grover's avatar