blob: 9b7bbbe70211e9b0a899e3f17583b28b54a759eb [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
Andy Grover5951146d2011-07-19 10:26:37 +000069static int transport_processing_thread(void *param);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080070static void transport_complete_task_attr(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -070071static void transport_handle_queue_full(struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -040072 struct se_device *dev);
Andy Grover05d1c7c2011-07-20 19:13:28 +000073static int transport_generic_get_mem(struct se_cmd *cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -070074static void transport_put_cmd(struct se_cmd *cmd);
Christoph Hellwig3df8d402011-10-17 13:56:43 -040075static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080076static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
Christoph Hellwig35e0e752011-10-17 13:56:53 -040077static void target_complete_ok_work(struct work_struct *work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078
Andy Grovere3d6f902011-07-19 08:55:10 +000079int init_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080080{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080081 se_sess_cache = kmem_cache_create("se_sess_cache",
82 sizeof(struct se_session), __alignof__(struct se_session),
83 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070084 if (!se_sess_cache) {
85 pr_err("kmem_cache_create() for struct se_session"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080086 " failed\n");
Andy Groverc8e31f22012-01-19 13:39:17 -080087 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080088 }
89 se_ua_cache = kmem_cache_create("se_ua_cache",
90 sizeof(struct se_ua), __alignof__(struct se_ua),
91 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070092 if (!se_ua_cache) {
93 pr_err("kmem_cache_create() for struct se_ua failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -040094 goto out_free_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080095 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080096 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
97 sizeof(struct t10_pr_registration),
98 __alignof__(struct t10_pr_registration), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070099 if (!t10_pr_reg_cache) {
100 pr_err("kmem_cache_create() for struct t10_pr_registration"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800101 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400102 goto out_free_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800103 }
104 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
105 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
106 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700107 if (!t10_alua_lu_gp_cache) {
108 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800109 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400110 goto out_free_pr_reg_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800111 }
112 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
113 sizeof(struct t10_alua_lu_gp_member),
114 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700115 if (!t10_alua_lu_gp_mem_cache) {
116 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800117 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400118 goto out_free_lu_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800119 }
120 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
121 sizeof(struct t10_alua_tg_pt_gp),
122 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700123 if (!t10_alua_tg_pt_gp_cache) {
124 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800125 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400126 goto out_free_lu_gp_mem_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800127 }
128 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
129 "t10_alua_tg_pt_gp_mem_cache",
130 sizeof(struct t10_alua_tg_pt_gp_member),
131 __alignof__(struct t10_alua_tg_pt_gp_member),
132 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700133 if (!t10_alua_tg_pt_gp_mem_cache) {
134 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800135 "mem_t failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400136 goto out_free_tg_pt_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800137 }
138
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400139 target_completion_wq = alloc_workqueue("target_completion",
140 WQ_MEM_RECLAIM, 0);
141 if (!target_completion_wq)
142 goto out_free_tg_pt_gp_mem_cache;
143
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800144 return 0;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400145
146out_free_tg_pt_gp_mem_cache:
147 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
148out_free_tg_pt_gp_cache:
149 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
150out_free_lu_gp_mem_cache:
151 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
152out_free_lu_gp_cache:
153 kmem_cache_destroy(t10_alua_lu_gp_cache);
154out_free_pr_reg_cache:
155 kmem_cache_destroy(t10_pr_reg_cache);
156out_free_ua_cache:
157 kmem_cache_destroy(se_ua_cache);
158out_free_sess_cache:
159 kmem_cache_destroy(se_sess_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800160out:
Andy Grovere3d6f902011-07-19 08:55:10 +0000161 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800162}
163
Andy Grovere3d6f902011-07-19 08:55:10 +0000164void release_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800165{
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400166 destroy_workqueue(target_completion_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800167 kmem_cache_destroy(se_sess_cache);
168 kmem_cache_destroy(se_ua_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800169 kmem_cache_destroy(t10_pr_reg_cache);
170 kmem_cache_destroy(t10_alua_lu_gp_cache);
171 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
172 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
173 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800174}
175
Andy Grovere3d6f902011-07-19 08:55:10 +0000176/* This code ensures unique mib indexes are handed out. */
177static DEFINE_SPINLOCK(scsi_mib_index_lock);
178static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800179
180/*
181 * Allocate a new row index for the entry type specified
182 */
183u32 scsi_get_new_index(scsi_index_t type)
184{
185 u32 new_index;
186
Andy Grovere3d6f902011-07-19 08:55:10 +0000187 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800188
Andy Grovere3d6f902011-07-19 08:55:10 +0000189 spin_lock(&scsi_mib_index_lock);
190 new_index = ++scsi_mib_index[type];
191 spin_unlock(&scsi_mib_index_lock);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800192
193 return new_index;
194}
195
Christoph Hellwige26d99a2011-11-14 12:30:30 -0500196static void transport_init_queue_obj(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800197{
198 atomic_set(&qobj->queue_cnt, 0);
199 INIT_LIST_HEAD(&qobj->qobj_list);
200 init_waitqueue_head(&qobj->thread_wq);
201 spin_lock_init(&qobj->cmd_queue_lock);
202}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800203
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700204void transport_subsystem_check_init(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800205{
206 int ret;
207
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700208 if (sub_api_initialized)
209 return;
210
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800211 ret = request_module("target_core_iblock");
212 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700213 pr_err("Unable to load target_core_iblock\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214
215 ret = request_module("target_core_file");
216 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700217 pr_err("Unable to load target_core_file\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800218
219 ret = request_module("target_core_pscsi");
220 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700221 pr_err("Unable to load target_core_pscsi\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800222
223 ret = request_module("target_core_stgt");
224 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700225 pr_err("Unable to load target_core_stgt\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800226
Andy Grovere3d6f902011-07-19 08:55:10 +0000227 sub_api_initialized = 1;
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700228 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800229}
230
231struct se_session *transport_init_session(void)
232{
233 struct se_session *se_sess;
234
235 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700236 if (!se_sess) {
237 pr_err("Unable to allocate struct se_session from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800238 " se_sess_cache\n");
239 return ERR_PTR(-ENOMEM);
240 }
241 INIT_LIST_HEAD(&se_sess->sess_list);
242 INIT_LIST_HEAD(&se_sess->sess_acl_list);
Nicholas Bellingera17f0912011-11-02 21:52:08 -0700243 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
244 INIT_LIST_HEAD(&se_sess->sess_wait_list);
245 spin_lock_init(&se_sess->sess_cmd_lock);
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800246 kref_init(&se_sess->sess_kref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800247
248 return se_sess;
249}
250EXPORT_SYMBOL(transport_init_session);
251
252/*
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700253 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800254 */
255void __transport_register_session(
256 struct se_portal_group *se_tpg,
257 struct se_node_acl *se_nacl,
258 struct se_session *se_sess,
259 void *fabric_sess_ptr)
260{
261 unsigned char buf[PR_REG_ISID_LEN];
262
263 se_sess->se_tpg = se_tpg;
264 se_sess->fabric_sess_ptr = fabric_sess_ptr;
265 /*
266 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
267 *
268 * Only set for struct se_session's that will actually be moving I/O.
269 * eg: *NOT* discovery sessions.
270 */
271 if (se_nacl) {
272 /*
273 * If the fabric module supports an ISID based TransportID,
274 * save this value in binary from the fabric I_T Nexus now.
275 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000276 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800277 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +0000278 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800279 &buf[0], PR_REG_ISID_LEN);
280 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
281 }
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800282 kref_get(&se_nacl->acl_kref);
283
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800284 spin_lock_irq(&se_nacl->nacl_sess_lock);
285 /*
286 * The se_nacl->nacl_sess pointer will be set to the
287 * last active I_T Nexus for each struct se_node_acl.
288 */
289 se_nacl->nacl_sess = se_sess;
290
291 list_add_tail(&se_sess->sess_acl_list,
292 &se_nacl->acl_sess_list);
293 spin_unlock_irq(&se_nacl->nacl_sess_lock);
294 }
295 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
296
Andy Grover6708bb22011-06-08 10:36:43 -0700297 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000298 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800299}
300EXPORT_SYMBOL(__transport_register_session);
301
302void transport_register_session(
303 struct se_portal_group *se_tpg,
304 struct se_node_acl *se_nacl,
305 struct se_session *se_sess,
306 void *fabric_sess_ptr)
307{
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700308 unsigned long flags;
309
310 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800311 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700312 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800313}
314EXPORT_SYMBOL(transport_register_session);
315
Joern Engel41492682012-05-18 13:57:19 -0700316void target_release_session(struct kref *kref)
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800317{
318 struct se_session *se_sess = container_of(kref,
319 struct se_session, sess_kref);
320 struct se_portal_group *se_tpg = se_sess->se_tpg;
321
322 se_tpg->se_tpg_tfo->close_session(se_sess);
323}
324
325void target_get_session(struct se_session *se_sess)
326{
327 kref_get(&se_sess->sess_kref);
328}
329EXPORT_SYMBOL(target_get_session);
330
Jörn Engel33933a02012-05-11 10:35:08 -0400331void target_put_session(struct se_session *se_sess)
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800332{
Joern Engel41492682012-05-18 13:57:19 -0700333 struct se_portal_group *tpg = se_sess->se_tpg;
334
335 if (tpg->se_tpg_tfo->put_session != NULL) {
336 tpg->se_tpg_tfo->put_session(se_sess);
337 return;
338 }
Jörn Engel33933a02012-05-11 10:35:08 -0400339 kref_put(&se_sess->sess_kref, target_release_session);
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800340}
341EXPORT_SYMBOL(target_put_session);
342
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800343static void target_complete_nacl(struct kref *kref)
344{
345 struct se_node_acl *nacl = container_of(kref,
346 struct se_node_acl, acl_kref);
347
348 complete(&nacl->acl_free_comp);
349}
350
351void target_put_nacl(struct se_node_acl *nacl)
352{
353 kref_put(&nacl->acl_kref, target_complete_nacl);
354}
355
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800356void transport_deregister_session_configfs(struct se_session *se_sess)
357{
358 struct se_node_acl *se_nacl;
Roland Dreier23388862011-06-22 01:02:21 -0700359 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800360 /*
361 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
362 */
363 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700364 if (se_nacl) {
Roland Dreier23388862011-06-22 01:02:21 -0700365 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800366 if (se_nacl->acl_stop == 0)
367 list_del(&se_sess->sess_acl_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800368 /*
369 * If the session list is empty, then clear the pointer.
370 * Otherwise, set the struct se_session pointer from the tail
371 * element of the per struct se_node_acl active session list.
372 */
373 if (list_empty(&se_nacl->acl_sess_list))
374 se_nacl->nacl_sess = NULL;
375 else {
376 se_nacl->nacl_sess = container_of(
377 se_nacl->acl_sess_list.prev,
378 struct se_session, sess_acl_list);
379 }
Roland Dreier23388862011-06-22 01:02:21 -0700380 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800381 }
382}
383EXPORT_SYMBOL(transport_deregister_session_configfs);
384
385void transport_free_session(struct se_session *se_sess)
386{
387 kmem_cache_free(se_sess_cache, se_sess);
388}
389EXPORT_SYMBOL(transport_free_session);
390
391void transport_deregister_session(struct se_session *se_sess)
392{
393 struct se_portal_group *se_tpg = se_sess->se_tpg;
Nicholas Bellinger01468342012-03-10 14:32:52 -0800394 struct target_core_fabric_ops *se_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800395 struct se_node_acl *se_nacl;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700396 unsigned long flags;
Nicholas Bellinger01468342012-03-10 14:32:52 -0800397 bool comp_nacl = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800398
Andy Grover6708bb22011-06-08 10:36:43 -0700399 if (!se_tpg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800400 transport_free_session(se_sess);
401 return;
402 }
Nicholas Bellinger01468342012-03-10 14:32:52 -0800403 se_tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800404
Roland Dreiere63a8e12011-08-12 16:01:02 -0700405 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406 list_del(&se_sess->sess_list);
407 se_sess->se_tpg = NULL;
408 se_sess->fabric_sess_ptr = NULL;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700409 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800410
411 /*
412 * Determine if we need to do extra work for this initiator node's
413 * struct se_node_acl if it had been previously dynamically generated.
414 */
415 se_nacl = se_sess->se_node_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800416
Nicholas Bellinger01468342012-03-10 14:32:52 -0800417 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
418 if (se_nacl && se_nacl->dynamic_node_acl) {
419 if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
420 list_del(&se_nacl->acl_list);
421 se_tpg->num_node_acls--;
422 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
423 core_tpg_wait_for_nacl_pr_ref(se_nacl);
424 core_free_device_list_for_node(se_nacl, se_tpg);
425 se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl);
426
427 comp_nacl = false;
428 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800429 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800430 }
Nicholas Bellinger01468342012-03-10 14:32:52 -0800431 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800432
Andy Grover6708bb22011-06-08 10:36:43 -0700433 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000434 se_tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellinger01468342012-03-10 14:32:52 -0800435 /*
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800436 * If last kref is dropping now for an explict NodeACL, awake sleeping
437 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
438 * removal context.
Nicholas Bellinger01468342012-03-10 14:32:52 -0800439 */
440 if (se_nacl && comp_nacl == true)
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800441 target_put_nacl(se_nacl);
Nicholas Bellinger01468342012-03-10 14:32:52 -0800442
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800443 transport_free_session(se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800444}
445EXPORT_SYMBOL(transport_deregister_session);
446
447/*
Andy Grovera1d8b492011-05-02 17:12:10 -0700448 * Called with cmd->t_state_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800449 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400450static void target_remove_from_state_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800451{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400452 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800453 unsigned long flags;
454
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400455 if (!dev)
456 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800457
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400458 if (cmd->transport_state & CMD_T_BUSY)
459 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800460
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400461 spin_lock_irqsave(&dev->execute_task_lock, flags);
462 if (cmd->state_active) {
463 list_del(&cmd->state_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400464 cmd->state_active = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800465 }
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400466 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800467}
468
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400469static int transport_cmd_check_stop(struct se_cmd *cmd, bool remove_from_lists)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800470{
471 unsigned long flags;
472
Andy Grovera1d8b492011-05-02 17:12:10 -0700473 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800474 /*
475 * Determine if IOCTL context caller in requesting the stopping of this
476 * command for LUN shutdown purposes.
477 */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500478 if (cmd->transport_state & CMD_T_LUN_STOP) {
479 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
480 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800481
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500482 cmd->transport_state &= ~CMD_T_ACTIVE;
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400483 if (remove_from_lists)
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400484 target_remove_from_state_list(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700485 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800486
Andy Grovera1d8b492011-05-02 17:12:10 -0700487 complete(&cmd->transport_lun_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800488 return 1;
489 }
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400490
491 if (remove_from_lists) {
492 target_remove_from_state_list(cmd);
493
494 /*
495 * Clear struct se_cmd->se_lun before the handoff to FE.
496 */
497 cmd->se_lun = NULL;
498 }
499
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500 /*
501 * Determine if frontend context caller is requesting the stopping of
Andy Grovere3d6f902011-07-19 08:55:10 +0000502 * this command for frontend exceptions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800503 */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500504 if (cmd->transport_state & CMD_T_STOP) {
505 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
506 __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000507 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800508
Andy Grovera1d8b492011-05-02 17:12:10 -0700509 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800510
Andy Grovera1d8b492011-05-02 17:12:10 -0700511 complete(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800512 return 1;
513 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800514
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400515 cmd->transport_state &= ~CMD_T_ACTIVE;
516 if (remove_from_lists) {
517 /*
518 * Some fabric modules like tcm_loop can release
519 * their internally allocated I/O reference now and
520 * struct se_cmd now.
521 *
522 * Fabric modules are expected to return '1' here if the
523 * se_cmd being passed is released at this point,
524 * or zero if not being released.
525 */
526 if (cmd->se_tfo->check_stop_free != NULL) {
527 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
528 return cmd->se_tfo->check_stop_free(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800529 }
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400530 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800531
Andy Grovera1d8b492011-05-02 17:12:10 -0700532 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800533 return 0;
534}
535
536static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
537{
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400538 return transport_cmd_check_stop(cmd, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800539}
540
541static void transport_lun_remove_cmd(struct se_cmd *cmd)
542{
Andy Grovere3d6f902011-07-19 08:55:10 +0000543 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800544 unsigned long flags;
545
546 if (!lun)
547 return;
548
Andy Grovera1d8b492011-05-02 17:12:10 -0700549 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500550 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
551 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400552 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800553 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700554 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800555
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800556 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -0500557 if (!list_empty(&cmd->se_lun_node))
558 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800559 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
560}
561
562void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
563{
Andy Groverc8e31f22012-01-19 13:39:17 -0800564 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellinger8dc52b52011-10-09 02:02:51 -0700565 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800566
567 if (transport_cmd_check_stop_to_fabric(cmd))
568 return;
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700569 if (remove) {
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400570 transport_remove_cmd_from_queue(cmd);
Christoph Hellwige6a25732011-09-13 23:08:50 +0200571 transport_put_cmd(cmd);
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700572 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800573}
574
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400575static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
576 bool at_head)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800577{
578 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +0000579 struct se_queue_obj *qobj = &dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800580 unsigned long flags;
581
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800582 if (t_state) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700583 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800584 cmd->t_state = t_state;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500585 cmd->transport_state |= CMD_T_ACTIVE;
Andy Grovera1d8b492011-05-02 17:12:10 -0700586 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800587 }
588
589 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Roland Dreier79a7fef2011-09-28 22:12:07 -0700590
591 /* If the cmd is already on the list, remove it before we add it */
592 if (!list_empty(&cmd->se_queue_node))
593 list_del(&cmd->se_queue_node);
594 else
595 atomic_inc(&qobj->queue_cnt);
596
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400597 if (at_head)
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700598 list_add(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400599 else
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700600 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500601 cmd->transport_state |= CMD_T_QUEUED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800602 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
603
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800604 wake_up_interruptible(&qobj->thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800605}
606
Andy Grover5951146d2011-07-19 10:26:37 +0000607static struct se_cmd *
608transport_get_cmd_from_queue(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800609{
Andy Grover5951146d2011-07-19 10:26:37 +0000610 struct se_cmd *cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800611 unsigned long flags;
612
613 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
614 if (list_empty(&qobj->qobj_list)) {
615 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
616 return NULL;
617 }
Andy Grover5951146d2011-07-19 10:26:37 +0000618 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800619
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500620 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700621 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800622 atomic_dec(&qobj->queue_cnt);
623 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
624
Andy Grover5951146d2011-07-19 10:26:37 +0000625 return cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800626}
627
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400628static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800629{
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400630 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800631 unsigned long flags;
632
633 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500634 if (!(cmd->transport_state & CMD_T_QUEUED)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800635 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
636 return;
637 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500638 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700639 atomic_dec(&qobj->queue_cnt);
640 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800641 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800642}
643
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400644static void target_complete_failure_work(struct work_struct *work)
645{
646 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
647
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700648 transport_generic_request_failure(cmd);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400649}
650
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400651void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800652{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400653 struct se_device *dev = cmd->se_dev;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400654 int success = scsi_status == GOOD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800655 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800656
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400657 cmd->scsi_status = scsi_status;
658
659
Andy Grovera1d8b492011-05-02 17:12:10 -0700660 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400661 cmd->transport_state &= ~CMD_T_BUSY;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800662
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800663 if (dev && dev->transport->transport_complete) {
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400664 if (dev->transport->transport_complete(cmd,
665 cmd->t_data_sg) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800666 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800667 success = 1;
668 }
669 }
670
671 /*
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400672 * See if we are waiting to complete for an exception condition.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800673 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400674 if (cmd->transport_state & CMD_T_REQUEST_STOP) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700675 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400676 complete(&cmd->task_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800677 return;
678 }
Christoph Hellwig2235007c2011-11-02 05:06:35 -0700679
680 if (!success)
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500681 cmd->transport_state |= CMD_T_FAILED;
Christoph Hellwig2235007c2011-11-02 05:06:35 -0700682
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800683 /*
684 * Check for case where an explict ABORT_TASK has been received
685 * and transport_wait_for_tasks() will be waiting for completion..
686 */
687 if (cmd->transport_state & CMD_T_ABORTED &&
688 cmd->transport_state & CMD_T_STOP) {
689 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
690 complete(&cmd->t_transport_stop_comp);
691 return;
692 } else if (cmd->transport_state & CMD_T_FAILED) {
Christoph Hellwig41e16e92011-11-23 06:54:15 -0500693 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400694 INIT_WORK(&cmd->work, target_complete_failure_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800695 } else {
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400696 INIT_WORK(&cmd->work, target_complete_ok_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800697 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400698
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400699 cmd->t_state = TRANSPORT_COMPLETE;
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800700 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
Andy Grovera1d8b492011-05-02 17:12:10 -0700701 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800702
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400703 queue_work(target_completion_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800704}
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400705EXPORT_SYMBOL(target_complete_cmd);
706
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400707static void target_add_to_state_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800708{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400709 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800710 unsigned long flags;
711
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800712 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400713 if (!cmd->state_active) {
714 list_add_tail(&cmd->state_list, &dev->state_list);
715 cmd->state_active = true;
716 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800717 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800718}
719
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700720/*
Nicholas Bellingerf147abb2011-10-25 23:57:41 -0700721 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700722 */
Christoph Hellwig7a6f0a12012-07-08 15:58:47 -0400723static void transport_write_pending_qf(struct se_cmd *cmd);
724static void transport_complete_qf(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700725
726static void target_qf_do_work(struct work_struct *work)
727{
728 struct se_device *dev = container_of(work, struct se_device,
729 qf_work_queue);
Roland Dreierbcac3642011-08-27 21:33:16 -0700730 LIST_HEAD(qf_cmd_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700731 struct se_cmd *cmd, *cmd_tmp;
732
733 spin_lock_irq(&dev->qf_cmd_lock);
Roland Dreierbcac3642011-08-27 21:33:16 -0700734 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
735 spin_unlock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700736
Roland Dreierbcac3642011-08-27 21:33:16 -0700737 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700738 list_del(&cmd->se_qf_node);
739 atomic_dec(&dev->dev_qf_count);
740 smp_mb__after_atomic_dec();
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700741
Andy Grover6708bb22011-06-08 10:36:43 -0700742 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700743 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -0400744 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700745 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
746 : "UNKNOWN");
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400747
Christoph Hellwig7a6f0a12012-07-08 15:58:47 -0400748 if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP)
749 transport_write_pending_qf(cmd);
750 else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK)
751 transport_complete_qf(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700752 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700753}
754
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800755unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
756{
757 switch (cmd->data_direction) {
758 case DMA_NONE:
759 return "NONE";
760 case DMA_FROM_DEVICE:
761 return "READ";
762 case DMA_TO_DEVICE:
763 return "WRITE";
764 case DMA_BIDIRECTIONAL:
765 return "BIDI";
766 default:
767 break;
768 }
769
770 return "UNKNOWN";
771}
772
773void transport_dump_dev_state(
774 struct se_device *dev,
775 char *b,
776 int *bl)
777{
778 *bl += sprintf(b + *bl, "Status: ");
779 switch (dev->dev_status) {
780 case TRANSPORT_DEVICE_ACTIVATED:
781 *bl += sprintf(b + *bl, "ACTIVATED");
782 break;
783 case TRANSPORT_DEVICE_DEACTIVATED:
784 *bl += sprintf(b + *bl, "DEACTIVATED");
785 break;
786 case TRANSPORT_DEVICE_SHUTDOWN:
787 *bl += sprintf(b + *bl, "SHUTDOWN");
788 break;
789 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
790 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
791 *bl += sprintf(b + *bl, "OFFLINE");
792 break;
793 default:
794 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
795 break;
796 }
797
Christoph Hellwig5f41a312012-05-20 14:34:44 -0400798 *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth);
Nicholas Bellinger11e764b2012-05-09 12:42:09 -0700799 *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n",
800 dev->se_sub_dev->se_dev_attrib.block_size,
801 dev->se_sub_dev->se_dev_attrib.hw_max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800802 *bl += sprintf(b + *bl, " ");
803}
804
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800805void transport_dump_vpd_proto_id(
806 struct t10_vpd *vpd,
807 unsigned char *p_buf,
808 int p_buf_len)
809{
810 unsigned char buf[VPD_TMP_BUF_SIZE];
811 int len;
812
813 memset(buf, 0, VPD_TMP_BUF_SIZE);
814 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
815
816 switch (vpd->protocol_identifier) {
817 case 0x00:
818 sprintf(buf+len, "Fibre Channel\n");
819 break;
820 case 0x10:
821 sprintf(buf+len, "Parallel SCSI\n");
822 break;
823 case 0x20:
824 sprintf(buf+len, "SSA\n");
825 break;
826 case 0x30:
827 sprintf(buf+len, "IEEE 1394\n");
828 break;
829 case 0x40:
830 sprintf(buf+len, "SCSI Remote Direct Memory Access"
831 " Protocol\n");
832 break;
833 case 0x50:
834 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
835 break;
836 case 0x60:
837 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
838 break;
839 case 0x70:
840 sprintf(buf+len, "Automation/Drive Interface Transport"
841 " Protocol\n");
842 break;
843 case 0x80:
844 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
845 break;
846 default:
847 sprintf(buf+len, "Unknown 0x%02x\n",
848 vpd->protocol_identifier);
849 break;
850 }
851
852 if (p_buf)
853 strncpy(p_buf, buf, p_buf_len);
854 else
Andy Grover6708bb22011-06-08 10:36:43 -0700855 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800856}
857
858void
859transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
860{
861 /*
862 * Check if the Protocol Identifier Valid (PIV) bit is set..
863 *
864 * from spc3r23.pdf section 7.5.1
865 */
866 if (page_83[1] & 0x80) {
867 vpd->protocol_identifier = (page_83[0] & 0xf0);
868 vpd->protocol_identifier_set = 1;
869 transport_dump_vpd_proto_id(vpd, NULL, 0);
870 }
871}
872EXPORT_SYMBOL(transport_set_vpd_proto_id);
873
874int transport_dump_vpd_assoc(
875 struct t10_vpd *vpd,
876 unsigned char *p_buf,
877 int p_buf_len)
878{
879 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +0000880 int ret = 0;
881 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800882
883 memset(buf, 0, VPD_TMP_BUF_SIZE);
884 len = sprintf(buf, "T10 VPD Identifier Association: ");
885
886 switch (vpd->association) {
887 case 0x00:
888 sprintf(buf+len, "addressed logical unit\n");
889 break;
890 case 0x10:
891 sprintf(buf+len, "target port\n");
892 break;
893 case 0x20:
894 sprintf(buf+len, "SCSI target device\n");
895 break;
896 default:
897 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
Andy Grovere3d6f902011-07-19 08:55:10 +0000898 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800899 break;
900 }
901
902 if (p_buf)
903 strncpy(p_buf, buf, p_buf_len);
904 else
Andy Grover6708bb22011-06-08 10:36:43 -0700905 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800906
907 return ret;
908}
909
910int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
911{
912 /*
913 * The VPD identification association..
914 *
915 * from spc3r23.pdf Section 7.6.3.1 Table 297
916 */
917 vpd->association = (page_83[1] & 0x30);
918 return transport_dump_vpd_assoc(vpd, NULL, 0);
919}
920EXPORT_SYMBOL(transport_set_vpd_assoc);
921
922int transport_dump_vpd_ident_type(
923 struct t10_vpd *vpd,
924 unsigned char *p_buf,
925 int p_buf_len)
926{
927 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +0000928 int ret = 0;
929 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800930
931 memset(buf, 0, VPD_TMP_BUF_SIZE);
932 len = sprintf(buf, "T10 VPD Identifier Type: ");
933
934 switch (vpd->device_identifier_type) {
935 case 0x00:
936 sprintf(buf+len, "Vendor specific\n");
937 break;
938 case 0x01:
939 sprintf(buf+len, "T10 Vendor ID based\n");
940 break;
941 case 0x02:
942 sprintf(buf+len, "EUI-64 based\n");
943 break;
944 case 0x03:
945 sprintf(buf+len, "NAA\n");
946 break;
947 case 0x04:
948 sprintf(buf+len, "Relative target port identifier\n");
949 break;
950 case 0x08:
951 sprintf(buf+len, "SCSI name string\n");
952 break;
953 default:
954 sprintf(buf+len, "Unsupported: 0x%02x\n",
955 vpd->device_identifier_type);
Andy Grovere3d6f902011-07-19 08:55:10 +0000956 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800957 break;
958 }
959
Andy Grovere3d6f902011-07-19 08:55:10 +0000960 if (p_buf) {
961 if (p_buf_len < strlen(buf)+1)
962 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800963 strncpy(p_buf, buf, p_buf_len);
Andy Grovere3d6f902011-07-19 08:55:10 +0000964 } else {
Andy Grover6708bb22011-06-08 10:36:43 -0700965 pr_debug("%s", buf);
Andy Grovere3d6f902011-07-19 08:55:10 +0000966 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800967
968 return ret;
969}
970
971int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
972{
973 /*
974 * The VPD identifier type..
975 *
976 * from spc3r23.pdf Section 7.6.3.1 Table 298
977 */
978 vpd->device_identifier_type = (page_83[1] & 0x0f);
979 return transport_dump_vpd_ident_type(vpd, NULL, 0);
980}
981EXPORT_SYMBOL(transport_set_vpd_ident_type);
982
983int transport_dump_vpd_ident(
984 struct t10_vpd *vpd,
985 unsigned char *p_buf,
986 int p_buf_len)
987{
988 unsigned char buf[VPD_TMP_BUF_SIZE];
989 int ret = 0;
990
991 memset(buf, 0, VPD_TMP_BUF_SIZE);
992
993 switch (vpd->device_identifier_code_set) {
994 case 0x01: /* Binary */
995 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
996 &vpd->device_identifier[0]);
997 break;
998 case 0x02: /* ASCII */
999 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1000 &vpd->device_identifier[0]);
1001 break;
1002 case 0x03: /* UTF-8 */
1003 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1004 &vpd->device_identifier[0]);
1005 break;
1006 default:
1007 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1008 " 0x%02x", vpd->device_identifier_code_set);
Andy Grovere3d6f902011-07-19 08:55:10 +00001009 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001010 break;
1011 }
1012
1013 if (p_buf)
1014 strncpy(p_buf, buf, p_buf_len);
1015 else
Andy Grover6708bb22011-06-08 10:36:43 -07001016 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001017
1018 return ret;
1019}
1020
1021int
1022transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1023{
1024 static const char hex_str[] = "0123456789abcdef";
1025 int j = 0, i = 4; /* offset to start of the identifer */
1026
1027 /*
1028 * The VPD Code Set (encoding)
1029 *
1030 * from spc3r23.pdf Section 7.6.3.1 Table 296
1031 */
1032 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1033 switch (vpd->device_identifier_code_set) {
1034 case 0x01: /* Binary */
1035 vpd->device_identifier[j++] =
1036 hex_str[vpd->device_identifier_type];
1037 while (i < (4 + page_83[3])) {
1038 vpd->device_identifier[j++] =
1039 hex_str[(page_83[i] & 0xf0) >> 4];
1040 vpd->device_identifier[j++] =
1041 hex_str[page_83[i] & 0x0f];
1042 i++;
1043 }
1044 break;
1045 case 0x02: /* ASCII */
1046 case 0x03: /* UTF-8 */
1047 while (i < (4 + page_83[3]))
1048 vpd->device_identifier[j++] = page_83[i++];
1049 break;
1050 default:
1051 break;
1052 }
1053
1054 return transport_dump_vpd_ident(vpd, NULL, 0);
1055}
1056EXPORT_SYMBOL(transport_set_vpd_ident);
1057
1058static void core_setup_task_attr_emulation(struct se_device *dev)
1059{
1060 /*
1061 * If this device is from Target_Core_Mod/pSCSI, disable the
1062 * SAM Task Attribute emulation.
1063 *
1064 * This is currently not available in upsream Linux/SCSI Target
1065 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1066 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001067 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001068 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1069 return;
1070 }
1071
1072 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
Andy Grover6708bb22011-06-08 10:36:43 -07001073 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001074 " device\n", dev->transport->name,
1075 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001076}
1077
1078static void scsi_dump_inquiry(struct se_device *dev)
1079{
Andy Grovere3d6f902011-07-19 08:55:10 +00001080 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001081 char buf[17];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001082 int i, device_type;
1083 /*
1084 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1085 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001086 for (i = 0; i < 8; i++)
1087 if (wwn->vendor[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001088 buf[i] = wwn->vendor[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001089 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001090 buf[i] = ' ';
1091 buf[i] = '\0';
1092 pr_debug(" Vendor: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001093
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001094 for (i = 0; i < 16; i++)
1095 if (wwn->model[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001096 buf[i] = wwn->model[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001097 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001098 buf[i] = ' ';
1099 buf[i] = '\0';
1100 pr_debug(" Model: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001101
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001102 for (i = 0; i < 4; i++)
1103 if (wwn->revision[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001104 buf[i] = wwn->revision[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001105 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001106 buf[i] = ' ';
1107 buf[i] = '\0';
1108 pr_debug(" Revision: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001109
Andy Grovere3d6f902011-07-19 08:55:10 +00001110 device_type = dev->transport->get_device_type(dev);
Andy Grover6708bb22011-06-08 10:36:43 -07001111 pr_debug(" Type: %s ", scsi_device_type(device_type));
1112 pr_debug(" ANSI SCSI revision: %02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001113 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001114}
1115
1116struct se_device *transport_add_device_to_core_hba(
1117 struct se_hba *hba,
1118 struct se_subsystem_api *transport,
1119 struct se_subsystem_dev *se_dev,
1120 u32 device_flags,
1121 void *transport_dev,
1122 struct se_dev_limits *dev_limits,
1123 const char *inquiry_prod,
1124 const char *inquiry_rev)
1125{
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001126 int force_pt;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001127 struct se_device *dev;
1128
1129 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001130 if (!dev) {
1131 pr_err("Unable to allocate memory for se_dev_t\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001132 return NULL;
1133 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001134
Andy Grovere3d6f902011-07-19 08:55:10 +00001135 transport_init_queue_obj(&dev->dev_queue_obj);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001136 dev->dev_flags = device_flags;
1137 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
Andy Grover5951146d2011-07-19 10:26:37 +00001138 dev->dev_ptr = transport_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001139 dev->se_hba = hba;
1140 dev->se_sub_dev = se_dev;
1141 dev->transport = transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001142 INIT_LIST_HEAD(&dev->dev_list);
1143 INIT_LIST_HEAD(&dev->dev_sep_list);
1144 INIT_LIST_HEAD(&dev->dev_tmr_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001145 INIT_LIST_HEAD(&dev->delayed_cmd_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001146 INIT_LIST_HEAD(&dev->state_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001147 INIT_LIST_HEAD(&dev->qf_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001148 spin_lock_init(&dev->execute_task_lock);
1149 spin_lock_init(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001150 spin_lock_init(&dev->dev_reservation_lock);
1151 spin_lock_init(&dev->dev_status_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001152 spin_lock_init(&dev->se_port_lock);
1153 spin_lock_init(&dev->se_tmr_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001154 spin_lock_init(&dev->qf_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001155 atomic_set(&dev->dev_ordered_id, 0);
1156
1157 se_dev_set_default_attribs(dev, dev_limits);
1158
1159 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1160 dev->creation_time = get_jiffies_64();
1161 spin_lock_init(&dev->stats_lock);
1162
1163 spin_lock(&hba->device_lock);
1164 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1165 hba->dev_count++;
1166 spin_unlock(&hba->device_lock);
1167 /*
1168 * Setup the SAM Task Attribute emulation for struct se_device
1169 */
1170 core_setup_task_attr_emulation(dev);
1171 /*
1172 * Force PR and ALUA passthrough emulation with internal object use.
1173 */
1174 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1175 /*
1176 * Setup the Reservations infrastructure for struct se_device
1177 */
1178 core_setup_reservations(dev, force_pt);
1179 /*
1180 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1181 */
1182 if (core_setup_alua(dev, force_pt) < 0)
1183 goto out;
1184
1185 /*
1186 * Startup the struct se_device processing thread
1187 */
1188 dev->process_thread = kthread_run(transport_processing_thread, dev,
Andy Grovere3d6f902011-07-19 08:55:10 +00001189 "LIO_%s", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001190 if (IS_ERR(dev->process_thread)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001191 pr_err("Unable to create kthread: LIO_%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001192 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001193 goto out;
1194 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001195 /*
1196 * Setup work_queue for QUEUE_FULL
1197 */
1198 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001199 /*
1200 * Preload the initial INQUIRY const values if we are doing
1201 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1202 * passthrough because this is being provided by the backend LLD.
1203 * This is required so that transport_get_inquiry() copies these
1204 * originals once back into DEV_T10_WWN(dev) for the virtual device
1205 * setup.
1206 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001207 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
Roland Dreierf22c1192011-05-02 22:15:37 -07001208 if (!inquiry_prod || !inquiry_rev) {
Andy Grover6708bb22011-06-08 10:36:43 -07001209 pr_err("All non TCM/pSCSI plugins require"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001210 " INQUIRY consts\n");
1211 goto out;
1212 }
1213
Andy Grovere3d6f902011-07-19 08:55:10 +00001214 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1215 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1216 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001217 }
1218 scsi_dump_inquiry(dev);
1219
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001220 return dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001221out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001222 kthread_stop(dev->process_thread);
1223
1224 spin_lock(&hba->device_lock);
1225 list_del(&dev->dev_list);
1226 hba->dev_count--;
1227 spin_unlock(&hba->device_lock);
1228
1229 se_release_vpd_for_dev(dev);
1230
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001231 kfree(dev);
1232
1233 return NULL;
1234}
1235EXPORT_SYMBOL(transport_add_device_to_core_hba);
1236
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001237int target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
Christoph Hellwig9b3b8042012-05-20 11:59:12 -04001238{
1239 struct se_device *dev = cmd->se_dev;
1240
1241 if (cmd->unknown_data_length) {
1242 cmd->data_length = size;
1243 } else if (size != cmd->data_length) {
1244 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
1245 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
1246 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
1247 cmd->data_length, size, cmd->t_task_cdb[0]);
1248
1249 cmd->cmd_spdtl = size;
1250
1251 if (cmd->data_direction == DMA_TO_DEVICE) {
1252 pr_err("Rejecting underflow/overflow"
1253 " WRITE data\n");
1254 goto out_invalid_cdb_field;
1255 }
1256 /*
1257 * Reject READ_* or WRITE_* with overflow/underflow for
1258 * type SCF_SCSI_DATA_CDB.
1259 */
1260 if (dev->se_sub_dev->se_dev_attrib.block_size != 512) {
1261 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
1262 " CDB on non 512-byte sector setup subsystem"
1263 " plugin: %s\n", dev->transport->name);
1264 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
1265 goto out_invalid_cdb_field;
1266 }
1267
1268 if (size > cmd->data_length) {
1269 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
1270 cmd->residual_count = (size - cmd->data_length);
1271 } else {
1272 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
1273 cmd->residual_count = (cmd->data_length - size);
1274 }
1275 cmd->data_length = size;
1276 }
1277
1278 return 0;
1279
1280out_invalid_cdb_field:
1281 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1282 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1283 return -EINVAL;
1284}
1285
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001286/*
1287 * Used by fabric modules containing a local struct se_cmd within their
1288 * fabric dependent per I/O descriptor.
1289 */
1290void transport_init_se_cmd(
1291 struct se_cmd *cmd,
1292 struct target_core_fabric_ops *tfo,
1293 struct se_session *se_sess,
1294 u32 data_length,
1295 int data_direction,
1296 int task_attr,
1297 unsigned char *sense_buffer)
1298{
Andy Grover5951146d2011-07-19 10:26:37 +00001299 INIT_LIST_HEAD(&cmd->se_lun_node);
1300 INIT_LIST_HEAD(&cmd->se_delayed_node);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001301 INIT_LIST_HEAD(&cmd->se_qf_node);
Roland Dreier79a7fef2011-09-28 22:12:07 -07001302 INIT_LIST_HEAD(&cmd->se_queue_node);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001303 INIT_LIST_HEAD(&cmd->se_cmd_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001304 INIT_LIST_HEAD(&cmd->state_list);
Andy Grovera1d8b492011-05-02 17:12:10 -07001305 init_completion(&cmd->transport_lun_fe_stop_comp);
1306 init_completion(&cmd->transport_lun_stop_comp);
1307 init_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001308 init_completion(&cmd->cmd_wait_comp);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001309 init_completion(&cmd->task_stop_comp);
Andy Grovera1d8b492011-05-02 17:12:10 -07001310 spin_lock_init(&cmd->t_state_lock);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001311 cmd->transport_state = CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312
1313 cmd->se_tfo = tfo;
1314 cmd->se_sess = se_sess;
1315 cmd->data_length = data_length;
1316 cmd->data_direction = data_direction;
1317 cmd->sam_task_attr = task_attr;
1318 cmd->sense_buffer = sense_buffer;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001319
1320 cmd->state_active = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001321}
1322EXPORT_SYMBOL(transport_init_se_cmd);
1323
1324static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1325{
1326 /*
1327 * Check if SAM Task Attribute emulation is enabled for this
1328 * struct se_device storage object
1329 */
Andy Grover5951146d2011-07-19 10:26:37 +00001330 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001331 return 0;
1332
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001333 if (cmd->sam_task_attr == MSG_ACA_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001334 pr_debug("SAM Task Attribute ACA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001335 " emulation is not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001336 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001337 }
1338 /*
1339 * Used to determine when ORDERED commands should go from
1340 * Dormant to Active status.
1341 */
Andy Grover5951146d2011-07-19 10:26:37 +00001342 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001343 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001344 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001345 cmd->se_ordered_id, cmd->sam_task_attr,
Andy Grover6708bb22011-06-08 10:36:43 -07001346 cmd->se_dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001347 return 0;
1348}
1349
Andy Grovera12f41f2012-04-03 15:51:20 -07001350/* target_setup_cmd_from_cdb():
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001351 *
1352 * Called from fabric RX Thread.
1353 */
Andy Grovera12f41f2012-04-03 15:51:20 -07001354int target_setup_cmd_from_cdb(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001355 struct se_cmd *cmd,
1356 unsigned char *cdb)
1357{
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001358 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
1359 u32 pr_reg_type = 0;
1360 u8 alua_ascq = 0;
1361 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001362 int ret;
1363
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001364 /*
1365 * Ensure that the received CDB is less than the max (252 + 8) bytes
1366 * for VARIABLE_LENGTH_CMD
1367 */
1368 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001369 pr_err("Received SCSI CDB with command_size: %d that"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001370 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1371 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001372 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1373 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grovere3d6f902011-07-19 08:55:10 +00001374 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001375 }
1376 /*
1377 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1378 * allocate the additional extended CDB buffer now.. Otherwise
1379 * setup the pointer from __t_task_cdb to t_task_cdb.
1380 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001381 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1382 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001383 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001384 if (!cmd->t_task_cdb) {
1385 pr_err("Unable to allocate cmd->t_task_cdb"
Andy Grovera1d8b492011-05-02 17:12:10 -07001386 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001387 scsi_command_size(cdb),
Andy Grovera1d8b492011-05-02 17:12:10 -07001388 (unsigned long)sizeof(cmd->__t_task_cdb));
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001389 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1390 cmd->scsi_sense_reason =
1391 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grovere3d6f902011-07-19 08:55:10 +00001392 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001393 }
1394 } else
Andy Grovera1d8b492011-05-02 17:12:10 -07001395 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001396 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07001397 * Copy the original CDB into cmd->
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001398 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001399 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001400
1401 /*
1402 * Check for an existing UNIT ATTENTION condition
1403 */
1404 if (core_scsi3_ua_check(cmd, cdb) < 0) {
1405 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1406 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
1407 return -EINVAL;
1408 }
1409
1410 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
1411 if (ret != 0) {
1412 /*
1413 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
1414 * The ALUA additional sense code qualifier (ASCQ) is determined
1415 * by the ALUA primary or secondary access state..
1416 */
1417 if (ret > 0) {
1418 pr_debug("[%s]: ALUA TG Port not available, "
1419 "SenseKey: NOT_READY, ASC/ASCQ: "
1420 "0x04/0x%02x\n",
1421 cmd->se_tfo->get_fabric_name(), alua_ascq);
1422
1423 transport_set_sense_codes(cmd, 0x04, alua_ascq);
1424 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1425 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
1426 return -EINVAL;
1427 }
1428 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1429 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1430 return -EINVAL;
1431 }
1432
1433 /*
1434 * Check status for SPC-3 Persistent Reservations
1435 */
1436 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type)) {
1437 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
1438 cmd, cdb, pr_reg_type) != 0) {
1439 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1440 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
1441 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1442 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
1443 return -EBUSY;
1444 }
1445 /*
1446 * This means the CDB is allowed for the SCSI Initiator port
1447 * when said port is *NOT* holding the legacy SPC-2 or
1448 * SPC-3 Persistent Reservation.
1449 */
1450 }
1451
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001452 ret = cmd->se_dev->transport->parse_cdb(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001453 if (ret < 0)
1454 return ret;
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001455
1456 spin_lock_irqsave(&cmd->t_state_lock, flags);
1457 cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1458 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1459
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001460 /*
1461 * Check for SAM Task Attribute Emulation
1462 */
1463 if (transport_check_alloc_task_attr(cmd) < 0) {
1464 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1465 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00001466 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001467 }
1468 spin_lock(&cmd->se_lun->lun_sep_lock);
1469 if (cmd->se_lun->lun_sep)
1470 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1471 spin_unlock(&cmd->se_lun->lun_sep_lock);
1472 return 0;
1473}
Andy Grovera12f41f2012-04-03 15:51:20 -07001474EXPORT_SYMBOL(target_setup_cmd_from_cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001475
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001476/*
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001477 * Used by fabric module frontends to queue tasks directly.
1478 * Many only be used from process context only
1479 */
1480int transport_handle_cdb_direct(
1481 struct se_cmd *cmd)
1482{
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001483 int ret;
1484
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001485 if (!cmd->se_lun) {
1486 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001487 pr_err("cmd->se_lun is NULL\n");
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001488 return -EINVAL;
1489 }
1490 if (in_interrupt()) {
1491 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001492 pr_err("transport_generic_handle_cdb cannot be called"
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001493 " from interrupt context\n");
1494 return -EINVAL;
1495 }
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001496 /*
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001497 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001498 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1499 * in existing usage to ensure that outstanding descriptors are handled
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07001500 * correctly during shutdown via transport_wait_for_tasks()
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001501 *
1502 * Also, we don't take cmd->t_state_lock here as we only expect
1503 * this to be called for initial descriptor submission.
1504 */
1505 cmd->t_state = TRANSPORT_NEW_CMD;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001506 cmd->transport_state |= CMD_T_ACTIVE;
1507
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001508 /*
1509 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1510 * so follow TRANSPORT_NEW_CMD processing thread context usage
1511 * and call transport_generic_request_failure() if necessary..
1512 */
1513 ret = transport_generic_new_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001514 if (ret < 0)
1515 transport_generic_request_failure(cmd);
1516
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001517 return 0;
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001518}
1519EXPORT_SYMBOL(transport_handle_cdb_direct);
1520
Nicholas Bellingera6360782011-11-18 20:36:22 -08001521/**
1522 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1523 *
1524 * @se_cmd: command descriptor to submit
1525 * @se_sess: associated se_sess for endpoint
1526 * @cdb: pointer to SCSI CDB
1527 * @sense: pointer to SCSI sense buffer
1528 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1529 * @data_length: fabric expected data transfer length
1530 * @task_addr: SAM task attribute
1531 * @data_dir: DMA data direction
1532 * @flags: flags for command submission from target_sc_flags_tables
1533 *
1534 * This may only be called from process context, and also currently
1535 * assumes internal allocation of fabric payload buffer by target-core.
1536 **/
Andy Grover1edcdb42012-01-19 13:39:23 -08001537void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
Nicholas Bellingera6360782011-11-18 20:36:22 -08001538 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1539 u32 data_length, int task_attr, int data_dir, int flags)
1540{
1541 struct se_portal_group *se_tpg;
1542 int rc;
1543
1544 se_tpg = se_sess->se_tpg;
1545 BUG_ON(!se_tpg);
1546 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1547 BUG_ON(in_interrupt());
1548 /*
1549 * Initialize se_cmd for target operation. From this point
1550 * exceptions are handled by sending exception status via
1551 * target_core_fabric_ops->queue_status() callback
1552 */
1553 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1554 data_length, data_dir, task_attr, sense);
Sebastian Andrzej Siewiorb0d79942012-01-10 14:16:59 +01001555 if (flags & TARGET_SCF_UNKNOWN_SIZE)
1556 se_cmd->unknown_data_length = 1;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001557 /*
1558 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1559 * se_sess->sess_cmd_list. A second kref_get here is necessary
1560 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1561 * kref_put() to happen during fabric packet acknowledgement.
1562 */
1563 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1564 /*
1565 * Signal bidirectional data payloads to target-core
1566 */
1567 if (flags & TARGET_SCF_BIDI_OP)
1568 se_cmd->se_cmd_flags |= SCF_BIDI;
1569 /*
1570 * Locate se_lun pointer and attach it to struct se_cmd
1571 */
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001572 if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1573 transport_send_check_condition_and_sense(se_cmd,
1574 se_cmd->scsi_sense_reason, 0);
1575 target_put_sess_cmd(se_sess, se_cmd);
1576 return;
1577 }
Christoph Hellwigd6e01752012-05-20 11:59:14 -04001578
Andy Grovera12f41f2012-04-03 15:51:20 -07001579 rc = target_setup_cmd_from_cdb(se_cmd, cdb);
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001580 if (rc != 0) {
1581 transport_generic_request_failure(se_cmd);
1582 return;
1583 }
Andy Grover11e319e2012-04-03 15:51:28 -07001584
1585 /*
1586 * Check if we need to delay processing because of ALUA
1587 * Active/NonOptimized primary access state..
1588 */
1589 core_alua_check_nonop_delay(se_cmd);
1590
Nicholas Bellingera6360782011-11-18 20:36:22 -08001591 transport_handle_cdb_direct(se_cmd);
Andy Grover1edcdb42012-01-19 13:39:23 -08001592 return;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001593}
1594EXPORT_SYMBOL(target_submit_cmd);
1595
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001596static void target_complete_tmr_failure(struct work_struct *work)
1597{
1598 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
1599
1600 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1601 se_cmd->se_tfo->queue_tm_rsp(se_cmd);
1602 transport_generic_free_cmd(se_cmd, 0);
1603}
1604
Andy Groverea98d7f2012-01-19 13:39:21 -08001605/**
1606 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1607 * for TMR CDBs
1608 *
1609 * @se_cmd: command descriptor to submit
1610 * @se_sess: associated se_sess for endpoint
1611 * @sense: pointer to SCSI sense buffer
1612 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1613 * @fabric_context: fabric context for TMR req
1614 * @tm_type: Type of TM request
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001615 * @gfp: gfp type for caller
1616 * @tag: referenced task tag for TMR_ABORT_TASK
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001617 * @flags: submit cmd flags
Andy Groverea98d7f2012-01-19 13:39:21 -08001618 *
1619 * Callable from all contexts.
1620 **/
1621
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001622int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
Andy Groverea98d7f2012-01-19 13:39:21 -08001623 unsigned char *sense, u32 unpacked_lun,
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001624 void *fabric_tmr_ptr, unsigned char tm_type,
1625 gfp_t gfp, unsigned int tag, int flags)
Andy Groverea98d7f2012-01-19 13:39:21 -08001626{
1627 struct se_portal_group *se_tpg;
1628 int ret;
1629
1630 se_tpg = se_sess->se_tpg;
1631 BUG_ON(!se_tpg);
1632
1633 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1634 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001635 /*
1636 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1637 * allocation failure.
1638 */
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001639 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001640 if (ret < 0)
1641 return -ENOMEM;
Andy Groverea98d7f2012-01-19 13:39:21 -08001642
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001643 if (tm_type == TMR_ABORT_TASK)
1644 se_cmd->se_tmr_req->ref_task_tag = tag;
1645
Andy Groverea98d7f2012-01-19 13:39:21 -08001646 /* See target_submit_cmd for commentary */
1647 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1648
Andy Groverea98d7f2012-01-19 13:39:21 -08001649 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1650 if (ret) {
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001651 /*
1652 * For callback during failure handling, push this work off
1653 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1654 */
1655 INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
1656 schedule_work(&se_cmd->work);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001657 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001658 }
1659 transport_generic_handle_tmr(se_cmd);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001660 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001661}
1662EXPORT_SYMBOL(target_submit_tmr);
1663
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001664/* transport_generic_handle_tmr():
1665 *
1666 *
1667 */
1668int transport_generic_handle_tmr(
1669 struct se_cmd *cmd)
1670{
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001671 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001672 return 0;
1673}
1674EXPORT_SYMBOL(transport_generic_handle_tmr);
1675
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001676/*
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001677 * If the cmd is active, request it to be stopped and sleep until it
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001678 * has completed.
1679 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001680bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001681{
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001682 bool was_active = false;
1683
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001684 if (cmd->transport_state & CMD_T_BUSY) {
1685 cmd->transport_state |= CMD_T_REQUEST_STOP;
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001686 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1687
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001688 pr_debug("cmd %p waiting to complete\n", cmd);
1689 wait_for_completion(&cmd->task_stop_comp);
1690 pr_debug("cmd %p stopped successfully\n", cmd);
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001691
1692 spin_lock_irqsave(&cmd->t_state_lock, *flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001693 cmd->transport_state &= ~CMD_T_REQUEST_STOP;
1694 cmd->transport_state &= ~CMD_T_BUSY;
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001695 was_active = true;
1696 }
1697
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001698 return was_active;
1699}
1700
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001701/*
1702 * Handle SAM-esque emulation for generic transport request failures.
1703 */
Nicholas Bellinger2fbff1272012-02-10 16:18:11 -08001704void transport_generic_request_failure(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001705{
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001706 int ret = 0;
1707
Andy Grover6708bb22011-06-08 10:36:43 -07001708 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001709 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -07001710 cmd->t_task_cdb[0]);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001711 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001712 cmd->se_tfo->get_cmd_state(cmd),
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001713 cmd->t_state, cmd->scsi_sense_reason);
Christoph Hellwigd43d6ae2012-04-24 00:25:08 -04001714 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001715 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1716 (cmd->transport_state & CMD_T_STOP) != 0,
1717 (cmd->transport_state & CMD_T_SENT) != 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001718
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001719 /*
1720 * For SAM Task Attribute emulation for failed struct se_cmd
1721 */
1722 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1723 transport_complete_task_attr(cmd);
1724
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001725 switch (cmd->scsi_sense_reason) {
1726 case TCM_NON_EXISTENT_LUN:
1727 case TCM_UNSUPPORTED_SCSI_OPCODE:
1728 case TCM_INVALID_CDB_FIELD:
1729 case TCM_INVALID_PARAMETER_LIST:
1730 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1731 case TCM_UNKNOWN_MODE_PAGE:
1732 case TCM_WRITE_PROTECTED:
1733 case TCM_CHECK_CONDITION_ABORT_CMD:
1734 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1735 case TCM_CHECK_CONDITION_NOT_READY:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001736 break;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001737 case TCM_RESERVATION_CONFLICT:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001738 /*
1739 * No SENSE Data payload for this case, set SCSI Status
1740 * and queue the response to $FABRIC_MOD.
1741 *
1742 * Uses linux/include/scsi/scsi.h SAM status codes defs
1743 */
1744 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1745 /*
1746 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1747 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1748 * CONFLICT STATUS.
1749 *
1750 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1751 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001752 if (cmd->se_sess &&
1753 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1754 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001755 cmd->orig_fe_lun, 0x2C,
1756 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1757
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001758 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001759 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001760 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001761 goto check_stop;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001762 default:
Andy Grover6708bb22011-06-08 10:36:43 -07001763 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001764 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001765 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1766 break;
1767 }
Christoph Hellwigf3146432012-07-08 15:58:48 -04001768
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001769 ret = transport_send_check_condition_and_sense(cmd,
1770 cmd->scsi_sense_reason, 0);
1771 if (ret == -EAGAIN || ret == -ENOMEM)
1772 goto queue_full;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001773
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001774check_stop:
1775 transport_lun_remove_cmd(cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07001776 if (!transport_cmd_check_stop_to_fabric(cmd))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001777 ;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001778 return;
1779
1780queue_full:
Christoph Hellwige057f532011-10-17 13:56:41 -04001781 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1782 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001783}
Nicholas Bellinger2fbff1272012-02-10 16:18:11 -08001784EXPORT_SYMBOL(transport_generic_request_failure);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001785
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001786static void __target_execute_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001787{
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -04001788 int error = 0;
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001789
1790 spin_lock_irq(&cmd->t_state_lock);
1791 cmd->transport_state |= (CMD_T_BUSY|CMD_T_SENT);
1792 spin_unlock_irq(&cmd->t_state_lock);
1793
1794 if (cmd->execute_cmd)
1795 error = cmd->execute_cmd(cmd);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001796
1797 if (error) {
1798 spin_lock_irq(&cmd->t_state_lock);
1799 cmd->transport_state &= ~(CMD_T_BUSY|CMD_T_SENT);
1800 spin_unlock_irq(&cmd->t_state_lock);
1801
1802 transport_generic_request_failure(cmd);
1803 }
1804}
1805
Christoph Hellwig70baf0a2012-07-08 15:58:39 -04001806void target_execute_cmd(struct se_cmd *cmd)
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001807{
1808 struct se_device *dev = cmd->se_dev;
1809
Christoph Hellwigf7113a42012-07-08 15:58:38 -04001810 /*
Christoph Hellwigd59a02b2012-07-08 15:58:40 -04001811 * If the received CDB has aleady been aborted stop processing it here.
1812 */
1813 if (transport_check_aborted_status(cmd, 1))
1814 return;
1815
1816 /*
Christoph Hellwigf7113a42012-07-08 15:58:38 -04001817 * Determine if IOCTL context caller in requesting the stopping of this
1818 * command for LUN shutdown purposes.
1819 */
1820 spin_lock_irq(&cmd->t_state_lock);
1821 if (cmd->transport_state & CMD_T_LUN_STOP) {
1822 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
1823 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
1824
1825 cmd->transport_state &= ~CMD_T_ACTIVE;
1826 spin_unlock_irq(&cmd->t_state_lock);
1827 complete(&cmd->transport_lun_stop_comp);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001828 return;
Christoph Hellwigf7113a42012-07-08 15:58:38 -04001829 }
1830 /*
1831 * Determine if frontend context caller is requesting the stopping of
1832 * this command for frontend exceptions.
1833 */
1834 if (cmd->transport_state & CMD_T_STOP) {
1835 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
1836 __func__, __LINE__,
1837 cmd->se_tfo->get_task_tag(cmd));
1838
1839 spin_unlock_irq(&cmd->t_state_lock);
1840 complete(&cmd->t_transport_stop_comp);
1841 return;
1842 }
1843
1844 cmd->t_state = TRANSPORT_PROCESSING;
1845 spin_unlock_irq(&cmd->t_state_lock);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001846
1847 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1848 goto execute;
1849
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001850 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001851 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001852 * to allow the passed struct se_cmd list of tasks to the front of the list.
1853 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001854 switch (cmd->sam_task_attr) {
1855 case MSG_HEAD_TAG:
1856 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, "
1857 "se_ordered_id: %u\n",
1858 cmd->t_task_cdb[0], cmd->se_ordered_id);
1859 goto execute;
1860 case MSG_ORDERED_TAG:
1861 atomic_inc(&dev->dev_ordered_sync);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001862 smp_mb__after_atomic_inc();
1863
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001864 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, "
1865 " se_ordered_id: %u\n",
1866 cmd->t_task_cdb[0], cmd->se_ordered_id);
1867
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001868 /*
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001869 * Execute an ORDERED command if no other older commands
1870 * exist that need to be completed first.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001871 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001872 if (!atomic_read(&dev->simple_cmds))
1873 goto execute;
1874 break;
1875 default:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001876 /*
1877 * For SIMPLE and UNTAGGED Task Attribute commands
1878 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001879 atomic_inc(&dev->simple_cmds);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001880 smp_mb__after_atomic_inc();
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001881 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001882 }
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001883
1884 if (atomic_read(&dev->dev_ordered_sync) != 0) {
1885 spin_lock(&dev->delayed_cmd_lock);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001886 list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list);
1887 spin_unlock(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001888
Andy Grover6708bb22011-06-08 10:36:43 -07001889 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001890 " delayed CMD list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07001891 cmd->t_task_cdb[0], cmd->sam_task_attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001892 cmd->se_ordered_id);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001893 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001894 }
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001895
1896execute:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001897 /*
1898 * Otherwise, no ORDERED task attributes exist..
1899 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001900 __target_execute_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001901}
Christoph Hellwig70baf0a2012-07-08 15:58:39 -04001902EXPORT_SYMBOL(target_execute_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001903
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001904/*
1905 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
1906 */
1907static int transport_get_sense_data(struct se_cmd *cmd)
1908{
1909 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
Christoph Hellwig42bf8292011-10-12 11:07:00 -04001910 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001911 unsigned long flags;
1912 u32 offset = 0;
1913
Andy Grovere3d6f902011-07-19 08:55:10 +00001914 WARN_ON(!cmd->se_lun);
1915
Christoph Hellwig42bf8292011-10-12 11:07:00 -04001916 if (!dev)
1917 return 0;
1918
Andy Grovera1d8b492011-05-02 17:12:10 -07001919 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001920 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07001921 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001922 return 0;
1923 }
1924
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001925 if (!(cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
1926 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001927
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001928 if (!dev->transport->get_sense_buffer) {
1929 pr_err("dev->transport->get_sense_buffer is NULL\n");
1930 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001931 }
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001932
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001933 sense_buffer = dev->transport->get_sense_buffer(cmd);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001934 if (!sense_buffer) {
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001935 pr_err("ITT 0x%08x cmd %p: Unable to locate"
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001936 " sense buffer for task with sense\n",
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001937 cmd->se_tfo->get_task_tag(cmd), cmd);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001938 goto out;
1939 }
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001940
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001941 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1942
1943 offset = cmd->se_tfo->set_fabric_sense_len(cmd, TRANSPORT_SENSE_BUFFER);
1944
1945 memcpy(&buffer[offset], sense_buffer, TRANSPORT_SENSE_BUFFER);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001946
1947 /* Automatically padded */
1948 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
1949
1950 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x and sense\n",
1951 dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
1952 return 0;
1953
Christoph Hellwig785fdf72012-04-24 00:25:04 -04001954out:
Andy Grovera1d8b492011-05-02 17:12:10 -07001955 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001956 return -1;
1957}
1958
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001959/*
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001960 * Process all commands up to the last received ORDERED task attribute which
1961 * requires another blocking boundary
1962 */
1963static void target_restart_delayed_cmds(struct se_device *dev)
1964{
1965 for (;;) {
1966 struct se_cmd *cmd;
1967
1968 spin_lock(&dev->delayed_cmd_lock);
1969 if (list_empty(&dev->delayed_cmd_list)) {
1970 spin_unlock(&dev->delayed_cmd_lock);
1971 break;
1972 }
1973
1974 cmd = list_entry(dev->delayed_cmd_list.next,
1975 struct se_cmd, se_delayed_node);
1976 list_del(&cmd->se_delayed_node);
1977 spin_unlock(&dev->delayed_cmd_lock);
1978
1979 __target_execute_cmd(cmd);
1980
1981 if (cmd->sam_task_attr == MSG_ORDERED_TAG)
1982 break;
1983 }
1984}
1985
1986/*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04001987 * Called from I/O completion to determine which dormant/delayed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001988 * and ordered cmds need to have their tasks added to the execution queue.
1989 */
1990static void transport_complete_task_attr(struct se_cmd *cmd)
1991{
Andy Grover5951146d2011-07-19 10:26:37 +00001992 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001993
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001994 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001995 atomic_dec(&dev->simple_cmds);
1996 smp_mb__after_atomic_dec();
1997 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07001998 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001999 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
2000 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002001 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002002 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07002003 pr_debug("Incremented dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002004 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
2005 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002006 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002007 atomic_dec(&dev->dev_ordered_sync);
2008 smp_mb__after_atomic_dec();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002009
2010 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07002011 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002012 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
2013 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002014
Christoph Hellwig5f41a312012-05-20 14:34:44 -04002015 target_restart_delayed_cmds(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002016}
2017
Christoph Hellwige057f532011-10-17 13:56:41 -04002018static void transport_complete_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002019{
2020 int ret = 0;
2021
Christoph Hellwige057f532011-10-17 13:56:41 -04002022 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2023 transport_complete_task_attr(cmd);
2024
2025 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
2026 ret = cmd->se_tfo->queue_status(cmd);
2027 if (ret)
2028 goto out;
2029 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002030
2031 switch (cmd->data_direction) {
2032 case DMA_FROM_DEVICE:
2033 ret = cmd->se_tfo->queue_data_in(cmd);
2034 break;
2035 case DMA_TO_DEVICE:
Andy Groverec98f782011-07-20 19:28:46 +00002036 if (cmd->t_bidi_data_sg) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002037 ret = cmd->se_tfo->queue_data_in(cmd);
2038 if (ret < 0)
Christoph Hellwige057f532011-10-17 13:56:41 -04002039 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002040 }
2041 /* Fall through for DMA_TO_DEVICE */
2042 case DMA_NONE:
2043 ret = cmd->se_tfo->queue_status(cmd);
2044 break;
2045 default:
2046 break;
2047 }
2048
Christoph Hellwige057f532011-10-17 13:56:41 -04002049out:
2050 if (ret < 0) {
2051 transport_handle_queue_full(cmd, cmd->se_dev);
2052 return;
2053 }
2054 transport_lun_remove_cmd(cmd);
2055 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002056}
2057
2058static void transport_handle_queue_full(
2059 struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -04002060 struct se_device *dev)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002061{
2062 spin_lock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002063 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
2064 atomic_inc(&dev->dev_qf_count);
2065 smp_mb__after_atomic_inc();
2066 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
2067
2068 schedule_work(&cmd->se_dev->qf_work_queue);
2069}
2070
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002071static void target_complete_ok_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002072{
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002073 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002074 int reason = 0, ret;
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002075
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002076 /*
2077 * Check if we need to move delayed/dormant tasks from cmds on the
2078 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
2079 * Attribute.
2080 */
Andy Grover5951146d2011-07-19 10:26:37 +00002081 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002082 transport_complete_task_attr(cmd);
2083 /*
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002084 * Check to schedule QUEUE_FULL work, or execute an existing
2085 * cmd->transport_qf_callback()
2086 */
2087 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
2088 schedule_work(&cmd->se_dev->qf_work_queue);
2089
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002090 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002091 * Check if we need to retrieve a sense buffer from
2092 * the struct se_cmd in question.
2093 */
2094 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
2095 if (transport_get_sense_data(cmd) < 0)
2096 reason = TCM_NON_EXISTENT_LUN;
2097
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002098 if (cmd->scsi_status) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002099 ret = transport_send_check_condition_and_sense(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002100 cmd, reason, 1);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002101 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002102 goto queue_full;
2103
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002104 transport_lun_remove_cmd(cmd);
2105 transport_cmd_check_stop_to_fabric(cmd);
2106 return;
2107 }
2108 }
2109 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002110 * Check for a callback, used by amongst other things
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002111 * XDWRITE_READ_10 emulation.
2112 */
2113 if (cmd->transport_complete_callback)
2114 cmd->transport_complete_callback(cmd);
2115
2116 switch (cmd->data_direction) {
2117 case DMA_FROM_DEVICE:
2118 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00002119 if (cmd->se_lun->lun_sep) {
2120 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002121 cmd->data_length;
2122 }
2123 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002124
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002125 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002126 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002127 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002128 break;
2129 case DMA_TO_DEVICE:
2130 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00002131 if (cmd->se_lun->lun_sep) {
2132 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002133 cmd->data_length;
2134 }
2135 spin_unlock(&cmd->se_lun->lun_sep_lock);
2136 /*
2137 * Check if we need to send READ payload for BIDI-COMMAND
2138 */
Andy Groverec98f782011-07-20 19:28:46 +00002139 if (cmd->t_bidi_data_sg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002140 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00002141 if (cmd->se_lun->lun_sep) {
2142 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002143 cmd->data_length;
2144 }
2145 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002146 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002147 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002148 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002149 break;
2150 }
2151 /* Fall through for DMA_TO_DEVICE */
2152 case DMA_NONE:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002153 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002154 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002155 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002156 break;
2157 default:
2158 break;
2159 }
2160
2161 transport_lun_remove_cmd(cmd);
2162 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002163 return;
2164
2165queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07002166 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002167 " data_direction: %d\n", cmd, cmd->data_direction);
Christoph Hellwige057f532011-10-17 13:56:41 -04002168 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
2169 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002170}
2171
Andy Grover6708bb22011-06-08 10:36:43 -07002172static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002173{
Andy Groverec98f782011-07-20 19:28:46 +00002174 struct scatterlist *sg;
Andy Groverec98f782011-07-20 19:28:46 +00002175 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002176
Andy Grover6708bb22011-06-08 10:36:43 -07002177 for_each_sg(sgl, sg, nents, count)
2178 __free_page(sg_page(sg));
2179
2180 kfree(sgl);
2181}
2182
2183static inline void transport_free_pages(struct se_cmd *cmd)
2184{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002185 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
Andy Grover6708bb22011-06-08 10:36:43 -07002186 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002187
Andy Grover6708bb22011-06-08 10:36:43 -07002188 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00002189 cmd->t_data_sg = NULL;
2190 cmd->t_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002191
Andy Grover6708bb22011-06-08 10:36:43 -07002192 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00002193 cmd->t_bidi_data_sg = NULL;
2194 cmd->t_bidi_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002195}
2196
Christoph Hellwigd3df7822011-09-13 23:08:32 +02002197/**
Christoph Hellwige26d99a2011-11-14 12:30:30 -05002198 * transport_release_cmd - free a command
2199 * @cmd: command to free
2200 *
2201 * This routine unconditionally frees a command, and reference counting
2202 * or list removal must be done in the caller.
2203 */
2204static void transport_release_cmd(struct se_cmd *cmd)
2205{
2206 BUG_ON(!cmd->se_tfo);
2207
Andy Groverc8e31f22012-01-19 13:39:17 -08002208 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Christoph Hellwige26d99a2011-11-14 12:30:30 -05002209 core_tmr_release_req(cmd->se_tmr_req);
2210 if (cmd->t_task_cdb != cmd->__t_task_cdb)
2211 kfree(cmd->t_task_cdb);
2212 /*
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002213 * If this cmd has been setup with target_get_sess_cmd(), drop
2214 * the kref and call ->release_cmd() in kref callback.
Christoph Hellwige26d99a2011-11-14 12:30:30 -05002215 */
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002216 if (cmd->check_release != 0) {
2217 target_put_sess_cmd(cmd->se_sess, cmd);
2218 return;
2219 }
Christoph Hellwige26d99a2011-11-14 12:30:30 -05002220 cmd->se_tfo->release_cmd(cmd);
2221}
2222
2223/**
Christoph Hellwigd3df7822011-09-13 23:08:32 +02002224 * transport_put_cmd - release a reference to a command
2225 * @cmd: command to release
2226 *
2227 * This routine releases our reference to the command and frees it if possible.
2228 */
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002229static void transport_put_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002230{
2231 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002232
Andy Grovera1d8b492011-05-02 17:12:10 -07002233 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02002234 if (atomic_read(&cmd->t_fe_count)) {
2235 if (!atomic_dec_and_test(&cmd->t_fe_count))
2236 goto out_busy;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002237 }
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02002238
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002239 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
2240 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002241 target_remove_from_state_list(cmd);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02002242 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002243 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002244
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002245 transport_free_pages(cmd);
Christoph Hellwig31afc392011-09-13 23:08:11 +02002246 transport_release_cmd(cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002247 return;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02002248out_busy:
2249 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002250}
2251
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002252/*
Andy Groverec98f782011-07-20 19:28:46 +00002253 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
2254 * allocating in the core.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002255 * @cmd: Associated se_cmd descriptor
2256 * @mem: SGL style memory for TCM WRITE / READ
2257 * @sg_mem_num: Number of SGL elements
2258 * @mem_bidi_in: SGL style memory for TCM BIDI READ
2259 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
2260 *
2261 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
2262 * of parameters.
2263 */
2264int transport_generic_map_mem_to_cmd(
2265 struct se_cmd *cmd,
Andy Grover5951146d2011-07-19 10:26:37 +00002266 struct scatterlist *sgl,
2267 u32 sgl_count,
2268 struct scatterlist *sgl_bidi,
2269 u32 sgl_bidi_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002270{
Andy Grover5951146d2011-07-19 10:26:37 +00002271 if (!sgl || !sgl_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002272 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002273
Christoph Hellwig64f1db32012-05-20 11:59:11 -04002274 /*
2275 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
2276 * scatterlists already have been set to follow what the fabric
2277 * passes for the original expected data transfer length.
2278 */
2279 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2280 pr_warn("Rejecting SCSI DATA overflow for fabric using"
2281 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
2282 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2283 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2284 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002285 }
2286
Christoph Hellwig64f1db32012-05-20 11:59:11 -04002287 cmd->t_data_sg = sgl;
2288 cmd->t_data_nents = sgl_count;
2289
2290 if (sgl_bidi && sgl_bidi_count) {
2291 cmd->t_bidi_data_sg = sgl_bidi;
2292 cmd->t_bidi_data_nents = sgl_bidi_count;
2293 }
2294 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002295 return 0;
2296}
2297EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
2298
Andy Grover49493142012-01-16 16:57:08 -08002299void *transport_kmap_data_sg(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002300{
Andy Groverec98f782011-07-20 19:28:46 +00002301 struct scatterlist *sg = cmd->t_data_sg;
Andy Grover49493142012-01-16 16:57:08 -08002302 struct page **pages;
2303 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002304
Andy Groverec98f782011-07-20 19:28:46 +00002305 BUG_ON(!sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002306 /*
Andy Groverec98f782011-07-20 19:28:46 +00002307 * We need to take into account a possible offset here for fabrics like
2308 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
2309 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
Andy Grover05d1c7c2011-07-20 19:13:28 +00002310 */
Andy Grover49493142012-01-16 16:57:08 -08002311 if (!cmd->t_data_nents)
2312 return NULL;
2313 else if (cmd->t_data_nents == 1)
2314 return kmap(sg_page(sg)) + sg->offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002315
Andy Grover49493142012-01-16 16:57:08 -08002316 /* >1 page. use vmap */
2317 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
2318 if (!pages)
2319 return NULL;
2320
2321 /* convert sg[] to pages[] */
2322 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
2323 pages[i] = sg_page(sg);
2324 }
2325
2326 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
2327 kfree(pages);
2328 if (!cmd->t_data_vmap)
2329 return NULL;
2330
2331 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002332}
Andy Grover49493142012-01-16 16:57:08 -08002333EXPORT_SYMBOL(transport_kmap_data_sg);
2334
2335void transport_kunmap_data_sg(struct se_cmd *cmd)
2336{
Andy Grovera1edf9c2012-02-09 12:18:06 -08002337 if (!cmd->t_data_nents) {
Andy Grover49493142012-01-16 16:57:08 -08002338 return;
Andy Grovera1edf9c2012-02-09 12:18:06 -08002339 } else if (cmd->t_data_nents == 1) {
Andy Grover49493142012-01-16 16:57:08 -08002340 kunmap(sg_page(cmd->t_data_sg));
Andy Grovera1edf9c2012-02-09 12:18:06 -08002341 return;
2342 }
Andy Grover49493142012-01-16 16:57:08 -08002343
2344 vunmap(cmd->t_data_vmap);
2345 cmd->t_data_vmap = NULL;
2346}
2347EXPORT_SYMBOL(transport_kunmap_data_sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002348
2349static int
2350transport_generic_get_mem(struct se_cmd *cmd)
2351{
Andy Groverec98f782011-07-20 19:28:46 +00002352 u32 length = cmd->data_length;
2353 unsigned int nents;
2354 struct page *page;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08002355 gfp_t zero_flag;
Andy Groverec98f782011-07-20 19:28:46 +00002356 int i = 0;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002357
Andy Groverec98f782011-07-20 19:28:46 +00002358 nents = DIV_ROUND_UP(length, PAGE_SIZE);
2359 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
2360 if (!cmd->t_data_sg)
Andy Grovere3d6f902011-07-19 08:55:10 +00002361 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002362
Andy Groverec98f782011-07-20 19:28:46 +00002363 cmd->t_data_nents = nents;
2364 sg_init_table(cmd->t_data_sg, nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002365
Christoph Hellwig64f1db32012-05-20 11:59:11 -04002366 zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_CDB ? 0 : __GFP_ZERO;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08002367
Andy Groverec98f782011-07-20 19:28:46 +00002368 while (length) {
2369 u32 page_len = min_t(u32, length, PAGE_SIZE);
roland@purestorage.com9db9da32012-01-04 15:59:58 -08002370 page = alloc_page(GFP_KERNEL | zero_flag);
Andy Groverec98f782011-07-20 19:28:46 +00002371 if (!page)
2372 goto out;
2373
2374 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
2375 length -= page_len;
2376 i++;
2377 }
2378 return 0;
2379
2380out:
2381 while (i >= 0) {
2382 __free_page(sg_page(&cmd->t_data_sg[i]));
2383 i--;
2384 }
2385 kfree(cmd->t_data_sg);
2386 cmd->t_data_sg = NULL;
2387 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002388}
2389
Andy Grovera1d8b492011-05-02 17:12:10 -07002390/*
Andy Groverb16a35b2012-04-03 15:51:21 -07002391 * Allocate any required resources to execute the command. For writes we
2392 * might not have the payload yet, so notify the fabric via a call to
2393 * ->write_pending instead. Otherwise place it on the execution queue.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002394 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002395int transport_generic_new_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002396{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002397 int ret = 0;
2398
2399 /*
2400 * Determine is the TCM fabric module has already allocated physical
2401 * memory, and is directly calling transport_generic_map_mem_to_cmd()
Andy Groverec98f782011-07-20 19:28:46 +00002402 * beforehand.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002403 */
Andy Groverec98f782011-07-20 19:28:46 +00002404 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
2405 cmd->data_length) {
Andy Grover05d1c7c2011-07-20 19:13:28 +00002406 ret = transport_generic_get_mem(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002407 if (ret < 0)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002408 goto out_fail;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002409 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04002410
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04002411 /* Workaround for handling zero-length control CDBs */
Christoph Hellwig64f1db32012-05-20 11:59:11 -04002412 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && !cmd->data_length) {
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002413 spin_lock_irq(&cmd->t_state_lock);
Roland Dreier410f6702011-11-22 13:51:32 -08002414 cmd->t_state = TRANSPORT_COMPLETE;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002415 cmd->transport_state |= CMD_T_ACTIVE;
2416 spin_unlock_irq(&cmd->t_state_lock);
Nicholas Bellinger91ec1d32012-01-13 12:01:34 -08002417
2418 if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
2419 u8 ua_asc = 0, ua_ascq = 0;
2420
2421 core_scsi3_ua_clear_for_request_sense(cmd,
2422 &ua_asc, &ua_ascq);
2423 }
2424
Roland Dreier410f6702011-11-22 13:51:32 -08002425 INIT_WORK(&cmd->work, target_complete_ok_work);
2426 queue_work(target_completion_wq, &cmd->work);
2427 return 0;
2428 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04002429
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04002430 atomic_inc(&cmd->t_fe_count);
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04002431
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002432 /*
Christoph Hellwigc3196f02012-07-08 15:58:41 -04002433 * If this command is not a write we can execute it right here,
2434 * for write buffers we need to notify the fabric driver first
2435 * and let it call back once the write buffers are ready.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002436 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04002437 target_add_to_state_list(cmd);
Christoph Hellwigc3196f02012-07-08 15:58:41 -04002438 if (cmd->data_direction != DMA_TO_DEVICE) {
2439 target_execute_cmd(cmd);
2440 return 0;
2441 }
2442
2443 spin_lock_irq(&cmd->t_state_lock);
2444 cmd->t_state = TRANSPORT_WRITE_PENDING;
2445 spin_unlock_irq(&cmd->t_state_lock);
2446
2447 transport_cmd_check_stop(cmd, false);
2448
2449 ret = cmd->se_tfo->write_pending(cmd);
2450 if (ret == -EAGAIN || ret == -ENOMEM)
2451 goto queue_full;
2452
2453 if (ret < 0)
2454 return ret;
2455 return 1;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04002456
2457out_fail:
2458 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2459 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2460 return -EINVAL;
Christoph Hellwigc3196f02012-07-08 15:58:41 -04002461queue_full:
2462 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
2463 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
2464 transport_handle_queue_full(cmd, cmd->se_dev);
2465 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002466}
Andy Grovera1d8b492011-05-02 17:12:10 -07002467EXPORT_SYMBOL(transport_generic_new_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002468
Christoph Hellwige057f532011-10-17 13:56:41 -04002469static void transport_write_pending_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002470{
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002471 int ret;
2472
2473 ret = cmd->se_tfo->write_pending(cmd);
2474 if (ret == -EAGAIN || ret == -ENOMEM) {
Christoph Hellwige057f532011-10-17 13:56:41 -04002475 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
2476 cmd);
2477 transport_handle_queue_full(cmd, cmd->se_dev);
2478 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002479}
2480
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002481void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002482{
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002483 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
Andy Groverc8e31f22012-01-19 13:39:17 -08002484 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002485 transport_wait_for_tasks(cmd);
2486
Christoph Hellwig35462972011-05-31 23:56:57 -04002487 transport_release_cmd(cmd);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002488 } else {
2489 if (wait_for_tasks)
2490 transport_wait_for_tasks(cmd);
2491
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002492 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
2493
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +02002494 if (cmd->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002495 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002496
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002497 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002498 }
2499}
2500EXPORT_SYMBOL(transport_generic_free_cmd);
2501
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002502/* target_get_sess_cmd - Add command to active ->sess_cmd_list
2503 * @se_sess: session to reference
2504 * @se_cmd: command descriptor to add
Nicholas Bellingera6360782011-11-18 20:36:22 -08002505 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002506 */
Nicholas Bellingera6360782011-11-18 20:36:22 -08002507void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
2508 bool ack_kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002509{
2510 unsigned long flags;
2511
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002512 kref_init(&se_cmd->cmd_kref);
Nicholas Bellingera6360782011-11-18 20:36:22 -08002513 /*
2514 * Add a second kref if the fabric caller is expecting to handle
2515 * fabric acknowledgement that requires two target_put_sess_cmd()
2516 * invocations before se_cmd descriptor release.
2517 */
Nicholas Bellinger86715562012-02-13 01:07:22 -08002518 if (ack_kref == true) {
Nicholas Bellingera6360782011-11-18 20:36:22 -08002519 kref_get(&se_cmd->cmd_kref);
Nicholas Bellinger86715562012-02-13 01:07:22 -08002520 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
2521 }
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002522
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002523 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2524 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
2525 se_cmd->check_release = 1;
2526 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2527}
2528EXPORT_SYMBOL(target_get_sess_cmd);
2529
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002530static void target_release_cmd_kref(struct kref *kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002531{
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002532 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
2533 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002534 unsigned long flags;
2535
2536 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2537 if (list_empty(&se_cmd->se_cmd_list)) {
2538 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Nicholas Bellingerffc32d52012-02-13 02:35:01 -08002539 se_cmd->se_tfo->release_cmd(se_cmd);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002540 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002541 }
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002542 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
2543 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2544 complete(&se_cmd->cmd_wait_comp);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002545 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002546 }
2547 list_del(&se_cmd->se_cmd_list);
2548 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2549
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002550 se_cmd->se_tfo->release_cmd(se_cmd);
2551}
2552
2553/* target_put_sess_cmd - Check for active I/O shutdown via kref_put
2554 * @se_sess: session to reference
2555 * @se_cmd: command descriptor to drop
2556 */
2557int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
2558{
2559 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002560}
2561EXPORT_SYMBOL(target_put_sess_cmd);
2562
2563/* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
2564 * @se_sess: session to split
2565 */
2566void target_splice_sess_cmd_list(struct se_session *se_sess)
2567{
2568 struct se_cmd *se_cmd;
2569 unsigned long flags;
2570
2571 WARN_ON(!list_empty(&se_sess->sess_wait_list));
2572 INIT_LIST_HEAD(&se_sess->sess_wait_list);
2573
2574 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2575 se_sess->sess_tearing_down = 1;
2576
2577 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
2578
2579 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
2580 se_cmd->cmd_wait_set = 1;
2581
2582 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2583}
2584EXPORT_SYMBOL(target_splice_sess_cmd_list);
2585
2586/* target_wait_for_sess_cmds - Wait for outstanding descriptors
2587 * @se_sess: session to wait for active I/O
2588 * @wait_for_tasks: Make extra transport_wait_for_tasks call
2589 */
2590void target_wait_for_sess_cmds(
2591 struct se_session *se_sess,
2592 int wait_for_tasks)
2593{
2594 struct se_cmd *se_cmd, *tmp_cmd;
2595 bool rc = false;
2596
2597 list_for_each_entry_safe(se_cmd, tmp_cmd,
2598 &se_sess->sess_wait_list, se_cmd_list) {
2599 list_del(&se_cmd->se_cmd_list);
2600
2601 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
2602 " %d\n", se_cmd, se_cmd->t_state,
2603 se_cmd->se_tfo->get_cmd_state(se_cmd));
2604
2605 if (wait_for_tasks) {
2606 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
2607 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2608 se_cmd->se_tfo->get_cmd_state(se_cmd));
2609
2610 rc = transport_wait_for_tasks(se_cmd);
2611
2612 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
2613 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2614 se_cmd->se_tfo->get_cmd_state(se_cmd));
2615 }
2616
2617 if (!rc) {
2618 wait_for_completion(&se_cmd->cmd_wait_comp);
2619 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
2620 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2621 se_cmd->se_tfo->get_cmd_state(se_cmd));
2622 }
2623
2624 se_cmd->se_tfo->release_cmd(se_cmd);
2625 }
2626}
2627EXPORT_SYMBOL(target_wait_for_sess_cmds);
2628
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002629/* transport_lun_wait_for_tasks():
2630 *
2631 * Called from ConfigFS context to stop the passed struct se_cmd to allow
2632 * an struct se_lun to be successfully shutdown.
2633 */
2634static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
2635{
2636 unsigned long flags;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002637 int ret = 0;
2638
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002639 /*
2640 * If the frontend has already requested this struct se_cmd to
2641 * be stopped, we can safely ignore this struct se_cmd.
2642 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002643 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002644 if (cmd->transport_state & CMD_T_STOP) {
2645 cmd->transport_state &= ~CMD_T_LUN_STOP;
2646
2647 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
2648 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07002649 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwigf7113a42012-07-08 15:58:38 -04002650 transport_cmd_check_stop(cmd, false);
Andy Grovere3d6f902011-07-19 08:55:10 +00002651 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002652 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002653 cmd->transport_state |= CMD_T_LUN_FE_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07002654 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002655
Andy Grover5951146d2011-07-19 10:26:37 +00002656 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002657
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002658 // XXX: audit task_flags checks.
2659 spin_lock_irqsave(&cmd->t_state_lock, flags);
2660 if ((cmd->transport_state & CMD_T_BUSY) &&
2661 (cmd->transport_state & CMD_T_SENT)) {
2662 if (!target_stop_cmd(cmd, &flags))
2663 ret++;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002664 }
Christoph Hellwig5f41a312012-05-20 14:34:44 -04002665 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002666
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002667 pr_debug("ConfigFS: cmd: %p stop tasks ret:"
2668 " %d\n", cmd, ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002669 if (!ret) {
Andy Grover6708bb22011-06-08 10:36:43 -07002670 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002671 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07002672 wait_for_completion(&cmd->transport_lun_stop_comp);
Andy Grover6708bb22011-06-08 10:36:43 -07002673 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002674 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002675 }
Christoph Hellwig3df8d402011-10-17 13:56:43 -04002676 transport_remove_cmd_from_queue(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002677
2678 return 0;
2679}
2680
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002681static void __transport_clear_lun_from_sessions(struct se_lun *lun)
2682{
2683 struct se_cmd *cmd = NULL;
2684 unsigned long lun_flags, cmd_flags;
2685 /*
2686 * Do exception processing and return CHECK_CONDITION status to the
2687 * Initiator Port.
2688 */
2689 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
Andy Grover5951146d2011-07-19 10:26:37 +00002690 while (!list_empty(&lun->lun_cmd_list)) {
2691 cmd = list_first_entry(&lun->lun_cmd_list,
2692 struct se_cmd, se_lun_node);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -05002693 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002694
Andy Grovera1d8b492011-05-02 17:12:10 -07002695 spin_lock(&cmd->t_state_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07002696 pr_debug("SE_LUN[%d] - Setting cmd->transport"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002697 "_lun_stop for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002698 cmd->se_lun->unpacked_lun,
2699 cmd->se_tfo->get_task_tag(cmd));
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002700 cmd->transport_state |= CMD_T_LUN_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07002701 spin_unlock(&cmd->t_state_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002702
2703 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
2704
Andy Grover6708bb22011-06-08 10:36:43 -07002705 if (!cmd->se_lun) {
2706 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002707 cmd->se_tfo->get_task_tag(cmd),
2708 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002709 BUG();
2710 }
2711 /*
2712 * If the Storage engine still owns the iscsi_cmd_t, determine
2713 * and/or stop its context.
2714 */
Andy Grover6708bb22011-06-08 10:36:43 -07002715 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
Andy Grovere3d6f902011-07-19 08:55:10 +00002716 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
2717 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002718
Andy Grovere3d6f902011-07-19 08:55:10 +00002719 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002720 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2721 continue;
2722 }
2723
Andy Grover6708bb22011-06-08 10:36:43 -07002724 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002725 "_wait_for_tasks(): SUCCESS\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002726 cmd->se_lun->unpacked_lun,
2727 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002728
Andy Grovera1d8b492011-05-02 17:12:10 -07002729 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002730 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002731 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002732 goto check_cond;
2733 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002734 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002735 target_remove_from_state_list(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002736 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002737
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002738 /*
2739 * The Storage engine stopped this struct se_cmd before it was
2740 * send to the fabric frontend for delivery back to the
2741 * Initiator Node. Return this SCSI CDB back with an
2742 * CHECK_CONDITION status.
2743 */
2744check_cond:
2745 transport_send_check_condition_and_sense(cmd,
2746 TCM_NON_EXISTENT_LUN, 0);
2747 /*
2748 * If the fabric frontend is waiting for this iscsi_cmd_t to
2749 * be released, notify the waiting thread now that LU has
2750 * finished accessing it.
2751 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002752 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002753 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07002754 pr_debug("SE_LUN[%d] - Detected FE stop for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002755 " struct se_cmd: %p ITT: 0x%08x\n",
2756 lun->unpacked_lun,
Andy Grovere3d6f902011-07-19 08:55:10 +00002757 cmd, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002758
Andy Grovera1d8b492011-05-02 17:12:10 -07002759 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002760 cmd_flags);
Christoph Hellwigf7113a42012-07-08 15:58:38 -04002761 transport_cmd_check_stop(cmd, false);
Andy Grovera1d8b492011-05-02 17:12:10 -07002762 complete(&cmd->transport_lun_fe_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002763 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2764 continue;
2765 }
Andy Grover6708bb22011-06-08 10:36:43 -07002766 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002767 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002768
Andy Grovera1d8b492011-05-02 17:12:10 -07002769 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002770 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2771 }
2772 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
2773}
2774
2775static int transport_clear_lun_thread(void *p)
2776{
Jörn Engel8359cf42011-11-24 02:05:51 +01002777 struct se_lun *lun = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002778
2779 __transport_clear_lun_from_sessions(lun);
2780 complete(&lun->lun_shutdown_comp);
2781
2782 return 0;
2783}
2784
2785int transport_clear_lun_from_sessions(struct se_lun *lun)
2786{
2787 struct task_struct *kt;
2788
Andy Grover5951146d2011-07-19 10:26:37 +00002789 kt = kthread_run(transport_clear_lun_thread, lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002790 "tcm_cl_%u", lun->unpacked_lun);
2791 if (IS_ERR(kt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002792 pr_err("Unable to start clear_lun thread\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00002793 return PTR_ERR(kt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002794 }
2795 wait_for_completion(&lun->lun_shutdown_comp);
2796
2797 return 0;
2798}
2799
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002800/**
2801 * transport_wait_for_tasks - wait for completion to occur
2802 * @cmd: command to wait
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002803 *
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002804 * Called from frontend fabric context to wait for storage engine
2805 * to pause and/or release frontend generated struct se_cmd.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002806 */
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002807bool transport_wait_for_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002808{
2809 unsigned long flags;
2810
Andy Grovera1d8b492011-05-02 17:12:10 -07002811 spin_lock_irqsave(&cmd->t_state_lock, flags);
Andy Groverc8e31f22012-01-19 13:39:17 -08002812 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
2813 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002814 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002815 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002816 }
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04002817
Andy Groverc8e31f22012-01-19 13:39:17 -08002818 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
2819 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002820 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002821 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002822 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002823 /*
2824 * If we are already stopped due to an external event (ie: LUN shutdown)
2825 * sleep until the connection can have the passed struct se_cmd back.
Andy Grovera1d8b492011-05-02 17:12:10 -07002826 * The cmd->transport_lun_stopped_sem will be upped by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002827 * transport_clear_lun_from_sessions() once the ConfigFS context caller
2828 * has completed its operation on the struct se_cmd.
2829 */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002830 if (cmd->transport_state & CMD_T_LUN_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07002831 pr_debug("wait_for_tasks: Stopping"
Andy Grovere3d6f902011-07-19 08:55:10 +00002832 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002833 "_stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002834 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002835 /*
2836 * There is a special case for WRITES where a FE exception +
2837 * LUN shutdown means ConfigFS context is still sleeping on
2838 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
2839 * We go ahead and up transport_lun_stop_comp just to be sure
2840 * here.
2841 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002842 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2843 complete(&cmd->transport_lun_stop_comp);
2844 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
2845 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002846
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002847 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002848 /*
2849 * At this point, the frontend who was the originator of this
2850 * struct se_cmd, now owns the structure and can be released through
2851 * normal means below.
2852 */
Andy Grover6708bb22011-06-08 10:36:43 -07002853 pr_debug("wait_for_tasks: Stopped"
Andy Grovere3d6f902011-07-19 08:55:10 +00002854 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002855 "stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002856 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002857
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002858 cmd->transport_state &= ~CMD_T_LUN_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002859 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002860
Nicholas Bellinger3d289342012-02-13 02:38:14 -08002861 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002862 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002863 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002864 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002865
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002866 cmd->transport_state |= CMD_T_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002867
Andy Grover6708bb22011-06-08 10:36:43 -07002868 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002869 " i_state: %d, t_state: %d, CMD_T_STOP\n",
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04002870 cmd, cmd->se_tfo->get_task_tag(cmd),
2871 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002872
Andy Grovera1d8b492011-05-02 17:12:10 -07002873 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002874
Andy Grover5951146d2011-07-19 10:26:37 +00002875 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002876
Andy Grovera1d8b492011-05-02 17:12:10 -07002877 wait_for_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002878
Andy Grovera1d8b492011-05-02 17:12:10 -07002879 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002880 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002881
Andy Grover6708bb22011-06-08 10:36:43 -07002882 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
Andy Grovera1d8b492011-05-02 17:12:10 -07002883 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002884 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002885
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002886 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002887
2888 return true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002889}
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002890EXPORT_SYMBOL(transport_wait_for_tasks);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002891
2892static int transport_get_sense_codes(
2893 struct se_cmd *cmd,
2894 u8 *asc,
2895 u8 *ascq)
2896{
2897 *asc = cmd->scsi_asc;
2898 *ascq = cmd->scsi_ascq;
2899
2900 return 0;
2901}
2902
2903static int transport_set_sense_codes(
2904 struct se_cmd *cmd,
2905 u8 asc,
2906 u8 ascq)
2907{
2908 cmd->scsi_asc = asc;
2909 cmd->scsi_ascq = ascq;
2910
2911 return 0;
2912}
2913
2914int transport_send_check_condition_and_sense(
2915 struct se_cmd *cmd,
2916 u8 reason,
2917 int from_transport)
2918{
2919 unsigned char *buffer = cmd->sense_buffer;
2920 unsigned long flags;
2921 int offset;
2922 u8 asc = 0, ascq = 0;
2923
Andy Grovera1d8b492011-05-02 17:12:10 -07002924 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002925 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002926 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002927 return 0;
2928 }
2929 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
Andy Grovera1d8b492011-05-02 17:12:10 -07002930 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002931
2932 if (!reason && from_transport)
2933 goto after_reason;
2934
2935 if (!from_transport)
2936 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
2937 /*
2938 * Data Segment and SenseLength of the fabric response PDU.
2939 *
2940 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
2941 * from include/scsi/scsi_cmnd.h
2942 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002943 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002944 TRANSPORT_SENSE_BUFFER);
2945 /*
2946 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
2947 * SENSE KEY values from include/scsi/scsi.h
2948 */
2949 switch (reason) {
2950 case TCM_NON_EXISTENT_LUN:
Nicholas Bellingereb39d342011-07-26 16:59:00 -07002951 /* CURRENT ERROR */
2952 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002953 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingereb39d342011-07-26 16:59:00 -07002954 /* ILLEGAL REQUEST */
2955 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2956 /* LOGICAL UNIT NOT SUPPORTED */
2957 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
2958 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002959 case TCM_UNSUPPORTED_SCSI_OPCODE:
2960 case TCM_SECTOR_COUNT_TOO_MANY:
2961 /* CURRENT ERROR */
2962 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002963 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002964 /* ILLEGAL REQUEST */
2965 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2966 /* INVALID COMMAND OPERATION CODE */
2967 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
2968 break;
2969 case TCM_UNKNOWN_MODE_PAGE:
2970 /* CURRENT ERROR */
2971 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002972 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002973 /* ILLEGAL REQUEST */
2974 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2975 /* INVALID FIELD IN CDB */
2976 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
2977 break;
2978 case TCM_CHECK_CONDITION_ABORT_CMD:
2979 /* CURRENT ERROR */
2980 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002981 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002982 /* ABORTED COMMAND */
2983 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2984 /* BUS DEVICE RESET FUNCTION OCCURRED */
2985 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
2986 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
2987 break;
2988 case TCM_INCORRECT_AMOUNT_OF_DATA:
2989 /* CURRENT ERROR */
2990 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002991 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002992 /* ABORTED COMMAND */
2993 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2994 /* WRITE ERROR */
2995 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
2996 /* NOT ENOUGH UNSOLICITED DATA */
2997 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
2998 break;
2999 case TCM_INVALID_CDB_FIELD:
3000 /* CURRENT ERROR */
3001 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003002 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08003003 /* ILLEGAL REQUEST */
3004 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003005 /* INVALID FIELD IN CDB */
3006 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
3007 break;
3008 case TCM_INVALID_PARAMETER_LIST:
3009 /* CURRENT ERROR */
3010 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003011 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08003012 /* ILLEGAL REQUEST */
3013 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003014 /* INVALID FIELD IN PARAMETER LIST */
3015 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
3016 break;
3017 case TCM_UNEXPECTED_UNSOLICITED_DATA:
3018 /* CURRENT ERROR */
3019 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003020 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003021 /* ABORTED COMMAND */
3022 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
3023 /* WRITE ERROR */
3024 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
3025 /* UNEXPECTED_UNSOLICITED_DATA */
3026 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
3027 break;
3028 case TCM_SERVICE_CRC_ERROR:
3029 /* CURRENT ERROR */
3030 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003031 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003032 /* ABORTED COMMAND */
3033 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
3034 /* PROTOCOL SERVICE CRC ERROR */
3035 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
3036 /* N/A */
3037 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
3038 break;
3039 case TCM_SNACK_REJECTED:
3040 /* CURRENT ERROR */
3041 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003042 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003043 /* ABORTED COMMAND */
3044 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
3045 /* READ ERROR */
3046 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
3047 /* FAILED RETRANSMISSION REQUEST */
3048 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
3049 break;
3050 case TCM_WRITE_PROTECTED:
3051 /* CURRENT ERROR */
3052 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003053 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003054 /* DATA PROTECT */
3055 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
3056 /* WRITE PROTECTED */
3057 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
3058 break;
3059 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
3060 /* CURRENT ERROR */
3061 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003062 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003063 /* UNIT ATTENTION */
3064 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
3065 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
3066 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
3067 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
3068 break;
3069 case TCM_CHECK_CONDITION_NOT_READY:
3070 /* CURRENT ERROR */
3071 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003072 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003073 /* Not Ready */
3074 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
3075 transport_get_sense_codes(cmd, &asc, &ascq);
3076 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
3077 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
3078 break;
3079 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
3080 default:
3081 /* CURRENT ERROR */
3082 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08003083 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003084 /* ILLEGAL REQUEST */
3085 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
3086 /* LOGICAL UNIT COMMUNICATION FAILURE */
3087 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
3088 break;
3089 }
3090 /*
3091 * This code uses linux/include/scsi/scsi.h SAM status codes!
3092 */
3093 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
3094 /*
3095 * Automatically padded, this value is encoded in the fabric's
3096 * data_length response PDU containing the SCSI defined sense data.
3097 */
3098 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
3099
3100after_reason:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003101 return cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003102}
3103EXPORT_SYMBOL(transport_send_check_condition_and_sense);
3104
3105int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
3106{
3107 int ret = 0;
3108
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05003109 if (cmd->transport_state & CMD_T_ABORTED) {
Andy Grover6708bb22011-06-08 10:36:43 -07003110 if (!send_status ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003111 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
3112 return 1;
Andy Grover8b1e1242012-04-03 15:51:12 -07003113
Andy Grover6708bb22011-06-08 10:36:43 -07003114 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003115 " status for CDB: 0x%02x ITT: 0x%08x\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07003116 cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00003117 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07003118
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003119 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
Andy Grovere3d6f902011-07-19 08:55:10 +00003120 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003121 ret = 1;
3122 }
3123 return ret;
3124}
3125EXPORT_SYMBOL(transport_check_aborted_status);
3126
3127void transport_send_task_abort(struct se_cmd *cmd)
3128{
Nicholas Bellingerc252f002011-09-29 14:22:13 -07003129 unsigned long flags;
3130
3131 spin_lock_irqsave(&cmd->t_state_lock, flags);
3132 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
3133 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3134 return;
3135 }
3136 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3137
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003138 /*
3139 * If there are still expected incoming fabric WRITEs, we wait
3140 * until until they have completed before sending a TASK_ABORTED
3141 * response. This response with TASK_ABORTED status will be
3142 * queued back to fabric module by transport_check_aborted_status().
3143 */
3144 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grovere3d6f902011-07-19 08:55:10 +00003145 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05003146 cmd->transport_state |= CMD_T_ABORTED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003147 smp_mb__after_atomic_inc();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003148 }
3149 }
3150 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
Andy Grover8b1e1242012-04-03 15:51:12 -07003151
Andy Grover6708bb22011-06-08 10:36:43 -07003152 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
Andy Grovera1d8b492011-05-02 17:12:10 -07003153 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00003154 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07003155
Andy Grovere3d6f902011-07-19 08:55:10 +00003156 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003157}
3158
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003159static int transport_generic_do_tmr(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003160{
Andy Grover5951146d2011-07-19 10:26:37 +00003161 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003162 struct se_tmr_req *tmr = cmd->se_tmr_req;
3163 int ret;
3164
3165 switch (tmr->function) {
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003166 case TMR_ABORT_TASK:
Nicholas Bellinger3d289342012-02-13 02:38:14 -08003167 core_tmr_abort_task(dev, tmr, cmd->se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003168 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003169 case TMR_ABORT_TASK_SET:
3170 case TMR_CLEAR_ACA:
3171 case TMR_CLEAR_TASK_SET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003172 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
3173 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003174 case TMR_LUN_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003175 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
3176 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
3177 TMR_FUNCTION_REJECTED;
3178 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003179 case TMR_TARGET_WARM_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003180 tmr->response = TMR_FUNCTION_REJECTED;
3181 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003182 case TMR_TARGET_COLD_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003183 tmr->response = TMR_FUNCTION_REJECTED;
3184 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003185 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003186 pr_err("Uknown TMR function: 0x%02x.\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003187 tmr->function);
3188 tmr->response = TMR_FUNCTION_REJECTED;
3189 break;
3190 }
3191
3192 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
Andy Grovere3d6f902011-07-19 08:55:10 +00003193 cmd->se_tfo->queue_tm_rsp(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003194
Christoph Hellwigb7b8bef2011-10-17 13:56:44 -04003195 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003196 return 0;
3197}
3198
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003199/* transport_processing_thread():
3200 *
3201 *
3202 */
3203static int transport_processing_thread(void *param)
3204{
Andy Grover5951146d2011-07-19 10:26:37 +00003205 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003206 struct se_cmd *cmd;
Jörn Engel8359cf42011-11-24 02:05:51 +01003207 struct se_device *dev = param;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003208
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003209 while (!kthread_should_stop()) {
Andy Grovere3d6f902011-07-19 08:55:10 +00003210 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
3211 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003212 kthread_should_stop());
3213 if (ret < 0)
3214 goto out;
3215
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003216get_cmd:
Andy Grover5951146d2011-07-19 10:26:37 +00003217 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
3218 if (!cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003219 continue;
3220
Andy Grover5951146d2011-07-19 10:26:37 +00003221 switch (cmd->t_state) {
Christoph Hellwig680b73c2011-09-12 21:51:14 +02003222 case TRANSPORT_NEW_CMD:
3223 BUG();
3224 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003225 case TRANSPORT_PROCESS_TMR:
3226 transport_generic_do_tmr(cmd);
3227 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003228 default:
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04003229 pr_err("Unknown t_state: %d for ITT: 0x%08x "
3230 "i_state: %d on SE LUN: %u\n",
3231 cmd->t_state,
Andy Grovere3d6f902011-07-19 08:55:10 +00003232 cmd->se_tfo->get_task_tag(cmd),
3233 cmd->se_tfo->get_cmd_state(cmd),
3234 cmd->se_lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003235 BUG();
3236 }
3237
3238 goto get_cmd;
3239 }
3240
3241out:
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003242 WARN_ON(!list_empty(&dev->state_list));
Nicholas Bellingerce8762f2011-10-09 02:19:01 -07003243 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003244 dev->process_thread = NULL;
3245 return 0;
3246}