blob: 3f20fbd88cd42e98c6ddbfdf973fce95fbb7d803 [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 Bellingerc66ac9d2010-12-17 11:11:26 -080071static void transport_complete_task_attr(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -070072static void transport_handle_queue_full(struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -040073 struct se_device *dev);
Andy Grover05d1c7c2011-07-20 19:13:28 +000074static int transport_generic_get_mem(struct se_cmd *cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -070075static void transport_put_cmd(struct se_cmd *cmd);
Christoph Hellwig3df8d402011-10-17 13:56:43 -040076static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080077static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
Christoph Hellwig35e0e752011-10-17 13:56:53 -040078static void target_complete_ok_work(struct work_struct *work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080079
Andy Grovere3d6f902011-07-19 08:55:10 +000080int init_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080081{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080082 se_sess_cache = kmem_cache_create("se_sess_cache",
83 sizeof(struct se_session), __alignof__(struct se_session),
84 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070085 if (!se_sess_cache) {
86 pr_err("kmem_cache_create() for struct se_session"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080087 " failed\n");
Andy Groverc8e31f22012-01-19 13:39:17 -080088 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080089 }
90 se_ua_cache = kmem_cache_create("se_ua_cache",
91 sizeof(struct se_ua), __alignof__(struct se_ua),
92 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070093 if (!se_ua_cache) {
94 pr_err("kmem_cache_create() for struct se_ua failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -040095 goto out_free_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080096 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080097 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
98 sizeof(struct t10_pr_registration),
99 __alignof__(struct t10_pr_registration), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700100 if (!t10_pr_reg_cache) {
101 pr_err("kmem_cache_create() for struct t10_pr_registration"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800102 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400103 goto out_free_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800104 }
105 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
106 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
107 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700108 if (!t10_alua_lu_gp_cache) {
109 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800110 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400111 goto out_free_pr_reg_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800112 }
113 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
114 sizeof(struct t10_alua_lu_gp_member),
115 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700116 if (!t10_alua_lu_gp_mem_cache) {
117 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800118 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400119 goto out_free_lu_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800120 }
121 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
122 sizeof(struct t10_alua_tg_pt_gp),
123 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700124 if (!t10_alua_tg_pt_gp_cache) {
125 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800126 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400127 goto out_free_lu_gp_mem_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800128 }
129 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
130 "t10_alua_tg_pt_gp_mem_cache",
131 sizeof(struct t10_alua_tg_pt_gp_member),
132 __alignof__(struct t10_alua_tg_pt_gp_member),
133 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700134 if (!t10_alua_tg_pt_gp_mem_cache) {
135 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800136 "mem_t failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400137 goto out_free_tg_pt_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800138 }
139
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400140 target_completion_wq = alloc_workqueue("target_completion",
141 WQ_MEM_RECLAIM, 0);
142 if (!target_completion_wq)
143 goto out_free_tg_pt_gp_mem_cache;
144
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800145 return 0;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400146
147out_free_tg_pt_gp_mem_cache:
148 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
149out_free_tg_pt_gp_cache:
150 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
151out_free_lu_gp_mem_cache:
152 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
153out_free_lu_gp_cache:
154 kmem_cache_destroy(t10_alua_lu_gp_cache);
155out_free_pr_reg_cache:
156 kmem_cache_destroy(t10_pr_reg_cache);
157out_free_ua_cache:
158 kmem_cache_destroy(se_ua_cache);
159out_free_sess_cache:
160 kmem_cache_destroy(se_sess_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800161out:
Andy Grovere3d6f902011-07-19 08:55:10 +0000162 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800163}
164
Andy Grovere3d6f902011-07-19 08:55:10 +0000165void release_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800166{
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400167 destroy_workqueue(target_completion_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800168 kmem_cache_destroy(se_sess_cache);
169 kmem_cache_destroy(se_ua_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800170 kmem_cache_destroy(t10_pr_reg_cache);
171 kmem_cache_destroy(t10_alua_lu_gp_cache);
172 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
173 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
174 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800175}
176
Andy Grovere3d6f902011-07-19 08:55:10 +0000177/* This code ensures unique mib indexes are handed out. */
178static DEFINE_SPINLOCK(scsi_mib_index_lock);
179static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800180
181/*
182 * Allocate a new row index for the entry type specified
183 */
184u32 scsi_get_new_index(scsi_index_t type)
185{
186 u32 new_index;
187
Andy Grovere3d6f902011-07-19 08:55:10 +0000188 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800189
Andy Grovere3d6f902011-07-19 08:55:10 +0000190 spin_lock(&scsi_mib_index_lock);
191 new_index = ++scsi_mib_index[type];
192 spin_unlock(&scsi_mib_index_lock);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800193
194 return new_index;
195}
196
Christoph Hellwige26d99a2011-11-14 12:30:30 -0500197static void transport_init_queue_obj(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800198{
199 atomic_set(&qobj->queue_cnt, 0);
200 INIT_LIST_HEAD(&qobj->qobj_list);
201 init_waitqueue_head(&qobj->thread_wq);
202 spin_lock_init(&qobj->cmd_queue_lock);
203}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800204
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700205void transport_subsystem_check_init(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800206{
207 int ret;
208
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700209 if (sub_api_initialized)
210 return;
211
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800212 ret = request_module("target_core_iblock");
213 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700214 pr_err("Unable to load target_core_iblock\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800215
216 ret = request_module("target_core_file");
217 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700218 pr_err("Unable to load target_core_file\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800219
220 ret = request_module("target_core_pscsi");
221 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700222 pr_err("Unable to load target_core_pscsi\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800223
224 ret = request_module("target_core_stgt");
225 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700226 pr_err("Unable to load target_core_stgt\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800227
Andy Grovere3d6f902011-07-19 08:55:10 +0000228 sub_api_initialized = 1;
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700229 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800230}
231
232struct se_session *transport_init_session(void)
233{
234 struct se_session *se_sess;
235
236 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700237 if (!se_sess) {
238 pr_err("Unable to allocate struct se_session from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800239 " se_sess_cache\n");
240 return ERR_PTR(-ENOMEM);
241 }
242 INIT_LIST_HEAD(&se_sess->sess_list);
243 INIT_LIST_HEAD(&se_sess->sess_acl_list);
Nicholas Bellingera17f0912011-11-02 21:52:08 -0700244 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
245 INIT_LIST_HEAD(&se_sess->sess_wait_list);
246 spin_lock_init(&se_sess->sess_cmd_lock);
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800247 kref_init(&se_sess->sess_kref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800248
249 return se_sess;
250}
251EXPORT_SYMBOL(transport_init_session);
252
253/*
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700254 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800255 */
256void __transport_register_session(
257 struct se_portal_group *se_tpg,
258 struct se_node_acl *se_nacl,
259 struct se_session *se_sess,
260 void *fabric_sess_ptr)
261{
262 unsigned char buf[PR_REG_ISID_LEN];
263
264 se_sess->se_tpg = se_tpg;
265 se_sess->fabric_sess_ptr = fabric_sess_ptr;
266 /*
267 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
268 *
269 * Only set for struct se_session's that will actually be moving I/O.
270 * eg: *NOT* discovery sessions.
271 */
272 if (se_nacl) {
273 /*
274 * If the fabric module supports an ISID based TransportID,
275 * save this value in binary from the fabric I_T Nexus now.
276 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000277 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800278 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +0000279 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800280 &buf[0], PR_REG_ISID_LEN);
281 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
282 }
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800283 kref_get(&se_nacl->acl_kref);
284
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800285 spin_lock_irq(&se_nacl->nacl_sess_lock);
286 /*
287 * The se_nacl->nacl_sess pointer will be set to the
288 * last active I_T Nexus for each struct se_node_acl.
289 */
290 se_nacl->nacl_sess = se_sess;
291
292 list_add_tail(&se_sess->sess_acl_list,
293 &se_nacl->acl_sess_list);
294 spin_unlock_irq(&se_nacl->nacl_sess_lock);
295 }
296 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
297
Andy Grover6708bb22011-06-08 10:36:43 -0700298 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000299 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800300}
301EXPORT_SYMBOL(__transport_register_session);
302
303void transport_register_session(
304 struct se_portal_group *se_tpg,
305 struct se_node_acl *se_nacl,
306 struct se_session *se_sess,
307 void *fabric_sess_ptr)
308{
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700309 unsigned long flags;
310
311 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800312 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700313 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800314}
315EXPORT_SYMBOL(transport_register_session);
316
Joern Engel41492682012-05-18 13:57:19 -0700317void target_release_session(struct kref *kref)
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800318{
319 struct se_session *se_sess = container_of(kref,
320 struct se_session, sess_kref);
321 struct se_portal_group *se_tpg = se_sess->se_tpg;
322
323 se_tpg->se_tpg_tfo->close_session(se_sess);
324}
325
326void target_get_session(struct se_session *se_sess)
327{
328 kref_get(&se_sess->sess_kref);
329}
330EXPORT_SYMBOL(target_get_session);
331
Jörn Engel33933a02012-05-11 10:35:08 -0400332void target_put_session(struct se_session *se_sess)
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800333{
Joern Engel41492682012-05-18 13:57:19 -0700334 struct se_portal_group *tpg = se_sess->se_tpg;
335
336 if (tpg->se_tpg_tfo->put_session != NULL) {
337 tpg->se_tpg_tfo->put_session(se_sess);
338 return;
339 }
Jörn Engel33933a02012-05-11 10:35:08 -0400340 kref_put(&se_sess->sess_kref, target_release_session);
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800341}
342EXPORT_SYMBOL(target_put_session);
343
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800344static void target_complete_nacl(struct kref *kref)
345{
346 struct se_node_acl *nacl = container_of(kref,
347 struct se_node_acl, acl_kref);
348
349 complete(&nacl->acl_free_comp);
350}
351
352void target_put_nacl(struct se_node_acl *nacl)
353{
354 kref_put(&nacl->acl_kref, target_complete_nacl);
355}
356
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800357void transport_deregister_session_configfs(struct se_session *se_sess)
358{
359 struct se_node_acl *se_nacl;
Roland Dreier23388862011-06-22 01:02:21 -0700360 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800361 /*
362 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
363 */
364 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700365 if (se_nacl) {
Roland Dreier23388862011-06-22 01:02:21 -0700366 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800367 if (se_nacl->acl_stop == 0)
368 list_del(&se_sess->sess_acl_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800369 /*
370 * If the session list is empty, then clear the pointer.
371 * Otherwise, set the struct se_session pointer from the tail
372 * element of the per struct se_node_acl active session list.
373 */
374 if (list_empty(&se_nacl->acl_sess_list))
375 se_nacl->nacl_sess = NULL;
376 else {
377 se_nacl->nacl_sess = container_of(
378 se_nacl->acl_sess_list.prev,
379 struct se_session, sess_acl_list);
380 }
Roland Dreier23388862011-06-22 01:02:21 -0700381 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800382 }
383}
384EXPORT_SYMBOL(transport_deregister_session_configfs);
385
386void transport_free_session(struct se_session *se_sess)
387{
388 kmem_cache_free(se_sess_cache, se_sess);
389}
390EXPORT_SYMBOL(transport_free_session);
391
392void transport_deregister_session(struct se_session *se_sess)
393{
394 struct se_portal_group *se_tpg = se_sess->se_tpg;
Nicholas Bellinger01468342012-03-10 14:32:52 -0800395 struct target_core_fabric_ops *se_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800396 struct se_node_acl *se_nacl;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700397 unsigned long flags;
Nicholas Bellinger01468342012-03-10 14:32:52 -0800398 bool comp_nacl = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800399
Andy Grover6708bb22011-06-08 10:36:43 -0700400 if (!se_tpg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800401 transport_free_session(se_sess);
402 return;
403 }
Nicholas Bellinger01468342012-03-10 14:32:52 -0800404 se_tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800405
Roland Dreiere63a8e12011-08-12 16:01:02 -0700406 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800407 list_del(&se_sess->sess_list);
408 se_sess->se_tpg = NULL;
409 se_sess->fabric_sess_ptr = NULL;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700410 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800411
412 /*
413 * Determine if we need to do extra work for this initiator node's
414 * struct se_node_acl if it had been previously dynamically generated.
415 */
416 se_nacl = se_sess->se_node_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800417
Nicholas Bellinger01468342012-03-10 14:32:52 -0800418 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
419 if (se_nacl && se_nacl->dynamic_node_acl) {
420 if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
421 list_del(&se_nacl->acl_list);
422 se_tpg->num_node_acls--;
423 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
424 core_tpg_wait_for_nacl_pr_ref(se_nacl);
425 core_free_device_list_for_node(se_nacl, se_tpg);
426 se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl);
427
428 comp_nacl = false;
429 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800430 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800431 }
Nicholas Bellinger01468342012-03-10 14:32:52 -0800432 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800433
Andy Grover6708bb22011-06-08 10:36:43 -0700434 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000435 se_tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellinger01468342012-03-10 14:32:52 -0800436 /*
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800437 * If last kref is dropping now for an explict NodeACL, awake sleeping
438 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
439 * removal context.
Nicholas Bellinger01468342012-03-10 14:32:52 -0800440 */
441 if (se_nacl && comp_nacl == true)
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800442 target_put_nacl(se_nacl);
Nicholas Bellinger01468342012-03-10 14:32:52 -0800443
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800444 transport_free_session(se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800445}
446EXPORT_SYMBOL(transport_deregister_session);
447
448/*
Andy Grovera1d8b492011-05-02 17:12:10 -0700449 * Called with cmd->t_state_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800450 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400451static void target_remove_from_state_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800452{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400453 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800454 unsigned long flags;
455
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400456 if (!dev)
457 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800458
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400459 if (cmd->transport_state & CMD_T_BUSY)
460 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800461
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400462 spin_lock_irqsave(&dev->execute_task_lock, flags);
463 if (cmd->state_active) {
464 list_del(&cmd->state_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400465 cmd->state_active = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800466 }
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400467 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800468}
469
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400470static int transport_cmd_check_stop(struct se_cmd *cmd, bool remove_from_lists)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800471{
472 unsigned long flags;
473
Andy Grovera1d8b492011-05-02 17:12:10 -0700474 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800475 /*
476 * Determine if IOCTL context caller in requesting the stopping of this
477 * command for LUN shutdown purposes.
478 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500479 if (cmd->transport_state & CMD_T_LUN_STOP) {
480 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
481 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800482
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500483 cmd->transport_state &= ~CMD_T_ACTIVE;
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400484 if (remove_from_lists)
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400485 target_remove_from_state_list(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700486 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800487
Andy Grovera1d8b492011-05-02 17:12:10 -0700488 complete(&cmd->transport_lun_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800489 return 1;
490 }
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400491
492 if (remove_from_lists) {
493 target_remove_from_state_list(cmd);
494
495 /*
496 * Clear struct se_cmd->se_lun before the handoff to FE.
497 */
498 cmd->se_lun = NULL;
499 }
500
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800501 /*
502 * Determine if frontend context caller is requesting the stopping of
Andy Grovere3d6f902011-07-19 08:55:10 +0000503 * this command for frontend exceptions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800504 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500505 if (cmd->transport_state & CMD_T_STOP) {
506 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
507 __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000508 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800509
Andy Grovera1d8b492011-05-02 17:12:10 -0700510 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800511
Andy Grovera1d8b492011-05-02 17:12:10 -0700512 complete(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800513 return 1;
514 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800515
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400516 cmd->transport_state &= ~CMD_T_ACTIVE;
517 if (remove_from_lists) {
518 /*
519 * Some fabric modules like tcm_loop can release
520 * their internally allocated I/O reference now and
521 * struct se_cmd now.
522 *
523 * Fabric modules are expected to return '1' here if the
524 * se_cmd being passed is released at this point,
525 * or zero if not being released.
526 */
527 if (cmd->se_tfo->check_stop_free != NULL) {
528 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
529 return cmd->se_tfo->check_stop_free(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800530 }
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400531 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800532
Andy Grovera1d8b492011-05-02 17:12:10 -0700533 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800534 return 0;
535}
536
537static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
538{
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400539 return transport_cmd_check_stop(cmd, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800540}
541
542static void transport_lun_remove_cmd(struct se_cmd *cmd)
543{
Andy Grovere3d6f902011-07-19 08:55:10 +0000544 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800545 unsigned long flags;
546
547 if (!lun)
548 return;
549
Andy Grovera1d8b492011-05-02 17:12:10 -0700550 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500551 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
552 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400553 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800554 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700555 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800556
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800557 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -0500558 if (!list_empty(&cmd->se_lun_node))
559 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800560 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
561}
562
563void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
564{
Andy Groverc8e31f22012-01-19 13:39:17 -0800565 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellinger8dc52b52011-10-09 02:02:51 -0700566 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800567
568 if (transport_cmd_check_stop_to_fabric(cmd))
569 return;
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700570 if (remove) {
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400571 transport_remove_cmd_from_queue(cmd);
Christoph Hellwige6a25732011-09-13 23:08:50 +0200572 transport_put_cmd(cmd);
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700573 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800574}
575
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400576static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
577 bool at_head)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800578{
579 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +0000580 struct se_queue_obj *qobj = &dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800581 unsigned long flags;
582
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800583 if (t_state) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700584 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800585 cmd->t_state = t_state;
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500586 cmd->transport_state |= CMD_T_ACTIVE;
Andy Grovera1d8b492011-05-02 17:12:10 -0700587 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588 }
589
590 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Roland Dreier79a7fef2011-09-28 22:12:07 -0700591
592 /* If the cmd is already on the list, remove it before we add it */
593 if (!list_empty(&cmd->se_queue_node))
594 list_del(&cmd->se_queue_node);
595 else
596 atomic_inc(&qobj->queue_cnt);
597
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400598 if (at_head)
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700599 list_add(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400600 else
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700601 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500602 cmd->transport_state |= CMD_T_QUEUED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800603 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
604
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800605 wake_up_interruptible(&qobj->thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800606}
607
Andy Grover5951146d2011-07-19 10:26:37 +0000608static struct se_cmd *
609transport_get_cmd_from_queue(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800610{
Andy Grover5951146d2011-07-19 10:26:37 +0000611 struct se_cmd *cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800612 unsigned long flags;
613
614 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
615 if (list_empty(&qobj->qobj_list)) {
616 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
617 return NULL;
618 }
Andy Grover5951146d2011-07-19 10:26:37 +0000619 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800620
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500621 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700622 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800623 atomic_dec(&qobj->queue_cnt);
624 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
625
Andy Grover5951146d2011-07-19 10:26:37 +0000626 return cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800627}
628
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400629static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800630{
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400631 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800632 unsigned long flags;
633
634 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500635 if (!(cmd->transport_state & CMD_T_QUEUED)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800636 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
637 return;
638 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500639 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700640 atomic_dec(&qobj->queue_cnt);
641 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800642 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800643}
644
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400645static void target_complete_failure_work(struct work_struct *work)
646{
647 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
648
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700649 transport_generic_request_failure(cmd);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400650}
651
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400652void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800653{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400654 struct se_device *dev = cmd->se_dev;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400655 int success = scsi_status == GOOD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800656 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800657
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400658 cmd->scsi_status = scsi_status;
659
660
Andy Grovera1d8b492011-05-02 17:12:10 -0700661 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400662 cmd->transport_state &= ~CMD_T_BUSY;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800663
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800664 if (dev && dev->transport->transport_complete) {
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400665 if (dev->transport->transport_complete(cmd,
666 cmd->t_data_sg) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800667 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800668 success = 1;
669 }
670 }
671
672 /*
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400673 * See if we are waiting to complete for an exception condition.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800674 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400675 if (cmd->transport_state & CMD_T_REQUEST_STOP) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700676 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400677 complete(&cmd->task_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800678 return;
679 }
Christoph Hellwig22350072011-11-02 05:06:35 -0700680
681 if (!success)
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500682 cmd->transport_state |= CMD_T_FAILED;
Christoph Hellwig22350072011-11-02 05:06:35 -0700683
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800684 /*
685 * Check for case where an explict ABORT_TASK has been received
686 * and transport_wait_for_tasks() will be waiting for completion..
687 */
688 if (cmd->transport_state & CMD_T_ABORTED &&
689 cmd->transport_state & CMD_T_STOP) {
690 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
691 complete(&cmd->t_transport_stop_comp);
692 return;
693 } else if (cmd->transport_state & CMD_T_FAILED) {
Christoph Hellwig41e16e92011-11-23 06:54:15 -0500694 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400695 INIT_WORK(&cmd->work, target_complete_failure_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800696 } else {
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400697 INIT_WORK(&cmd->work, target_complete_ok_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800698 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400699
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400700 cmd->t_state = TRANSPORT_COMPLETE;
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800701 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
Andy Grovera1d8b492011-05-02 17:12:10 -0700702 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800703
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400704 queue_work(target_completion_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800705}
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400706EXPORT_SYMBOL(target_complete_cmd);
707
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400708static void target_add_to_state_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800709{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400710 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800711 unsigned long flags;
712
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800713 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400714 if (!cmd->state_active) {
715 list_add_tail(&cmd->state_list, &dev->state_list);
716 cmd->state_active = true;
717 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800718 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800719}
720
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700721/*
Nicholas Bellingerf147abb2011-10-25 23:57:41 -0700722 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700723 */
724
725static void target_qf_do_work(struct work_struct *work)
726{
727 struct se_device *dev = container_of(work, struct se_device,
728 qf_work_queue);
Roland Dreierbcac3642011-08-27 21:33:16 -0700729 LIST_HEAD(qf_cmd_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700730 struct se_cmd *cmd, *cmd_tmp;
731
732 spin_lock_irq(&dev->qf_cmd_lock);
Roland Dreierbcac3642011-08-27 21:33:16 -0700733 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
734 spin_unlock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700735
Roland Dreierbcac3642011-08-27 21:33:16 -0700736 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700737 list_del(&cmd->se_qf_node);
738 atomic_dec(&dev->dev_qf_count);
739 smp_mb__after_atomic_dec();
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700740
Andy Grover6708bb22011-06-08 10:36:43 -0700741 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700742 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -0400743 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700744 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
745 : "UNKNOWN");
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400746
747 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700748 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700749}
750
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800751unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
752{
753 switch (cmd->data_direction) {
754 case DMA_NONE:
755 return "NONE";
756 case DMA_FROM_DEVICE:
757 return "READ";
758 case DMA_TO_DEVICE:
759 return "WRITE";
760 case DMA_BIDIRECTIONAL:
761 return "BIDI";
762 default:
763 break;
764 }
765
766 return "UNKNOWN";
767}
768
769void transport_dump_dev_state(
770 struct se_device *dev,
771 char *b,
772 int *bl)
773{
774 *bl += sprintf(b + *bl, "Status: ");
775 switch (dev->dev_status) {
776 case TRANSPORT_DEVICE_ACTIVATED:
777 *bl += sprintf(b + *bl, "ACTIVATED");
778 break;
779 case TRANSPORT_DEVICE_DEACTIVATED:
780 *bl += sprintf(b + *bl, "DEACTIVATED");
781 break;
782 case TRANSPORT_DEVICE_SHUTDOWN:
783 *bl += sprintf(b + *bl, "SHUTDOWN");
784 break;
785 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
786 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
787 *bl += sprintf(b + *bl, "OFFLINE");
788 break;
789 default:
790 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
791 break;
792 }
793
Christoph Hellwig5f41a312012-05-20 14:34:44 -0400794 *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth);
Nicholas Bellinger11e764b2012-05-09 12:42:09 -0700795 *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n",
796 dev->se_sub_dev->se_dev_attrib.block_size,
797 dev->se_sub_dev->se_dev_attrib.hw_max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800798 *bl += sprintf(b + *bl, " ");
799}
800
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800801void transport_dump_vpd_proto_id(
802 struct t10_vpd *vpd,
803 unsigned char *p_buf,
804 int p_buf_len)
805{
806 unsigned char buf[VPD_TMP_BUF_SIZE];
807 int len;
808
809 memset(buf, 0, VPD_TMP_BUF_SIZE);
810 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
811
812 switch (vpd->protocol_identifier) {
813 case 0x00:
814 sprintf(buf+len, "Fibre Channel\n");
815 break;
816 case 0x10:
817 sprintf(buf+len, "Parallel SCSI\n");
818 break;
819 case 0x20:
820 sprintf(buf+len, "SSA\n");
821 break;
822 case 0x30:
823 sprintf(buf+len, "IEEE 1394\n");
824 break;
825 case 0x40:
826 sprintf(buf+len, "SCSI Remote Direct Memory Access"
827 " Protocol\n");
828 break;
829 case 0x50:
830 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
831 break;
832 case 0x60:
833 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
834 break;
835 case 0x70:
836 sprintf(buf+len, "Automation/Drive Interface Transport"
837 " Protocol\n");
838 break;
839 case 0x80:
840 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
841 break;
842 default:
843 sprintf(buf+len, "Unknown 0x%02x\n",
844 vpd->protocol_identifier);
845 break;
846 }
847
848 if (p_buf)
849 strncpy(p_buf, buf, p_buf_len);
850 else
Andy Grover6708bb22011-06-08 10:36:43 -0700851 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800852}
853
854void
855transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
856{
857 /*
858 * Check if the Protocol Identifier Valid (PIV) bit is set..
859 *
860 * from spc3r23.pdf section 7.5.1
861 */
862 if (page_83[1] & 0x80) {
863 vpd->protocol_identifier = (page_83[0] & 0xf0);
864 vpd->protocol_identifier_set = 1;
865 transport_dump_vpd_proto_id(vpd, NULL, 0);
866 }
867}
868EXPORT_SYMBOL(transport_set_vpd_proto_id);
869
870int transport_dump_vpd_assoc(
871 struct t10_vpd *vpd,
872 unsigned char *p_buf,
873 int p_buf_len)
874{
875 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +0000876 int ret = 0;
877 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800878
879 memset(buf, 0, VPD_TMP_BUF_SIZE);
880 len = sprintf(buf, "T10 VPD Identifier Association: ");
881
882 switch (vpd->association) {
883 case 0x00:
884 sprintf(buf+len, "addressed logical unit\n");
885 break;
886 case 0x10:
887 sprintf(buf+len, "target port\n");
888 break;
889 case 0x20:
890 sprintf(buf+len, "SCSI target device\n");
891 break;
892 default:
893 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
Andy Grovere3d6f902011-07-19 08:55:10 +0000894 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800895 break;
896 }
897
898 if (p_buf)
899 strncpy(p_buf, buf, p_buf_len);
900 else
Andy Grover6708bb22011-06-08 10:36:43 -0700901 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800902
903 return ret;
904}
905
906int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
907{
908 /*
909 * The VPD identification association..
910 *
911 * from spc3r23.pdf Section 7.6.3.1 Table 297
912 */
913 vpd->association = (page_83[1] & 0x30);
914 return transport_dump_vpd_assoc(vpd, NULL, 0);
915}
916EXPORT_SYMBOL(transport_set_vpd_assoc);
917
918int transport_dump_vpd_ident_type(
919 struct t10_vpd *vpd,
920 unsigned char *p_buf,
921 int p_buf_len)
922{
923 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +0000924 int ret = 0;
925 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800926
927 memset(buf, 0, VPD_TMP_BUF_SIZE);
928 len = sprintf(buf, "T10 VPD Identifier Type: ");
929
930 switch (vpd->device_identifier_type) {
931 case 0x00:
932 sprintf(buf+len, "Vendor specific\n");
933 break;
934 case 0x01:
935 sprintf(buf+len, "T10 Vendor ID based\n");
936 break;
937 case 0x02:
938 sprintf(buf+len, "EUI-64 based\n");
939 break;
940 case 0x03:
941 sprintf(buf+len, "NAA\n");
942 break;
943 case 0x04:
944 sprintf(buf+len, "Relative target port identifier\n");
945 break;
946 case 0x08:
947 sprintf(buf+len, "SCSI name string\n");
948 break;
949 default:
950 sprintf(buf+len, "Unsupported: 0x%02x\n",
951 vpd->device_identifier_type);
Andy Grovere3d6f902011-07-19 08:55:10 +0000952 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800953 break;
954 }
955
Andy Grovere3d6f902011-07-19 08:55:10 +0000956 if (p_buf) {
957 if (p_buf_len < strlen(buf)+1)
958 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800959 strncpy(p_buf, buf, p_buf_len);
Andy Grovere3d6f902011-07-19 08:55:10 +0000960 } else {
Andy Grover6708bb22011-06-08 10:36:43 -0700961 pr_debug("%s", buf);
Andy Grovere3d6f902011-07-19 08:55:10 +0000962 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800963
964 return ret;
965}
966
967int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
968{
969 /*
970 * The VPD identifier type..
971 *
972 * from spc3r23.pdf Section 7.6.3.1 Table 298
973 */
974 vpd->device_identifier_type = (page_83[1] & 0x0f);
975 return transport_dump_vpd_ident_type(vpd, NULL, 0);
976}
977EXPORT_SYMBOL(transport_set_vpd_ident_type);
978
979int transport_dump_vpd_ident(
980 struct t10_vpd *vpd,
981 unsigned char *p_buf,
982 int p_buf_len)
983{
984 unsigned char buf[VPD_TMP_BUF_SIZE];
985 int ret = 0;
986
987 memset(buf, 0, VPD_TMP_BUF_SIZE);
988
989 switch (vpd->device_identifier_code_set) {
990 case 0x01: /* Binary */
991 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
992 &vpd->device_identifier[0]);
993 break;
994 case 0x02: /* ASCII */
995 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
996 &vpd->device_identifier[0]);
997 break;
998 case 0x03: /* UTF-8 */
999 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1000 &vpd->device_identifier[0]);
1001 break;
1002 default:
1003 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1004 " 0x%02x", vpd->device_identifier_code_set);
Andy Grovere3d6f902011-07-19 08:55:10 +00001005 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001006 break;
1007 }
1008
1009 if (p_buf)
1010 strncpy(p_buf, buf, p_buf_len);
1011 else
Andy Grover6708bb22011-06-08 10:36:43 -07001012 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001013
1014 return ret;
1015}
1016
1017int
1018transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1019{
1020 static const char hex_str[] = "0123456789abcdef";
1021 int j = 0, i = 4; /* offset to start of the identifer */
1022
1023 /*
1024 * The VPD Code Set (encoding)
1025 *
1026 * from spc3r23.pdf Section 7.6.3.1 Table 296
1027 */
1028 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1029 switch (vpd->device_identifier_code_set) {
1030 case 0x01: /* Binary */
1031 vpd->device_identifier[j++] =
1032 hex_str[vpd->device_identifier_type];
1033 while (i < (4 + page_83[3])) {
1034 vpd->device_identifier[j++] =
1035 hex_str[(page_83[i] & 0xf0) >> 4];
1036 vpd->device_identifier[j++] =
1037 hex_str[page_83[i] & 0x0f];
1038 i++;
1039 }
1040 break;
1041 case 0x02: /* ASCII */
1042 case 0x03: /* UTF-8 */
1043 while (i < (4 + page_83[3]))
1044 vpd->device_identifier[j++] = page_83[i++];
1045 break;
1046 default:
1047 break;
1048 }
1049
1050 return transport_dump_vpd_ident(vpd, NULL, 0);
1051}
1052EXPORT_SYMBOL(transport_set_vpd_ident);
1053
1054static void core_setup_task_attr_emulation(struct se_device *dev)
1055{
1056 /*
1057 * If this device is from Target_Core_Mod/pSCSI, disable the
1058 * SAM Task Attribute emulation.
1059 *
1060 * This is currently not available in upsream Linux/SCSI Target
1061 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1062 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001063 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001064 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1065 return;
1066 }
1067
1068 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
Andy Grover6708bb22011-06-08 10:36:43 -07001069 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001070 " device\n", dev->transport->name,
1071 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001072}
1073
1074static void scsi_dump_inquiry(struct se_device *dev)
1075{
Andy Grovere3d6f902011-07-19 08:55:10 +00001076 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001077 char buf[17];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001078 int i, device_type;
1079 /*
1080 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1081 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001082 for (i = 0; i < 8; i++)
1083 if (wwn->vendor[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001084 buf[i] = wwn->vendor[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001085 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001086 buf[i] = ' ';
1087 buf[i] = '\0';
1088 pr_debug(" Vendor: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001089
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001090 for (i = 0; i < 16; i++)
1091 if (wwn->model[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001092 buf[i] = wwn->model[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001093 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001094 buf[i] = ' ';
1095 buf[i] = '\0';
1096 pr_debug(" Model: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001097
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001098 for (i = 0; i < 4; i++)
1099 if (wwn->revision[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001100 buf[i] = wwn->revision[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001101 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001102 buf[i] = ' ';
1103 buf[i] = '\0';
1104 pr_debug(" Revision: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001105
Andy Grovere3d6f902011-07-19 08:55:10 +00001106 device_type = dev->transport->get_device_type(dev);
Andy Grover6708bb22011-06-08 10:36:43 -07001107 pr_debug(" Type: %s ", scsi_device_type(device_type));
1108 pr_debug(" ANSI SCSI revision: %02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001109 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001110}
1111
1112struct se_device *transport_add_device_to_core_hba(
1113 struct se_hba *hba,
1114 struct se_subsystem_api *transport,
1115 struct se_subsystem_dev *se_dev,
1116 u32 device_flags,
1117 void *transport_dev,
1118 struct se_dev_limits *dev_limits,
1119 const char *inquiry_prod,
1120 const char *inquiry_rev)
1121{
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001122 int force_pt;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001123 struct se_device *dev;
1124
1125 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001126 if (!dev) {
1127 pr_err("Unable to allocate memory for se_dev_t\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001128 return NULL;
1129 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001130
Andy Grovere3d6f902011-07-19 08:55:10 +00001131 transport_init_queue_obj(&dev->dev_queue_obj);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001132 dev->dev_flags = device_flags;
1133 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
Andy Grover5951146d2011-07-19 10:26:37 +00001134 dev->dev_ptr = transport_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001135 dev->se_hba = hba;
1136 dev->se_sub_dev = se_dev;
1137 dev->transport = transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001138 INIT_LIST_HEAD(&dev->dev_list);
1139 INIT_LIST_HEAD(&dev->dev_sep_list);
1140 INIT_LIST_HEAD(&dev->dev_tmr_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001141 INIT_LIST_HEAD(&dev->delayed_cmd_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001142 INIT_LIST_HEAD(&dev->state_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001143 INIT_LIST_HEAD(&dev->qf_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001144 spin_lock_init(&dev->execute_task_lock);
1145 spin_lock_init(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001146 spin_lock_init(&dev->dev_reservation_lock);
1147 spin_lock_init(&dev->dev_status_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001148 spin_lock_init(&dev->se_port_lock);
1149 spin_lock_init(&dev->se_tmr_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001150 spin_lock_init(&dev->qf_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001151 atomic_set(&dev->dev_ordered_id, 0);
1152
1153 se_dev_set_default_attribs(dev, dev_limits);
1154
1155 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1156 dev->creation_time = get_jiffies_64();
1157 spin_lock_init(&dev->stats_lock);
1158
1159 spin_lock(&hba->device_lock);
1160 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1161 hba->dev_count++;
1162 spin_unlock(&hba->device_lock);
1163 /*
1164 * Setup the SAM Task Attribute emulation for struct se_device
1165 */
1166 core_setup_task_attr_emulation(dev);
1167 /*
1168 * Force PR and ALUA passthrough emulation with internal object use.
1169 */
1170 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1171 /*
1172 * Setup the Reservations infrastructure for struct se_device
1173 */
1174 core_setup_reservations(dev, force_pt);
1175 /*
1176 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1177 */
1178 if (core_setup_alua(dev, force_pt) < 0)
1179 goto out;
1180
1181 /*
1182 * Startup the struct se_device processing thread
1183 */
1184 dev->process_thread = kthread_run(transport_processing_thread, dev,
Andy Grovere3d6f902011-07-19 08:55:10 +00001185 "LIO_%s", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001186 if (IS_ERR(dev->process_thread)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001187 pr_err("Unable to create kthread: LIO_%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001188 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001189 goto out;
1190 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001191 /*
1192 * Setup work_queue for QUEUE_FULL
1193 */
1194 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001195 /*
1196 * Preload the initial INQUIRY const values if we are doing
1197 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1198 * passthrough because this is being provided by the backend LLD.
1199 * This is required so that transport_get_inquiry() copies these
1200 * originals once back into DEV_T10_WWN(dev) for the virtual device
1201 * setup.
1202 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001203 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
Roland Dreierf22c1192011-05-02 22:15:37 -07001204 if (!inquiry_prod || !inquiry_rev) {
Andy Grover6708bb22011-06-08 10:36:43 -07001205 pr_err("All non TCM/pSCSI plugins require"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001206 " INQUIRY consts\n");
1207 goto out;
1208 }
1209
Andy Grovere3d6f902011-07-19 08:55:10 +00001210 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1211 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1212 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001213 }
1214 scsi_dump_inquiry(dev);
1215
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001216 return dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001217out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001218 kthread_stop(dev->process_thread);
1219
1220 spin_lock(&hba->device_lock);
1221 list_del(&dev->dev_list);
1222 hba->dev_count--;
1223 spin_unlock(&hba->device_lock);
1224
1225 se_release_vpd_for_dev(dev);
1226
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001227 kfree(dev);
1228
1229 return NULL;
1230}
1231EXPORT_SYMBOL(transport_add_device_to_core_hba);
1232
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001233int target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
Christoph Hellwig9b3b8042012-05-20 11:59:12 -04001234{
1235 struct se_device *dev = cmd->se_dev;
1236
1237 if (cmd->unknown_data_length) {
1238 cmd->data_length = size;
1239 } else if (size != cmd->data_length) {
1240 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
1241 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
1242 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
1243 cmd->data_length, size, cmd->t_task_cdb[0]);
1244
1245 cmd->cmd_spdtl = size;
1246
1247 if (cmd->data_direction == DMA_TO_DEVICE) {
1248 pr_err("Rejecting underflow/overflow"
1249 " WRITE data\n");
1250 goto out_invalid_cdb_field;
1251 }
1252 /*
1253 * Reject READ_* or WRITE_* with overflow/underflow for
1254 * type SCF_SCSI_DATA_CDB.
1255 */
1256 if (dev->se_sub_dev->se_dev_attrib.block_size != 512) {
1257 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
1258 " CDB on non 512-byte sector setup subsystem"
1259 " plugin: %s\n", dev->transport->name);
1260 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
1261 goto out_invalid_cdb_field;
1262 }
1263
1264 if (size > cmd->data_length) {
1265 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
1266 cmd->residual_count = (size - cmd->data_length);
1267 } else {
1268 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
1269 cmd->residual_count = (cmd->data_length - size);
1270 }
1271 cmd->data_length = size;
1272 }
1273
1274 return 0;
1275
1276out_invalid_cdb_field:
1277 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1278 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1279 return -EINVAL;
1280}
1281
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001282/*
1283 * Used by fabric modules containing a local struct se_cmd within their
1284 * fabric dependent per I/O descriptor.
1285 */
1286void transport_init_se_cmd(
1287 struct se_cmd *cmd,
1288 struct target_core_fabric_ops *tfo,
1289 struct se_session *se_sess,
1290 u32 data_length,
1291 int data_direction,
1292 int task_attr,
1293 unsigned char *sense_buffer)
1294{
Andy Grover5951146d2011-07-19 10:26:37 +00001295 INIT_LIST_HEAD(&cmd->se_lun_node);
1296 INIT_LIST_HEAD(&cmd->se_delayed_node);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001297 INIT_LIST_HEAD(&cmd->se_qf_node);
Roland Dreier79a7fef2011-09-28 22:12:07 -07001298 INIT_LIST_HEAD(&cmd->se_queue_node);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001299 INIT_LIST_HEAD(&cmd->se_cmd_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001300 INIT_LIST_HEAD(&cmd->state_list);
Andy Grovera1d8b492011-05-02 17:12:10 -07001301 init_completion(&cmd->transport_lun_fe_stop_comp);
1302 init_completion(&cmd->transport_lun_stop_comp);
1303 init_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001304 init_completion(&cmd->cmd_wait_comp);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001305 init_completion(&cmd->task_stop_comp);
Andy Grovera1d8b492011-05-02 17:12:10 -07001306 spin_lock_init(&cmd->t_state_lock);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001307 cmd->transport_state = CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001308
1309 cmd->se_tfo = tfo;
1310 cmd->se_sess = se_sess;
1311 cmd->data_length = data_length;
1312 cmd->data_direction = data_direction;
1313 cmd->sam_task_attr = task_attr;
1314 cmd->sense_buffer = sense_buffer;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001315
1316 cmd->state_active = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001317}
1318EXPORT_SYMBOL(transport_init_se_cmd);
1319
1320static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1321{
1322 /*
1323 * Check if SAM Task Attribute emulation is enabled for this
1324 * struct se_device storage object
1325 */
Andy Grover5951146d2011-07-19 10:26:37 +00001326 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001327 return 0;
1328
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001329 if (cmd->sam_task_attr == MSG_ACA_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001330 pr_debug("SAM Task Attribute ACA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001331 " emulation is not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001332 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001333 }
1334 /*
1335 * Used to determine when ORDERED commands should go from
1336 * Dormant to Active status.
1337 */
Andy Grover5951146d2011-07-19 10:26:37 +00001338 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001339 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001340 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001341 cmd->se_ordered_id, cmd->sam_task_attr,
Andy Grover6708bb22011-06-08 10:36:43 -07001342 cmd->se_dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001343 return 0;
1344}
1345
Andy Grovera12f41f2012-04-03 15:51:20 -07001346/* target_setup_cmd_from_cdb():
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001347 *
1348 * Called from fabric RX Thread.
1349 */
Andy Grovera12f41f2012-04-03 15:51:20 -07001350int target_setup_cmd_from_cdb(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001351 struct se_cmd *cmd,
1352 unsigned char *cdb)
1353{
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001354 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
1355 u32 pr_reg_type = 0;
1356 u8 alua_ascq = 0;
1357 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001358 int ret;
1359
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001360 /*
1361 * Ensure that the received CDB is less than the max (252 + 8) bytes
1362 * for VARIABLE_LENGTH_CMD
1363 */
1364 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001365 pr_err("Received SCSI CDB with command_size: %d that"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001366 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1367 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001368 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1369 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grovere3d6f902011-07-19 08:55:10 +00001370 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001371 }
1372 /*
1373 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1374 * allocate the additional extended CDB buffer now.. Otherwise
1375 * setup the pointer from __t_task_cdb to t_task_cdb.
1376 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001377 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1378 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001379 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001380 if (!cmd->t_task_cdb) {
1381 pr_err("Unable to allocate cmd->t_task_cdb"
Andy Grovera1d8b492011-05-02 17:12:10 -07001382 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001383 scsi_command_size(cdb),
Andy Grovera1d8b492011-05-02 17:12:10 -07001384 (unsigned long)sizeof(cmd->__t_task_cdb));
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001385 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1386 cmd->scsi_sense_reason =
1387 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grovere3d6f902011-07-19 08:55:10 +00001388 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001389 }
1390 } else
Andy Grovera1d8b492011-05-02 17:12:10 -07001391 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001392 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07001393 * Copy the original CDB into cmd->
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001394 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001395 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001396
1397 /*
1398 * Check for an existing UNIT ATTENTION condition
1399 */
1400 if (core_scsi3_ua_check(cmd, cdb) < 0) {
1401 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1402 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
1403 return -EINVAL;
1404 }
1405
1406 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
1407 if (ret != 0) {
1408 /*
1409 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
1410 * The ALUA additional sense code qualifier (ASCQ) is determined
1411 * by the ALUA primary or secondary access state..
1412 */
1413 if (ret > 0) {
1414 pr_debug("[%s]: ALUA TG Port not available, "
1415 "SenseKey: NOT_READY, ASC/ASCQ: "
1416 "0x04/0x%02x\n",
1417 cmd->se_tfo->get_fabric_name(), alua_ascq);
1418
1419 transport_set_sense_codes(cmd, 0x04, alua_ascq);
1420 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1421 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
1422 return -EINVAL;
1423 }
1424 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1425 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1426 return -EINVAL;
1427 }
1428
1429 /*
1430 * Check status for SPC-3 Persistent Reservations
1431 */
1432 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type)) {
1433 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
1434 cmd, cdb, pr_reg_type) != 0) {
1435 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1436 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
1437 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1438 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
1439 return -EBUSY;
1440 }
1441 /*
1442 * This means the CDB is allowed for the SCSI Initiator port
1443 * when said port is *NOT* holding the legacy SPC-2 or
1444 * SPC-3 Persistent Reservation.
1445 */
1446 }
1447
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001448 ret = cmd->se_dev->transport->parse_cdb(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001449 if (ret < 0)
1450 return ret;
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001451
1452 spin_lock_irqsave(&cmd->t_state_lock, flags);
1453 cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1454 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1455
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001456 /*
1457 * Check for SAM Task Attribute Emulation
1458 */
1459 if (transport_check_alloc_task_attr(cmd) < 0) {
1460 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1461 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00001462 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001463 }
1464 spin_lock(&cmd->se_lun->lun_sep_lock);
1465 if (cmd->se_lun->lun_sep)
1466 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1467 spin_unlock(&cmd->se_lun->lun_sep_lock);
1468 return 0;
1469}
Andy Grovera12f41f2012-04-03 15:51:20 -07001470EXPORT_SYMBOL(target_setup_cmd_from_cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001471
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001472/*
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001473 * Used by fabric module frontends to queue tasks directly.
1474 * Many only be used from process context only
1475 */
1476int transport_handle_cdb_direct(
1477 struct se_cmd *cmd)
1478{
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001479 int ret;
1480
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001481 if (!cmd->se_lun) {
1482 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001483 pr_err("cmd->se_lun is NULL\n");
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001484 return -EINVAL;
1485 }
1486 if (in_interrupt()) {
1487 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001488 pr_err("transport_generic_handle_cdb cannot be called"
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001489 " from interrupt context\n");
1490 return -EINVAL;
1491 }
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001492 /*
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001493 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001494 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1495 * in existing usage to ensure that outstanding descriptors are handled
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07001496 * correctly during shutdown via transport_wait_for_tasks()
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001497 *
1498 * Also, we don't take cmd->t_state_lock here as we only expect
1499 * this to be called for initial descriptor submission.
1500 */
1501 cmd->t_state = TRANSPORT_NEW_CMD;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001502 cmd->transport_state |= CMD_T_ACTIVE;
1503
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001504 /*
1505 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1506 * so follow TRANSPORT_NEW_CMD processing thread context usage
1507 * and call transport_generic_request_failure() if necessary..
1508 */
1509 ret = transport_generic_new_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001510 if (ret < 0)
1511 transport_generic_request_failure(cmd);
1512
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001513 return 0;
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001514}
1515EXPORT_SYMBOL(transport_handle_cdb_direct);
1516
Nicholas Bellingera6360782011-11-18 20:36:22 -08001517/**
1518 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1519 *
1520 * @se_cmd: command descriptor to submit
1521 * @se_sess: associated se_sess for endpoint
1522 * @cdb: pointer to SCSI CDB
1523 * @sense: pointer to SCSI sense buffer
1524 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1525 * @data_length: fabric expected data transfer length
1526 * @task_addr: SAM task attribute
1527 * @data_dir: DMA data direction
1528 * @flags: flags for command submission from target_sc_flags_tables
1529 *
1530 * This may only be called from process context, and also currently
1531 * assumes internal allocation of fabric payload buffer by target-core.
1532 **/
Andy Grover1edcdb42012-01-19 13:39:23 -08001533void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
Nicholas Bellingera6360782011-11-18 20:36:22 -08001534 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1535 u32 data_length, int task_attr, int data_dir, int flags)
1536{
1537 struct se_portal_group *se_tpg;
1538 int rc;
1539
1540 se_tpg = se_sess->se_tpg;
1541 BUG_ON(!se_tpg);
1542 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1543 BUG_ON(in_interrupt());
1544 /*
1545 * Initialize se_cmd for target operation. From this point
1546 * exceptions are handled by sending exception status via
1547 * target_core_fabric_ops->queue_status() callback
1548 */
1549 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1550 data_length, data_dir, task_attr, sense);
Sebastian Andrzej Siewiorb0d79942012-01-10 14:16:59 +01001551 if (flags & TARGET_SCF_UNKNOWN_SIZE)
1552 se_cmd->unknown_data_length = 1;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001553 /*
1554 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1555 * se_sess->sess_cmd_list. A second kref_get here is necessary
1556 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1557 * kref_put() to happen during fabric packet acknowledgement.
1558 */
1559 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1560 /*
1561 * Signal bidirectional data payloads to target-core
1562 */
1563 if (flags & TARGET_SCF_BIDI_OP)
1564 se_cmd->se_cmd_flags |= SCF_BIDI;
1565 /*
1566 * Locate se_lun pointer and attach it to struct se_cmd
1567 */
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001568 if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1569 transport_send_check_condition_and_sense(se_cmd,
1570 se_cmd->scsi_sense_reason, 0);
1571 target_put_sess_cmd(se_sess, se_cmd);
1572 return;
1573 }
Christoph Hellwigd6e01752012-05-20 11:59:14 -04001574
Andy Grovera12f41f2012-04-03 15:51:20 -07001575 rc = target_setup_cmd_from_cdb(se_cmd, cdb);
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001576 if (rc != 0) {
1577 transport_generic_request_failure(se_cmd);
1578 return;
1579 }
Andy Grover11e319e2012-04-03 15:51:28 -07001580
1581 /*
1582 * Check if we need to delay processing because of ALUA
1583 * Active/NonOptimized primary access state..
1584 */
1585 core_alua_check_nonop_delay(se_cmd);
1586
Nicholas Bellingera6360782011-11-18 20:36:22 -08001587 /*
1588 * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
1589 * for immediate execution of READs, otherwise wait for
1590 * transport_generic_handle_data() to be called for WRITEs
1591 * when fabric has filled the incoming buffer.
1592 */
1593 transport_handle_cdb_direct(se_cmd);
Andy Grover1edcdb42012-01-19 13:39:23 -08001594 return;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001595}
1596EXPORT_SYMBOL(target_submit_cmd);
1597
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001598static void target_complete_tmr_failure(struct work_struct *work)
1599{
1600 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
1601
1602 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1603 se_cmd->se_tfo->queue_tm_rsp(se_cmd);
1604 transport_generic_free_cmd(se_cmd, 0);
1605}
1606
Andy Groverea98d7f2012-01-19 13:39:21 -08001607/**
1608 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1609 * for TMR CDBs
1610 *
1611 * @se_cmd: command descriptor to submit
1612 * @se_sess: associated se_sess for endpoint
1613 * @sense: pointer to SCSI sense buffer
1614 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1615 * @fabric_context: fabric context for TMR req
1616 * @tm_type: Type of TM request
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001617 * @gfp: gfp type for caller
1618 * @tag: referenced task tag for TMR_ABORT_TASK
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001619 * @flags: submit cmd flags
Andy Groverea98d7f2012-01-19 13:39:21 -08001620 *
1621 * Callable from all contexts.
1622 **/
1623
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001624int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
Andy Groverea98d7f2012-01-19 13:39:21 -08001625 unsigned char *sense, u32 unpacked_lun,
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001626 void *fabric_tmr_ptr, unsigned char tm_type,
1627 gfp_t gfp, unsigned int tag, int flags)
Andy Groverea98d7f2012-01-19 13:39:21 -08001628{
1629 struct se_portal_group *se_tpg;
1630 int ret;
1631
1632 se_tpg = se_sess->se_tpg;
1633 BUG_ON(!se_tpg);
1634
1635 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1636 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001637 /*
1638 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1639 * allocation failure.
1640 */
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001641 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001642 if (ret < 0)
1643 return -ENOMEM;
Andy Groverea98d7f2012-01-19 13:39:21 -08001644
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001645 if (tm_type == TMR_ABORT_TASK)
1646 se_cmd->se_tmr_req->ref_task_tag = tag;
1647
Andy Groverea98d7f2012-01-19 13:39:21 -08001648 /* See target_submit_cmd for commentary */
1649 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1650
Andy Groverea98d7f2012-01-19 13:39:21 -08001651 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1652 if (ret) {
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001653 /*
1654 * For callback during failure handling, push this work off
1655 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1656 */
1657 INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
1658 schedule_work(&se_cmd->work);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001659 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001660 }
1661 transport_generic_handle_tmr(se_cmd);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001662 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001663}
1664EXPORT_SYMBOL(target_submit_tmr);
1665
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001666/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001667 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1668 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1669 * complete setup in TCM process context w/ TFO->new_cmd_map().
1670 */
1671int transport_generic_handle_cdb_map(
1672 struct se_cmd *cmd)
1673{
Andy Grovere3d6f902011-07-19 08:55:10 +00001674 if (!cmd->se_lun) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001675 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001676 pr_err("cmd->se_lun is NULL\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001677 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001678 }
1679
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001680 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001681 return 0;
1682}
1683EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1684
1685/* transport_generic_handle_data():
1686 *
1687 *
1688 */
1689int transport_generic_handle_data(
1690 struct se_cmd *cmd)
1691{
1692 /*
1693 * For the software fabric case, then we assume the nexus is being
1694 * failed/shutdown when signals are pending from the kthread context
1695 * caller, so we return a failure. For the HW target mode case running
1696 * in interrupt code, the signal_pending() check is skipped.
1697 */
1698 if (!in_interrupt() && signal_pending(current))
Andy Grovere3d6f902011-07-19 08:55:10 +00001699 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001700 /*
1701 * If the received CDB has aleady been ABORTED by the generic
1702 * target engine, we now call transport_check_aborted_status()
1703 * to queue any delated TASK_ABORTED status for the received CDB to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001704 * fabric module as we are expecting no further incoming DATA OUT
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001705 * sequences at this point.
1706 */
1707 if (transport_check_aborted_status(cmd, 1) != 0)
1708 return 0;
1709
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001710 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001711 return 0;
1712}
1713EXPORT_SYMBOL(transport_generic_handle_data);
1714
1715/* transport_generic_handle_tmr():
1716 *
1717 *
1718 */
1719int transport_generic_handle_tmr(
1720 struct se_cmd *cmd)
1721{
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001722 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001723 return 0;
1724}
1725EXPORT_SYMBOL(transport_generic_handle_tmr);
1726
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001727/*
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001728 * If the cmd is active, request it to be stopped and sleep until it
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001729 * has completed.
1730 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001731bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001732{
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001733 bool was_active = false;
1734
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001735 if (cmd->transport_state & CMD_T_BUSY) {
1736 cmd->transport_state |= CMD_T_REQUEST_STOP;
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001737 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1738
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001739 pr_debug("cmd %p waiting to complete\n", cmd);
1740 wait_for_completion(&cmd->task_stop_comp);
1741 pr_debug("cmd %p stopped successfully\n", cmd);
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001742
1743 spin_lock_irqsave(&cmd->t_state_lock, *flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001744 cmd->transport_state &= ~CMD_T_REQUEST_STOP;
1745 cmd->transport_state &= ~CMD_T_BUSY;
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001746 was_active = true;
1747 }
1748
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001749 return was_active;
1750}
1751
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001752/*
1753 * Handle SAM-esque emulation for generic transport request failures.
1754 */
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001755void transport_generic_request_failure(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001756{
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001757 int ret = 0;
1758
Andy Grover6708bb22011-06-08 10:36:43 -07001759 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001760 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -07001761 cmd->t_task_cdb[0]);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001762 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001763 cmd->se_tfo->get_cmd_state(cmd),
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001764 cmd->t_state, cmd->scsi_sense_reason);
Christoph Hellwigd43d6ae2012-04-24 00:25:08 -04001765 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001766 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1767 (cmd->transport_state & CMD_T_STOP) != 0,
1768 (cmd->transport_state & CMD_T_SENT) != 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001769
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001770 /*
1771 * For SAM Task Attribute emulation for failed struct se_cmd
1772 */
1773 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1774 transport_complete_task_attr(cmd);
1775
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001776 switch (cmd->scsi_sense_reason) {
1777 case TCM_NON_EXISTENT_LUN:
1778 case TCM_UNSUPPORTED_SCSI_OPCODE:
1779 case TCM_INVALID_CDB_FIELD:
1780 case TCM_INVALID_PARAMETER_LIST:
1781 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1782 case TCM_UNKNOWN_MODE_PAGE:
1783 case TCM_WRITE_PROTECTED:
1784 case TCM_CHECK_CONDITION_ABORT_CMD:
1785 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1786 case TCM_CHECK_CONDITION_NOT_READY:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001787 break;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001788 case TCM_RESERVATION_CONFLICT:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001789 /*
1790 * No SENSE Data payload for this case, set SCSI Status
1791 * and queue the response to $FABRIC_MOD.
1792 *
1793 * Uses linux/include/scsi/scsi.h SAM status codes defs
1794 */
1795 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1796 /*
1797 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1798 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1799 * CONFLICT STATUS.
1800 *
1801 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1802 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001803 if (cmd->se_sess &&
1804 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1805 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001806 cmd->orig_fe_lun, 0x2C,
1807 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1808
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001809 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001810 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001811 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001812 goto check_stop;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001813 default:
Andy Grover6708bb22011-06-08 10:36:43 -07001814 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001815 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001816 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1817 break;
1818 }
Nicholas Bellinger16ab8e62011-08-08 19:03:38 -07001819 /*
1820 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1821 * make the call to transport_send_check_condition_and_sense()
1822 * directly. Otherwise expect the fabric to make the call to
1823 * transport_send_check_condition_and_sense() after handling
1824 * possible unsoliticied write data payloads.
1825 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001826 ret = transport_send_check_condition_and_sense(cmd,
1827 cmd->scsi_sense_reason, 0);
1828 if (ret == -EAGAIN || ret == -ENOMEM)
1829 goto queue_full;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001830
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001831check_stop:
1832 transport_lun_remove_cmd(cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07001833 if (!transport_cmd_check_stop_to_fabric(cmd))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001834 ;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001835 return;
1836
1837queue_full:
Christoph Hellwige057f532011-10-17 13:56:41 -04001838 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1839 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001840}
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001841EXPORT_SYMBOL(transport_generic_request_failure);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001842
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001843static void __target_execute_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001844{
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -04001845 int error = 0;
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001846
1847 spin_lock_irq(&cmd->t_state_lock);
1848 cmd->transport_state |= (CMD_T_BUSY|CMD_T_SENT);
1849 spin_unlock_irq(&cmd->t_state_lock);
1850
1851 if (cmd->execute_cmd)
1852 error = cmd->execute_cmd(cmd);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001853
1854 if (error) {
1855 spin_lock_irq(&cmd->t_state_lock);
1856 cmd->transport_state &= ~(CMD_T_BUSY|CMD_T_SENT);
1857 spin_unlock_irq(&cmd->t_state_lock);
1858
1859 transport_generic_request_failure(cmd);
1860 }
1861}
1862
1863static void target_execute_cmd(struct se_cmd *cmd)
1864{
1865 struct se_device *dev = cmd->se_dev;
1866
Christoph Hellwigf7113a42012-07-08 15:58:38 -04001867 /*
1868 * Determine if IOCTL context caller in requesting the stopping of this
1869 * command for LUN shutdown purposes.
1870 */
1871 spin_lock_irq(&cmd->t_state_lock);
1872 if (cmd->transport_state & CMD_T_LUN_STOP) {
1873 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
1874 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
1875
1876 cmd->transport_state &= ~CMD_T_ACTIVE;
1877 spin_unlock_irq(&cmd->t_state_lock);
1878 complete(&cmd->transport_lun_stop_comp);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001879 return;
Christoph Hellwigf7113a42012-07-08 15:58:38 -04001880 }
1881 /*
1882 * Determine if frontend context caller is requesting the stopping of
1883 * this command for frontend exceptions.
1884 */
1885 if (cmd->transport_state & CMD_T_STOP) {
1886 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
1887 __func__, __LINE__,
1888 cmd->se_tfo->get_task_tag(cmd));
1889
1890 spin_unlock_irq(&cmd->t_state_lock);
1891 complete(&cmd->t_transport_stop_comp);
1892 return;
1893 }
1894
1895 cmd->t_state = TRANSPORT_PROCESSING;
1896 spin_unlock_irq(&cmd->t_state_lock);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001897
1898 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1899 goto execute;
1900
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001901 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001902 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001903 * to allow the passed struct se_cmd list of tasks to the front of the list.
1904 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001905 switch (cmd->sam_task_attr) {
1906 case MSG_HEAD_TAG:
1907 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, "
1908 "se_ordered_id: %u\n",
1909 cmd->t_task_cdb[0], cmd->se_ordered_id);
1910 goto execute;
1911 case MSG_ORDERED_TAG:
1912 atomic_inc(&dev->dev_ordered_sync);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001913 smp_mb__after_atomic_inc();
1914
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001915 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, "
1916 " se_ordered_id: %u\n",
1917 cmd->t_task_cdb[0], cmd->se_ordered_id);
1918
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001919 /*
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001920 * Execute an ORDERED command if no other older commands
1921 * exist that need to be completed first.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001922 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001923 if (!atomic_read(&dev->simple_cmds))
1924 goto execute;
1925 break;
1926 default:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001927 /*
1928 * For SIMPLE and UNTAGGED Task Attribute commands
1929 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001930 atomic_inc(&dev->simple_cmds);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001931 smp_mb__after_atomic_inc();
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001932 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001933 }
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001934
1935 if (atomic_read(&dev->dev_ordered_sync) != 0) {
1936 spin_lock(&dev->delayed_cmd_lock);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001937 list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list);
1938 spin_unlock(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001939
Andy Grover6708bb22011-06-08 10:36:43 -07001940 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001941 " delayed CMD list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07001942 cmd->t_task_cdb[0], cmd->sam_task_attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001943 cmd->se_ordered_id);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001944 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001945 }
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001946
1947execute:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001948 /*
1949 * Otherwise, no ORDERED task attributes exist..
1950 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001951 __target_execute_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001952}
1953
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001954/*
1955 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
1956 */
1957static int transport_get_sense_data(struct se_cmd *cmd)
1958{
1959 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
Christoph Hellwig42bf8292011-10-12 11:07:00 -04001960 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001961 unsigned long flags;
1962 u32 offset = 0;
1963
Andy Grovere3d6f902011-07-19 08:55:10 +00001964 WARN_ON(!cmd->se_lun);
1965
Christoph Hellwig42bf8292011-10-12 11:07:00 -04001966 if (!dev)
1967 return 0;
1968
Andy Grovera1d8b492011-05-02 17:12:10 -07001969 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001970 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07001971 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001972 return 0;
1973 }
1974
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001975 if (!(cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
1976 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001977
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001978 if (!dev->transport->get_sense_buffer) {
1979 pr_err("dev->transport->get_sense_buffer is NULL\n");
1980 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001981 }
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001982
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001983 sense_buffer = dev->transport->get_sense_buffer(cmd);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001984 if (!sense_buffer) {
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001985 pr_err("ITT 0x%08x cmd %p: Unable to locate"
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001986 " sense buffer for task with sense\n",
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001987 cmd->se_tfo->get_task_tag(cmd), cmd);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001988 goto out;
1989 }
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001990
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001991 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1992
1993 offset = cmd->se_tfo->set_fabric_sense_len(cmd, TRANSPORT_SENSE_BUFFER);
1994
1995 memcpy(&buffer[offset], sense_buffer, TRANSPORT_SENSE_BUFFER);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001996
1997 /* Automatically padded */
1998 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
1999
2000 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x and sense\n",
2001 dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
2002 return 0;
2003
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002004out:
Andy Grovera1d8b492011-05-02 17:12:10 -07002005 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002006 return -1;
2007}
2008
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002009/*
Christoph Hellwig5f41a312012-05-20 14:34:44 -04002010 * Process all commands up to the last received ORDERED task attribute which
2011 * requires another blocking boundary
2012 */
2013static void target_restart_delayed_cmds(struct se_device *dev)
2014{
2015 for (;;) {
2016 struct se_cmd *cmd;
2017
2018 spin_lock(&dev->delayed_cmd_lock);
2019 if (list_empty(&dev->delayed_cmd_list)) {
2020 spin_unlock(&dev->delayed_cmd_lock);
2021 break;
2022 }
2023
2024 cmd = list_entry(dev->delayed_cmd_list.next,
2025 struct se_cmd, se_delayed_node);
2026 list_del(&cmd->se_delayed_node);
2027 spin_unlock(&dev->delayed_cmd_lock);
2028
2029 __target_execute_cmd(cmd);
2030
2031 if (cmd->sam_task_attr == MSG_ORDERED_TAG)
2032 break;
2033 }
2034}
2035
2036/*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002037 * Called from I/O completion to determine which dormant/delayed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002038 * and ordered cmds need to have their tasks added to the execution queue.
2039 */
2040static void transport_complete_task_attr(struct se_cmd *cmd)
2041{
Andy Grover5951146d2011-07-19 10:26:37 +00002042 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002043
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002044 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002045 atomic_dec(&dev->simple_cmds);
2046 smp_mb__after_atomic_dec();
2047 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07002048 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002049 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
2050 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002051 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002052 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07002053 pr_debug("Incremented dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002054 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
2055 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002056 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002057 atomic_dec(&dev->dev_ordered_sync);
2058 smp_mb__after_atomic_dec();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002059
2060 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07002061 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002062 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
2063 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002064
Christoph Hellwig5f41a312012-05-20 14:34:44 -04002065 target_restart_delayed_cmds(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002066}
2067
Christoph Hellwige057f532011-10-17 13:56:41 -04002068static void transport_complete_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002069{
2070 int ret = 0;
2071
Christoph Hellwige057f532011-10-17 13:56:41 -04002072 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2073 transport_complete_task_attr(cmd);
2074
2075 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
2076 ret = cmd->se_tfo->queue_status(cmd);
2077 if (ret)
2078 goto out;
2079 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002080
2081 switch (cmd->data_direction) {
2082 case DMA_FROM_DEVICE:
2083 ret = cmd->se_tfo->queue_data_in(cmd);
2084 break;
2085 case DMA_TO_DEVICE:
Andy Groverec98f782011-07-20 19:28:46 +00002086 if (cmd->t_bidi_data_sg) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002087 ret = cmd->se_tfo->queue_data_in(cmd);
2088 if (ret < 0)
Christoph Hellwige057f532011-10-17 13:56:41 -04002089 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002090 }
2091 /* Fall through for DMA_TO_DEVICE */
2092 case DMA_NONE:
2093 ret = cmd->se_tfo->queue_status(cmd);
2094 break;
2095 default:
2096 break;
2097 }
2098
Christoph Hellwige057f532011-10-17 13:56:41 -04002099out:
2100 if (ret < 0) {
2101 transport_handle_queue_full(cmd, cmd->se_dev);
2102 return;
2103 }
2104 transport_lun_remove_cmd(cmd);
2105 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002106}
2107
2108static void transport_handle_queue_full(
2109 struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -04002110 struct se_device *dev)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002111{
2112 spin_lock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002113 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
2114 atomic_inc(&dev->dev_qf_count);
2115 smp_mb__after_atomic_inc();
2116 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
2117
2118 schedule_work(&cmd->se_dev->qf_work_queue);
2119}
2120
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002121static void target_complete_ok_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002122{
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002123 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002124 int reason = 0, ret;
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002125
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002126 /*
2127 * Check if we need to move delayed/dormant tasks from cmds on the
2128 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
2129 * Attribute.
2130 */
Andy Grover5951146d2011-07-19 10:26:37 +00002131 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002132 transport_complete_task_attr(cmd);
2133 /*
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002134 * Check to schedule QUEUE_FULL work, or execute an existing
2135 * cmd->transport_qf_callback()
2136 */
2137 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
2138 schedule_work(&cmd->se_dev->qf_work_queue);
2139
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002140 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002141 * Check if we need to retrieve a sense buffer from
2142 * the struct se_cmd in question.
2143 */
2144 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
2145 if (transport_get_sense_data(cmd) < 0)
2146 reason = TCM_NON_EXISTENT_LUN;
2147
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002148 if (cmd->scsi_status) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002149 ret = transport_send_check_condition_and_sense(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002150 cmd, reason, 1);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002151 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002152 goto queue_full;
2153
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002154 transport_lun_remove_cmd(cmd);
2155 transport_cmd_check_stop_to_fabric(cmd);
2156 return;
2157 }
2158 }
2159 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002160 * Check for a callback, used by amongst other things
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002161 * XDWRITE_READ_10 emulation.
2162 */
2163 if (cmd->transport_complete_callback)
2164 cmd->transport_complete_callback(cmd);
2165
2166 switch (cmd->data_direction) {
2167 case DMA_FROM_DEVICE:
2168 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00002169 if (cmd->se_lun->lun_sep) {
2170 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002171 cmd->data_length;
2172 }
2173 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002174
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002175 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002176 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002177 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002178 break;
2179 case DMA_TO_DEVICE:
2180 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00002181 if (cmd->se_lun->lun_sep) {
2182 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002183 cmd->data_length;
2184 }
2185 spin_unlock(&cmd->se_lun->lun_sep_lock);
2186 /*
2187 * Check if we need to send READ payload for BIDI-COMMAND
2188 */
Andy Groverec98f782011-07-20 19:28:46 +00002189 if (cmd->t_bidi_data_sg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002190 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00002191 if (cmd->se_lun->lun_sep) {
2192 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002193 cmd->data_length;
2194 }
2195 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002196 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002197 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002198 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002199 break;
2200 }
2201 /* Fall through for DMA_TO_DEVICE */
2202 case DMA_NONE:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002203 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002204 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002205 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002206 break;
2207 default:
2208 break;
2209 }
2210
2211 transport_lun_remove_cmd(cmd);
2212 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002213 return;
2214
2215queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07002216 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002217 " data_direction: %d\n", cmd, cmd->data_direction);
Christoph Hellwige057f532011-10-17 13:56:41 -04002218 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
2219 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002220}
2221
Andy Grover6708bb22011-06-08 10:36:43 -07002222static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002223{
Andy Groverec98f782011-07-20 19:28:46 +00002224 struct scatterlist *sg;
Andy Groverec98f782011-07-20 19:28:46 +00002225 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002226
Andy Grover6708bb22011-06-08 10:36:43 -07002227 for_each_sg(sgl, sg, nents, count)
2228 __free_page(sg_page(sg));
2229
2230 kfree(sgl);
2231}
2232
2233static inline void transport_free_pages(struct se_cmd *cmd)
2234{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002235 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
Andy Grover6708bb22011-06-08 10:36:43 -07002236 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002237
Andy Grover6708bb22011-06-08 10:36:43 -07002238 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00002239 cmd->t_data_sg = NULL;
2240 cmd->t_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002241
Andy Grover6708bb22011-06-08 10:36:43 -07002242 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00002243 cmd->t_bidi_data_sg = NULL;
2244 cmd->t_bidi_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002245}
2246
Christoph Hellwigd3df7822011-09-13 23:08:32 +02002247/**
Christoph Hellwige26d99a2011-11-14 12:30:30 -05002248 * transport_release_cmd - free a command
2249 * @cmd: command to free
2250 *
2251 * This routine unconditionally frees a command, and reference counting
2252 * or list removal must be done in the caller.
2253 */
2254static void transport_release_cmd(struct se_cmd *cmd)
2255{
2256 BUG_ON(!cmd->se_tfo);
2257
Andy Groverc8e31f22012-01-19 13:39:17 -08002258 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Christoph Hellwige26d99a2011-11-14 12:30:30 -05002259 core_tmr_release_req(cmd->se_tmr_req);
2260 if (cmd->t_task_cdb != cmd->__t_task_cdb)
2261 kfree(cmd->t_task_cdb);
2262 /*
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002263 * If this cmd has been setup with target_get_sess_cmd(), drop
2264 * the kref and call ->release_cmd() in kref callback.
Christoph Hellwige26d99a2011-11-14 12:30:30 -05002265 */
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002266 if (cmd->check_release != 0) {
2267 target_put_sess_cmd(cmd->se_sess, cmd);
2268 return;
2269 }
Christoph Hellwige26d99a2011-11-14 12:30:30 -05002270 cmd->se_tfo->release_cmd(cmd);
2271}
2272
2273/**
Christoph Hellwigd3df7822011-09-13 23:08:32 +02002274 * transport_put_cmd - release a reference to a command
2275 * @cmd: command to release
2276 *
2277 * This routine releases our reference to the command and frees it if possible.
2278 */
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002279static void transport_put_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002280{
2281 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002282
Andy Grovera1d8b492011-05-02 17:12:10 -07002283 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02002284 if (atomic_read(&cmd->t_fe_count)) {
2285 if (!atomic_dec_and_test(&cmd->t_fe_count))
2286 goto out_busy;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002287 }
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02002288
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002289 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
2290 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002291 target_remove_from_state_list(cmd);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02002292 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002293 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002294
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002295 transport_free_pages(cmd);
Christoph Hellwig31afc392011-09-13 23:08:11 +02002296 transport_release_cmd(cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002297 return;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02002298out_busy:
2299 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002300}
2301
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002302/*
Andy Groverec98f782011-07-20 19:28:46 +00002303 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
2304 * allocating in the core.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002305 * @cmd: Associated se_cmd descriptor
2306 * @mem: SGL style memory for TCM WRITE / READ
2307 * @sg_mem_num: Number of SGL elements
2308 * @mem_bidi_in: SGL style memory for TCM BIDI READ
2309 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
2310 *
2311 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
2312 * of parameters.
2313 */
2314int transport_generic_map_mem_to_cmd(
2315 struct se_cmd *cmd,
Andy Grover5951146d2011-07-19 10:26:37 +00002316 struct scatterlist *sgl,
2317 u32 sgl_count,
2318 struct scatterlist *sgl_bidi,
2319 u32 sgl_bidi_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002320{
Andy Grover5951146d2011-07-19 10:26:37 +00002321 if (!sgl || !sgl_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002322 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002323
Christoph Hellwig64f1db32012-05-20 11:59:11 -04002324 /*
2325 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
2326 * scatterlists already have been set to follow what the fabric
2327 * passes for the original expected data transfer length.
2328 */
2329 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2330 pr_warn("Rejecting SCSI DATA overflow for fabric using"
2331 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
2332 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2333 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2334 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002335 }
2336
Christoph Hellwig64f1db32012-05-20 11:59:11 -04002337 cmd->t_data_sg = sgl;
2338 cmd->t_data_nents = sgl_count;
2339
2340 if (sgl_bidi && sgl_bidi_count) {
2341 cmd->t_bidi_data_sg = sgl_bidi;
2342 cmd->t_bidi_data_nents = sgl_bidi_count;
2343 }
2344 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002345 return 0;
2346}
2347EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
2348
Andy Grover49493142012-01-16 16:57:08 -08002349void *transport_kmap_data_sg(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002350{
Andy Groverec98f782011-07-20 19:28:46 +00002351 struct scatterlist *sg = cmd->t_data_sg;
Andy Grover49493142012-01-16 16:57:08 -08002352 struct page **pages;
2353 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002354
Andy Groverec98f782011-07-20 19:28:46 +00002355 BUG_ON(!sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002356 /*
Andy Groverec98f782011-07-20 19:28:46 +00002357 * We need to take into account a possible offset here for fabrics like
2358 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
2359 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
Andy Grover05d1c7c2011-07-20 19:13:28 +00002360 */
Andy Grover49493142012-01-16 16:57:08 -08002361 if (!cmd->t_data_nents)
2362 return NULL;
2363 else if (cmd->t_data_nents == 1)
2364 return kmap(sg_page(sg)) + sg->offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002365
Andy Grover49493142012-01-16 16:57:08 -08002366 /* >1 page. use vmap */
2367 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
2368 if (!pages)
2369 return NULL;
2370
2371 /* convert sg[] to pages[] */
2372 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
2373 pages[i] = sg_page(sg);
2374 }
2375
2376 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
2377 kfree(pages);
2378 if (!cmd->t_data_vmap)
2379 return NULL;
2380
2381 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002382}
Andy Grover49493142012-01-16 16:57:08 -08002383EXPORT_SYMBOL(transport_kmap_data_sg);
2384
2385void transport_kunmap_data_sg(struct se_cmd *cmd)
2386{
Andy Grovera1edf9c2012-02-09 12:18:06 -08002387 if (!cmd->t_data_nents) {
Andy Grover49493142012-01-16 16:57:08 -08002388 return;
Andy Grovera1edf9c2012-02-09 12:18:06 -08002389 } else if (cmd->t_data_nents == 1) {
Andy Grover49493142012-01-16 16:57:08 -08002390 kunmap(sg_page(cmd->t_data_sg));
Andy Grovera1edf9c2012-02-09 12:18:06 -08002391 return;
2392 }
Andy Grover49493142012-01-16 16:57:08 -08002393
2394 vunmap(cmd->t_data_vmap);
2395 cmd->t_data_vmap = NULL;
2396}
2397EXPORT_SYMBOL(transport_kunmap_data_sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002398
2399static int
2400transport_generic_get_mem(struct se_cmd *cmd)
2401{
Andy Groverec98f782011-07-20 19:28:46 +00002402 u32 length = cmd->data_length;
2403 unsigned int nents;
2404 struct page *page;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08002405 gfp_t zero_flag;
Andy Groverec98f782011-07-20 19:28:46 +00002406 int i = 0;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002407
Andy Groverec98f782011-07-20 19:28:46 +00002408 nents = DIV_ROUND_UP(length, PAGE_SIZE);
2409 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
2410 if (!cmd->t_data_sg)
Andy Grovere3d6f902011-07-19 08:55:10 +00002411 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002412
Andy Groverec98f782011-07-20 19:28:46 +00002413 cmd->t_data_nents = nents;
2414 sg_init_table(cmd->t_data_sg, nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002415
Christoph Hellwig64f1db32012-05-20 11:59:11 -04002416 zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_CDB ? 0 : __GFP_ZERO;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08002417
Andy Groverec98f782011-07-20 19:28:46 +00002418 while (length) {
2419 u32 page_len = min_t(u32, length, PAGE_SIZE);
roland@purestorage.com9db9da32012-01-04 15:59:58 -08002420 page = alloc_page(GFP_KERNEL | zero_flag);
Andy Groverec98f782011-07-20 19:28:46 +00002421 if (!page)
2422 goto out;
2423
2424 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
2425 length -= page_len;
2426 i++;
2427 }
2428 return 0;
2429
2430out:
2431 while (i >= 0) {
2432 __free_page(sg_page(&cmd->t_data_sg[i]));
2433 i--;
2434 }
2435 kfree(cmd->t_data_sg);
2436 cmd->t_data_sg = NULL;
2437 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002438}
2439
Andy Grovera1d8b492011-05-02 17:12:10 -07002440/*
Andy Groverb16a35b2012-04-03 15:51:21 -07002441 * Allocate any required resources to execute the command. For writes we
2442 * might not have the payload yet, so notify the fabric via a call to
2443 * ->write_pending instead. Otherwise place it on the execution queue.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002444 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002445int transport_generic_new_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002446{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002447 int ret = 0;
2448
2449 /*
2450 * Determine is the TCM fabric module has already allocated physical
2451 * memory, and is directly calling transport_generic_map_mem_to_cmd()
Andy Groverec98f782011-07-20 19:28:46 +00002452 * beforehand.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002453 */
Andy Groverec98f782011-07-20 19:28:46 +00002454 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
2455 cmd->data_length) {
Andy Grover05d1c7c2011-07-20 19:13:28 +00002456 ret = transport_generic_get_mem(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002457 if (ret < 0)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002458 goto out_fail;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002459 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04002460
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04002461 /* Workaround for handling zero-length control CDBs */
Christoph Hellwig64f1db32012-05-20 11:59:11 -04002462 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && !cmd->data_length) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002463 spin_lock_irq(&cmd->t_state_lock);
Roland Dreier410f6702011-11-22 13:51:32 -08002464 cmd->t_state = TRANSPORT_COMPLETE;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002465 cmd->transport_state |= CMD_T_ACTIVE;
2466 spin_unlock_irq(&cmd->t_state_lock);
Nicholas Bellinger91ec1d32012-01-13 12:01:34 -08002467
2468 if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
2469 u8 ua_asc = 0, ua_ascq = 0;
2470
2471 core_scsi3_ua_clear_for_request_sense(cmd,
2472 &ua_asc, &ua_ascq);
2473 }
2474
Roland Dreier410f6702011-11-22 13:51:32 -08002475 INIT_WORK(&cmd->work, target_complete_ok_work);
2476 queue_work(target_completion_wq, &cmd->work);
2477 return 0;
2478 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04002479
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04002480 atomic_inc(&cmd->t_fe_count);
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04002481
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002482 /*
Christoph Hellwig5787cac2012-04-24 00:25:06 -04002483 * For WRITEs, let the fabric know its buffer is ready.
2484 *
2485 * The command will be added to the execution queue after its write
2486 * data has arrived.
Christoph Hellwig5f41a312012-05-20 14:34:44 -04002487 *
Christoph Hellwig5787cac2012-04-24 00:25:06 -04002488 * Everything else but a WRITE, add the command to the execution queue.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002489 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04002490 target_add_to_state_list(cmd);
2491 if (cmd->data_direction == DMA_TO_DEVICE)
2492 return transport_generic_write_pending(cmd);
2493 target_execute_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002494 return 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04002495
2496out_fail:
2497 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2498 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2499 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002500}
Andy Grovera1d8b492011-05-02 17:12:10 -07002501EXPORT_SYMBOL(transport_generic_new_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002502
2503/* transport_generic_process_write():
2504 *
2505 *
2506 */
2507void transport_generic_process_write(struct se_cmd *cmd)
2508{
Christoph Hellwig5f41a312012-05-20 14:34:44 -04002509 target_execute_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002510}
2511EXPORT_SYMBOL(transport_generic_process_write);
2512
Christoph Hellwige057f532011-10-17 13:56:41 -04002513static void transport_write_pending_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002514{
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002515 int ret;
2516
2517 ret = cmd->se_tfo->write_pending(cmd);
2518 if (ret == -EAGAIN || ret == -ENOMEM) {
Christoph Hellwige057f532011-10-17 13:56:41 -04002519 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
2520 cmd);
2521 transport_handle_queue_full(cmd, cmd->se_dev);
2522 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002523}
2524
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002525static int transport_generic_write_pending(struct se_cmd *cmd)
2526{
2527 unsigned long flags;
2528 int ret;
2529
Andy Grovera1d8b492011-05-02 17:12:10 -07002530 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002531 cmd->t_state = TRANSPORT_WRITE_PENDING;
Andy Grovera1d8b492011-05-02 17:12:10 -07002532 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002533
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002534 /*
2535 * Clear the se_cmd for WRITE_PENDING status in order to set
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002536 * CMD_T_ACTIVE so that transport_generic_handle_data can be called
2537 * from HW target mode interrupt code. This is safe to be called
Christoph Hellwigf7113a42012-07-08 15:58:38 -04002538 * with remove_from_lists false before the cmd->se_tfo->write_pending
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002539 * because the se_cmd->se_lun pointer is not being cleared.
2540 */
Christoph Hellwigf7113a42012-07-08 15:58:38 -04002541 transport_cmd_check_stop(cmd, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002542
2543 /*
2544 * Call the fabric write_pending function here to let the
2545 * frontend know that WRITE buffers are ready.
2546 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002547 ret = cmd->se_tfo->write_pending(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002548 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002549 goto queue_full;
2550 else if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002551 return ret;
2552
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002553 return 1;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002554
2555queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07002556 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002557 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
Christoph Hellwige057f532011-10-17 13:56:41 -04002558 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002559 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002560}
2561
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002562void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002563{
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002564 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
Andy Groverc8e31f22012-01-19 13:39:17 -08002565 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002566 transport_wait_for_tasks(cmd);
2567
Christoph Hellwig35462972011-05-31 23:56:57 -04002568 transport_release_cmd(cmd);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002569 } else {
2570 if (wait_for_tasks)
2571 transport_wait_for_tasks(cmd);
2572
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002573 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
2574
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +02002575 if (cmd->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002576 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002577
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002578 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002579 }
2580}
2581EXPORT_SYMBOL(transport_generic_free_cmd);
2582
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002583/* target_get_sess_cmd - Add command to active ->sess_cmd_list
2584 * @se_sess: session to reference
2585 * @se_cmd: command descriptor to add
Nicholas Bellingera6360782011-11-18 20:36:22 -08002586 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002587 */
Nicholas Bellingera6360782011-11-18 20:36:22 -08002588void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
2589 bool ack_kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002590{
2591 unsigned long flags;
2592
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002593 kref_init(&se_cmd->cmd_kref);
Nicholas Bellingera6360782011-11-18 20:36:22 -08002594 /*
2595 * Add a second kref if the fabric caller is expecting to handle
2596 * fabric acknowledgement that requires two target_put_sess_cmd()
2597 * invocations before se_cmd descriptor release.
2598 */
Nicholas Bellinger86715562012-02-13 01:07:22 -08002599 if (ack_kref == true) {
Nicholas Bellingera6360782011-11-18 20:36:22 -08002600 kref_get(&se_cmd->cmd_kref);
Nicholas Bellinger86715562012-02-13 01:07:22 -08002601 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
2602 }
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002603
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002604 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2605 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
2606 se_cmd->check_release = 1;
2607 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2608}
2609EXPORT_SYMBOL(target_get_sess_cmd);
2610
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002611static void target_release_cmd_kref(struct kref *kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002612{
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002613 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
2614 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002615 unsigned long flags;
2616
2617 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2618 if (list_empty(&se_cmd->se_cmd_list)) {
2619 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Nicholas Bellingerffc32d52012-02-13 02:35:01 -08002620 se_cmd->se_tfo->release_cmd(se_cmd);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002621 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002622 }
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002623 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
2624 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2625 complete(&se_cmd->cmd_wait_comp);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002626 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002627 }
2628 list_del(&se_cmd->se_cmd_list);
2629 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2630
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002631 se_cmd->se_tfo->release_cmd(se_cmd);
2632}
2633
2634/* target_put_sess_cmd - Check for active I/O shutdown via kref_put
2635 * @se_sess: session to reference
2636 * @se_cmd: command descriptor to drop
2637 */
2638int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
2639{
2640 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002641}
2642EXPORT_SYMBOL(target_put_sess_cmd);
2643
2644/* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
2645 * @se_sess: session to split
2646 */
2647void target_splice_sess_cmd_list(struct se_session *se_sess)
2648{
2649 struct se_cmd *se_cmd;
2650 unsigned long flags;
2651
2652 WARN_ON(!list_empty(&se_sess->sess_wait_list));
2653 INIT_LIST_HEAD(&se_sess->sess_wait_list);
2654
2655 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2656 se_sess->sess_tearing_down = 1;
2657
2658 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
2659
2660 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
2661 se_cmd->cmd_wait_set = 1;
2662
2663 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2664}
2665EXPORT_SYMBOL(target_splice_sess_cmd_list);
2666
2667/* target_wait_for_sess_cmds - Wait for outstanding descriptors
2668 * @se_sess: session to wait for active I/O
2669 * @wait_for_tasks: Make extra transport_wait_for_tasks call
2670 */
2671void target_wait_for_sess_cmds(
2672 struct se_session *se_sess,
2673 int wait_for_tasks)
2674{
2675 struct se_cmd *se_cmd, *tmp_cmd;
2676 bool rc = false;
2677
2678 list_for_each_entry_safe(se_cmd, tmp_cmd,
2679 &se_sess->sess_wait_list, se_cmd_list) {
2680 list_del(&se_cmd->se_cmd_list);
2681
2682 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
2683 " %d\n", se_cmd, se_cmd->t_state,
2684 se_cmd->se_tfo->get_cmd_state(se_cmd));
2685
2686 if (wait_for_tasks) {
2687 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
2688 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2689 se_cmd->se_tfo->get_cmd_state(se_cmd));
2690
2691 rc = transport_wait_for_tasks(se_cmd);
2692
2693 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
2694 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2695 se_cmd->se_tfo->get_cmd_state(se_cmd));
2696 }
2697
2698 if (!rc) {
2699 wait_for_completion(&se_cmd->cmd_wait_comp);
2700 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
2701 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2702 se_cmd->se_tfo->get_cmd_state(se_cmd));
2703 }
2704
2705 se_cmd->se_tfo->release_cmd(se_cmd);
2706 }
2707}
2708EXPORT_SYMBOL(target_wait_for_sess_cmds);
2709
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002710/* transport_lun_wait_for_tasks():
2711 *
2712 * Called from ConfigFS context to stop the passed struct se_cmd to allow
2713 * an struct se_lun to be successfully shutdown.
2714 */
2715static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
2716{
2717 unsigned long flags;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002718 int ret = 0;
2719
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002720 /*
2721 * If the frontend has already requested this struct se_cmd to
2722 * be stopped, we can safely ignore this struct se_cmd.
2723 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002724 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002725 if (cmd->transport_state & CMD_T_STOP) {
2726 cmd->transport_state &= ~CMD_T_LUN_STOP;
2727
2728 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
2729 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07002730 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwigf7113a42012-07-08 15:58:38 -04002731 transport_cmd_check_stop(cmd, false);
Andy Grovere3d6f902011-07-19 08:55:10 +00002732 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002733 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002734 cmd->transport_state |= CMD_T_LUN_FE_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07002735 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002736
Andy Grover5951146d2011-07-19 10:26:37 +00002737 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002738
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002739 // XXX: audit task_flags checks.
2740 spin_lock_irqsave(&cmd->t_state_lock, flags);
2741 if ((cmd->transport_state & CMD_T_BUSY) &&
2742 (cmd->transport_state & CMD_T_SENT)) {
2743 if (!target_stop_cmd(cmd, &flags))
2744 ret++;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002745 }
Christoph Hellwig5f41a312012-05-20 14:34:44 -04002746 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002747
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002748 pr_debug("ConfigFS: cmd: %p stop tasks ret:"
2749 " %d\n", cmd, ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002750 if (!ret) {
Andy Grover6708bb22011-06-08 10:36:43 -07002751 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002752 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07002753 wait_for_completion(&cmd->transport_lun_stop_comp);
Andy Grover6708bb22011-06-08 10:36:43 -07002754 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002755 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002756 }
Christoph Hellwig3df8d402011-10-17 13:56:43 -04002757 transport_remove_cmd_from_queue(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002758
2759 return 0;
2760}
2761
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002762static void __transport_clear_lun_from_sessions(struct se_lun *lun)
2763{
2764 struct se_cmd *cmd = NULL;
2765 unsigned long lun_flags, cmd_flags;
2766 /*
2767 * Do exception processing and return CHECK_CONDITION status to the
2768 * Initiator Port.
2769 */
2770 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
Andy Grover5951146d2011-07-19 10:26:37 +00002771 while (!list_empty(&lun->lun_cmd_list)) {
2772 cmd = list_first_entry(&lun->lun_cmd_list,
2773 struct se_cmd, se_lun_node);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -05002774 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002775
Andy Grovera1d8b492011-05-02 17:12:10 -07002776 spin_lock(&cmd->t_state_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07002777 pr_debug("SE_LUN[%d] - Setting cmd->transport"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002778 "_lun_stop for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002779 cmd->se_lun->unpacked_lun,
2780 cmd->se_tfo->get_task_tag(cmd));
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002781 cmd->transport_state |= CMD_T_LUN_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07002782 spin_unlock(&cmd->t_state_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002783
2784 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
2785
Andy Grover6708bb22011-06-08 10:36:43 -07002786 if (!cmd->se_lun) {
2787 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002788 cmd->se_tfo->get_task_tag(cmd),
2789 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002790 BUG();
2791 }
2792 /*
2793 * If the Storage engine still owns the iscsi_cmd_t, determine
2794 * and/or stop its context.
2795 */
Andy Grover6708bb22011-06-08 10:36:43 -07002796 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
Andy Grovere3d6f902011-07-19 08:55:10 +00002797 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
2798 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002799
Andy Grovere3d6f902011-07-19 08:55:10 +00002800 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002801 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2802 continue;
2803 }
2804
Andy Grover6708bb22011-06-08 10:36:43 -07002805 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002806 "_wait_for_tasks(): SUCCESS\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002807 cmd->se_lun->unpacked_lun,
2808 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002809
Andy Grovera1d8b492011-05-02 17:12:10 -07002810 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002811 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002812 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002813 goto check_cond;
2814 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002815 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002816 target_remove_from_state_list(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002817 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002818
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002819 /*
2820 * The Storage engine stopped this struct se_cmd before it was
2821 * send to the fabric frontend for delivery back to the
2822 * Initiator Node. Return this SCSI CDB back with an
2823 * CHECK_CONDITION status.
2824 */
2825check_cond:
2826 transport_send_check_condition_and_sense(cmd,
2827 TCM_NON_EXISTENT_LUN, 0);
2828 /*
2829 * If the fabric frontend is waiting for this iscsi_cmd_t to
2830 * be released, notify the waiting thread now that LU has
2831 * finished accessing it.
2832 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002833 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002834 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07002835 pr_debug("SE_LUN[%d] - Detected FE stop for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002836 " struct se_cmd: %p ITT: 0x%08x\n",
2837 lun->unpacked_lun,
Andy Grovere3d6f902011-07-19 08:55:10 +00002838 cmd, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002839
Andy Grovera1d8b492011-05-02 17:12:10 -07002840 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002841 cmd_flags);
Christoph Hellwigf7113a42012-07-08 15:58:38 -04002842 transport_cmd_check_stop(cmd, false);
Andy Grovera1d8b492011-05-02 17:12:10 -07002843 complete(&cmd->transport_lun_fe_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002844 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2845 continue;
2846 }
Andy Grover6708bb22011-06-08 10:36:43 -07002847 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002848 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002849
Andy Grovera1d8b492011-05-02 17:12:10 -07002850 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002851 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2852 }
2853 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
2854}
2855
2856static int transport_clear_lun_thread(void *p)
2857{
Jörn Engel8359cf42011-11-24 02:05:51 +01002858 struct se_lun *lun = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002859
2860 __transport_clear_lun_from_sessions(lun);
2861 complete(&lun->lun_shutdown_comp);
2862
2863 return 0;
2864}
2865
2866int transport_clear_lun_from_sessions(struct se_lun *lun)
2867{
2868 struct task_struct *kt;
2869
Andy Grover5951146d2011-07-19 10:26:37 +00002870 kt = kthread_run(transport_clear_lun_thread, lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002871 "tcm_cl_%u", lun->unpacked_lun);
2872 if (IS_ERR(kt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002873 pr_err("Unable to start clear_lun thread\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00002874 return PTR_ERR(kt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002875 }
2876 wait_for_completion(&lun->lun_shutdown_comp);
2877
2878 return 0;
2879}
2880
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002881/**
2882 * transport_wait_for_tasks - wait for completion to occur
2883 * @cmd: command to wait
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002884 *
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002885 * Called from frontend fabric context to wait for storage engine
2886 * to pause and/or release frontend generated struct se_cmd.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002887 */
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002888bool transport_wait_for_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002889{
2890 unsigned long flags;
2891
Andy Grovera1d8b492011-05-02 17:12:10 -07002892 spin_lock_irqsave(&cmd->t_state_lock, flags);
Andy Groverc8e31f22012-01-19 13:39:17 -08002893 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
2894 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002895 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002896 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002897 }
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04002898
Andy Groverc8e31f22012-01-19 13:39:17 -08002899 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
2900 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002901 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002902 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002903 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002904 /*
2905 * If we are already stopped due to an external event (ie: LUN shutdown)
2906 * sleep until the connection can have the passed struct se_cmd back.
Andy Grovera1d8b492011-05-02 17:12:10 -07002907 * The cmd->transport_lun_stopped_sem will be upped by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002908 * transport_clear_lun_from_sessions() once the ConfigFS context caller
2909 * has completed its operation on the struct se_cmd.
2910 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002911 if (cmd->transport_state & CMD_T_LUN_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07002912 pr_debug("wait_for_tasks: Stopping"
Andy Grovere3d6f902011-07-19 08:55:10 +00002913 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002914 "_stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002915 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002916 /*
2917 * There is a special case for WRITES where a FE exception +
2918 * LUN shutdown means ConfigFS context is still sleeping on
2919 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
2920 * We go ahead and up transport_lun_stop_comp just to be sure
2921 * here.
2922 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002923 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2924 complete(&cmd->transport_lun_stop_comp);
2925 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
2926 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002927
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002928 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002929 /*
2930 * At this point, the frontend who was the originator of this
2931 * struct se_cmd, now owns the structure and can be released through
2932 * normal means below.
2933 */
Andy Grover6708bb22011-06-08 10:36:43 -07002934 pr_debug("wait_for_tasks: Stopped"
Andy Grovere3d6f902011-07-19 08:55:10 +00002935 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002936 "stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002937 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002938
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002939 cmd->transport_state &= ~CMD_T_LUN_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002940 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002941
Nicholas Bellinger3d289342012-02-13 02:38:14 -08002942 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002943 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002944 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002945 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002946
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002947 cmd->transport_state |= CMD_T_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002948
Andy Grover6708bb22011-06-08 10:36:43 -07002949 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002950 " i_state: %d, t_state: %d, CMD_T_STOP\n",
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04002951 cmd, cmd->se_tfo->get_task_tag(cmd),
2952 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002953
Andy Grovera1d8b492011-05-02 17:12:10 -07002954 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002955
Andy Grover5951146d2011-07-19 10:26:37 +00002956 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002957
Andy Grovera1d8b492011-05-02 17:12:10 -07002958 wait_for_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002959
Andy Grovera1d8b492011-05-02 17:12:10 -07002960 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002961 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002962
Andy Grover6708bb22011-06-08 10:36:43 -07002963 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
Andy Grovera1d8b492011-05-02 17:12:10 -07002964 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002965 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002966
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002967 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002968
2969 return true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002970}
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002971EXPORT_SYMBOL(transport_wait_for_tasks);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002972
2973static int transport_get_sense_codes(
2974 struct se_cmd *cmd,
2975 u8 *asc,
2976 u8 *ascq)
2977{
2978 *asc = cmd->scsi_asc;
2979 *ascq = cmd->scsi_ascq;
2980
2981 return 0;
2982}
2983
2984static int transport_set_sense_codes(
2985 struct se_cmd *cmd,
2986 u8 asc,
2987 u8 ascq)
2988{
2989 cmd->scsi_asc = asc;
2990 cmd->scsi_ascq = ascq;
2991
2992 return 0;
2993}
2994
2995int transport_send_check_condition_and_sense(
2996 struct se_cmd *cmd,
2997 u8 reason,
2998 int from_transport)
2999{
3000 unsigned char *buffer = cmd->sense_buffer;
3001 unsigned long flags;
3002 int offset;
3003 u8 asc = 0, ascq = 0;
3004
Andy Grovera1d8b492011-05-02 17:12:10 -07003005 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003006 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07003007 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003008 return 0;
3009 }
3010 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
Andy Grovera1d8b492011-05-02 17:12:10 -07003011 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003012
3013 if (!reason && from_transport)
3014 goto after_reason;
3015
3016 if (!from_transport)
3017 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
3018 /*
3019 * Data Segment and SenseLength of the fabric response PDU.
3020 *
3021 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
3022 * from include/scsi/scsi_cmnd.h
3023 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003024 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003025 TRANSPORT_SENSE_BUFFER);
3026 /*
3027 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
3028 * SENSE KEY values from include/scsi/scsi.h
3029 */
3030 switch (reason) {
3031 case TCM_NON_EXISTENT_LUN:
Nicholas Bellingereb39d342011-07-26 16:59:00 -07003032 /* CURRENT ERROR */
3033 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003034 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingereb39d342011-07-26 16:59:00 -07003035 /* ILLEGAL REQUEST */
3036 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
3037 /* LOGICAL UNIT NOT SUPPORTED */
3038 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
3039 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003040 case TCM_UNSUPPORTED_SCSI_OPCODE:
3041 case TCM_SECTOR_COUNT_TOO_MANY:
3042 /* CURRENT ERROR */
3043 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003044 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003045 /* ILLEGAL REQUEST */
3046 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
3047 /* INVALID COMMAND OPERATION CODE */
3048 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
3049 break;
3050 case TCM_UNKNOWN_MODE_PAGE:
3051 /* CURRENT ERROR */
3052 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003053 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003054 /* ILLEGAL REQUEST */
3055 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
3056 /* INVALID FIELD IN CDB */
3057 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
3058 break;
3059 case TCM_CHECK_CONDITION_ABORT_CMD:
3060 /* CURRENT ERROR */
3061 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003062 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003063 /* ABORTED COMMAND */
3064 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
3065 /* BUS DEVICE RESET FUNCTION OCCURRED */
3066 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
3067 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
3068 break;
3069 case TCM_INCORRECT_AMOUNT_OF_DATA:
3070 /* CURRENT ERROR */
3071 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003072 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003073 /* ABORTED COMMAND */
3074 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
3075 /* WRITE ERROR */
3076 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
3077 /* NOT ENOUGH UNSOLICITED DATA */
3078 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
3079 break;
3080 case TCM_INVALID_CDB_FIELD:
3081 /* CURRENT ERROR */
3082 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003083 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08003084 /* ILLEGAL REQUEST */
3085 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003086 /* INVALID FIELD IN CDB */
3087 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
3088 break;
3089 case TCM_INVALID_PARAMETER_LIST:
3090 /* CURRENT ERROR */
3091 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003092 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08003093 /* ILLEGAL REQUEST */
3094 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003095 /* INVALID FIELD IN PARAMETER LIST */
3096 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
3097 break;
3098 case TCM_UNEXPECTED_UNSOLICITED_DATA:
3099 /* CURRENT ERROR */
3100 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003101 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003102 /* ABORTED COMMAND */
3103 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
3104 /* WRITE ERROR */
3105 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
3106 /* UNEXPECTED_UNSOLICITED_DATA */
3107 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
3108 break;
3109 case TCM_SERVICE_CRC_ERROR:
3110 /* CURRENT ERROR */
3111 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003112 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003113 /* ABORTED COMMAND */
3114 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
3115 /* PROTOCOL SERVICE CRC ERROR */
3116 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
3117 /* N/A */
3118 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
3119 break;
3120 case TCM_SNACK_REJECTED:
3121 /* CURRENT ERROR */
3122 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003123 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003124 /* ABORTED COMMAND */
3125 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
3126 /* READ ERROR */
3127 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
3128 /* FAILED RETRANSMISSION REQUEST */
3129 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
3130 break;
3131 case TCM_WRITE_PROTECTED:
3132 /* CURRENT ERROR */
3133 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003134 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003135 /* DATA PROTECT */
3136 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
3137 /* WRITE PROTECTED */
3138 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
3139 break;
3140 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
3141 /* CURRENT ERROR */
3142 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003143 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003144 /* UNIT ATTENTION */
3145 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
3146 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
3147 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
3148 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
3149 break;
3150 case TCM_CHECK_CONDITION_NOT_READY:
3151 /* CURRENT ERROR */
3152 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003153 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003154 /* Not Ready */
3155 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
3156 transport_get_sense_codes(cmd, &asc, &ascq);
3157 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
3158 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
3159 break;
3160 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
3161 default:
3162 /* CURRENT ERROR */
3163 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003164 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003165 /* ILLEGAL REQUEST */
3166 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
3167 /* LOGICAL UNIT COMMUNICATION FAILURE */
3168 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
3169 break;
3170 }
3171 /*
3172 * This code uses linux/include/scsi/scsi.h SAM status codes!
3173 */
3174 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
3175 /*
3176 * Automatically padded, this value is encoded in the fabric's
3177 * data_length response PDU containing the SCSI defined sense data.
3178 */
3179 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
3180
3181after_reason:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003182 return cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003183}
3184EXPORT_SYMBOL(transport_send_check_condition_and_sense);
3185
3186int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
3187{
3188 int ret = 0;
3189
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003190 if (cmd->transport_state & CMD_T_ABORTED) {
Andy Grover6708bb22011-06-08 10:36:43 -07003191 if (!send_status ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003192 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
3193 return 1;
Andy Grover8b1e1242012-04-03 15:51:12 -07003194
Andy Grover6708bb22011-06-08 10:36:43 -07003195 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003196 " status for CDB: 0x%02x ITT: 0x%08x\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07003197 cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00003198 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07003199
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003200 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
Andy Grovere3d6f902011-07-19 08:55:10 +00003201 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003202 ret = 1;
3203 }
3204 return ret;
3205}
3206EXPORT_SYMBOL(transport_check_aborted_status);
3207
3208void transport_send_task_abort(struct se_cmd *cmd)
3209{
Nicholas Bellingerc252f002011-09-29 14:22:13 -07003210 unsigned long flags;
3211
3212 spin_lock_irqsave(&cmd->t_state_lock, flags);
3213 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
3214 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3215 return;
3216 }
3217 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3218
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003219 /*
3220 * If there are still expected incoming fabric WRITEs, we wait
3221 * until until they have completed before sending a TASK_ABORTED
3222 * response. This response with TASK_ABORTED status will be
3223 * queued back to fabric module by transport_check_aborted_status().
3224 */
3225 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grovere3d6f902011-07-19 08:55:10 +00003226 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003227 cmd->transport_state |= CMD_T_ABORTED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003228 smp_mb__after_atomic_inc();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003229 }
3230 }
3231 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
Andy Grover8b1e1242012-04-03 15:51:12 -07003232
Andy Grover6708bb22011-06-08 10:36:43 -07003233 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
Andy Grovera1d8b492011-05-02 17:12:10 -07003234 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00003235 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07003236
Andy Grovere3d6f902011-07-19 08:55:10 +00003237 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003238}
3239
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003240static int transport_generic_do_tmr(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003241{
Andy Grover5951146d2011-07-19 10:26:37 +00003242 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003243 struct se_tmr_req *tmr = cmd->se_tmr_req;
3244 int ret;
3245
3246 switch (tmr->function) {
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003247 case TMR_ABORT_TASK:
Nicholas Bellinger3d289342012-02-13 02:38:14 -08003248 core_tmr_abort_task(dev, tmr, cmd->se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003249 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003250 case TMR_ABORT_TASK_SET:
3251 case TMR_CLEAR_ACA:
3252 case TMR_CLEAR_TASK_SET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003253 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
3254 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003255 case TMR_LUN_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003256 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
3257 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
3258 TMR_FUNCTION_REJECTED;
3259 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003260 case TMR_TARGET_WARM_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003261 tmr->response = TMR_FUNCTION_REJECTED;
3262 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003263 case TMR_TARGET_COLD_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003264 tmr->response = TMR_FUNCTION_REJECTED;
3265 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003266 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003267 pr_err("Uknown TMR function: 0x%02x.\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003268 tmr->function);
3269 tmr->response = TMR_FUNCTION_REJECTED;
3270 break;
3271 }
3272
3273 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
Andy Grovere3d6f902011-07-19 08:55:10 +00003274 cmd->se_tfo->queue_tm_rsp(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003275
Christoph Hellwigb7b8bef2011-10-17 13:56:44 -04003276 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003277 return 0;
3278}
3279
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003280/* transport_processing_thread():
3281 *
3282 *
3283 */
3284static int transport_processing_thread(void *param)
3285{
Andy Grover5951146d2011-07-19 10:26:37 +00003286 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003287 struct se_cmd *cmd;
Jörn Engel8359cf42011-11-24 02:05:51 +01003288 struct se_device *dev = param;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003289
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003290 while (!kthread_should_stop()) {
Andy Grovere3d6f902011-07-19 08:55:10 +00003291 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
3292 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003293 kthread_should_stop());
3294 if (ret < 0)
3295 goto out;
3296
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003297get_cmd:
Andy Grover5951146d2011-07-19 10:26:37 +00003298 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
3299 if (!cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003300 continue;
3301
Andy Grover5951146d2011-07-19 10:26:37 +00003302 switch (cmd->t_state) {
Christoph Hellwig680b73c2011-09-12 21:51:14 +02003303 case TRANSPORT_NEW_CMD:
3304 BUG();
3305 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003306 case TRANSPORT_NEW_CMD_MAP:
Andy Grover6708bb22011-06-08 10:36:43 -07003307 if (!cmd->se_tfo->new_cmd_map) {
3308 pr_err("cmd->se_tfo->new_cmd_map is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003309 " NULL for TRANSPORT_NEW_CMD_MAP\n");
3310 BUG();
3311 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003312 ret = cmd->se_tfo->new_cmd_map(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003313 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003314 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003315 break;
3316 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003317 ret = transport_generic_new_cmd(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003318 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003319 transport_generic_request_failure(cmd);
3320 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003321 }
3322 break;
3323 case TRANSPORT_PROCESS_WRITE:
3324 transport_generic_process_write(cmd);
3325 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003326 case TRANSPORT_PROCESS_TMR:
3327 transport_generic_do_tmr(cmd);
3328 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003329 case TRANSPORT_COMPLETE_QF_WP:
Christoph Hellwige057f532011-10-17 13:56:41 -04003330 transport_write_pending_qf(cmd);
3331 break;
3332 case TRANSPORT_COMPLETE_QF_OK:
3333 transport_complete_qf(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003334 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003335 default:
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04003336 pr_err("Unknown t_state: %d for ITT: 0x%08x "
3337 "i_state: %d on SE LUN: %u\n",
3338 cmd->t_state,
Andy Grovere3d6f902011-07-19 08:55:10 +00003339 cmd->se_tfo->get_task_tag(cmd),
3340 cmd->se_tfo->get_cmd_state(cmd),
3341 cmd->se_lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003342 BUG();
3343 }
3344
3345 goto get_cmd;
3346 }
3347
3348out:
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003349 WARN_ON(!list_empty(&dev->state_list));
Nicholas Bellingerce8762f2011-10-09 02:19:01 -07003350 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003351 dev->process_thread = NULL;
3352 return 0;
3353}