Linux Audio

Check our new training course

Loading...
v3.1
   1/*******************************************************************************
   2 * Filename:  target_core_transport.c
   3 *
   4 * This file contains the Generic Target Engine Core.
   5 *
   6 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
   7 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
   8 * Copyright (c) 2007-2010 Rising Tide Systems
   9 * Copyright (c) 2008-2010 Linux-iSCSI.org
  10 *
  11 * Nicholas A. Bellinger <nab@kernel.org>
  12 *
  13 * This program is free software; you can redistribute it and/or modify
  14 * it under the terms of the GNU General Public License as published by
  15 * the Free Software Foundation; either version 2 of the License, or
  16 * (at your option) any later version.
  17 *
  18 * This program is distributed in the hope that it will be useful,
  19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21 * GNU General Public License for more details.
  22 *
  23 * You should have received a copy of the GNU General Public License
  24 * along with this program; if not, write to the Free Software
  25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26 *
  27 ******************************************************************************/
  28
  29#include <linux/version.h>
  30#include <linux/net.h>
  31#include <linux/delay.h>
  32#include <linux/string.h>
  33#include <linux/timer.h>
  34#include <linux/slab.h>
  35#include <linux/blkdev.h>
  36#include <linux/spinlock.h>
  37#include <linux/kthread.h>
  38#include <linux/in.h>
  39#include <linux/cdrom.h>
 
 
  40#include <asm/unaligned.h>
  41#include <net/sock.h>
  42#include <net/tcp.h>
  43#include <scsi/scsi.h>
  44#include <scsi/scsi_cmnd.h>
  45#include <scsi/scsi_tcq.h>
  46
  47#include <target/target_core_base.h>
  48#include <target/target_core_device.h>
  49#include <target/target_core_tmr.h>
  50#include <target/target_core_tpg.h>
  51#include <target/target_core_transport.h>
  52#include <target/target_core_fabric_ops.h>
  53#include <target/target_core_configfs.h>
  54
 
  55#include "target_core_alua.h"
  56#include "target_core_hba.h"
  57#include "target_core_pr.h"
  58#include "target_core_scdb.h"
  59#include "target_core_ua.h"
  60
  61static int sub_api_initialized;
  62
  63static struct kmem_cache *se_cmd_cache;
  64static struct kmem_cache *se_sess_cache;
  65struct kmem_cache *se_tmr_req_cache;
  66struct kmem_cache *se_ua_cache;
  67struct kmem_cache *t10_pr_reg_cache;
  68struct kmem_cache *t10_alua_lu_gp_cache;
  69struct kmem_cache *t10_alua_lu_gp_mem_cache;
  70struct kmem_cache *t10_alua_tg_pt_gp_cache;
  71struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
  72
  73/* Used for transport_dev_get_map_*() */
  74typedef int (*map_func_t)(struct se_task *, u32);
  75
  76static int transport_generic_write_pending(struct se_cmd *);
  77static int transport_processing_thread(void *param);
  78static int __transport_execute_tasks(struct se_device *dev);
  79static void transport_complete_task_attr(struct se_cmd *cmd);
  80static int transport_complete_qf(struct se_cmd *cmd);
  81static void transport_handle_queue_full(struct se_cmd *cmd,
  82		struct se_device *dev, int (*qf_callback)(struct se_cmd *));
  83static void transport_direct_request_timeout(struct se_cmd *cmd);
  84static void transport_free_dev_tasks(struct se_cmd *cmd);
  85static u32 transport_allocate_tasks(struct se_cmd *cmd,
  86		unsigned long long starting_lba,
  87		enum dma_data_direction data_direction,
  88		struct scatterlist *sgl, unsigned int nents);
  89static int transport_generic_get_mem(struct se_cmd *cmd);
  90static int transport_generic_remove(struct se_cmd *cmd,
  91		int session_reinstatement);
  92static void transport_release_fe_cmd(struct se_cmd *cmd);
  93static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
  94		struct se_queue_obj *qobj);
  95static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
  96static void transport_stop_all_task_timers(struct se_cmd *cmd);
  97
  98int init_se_kmem_caches(void)
  99{
 100	se_cmd_cache = kmem_cache_create("se_cmd_cache",
 101			sizeof(struct se_cmd), __alignof__(struct se_cmd), 0, NULL);
 102	if (!se_cmd_cache) {
 103		pr_err("kmem_cache_create for struct se_cmd failed\n");
 104		goto out;
 105	}
 106	se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
 107			sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
 108			0, NULL);
 109	if (!se_tmr_req_cache) {
 110		pr_err("kmem_cache_create() for struct se_tmr_req"
 111				" failed\n");
 112		goto out;
 113	}
 114	se_sess_cache = kmem_cache_create("se_sess_cache",
 115			sizeof(struct se_session), __alignof__(struct se_session),
 116			0, NULL);
 117	if (!se_sess_cache) {
 118		pr_err("kmem_cache_create() for struct se_session"
 119				" failed\n");
 120		goto out;
 121	}
 122	se_ua_cache = kmem_cache_create("se_ua_cache",
 123			sizeof(struct se_ua), __alignof__(struct se_ua),
 124			0, NULL);
 125	if (!se_ua_cache) {
 126		pr_err("kmem_cache_create() for struct se_ua failed\n");
 127		goto out;
 128	}
 129	t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
 130			sizeof(struct t10_pr_registration),
 131			__alignof__(struct t10_pr_registration), 0, NULL);
 132	if (!t10_pr_reg_cache) {
 133		pr_err("kmem_cache_create() for struct t10_pr_registration"
 134				" failed\n");
 135		goto out;
 136	}
 137	t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
 138			sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
 139			0, NULL);
 140	if (!t10_alua_lu_gp_cache) {
 141		pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
 142				" failed\n");
 143		goto out;
 144	}
 145	t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
 146			sizeof(struct t10_alua_lu_gp_member),
 147			__alignof__(struct t10_alua_lu_gp_member), 0, NULL);
 148	if (!t10_alua_lu_gp_mem_cache) {
 149		pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
 150				"cache failed\n");
 151		goto out;
 152	}
 153	t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
 154			sizeof(struct t10_alua_tg_pt_gp),
 155			__alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
 156	if (!t10_alua_tg_pt_gp_cache) {
 157		pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
 158				"cache failed\n");
 159		goto out;
 160	}
 161	t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
 162			"t10_alua_tg_pt_gp_mem_cache",
 163			sizeof(struct t10_alua_tg_pt_gp_member),
 164			__alignof__(struct t10_alua_tg_pt_gp_member),
 165			0, NULL);
 166	if (!t10_alua_tg_pt_gp_mem_cache) {
 167		pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
 168				"mem_t failed\n");
 169		goto out;
 170	}
 171
 
 
 
 
 
 172	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 173out:
 174	if (se_cmd_cache)
 175		kmem_cache_destroy(se_cmd_cache);
 176	if (se_tmr_req_cache)
 177		kmem_cache_destroy(se_tmr_req_cache);
 178	if (se_sess_cache)
 179		kmem_cache_destroy(se_sess_cache);
 180	if (se_ua_cache)
 181		kmem_cache_destroy(se_ua_cache);
 182	if (t10_pr_reg_cache)
 183		kmem_cache_destroy(t10_pr_reg_cache);
 184	if (t10_alua_lu_gp_cache)
 185		kmem_cache_destroy(t10_alua_lu_gp_cache);
 186	if (t10_alua_lu_gp_mem_cache)
 187		kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
 188	if (t10_alua_tg_pt_gp_cache)
 189		kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
 190	if (t10_alua_tg_pt_gp_mem_cache)
 191		kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
 192	return -ENOMEM;
 193}
 194
 195void release_se_kmem_caches(void)
 196{
 197	kmem_cache_destroy(se_cmd_cache);
 198	kmem_cache_destroy(se_tmr_req_cache);
 199	kmem_cache_destroy(se_sess_cache);
 200	kmem_cache_destroy(se_ua_cache);
 201	kmem_cache_destroy(t10_pr_reg_cache);
 202	kmem_cache_destroy(t10_alua_lu_gp_cache);
 203	kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
 204	kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
 205	kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
 206}
 207
 208/* This code ensures unique mib indexes are handed out. */
 209static DEFINE_SPINLOCK(scsi_mib_index_lock);
 210static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
 211
 212/*
 213 * Allocate a new row index for the entry type specified
 214 */
 215u32 scsi_get_new_index(scsi_index_t type)
 216{
 217	u32 new_index;
 218
 219	BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
 220
 221	spin_lock(&scsi_mib_index_lock);
 222	new_index = ++scsi_mib_index[type];
 223	spin_unlock(&scsi_mib_index_lock);
 224
 225	return new_index;
 226}
 227
 228void transport_init_queue_obj(struct se_queue_obj *qobj)
 229{
 230	atomic_set(&qobj->queue_cnt, 0);
 231	INIT_LIST_HEAD(&qobj->qobj_list);
 232	init_waitqueue_head(&qobj->thread_wq);
 233	spin_lock_init(&qobj->cmd_queue_lock);
 234}
 235EXPORT_SYMBOL(transport_init_queue_obj);
 236
 237static int transport_subsystem_reqmods(void)
 238{
 239	int ret;
 240
 
 
 
 241	ret = request_module("target_core_iblock");
 242	if (ret != 0)
 243		pr_err("Unable to load target_core_iblock\n");
 244
 245	ret = request_module("target_core_file");
 246	if (ret != 0)
 247		pr_err("Unable to load target_core_file\n");
 248
 249	ret = request_module("target_core_pscsi");
 250	if (ret != 0)
 251		pr_err("Unable to load target_core_pscsi\n");
 252
 253	ret = request_module("target_core_stgt");
 254	if (ret != 0)
 255		pr_err("Unable to load target_core_stgt\n");
 256
 257	return 0;
 258}
 259
 260int transport_subsystem_check_init(void)
 261{
 262	int ret;
 263
 264	if (sub_api_initialized)
 265		return 0;
 266	/*
 267	 * Request the loading of known TCM subsystem plugins..
 268	 */
 269	ret = transport_subsystem_reqmods();
 270	if (ret < 0)
 271		return ret;
 272
 273	sub_api_initialized = 1;
 274	return 0;
 275}
 276
 277struct se_session *transport_init_session(void)
 278{
 279	struct se_session *se_sess;
 280
 281	se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
 282	if (!se_sess) {
 283		pr_err("Unable to allocate struct se_session from"
 284				" se_sess_cache\n");
 285		return ERR_PTR(-ENOMEM);
 286	}
 287	INIT_LIST_HEAD(&se_sess->sess_list);
 288	INIT_LIST_HEAD(&se_sess->sess_acl_list);
 
 
 
 
 289
 290	return se_sess;
 291}
 292EXPORT_SYMBOL(transport_init_session);
 293
 294/*
 295 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
 296 */
 297void __transport_register_session(
 298	struct se_portal_group *se_tpg,
 299	struct se_node_acl *se_nacl,
 300	struct se_session *se_sess,
 301	void *fabric_sess_ptr)
 302{
 303	unsigned char buf[PR_REG_ISID_LEN];
 304
 305	se_sess->se_tpg = se_tpg;
 306	se_sess->fabric_sess_ptr = fabric_sess_ptr;
 307	/*
 308	 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
 309	 *
 310	 * Only set for struct se_session's that will actually be moving I/O.
 311	 * eg: *NOT* discovery sessions.
 312	 */
 313	if (se_nacl) {
 314		/*
 315		 * If the fabric module supports an ISID based TransportID,
 316		 * save this value in binary from the fabric I_T Nexus now.
 317		 */
 318		if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
 319			memset(&buf[0], 0, PR_REG_ISID_LEN);
 320			se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
 321					&buf[0], PR_REG_ISID_LEN);
 322			se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
 323		}
 
 
 324		spin_lock_irq(&se_nacl->nacl_sess_lock);
 325		/*
 326		 * The se_nacl->nacl_sess pointer will be set to the
 327		 * last active I_T Nexus for each struct se_node_acl.
 328		 */
 329		se_nacl->nacl_sess = se_sess;
 330
 331		list_add_tail(&se_sess->sess_acl_list,
 332			      &se_nacl->acl_sess_list);
 333		spin_unlock_irq(&se_nacl->nacl_sess_lock);
 334	}
 335	list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
 336
 337	pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
 338		se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
 339}
 340EXPORT_SYMBOL(__transport_register_session);
 341
 342void transport_register_session(
 343	struct se_portal_group *se_tpg,
 344	struct se_node_acl *se_nacl,
 345	struct se_session *se_sess,
 346	void *fabric_sess_ptr)
 347{
 348	spin_lock_bh(&se_tpg->session_lock);
 
 
 349	__transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
 350	spin_unlock_bh(&se_tpg->session_lock);
 351}
 352EXPORT_SYMBOL(transport_register_session);
 353
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 354void transport_deregister_session_configfs(struct se_session *se_sess)
 355{
 356	struct se_node_acl *se_nacl;
 357	unsigned long flags;
 358	/*
 359	 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
 360	 */
 361	se_nacl = se_sess->se_node_acl;
 362	if (se_nacl) {
 363		spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
 364		list_del(&se_sess->sess_acl_list);
 
 365		/*
 366		 * If the session list is empty, then clear the pointer.
 367		 * Otherwise, set the struct se_session pointer from the tail
 368		 * element of the per struct se_node_acl active session list.
 369		 */
 370		if (list_empty(&se_nacl->acl_sess_list))
 371			se_nacl->nacl_sess = NULL;
 372		else {
 373			se_nacl->nacl_sess = container_of(
 374					se_nacl->acl_sess_list.prev,
 375					struct se_session, sess_acl_list);
 376		}
 377		spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
 378	}
 379}
 380EXPORT_SYMBOL(transport_deregister_session_configfs);
 381
 382void transport_free_session(struct se_session *se_sess)
 383{
 384	kmem_cache_free(se_sess_cache, se_sess);
 385}
 386EXPORT_SYMBOL(transport_free_session);
 387
 388void transport_deregister_session(struct se_session *se_sess)
 389{
 390	struct se_portal_group *se_tpg = se_sess->se_tpg;
 
 391	struct se_node_acl *se_nacl;
 392	unsigned long flags;
 
 393
 394	if (!se_tpg) {
 395		transport_free_session(se_sess);
 396		return;
 397	}
 
 398
 399	spin_lock_irqsave(&se_tpg->session_lock, flags);
 400	list_del(&se_sess->sess_list);
 401	se_sess->se_tpg = NULL;
 402	se_sess->fabric_sess_ptr = NULL;
 403	spin_unlock_irqrestore(&se_tpg->session_lock, flags);
 404
 405	/*
 406	 * Determine if we need to do extra work for this initiator node's
 407	 * struct se_node_acl if it had been previously dynamically generated.
 408	 */
 409	se_nacl = se_sess->se_node_acl;
 410	if (se_nacl) {
 411		spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
 412		if (se_nacl->dynamic_node_acl) {
 413			if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
 414					se_tpg)) {
 415				list_del(&se_nacl->acl_list);
 416				se_tpg->num_node_acls--;
 417				spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
 418
 419				core_tpg_wait_for_nacl_pr_ref(se_nacl);
 420				core_free_device_list_for_node(se_nacl, se_tpg);
 421				se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
 422						se_nacl);
 423				spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
 424			}
 425		}
 426		spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
 427	}
 428
 429	transport_free_session(se_sess);
 430
 431	pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
 432		se_tpg->se_tpg_tfo->get_fabric_name());
 
 
 
 
 
 
 
 
 
 433}
 434EXPORT_SYMBOL(transport_deregister_session);
 435
 436/*
 437 * Called with cmd->t_state_lock held.
 438 */
 439static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
 440{
 441	struct se_device *dev;
 442	struct se_task *task;
 443	unsigned long flags;
 444
 445	list_for_each_entry(task, &cmd->t_task_list, t_list) {
 446		dev = task->se_dev;
 447		if (!dev)
 448			continue;
 449
 450		if (atomic_read(&task->task_active))
 451			continue;
 452
 453		if (!atomic_read(&task->task_state_active))
 454			continue;
 455
 456		spin_lock_irqsave(&dev->execute_task_lock, flags);
 457		list_del(&task->t_state_list);
 458		pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
 459			cmd->se_tfo->get_task_tag(cmd), dev, task);
 460		spin_unlock_irqrestore(&dev->execute_task_lock, flags);
 461
 462		atomic_set(&task->task_state_active, 0);
 463		atomic_dec(&cmd->t_task_cdbs_ex_left);
 
 
 464	}
 
 465}
 466
 467/*	transport_cmd_check_stop():
 468 *
 469 *	'transport_off = 1' determines if t_transport_active should be cleared.
 470 *	'transport_off = 2' determines if task_dev_state should be removed.
 471 *
 472 *	A non-zero u8 t_state sets cmd->t_state.
 473 *	Returns 1 when command is stopped, else 0.
 474 */
 475static int transport_cmd_check_stop(
 476	struct se_cmd *cmd,
 477	int transport_off,
 478	u8 t_state)
 479{
 480	unsigned long flags;
 481
 482	spin_lock_irqsave(&cmd->t_state_lock, flags);
 483	/*
 484	 * Determine if IOCTL context caller in requesting the stopping of this
 485	 * command for LUN shutdown purposes.
 486	 */
 487	if (atomic_read(&cmd->transport_lun_stop)) {
 488		pr_debug("%s:%d atomic_read(&cmd->transport_lun_stop)"
 489			" == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
 490			cmd->se_tfo->get_task_tag(cmd));
 491
 492		cmd->deferred_t_state = cmd->t_state;
 493		cmd->t_state = TRANSPORT_DEFERRED_CMD;
 494		atomic_set(&cmd->t_transport_active, 0);
 495		if (transport_off == 2)
 496			transport_all_task_dev_remove_state(cmd);
 497		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 498
 499		complete(&cmd->transport_lun_stop_comp);
 500		return 1;
 501	}
 502	/*
 503	 * Determine if frontend context caller is requesting the stopping of
 504	 * this command for frontend exceptions.
 505	 */
 506	if (atomic_read(&cmd->t_transport_stop)) {
 507		pr_debug("%s:%d atomic_read(&cmd->t_transport_stop) =="
 508			" TRUE for ITT: 0x%08x\n", __func__, __LINE__,
 509			cmd->se_tfo->get_task_tag(cmd));
 510
 511		cmd->deferred_t_state = cmd->t_state;
 512		cmd->t_state = TRANSPORT_DEFERRED_CMD;
 513		if (transport_off == 2)
 514			transport_all_task_dev_remove_state(cmd);
 515
 516		/*
 517		 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
 518		 * to FE.
 519		 */
 520		if (transport_off == 2)
 521			cmd->se_lun = NULL;
 522		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 523
 524		complete(&cmd->t_transport_stop_comp);
 525		return 1;
 526	}
 527	if (transport_off) {
 528		atomic_set(&cmd->t_transport_active, 0);
 529		if (transport_off == 2) {
 530			transport_all_task_dev_remove_state(cmd);
 531			/*
 532			 * Clear struct se_cmd->se_lun before the transport_off == 2
 533			 * handoff to fabric module.
 534			 */
 535			cmd->se_lun = NULL;
 536			/*
 537			 * Some fabric modules like tcm_loop can release
 538			 * their internally allocated I/O reference now and
 539			 * struct se_cmd now.
 
 
 
 
 540			 */
 541			if (cmd->se_tfo->check_stop_free != NULL) {
 542				spin_unlock_irqrestore(
 543					&cmd->t_state_lock, flags);
 544
 545				cmd->se_tfo->check_stop_free(cmd);
 546				return 1;
 547			}
 548		}
 549		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 550
 551		return 0;
 552	} else if (t_state)
 553		cmd->t_state = t_state;
 554	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 555
 556	return 0;
 557}
 558
 559static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
 560{
 561	return transport_cmd_check_stop(cmd, 2, 0);
 562}
 563
 564static void transport_lun_remove_cmd(struct se_cmd *cmd)
 565{
 566	struct se_lun *lun = cmd->se_lun;
 567	unsigned long flags;
 568
 569	if (!lun)
 570		return;
 571
 572	spin_lock_irqsave(&cmd->t_state_lock, flags);
 573	if (!atomic_read(&cmd->transport_dev_active)) {
 574		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 575		goto check_lun;
 576	}
 577	atomic_set(&cmd->transport_dev_active, 0);
 578	transport_all_task_dev_remove_state(cmd);
 579	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 580
 581
 582check_lun:
 583	spin_lock_irqsave(&lun->lun_cmd_lock, flags);
 584	if (atomic_read(&cmd->transport_lun_active)) {
 585		list_del(&cmd->se_lun_node);
 586		atomic_set(&cmd->transport_lun_active, 0);
 587#if 0
 588		pr_debug("Removed ITT: 0x%08x from LUN LIST[%d]\n"
 589			cmd->se_tfo->get_task_tag(cmd), lun->unpacked_lun);
 590#endif
 591	}
 592	spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
 593}
 594
 595void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
 596{
 597	transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj);
 598	transport_lun_remove_cmd(cmd);
 599
 600	if (transport_cmd_check_stop_to_fabric(cmd))
 601		return;
 602	if (remove)
 603		transport_generic_remove(cmd, 0);
 604}
 605
 606void transport_cmd_finish_abort_tmr(struct se_cmd *cmd)
 607{
 608	transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj);
 609
 610	if (transport_cmd_check_stop_to_fabric(cmd))
 611		return;
 612
 613	transport_generic_remove(cmd, 0);
 
 
 614}
 615
 616static void transport_add_cmd_to_queue(
 617	struct se_cmd *cmd,
 618	int t_state)
 619{
 620	struct se_device *dev = cmd->se_dev;
 621	struct se_queue_obj *qobj = &dev->dev_queue_obj;
 622	unsigned long flags;
 623
 624	INIT_LIST_HEAD(&cmd->se_queue_node);
 625
 626	if (t_state) {
 627		spin_lock_irqsave(&cmd->t_state_lock, flags);
 628		cmd->t_state = t_state;
 629		atomic_set(&cmd->t_transport_active, 1);
 630		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 631	}
 632
 633	spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
 634	if (cmd->se_cmd_flags & SCF_EMULATE_QUEUE_FULL) {
 635		cmd->se_cmd_flags &= ~SCF_EMULATE_QUEUE_FULL;
 
 
 
 
 
 
 636		list_add(&cmd->se_queue_node, &qobj->qobj_list);
 637	} else
 638		list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
 639	atomic_inc(&cmd->t_transport_queue_active);
 640	spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
 641
 642	atomic_inc(&qobj->queue_cnt);
 643	wake_up_interruptible(&qobj->thread_wq);
 644}
 645
 646static struct se_cmd *
 647transport_get_cmd_from_queue(struct se_queue_obj *qobj)
 648{
 649	struct se_cmd *cmd;
 650	unsigned long flags;
 651
 652	spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
 653	if (list_empty(&qobj->qobj_list)) {
 654		spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
 655		return NULL;
 656	}
 657	cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
 658
 659	atomic_dec(&cmd->t_transport_queue_active);
 660
 661	list_del(&cmd->se_queue_node);
 662	atomic_dec(&qobj->queue_cnt);
 663	spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
 664
 665	return cmd;
 666}
 667
 668static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
 669		struct se_queue_obj *qobj)
 670{
 671	struct se_cmd *t;
 672	unsigned long flags;
 673
 674	spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
 675	if (!atomic_read(&cmd->t_transport_queue_active)) {
 676		spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
 677		return;
 678	}
 679
 680	list_for_each_entry(t, &qobj->qobj_list, se_queue_node)
 681		if (t == cmd) {
 682			atomic_dec(&cmd->t_transport_queue_active);
 683			atomic_dec(&qobj->queue_cnt);
 684			list_del(&cmd->se_queue_node);
 685			break;
 686		}
 687	spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
 688
 689	if (atomic_read(&cmd->t_transport_queue_active)) {
 690		pr_err("ITT: 0x%08x t_transport_queue_active: %d\n",
 691			cmd->se_tfo->get_task_tag(cmd),
 692			atomic_read(&cmd->t_transport_queue_active));
 693	}
 694}
 695
 696/*
 697 * Completion function used by TCM subsystem plugins (such as FILEIO)
 698 * for queueing up response from struct se_subsystem_api->do_task()
 699 */
 700void transport_complete_sync_cache(struct se_cmd *cmd, int good)
 701{
 702	struct se_task *task = list_entry(cmd->t_task_list.next,
 703				struct se_task, t_list);
 704
 705	if (good) {
 706		cmd->scsi_status = SAM_STAT_GOOD;
 707		task->task_scsi_status = GOOD;
 708	} else {
 709		task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
 710		task->task_error_status = PYX_TRANSPORT_ILLEGAL_REQUEST;
 711		task->task_se_cmd->transport_error_status =
 712					PYX_TRANSPORT_ILLEGAL_REQUEST;
 713	}
 714
 715	transport_complete_task(task, good);
 716}
 717EXPORT_SYMBOL(transport_complete_sync_cache);
 718
 719/*	transport_complete_task():
 720 *
 721 *	Called from interrupt and non interrupt context depending
 722 *	on the transport plugin.
 723 */
 724void transport_complete_task(struct se_task *task, int success)
 725{
 726	struct se_cmd *cmd = task->task_se_cmd;
 727	struct se_device *dev = task->se_dev;
 728	int t_state;
 729	unsigned long flags;
 730#if 0
 731	pr_debug("task: %p CDB: 0x%02x obj_ptr: %p\n", task,
 732			cmd->t_task_cdb[0], dev);
 733#endif
 734	if (dev)
 735		atomic_inc(&dev->depth_left);
 736
 737	spin_lock_irqsave(&cmd->t_state_lock, flags);
 738	atomic_set(&task->task_active, 0);
 739
 740	/*
 741	 * See if any sense data exists, if so set the TASK_SENSE flag.
 742	 * Also check for any other post completion work that needs to be
 743	 * done by the plugins.
 744	 */
 745	if (dev && dev->transport->transport_complete) {
 746		if (dev->transport->transport_complete(task) != 0) {
 
 747			cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
 748			task->task_sense = 1;
 749			success = 1;
 750		}
 751	}
 752
 753	/*
 754	 * See if we are waiting for outstanding struct se_task
 755	 * to complete for an exception condition
 756	 */
 757	if (atomic_read(&task->task_stop)) {
 758		/*
 759		 * Decrement cmd->t_se_count if this task had
 760		 * previously thrown its timeout exception handler.
 761		 */
 762		if (atomic_read(&task->task_timeout)) {
 763			atomic_dec(&cmd->t_se_count);
 764			atomic_set(&task->task_timeout, 0);
 765		}
 766		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 767
 768		complete(&task->task_stop_comp);
 769		return;
 770	}
 771	/*
 772	 * If the task's timeout handler has fired, use the t_task_cdbs_timeout
 773	 * left counter to determine when the struct se_cmd is ready to be queued to
 774	 * the processing thread.
 775	 */
 776	if (atomic_read(&task->task_timeout)) {
 777		if (!atomic_dec_and_test(
 778				&cmd->t_task_cdbs_timeout_left)) {
 779			spin_unlock_irqrestore(&cmd->t_state_lock,
 780				flags);
 781			return;
 782		}
 783		t_state = TRANSPORT_COMPLETE_TIMEOUT;
 784		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 785
 786		transport_add_cmd_to_queue(cmd, t_state);
 787		return;
 788	}
 789	atomic_dec(&cmd->t_task_cdbs_timeout_left);
 790
 791	/*
 792	 * Decrement the outstanding t_task_cdbs_left count.  The last
 793	 * struct se_task from struct se_cmd will complete itself into the
 794	 * device queue depending upon int success.
 795	 */
 796	if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
 797		if (!success)
 798			cmd->t_tasks_failed = 1;
 799
 800		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 
 801		return;
 802	}
 803
 804	if (!success || cmd->t_tasks_failed) {
 805		t_state = TRANSPORT_COMPLETE_FAILURE;
 806		if (!task->task_error_status) {
 807			task->task_error_status =
 808				PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
 809			cmd->transport_error_status =
 810				PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
 811		}
 812	} else {
 813		atomic_set(&cmd->t_transport_complete, 1);
 814		t_state = TRANSPORT_COMPLETE_OK;
 815	}
 
 
 
 816	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 817
 818	transport_add_cmd_to_queue(cmd, t_state);
 819}
 820EXPORT_SYMBOL(transport_complete_task);
 821
 822/*
 823 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
 824 * struct se_task list are ready to be added to the active execution list
 825 * struct se_device
 826
 827 * Called with se_dev_t->execute_task_lock called.
 828 */
 829static inline int transport_add_task_check_sam_attr(
 830	struct se_task *task,
 831	struct se_task *task_prev,
 832	struct se_device *dev)
 833{
 834	/*
 835	 * No SAM Task attribute emulation enabled, add to tail of
 836	 * execution queue
 837	 */
 838	if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
 839		list_add_tail(&task->t_execute_list, &dev->execute_task_list);
 840		return 0;
 841	}
 842	/*
 843	 * HEAD_OF_QUEUE attribute for received CDB, which means
 844	 * the first task that is associated with a struct se_cmd goes to
 845	 * head of the struct se_device->execute_task_list, and task_prev
 846	 * after that for each subsequent task
 847	 */
 848	if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
 849		list_add(&task->t_execute_list,
 850				(task_prev != NULL) ?
 851				&task_prev->t_execute_list :
 852				&dev->execute_task_list);
 853
 854		pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
 855				" in execution queue\n",
 856				task->task_se_cmd->t_task_cdb[0]);
 857		return 1;
 858	}
 859	/*
 860	 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
 861	 * transitioned from Dermant -> Active state, and are added to the end
 862	 * of the struct se_device->execute_task_list
 863	 */
 864	list_add_tail(&task->t_execute_list, &dev->execute_task_list);
 865	return 0;
 866}
 867
 868/*	__transport_add_task_to_execute_queue():
 869 *
 870 *	Called with se_dev_t->execute_task_lock called.
 871 */
 872static void __transport_add_task_to_execute_queue(
 873	struct se_task *task,
 874	struct se_task *task_prev,
 875	struct se_device *dev)
 876{
 877	int head_of_queue;
 878
 879	head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
 880	atomic_inc(&dev->execute_tasks);
 881
 882	if (atomic_read(&task->task_state_active))
 883		return;
 884	/*
 885	 * Determine if this task needs to go to HEAD_OF_QUEUE for the
 886	 * state list as well.  Running with SAM Task Attribute emulation
 887	 * will always return head_of_queue == 0 here
 888	 */
 889	if (head_of_queue)
 890		list_add(&task->t_state_list, (task_prev) ?
 891				&task_prev->t_state_list :
 892				&dev->state_task_list);
 893	else
 894		list_add_tail(&task->t_state_list, &dev->state_task_list);
 895
 896	atomic_set(&task->task_state_active, 1);
 
 
 897
 898	pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
 899		task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
 900		task, dev);
 901}
 902
 903static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
 904{
 905	struct se_device *dev;
 906	struct se_task *task;
 907	unsigned long flags;
 908
 909	spin_lock_irqsave(&cmd->t_state_lock, flags);
 910	list_for_each_entry(task, &cmd->t_task_list, t_list) {
 911		dev = task->se_dev;
 912
 913		if (atomic_read(&task->task_state_active))
 914			continue;
 915
 916		spin_lock(&dev->execute_task_lock);
 917		list_add_tail(&task->t_state_list, &dev->state_task_list);
 918		atomic_set(&task->task_state_active, 1);
 919
 920		pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
 921			task->task_se_cmd->se_tfo->get_task_tag(
 922			task->task_se_cmd), task, dev);
 923
 924		spin_unlock(&dev->execute_task_lock);
 925	}
 926	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 927}
 928
 929static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
 930{
 931	struct se_device *dev = cmd->se_dev;
 932	struct se_task *task, *task_prev = NULL;
 933	unsigned long flags;
 
 934
 935	spin_lock_irqsave(&dev->execute_task_lock, flags);
 936	list_for_each_entry(task, &cmd->t_task_list, t_list) {
 937		if (atomic_read(&task->task_execute_queue))
 938			continue;
 939		/*
 940		 * __transport_add_task_to_execute_queue() handles the
 941		 * SAM Task Attribute emulation if enabled
 942		 */
 943		__transport_add_task_to_execute_queue(task, task_prev, dev);
 944		atomic_set(&task->task_execute_queue, 1);
 945		task_prev = task;
 946	}
 947	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
 948}
 949
 950/*	transport_remove_task_from_execute_queue():
 951 *
 952 *
 953 */
 954void transport_remove_task_from_execute_queue(
 955	struct se_task *task,
 956	struct se_device *dev)
 957{
 
 
 
 
 
 
 
 958	unsigned long flags;
 959
 960	if (atomic_read(&task->task_execute_queue) == 0) {
 961		dump_stack();
 962		return;
 963	}
 964
 965	spin_lock_irqsave(&dev->execute_task_lock, flags);
 966	list_del(&task->t_execute_list);
 967	atomic_set(&task->task_execute_queue, 0);
 968	atomic_dec(&dev->execute_tasks);
 969	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
 970}
 971
 972/*
 973 * Handle QUEUE_FULL / -EAGAIN status
 974 */
 975
 976static void target_qf_do_work(struct work_struct *work)
 977{
 978	struct se_device *dev = container_of(work, struct se_device,
 979					qf_work_queue);
 980	LIST_HEAD(qf_cmd_list);
 981	struct se_cmd *cmd, *cmd_tmp;
 982
 983	spin_lock_irq(&dev->qf_cmd_lock);
 984	list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
 985	spin_unlock_irq(&dev->qf_cmd_lock);
 986
 987	list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
 988		list_del(&cmd->se_qf_node);
 989		atomic_dec(&dev->dev_qf_count);
 990		smp_mb__after_atomic_dec();
 991
 992		pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
 993			" context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
 994			(cmd->t_state == TRANSPORT_COMPLETE_OK) ? "COMPLETE_OK" :
 995			(cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
 996			: "UNKNOWN");
 997		/*
 998		 * The SCF_EMULATE_QUEUE_FULL flag will be cleared once se_cmd
 999		 * has been added to head of queue
1000		 */
1001		transport_add_cmd_to_queue(cmd, cmd->t_state);
1002	}
1003}
1004
1005unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
1006{
1007	switch (cmd->data_direction) {
1008	case DMA_NONE:
1009		return "NONE";
1010	case DMA_FROM_DEVICE:
1011		return "READ";
1012	case DMA_TO_DEVICE:
1013		return "WRITE";
1014	case DMA_BIDIRECTIONAL:
1015		return "BIDI";
1016	default:
1017		break;
1018	}
1019
1020	return "UNKNOWN";
1021}
1022
1023void transport_dump_dev_state(
1024	struct se_device *dev,
1025	char *b,
1026	int *bl)
1027{
1028	*bl += sprintf(b + *bl, "Status: ");
1029	switch (dev->dev_status) {
1030	case TRANSPORT_DEVICE_ACTIVATED:
1031		*bl += sprintf(b + *bl, "ACTIVATED");
1032		break;
1033	case TRANSPORT_DEVICE_DEACTIVATED:
1034		*bl += sprintf(b + *bl, "DEACTIVATED");
1035		break;
1036	case TRANSPORT_DEVICE_SHUTDOWN:
1037		*bl += sprintf(b + *bl, "SHUTDOWN");
1038		break;
1039	case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
1040	case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
1041		*bl += sprintf(b + *bl, "OFFLINE");
1042		break;
1043	default:
1044		*bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
1045		break;
1046	}
1047
1048	*bl += sprintf(b + *bl, "  Execute/Left/Max Queue Depth: %d/%d/%d",
1049		atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
1050		dev->queue_depth);
1051	*bl += sprintf(b + *bl, "  SectorSize: %u  MaxSectors: %u\n",
1052		dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors);
1053	*bl += sprintf(b + *bl, "        ");
1054}
1055
1056/*	transport_release_all_cmds():
1057 *
1058 *
1059 */
1060static void transport_release_all_cmds(struct se_device *dev)
1061{
1062	struct se_cmd *cmd, *tcmd;
1063	int bug_out = 0, t_state;
1064	unsigned long flags;
1065
1066	spin_lock_irqsave(&dev->dev_queue_obj.cmd_queue_lock, flags);
1067	list_for_each_entry_safe(cmd, tcmd, &dev->dev_queue_obj.qobj_list,
1068				se_queue_node) {
1069		t_state = cmd->t_state;
1070		list_del(&cmd->se_queue_node);
1071		spin_unlock_irqrestore(&dev->dev_queue_obj.cmd_queue_lock,
1072				flags);
1073
1074		pr_err("Releasing ITT: 0x%08x, i_state: %u,"
1075			" t_state: %u directly\n",
1076			cmd->se_tfo->get_task_tag(cmd),
1077			cmd->se_tfo->get_cmd_state(cmd), t_state);
1078
1079		transport_release_fe_cmd(cmd);
1080		bug_out = 1;
1081
1082		spin_lock_irqsave(&dev->dev_queue_obj.cmd_queue_lock, flags);
1083	}
1084	spin_unlock_irqrestore(&dev->dev_queue_obj.cmd_queue_lock, flags);
1085#if 0
1086	if (bug_out)
1087		BUG();
1088#endif
1089}
1090
1091void transport_dump_vpd_proto_id(
1092	struct t10_vpd *vpd,
1093	unsigned char *p_buf,
1094	int p_buf_len)
1095{
1096	unsigned char buf[VPD_TMP_BUF_SIZE];
1097	int len;
1098
1099	memset(buf, 0, VPD_TMP_BUF_SIZE);
1100	len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1101
1102	switch (vpd->protocol_identifier) {
1103	case 0x00:
1104		sprintf(buf+len, "Fibre Channel\n");
1105		break;
1106	case 0x10:
1107		sprintf(buf+len, "Parallel SCSI\n");
1108		break;
1109	case 0x20:
1110		sprintf(buf+len, "SSA\n");
1111		break;
1112	case 0x30:
1113		sprintf(buf+len, "IEEE 1394\n");
1114		break;
1115	case 0x40:
1116		sprintf(buf+len, "SCSI Remote Direct Memory Access"
1117				" Protocol\n");
1118		break;
1119	case 0x50:
1120		sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1121		break;
1122	case 0x60:
1123		sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1124		break;
1125	case 0x70:
1126		sprintf(buf+len, "Automation/Drive Interface Transport"
1127				" Protocol\n");
1128		break;
1129	case 0x80:
1130		sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1131		break;
1132	default:
1133		sprintf(buf+len, "Unknown 0x%02x\n",
1134				vpd->protocol_identifier);
1135		break;
1136	}
1137
1138	if (p_buf)
1139		strncpy(p_buf, buf, p_buf_len);
1140	else
1141		pr_debug("%s", buf);
1142}
1143
1144void
1145transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1146{
1147	/*
1148	 * Check if the Protocol Identifier Valid (PIV) bit is set..
1149	 *
1150	 * from spc3r23.pdf section 7.5.1
1151	 */
1152	 if (page_83[1] & 0x80) {
1153		vpd->protocol_identifier = (page_83[0] & 0xf0);
1154		vpd->protocol_identifier_set = 1;
1155		transport_dump_vpd_proto_id(vpd, NULL, 0);
1156	}
1157}
1158EXPORT_SYMBOL(transport_set_vpd_proto_id);
1159
1160int transport_dump_vpd_assoc(
1161	struct t10_vpd *vpd,
1162	unsigned char *p_buf,
1163	int p_buf_len)
1164{
1165	unsigned char buf[VPD_TMP_BUF_SIZE];
1166	int ret = 0;
1167	int len;
1168
1169	memset(buf, 0, VPD_TMP_BUF_SIZE);
1170	len = sprintf(buf, "T10 VPD Identifier Association: ");
1171
1172	switch (vpd->association) {
1173	case 0x00:
1174		sprintf(buf+len, "addressed logical unit\n");
1175		break;
1176	case 0x10:
1177		sprintf(buf+len, "target port\n");
1178		break;
1179	case 0x20:
1180		sprintf(buf+len, "SCSI target device\n");
1181		break;
1182	default:
1183		sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
1184		ret = -EINVAL;
1185		break;
1186	}
1187
1188	if (p_buf)
1189		strncpy(p_buf, buf, p_buf_len);
1190	else
1191		pr_debug("%s", buf);
1192
1193	return ret;
1194}
1195
1196int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1197{
1198	/*
1199	 * The VPD identification association..
1200	 *
1201	 * from spc3r23.pdf Section 7.6.3.1 Table 297
1202	 */
1203	vpd->association = (page_83[1] & 0x30);
1204	return transport_dump_vpd_assoc(vpd, NULL, 0);
1205}
1206EXPORT_SYMBOL(transport_set_vpd_assoc);
1207
1208int transport_dump_vpd_ident_type(
1209	struct t10_vpd *vpd,
1210	unsigned char *p_buf,
1211	int p_buf_len)
1212{
1213	unsigned char buf[VPD_TMP_BUF_SIZE];
1214	int ret = 0;
1215	int len;
1216
1217	memset(buf, 0, VPD_TMP_BUF_SIZE);
1218	len = sprintf(buf, "T10 VPD Identifier Type: ");
1219
1220	switch (vpd->device_identifier_type) {
1221	case 0x00:
1222		sprintf(buf+len, "Vendor specific\n");
1223		break;
1224	case 0x01:
1225		sprintf(buf+len, "T10 Vendor ID based\n");
1226		break;
1227	case 0x02:
1228		sprintf(buf+len, "EUI-64 based\n");
1229		break;
1230	case 0x03:
1231		sprintf(buf+len, "NAA\n");
1232		break;
1233	case 0x04:
1234		sprintf(buf+len, "Relative target port identifier\n");
1235		break;
1236	case 0x08:
1237		sprintf(buf+len, "SCSI name string\n");
1238		break;
1239	default:
1240		sprintf(buf+len, "Unsupported: 0x%02x\n",
1241				vpd->device_identifier_type);
1242		ret = -EINVAL;
1243		break;
1244	}
1245
1246	if (p_buf) {
1247		if (p_buf_len < strlen(buf)+1)
1248			return -EINVAL;
1249		strncpy(p_buf, buf, p_buf_len);
1250	} else {
1251		pr_debug("%s", buf);
1252	}
1253
1254	return ret;
1255}
1256
1257int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1258{
1259	/*
1260	 * The VPD identifier type..
1261	 *
1262	 * from spc3r23.pdf Section 7.6.3.1 Table 298
1263	 */
1264	vpd->device_identifier_type = (page_83[1] & 0x0f);
1265	return transport_dump_vpd_ident_type(vpd, NULL, 0);
1266}
1267EXPORT_SYMBOL(transport_set_vpd_ident_type);
1268
1269int transport_dump_vpd_ident(
1270	struct t10_vpd *vpd,
1271	unsigned char *p_buf,
1272	int p_buf_len)
1273{
1274	unsigned char buf[VPD_TMP_BUF_SIZE];
1275	int ret = 0;
1276
1277	memset(buf, 0, VPD_TMP_BUF_SIZE);
1278
1279	switch (vpd->device_identifier_code_set) {
1280	case 0x01: /* Binary */
1281		sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1282			&vpd->device_identifier[0]);
1283		break;
1284	case 0x02: /* ASCII */
1285		sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1286			&vpd->device_identifier[0]);
1287		break;
1288	case 0x03: /* UTF-8 */
1289		sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1290			&vpd->device_identifier[0]);
1291		break;
1292	default:
1293		sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1294			" 0x%02x", vpd->device_identifier_code_set);
1295		ret = -EINVAL;
1296		break;
1297	}
1298
1299	if (p_buf)
1300		strncpy(p_buf, buf, p_buf_len);
1301	else
1302		pr_debug("%s", buf);
1303
1304	return ret;
1305}
1306
1307int
1308transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1309{
1310	static const char hex_str[] = "0123456789abcdef";
1311	int j = 0, i = 4; /* offset to start of the identifer */
1312
1313	/*
1314	 * The VPD Code Set (encoding)
1315	 *
1316	 * from spc3r23.pdf Section 7.6.3.1 Table 296
1317	 */
1318	vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1319	switch (vpd->device_identifier_code_set) {
1320	case 0x01: /* Binary */
1321		vpd->device_identifier[j++] =
1322				hex_str[vpd->device_identifier_type];
1323		while (i < (4 + page_83[3])) {
1324			vpd->device_identifier[j++] =
1325				hex_str[(page_83[i] & 0xf0) >> 4];
1326			vpd->device_identifier[j++] =
1327				hex_str[page_83[i] & 0x0f];
1328			i++;
1329		}
1330		break;
1331	case 0x02: /* ASCII */
1332	case 0x03: /* UTF-8 */
1333		while (i < (4 + page_83[3]))
1334			vpd->device_identifier[j++] = page_83[i++];
1335		break;
1336	default:
1337		break;
1338	}
1339
1340	return transport_dump_vpd_ident(vpd, NULL, 0);
1341}
1342EXPORT_SYMBOL(transport_set_vpd_ident);
1343
1344static void core_setup_task_attr_emulation(struct se_device *dev)
1345{
1346	/*
1347	 * If this device is from Target_Core_Mod/pSCSI, disable the
1348	 * SAM Task Attribute emulation.
1349	 *
1350	 * This is currently not available in upsream Linux/SCSI Target
1351	 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1352	 */
1353	if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1354		dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1355		return;
1356	}
1357
1358	dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
1359	pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1360		" device\n", dev->transport->name,
1361		dev->transport->get_device_rev(dev));
1362}
1363
1364static void scsi_dump_inquiry(struct se_device *dev)
1365{
1366	struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
 
1367	int i, device_type;
1368	/*
1369	 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1370	 */
1371	pr_debug("  Vendor: ");
1372	for (i = 0; i < 8; i++)
1373		if (wwn->vendor[i] >= 0x20)
1374			pr_debug("%c", wwn->vendor[i]);
1375		else
1376			pr_debug(" ");
 
 
1377
1378	pr_debug("  Model: ");
1379	for (i = 0; i < 16; i++)
1380		if (wwn->model[i] >= 0x20)
1381			pr_debug("%c", wwn->model[i]);
1382		else
1383			pr_debug(" ");
 
 
1384
1385	pr_debug("  Revision: ");
1386	for (i = 0; i < 4; i++)
1387		if (wwn->revision[i] >= 0x20)
1388			pr_debug("%c", wwn->revision[i]);
1389		else
1390			pr_debug(" ");
1391
1392	pr_debug("\n");
1393
1394	device_type = dev->transport->get_device_type(dev);
1395	pr_debug("  Type:   %s ", scsi_device_type(device_type));
1396	pr_debug("                 ANSI SCSI revision: %02x\n",
1397				dev->transport->get_device_rev(dev));
1398}
1399
1400struct se_device *transport_add_device_to_core_hba(
1401	struct se_hba *hba,
1402	struct se_subsystem_api *transport,
1403	struct se_subsystem_dev *se_dev,
1404	u32 device_flags,
1405	void *transport_dev,
1406	struct se_dev_limits *dev_limits,
1407	const char *inquiry_prod,
1408	const char *inquiry_rev)
1409{
1410	int force_pt;
1411	struct se_device  *dev;
1412
1413	dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
1414	if (!dev) {
1415		pr_err("Unable to allocate memory for se_dev_t\n");
1416		return NULL;
1417	}
1418
1419	transport_init_queue_obj(&dev->dev_queue_obj);
1420	dev->dev_flags		= device_flags;
1421	dev->dev_status		|= TRANSPORT_DEVICE_DEACTIVATED;
1422	dev->dev_ptr		= transport_dev;
1423	dev->se_hba		= hba;
1424	dev->se_sub_dev		= se_dev;
1425	dev->transport		= transport;
1426	atomic_set(&dev->active_cmds, 0);
1427	INIT_LIST_HEAD(&dev->dev_list);
1428	INIT_LIST_HEAD(&dev->dev_sep_list);
1429	INIT_LIST_HEAD(&dev->dev_tmr_list);
1430	INIT_LIST_HEAD(&dev->execute_task_list);
1431	INIT_LIST_HEAD(&dev->delayed_cmd_list);
1432	INIT_LIST_HEAD(&dev->ordered_cmd_list);
1433	INIT_LIST_HEAD(&dev->state_task_list);
1434	INIT_LIST_HEAD(&dev->qf_cmd_list);
1435	spin_lock_init(&dev->execute_task_lock);
1436	spin_lock_init(&dev->delayed_cmd_lock);
1437	spin_lock_init(&dev->ordered_cmd_lock);
1438	spin_lock_init(&dev->state_task_lock);
1439	spin_lock_init(&dev->dev_alua_lock);
1440	spin_lock_init(&dev->dev_reservation_lock);
1441	spin_lock_init(&dev->dev_status_lock);
1442	spin_lock_init(&dev->dev_status_thr_lock);
1443	spin_lock_init(&dev->se_port_lock);
1444	spin_lock_init(&dev->se_tmr_lock);
1445	spin_lock_init(&dev->qf_cmd_lock);
1446
1447	dev->queue_depth	= dev_limits->queue_depth;
1448	atomic_set(&dev->depth_left, dev->queue_depth);
1449	atomic_set(&dev->dev_ordered_id, 0);
1450
1451	se_dev_set_default_attribs(dev, dev_limits);
1452
1453	dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1454	dev->creation_time = get_jiffies_64();
1455	spin_lock_init(&dev->stats_lock);
1456
1457	spin_lock(&hba->device_lock);
1458	list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1459	hba->dev_count++;
1460	spin_unlock(&hba->device_lock);
1461	/*
1462	 * Setup the SAM Task Attribute emulation for struct se_device
1463	 */
1464	core_setup_task_attr_emulation(dev);
1465	/*
1466	 * Force PR and ALUA passthrough emulation with internal object use.
1467	 */
1468	force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1469	/*
1470	 * Setup the Reservations infrastructure for struct se_device
1471	 */
1472	core_setup_reservations(dev, force_pt);
1473	/*
1474	 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1475	 */
1476	if (core_setup_alua(dev, force_pt) < 0)
1477		goto out;
1478
1479	/*
1480	 * Startup the struct se_device processing thread
1481	 */
1482	dev->process_thread = kthread_run(transport_processing_thread, dev,
1483					  "LIO_%s", dev->transport->name);
1484	if (IS_ERR(dev->process_thread)) {
1485		pr_err("Unable to create kthread: LIO_%s\n",
1486			dev->transport->name);
1487		goto out;
1488	}
1489	/*
1490	 * Setup work_queue for QUEUE_FULL
1491	 */
1492	INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1493	/*
1494	 * Preload the initial INQUIRY const values if we are doing
1495	 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1496	 * passthrough because this is being provided by the backend LLD.
1497	 * This is required so that transport_get_inquiry() copies these
1498	 * originals once back into DEV_T10_WWN(dev) for the virtual device
1499	 * setup.
1500	 */
1501	if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1502		if (!inquiry_prod || !inquiry_rev) {
1503			pr_err("All non TCM/pSCSI plugins require"
1504				" INQUIRY consts\n");
1505			goto out;
1506		}
1507
1508		strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1509		strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1510		strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
1511	}
1512	scsi_dump_inquiry(dev);
1513
1514	return dev;
1515out:
1516	kthread_stop(dev->process_thread);
1517
1518	spin_lock(&hba->device_lock);
1519	list_del(&dev->dev_list);
1520	hba->dev_count--;
1521	spin_unlock(&hba->device_lock);
1522
1523	se_release_vpd_for_dev(dev);
1524
1525	kfree(dev);
1526
1527	return NULL;
1528}
1529EXPORT_SYMBOL(transport_add_device_to_core_hba);
1530
1531/*	transport_generic_prepare_cdb():
1532 *
1533 *	Since the Initiator sees iSCSI devices as LUNs,  the SCSI CDB will
1534 *	contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1535 *	The point of this is since we are mapping iSCSI LUNs to
1536 *	SCSI Target IDs having a non-zero LUN in the CDB will throw the
1537 *	devices and HBAs for a loop.
1538 */
1539static inline void transport_generic_prepare_cdb(
1540	unsigned char *cdb)
1541{
1542	switch (cdb[0]) {
1543	case READ_10: /* SBC - RDProtect */
1544	case READ_12: /* SBC - RDProtect */
1545	case READ_16: /* SBC - RDProtect */
1546	case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1547	case VERIFY: /* SBC - VRProtect */
1548	case VERIFY_16: /* SBC - VRProtect */
1549	case WRITE_VERIFY: /* SBC - VRProtect */
1550	case WRITE_VERIFY_12: /* SBC - VRProtect */
 
1551		break;
1552	default:
1553		cdb[1] &= 0x1f; /* clear logical unit number */
1554		break;
1555	}
1556}
1557
1558static struct se_task *
1559transport_generic_get_task(struct se_cmd *cmd,
1560		enum dma_data_direction data_direction)
1561{
1562	struct se_task *task;
1563	struct se_device *dev = cmd->se_dev;
1564
1565	task = dev->transport->alloc_task(cmd->t_task_cdb);
1566	if (!task) {
1567		pr_err("Unable to allocate struct se_task\n");
1568		return NULL;
1569	}
1570
1571	INIT_LIST_HEAD(&task->t_list);
1572	INIT_LIST_HEAD(&task->t_execute_list);
1573	INIT_LIST_HEAD(&task->t_state_list);
1574	init_completion(&task->task_stop_comp);
1575	task->task_se_cmd = cmd;
1576	task->se_dev = dev;
1577	task->task_data_direction = data_direction;
1578
1579	return task;
1580}
1581
1582static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1583
1584/*
1585 * Used by fabric modules containing a local struct se_cmd within their
1586 * fabric dependent per I/O descriptor.
1587 */
1588void transport_init_se_cmd(
1589	struct se_cmd *cmd,
1590	struct target_core_fabric_ops *tfo,
1591	struct se_session *se_sess,
1592	u32 data_length,
1593	int data_direction,
1594	int task_attr,
1595	unsigned char *sense_buffer)
1596{
1597	INIT_LIST_HEAD(&cmd->se_lun_node);
1598	INIT_LIST_HEAD(&cmd->se_delayed_node);
1599	INIT_LIST_HEAD(&cmd->se_ordered_node);
1600	INIT_LIST_HEAD(&cmd->se_qf_node);
1601
1602	INIT_LIST_HEAD(&cmd->t_task_list);
 
 
1603	init_completion(&cmd->transport_lun_fe_stop_comp);
1604	init_completion(&cmd->transport_lun_stop_comp);
1605	init_completion(&cmd->t_transport_stop_comp);
 
 
1606	spin_lock_init(&cmd->t_state_lock);
1607	atomic_set(&cmd->transport_dev_active, 1);
1608
1609	cmd->se_tfo = tfo;
1610	cmd->se_sess = se_sess;
1611	cmd->data_length = data_length;
1612	cmd->data_direction = data_direction;
1613	cmd->sam_task_attr = task_attr;
1614	cmd->sense_buffer = sense_buffer;
 
 
1615}
1616EXPORT_SYMBOL(transport_init_se_cmd);
1617
1618static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1619{
1620	/*
1621	 * Check if SAM Task Attribute emulation is enabled for this
1622	 * struct se_device storage object
1623	 */
1624	if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1625		return 0;
1626
1627	if (cmd->sam_task_attr == MSG_ACA_TAG) {
1628		pr_debug("SAM Task Attribute ACA"
1629			" emulation is not supported\n");
1630		return -EINVAL;
1631	}
1632	/*
1633	 * Used to determine when ORDERED commands should go from
1634	 * Dormant to Active status.
1635	 */
1636	cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
1637	smp_mb__after_atomic_inc();
1638	pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1639			cmd->se_ordered_id, cmd->sam_task_attr,
1640			cmd->se_dev->transport->name);
1641	return 0;
1642}
1643
1644void transport_free_se_cmd(
1645	struct se_cmd *se_cmd)
1646{
1647	if (se_cmd->se_tmr_req)
1648		core_tmr_release_req(se_cmd->se_tmr_req);
1649	/*
1650	 * Check and free any extended CDB buffer that was allocated
1651	 */
1652	if (se_cmd->t_task_cdb != se_cmd->__t_task_cdb)
1653		kfree(se_cmd->t_task_cdb);
1654}
1655EXPORT_SYMBOL(transport_free_se_cmd);
1656
1657static void transport_generic_wait_for_tasks(struct se_cmd *, int, int);
1658
1659/*	transport_generic_allocate_tasks():
1660 *
1661 *	Called from fabric RX Thread.
1662 */
1663int transport_generic_allocate_tasks(
1664	struct se_cmd *cmd,
1665	unsigned char *cdb)
1666{
1667	int ret;
1668
1669	transport_generic_prepare_cdb(cdb);
1670
1671	/*
1672	 * This is needed for early exceptions.
1673	 */
1674	cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
1675
1676	/*
1677	 * Ensure that the received CDB is less than the max (252 + 8) bytes
1678	 * for VARIABLE_LENGTH_CMD
1679	 */
1680	if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1681		pr_err("Received SCSI CDB with command_size: %d that"
1682			" exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1683			scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
 
 
1684		return -EINVAL;
1685	}
1686	/*
1687	 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1688	 * allocate the additional extended CDB buffer now..  Otherwise
1689	 * setup the pointer from __t_task_cdb to t_task_cdb.
1690	 */
1691	if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1692		cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1693						GFP_KERNEL);
1694		if (!cmd->t_task_cdb) {
1695			pr_err("Unable to allocate cmd->t_task_cdb"
1696				" %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1697				scsi_command_size(cdb),
1698				(unsigned long)sizeof(cmd->__t_task_cdb));
 
 
 
1699			return -ENOMEM;
1700		}
1701	} else
1702		cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1703	/*
1704	 * Copy the original CDB into cmd->
1705	 */
1706	memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1707	/*
1708	 * Setup the received CDB based on SCSI defined opcodes and
1709	 * perform unit attention, persistent reservations and ALUA
1710	 * checks for virtual device backends.  The cmd->t_task_cdb
1711	 * pointer is expected to be setup before we reach this point.
1712	 */
1713	ret = transport_generic_cmd_sequencer(cmd, cdb);
1714	if (ret < 0)
1715		return ret;
1716	/*
1717	 * Check for SAM Task Attribute Emulation
1718	 */
1719	if (transport_check_alloc_task_attr(cmd) < 0) {
1720		cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1721		cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1722		return -EINVAL;
1723	}
1724	spin_lock(&cmd->se_lun->lun_sep_lock);
1725	if (cmd->se_lun->lun_sep)
1726		cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1727	spin_unlock(&cmd->se_lun->lun_sep_lock);
1728	return 0;
1729}
1730EXPORT_SYMBOL(transport_generic_allocate_tasks);
1731
1732/*
1733 * Used by fabric module frontends not defining a TFO->new_cmd_map()
1734 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD statis
1735 */
1736int transport_generic_handle_cdb(
1737	struct se_cmd *cmd)
1738{
1739	if (!cmd->se_lun) {
1740		dump_stack();
1741		pr_err("cmd->se_lun is NULL\n");
1742		return -EINVAL;
1743	}
1744
1745	transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD);
1746	return 0;
1747}
1748EXPORT_SYMBOL(transport_generic_handle_cdb);
1749
1750static void transport_generic_request_failure(struct se_cmd *,
1751			struct se_device *, int, int);
1752/*
1753 * Used by fabric module frontends to queue tasks directly.
1754 * Many only be used from process context only
1755 */
1756int transport_handle_cdb_direct(
1757	struct se_cmd *cmd)
1758{
1759	int ret;
1760
1761	if (!cmd->se_lun) {
1762		dump_stack();
1763		pr_err("cmd->se_lun is NULL\n");
1764		return -EINVAL;
1765	}
1766	if (in_interrupt()) {
1767		dump_stack();
1768		pr_err("transport_generic_handle_cdb cannot be called"
1769				" from interrupt context\n");
1770		return -EINVAL;
1771	}
1772	/*
1773	 * Set TRANSPORT_NEW_CMD state and cmd->t_transport_active=1 following
1774	 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1775	 * in existing usage to ensure that outstanding descriptors are handled
1776	 * correctly during shutdown via transport_generic_wait_for_tasks()
1777	 *
1778	 * Also, we don't take cmd->t_state_lock here as we only expect
1779	 * this to be called for initial descriptor submission.
1780	 */
1781	cmd->t_state = TRANSPORT_NEW_CMD;
1782	atomic_set(&cmd->t_transport_active, 1);
 
1783	/*
1784	 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1785	 * so follow TRANSPORT_NEW_CMD processing thread context usage
1786	 * and call transport_generic_request_failure() if necessary..
1787	 */
1788	ret = transport_generic_new_cmd(cmd);
1789	if (ret == -EAGAIN)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1790		return 0;
1791	else if (ret < 0) {
1792		cmd->transport_error_status = ret;
1793		transport_generic_request_failure(cmd, NULL, 0,
1794				(cmd->data_direction != DMA_TO_DEVICE));
1795	}
 
1796	return 0;
1797}
1798EXPORT_SYMBOL(transport_handle_cdb_direct);
1799
1800/*
1801 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1802 * to  queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1803 * complete setup in TCM process context w/ TFO->new_cmd_map().
1804 */
1805int transport_generic_handle_cdb_map(
1806	struct se_cmd *cmd)
1807{
1808	if (!cmd->se_lun) {
1809		dump_stack();
1810		pr_err("cmd->se_lun is NULL\n");
1811		return -EINVAL;
1812	}
1813
1814	transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP);
1815	return 0;
1816}
1817EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1818
1819/*	transport_generic_handle_data():
1820 *
1821 *
1822 */
1823int transport_generic_handle_data(
1824	struct se_cmd *cmd)
1825{
1826	/*
1827	 * For the software fabric case, then we assume the nexus is being
1828	 * failed/shutdown when signals are pending from the kthread context
1829	 * caller, so we return a failure.  For the HW target mode case running
1830	 * in interrupt code, the signal_pending() check is skipped.
1831	 */
1832	if (!in_interrupt() && signal_pending(current))
1833		return -EPERM;
1834	/*
1835	 * If the received CDB has aleady been ABORTED by the generic
1836	 * target engine, we now call transport_check_aborted_status()
1837	 * to queue any delated TASK_ABORTED status for the received CDB to the
1838	 * fabric module as we are expecting no further incoming DATA OUT
1839	 * sequences at this point.
1840	 */
1841	if (transport_check_aborted_status(cmd, 1) != 0)
1842		return 0;
1843
1844	transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE);
1845	return 0;
1846}
1847EXPORT_SYMBOL(transport_generic_handle_data);
1848
1849/*	transport_generic_handle_tmr():
1850 *
1851 *
1852 */
1853int transport_generic_handle_tmr(
1854	struct se_cmd *cmd)
1855{
1856	/*
1857	 * This is needed for early exceptions.
1858	 */
1859	cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
1860
1861	transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR);
1862	return 0;
1863}
1864EXPORT_SYMBOL(transport_generic_handle_tmr);
1865
1866void transport_generic_free_cmd_intr(
1867	struct se_cmd *cmd)
1868{
1869	transport_add_cmd_to_queue(cmd, TRANSPORT_FREE_CMD_INTR);
1870}
1871EXPORT_SYMBOL(transport_generic_free_cmd_intr);
1872
1873static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1874{
1875	struct se_task *task, *task_tmp;
1876	unsigned long flags;
1877	int ret = 0;
1878
1879	pr_debug("ITT[0x%08x] - Stopping tasks\n",
1880		cmd->se_tfo->get_task_tag(cmd));
 
1881
1882	/*
1883	 * No tasks remain in the execution queue
1884	 */
1885	spin_lock_irqsave(&cmd->t_state_lock, flags);
1886	list_for_each_entry_safe(task, task_tmp,
1887				&cmd->t_task_list, t_list) {
1888		pr_debug("task_no[%d] - Processing task %p\n",
1889				task->task_no, task);
1890		/*
1891		 * If the struct se_task has not been sent and is not active,
1892		 * remove the struct se_task from the execution queue.
1893		 */
1894		if (!atomic_read(&task->task_sent) &&
1895		    !atomic_read(&task->task_active)) {
1896			spin_unlock_irqrestore(&cmd->t_state_lock,
1897					flags);
1898			transport_remove_task_from_execute_queue(task,
1899					task->se_dev);
1900
1901			pr_debug("task_no[%d] - Removed from execute queue\n",
1902				task->task_no);
1903			spin_lock_irqsave(&cmd->t_state_lock, flags);
1904			continue;
1905		}
1906
1907		/*
1908		 * If the struct se_task is active, sleep until it is returned
1909		 * from the plugin.
1910		 */
1911		if (atomic_read(&task->task_active)) {
1912			atomic_set(&task->task_stop, 1);
1913			spin_unlock_irqrestore(&cmd->t_state_lock,
1914					flags);
1915
1916			pr_debug("task_no[%d] - Waiting to complete\n",
1917				task->task_no);
1918			wait_for_completion(&task->task_stop_comp);
1919			pr_debug("task_no[%d] - Stopped successfully\n",
1920				task->task_no);
1921
1922			spin_lock_irqsave(&cmd->t_state_lock, flags);
1923			atomic_dec(&cmd->t_task_cdbs_left);
1924
1925			atomic_set(&task->task_active, 0);
1926			atomic_set(&task->task_stop, 0);
1927		} else {
1928			pr_debug("task_no[%d] - Did nothing\n", task->task_no);
1929			ret++;
1930		}
1931
1932		__transport_stop_task_timer(task, &flags);
1933	}
1934	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1935
1936	return ret;
1937}
1938
1939/*
1940 * Handle SAM-esque emulation for generic transport request failures.
1941 */
1942static void transport_generic_request_failure(
1943	struct se_cmd *cmd,
1944	struct se_device *dev,
1945	int complete,
1946	int sc)
1947{
1948	int ret = 0;
1949
1950	pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1951		" CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
1952		cmd->t_task_cdb[0]);
1953	pr_debug("-----[ i_state: %d t_state/def_t_state:"
1954		" %d/%d transport_error_status: %d\n",
1955		cmd->se_tfo->get_cmd_state(cmd),
1956		cmd->t_state, cmd->deferred_t_state,
1957		cmd->transport_error_status);
1958	pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
1959		" t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
1960		" t_transport_active: %d t_transport_stop: %d"
1961		" t_transport_sent: %d\n", cmd->t_task_list_num,
1962		atomic_read(&cmd->t_task_cdbs_left),
1963		atomic_read(&cmd->t_task_cdbs_sent),
1964		atomic_read(&cmd->t_task_cdbs_ex_left),
1965		atomic_read(&cmd->t_transport_active),
1966		atomic_read(&cmd->t_transport_stop),
1967		atomic_read(&cmd->t_transport_sent));
1968
1969	transport_stop_all_task_timers(cmd);
1970
1971	if (dev)
1972		atomic_inc(&dev->depth_left);
1973	/*
1974	 * For SAM Task Attribute emulation for failed struct se_cmd
1975	 */
1976	if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1977		transport_complete_task_attr(cmd);
1978
1979	if (complete) {
1980		transport_direct_request_timeout(cmd);
1981		cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
1982	}
1983
1984	switch (cmd->transport_error_status) {
1985	case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE:
1986		cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1987		break;
1988	case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS:
1989		cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
1990		break;
1991	case PYX_TRANSPORT_INVALID_CDB_FIELD:
1992		cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1993		break;
1994	case PYX_TRANSPORT_INVALID_PARAMETER_LIST:
1995		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1996		break;
1997	case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES:
1998		if (!sc)
1999			transport_new_cmd_failure(cmd);
2000		/*
2001		 * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
2002		 * we force this session to fall back to session
2003		 * recovery.
2004		 */
2005		cmd->se_tfo->fall_back_to_erl0(cmd->se_sess);
2006		cmd->se_tfo->stop_session(cmd->se_sess, 0, 0);
2007
2008		goto check_stop;
2009	case PYX_TRANSPORT_LU_COMM_FAILURE:
2010	case PYX_TRANSPORT_ILLEGAL_REQUEST:
2011		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2012		break;
2013	case PYX_TRANSPORT_UNKNOWN_MODE_PAGE:
2014		cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
2015		break;
2016	case PYX_TRANSPORT_WRITE_PROTECTED:
2017		cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
2018		break;
2019	case PYX_TRANSPORT_RESERVATION_CONFLICT:
2020		/*
2021		 * No SENSE Data payload for this case, set SCSI Status
2022		 * and queue the response to $FABRIC_MOD.
2023		 *
2024		 * Uses linux/include/scsi/scsi.h SAM status codes defs
2025		 */
2026		cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2027		/*
2028		 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2029		 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2030		 * CONFLICT STATUS.
2031		 *
2032		 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2033		 */
2034		if (cmd->se_sess &&
2035		    cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
2036			core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
2037				cmd->orig_fe_lun, 0x2C,
2038				ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
2039
2040		ret = cmd->se_tfo->queue_status(cmd);
2041		if (ret == -EAGAIN)
2042			goto queue_full;
2043		goto check_stop;
2044	case PYX_TRANSPORT_USE_SENSE_REASON:
2045		/*
2046		 * struct se_cmd->scsi_sense_reason already set
2047		 */
2048		break;
2049	default:
2050		pr_err("Unknown transport error for CDB 0x%02x: %d\n",
2051			cmd->t_task_cdb[0],
2052			cmd->transport_error_status);
2053		cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
2054		break;
2055	}
2056	/*
2057	 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
2058	 * make the call to transport_send_check_condition_and_sense()
2059	 * directly.  Otherwise expect the fabric to make the call to
2060	 * transport_send_check_condition_and_sense() after handling
2061	 * possible unsoliticied write data payloads.
2062	 */
2063	if (!sc && !cmd->se_tfo->new_cmd_map)
2064		transport_new_cmd_failure(cmd);
2065	else {
2066		ret = transport_send_check_condition_and_sense(cmd,
2067				cmd->scsi_sense_reason, 0);
2068		if (ret == -EAGAIN)
2069			goto queue_full;
2070	}
2071
2072check_stop:
2073	transport_lun_remove_cmd(cmd);
2074	if (!transport_cmd_check_stop_to_fabric(cmd))
2075		;
2076	return;
2077
2078queue_full:
2079	cmd->t_state = TRANSPORT_COMPLETE_OK;
2080	transport_handle_queue_full(cmd, cmd->se_dev, transport_complete_qf);
2081}
2082
2083static void transport_direct_request_timeout(struct se_cmd *cmd)
2084{
2085	unsigned long flags;
2086
2087	spin_lock_irqsave(&cmd->t_state_lock, flags);
2088	if (!atomic_read(&cmd->t_transport_timeout)) {
2089		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2090		return;
2091	}
2092	if (atomic_read(&cmd->t_task_cdbs_timeout_left)) {
2093		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2094		return;
2095	}
2096
2097	atomic_sub(atomic_read(&cmd->t_transport_timeout),
2098		   &cmd->t_se_count);
2099	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2100}
2101
2102static void transport_generic_request_timeout(struct se_cmd *cmd)
2103{
2104	unsigned long flags;
2105
2106	/*
2107	 * Reset cmd->t_se_count to allow transport_generic_remove()
2108	 * to allow last call to free memory resources.
2109	 */
2110	spin_lock_irqsave(&cmd->t_state_lock, flags);
2111	if (atomic_read(&cmd->t_transport_timeout) > 1) {
2112		int tmp = (atomic_read(&cmd->t_transport_timeout) - 1);
2113
2114		atomic_sub(tmp, &cmd->t_se_count);
2115	}
2116	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2117
2118	transport_generic_remove(cmd, 0);
2119}
 
2120
2121static inline u32 transport_lba_21(unsigned char *cdb)
2122{
2123	return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
2124}
2125
2126static inline u32 transport_lba_32(unsigned char *cdb)
2127{
2128	return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2129}
2130
2131static inline unsigned long long transport_lba_64(unsigned char *cdb)
2132{
2133	unsigned int __v1, __v2;
2134
2135	__v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2136	__v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2137
2138	return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2139}
2140
2141/*
2142 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
2143 */
2144static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
2145{
2146	unsigned int __v1, __v2;
2147
2148	__v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
2149	__v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
2150
2151	return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2152}
2153
2154static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
2155{
2156	unsigned long flags;
2157
2158	spin_lock_irqsave(&se_cmd->t_state_lock, flags);
2159	se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
2160	spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
2161}
2162
2163/*
2164 * Called from interrupt context.
2165 */
2166static void transport_task_timeout_handler(unsigned long data)
2167{
2168	struct se_task *task = (struct se_task *)data;
2169	struct se_cmd *cmd = task->task_se_cmd;
2170	unsigned long flags;
2171
2172	pr_debug("transport task timeout fired! task: %p cmd: %p\n", task, cmd);
2173
2174	spin_lock_irqsave(&cmd->t_state_lock, flags);
2175	if (task->task_flags & TF_STOP) {
2176		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2177		return;
2178	}
2179	task->task_flags &= ~TF_RUNNING;
2180
2181	/*
2182	 * Determine if transport_complete_task() has already been called.
2183	 */
2184	if (!atomic_read(&task->task_active)) {
2185		pr_debug("transport task: %p cmd: %p timeout task_active"
2186				" == 0\n", task, cmd);
2187		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2188		return;
2189	}
2190
2191	atomic_inc(&cmd->t_se_count);
2192	atomic_inc(&cmd->t_transport_timeout);
2193	cmd->t_tasks_failed = 1;
2194
2195	atomic_set(&task->task_timeout, 1);
2196	task->task_error_status = PYX_TRANSPORT_TASK_TIMEOUT;
2197	task->task_scsi_status = 1;
2198
2199	if (atomic_read(&task->task_stop)) {
2200		pr_debug("transport task: %p cmd: %p timeout task_stop"
2201				" == 1\n", task, cmd);
2202		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2203		complete(&task->task_stop_comp);
2204		return;
2205	}
2206
2207	if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
2208		pr_debug("transport task: %p cmd: %p timeout non zero"
2209				" t_task_cdbs_left\n", task, cmd);
2210		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2211		return;
2212	}
2213	pr_debug("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
2214			task, cmd);
2215
2216	cmd->t_state = TRANSPORT_COMPLETE_FAILURE;
2217	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2218
2219	transport_add_cmd_to_queue(cmd, TRANSPORT_COMPLETE_FAILURE);
2220}
2221
2222/*
2223 * Called with cmd->t_state_lock held.
2224 */
2225static void transport_start_task_timer(struct se_task *task)
2226{
2227	struct se_device *dev = task->se_dev;
2228	int timeout;
2229
2230	if (task->task_flags & TF_RUNNING)
2231		return;
2232	/*
2233	 * If the task_timeout is disabled, exit now.
2234	 */
2235	timeout = dev->se_sub_dev->se_dev_attrib.task_timeout;
2236	if (!timeout)
2237		return;
2238
2239	init_timer(&task->task_timer);
2240	task->task_timer.expires = (get_jiffies_64() + timeout * HZ);
2241	task->task_timer.data = (unsigned long) task;
2242	task->task_timer.function = transport_task_timeout_handler;
2243
2244	task->task_flags |= TF_RUNNING;
2245	add_timer(&task->task_timer);
2246#if 0
2247	pr_debug("Starting task timer for cmd: %p task: %p seconds:"
2248		" %d\n", task->task_se_cmd, task, timeout);
2249#endif
2250}
2251
2252/*
2253 * Called with spin_lock_irq(&cmd->t_state_lock) held.
2254 */
2255void __transport_stop_task_timer(struct se_task *task, unsigned long *flags)
2256{
2257	struct se_cmd *cmd = task->task_se_cmd;
2258
2259	if (!task->task_flags & TF_RUNNING)
2260		return;
2261
2262	task->task_flags |= TF_STOP;
2263	spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
2264
2265	del_timer_sync(&task->task_timer);
2266
2267	spin_lock_irqsave(&cmd->t_state_lock, *flags);
2268	task->task_flags &= ~TF_RUNNING;
2269	task->task_flags &= ~TF_STOP;
2270}
2271
2272static void transport_stop_all_task_timers(struct se_cmd *cmd)
2273{
2274	struct se_task *task = NULL, *task_tmp;
2275	unsigned long flags;
2276
2277	spin_lock_irqsave(&cmd->t_state_lock, flags);
2278	list_for_each_entry_safe(task, task_tmp,
2279				&cmd->t_task_list, t_list)
2280		__transport_stop_task_timer(task, &flags);
2281	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2282}
2283
2284static inline int transport_tcq_window_closed(struct se_device *dev)
2285{
2286	if (dev->dev_tcq_window_closed++ <
2287			PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
2288		msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
2289	} else
2290		msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
2291
2292	wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
2293	return 0;
2294}
2295
2296/*
2297 * Called from Fabric Module context from transport_execute_tasks()
2298 *
2299 * The return of this function determins if the tasks from struct se_cmd
2300 * get added to the execution queue in transport_execute_tasks(),
2301 * or are added to the delayed or ordered lists here.
2302 */
2303static inline int transport_execute_task_attr(struct se_cmd *cmd)
2304{
2305	if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
2306		return 1;
2307	/*
2308	 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
2309	 * to allow the passed struct se_cmd list of tasks to the front of the list.
2310	 */
2311	 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
2312		atomic_inc(&cmd->se_dev->dev_hoq_count);
2313		smp_mb__after_atomic_inc();
2314		pr_debug("Added HEAD_OF_QUEUE for CDB:"
2315			" 0x%02x, se_ordered_id: %u\n",
2316			cmd->t_task_cdb[0],
2317			cmd->se_ordered_id);
2318		return 1;
2319	} else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
2320		spin_lock(&cmd->se_dev->ordered_cmd_lock);
2321		list_add_tail(&cmd->se_ordered_node,
2322				&cmd->se_dev->ordered_cmd_list);
2323		spin_unlock(&cmd->se_dev->ordered_cmd_lock);
2324
2325		atomic_inc(&cmd->se_dev->dev_ordered_sync);
2326		smp_mb__after_atomic_inc();
2327
2328		pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
2329				" list, se_ordered_id: %u\n",
2330				cmd->t_task_cdb[0],
2331				cmd->se_ordered_id);
2332		/*
2333		 * Add ORDERED command to tail of execution queue if
2334		 * no other older commands exist that need to be
2335		 * completed first.
2336		 */
2337		if (!atomic_read(&cmd->se_dev->simple_cmds))
2338			return 1;
2339	} else {
2340		/*
2341		 * For SIMPLE and UNTAGGED Task Attribute commands
2342		 */
2343		atomic_inc(&cmd->se_dev->simple_cmds);
2344		smp_mb__after_atomic_inc();
2345	}
2346	/*
2347	 * Otherwise if one or more outstanding ORDERED task attribute exist,
2348	 * add the dormant task(s) built for the passed struct se_cmd to the
2349	 * execution queue and become in Active state for this struct se_device.
2350	 */
2351	if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
2352		/*
2353		 * Otherwise, add cmd w/ tasks to delayed cmd queue that
2354		 * will be drained upon completion of HEAD_OF_QUEUE task.
2355		 */
2356		spin_lock(&cmd->se_dev->delayed_cmd_lock);
2357		cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
2358		list_add_tail(&cmd->se_delayed_node,
2359				&cmd->se_dev->delayed_cmd_list);
2360		spin_unlock(&cmd->se_dev->delayed_cmd_lock);
2361
2362		pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
2363			" delayed CMD list, se_ordered_id: %u\n",
2364			cmd->t_task_cdb[0], cmd->sam_task_attr,
2365			cmd->se_ordered_id);
2366		/*
2367		 * Return zero to let transport_execute_tasks() know
2368		 * not to add the delayed tasks to the execution list.
2369		 */
2370		return 0;
2371	}
2372	/*
2373	 * Otherwise, no ORDERED task attributes exist..
2374	 */
2375	return 1;
2376}
2377
2378/*
2379 * Called from fabric module context in transport_generic_new_cmd() and
2380 * transport_generic_process_write()
2381 */
2382static int transport_execute_tasks(struct se_cmd *cmd)
2383{
2384	int add_tasks;
2385
2386	if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
2387		cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
2388		transport_generic_request_failure(cmd, NULL, 0, 1);
2389		return 0;
2390	}
2391
2392	/*
2393	 * Call transport_cmd_check_stop() to see if a fabric exception
2394	 * has occurred that prevents execution.
2395	 */
2396	if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
2397		/*
2398		 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2399		 * attribute for the tasks of the received struct se_cmd CDB
2400		 */
2401		add_tasks = transport_execute_task_attr(cmd);
2402		if (!add_tasks)
2403			goto execute_tasks;
2404		/*
2405		 * This calls transport_add_tasks_from_cmd() to handle
2406		 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2407		 * (if enabled) in __transport_add_task_to_execute_queue() and
2408		 * transport_add_task_check_sam_attr().
2409		 */
2410		transport_add_tasks_from_cmd(cmd);
2411	}
2412	/*
2413	 * Kick the execution queue for the cmd associated struct se_device
2414	 * storage object.
2415	 */
2416execute_tasks:
2417	__transport_execute_tasks(cmd->se_dev);
2418	return 0;
2419}
2420
2421/*
2422 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2423 * from struct se_device->execute_task_list and
2424 *
2425 * Called from transport_processing_thread()
2426 */
2427static int __transport_execute_tasks(struct se_device *dev)
2428{
2429	int error;
2430	struct se_cmd *cmd = NULL;
2431	struct se_task *task = NULL;
2432	unsigned long flags;
2433
2434	/*
2435	 * Check if there is enough room in the device and HBA queue to send
2436	 * struct se_tasks to the selected transport.
2437	 */
2438check_depth:
2439	if (!atomic_read(&dev->depth_left))
2440		return transport_tcq_window_closed(dev);
2441
2442	dev->dev_tcq_window_closed = 0;
2443
2444	spin_lock_irq(&dev->execute_task_lock);
2445	if (list_empty(&dev->execute_task_list)) {
 
 
 
2446		spin_unlock_irq(&dev->execute_task_lock);
2447		return 0;
2448	}
2449	task = list_first_entry(&dev->execute_task_list,
2450				struct se_task, t_execute_list);
2451	list_del(&task->t_execute_list);
2452	atomic_set(&task->task_execute_queue, 0);
2453	atomic_dec(&dev->execute_tasks);
2454	spin_unlock_irq(&dev->execute_task_lock);
2455
2456	atomic_dec(&dev->depth_left);
2457
2458	cmd = task->task_se_cmd;
2459
2460	spin_lock_irqsave(&cmd->t_state_lock, flags);
2461	atomic_set(&task->task_active, 1);
2462	atomic_set(&task->task_sent, 1);
2463	atomic_inc(&cmd->t_task_cdbs_sent);
2464
2465	if (atomic_read(&cmd->t_task_cdbs_sent) ==
2466	    cmd->t_task_list_num)
2467		atomic_set(&cmd->transport_sent, 1);
2468
2469	transport_start_task_timer(task);
2470	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2471	/*
2472	 * The struct se_cmd->transport_emulate_cdb() function pointer is used
2473	 * to grab REPORT_LUNS and other CDBs we want to handle before they hit the
2474	 * struct se_subsystem_api->do_task() caller below.
2475	 */
2476	if (cmd->transport_emulate_cdb) {
2477		error = cmd->transport_emulate_cdb(cmd);
2478		if (error != 0) {
2479			cmd->transport_error_status = error;
2480			atomic_set(&task->task_active, 0);
2481			atomic_set(&cmd->transport_sent, 0);
2482			transport_stop_tasks_for_cmd(cmd);
2483			transport_generic_request_failure(cmd, dev, 0, 1);
2484			goto check_depth;
2485		}
2486		/*
2487		 * Handle the successful completion for transport_emulate_cdb()
2488		 * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
2489		 * Otherwise the caller is expected to complete the task with
2490		 * proper status.
2491		 */
2492		if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
2493			cmd->scsi_status = SAM_STAT_GOOD;
2494			task->task_scsi_status = GOOD;
2495			transport_complete_task(task, 1);
2496		}
2497	} else {
2498		/*
2499		 * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
2500		 * RAMDISK we use the internal transport_emulate_control_cdb() logic
2501		 * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
2502		 * LUN emulation code.
2503		 *
2504		 * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
2505		 * call ->do_task() directly and let the underlying TCM subsystem plugin
2506		 * code handle the CDB emulation.
2507		 */
2508		if ((dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
2509		    (!(task->task_se_cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
2510			error = transport_emulate_control_cdb(task);
2511		else
2512			error = dev->transport->do_task(task);
2513
2514		if (error != 0) {
2515			cmd->transport_error_status = error;
2516			atomic_set(&task->task_active, 0);
2517			atomic_set(&cmd->transport_sent, 0);
2518			transport_stop_tasks_for_cmd(cmd);
2519			transport_generic_request_failure(cmd, dev, 0, 1);
2520		}
2521	}
2522
 
 
 
 
 
 
 
 
 
 
2523	goto check_depth;
2524
2525	return 0;
2526}
2527
2528void transport_new_cmd_failure(struct se_cmd *se_cmd)
2529{
2530	unsigned long flags;
2531	/*
2532	 * Any unsolicited data will get dumped for failed command inside of
2533	 * the fabric plugin
2534	 */
2535	spin_lock_irqsave(&se_cmd->t_state_lock, flags);
2536	se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
2537	se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2538	spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
2539}
2540
2541static void transport_nop_wait_for_tasks(struct se_cmd *, int, int);
2542
2543static inline u32 transport_get_sectors_6(
2544	unsigned char *cdb,
2545	struct se_cmd *cmd,
2546	int *ret)
2547{
2548	struct se_device *dev = cmd->se_dev;
2549
2550	/*
2551	 * Assume TYPE_DISK for non struct se_device objects.
2552	 * Use 8-bit sector value.
2553	 */
2554	if (!dev)
2555		goto type_disk;
2556
2557	/*
2558	 * Use 24-bit allocation length for TYPE_TAPE.
2559	 */
2560	if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2561		return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2562
2563	/*
2564	 * Everything else assume TYPE_DISK Sector CDB location.
2565	 * Use 8-bit sector value.
 
 
 
 
 
2566	 */
2567type_disk:
2568	return (u32)cdb[4];
2569}
2570
2571static inline u32 transport_get_sectors_10(
2572	unsigned char *cdb,
2573	struct se_cmd *cmd,
2574	int *ret)
2575{
2576	struct se_device *dev = cmd->se_dev;
2577
2578	/*
2579	 * Assume TYPE_DISK for non struct se_device objects.
2580	 * Use 16-bit sector value.
2581	 */
2582	if (!dev)
2583		goto type_disk;
2584
2585	/*
2586	 * XXX_10 is not defined in SSC, throw an exception
2587	 */
2588	if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2589		*ret = -EINVAL;
2590		return 0;
2591	}
2592
2593	/*
2594	 * Everything else assume TYPE_DISK Sector CDB location.
2595	 * Use 16-bit sector value.
2596	 */
2597type_disk:
2598	return (u32)(cdb[7] << 8) + cdb[8];
2599}
2600
2601static inline u32 transport_get_sectors_12(
2602	unsigned char *cdb,
2603	struct se_cmd *cmd,
2604	int *ret)
2605{
2606	struct se_device *dev = cmd->se_dev;
2607
2608	/*
2609	 * Assume TYPE_DISK for non struct se_device objects.
2610	 * Use 32-bit sector value.
2611	 */
2612	if (!dev)
2613		goto type_disk;
2614
2615	/*
2616	 * XXX_12 is not defined in SSC, throw an exception
2617	 */
2618	if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2619		*ret = -EINVAL;
2620		return 0;
2621	}
2622
2623	/*
2624	 * Everything else assume TYPE_DISK Sector CDB location.
2625	 * Use 32-bit sector value.
2626	 */
2627type_disk:
2628	return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2629}
2630
2631static inline u32 transport_get_sectors_16(
2632	unsigned char *cdb,
2633	struct se_cmd *cmd,
2634	int *ret)
2635{
2636	struct se_device *dev = cmd->se_dev;
2637
2638	/*
2639	 * Assume TYPE_DISK for non struct se_device objects.
2640	 * Use 32-bit sector value.
2641	 */
2642	if (!dev)
2643		goto type_disk;
2644
2645	/*
2646	 * Use 24-bit allocation length for TYPE_TAPE.
2647	 */
2648	if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2649		return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2650
2651type_disk:
2652	return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2653		    (cdb[12] << 8) + cdb[13];
2654}
2655
2656/*
2657 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2658 */
2659static inline u32 transport_get_sectors_32(
2660	unsigned char *cdb,
2661	struct se_cmd *cmd,
2662	int *ret)
2663{
2664	/*
2665	 * Assume TYPE_DISK for non struct se_device objects.
2666	 * Use 32-bit sector value.
2667	 */
2668	return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2669		    (cdb[30] << 8) + cdb[31];
2670
2671}
2672
2673static inline u32 transport_get_size(
2674	u32 sectors,
2675	unsigned char *cdb,
2676	struct se_cmd *cmd)
2677{
2678	struct se_device *dev = cmd->se_dev;
2679
2680	if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2681		if (cdb[1] & 1) { /* sectors */
2682			return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2683		} else /* bytes */
2684			return sectors;
2685	}
2686#if 0
2687	pr_debug("Returning block_size: %u, sectors: %u == %u for"
2688			" %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2689			dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2690			dev->transport->name);
2691#endif
2692	return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2693}
2694
2695static void transport_xor_callback(struct se_cmd *cmd)
2696{
2697	unsigned char *buf, *addr;
2698	struct scatterlist *sg;
2699	unsigned int offset;
2700	int i;
2701	int count;
2702	/*
2703	 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2704	 *
2705	 * 1) read the specified logical block(s);
2706	 * 2) transfer logical blocks from the data-out buffer;
2707	 * 3) XOR the logical blocks transferred from the data-out buffer with
2708	 *    the logical blocks read, storing the resulting XOR data in a buffer;
2709	 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2710	 *    blocks transferred from the data-out buffer; and
2711	 * 5) transfer the resulting XOR data to the data-in buffer.
2712	 */
2713	buf = kmalloc(cmd->data_length, GFP_KERNEL);
2714	if (!buf) {
2715		pr_err("Unable to allocate xor_callback buf\n");
2716		return;
2717	}
2718	/*
2719	 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
2720	 * into the locally allocated *buf
2721	 */
2722	sg_copy_to_buffer(cmd->t_data_sg,
2723			  cmd->t_data_nents,
2724			  buf,
2725			  cmd->data_length);
2726
2727	/*
2728	 * Now perform the XOR against the BIDI read memory located at
2729	 * cmd->t_mem_bidi_list
2730	 */
2731
2732	offset = 0;
2733	for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2734		addr = kmap_atomic(sg_page(sg), KM_USER0);
2735		if (!addr)
2736			goto out;
2737
2738		for (i = 0; i < sg->length; i++)
2739			*(addr + sg->offset + i) ^= *(buf + offset + i);
2740
2741		offset += sg->length;
2742		kunmap_atomic(addr, KM_USER0);
2743	}
2744
2745out:
2746	kfree(buf);
2747}
2748
2749/*
2750 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2751 */
2752static int transport_get_sense_data(struct se_cmd *cmd)
2753{
2754	unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
2755	struct se_device *dev;
2756	struct se_task *task = NULL, *task_tmp;
2757	unsigned long flags;
2758	u32 offset = 0;
2759
2760	WARN_ON(!cmd->se_lun);
2761
 
 
 
2762	spin_lock_irqsave(&cmd->t_state_lock, flags);
2763	if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2764		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2765		return 0;
2766	}
2767
2768	list_for_each_entry_safe(task, task_tmp,
2769				&cmd->t_task_list, t_list) {
2770
2771		if (!task->task_sense)
2772			continue;
2773
2774		dev = task->se_dev;
2775		if (!dev)
2776			continue;
2777
2778		if (!dev->transport->get_sense_buffer) {
2779			pr_err("dev->transport->get_sense_buffer"
2780					" is NULL\n");
2781			continue;
2782		}
2783
2784		sense_buffer = dev->transport->get_sense_buffer(task);
2785		if (!sense_buffer) {
2786			pr_err("ITT[0x%08x]_TASK[%d]: Unable to locate"
2787				" sense buffer for task with sense\n",
2788				cmd->se_tfo->get_task_tag(cmd), task->task_no);
2789			continue;
2790		}
2791		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2792
2793		offset = cmd->se_tfo->set_fabric_sense_len(cmd,
2794				TRANSPORT_SENSE_BUFFER);
2795
2796		memcpy(&buffer[offset], sense_buffer,
2797				TRANSPORT_SENSE_BUFFER);
2798		cmd->scsi_status = task->task_scsi_status;
2799		/* Automatically padded */
2800		cmd->scsi_sense_length =
2801				(TRANSPORT_SENSE_BUFFER + offset);
2802
2803		pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
2804				" and sense\n",
2805			dev->se_hba->hba_id, dev->transport->name,
2806				cmd->scsi_status);
2807		return 0;
2808	}
2809	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2810
2811	return -1;
2812}
2813
2814static int
2815transport_handle_reservation_conflict(struct se_cmd *cmd)
2816{
2817	cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
2818	cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2819	cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2820	cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2821	/*
2822	 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2823	 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2824	 * CONFLICT STATUS.
2825	 *
2826	 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2827	 */
2828	if (cmd->se_sess &&
2829	    cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
2830		core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
2831			cmd->orig_fe_lun, 0x2C,
2832			ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
2833	return -EINVAL;
2834}
2835
2836static inline long long transport_dev_end_lba(struct se_device *dev)
2837{
2838	return dev->transport->get_blocks(dev) + 1;
2839}
2840
2841static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2842{
2843	struct se_device *dev = cmd->se_dev;
2844	u32 sectors;
2845
2846	if (dev->transport->get_device_type(dev) != TYPE_DISK)
2847		return 0;
2848
2849	sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2850
2851	if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2852		pr_err("LBA: %llu Sectors: %u exceeds"
2853			" transport_dev_end_lba(): %llu\n",
2854			cmd->t_task_lba, sectors,
2855			transport_dev_end_lba(dev));
2856		return -EINVAL;
2857	}
2858
2859	return 0;
2860}
2861
2862static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2863{
2864	/*
2865	 * Determine if the received WRITE_SAME is used to for direct
2866	 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2867	 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2868	 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2869	 */
2870	int passthrough = (dev->transport->transport_type ==
2871				TRANSPORT_PLUGIN_PHBA_PDEV);
2872
2873	if (!passthrough) {
2874		if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2875			pr_err("WRITE_SAME PBDATA and LBDATA"
2876				" bits not supported for Block Discard"
2877				" Emulation\n");
2878			return -ENOSYS;
2879		}
2880		/*
2881		 * Currently for the emulated case we only accept
2882		 * tpws with the UNMAP=1 bit set.
2883		 */
2884		if (!(flags[0] & 0x08)) {
2885			pr_err("WRITE_SAME w/o UNMAP bit not"
2886				" supported for Block Discard Emulation\n");
2887			return -ENOSYS;
2888		}
2889	}
2890
2891	return 0;
2892}
2893
2894/*	transport_generic_cmd_sequencer():
2895 *
2896 *	Generic Command Sequencer that should work for most DAS transport
2897 *	drivers.
2898 *
2899 *	Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2900 *	RX Thread.
2901 *
2902 *	FIXME: Need to support other SCSI OPCODES where as well.
2903 */
2904static int transport_generic_cmd_sequencer(
2905	struct se_cmd *cmd,
2906	unsigned char *cdb)
2907{
2908	struct se_device *dev = cmd->se_dev;
2909	struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2910	int ret = 0, sector_ret = 0, passthrough;
2911	u32 sectors = 0, size = 0, pr_reg_type = 0;
2912	u16 service_action;
2913	u8 alua_ascq = 0;
2914	/*
2915	 * Check for an existing UNIT ATTENTION condition
2916	 */
2917	if (core_scsi3_ua_check(cmd, cdb) < 0) {
2918		cmd->transport_wait_for_tasks =
2919				&transport_nop_wait_for_tasks;
2920		cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2921		cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
2922		return -EINVAL;
2923	}
2924	/*
2925	 * Check status of Asymmetric Logical Unit Assignment port
2926	 */
2927	ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
2928	if (ret != 0) {
2929		cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
2930		/*
2931		 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
2932		 * The ALUA additional sense code qualifier (ASCQ) is determined
2933		 * by the ALUA primary or secondary access state..
2934		 */
2935		if (ret > 0) {
2936#if 0
2937			pr_debug("[%s]: ALUA TG Port not available,"
2938				" SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
2939				cmd->se_tfo->get_fabric_name(), alua_ascq);
2940#endif
2941			transport_set_sense_codes(cmd, 0x04, alua_ascq);
2942			cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2943			cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
2944			return -EINVAL;
2945		}
2946		goto out_invalid_cdb_field;
2947	}
2948	/*
2949	 * Check status for SPC-3 Persistent Reservations
2950	 */
2951	if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2952		if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
2953					cmd, cdb, pr_reg_type) != 0)
2954			return transport_handle_reservation_conflict(cmd);
 
 
 
 
 
2955		/*
2956		 * This means the CDB is allowed for the SCSI Initiator port
2957		 * when said port is *NOT* holding the legacy SPC-2 or
2958		 * SPC-3 Persistent Reservation.
2959		 */
2960	}
2961
 
 
 
 
 
 
 
2962	switch (cdb[0]) {
2963	case READ_6:
2964		sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2965		if (sector_ret)
2966			goto out_unsupported_cdb;
2967		size = transport_get_size(sectors, cdb, cmd);
2968		cmd->transport_split_cdb = &split_cdb_XX_6;
2969		cmd->t_task_lba = transport_lba_21(cdb);
2970		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2971		break;
2972	case READ_10:
2973		sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2974		if (sector_ret)
2975			goto out_unsupported_cdb;
2976		size = transport_get_size(sectors, cdb, cmd);
2977		cmd->transport_split_cdb = &split_cdb_XX_10;
2978		cmd->t_task_lba = transport_lba_32(cdb);
2979		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2980		break;
2981	case READ_12:
2982		sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2983		if (sector_ret)
2984			goto out_unsupported_cdb;
2985		size = transport_get_size(sectors, cdb, cmd);
2986		cmd->transport_split_cdb = &split_cdb_XX_12;
2987		cmd->t_task_lba = transport_lba_32(cdb);
2988		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2989		break;
2990	case READ_16:
2991		sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2992		if (sector_ret)
2993			goto out_unsupported_cdb;
2994		size = transport_get_size(sectors, cdb, cmd);
2995		cmd->transport_split_cdb = &split_cdb_XX_16;
2996		cmd->t_task_lba = transport_lba_64(cdb);
2997		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2998		break;
2999	case WRITE_6:
3000		sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
3001		if (sector_ret)
3002			goto out_unsupported_cdb;
3003		size = transport_get_size(sectors, cdb, cmd);
3004		cmd->transport_split_cdb = &split_cdb_XX_6;
3005		cmd->t_task_lba = transport_lba_21(cdb);
3006		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3007		break;
3008	case WRITE_10:
 
3009		sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3010		if (sector_ret)
3011			goto out_unsupported_cdb;
3012		size = transport_get_size(sectors, cdb, cmd);
3013		cmd->transport_split_cdb = &split_cdb_XX_10;
3014		cmd->t_task_lba = transport_lba_32(cdb);
3015		cmd->t_tasks_fua = (cdb[1] & 0x8);
 
3016		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3017		break;
3018	case WRITE_12:
3019		sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
3020		if (sector_ret)
3021			goto out_unsupported_cdb;
3022		size = transport_get_size(sectors, cdb, cmd);
3023		cmd->transport_split_cdb = &split_cdb_XX_12;
3024		cmd->t_task_lba = transport_lba_32(cdb);
3025		cmd->t_tasks_fua = (cdb[1] & 0x8);
 
3026		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3027		break;
3028	case WRITE_16:
3029		sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3030		if (sector_ret)
3031			goto out_unsupported_cdb;
3032		size = transport_get_size(sectors, cdb, cmd);
3033		cmd->transport_split_cdb = &split_cdb_XX_16;
3034		cmd->t_task_lba = transport_lba_64(cdb);
3035		cmd->t_tasks_fua = (cdb[1] & 0x8);
 
3036		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3037		break;
3038	case XDWRITEREAD_10:
3039		if ((cmd->data_direction != DMA_TO_DEVICE) ||
3040		    !(cmd->t_tasks_bidi))
3041			goto out_invalid_cdb_field;
3042		sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3043		if (sector_ret)
3044			goto out_unsupported_cdb;
3045		size = transport_get_size(sectors, cdb, cmd);
3046		cmd->transport_split_cdb = &split_cdb_XX_10;
3047		cmd->t_task_lba = transport_lba_32(cdb);
3048		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3049		passthrough = (dev->transport->transport_type ==
3050				TRANSPORT_PLUGIN_PHBA_PDEV);
3051		/*
3052		 * Skip the remaining assignments for TCM/PSCSI passthrough
3053		 */
3054		if (passthrough)
3055			break;
 
3056		/*
3057		 * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
3058		 */
3059		cmd->transport_complete_callback = &transport_xor_callback;
3060		cmd->t_tasks_fua = (cdb[1] & 0x8);
 
3061		break;
3062	case VARIABLE_LENGTH_CMD:
3063		service_action = get_unaligned_be16(&cdb[8]);
3064		/*
3065		 * Determine if this is TCM/PSCSI device and we should disable
3066		 * internal emulation for this CDB.
3067		 */
3068		passthrough = (dev->transport->transport_type ==
3069					TRANSPORT_PLUGIN_PHBA_PDEV);
3070
3071		switch (service_action) {
3072		case XDWRITEREAD_32:
3073			sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
3074			if (sector_ret)
3075				goto out_unsupported_cdb;
3076			size = transport_get_size(sectors, cdb, cmd);
3077			/*
3078			 * Use WRITE_32 and READ_32 opcodes for the emulated
3079			 * XDWRITE_READ_32 logic.
3080			 */
3081			cmd->transport_split_cdb = &split_cdb_XX_32;
3082			cmd->t_task_lba = transport_lba_64_ext(cdb);
3083			cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3084
3085			/*
3086			 * Skip the remaining assignments for TCM/PSCSI passthrough
3087			 */
3088			if (passthrough)
3089				break;
3090
3091			/*
3092			 * Setup BIDI XOR callback to be run during
3093			 * transport_generic_complete_ok()
3094			 */
3095			cmd->transport_complete_callback = &transport_xor_callback;
3096			cmd->t_tasks_fua = (cdb[10] & 0x8);
 
3097			break;
3098		case WRITE_SAME_32:
3099			sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
3100			if (sector_ret)
3101				goto out_unsupported_cdb;
3102
3103			if (sectors)
3104				size = transport_get_size(1, cdb, cmd);
3105			else {
3106				pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
3107				       " supported\n");
3108				goto out_invalid_cdb_field;
3109			}
3110
3111			cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
3112			cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3113
3114			if (target_check_write_same_discard(&cdb[10], dev) < 0)
3115				goto out_invalid_cdb_field;
3116
 
3117			break;
3118		default:
3119			pr_err("VARIABLE_LENGTH_CMD service action"
3120				" 0x%04x not supported\n", service_action);
3121			goto out_unsupported_cdb;
3122		}
3123		break;
3124	case MAINTENANCE_IN:
3125		if (dev->transport->get_device_type(dev) != TYPE_ROM) {
3126			/* MAINTENANCE_IN from SCC-2 */
3127			/*
3128			 * Check for emulated MI_REPORT_TARGET_PGS.
3129			 */
3130			if (cdb[1] == MI_REPORT_TARGET_PGS) {
3131				cmd->transport_emulate_cdb =
3132				(su_dev->t10_alua.alua_type ==
3133				 SPC3_ALUA_EMULATED) ?
3134				core_emulate_report_target_port_groups :
3135				NULL;
3136			}
3137			size = (cdb[6] << 24) | (cdb[7] << 16) |
3138			       (cdb[8] << 8) | cdb[9];
3139		} else {
3140			/* GPCMD_SEND_KEY from multi media commands */
3141			size = (cdb[8] << 8) + cdb[9];
3142		}
3143		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3144		break;
3145	case MODE_SELECT:
3146		size = cdb[4];
3147		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3148		break;
3149	case MODE_SELECT_10:
3150		size = (cdb[7] << 8) + cdb[8];
3151		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3152		break;
3153	case MODE_SENSE:
3154		size = cdb[4];
3155		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
 
 
3156		break;
3157	case MODE_SENSE_10:
 
 
 
 
 
3158	case GPCMD_READ_BUFFER_CAPACITY:
3159	case GPCMD_SEND_OPC:
3160	case LOG_SELECT:
3161	case LOG_SENSE:
3162		size = (cdb[7] << 8) + cdb[8];
3163		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3164		break;
3165	case READ_BLOCK_LIMITS:
3166		size = READ_BLOCK_LEN;
3167		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3168		break;
3169	case GPCMD_GET_CONFIGURATION:
3170	case GPCMD_READ_FORMAT_CAPACITIES:
3171	case GPCMD_READ_DISC_INFO:
3172	case GPCMD_READ_TRACK_RZONE_INFO:
3173		size = (cdb[7] << 8) + cdb[8];
3174		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3175		break;
3176	case PERSISTENT_RESERVE_IN:
 
 
 
 
 
3177	case PERSISTENT_RESERVE_OUT:
3178		cmd->transport_emulate_cdb =
3179			(su_dev->t10_pr.res_type ==
3180			 SPC3_PERSISTENT_RESERVATIONS) ?
3181			core_scsi3_emulate_pr : NULL;
3182		size = (cdb[7] << 8) + cdb[8];
3183		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3184		break;
3185	case GPCMD_MECHANISM_STATUS:
3186	case GPCMD_READ_DVD_STRUCTURE:
3187		size = (cdb[8] << 8) + cdb[9];
3188		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3189		break;
3190	case READ_POSITION:
3191		size = READ_POSITION_LEN;
3192		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3193		break;
3194	case MAINTENANCE_OUT:
3195		if (dev->transport->get_device_type(dev) != TYPE_ROM) {
3196			/* MAINTENANCE_OUT from SCC-2
3197			 *
3198			 * Check for emulated MO_SET_TARGET_PGS.
3199			 */
3200			if (cdb[1] == MO_SET_TARGET_PGS) {
3201				cmd->transport_emulate_cdb =
3202				(su_dev->t10_alua.alua_type ==
3203					SPC3_ALUA_EMULATED) ?
3204				core_emulate_set_target_port_groups :
3205				NULL;
3206			}
3207
3208			size = (cdb[6] << 24) | (cdb[7] << 16) |
3209			       (cdb[8] << 8) | cdb[9];
3210		} else  {
3211			/* GPCMD_REPORT_KEY from multi media commands */
3212			size = (cdb[8] << 8) + cdb[9];
3213		}
3214		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3215		break;
3216	case INQUIRY:
3217		size = (cdb[3] << 8) + cdb[4];
3218		/*
3219		 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
3220		 * See spc4r17 section 5.3
3221		 */
3222		if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3223			cmd->sam_task_attr = MSG_HEAD_TAG;
3224		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
 
 
3225		break;
3226	case READ_BUFFER:
3227		size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3228		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3229		break;
3230	case READ_CAPACITY:
3231		size = READ_CAP_LEN;
3232		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
 
 
3233		break;
3234	case READ_MEDIA_SERIAL_NUMBER:
3235	case SECURITY_PROTOCOL_IN:
3236	case SECURITY_PROTOCOL_OUT:
3237		size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3238		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3239		break;
3240	case SERVICE_ACTION_IN:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3241	case ACCESS_CONTROL_IN:
3242	case ACCESS_CONTROL_OUT:
3243	case EXTENDED_COPY:
3244	case READ_ATTRIBUTE:
3245	case RECEIVE_COPY_RESULTS:
3246	case WRITE_ATTRIBUTE:
3247		size = (cdb[10] << 24) | (cdb[11] << 16) |
3248		       (cdb[12] << 8) | cdb[13];
3249		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3250		break;
3251	case RECEIVE_DIAGNOSTIC:
3252	case SEND_DIAGNOSTIC:
3253		size = (cdb[3] << 8) | cdb[4];
3254		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3255		break;
3256/* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
3257#if 0
3258	case GPCMD_READ_CD:
3259		sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3260		size = (2336 * sectors);
3261		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3262		break;
3263#endif
3264	case READ_TOC:
3265		size = cdb[8];
3266		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3267		break;
3268	case REQUEST_SENSE:
3269		size = cdb[4];
3270		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
 
 
3271		break;
3272	case READ_ELEMENT_STATUS:
3273		size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
3274		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3275		break;
3276	case WRITE_BUFFER:
3277		size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3278		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3279		break;
3280	case RESERVE:
3281	case RESERVE_10:
3282		/*
3283		 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
3284		 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3285		 */
3286		if (cdb[0] == RESERVE_10)
3287			size = (cdb[7] << 8) | cdb[8];
3288		else
3289			size = cmd->data_length;
3290
3291		/*
3292		 * Setup the legacy emulated handler for SPC-2 and
3293		 * >= SPC-3 compatible reservation handling (CRH=1)
3294		 * Otherwise, we assume the underlying SCSI logic is
3295		 * is running in SPC_PASSTHROUGH, and wants reservations
3296		 * emulation disabled.
3297		 */
3298		cmd->transport_emulate_cdb =
3299				(su_dev->t10_pr.res_type !=
3300				 SPC_PASSTHROUGH) ?
3301				core_scsi2_emulate_crh : NULL;
3302		cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3303		break;
3304	case RELEASE:
3305	case RELEASE_10:
3306		/*
3307		 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
3308		 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3309		*/
3310		if (cdb[0] == RELEASE_10)
3311			size = (cdb[7] << 8) | cdb[8];
3312		else
3313			size = cmd->data_length;
3314
3315		cmd->transport_emulate_cdb =
3316				(su_dev->t10_pr.res_type !=
3317				 SPC_PASSTHROUGH) ?
3318				core_scsi2_emulate_crh : NULL;
3319		cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3320		break;
3321	case SYNCHRONIZE_CACHE:
3322	case 0x91: /* SYNCHRONIZE_CACHE_16: */
3323		/*
3324		 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
3325		 */
3326		if (cdb[0] == SYNCHRONIZE_CACHE) {
3327			sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3328			cmd->t_task_lba = transport_lba_32(cdb);
3329		} else {
3330			sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3331			cmd->t_task_lba = transport_lba_64(cdb);
3332		}
3333		if (sector_ret)
3334			goto out_unsupported_cdb;
3335
3336		size = transport_get_size(sectors, cdb, cmd);
3337		cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3338
3339		/*
3340		 * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
3341		 */
3342		if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
3343			break;
3344		/*
3345		 * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
3346		 * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
3347		 */
3348		cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
3349		/*
3350		 * Check to ensure that LBA + Range does not exceed past end of
3351		 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
3352		 */
3353		if ((cmd->t_task_lba != 0) || (sectors != 0)) {
3354			if (transport_cmd_get_valid_sectors(cmd) < 0)
3355				goto out_invalid_cdb_field;
3356		}
 
3357		break;
3358	case UNMAP:
3359		size = get_unaligned_be16(&cdb[7]);
3360		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
 
 
3361		break;
3362	case WRITE_SAME_16:
3363		sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3364		if (sector_ret)
3365			goto out_unsupported_cdb;
3366
3367		if (sectors)
3368			size = transport_get_size(1, cdb, cmd);
3369		else {
3370			pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3371			goto out_invalid_cdb_field;
3372		}
3373
3374		cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
3375		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3376
3377		if (target_check_write_same_discard(&cdb[1], dev) < 0)
3378			goto out_invalid_cdb_field;
 
 
3379		break;
3380	case WRITE_SAME:
3381		sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3382		if (sector_ret)
3383			goto out_unsupported_cdb;
3384
3385		if (sectors)
3386			size = transport_get_size(1, cdb, cmd);
3387		else {
3388			pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3389			goto out_invalid_cdb_field;
3390		}
3391
3392		cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
3393		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3394		/*
3395		 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
3396		 * of byte 1 bit 3 UNMAP instead of original reserved field
3397		 */
3398		if (target_check_write_same_discard(&cdb[1], dev) < 0)
3399			goto out_invalid_cdb_field;
 
 
3400		break;
3401	case ALLOW_MEDIUM_REMOVAL:
3402	case GPCMD_CLOSE_TRACK:
3403	case ERASE:
3404	case INITIALIZE_ELEMENT_STATUS:
3405	case GPCMD_LOAD_UNLOAD:
3406	case REZERO_UNIT:
3407	case SEEK_10:
3408	case GPCMD_SET_SPEED:
3409	case SPACE:
3410	case START_STOP:
3411	case TEST_UNIT_READY:
3412	case VERIFY:
3413	case WRITE_FILEMARKS:
 
 
 
 
 
 
 
 
3414	case MOVE_MEDIUM:
3415		cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3416		break;
3417	case REPORT_LUNS:
3418		cmd->transport_emulate_cdb =
3419				transport_core_report_lun_response;
3420		size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3421		/*
3422		 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3423		 * See spc4r17 section 5.3
3424		 */
3425		if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3426			cmd->sam_task_attr = MSG_HEAD_TAG;
3427		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3428		break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3429	default:
3430		pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
3431			" 0x%02x, sending CHECK_CONDITION.\n",
3432			cmd->se_tfo->get_fabric_name(), cdb[0]);
3433		cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
3434		goto out_unsupported_cdb;
3435	}
3436
 
 
 
3437	if (size != cmd->data_length) {
3438		pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
3439			" %u does not match SCSI CDB Length: %u for SAM Opcode:"
3440			" 0x%02x\n", cmd->se_tfo->get_fabric_name(),
3441				cmd->data_length, size, cdb[0]);
3442
3443		cmd->cmd_spdtl = size;
3444
3445		if (cmd->data_direction == DMA_TO_DEVICE) {
3446			pr_err("Rejecting underflow/overflow"
3447					" WRITE data\n");
3448			goto out_invalid_cdb_field;
3449		}
3450		/*
3451		 * Reject READ_* or WRITE_* with overflow/underflow for
3452		 * type SCF_SCSI_DATA_SG_IO_CDB.
3453		 */
3454		if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512))  {
3455			pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
3456				" CDB on non 512-byte sector setup subsystem"
3457				" plugin: %s\n", dev->transport->name);
3458			/* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3459			goto out_invalid_cdb_field;
3460		}
3461
 
 
 
 
 
3462		if (size > cmd->data_length) {
3463			cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3464			cmd->residual_count = (size - cmd->data_length);
3465		} else {
3466			cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3467			cmd->residual_count = (cmd->data_length - size);
 
3468		}
3469		cmd->data_length = size;
3470	}
3471
3472	/* Let's limit control cdbs to a page, for simplicity's sake. */
3473	if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3474	    size > PAGE_SIZE)
3475		goto out_invalid_cdb_field;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3476
3477	transport_set_supported_SAM_opcode(cmd);
3478	return ret;
3479
3480out_unsupported_cdb:
3481	cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3482	cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
3483	return -EINVAL;
3484out_invalid_cdb_field:
3485	cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3486	cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3487	return -EINVAL;
3488}
3489
3490/*
3491 * Called from transport_generic_complete_ok() and
3492 * transport_generic_request_failure() to determine which dormant/delayed
3493 * and ordered cmds need to have their tasks added to the execution queue.
3494 */
3495static void transport_complete_task_attr(struct se_cmd *cmd)
3496{
3497	struct se_device *dev = cmd->se_dev;
3498	struct se_cmd *cmd_p, *cmd_tmp;
3499	int new_active_tasks = 0;
3500
3501	if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
3502		atomic_dec(&dev->simple_cmds);
3503		smp_mb__after_atomic_dec();
3504		dev->dev_cur_ordered_id++;
3505		pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
3506			" SIMPLE: %u\n", dev->dev_cur_ordered_id,
3507			cmd->se_ordered_id);
3508	} else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
3509		atomic_dec(&dev->dev_hoq_count);
3510		smp_mb__after_atomic_dec();
3511		dev->dev_cur_ordered_id++;
3512		pr_debug("Incremented dev_cur_ordered_id: %u for"
3513			" HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3514			cmd->se_ordered_id);
3515	} else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
3516		spin_lock(&dev->ordered_cmd_lock);
3517		list_del(&cmd->se_ordered_node);
3518		atomic_dec(&dev->dev_ordered_sync);
3519		smp_mb__after_atomic_dec();
3520		spin_unlock(&dev->ordered_cmd_lock);
3521
3522		dev->dev_cur_ordered_id++;
3523		pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
3524			" %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3525	}
3526	/*
3527	 * Process all commands up to the last received
3528	 * ORDERED task attribute which requires another blocking
3529	 * boundary
3530	 */
3531	spin_lock(&dev->delayed_cmd_lock);
3532	list_for_each_entry_safe(cmd_p, cmd_tmp,
3533			&dev->delayed_cmd_list, se_delayed_node) {
3534
3535		list_del(&cmd_p->se_delayed_node);
3536		spin_unlock(&dev->delayed_cmd_lock);
3537
3538		pr_debug("Calling add_tasks() for"
3539			" cmd_p: 0x%02x Task Attr: 0x%02x"
3540			" Dormant -> Active, se_ordered_id: %u\n",
3541			cmd_p->t_task_cdb[0],
3542			cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3543
3544		transport_add_tasks_from_cmd(cmd_p);
3545		new_active_tasks++;
3546
3547		spin_lock(&dev->delayed_cmd_lock);
3548		if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
3549			break;
3550	}
3551	spin_unlock(&dev->delayed_cmd_lock);
3552	/*
3553	 * If new tasks have become active, wake up the transport thread
3554	 * to do the processing of the Active tasks.
3555	 */
3556	if (new_active_tasks != 0)
3557		wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
3558}
3559
3560static int transport_complete_qf(struct se_cmd *cmd)
3561{
3562	int ret = 0;
3563
3564	if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)
3565		return cmd->se_tfo->queue_status(cmd);
 
 
 
 
 
 
3566
3567	switch (cmd->data_direction) {
3568	case DMA_FROM_DEVICE:
3569		ret = cmd->se_tfo->queue_data_in(cmd);
3570		break;
3571	case DMA_TO_DEVICE:
3572		if (cmd->t_bidi_data_sg) {
3573			ret = cmd->se_tfo->queue_data_in(cmd);
3574			if (ret < 0)
3575				return ret;
3576		}
3577		/* Fall through for DMA_TO_DEVICE */
3578	case DMA_NONE:
3579		ret = cmd->se_tfo->queue_status(cmd);
3580		break;
3581	default:
3582		break;
3583	}
3584
3585	return ret;
 
 
 
 
 
 
3586}
3587
3588static void transport_handle_queue_full(
3589	struct se_cmd *cmd,
3590	struct se_device *dev,
3591	int (*qf_callback)(struct se_cmd *))
3592{
3593	spin_lock_irq(&dev->qf_cmd_lock);
3594	cmd->se_cmd_flags |= SCF_EMULATE_QUEUE_FULL;
3595	cmd->transport_qf_callback = qf_callback;
3596	list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3597	atomic_inc(&dev->dev_qf_count);
3598	smp_mb__after_atomic_inc();
3599	spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3600
3601	schedule_work(&cmd->se_dev->qf_work_queue);
3602}
3603
3604static void transport_generic_complete_ok(struct se_cmd *cmd)
3605{
3606	int reason = 0, ret;
 
 
3607	/*
3608	 * Check if we need to move delayed/dormant tasks from cmds on the
3609	 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3610	 * Attribute.
3611	 */
3612	if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3613		transport_complete_task_attr(cmd);
3614	/*
3615	 * Check to schedule QUEUE_FULL work, or execute an existing
3616	 * cmd->transport_qf_callback()
3617	 */
3618	if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3619		schedule_work(&cmd->se_dev->qf_work_queue);
3620
3621	if (cmd->transport_qf_callback) {
3622		ret = cmd->transport_qf_callback(cmd);
3623		if (ret < 0)
3624			goto queue_full;
3625
3626		cmd->transport_qf_callback = NULL;
3627		goto done;
3628	}
3629	/*
3630	 * Check if we need to retrieve a sense buffer from
3631	 * the struct se_cmd in question.
3632	 */
3633	if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3634		if (transport_get_sense_data(cmd) < 0)
3635			reason = TCM_NON_EXISTENT_LUN;
3636
3637		/*
3638		 * Only set when an struct se_task->task_scsi_status returned
3639		 * a non GOOD status.
3640		 */
3641		if (cmd->scsi_status) {
3642			ret = transport_send_check_condition_and_sense(
3643					cmd, reason, 1);
3644			if (ret == -EAGAIN)
3645				goto queue_full;
3646
3647			transport_lun_remove_cmd(cmd);
3648			transport_cmd_check_stop_to_fabric(cmd);
3649			return;
3650		}
3651	}
3652	/*
3653	 * Check for a callback, used by amongst other things
3654	 * XDWRITE_READ_10 emulation.
3655	 */
3656	if (cmd->transport_complete_callback)
3657		cmd->transport_complete_callback(cmd);
3658
3659	switch (cmd->data_direction) {
3660	case DMA_FROM_DEVICE:
3661		spin_lock(&cmd->se_lun->lun_sep_lock);
3662		if (cmd->se_lun->lun_sep) {
3663			cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3664					cmd->data_length;
3665		}
3666		spin_unlock(&cmd->se_lun->lun_sep_lock);
3667
3668		ret = cmd->se_tfo->queue_data_in(cmd);
3669		if (ret == -EAGAIN)
3670			goto queue_full;
3671		break;
3672	case DMA_TO_DEVICE:
3673		spin_lock(&cmd->se_lun->lun_sep_lock);
3674		if (cmd->se_lun->lun_sep) {
3675			cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
3676				cmd->data_length;
3677		}
3678		spin_unlock(&cmd->se_lun->lun_sep_lock);
3679		/*
3680		 * Check if we need to send READ payload for BIDI-COMMAND
3681		 */
3682		if (cmd->t_bidi_data_sg) {
3683			spin_lock(&cmd->se_lun->lun_sep_lock);
3684			if (cmd->se_lun->lun_sep) {
3685				cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3686					cmd->data_length;
3687			}
3688			spin_unlock(&cmd->se_lun->lun_sep_lock);
3689			ret = cmd->se_tfo->queue_data_in(cmd);
3690			if (ret == -EAGAIN)
3691				goto queue_full;
3692			break;
3693		}
3694		/* Fall through for DMA_TO_DEVICE */
3695	case DMA_NONE:
3696		ret = cmd->se_tfo->queue_status(cmd);
3697		if (ret == -EAGAIN)
3698			goto queue_full;
3699		break;
3700	default:
3701		break;
3702	}
3703
3704done:
3705	transport_lun_remove_cmd(cmd);
3706	transport_cmd_check_stop_to_fabric(cmd);
3707	return;
3708
3709queue_full:
3710	pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
3711		" data_direction: %d\n", cmd, cmd->data_direction);
3712	transport_handle_queue_full(cmd, cmd->se_dev, transport_complete_qf);
3713}
3714
3715static void transport_free_dev_tasks(struct se_cmd *cmd)
3716{
3717	struct se_task *task, *task_tmp;
3718	unsigned long flags;
3719
3720	spin_lock_irqsave(&cmd->t_state_lock, flags);
3721	list_for_each_entry_safe(task, task_tmp,
3722				&cmd->t_task_list, t_list) {
3723		if (atomic_read(&task->task_active))
3724			continue;
3725
3726		kfree(task->task_sg_bidi);
3727		kfree(task->task_sg);
3728
3729		list_del(&task->t_list);
3730
3731		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3732		if (task->se_dev)
3733			task->se_dev->transport->free_task(task);
3734		else
3735			pr_err("task[%u] - task->se_dev is NULL\n",
3736				task->task_no);
3737		spin_lock_irqsave(&cmd->t_state_lock, flags);
3738	}
3739	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3740}
3741
3742static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
3743{
3744	struct scatterlist *sg;
3745	int count;
3746
3747	for_each_sg(sgl, sg, nents, count)
3748		__free_page(sg_page(sg));
3749
3750	kfree(sgl);
3751}
3752
3753static inline void transport_free_pages(struct se_cmd *cmd)
3754{
3755	if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
3756		return;
3757
3758	transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
3759	cmd->t_data_sg = NULL;
3760	cmd->t_data_nents = 0;
3761
3762	transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
3763	cmd->t_bidi_data_sg = NULL;
3764	cmd->t_bidi_data_nents = 0;
3765}
3766
3767static inline void transport_release_tasks(struct se_cmd *cmd)
3768{
3769	transport_free_dev_tasks(cmd);
3770}
3771
3772static inline int transport_dec_and_check(struct se_cmd *cmd)
3773{
3774	unsigned long flags;
3775
3776	spin_lock_irqsave(&cmd->t_state_lock, flags);
3777	if (atomic_read(&cmd->t_fe_count)) {
3778		if (!atomic_dec_and_test(&cmd->t_fe_count)) {
3779			spin_unlock_irqrestore(&cmd->t_state_lock,
3780					flags);
3781			return 1;
3782		}
3783	}
3784
3785	if (atomic_read(&cmd->t_se_count)) {
3786		if (!atomic_dec_and_test(&cmd->t_se_count)) {
3787			spin_unlock_irqrestore(&cmd->t_state_lock,
3788					flags);
3789			return 1;
3790		}
3791	}
3792	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3793
3794	return 0;
3795}
3796
3797static void transport_release_fe_cmd(struct se_cmd *cmd)
3798{
3799	unsigned long flags;
3800
3801	if (transport_dec_and_check(cmd))
 
 
 
 
 
 
 
 
 
3802		return;
3803
3804	spin_lock_irqsave(&cmd->t_state_lock, flags);
3805	if (!atomic_read(&cmd->transport_dev_active)) {
3806		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3807		goto free_pages;
3808	}
3809	atomic_set(&cmd->transport_dev_active, 0);
3810	transport_all_task_dev_remove_state(cmd);
3811	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3812
3813	transport_release_tasks(cmd);
3814free_pages:
3815	transport_free_pages(cmd);
3816	transport_free_se_cmd(cmd);
3817	cmd->se_tfo->release_cmd(cmd);
3818}
3819
3820static int
3821transport_generic_remove(struct se_cmd *cmd, int session_reinstatement)
 
 
 
 
 
3822{
3823	unsigned long flags;
3824
3825	if (transport_dec_and_check(cmd)) {
3826		if (session_reinstatement) {
3827			spin_lock_irqsave(&cmd->t_state_lock, flags);
3828			transport_all_task_dev_remove_state(cmd);
3829			spin_unlock_irqrestore(&cmd->t_state_lock,
3830					flags);
3831		}
3832		return 1;
3833	}
3834
3835	spin_lock_irqsave(&cmd->t_state_lock, flags);
3836	if (!atomic_read(&cmd->transport_dev_active)) {
3837		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3838		goto free_pages;
3839	}
3840	atomic_set(&cmd->transport_dev_active, 0);
3841	transport_all_task_dev_remove_state(cmd);
3842	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3843
3844	transport_release_tasks(cmd);
3845
3846free_pages:
3847	transport_free_pages(cmd);
3848	transport_release_cmd(cmd);
3849	return 0;
 
 
3850}
3851
3852/*
3853 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3854 * allocating in the core.
3855 * @cmd:  Associated se_cmd descriptor
3856 * @mem:  SGL style memory for TCM WRITE / READ
3857 * @sg_mem_num: Number of SGL elements
3858 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3859 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3860 *
3861 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3862 * of parameters.
3863 */
3864int transport_generic_map_mem_to_cmd(
3865	struct se_cmd *cmd,
3866	struct scatterlist *sgl,
3867	u32 sgl_count,
3868	struct scatterlist *sgl_bidi,
3869	u32 sgl_bidi_count)
3870{
3871	if (!sgl || !sgl_count)
3872		return 0;
3873
3874	if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3875	    (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
 
 
 
 
 
 
 
 
 
 
 
 
3876
3877		cmd->t_data_sg = sgl;
3878		cmd->t_data_nents = sgl_count;
3879
3880		if (sgl_bidi && sgl_bidi_count) {
3881			cmd->t_bidi_data_sg = sgl_bidi;
3882			cmd->t_bidi_data_nents = sgl_bidi_count;
3883		}
3884		cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
3885	}
3886
3887	return 0;
3888}
3889EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3890
3891static int transport_new_cmd_obj(struct se_cmd *cmd)
3892{
3893	struct se_device *dev = cmd->se_dev;
3894	int set_counts = 1, rc, task_cdbs;
3895
3896	/*
3897	 * Setup any BIDI READ tasks and memory from
3898	 * cmd->t_mem_bidi_list so the READ struct se_tasks
3899	 * are queued first for the non pSCSI passthrough case.
3900	 */
3901	if (cmd->t_bidi_data_sg &&
3902	    (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV)) {
3903		rc = transport_allocate_tasks(cmd,
3904					      cmd->t_task_lba,
3905					      DMA_FROM_DEVICE,
3906					      cmd->t_bidi_data_sg,
3907					      cmd->t_bidi_data_nents);
3908		if (rc <= 0) {
3909			cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3910			cmd->scsi_sense_reason =
3911				TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3912			return -EINVAL;
3913		}
3914		atomic_inc(&cmd->t_fe_count);
3915		atomic_inc(&cmd->t_se_count);
3916		set_counts = 0;
3917	}
3918	/*
3919	 * Setup the tasks and memory from cmd->t_mem_list
3920	 * Note for BIDI transfers this will contain the WRITE payload
3921	 */
3922	task_cdbs = transport_allocate_tasks(cmd,
3923					     cmd->t_task_lba,
3924					     cmd->data_direction,
3925					     cmd->t_data_sg,
3926					     cmd->t_data_nents);
3927	if (task_cdbs <= 0) {
3928		cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3929		cmd->scsi_sense_reason =
3930			TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3931		return -EINVAL;
3932	}
3933
3934	if (set_counts) {
3935		atomic_inc(&cmd->t_fe_count);
3936		atomic_inc(&cmd->t_se_count);
3937	}
3938
3939	cmd->t_task_list_num = task_cdbs;
3940
3941	atomic_set(&cmd->t_task_cdbs_left, task_cdbs);
3942	atomic_set(&cmd->t_task_cdbs_ex_left, task_cdbs);
3943	atomic_set(&cmd->t_task_cdbs_timeout_left, task_cdbs);
3944	return 0;
3945}
3946
3947void *transport_kmap_first_data_page(struct se_cmd *cmd)
3948{
3949	struct scatterlist *sg = cmd->t_data_sg;
 
 
3950
3951	BUG_ON(!sg);
3952	/*
3953	 * We need to take into account a possible offset here for fabrics like
3954	 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3955	 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
3956	 */
3957	return kmap(sg_page(sg)) + sg->offset;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3958}
3959EXPORT_SYMBOL(transport_kmap_first_data_page);
3960
3961void transport_kunmap_first_data_page(struct se_cmd *cmd)
3962{
3963	kunmap(sg_page(cmd->t_data_sg));
 
 
 
 
 
 
 
 
3964}
3965EXPORT_SYMBOL(transport_kunmap_first_data_page);
3966
3967static int
3968transport_generic_get_mem(struct se_cmd *cmd)
3969{
3970	u32 length = cmd->data_length;
3971	unsigned int nents;
3972	struct page *page;
 
3973	int i = 0;
3974
3975	nents = DIV_ROUND_UP(length, PAGE_SIZE);
3976	cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3977	if (!cmd->t_data_sg)
3978		return -ENOMEM;
3979
3980	cmd->t_data_nents = nents;
3981	sg_init_table(cmd->t_data_sg, nents);
3982
 
 
3983	while (length) {
3984		u32 page_len = min_t(u32, length, PAGE_SIZE);
3985		page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3986		if (!page)
3987			goto out;
3988
3989		sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3990		length -= page_len;
3991		i++;
3992	}
3993	return 0;
3994
3995out:
3996	while (i >= 0) {
3997		__free_page(sg_page(&cmd->t_data_sg[i]));
3998		i--;
 
3999	}
4000	kfree(cmd->t_data_sg);
4001	cmd->t_data_sg = NULL;
4002	return -ENOMEM;
4003}
4004
4005/* Reduce sectors if they are too long for the device */
4006static inline sector_t transport_limit_task_sectors(
4007	struct se_device *dev,
4008	unsigned long long lba,
4009	sector_t sectors)
4010{
4011	sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
4012
4013	if (dev->transport->get_device_type(dev) == TYPE_DISK)
4014		if ((lba + sectors) > transport_dev_end_lba(dev))
4015			sectors = ((transport_dev_end_lba(dev) - lba) + 1);
4016
4017	return sectors;
4018}
4019
4020
4021/*
4022 * This function can be used by HW target mode drivers to create a linked
4023 * scatterlist from all contiguously allocated struct se_task->task_sg[].
4024 * This is intended to be called during the completion path by TCM Core
4025 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
4026 */
4027void transport_do_task_sg_chain(struct se_cmd *cmd)
4028{
4029	struct scatterlist *sg_first = NULL;
4030	struct scatterlist *sg_prev = NULL;
4031	int sg_prev_nents = 0;
4032	struct scatterlist *sg;
4033	struct se_task *task;
4034	u32 chained_nents = 0;
4035	int i;
4036
4037	BUG_ON(!cmd->se_tfo->task_sg_chaining);
4038
4039	/*
4040	 * Walk the struct se_task list and setup scatterlist chains
4041	 * for each contiguously allocated struct se_task->task_sg[].
4042	 */
4043	list_for_each_entry(task, &cmd->t_task_list, t_list) {
4044		if (!task->task_sg)
4045			continue;
4046
4047		if (!sg_first) {
4048			sg_first = task->task_sg;
4049			chained_nents = task->task_sg_nents;
4050		} else {
4051			sg_chain(sg_prev, sg_prev_nents, task->task_sg);
4052			chained_nents += task->task_sg_nents;
4053		}
4054		/*
4055		 * For the padded tasks, use the extra SGL vector allocated
4056		 * in transport_allocate_data_tasks() for the sg_prev_nents
4057		 * offset into sg_chain() above..  The last task of a
4058		 * multi-task list, or a single task will not have
4059		 * task->task_sg_padded set..
4060		 */
4061		if (task->task_padded_sg)
4062			sg_prev_nents = (task->task_sg_nents + 1);
4063		else
4064			sg_prev_nents = task->task_sg_nents;
4065
4066		sg_prev = task->task_sg;
4067	}
4068	/*
4069	 * Setup the starting pointer and total t_tasks_sg_linked_no including
4070	 * padding SGs for linking and to mark the end.
4071	 */
4072	cmd->t_tasks_sg_chained = sg_first;
4073	cmd->t_tasks_sg_chained_no = chained_nents;
4074
4075	pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
4076		" t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
4077		cmd->t_tasks_sg_chained_no);
4078
4079	for_each_sg(cmd->t_tasks_sg_chained, sg,
4080			cmd->t_tasks_sg_chained_no, i) {
4081
4082		pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
4083			i, sg, sg_page(sg), sg->length, sg->offset);
4084		if (sg_is_chain(sg))
4085			pr_debug("SG: %p sg_is_chain=1\n", sg);
4086		if (sg_is_last(sg))
4087			pr_debug("SG: %p sg_is_last=1\n", sg);
4088	}
4089}
4090EXPORT_SYMBOL(transport_do_task_sg_chain);
4091
4092/*
4093 * Break up cmd into chunks transport can handle
 
 
4094 */
4095static int transport_allocate_data_tasks(
4096	struct se_cmd *cmd,
4097	unsigned long long lba,
4098	enum dma_data_direction data_direction,
4099	struct scatterlist *sgl,
4100	unsigned int sgl_nents)
4101{
4102	unsigned char *cdb = NULL;
4103	struct se_task *task;
4104	struct se_device *dev = cmd->se_dev;
4105	unsigned long flags;
4106	int task_count, i, ret;
4107	sector_t sectors, dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
4108	u32 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
4109	struct scatterlist *sg;
4110	struct scatterlist *cmd_sg;
4111
4112	WARN_ON(cmd->data_length % sector_size);
4113	sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
4114	task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
4115	
4116	cmd_sg = sgl;
4117	for (i = 0; i < task_count; i++) {
4118		unsigned int task_size, task_sg_nents_padded;
4119		int count;
4120
4121		task = transport_generic_get_task(cmd, data_direction);
4122		if (!task)
4123			return -ENOMEM;
4124
4125		task->task_lba = lba;
4126		task->task_sectors = min(sectors, dev_max_sectors);
4127		task->task_size = task->task_sectors * sector_size;
4128
4129		cdb = dev->transport->get_cdb(task);
4130		BUG_ON(!cdb);
4131
4132		memcpy(cdb, cmd->t_task_cdb,
4133		       scsi_command_size(cmd->t_task_cdb));
4134
4135		/* Update new cdb with updated lba/sectors */
4136		cmd->transport_split_cdb(task->task_lba, task->task_sectors, cdb);
4137		/*
4138		 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
4139		 * in order to calculate the number per task SGL entries
4140		 */
4141		task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
4142		/*
4143		 * Check if the fabric module driver is requesting that all
4144		 * struct se_task->task_sg[] be chained together..  If so,
4145		 * then allocate an extra padding SG entry for linking and
4146		 * marking the end of the chained SGL for every task except
4147		 * the last one for (task_count > 1) operation, or skipping
4148		 * the extra padding for the (task_count == 1) case.
4149		 */
4150		if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
4151			task_sg_nents_padded = (task->task_sg_nents + 1);
4152			task->task_padded_sg = 1;
4153		} else
4154			task_sg_nents_padded = task->task_sg_nents;
4155
4156		task->task_sg = kmalloc(sizeof(struct scatterlist) *
4157					task_sg_nents_padded, GFP_KERNEL);
4158		if (!task->task_sg) {
4159			cmd->se_dev->transport->free_task(task);
4160			return -ENOMEM;
4161		}
4162
4163		sg_init_table(task->task_sg, task_sg_nents_padded);
4164
4165		task_size = task->task_size;
4166
4167		/* Build new sgl, only up to task_size */
4168		for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
4169			if (cmd_sg->length > task_size)
4170				break;
4171
4172			*sg = *cmd_sg;
4173			task_size -= cmd_sg->length;
4174			cmd_sg = sg_next(cmd_sg);
4175		}
4176
4177		lba += task->task_sectors;
4178		sectors -= task->task_sectors;
4179
4180		spin_lock_irqsave(&cmd->t_state_lock, flags);
4181		list_add_tail(&task->t_list, &cmd->t_task_list);
4182		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4183	}
4184	/*
4185	 * Now perform the memory map of task->task_sg[] into backend
4186	 * subsystem memory..
 
4187	 */
4188	list_for_each_entry(task, &cmd->t_task_list, t_list) {
4189		if (atomic_read(&task->task_sent))
4190			continue;
4191		if (!dev->transport->map_data_SG)
4192			continue;
4193
4194		ret = dev->transport->map_data_SG(task);
4195		if (ret < 0)
4196			return 0;
4197	}
4198
4199	return task_count;
4200}
4201
4202static int
4203transport_allocate_control_task(struct se_cmd *cmd)
4204{
4205	struct se_device *dev = cmd->se_dev;
4206	unsigned char *cdb;
4207	struct se_task *task;
4208	unsigned long flags;
4209	int ret = 0;
4210
4211	task = transport_generic_get_task(cmd, cmd->data_direction);
4212	if (!task)
4213		return -ENOMEM;
4214
4215	cdb = dev->transport->get_cdb(task);
4216	BUG_ON(!cdb);
4217	memcpy(cdb, cmd->t_task_cdb,
4218	       scsi_command_size(cmd->t_task_cdb));
4219
4220	task->task_sg = kmalloc(sizeof(struct scatterlist) * cmd->t_data_nents,
4221				GFP_KERNEL);
4222	if (!task->task_sg) {
4223		cmd->se_dev->transport->free_task(task);
4224		return -ENOMEM;
4225	}
4226
4227	memcpy(task->task_sg, cmd->t_data_sg,
4228	       sizeof(struct scatterlist) * cmd->t_data_nents);
4229	task->task_size = cmd->data_length;
4230	task->task_sg_nents = cmd->t_data_nents;
4231
4232	spin_lock_irqsave(&cmd->t_state_lock, flags);
4233	list_add_tail(&task->t_list, &cmd->t_task_list);
4234	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4235
4236	if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) {
4237		if (dev->transport->map_control_SG)
4238			ret = dev->transport->map_control_SG(task);
4239	} else if (cmd->se_cmd_flags & SCF_SCSI_NON_DATA_CDB) {
4240		if (dev->transport->cdb_none)
4241			ret = dev->transport->cdb_none(task);
4242	} else {
4243		pr_err("target: Unknown control cmd type!\n");
4244		BUG();
4245	}
4246
4247	/* Success! Return number of tasks allocated */
4248	if (ret == 0)
4249		return 1;
4250	return ret;
4251}
4252
4253static u32 transport_allocate_tasks(
4254	struct se_cmd *cmd,
4255	unsigned long long lba,
4256	enum dma_data_direction data_direction,
4257	struct scatterlist *sgl,
4258	unsigned int sgl_nents)
4259{
4260	if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
 
 
4261		if (transport_cmd_get_valid_sectors(cmd) < 0)
4262			return -EINVAL;
4263
4264		return transport_allocate_data_tasks(cmd, lba, data_direction,
4265						     sgl, sgl_nents);
4266	} else
4267		return transport_allocate_control_task(cmd);
4268
4269}
4270
 
4271
4272/*	 transport_generic_new_cmd(): Called from transport_processing_thread()
4273 *
4274 *	 Allocate storage transport resources from a set of values predefined
4275 *	 by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
4276 *	 Any non zero return here is treated as an "out of resource' op here.
4277 */
4278	/*
4279	 * Generate struct se_task(s) and/or their payloads for this CDB.
4280	 */
4281int transport_generic_new_cmd(struct se_cmd *cmd)
4282{
4283	int ret = 0;
4284
4285	/*
4286	 * Determine is the TCM fabric module has already allocated physical
4287	 * memory, and is directly calling transport_generic_map_mem_to_cmd()
4288	 * beforehand.
4289	 */
4290	if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
4291	    cmd->data_length) {
4292		ret = transport_generic_get_mem(cmd);
4293		if (ret < 0)
4294			return ret;
4295	}
4296	/*
4297	 * Call transport_new_cmd_obj() to invoke transport_allocate_tasks() for
4298	 * control or data CDB types, and perform the map to backend subsystem
4299	 * code from SGL memory allocated here by transport_generic_get_mem(), or
4300	 * via pre-existing SGL memory setup explictly by fabric module code with
4301	 * transport_generic_map_mem_to_cmd().
4302	 */
4303	ret = transport_new_cmd_obj(cmd);
4304	if (ret < 0)
4305		return ret;
4306	/*
4307	 * For WRITEs, let the fabric know its buffer is ready..
4308	 * This WRITE struct se_cmd (and all of its associated struct se_task's)
4309	 * will be added to the struct se_device execution queue after its WRITE
4310	 * data has arrived. (ie: It gets handled by the transport processing
4311	 * thread a second time)
4312	 */
4313	if (cmd->data_direction == DMA_TO_DEVICE) {
4314		transport_add_tasks_to_state_queue(cmd);
4315		return transport_generic_write_pending(cmd);
4316	}
4317	/*
4318	 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
4319	 * to the execution queue.
4320	 */
4321	transport_execute_tasks(cmd);
4322	return 0;
 
 
 
 
 
4323}
4324EXPORT_SYMBOL(transport_generic_new_cmd);
4325
4326/*	transport_generic_process_write():
4327 *
4328 *
4329 */
4330void transport_generic_process_write(struct se_cmd *cmd)
4331{
4332	transport_execute_tasks(cmd);
4333}
4334EXPORT_SYMBOL(transport_generic_process_write);
4335
4336static int transport_write_pending_qf(struct se_cmd *cmd)
4337{
4338	return cmd->se_tfo->write_pending(cmd);
 
 
 
 
 
 
 
4339}
4340
4341/*	transport_generic_write_pending():
4342 *
4343 *
4344 */
4345static int transport_generic_write_pending(struct se_cmd *cmd)
4346{
4347	unsigned long flags;
4348	int ret;
4349
4350	spin_lock_irqsave(&cmd->t_state_lock, flags);
4351	cmd->t_state = TRANSPORT_WRITE_PENDING;
4352	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4353
4354	if (cmd->transport_qf_callback) {
4355		ret = cmd->transport_qf_callback(cmd);
4356		if (ret == -EAGAIN)
4357			goto queue_full;
4358		else if (ret < 0)
4359			return ret;
4360
4361		cmd->transport_qf_callback = NULL;
4362		return 0;
4363	}
4364
4365	/*
4366	 * Clear the se_cmd for WRITE_PENDING status in order to set
4367	 * cmd->t_transport_active=0 so that transport_generic_handle_data
4368	 * can be called from HW target mode interrupt code.  This is safe
4369	 * to be called with transport_off=1 before the cmd->se_tfo->write_pending
4370	 * because the se_cmd->se_lun pointer is not being cleared.
4371	 */
4372	transport_cmd_check_stop(cmd, 1, 0);
4373
4374	/*
4375	 * Call the fabric write_pending function here to let the
4376	 * frontend know that WRITE buffers are ready.
4377	 */
4378	ret = cmd->se_tfo->write_pending(cmd);
4379	if (ret == -EAGAIN)
4380		goto queue_full;
4381	else if (ret < 0)
4382		return ret;
4383
4384	return PYX_TRANSPORT_WRITE_PENDING;
4385
4386queue_full:
4387	pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
4388	cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
4389	transport_handle_queue_full(cmd, cmd->se_dev,
4390			transport_write_pending_qf);
4391	return ret;
4392}
4393
4394void transport_release_cmd(struct se_cmd *cmd)
4395{
4396	BUG_ON(!cmd->se_tfo);
 
 
4397
4398	transport_free_se_cmd(cmd);
4399	cmd->se_tfo->release_cmd(cmd);
 
 
 
 
 
 
 
 
 
 
4400}
4401EXPORT_SYMBOL(transport_release_cmd);
4402
4403/*	transport_generic_free_cmd():
4404 *
4405 *	Called from processing frontend to release storage engine resources
 
4406 */
4407void transport_generic_free_cmd(
4408	struct se_cmd *cmd,
4409	int wait_for_tasks,
4410	int session_reinstatement)
4411{
4412	if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD))
4413		transport_release_cmd(cmd);
4414	else {
4415		core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
4416
4417		if (cmd->se_lun) {
4418#if 0
4419			pr_debug("cmd: %p ITT: 0x%08x contains"
4420				" cmd->se_lun\n", cmd,
4421				cmd->se_tfo->get_task_tag(cmd));
4422#endif
4423			transport_lun_remove_cmd(cmd);
4424		}
 
 
4425
4426		if (wait_for_tasks && cmd->transport_wait_for_tasks)
4427			cmd->transport_wait_for_tasks(cmd, 0, 0);
 
 
 
 
4428
4429		transport_free_dev_tasks(cmd);
 
 
 
 
4430
4431		transport_generic_remove(cmd, session_reinstatement);
 
 
 
 
4432	}
 
 
 
 
 
 
 
 
 
4433}
4434EXPORT_SYMBOL(transport_generic_free_cmd);
4435
4436static void transport_nop_wait_for_tasks(
4437	struct se_cmd *cmd,
4438	int remove_cmd,
4439	int session_reinstatement)
 
4440{
4441	return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4442}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4443
4444/*	transport_lun_wait_for_tasks():
4445 *
4446 *	Called from ConfigFS context to stop the passed struct se_cmd to allow
4447 *	an struct se_lun to be successfully shutdown.
4448 */
4449static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
4450{
4451	unsigned long flags;
4452	int ret;
 
4453	/*
4454	 * If the frontend has already requested this struct se_cmd to
4455	 * be stopped, we can safely ignore this struct se_cmd.
4456	 */
4457	spin_lock_irqsave(&cmd->t_state_lock, flags);
4458	if (atomic_read(&cmd->t_transport_stop)) {
4459		atomic_set(&cmd->transport_lun_stop, 0);
4460		pr_debug("ConfigFS ITT[0x%08x] - t_transport_stop =="
4461			" TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
 
4462		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4463		transport_cmd_check_stop(cmd, 1, 0);
4464		return -EPERM;
4465	}
4466	atomic_set(&cmd->transport_lun_fe_stop, 1);
4467	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4468
4469	wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4470
4471	ret = transport_stop_tasks_for_cmd(cmd);
 
 
 
 
 
 
 
 
 
 
 
4472
4473	pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4474			" %d\n", cmd, cmd->t_task_list_num, ret);
4475	if (!ret) {
4476		pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
4477				cmd->se_tfo->get_task_tag(cmd));
4478		wait_for_completion(&cmd->transport_lun_stop_comp);
4479		pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
4480				cmd->se_tfo->get_task_tag(cmd));
4481	}
4482	transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj);
4483
4484	return 0;
4485}
4486
4487static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4488{
4489	struct se_cmd *cmd = NULL;
4490	unsigned long lun_flags, cmd_flags;
4491	/*
4492	 * Do exception processing and return CHECK_CONDITION status to the
4493	 * Initiator Port.
4494	 */
4495	spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4496	while (!list_empty(&lun->lun_cmd_list)) {
4497		cmd = list_first_entry(&lun->lun_cmd_list,
4498		       struct se_cmd, se_lun_node);
4499		list_del(&cmd->se_lun_node);
4500
4501		atomic_set(&cmd->transport_lun_active, 0);
4502		/*
4503		 * This will notify iscsi_target_transport.c:
4504		 * transport_cmd_check_stop() that a LUN shutdown is in
4505		 * progress for the iscsi_cmd_t.
4506		 */
4507		spin_lock(&cmd->t_state_lock);
4508		pr_debug("SE_LUN[%d] - Setting cmd->transport"
4509			"_lun_stop for  ITT: 0x%08x\n",
4510			cmd->se_lun->unpacked_lun,
4511			cmd->se_tfo->get_task_tag(cmd));
4512		atomic_set(&cmd->transport_lun_stop, 1);
4513		spin_unlock(&cmd->t_state_lock);
4514
4515		spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4516
4517		if (!cmd->se_lun) {
4518			pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
4519				cmd->se_tfo->get_task_tag(cmd),
4520				cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
4521			BUG();
4522		}
4523		/*
4524		 * If the Storage engine still owns the iscsi_cmd_t, determine
4525		 * and/or stop its context.
4526		 */
4527		pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
4528			"_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4529			cmd->se_tfo->get_task_tag(cmd));
4530
4531		if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
4532			spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4533			continue;
4534		}
4535
4536		pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
4537			"_wait_for_tasks(): SUCCESS\n",
4538			cmd->se_lun->unpacked_lun,
4539			cmd->se_tfo->get_task_tag(cmd));
4540
4541		spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4542		if (!atomic_read(&cmd->transport_dev_active)) {
4543			spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4544			goto check_cond;
4545		}
4546		atomic_set(&cmd->transport_dev_active, 0);
4547		transport_all_task_dev_remove_state(cmd);
4548		spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4549
4550		transport_free_dev_tasks(cmd);
4551		/*
4552		 * The Storage engine stopped this struct se_cmd before it was
4553		 * send to the fabric frontend for delivery back to the
4554		 * Initiator Node.  Return this SCSI CDB back with an
4555		 * CHECK_CONDITION status.
4556		 */
4557check_cond:
4558		transport_send_check_condition_and_sense(cmd,
4559				TCM_NON_EXISTENT_LUN, 0);
4560		/*
4561		 *  If the fabric frontend is waiting for this iscsi_cmd_t to
4562		 * be released, notify the waiting thread now that LU has
4563		 * finished accessing it.
4564		 */
4565		spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4566		if (atomic_read(&cmd->transport_lun_fe_stop)) {
4567			pr_debug("SE_LUN[%d] - Detected FE stop for"
4568				" struct se_cmd: %p ITT: 0x%08x\n",
4569				lun->unpacked_lun,
4570				cmd, cmd->se_tfo->get_task_tag(cmd));
4571
4572			spin_unlock_irqrestore(&cmd->t_state_lock,
4573					cmd_flags);
4574			transport_cmd_check_stop(cmd, 1, 0);
4575			complete(&cmd->transport_lun_fe_stop_comp);
4576			spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4577			continue;
4578		}
4579		pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
4580			lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
4581
4582		spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4583		spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4584	}
4585	spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4586}
4587
4588static int transport_clear_lun_thread(void *p)
4589{
4590	struct se_lun *lun = (struct se_lun *)p;
4591
4592	__transport_clear_lun_from_sessions(lun);
4593	complete(&lun->lun_shutdown_comp);
4594
4595	return 0;
4596}
4597
4598int transport_clear_lun_from_sessions(struct se_lun *lun)
4599{
4600	struct task_struct *kt;
4601
4602	kt = kthread_run(transport_clear_lun_thread, lun,
4603			"tcm_cl_%u", lun->unpacked_lun);
4604	if (IS_ERR(kt)) {
4605		pr_err("Unable to start clear_lun thread\n");
4606		return PTR_ERR(kt);
4607	}
4608	wait_for_completion(&lun->lun_shutdown_comp);
4609
4610	return 0;
4611}
4612
4613/*	transport_generic_wait_for_tasks():
 
 
4614 *
4615 *	Called from frontend or passthrough context to wait for storage engine
4616 *	to pause and/or release frontend generated struct se_cmd.
4617 */
4618static void transport_generic_wait_for_tasks(
4619	struct se_cmd *cmd,
4620	int remove_cmd,
4621	int session_reinstatement)
4622{
4623	unsigned long flags;
4624
4625	if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req))
4626		return;
4627
4628	spin_lock_irqsave(&cmd->t_state_lock, flags);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4629	/*
4630	 * If we are already stopped due to an external event (ie: LUN shutdown)
4631	 * sleep until the connection can have the passed struct se_cmd back.
4632	 * The cmd->transport_lun_stopped_sem will be upped by
4633	 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4634	 * has completed its operation on the struct se_cmd.
4635	 */
4636	if (atomic_read(&cmd->transport_lun_stop)) {
4637
4638		pr_debug("wait_for_tasks: Stopping"
4639			" wait_for_completion(&cmd->t_tasktransport_lun_fe"
4640			"_stop_comp); for ITT: 0x%08x\n",
4641			cmd->se_tfo->get_task_tag(cmd));
4642		/*
4643		 * There is a special case for WRITES where a FE exception +
4644		 * LUN shutdown means ConfigFS context is still sleeping on
4645		 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4646		 * We go ahead and up transport_lun_stop_comp just to be sure
4647		 * here.
4648		 */
4649		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4650		complete(&cmd->transport_lun_stop_comp);
4651		wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4652		spin_lock_irqsave(&cmd->t_state_lock, flags);
4653
4654		transport_all_task_dev_remove_state(cmd);
4655		/*
4656		 * At this point, the frontend who was the originator of this
4657		 * struct se_cmd, now owns the structure and can be released through
4658		 * normal means below.
4659		 */
4660		pr_debug("wait_for_tasks: Stopped"
4661			" wait_for_completion(&cmd->t_tasktransport_lun_fe_"
4662			"stop_comp); for ITT: 0x%08x\n",
4663			cmd->se_tfo->get_task_tag(cmd));
4664
4665		atomic_set(&cmd->transport_lun_stop, 0);
4666	}
4667	if (!atomic_read(&cmd->t_transport_active) ||
4668	     atomic_read(&cmd->t_transport_aborted))
4669		goto remove;
4670
4671	atomic_set(&cmd->t_transport_stop, 1);
 
 
 
 
 
4672
4673	pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
4674		" i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
4675		" = TRUE\n", cmd, cmd->se_tfo->get_task_tag(cmd),
4676		cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
4677		cmd->deferred_t_state);
4678
4679	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4680
4681	wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4682
4683	wait_for_completion(&cmd->t_transport_stop_comp);
4684
4685	spin_lock_irqsave(&cmd->t_state_lock, flags);
4686	atomic_set(&cmd->t_transport_active, 0);
4687	atomic_set(&cmd->t_transport_stop, 0);
4688
4689	pr_debug("wait_for_tasks: Stopped wait_for_compltion("
4690		"&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
4691		cmd->se_tfo->get_task_tag(cmd));
4692remove:
4693	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4694	if (!remove_cmd)
4695		return;
4696
4697	transport_generic_free_cmd(cmd, 0, session_reinstatement);
4698}
 
4699
4700static int transport_get_sense_codes(
4701	struct se_cmd *cmd,
4702	u8 *asc,
4703	u8 *ascq)
4704{
4705	*asc = cmd->scsi_asc;
4706	*ascq = cmd->scsi_ascq;
4707
4708	return 0;
4709}
4710
4711static int transport_set_sense_codes(
4712	struct se_cmd *cmd,
4713	u8 asc,
4714	u8 ascq)
4715{
4716	cmd->scsi_asc = asc;
4717	cmd->scsi_ascq = ascq;
4718
4719	return 0;
4720}
4721
4722int transport_send_check_condition_and_sense(
4723	struct se_cmd *cmd,
4724	u8 reason,
4725	int from_transport)
4726{
4727	unsigned char *buffer = cmd->sense_buffer;
4728	unsigned long flags;
4729	int offset;
4730	u8 asc = 0, ascq = 0;
4731
4732	spin_lock_irqsave(&cmd->t_state_lock, flags);
4733	if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4734		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4735		return 0;
4736	}
4737	cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
4738	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4739
4740	if (!reason && from_transport)
4741		goto after_reason;
4742
4743	if (!from_transport)
4744		cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4745	/*
4746	 * Data Segment and SenseLength of the fabric response PDU.
4747	 *
4748	 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4749	 * from include/scsi/scsi_cmnd.h
4750	 */
4751	offset = cmd->se_tfo->set_fabric_sense_len(cmd,
4752				TRANSPORT_SENSE_BUFFER);
4753	/*
4754	 * Actual SENSE DATA, see SPC-3 7.23.2  SPC_SENSE_KEY_OFFSET uses
4755	 * SENSE KEY values from include/scsi/scsi.h
4756	 */
4757	switch (reason) {
4758	case TCM_NON_EXISTENT_LUN:
4759		/* CURRENT ERROR */
4760		buffer[offset] = 0x70;
 
4761		/* ILLEGAL REQUEST */
4762		buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4763		/* LOGICAL UNIT NOT SUPPORTED */
4764		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4765		break;
4766	case TCM_UNSUPPORTED_SCSI_OPCODE:
4767	case TCM_SECTOR_COUNT_TOO_MANY:
4768		/* CURRENT ERROR */
4769		buffer[offset] = 0x70;
 
4770		/* ILLEGAL REQUEST */
4771		buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4772		/* INVALID COMMAND OPERATION CODE */
4773		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4774		break;
4775	case TCM_UNKNOWN_MODE_PAGE:
4776		/* CURRENT ERROR */
4777		buffer[offset] = 0x70;
 
4778		/* ILLEGAL REQUEST */
4779		buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4780		/* INVALID FIELD IN CDB */
4781		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4782		break;
4783	case TCM_CHECK_CONDITION_ABORT_CMD:
4784		/* CURRENT ERROR */
4785		buffer[offset] = 0x70;
 
4786		/* ABORTED COMMAND */
4787		buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4788		/* BUS DEVICE RESET FUNCTION OCCURRED */
4789		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4790		buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4791		break;
4792	case TCM_INCORRECT_AMOUNT_OF_DATA:
4793		/* CURRENT ERROR */
4794		buffer[offset] = 0x70;
 
4795		/* ABORTED COMMAND */
4796		buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4797		/* WRITE ERROR */
4798		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4799		/* NOT ENOUGH UNSOLICITED DATA */
4800		buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4801		break;
4802	case TCM_INVALID_CDB_FIELD:
4803		/* CURRENT ERROR */
4804		buffer[offset] = 0x70;
4805		/* ABORTED COMMAND */
4806		buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
 
4807		/* INVALID FIELD IN CDB */
4808		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4809		break;
4810	case TCM_INVALID_PARAMETER_LIST:
4811		/* CURRENT ERROR */
4812		buffer[offset] = 0x70;
4813		/* ABORTED COMMAND */
4814		buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
 
4815		/* INVALID FIELD IN PARAMETER LIST */
4816		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4817		break;
4818	case TCM_UNEXPECTED_UNSOLICITED_DATA:
4819		/* CURRENT ERROR */
4820		buffer[offset] = 0x70;
 
4821		/* ABORTED COMMAND */
4822		buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4823		/* WRITE ERROR */
4824		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4825		/* UNEXPECTED_UNSOLICITED_DATA */
4826		buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4827		break;
4828	case TCM_SERVICE_CRC_ERROR:
4829		/* CURRENT ERROR */
4830		buffer[offset] = 0x70;
 
4831		/* ABORTED COMMAND */
4832		buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4833		/* PROTOCOL SERVICE CRC ERROR */
4834		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4835		/* N/A */
4836		buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4837		break;
4838	case TCM_SNACK_REJECTED:
4839		/* CURRENT ERROR */
4840		buffer[offset] = 0x70;
 
4841		/* ABORTED COMMAND */
4842		buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4843		/* READ ERROR */
4844		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4845		/* FAILED RETRANSMISSION REQUEST */
4846		buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4847		break;
4848	case TCM_WRITE_PROTECTED:
4849		/* CURRENT ERROR */
4850		buffer[offset] = 0x70;
 
4851		/* DATA PROTECT */
4852		buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4853		/* WRITE PROTECTED */
4854		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4855		break;
 
 
 
 
 
 
 
 
 
4856	case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4857		/* CURRENT ERROR */
4858		buffer[offset] = 0x70;
 
4859		/* UNIT ATTENTION */
4860		buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4861		core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4862		buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4863		buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4864		break;
4865	case TCM_CHECK_CONDITION_NOT_READY:
4866		/* CURRENT ERROR */
4867		buffer[offset] = 0x70;
 
4868		/* Not Ready */
4869		buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4870		transport_get_sense_codes(cmd, &asc, &ascq);
4871		buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4872		buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4873		break;
4874	case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4875	default:
4876		/* CURRENT ERROR */
4877		buffer[offset] = 0x70;
 
4878		/* ILLEGAL REQUEST */
4879		buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4880		/* LOGICAL UNIT COMMUNICATION FAILURE */
4881		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4882		break;
4883	}
4884	/*
4885	 * This code uses linux/include/scsi/scsi.h SAM status codes!
4886	 */
4887	cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4888	/*
4889	 * Automatically padded, this value is encoded in the fabric's
4890	 * data_length response PDU containing the SCSI defined sense data.
4891	 */
4892	cmd->scsi_sense_length  = TRANSPORT_SENSE_BUFFER + offset;
4893
4894after_reason:
4895	return cmd->se_tfo->queue_status(cmd);
4896}
4897EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4898
4899int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4900{
4901	int ret = 0;
4902
4903	if (atomic_read(&cmd->t_transport_aborted) != 0) {
4904		if (!send_status ||
4905		     (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4906			return 1;
4907#if 0
4908		pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
4909			" status for CDB: 0x%02x ITT: 0x%08x\n",
4910			cmd->t_task_cdb[0],
4911			cmd->se_tfo->get_task_tag(cmd));
4912#endif
4913		cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
4914		cmd->se_tfo->queue_status(cmd);
4915		ret = 1;
4916	}
4917	return ret;
4918}
4919EXPORT_SYMBOL(transport_check_aborted_status);
4920
4921void transport_send_task_abort(struct se_cmd *cmd)
4922{
 
 
 
 
 
 
 
 
 
4923	/*
4924	 * If there are still expected incoming fabric WRITEs, we wait
4925	 * until until they have completed before sending a TASK_ABORTED
4926	 * response.  This response with TASK_ABORTED status will be
4927	 * queued back to fabric module by transport_check_aborted_status().
4928	 */
4929	if (cmd->data_direction == DMA_TO_DEVICE) {
4930		if (cmd->se_tfo->write_pending_status(cmd) != 0) {
4931			atomic_inc(&cmd->t_transport_aborted);
4932			smp_mb__after_atomic_inc();
4933			cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4934			transport_new_cmd_failure(cmd);
4935			return;
4936		}
4937	}
4938	cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4939#if 0
4940	pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
4941		" ITT: 0x%08x\n", cmd->t_task_cdb[0],
4942		cmd->se_tfo->get_task_tag(cmd));
4943#endif
4944	cmd->se_tfo->queue_status(cmd);
4945}
4946
4947/*	transport_generic_do_tmr():
4948 *
4949 *
4950 */
4951int transport_generic_do_tmr(struct se_cmd *cmd)
4952{
4953	struct se_device *dev = cmd->se_dev;
4954	struct se_tmr_req *tmr = cmd->se_tmr_req;
4955	int ret;
4956
4957	switch (tmr->function) {
4958	case TMR_ABORT_TASK:
4959		tmr->response = TMR_FUNCTION_REJECTED;
4960		break;
4961	case TMR_ABORT_TASK_SET:
4962	case TMR_CLEAR_ACA:
4963	case TMR_CLEAR_TASK_SET:
4964		tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4965		break;
4966	case TMR_LUN_RESET:
4967		ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4968		tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4969					 TMR_FUNCTION_REJECTED;
4970		break;
4971	case TMR_TARGET_WARM_RESET:
4972		tmr->response = TMR_FUNCTION_REJECTED;
4973		break;
4974	case TMR_TARGET_COLD_RESET:
4975		tmr->response = TMR_FUNCTION_REJECTED;
4976		break;
4977	default:
4978		pr_err("Uknown TMR function: 0x%02x.\n",
4979				tmr->function);
4980		tmr->response = TMR_FUNCTION_REJECTED;
4981		break;
4982	}
4983
4984	cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
4985	cmd->se_tfo->queue_tm_rsp(cmd);
4986
4987	transport_cmd_check_stop(cmd, 2, 0);
4988	return 0;
4989}
4990
4991/*
4992 *	Called with spin_lock_irq(&dev->execute_task_lock); held
4993 *
4994 */
4995static struct se_task *
4996transport_get_task_from_state_list(struct se_device *dev)
4997{
4998	struct se_task *task;
4999
5000	if (list_empty(&dev->state_task_list))
5001		return NULL;
5002
5003	list_for_each_entry(task, &dev->state_task_list, t_state_list)
5004		break;
5005
5006	list_del(&task->t_state_list);
5007	atomic_set(&task->task_state_active, 0);
5008
5009	return task;
5010}
5011
5012static void transport_processing_shutdown(struct se_device *dev)
5013{
5014	struct se_cmd *cmd;
5015	struct se_task *task;
5016	unsigned long flags;
5017	/*
5018	 * Empty the struct se_device's struct se_task state list.
5019	 */
5020	spin_lock_irqsave(&dev->execute_task_lock, flags);
5021	while ((task = transport_get_task_from_state_list(dev))) {
5022		if (!task->task_se_cmd) {
5023			pr_err("task->task_se_cmd is NULL!\n");
5024			continue;
5025		}
5026		cmd = task->task_se_cmd;
5027
5028		spin_unlock_irqrestore(&dev->execute_task_lock, flags);
5029
5030		spin_lock_irqsave(&cmd->t_state_lock, flags);
5031
5032		pr_debug("PT: cmd: %p task: %p ITT: 0x%08x,"
5033			" i_state: %d, t_state/def_t_state:"
5034			" %d/%d cdb: 0x%02x\n", cmd, task,
5035			cmd->se_tfo->get_task_tag(cmd),
5036			cmd->se_tfo->get_cmd_state(cmd),
5037			cmd->t_state, cmd->deferred_t_state,
5038			cmd->t_task_cdb[0]);
5039		pr_debug("PT: ITT[0x%08x] - t_tasks: %d t_task_cdbs_left:"
5040			" %d t_task_cdbs_sent: %d -- t_transport_active: %d"
5041			" t_transport_stop: %d t_transport_sent: %d\n",
5042			cmd->se_tfo->get_task_tag(cmd),
5043			cmd->t_task_list_num,
5044			atomic_read(&cmd->t_task_cdbs_left),
5045			atomic_read(&cmd->t_task_cdbs_sent),
5046			atomic_read(&cmd->t_transport_active),
5047			atomic_read(&cmd->t_transport_stop),
5048			atomic_read(&cmd->t_transport_sent));
5049
5050		if (atomic_read(&task->task_active)) {
5051			atomic_set(&task->task_stop, 1);
5052			spin_unlock_irqrestore(
5053				&cmd->t_state_lock, flags);
5054
5055			pr_debug("Waiting for task: %p to shutdown for dev:"
5056				" %p\n", task, dev);
5057			wait_for_completion(&task->task_stop_comp);
5058			pr_debug("Completed task: %p shutdown for dev: %p\n",
5059				task, dev);
5060
5061			spin_lock_irqsave(&cmd->t_state_lock, flags);
5062			atomic_dec(&cmd->t_task_cdbs_left);
5063
5064			atomic_set(&task->task_active, 0);
5065			atomic_set(&task->task_stop, 0);
5066		} else {
5067			if (atomic_read(&task->task_execute_queue) != 0)
5068				transport_remove_task_from_execute_queue(task, dev);
5069		}
5070		__transport_stop_task_timer(task, &flags);
5071
5072		if (!atomic_dec_and_test(&cmd->t_task_cdbs_ex_left)) {
5073			spin_unlock_irqrestore(
5074					&cmd->t_state_lock, flags);
5075
5076			pr_debug("Skipping task: %p, dev: %p for"
5077				" t_task_cdbs_ex_left: %d\n", task, dev,
5078				atomic_read(&cmd->t_task_cdbs_ex_left));
5079
5080			spin_lock_irqsave(&dev->execute_task_lock, flags);
5081			continue;
5082		}
5083
5084		if (atomic_read(&cmd->t_transport_active)) {
5085			pr_debug("got t_transport_active = 1 for task: %p, dev:"
5086					" %p\n", task, dev);
5087
5088			if (atomic_read(&cmd->t_fe_count)) {
5089				spin_unlock_irqrestore(
5090					&cmd->t_state_lock, flags);
5091				transport_send_check_condition_and_sense(
5092					cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE,
5093					0);
5094				transport_remove_cmd_from_queue(cmd,
5095					&cmd->se_dev->dev_queue_obj);
5096
5097				transport_lun_remove_cmd(cmd);
5098				transport_cmd_check_stop(cmd, 1, 0);
5099			} else {
5100				spin_unlock_irqrestore(
5101					&cmd->t_state_lock, flags);
5102
5103				transport_remove_cmd_from_queue(cmd,
5104					&cmd->se_dev->dev_queue_obj);
5105
5106				transport_lun_remove_cmd(cmd);
5107
5108				if (transport_cmd_check_stop(cmd, 1, 0))
5109					transport_generic_remove(cmd, 0);
5110			}
5111
5112			spin_lock_irqsave(&dev->execute_task_lock, flags);
5113			continue;
5114		}
5115		pr_debug("Got t_transport_active = 0 for task: %p, dev: %p\n",
5116				task, dev);
5117
5118		if (atomic_read(&cmd->t_fe_count)) {
5119			spin_unlock_irqrestore(
5120				&cmd->t_state_lock, flags);
5121			transport_send_check_condition_and_sense(cmd,
5122				TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
5123			transport_remove_cmd_from_queue(cmd,
5124				&cmd->se_dev->dev_queue_obj);
5125
5126			transport_lun_remove_cmd(cmd);
5127			transport_cmd_check_stop(cmd, 1, 0);
5128		} else {
5129			spin_unlock_irqrestore(
5130				&cmd->t_state_lock, flags);
5131
5132			transport_remove_cmd_from_queue(cmd,
5133				&cmd->se_dev->dev_queue_obj);
5134			transport_lun_remove_cmd(cmd);
5135
5136			if (transport_cmd_check_stop(cmd, 1, 0))
5137				transport_generic_remove(cmd, 0);
5138		}
5139
5140		spin_lock_irqsave(&dev->execute_task_lock, flags);
5141	}
5142	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
5143	/*
5144	 * Empty the struct se_device's struct se_cmd list.
5145	 */
5146	while ((cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj))) {
5147
5148		pr_debug("From Device Queue: cmd: %p t_state: %d\n",
5149				cmd, cmd->t_state);
5150
5151		if (atomic_read(&cmd->t_fe_count)) {
5152			transport_send_check_condition_and_sense(cmd,
5153				TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
5154
5155			transport_lun_remove_cmd(cmd);
5156			transport_cmd_check_stop(cmd, 1, 0);
5157		} else {
5158			transport_lun_remove_cmd(cmd);
5159			if (transport_cmd_check_stop(cmd, 1, 0))
5160				transport_generic_remove(cmd, 0);
5161		}
5162	}
5163}
5164
5165/*	transport_processing_thread():
5166 *
5167 *
5168 */
5169static int transport_processing_thread(void *param)
5170{
5171	int ret;
5172	struct se_cmd *cmd;
5173	struct se_device *dev = (struct se_device *) param;
5174
5175	set_user_nice(current, -20);
5176
5177	while (!kthread_should_stop()) {
5178		ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
5179				atomic_read(&dev->dev_queue_obj.queue_cnt) ||
5180				kthread_should_stop());
5181		if (ret < 0)
5182			goto out;
5183
5184		spin_lock_irq(&dev->dev_status_lock);
5185		if (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) {
5186			spin_unlock_irq(&dev->dev_status_lock);
5187			transport_processing_shutdown(dev);
5188			continue;
5189		}
5190		spin_unlock_irq(&dev->dev_status_lock);
5191
5192get_cmd:
5193		__transport_execute_tasks(dev);
5194
5195		cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
5196		if (!cmd)
5197			continue;
5198
5199		switch (cmd->t_state) {
 
 
 
5200		case TRANSPORT_NEW_CMD_MAP:
5201			if (!cmd->se_tfo->new_cmd_map) {
5202				pr_err("cmd->se_tfo->new_cmd_map is"
5203					" NULL for TRANSPORT_NEW_CMD_MAP\n");
5204				BUG();
5205			}
5206			ret = cmd->se_tfo->new_cmd_map(cmd);
5207			if (ret < 0) {
5208				cmd->transport_error_status = ret;
5209				transport_generic_request_failure(cmd, NULL,
5210						0, (cmd->data_direction !=
5211						    DMA_TO_DEVICE));
5212				break;
5213			}
5214			/* Fall through */
5215		case TRANSPORT_NEW_CMD:
5216			ret = transport_generic_new_cmd(cmd);
5217			if (ret == -EAGAIN)
 
5218				break;
5219			else if (ret < 0) {
5220				cmd->transport_error_status = ret;
5221				transport_generic_request_failure(cmd, NULL,
5222					0, (cmd->data_direction !=
5223					 DMA_TO_DEVICE));
5224			}
5225			break;
5226		case TRANSPORT_PROCESS_WRITE:
5227			transport_generic_process_write(cmd);
5228			break;
5229		case TRANSPORT_COMPLETE_OK:
5230			transport_stop_all_task_timers(cmd);
5231			transport_generic_complete_ok(cmd);
5232			break;
5233		case TRANSPORT_REMOVE:
5234			transport_generic_remove(cmd, 0);
5235			break;
5236		case TRANSPORT_FREE_CMD_INTR:
5237			transport_generic_free_cmd(cmd, 0, 0);
5238			break;
5239		case TRANSPORT_PROCESS_TMR:
5240			transport_generic_do_tmr(cmd);
5241			break;
5242		case TRANSPORT_COMPLETE_FAILURE:
5243			transport_generic_request_failure(cmd, NULL, 1, 1);
5244			break;
5245		case TRANSPORT_COMPLETE_TIMEOUT:
5246			transport_stop_all_task_timers(cmd);
5247			transport_generic_request_timeout(cmd);
5248			break;
5249		case TRANSPORT_COMPLETE_QF_WP:
5250			transport_generic_write_pending(cmd);
 
 
 
5251			break;
5252		default:
5253			pr_err("Unknown t_state: %d deferred_t_state:"
5254				" %d for ITT: 0x%08x i_state: %d on SE LUN:"
5255				" %u\n", cmd->t_state, cmd->deferred_t_state,
5256				cmd->se_tfo->get_task_tag(cmd),
5257				cmd->se_tfo->get_cmd_state(cmd),
5258				cmd->se_lun->unpacked_lun);
5259			BUG();
5260		}
5261
5262		goto get_cmd;
5263	}
5264
5265out:
5266	transport_release_all_cmds(dev);
 
5267	dev->process_thread = NULL;
5268	return 0;
5269}
v3.5.6
   1/*******************************************************************************
   2 * Filename:  target_core_transport.c
   3 *
   4 * This file contains the Generic Target Engine Core.
   5 *
   6 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
   7 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
   8 * Copyright (c) 2007-2010 Rising Tide Systems
   9 * Copyright (c) 2008-2010 Linux-iSCSI.org
  10 *
  11 * Nicholas A. Bellinger <nab@kernel.org>
  12 *
  13 * This program is free software; you can redistribute it and/or modify
  14 * it under the terms of the GNU General Public License as published by
  15 * the Free Software Foundation; either version 2 of the License, or
  16 * (at your option) any later version.
  17 *
  18 * This program is distributed in the hope that it will be useful,
  19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21 * GNU General Public License for more details.
  22 *
  23 * You should have received a copy of the GNU General Public License
  24 * along with this program; if not, write to the Free Software
  25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26 *
  27 ******************************************************************************/
  28
 
  29#include <linux/net.h>
  30#include <linux/delay.h>
  31#include <linux/string.h>
  32#include <linux/timer.h>
  33#include <linux/slab.h>
  34#include <linux/blkdev.h>
  35#include <linux/spinlock.h>
  36#include <linux/kthread.h>
  37#include <linux/in.h>
  38#include <linux/cdrom.h>
  39#include <linux/module.h>
  40#include <linux/ratelimit.h>
  41#include <asm/unaligned.h>
  42#include <net/sock.h>
  43#include <net/tcp.h>
  44#include <scsi/scsi.h>
  45#include <scsi/scsi_cmnd.h>
  46#include <scsi/scsi_tcq.h>
  47
  48#include <target/target_core_base.h>
  49#include <target/target_core_backend.h>
  50#include <target/target_core_fabric.h>
 
 
 
  51#include <target/target_core_configfs.h>
  52
  53#include "target_core_internal.h"
  54#include "target_core_alua.h"
 
  55#include "target_core_pr.h"
 
  56#include "target_core_ua.h"
  57
  58static int sub_api_initialized;
  59
  60static struct workqueue_struct *target_completion_wq;
  61static struct kmem_cache *se_sess_cache;
 
  62struct kmem_cache *se_ua_cache;
  63struct kmem_cache *t10_pr_reg_cache;
  64struct kmem_cache *t10_alua_lu_gp_cache;
  65struct kmem_cache *t10_alua_lu_gp_mem_cache;
  66struct kmem_cache *t10_alua_tg_pt_gp_cache;
  67struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
  68
 
 
 
  69static int transport_generic_write_pending(struct se_cmd *);
  70static int transport_processing_thread(void *param);
  71static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *);
  72static void transport_complete_task_attr(struct se_cmd *cmd);
 
  73static void transport_handle_queue_full(struct se_cmd *cmd,
  74		struct se_device *dev);
 
 
 
 
 
 
  75static int transport_generic_get_mem(struct se_cmd *cmd);
  76static void transport_put_cmd(struct se_cmd *cmd);
  77static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
 
 
 
  78static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
  79static void target_complete_ok_work(struct work_struct *work);
  80
  81int init_se_kmem_caches(void)
  82{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  83	se_sess_cache = kmem_cache_create("se_sess_cache",
  84			sizeof(struct se_session), __alignof__(struct se_session),
  85			0, NULL);
  86	if (!se_sess_cache) {
  87		pr_err("kmem_cache_create() for struct se_session"
  88				" failed\n");
  89		goto out;
  90	}
  91	se_ua_cache = kmem_cache_create("se_ua_cache",
  92			sizeof(struct se_ua), __alignof__(struct se_ua),
  93			0, NULL);
  94	if (!se_ua_cache) {
  95		pr_err("kmem_cache_create() for struct se_ua failed\n");
  96		goto out_free_sess_cache;
  97	}
  98	t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
  99			sizeof(struct t10_pr_registration),
 100			__alignof__(struct t10_pr_registration), 0, NULL);
 101	if (!t10_pr_reg_cache) {
 102		pr_err("kmem_cache_create() for struct t10_pr_registration"
 103				" failed\n");
 104		goto out_free_ua_cache;
 105	}
 106	t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
 107			sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
 108			0, NULL);
 109	if (!t10_alua_lu_gp_cache) {
 110		pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
 111				" failed\n");
 112		goto out_free_pr_reg_cache;
 113	}
 114	t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
 115			sizeof(struct t10_alua_lu_gp_member),
 116			__alignof__(struct t10_alua_lu_gp_member), 0, NULL);
 117	if (!t10_alua_lu_gp_mem_cache) {
 118		pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
 119				"cache failed\n");
 120		goto out_free_lu_gp_cache;
 121	}
 122	t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
 123			sizeof(struct t10_alua_tg_pt_gp),
 124			__alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
 125	if (!t10_alua_tg_pt_gp_cache) {
 126		pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
 127				"cache failed\n");
 128		goto out_free_lu_gp_mem_cache;
 129	}
 130	t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
 131			"t10_alua_tg_pt_gp_mem_cache",
 132			sizeof(struct t10_alua_tg_pt_gp_member),
 133			__alignof__(struct t10_alua_tg_pt_gp_member),
 134			0, NULL);
 135	if (!t10_alua_tg_pt_gp_mem_cache) {
 136		pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
 137				"mem_t failed\n");
 138		goto out_free_tg_pt_gp_cache;
 139	}
 140
 141	target_completion_wq = alloc_workqueue("target_completion",
 142					       WQ_MEM_RECLAIM, 0);
 143	if (!target_completion_wq)
 144		goto out_free_tg_pt_gp_mem_cache;
 145
 146	return 0;
 147
 148out_free_tg_pt_gp_mem_cache:
 149	kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
 150out_free_tg_pt_gp_cache:
 151	kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
 152out_free_lu_gp_mem_cache:
 153	kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
 154out_free_lu_gp_cache:
 155	kmem_cache_destroy(t10_alua_lu_gp_cache);
 156out_free_pr_reg_cache:
 157	kmem_cache_destroy(t10_pr_reg_cache);
 158out_free_ua_cache:
 159	kmem_cache_destroy(se_ua_cache);
 160out_free_sess_cache:
 161	kmem_cache_destroy(se_sess_cache);
 162out:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 163	return -ENOMEM;
 164}
 165
 166void release_se_kmem_caches(void)
 167{
 168	destroy_workqueue(target_completion_wq);
 
 169	kmem_cache_destroy(se_sess_cache);
 170	kmem_cache_destroy(se_ua_cache);
 171	kmem_cache_destroy(t10_pr_reg_cache);
 172	kmem_cache_destroy(t10_alua_lu_gp_cache);
 173	kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
 174	kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
 175	kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
 176}
 177
 178/* This code ensures unique mib indexes are handed out. */
 179static DEFINE_SPINLOCK(scsi_mib_index_lock);
 180static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
 181
 182/*
 183 * Allocate a new row index for the entry type specified
 184 */
 185u32 scsi_get_new_index(scsi_index_t type)
 186{
 187	u32 new_index;
 188
 189	BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
 190
 191	spin_lock(&scsi_mib_index_lock);
 192	new_index = ++scsi_mib_index[type];
 193	spin_unlock(&scsi_mib_index_lock);
 194
 195	return new_index;
 196}
 197
 198static void transport_init_queue_obj(struct se_queue_obj *qobj)
 199{
 200	atomic_set(&qobj->queue_cnt, 0);
 201	INIT_LIST_HEAD(&qobj->qobj_list);
 202	init_waitqueue_head(&qobj->thread_wq);
 203	spin_lock_init(&qobj->cmd_queue_lock);
 204}
 
 205
 206void transport_subsystem_check_init(void)
 207{
 208	int ret;
 209
 210	if (sub_api_initialized)
 211		return;
 212
 213	ret = request_module("target_core_iblock");
 214	if (ret != 0)
 215		pr_err("Unable to load target_core_iblock\n");
 216
 217	ret = request_module("target_core_file");
 218	if (ret != 0)
 219		pr_err("Unable to load target_core_file\n");
 220
 221	ret = request_module("target_core_pscsi");
 222	if (ret != 0)
 223		pr_err("Unable to load target_core_pscsi\n");
 224
 225	ret = request_module("target_core_stgt");
 226	if (ret != 0)
 227		pr_err("Unable to load target_core_stgt\n");
 228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 229	sub_api_initialized = 1;
 230	return;
 231}
 232
 233struct se_session *transport_init_session(void)
 234{
 235	struct se_session *se_sess;
 236
 237	se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
 238	if (!se_sess) {
 239		pr_err("Unable to allocate struct se_session from"
 240				" se_sess_cache\n");
 241		return ERR_PTR(-ENOMEM);
 242	}
 243	INIT_LIST_HEAD(&se_sess->sess_list);
 244	INIT_LIST_HEAD(&se_sess->sess_acl_list);
 245	INIT_LIST_HEAD(&se_sess->sess_cmd_list);
 246	INIT_LIST_HEAD(&se_sess->sess_wait_list);
 247	spin_lock_init(&se_sess->sess_cmd_lock);
 248	kref_init(&se_sess->sess_kref);
 249
 250	return se_sess;
 251}
 252EXPORT_SYMBOL(transport_init_session);
 253
 254/*
 255 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
 256 */
 257void __transport_register_session(
 258	struct se_portal_group *se_tpg,
 259	struct se_node_acl *se_nacl,
 260	struct se_session *se_sess,
 261	void *fabric_sess_ptr)
 262{
 263	unsigned char buf[PR_REG_ISID_LEN];
 264
 265	se_sess->se_tpg = se_tpg;
 266	se_sess->fabric_sess_ptr = fabric_sess_ptr;
 267	/*
 268	 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
 269	 *
 270	 * Only set for struct se_session's that will actually be moving I/O.
 271	 * eg: *NOT* discovery sessions.
 272	 */
 273	if (se_nacl) {
 274		/*
 275		 * If the fabric module supports an ISID based TransportID,
 276		 * save this value in binary from the fabric I_T Nexus now.
 277		 */
 278		if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
 279			memset(&buf[0], 0, PR_REG_ISID_LEN);
 280			se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
 281					&buf[0], PR_REG_ISID_LEN);
 282			se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
 283		}
 284		kref_get(&se_nacl->acl_kref);
 285
 286		spin_lock_irq(&se_nacl->nacl_sess_lock);
 287		/*
 288		 * The se_nacl->nacl_sess pointer will be set to the
 289		 * last active I_T Nexus for each struct se_node_acl.
 290		 */
 291		se_nacl->nacl_sess = se_sess;
 292
 293		list_add_tail(&se_sess->sess_acl_list,
 294			      &se_nacl->acl_sess_list);
 295		spin_unlock_irq(&se_nacl->nacl_sess_lock);
 296	}
 297	list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
 298
 299	pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
 300		se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
 301}
 302EXPORT_SYMBOL(__transport_register_session);
 303
 304void transport_register_session(
 305	struct se_portal_group *se_tpg,
 306	struct se_node_acl *se_nacl,
 307	struct se_session *se_sess,
 308	void *fabric_sess_ptr)
 309{
 310	unsigned long flags;
 311
 312	spin_lock_irqsave(&se_tpg->session_lock, flags);
 313	__transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
 314	spin_unlock_irqrestore(&se_tpg->session_lock, flags);
 315}
 316EXPORT_SYMBOL(transport_register_session);
 317
 318void target_release_session(struct kref *kref)
 319{
 320	struct se_session *se_sess = container_of(kref,
 321			struct se_session, sess_kref);
 322	struct se_portal_group *se_tpg = se_sess->se_tpg;
 323
 324	se_tpg->se_tpg_tfo->close_session(se_sess);
 325}
 326
 327void target_get_session(struct se_session *se_sess)
 328{
 329	kref_get(&se_sess->sess_kref);
 330}
 331EXPORT_SYMBOL(target_get_session);
 332
 333void target_put_session(struct se_session *se_sess)
 334{
 335	struct se_portal_group *tpg = se_sess->se_tpg;
 336
 337	if (tpg->se_tpg_tfo->put_session != NULL) {
 338		tpg->se_tpg_tfo->put_session(se_sess);
 339		return;
 340	}
 341	kref_put(&se_sess->sess_kref, target_release_session);
 342}
 343EXPORT_SYMBOL(target_put_session);
 344
 345static void target_complete_nacl(struct kref *kref)
 346{
 347	struct se_node_acl *nacl = container_of(kref,
 348				struct se_node_acl, acl_kref);
 349
 350	complete(&nacl->acl_free_comp);
 351}
 352
 353void target_put_nacl(struct se_node_acl *nacl)
 354{
 355	kref_put(&nacl->acl_kref, target_complete_nacl);
 356}
 357
 358void transport_deregister_session_configfs(struct se_session *se_sess)
 359{
 360	struct se_node_acl *se_nacl;
 361	unsigned long flags;
 362	/*
 363	 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
 364	 */
 365	se_nacl = se_sess->se_node_acl;
 366	if (se_nacl) {
 367		spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
 368		if (se_nacl->acl_stop == 0)
 369			list_del(&se_sess->sess_acl_list);
 370		/*
 371		 * If the session list is empty, then clear the pointer.
 372		 * Otherwise, set the struct se_session pointer from the tail
 373		 * element of the per struct se_node_acl active session list.
 374		 */
 375		if (list_empty(&se_nacl->acl_sess_list))
 376			se_nacl->nacl_sess = NULL;
 377		else {
 378			se_nacl->nacl_sess = container_of(
 379					se_nacl->acl_sess_list.prev,
 380					struct se_session, sess_acl_list);
 381		}
 382		spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
 383	}
 384}
 385EXPORT_SYMBOL(transport_deregister_session_configfs);
 386
 387void transport_free_session(struct se_session *se_sess)
 388{
 389	kmem_cache_free(se_sess_cache, se_sess);
 390}
 391EXPORT_SYMBOL(transport_free_session);
 392
 393void transport_deregister_session(struct se_session *se_sess)
 394{
 395	struct se_portal_group *se_tpg = se_sess->se_tpg;
 396	struct target_core_fabric_ops *se_tfo;
 397	struct se_node_acl *se_nacl;
 398	unsigned long flags;
 399	bool comp_nacl = true;
 400
 401	if (!se_tpg) {
 402		transport_free_session(se_sess);
 403		return;
 404	}
 405	se_tfo = se_tpg->se_tpg_tfo;
 406
 407	spin_lock_irqsave(&se_tpg->session_lock, flags);
 408	list_del(&se_sess->sess_list);
 409	se_sess->se_tpg = NULL;
 410	se_sess->fabric_sess_ptr = NULL;
 411	spin_unlock_irqrestore(&se_tpg->session_lock, flags);
 412
 413	/*
 414	 * Determine if we need to do extra work for this initiator node's
 415	 * struct se_node_acl if it had been previously dynamically generated.
 416	 */
 417	se_nacl = se_sess->se_node_acl;
 418
 419	spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
 420	if (se_nacl && se_nacl->dynamic_node_acl) {
 421		if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
 422			list_del(&se_nacl->acl_list);
 423			se_tpg->num_node_acls--;
 424			spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
 425			core_tpg_wait_for_nacl_pr_ref(se_nacl);
 426			core_free_device_list_for_node(se_nacl, se_tpg);
 427			se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl);
 428
 429			comp_nacl = false;
 430			spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
 
 
 431		}
 
 432	}
 433	spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
 
 434
 435	pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
 436		se_tpg->se_tpg_tfo->get_fabric_name());
 437	/*
 438	 * If last kref is dropping now for an explict NodeACL, awake sleeping
 439	 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
 440	 * removal context.
 441	 */
 442	if (se_nacl && comp_nacl == true)
 443		target_put_nacl(se_nacl);
 444
 445	transport_free_session(se_sess);
 446}
 447EXPORT_SYMBOL(transport_deregister_session);
 448
 449/*
 450 * Called with cmd->t_state_lock held.
 451 */
 452static void target_remove_from_state_list(struct se_cmd *cmd)
 453{
 454	struct se_device *dev = cmd->se_dev;
 
 455	unsigned long flags;
 456
 457	if (!dev)
 458		return;
 
 
 
 
 
 
 
 
 459
 460	if (cmd->transport_state & CMD_T_BUSY)
 461		return;
 
 
 
 462
 463	spin_lock_irqsave(&dev->execute_task_lock, flags);
 464	if (cmd->state_active) {
 465		list_del(&cmd->state_list);
 466		cmd->state_active = false;
 467	}
 468	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
 469}
 470
 471/*	transport_cmd_check_stop():
 472 *
 473 *	'transport_off = 1' determines if CMD_T_ACTIVE should be cleared.
 474 *	'transport_off = 2' determines if task_dev_state should be removed.
 475 *
 476 *	A non-zero u8 t_state sets cmd->t_state.
 477 *	Returns 1 when command is stopped, else 0.
 478 */
 479static int transport_cmd_check_stop(
 480	struct se_cmd *cmd,
 481	int transport_off,
 482	u8 t_state)
 483{
 484	unsigned long flags;
 485
 486	spin_lock_irqsave(&cmd->t_state_lock, flags);
 487	/*
 488	 * Determine if IOCTL context caller in requesting the stopping of this
 489	 * command for LUN shutdown purposes.
 490	 */
 491	if (cmd->transport_state & CMD_T_LUN_STOP) {
 492		pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
 493			__func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
 
 494
 495		cmd->transport_state &= ~CMD_T_ACTIVE;
 
 
 496		if (transport_off == 2)
 497			target_remove_from_state_list(cmd);
 498		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 499
 500		complete(&cmd->transport_lun_stop_comp);
 501		return 1;
 502	}
 503	/*
 504	 * Determine if frontend context caller is requesting the stopping of
 505	 * this command for frontend exceptions.
 506	 */
 507	if (cmd->transport_state & CMD_T_STOP) {
 508		pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
 509			__func__, __LINE__,
 510			cmd->se_tfo->get_task_tag(cmd));
 511
 
 
 512		if (transport_off == 2)
 513			target_remove_from_state_list(cmd);
 514
 515		/*
 516		 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
 517		 * to FE.
 518		 */
 519		if (transport_off == 2)
 520			cmd->se_lun = NULL;
 521		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 522
 523		complete(&cmd->t_transport_stop_comp);
 524		return 1;
 525	}
 526	if (transport_off) {
 527		cmd->transport_state &= ~CMD_T_ACTIVE;
 528		if (transport_off == 2) {
 529			target_remove_from_state_list(cmd);
 530			/*
 531			 * Clear struct se_cmd->se_lun before the transport_off == 2
 532			 * handoff to fabric module.
 533			 */
 534			cmd->se_lun = NULL;
 535			/*
 536			 * Some fabric modules like tcm_loop can release
 537			 * their internally allocated I/O reference now and
 538			 * struct se_cmd now.
 539			 *
 540			 * Fabric modules are expected to return '1' here if the
 541			 * se_cmd being passed is released at this point,
 542			 * or zero if not being released.
 543			 */
 544			if (cmd->se_tfo->check_stop_free != NULL) {
 545				spin_unlock_irqrestore(
 546					&cmd->t_state_lock, flags);
 547
 548				return cmd->se_tfo->check_stop_free(cmd);
 
 549			}
 550		}
 551		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 552
 553		return 0;
 554	} else if (t_state)
 555		cmd->t_state = t_state;
 556	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 557
 558	return 0;
 559}
 560
 561static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
 562{
 563	return transport_cmd_check_stop(cmd, 2, 0);
 564}
 565
 566static void transport_lun_remove_cmd(struct se_cmd *cmd)
 567{
 568	struct se_lun *lun = cmd->se_lun;
 569	unsigned long flags;
 570
 571	if (!lun)
 572		return;
 573
 574	spin_lock_irqsave(&cmd->t_state_lock, flags);
 575	if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
 576		cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
 577		target_remove_from_state_list(cmd);
 578	}
 
 
 579	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 580
 
 
 581	spin_lock_irqsave(&lun->lun_cmd_lock, flags);
 582	if (!list_empty(&cmd->se_lun_node))
 583		list_del_init(&cmd->se_lun_node);
 
 
 
 
 
 
 584	spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
 585}
 586
 587void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
 588{
 589	if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
 590		transport_lun_remove_cmd(cmd);
 
 
 
 
 
 
 
 
 
 
 591
 592	if (transport_cmd_check_stop_to_fabric(cmd))
 593		return;
 594	if (remove) {
 595		transport_remove_cmd_from_queue(cmd);
 596		transport_put_cmd(cmd);
 597	}
 598}
 599
 600static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
 601		bool at_head)
 
 602{
 603	struct se_device *dev = cmd->se_dev;
 604	struct se_queue_obj *qobj = &dev->dev_queue_obj;
 605	unsigned long flags;
 606
 
 
 607	if (t_state) {
 608		spin_lock_irqsave(&cmd->t_state_lock, flags);
 609		cmd->t_state = t_state;
 610		cmd->transport_state |= CMD_T_ACTIVE;
 611		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 612	}
 613
 614	spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
 615
 616	/* If the cmd is already on the list, remove it before we add it */
 617	if (!list_empty(&cmd->se_queue_node))
 618		list_del(&cmd->se_queue_node);
 619	else
 620		atomic_inc(&qobj->queue_cnt);
 621
 622	if (at_head)
 623		list_add(&cmd->se_queue_node, &qobj->qobj_list);
 624	else
 625		list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
 626	cmd->transport_state |= CMD_T_QUEUED;
 627	spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
 628
 
 629	wake_up_interruptible(&qobj->thread_wq);
 630}
 631
 632static struct se_cmd *
 633transport_get_cmd_from_queue(struct se_queue_obj *qobj)
 634{
 635	struct se_cmd *cmd;
 636	unsigned long flags;
 637
 638	spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
 639	if (list_empty(&qobj->qobj_list)) {
 640		spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
 641		return NULL;
 642	}
 643	cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
 644
 645	cmd->transport_state &= ~CMD_T_QUEUED;
 646	list_del_init(&cmd->se_queue_node);
 
 647	atomic_dec(&qobj->queue_cnt);
 648	spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
 649
 650	return cmd;
 651}
 652
 653static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
 
 654{
 655	struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
 656	unsigned long flags;
 657
 658	spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
 659	if (!(cmd->transport_state & CMD_T_QUEUED)) {
 660		spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
 661		return;
 662	}
 663	cmd->transport_state &= ~CMD_T_QUEUED;
 664	atomic_dec(&qobj->queue_cnt);
 665	list_del_init(&cmd->se_queue_node);
 
 
 
 
 
 666	spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
 
 
 
 
 
 
 667}
 668
 669static void target_complete_failure_work(struct work_struct *work)
 
 
 
 
 670{
 671	struct se_cmd *cmd = container_of(work, struct se_cmd, work);
 
 672
 673	transport_generic_request_failure(cmd);
 
 
 
 
 
 
 
 
 
 
 674}
 
 675
 676void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
 
 
 
 
 
 677{
 678	struct se_device *dev = cmd->se_dev;
 679	int success = scsi_status == GOOD;
 
 680	unsigned long flags;
 681
 682	cmd->scsi_status = scsi_status;
 683
 
 
 
 684
 685	spin_lock_irqsave(&cmd->t_state_lock, flags);
 686	cmd->transport_state &= ~CMD_T_BUSY;
 687
 
 
 
 
 
 688	if (dev && dev->transport->transport_complete) {
 689		if (dev->transport->transport_complete(cmd,
 690				cmd->t_data_sg) != 0) {
 691			cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
 
 692			success = 1;
 693		}
 694	}
 695
 696	/*
 697	 * See if we are waiting to complete for an exception condition.
 
 698	 */
 699	if (cmd->transport_state & CMD_T_REQUEST_STOP) {
 
 
 
 
 
 
 
 
 700		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 701		complete(&cmd->task_stop_comp);
 
 702		return;
 703	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 704
 705	if (!success)
 706		cmd->transport_state |= CMD_T_FAILED;
 
 
 707
 708	/*
 709	 * Check for case where an explict ABORT_TASK has been received
 710	 * and transport_wait_for_tasks() will be waiting for completion..
 
 711	 */
 712	if (cmd->transport_state & CMD_T_ABORTED &&
 713	    cmd->transport_state & CMD_T_STOP) {
 
 
 714		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 715		complete(&cmd->t_transport_stop_comp);
 716		return;
 717	} else if (cmd->transport_state & CMD_T_FAILED) {
 718		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
 719		INIT_WORK(&cmd->work, target_complete_failure_work);
 
 
 
 
 
 
 
 720	} else {
 721		INIT_WORK(&cmd->work, target_complete_ok_work);
 
 722	}
 723
 724	cmd->t_state = TRANSPORT_COMPLETE;
 725	cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
 726	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 727
 728	queue_work(target_completion_wq, &cmd->work);
 729}
 730EXPORT_SYMBOL(target_complete_cmd);
 
 
 
 
 
 731
 732static void target_add_to_state_list(struct se_cmd *cmd)
 
 
 
 
 
 733{
 734	struct se_device *dev = cmd->se_dev;
 735	unsigned long flags;
 736
 737	spin_lock_irqsave(&dev->execute_task_lock, flags);
 738	if (!cmd->state_active) {
 739		list_add_tail(&cmd->state_list, &dev->state_list);
 740		cmd->state_active = true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 741	}
 742	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
 
 
 
 
 
 
 743}
 744
 745static void __target_add_to_execute_list(struct se_cmd *cmd)
 
 
 
 
 
 
 
 746{
 747	struct se_device *dev = cmd->se_dev;
 748	bool head_of_queue = false;
 
 
 749
 750	if (!list_empty(&cmd->execute_list))
 751		return;
 
 
 
 
 
 
 
 
 
 
 
 752
 753	if (dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED &&
 754	    cmd->sam_task_attr == MSG_HEAD_TAG)
 755		head_of_queue = true;
 756
 757	if (head_of_queue)
 758		list_add(&cmd->execute_list, &dev->execute_list);
 759	else
 760		list_add_tail(&cmd->execute_list, &dev->execute_list);
 
 
 
 
 
 
 761
 762	atomic_inc(&dev->execute_tasks);
 
 
 763
 764	if (cmd->state_active)
 765		return;
 766
 767	if (head_of_queue)
 768		list_add(&cmd->state_list, &dev->state_list);
 769	else
 770		list_add_tail(&cmd->state_list, &dev->state_list);
 
 
 
 771
 772	cmd->state_active = true;
 
 
 773}
 774
 775static void target_add_to_execute_list(struct se_cmd *cmd)
 776{
 
 
 777	unsigned long flags;
 778	struct se_device *dev = cmd->se_dev;
 779
 780	spin_lock_irqsave(&dev->execute_task_lock, flags);
 781	__target_add_to_execute_list(cmd);
 
 
 
 
 
 
 
 
 
 
 782	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
 783}
 784
 785void __target_remove_from_execute_list(struct se_cmd *cmd)
 
 
 
 
 
 
 786{
 787	list_del_init(&cmd->execute_list);
 788	atomic_dec(&cmd->se_dev->execute_tasks);
 789}
 790
 791static void target_remove_from_execute_list(struct se_cmd *cmd)
 792{
 793	struct se_device *dev = cmd->se_dev;
 794	unsigned long flags;
 795
 796	if (WARN_ON(list_empty(&cmd->execute_list)))
 
 797		return;
 
 798
 799	spin_lock_irqsave(&dev->execute_task_lock, flags);
 800	__target_remove_from_execute_list(cmd);
 
 
 801	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
 802}
 803
 804/*
 805 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
 806 */
 807
 808static void target_qf_do_work(struct work_struct *work)
 809{
 810	struct se_device *dev = container_of(work, struct se_device,
 811					qf_work_queue);
 812	LIST_HEAD(qf_cmd_list);
 813	struct se_cmd *cmd, *cmd_tmp;
 814
 815	spin_lock_irq(&dev->qf_cmd_lock);
 816	list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
 817	spin_unlock_irq(&dev->qf_cmd_lock);
 818
 819	list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
 820		list_del(&cmd->se_qf_node);
 821		atomic_dec(&dev->dev_qf_count);
 822		smp_mb__after_atomic_dec();
 823
 824		pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
 825			" context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
 826			(cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
 827			(cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
 828			: "UNKNOWN");
 829
 830		transport_add_cmd_to_queue(cmd, cmd->t_state, true);
 
 
 
 831	}
 832}
 833
 834unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
 835{
 836	switch (cmd->data_direction) {
 837	case DMA_NONE:
 838		return "NONE";
 839	case DMA_FROM_DEVICE:
 840		return "READ";
 841	case DMA_TO_DEVICE:
 842		return "WRITE";
 843	case DMA_BIDIRECTIONAL:
 844		return "BIDI";
 845	default:
 846		break;
 847	}
 848
 849	return "UNKNOWN";
 850}
 851
 852void transport_dump_dev_state(
 853	struct se_device *dev,
 854	char *b,
 855	int *bl)
 856{
 857	*bl += sprintf(b + *bl, "Status: ");
 858	switch (dev->dev_status) {
 859	case TRANSPORT_DEVICE_ACTIVATED:
 860		*bl += sprintf(b + *bl, "ACTIVATED");
 861		break;
 862	case TRANSPORT_DEVICE_DEACTIVATED:
 863		*bl += sprintf(b + *bl, "DEACTIVATED");
 864		break;
 865	case TRANSPORT_DEVICE_SHUTDOWN:
 866		*bl += sprintf(b + *bl, "SHUTDOWN");
 867		break;
 868	case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
 869	case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
 870		*bl += sprintf(b + *bl, "OFFLINE");
 871		break;
 872	default:
 873		*bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
 874		break;
 875	}
 876
 877	*bl += sprintf(b + *bl, "  Execute/Max Queue Depth: %d/%d",
 878		atomic_read(&dev->execute_tasks), dev->queue_depth);
 879	*bl += sprintf(b + *bl, "  SectorSize: %u  HwMaxSectors: %u\n",
 880		dev->se_sub_dev->se_dev_attrib.block_size,
 881		dev->se_sub_dev->se_dev_attrib.hw_max_sectors);
 882	*bl += sprintf(b + *bl, "        ");
 883}
 884
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 885void transport_dump_vpd_proto_id(
 886	struct t10_vpd *vpd,
 887	unsigned char *p_buf,
 888	int p_buf_len)
 889{
 890	unsigned char buf[VPD_TMP_BUF_SIZE];
 891	int len;
 892
 893	memset(buf, 0, VPD_TMP_BUF_SIZE);
 894	len = sprintf(buf, "T10 VPD Protocol Identifier: ");
 895
 896	switch (vpd->protocol_identifier) {
 897	case 0x00:
 898		sprintf(buf+len, "Fibre Channel\n");
 899		break;
 900	case 0x10:
 901		sprintf(buf+len, "Parallel SCSI\n");
 902		break;
 903	case 0x20:
 904		sprintf(buf+len, "SSA\n");
 905		break;
 906	case 0x30:
 907		sprintf(buf+len, "IEEE 1394\n");
 908		break;
 909	case 0x40:
 910		sprintf(buf+len, "SCSI Remote Direct Memory Access"
 911				" Protocol\n");
 912		break;
 913	case 0x50:
 914		sprintf(buf+len, "Internet SCSI (iSCSI)\n");
 915		break;
 916	case 0x60:
 917		sprintf(buf+len, "SAS Serial SCSI Protocol\n");
 918		break;
 919	case 0x70:
 920		sprintf(buf+len, "Automation/Drive Interface Transport"
 921				" Protocol\n");
 922		break;
 923	case 0x80:
 924		sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
 925		break;
 926	default:
 927		sprintf(buf+len, "Unknown 0x%02x\n",
 928				vpd->protocol_identifier);
 929		break;
 930	}
 931
 932	if (p_buf)
 933		strncpy(p_buf, buf, p_buf_len);
 934	else
 935		pr_debug("%s", buf);
 936}
 937
 938void
 939transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
 940{
 941	/*
 942	 * Check if the Protocol Identifier Valid (PIV) bit is set..
 943	 *
 944	 * from spc3r23.pdf section 7.5.1
 945	 */
 946	 if (page_83[1] & 0x80) {
 947		vpd->protocol_identifier = (page_83[0] & 0xf0);
 948		vpd->protocol_identifier_set = 1;
 949		transport_dump_vpd_proto_id(vpd, NULL, 0);
 950	}
 951}
 952EXPORT_SYMBOL(transport_set_vpd_proto_id);
 953
 954int transport_dump_vpd_assoc(
 955	struct t10_vpd *vpd,
 956	unsigned char *p_buf,
 957	int p_buf_len)
 958{
 959	unsigned char buf[VPD_TMP_BUF_SIZE];
 960	int ret = 0;
 961	int len;
 962
 963	memset(buf, 0, VPD_TMP_BUF_SIZE);
 964	len = sprintf(buf, "T10 VPD Identifier Association: ");
 965
 966	switch (vpd->association) {
 967	case 0x00:
 968		sprintf(buf+len, "addressed logical unit\n");
 969		break;
 970	case 0x10:
 971		sprintf(buf+len, "target port\n");
 972		break;
 973	case 0x20:
 974		sprintf(buf+len, "SCSI target device\n");
 975		break;
 976	default:
 977		sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
 978		ret = -EINVAL;
 979		break;
 980	}
 981
 982	if (p_buf)
 983		strncpy(p_buf, buf, p_buf_len);
 984	else
 985		pr_debug("%s", buf);
 986
 987	return ret;
 988}
 989
 990int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
 991{
 992	/*
 993	 * The VPD identification association..
 994	 *
 995	 * from spc3r23.pdf Section 7.6.3.1 Table 297
 996	 */
 997	vpd->association = (page_83[1] & 0x30);
 998	return transport_dump_vpd_assoc(vpd, NULL, 0);
 999}
1000EXPORT_SYMBOL(transport_set_vpd_assoc);
1001
1002int transport_dump_vpd_ident_type(
1003	struct t10_vpd *vpd,
1004	unsigned char *p_buf,
1005	int p_buf_len)
1006{
1007	unsigned char buf[VPD_TMP_BUF_SIZE];
1008	int ret = 0;
1009	int len;
1010
1011	memset(buf, 0, VPD_TMP_BUF_SIZE);
1012	len = sprintf(buf, "T10 VPD Identifier Type: ");
1013
1014	switch (vpd->device_identifier_type) {
1015	case 0x00:
1016		sprintf(buf+len, "Vendor specific\n");
1017		break;
1018	case 0x01:
1019		sprintf(buf+len, "T10 Vendor ID based\n");
1020		break;
1021	case 0x02:
1022		sprintf(buf+len, "EUI-64 based\n");
1023		break;
1024	case 0x03:
1025		sprintf(buf+len, "NAA\n");
1026		break;
1027	case 0x04:
1028		sprintf(buf+len, "Relative target port identifier\n");
1029		break;
1030	case 0x08:
1031		sprintf(buf+len, "SCSI name string\n");
1032		break;
1033	default:
1034		sprintf(buf+len, "Unsupported: 0x%02x\n",
1035				vpd->device_identifier_type);
1036		ret = -EINVAL;
1037		break;
1038	}
1039
1040	if (p_buf) {
1041		if (p_buf_len < strlen(buf)+1)
1042			return -EINVAL;
1043		strncpy(p_buf, buf, p_buf_len);
1044	} else {
1045		pr_debug("%s", buf);
1046	}
1047
1048	return ret;
1049}
1050
1051int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1052{
1053	/*
1054	 * The VPD identifier type..
1055	 *
1056	 * from spc3r23.pdf Section 7.6.3.1 Table 298
1057	 */
1058	vpd->device_identifier_type = (page_83[1] & 0x0f);
1059	return transport_dump_vpd_ident_type(vpd, NULL, 0);
1060}
1061EXPORT_SYMBOL(transport_set_vpd_ident_type);
1062
1063int transport_dump_vpd_ident(
1064	struct t10_vpd *vpd,
1065	unsigned char *p_buf,
1066	int p_buf_len)
1067{
1068	unsigned char buf[VPD_TMP_BUF_SIZE];
1069	int ret = 0;
1070
1071	memset(buf, 0, VPD_TMP_BUF_SIZE);
1072
1073	switch (vpd->device_identifier_code_set) {
1074	case 0x01: /* Binary */
1075		sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1076			&vpd->device_identifier[0]);
1077		break;
1078	case 0x02: /* ASCII */
1079		sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1080			&vpd->device_identifier[0]);
1081		break;
1082	case 0x03: /* UTF-8 */
1083		sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1084			&vpd->device_identifier[0]);
1085		break;
1086	default:
1087		sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1088			" 0x%02x", vpd->device_identifier_code_set);
1089		ret = -EINVAL;
1090		break;
1091	}
1092
1093	if (p_buf)
1094		strncpy(p_buf, buf, p_buf_len);
1095	else
1096		pr_debug("%s", buf);
1097
1098	return ret;
1099}
1100
1101int
1102transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1103{
1104	static const char hex_str[] = "0123456789abcdef";
1105	int j = 0, i = 4; /* offset to start of the identifer */
1106
1107	/*
1108	 * The VPD Code Set (encoding)
1109	 *
1110	 * from spc3r23.pdf Section 7.6.3.1 Table 296
1111	 */
1112	vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1113	switch (vpd->device_identifier_code_set) {
1114	case 0x01: /* Binary */
1115		vpd->device_identifier[j++] =
1116				hex_str[vpd->device_identifier_type];
1117		while (i < (4 + page_83[3])) {
1118			vpd->device_identifier[j++] =
1119				hex_str[(page_83[i] & 0xf0) >> 4];
1120			vpd->device_identifier[j++] =
1121				hex_str[page_83[i] & 0x0f];
1122			i++;
1123		}
1124		break;
1125	case 0x02: /* ASCII */
1126	case 0x03: /* UTF-8 */
1127		while (i < (4 + page_83[3]))
1128			vpd->device_identifier[j++] = page_83[i++];
1129		break;
1130	default:
1131		break;
1132	}
1133
1134	return transport_dump_vpd_ident(vpd, NULL, 0);
1135}
1136EXPORT_SYMBOL(transport_set_vpd_ident);
1137
1138static void core_setup_task_attr_emulation(struct se_device *dev)
1139{
1140	/*
1141	 * If this device is from Target_Core_Mod/pSCSI, disable the
1142	 * SAM Task Attribute emulation.
1143	 *
1144	 * This is currently not available in upsream Linux/SCSI Target
1145	 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1146	 */
1147	if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1148		dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1149		return;
1150	}
1151
1152	dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
1153	pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1154		" device\n", dev->transport->name,
1155		dev->transport->get_device_rev(dev));
1156}
1157
1158static void scsi_dump_inquiry(struct se_device *dev)
1159{
1160	struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
1161	char buf[17];
1162	int i, device_type;
1163	/*
1164	 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1165	 */
 
1166	for (i = 0; i < 8; i++)
1167		if (wwn->vendor[i] >= 0x20)
1168			buf[i] = wwn->vendor[i];
1169		else
1170			buf[i] = ' ';
1171	buf[i] = '\0';
1172	pr_debug("  Vendor: %s\n", buf);
1173
 
1174	for (i = 0; i < 16; i++)
1175		if (wwn->model[i] >= 0x20)
1176			buf[i] = wwn->model[i];
1177		else
1178			buf[i] = ' ';
1179	buf[i] = '\0';
1180	pr_debug("  Model: %s\n", buf);
1181
 
1182	for (i = 0; i < 4; i++)
1183		if (wwn->revision[i] >= 0x20)
1184			buf[i] = wwn->revision[i];
1185		else
1186			buf[i] = ' ';
1187	buf[i] = '\0';
1188	pr_debug("  Revision: %s\n", buf);
1189
1190	device_type = dev->transport->get_device_type(dev);
1191	pr_debug("  Type:   %s ", scsi_device_type(device_type));
1192	pr_debug("                 ANSI SCSI revision: %02x\n",
1193				dev->transport->get_device_rev(dev));
1194}
1195
1196struct se_device *transport_add_device_to_core_hba(
1197	struct se_hba *hba,
1198	struct se_subsystem_api *transport,
1199	struct se_subsystem_dev *se_dev,
1200	u32 device_flags,
1201	void *transport_dev,
1202	struct se_dev_limits *dev_limits,
1203	const char *inquiry_prod,
1204	const char *inquiry_rev)
1205{
1206	int force_pt;
1207	struct se_device  *dev;
1208
1209	dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
1210	if (!dev) {
1211		pr_err("Unable to allocate memory for se_dev_t\n");
1212		return NULL;
1213	}
1214
1215	transport_init_queue_obj(&dev->dev_queue_obj);
1216	dev->dev_flags		= device_flags;
1217	dev->dev_status		|= TRANSPORT_DEVICE_DEACTIVATED;
1218	dev->dev_ptr		= transport_dev;
1219	dev->se_hba		= hba;
1220	dev->se_sub_dev		= se_dev;
1221	dev->transport		= transport;
 
1222	INIT_LIST_HEAD(&dev->dev_list);
1223	INIT_LIST_HEAD(&dev->dev_sep_list);
1224	INIT_LIST_HEAD(&dev->dev_tmr_list);
1225	INIT_LIST_HEAD(&dev->execute_list);
1226	INIT_LIST_HEAD(&dev->delayed_cmd_list);
1227	INIT_LIST_HEAD(&dev->state_list);
 
1228	INIT_LIST_HEAD(&dev->qf_cmd_list);
1229	spin_lock_init(&dev->execute_task_lock);
1230	spin_lock_init(&dev->delayed_cmd_lock);
 
 
 
1231	spin_lock_init(&dev->dev_reservation_lock);
1232	spin_lock_init(&dev->dev_status_lock);
 
1233	spin_lock_init(&dev->se_port_lock);
1234	spin_lock_init(&dev->se_tmr_lock);
1235	spin_lock_init(&dev->qf_cmd_lock);
 
 
 
1236	atomic_set(&dev->dev_ordered_id, 0);
1237
1238	se_dev_set_default_attribs(dev, dev_limits);
1239
1240	dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1241	dev->creation_time = get_jiffies_64();
1242	spin_lock_init(&dev->stats_lock);
1243
1244	spin_lock(&hba->device_lock);
1245	list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1246	hba->dev_count++;
1247	spin_unlock(&hba->device_lock);
1248	/*
1249	 * Setup the SAM Task Attribute emulation for struct se_device
1250	 */
1251	core_setup_task_attr_emulation(dev);
1252	/*
1253	 * Force PR and ALUA passthrough emulation with internal object use.
1254	 */
1255	force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1256	/*
1257	 * Setup the Reservations infrastructure for struct se_device
1258	 */
1259	core_setup_reservations(dev, force_pt);
1260	/*
1261	 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1262	 */
1263	if (core_setup_alua(dev, force_pt) < 0)
1264		goto out;
1265
1266	/*
1267	 * Startup the struct se_device processing thread
1268	 */
1269	dev->process_thread = kthread_run(transport_processing_thread, dev,
1270					  "LIO_%s", dev->transport->name);
1271	if (IS_ERR(dev->process_thread)) {
1272		pr_err("Unable to create kthread: LIO_%s\n",
1273			dev->transport->name);
1274		goto out;
1275	}
1276	/*
1277	 * Setup work_queue for QUEUE_FULL
1278	 */
1279	INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1280	/*
1281	 * Preload the initial INQUIRY const values if we are doing
1282	 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1283	 * passthrough because this is being provided by the backend LLD.
1284	 * This is required so that transport_get_inquiry() copies these
1285	 * originals once back into DEV_T10_WWN(dev) for the virtual device
1286	 * setup.
1287	 */
1288	if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1289		if (!inquiry_prod || !inquiry_rev) {
1290			pr_err("All non TCM/pSCSI plugins require"
1291				" INQUIRY consts\n");
1292			goto out;
1293		}
1294
1295		strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1296		strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1297		strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
1298	}
1299	scsi_dump_inquiry(dev);
1300
1301	return dev;
1302out:
1303	kthread_stop(dev->process_thread);
1304
1305	spin_lock(&hba->device_lock);
1306	list_del(&dev->dev_list);
1307	hba->dev_count--;
1308	spin_unlock(&hba->device_lock);
1309
1310	se_release_vpd_for_dev(dev);
1311
1312	kfree(dev);
1313
1314	return NULL;
1315}
1316EXPORT_SYMBOL(transport_add_device_to_core_hba);
1317
1318/*	transport_generic_prepare_cdb():
1319 *
1320 *	Since the Initiator sees iSCSI devices as LUNs,  the SCSI CDB will
1321 *	contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1322 *	The point of this is since we are mapping iSCSI LUNs to
1323 *	SCSI Target IDs having a non-zero LUN in the CDB will throw the
1324 *	devices and HBAs for a loop.
1325 */
1326static inline void transport_generic_prepare_cdb(
1327	unsigned char *cdb)
1328{
1329	switch (cdb[0]) {
1330	case READ_10: /* SBC - RDProtect */
1331	case READ_12: /* SBC - RDProtect */
1332	case READ_16: /* SBC - RDProtect */
1333	case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1334	case VERIFY: /* SBC - VRProtect */
1335	case VERIFY_16: /* SBC - VRProtect */
1336	case WRITE_VERIFY: /* SBC - VRProtect */
1337	case WRITE_VERIFY_12: /* SBC - VRProtect */
1338	case MAINTENANCE_IN: /* SPC - Parameter Data Format for SA RTPG */
1339		break;
1340	default:
1341		cdb[1] &= 0x1f; /* clear logical unit number */
1342		break;
1343	}
1344}
1345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1346static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1347
1348/*
1349 * Used by fabric modules containing a local struct se_cmd within their
1350 * fabric dependent per I/O descriptor.
1351 */
1352void transport_init_se_cmd(
1353	struct se_cmd *cmd,
1354	struct target_core_fabric_ops *tfo,
1355	struct se_session *se_sess,
1356	u32 data_length,
1357	int data_direction,
1358	int task_attr,
1359	unsigned char *sense_buffer)
1360{
1361	INIT_LIST_HEAD(&cmd->se_lun_node);
1362	INIT_LIST_HEAD(&cmd->se_delayed_node);
 
1363	INIT_LIST_HEAD(&cmd->se_qf_node);
1364	INIT_LIST_HEAD(&cmd->se_queue_node);
1365	INIT_LIST_HEAD(&cmd->se_cmd_list);
1366	INIT_LIST_HEAD(&cmd->execute_list);
1367	INIT_LIST_HEAD(&cmd->state_list);
1368	init_completion(&cmd->transport_lun_fe_stop_comp);
1369	init_completion(&cmd->transport_lun_stop_comp);
1370	init_completion(&cmd->t_transport_stop_comp);
1371	init_completion(&cmd->cmd_wait_comp);
1372	init_completion(&cmd->task_stop_comp);
1373	spin_lock_init(&cmd->t_state_lock);
1374	cmd->transport_state = CMD_T_DEV_ACTIVE;
1375
1376	cmd->se_tfo = tfo;
1377	cmd->se_sess = se_sess;
1378	cmd->data_length = data_length;
1379	cmd->data_direction = data_direction;
1380	cmd->sam_task_attr = task_attr;
1381	cmd->sense_buffer = sense_buffer;
1382
1383	cmd->state_active = false;
1384}
1385EXPORT_SYMBOL(transport_init_se_cmd);
1386
1387static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1388{
1389	/*
1390	 * Check if SAM Task Attribute emulation is enabled for this
1391	 * struct se_device storage object
1392	 */
1393	if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1394		return 0;
1395
1396	if (cmd->sam_task_attr == MSG_ACA_TAG) {
1397		pr_debug("SAM Task Attribute ACA"
1398			" emulation is not supported\n");
1399		return -EINVAL;
1400	}
1401	/*
1402	 * Used to determine when ORDERED commands should go from
1403	 * Dormant to Active status.
1404	 */
1405	cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
1406	smp_mb__after_atomic_inc();
1407	pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1408			cmd->se_ordered_id, cmd->sam_task_attr,
1409			cmd->se_dev->transport->name);
1410	return 0;
1411}
1412
1413/*	target_setup_cmd_from_cdb():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1414 *
1415 *	Called from fabric RX Thread.
1416 */
1417int target_setup_cmd_from_cdb(
1418	struct se_cmd *cmd,
1419	unsigned char *cdb)
1420{
1421	int ret;
1422
1423	transport_generic_prepare_cdb(cdb);
 
 
 
 
 
 
1424	/*
1425	 * Ensure that the received CDB is less than the max (252 + 8) bytes
1426	 * for VARIABLE_LENGTH_CMD
1427	 */
1428	if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1429		pr_err("Received SCSI CDB with command_size: %d that"
1430			" exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1431			scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1432		cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1433		cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1434		return -EINVAL;
1435	}
1436	/*
1437	 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1438	 * allocate the additional extended CDB buffer now..  Otherwise
1439	 * setup the pointer from __t_task_cdb to t_task_cdb.
1440	 */
1441	if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1442		cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1443						GFP_KERNEL);
1444		if (!cmd->t_task_cdb) {
1445			pr_err("Unable to allocate cmd->t_task_cdb"
1446				" %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1447				scsi_command_size(cdb),
1448				(unsigned long)sizeof(cmd->__t_task_cdb));
1449			cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1450			cmd->scsi_sense_reason =
1451					TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1452			return -ENOMEM;
1453		}
1454	} else
1455		cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1456	/*
1457	 * Copy the original CDB into cmd->
1458	 */
1459	memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1460	/*
1461	 * Setup the received CDB based on SCSI defined opcodes and
1462	 * perform unit attention, persistent reservations and ALUA
1463	 * checks for virtual device backends.  The cmd->t_task_cdb
1464	 * pointer is expected to be setup before we reach this point.
1465	 */
1466	ret = transport_generic_cmd_sequencer(cmd, cdb);
1467	if (ret < 0)
1468		return ret;
1469	/*
1470	 * Check for SAM Task Attribute Emulation
1471	 */
1472	if (transport_check_alloc_task_attr(cmd) < 0) {
1473		cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1474		cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1475		return -EINVAL;
1476	}
1477	spin_lock(&cmd->se_lun->lun_sep_lock);
1478	if (cmd->se_lun->lun_sep)
1479		cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1480	spin_unlock(&cmd->se_lun->lun_sep_lock);
1481	return 0;
1482}
1483EXPORT_SYMBOL(target_setup_cmd_from_cdb);
1484
1485/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1486 * Used by fabric module frontends to queue tasks directly.
1487 * Many only be used from process context only
1488 */
1489int transport_handle_cdb_direct(
1490	struct se_cmd *cmd)
1491{
1492	int ret;
1493
1494	if (!cmd->se_lun) {
1495		dump_stack();
1496		pr_err("cmd->se_lun is NULL\n");
1497		return -EINVAL;
1498	}
1499	if (in_interrupt()) {
1500		dump_stack();
1501		pr_err("transport_generic_handle_cdb cannot be called"
1502				" from interrupt context\n");
1503		return -EINVAL;
1504	}
1505	/*
1506	 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
1507	 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1508	 * in existing usage to ensure that outstanding descriptors are handled
1509	 * correctly during shutdown via transport_wait_for_tasks()
1510	 *
1511	 * Also, we don't take cmd->t_state_lock here as we only expect
1512	 * this to be called for initial descriptor submission.
1513	 */
1514	cmd->t_state = TRANSPORT_NEW_CMD;
1515	cmd->transport_state |= CMD_T_ACTIVE;
1516
1517	/*
1518	 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1519	 * so follow TRANSPORT_NEW_CMD processing thread context usage
1520	 * and call transport_generic_request_failure() if necessary..
1521	 */
1522	ret = transport_generic_new_cmd(cmd);
1523	if (ret < 0)
1524		transport_generic_request_failure(cmd);
1525
1526	return 0;
1527}
1528EXPORT_SYMBOL(transport_handle_cdb_direct);
1529
1530/**
1531 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1532 *
1533 * @se_cmd: command descriptor to submit
1534 * @se_sess: associated se_sess for endpoint
1535 * @cdb: pointer to SCSI CDB
1536 * @sense: pointer to SCSI sense buffer
1537 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1538 * @data_length: fabric expected data transfer length
1539 * @task_addr: SAM task attribute
1540 * @data_dir: DMA data direction
1541 * @flags: flags for command submission from target_sc_flags_tables
1542 *
1543 * This may only be called from process context, and also currently
1544 * assumes internal allocation of fabric payload buffer by target-core.
1545 **/
1546void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
1547		unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1548		u32 data_length, int task_attr, int data_dir, int flags)
1549{
1550	struct se_portal_group *se_tpg;
1551	int rc;
1552
1553	se_tpg = se_sess->se_tpg;
1554	BUG_ON(!se_tpg);
1555	BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1556	BUG_ON(in_interrupt());
1557	/*
1558	 * Initialize se_cmd for target operation.  From this point
1559	 * exceptions are handled by sending exception status via
1560	 * target_core_fabric_ops->queue_status() callback
1561	 */
1562	transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1563				data_length, data_dir, task_attr, sense);
1564	if (flags & TARGET_SCF_UNKNOWN_SIZE)
1565		se_cmd->unknown_data_length = 1;
1566	/*
1567	 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1568	 * se_sess->sess_cmd_list.  A second kref_get here is necessary
1569	 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1570	 * kref_put() to happen during fabric packet acknowledgement.
1571	 */
1572	target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1573	/*
1574	 * Signal bidirectional data payloads to target-core
1575	 */
1576	if (flags & TARGET_SCF_BIDI_OP)
1577		se_cmd->se_cmd_flags |= SCF_BIDI;
1578	/*
1579	 * Locate se_lun pointer and attach it to struct se_cmd
1580	 */
1581	if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1582		transport_send_check_condition_and_sense(se_cmd,
1583				se_cmd->scsi_sense_reason, 0);
1584		target_put_sess_cmd(se_sess, se_cmd);
1585		return;
1586	}
1587	/*
1588	 * Sanitize CDBs via transport_generic_cmd_sequencer() and
1589	 * allocate the necessary tasks to complete the received CDB+data
1590	 */
1591	rc = target_setup_cmd_from_cdb(se_cmd, cdb);
1592	if (rc != 0) {
1593		transport_generic_request_failure(se_cmd);
1594		return;
1595	}
1596
1597	/*
1598	 * Check if we need to delay processing because of ALUA
1599	 * Active/NonOptimized primary access state..
1600	 */
1601	core_alua_check_nonop_delay(se_cmd);
1602
1603	/*
1604	 * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
1605	 * for immediate execution of READs, otherwise wait for
1606	 * transport_generic_handle_data() to be called for WRITEs
1607	 * when fabric has filled the incoming buffer.
1608	 */
1609	transport_handle_cdb_direct(se_cmd);
1610	return;
1611}
1612EXPORT_SYMBOL(target_submit_cmd);
1613
1614static void target_complete_tmr_failure(struct work_struct *work)
1615{
1616	struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
1617
1618	se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1619	se_cmd->se_tfo->queue_tm_rsp(se_cmd);
1620	transport_generic_free_cmd(se_cmd, 0);
1621}
1622
1623/**
1624 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1625 *                     for TMR CDBs
1626 *
1627 * @se_cmd: command descriptor to submit
1628 * @se_sess: associated se_sess for endpoint
1629 * @sense: pointer to SCSI sense buffer
1630 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1631 * @fabric_context: fabric context for TMR req
1632 * @tm_type: Type of TM request
1633 * @gfp: gfp type for caller
1634 * @tag: referenced task tag for TMR_ABORT_TASK
1635 * @flags: submit cmd flags
1636 *
1637 * Callable from all contexts.
1638 **/
1639
1640int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
1641		unsigned char *sense, u32 unpacked_lun,
1642		void *fabric_tmr_ptr, unsigned char tm_type,
1643		gfp_t gfp, unsigned int tag, int flags)
1644{
1645	struct se_portal_group *se_tpg;
1646	int ret;
1647
1648	se_tpg = se_sess->se_tpg;
1649	BUG_ON(!se_tpg);
1650
1651	transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1652			      0, DMA_NONE, MSG_SIMPLE_TAG, sense);
1653	/*
1654	 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1655	 * allocation failure.
1656	 */
1657	ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
1658	if (ret < 0)
1659		return -ENOMEM;
1660
1661	if (tm_type == TMR_ABORT_TASK)
1662		se_cmd->se_tmr_req->ref_task_tag = tag;
1663
1664	/* See target_submit_cmd for commentary */
1665	target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1666
1667	ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1668	if (ret) {
1669		/*
1670		 * For callback during failure handling, push this work off
1671		 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1672		 */
1673		INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
1674		schedule_work(&se_cmd->work);
1675		return 0;
 
 
 
 
1676	}
1677	transport_generic_handle_tmr(se_cmd);
1678	return 0;
1679}
1680EXPORT_SYMBOL(target_submit_tmr);
1681
1682/*
1683 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1684 * to  queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1685 * complete setup in TCM process context w/ TFO->new_cmd_map().
1686 */
1687int transport_generic_handle_cdb_map(
1688	struct se_cmd *cmd)
1689{
1690	if (!cmd->se_lun) {
1691		dump_stack();
1692		pr_err("cmd->se_lun is NULL\n");
1693		return -EINVAL;
1694	}
1695
1696	transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
1697	return 0;
1698}
1699EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1700
1701/*	transport_generic_handle_data():
1702 *
1703 *
1704 */
1705int transport_generic_handle_data(
1706	struct se_cmd *cmd)
1707{
1708	/*
1709	 * For the software fabric case, then we assume the nexus is being
1710	 * failed/shutdown when signals are pending from the kthread context
1711	 * caller, so we return a failure.  For the HW target mode case running
1712	 * in interrupt code, the signal_pending() check is skipped.
1713	 */
1714	if (!in_interrupt() && signal_pending(current))
1715		return -EPERM;
1716	/*
1717	 * If the received CDB has aleady been ABORTED by the generic
1718	 * target engine, we now call transport_check_aborted_status()
1719	 * to queue any delated TASK_ABORTED status for the received CDB to the
1720	 * fabric module as we are expecting no further incoming DATA OUT
1721	 * sequences at this point.
1722	 */
1723	if (transport_check_aborted_status(cmd, 1) != 0)
1724		return 0;
1725
1726	transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
1727	return 0;
1728}
1729EXPORT_SYMBOL(transport_generic_handle_data);
1730
1731/*	transport_generic_handle_tmr():
1732 *
1733 *
1734 */
1735int transport_generic_handle_tmr(
1736	struct se_cmd *cmd)
1737{
1738	transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
 
 
 
 
 
1739	return 0;
1740}
1741EXPORT_SYMBOL(transport_generic_handle_tmr);
1742
1743/*
1744 * If the cmd is active, request it to be stopped and sleep until it
1745 * has completed.
1746 */
1747bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
 
 
 
1748{
1749	bool was_active = false;
 
 
1750
1751	if (cmd->transport_state & CMD_T_BUSY) {
1752		cmd->transport_state |= CMD_T_REQUEST_STOP;
1753		spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1754
1755		pr_debug("cmd %p waiting to complete\n", cmd);
1756		wait_for_completion(&cmd->task_stop_comp);
1757		pr_debug("cmd %p stopped successfully\n", cmd);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1758
1759		spin_lock_irqsave(&cmd->t_state_lock, *flags);
1760		cmd->transport_state &= ~CMD_T_REQUEST_STOP;
1761		cmd->transport_state &= ~CMD_T_BUSY;
1762		was_active = true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1763	}
 
1764
1765	return was_active;
1766}
1767
1768/*
1769 * Handle SAM-esque emulation for generic transport request failures.
1770 */
1771void transport_generic_request_failure(struct se_cmd *cmd)
 
 
 
 
1772{
1773	int ret = 0;
1774
1775	pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1776		" CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
1777		cmd->t_task_cdb[0]);
1778	pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
 
1779		cmd->se_tfo->get_cmd_state(cmd),
1780		cmd->t_state, cmd->scsi_sense_reason);
1781	pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
1782		(cmd->transport_state & CMD_T_ACTIVE) != 0,
1783		(cmd->transport_state & CMD_T_STOP) != 0,
1784		(cmd->transport_state & CMD_T_SENT) != 0);
 
 
 
 
 
 
 
1785
 
 
 
 
1786	/*
1787	 * For SAM Task Attribute emulation for failed struct se_cmd
1788	 */
1789	if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1790		transport_complete_task_attr(cmd);
1791
1792	switch (cmd->scsi_sense_reason) {
1793	case TCM_NON_EXISTENT_LUN:
1794	case TCM_UNSUPPORTED_SCSI_OPCODE:
1795	case TCM_INVALID_CDB_FIELD:
1796	case TCM_INVALID_PARAMETER_LIST:
1797	case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1798	case TCM_UNKNOWN_MODE_PAGE:
1799	case TCM_WRITE_PROTECTED:
1800	case TCM_ADDRESS_OUT_OF_RANGE:
1801	case TCM_CHECK_CONDITION_ABORT_CMD:
1802	case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1803	case TCM_CHECK_CONDITION_NOT_READY:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1804		break;
1805	case TCM_RESERVATION_CONFLICT:
1806		/*
1807		 * No SENSE Data payload for this case, set SCSI Status
1808		 * and queue the response to $FABRIC_MOD.
1809		 *
1810		 * Uses linux/include/scsi/scsi.h SAM status codes defs
1811		 */
1812		cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1813		/*
1814		 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1815		 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1816		 * CONFLICT STATUS.
1817		 *
1818		 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1819		 */
1820		if (cmd->se_sess &&
1821		    cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1822			core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
1823				cmd->orig_fe_lun, 0x2C,
1824				ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1825
1826		ret = cmd->se_tfo->queue_status(cmd);
1827		if (ret == -EAGAIN || ret == -ENOMEM)
1828			goto queue_full;
1829		goto check_stop;
 
 
 
 
 
1830	default:
1831		pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1832			cmd->t_task_cdb[0], cmd->scsi_sense_reason);
 
1833		cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1834		break;
1835	}
1836	/*
1837	 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1838	 * make the call to transport_send_check_condition_and_sense()
1839	 * directly.  Otherwise expect the fabric to make the call to
1840	 * transport_send_check_condition_and_sense() after handling
1841	 * possible unsoliticied write data payloads.
1842	 */
1843	ret = transport_send_check_condition_and_sense(cmd,
1844			cmd->scsi_sense_reason, 0);
1845	if (ret == -EAGAIN || ret == -ENOMEM)
1846		goto queue_full;
 
 
 
 
1847
1848check_stop:
1849	transport_lun_remove_cmd(cmd);
1850	if (!transport_cmd_check_stop_to_fabric(cmd))
1851		;
1852	return;
1853
1854queue_full:
1855	cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1856	transport_handle_queue_full(cmd, cmd->se_dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1857}
1858EXPORT_SYMBOL(transport_generic_request_failure);
1859
1860static inline u32 transport_lba_21(unsigned char *cdb)
1861{
1862	return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1863}
1864
1865static inline u32 transport_lba_32(unsigned char *cdb)
1866{
1867	return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1868}
1869
1870static inline unsigned long long transport_lba_64(unsigned char *cdb)
1871{
1872	unsigned int __v1, __v2;
1873
1874	__v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1875	__v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1876
1877	return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1878}
1879
1880/*
1881 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
1882 */
1883static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
1884{
1885	unsigned int __v1, __v2;
1886
1887	__v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
1888	__v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
1889
1890	return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1891}
1892
1893static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
1894{
1895	unsigned long flags;
1896
1897	spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1898	se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1899	spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1900}
1901
1902/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1903 * Called from Fabric Module context from transport_execute_tasks()
1904 *
1905 * The return of this function determins if the tasks from struct se_cmd
1906 * get added to the execution queue in transport_execute_tasks(),
1907 * or are added to the delayed or ordered lists here.
1908 */
1909static inline int transport_execute_task_attr(struct se_cmd *cmd)
1910{
1911	if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1912		return 1;
1913	/*
1914	 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
1915	 * to allow the passed struct se_cmd list of tasks to the front of the list.
1916	 */
1917	 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
 
 
1918		pr_debug("Added HEAD_OF_QUEUE for CDB:"
1919			" 0x%02x, se_ordered_id: %u\n",
1920			cmd->t_task_cdb[0],
1921			cmd->se_ordered_id);
1922		return 1;
1923	} else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
 
 
 
 
 
1924		atomic_inc(&cmd->se_dev->dev_ordered_sync);
1925		smp_mb__after_atomic_inc();
1926
1927		pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
1928				" list, se_ordered_id: %u\n",
1929				cmd->t_task_cdb[0],
1930				cmd->se_ordered_id);
1931		/*
1932		 * Add ORDERED command to tail of execution queue if
1933		 * no other older commands exist that need to be
1934		 * completed first.
1935		 */
1936		if (!atomic_read(&cmd->se_dev->simple_cmds))
1937			return 1;
1938	} else {
1939		/*
1940		 * For SIMPLE and UNTAGGED Task Attribute commands
1941		 */
1942		atomic_inc(&cmd->se_dev->simple_cmds);
1943		smp_mb__after_atomic_inc();
1944	}
1945	/*
1946	 * Otherwise if one or more outstanding ORDERED task attribute exist,
1947	 * add the dormant task(s) built for the passed struct se_cmd to the
1948	 * execution queue and become in Active state for this struct se_device.
1949	 */
1950	if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
1951		/*
1952		 * Otherwise, add cmd w/ tasks to delayed cmd queue that
1953		 * will be drained upon completion of HEAD_OF_QUEUE task.
1954		 */
1955		spin_lock(&cmd->se_dev->delayed_cmd_lock);
1956		cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
1957		list_add_tail(&cmd->se_delayed_node,
1958				&cmd->se_dev->delayed_cmd_list);
1959		spin_unlock(&cmd->se_dev->delayed_cmd_lock);
1960
1961		pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
1962			" delayed CMD list, se_ordered_id: %u\n",
1963			cmd->t_task_cdb[0], cmd->sam_task_attr,
1964			cmd->se_ordered_id);
1965		/*
1966		 * Return zero to let transport_execute_tasks() know
1967		 * not to add the delayed tasks to the execution list.
1968		 */
1969		return 0;
1970	}
1971	/*
1972	 * Otherwise, no ORDERED task attributes exist..
1973	 */
1974	return 1;
1975}
1976
1977/*
1978 * Called from fabric module context in transport_generic_new_cmd() and
1979 * transport_generic_process_write()
1980 */
1981static void transport_execute_tasks(struct se_cmd *cmd)
1982{
1983	int add_tasks;
1984	struct se_device *se_dev = cmd->se_dev;
 
 
 
 
 
 
1985	/*
1986	 * Call transport_cmd_check_stop() to see if a fabric exception
1987	 * has occurred that prevents execution.
1988	 */
1989	if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
1990		/*
1991		 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
1992		 * attribute for the tasks of the received struct se_cmd CDB
1993		 */
1994		add_tasks = transport_execute_task_attr(cmd);
1995		if (add_tasks) {
1996			__transport_execute_tasks(se_dev, cmd);
1997			return;
1998		}
 
 
 
 
 
1999	}
2000	__transport_execute_tasks(se_dev, NULL);
 
 
 
 
 
 
2001}
2002
2003static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
 
 
 
 
 
 
2004{
2005	int error;
2006	struct se_cmd *cmd = NULL;
 
2007	unsigned long flags;
2008
 
 
 
 
2009check_depth:
 
 
 
 
 
2010	spin_lock_irq(&dev->execute_task_lock);
2011	if (new_cmd != NULL)
2012		__target_add_to_execute_list(new_cmd);
2013
2014	if (list_empty(&dev->execute_list)) {
2015		spin_unlock_irq(&dev->execute_task_lock);
2016		return 0;
2017	}
2018	cmd = list_first_entry(&dev->execute_list, struct se_cmd, execute_list);
2019	__target_remove_from_execute_list(cmd);
 
 
 
2020	spin_unlock_irq(&dev->execute_task_lock);
2021
 
 
 
 
2022	spin_lock_irqsave(&cmd->t_state_lock, flags);
2023	cmd->transport_state |= CMD_T_BUSY;
2024	cmd->transport_state |= CMD_T_SENT;
 
 
 
 
 
2025
 
2026	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2027
2028	if (cmd->execute_cmd)
2029		error = cmd->execute_cmd(cmd);
2030	else {
2031		error = dev->transport->execute_cmd(cmd, cmd->t_data_sg,
2032				cmd->t_data_nents, cmd->data_direction);
 
 
2033	}
2034
2035	if (error != 0) {
2036		spin_lock_irqsave(&cmd->t_state_lock, flags);
2037		cmd->transport_state &= ~CMD_T_BUSY;
2038		cmd->transport_state &= ~CMD_T_SENT;
2039		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2040
2041		transport_generic_request_failure(cmd);
2042	}
2043
2044	new_cmd = NULL;
2045	goto check_depth;
2046
2047	return 0;
2048}
2049
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2050static inline u32 transport_get_sectors_6(
2051	unsigned char *cdb,
2052	struct se_cmd *cmd,
2053	int *ret)
2054{
2055	struct se_device *dev = cmd->se_dev;
2056
2057	/*
2058	 * Assume TYPE_DISK for non struct se_device objects.
2059	 * Use 8-bit sector value.
2060	 */
2061	if (!dev)
2062		goto type_disk;
2063
2064	/*
2065	 * Use 24-bit allocation length for TYPE_TAPE.
2066	 */
2067	if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2068		return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2069
2070	/*
2071	 * Everything else assume TYPE_DISK Sector CDB location.
2072	 * Use 8-bit sector value.  SBC-3 says:
2073	 *
2074	 *   A TRANSFER LENGTH field set to zero specifies that 256
2075	 *   logical blocks shall be written.  Any other value
2076	 *   specifies the number of logical blocks that shall be
2077	 *   written.
2078	 */
2079type_disk:
2080	return cdb[4] ? : 256;
2081}
2082
2083static inline u32 transport_get_sectors_10(
2084	unsigned char *cdb,
2085	struct se_cmd *cmd,
2086	int *ret)
2087{
2088	struct se_device *dev = cmd->se_dev;
2089
2090	/*
2091	 * Assume TYPE_DISK for non struct se_device objects.
2092	 * Use 16-bit sector value.
2093	 */
2094	if (!dev)
2095		goto type_disk;
2096
2097	/*
2098	 * XXX_10 is not defined in SSC, throw an exception
2099	 */
2100	if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2101		*ret = -EINVAL;
2102		return 0;
2103	}
2104
2105	/*
2106	 * Everything else assume TYPE_DISK Sector CDB location.
2107	 * Use 16-bit sector value.
2108	 */
2109type_disk:
2110	return (u32)(cdb[7] << 8) + cdb[8];
2111}
2112
2113static inline u32 transport_get_sectors_12(
2114	unsigned char *cdb,
2115	struct se_cmd *cmd,
2116	int *ret)
2117{
2118	struct se_device *dev = cmd->se_dev;
2119
2120	/*
2121	 * Assume TYPE_DISK for non struct se_device objects.
2122	 * Use 32-bit sector value.
2123	 */
2124	if (!dev)
2125		goto type_disk;
2126
2127	/*
2128	 * XXX_12 is not defined in SSC, throw an exception
2129	 */
2130	if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2131		*ret = -EINVAL;
2132		return 0;
2133	}
2134
2135	/*
2136	 * Everything else assume TYPE_DISK Sector CDB location.
2137	 * Use 32-bit sector value.
2138	 */
2139type_disk:
2140	return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2141}
2142
2143static inline u32 transport_get_sectors_16(
2144	unsigned char *cdb,
2145	struct se_cmd *cmd,
2146	int *ret)
2147{
2148	struct se_device *dev = cmd->se_dev;
2149
2150	/*
2151	 * Assume TYPE_DISK for non struct se_device objects.
2152	 * Use 32-bit sector value.
2153	 */
2154	if (!dev)
2155		goto type_disk;
2156
2157	/*
2158	 * Use 24-bit allocation length for TYPE_TAPE.
2159	 */
2160	if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2161		return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2162
2163type_disk:
2164	return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2165		    (cdb[12] << 8) + cdb[13];
2166}
2167
2168/*
2169 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2170 */
2171static inline u32 transport_get_sectors_32(
2172	unsigned char *cdb,
2173	struct se_cmd *cmd,
2174	int *ret)
2175{
2176	/*
2177	 * Assume TYPE_DISK for non struct se_device objects.
2178	 * Use 32-bit sector value.
2179	 */
2180	return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2181		    (cdb[30] << 8) + cdb[31];
2182
2183}
2184
2185static inline u32 transport_get_size(
2186	u32 sectors,
2187	unsigned char *cdb,
2188	struct se_cmd *cmd)
2189{
2190	struct se_device *dev = cmd->se_dev;
2191
2192	if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2193		if (cdb[1] & 1) { /* sectors */
2194			return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2195		} else /* bytes */
2196			return sectors;
2197	}
2198
2199	pr_debug("Returning block_size: %u, sectors: %u == %u for"
2200		" %s object\n", dev->se_sub_dev->se_dev_attrib.block_size,
2201		sectors, dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2202		dev->transport->name);
2203
2204	return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2205}
2206
2207static void transport_xor_callback(struct se_cmd *cmd)
2208{
2209	unsigned char *buf, *addr;
2210	struct scatterlist *sg;
2211	unsigned int offset;
2212	int i;
2213	int count;
2214	/*
2215	 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2216	 *
2217	 * 1) read the specified logical block(s);
2218	 * 2) transfer logical blocks from the data-out buffer;
2219	 * 3) XOR the logical blocks transferred from the data-out buffer with
2220	 *    the logical blocks read, storing the resulting XOR data in a buffer;
2221	 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2222	 *    blocks transferred from the data-out buffer; and
2223	 * 5) transfer the resulting XOR data to the data-in buffer.
2224	 */
2225	buf = kmalloc(cmd->data_length, GFP_KERNEL);
2226	if (!buf) {
2227		pr_err("Unable to allocate xor_callback buf\n");
2228		return;
2229	}
2230	/*
2231	 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
2232	 * into the locally allocated *buf
2233	 */
2234	sg_copy_to_buffer(cmd->t_data_sg,
2235			  cmd->t_data_nents,
2236			  buf,
2237			  cmd->data_length);
2238
2239	/*
2240	 * Now perform the XOR against the BIDI read memory located at
2241	 * cmd->t_mem_bidi_list
2242	 */
2243
2244	offset = 0;
2245	for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2246		addr = kmap_atomic(sg_page(sg));
2247		if (!addr)
2248			goto out;
2249
2250		for (i = 0; i < sg->length; i++)
2251			*(addr + sg->offset + i) ^= *(buf + offset + i);
2252
2253		offset += sg->length;
2254		kunmap_atomic(addr);
2255	}
2256
2257out:
2258	kfree(buf);
2259}
2260
2261/*
2262 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2263 */
2264static void transport_get_sense_data(struct se_cmd *cmd)
2265{
2266	unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
2267	struct se_device *dev = cmd->se_dev;
 
2268	unsigned long flags;
2269	u32 offset = 0;
2270
2271	WARN_ON(!cmd->se_lun);
2272
2273	if (!dev)
2274		return;
2275
2276	spin_lock_irqsave(&cmd->t_state_lock, flags);
2277	if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2278		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2279		return;
2280	}
2281
2282	sense_buffer = dev->transport->get_sense_buffer(cmd);
2283	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 
 
 
 
 
 
 
2284
2285	offset = cmd->se_tfo->set_fabric_sense_len(cmd, TRANSPORT_SENSE_BUFFER);
 
 
 
 
2286
2287	memcpy(&buffer[offset], sense_buffer, TRANSPORT_SENSE_BUFFER);
 
 
 
 
 
 
 
2288
2289	/* Automatically padded */
2290	cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
2291
2292	pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x and sense\n",
2293		dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2294}
2295
2296static inline long long transport_dev_end_lba(struct se_device *dev)
2297{
2298	return dev->transport->get_blocks(dev) + 1;
2299}
2300
2301static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2302{
2303	struct se_device *dev = cmd->se_dev;
2304	u32 sectors;
2305
2306	if (dev->transport->get_device_type(dev) != TYPE_DISK)
2307		return 0;
2308
2309	sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2310
2311	if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2312		pr_err("LBA: %llu Sectors: %u exceeds"
2313			" transport_dev_end_lba(): %llu\n",
2314			cmd->t_task_lba, sectors,
2315			transport_dev_end_lba(dev));
2316		return -EINVAL;
2317	}
2318
2319	return 0;
2320}
2321
2322static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2323{
2324	/*
2325	 * Determine if the received WRITE_SAME is used to for direct
2326	 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2327	 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2328	 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2329	 */
2330	int passthrough = (dev->transport->transport_type ==
2331				TRANSPORT_PLUGIN_PHBA_PDEV);
2332
2333	if (!passthrough) {
2334		if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2335			pr_err("WRITE_SAME PBDATA and LBDATA"
2336				" bits not supported for Block Discard"
2337				" Emulation\n");
2338			return -ENOSYS;
2339		}
2340		/*
2341		 * Currently for the emulated case we only accept
2342		 * tpws with the UNMAP=1 bit set.
2343		 */
2344		if (!(flags[0] & 0x08)) {
2345			pr_err("WRITE_SAME w/o UNMAP bit not"
2346				" supported for Block Discard Emulation\n");
2347			return -ENOSYS;
2348		}
2349	}
2350
2351	return 0;
2352}
2353
2354/*	transport_generic_cmd_sequencer():
2355 *
2356 *	Generic Command Sequencer that should work for most DAS transport
2357 *	drivers.
2358 *
2359 *	Called from target_setup_cmd_from_cdb() in the $FABRIC_MOD
2360 *	RX Thread.
2361 *
2362 *	FIXME: Need to support other SCSI OPCODES where as well.
2363 */
2364static int transport_generic_cmd_sequencer(
2365	struct se_cmd *cmd,
2366	unsigned char *cdb)
2367{
2368	struct se_device *dev = cmd->se_dev;
2369	struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2370	int ret = 0, sector_ret = 0, passthrough;
2371	u32 sectors = 0, size = 0, pr_reg_type = 0;
2372	u16 service_action;
2373	u8 alua_ascq = 0;
2374	/*
2375	 * Check for an existing UNIT ATTENTION condition
2376	 */
2377	if (core_scsi3_ua_check(cmd, cdb) < 0) {
 
 
2378		cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2379		cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
2380		return -EINVAL;
2381	}
2382	/*
2383	 * Check status of Asymmetric Logical Unit Assignment port
2384	 */
2385	ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
2386	if (ret != 0) {
 
2387		/*
2388		 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
2389		 * The ALUA additional sense code qualifier (ASCQ) is determined
2390		 * by the ALUA primary or secondary access state..
2391		 */
2392		if (ret > 0) {
 
2393			pr_debug("[%s]: ALUA TG Port not available,"
2394				" SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
2395				cmd->se_tfo->get_fabric_name(), alua_ascq);
2396
2397			transport_set_sense_codes(cmd, 0x04, alua_ascq);
2398			cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2399			cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
2400			return -EINVAL;
2401		}
2402		goto out_invalid_cdb_field;
2403	}
2404	/*
2405	 * Check status for SPC-3 Persistent Reservations
2406	 */
2407	if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2408		if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
2409					cmd, cdb, pr_reg_type) != 0) {
2410			cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2411			cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2412			cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2413			cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2414			return -EBUSY;
2415		}
2416		/*
2417		 * This means the CDB is allowed for the SCSI Initiator port
2418		 * when said port is *NOT* holding the legacy SPC-2 or
2419		 * SPC-3 Persistent Reservation.
2420		 */
2421	}
2422
2423	/*
2424	 * If we operate in passthrough mode we skip most CDB emulation and
2425	 * instead hand the commands down to the physical SCSI device.
2426	 */
2427	passthrough =
2428		(dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
2429
2430	switch (cdb[0]) {
2431	case READ_6:
2432		sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2433		if (sector_ret)
2434			goto out_unsupported_cdb;
2435		size = transport_get_size(sectors, cdb, cmd);
 
2436		cmd->t_task_lba = transport_lba_21(cdb);
2437		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2438		break;
2439	case READ_10:
2440		sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2441		if (sector_ret)
2442			goto out_unsupported_cdb;
2443		size = transport_get_size(sectors, cdb, cmd);
 
2444		cmd->t_task_lba = transport_lba_32(cdb);
2445		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2446		break;
2447	case READ_12:
2448		sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2449		if (sector_ret)
2450			goto out_unsupported_cdb;
2451		size = transport_get_size(sectors, cdb, cmd);
 
2452		cmd->t_task_lba = transport_lba_32(cdb);
2453		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2454		break;
2455	case READ_16:
2456		sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2457		if (sector_ret)
2458			goto out_unsupported_cdb;
2459		size = transport_get_size(sectors, cdb, cmd);
 
2460		cmd->t_task_lba = transport_lba_64(cdb);
2461		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2462		break;
2463	case WRITE_6:
2464		sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2465		if (sector_ret)
2466			goto out_unsupported_cdb;
2467		size = transport_get_size(sectors, cdb, cmd);
 
2468		cmd->t_task_lba = transport_lba_21(cdb);
2469		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2470		break;
2471	case WRITE_10:
2472	case WRITE_VERIFY:
2473		sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2474		if (sector_ret)
2475			goto out_unsupported_cdb;
2476		size = transport_get_size(sectors, cdb, cmd);
 
2477		cmd->t_task_lba = transport_lba_32(cdb);
2478		if (cdb[1] & 0x8)
2479			cmd->se_cmd_flags |= SCF_FUA;
2480		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2481		break;
2482	case WRITE_12:
2483		sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2484		if (sector_ret)
2485			goto out_unsupported_cdb;
2486		size = transport_get_size(sectors, cdb, cmd);
 
2487		cmd->t_task_lba = transport_lba_32(cdb);
2488		if (cdb[1] & 0x8)
2489			cmd->se_cmd_flags |= SCF_FUA;
2490		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2491		break;
2492	case WRITE_16:
2493		sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2494		if (sector_ret)
2495			goto out_unsupported_cdb;
2496		size = transport_get_size(sectors, cdb, cmd);
 
2497		cmd->t_task_lba = transport_lba_64(cdb);
2498		if (cdb[1] & 0x8)
2499			cmd->se_cmd_flags |= SCF_FUA;
2500		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2501		break;
2502	case XDWRITEREAD_10:
2503		if ((cmd->data_direction != DMA_TO_DEVICE) ||
2504		    !(cmd->se_cmd_flags & SCF_BIDI))
2505			goto out_invalid_cdb_field;
2506		sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2507		if (sector_ret)
2508			goto out_unsupported_cdb;
2509		size = transport_get_size(sectors, cdb, cmd);
 
2510		cmd->t_task_lba = transport_lba_32(cdb);
2511		cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2512
 
2513		/*
2514		 * Do now allow BIDI commands for passthrough mode.
2515		 */
2516		if (passthrough)
2517			goto out_unsupported_cdb;
2518
2519		/*
2520		 * Setup BIDI XOR callback to be run after I/O completion.
2521		 */
2522		cmd->transport_complete_callback = &transport_xor_callback;
2523		if (cdb[1] & 0x8)
2524			cmd->se_cmd_flags |= SCF_FUA;
2525		break;
2526	case VARIABLE_LENGTH_CMD:
2527		service_action = get_unaligned_be16(&cdb[8]);
 
 
 
 
 
 
 
2528		switch (service_action) {
2529		case XDWRITEREAD_32:
2530			sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2531			if (sector_ret)
2532				goto out_unsupported_cdb;
2533			size = transport_get_size(sectors, cdb, cmd);
2534			/*
2535			 * Use WRITE_32 and READ_32 opcodes for the emulated
2536			 * XDWRITE_READ_32 logic.
2537			 */
 
2538			cmd->t_task_lba = transport_lba_64_ext(cdb);
2539			cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2540
2541			/*
2542			 * Do now allow BIDI commands for passthrough mode.
2543			 */
2544			if (passthrough)
2545				goto out_unsupported_cdb;
2546
2547			/*
2548			 * Setup BIDI XOR callback to be run during after I/O
2549			 * completion.
2550			 */
2551			cmd->transport_complete_callback = &transport_xor_callback;
2552			if (cdb[1] & 0x8)
2553				cmd->se_cmd_flags |= SCF_FUA;
2554			break;
2555		case WRITE_SAME_32:
2556			sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2557			if (sector_ret)
2558				goto out_unsupported_cdb;
2559
2560			if (sectors)
2561				size = transport_get_size(1, cdb, cmd);
2562			else {
2563				pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2564				       " supported\n");
2565				goto out_invalid_cdb_field;
2566			}
2567
2568			cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
2569			cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2570
2571			if (target_check_write_same_discard(&cdb[10], dev) < 0)
2572				goto out_unsupported_cdb;
2573			if (!passthrough)
2574				cmd->execute_cmd = target_emulate_write_same;
2575			break;
2576		default:
2577			pr_err("VARIABLE_LENGTH_CMD service action"
2578				" 0x%04x not supported\n", service_action);
2579			goto out_unsupported_cdb;
2580		}
2581		break;
2582	case MAINTENANCE_IN:
2583		if (dev->transport->get_device_type(dev) != TYPE_ROM) {
2584			/* MAINTENANCE_IN from SCC-2 */
2585			/*
2586			 * Check for emulated MI_REPORT_TARGET_PGS.
2587			 */
2588			if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS &&
2589			    su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2590				cmd->execute_cmd =
2591					target_emulate_report_target_port_groups;
 
 
2592			}
2593			size = (cdb[6] << 24) | (cdb[7] << 16) |
2594			       (cdb[8] << 8) | cdb[9];
2595		} else {
2596			/* GPCMD_SEND_KEY from multi media commands */
2597			size = (cdb[8] << 8) + cdb[9];
2598		}
2599		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2600		break;
2601	case MODE_SELECT:
2602		size = cdb[4];
2603		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2604		break;
2605	case MODE_SELECT_10:
2606		size = (cdb[7] << 8) + cdb[8];
2607		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2608		break;
2609	case MODE_SENSE:
2610		size = cdb[4];
2611		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2612		if (!passthrough)
2613			cmd->execute_cmd = target_emulate_modesense;
2614		break;
2615	case MODE_SENSE_10:
2616		size = (cdb[7] << 8) + cdb[8];
2617		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2618		if (!passthrough)
2619			cmd->execute_cmd = target_emulate_modesense;
2620		break;
2621	case GPCMD_READ_BUFFER_CAPACITY:
2622	case GPCMD_SEND_OPC:
2623	case LOG_SELECT:
2624	case LOG_SENSE:
2625		size = (cdb[7] << 8) + cdb[8];
2626		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2627		break;
2628	case READ_BLOCK_LIMITS:
2629		size = READ_BLOCK_LEN;
2630		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2631		break;
2632	case GPCMD_GET_CONFIGURATION:
2633	case GPCMD_READ_FORMAT_CAPACITIES:
2634	case GPCMD_READ_DISC_INFO:
2635	case GPCMD_READ_TRACK_RZONE_INFO:
2636		size = (cdb[7] << 8) + cdb[8];
2637		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2638		break;
2639	case PERSISTENT_RESERVE_IN:
2640		if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
2641			cmd->execute_cmd = target_scsi3_emulate_pr_in;
2642		size = (cdb[7] << 8) + cdb[8];
2643		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2644		break;
2645	case PERSISTENT_RESERVE_OUT:
2646		if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
2647			cmd->execute_cmd = target_scsi3_emulate_pr_out;
 
 
2648		size = (cdb[7] << 8) + cdb[8];
2649		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2650		break;
2651	case GPCMD_MECHANISM_STATUS:
2652	case GPCMD_READ_DVD_STRUCTURE:
2653		size = (cdb[8] << 8) + cdb[9];
2654		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2655		break;
2656	case READ_POSITION:
2657		size = READ_POSITION_LEN;
2658		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2659		break;
2660	case MAINTENANCE_OUT:
2661		if (dev->transport->get_device_type(dev) != TYPE_ROM) {
2662			/* MAINTENANCE_OUT from SCC-2
2663			 *
2664			 * Check for emulated MO_SET_TARGET_PGS.
2665			 */
2666			if (cdb[1] == MO_SET_TARGET_PGS &&
2667			    su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2668				cmd->execute_cmd =
2669					target_emulate_set_target_port_groups;
 
 
2670			}
2671
2672			size = (cdb[6] << 24) | (cdb[7] << 16) |
2673			       (cdb[8] << 8) | cdb[9];
2674		} else  {
2675			/* GPCMD_REPORT_KEY from multi media commands */
2676			size = (cdb[8] << 8) + cdb[9];
2677		}
2678		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2679		break;
2680	case INQUIRY:
2681		size = (cdb[3] << 8) + cdb[4];
2682		/*
2683		 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2684		 * See spc4r17 section 5.3
2685		 */
2686		if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2687			cmd->sam_task_attr = MSG_HEAD_TAG;
2688		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2689		if (!passthrough)
2690			cmd->execute_cmd = target_emulate_inquiry;
2691		break;
2692	case READ_BUFFER:
2693		size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2694		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2695		break;
2696	case READ_CAPACITY:
2697		size = READ_CAP_LEN;
2698		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2699		if (!passthrough)
2700			cmd->execute_cmd = target_emulate_readcapacity;
2701		break;
2702	case READ_MEDIA_SERIAL_NUMBER:
2703	case SECURITY_PROTOCOL_IN:
2704	case SECURITY_PROTOCOL_OUT:
2705		size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2706		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2707		break;
2708	case SERVICE_ACTION_IN:
2709		switch (cmd->t_task_cdb[1] & 0x1f) {
2710		case SAI_READ_CAPACITY_16:
2711			if (!passthrough)
2712				cmd->execute_cmd =
2713					target_emulate_readcapacity_16;
2714			break;
2715		default:
2716			if (passthrough)
2717				break;
2718
2719			pr_err("Unsupported SA: 0x%02x\n",
2720				cmd->t_task_cdb[1] & 0x1f);
2721			goto out_invalid_cdb_field;
2722		}
2723		/*FALLTHROUGH*/
2724	case ACCESS_CONTROL_IN:
2725	case ACCESS_CONTROL_OUT:
2726	case EXTENDED_COPY:
2727	case READ_ATTRIBUTE:
2728	case RECEIVE_COPY_RESULTS:
2729	case WRITE_ATTRIBUTE:
2730		size = (cdb[10] << 24) | (cdb[11] << 16) |
2731		       (cdb[12] << 8) | cdb[13];
2732		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2733		break;
2734	case RECEIVE_DIAGNOSTIC:
2735	case SEND_DIAGNOSTIC:
2736		size = (cdb[3] << 8) | cdb[4];
2737		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2738		break;
2739/* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2740#if 0
2741	case GPCMD_READ_CD:
2742		sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2743		size = (2336 * sectors);
2744		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2745		break;
2746#endif
2747	case READ_TOC:
2748		size = cdb[8];
2749		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2750		break;
2751	case REQUEST_SENSE:
2752		size = cdb[4];
2753		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2754		if (!passthrough)
2755			cmd->execute_cmd = target_emulate_request_sense;
2756		break;
2757	case READ_ELEMENT_STATUS:
2758		size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
2759		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2760		break;
2761	case WRITE_BUFFER:
2762		size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2763		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2764		break;
2765	case RESERVE:
2766	case RESERVE_10:
2767		/*
2768		 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2769		 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2770		 */
2771		if (cdb[0] == RESERVE_10)
2772			size = (cdb[7] << 8) | cdb[8];
2773		else
2774			size = cmd->data_length;
2775
2776		/*
2777		 * Setup the legacy emulated handler for SPC-2 and
2778		 * >= SPC-3 compatible reservation handling (CRH=1)
2779		 * Otherwise, we assume the underlying SCSI logic is
2780		 * is running in SPC_PASSTHROUGH, and wants reservations
2781		 * emulation disabled.
2782		 */
2783		if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2784			cmd->execute_cmd = target_scsi2_reservation_reserve;
 
 
2785		cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2786		break;
2787	case RELEASE:
2788	case RELEASE_10:
2789		/*
2790		 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2791		 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2792		*/
2793		if (cdb[0] == RELEASE_10)
2794			size = (cdb[7] << 8) | cdb[8];
2795		else
2796			size = cmd->data_length;
2797
2798		if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2799			cmd->execute_cmd = target_scsi2_reservation_release;
 
 
2800		cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2801		break;
2802	case SYNCHRONIZE_CACHE:
2803	case SYNCHRONIZE_CACHE_16:
2804		/*
2805		 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2806		 */
2807		if (cdb[0] == SYNCHRONIZE_CACHE) {
2808			sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2809			cmd->t_task_lba = transport_lba_32(cdb);
2810		} else {
2811			sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2812			cmd->t_task_lba = transport_lba_64(cdb);
2813		}
2814		if (sector_ret)
2815			goto out_unsupported_cdb;
2816
2817		size = transport_get_size(sectors, cdb, cmd);
2818		cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2819
2820		if (passthrough)
 
 
 
2821			break;
2822
 
 
 
 
2823		/*
2824		 * Check to ensure that LBA + Range does not exceed past end of
2825		 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
2826		 */
2827		if ((cmd->t_task_lba != 0) || (sectors != 0)) {
2828			if (transport_cmd_get_valid_sectors(cmd) < 0)
2829				goto out_invalid_cdb_field;
2830		}
2831		cmd->execute_cmd = target_emulate_synchronize_cache;
2832		break;
2833	case UNMAP:
2834		size = get_unaligned_be16(&cdb[7]);
2835		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2836		if (!passthrough)
2837			cmd->execute_cmd = target_emulate_unmap;
2838		break;
2839	case WRITE_SAME_16:
2840		sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2841		if (sector_ret)
2842			goto out_unsupported_cdb;
2843
2844		if (sectors)
2845			size = transport_get_size(1, cdb, cmd);
2846		else {
2847			pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2848			goto out_invalid_cdb_field;
2849		}
2850
2851		cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
2852		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2853
2854		if (target_check_write_same_discard(&cdb[1], dev) < 0)
2855			goto out_unsupported_cdb;
2856		if (!passthrough)
2857			cmd->execute_cmd = target_emulate_write_same;
2858		break;
2859	case WRITE_SAME:
2860		sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2861		if (sector_ret)
2862			goto out_unsupported_cdb;
2863
2864		if (sectors)
2865			size = transport_get_size(1, cdb, cmd);
2866		else {
2867			pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2868			goto out_invalid_cdb_field;
2869		}
2870
2871		cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
2872		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2873		/*
2874		 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
2875		 * of byte 1 bit 3 UNMAP instead of original reserved field
2876		 */
2877		if (target_check_write_same_discard(&cdb[1], dev) < 0)
2878			goto out_unsupported_cdb;
2879		if (!passthrough)
2880			cmd->execute_cmd = target_emulate_write_same;
2881		break;
2882	case ALLOW_MEDIUM_REMOVAL:
 
2883	case ERASE:
 
 
2884	case REZERO_UNIT:
2885	case SEEK_10:
 
2886	case SPACE:
2887	case START_STOP:
2888	case TEST_UNIT_READY:
2889	case VERIFY:
2890	case WRITE_FILEMARKS:
2891		cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2892		if (!passthrough)
2893			cmd->execute_cmd = target_emulate_noop;
2894		break;
2895	case GPCMD_CLOSE_TRACK:
2896	case INITIALIZE_ELEMENT_STATUS:
2897	case GPCMD_LOAD_UNLOAD:
2898	case GPCMD_SET_SPEED:
2899	case MOVE_MEDIUM:
2900		cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2901		break;
2902	case REPORT_LUNS:
2903		cmd->execute_cmd = target_report_luns;
 
2904		size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2905		/*
2906		 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
2907		 * See spc4r17 section 5.3
2908		 */
2909		if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2910			cmd->sam_task_attr = MSG_HEAD_TAG;
2911		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2912		break;
2913	case GET_EVENT_STATUS_NOTIFICATION:
2914		size = (cdb[7] << 8) | cdb[8];
2915		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2916		break;
2917	case ATA_16:
2918		/* Only support ATA passthrough to pSCSI backends.. */
2919		if (!passthrough)
2920			goto out_unsupported_cdb;
2921
2922		/* T_LENGTH */
2923		switch (cdb[2] & 0x3) {
2924		case 0x0:
2925			sectors = 0;
2926			break;
2927		case 0x1:
2928			sectors = (((cdb[1] & 0x1) ? cdb[3] : 0) << 8) | cdb[4];
2929			break;
2930		case 0x2:
2931			sectors = (((cdb[1] & 0x1) ? cdb[5] : 0) << 8) | cdb[6];
2932			break;
2933		case 0x3:
2934			pr_err("T_LENGTH=0x3 not supported for ATA_16\n");
2935			goto out_invalid_cdb_field;
2936		}
2937
2938		/* BYTE_BLOCK */
2939		if (cdb[2] & 0x4) {
2940			/* BLOCK T_TYPE: 512 or sector */
2941			size = sectors * ((cdb[2] & 0x10) ?
2942				dev->se_sub_dev->se_dev_attrib.block_size : 512);
2943		} else {
2944			/* BYTE */
2945			size = sectors;
2946		}
2947		cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2948		break;
2949	default:
2950		pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
2951			" 0x%02x, sending CHECK_CONDITION.\n",
2952			cmd->se_tfo->get_fabric_name(), cdb[0]);
 
2953		goto out_unsupported_cdb;
2954	}
2955
2956	if (cmd->unknown_data_length)
2957		cmd->data_length = size;
2958
2959	if (size != cmd->data_length) {
2960		pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
2961			" %u does not match SCSI CDB Length: %u for SAM Opcode:"
2962			" 0x%02x\n", cmd->se_tfo->get_fabric_name(),
2963				cmd->data_length, size, cdb[0]);
2964
2965		cmd->cmd_spdtl = size;
2966
2967		if (cmd->data_direction == DMA_TO_DEVICE) {
2968			pr_err("Rejecting underflow/overflow"
2969					" WRITE data\n");
2970			goto out_invalid_cdb_field;
2971		}
2972		/*
2973		 * Reject READ_* or WRITE_* with overflow/underflow for
2974		 * type SCF_SCSI_DATA_SG_IO_CDB.
2975		 */
2976		if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512))  {
2977			pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
2978				" CDB on non 512-byte sector setup subsystem"
2979				" plugin: %s\n", dev->transport->name);
2980			/* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
2981			goto out_invalid_cdb_field;
2982		}
2983		/*
2984		 * For the overflow case keep the existing fabric provided
2985		 * ->data_length.  Otherwise for the underflow case, reset
2986		 * ->data_length to the smaller SCSI expected data transfer
2987		 * length.
2988		 */
2989		if (size > cmd->data_length) {
2990			cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
2991			cmd->residual_count = (size - cmd->data_length);
2992		} else {
2993			cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
2994			cmd->residual_count = (cmd->data_length - size);
2995			cmd->data_length = size;
2996		}
 
2997	}
2998
2999	if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3000		if (sectors > su_dev->se_dev_attrib.fabric_max_sectors) {
3001			printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
3002				" big sectors %u exceeds fabric_max_sectors:"
3003				" %u\n", cdb[0], sectors,
3004				su_dev->se_dev_attrib.fabric_max_sectors);
3005			goto out_invalid_cdb_field;
3006		}
3007		if (sectors > su_dev->se_dev_attrib.hw_max_sectors) {
3008			printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
3009				" big sectors %u exceeds backend hw_max_sectors:"
3010				" %u\n", cdb[0], sectors,
3011				su_dev->se_dev_attrib.hw_max_sectors);
3012			goto out_invalid_cdb_field;
3013		}
3014	}
3015
3016	/* reject any command that we don't have a handler for */
3017	if (!(passthrough || cmd->execute_cmd ||
3018	     (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
3019		goto out_unsupported_cdb;
3020
3021	transport_set_supported_SAM_opcode(cmd);
3022	return ret;
3023
3024out_unsupported_cdb:
3025	cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3026	cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
3027	return -EINVAL;
3028out_invalid_cdb_field:
3029	cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3030	cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3031	return -EINVAL;
3032}
3033
3034/*
3035 * Called from I/O completion to determine which dormant/delayed
 
3036 * and ordered cmds need to have their tasks added to the execution queue.
3037 */
3038static void transport_complete_task_attr(struct se_cmd *cmd)
3039{
3040	struct se_device *dev = cmd->se_dev;
3041	struct se_cmd *cmd_p, *cmd_tmp;
3042	int new_active_tasks = 0;
3043
3044	if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
3045		atomic_dec(&dev->simple_cmds);
3046		smp_mb__after_atomic_dec();
3047		dev->dev_cur_ordered_id++;
3048		pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
3049			" SIMPLE: %u\n", dev->dev_cur_ordered_id,
3050			cmd->se_ordered_id);
3051	} else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
 
 
3052		dev->dev_cur_ordered_id++;
3053		pr_debug("Incremented dev_cur_ordered_id: %u for"
3054			" HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3055			cmd->se_ordered_id);
3056	} else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
 
 
3057		atomic_dec(&dev->dev_ordered_sync);
3058		smp_mb__after_atomic_dec();
 
3059
3060		dev->dev_cur_ordered_id++;
3061		pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
3062			" %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3063	}
3064	/*
3065	 * Process all commands up to the last received
3066	 * ORDERED task attribute which requires another blocking
3067	 * boundary
3068	 */
3069	spin_lock(&dev->delayed_cmd_lock);
3070	list_for_each_entry_safe(cmd_p, cmd_tmp,
3071			&dev->delayed_cmd_list, se_delayed_node) {
3072
3073		list_del(&cmd_p->se_delayed_node);
3074		spin_unlock(&dev->delayed_cmd_lock);
3075
3076		pr_debug("Calling add_tasks() for"
3077			" cmd_p: 0x%02x Task Attr: 0x%02x"
3078			" Dormant -> Active, se_ordered_id: %u\n",
3079			cmd_p->t_task_cdb[0],
3080			cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3081
3082		target_add_to_execute_list(cmd_p);
3083		new_active_tasks++;
3084
3085		spin_lock(&dev->delayed_cmd_lock);
3086		if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
3087			break;
3088	}
3089	spin_unlock(&dev->delayed_cmd_lock);
3090	/*
3091	 * If new tasks have become active, wake up the transport thread
3092	 * to do the processing of the Active tasks.
3093	 */
3094	if (new_active_tasks != 0)
3095		wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
3096}
3097
3098static void transport_complete_qf(struct se_cmd *cmd)
3099{
3100	int ret = 0;
3101
3102	if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3103		transport_complete_task_attr(cmd);
3104
3105	if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3106		ret = cmd->se_tfo->queue_status(cmd);
3107		if (ret)
3108			goto out;
3109	}
3110
3111	switch (cmd->data_direction) {
3112	case DMA_FROM_DEVICE:
3113		ret = cmd->se_tfo->queue_data_in(cmd);
3114		break;
3115	case DMA_TO_DEVICE:
3116		if (cmd->t_bidi_data_sg) {
3117			ret = cmd->se_tfo->queue_data_in(cmd);
3118			if (ret < 0)
3119				break;
3120		}
3121		/* Fall through for DMA_TO_DEVICE */
3122	case DMA_NONE:
3123		ret = cmd->se_tfo->queue_status(cmd);
3124		break;
3125	default:
3126		break;
3127	}
3128
3129out:
3130	if (ret < 0) {
3131		transport_handle_queue_full(cmd, cmd->se_dev);
3132		return;
3133	}
3134	transport_lun_remove_cmd(cmd);
3135	transport_cmd_check_stop_to_fabric(cmd);
3136}
3137
3138static void transport_handle_queue_full(
3139	struct se_cmd *cmd,
3140	struct se_device *dev)
 
3141{
3142	spin_lock_irq(&dev->qf_cmd_lock);
 
 
3143	list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3144	atomic_inc(&dev->dev_qf_count);
3145	smp_mb__after_atomic_inc();
3146	spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3147
3148	schedule_work(&cmd->se_dev->qf_work_queue);
3149}
3150
3151static void target_complete_ok_work(struct work_struct *work)
3152{
3153	struct se_cmd *cmd = container_of(work, struct se_cmd, work);
3154	int ret;
3155
3156	/*
3157	 * Check if we need to move delayed/dormant tasks from cmds on the
3158	 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3159	 * Attribute.
3160	 */
3161	if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3162		transport_complete_task_attr(cmd);
3163	/*
3164	 * Check to schedule QUEUE_FULL work, or execute an existing
3165	 * cmd->transport_qf_callback()
3166	 */
3167	if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3168		schedule_work(&cmd->se_dev->qf_work_queue);
3169
 
 
 
 
 
 
 
 
3170	/*
3171	 * Check if we need to retrieve a sense buffer from
3172	 * the struct se_cmd in question.
3173	 */
3174	if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3175		WARN_ON(!cmd->scsi_status);
3176		transport_get_sense_data(cmd);
3177		ret = transport_send_check_condition_and_sense(
3178					cmd, 0, 1);
3179		if (ret == -EAGAIN || ret == -ENOMEM)
3180			goto queue_full;
 
 
 
 
 
 
3181
3182		transport_lun_remove_cmd(cmd);
3183		transport_cmd_check_stop_to_fabric(cmd);
3184		return;
 
3185	}
3186	/*
3187	 * Check for a callback, used by amongst other things
3188	 * XDWRITE_READ_10 emulation.
3189	 */
3190	if (cmd->transport_complete_callback)
3191		cmd->transport_complete_callback(cmd);
3192
3193	switch (cmd->data_direction) {
3194	case DMA_FROM_DEVICE:
3195		spin_lock(&cmd->se_lun->lun_sep_lock);
3196		if (cmd->se_lun->lun_sep) {
3197			cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3198					cmd->data_length;
3199		}
3200		spin_unlock(&cmd->se_lun->lun_sep_lock);
3201
3202		ret = cmd->se_tfo->queue_data_in(cmd);
3203		if (ret == -EAGAIN || ret == -ENOMEM)
3204			goto queue_full;
3205		break;
3206	case DMA_TO_DEVICE:
3207		spin_lock(&cmd->se_lun->lun_sep_lock);
3208		if (cmd->se_lun->lun_sep) {
3209			cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
3210				cmd->data_length;
3211		}
3212		spin_unlock(&cmd->se_lun->lun_sep_lock);
3213		/*
3214		 * Check if we need to send READ payload for BIDI-COMMAND
3215		 */
3216		if (cmd->t_bidi_data_sg) {
3217			spin_lock(&cmd->se_lun->lun_sep_lock);
3218			if (cmd->se_lun->lun_sep) {
3219				cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3220					cmd->data_length;
3221			}
3222			spin_unlock(&cmd->se_lun->lun_sep_lock);
3223			ret = cmd->se_tfo->queue_data_in(cmd);
3224			if (ret == -EAGAIN || ret == -ENOMEM)
3225				goto queue_full;
3226			break;
3227		}
3228		/* Fall through for DMA_TO_DEVICE */
3229	case DMA_NONE:
3230		ret = cmd->se_tfo->queue_status(cmd);
3231		if (ret == -EAGAIN || ret == -ENOMEM)
3232			goto queue_full;
3233		break;
3234	default:
3235		break;
3236	}
3237
 
3238	transport_lun_remove_cmd(cmd);
3239	transport_cmd_check_stop_to_fabric(cmd);
3240	return;
3241
3242queue_full:
3243	pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
3244		" data_direction: %d\n", cmd, cmd->data_direction);
3245	cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3246	transport_handle_queue_full(cmd, cmd->se_dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3247}
3248
3249static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
3250{
3251	struct scatterlist *sg;
3252	int count;
3253
3254	for_each_sg(sgl, sg, nents, count)
3255		__free_page(sg_page(sg));
3256
3257	kfree(sgl);
3258}
3259
3260static inline void transport_free_pages(struct se_cmd *cmd)
3261{
3262	if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
3263		return;
3264
3265	transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
3266	cmd->t_data_sg = NULL;
3267	cmd->t_data_nents = 0;
3268
3269	transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
3270	cmd->t_bidi_data_sg = NULL;
3271	cmd->t_bidi_data_nents = 0;
3272}
3273
3274/**
3275 * transport_release_cmd - free a command
3276 * @cmd:       command to free
3277 *
3278 * This routine unconditionally frees a command, and reference counting
3279 * or list removal must be done in the caller.
3280 */
3281static void transport_release_cmd(struct se_cmd *cmd)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3282{
3283	BUG_ON(!cmd->se_tfo);
3284
3285	if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
3286		core_tmr_release_req(cmd->se_tmr_req);
3287	if (cmd->t_task_cdb != cmd->__t_task_cdb)
3288		kfree(cmd->t_task_cdb);
3289	/*
3290	 * If this cmd has been setup with target_get_sess_cmd(), drop
3291	 * the kref and call ->release_cmd() in kref callback.
3292	 */
3293	 if (cmd->check_release != 0) {
3294		target_put_sess_cmd(cmd->se_sess, cmd);
3295		return;
 
 
 
 
 
3296	}
 
 
 
 
 
 
 
 
3297	cmd->se_tfo->release_cmd(cmd);
3298}
3299
3300/**
3301 * transport_put_cmd - release a reference to a command
3302 * @cmd:       command to release
3303 *
3304 * This routine releases our reference to the command and frees it if possible.
3305 */
3306static void transport_put_cmd(struct se_cmd *cmd)
3307{
3308	unsigned long flags;
3309
3310	spin_lock_irqsave(&cmd->t_state_lock, flags);
3311	if (atomic_read(&cmd->t_fe_count)) {
3312		if (!atomic_dec_and_test(&cmd->t_fe_count))
3313			goto out_busy;
 
 
 
 
3314	}
3315
3316	if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
3317		cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
3318		target_remove_from_state_list(cmd);
 
3319	}
 
 
3320	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3321
 
 
 
3322	transport_free_pages(cmd);
3323	transport_release_cmd(cmd);
3324	return;
3325out_busy:
3326	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3327}
3328
3329/*
3330 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3331 * allocating in the core.
3332 * @cmd:  Associated se_cmd descriptor
3333 * @mem:  SGL style memory for TCM WRITE / READ
3334 * @sg_mem_num: Number of SGL elements
3335 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3336 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3337 *
3338 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3339 * of parameters.
3340 */
3341int transport_generic_map_mem_to_cmd(
3342	struct se_cmd *cmd,
3343	struct scatterlist *sgl,
3344	u32 sgl_count,
3345	struct scatterlist *sgl_bidi,
3346	u32 sgl_bidi_count)
3347{
3348	if (!sgl || !sgl_count)
3349		return 0;
3350
3351	if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3352	    (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
3353		/*
3354		 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
3355		 * scatterlists already have been set to follow what the fabric
3356		 * passes for the original expected data transfer length.
3357		 */
3358		if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
3359			pr_warn("Rejecting SCSI DATA overflow for fabric using"
3360				" SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
3361			cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3362			cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3363			return -EINVAL;
3364		}
3365
3366		cmd->t_data_sg = sgl;
3367		cmd->t_data_nents = sgl_count;
3368
3369		if (sgl_bidi && sgl_bidi_count) {
3370			cmd->t_bidi_data_sg = sgl_bidi;
3371			cmd->t_bidi_data_nents = sgl_bidi_count;
3372		}
3373		cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
3374	}
3375
3376	return 0;
3377}
3378EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3379
3380void *transport_kmap_data_sg(struct se_cmd *cmd)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3381{
3382	struct scatterlist *sg = cmd->t_data_sg;
3383	struct page **pages;
3384	int i;
3385
3386	BUG_ON(!sg);
3387	/*
3388	 * We need to take into account a possible offset here for fabrics like
3389	 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3390	 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
3391	 */
3392	if (!cmd->t_data_nents)
3393		return NULL;
3394	else if (cmd->t_data_nents == 1)
3395		return kmap(sg_page(sg)) + sg->offset;
3396
3397	/* >1 page. use vmap */
3398	pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
3399	if (!pages)
3400		return NULL;
3401
3402	/* convert sg[] to pages[] */
3403	for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
3404		pages[i] = sg_page(sg);
3405	}
3406
3407	cmd->t_data_vmap = vmap(pages, cmd->t_data_nents,  VM_MAP, PAGE_KERNEL);
3408	kfree(pages);
3409	if (!cmd->t_data_vmap)
3410		return NULL;
3411
3412	return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
3413}
3414EXPORT_SYMBOL(transport_kmap_data_sg);
3415
3416void transport_kunmap_data_sg(struct se_cmd *cmd)
3417{
3418	if (!cmd->t_data_nents) {
3419		return;
3420	} else if (cmd->t_data_nents == 1) {
3421		kunmap(sg_page(cmd->t_data_sg));
3422		return;
3423	}
3424
3425	vunmap(cmd->t_data_vmap);
3426	cmd->t_data_vmap = NULL;
3427}
3428EXPORT_SYMBOL(transport_kunmap_data_sg);
3429
3430static int
3431transport_generic_get_mem(struct se_cmd *cmd)
3432{
3433	u32 length = cmd->data_length;
3434	unsigned int nents;
3435	struct page *page;
3436	gfp_t zero_flag;
3437	int i = 0;
3438
3439	nents = DIV_ROUND_UP(length, PAGE_SIZE);
3440	cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3441	if (!cmd->t_data_sg)
3442		return -ENOMEM;
3443
3444	cmd->t_data_nents = nents;
3445	sg_init_table(cmd->t_data_sg, nents);
3446
3447	zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
3448
3449	while (length) {
3450		u32 page_len = min_t(u32, length, PAGE_SIZE);
3451		page = alloc_page(GFP_KERNEL | zero_flag);
3452		if (!page)
3453			goto out;
3454
3455		sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3456		length -= page_len;
3457		i++;
3458	}
3459	return 0;
3460
3461out:
3462	while (i > 0) {
 
3463		i--;
3464		__free_page(sg_page(&cmd->t_data_sg[i]));
3465	}
3466	kfree(cmd->t_data_sg);
3467	cmd->t_data_sg = NULL;
3468	return -ENOMEM;
3469}
3470
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3471/*
3472 * Allocate any required resources to execute the command.  For writes we
3473 * might not have the payload yet, so notify the fabric via a call to
3474 * ->write_pending instead. Otherwise place it on the execution queue.
3475 */
3476int transport_generic_new_cmd(struct se_cmd *cmd)
 
 
 
 
 
3477{
 
 
3478	struct se_device *dev = cmd->se_dev;
3479	int ret = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3480
 
 
 
 
3481	/*
3482	 * Determine is the TCM fabric module has already allocated physical
3483	 * memory, and is directly calling transport_generic_map_mem_to_cmd()
3484	 * beforehand.
3485	 */
3486	if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3487	    cmd->data_length) {
3488		ret = transport_generic_get_mem(cmd);
 
 
 
 
3489		if (ret < 0)
3490			goto out_fail;
3491	}
3492
3493	/* Workaround for handling zero-length control CDBs */
3494	if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3495	    !cmd->data_length) {
3496		spin_lock_irq(&cmd->t_state_lock);
3497		cmd->t_state = TRANSPORT_COMPLETE;
3498		cmd->transport_state |= CMD_T_ACTIVE;
3499		spin_unlock_irq(&cmd->t_state_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3500
3501		if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
3502			u8 ua_asc = 0, ua_ascq = 0;
 
 
3503
3504			core_scsi3_ua_clear_for_request_sense(cmd,
3505					&ua_asc, &ua_ascq);
3506		}
3507
3508		INIT_WORK(&cmd->work, target_complete_ok_work);
3509		queue_work(target_completion_wq, &cmd->work);
3510		return 0;
 
 
 
 
 
 
3511	}
3512
 
 
 
 
 
 
 
 
 
 
 
 
 
3513	if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3514		struct se_dev_attrib *attr = &dev->se_sub_dev->se_dev_attrib;
3515
3516		if (transport_cmd_get_valid_sectors(cmd) < 0)
3517			return -EINVAL;
3518
3519		BUG_ON(cmd->data_length % attr->block_size);
3520		BUG_ON(DIV_ROUND_UP(cmd->data_length, attr->block_size) >
3521			attr->hw_max_sectors);
3522	}
 
 
3523
3524	atomic_inc(&cmd->t_fe_count);
3525
 
 
 
 
 
 
3526	/*
3527	 * For WRITEs, let the fabric know its buffer is ready.
3528	 *
3529	 * The command will be added to the execution queue after its write
3530	 * data has arrived.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3531	 */
3532	if (cmd->data_direction == DMA_TO_DEVICE) {
3533		target_add_to_state_list(cmd);
3534		return transport_generic_write_pending(cmd);
3535	}
3536	/*
3537	 * Everything else but a WRITE, add the command to the execution queue.
 
3538	 */
3539	transport_execute_tasks(cmd);
3540	return 0;
3541
3542out_fail:
3543	cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3544	cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3545	return -EINVAL;
3546}
3547EXPORT_SYMBOL(transport_generic_new_cmd);
3548
3549/*	transport_generic_process_write():
3550 *
3551 *
3552 */
3553void transport_generic_process_write(struct se_cmd *cmd)
3554{
3555	transport_execute_tasks(cmd);
3556}
3557EXPORT_SYMBOL(transport_generic_process_write);
3558
3559static void transport_write_pending_qf(struct se_cmd *cmd)
3560{
3561	int ret;
3562
3563	ret = cmd->se_tfo->write_pending(cmd);
3564	if (ret == -EAGAIN || ret == -ENOMEM) {
3565		pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3566			 cmd);
3567		transport_handle_queue_full(cmd, cmd->se_dev);
3568	}
3569}
3570
 
 
 
 
3571static int transport_generic_write_pending(struct se_cmd *cmd)
3572{
3573	unsigned long flags;
3574	int ret;
3575
3576	spin_lock_irqsave(&cmd->t_state_lock, flags);
3577	cmd->t_state = TRANSPORT_WRITE_PENDING;
3578	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3579
 
 
 
 
 
 
 
 
 
 
 
3580	/*
3581	 * Clear the se_cmd for WRITE_PENDING status in order to set
3582	 * CMD_T_ACTIVE so that transport_generic_handle_data can be called
3583	 * from HW target mode interrupt code.  This is safe to be called
3584	 * with transport_off=1 before the cmd->se_tfo->write_pending
3585	 * because the se_cmd->se_lun pointer is not being cleared.
3586	 */
3587	transport_cmd_check_stop(cmd, 1, 0);
3588
3589	/*
3590	 * Call the fabric write_pending function here to let the
3591	 * frontend know that WRITE buffers are ready.
3592	 */
3593	ret = cmd->se_tfo->write_pending(cmd);
3594	if (ret == -EAGAIN || ret == -ENOMEM)
3595		goto queue_full;
3596	else if (ret < 0)
3597		return ret;
3598
3599	return 1;
3600
3601queue_full:
3602	pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
3603	cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
3604	transport_handle_queue_full(cmd, cmd->se_dev);
3605	return 0;
 
3606}
3607
3608void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
3609{
3610	if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
3611		if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
3612			 transport_wait_for_tasks(cmd);
3613
3614		transport_release_cmd(cmd);
3615	} else {
3616		if (wait_for_tasks)
3617			transport_wait_for_tasks(cmd);
3618
3619		core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
3620
3621		if (cmd->se_lun)
3622			transport_lun_remove_cmd(cmd);
3623
3624		transport_put_cmd(cmd);
3625	}
3626}
3627EXPORT_SYMBOL(transport_generic_free_cmd);
3628
3629/* target_get_sess_cmd - Add command to active ->sess_cmd_list
3630 * @se_sess:	session to reference
3631 * @se_cmd:	command descriptor to add
3632 * @ack_kref:	Signal that fabric will perform an ack target_put_sess_cmd()
3633 */
3634void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
3635			bool ack_kref)
 
 
3636{
3637	unsigned long flags;
 
 
 
3638
3639	kref_init(&se_cmd->cmd_kref);
3640	/*
3641	 * Add a second kref if the fabric caller is expecting to handle
3642	 * fabric acknowledgement that requires two target_put_sess_cmd()
3643	 * invocations before se_cmd descriptor release.
3644	 */
3645	if (ack_kref == true) {
3646		kref_get(&se_cmd->cmd_kref);
3647		se_cmd->se_cmd_flags |= SCF_ACK_KREF;
3648	}
3649
3650	spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3651	list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
3652	se_cmd->check_release = 1;
3653	spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3654}
3655EXPORT_SYMBOL(target_get_sess_cmd);
3656
3657static void target_release_cmd_kref(struct kref *kref)
3658{
3659	struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
3660	struct se_session *se_sess = se_cmd->se_sess;
3661	unsigned long flags;
3662
3663	spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3664	if (list_empty(&se_cmd->se_cmd_list)) {
3665		spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3666		se_cmd->se_tfo->release_cmd(se_cmd);
3667		return;
3668	}
3669	if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
3670		spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3671		complete(&se_cmd->cmd_wait_comp);
3672		return;
3673	}
3674	list_del(&se_cmd->se_cmd_list);
3675	spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3676
3677	se_cmd->se_tfo->release_cmd(se_cmd);
3678}
 
3679
3680/* target_put_sess_cmd - Check for active I/O shutdown via kref_put
3681 * @se_sess:	session to reference
3682 * @se_cmd:	command descriptor to drop
3683 */
3684int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
3685{
3686	return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
3687}
3688EXPORT_SYMBOL(target_put_sess_cmd);
3689
3690/* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
3691 * @se_sess:	session to split
3692 */
3693void target_splice_sess_cmd_list(struct se_session *se_sess)
3694{
3695	struct se_cmd *se_cmd;
3696	unsigned long flags;
3697
3698	WARN_ON(!list_empty(&se_sess->sess_wait_list));
3699	INIT_LIST_HEAD(&se_sess->sess_wait_list);
3700
3701	spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3702	se_sess->sess_tearing_down = 1;
3703
3704	list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
3705
3706	list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
3707		se_cmd->cmd_wait_set = 1;
3708
3709	spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3710}
3711EXPORT_SYMBOL(target_splice_sess_cmd_list);
3712
3713/* target_wait_for_sess_cmds - Wait for outstanding descriptors
3714 * @se_sess:    session to wait for active I/O
3715 * @wait_for_tasks:	Make extra transport_wait_for_tasks call
3716 */
3717void target_wait_for_sess_cmds(
3718	struct se_session *se_sess,
3719	int wait_for_tasks)
3720{
3721	struct se_cmd *se_cmd, *tmp_cmd;
3722	bool rc = false;
3723
3724	list_for_each_entry_safe(se_cmd, tmp_cmd,
3725				&se_sess->sess_wait_list, se_cmd_list) {
3726		list_del(&se_cmd->se_cmd_list);
3727
3728		pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
3729			" %d\n", se_cmd, se_cmd->t_state,
3730			se_cmd->se_tfo->get_cmd_state(se_cmd));
3731
3732		if (wait_for_tasks) {
3733			pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
3734				" fabric state: %d\n", se_cmd, se_cmd->t_state,
3735				se_cmd->se_tfo->get_cmd_state(se_cmd));
3736
3737			rc = transport_wait_for_tasks(se_cmd);
3738
3739			pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
3740				" fabric state: %d\n", se_cmd, se_cmd->t_state,
3741				se_cmd->se_tfo->get_cmd_state(se_cmd));
3742		}
3743
3744		if (!rc) {
3745			wait_for_completion(&se_cmd->cmd_wait_comp);
3746			pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
3747				" fabric state: %d\n", se_cmd, se_cmd->t_state,
3748				se_cmd->se_tfo->get_cmd_state(se_cmd));
3749		}
3750
3751		se_cmd->se_tfo->release_cmd(se_cmd);
3752	}
3753}
3754EXPORT_SYMBOL(target_wait_for_sess_cmds);
3755
3756/*	transport_lun_wait_for_tasks():
3757 *
3758 *	Called from ConfigFS context to stop the passed struct se_cmd to allow
3759 *	an struct se_lun to be successfully shutdown.
3760 */
3761static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
3762{
3763	unsigned long flags;
3764	int ret = 0;
3765
3766	/*
3767	 * If the frontend has already requested this struct se_cmd to
3768	 * be stopped, we can safely ignore this struct se_cmd.
3769	 */
3770	spin_lock_irqsave(&cmd->t_state_lock, flags);
3771	if (cmd->transport_state & CMD_T_STOP) {
3772		cmd->transport_state &= ~CMD_T_LUN_STOP;
3773
3774		pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
3775			 cmd->se_tfo->get_task_tag(cmd));
3776		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3777		transport_cmd_check_stop(cmd, 1, 0);
3778		return -EPERM;
3779	}
3780	cmd->transport_state |= CMD_T_LUN_FE_STOP;
3781	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3782
3783	wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
3784
3785	// XXX: audit task_flags checks.
3786	spin_lock_irqsave(&cmd->t_state_lock, flags);
3787	if ((cmd->transport_state & CMD_T_BUSY) &&
3788	    (cmd->transport_state & CMD_T_SENT)) {
3789		if (!target_stop_cmd(cmd, &flags))
3790			ret++;
3791		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3792	} else {
3793		spin_unlock_irqrestore(&cmd->t_state_lock,
3794				flags);
3795		target_remove_from_execute_list(cmd);
3796	}
3797
3798	pr_debug("ConfigFS: cmd: %p stop tasks ret:"
3799			" %d\n", cmd, ret);
3800	if (!ret) {
3801		pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
3802				cmd->se_tfo->get_task_tag(cmd));
3803		wait_for_completion(&cmd->transport_lun_stop_comp);
3804		pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
3805				cmd->se_tfo->get_task_tag(cmd));
3806	}
3807	transport_remove_cmd_from_queue(cmd);
3808
3809	return 0;
3810}
3811
3812static void __transport_clear_lun_from_sessions(struct se_lun *lun)
3813{
3814	struct se_cmd *cmd = NULL;
3815	unsigned long lun_flags, cmd_flags;
3816	/*
3817	 * Do exception processing and return CHECK_CONDITION status to the
3818	 * Initiator Port.
3819	 */
3820	spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
3821	while (!list_empty(&lun->lun_cmd_list)) {
3822		cmd = list_first_entry(&lun->lun_cmd_list,
3823		       struct se_cmd, se_lun_node);
3824		list_del_init(&cmd->se_lun_node);
3825
 
3826		/*
3827		 * This will notify iscsi_target_transport.c:
3828		 * transport_cmd_check_stop() that a LUN shutdown is in
3829		 * progress for the iscsi_cmd_t.
3830		 */
3831		spin_lock(&cmd->t_state_lock);
3832		pr_debug("SE_LUN[%d] - Setting cmd->transport"
3833			"_lun_stop for  ITT: 0x%08x\n",
3834			cmd->se_lun->unpacked_lun,
3835			cmd->se_tfo->get_task_tag(cmd));
3836		cmd->transport_state |= CMD_T_LUN_STOP;
3837		spin_unlock(&cmd->t_state_lock);
3838
3839		spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
3840
3841		if (!cmd->se_lun) {
3842			pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
3843				cmd->se_tfo->get_task_tag(cmd),
3844				cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
3845			BUG();
3846		}
3847		/*
3848		 * If the Storage engine still owns the iscsi_cmd_t, determine
3849		 * and/or stop its context.
3850		 */
3851		pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
3852			"_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
3853			cmd->se_tfo->get_task_tag(cmd));
3854
3855		if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
3856			spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
3857			continue;
3858		}
3859
3860		pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
3861			"_wait_for_tasks(): SUCCESS\n",
3862			cmd->se_lun->unpacked_lun,
3863			cmd->se_tfo->get_task_tag(cmd));
3864
3865		spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
3866		if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
3867			spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
3868			goto check_cond;
3869		}
3870		cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
3871		target_remove_from_state_list(cmd);
3872		spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
3873
 
3874		/*
3875		 * The Storage engine stopped this struct se_cmd before it was
3876		 * send to the fabric frontend for delivery back to the
3877		 * Initiator Node.  Return this SCSI CDB back with an
3878		 * CHECK_CONDITION status.
3879		 */
3880check_cond:
3881		transport_send_check_condition_and_sense(cmd,
3882				TCM_NON_EXISTENT_LUN, 0);
3883		/*
3884		 *  If the fabric frontend is waiting for this iscsi_cmd_t to
3885		 * be released, notify the waiting thread now that LU has
3886		 * finished accessing it.
3887		 */
3888		spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
3889		if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
3890			pr_debug("SE_LUN[%d] - Detected FE stop for"
3891				" struct se_cmd: %p ITT: 0x%08x\n",
3892				lun->unpacked_lun,
3893				cmd, cmd->se_tfo->get_task_tag(cmd));
3894
3895			spin_unlock_irqrestore(&cmd->t_state_lock,
3896					cmd_flags);
3897			transport_cmd_check_stop(cmd, 1, 0);
3898			complete(&cmd->transport_lun_fe_stop_comp);
3899			spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
3900			continue;
3901		}
3902		pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
3903			lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
3904
3905		spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
3906		spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
3907	}
3908	spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
3909}
3910
3911static int transport_clear_lun_thread(void *p)
3912{
3913	struct se_lun *lun = p;
3914
3915	__transport_clear_lun_from_sessions(lun);
3916	complete(&lun->lun_shutdown_comp);
3917
3918	return 0;
3919}
3920
3921int transport_clear_lun_from_sessions(struct se_lun *lun)
3922{
3923	struct task_struct *kt;
3924
3925	kt = kthread_run(transport_clear_lun_thread, lun,
3926			"tcm_cl_%u", lun->unpacked_lun);
3927	if (IS_ERR(kt)) {
3928		pr_err("Unable to start clear_lun thread\n");
3929		return PTR_ERR(kt);
3930	}
3931	wait_for_completion(&lun->lun_shutdown_comp);
3932
3933	return 0;
3934}
3935
3936/**
3937 * transport_wait_for_tasks - wait for completion to occur
3938 * @cmd:	command to wait
3939 *
3940 * Called from frontend fabric context to wait for storage engine
3941 * to pause and/or release frontend generated struct se_cmd.
3942 */
3943bool transport_wait_for_tasks(struct se_cmd *cmd)
 
 
 
3944{
3945	unsigned long flags;
3946
 
 
 
3947	spin_lock_irqsave(&cmd->t_state_lock, flags);
3948	if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
3949	    !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
3950		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3951		return false;
3952	}
3953	/*
3954	 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
3955	 * has been set in transport_set_supported_SAM_opcode().
3956	 */
3957	if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
3958	    !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
3959		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3960		return false;
3961	}
3962	/*
3963	 * If we are already stopped due to an external event (ie: LUN shutdown)
3964	 * sleep until the connection can have the passed struct se_cmd back.
3965	 * The cmd->transport_lun_stopped_sem will be upped by
3966	 * transport_clear_lun_from_sessions() once the ConfigFS context caller
3967	 * has completed its operation on the struct se_cmd.
3968	 */
3969	if (cmd->transport_state & CMD_T_LUN_STOP) {
 
3970		pr_debug("wait_for_tasks: Stopping"
3971			" wait_for_completion(&cmd->t_tasktransport_lun_fe"
3972			"_stop_comp); for ITT: 0x%08x\n",
3973			cmd->se_tfo->get_task_tag(cmd));
3974		/*
3975		 * There is a special case for WRITES where a FE exception +
3976		 * LUN shutdown means ConfigFS context is still sleeping on
3977		 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
3978		 * We go ahead and up transport_lun_stop_comp just to be sure
3979		 * here.
3980		 */
3981		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3982		complete(&cmd->transport_lun_stop_comp);
3983		wait_for_completion(&cmd->transport_lun_fe_stop_comp);
3984		spin_lock_irqsave(&cmd->t_state_lock, flags);
3985
3986		target_remove_from_state_list(cmd);
3987		/*
3988		 * At this point, the frontend who was the originator of this
3989		 * struct se_cmd, now owns the structure and can be released through
3990		 * normal means below.
3991		 */
3992		pr_debug("wait_for_tasks: Stopped"
3993			" wait_for_completion(&cmd->t_tasktransport_lun_fe_"
3994			"stop_comp); for ITT: 0x%08x\n",
3995			cmd->se_tfo->get_task_tag(cmd));
3996
3997		cmd->transport_state &= ~CMD_T_LUN_STOP;
3998	}
 
 
 
3999
4000	if (!(cmd->transport_state & CMD_T_ACTIVE)) {
4001		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4002		return false;
4003	}
4004
4005	cmd->transport_state |= CMD_T_STOP;
4006
4007	pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
4008		" i_state: %d, t_state: %d, CMD_T_STOP\n",
4009		cmd, cmd->se_tfo->get_task_tag(cmd),
4010		cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
 
4011
4012	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4013
4014	wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4015
4016	wait_for_completion(&cmd->t_transport_stop_comp);
4017
4018	spin_lock_irqsave(&cmd->t_state_lock, flags);
4019	cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
 
4020
4021	pr_debug("wait_for_tasks: Stopped wait_for_compltion("
4022		"&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
4023		cmd->se_tfo->get_task_tag(cmd));
4024
4025	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
 
 
4026
4027	return true;
4028}
4029EXPORT_SYMBOL(transport_wait_for_tasks);
4030
4031static int transport_get_sense_codes(
4032	struct se_cmd *cmd,
4033	u8 *asc,
4034	u8 *ascq)
4035{
4036	*asc = cmd->scsi_asc;
4037	*ascq = cmd->scsi_ascq;
4038
4039	return 0;
4040}
4041
4042static int transport_set_sense_codes(
4043	struct se_cmd *cmd,
4044	u8 asc,
4045	u8 ascq)
4046{
4047	cmd->scsi_asc = asc;
4048	cmd->scsi_ascq = ascq;
4049
4050	return 0;
4051}
4052
4053int transport_send_check_condition_and_sense(
4054	struct se_cmd *cmd,
4055	u8 reason,
4056	int from_transport)
4057{
4058	unsigned char *buffer = cmd->sense_buffer;
4059	unsigned long flags;
4060	int offset;
4061	u8 asc = 0, ascq = 0;
4062
4063	spin_lock_irqsave(&cmd->t_state_lock, flags);
4064	if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4065		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4066		return 0;
4067	}
4068	cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
4069	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4070
4071	if (!reason && from_transport)
4072		goto after_reason;
4073
4074	if (!from_transport)
4075		cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4076	/*
4077	 * Data Segment and SenseLength of the fabric response PDU.
4078	 *
4079	 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4080	 * from include/scsi/scsi_cmnd.h
4081	 */
4082	offset = cmd->se_tfo->set_fabric_sense_len(cmd,
4083				TRANSPORT_SENSE_BUFFER);
4084	/*
4085	 * Actual SENSE DATA, see SPC-3 7.23.2  SPC_SENSE_KEY_OFFSET uses
4086	 * SENSE KEY values from include/scsi/scsi.h
4087	 */
4088	switch (reason) {
4089	case TCM_NON_EXISTENT_LUN:
4090		/* CURRENT ERROR */
4091		buffer[offset] = 0x70;
4092		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4093		/* ILLEGAL REQUEST */
4094		buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4095		/* LOGICAL UNIT NOT SUPPORTED */
4096		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4097		break;
4098	case TCM_UNSUPPORTED_SCSI_OPCODE:
4099	case TCM_SECTOR_COUNT_TOO_MANY:
4100		/* CURRENT ERROR */
4101		buffer[offset] = 0x70;
4102		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4103		/* ILLEGAL REQUEST */
4104		buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4105		/* INVALID COMMAND OPERATION CODE */
4106		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4107		break;
4108	case TCM_UNKNOWN_MODE_PAGE:
4109		/* CURRENT ERROR */
4110		buffer[offset] = 0x70;
4111		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4112		/* ILLEGAL REQUEST */
4113		buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4114		/* INVALID FIELD IN CDB */
4115		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4116		break;
4117	case TCM_CHECK_CONDITION_ABORT_CMD:
4118		/* CURRENT ERROR */
4119		buffer[offset] = 0x70;
4120		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4121		/* ABORTED COMMAND */
4122		buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4123		/* BUS DEVICE RESET FUNCTION OCCURRED */
4124		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4125		buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4126		break;
4127	case TCM_INCORRECT_AMOUNT_OF_DATA:
4128		/* CURRENT ERROR */
4129		buffer[offset] = 0x70;
4130		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4131		/* ABORTED COMMAND */
4132		buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4133		/* WRITE ERROR */
4134		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4135		/* NOT ENOUGH UNSOLICITED DATA */
4136		buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4137		break;
4138	case TCM_INVALID_CDB_FIELD:
4139		/* CURRENT ERROR */
4140		buffer[offset] = 0x70;
4141		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4142		/* ILLEGAL REQUEST */
4143		buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4144		/* INVALID FIELD IN CDB */
4145		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4146		break;
4147	case TCM_INVALID_PARAMETER_LIST:
4148		/* CURRENT ERROR */
4149		buffer[offset] = 0x70;
4150		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4151		/* ILLEGAL REQUEST */
4152		buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4153		/* INVALID FIELD IN PARAMETER LIST */
4154		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4155		break;
4156	case TCM_UNEXPECTED_UNSOLICITED_DATA:
4157		/* CURRENT ERROR */
4158		buffer[offset] = 0x70;
4159		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4160		/* ABORTED COMMAND */
4161		buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4162		/* WRITE ERROR */
4163		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4164		/* UNEXPECTED_UNSOLICITED_DATA */
4165		buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4166		break;
4167	case TCM_SERVICE_CRC_ERROR:
4168		/* CURRENT ERROR */
4169		buffer[offset] = 0x70;
4170		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4171		/* ABORTED COMMAND */
4172		buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4173		/* PROTOCOL SERVICE CRC ERROR */
4174		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4175		/* N/A */
4176		buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4177		break;
4178	case TCM_SNACK_REJECTED:
4179		/* CURRENT ERROR */
4180		buffer[offset] = 0x70;
4181		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4182		/* ABORTED COMMAND */
4183		buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4184		/* READ ERROR */
4185		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4186		/* FAILED RETRANSMISSION REQUEST */
4187		buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4188		break;
4189	case TCM_WRITE_PROTECTED:
4190		/* CURRENT ERROR */
4191		buffer[offset] = 0x70;
4192		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4193		/* DATA PROTECT */
4194		buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4195		/* WRITE PROTECTED */
4196		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4197		break;
4198	case TCM_ADDRESS_OUT_OF_RANGE:
4199		/* CURRENT ERROR */
4200		buffer[offset] = 0x70;
4201		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4202		/* ILLEGAL REQUEST */
4203		buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4204		/* LOGICAL BLOCK ADDRESS OUT OF RANGE */
4205		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x21;
4206		break;
4207	case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4208		/* CURRENT ERROR */
4209		buffer[offset] = 0x70;
4210		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4211		/* UNIT ATTENTION */
4212		buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4213		core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4214		buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4215		buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4216		break;
4217	case TCM_CHECK_CONDITION_NOT_READY:
4218		/* CURRENT ERROR */
4219		buffer[offset] = 0x70;
4220		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4221		/* Not Ready */
4222		buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4223		transport_get_sense_codes(cmd, &asc, &ascq);
4224		buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4225		buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4226		break;
4227	case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4228	default:
4229		/* CURRENT ERROR */
4230		buffer[offset] = 0x70;
4231		buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4232		/* ILLEGAL REQUEST */
4233		buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4234		/* LOGICAL UNIT COMMUNICATION FAILURE */
4235		buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4236		break;
4237	}
4238	/*
4239	 * This code uses linux/include/scsi/scsi.h SAM status codes!
4240	 */
4241	cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4242	/*
4243	 * Automatically padded, this value is encoded in the fabric's
4244	 * data_length response PDU containing the SCSI defined sense data.
4245	 */
4246	cmd->scsi_sense_length  = TRANSPORT_SENSE_BUFFER + offset;
4247
4248after_reason:
4249	return cmd->se_tfo->queue_status(cmd);
4250}
4251EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4252
4253int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4254{
4255	int ret = 0;
4256
4257	if (cmd->transport_state & CMD_T_ABORTED) {
4258		if (!send_status ||
4259		     (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4260			return 1;
4261
4262		pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
4263			" status for CDB: 0x%02x ITT: 0x%08x\n",
4264			cmd->t_task_cdb[0],
4265			cmd->se_tfo->get_task_tag(cmd));
4266
4267		cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
4268		cmd->se_tfo->queue_status(cmd);
4269		ret = 1;
4270	}
4271	return ret;
4272}
4273EXPORT_SYMBOL(transport_check_aborted_status);
4274
4275void transport_send_task_abort(struct se_cmd *cmd)
4276{
4277	unsigned long flags;
4278
4279	spin_lock_irqsave(&cmd->t_state_lock, flags);
4280	if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4281		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4282		return;
4283	}
4284	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4285
4286	/*
4287	 * If there are still expected incoming fabric WRITEs, we wait
4288	 * until until they have completed before sending a TASK_ABORTED
4289	 * response.  This response with TASK_ABORTED status will be
4290	 * queued back to fabric module by transport_check_aborted_status().
4291	 */
4292	if (cmd->data_direction == DMA_TO_DEVICE) {
4293		if (cmd->se_tfo->write_pending_status(cmd) != 0) {
4294			cmd->transport_state |= CMD_T_ABORTED;
4295			smp_mb__after_atomic_inc();
 
 
 
4296		}
4297	}
4298	cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4299
4300	pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
4301		" ITT: 0x%08x\n", cmd->t_task_cdb[0],
4302		cmd->se_tfo->get_task_tag(cmd));
4303
4304	cmd->se_tfo->queue_status(cmd);
4305}
4306
4307static int transport_generic_do_tmr(struct se_cmd *cmd)
 
 
 
 
4308{
4309	struct se_device *dev = cmd->se_dev;
4310	struct se_tmr_req *tmr = cmd->se_tmr_req;
4311	int ret;
4312
4313	switch (tmr->function) {
4314	case TMR_ABORT_TASK:
4315		core_tmr_abort_task(dev, tmr, cmd->se_sess);
4316		break;
4317	case TMR_ABORT_TASK_SET:
4318	case TMR_CLEAR_ACA:
4319	case TMR_CLEAR_TASK_SET:
4320		tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4321		break;
4322	case TMR_LUN_RESET:
4323		ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4324		tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4325					 TMR_FUNCTION_REJECTED;
4326		break;
4327	case TMR_TARGET_WARM_RESET:
4328		tmr->response = TMR_FUNCTION_REJECTED;
4329		break;
4330	case TMR_TARGET_COLD_RESET:
4331		tmr->response = TMR_FUNCTION_REJECTED;
4332		break;
4333	default:
4334		pr_err("Uknown TMR function: 0x%02x.\n",
4335				tmr->function);
4336		tmr->response = TMR_FUNCTION_REJECTED;
4337		break;
4338	}
4339
4340	cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
4341	cmd->se_tfo->queue_tm_rsp(cmd);
4342
4343	transport_cmd_check_stop_to_fabric(cmd);
4344	return 0;
4345}
4346
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4347/*	transport_processing_thread():
4348 *
4349 *
4350 */
4351static int transport_processing_thread(void *param)
4352{
4353	int ret;
4354	struct se_cmd *cmd;
4355	struct se_device *dev = param;
 
 
4356
4357	while (!kthread_should_stop()) {
4358		ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4359				atomic_read(&dev->dev_queue_obj.queue_cnt) ||
4360				kthread_should_stop());
4361		if (ret < 0)
4362			goto out;
4363
 
 
 
 
 
 
 
 
4364get_cmd:
 
 
4365		cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4366		if (!cmd)
4367			continue;
4368
4369		switch (cmd->t_state) {
4370		case TRANSPORT_NEW_CMD:
4371			BUG();
4372			break;
4373		case TRANSPORT_NEW_CMD_MAP:
4374			if (!cmd->se_tfo->new_cmd_map) {
4375				pr_err("cmd->se_tfo->new_cmd_map is"
4376					" NULL for TRANSPORT_NEW_CMD_MAP\n");
4377				BUG();
4378			}
4379			ret = cmd->se_tfo->new_cmd_map(cmd);
4380			if (ret < 0) {
4381				transport_generic_request_failure(cmd);
 
 
 
4382				break;
4383			}
 
 
4384			ret = transport_generic_new_cmd(cmd);
4385			if (ret < 0) {
4386				transport_generic_request_failure(cmd);
4387				break;
 
 
 
 
 
4388			}
4389			break;
4390		case TRANSPORT_PROCESS_WRITE:
4391			transport_generic_process_write(cmd);
4392			break;
 
 
 
 
 
 
 
 
 
 
4393		case TRANSPORT_PROCESS_TMR:
4394			transport_generic_do_tmr(cmd);
4395			break;
 
 
 
 
 
 
 
4396		case TRANSPORT_COMPLETE_QF_WP:
4397			transport_write_pending_qf(cmd);
4398			break;
4399		case TRANSPORT_COMPLETE_QF_OK:
4400			transport_complete_qf(cmd);
4401			break;
4402		default:
4403			pr_err("Unknown t_state: %d  for ITT: 0x%08x "
4404				"i_state: %d on SE LUN: %u\n",
4405				cmd->t_state,
4406				cmd->se_tfo->get_task_tag(cmd),
4407				cmd->se_tfo->get_cmd_state(cmd),
4408				cmd->se_lun->unpacked_lun);
4409			BUG();
4410		}
4411
4412		goto get_cmd;
4413	}
4414
4415out:
4416	WARN_ON(!list_empty(&dev->state_list));
4417	WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
4418	dev->process_thread = NULL;
4419	return 0;
4420}