blob: 634d0f31a28cc59f621ea6122a13777c320339d5 [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
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
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080029#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>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080036#include <linux/kthread.h>
37#include <linux/in.h>
38#include <linux/cdrom.h>
Paul Gortmaker827509e2011-08-30 14:20:44 -040039#include <linux/module.h>
Roland Dreier015487b2012-02-13 16:18:17 -080040#include <linux/ratelimit.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080041#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>
Nicholas Bellingere66ecd52011-05-19 20:19:14 -070046#include <scsi/scsi_tcq.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080047
48#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050049#include <target/target_core_backend.h>
50#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080051#include <target/target_core_configfs.h>
52
Christoph Hellwige26d99a2011-11-14 12:30:30 -050053#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080054#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080055#include "target_core_pr.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080056#include "target_core_ua.h"
57
Andy Grovere3d6f902011-07-19 08:55:10 +000058static int sub_api_initialized;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080059
Christoph Hellwig35e0e752011-10-17 13:56:53 -040060static struct workqueue_struct *target_completion_wq;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080061static struct kmem_cache *se_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080062struct kmem_cache *se_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080063struct 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
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080069static int transport_generic_write_pending(struct se_cmd *);
Andy Grover5951146d2011-07-19 10:26:37 +000070static int transport_processing_thread(void *param);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -080071static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080072static void transport_complete_task_attr(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -070073static void transport_handle_queue_full(struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -040074 struct se_device *dev);
Andy Grover05d1c7c2011-07-20 19:13:28 +000075static int transport_generic_get_mem(struct se_cmd *cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -070076static void transport_put_cmd(struct se_cmd *cmd);
Christoph Hellwig3df8d402011-10-17 13:56:43 -040077static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
Christoph Hellwig35e0e752011-10-17 13:56:53 -040079static void target_complete_ok_work(struct work_struct *work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080080
Andy Grovere3d6f902011-07-19 08:55:10 +000081int init_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080082{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083 se_sess_cache = kmem_cache_create("se_sess_cache",
84 sizeof(struct se_session), __alignof__(struct se_session),
85 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070086 if (!se_sess_cache) {
87 pr_err("kmem_cache_create() for struct se_session"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080088 " failed\n");
Andy Groverc8e31f22012-01-19 13:39:17 -080089 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080090 }
91 se_ua_cache = kmem_cache_create("se_ua_cache",
92 sizeof(struct se_ua), __alignof__(struct se_ua),
93 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070094 if (!se_ua_cache) {
95 pr_err("kmem_cache_create() for struct se_ua failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -040096 goto out_free_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080097 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080098 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);
Andy Grover6708bb22011-06-08 10:36:43 -0700101 if (!t10_pr_reg_cache) {
102 pr_err("kmem_cache_create() for struct t10_pr_registration"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800103 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400104 goto out_free_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800105 }
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);
Andy Grover6708bb22011-06-08 10:36:43 -0700109 if (!t10_alua_lu_gp_cache) {
110 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800111 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400112 goto out_free_pr_reg_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800113 }
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);
Andy Grover6708bb22011-06-08 10:36:43 -0700117 if (!t10_alua_lu_gp_mem_cache) {
118 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800119 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400120 goto out_free_lu_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800121 }
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);
Andy Grover6708bb22011-06-08 10:36:43 -0700125 if (!t10_alua_tg_pt_gp_cache) {
126 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800127 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400128 goto out_free_lu_gp_mem_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800129 }
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);
Andy Grover6708bb22011-06-08 10:36:43 -0700135 if (!t10_alua_tg_pt_gp_mem_cache) {
136 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800137 "mem_t failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400138 goto out_free_tg_pt_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800139 }
140
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400141 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
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800146 return 0;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400147
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);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800162out:
Andy Grovere3d6f902011-07-19 08:55:10 +0000163 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800164}
165
Andy Grovere3d6f902011-07-19 08:55:10 +0000166void release_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800167{
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400168 destroy_workqueue(target_completion_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800169 kmem_cache_destroy(se_sess_cache);
170 kmem_cache_destroy(se_ua_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800171 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);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800176}
177
Andy Grovere3d6f902011-07-19 08:55:10 +0000178/* 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];
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800181
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
Andy Grovere3d6f902011-07-19 08:55:10 +0000189 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800190
Andy Grovere3d6f902011-07-19 08:55:10 +0000191 spin_lock(&scsi_mib_index_lock);
192 new_index = ++scsi_mib_index[type];
193 spin_unlock(&scsi_mib_index_lock);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800194
195 return new_index;
196}
197
Christoph Hellwige26d99a2011-11-14 12:30:30 -0500198static void transport_init_queue_obj(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800199{
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}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800205
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700206void transport_subsystem_check_init(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800207{
208 int ret;
209
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700210 if (sub_api_initialized)
211 return;
212
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800213 ret = request_module("target_core_iblock");
214 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700215 pr_err("Unable to load target_core_iblock\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800216
217 ret = request_module("target_core_file");
218 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700219 pr_err("Unable to load target_core_file\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800220
221 ret = request_module("target_core_pscsi");
222 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700223 pr_err("Unable to load target_core_pscsi\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800224
225 ret = request_module("target_core_stgt");
226 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700227 pr_err("Unable to load target_core_stgt\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800228
Andy Grovere3d6f902011-07-19 08:55:10 +0000229 sub_api_initialized = 1;
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700230 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800231}
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);
Andy Grover6708bb22011-06-08 10:36:43 -0700238 if (!se_sess) {
239 pr_err("Unable to allocate struct se_session from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800240 " 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);
Nicholas Bellingera17f0912011-11-02 21:52:08 -0700245 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);
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800248 kref_init(&se_sess->sess_kref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800249
250 return se_sess;
251}
252EXPORT_SYMBOL(transport_init_session);
253
254/*
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700255 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800256 */
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 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000278 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800279 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +0000280 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800281 &buf[0], PR_REG_ISID_LEN);
282 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
283 }
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800284 kref_get(&se_nacl->acl_kref);
285
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800286 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
Andy Grover6708bb22011-06-08 10:36:43 -0700299 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000300 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800301}
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{
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700310 unsigned long flags;
311
312 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800313 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700314 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800315}
316EXPORT_SYMBOL(transport_register_session);
317
Joern Engel41492682012-05-18 13:57:19 -0700318void target_release_session(struct kref *kref)
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800319{
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
Jörn Engel33933a02012-05-11 10:35:08 -0400333void target_put_session(struct se_session *se_sess)
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800334{
Joern Engel41492682012-05-18 13:57:19 -0700335 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 }
Jörn Engel33933a02012-05-11 10:35:08 -0400341 kref_put(&se_sess->sess_kref, target_release_session);
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800342}
343EXPORT_SYMBOL(target_put_session);
344
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800345static 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
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800358void transport_deregister_session_configfs(struct se_session *se_sess)
359{
360 struct se_node_acl *se_nacl;
Roland Dreier23388862011-06-22 01:02:21 -0700361 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800362 /*
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;
Andy Grover6708bb22011-06-08 10:36:43 -0700366 if (se_nacl) {
Roland Dreier23388862011-06-22 01:02:21 -0700367 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800368 if (se_nacl->acl_stop == 0)
369 list_del(&se_sess->sess_acl_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800370 /*
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 }
Roland Dreier23388862011-06-22 01:02:21 -0700382 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800383 }
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;
Nicholas Bellinger01468342012-03-10 14:32:52 -0800396 struct target_core_fabric_ops *se_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800397 struct se_node_acl *se_nacl;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700398 unsigned long flags;
Nicholas Bellinger01468342012-03-10 14:32:52 -0800399 bool comp_nacl = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800400
Andy Grover6708bb22011-06-08 10:36:43 -0700401 if (!se_tpg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800402 transport_free_session(se_sess);
403 return;
404 }
Nicholas Bellinger01468342012-03-10 14:32:52 -0800405 se_tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406
Roland Dreiere63a8e12011-08-12 16:01:02 -0700407 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800408 list_del(&se_sess->sess_list);
409 se_sess->se_tpg = NULL;
410 se_sess->fabric_sess_ptr = NULL;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700411 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800412
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;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800418
Nicholas Bellinger01468342012-03-10 14:32:52 -0800419 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);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800431 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800432 }
Nicholas Bellinger01468342012-03-10 14:32:52 -0800433 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800434
Andy Grover6708bb22011-06-08 10:36:43 -0700435 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000436 se_tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellinger01468342012-03-10 14:32:52 -0800437 /*
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800438 * 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.
Nicholas Bellinger01468342012-03-10 14:32:52 -0800441 */
442 if (se_nacl && comp_nacl == true)
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800443 target_put_nacl(se_nacl);
Nicholas Bellinger01468342012-03-10 14:32:52 -0800444
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800445 transport_free_session(se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800446}
447EXPORT_SYMBOL(transport_deregister_session);
448
449/*
Andy Grovera1d8b492011-05-02 17:12:10 -0700450 * Called with cmd->t_state_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800451 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400452static void target_remove_from_state_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800453{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400454 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800455 unsigned long flags;
456
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400457 if (!dev)
458 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800459
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400460 if (cmd->transport_state & CMD_T_BUSY)
461 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800462
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400463 spin_lock_irqsave(&dev->execute_task_lock, flags);
464 if (cmd->state_active) {
465 list_del(&cmd->state_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400466 cmd->state_active = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800467 }
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400468 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800469}
470
471/* transport_cmd_check_stop():
472 *
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500473 * 'transport_off = 1' determines if CMD_T_ACTIVE should be cleared.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800474 * '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
Andy Grovera1d8b492011-05-02 17:12:10 -0700486 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800487 /*
488 * Determine if IOCTL context caller in requesting the stopping of this
489 * command for LUN shutdown purposes.
490 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500491 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));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800494
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500495 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800496 if (transport_off == 2)
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400497 target_remove_from_state_list(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700498 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800499
Andy Grovera1d8b492011-05-02 17:12:10 -0700500 complete(&cmd->transport_lun_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800501 return 1;
502 }
503 /*
504 * Determine if frontend context caller is requesting the stopping of
Andy Grovere3d6f902011-07-19 08:55:10 +0000505 * this command for frontend exceptions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800506 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500507 if (cmd->transport_state & CMD_T_STOP) {
508 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
509 __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000510 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800511
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800512 if (transport_off == 2)
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400513 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800514
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;
Andy Grovera1d8b492011-05-02 17:12:10 -0700521 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800522
Andy Grovera1d8b492011-05-02 17:12:10 -0700523 complete(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800524 return 1;
525 }
526 if (transport_off) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500527 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800528 if (transport_off == 2) {
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400529 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800530 /*
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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300537 * their internally allocated I/O reference now and
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800538 * struct se_cmd now.
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700539 *
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.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800543 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000544 if (cmd->se_tfo->check_stop_free != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800545 spin_unlock_irqrestore(
Andy Grovera1d8b492011-05-02 17:12:10 -0700546 &cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800547
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700548 return cmd->se_tfo->check_stop_free(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800549 }
550 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700551 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800552
553 return 0;
554 } else if (t_state)
555 cmd->t_state = t_state;
Andy Grovera1d8b492011-05-02 17:12:10 -0700556 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800557
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{
Andy Grovere3d6f902011-07-19 08:55:10 +0000568 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800569 unsigned long flags;
570
571 if (!lun)
572 return;
573
Andy Grovera1d8b492011-05-02 17:12:10 -0700574 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500575 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
576 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400577 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800578 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700579 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800580
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800581 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -0500582 if (!list_empty(&cmd->se_lun_node))
583 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800584 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
585}
586
587void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
588{
Andy Groverc8e31f22012-01-19 13:39:17 -0800589 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellinger8dc52b52011-10-09 02:02:51 -0700590 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800591
592 if (transport_cmd_check_stop_to_fabric(cmd))
593 return;
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700594 if (remove) {
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400595 transport_remove_cmd_from_queue(cmd);
Christoph Hellwige6a25732011-09-13 23:08:50 +0200596 transport_put_cmd(cmd);
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700597 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800598}
599
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400600static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
601 bool at_head)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800602{
603 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +0000604 struct se_queue_obj *qobj = &dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800605 unsigned long flags;
606
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800607 if (t_state) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700608 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800609 cmd->t_state = t_state;
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500610 cmd->transport_state |= CMD_T_ACTIVE;
Andy Grovera1d8b492011-05-02 17:12:10 -0700611 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800612 }
613
614 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Roland Dreier79a7fef2011-09-28 22:12:07 -0700615
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
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400622 if (at_head)
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700623 list_add(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400624 else
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700625 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500626 cmd->transport_state |= CMD_T_QUEUED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800627 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
628
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800629 wake_up_interruptible(&qobj->thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800630}
631
Andy Grover5951146d2011-07-19 10:26:37 +0000632static struct se_cmd *
633transport_get_cmd_from_queue(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800634{
Andy Grover5951146d2011-07-19 10:26:37 +0000635 struct se_cmd *cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800636 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 }
Andy Grover5951146d2011-07-19 10:26:37 +0000643 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800644
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500645 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700646 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800647 atomic_dec(&qobj->queue_cnt);
648 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
649
Andy Grover5951146d2011-07-19 10:26:37 +0000650 return cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800651}
652
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400653static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800654{
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400655 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800656 unsigned long flags;
657
658 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500659 if (!(cmd->transport_state & CMD_T_QUEUED)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800660 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
661 return;
662 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500663 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700664 atomic_dec(&qobj->queue_cnt);
665 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800666 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800667}
668
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400669static void target_complete_failure_work(struct work_struct *work)
670{
671 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
672
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700673 transport_generic_request_failure(cmd);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400674}
675
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400676void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800677{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400678 struct se_device *dev = cmd->se_dev;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400679 int success = scsi_status == GOOD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800680 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800681
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400682 cmd->scsi_status = scsi_status;
683
684
Andy Grovera1d8b492011-05-02 17:12:10 -0700685 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400686 cmd->transport_state &= ~CMD_T_BUSY;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800687
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800688 if (dev && dev->transport->transport_complete) {
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400689 if (dev->transport->transport_complete(cmd,
690 cmd->t_data_sg) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800691 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800692 success = 1;
693 }
694 }
695
696 /*
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400697 * See if we are waiting to complete for an exception condition.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800698 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400699 if (cmd->transport_state & CMD_T_REQUEST_STOP) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700700 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400701 complete(&cmd->task_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800702 return;
703 }
Christoph Hellwig22350072011-11-02 05:06:35 -0700704
705 if (!success)
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500706 cmd->transport_state |= CMD_T_FAILED;
Christoph Hellwig22350072011-11-02 05:06:35 -0700707
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800708 /*
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) {
Christoph Hellwig41e16e92011-11-23 06:54:15 -0500718 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400719 INIT_WORK(&cmd->work, target_complete_failure_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800720 } else {
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400721 INIT_WORK(&cmd->work, target_complete_ok_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800722 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400723
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400724 cmd->t_state = TRANSPORT_COMPLETE;
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800725 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
Andy Grovera1d8b492011-05-02 17:12:10 -0700726 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800727
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400728 queue_work(target_completion_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800729}
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400730EXPORT_SYMBOL(target_complete_cmd);
731
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400732static void target_add_to_state_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800733{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400734 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800735 unsigned long flags;
736
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800737 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400738 if (!cmd->state_active) {
739 list_add_tail(&cmd->state_list, &dev->state_list);
740 cmd->state_active = true;
741 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800742 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800743}
744
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400745static void __target_add_to_execute_list(struct se_cmd *cmd)
Christoph Hellwig04629b72011-10-12 11:07:04 -0400746{
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400747 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;
Christoph Hellwig04629b72011-10-12 11:07:04 -0400773}
774
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400775static void target_add_to_execute_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800776{
777 unsigned long flags;
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400778 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800779
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400780 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)))
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700797 return;
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700798
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800799 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400800 __target_remove_from_execute_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800801 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
802}
803
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700804/*
Nicholas Bellingerf147abb2011-10-25 23:57:41 -0700805 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700806 */
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);
Roland Dreierbcac3642011-08-27 21:33:16 -0700812 LIST_HEAD(qf_cmd_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700813 struct se_cmd *cmd, *cmd_tmp;
814
815 spin_lock_irq(&dev->qf_cmd_lock);
Roland Dreierbcac3642011-08-27 21:33:16 -0700816 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
817 spin_unlock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700818
Roland Dreierbcac3642011-08-27 21:33:16 -0700819 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700820 list_del(&cmd->se_qf_node);
821 atomic_dec(&dev->dev_qf_count);
822 smp_mb__after_atomic_dec();
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700823
Andy Grover6708bb22011-06-08 10:36:43 -0700824 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700825 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -0400826 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700827 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
828 : "UNKNOWN");
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400829
830 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700831 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700832}
833
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800834unsigned 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
Nicholas Bellinger65586d52011-11-30 01:25:21 -0800877 *bl += sprintf(b + *bl, " Execute/Max Queue Depth: %d/%d",
878 atomic_read(&dev->execute_tasks), dev->queue_depth);
Nicholas Bellinger11e764b2012-05-09 12:42:09 -0700879 *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);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800882 *bl += sprintf(b + *bl, " ");
883}
884
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800885void 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
Andy Grover6708bb22011-06-08 10:36:43 -0700935 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800936}
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];
Andy Grovere3d6f902011-07-19 08:55:10 +0000960 int ret = 0;
961 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800962
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);
Andy Grovere3d6f902011-07-19 08:55:10 +0000978 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800979 break;
980 }
981
982 if (p_buf)
983 strncpy(p_buf, buf, p_buf_len);
984 else
Andy Grover6708bb22011-06-08 10:36:43 -0700985 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800986
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];
Andy Grovere3d6f902011-07-19 08:55:10 +00001008 int ret = 0;
1009 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001010
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);
Andy Grovere3d6f902011-07-19 08:55:10 +00001036 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001037 break;
1038 }
1039
Andy Grovere3d6f902011-07-19 08:55:10 +00001040 if (p_buf) {
1041 if (p_buf_len < strlen(buf)+1)
1042 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001043 strncpy(p_buf, buf, p_buf_len);
Andy Grovere3d6f902011-07-19 08:55:10 +00001044 } else {
Andy Grover6708bb22011-06-08 10:36:43 -07001045 pr_debug("%s", buf);
Andy Grovere3d6f902011-07-19 08:55:10 +00001046 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001047
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);
Andy Grovere3d6f902011-07-19 08:55:10 +00001089 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001090 break;
1091 }
1092
1093 if (p_buf)
1094 strncpy(p_buf, buf, p_buf_len);
1095 else
Andy Grover6708bb22011-06-08 10:36:43 -07001096 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001097
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 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001147 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001148 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1149 return;
1150 }
1151
1152 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
Andy Grover6708bb22011-06-08 10:36:43 -07001153 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001154 " device\n", dev->transport->name,
1155 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001156}
1157
1158static void scsi_dump_inquiry(struct se_device *dev)
1159{
Andy Grovere3d6f902011-07-19 08:55:10 +00001160 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001161 char buf[17];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001162 int i, device_type;
1163 /*
1164 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1165 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001166 for (i = 0; i < 8; i++)
1167 if (wwn->vendor[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001168 buf[i] = wwn->vendor[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001169 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001170 buf[i] = ' ';
1171 buf[i] = '\0';
1172 pr_debug(" Vendor: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001173
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001174 for (i = 0; i < 16; i++)
1175 if (wwn->model[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001176 buf[i] = wwn->model[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001177 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001178 buf[i] = ' ';
1179 buf[i] = '\0';
1180 pr_debug(" Model: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001181
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001182 for (i = 0; i < 4; i++)
1183 if (wwn->revision[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001184 buf[i] = wwn->revision[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001185 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001186 buf[i] = ' ';
1187 buf[i] = '\0';
1188 pr_debug(" Revision: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001189
Andy Grovere3d6f902011-07-19 08:55:10 +00001190 device_type = dev->transport->get_device_type(dev);
Andy Grover6708bb22011-06-08 10:36:43 -07001191 pr_debug(" Type: %s ", scsi_device_type(device_type));
1192 pr_debug(" ANSI SCSI revision: %02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001193 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001194}
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{
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001206 int force_pt;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001207 struct se_device *dev;
1208
1209 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001210 if (!dev) {
1211 pr_err("Unable to allocate memory for se_dev_t\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001212 return NULL;
1213 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001214
Andy Grovere3d6f902011-07-19 08:55:10 +00001215 transport_init_queue_obj(&dev->dev_queue_obj);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001216 dev->dev_flags = device_flags;
1217 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
Andy Grover5951146d2011-07-19 10:26:37 +00001218 dev->dev_ptr = transport_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001219 dev->se_hba = hba;
1220 dev->se_sub_dev = se_dev;
1221 dev->transport = transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001222 INIT_LIST_HEAD(&dev->dev_list);
1223 INIT_LIST_HEAD(&dev->dev_sep_list);
1224 INIT_LIST_HEAD(&dev->dev_tmr_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001225 INIT_LIST_HEAD(&dev->execute_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001226 INIT_LIST_HEAD(&dev->delayed_cmd_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001227 INIT_LIST_HEAD(&dev->state_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001228 INIT_LIST_HEAD(&dev->qf_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001229 spin_lock_init(&dev->execute_task_lock);
1230 spin_lock_init(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001231 spin_lock_init(&dev->dev_reservation_lock);
1232 spin_lock_init(&dev->dev_status_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001233 spin_lock_init(&dev->se_port_lock);
1234 spin_lock_init(&dev->se_tmr_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001235 spin_lock_init(&dev->qf_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001236 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,
Andy Grovere3d6f902011-07-19 08:55:10 +00001270 "LIO_%s", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001271 if (IS_ERR(dev->process_thread)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001272 pr_err("Unable to create kthread: LIO_%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001273 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001274 goto out;
1275 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001276 /*
1277 * Setup work_queue for QUEUE_FULL
1278 */
1279 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001280 /*
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 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001288 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
Roland Dreierf22c1192011-05-02 22:15:37 -07001289 if (!inquiry_prod || !inquiry_rev) {
Andy Grover6708bb22011-06-08 10:36:43 -07001290 pr_err("All non TCM/pSCSI plugins require"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001291 " INQUIRY consts\n");
1292 goto out;
1293 }
1294
Andy Grovere3d6f902011-07-19 08:55:10 +00001295 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);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001298 }
1299 scsi_dump_inquiry(dev);
1300
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001301 return dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001302out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001303 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
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312 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 */
Nicholas Bellingerba539742012-05-16 21:52:10 -07001338 case MAINTENANCE_IN: /* SPC - Parameter Data Format for SA RTPG */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001339 break;
1340 default:
1341 cdb[1] &= 0x1f; /* clear logical unit number */
1342 break;
1343 }
1344}
1345
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001346static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1347
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001348/*
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{
Andy Grover5951146d2011-07-19 10:26:37 +00001361 INIT_LIST_HEAD(&cmd->se_lun_node);
1362 INIT_LIST_HEAD(&cmd->se_delayed_node);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001363 INIT_LIST_HEAD(&cmd->se_qf_node);
Roland Dreier79a7fef2011-09-28 22:12:07 -07001364 INIT_LIST_HEAD(&cmd->se_queue_node);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001365 INIT_LIST_HEAD(&cmd->se_cmd_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001366 INIT_LIST_HEAD(&cmd->execute_list);
1367 INIT_LIST_HEAD(&cmd->state_list);
Andy Grovera1d8b492011-05-02 17:12:10 -07001368 init_completion(&cmd->transport_lun_fe_stop_comp);
1369 init_completion(&cmd->transport_lun_stop_comp);
1370 init_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001371 init_completion(&cmd->cmd_wait_comp);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001372 init_completion(&cmd->task_stop_comp);
Andy Grovera1d8b492011-05-02 17:12:10 -07001373 spin_lock_init(&cmd->t_state_lock);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001374 cmd->transport_state = CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001375
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;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001382
1383 cmd->state_active = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001384}
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 */
Andy Grover5951146d2011-07-19 10:26:37 +00001393 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001394 return 0;
1395
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001396 if (cmd->sam_task_attr == MSG_ACA_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001397 pr_debug("SAM Task Attribute ACA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001398 " emulation is not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001399 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001400 }
1401 /*
1402 * Used to determine when ORDERED commands should go from
1403 * Dormant to Active status.
1404 */
Andy Grover5951146d2011-07-19 10:26:37 +00001405 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001406 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001407 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001408 cmd->se_ordered_id, cmd->sam_task_attr,
Andy Grover6708bb22011-06-08 10:36:43 -07001409 cmd->se_dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001410 return 0;
1411}
1412
Andy Grovera12f41f2012-04-03 15:51:20 -07001413/* target_setup_cmd_from_cdb():
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001414 *
1415 * Called from fabric RX Thread.
1416 */
Andy Grovera12f41f2012-04-03 15:51:20 -07001417int target_setup_cmd_from_cdb(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001418 struct se_cmd *cmd,
1419 unsigned char *cdb)
1420{
1421 int ret;
1422
1423 transport_generic_prepare_cdb(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001424 /*
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) {
Andy Grover6708bb22011-06-08 10:36:43 -07001429 pr_err("Received SCSI CDB with command_size: %d that"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001430 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1431 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001432 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1433 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grovere3d6f902011-07-19 08:55:10 +00001434 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001435 }
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 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001441 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1442 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001443 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001444 if (!cmd->t_task_cdb) {
1445 pr_err("Unable to allocate cmd->t_task_cdb"
Andy Grovera1d8b492011-05-02 17:12:10 -07001446 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001447 scsi_command_size(cdb),
Andy Grovera1d8b492011-05-02 17:12:10 -07001448 (unsigned long)sizeof(cmd->__t_task_cdb));
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001449 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1450 cmd->scsi_sense_reason =
1451 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grovere3d6f902011-07-19 08:55:10 +00001452 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001453 }
1454 } else
Andy Grovera1d8b492011-05-02 17:12:10 -07001455 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001456 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07001457 * Copy the original CDB into cmd->
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001458 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001459 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001460 /*
1461 * Setup the received CDB based on SCSI defined opcodes and
1462 * perform unit attention, persistent reservations and ALUA
Andy Grovera1d8b492011-05-02 17:12:10 -07001463 * checks for virtual device backends. The cmd->t_task_cdb
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001464 * 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;
Andy Grover5951146d2011-07-19 10:26:37 +00001475 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001476 }
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}
Andy Grovera12f41f2012-04-03 15:51:20 -07001483EXPORT_SYMBOL(target_setup_cmd_from_cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001484
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001485/*
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001486 * 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{
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001492 int ret;
1493
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001494 if (!cmd->se_lun) {
1495 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001496 pr_err("cmd->se_lun is NULL\n");
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001497 return -EINVAL;
1498 }
1499 if (in_interrupt()) {
1500 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001501 pr_err("transport_generic_handle_cdb cannot be called"
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001502 " from interrupt context\n");
1503 return -EINVAL;
1504 }
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001505 /*
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001506 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001507 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1508 * in existing usage to ensure that outstanding descriptors are handled
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07001509 * correctly during shutdown via transport_wait_for_tasks()
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001510 *
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;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001515 cmd->transport_state |= CMD_T_ACTIVE;
1516
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001517 /*
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);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001523 if (ret < 0)
1524 transport_generic_request_failure(cmd);
1525
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001526 return 0;
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001527}
1528EXPORT_SYMBOL(transport_handle_cdb_direct);
1529
Nicholas Bellingera6360782011-11-18 20:36:22 -08001530/**
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 **/
Andy Grover1edcdb42012-01-19 13:39:23 -08001546void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
Nicholas Bellingera6360782011-11-18 20:36:22 -08001547 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);
Sebastian Andrzej Siewiorb0d79942012-01-10 14:16:59 +01001564 if (flags & TARGET_SCF_UNKNOWN_SIZE)
1565 se_cmd->unknown_data_length = 1;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001566 /*
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 */
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001581 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 }
Nicholas Bellingera6360782011-11-18 20:36:22 -08001587 /*
1588 * Sanitize CDBs via transport_generic_cmd_sequencer() and
1589 * allocate the necessary tasks to complete the received CDB+data
1590 */
Andy Grovera12f41f2012-04-03 15:51:20 -07001591 rc = target_setup_cmd_from_cdb(se_cmd, cdb);
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001592 if (rc != 0) {
1593 transport_generic_request_failure(se_cmd);
1594 return;
1595 }
Andy Grover11e319e2012-04-03 15:51:28 -07001596
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
Nicholas Bellingera6360782011-11-18 20:36:22 -08001603 /*
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);
Andy Grover1edcdb42012-01-19 13:39:23 -08001610 return;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001611}
1612EXPORT_SYMBOL(target_submit_cmd);
1613
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001614static 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
Andy Groverea98d7f2012-01-19 13:39:21 -08001623/**
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
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001633 * @gfp: gfp type for caller
1634 * @tag: referenced task tag for TMR_ABORT_TASK
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001635 * @flags: submit cmd flags
Andy Groverea98d7f2012-01-19 13:39:21 -08001636 *
1637 * Callable from all contexts.
1638 **/
1639
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001640int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
Andy Groverea98d7f2012-01-19 13:39:21 -08001641 unsigned char *sense, u32 unpacked_lun,
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001642 void *fabric_tmr_ptr, unsigned char tm_type,
1643 gfp_t gfp, unsigned int tag, int flags)
Andy Groverea98d7f2012-01-19 13:39:21 -08001644{
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);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001653 /*
1654 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1655 * allocation failure.
1656 */
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001657 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001658 if (ret < 0)
1659 return -ENOMEM;
Andy Groverea98d7f2012-01-19 13:39:21 -08001660
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001661 if (tm_type == TMR_ABORT_TASK)
1662 se_cmd->se_tmr_req->ref_task_tag = tag;
1663
Andy Groverea98d7f2012-01-19 13:39:21 -08001664 /* See target_submit_cmd for commentary */
1665 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1666
Andy Groverea98d7f2012-01-19 13:39:21 -08001667 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1668 if (ret) {
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001669 /*
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);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001675 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001676 }
1677 transport_generic_handle_tmr(se_cmd);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001678 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001679}
1680EXPORT_SYMBOL(target_submit_tmr);
1681
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001682/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001683 * 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{
Andy Grovere3d6f902011-07-19 08:55:10 +00001690 if (!cmd->se_lun) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001691 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001692 pr_err("cmd->se_lun is NULL\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001693 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001694 }
1695
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001696 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001697 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))
Andy Grovere3d6f902011-07-19 08:55:10 +00001715 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001716 /*
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
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001720 * fabric module as we are expecting no further incoming DATA OUT
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001721 * sequences at this point.
1722 */
1723 if (transport_check_aborted_status(cmd, 1) != 0)
1724 return 0;
1725
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001726 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001727 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{
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001738 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001739 return 0;
1740}
1741EXPORT_SYMBOL(transport_generic_handle_tmr);
1742
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001743/*
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001744 * If the cmd is active, request it to be stopped and sleep until it
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001745 * has completed.
1746 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001747bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001748{
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001749 bool was_active = false;
1750
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001751 if (cmd->transport_state & CMD_T_BUSY) {
1752 cmd->transport_state |= CMD_T_REQUEST_STOP;
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001753 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1754
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001755 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);
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001758
1759 spin_lock_irqsave(&cmd->t_state_lock, *flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001760 cmd->transport_state &= ~CMD_T_REQUEST_STOP;
1761 cmd->transport_state &= ~CMD_T_BUSY;
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001762 was_active = true;
1763 }
1764
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001765 return was_active;
1766}
1767
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001768/*
1769 * Handle SAM-esque emulation for generic transport request failures.
1770 */
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001771void transport_generic_request_failure(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001772{
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001773 int ret = 0;
1774
Andy Grover6708bb22011-06-08 10:36:43 -07001775 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001776 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -07001777 cmd->t_task_cdb[0]);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001778 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001779 cmd->se_tfo->get_cmd_state(cmd),
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001780 cmd->t_state, cmd->scsi_sense_reason);
Christoph Hellwigd43d6ae2012-04-24 00:25:08 -04001781 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001782 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1783 (cmd->transport_state & CMD_T_STOP) != 0,
1784 (cmd->transport_state & CMD_T_SENT) != 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001785
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001786 /*
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
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001792 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_CHECK_CONDITION_ABORT_CMD:
1801 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1802 case TCM_CHECK_CONDITION_NOT_READY:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001803 break;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001804 case TCM_RESERVATION_CONFLICT:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001805 /*
1806 * No SENSE Data payload for this case, set SCSI Status
1807 * and queue the response to $FABRIC_MOD.
1808 *
1809 * Uses linux/include/scsi/scsi.h SAM status codes defs
1810 */
1811 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1812 /*
1813 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1814 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1815 * CONFLICT STATUS.
1816 *
1817 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1818 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001819 if (cmd->se_sess &&
1820 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1821 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001822 cmd->orig_fe_lun, 0x2C,
1823 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1824
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001825 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001826 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001827 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001828 goto check_stop;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001829 default:
Andy Grover6708bb22011-06-08 10:36:43 -07001830 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001831 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001832 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1833 break;
1834 }
Nicholas Bellinger16ab8e62011-08-08 19:03:38 -07001835 /*
1836 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1837 * make the call to transport_send_check_condition_and_sense()
1838 * directly. Otherwise expect the fabric to make the call to
1839 * transport_send_check_condition_and_sense() after handling
1840 * possible unsoliticied write data payloads.
1841 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001842 ret = transport_send_check_condition_and_sense(cmd,
1843 cmd->scsi_sense_reason, 0);
1844 if (ret == -EAGAIN || ret == -ENOMEM)
1845 goto queue_full;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001846
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001847check_stop:
1848 transport_lun_remove_cmd(cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07001849 if (!transport_cmd_check_stop_to_fabric(cmd))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001850 ;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001851 return;
1852
1853queue_full:
Christoph Hellwige057f532011-10-17 13:56:41 -04001854 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1855 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001856}
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001857EXPORT_SYMBOL(transport_generic_request_failure);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001858
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001859static inline u32 transport_lba_21(unsigned char *cdb)
1860{
1861 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1862}
1863
1864static inline u32 transport_lba_32(unsigned char *cdb)
1865{
1866 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1867}
1868
1869static inline unsigned long long transport_lba_64(unsigned char *cdb)
1870{
1871 unsigned int __v1, __v2;
1872
1873 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1874 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1875
1876 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1877}
1878
1879/*
1880 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
1881 */
1882static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
1883{
1884 unsigned int __v1, __v2;
1885
1886 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
1887 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
1888
1889 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1890}
1891
1892static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
1893{
1894 unsigned long flags;
1895
Andy Grovera1d8b492011-05-02 17:12:10 -07001896 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001897 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
Andy Grovera1d8b492011-05-02 17:12:10 -07001898 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001899}
1900
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001901/*
1902 * Called from Fabric Module context from transport_execute_tasks()
1903 *
1904 * The return of this function determins if the tasks from struct se_cmd
1905 * get added to the execution queue in transport_execute_tasks(),
1906 * or are added to the delayed or ordered lists here.
1907 */
1908static inline int transport_execute_task_attr(struct se_cmd *cmd)
1909{
Andy Grover5951146d2011-07-19 10:26:37 +00001910 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001911 return 1;
1912 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001913 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001914 * to allow the passed struct se_cmd list of tasks to the front of the list.
1915 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001916 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001917 pr_debug("Added HEAD_OF_QUEUE for CDB:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001918 " 0x%02x, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07001919 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001920 cmd->se_ordered_id);
1921 return 1;
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001922 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Andy Grover5951146d2011-07-19 10:26:37 +00001923 atomic_inc(&cmd->se_dev->dev_ordered_sync);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001924 smp_mb__after_atomic_inc();
1925
Andy Grover6708bb22011-06-08 10:36:43 -07001926 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001927 " list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07001928 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001929 cmd->se_ordered_id);
1930 /*
1931 * Add ORDERED command to tail of execution queue if
1932 * no other older commands exist that need to be
1933 * completed first.
1934 */
Andy Grover6708bb22011-06-08 10:36:43 -07001935 if (!atomic_read(&cmd->se_dev->simple_cmds))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001936 return 1;
1937 } else {
1938 /*
1939 * For SIMPLE and UNTAGGED Task Attribute commands
1940 */
Andy Grover5951146d2011-07-19 10:26:37 +00001941 atomic_inc(&cmd->se_dev->simple_cmds);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001942 smp_mb__after_atomic_inc();
1943 }
1944 /*
1945 * Otherwise if one or more outstanding ORDERED task attribute exist,
1946 * add the dormant task(s) built for the passed struct se_cmd to the
1947 * execution queue and become in Active state for this struct se_device.
1948 */
Andy Grover5951146d2011-07-19 10:26:37 +00001949 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001950 /*
1951 * Otherwise, add cmd w/ tasks to delayed cmd queue that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001952 * will be drained upon completion of HEAD_OF_QUEUE task.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001953 */
Andy Grover5951146d2011-07-19 10:26:37 +00001954 spin_lock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001955 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
Andy Grover5951146d2011-07-19 10:26:37 +00001956 list_add_tail(&cmd->se_delayed_node,
1957 &cmd->se_dev->delayed_cmd_list);
1958 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001959
Andy Grover6708bb22011-06-08 10:36:43 -07001960 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001961 " delayed CMD list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07001962 cmd->t_task_cdb[0], cmd->sam_task_attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001963 cmd->se_ordered_id);
1964 /*
1965 * Return zero to let transport_execute_tasks() know
1966 * not to add the delayed tasks to the execution list.
1967 */
1968 return 0;
1969 }
1970 /*
1971 * Otherwise, no ORDERED task attributes exist..
1972 */
1973 return 1;
1974}
1975
1976/*
1977 * Called from fabric module context in transport_generic_new_cmd() and
1978 * transport_generic_process_write()
1979 */
Jörn Engeld28d96d2012-05-11 10:33:41 -04001980static void transport_execute_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001981{
1982 int add_tasks;
Nicholas Bellinger40be67f2011-11-30 00:41:20 -08001983 struct se_device *se_dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001984 /*
1985 * Call transport_cmd_check_stop() to see if a fabric exception
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001986 * has occurred that prevents execution.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001987 */
Andy Grover6708bb22011-06-08 10:36:43 -07001988 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001989 /*
1990 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
1991 * attribute for the tasks of the received struct se_cmd CDB
1992 */
1993 add_tasks = transport_execute_task_attr(cmd);
Jörn Engeld28d96d2012-05-11 10:33:41 -04001994 if (add_tasks) {
1995 __transport_execute_tasks(se_dev, cmd);
1996 return;
1997 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001998 }
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08001999 __transport_execute_tasks(se_dev, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002000}
2001
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002002static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002003{
2004 int error;
2005 struct se_cmd *cmd = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002006 unsigned long flags;
2007
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002008check_depth:
Andy Grovere3d6f902011-07-19 08:55:10 +00002009 spin_lock_irq(&dev->execute_task_lock);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002010 if (new_cmd != NULL)
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002011 __target_add_to_execute_list(new_cmd);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002012
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002013 if (list_empty(&dev->execute_list)) {
Andy Grovere3d6f902011-07-19 08:55:10 +00002014 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002015 return 0;
2016 }
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002017 cmd = list_first_entry(&dev->execute_list, struct se_cmd, execute_list);
2018 __target_remove_from_execute_list(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00002019 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002020
Andy Grovera1d8b492011-05-02 17:12:10 -07002021 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002022 cmd->transport_state |= CMD_T_BUSY;
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002023 cmd->transport_state |= CMD_T_SENT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002024
Andy Grovera1d8b492011-05-02 17:12:10 -07002025 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002026
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002027 if (cmd->execute_cmd)
2028 error = cmd->execute_cmd(cmd);
Christoph Hellwig5787cac2012-04-24 00:25:06 -04002029 else {
2030 error = dev->transport->execute_cmd(cmd, cmd->t_data_sg,
2031 cmd->t_data_nents, cmd->data_direction);
2032 }
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002033
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002034 if (error != 0) {
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002035 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002036 cmd->transport_state &= ~CMD_T_BUSY;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002037 cmd->transport_state &= ~CMD_T_SENT;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002038 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002039
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002040 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002041 }
2042
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002043 new_cmd = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002044 goto check_depth;
2045
2046 return 0;
2047}
2048
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002049static inline u32 transport_get_sectors_6(
2050 unsigned char *cdb,
2051 struct se_cmd *cmd,
2052 int *ret)
2053{
Andy Grover5951146d2011-07-19 10:26:37 +00002054 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002055
2056 /*
2057 * Assume TYPE_DISK for non struct se_device objects.
2058 * Use 8-bit sector value.
2059 */
2060 if (!dev)
2061 goto type_disk;
2062
2063 /*
2064 * Use 24-bit allocation length for TYPE_TAPE.
2065 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002066 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002067 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2068
2069 /*
2070 * Everything else assume TYPE_DISK Sector CDB location.
Roland Dreier9b5cd7f2011-11-22 13:51:33 -08002071 * Use 8-bit sector value. SBC-3 says:
2072 *
2073 * A TRANSFER LENGTH field set to zero specifies that 256
2074 * logical blocks shall be written. Any other value
2075 * specifies the number of logical blocks that shall be
2076 * written.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002077 */
2078type_disk:
Roland Dreier9b5cd7f2011-11-22 13:51:33 -08002079 return cdb[4] ? : 256;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002080}
2081
2082static inline u32 transport_get_sectors_10(
2083 unsigned char *cdb,
2084 struct se_cmd *cmd,
2085 int *ret)
2086{
Andy Grover5951146d2011-07-19 10:26:37 +00002087 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002088
2089 /*
2090 * Assume TYPE_DISK for non struct se_device objects.
2091 * Use 16-bit sector value.
2092 */
2093 if (!dev)
2094 goto type_disk;
2095
2096 /*
2097 * XXX_10 is not defined in SSC, throw an exception
2098 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002099 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2100 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002101 return 0;
2102 }
2103
2104 /*
2105 * Everything else assume TYPE_DISK Sector CDB location.
2106 * Use 16-bit sector value.
2107 */
2108type_disk:
2109 return (u32)(cdb[7] << 8) + cdb[8];
2110}
2111
2112static inline u32 transport_get_sectors_12(
2113 unsigned char *cdb,
2114 struct se_cmd *cmd,
2115 int *ret)
2116{
Andy Grover5951146d2011-07-19 10:26:37 +00002117 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002118
2119 /*
2120 * Assume TYPE_DISK for non struct se_device objects.
2121 * Use 32-bit sector value.
2122 */
2123 if (!dev)
2124 goto type_disk;
2125
2126 /*
2127 * XXX_12 is not defined in SSC, throw an exception
2128 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002129 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2130 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002131 return 0;
2132 }
2133
2134 /*
2135 * Everything else assume TYPE_DISK Sector CDB location.
2136 * Use 32-bit sector value.
2137 */
2138type_disk:
2139 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2140}
2141
2142static inline u32 transport_get_sectors_16(
2143 unsigned char *cdb,
2144 struct se_cmd *cmd,
2145 int *ret)
2146{
Andy Grover5951146d2011-07-19 10:26:37 +00002147 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002148
2149 /*
2150 * Assume TYPE_DISK for non struct se_device objects.
2151 * Use 32-bit sector value.
2152 */
2153 if (!dev)
2154 goto type_disk;
2155
2156 /*
2157 * Use 24-bit allocation length for TYPE_TAPE.
2158 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002159 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002160 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2161
2162type_disk:
2163 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2164 (cdb[12] << 8) + cdb[13];
2165}
2166
2167/*
2168 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2169 */
2170static inline u32 transport_get_sectors_32(
2171 unsigned char *cdb,
2172 struct se_cmd *cmd,
2173 int *ret)
2174{
2175 /*
2176 * Assume TYPE_DISK for non struct se_device objects.
2177 * Use 32-bit sector value.
2178 */
2179 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2180 (cdb[30] << 8) + cdb[31];
2181
2182}
2183
2184static inline u32 transport_get_size(
2185 u32 sectors,
2186 unsigned char *cdb,
2187 struct se_cmd *cmd)
2188{
Andy Grover5951146d2011-07-19 10:26:37 +00002189 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002190
Andy Grovere3d6f902011-07-19 08:55:10 +00002191 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002192 if (cdb[1] & 1) { /* sectors */
Andy Grovere3d6f902011-07-19 08:55:10 +00002193 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002194 } else /* bytes */
2195 return sectors;
2196 }
Andy Grover8b1e1242012-04-03 15:51:12 -07002197
Andy Grover6708bb22011-06-08 10:36:43 -07002198 pr_debug("Returning block_size: %u, sectors: %u == %u for"
Andy Grover8b1e1242012-04-03 15:51:12 -07002199 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size,
2200 sectors, dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2201 dev->transport->name);
2202
Andy Grovere3d6f902011-07-19 08:55:10 +00002203 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002204}
2205
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002206static void transport_xor_callback(struct se_cmd *cmd)
2207{
2208 unsigned char *buf, *addr;
Andy Groverec98f782011-07-20 19:28:46 +00002209 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002210 unsigned int offset;
2211 int i;
Andy Groverec98f782011-07-20 19:28:46 +00002212 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002213 /*
2214 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2215 *
2216 * 1) read the specified logical block(s);
2217 * 2) transfer logical blocks from the data-out buffer;
2218 * 3) XOR the logical blocks transferred from the data-out buffer with
2219 * the logical blocks read, storing the resulting XOR data in a buffer;
2220 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2221 * blocks transferred from the data-out buffer; and
2222 * 5) transfer the resulting XOR data to the data-in buffer.
2223 */
2224 buf = kmalloc(cmd->data_length, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002225 if (!buf) {
2226 pr_err("Unable to allocate xor_callback buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002227 return;
2228 }
2229 /*
Andy Groverec98f782011-07-20 19:28:46 +00002230 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002231 * into the locally allocated *buf
2232 */
Andy Groverec98f782011-07-20 19:28:46 +00002233 sg_copy_to_buffer(cmd->t_data_sg,
2234 cmd->t_data_nents,
2235 buf,
2236 cmd->data_length);
2237
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002238 /*
2239 * Now perform the XOR against the BIDI read memory located at
Andy Grovera1d8b492011-05-02 17:12:10 -07002240 * cmd->t_mem_bidi_list
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002241 */
2242
2243 offset = 0;
Andy Groverec98f782011-07-20 19:28:46 +00002244 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
Cong Wangca747d62011-11-25 23:14:25 +08002245 addr = kmap_atomic(sg_page(sg));
Andy Groverec98f782011-07-20 19:28:46 +00002246 if (!addr)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002247 goto out;
2248
Andy Groverec98f782011-07-20 19:28:46 +00002249 for (i = 0; i < sg->length; i++)
2250 *(addr + sg->offset + i) ^= *(buf + offset + i);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002251
Andy Groverec98f782011-07-20 19:28:46 +00002252 offset += sg->length;
Cong Wangca747d62011-11-25 23:14:25 +08002253 kunmap_atomic(addr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002254 }
Andy Groverec98f782011-07-20 19:28:46 +00002255
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002256out:
2257 kfree(buf);
2258}
2259
2260/*
2261 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2262 */
2263static int transport_get_sense_data(struct se_cmd *cmd)
2264{
2265 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002266 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002267 unsigned long flags;
2268 u32 offset = 0;
2269
Andy Grovere3d6f902011-07-19 08:55:10 +00002270 WARN_ON(!cmd->se_lun);
2271
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002272 if (!dev)
2273 return 0;
2274
Andy Grovera1d8b492011-05-02 17:12:10 -07002275 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002276 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002277 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002278 return 0;
2279 }
2280
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002281 if (!(cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2282 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002283
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002284 if (!dev->transport->get_sense_buffer) {
2285 pr_err("dev->transport->get_sense_buffer is NULL\n");
2286 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002287 }
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002288
Christoph Hellwig5787cac2012-04-24 00:25:06 -04002289 sense_buffer = dev->transport->get_sense_buffer(cmd);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002290 if (!sense_buffer) {
Christoph Hellwig5787cac2012-04-24 00:25:06 -04002291 pr_err("ITT 0x%08x cmd %p: Unable to locate"
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002292 " sense buffer for task with sense\n",
Christoph Hellwig5787cac2012-04-24 00:25:06 -04002293 cmd->se_tfo->get_task_tag(cmd), cmd);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002294 goto out;
2295 }
Christoph Hellwig5787cac2012-04-24 00:25:06 -04002296
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002297 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2298
2299 offset = cmd->se_tfo->set_fabric_sense_len(cmd, TRANSPORT_SENSE_BUFFER);
2300
2301 memcpy(&buffer[offset], sense_buffer, TRANSPORT_SENSE_BUFFER);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002302
2303 /* Automatically padded */
2304 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
2305
2306 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x and sense\n",
2307 dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
2308 return 0;
2309
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002310out:
Andy Grovera1d8b492011-05-02 17:12:10 -07002311 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002312 return -1;
2313}
2314
Andy Groverec98f782011-07-20 19:28:46 +00002315static inline long long transport_dev_end_lba(struct se_device *dev)
2316{
2317 return dev->transport->get_blocks(dev) + 1;
2318}
2319
2320static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2321{
2322 struct se_device *dev = cmd->se_dev;
2323 u32 sectors;
2324
2325 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2326 return 0;
2327
2328 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2329
Andy Grover6708bb22011-06-08 10:36:43 -07002330 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2331 pr_err("LBA: %llu Sectors: %u exceeds"
Andy Groverec98f782011-07-20 19:28:46 +00002332 " transport_dev_end_lba(): %llu\n",
2333 cmd->t_task_lba, sectors,
2334 transport_dev_end_lba(dev));
Nicholas Bellinger7abbe7f2011-08-10 18:41:14 -07002335 return -EINVAL;
Andy Groverec98f782011-07-20 19:28:46 +00002336 }
2337
Nicholas Bellinger7abbe7f2011-08-10 18:41:14 -07002338 return 0;
Andy Groverec98f782011-07-20 19:28:46 +00002339}
2340
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002341static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2342{
2343 /*
2344 * Determine if the received WRITE_SAME is used to for direct
2345 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2346 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2347 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2348 */
2349 int passthrough = (dev->transport->transport_type ==
2350 TRANSPORT_PLUGIN_PHBA_PDEV);
2351
2352 if (!passthrough) {
2353 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2354 pr_err("WRITE_SAME PBDATA and LBDATA"
2355 " bits not supported for Block Discard"
2356 " Emulation\n");
2357 return -ENOSYS;
2358 }
2359 /*
2360 * Currently for the emulated case we only accept
2361 * tpws with the UNMAP=1 bit set.
2362 */
2363 if (!(flags[0] & 0x08)) {
2364 pr_err("WRITE_SAME w/o UNMAP bit not"
2365 " supported for Block Discard Emulation\n");
2366 return -ENOSYS;
2367 }
2368 }
2369
2370 return 0;
2371}
2372
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002373/* transport_generic_cmd_sequencer():
2374 *
2375 * Generic Command Sequencer that should work for most DAS transport
2376 * drivers.
2377 *
Andy Grovera12f41f2012-04-03 15:51:20 -07002378 * Called from target_setup_cmd_from_cdb() in the $FABRIC_MOD
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002379 * RX Thread.
2380 *
2381 * FIXME: Need to support other SCSI OPCODES where as well.
2382 */
2383static int transport_generic_cmd_sequencer(
2384 struct se_cmd *cmd,
2385 unsigned char *cdb)
2386{
Andy Grover5951146d2011-07-19 10:26:37 +00002387 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002388 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2389 int ret = 0, sector_ret = 0, passthrough;
2390 u32 sectors = 0, size = 0, pr_reg_type = 0;
2391 u16 service_action;
2392 u8 alua_ascq = 0;
2393 /*
2394 * Check for an existing UNIT ATTENTION condition
2395 */
2396 if (core_scsi3_ua_check(cmd, cdb) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002397 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2398 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
Andy Grover5951146d2011-07-19 10:26:37 +00002399 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002400 }
2401 /*
2402 * Check status of Asymmetric Logical Unit Assignment port
2403 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002404 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002405 if (ret != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002406 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002407 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002408 * The ALUA additional sense code qualifier (ASCQ) is determined
2409 * by the ALUA primary or secondary access state..
2410 */
2411 if (ret > 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002412 pr_debug("[%s]: ALUA TG Port not available,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002413 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002414 cmd->se_tfo->get_fabric_name(), alua_ascq);
Andy Grover8b1e1242012-04-03 15:51:12 -07002415
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002416 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2417 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2418 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
Andy Grover5951146d2011-07-19 10:26:37 +00002419 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002420 }
2421 goto out_invalid_cdb_field;
2422 }
2423 /*
2424 * Check status for SPC-3 Persistent Reservations
2425 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002426 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2427 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002428 cmd, cdb, pr_reg_type) != 0) {
2429 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2430 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -07002431 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002432 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2433 return -EBUSY;
2434 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002435 /*
2436 * This means the CDB is allowed for the SCSI Initiator port
2437 * when said port is *NOT* holding the legacy SPC-2 or
2438 * SPC-3 Persistent Reservation.
2439 */
2440 }
2441
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002442 /*
2443 * If we operate in passthrough mode we skip most CDB emulation and
2444 * instead hand the commands down to the physical SCSI device.
2445 */
2446 passthrough =
2447 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
2448
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002449 switch (cdb[0]) {
2450 case READ_6:
2451 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2452 if (sector_ret)
2453 goto out_unsupported_cdb;
2454 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002455 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002456 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2457 break;
2458 case READ_10:
2459 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2460 if (sector_ret)
2461 goto out_unsupported_cdb;
2462 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002463 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002464 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2465 break;
2466 case READ_12:
2467 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2468 if (sector_ret)
2469 goto out_unsupported_cdb;
2470 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002471 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002472 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2473 break;
2474 case READ_16:
2475 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2476 if (sector_ret)
2477 goto out_unsupported_cdb;
2478 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002479 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002480 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2481 break;
2482 case WRITE_6:
2483 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2484 if (sector_ret)
2485 goto out_unsupported_cdb;
2486 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002487 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002488 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2489 break;
2490 case WRITE_10:
Bernhard Kohl66246612012-05-13 23:39:01 +02002491 case WRITE_VERIFY:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002492 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2493 if (sector_ret)
2494 goto out_unsupported_cdb;
2495 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002496 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002497 if (cdb[1] & 0x8)
2498 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002499 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2500 break;
2501 case WRITE_12:
2502 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2503 if (sector_ret)
2504 goto out_unsupported_cdb;
2505 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002506 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002507 if (cdb[1] & 0x8)
2508 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002509 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2510 break;
2511 case WRITE_16:
2512 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2513 if (sector_ret)
2514 goto out_unsupported_cdb;
2515 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002516 cmd->t_task_lba = transport_lba_64(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002517 if (cdb[1] & 0x8)
2518 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002519 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2520 break;
2521 case XDWRITEREAD_10:
2522 if ((cmd->data_direction != DMA_TO_DEVICE) ||
Christoph Hellwig33c3faf2011-11-14 11:36:30 -05002523 !(cmd->se_cmd_flags & SCF_BIDI))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002524 goto out_invalid_cdb_field;
2525 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2526 if (sector_ret)
2527 goto out_unsupported_cdb;
2528 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002529 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002530 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002531
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002532 /*
2533 * Do now allow BIDI commands for passthrough mode.
2534 */
2535 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002536 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002537
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002538 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002539 * Setup BIDI XOR callback to be run after I/O completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002540 */
2541 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002542 if (cdb[1] & 0x8)
2543 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002544 break;
2545 case VARIABLE_LENGTH_CMD:
2546 service_action = get_unaligned_be16(&cdb[8]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002547 switch (service_action) {
2548 case XDWRITEREAD_32:
2549 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2550 if (sector_ret)
2551 goto out_unsupported_cdb;
2552 size = transport_get_size(sectors, cdb, cmd);
2553 /*
2554 * Use WRITE_32 and READ_32 opcodes for the emulated
2555 * XDWRITE_READ_32 logic.
2556 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002557 cmd->t_task_lba = transport_lba_64_ext(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002558 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2559
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002560 /*
2561 * Do now allow BIDI commands for passthrough mode.
2562 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002563 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002564 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002565
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002566 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002567 * Setup BIDI XOR callback to be run during after I/O
2568 * completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002569 */
2570 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002571 if (cdb[1] & 0x8)
2572 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002573 break;
2574 case WRITE_SAME_32:
2575 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2576 if (sector_ret)
2577 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002578
Andy Grover6708bb22011-06-08 10:36:43 -07002579 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002580 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07002581 else {
2582 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2583 " supported\n");
2584 goto out_invalid_cdb_field;
2585 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002586
Andy Grovera1d8b492011-05-02 17:12:10 -07002587 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002588 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2589
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002590 if (target_check_write_same_discard(&cdb[10], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08002591 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002592 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002593 cmd->execute_cmd = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002594 break;
2595 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002596 pr_err("VARIABLE_LENGTH_CMD service action"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002597 " 0x%04x not supported\n", service_action);
2598 goto out_unsupported_cdb;
2599 }
2600 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002601 case MAINTENANCE_IN:
Andy Grovere3d6f902011-07-19 08:55:10 +00002602 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002603 /* MAINTENANCE_IN from SCC-2 */
2604 /*
2605 * Check for emulated MI_REPORT_TARGET_PGS.
2606 */
Nicholas Bellingerba539742012-05-16 21:52:10 -07002607 if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS &&
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002608 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002609 cmd->execute_cmd =
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002610 target_emulate_report_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002611 }
2612 size = (cdb[6] << 24) | (cdb[7] << 16) |
2613 (cdb[8] << 8) | cdb[9];
2614 } else {
2615 /* GPCMD_SEND_KEY from multi media commands */
2616 size = (cdb[8] << 8) + cdb[9];
2617 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002618 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002619 break;
2620 case MODE_SELECT:
2621 size = cdb[4];
2622 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2623 break;
2624 case MODE_SELECT_10:
2625 size = (cdb[7] << 8) + cdb[8];
2626 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2627 break;
2628 case MODE_SENSE:
2629 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002630 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002631 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002632 cmd->execute_cmd = target_emulate_modesense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002633 break;
2634 case MODE_SENSE_10:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002635 size = (cdb[7] << 8) + cdb[8];
2636 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2637 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002638 cmd->execute_cmd = target_emulate_modesense;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002639 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002640 case GPCMD_READ_BUFFER_CAPACITY:
2641 case GPCMD_SEND_OPC:
2642 case LOG_SELECT:
2643 case LOG_SENSE:
2644 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002645 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002646 break;
2647 case READ_BLOCK_LIMITS:
2648 size = READ_BLOCK_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002649 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002650 break;
2651 case GPCMD_GET_CONFIGURATION:
2652 case GPCMD_READ_FORMAT_CAPACITIES:
2653 case GPCMD_READ_DISC_INFO:
2654 case GPCMD_READ_TRACK_RZONE_INFO:
2655 size = (cdb[7] << 8) + cdb[8];
2656 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2657 break;
2658 case PERSISTENT_RESERVE_IN:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002659 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002660 cmd->execute_cmd = target_scsi3_emulate_pr_in;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002661 size = (cdb[7] << 8) + cdb[8];
2662 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2663 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002664 case PERSISTENT_RESERVE_OUT:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002665 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002666 cmd->execute_cmd = target_scsi3_emulate_pr_out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002667 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002668 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002669 break;
2670 case GPCMD_MECHANISM_STATUS:
2671 case GPCMD_READ_DVD_STRUCTURE:
2672 size = (cdb[8] << 8) + cdb[9];
2673 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2674 break;
2675 case READ_POSITION:
2676 size = READ_POSITION_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002677 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002678 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002679 case MAINTENANCE_OUT:
Andy Grovere3d6f902011-07-19 08:55:10 +00002680 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002681 /* MAINTENANCE_OUT from SCC-2
2682 *
2683 * Check for emulated MO_SET_TARGET_PGS.
2684 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002685 if (cdb[1] == MO_SET_TARGET_PGS &&
2686 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002687 cmd->execute_cmd =
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002688 target_emulate_set_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002689 }
2690
2691 size = (cdb[6] << 24) | (cdb[7] << 16) |
2692 (cdb[8] << 8) | cdb[9];
2693 } else {
2694 /* GPCMD_REPORT_KEY from multi media commands */
2695 size = (cdb[8] << 8) + cdb[9];
2696 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002697 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002698 break;
2699 case INQUIRY:
2700 size = (cdb[3] << 8) + cdb[4];
2701 /*
2702 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2703 * See spc4r17 section 5.3
2704 */
Andy Grover5951146d2011-07-19 10:26:37 +00002705 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002706 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002707 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002708 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002709 cmd->execute_cmd = target_emulate_inquiry;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002710 break;
2711 case READ_BUFFER:
2712 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002713 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002714 break;
2715 case READ_CAPACITY:
2716 size = READ_CAP_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002717 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002718 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002719 cmd->execute_cmd = target_emulate_readcapacity;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002720 break;
2721 case READ_MEDIA_SERIAL_NUMBER:
2722 case SECURITY_PROTOCOL_IN:
2723 case SECURITY_PROTOCOL_OUT:
2724 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002725 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002726 break;
2727 case SERVICE_ACTION_IN:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002728 switch (cmd->t_task_cdb[1] & 0x1f) {
2729 case SAI_READ_CAPACITY_16:
2730 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002731 cmd->execute_cmd =
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002732 target_emulate_readcapacity_16;
2733 break;
2734 default:
2735 if (passthrough)
2736 break;
2737
2738 pr_err("Unsupported SA: 0x%02x\n",
2739 cmd->t_task_cdb[1] & 0x1f);
Roland Dreierb168fe82012-03-14 10:40:43 -07002740 goto out_invalid_cdb_field;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002741 }
2742 /*FALLTHROUGH*/
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002743 case ACCESS_CONTROL_IN:
2744 case ACCESS_CONTROL_OUT:
2745 case EXTENDED_COPY:
2746 case READ_ATTRIBUTE:
2747 case RECEIVE_COPY_RESULTS:
2748 case WRITE_ATTRIBUTE:
2749 size = (cdb[10] << 24) | (cdb[11] << 16) |
2750 (cdb[12] << 8) | cdb[13];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002751 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002752 break;
2753 case RECEIVE_DIAGNOSTIC:
2754 case SEND_DIAGNOSTIC:
2755 size = (cdb[3] << 8) | cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002756 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002757 break;
2758/* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2759#if 0
2760 case GPCMD_READ_CD:
2761 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2762 size = (2336 * sectors);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002763 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002764 break;
2765#endif
2766 case READ_TOC:
2767 size = cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002768 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002769 break;
2770 case REQUEST_SENSE:
2771 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002772 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002773 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002774 cmd->execute_cmd = target_emulate_request_sense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002775 break;
2776 case READ_ELEMENT_STATUS:
2777 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002778 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002779 break;
2780 case WRITE_BUFFER:
2781 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002782 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002783 break;
2784 case RESERVE:
2785 case RESERVE_10:
2786 /*
2787 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2788 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2789 */
2790 if (cdb[0] == RESERVE_10)
2791 size = (cdb[7] << 8) | cdb[8];
2792 else
2793 size = cmd->data_length;
2794
2795 /*
2796 * Setup the legacy emulated handler for SPC-2 and
2797 * >= SPC-3 compatible reservation handling (CRH=1)
2798 * Otherwise, we assume the underlying SCSI logic is
2799 * is running in SPC_PASSTHROUGH, and wants reservations
2800 * emulation disabled.
2801 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002802 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002803 cmd->execute_cmd = target_scsi2_reservation_reserve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002804 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2805 break;
2806 case RELEASE:
2807 case RELEASE_10:
2808 /*
2809 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2810 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2811 */
2812 if (cdb[0] == RELEASE_10)
2813 size = (cdb[7] << 8) | cdb[8];
2814 else
2815 size = cmd->data_length;
2816
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002817 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002818 cmd->execute_cmd = target_scsi2_reservation_release;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002819 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2820 break;
2821 case SYNCHRONIZE_CACHE:
Andy Grover8e94b8d2012-01-16 16:57:07 -08002822 case SYNCHRONIZE_CACHE_16:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002823 /*
2824 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2825 */
2826 if (cdb[0] == SYNCHRONIZE_CACHE) {
2827 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002828 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002829 } else {
2830 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002831 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002832 }
2833 if (sector_ret)
2834 goto out_unsupported_cdb;
2835
2836 size = transport_get_size(sectors, cdb, cmd);
2837 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2838
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002839 if (passthrough)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002840 break;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002841
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002842 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002843 * Check to ensure that LBA + Range does not exceed past end of
Nicholas Bellinger7abbe7f2011-08-10 18:41:14 -07002844 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002845 */
Nicholas Bellinger7abbe7f2011-08-10 18:41:14 -07002846 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
2847 if (transport_cmd_get_valid_sectors(cmd) < 0)
2848 goto out_invalid_cdb_field;
2849 }
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002850 cmd->execute_cmd = target_emulate_synchronize_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002851 break;
2852 case UNMAP:
2853 size = get_unaligned_be16(&cdb[7]);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002854 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002855 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002856 cmd->execute_cmd = target_emulate_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002857 break;
2858 case WRITE_SAME_16:
2859 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2860 if (sector_ret)
2861 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002862
Andy Grover6708bb22011-06-08 10:36:43 -07002863 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002864 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07002865 else {
2866 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2867 goto out_invalid_cdb_field;
2868 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002869
Nicholas Bellinger5db07532011-07-27 22:18:52 -07002870 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002871 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002872
2873 if (target_check_write_same_discard(&cdb[1], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08002874 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002875 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002876 cmd->execute_cmd = target_emulate_write_same;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002877 break;
2878 case WRITE_SAME:
2879 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2880 if (sector_ret)
2881 goto out_unsupported_cdb;
2882
2883 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002884 size = transport_get_size(1, cdb, cmd);
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002885 else {
2886 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2887 goto out_invalid_cdb_field;
2888 }
2889
2890 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
2891 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2892 /*
2893 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
2894 * of byte 1 bit 3 UNMAP instead of original reserved field
2895 */
2896 if (target_check_write_same_discard(&cdb[1], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08002897 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002898 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002899 cmd->execute_cmd = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002900 break;
2901 case ALLOW_MEDIUM_REMOVAL:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002902 case ERASE:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002903 case REZERO_UNIT:
2904 case SEEK_10:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002905 case SPACE:
2906 case START_STOP:
2907 case TEST_UNIT_READY:
2908 case VERIFY:
2909 case WRITE_FILEMARKS:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002910 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2911 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002912 cmd->execute_cmd = target_emulate_noop;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002913 break;
2914 case GPCMD_CLOSE_TRACK:
2915 case INITIALIZE_ELEMENT_STATUS:
2916 case GPCMD_LOAD_UNLOAD:
2917 case GPCMD_SET_SPEED:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002918 case MOVE_MEDIUM:
2919 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2920 break;
2921 case REPORT_LUNS:
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002922 cmd->execute_cmd = target_report_luns;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002923 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2924 /*
2925 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
2926 * See spc4r17 section 5.3
2927 */
Andy Grover5951146d2011-07-19 10:26:37 +00002928 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002929 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002930 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002931 break;
Stefan Hajnoczied0b2142012-04-30 16:35:13 +01002932 case GET_EVENT_STATUS_NOTIFICATION:
2933 size = (cdb[7] << 8) | cdb[8];
2934 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2935 break;
mengcong8da10932012-05-17 11:14:46 +08002936 case ATA_16:
2937 /* Only support ATA passthrough to pSCSI backends.. */
2938 if (!passthrough)
2939 goto out_unsupported_cdb;
2940
2941 /* T_LENGTH */
2942 switch (cdb[2] & 0x3) {
2943 case 0x0:
2944 sectors = 0;
2945 break;
2946 case 0x1:
2947 sectors = (((cdb[1] & 0x1) ? cdb[3] : 0) << 8) | cdb[4];
2948 break;
2949 case 0x2:
2950 sectors = (((cdb[1] & 0x1) ? cdb[5] : 0) << 8) | cdb[6];
2951 break;
2952 case 0x3:
2953 pr_err("T_LENGTH=0x3 not supported for ATA_16\n");
2954 goto out_invalid_cdb_field;
2955 }
2956
2957 /* BYTE_BLOCK */
2958 if (cdb[2] & 0x4) {
2959 /* BLOCK T_TYPE: 512 or sector */
2960 size = sectors * ((cdb[2] & 0x10) ?
2961 dev->se_sub_dev->se_dev_attrib.block_size : 512);
2962 } else {
2963 /* BYTE */
2964 size = sectors;
2965 }
2966 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2967 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002968 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002969 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002970 " 0x%02x, sending CHECK_CONDITION.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002971 cmd->se_tfo->get_fabric_name(), cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002972 goto out_unsupported_cdb;
2973 }
2974
Sebastian Andrzej Siewiorb0d79942012-01-10 14:16:59 +01002975 if (cmd->unknown_data_length)
2976 cmd->data_length = size;
2977
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002978 if (size != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07002979 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002980 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
Andy Grovere3d6f902011-07-19 08:55:10 +00002981 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002982 cmd->data_length, size, cdb[0]);
2983
2984 cmd->cmd_spdtl = size;
2985
2986 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002987 pr_err("Rejecting underflow/overflow"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002988 " WRITE data\n");
2989 goto out_invalid_cdb_field;
2990 }
2991 /*
2992 * Reject READ_* or WRITE_* with overflow/underflow for
2993 * type SCF_SCSI_DATA_SG_IO_CDB.
2994 */
Andy Grover6708bb22011-06-08 10:36:43 -07002995 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
2996 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002997 " CDB on non 512-byte sector setup subsystem"
Andy Grovere3d6f902011-07-19 08:55:10 +00002998 " plugin: %s\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002999 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3000 goto out_invalid_cdb_field;
3001 }
3002
3003 if (size > cmd->data_length) {
3004 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3005 cmd->residual_count = (size - cmd->data_length);
3006 } else {
3007 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3008 cmd->residual_count = (cmd->data_length - size);
3009 }
3010 cmd->data_length = size;
3011 }
3012
Nicholas Bellinger7b4deef2012-05-06 12:59:45 -07003013 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3014 if (sectors > su_dev->se_dev_attrib.fabric_max_sectors) {
3015 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
3016 " big sectors %u exceeds fabric_max_sectors:"
3017 " %u\n", cdb[0], sectors,
3018 su_dev->se_dev_attrib.fabric_max_sectors);
3019 goto out_invalid_cdb_field;
3020 }
3021 if (sectors > su_dev->se_dev_attrib.hw_max_sectors) {
3022 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
3023 " big sectors %u exceeds backend hw_max_sectors:"
3024 " %u\n", cdb[0], sectors,
3025 su_dev->se_dev_attrib.hw_max_sectors);
3026 goto out_invalid_cdb_field;
3027 }
Roland Dreier015487b2012-02-13 16:18:17 -08003028 }
3029
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003030 /* reject any command that we don't have a handler for */
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003031 if (!(passthrough || cmd->execute_cmd ||
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003032 (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
3033 goto out_unsupported_cdb;
3034
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003035 transport_set_supported_SAM_opcode(cmd);
3036 return ret;
3037
3038out_unsupported_cdb:
3039 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3040 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover5951146d2011-07-19 10:26:37 +00003041 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003042out_invalid_cdb_field:
3043 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3044 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00003045 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003046}
3047
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003048/*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003049 * Called from I/O completion to determine which dormant/delayed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003050 * and ordered cmds need to have their tasks added to the execution queue.
3051 */
3052static void transport_complete_task_attr(struct se_cmd *cmd)
3053{
Andy Grover5951146d2011-07-19 10:26:37 +00003054 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003055 struct se_cmd *cmd_p, *cmd_tmp;
3056 int new_active_tasks = 0;
3057
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003058 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003059 atomic_dec(&dev->simple_cmds);
3060 smp_mb__after_atomic_dec();
3061 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003062 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003063 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3064 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003065 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003066 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003067 pr_debug("Incremented dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003068 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3069 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003070 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003071 atomic_dec(&dev->dev_ordered_sync);
3072 smp_mb__after_atomic_dec();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003073
3074 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003075 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003076 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3077 }
3078 /*
3079 * Process all commands up to the last received
3080 * ORDERED task attribute which requires another blocking
3081 * boundary
3082 */
3083 spin_lock(&dev->delayed_cmd_lock);
3084 list_for_each_entry_safe(cmd_p, cmd_tmp,
Andy Grover5951146d2011-07-19 10:26:37 +00003085 &dev->delayed_cmd_list, se_delayed_node) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003086
Andy Grover5951146d2011-07-19 10:26:37 +00003087 list_del(&cmd_p->se_delayed_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003088 spin_unlock(&dev->delayed_cmd_lock);
3089
Andy Grover6708bb22011-06-08 10:36:43 -07003090 pr_debug("Calling add_tasks() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003091 " cmd_p: 0x%02x Task Attr: 0x%02x"
3092 " Dormant -> Active, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07003093 cmd_p->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003094 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3095
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003096 target_add_to_execute_list(cmd_p);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003097 new_active_tasks++;
3098
3099 spin_lock(&dev->delayed_cmd_lock);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003100 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003101 break;
3102 }
3103 spin_unlock(&dev->delayed_cmd_lock);
3104 /*
3105 * If new tasks have become active, wake up the transport thread
3106 * to do the processing of the Active tasks.
3107 */
3108 if (new_active_tasks != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003109 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003110}
3111
Christoph Hellwige057f532011-10-17 13:56:41 -04003112static void transport_complete_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003113{
3114 int ret = 0;
3115
Christoph Hellwige057f532011-10-17 13:56:41 -04003116 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3117 transport_complete_task_attr(cmd);
3118
3119 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3120 ret = cmd->se_tfo->queue_status(cmd);
3121 if (ret)
3122 goto out;
3123 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003124
3125 switch (cmd->data_direction) {
3126 case DMA_FROM_DEVICE:
3127 ret = cmd->se_tfo->queue_data_in(cmd);
3128 break;
3129 case DMA_TO_DEVICE:
Andy Groverec98f782011-07-20 19:28:46 +00003130 if (cmd->t_bidi_data_sg) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003131 ret = cmd->se_tfo->queue_data_in(cmd);
3132 if (ret < 0)
Christoph Hellwige057f532011-10-17 13:56:41 -04003133 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003134 }
3135 /* Fall through for DMA_TO_DEVICE */
3136 case DMA_NONE:
3137 ret = cmd->se_tfo->queue_status(cmd);
3138 break;
3139 default:
3140 break;
3141 }
3142
Christoph Hellwige057f532011-10-17 13:56:41 -04003143out:
3144 if (ret < 0) {
3145 transport_handle_queue_full(cmd, cmd->se_dev);
3146 return;
3147 }
3148 transport_lun_remove_cmd(cmd);
3149 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003150}
3151
3152static void transport_handle_queue_full(
3153 struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -04003154 struct se_device *dev)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003155{
3156 spin_lock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003157 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3158 atomic_inc(&dev->dev_qf_count);
3159 smp_mb__after_atomic_inc();
3160 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3161
3162 schedule_work(&cmd->se_dev->qf_work_queue);
3163}
3164
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003165static void target_complete_ok_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003166{
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003167 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003168 int reason = 0, ret;
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003169
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003170 /*
3171 * Check if we need to move delayed/dormant tasks from cmds on the
3172 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3173 * Attribute.
3174 */
Andy Grover5951146d2011-07-19 10:26:37 +00003175 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003176 transport_complete_task_attr(cmd);
3177 /*
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003178 * Check to schedule QUEUE_FULL work, or execute an existing
3179 * cmd->transport_qf_callback()
3180 */
3181 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3182 schedule_work(&cmd->se_dev->qf_work_queue);
3183
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003184 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003185 * Check if we need to retrieve a sense buffer from
3186 * the struct se_cmd in question.
3187 */
3188 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3189 if (transport_get_sense_data(cmd) < 0)
3190 reason = TCM_NON_EXISTENT_LUN;
3191
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003192 if (cmd->scsi_status) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003193 ret = transport_send_check_condition_and_sense(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003194 cmd, reason, 1);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003195 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003196 goto queue_full;
3197
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003198 transport_lun_remove_cmd(cmd);
3199 transport_cmd_check_stop_to_fabric(cmd);
3200 return;
3201 }
3202 }
3203 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003204 * Check for a callback, used by amongst other things
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003205 * XDWRITE_READ_10 emulation.
3206 */
3207 if (cmd->transport_complete_callback)
3208 cmd->transport_complete_callback(cmd);
3209
3210 switch (cmd->data_direction) {
3211 case DMA_FROM_DEVICE:
3212 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003213 if (cmd->se_lun->lun_sep) {
3214 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003215 cmd->data_length;
3216 }
3217 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003218
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003219 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003220 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003221 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003222 break;
3223 case DMA_TO_DEVICE:
3224 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003225 if (cmd->se_lun->lun_sep) {
3226 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003227 cmd->data_length;
3228 }
3229 spin_unlock(&cmd->se_lun->lun_sep_lock);
3230 /*
3231 * Check if we need to send READ payload for BIDI-COMMAND
3232 */
Andy Groverec98f782011-07-20 19:28:46 +00003233 if (cmd->t_bidi_data_sg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003234 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003235 if (cmd->se_lun->lun_sep) {
3236 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003237 cmd->data_length;
3238 }
3239 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003240 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003241 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003242 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003243 break;
3244 }
3245 /* Fall through for DMA_TO_DEVICE */
3246 case DMA_NONE:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003247 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003248 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003249 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003250 break;
3251 default:
3252 break;
3253 }
3254
3255 transport_lun_remove_cmd(cmd);
3256 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003257 return;
3258
3259queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003260 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003261 " data_direction: %d\n", cmd, cmd->data_direction);
Christoph Hellwige057f532011-10-17 13:56:41 -04003262 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3263 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003264}
3265
Andy Grover6708bb22011-06-08 10:36:43 -07003266static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003267{
Andy Groverec98f782011-07-20 19:28:46 +00003268 struct scatterlist *sg;
Andy Groverec98f782011-07-20 19:28:46 +00003269 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003270
Andy Grover6708bb22011-06-08 10:36:43 -07003271 for_each_sg(sgl, sg, nents, count)
3272 __free_page(sg_page(sg));
3273
3274 kfree(sgl);
3275}
3276
3277static inline void transport_free_pages(struct se_cmd *cmd)
3278{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003279 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
Andy Grover6708bb22011-06-08 10:36:43 -07003280 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003281
Andy Grover6708bb22011-06-08 10:36:43 -07003282 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003283 cmd->t_data_sg = NULL;
3284 cmd->t_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003285
Andy Grover6708bb22011-06-08 10:36:43 -07003286 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003287 cmd->t_bidi_data_sg = NULL;
3288 cmd->t_bidi_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003289}
3290
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003291/**
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003292 * transport_release_cmd - free a command
3293 * @cmd: command to free
3294 *
3295 * This routine unconditionally frees a command, and reference counting
3296 * or list removal must be done in the caller.
3297 */
3298static void transport_release_cmd(struct se_cmd *cmd)
3299{
3300 BUG_ON(!cmd->se_tfo);
3301
Andy Groverc8e31f22012-01-19 13:39:17 -08003302 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003303 core_tmr_release_req(cmd->se_tmr_req);
3304 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3305 kfree(cmd->t_task_cdb);
3306 /*
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003307 * If this cmd has been setup with target_get_sess_cmd(), drop
3308 * the kref and call ->release_cmd() in kref callback.
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003309 */
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003310 if (cmd->check_release != 0) {
3311 target_put_sess_cmd(cmd->se_sess, cmd);
3312 return;
3313 }
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003314 cmd->se_tfo->release_cmd(cmd);
3315}
3316
3317/**
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003318 * transport_put_cmd - release a reference to a command
3319 * @cmd: command to release
3320 *
3321 * This routine releases our reference to the command and frees it if possible.
3322 */
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003323static void transport_put_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003324{
3325 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003326
Andy Grovera1d8b492011-05-02 17:12:10 -07003327 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003328 if (atomic_read(&cmd->t_fe_count)) {
3329 if (!atomic_dec_and_test(&cmd->t_fe_count))
3330 goto out_busy;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003331 }
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003332
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003333 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
3334 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003335 target_remove_from_state_list(cmd);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003336 }
Andy Grovera1d8b492011-05-02 17:12:10 -07003337 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003338
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003339 transport_free_pages(cmd);
Christoph Hellwig31afc392011-09-13 23:08:11 +02003340 transport_release_cmd(cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003341 return;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003342out_busy:
3343 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003344}
3345
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003346/*
Andy Groverec98f782011-07-20 19:28:46 +00003347 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3348 * allocating in the core.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003349 * @cmd: Associated se_cmd descriptor
3350 * @mem: SGL style memory for TCM WRITE / READ
3351 * @sg_mem_num: Number of SGL elements
3352 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3353 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3354 *
3355 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3356 * of parameters.
3357 */
3358int transport_generic_map_mem_to_cmd(
3359 struct se_cmd *cmd,
Andy Grover5951146d2011-07-19 10:26:37 +00003360 struct scatterlist *sgl,
3361 u32 sgl_count,
3362 struct scatterlist *sgl_bidi,
3363 u32 sgl_bidi_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003364{
Andy Grover5951146d2011-07-19 10:26:37 +00003365 if (!sgl || !sgl_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003366 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003367
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003368 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3369 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
Nicholas Bellingerfef58a62011-11-15 22:13:24 -08003370 /*
3371 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
3372 * scatterlists already have been set to follow what the fabric
3373 * passes for the original expected data transfer length.
3374 */
3375 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
3376 pr_warn("Rejecting SCSI DATA overflow for fabric using"
3377 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
3378 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3379 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3380 return -EINVAL;
3381 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003382
Andy Groverec98f782011-07-20 19:28:46 +00003383 cmd->t_data_sg = sgl;
3384 cmd->t_data_nents = sgl_count;
3385
Andy Grover5951146d2011-07-19 10:26:37 +00003386 if (sgl_bidi && sgl_bidi_count) {
Andy Groverec98f782011-07-20 19:28:46 +00003387 cmd->t_bidi_data_sg = sgl_bidi;
3388 cmd->t_bidi_data_nents = sgl_bidi_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003389 }
3390 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003391 }
3392
3393 return 0;
3394}
3395EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3396
Andy Grover49493142012-01-16 16:57:08 -08003397void *transport_kmap_data_sg(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003398{
Andy Groverec98f782011-07-20 19:28:46 +00003399 struct scatterlist *sg = cmd->t_data_sg;
Andy Grover49493142012-01-16 16:57:08 -08003400 struct page **pages;
3401 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003402
Andy Groverec98f782011-07-20 19:28:46 +00003403 BUG_ON(!sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003404 /*
Andy Groverec98f782011-07-20 19:28:46 +00003405 * We need to take into account a possible offset here for fabrics like
3406 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3407 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
Andy Grover05d1c7c2011-07-20 19:13:28 +00003408 */
Andy Grover49493142012-01-16 16:57:08 -08003409 if (!cmd->t_data_nents)
3410 return NULL;
3411 else if (cmd->t_data_nents == 1)
3412 return kmap(sg_page(sg)) + sg->offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003413
Andy Grover49493142012-01-16 16:57:08 -08003414 /* >1 page. use vmap */
3415 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
3416 if (!pages)
3417 return NULL;
3418
3419 /* convert sg[] to pages[] */
3420 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
3421 pages[i] = sg_page(sg);
3422 }
3423
3424 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
3425 kfree(pages);
3426 if (!cmd->t_data_vmap)
3427 return NULL;
3428
3429 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003430}
Andy Grover49493142012-01-16 16:57:08 -08003431EXPORT_SYMBOL(transport_kmap_data_sg);
3432
3433void transport_kunmap_data_sg(struct se_cmd *cmd)
3434{
Andy Grovera1edf9c2012-02-09 12:18:06 -08003435 if (!cmd->t_data_nents) {
Andy Grover49493142012-01-16 16:57:08 -08003436 return;
Andy Grovera1edf9c2012-02-09 12:18:06 -08003437 } else if (cmd->t_data_nents == 1) {
Andy Grover49493142012-01-16 16:57:08 -08003438 kunmap(sg_page(cmd->t_data_sg));
Andy Grovera1edf9c2012-02-09 12:18:06 -08003439 return;
3440 }
Andy Grover49493142012-01-16 16:57:08 -08003441
3442 vunmap(cmd->t_data_vmap);
3443 cmd->t_data_vmap = NULL;
3444}
3445EXPORT_SYMBOL(transport_kunmap_data_sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003446
3447static int
3448transport_generic_get_mem(struct se_cmd *cmd)
3449{
Andy Groverec98f782011-07-20 19:28:46 +00003450 u32 length = cmd->data_length;
3451 unsigned int nents;
3452 struct page *page;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003453 gfp_t zero_flag;
Andy Groverec98f782011-07-20 19:28:46 +00003454 int i = 0;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003455
Andy Groverec98f782011-07-20 19:28:46 +00003456 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3457 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3458 if (!cmd->t_data_sg)
Andy Grovere3d6f902011-07-19 08:55:10 +00003459 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003460
Andy Groverec98f782011-07-20 19:28:46 +00003461 cmd->t_data_nents = nents;
3462 sg_init_table(cmd->t_data_sg, nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003463
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003464 zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
3465
Andy Groverec98f782011-07-20 19:28:46 +00003466 while (length) {
3467 u32 page_len = min_t(u32, length, PAGE_SIZE);
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003468 page = alloc_page(GFP_KERNEL | zero_flag);
Andy Groverec98f782011-07-20 19:28:46 +00003469 if (!page)
3470 goto out;
3471
3472 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3473 length -= page_len;
3474 i++;
3475 }
3476 return 0;
3477
3478out:
3479 while (i >= 0) {
3480 __free_page(sg_page(&cmd->t_data_sg[i]));
3481 i--;
3482 }
3483 kfree(cmd->t_data_sg);
3484 cmd->t_data_sg = NULL;
3485 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003486}
3487
Andy Grovera1d8b492011-05-02 17:12:10 -07003488/*
Andy Groverb16a35b2012-04-03 15:51:21 -07003489 * Allocate any required resources to execute the command. For writes we
3490 * might not have the payload yet, so notify the fabric via a call to
3491 * ->write_pending instead. Otherwise place it on the execution queue.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003492 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003493int transport_generic_new_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003494{
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003495 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003496 int ret = 0;
3497
3498 /*
3499 * Determine is the TCM fabric module has already allocated physical
3500 * memory, and is directly calling transport_generic_map_mem_to_cmd()
Andy Groverec98f782011-07-20 19:28:46 +00003501 * beforehand.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003502 */
Andy Groverec98f782011-07-20 19:28:46 +00003503 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3504 cmd->data_length) {
Andy Grover05d1c7c2011-07-20 19:13:28 +00003505 ret = transport_generic_get_mem(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003506 if (ret < 0)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003507 goto out_fail;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003508 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003509
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04003510 /* Workaround for handling zero-length control CDBs */
3511 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3512 !cmd->data_length) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003513 spin_lock_irq(&cmd->t_state_lock);
Roland Dreier410f6702011-11-22 13:51:32 -08003514 cmd->t_state = TRANSPORT_COMPLETE;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003515 cmd->transport_state |= CMD_T_ACTIVE;
3516 spin_unlock_irq(&cmd->t_state_lock);
Nicholas Bellinger91ec1d32012-01-13 12:01:34 -08003517
3518 if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
3519 u8 ua_asc = 0, ua_ascq = 0;
3520
3521 core_scsi3_ua_clear_for_request_sense(cmd,
3522 &ua_asc, &ua_ascq);
3523 }
3524
Roland Dreier410f6702011-11-22 13:51:32 -08003525 INIT_WORK(&cmd->work, target_complete_ok_work);
3526 queue_work(target_completion_wq, &cmd->work);
3527 return 0;
3528 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003529
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04003530 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3531 struct se_dev_attrib *attr = &dev->se_sub_dev->se_dev_attrib;
3532
3533 if (transport_cmd_get_valid_sectors(cmd) < 0)
3534 return -EINVAL;
3535
3536 BUG_ON(cmd->data_length % attr->block_size);
3537 BUG_ON(DIV_ROUND_UP(cmd->data_length, attr->block_size) >
Nicholas Bellinger11e764b2012-05-09 12:42:09 -07003538 attr->hw_max_sectors);
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003539 }
3540
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04003541 atomic_inc(&cmd->t_fe_count);
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04003542
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003543 /*
Christoph Hellwig5787cac2012-04-24 00:25:06 -04003544 * For WRITEs, let the fabric know its buffer is ready.
3545 *
3546 * The command will be added to the execution queue after its write
3547 * data has arrived.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003548 */
3549 if (cmd->data_direction == DMA_TO_DEVICE) {
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003550 target_add_to_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003551 return transport_generic_write_pending(cmd);
3552 }
3553 /*
Christoph Hellwig5787cac2012-04-24 00:25:06 -04003554 * Everything else but a WRITE, add the command to the execution queue.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003555 */
3556 transport_execute_tasks(cmd);
3557 return 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003558
3559out_fail:
3560 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3561 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3562 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003563}
Andy Grovera1d8b492011-05-02 17:12:10 -07003564EXPORT_SYMBOL(transport_generic_new_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003565
3566/* transport_generic_process_write():
3567 *
3568 *
3569 */
3570void transport_generic_process_write(struct se_cmd *cmd)
3571{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003572 transport_execute_tasks(cmd);
3573}
3574EXPORT_SYMBOL(transport_generic_process_write);
3575
Christoph Hellwige057f532011-10-17 13:56:41 -04003576static void transport_write_pending_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003577{
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003578 int ret;
3579
3580 ret = cmd->se_tfo->write_pending(cmd);
3581 if (ret == -EAGAIN || ret == -ENOMEM) {
Christoph Hellwige057f532011-10-17 13:56:41 -04003582 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3583 cmd);
3584 transport_handle_queue_full(cmd, cmd->se_dev);
3585 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003586}
3587
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003588static int transport_generic_write_pending(struct se_cmd *cmd)
3589{
3590 unsigned long flags;
3591 int ret;
3592
Andy Grovera1d8b492011-05-02 17:12:10 -07003593 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003594 cmd->t_state = TRANSPORT_WRITE_PENDING;
Andy Grovera1d8b492011-05-02 17:12:10 -07003595 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003596
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003597 /*
3598 * Clear the se_cmd for WRITE_PENDING status in order to set
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003599 * CMD_T_ACTIVE so that transport_generic_handle_data can be called
3600 * from HW target mode interrupt code. This is safe to be called
3601 * with transport_off=1 before the cmd->se_tfo->write_pending
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003602 * because the se_cmd->se_lun pointer is not being cleared.
3603 */
3604 transport_cmd_check_stop(cmd, 1, 0);
3605
3606 /*
3607 * Call the fabric write_pending function here to let the
3608 * frontend know that WRITE buffers are ready.
3609 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003610 ret = cmd->se_tfo->write_pending(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003611 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003612 goto queue_full;
3613 else if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003614 return ret;
3615
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003616 return 1;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003617
3618queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003619 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003620 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
Christoph Hellwige057f532011-10-17 13:56:41 -04003621 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003622 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003623}
3624
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003625void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003626{
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003627 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
Andy Groverc8e31f22012-01-19 13:39:17 -08003628 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003629 transport_wait_for_tasks(cmd);
3630
Christoph Hellwig35462972011-05-31 23:56:57 -04003631 transport_release_cmd(cmd);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003632 } else {
3633 if (wait_for_tasks)
3634 transport_wait_for_tasks(cmd);
3635
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003636 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
3637
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +02003638 if (cmd->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003639 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003640
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003641 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003642 }
3643}
3644EXPORT_SYMBOL(transport_generic_free_cmd);
3645
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003646/* target_get_sess_cmd - Add command to active ->sess_cmd_list
3647 * @se_sess: session to reference
3648 * @se_cmd: command descriptor to add
Nicholas Bellingera6360782011-11-18 20:36:22 -08003649 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003650 */
Nicholas Bellingera6360782011-11-18 20:36:22 -08003651void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
3652 bool ack_kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003653{
3654 unsigned long flags;
3655
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003656 kref_init(&se_cmd->cmd_kref);
Nicholas Bellingera6360782011-11-18 20:36:22 -08003657 /*
3658 * Add a second kref if the fabric caller is expecting to handle
3659 * fabric acknowledgement that requires two target_put_sess_cmd()
3660 * invocations before se_cmd descriptor release.
3661 */
Nicholas Bellinger86715562012-02-13 01:07:22 -08003662 if (ack_kref == true) {
Nicholas Bellingera6360782011-11-18 20:36:22 -08003663 kref_get(&se_cmd->cmd_kref);
Nicholas Bellinger86715562012-02-13 01:07:22 -08003664 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
3665 }
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003666
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003667 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3668 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
3669 se_cmd->check_release = 1;
3670 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3671}
3672EXPORT_SYMBOL(target_get_sess_cmd);
3673
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003674static void target_release_cmd_kref(struct kref *kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003675{
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003676 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
3677 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003678 unsigned long flags;
3679
3680 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3681 if (list_empty(&se_cmd->se_cmd_list)) {
3682 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Nicholas Bellingerffc32d52012-02-13 02:35:01 -08003683 se_cmd->se_tfo->release_cmd(se_cmd);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003684 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003685 }
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003686 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
3687 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3688 complete(&se_cmd->cmd_wait_comp);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003689 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003690 }
3691 list_del(&se_cmd->se_cmd_list);
3692 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3693
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003694 se_cmd->se_tfo->release_cmd(se_cmd);
3695}
3696
3697/* target_put_sess_cmd - Check for active I/O shutdown via kref_put
3698 * @se_sess: session to reference
3699 * @se_cmd: command descriptor to drop
3700 */
3701int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
3702{
3703 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003704}
3705EXPORT_SYMBOL(target_put_sess_cmd);
3706
3707/* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
3708 * @se_sess: session to split
3709 */
3710void target_splice_sess_cmd_list(struct se_session *se_sess)
3711{
3712 struct se_cmd *se_cmd;
3713 unsigned long flags;
3714
3715 WARN_ON(!list_empty(&se_sess->sess_wait_list));
3716 INIT_LIST_HEAD(&se_sess->sess_wait_list);
3717
3718 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3719 se_sess->sess_tearing_down = 1;
3720
3721 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
3722
3723 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
3724 se_cmd->cmd_wait_set = 1;
3725
3726 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3727}
3728EXPORT_SYMBOL(target_splice_sess_cmd_list);
3729
3730/* target_wait_for_sess_cmds - Wait for outstanding descriptors
3731 * @se_sess: session to wait for active I/O
3732 * @wait_for_tasks: Make extra transport_wait_for_tasks call
3733 */
3734void target_wait_for_sess_cmds(
3735 struct se_session *se_sess,
3736 int wait_for_tasks)
3737{
3738 struct se_cmd *se_cmd, *tmp_cmd;
3739 bool rc = false;
3740
3741 list_for_each_entry_safe(se_cmd, tmp_cmd,
3742 &se_sess->sess_wait_list, se_cmd_list) {
3743 list_del(&se_cmd->se_cmd_list);
3744
3745 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
3746 " %d\n", se_cmd, se_cmd->t_state,
3747 se_cmd->se_tfo->get_cmd_state(se_cmd));
3748
3749 if (wait_for_tasks) {
3750 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
3751 " fabric state: %d\n", se_cmd, se_cmd->t_state,
3752 se_cmd->se_tfo->get_cmd_state(se_cmd));
3753
3754 rc = transport_wait_for_tasks(se_cmd);
3755
3756 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
3757 " fabric state: %d\n", se_cmd, se_cmd->t_state,
3758 se_cmd->se_tfo->get_cmd_state(se_cmd));
3759 }
3760
3761 if (!rc) {
3762 wait_for_completion(&se_cmd->cmd_wait_comp);
3763 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
3764 " fabric state: %d\n", se_cmd, se_cmd->t_state,
3765 se_cmd->se_tfo->get_cmd_state(se_cmd));
3766 }
3767
3768 se_cmd->se_tfo->release_cmd(se_cmd);
3769 }
3770}
3771EXPORT_SYMBOL(target_wait_for_sess_cmds);
3772
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003773/* transport_lun_wait_for_tasks():
3774 *
3775 * Called from ConfigFS context to stop the passed struct se_cmd to allow
3776 * an struct se_lun to be successfully shutdown.
3777 */
3778static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
3779{
3780 unsigned long flags;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003781 int ret = 0;
3782
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003783 /*
3784 * If the frontend has already requested this struct se_cmd to
3785 * be stopped, we can safely ignore this struct se_cmd.
3786 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003787 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003788 if (cmd->transport_state & CMD_T_STOP) {
3789 cmd->transport_state &= ~CMD_T_LUN_STOP;
3790
3791 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
3792 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07003793 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003794 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovere3d6f902011-07-19 08:55:10 +00003795 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003796 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003797 cmd->transport_state |= CMD_T_LUN_FE_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07003798 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003799
Andy Grover5951146d2011-07-19 10:26:37 +00003800 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003801
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003802 // XXX: audit task_flags checks.
3803 spin_lock_irqsave(&cmd->t_state_lock, flags);
3804 if ((cmd->transport_state & CMD_T_BUSY) &&
3805 (cmd->transport_state & CMD_T_SENT)) {
3806 if (!target_stop_cmd(cmd, &flags))
3807 ret++;
Dan Carpenter23019172012-05-08 13:28:59 +03003808 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003809 } else {
3810 spin_unlock_irqrestore(&cmd->t_state_lock,
3811 flags);
3812 target_remove_from_execute_list(cmd);
3813 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003814
Christoph Hellwig785fdf72012-04-24 00:25:04 -04003815 pr_debug("ConfigFS: cmd: %p stop tasks ret:"
3816 " %d\n", cmd, ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003817 if (!ret) {
Andy Grover6708bb22011-06-08 10:36:43 -07003818 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003819 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07003820 wait_for_completion(&cmd->transport_lun_stop_comp);
Andy Grover6708bb22011-06-08 10:36:43 -07003821 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003822 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003823 }
Christoph Hellwig3df8d402011-10-17 13:56:43 -04003824 transport_remove_cmd_from_queue(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003825
3826 return 0;
3827}
3828
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003829static void __transport_clear_lun_from_sessions(struct se_lun *lun)
3830{
3831 struct se_cmd *cmd = NULL;
3832 unsigned long lun_flags, cmd_flags;
3833 /*
3834 * Do exception processing and return CHECK_CONDITION status to the
3835 * Initiator Port.
3836 */
3837 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
Andy Grover5951146d2011-07-19 10:26:37 +00003838 while (!list_empty(&lun->lun_cmd_list)) {
3839 cmd = list_first_entry(&lun->lun_cmd_list,
3840 struct se_cmd, se_lun_node);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -05003841 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003842
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003843 /*
3844 * This will notify iscsi_target_transport.c:
3845 * transport_cmd_check_stop() that a LUN shutdown is in
3846 * progress for the iscsi_cmd_t.
3847 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003848 spin_lock(&cmd->t_state_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07003849 pr_debug("SE_LUN[%d] - Setting cmd->transport"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003850 "_lun_stop for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003851 cmd->se_lun->unpacked_lun,
3852 cmd->se_tfo->get_task_tag(cmd));
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003853 cmd->transport_state |= CMD_T_LUN_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07003854 spin_unlock(&cmd->t_state_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003855
3856 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
3857
Andy Grover6708bb22011-06-08 10:36:43 -07003858 if (!cmd->se_lun) {
3859 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003860 cmd->se_tfo->get_task_tag(cmd),
3861 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003862 BUG();
3863 }
3864 /*
3865 * If the Storage engine still owns the iscsi_cmd_t, determine
3866 * and/or stop its context.
3867 */
Andy Grover6708bb22011-06-08 10:36:43 -07003868 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
Andy Grovere3d6f902011-07-19 08:55:10 +00003869 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
3870 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003871
Andy Grovere3d6f902011-07-19 08:55:10 +00003872 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003873 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
3874 continue;
3875 }
3876
Andy Grover6708bb22011-06-08 10:36:43 -07003877 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003878 "_wait_for_tasks(): SUCCESS\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003879 cmd->se_lun->unpacked_lun,
3880 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003881
Andy Grovera1d8b492011-05-02 17:12:10 -07003882 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003883 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
Andy Grovera1d8b492011-05-02 17:12:10 -07003884 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003885 goto check_cond;
3886 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003887 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003888 target_remove_from_state_list(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07003889 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003890
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003891 /*
3892 * The Storage engine stopped this struct se_cmd before it was
3893 * send to the fabric frontend for delivery back to the
3894 * Initiator Node. Return this SCSI CDB back with an
3895 * CHECK_CONDITION status.
3896 */
3897check_cond:
3898 transport_send_check_condition_and_sense(cmd,
3899 TCM_NON_EXISTENT_LUN, 0);
3900 /*
3901 * If the fabric frontend is waiting for this iscsi_cmd_t to
3902 * be released, notify the waiting thread now that LU has
3903 * finished accessing it.
3904 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003905 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003906 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07003907 pr_debug("SE_LUN[%d] - Detected FE stop for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003908 " struct se_cmd: %p ITT: 0x%08x\n",
3909 lun->unpacked_lun,
Andy Grovere3d6f902011-07-19 08:55:10 +00003910 cmd, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003911
Andy Grovera1d8b492011-05-02 17:12:10 -07003912 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003913 cmd_flags);
3914 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovera1d8b492011-05-02 17:12:10 -07003915 complete(&cmd->transport_lun_fe_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003916 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
3917 continue;
3918 }
Andy Grover6708bb22011-06-08 10:36:43 -07003919 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003920 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003921
Andy Grovera1d8b492011-05-02 17:12:10 -07003922 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003923 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
3924 }
3925 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
3926}
3927
3928static int transport_clear_lun_thread(void *p)
3929{
Jörn Engel8359cf42011-11-24 02:05:51 +01003930 struct se_lun *lun = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003931
3932 __transport_clear_lun_from_sessions(lun);
3933 complete(&lun->lun_shutdown_comp);
3934
3935 return 0;
3936}
3937
3938int transport_clear_lun_from_sessions(struct se_lun *lun)
3939{
3940 struct task_struct *kt;
3941
Andy Grover5951146d2011-07-19 10:26:37 +00003942 kt = kthread_run(transport_clear_lun_thread, lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003943 "tcm_cl_%u", lun->unpacked_lun);
3944 if (IS_ERR(kt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003945 pr_err("Unable to start clear_lun thread\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00003946 return PTR_ERR(kt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003947 }
3948 wait_for_completion(&lun->lun_shutdown_comp);
3949
3950 return 0;
3951}
3952
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003953/**
3954 * transport_wait_for_tasks - wait for completion to occur
3955 * @cmd: command to wait
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003956 *
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003957 * Called from frontend fabric context to wait for storage engine
3958 * to pause and/or release frontend generated struct se_cmd.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003959 */
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003960bool transport_wait_for_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003961{
3962 unsigned long flags;
3963
Andy Grovera1d8b492011-05-02 17:12:10 -07003964 spin_lock_irqsave(&cmd->t_state_lock, flags);
Andy Groverc8e31f22012-01-19 13:39:17 -08003965 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
3966 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003967 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003968 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003969 }
3970 /*
3971 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
3972 * has been set in transport_set_supported_SAM_opcode().
3973 */
Andy Groverc8e31f22012-01-19 13:39:17 -08003974 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
3975 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003976 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003977 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003978 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003979 /*
3980 * If we are already stopped due to an external event (ie: LUN shutdown)
3981 * sleep until the connection can have the passed struct se_cmd back.
Andy Grovera1d8b492011-05-02 17:12:10 -07003982 * The cmd->transport_lun_stopped_sem will be upped by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003983 * transport_clear_lun_from_sessions() once the ConfigFS context caller
3984 * has completed its operation on the struct se_cmd.
3985 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003986 if (cmd->transport_state & CMD_T_LUN_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07003987 pr_debug("wait_for_tasks: Stopping"
Andy Grovere3d6f902011-07-19 08:55:10 +00003988 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003989 "_stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003990 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003991 /*
3992 * There is a special case for WRITES where a FE exception +
3993 * LUN shutdown means ConfigFS context is still sleeping on
3994 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
3995 * We go ahead and up transport_lun_stop_comp just to be sure
3996 * here.
3997 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003998 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3999 complete(&cmd->transport_lun_stop_comp);
4000 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4001 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004002
Christoph Hellwigcf572a92012-04-24 00:25:05 -04004003 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004004 /*
4005 * At this point, the frontend who was the originator of this
4006 * struct se_cmd, now owns the structure and can be released through
4007 * normal means below.
4008 */
Andy Grover6708bb22011-06-08 10:36:43 -07004009 pr_debug("wait_for_tasks: Stopped"
Andy Grovere3d6f902011-07-19 08:55:10 +00004010 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004011 "stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004012 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004013
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004014 cmd->transport_state &= ~CMD_T_LUN_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004015 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004016
Nicholas Bellinger3d289342012-02-13 02:38:14 -08004017 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004018 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004019 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004020 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004021
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004022 cmd->transport_state |= CMD_T_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004023
Andy Grover6708bb22011-06-08 10:36:43 -07004024 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004025 " i_state: %d, t_state: %d, CMD_T_STOP\n",
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004026 cmd, cmd->se_tfo->get_task_tag(cmd),
4027 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004028
Andy Grovera1d8b492011-05-02 17:12:10 -07004029 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004030
Andy Grover5951146d2011-07-19 10:26:37 +00004031 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004032
Andy Grovera1d8b492011-05-02 17:12:10 -07004033 wait_for_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004034
Andy Grovera1d8b492011-05-02 17:12:10 -07004035 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004036 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004037
Andy Grover6708bb22011-06-08 10:36:43 -07004038 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
Andy Grovera1d8b492011-05-02 17:12:10 -07004039 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004040 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004041
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004042 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004043
4044 return true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004045}
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004046EXPORT_SYMBOL(transport_wait_for_tasks);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004047
4048static int transport_get_sense_codes(
4049 struct se_cmd *cmd,
4050 u8 *asc,
4051 u8 *ascq)
4052{
4053 *asc = cmd->scsi_asc;
4054 *ascq = cmd->scsi_ascq;
4055
4056 return 0;
4057}
4058
4059static int transport_set_sense_codes(
4060 struct se_cmd *cmd,
4061 u8 asc,
4062 u8 ascq)
4063{
4064 cmd->scsi_asc = asc;
4065 cmd->scsi_ascq = ascq;
4066
4067 return 0;
4068}
4069
4070int transport_send_check_condition_and_sense(
4071 struct se_cmd *cmd,
4072 u8 reason,
4073 int from_transport)
4074{
4075 unsigned char *buffer = cmd->sense_buffer;
4076 unsigned long flags;
4077 int offset;
4078 u8 asc = 0, ascq = 0;
4079
Andy Grovera1d8b492011-05-02 17:12:10 -07004080 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004081 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004082 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004083 return 0;
4084 }
4085 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
Andy Grovera1d8b492011-05-02 17:12:10 -07004086 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004087
4088 if (!reason && from_transport)
4089 goto after_reason;
4090
4091 if (!from_transport)
4092 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4093 /*
4094 * Data Segment and SenseLength of the fabric response PDU.
4095 *
4096 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4097 * from include/scsi/scsi_cmnd.h
4098 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004099 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004100 TRANSPORT_SENSE_BUFFER);
4101 /*
4102 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4103 * SENSE KEY values from include/scsi/scsi.h
4104 */
4105 switch (reason) {
4106 case TCM_NON_EXISTENT_LUN:
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004107 /* CURRENT ERROR */
4108 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004109 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004110 /* ILLEGAL REQUEST */
4111 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4112 /* LOGICAL UNIT NOT SUPPORTED */
4113 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4114 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004115 case TCM_UNSUPPORTED_SCSI_OPCODE:
4116 case TCM_SECTOR_COUNT_TOO_MANY:
4117 /* CURRENT ERROR */
4118 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004119 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004120 /* ILLEGAL REQUEST */
4121 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4122 /* INVALID COMMAND OPERATION CODE */
4123 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4124 break;
4125 case TCM_UNKNOWN_MODE_PAGE:
4126 /* CURRENT ERROR */
4127 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004128 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004129 /* ILLEGAL REQUEST */
4130 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4131 /* INVALID FIELD IN CDB */
4132 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4133 break;
4134 case TCM_CHECK_CONDITION_ABORT_CMD:
4135 /* CURRENT ERROR */
4136 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004137 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004138 /* ABORTED COMMAND */
4139 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4140 /* BUS DEVICE RESET FUNCTION OCCURRED */
4141 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4142 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4143 break;
4144 case TCM_INCORRECT_AMOUNT_OF_DATA:
4145 /* CURRENT ERROR */
4146 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004147 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004148 /* ABORTED COMMAND */
4149 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4150 /* WRITE ERROR */
4151 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4152 /* NOT ENOUGH UNSOLICITED DATA */
4153 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4154 break;
4155 case TCM_INVALID_CDB_FIELD:
4156 /* CURRENT ERROR */
4157 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004158 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08004159 /* ILLEGAL REQUEST */
4160 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004161 /* INVALID FIELD IN CDB */
4162 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4163 break;
4164 case TCM_INVALID_PARAMETER_LIST:
4165 /* CURRENT ERROR */
4166 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004167 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08004168 /* ILLEGAL REQUEST */
4169 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004170 /* INVALID FIELD IN PARAMETER LIST */
4171 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4172 break;
4173 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4174 /* CURRENT ERROR */
4175 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004176 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004177 /* ABORTED COMMAND */
4178 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4179 /* WRITE ERROR */
4180 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4181 /* UNEXPECTED_UNSOLICITED_DATA */
4182 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4183 break;
4184 case TCM_SERVICE_CRC_ERROR:
4185 /* CURRENT ERROR */
4186 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004187 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004188 /* ABORTED COMMAND */
4189 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4190 /* PROTOCOL SERVICE CRC ERROR */
4191 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4192 /* N/A */
4193 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4194 break;
4195 case TCM_SNACK_REJECTED:
4196 /* CURRENT ERROR */
4197 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004198 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004199 /* ABORTED COMMAND */
4200 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4201 /* READ ERROR */
4202 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4203 /* FAILED RETRANSMISSION REQUEST */
4204 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4205 break;
4206 case TCM_WRITE_PROTECTED:
4207 /* CURRENT ERROR */
4208 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004209 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004210 /* DATA PROTECT */
4211 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4212 /* WRITE PROTECTED */
4213 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4214 break;
4215 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4216 /* CURRENT ERROR */
4217 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004218 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004219 /* UNIT ATTENTION */
4220 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4221 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4222 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4223 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4224 break;
4225 case TCM_CHECK_CONDITION_NOT_READY:
4226 /* CURRENT ERROR */
4227 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004228 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004229 /* Not Ready */
4230 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4231 transport_get_sense_codes(cmd, &asc, &ascq);
4232 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4233 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4234 break;
4235 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4236 default:
4237 /* CURRENT ERROR */
4238 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004239 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004240 /* ILLEGAL REQUEST */
4241 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4242 /* LOGICAL UNIT COMMUNICATION FAILURE */
4243 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4244 break;
4245 }
4246 /*
4247 * This code uses linux/include/scsi/scsi.h SAM status codes!
4248 */
4249 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4250 /*
4251 * Automatically padded, this value is encoded in the fabric's
4252 * data_length response PDU containing the SCSI defined sense data.
4253 */
4254 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4255
4256after_reason:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004257 return cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004258}
4259EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4260
4261int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4262{
4263 int ret = 0;
4264
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004265 if (cmd->transport_state & CMD_T_ABORTED) {
Andy Grover6708bb22011-06-08 10:36:43 -07004266 if (!send_status ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004267 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4268 return 1;
Andy Grover8b1e1242012-04-03 15:51:12 -07004269
Andy Grover6708bb22011-06-08 10:36:43 -07004270 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004271 " status for CDB: 0x%02x ITT: 0x%08x\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07004272 cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004273 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07004274
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004275 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
Andy Grovere3d6f902011-07-19 08:55:10 +00004276 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004277 ret = 1;
4278 }
4279 return ret;
4280}
4281EXPORT_SYMBOL(transport_check_aborted_status);
4282
4283void transport_send_task_abort(struct se_cmd *cmd)
4284{
Nicholas Bellingerc252f002011-09-29 14:22:13 -07004285 unsigned long flags;
4286
4287 spin_lock_irqsave(&cmd->t_state_lock, flags);
4288 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4289 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4290 return;
4291 }
4292 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4293
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004294 /*
4295 * If there are still expected incoming fabric WRITEs, we wait
4296 * until until they have completed before sending a TASK_ABORTED
4297 * response. This response with TASK_ABORTED status will be
4298 * queued back to fabric module by transport_check_aborted_status().
4299 */
4300 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004301 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004302 cmd->transport_state |= CMD_T_ABORTED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004303 smp_mb__after_atomic_inc();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004304 }
4305 }
4306 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
Andy Grover8b1e1242012-04-03 15:51:12 -07004307
Andy Grover6708bb22011-06-08 10:36:43 -07004308 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
Andy Grovera1d8b492011-05-02 17:12:10 -07004309 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004310 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07004311
Andy Grovere3d6f902011-07-19 08:55:10 +00004312 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004313}
4314
Christoph Hellwige26d99a2011-11-14 12:30:30 -05004315static int transport_generic_do_tmr(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004316{
Andy Grover5951146d2011-07-19 10:26:37 +00004317 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004318 struct se_tmr_req *tmr = cmd->se_tmr_req;
4319 int ret;
4320
4321 switch (tmr->function) {
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004322 case TMR_ABORT_TASK:
Nicholas Bellinger3d289342012-02-13 02:38:14 -08004323 core_tmr_abort_task(dev, tmr, cmd->se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004324 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004325 case TMR_ABORT_TASK_SET:
4326 case TMR_CLEAR_ACA:
4327 case TMR_CLEAR_TASK_SET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004328 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4329 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004330 case TMR_LUN_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004331 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4332 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4333 TMR_FUNCTION_REJECTED;
4334 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004335 case TMR_TARGET_WARM_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004336 tmr->response = TMR_FUNCTION_REJECTED;
4337 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004338 case TMR_TARGET_COLD_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004339 tmr->response = TMR_FUNCTION_REJECTED;
4340 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004341 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004342 pr_err("Uknown TMR function: 0x%02x.\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004343 tmr->function);
4344 tmr->response = TMR_FUNCTION_REJECTED;
4345 break;
4346 }
4347
4348 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
Andy Grovere3d6f902011-07-19 08:55:10 +00004349 cmd->se_tfo->queue_tm_rsp(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004350
Christoph Hellwigb7b8bef2011-10-17 13:56:44 -04004351 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004352 return 0;
4353}
4354
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004355/* transport_processing_thread():
4356 *
4357 *
4358 */
4359static int transport_processing_thread(void *param)
4360{
Andy Grover5951146d2011-07-19 10:26:37 +00004361 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004362 struct se_cmd *cmd;
Jörn Engel8359cf42011-11-24 02:05:51 +01004363 struct se_device *dev = param;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004364
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004365 while (!kthread_should_stop()) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004366 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4367 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004368 kthread_should_stop());
4369 if (ret < 0)
4370 goto out;
4371
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004372get_cmd:
Andy Grover5951146d2011-07-19 10:26:37 +00004373 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4374 if (!cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004375 continue;
4376
Andy Grover5951146d2011-07-19 10:26:37 +00004377 switch (cmd->t_state) {
Christoph Hellwig680b73c2011-09-12 21:51:14 +02004378 case TRANSPORT_NEW_CMD:
4379 BUG();
4380 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004381 case TRANSPORT_NEW_CMD_MAP:
Andy Grover6708bb22011-06-08 10:36:43 -07004382 if (!cmd->se_tfo->new_cmd_map) {
4383 pr_err("cmd->se_tfo->new_cmd_map is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004384 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4385 BUG();
4386 }
Andy Grovere3d6f902011-07-19 08:55:10 +00004387 ret = cmd->se_tfo->new_cmd_map(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004388 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004389 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004390 break;
4391 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004392 ret = transport_generic_new_cmd(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07004393 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004394 transport_generic_request_failure(cmd);
4395 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004396 }
4397 break;
4398 case TRANSPORT_PROCESS_WRITE:
4399 transport_generic_process_write(cmd);
4400 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004401 case TRANSPORT_PROCESS_TMR:
4402 transport_generic_do_tmr(cmd);
4403 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004404 case TRANSPORT_COMPLETE_QF_WP:
Christoph Hellwige057f532011-10-17 13:56:41 -04004405 transport_write_pending_qf(cmd);
4406 break;
4407 case TRANSPORT_COMPLETE_QF_OK:
4408 transport_complete_qf(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004409 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004410 default:
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004411 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4412 "i_state: %d on SE LUN: %u\n",
4413 cmd->t_state,
Andy Grovere3d6f902011-07-19 08:55:10 +00004414 cmd->se_tfo->get_task_tag(cmd),
4415 cmd->se_tfo->get_cmd_state(cmd),
4416 cmd->se_lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004417 BUG();
4418 }
4419
4420 goto get_cmd;
4421 }
4422
4423out:
Christoph Hellwigcf572a92012-04-24 00:25:05 -04004424 WARN_ON(!list_empty(&dev->state_list));
Nicholas Bellingerce8762f2011-10-09 02:19:01 -07004425 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004426 dev->process_thread = NULL;
4427 return 0;
4428}