blob: 1c53fcec11338b9f432dacd699bb76dc6e50770e [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
470/* transport_cmd_check_stop():
471 *
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500472 * 'transport_off = 1' determines if CMD_T_ACTIVE should be cleared.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800473 * 'transport_off = 2' determines if task_dev_state should be removed.
474 *
475 * A non-zero u8 t_state sets cmd->t_state.
476 * Returns 1 when command is stopped, else 0.
477 */
478static int transport_cmd_check_stop(
479 struct se_cmd *cmd,
480 int transport_off,
481 u8 t_state)
482{
483 unsigned long flags;
484
Andy Grovera1d8b492011-05-02 17:12:10 -0700485 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800486 /*
487 * Determine if IOCTL context caller in requesting the stopping of this
488 * command for LUN shutdown purposes.
489 */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500490 if (cmd->transport_state & CMD_T_LUN_STOP) {
491 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
492 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800493
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500494 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800495 if (transport_off == 2)
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400496 target_remove_from_state_list(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700497 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800498
Andy Grovera1d8b492011-05-02 17:12:10 -0700499 complete(&cmd->transport_lun_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500 return 1;
501 }
502 /*
503 * Determine if frontend context caller is requesting the stopping of
Andy Grovere3d6f902011-07-19 08:55:10 +0000504 * this command for frontend exceptions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800505 */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500506 if (cmd->transport_state & CMD_T_STOP) {
507 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
508 __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000509 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800510
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800511 if (transport_off == 2)
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400512 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800513
514 /*
515 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
516 * to FE.
517 */
518 if (transport_off == 2)
519 cmd->se_lun = NULL;
Andy Grovera1d8b492011-05-02 17:12:10 -0700520 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800521
Andy Grovera1d8b492011-05-02 17:12:10 -0700522 complete(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800523 return 1;
524 }
525 if (transport_off) {
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500526 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800527 if (transport_off == 2) {
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400528 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800529 /*
530 * Clear struct se_cmd->se_lun before the transport_off == 2
531 * handoff to fabric module.
532 */
533 cmd->se_lun = NULL;
534 /*
535 * Some fabric modules like tcm_loop can release
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300536 * their internally allocated I/O reference now and
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800537 * struct se_cmd now.
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700538 *
539 * Fabric modules are expected to return '1' here if the
540 * se_cmd being passed is released at this point,
541 * or zero if not being released.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800542 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000543 if (cmd->se_tfo->check_stop_free != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800544 spin_unlock_irqrestore(
Andy Grovera1d8b492011-05-02 17:12:10 -0700545 &cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800546
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700547 return cmd->se_tfo->check_stop_free(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800548 }
549 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700550 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800551
552 return 0;
553 } else if (t_state)
554 cmd->t_state = t_state;
Andy Grovera1d8b492011-05-02 17:12:10 -0700555 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800556
557 return 0;
558}
559
560static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
561{
562 return transport_cmd_check_stop(cmd, 2, 0);
563}
564
565static void transport_lun_remove_cmd(struct se_cmd *cmd)
566{
Andy Grovere3d6f902011-07-19 08:55:10 +0000567 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800568 unsigned long flags;
569
570 if (!lun)
571 return;
572
Andy Grovera1d8b492011-05-02 17:12:10 -0700573 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500574 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
575 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400576 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800577 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700578 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800579
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800580 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -0500581 if (!list_empty(&cmd->se_lun_node))
582 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800583 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
584}
585
586void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
587{
Andy Groverc8e31f22012-01-19 13:39:17 -0800588 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellinger8dc52b52011-10-09 02:02:51 -0700589 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800590
591 if (transport_cmd_check_stop_to_fabric(cmd))
592 return;
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700593 if (remove) {
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400594 transport_remove_cmd_from_queue(cmd);
Christoph Hellwige6a25732011-09-13 23:08:50 +0200595 transport_put_cmd(cmd);
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700596 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800597}
598
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400599static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
600 bool at_head)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800601{
602 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +0000603 struct se_queue_obj *qobj = &dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800604 unsigned long flags;
605
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800606 if (t_state) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700607 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800608 cmd->t_state = t_state;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500609 cmd->transport_state |= CMD_T_ACTIVE;
Andy Grovera1d8b492011-05-02 17:12:10 -0700610 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800611 }
612
613 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Roland Dreier79a7fef2011-09-28 22:12:07 -0700614
615 /* If the cmd is already on the list, remove it before we add it */
616 if (!list_empty(&cmd->se_queue_node))
617 list_del(&cmd->se_queue_node);
618 else
619 atomic_inc(&qobj->queue_cnt);
620
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400621 if (at_head)
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700622 list_add(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400623 else
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700624 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500625 cmd->transport_state |= CMD_T_QUEUED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800626 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
627
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800628 wake_up_interruptible(&qobj->thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800629}
630
Andy Grover5951146d2011-07-19 10:26:37 +0000631static struct se_cmd *
632transport_get_cmd_from_queue(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800633{
Andy Grover5951146d2011-07-19 10:26:37 +0000634 struct se_cmd *cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800635 unsigned long flags;
636
637 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
638 if (list_empty(&qobj->qobj_list)) {
639 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
640 return NULL;
641 }
Andy Grover5951146d2011-07-19 10:26:37 +0000642 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800643
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500644 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700645 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800646 atomic_dec(&qobj->queue_cnt);
647 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
648
Andy Grover5951146d2011-07-19 10:26:37 +0000649 return cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800650}
651
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400652static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800653{
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400654 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800655 unsigned long flags;
656
657 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500658 if (!(cmd->transport_state & CMD_T_QUEUED)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800659 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
660 return;
661 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500662 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700663 atomic_dec(&qobj->queue_cnt);
664 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800665 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800666}
667
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400668static void target_complete_failure_work(struct work_struct *work)
669{
670 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
671
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700672 transport_generic_request_failure(cmd);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400673}
674
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400675void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800676{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400677 struct se_device *dev = cmd->se_dev;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400678 int success = scsi_status == GOOD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800679 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800680
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400681 cmd->scsi_status = scsi_status;
682
683
Andy Grovera1d8b492011-05-02 17:12:10 -0700684 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400685 cmd->transport_state &= ~CMD_T_BUSY;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800686
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800687 if (dev && dev->transport->transport_complete) {
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400688 if (dev->transport->transport_complete(cmd,
689 cmd->t_data_sg) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800690 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800691 success = 1;
692 }
693 }
694
695 /*
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400696 * See if we are waiting to complete for an exception condition.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800697 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400698 if (cmd->transport_state & CMD_T_REQUEST_STOP) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700699 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400700 complete(&cmd->task_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800701 return;
702 }
Christoph Hellwig2235007c2011-11-02 05:06:35 -0700703
704 if (!success)
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500705 cmd->transport_state |= CMD_T_FAILED;
Christoph Hellwig2235007c2011-11-02 05:06:35 -0700706
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800707 /*
708 * Check for case where an explict ABORT_TASK has been received
709 * and transport_wait_for_tasks() will be waiting for completion..
710 */
711 if (cmd->transport_state & CMD_T_ABORTED &&
712 cmd->transport_state & CMD_T_STOP) {
713 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
714 complete(&cmd->t_transport_stop_comp);
715 return;
716 } else if (cmd->transport_state & CMD_T_FAILED) {
Christoph Hellwig41e16e92011-11-23 06:54:15 -0500717 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400718 INIT_WORK(&cmd->work, target_complete_failure_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800719 } else {
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400720 INIT_WORK(&cmd->work, target_complete_ok_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800721 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400722
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400723 cmd->t_state = TRANSPORT_COMPLETE;
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800724 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
Andy Grovera1d8b492011-05-02 17:12:10 -0700725 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800726
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400727 queue_work(target_completion_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800728}
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400729EXPORT_SYMBOL(target_complete_cmd);
730
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400731static void target_add_to_state_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800732{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400733 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800734 unsigned long flags;
735
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800736 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400737 if (!cmd->state_active) {
738 list_add_tail(&cmd->state_list, &dev->state_list);
739 cmd->state_active = true;
740 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800741 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800742}
743
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700744/*
Nicholas Bellingerf147abb2011-10-25 23:57:41 -0700745 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700746 */
747
748static void target_qf_do_work(struct work_struct *work)
749{
750 struct se_device *dev = container_of(work, struct se_device,
751 qf_work_queue);
Roland Dreierbcac3642011-08-27 21:33:16 -0700752 LIST_HEAD(qf_cmd_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700753 struct se_cmd *cmd, *cmd_tmp;
754
755 spin_lock_irq(&dev->qf_cmd_lock);
Roland Dreierbcac3642011-08-27 21:33:16 -0700756 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
757 spin_unlock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700758
Roland Dreierbcac3642011-08-27 21:33:16 -0700759 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700760 list_del(&cmd->se_qf_node);
761 atomic_dec(&dev->dev_qf_count);
762 smp_mb__after_atomic_dec();
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700763
Andy Grover6708bb22011-06-08 10:36:43 -0700764 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700765 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -0400766 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700767 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
768 : "UNKNOWN");
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400769
770 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700771 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700772}
773
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800774unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
775{
776 switch (cmd->data_direction) {
777 case DMA_NONE:
778 return "NONE";
779 case DMA_FROM_DEVICE:
780 return "READ";
781 case DMA_TO_DEVICE:
782 return "WRITE";
783 case DMA_BIDIRECTIONAL:
784 return "BIDI";
785 default:
786 break;
787 }
788
789 return "UNKNOWN";
790}
791
792void transport_dump_dev_state(
793 struct se_device *dev,
794 char *b,
795 int *bl)
796{
797 *bl += sprintf(b + *bl, "Status: ");
798 switch (dev->dev_status) {
799 case TRANSPORT_DEVICE_ACTIVATED:
800 *bl += sprintf(b + *bl, "ACTIVATED");
801 break;
802 case TRANSPORT_DEVICE_DEACTIVATED:
803 *bl += sprintf(b + *bl, "DEACTIVATED");
804 break;
805 case TRANSPORT_DEVICE_SHUTDOWN:
806 *bl += sprintf(b + *bl, "SHUTDOWN");
807 break;
808 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
809 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
810 *bl += sprintf(b + *bl, "OFFLINE");
811 break;
812 default:
813 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
814 break;
815 }
816
Christoph Hellwig5f41a312012-05-20 14:34:44 -0400817 *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth);
Nicholas Bellinger11e764b2012-05-09 12:42:09 -0700818 *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n",
819 dev->se_sub_dev->se_dev_attrib.block_size,
820 dev->se_sub_dev->se_dev_attrib.hw_max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800821 *bl += sprintf(b + *bl, " ");
822}
823
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800824void transport_dump_vpd_proto_id(
825 struct t10_vpd *vpd,
826 unsigned char *p_buf,
827 int p_buf_len)
828{
829 unsigned char buf[VPD_TMP_BUF_SIZE];
830 int len;
831
832 memset(buf, 0, VPD_TMP_BUF_SIZE);
833 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
834
835 switch (vpd->protocol_identifier) {
836 case 0x00:
837 sprintf(buf+len, "Fibre Channel\n");
838 break;
839 case 0x10:
840 sprintf(buf+len, "Parallel SCSI\n");
841 break;
842 case 0x20:
843 sprintf(buf+len, "SSA\n");
844 break;
845 case 0x30:
846 sprintf(buf+len, "IEEE 1394\n");
847 break;
848 case 0x40:
849 sprintf(buf+len, "SCSI Remote Direct Memory Access"
850 " Protocol\n");
851 break;
852 case 0x50:
853 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
854 break;
855 case 0x60:
856 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
857 break;
858 case 0x70:
859 sprintf(buf+len, "Automation/Drive Interface Transport"
860 " Protocol\n");
861 break;
862 case 0x80:
863 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
864 break;
865 default:
866 sprintf(buf+len, "Unknown 0x%02x\n",
867 vpd->protocol_identifier);
868 break;
869 }
870
871 if (p_buf)
872 strncpy(p_buf, buf, p_buf_len);
873 else
Andy Grover6708bb22011-06-08 10:36:43 -0700874 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800875}
876
877void
878transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
879{
880 /*
881 * Check if the Protocol Identifier Valid (PIV) bit is set..
882 *
883 * from spc3r23.pdf section 7.5.1
884 */
885 if (page_83[1] & 0x80) {
886 vpd->protocol_identifier = (page_83[0] & 0xf0);
887 vpd->protocol_identifier_set = 1;
888 transport_dump_vpd_proto_id(vpd, NULL, 0);
889 }
890}
891EXPORT_SYMBOL(transport_set_vpd_proto_id);
892
893int transport_dump_vpd_assoc(
894 struct t10_vpd *vpd,
895 unsigned char *p_buf,
896 int p_buf_len)
897{
898 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +0000899 int ret = 0;
900 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800901
902 memset(buf, 0, VPD_TMP_BUF_SIZE);
903 len = sprintf(buf, "T10 VPD Identifier Association: ");
904
905 switch (vpd->association) {
906 case 0x00:
907 sprintf(buf+len, "addressed logical unit\n");
908 break;
909 case 0x10:
910 sprintf(buf+len, "target port\n");
911 break;
912 case 0x20:
913 sprintf(buf+len, "SCSI target device\n");
914 break;
915 default:
916 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
Andy Grovere3d6f902011-07-19 08:55:10 +0000917 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800918 break;
919 }
920
921 if (p_buf)
922 strncpy(p_buf, buf, p_buf_len);
923 else
Andy Grover6708bb22011-06-08 10:36:43 -0700924 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800925
926 return ret;
927}
928
929int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
930{
931 /*
932 * The VPD identification association..
933 *
934 * from spc3r23.pdf Section 7.6.3.1 Table 297
935 */
936 vpd->association = (page_83[1] & 0x30);
937 return transport_dump_vpd_assoc(vpd, NULL, 0);
938}
939EXPORT_SYMBOL(transport_set_vpd_assoc);
940
941int transport_dump_vpd_ident_type(
942 struct t10_vpd *vpd,
943 unsigned char *p_buf,
944 int p_buf_len)
945{
946 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +0000947 int ret = 0;
948 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800949
950 memset(buf, 0, VPD_TMP_BUF_SIZE);
951 len = sprintf(buf, "T10 VPD Identifier Type: ");
952
953 switch (vpd->device_identifier_type) {
954 case 0x00:
955 sprintf(buf+len, "Vendor specific\n");
956 break;
957 case 0x01:
958 sprintf(buf+len, "T10 Vendor ID based\n");
959 break;
960 case 0x02:
961 sprintf(buf+len, "EUI-64 based\n");
962 break;
963 case 0x03:
964 sprintf(buf+len, "NAA\n");
965 break;
966 case 0x04:
967 sprintf(buf+len, "Relative target port identifier\n");
968 break;
969 case 0x08:
970 sprintf(buf+len, "SCSI name string\n");
971 break;
972 default:
973 sprintf(buf+len, "Unsupported: 0x%02x\n",
974 vpd->device_identifier_type);
Andy Grovere3d6f902011-07-19 08:55:10 +0000975 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800976 break;
977 }
978
Andy Grovere3d6f902011-07-19 08:55:10 +0000979 if (p_buf) {
980 if (p_buf_len < strlen(buf)+1)
981 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800982 strncpy(p_buf, buf, p_buf_len);
Andy Grovere3d6f902011-07-19 08:55:10 +0000983 } else {
Andy Grover6708bb22011-06-08 10:36:43 -0700984 pr_debug("%s", buf);
Andy Grovere3d6f902011-07-19 08:55:10 +0000985 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800986
987 return ret;
988}
989
990int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
991{
992 /*
993 * The VPD identifier type..
994 *
995 * from spc3r23.pdf Section 7.6.3.1 Table 298
996 */
997 vpd->device_identifier_type = (page_83[1] & 0x0f);
998 return transport_dump_vpd_ident_type(vpd, NULL, 0);
999}
1000EXPORT_SYMBOL(transport_set_vpd_ident_type);
1001
1002int transport_dump_vpd_ident(
1003 struct t10_vpd *vpd,
1004 unsigned char *p_buf,
1005 int p_buf_len)
1006{
1007 unsigned char buf[VPD_TMP_BUF_SIZE];
1008 int ret = 0;
1009
1010 memset(buf, 0, VPD_TMP_BUF_SIZE);
1011
1012 switch (vpd->device_identifier_code_set) {
1013 case 0x01: /* Binary */
1014 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1015 &vpd->device_identifier[0]);
1016 break;
1017 case 0x02: /* ASCII */
1018 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1019 &vpd->device_identifier[0]);
1020 break;
1021 case 0x03: /* UTF-8 */
1022 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1023 &vpd->device_identifier[0]);
1024 break;
1025 default:
1026 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1027 " 0x%02x", vpd->device_identifier_code_set);
Andy Grovere3d6f902011-07-19 08:55:10 +00001028 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001029 break;
1030 }
1031
1032 if (p_buf)
1033 strncpy(p_buf, buf, p_buf_len);
1034 else
Andy Grover6708bb22011-06-08 10:36:43 -07001035 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001036
1037 return ret;
1038}
1039
1040int
1041transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1042{
1043 static const char hex_str[] = "0123456789abcdef";
1044 int j = 0, i = 4; /* offset to start of the identifer */
1045
1046 /*
1047 * The VPD Code Set (encoding)
1048 *
1049 * from spc3r23.pdf Section 7.6.3.1 Table 296
1050 */
1051 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1052 switch (vpd->device_identifier_code_set) {
1053 case 0x01: /* Binary */
1054 vpd->device_identifier[j++] =
1055 hex_str[vpd->device_identifier_type];
1056 while (i < (4 + page_83[3])) {
1057 vpd->device_identifier[j++] =
1058 hex_str[(page_83[i] & 0xf0) >> 4];
1059 vpd->device_identifier[j++] =
1060 hex_str[page_83[i] & 0x0f];
1061 i++;
1062 }
1063 break;
1064 case 0x02: /* ASCII */
1065 case 0x03: /* UTF-8 */
1066 while (i < (4 + page_83[3]))
1067 vpd->device_identifier[j++] = page_83[i++];
1068 break;
1069 default:
1070 break;
1071 }
1072
1073 return transport_dump_vpd_ident(vpd, NULL, 0);
1074}
1075EXPORT_SYMBOL(transport_set_vpd_ident);
1076
1077static void core_setup_task_attr_emulation(struct se_device *dev)
1078{
1079 /*
1080 * If this device is from Target_Core_Mod/pSCSI, disable the
1081 * SAM Task Attribute emulation.
1082 *
1083 * This is currently not available in upsream Linux/SCSI Target
1084 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1085 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001086 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001087 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1088 return;
1089 }
1090
1091 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
Andy Grover6708bb22011-06-08 10:36:43 -07001092 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001093 " device\n", dev->transport->name,
1094 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001095}
1096
1097static void scsi_dump_inquiry(struct se_device *dev)
1098{
Andy Grovere3d6f902011-07-19 08:55:10 +00001099 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001100 char buf[17];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001101 int i, device_type;
1102 /*
1103 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1104 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001105 for (i = 0; i < 8; i++)
1106 if (wwn->vendor[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001107 buf[i] = wwn->vendor[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001108 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001109 buf[i] = ' ';
1110 buf[i] = '\0';
1111 pr_debug(" Vendor: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001112
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001113 for (i = 0; i < 16; i++)
1114 if (wwn->model[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001115 buf[i] = wwn->model[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001116 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001117 buf[i] = ' ';
1118 buf[i] = '\0';
1119 pr_debug(" Model: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001120
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001121 for (i = 0; i < 4; i++)
1122 if (wwn->revision[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001123 buf[i] = wwn->revision[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001124 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001125 buf[i] = ' ';
1126 buf[i] = '\0';
1127 pr_debug(" Revision: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001128
Andy Grovere3d6f902011-07-19 08:55:10 +00001129 device_type = dev->transport->get_device_type(dev);
Andy Grover6708bb22011-06-08 10:36:43 -07001130 pr_debug(" Type: %s ", scsi_device_type(device_type));
1131 pr_debug(" ANSI SCSI revision: %02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001132 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001133}
1134
1135struct se_device *transport_add_device_to_core_hba(
1136 struct se_hba *hba,
1137 struct se_subsystem_api *transport,
1138 struct se_subsystem_dev *se_dev,
1139 u32 device_flags,
1140 void *transport_dev,
1141 struct se_dev_limits *dev_limits,
1142 const char *inquiry_prod,
1143 const char *inquiry_rev)
1144{
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001145 int force_pt;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001146 struct se_device *dev;
1147
1148 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001149 if (!dev) {
1150 pr_err("Unable to allocate memory for se_dev_t\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001151 return NULL;
1152 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001153
Andy Grovere3d6f902011-07-19 08:55:10 +00001154 transport_init_queue_obj(&dev->dev_queue_obj);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001155 dev->dev_flags = device_flags;
1156 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
Andy Grover5951146d2011-07-19 10:26:37 +00001157 dev->dev_ptr = transport_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001158 dev->se_hba = hba;
1159 dev->se_sub_dev = se_dev;
1160 dev->transport = transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001161 INIT_LIST_HEAD(&dev->dev_list);
1162 INIT_LIST_HEAD(&dev->dev_sep_list);
1163 INIT_LIST_HEAD(&dev->dev_tmr_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001164 INIT_LIST_HEAD(&dev->delayed_cmd_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001165 INIT_LIST_HEAD(&dev->state_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001166 INIT_LIST_HEAD(&dev->qf_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001167 spin_lock_init(&dev->execute_task_lock);
1168 spin_lock_init(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001169 spin_lock_init(&dev->dev_reservation_lock);
1170 spin_lock_init(&dev->dev_status_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001171 spin_lock_init(&dev->se_port_lock);
1172 spin_lock_init(&dev->se_tmr_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001173 spin_lock_init(&dev->qf_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001174 atomic_set(&dev->dev_ordered_id, 0);
1175
1176 se_dev_set_default_attribs(dev, dev_limits);
1177
1178 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1179 dev->creation_time = get_jiffies_64();
1180 spin_lock_init(&dev->stats_lock);
1181
1182 spin_lock(&hba->device_lock);
1183 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1184 hba->dev_count++;
1185 spin_unlock(&hba->device_lock);
1186 /*
1187 * Setup the SAM Task Attribute emulation for struct se_device
1188 */
1189 core_setup_task_attr_emulation(dev);
1190 /*
1191 * Force PR and ALUA passthrough emulation with internal object use.
1192 */
1193 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1194 /*
1195 * Setup the Reservations infrastructure for struct se_device
1196 */
1197 core_setup_reservations(dev, force_pt);
1198 /*
1199 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1200 */
1201 if (core_setup_alua(dev, force_pt) < 0)
1202 goto out;
1203
1204 /*
1205 * Startup the struct se_device processing thread
1206 */
1207 dev->process_thread = kthread_run(transport_processing_thread, dev,
Andy Grovere3d6f902011-07-19 08:55:10 +00001208 "LIO_%s", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001209 if (IS_ERR(dev->process_thread)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001210 pr_err("Unable to create kthread: LIO_%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001211 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001212 goto out;
1213 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001214 /*
1215 * Setup work_queue for QUEUE_FULL
1216 */
1217 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001218 /*
1219 * Preload the initial INQUIRY const values if we are doing
1220 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1221 * passthrough because this is being provided by the backend LLD.
1222 * This is required so that transport_get_inquiry() copies these
1223 * originals once back into DEV_T10_WWN(dev) for the virtual device
1224 * setup.
1225 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001226 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
Roland Dreierf22c1192011-05-02 22:15:37 -07001227 if (!inquiry_prod || !inquiry_rev) {
Andy Grover6708bb22011-06-08 10:36:43 -07001228 pr_err("All non TCM/pSCSI plugins require"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001229 " INQUIRY consts\n");
1230 goto out;
1231 }
1232
Andy Grovere3d6f902011-07-19 08:55:10 +00001233 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1234 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1235 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001236 }
1237 scsi_dump_inquiry(dev);
1238
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001239 return dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001240out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001241 kthread_stop(dev->process_thread);
1242
1243 spin_lock(&hba->device_lock);
1244 list_del(&dev->dev_list);
1245 hba->dev_count--;
1246 spin_unlock(&hba->device_lock);
1247
1248 se_release_vpd_for_dev(dev);
1249
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001250 kfree(dev);
1251
1252 return NULL;
1253}
1254EXPORT_SYMBOL(transport_add_device_to_core_hba);
1255
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001256int target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
Christoph Hellwig9b3b8042012-05-20 11:59:12 -04001257{
1258 struct se_device *dev = cmd->se_dev;
1259
1260 if (cmd->unknown_data_length) {
1261 cmd->data_length = size;
1262 } else if (size != cmd->data_length) {
1263 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
1264 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
1265 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
1266 cmd->data_length, size, cmd->t_task_cdb[0]);
1267
1268 cmd->cmd_spdtl = size;
1269
1270 if (cmd->data_direction == DMA_TO_DEVICE) {
1271 pr_err("Rejecting underflow/overflow"
1272 " WRITE data\n");
1273 goto out_invalid_cdb_field;
1274 }
1275 /*
1276 * Reject READ_* or WRITE_* with overflow/underflow for
1277 * type SCF_SCSI_DATA_CDB.
1278 */
1279 if (dev->se_sub_dev->se_dev_attrib.block_size != 512) {
1280 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
1281 " CDB on non 512-byte sector setup subsystem"
1282 " plugin: %s\n", dev->transport->name);
1283 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
1284 goto out_invalid_cdb_field;
1285 }
1286
1287 if (size > cmd->data_length) {
1288 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
1289 cmd->residual_count = (size - cmd->data_length);
1290 } else {
1291 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
1292 cmd->residual_count = (cmd->data_length - size);
1293 }
1294 cmd->data_length = size;
1295 }
1296
1297 return 0;
1298
1299out_invalid_cdb_field:
1300 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1301 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1302 return -EINVAL;
1303}
1304
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001305/*
1306 * Used by fabric modules containing a local struct se_cmd within their
1307 * fabric dependent per I/O descriptor.
1308 */
1309void transport_init_se_cmd(
1310 struct se_cmd *cmd,
1311 struct target_core_fabric_ops *tfo,
1312 struct se_session *se_sess,
1313 u32 data_length,
1314 int data_direction,
1315 int task_attr,
1316 unsigned char *sense_buffer)
1317{
Andy Grover5951146d2011-07-19 10:26:37 +00001318 INIT_LIST_HEAD(&cmd->se_lun_node);
1319 INIT_LIST_HEAD(&cmd->se_delayed_node);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001320 INIT_LIST_HEAD(&cmd->se_qf_node);
Roland Dreier79a7fef2011-09-28 22:12:07 -07001321 INIT_LIST_HEAD(&cmd->se_queue_node);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001322 INIT_LIST_HEAD(&cmd->se_cmd_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001323 INIT_LIST_HEAD(&cmd->state_list);
Andy Grovera1d8b492011-05-02 17:12:10 -07001324 init_completion(&cmd->transport_lun_fe_stop_comp);
1325 init_completion(&cmd->transport_lun_stop_comp);
1326 init_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001327 init_completion(&cmd->cmd_wait_comp);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001328 init_completion(&cmd->task_stop_comp);
Andy Grovera1d8b492011-05-02 17:12:10 -07001329 spin_lock_init(&cmd->t_state_lock);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001330 cmd->transport_state = CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001331
1332 cmd->se_tfo = tfo;
1333 cmd->se_sess = se_sess;
1334 cmd->data_length = data_length;
1335 cmd->data_direction = data_direction;
1336 cmd->sam_task_attr = task_attr;
1337 cmd->sense_buffer = sense_buffer;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001338
1339 cmd->state_active = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001340}
1341EXPORT_SYMBOL(transport_init_se_cmd);
1342
1343static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1344{
1345 /*
1346 * Check if SAM Task Attribute emulation is enabled for this
1347 * struct se_device storage object
1348 */
Andy Grover5951146d2011-07-19 10:26:37 +00001349 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001350 return 0;
1351
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001352 if (cmd->sam_task_attr == MSG_ACA_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001353 pr_debug("SAM Task Attribute ACA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001354 " emulation is not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001355 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001356 }
1357 /*
1358 * Used to determine when ORDERED commands should go from
1359 * Dormant to Active status.
1360 */
Andy Grover5951146d2011-07-19 10:26:37 +00001361 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001362 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001363 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001364 cmd->se_ordered_id, cmd->sam_task_attr,
Andy Grover6708bb22011-06-08 10:36:43 -07001365 cmd->se_dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001366 return 0;
1367}
1368
Andy Grovera12f41f2012-04-03 15:51:20 -07001369/* target_setup_cmd_from_cdb():
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001370 *
1371 * Called from fabric RX Thread.
1372 */
Andy Grovera12f41f2012-04-03 15:51:20 -07001373int target_setup_cmd_from_cdb(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001374 struct se_cmd *cmd,
1375 unsigned char *cdb)
1376{
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001377 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
1378 u32 pr_reg_type = 0;
1379 u8 alua_ascq = 0;
1380 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001381 int ret;
1382
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001383 /*
1384 * Ensure that the received CDB is less than the max (252 + 8) bytes
1385 * for VARIABLE_LENGTH_CMD
1386 */
1387 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001388 pr_err("Received SCSI CDB with command_size: %d that"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001389 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1390 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001391 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1392 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grovere3d6f902011-07-19 08:55:10 +00001393 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001394 }
1395 /*
1396 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1397 * allocate the additional extended CDB buffer now.. Otherwise
1398 * setup the pointer from __t_task_cdb to t_task_cdb.
1399 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001400 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1401 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001402 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001403 if (!cmd->t_task_cdb) {
1404 pr_err("Unable to allocate cmd->t_task_cdb"
Andy Grovera1d8b492011-05-02 17:12:10 -07001405 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001406 scsi_command_size(cdb),
Andy Grovera1d8b492011-05-02 17:12:10 -07001407 (unsigned long)sizeof(cmd->__t_task_cdb));
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001408 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1409 cmd->scsi_sense_reason =
1410 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grovere3d6f902011-07-19 08:55:10 +00001411 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001412 }
1413 } else
Andy Grovera1d8b492011-05-02 17:12:10 -07001414 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001415 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07001416 * Copy the original CDB into cmd->
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001417 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001418 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001419
1420 /*
1421 * Check for an existing UNIT ATTENTION condition
1422 */
1423 if (core_scsi3_ua_check(cmd, cdb) < 0) {
1424 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1425 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
1426 return -EINVAL;
1427 }
1428
1429 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
1430 if (ret != 0) {
1431 /*
1432 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
1433 * The ALUA additional sense code qualifier (ASCQ) is determined
1434 * by the ALUA primary or secondary access state..
1435 */
1436 if (ret > 0) {
1437 pr_debug("[%s]: ALUA TG Port not available, "
1438 "SenseKey: NOT_READY, ASC/ASCQ: "
1439 "0x04/0x%02x\n",
1440 cmd->se_tfo->get_fabric_name(), alua_ascq);
1441
1442 transport_set_sense_codes(cmd, 0x04, alua_ascq);
1443 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1444 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
1445 return -EINVAL;
1446 }
1447 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1448 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1449 return -EINVAL;
1450 }
1451
1452 /*
1453 * Check status for SPC-3 Persistent Reservations
1454 */
1455 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type)) {
1456 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
1457 cmd, cdb, pr_reg_type) != 0) {
1458 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1459 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
1460 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1461 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
1462 return -EBUSY;
1463 }
1464 /*
1465 * This means the CDB is allowed for the SCSI Initiator port
1466 * when said port is *NOT* holding the legacy SPC-2 or
1467 * SPC-3 Persistent Reservation.
1468 */
1469 }
1470
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001471 ret = cmd->se_dev->transport->parse_cdb(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001472 if (ret < 0)
1473 return ret;
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001474
1475 spin_lock_irqsave(&cmd->t_state_lock, flags);
1476 cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1477 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1478
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001479 /*
1480 * Check for SAM Task Attribute Emulation
1481 */
1482 if (transport_check_alloc_task_attr(cmd) < 0) {
1483 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1484 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00001485 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001486 }
1487 spin_lock(&cmd->se_lun->lun_sep_lock);
1488 if (cmd->se_lun->lun_sep)
1489 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1490 spin_unlock(&cmd->se_lun->lun_sep_lock);
1491 return 0;
1492}
Andy Grovera12f41f2012-04-03 15:51:20 -07001493EXPORT_SYMBOL(target_setup_cmd_from_cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001494
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001495/*
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001496 * Used by fabric module frontends to queue tasks directly.
1497 * Many only be used from process context only
1498 */
1499int transport_handle_cdb_direct(
1500 struct se_cmd *cmd)
1501{
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001502 int ret;
1503
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001504 if (!cmd->se_lun) {
1505 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001506 pr_err("cmd->se_lun is NULL\n");
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001507 return -EINVAL;
1508 }
1509 if (in_interrupt()) {
1510 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001511 pr_err("transport_generic_handle_cdb cannot be called"
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001512 " from interrupt context\n");
1513 return -EINVAL;
1514 }
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001515 /*
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001516 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001517 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1518 * in existing usage to ensure that outstanding descriptors are handled
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07001519 * correctly during shutdown via transport_wait_for_tasks()
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001520 *
1521 * Also, we don't take cmd->t_state_lock here as we only expect
1522 * this to be called for initial descriptor submission.
1523 */
1524 cmd->t_state = TRANSPORT_NEW_CMD;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001525 cmd->transport_state |= CMD_T_ACTIVE;
1526
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001527 /*
1528 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1529 * so follow TRANSPORT_NEW_CMD processing thread context usage
1530 * and call transport_generic_request_failure() if necessary..
1531 */
1532 ret = transport_generic_new_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001533 if (ret < 0)
1534 transport_generic_request_failure(cmd);
1535
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001536 return 0;
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001537}
1538EXPORT_SYMBOL(transport_handle_cdb_direct);
1539
Nicholas Bellingera6360782011-11-18 20:36:22 -08001540/**
1541 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1542 *
1543 * @se_cmd: command descriptor to submit
1544 * @se_sess: associated se_sess for endpoint
1545 * @cdb: pointer to SCSI CDB
1546 * @sense: pointer to SCSI sense buffer
1547 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1548 * @data_length: fabric expected data transfer length
1549 * @task_addr: SAM task attribute
1550 * @data_dir: DMA data direction
1551 * @flags: flags for command submission from target_sc_flags_tables
1552 *
1553 * This may only be called from process context, and also currently
1554 * assumes internal allocation of fabric payload buffer by target-core.
1555 **/
Andy Grover1edcdb42012-01-19 13:39:23 -08001556void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
Nicholas Bellingera6360782011-11-18 20:36:22 -08001557 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1558 u32 data_length, int task_attr, int data_dir, int flags)
1559{
1560 struct se_portal_group *se_tpg;
1561 int rc;
1562
1563 se_tpg = se_sess->se_tpg;
1564 BUG_ON(!se_tpg);
1565 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1566 BUG_ON(in_interrupt());
1567 /*
1568 * Initialize se_cmd for target operation. From this point
1569 * exceptions are handled by sending exception status via
1570 * target_core_fabric_ops->queue_status() callback
1571 */
1572 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1573 data_length, data_dir, task_attr, sense);
Sebastian Andrzej Siewiorb0d79942012-01-10 14:16:59 +01001574 if (flags & TARGET_SCF_UNKNOWN_SIZE)
1575 se_cmd->unknown_data_length = 1;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001576 /*
1577 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1578 * se_sess->sess_cmd_list. A second kref_get here is necessary
1579 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1580 * kref_put() to happen during fabric packet acknowledgement.
1581 */
1582 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1583 /*
1584 * Signal bidirectional data payloads to target-core
1585 */
1586 if (flags & TARGET_SCF_BIDI_OP)
1587 se_cmd->se_cmd_flags |= SCF_BIDI;
1588 /*
1589 * Locate se_lun pointer and attach it to struct se_cmd
1590 */
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001591 if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1592 transport_send_check_condition_and_sense(se_cmd,
1593 se_cmd->scsi_sense_reason, 0);
1594 target_put_sess_cmd(se_sess, se_cmd);
1595 return;
1596 }
Christoph Hellwigd6e01752012-05-20 11:59:14 -04001597
Andy Grovera12f41f2012-04-03 15:51:20 -07001598 rc = target_setup_cmd_from_cdb(se_cmd, cdb);
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001599 if (rc != 0) {
1600 transport_generic_request_failure(se_cmd);
1601 return;
1602 }
Andy Grover11e319e2012-04-03 15:51:28 -07001603
1604 /*
1605 * Check if we need to delay processing because of ALUA
1606 * Active/NonOptimized primary access state..
1607 */
1608 core_alua_check_nonop_delay(se_cmd);
1609
Nicholas Bellingera6360782011-11-18 20:36:22 -08001610 /*
1611 * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
1612 * for immediate execution of READs, otherwise wait for
1613 * transport_generic_handle_data() to be called for WRITEs
1614 * when fabric has filled the incoming buffer.
1615 */
1616 transport_handle_cdb_direct(se_cmd);
Andy Grover1edcdb42012-01-19 13:39:23 -08001617 return;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001618}
1619EXPORT_SYMBOL(target_submit_cmd);
1620
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001621static void target_complete_tmr_failure(struct work_struct *work)
1622{
1623 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
1624
1625 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1626 se_cmd->se_tfo->queue_tm_rsp(se_cmd);
1627 transport_generic_free_cmd(se_cmd, 0);
1628}
1629
Andy Groverea98d7f2012-01-19 13:39:21 -08001630/**
1631 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1632 * for TMR CDBs
1633 *
1634 * @se_cmd: command descriptor to submit
1635 * @se_sess: associated se_sess for endpoint
1636 * @sense: pointer to SCSI sense buffer
1637 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1638 * @fabric_context: fabric context for TMR req
1639 * @tm_type: Type of TM request
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001640 * @gfp: gfp type for caller
1641 * @tag: referenced task tag for TMR_ABORT_TASK
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001642 * @flags: submit cmd flags
Andy Groverea98d7f2012-01-19 13:39:21 -08001643 *
1644 * Callable from all contexts.
1645 **/
1646
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001647int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
Andy Groverea98d7f2012-01-19 13:39:21 -08001648 unsigned char *sense, u32 unpacked_lun,
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001649 void *fabric_tmr_ptr, unsigned char tm_type,
1650 gfp_t gfp, unsigned int tag, int flags)
Andy Groverea98d7f2012-01-19 13:39:21 -08001651{
1652 struct se_portal_group *se_tpg;
1653 int ret;
1654
1655 se_tpg = se_sess->se_tpg;
1656 BUG_ON(!se_tpg);
1657
1658 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1659 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001660 /*
1661 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1662 * allocation failure.
1663 */
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001664 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001665 if (ret < 0)
1666 return -ENOMEM;
Andy Groverea98d7f2012-01-19 13:39:21 -08001667
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001668 if (tm_type == TMR_ABORT_TASK)
1669 se_cmd->se_tmr_req->ref_task_tag = tag;
1670
Andy Groverea98d7f2012-01-19 13:39:21 -08001671 /* See target_submit_cmd for commentary */
1672 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1673
Andy Groverea98d7f2012-01-19 13:39:21 -08001674 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1675 if (ret) {
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001676 /*
1677 * For callback during failure handling, push this work off
1678 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1679 */
1680 INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
1681 schedule_work(&se_cmd->work);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001682 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001683 }
1684 transport_generic_handle_tmr(se_cmd);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001685 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001686}
1687EXPORT_SYMBOL(target_submit_tmr);
1688
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001689/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001690 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1691 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1692 * complete setup in TCM process context w/ TFO->new_cmd_map().
1693 */
1694int transport_generic_handle_cdb_map(
1695 struct se_cmd *cmd)
1696{
Andy Grovere3d6f902011-07-19 08:55:10 +00001697 if (!cmd->se_lun) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001698 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001699 pr_err("cmd->se_lun is NULL\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001700 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001701 }
1702
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001703 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001704 return 0;
1705}
1706EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1707
1708/* transport_generic_handle_data():
1709 *
1710 *
1711 */
1712int transport_generic_handle_data(
1713 struct se_cmd *cmd)
1714{
1715 /*
1716 * For the software fabric case, then we assume the nexus is being
1717 * failed/shutdown when signals are pending from the kthread context
1718 * caller, so we return a failure. For the HW target mode case running
1719 * in interrupt code, the signal_pending() check is skipped.
1720 */
1721 if (!in_interrupt() && signal_pending(current))
Andy Grovere3d6f902011-07-19 08:55:10 +00001722 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001723 /*
1724 * If the received CDB has aleady been ABORTED by the generic
1725 * target engine, we now call transport_check_aborted_status()
1726 * to queue any delated TASK_ABORTED status for the received CDB to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001727 * fabric module as we are expecting no further incoming DATA OUT
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001728 * sequences at this point.
1729 */
1730 if (transport_check_aborted_status(cmd, 1) != 0)
1731 return 0;
1732
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001733 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001734 return 0;
1735}
1736EXPORT_SYMBOL(transport_generic_handle_data);
1737
1738/* transport_generic_handle_tmr():
1739 *
1740 *
1741 */
1742int transport_generic_handle_tmr(
1743 struct se_cmd *cmd)
1744{
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001745 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001746 return 0;
1747}
1748EXPORT_SYMBOL(transport_generic_handle_tmr);
1749
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001750/*
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001751 * If the cmd is active, request it to be stopped and sleep until it
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001752 * has completed.
1753 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001754bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001755{
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001756 bool was_active = false;
1757
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001758 if (cmd->transport_state & CMD_T_BUSY) {
1759 cmd->transport_state |= CMD_T_REQUEST_STOP;
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001760 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1761
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001762 pr_debug("cmd %p waiting to complete\n", cmd);
1763 wait_for_completion(&cmd->task_stop_comp);
1764 pr_debug("cmd %p stopped successfully\n", cmd);
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001765
1766 spin_lock_irqsave(&cmd->t_state_lock, *flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001767 cmd->transport_state &= ~CMD_T_REQUEST_STOP;
1768 cmd->transport_state &= ~CMD_T_BUSY;
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001769 was_active = true;
1770 }
1771
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001772 return was_active;
1773}
1774
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001775/*
1776 * Handle SAM-esque emulation for generic transport request failures.
1777 */
Nicholas Bellinger2fbff1272012-02-10 16:18:11 -08001778void transport_generic_request_failure(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001779{
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001780 int ret = 0;
1781
Andy Grover6708bb22011-06-08 10:36:43 -07001782 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001783 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -07001784 cmd->t_task_cdb[0]);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001785 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001786 cmd->se_tfo->get_cmd_state(cmd),
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001787 cmd->t_state, cmd->scsi_sense_reason);
Christoph Hellwigd43d6ae2012-04-24 00:25:08 -04001788 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001789 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1790 (cmd->transport_state & CMD_T_STOP) != 0,
1791 (cmd->transport_state & CMD_T_SENT) != 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001792
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001793 /*
1794 * For SAM Task Attribute emulation for failed struct se_cmd
1795 */
1796 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1797 transport_complete_task_attr(cmd);
1798
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001799 switch (cmd->scsi_sense_reason) {
1800 case TCM_NON_EXISTENT_LUN:
1801 case TCM_UNSUPPORTED_SCSI_OPCODE:
1802 case TCM_INVALID_CDB_FIELD:
1803 case TCM_INVALID_PARAMETER_LIST:
1804 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1805 case TCM_UNKNOWN_MODE_PAGE:
1806 case TCM_WRITE_PROTECTED:
1807 case TCM_CHECK_CONDITION_ABORT_CMD:
1808 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1809 case TCM_CHECK_CONDITION_NOT_READY:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001810 break;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001811 case TCM_RESERVATION_CONFLICT:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001812 /*
1813 * No SENSE Data payload for this case, set SCSI Status
1814 * and queue the response to $FABRIC_MOD.
1815 *
1816 * Uses linux/include/scsi/scsi.h SAM status codes defs
1817 */
1818 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1819 /*
1820 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1821 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1822 * CONFLICT STATUS.
1823 *
1824 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1825 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001826 if (cmd->se_sess &&
1827 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1828 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001829 cmd->orig_fe_lun, 0x2C,
1830 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1831
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001832 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001833 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001834 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001835 goto check_stop;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001836 default:
Andy Grover6708bb22011-06-08 10:36:43 -07001837 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001838 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001839 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1840 break;
1841 }
Nicholas Bellinger16ab8e62011-08-08 19:03:38 -07001842 /*
1843 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1844 * make the call to transport_send_check_condition_and_sense()
1845 * directly. Otherwise expect the fabric to make the call to
1846 * transport_send_check_condition_and_sense() after handling
1847 * possible unsoliticied write data payloads.
1848 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001849 ret = transport_send_check_condition_and_sense(cmd,
1850 cmd->scsi_sense_reason, 0);
1851 if (ret == -EAGAIN || ret == -ENOMEM)
1852 goto queue_full;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001853
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001854check_stop:
1855 transport_lun_remove_cmd(cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07001856 if (!transport_cmd_check_stop_to_fabric(cmd))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001857 ;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001858 return;
1859
1860queue_full:
Christoph Hellwige057f532011-10-17 13:56:41 -04001861 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1862 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001863}
Nicholas Bellinger2fbff1272012-02-10 16:18:11 -08001864EXPORT_SYMBOL(transport_generic_request_failure);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001865
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001866static void __target_execute_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001867{
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001868 int error;
1869
1870 spin_lock_irq(&cmd->t_state_lock);
1871 cmd->transport_state |= (CMD_T_BUSY|CMD_T_SENT);
1872 spin_unlock_irq(&cmd->t_state_lock);
1873
1874 if (cmd->execute_cmd)
1875 error = cmd->execute_cmd(cmd);
1876 else {
1877 error = cmd->se_dev->transport->execute_cmd(cmd, cmd->t_data_sg,
1878 cmd->t_data_nents, cmd->data_direction);
1879 }
1880
1881 if (error) {
1882 spin_lock_irq(&cmd->t_state_lock);
1883 cmd->transport_state &= ~(CMD_T_BUSY|CMD_T_SENT);
1884 spin_unlock_irq(&cmd->t_state_lock);
1885
1886 transport_generic_request_failure(cmd);
1887 }
1888}
1889
1890static void target_execute_cmd(struct se_cmd *cmd)
1891{
1892 struct se_device *dev = cmd->se_dev;
1893
1894 if (transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING))
1895 return;
1896
1897 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1898 goto execute;
1899
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001900 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001901 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001902 * to allow the passed struct se_cmd list of tasks to the front of the list.
1903 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001904 switch (cmd->sam_task_attr) {
1905 case MSG_HEAD_TAG:
1906 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, "
1907 "se_ordered_id: %u\n",
1908 cmd->t_task_cdb[0], cmd->se_ordered_id);
1909 goto execute;
1910 case MSG_ORDERED_TAG:
1911 atomic_inc(&dev->dev_ordered_sync);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001912 smp_mb__after_atomic_inc();
1913
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001914 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, "
1915 " se_ordered_id: %u\n",
1916 cmd->t_task_cdb[0], cmd->se_ordered_id);
1917
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001918 /*
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001919 * Execute an ORDERED command if no other older commands
1920 * exist that need to be completed first.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001921 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001922 if (!atomic_read(&dev->simple_cmds))
1923 goto execute;
1924 break;
1925 default:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001926 /*
1927 * For SIMPLE and UNTAGGED Task Attribute commands
1928 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001929 atomic_inc(&dev->simple_cmds);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001930 smp_mb__after_atomic_inc();
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001931 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001932 }
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001933
1934 if (atomic_read(&dev->dev_ordered_sync) != 0) {
1935 spin_lock(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001936 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
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 Hellwig7d680f3b2011-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 Hellwig7d680f3b2011-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 Hellwig7d680f3b2011-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 Hellwig7d680f3b2011-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
2538 * with transport_off=1 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 */
2541 transport_cmd_check_stop(cmd, 1, 0);
2542
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 Hellwig7d680f3b2011-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);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002731 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovere3d6f902011-07-19 08:55:10 +00002732 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002733 }
Christoph Hellwig7d680f3b2011-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
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002776 /*
2777 * This will notify iscsi_target_transport.c:
2778 * transport_cmd_check_stop() that a LUN shutdown is in
2779 * progress for the iscsi_cmd_t.
2780 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002781 spin_lock(&cmd->t_state_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07002782 pr_debug("SE_LUN[%d] - Setting cmd->transport"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002783 "_lun_stop for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002784 cmd->se_lun->unpacked_lun,
2785 cmd->se_tfo->get_task_tag(cmd));
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002786 cmd->transport_state |= CMD_T_LUN_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07002787 spin_unlock(&cmd->t_state_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002788
2789 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
2790
Andy Grover6708bb22011-06-08 10:36:43 -07002791 if (!cmd->se_lun) {
2792 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002793 cmd->se_tfo->get_task_tag(cmd),
2794 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002795 BUG();
2796 }
2797 /*
2798 * If the Storage engine still owns the iscsi_cmd_t, determine
2799 * and/or stop its context.
2800 */
Andy Grover6708bb22011-06-08 10:36:43 -07002801 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
Andy Grovere3d6f902011-07-19 08:55:10 +00002802 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
2803 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002804
Andy Grovere3d6f902011-07-19 08:55:10 +00002805 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002806 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2807 continue;
2808 }
2809
Andy Grover6708bb22011-06-08 10:36:43 -07002810 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002811 "_wait_for_tasks(): SUCCESS\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002812 cmd->se_lun->unpacked_lun,
2813 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002814
Andy Grovera1d8b492011-05-02 17:12:10 -07002815 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002816 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
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 goto check_cond;
2819 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002820 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002821 target_remove_from_state_list(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002822 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002823
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002824 /*
2825 * The Storage engine stopped this struct se_cmd before it was
2826 * send to the fabric frontend for delivery back to the
2827 * Initiator Node. Return this SCSI CDB back with an
2828 * CHECK_CONDITION status.
2829 */
2830check_cond:
2831 transport_send_check_condition_and_sense(cmd,
2832 TCM_NON_EXISTENT_LUN, 0);
2833 /*
2834 * If the fabric frontend is waiting for this iscsi_cmd_t to
2835 * be released, notify the waiting thread now that LU has
2836 * finished accessing it.
2837 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002838 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002839 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07002840 pr_debug("SE_LUN[%d] - Detected FE stop for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002841 " struct se_cmd: %p ITT: 0x%08x\n",
2842 lun->unpacked_lun,
Andy Grovere3d6f902011-07-19 08:55:10 +00002843 cmd, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002844
Andy Grovera1d8b492011-05-02 17:12:10 -07002845 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002846 cmd_flags);
2847 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovera1d8b492011-05-02 17:12:10 -07002848 complete(&cmd->transport_lun_fe_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002849 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2850 continue;
2851 }
Andy Grover6708bb22011-06-08 10:36:43 -07002852 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002853 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002854
Andy Grovera1d8b492011-05-02 17:12:10 -07002855 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002856 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2857 }
2858 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
2859}
2860
2861static int transport_clear_lun_thread(void *p)
2862{
Jörn Engel8359cf42011-11-24 02:05:51 +01002863 struct se_lun *lun = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002864
2865 __transport_clear_lun_from_sessions(lun);
2866 complete(&lun->lun_shutdown_comp);
2867
2868 return 0;
2869}
2870
2871int transport_clear_lun_from_sessions(struct se_lun *lun)
2872{
2873 struct task_struct *kt;
2874
Andy Grover5951146d2011-07-19 10:26:37 +00002875 kt = kthread_run(transport_clear_lun_thread, lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002876 "tcm_cl_%u", lun->unpacked_lun);
2877 if (IS_ERR(kt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002878 pr_err("Unable to start clear_lun thread\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00002879 return PTR_ERR(kt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002880 }
2881 wait_for_completion(&lun->lun_shutdown_comp);
2882
2883 return 0;
2884}
2885
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002886/**
2887 * transport_wait_for_tasks - wait for completion to occur
2888 * @cmd: command to wait
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002889 *
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002890 * Called from frontend fabric context to wait for storage engine
2891 * to pause and/or release frontend generated struct se_cmd.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002892 */
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002893bool transport_wait_for_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002894{
2895 unsigned long flags;
2896
Andy Grovera1d8b492011-05-02 17:12:10 -07002897 spin_lock_irqsave(&cmd->t_state_lock, flags);
Andy Groverc8e31f22012-01-19 13:39:17 -08002898 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
2899 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002900 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002901 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002902 }
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04002903
Andy Groverc8e31f22012-01-19 13:39:17 -08002904 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
2905 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002906 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002907 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002908 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002909 /*
2910 * If we are already stopped due to an external event (ie: LUN shutdown)
2911 * sleep until the connection can have the passed struct se_cmd back.
Andy Grovera1d8b492011-05-02 17:12:10 -07002912 * The cmd->transport_lun_stopped_sem will be upped by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002913 * transport_clear_lun_from_sessions() once the ConfigFS context caller
2914 * has completed its operation on the struct se_cmd.
2915 */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002916 if (cmd->transport_state & CMD_T_LUN_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07002917 pr_debug("wait_for_tasks: Stopping"
Andy Grovere3d6f902011-07-19 08:55:10 +00002918 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002919 "_stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002920 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002921 /*
2922 * There is a special case for WRITES where a FE exception +
2923 * LUN shutdown means ConfigFS context is still sleeping on
2924 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
2925 * We go ahead and up transport_lun_stop_comp just to be sure
2926 * here.
2927 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002928 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2929 complete(&cmd->transport_lun_stop_comp);
2930 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
2931 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002932
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002933 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002934 /*
2935 * At this point, the frontend who was the originator of this
2936 * struct se_cmd, now owns the structure and can be released through
2937 * normal means below.
2938 */
Andy Grover6708bb22011-06-08 10:36:43 -07002939 pr_debug("wait_for_tasks: Stopped"
Andy Grovere3d6f902011-07-19 08:55:10 +00002940 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002941 "stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002942 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002943
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002944 cmd->transport_state &= ~CMD_T_LUN_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002945 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002946
Nicholas Bellinger3d289342012-02-13 02:38:14 -08002947 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002948 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002949 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002950 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002951
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002952 cmd->transport_state |= CMD_T_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002953
Andy Grover6708bb22011-06-08 10:36:43 -07002954 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002955 " i_state: %d, t_state: %d, CMD_T_STOP\n",
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04002956 cmd, cmd->se_tfo->get_task_tag(cmd),
2957 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002958
Andy Grovera1d8b492011-05-02 17:12:10 -07002959 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002960
Andy Grover5951146d2011-07-19 10:26:37 +00002961 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002962
Andy Grovera1d8b492011-05-02 17:12:10 -07002963 wait_for_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002964
Andy Grovera1d8b492011-05-02 17:12:10 -07002965 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002966 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002967
Andy Grover6708bb22011-06-08 10:36:43 -07002968 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
Andy Grovera1d8b492011-05-02 17:12:10 -07002969 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002970 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002971
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002972 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002973
2974 return true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002975}
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002976EXPORT_SYMBOL(transport_wait_for_tasks);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002977
2978static int transport_get_sense_codes(
2979 struct se_cmd *cmd,
2980 u8 *asc,
2981 u8 *ascq)
2982{
2983 *asc = cmd->scsi_asc;
2984 *ascq = cmd->scsi_ascq;
2985
2986 return 0;
2987}
2988
2989static int transport_set_sense_codes(
2990 struct se_cmd *cmd,
2991 u8 asc,
2992 u8 ascq)
2993{
2994 cmd->scsi_asc = asc;
2995 cmd->scsi_ascq = ascq;
2996
2997 return 0;
2998}
2999
3000int transport_send_check_condition_and_sense(
3001 struct se_cmd *cmd,
3002 u8 reason,
3003 int from_transport)
3004{
3005 unsigned char *buffer = cmd->sense_buffer;
3006 unsigned long flags;
3007 int offset;
3008 u8 asc = 0, ascq = 0;
3009
Andy Grovera1d8b492011-05-02 17:12:10 -07003010 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003011 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07003012 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003013 return 0;
3014 }
3015 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
Andy Grovera1d8b492011-05-02 17:12:10 -07003016 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003017
3018 if (!reason && from_transport)
3019 goto after_reason;
3020
3021 if (!from_transport)
3022 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
3023 /*
3024 * Data Segment and SenseLength of the fabric response PDU.
3025 *
3026 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
3027 * from include/scsi/scsi_cmnd.h
3028 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003029 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003030 TRANSPORT_SENSE_BUFFER);
3031 /*
3032 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
3033 * SENSE KEY values from include/scsi/scsi.h
3034 */
3035 switch (reason) {
3036 case TCM_NON_EXISTENT_LUN:
Nicholas Bellingereb39d342011-07-26 16:59:00 -07003037 /* CURRENT ERROR */
3038 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003039 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingereb39d342011-07-26 16:59:00 -07003040 /* ILLEGAL REQUEST */
3041 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
3042 /* LOGICAL UNIT NOT SUPPORTED */
3043 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
3044 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003045 case TCM_UNSUPPORTED_SCSI_OPCODE:
3046 case TCM_SECTOR_COUNT_TOO_MANY:
3047 /* CURRENT ERROR */
3048 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003049 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003050 /* ILLEGAL REQUEST */
3051 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
3052 /* INVALID COMMAND OPERATION CODE */
3053 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
3054 break;
3055 case TCM_UNKNOWN_MODE_PAGE:
3056 /* CURRENT ERROR */
3057 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003058 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003059 /* ILLEGAL REQUEST */
3060 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
3061 /* INVALID FIELD IN CDB */
3062 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
3063 break;
3064 case TCM_CHECK_CONDITION_ABORT_CMD:
3065 /* CURRENT ERROR */
3066 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003067 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003068 /* ABORTED COMMAND */
3069 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
3070 /* BUS DEVICE RESET FUNCTION OCCURRED */
3071 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
3072 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
3073 break;
3074 case TCM_INCORRECT_AMOUNT_OF_DATA:
3075 /* CURRENT ERROR */
3076 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003077 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003078 /* ABORTED COMMAND */
3079 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
3080 /* WRITE ERROR */
3081 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
3082 /* NOT ENOUGH UNSOLICITED DATA */
3083 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
3084 break;
3085 case TCM_INVALID_CDB_FIELD:
3086 /* CURRENT ERROR */
3087 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003088 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08003089 /* ILLEGAL REQUEST */
3090 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003091 /* INVALID FIELD IN CDB */
3092 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
3093 break;
3094 case TCM_INVALID_PARAMETER_LIST:
3095 /* CURRENT ERROR */
3096 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003097 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08003098 /* ILLEGAL REQUEST */
3099 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003100 /* INVALID FIELD IN PARAMETER LIST */
3101 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
3102 break;
3103 case TCM_UNEXPECTED_UNSOLICITED_DATA:
3104 /* CURRENT ERROR */
3105 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003106 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003107 /* ABORTED COMMAND */
3108 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
3109 /* WRITE ERROR */
3110 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
3111 /* UNEXPECTED_UNSOLICITED_DATA */
3112 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
3113 break;
3114 case TCM_SERVICE_CRC_ERROR:
3115 /* CURRENT ERROR */
3116 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003117 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003118 /* ABORTED COMMAND */
3119 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
3120 /* PROTOCOL SERVICE CRC ERROR */
3121 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
3122 /* N/A */
3123 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
3124 break;
3125 case TCM_SNACK_REJECTED:
3126 /* CURRENT ERROR */
3127 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003128 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003129 /* ABORTED COMMAND */
3130 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
3131 /* READ ERROR */
3132 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
3133 /* FAILED RETRANSMISSION REQUEST */
3134 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
3135 break;
3136 case TCM_WRITE_PROTECTED:
3137 /* CURRENT ERROR */
3138 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003139 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003140 /* DATA PROTECT */
3141 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
3142 /* WRITE PROTECTED */
3143 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
3144 break;
3145 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
3146 /* CURRENT ERROR */
3147 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003148 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003149 /* UNIT ATTENTION */
3150 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
3151 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
3152 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
3153 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
3154 break;
3155 case TCM_CHECK_CONDITION_NOT_READY:
3156 /* CURRENT ERROR */
3157 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003158 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003159 /* Not Ready */
3160 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
3161 transport_get_sense_codes(cmd, &asc, &ascq);
3162 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
3163 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
3164 break;
3165 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
3166 default:
3167 /* CURRENT ERROR */
3168 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003169 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003170 /* ILLEGAL REQUEST */
3171 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
3172 /* LOGICAL UNIT COMMUNICATION FAILURE */
3173 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
3174 break;
3175 }
3176 /*
3177 * This code uses linux/include/scsi/scsi.h SAM status codes!
3178 */
3179 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
3180 /*
3181 * Automatically padded, this value is encoded in the fabric's
3182 * data_length response PDU containing the SCSI defined sense data.
3183 */
3184 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
3185
3186after_reason:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003187 return cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003188}
3189EXPORT_SYMBOL(transport_send_check_condition_and_sense);
3190
3191int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
3192{
3193 int ret = 0;
3194
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05003195 if (cmd->transport_state & CMD_T_ABORTED) {
Andy Grover6708bb22011-06-08 10:36:43 -07003196 if (!send_status ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003197 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
3198 return 1;
Andy Grover8b1e1242012-04-03 15:51:12 -07003199
Andy Grover6708bb22011-06-08 10:36:43 -07003200 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003201 " status for CDB: 0x%02x ITT: 0x%08x\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07003202 cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00003203 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07003204
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003205 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
Andy Grovere3d6f902011-07-19 08:55:10 +00003206 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003207 ret = 1;
3208 }
3209 return ret;
3210}
3211EXPORT_SYMBOL(transport_check_aborted_status);
3212
3213void transport_send_task_abort(struct se_cmd *cmd)
3214{
Nicholas Bellingerc252f002011-09-29 14:22:13 -07003215 unsigned long flags;
3216
3217 spin_lock_irqsave(&cmd->t_state_lock, flags);
3218 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
3219 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3220 return;
3221 }
3222 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3223
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003224 /*
3225 * If there are still expected incoming fabric WRITEs, we wait
3226 * until until they have completed before sending a TASK_ABORTED
3227 * response. This response with TASK_ABORTED status will be
3228 * queued back to fabric module by transport_check_aborted_status().
3229 */
3230 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grovere3d6f902011-07-19 08:55:10 +00003231 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05003232 cmd->transport_state |= CMD_T_ABORTED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003233 smp_mb__after_atomic_inc();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003234 }
3235 }
3236 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
Andy Grover8b1e1242012-04-03 15:51:12 -07003237
Andy Grover6708bb22011-06-08 10:36:43 -07003238 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
Andy Grovera1d8b492011-05-02 17:12:10 -07003239 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00003240 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07003241
Andy Grovere3d6f902011-07-19 08:55:10 +00003242 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003243}
3244
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003245static int transport_generic_do_tmr(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003246{
Andy Grover5951146d2011-07-19 10:26:37 +00003247 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003248 struct se_tmr_req *tmr = cmd->se_tmr_req;
3249 int ret;
3250
3251 switch (tmr->function) {
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003252 case TMR_ABORT_TASK:
Nicholas Bellinger3d289342012-02-13 02:38:14 -08003253 core_tmr_abort_task(dev, tmr, cmd->se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003254 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003255 case TMR_ABORT_TASK_SET:
3256 case TMR_CLEAR_ACA:
3257 case TMR_CLEAR_TASK_SET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003258 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
3259 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003260 case TMR_LUN_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003261 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
3262 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
3263 TMR_FUNCTION_REJECTED;
3264 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003265 case TMR_TARGET_WARM_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003266 tmr->response = TMR_FUNCTION_REJECTED;
3267 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003268 case TMR_TARGET_COLD_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003269 tmr->response = TMR_FUNCTION_REJECTED;
3270 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003271 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003272 pr_err("Uknown TMR function: 0x%02x.\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003273 tmr->function);
3274 tmr->response = TMR_FUNCTION_REJECTED;
3275 break;
3276 }
3277
3278 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
Andy Grovere3d6f902011-07-19 08:55:10 +00003279 cmd->se_tfo->queue_tm_rsp(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003280
Christoph Hellwigb7b8bef2011-10-17 13:56:44 -04003281 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003282 return 0;
3283}
3284
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003285/* transport_processing_thread():
3286 *
3287 *
3288 */
3289static int transport_processing_thread(void *param)
3290{
Andy Grover5951146d2011-07-19 10:26:37 +00003291 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003292 struct se_cmd *cmd;
Jörn Engel8359cf42011-11-24 02:05:51 +01003293 struct se_device *dev = param;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003294
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003295 while (!kthread_should_stop()) {
Andy Grovere3d6f902011-07-19 08:55:10 +00003296 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
3297 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003298 kthread_should_stop());
3299 if (ret < 0)
3300 goto out;
3301
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003302get_cmd:
Andy Grover5951146d2011-07-19 10:26:37 +00003303 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
3304 if (!cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003305 continue;
3306
Andy Grover5951146d2011-07-19 10:26:37 +00003307 switch (cmd->t_state) {
Christoph Hellwig680b73c2011-09-12 21:51:14 +02003308 case TRANSPORT_NEW_CMD:
3309 BUG();
3310 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003311 case TRANSPORT_NEW_CMD_MAP:
Andy Grover6708bb22011-06-08 10:36:43 -07003312 if (!cmd->se_tfo->new_cmd_map) {
3313 pr_err("cmd->se_tfo->new_cmd_map is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003314 " NULL for TRANSPORT_NEW_CMD_MAP\n");
3315 BUG();
3316 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003317 ret = cmd->se_tfo->new_cmd_map(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003318 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003319 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003320 break;
3321 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003322 ret = transport_generic_new_cmd(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003323 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003324 transport_generic_request_failure(cmd);
3325 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003326 }
3327 break;
3328 case TRANSPORT_PROCESS_WRITE:
3329 transport_generic_process_write(cmd);
3330 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003331 case TRANSPORT_PROCESS_TMR:
3332 transport_generic_do_tmr(cmd);
3333 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003334 case TRANSPORT_COMPLETE_QF_WP:
Christoph Hellwige057f532011-10-17 13:56:41 -04003335 transport_write_pending_qf(cmd);
3336 break;
3337 case TRANSPORT_COMPLETE_QF_OK:
3338 transport_complete_qf(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003339 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003340 default:
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04003341 pr_err("Unknown t_state: %d for ITT: 0x%08x "
3342 "i_state: %d on SE LUN: %u\n",
3343 cmd->t_state,
Andy Grovere3d6f902011-07-19 08:55:10 +00003344 cmd->se_tfo->get_task_tag(cmd),
3345 cmd->se_tfo->get_cmd_state(cmd),
3346 cmd->se_lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003347 BUG();
3348 }
3349
3350 goto get_cmd;
3351 }
3352
3353out:
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003354 WARN_ON(!list_empty(&dev->state_list));
Nicholas Bellingerce8762f2011-10-09 02:19:01 -07003355 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003356 dev->process_thread = NULL;
3357 return 0;
3358}