blob: 040f05fde4d6f69bf00a71eeee1a64c659ab5f67 [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_transport.c
3 *
4 * This file contains the Generic Target Engine Core.
5 *
6 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
7 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
8 * Copyright (c) 2007-2010 Rising Tide Systems
9 * Copyright (c) 2008-2010 Linux-iSCSI.org
10 *
11 * Nicholas A. Bellinger <nab@kernel.org>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 *
27 ******************************************************************************/
28
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080029#include <linux/net.h>
30#include <linux/delay.h>
31#include <linux/string.h>
32#include <linux/timer.h>
33#include <linux/slab.h>
34#include <linux/blkdev.h>
35#include <linux/spinlock.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080036#include <linux/kthread.h>
37#include <linux/in.h>
38#include <linux/cdrom.h>
Paul Gortmaker827509e2011-08-30 14:20:44 -040039#include <linux/module.h>
Roland Dreier015487b2012-02-13 16:18:17 -080040#include <linux/ratelimit.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080041#include <asm/unaligned.h>
42#include <net/sock.h>
43#include <net/tcp.h>
44#include <scsi/scsi.h>
45#include <scsi/scsi_cmnd.h>
Nicholas Bellingere66ecd52011-05-19 20:19:14 -070046#include <scsi/scsi_tcq.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080047
48#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050049#include <target/target_core_backend.h>
50#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080051#include <target/target_core_configfs.h>
52
Christoph Hellwige26d99a2011-11-14 12:30:30 -050053#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080054#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080055#include "target_core_pr.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080056#include "target_core_ua.h"
57
Andy Grovere3d6f902011-07-19 08:55:10 +000058static int sub_api_initialized;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080059
Christoph Hellwig35e0e752011-10-17 13:56:53 -040060static struct workqueue_struct *target_completion_wq;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080061static struct kmem_cache *se_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080062struct kmem_cache *se_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080063struct kmem_cache *t10_pr_reg_cache;
64struct kmem_cache *t10_alua_lu_gp_cache;
65struct kmem_cache *t10_alua_lu_gp_mem_cache;
66struct kmem_cache *t10_alua_tg_pt_gp_cache;
67struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
68
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080069static void transport_complete_task_attr(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -070070static void transport_handle_queue_full(struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -040071 struct se_device *dev);
Andy Grover05d1c7c2011-07-20 19:13:28 +000072static int transport_generic_get_mem(struct se_cmd *cmd);
Roland Dreierbc187ea2012-07-16 11:04:40 -070073static int target_get_sess_cmd(struct se_session *, struct se_cmd *, bool);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -070074static void transport_put_cmd(struct se_cmd *cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080075static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
Christoph Hellwig35e0e752011-10-17 13:56:53 -040076static void target_complete_ok_work(struct work_struct *work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080077
Andy Grovere3d6f902011-07-19 08:55:10 +000078int init_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080079{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080080 se_sess_cache = kmem_cache_create("se_sess_cache",
81 sizeof(struct se_session), __alignof__(struct se_session),
82 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070083 if (!se_sess_cache) {
84 pr_err("kmem_cache_create() for struct se_session"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080085 " failed\n");
Andy Groverc8e31f22012-01-19 13:39:17 -080086 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080087 }
88 se_ua_cache = kmem_cache_create("se_ua_cache",
89 sizeof(struct se_ua), __alignof__(struct se_ua),
90 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070091 if (!se_ua_cache) {
92 pr_err("kmem_cache_create() for struct se_ua failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -040093 goto out_free_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080094 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080095 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
96 sizeof(struct t10_pr_registration),
97 __alignof__(struct t10_pr_registration), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070098 if (!t10_pr_reg_cache) {
99 pr_err("kmem_cache_create() for struct t10_pr_registration"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800100 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400101 goto out_free_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800102 }
103 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
104 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
105 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700106 if (!t10_alua_lu_gp_cache) {
107 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800108 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400109 goto out_free_pr_reg_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800110 }
111 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
112 sizeof(struct t10_alua_lu_gp_member),
113 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700114 if (!t10_alua_lu_gp_mem_cache) {
115 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800116 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400117 goto out_free_lu_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800118 }
119 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
120 sizeof(struct t10_alua_tg_pt_gp),
121 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700122 if (!t10_alua_tg_pt_gp_cache) {
123 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800124 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400125 goto out_free_lu_gp_mem_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800126 }
127 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
128 "t10_alua_tg_pt_gp_mem_cache",
129 sizeof(struct t10_alua_tg_pt_gp_member),
130 __alignof__(struct t10_alua_tg_pt_gp_member),
131 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700132 if (!t10_alua_tg_pt_gp_mem_cache) {
133 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800134 "mem_t failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400135 goto out_free_tg_pt_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800136 }
137
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400138 target_completion_wq = alloc_workqueue("target_completion",
139 WQ_MEM_RECLAIM, 0);
140 if (!target_completion_wq)
141 goto out_free_tg_pt_gp_mem_cache;
142
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800143 return 0;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400144
145out_free_tg_pt_gp_mem_cache:
146 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
147out_free_tg_pt_gp_cache:
148 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
149out_free_lu_gp_mem_cache:
150 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
151out_free_lu_gp_cache:
152 kmem_cache_destroy(t10_alua_lu_gp_cache);
153out_free_pr_reg_cache:
154 kmem_cache_destroy(t10_pr_reg_cache);
155out_free_ua_cache:
156 kmem_cache_destroy(se_ua_cache);
157out_free_sess_cache:
158 kmem_cache_destroy(se_sess_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800159out:
Andy Grovere3d6f902011-07-19 08:55:10 +0000160 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800161}
162
Andy Grovere3d6f902011-07-19 08:55:10 +0000163void release_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800164{
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400165 destroy_workqueue(target_completion_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800166 kmem_cache_destroy(se_sess_cache);
167 kmem_cache_destroy(se_ua_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800168 kmem_cache_destroy(t10_pr_reg_cache);
169 kmem_cache_destroy(t10_alua_lu_gp_cache);
170 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
171 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
172 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800173}
174
Andy Grovere3d6f902011-07-19 08:55:10 +0000175/* This code ensures unique mib indexes are handed out. */
176static DEFINE_SPINLOCK(scsi_mib_index_lock);
177static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800178
179/*
180 * Allocate a new row index for the entry type specified
181 */
182u32 scsi_get_new_index(scsi_index_t type)
183{
184 u32 new_index;
185
Andy Grovere3d6f902011-07-19 08:55:10 +0000186 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800187
Andy Grovere3d6f902011-07-19 08:55:10 +0000188 spin_lock(&scsi_mib_index_lock);
189 new_index = ++scsi_mib_index[type];
190 spin_unlock(&scsi_mib_index_lock);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800191
192 return new_index;
193}
194
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700195void transport_subsystem_check_init(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800196{
197 int ret;
198
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700199 if (sub_api_initialized)
200 return;
201
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800202 ret = request_module("target_core_iblock");
203 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700204 pr_err("Unable to load target_core_iblock\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800205
206 ret = request_module("target_core_file");
207 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700208 pr_err("Unable to load target_core_file\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800209
210 ret = request_module("target_core_pscsi");
211 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700212 pr_err("Unable to load target_core_pscsi\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800213
214 ret = request_module("target_core_stgt");
215 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700216 pr_err("Unable to load target_core_stgt\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800217
Andy Grovere3d6f902011-07-19 08:55:10 +0000218 sub_api_initialized = 1;
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700219 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800220}
221
222struct se_session *transport_init_session(void)
223{
224 struct se_session *se_sess;
225
226 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700227 if (!se_sess) {
228 pr_err("Unable to allocate struct se_session from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800229 " se_sess_cache\n");
230 return ERR_PTR(-ENOMEM);
231 }
232 INIT_LIST_HEAD(&se_sess->sess_list);
233 INIT_LIST_HEAD(&se_sess->sess_acl_list);
Nicholas Bellingera17f0912011-11-02 21:52:08 -0700234 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
Nicholas Bellingera17f0912011-11-02 21:52:08 -0700235 spin_lock_init(&se_sess->sess_cmd_lock);
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800236 kref_init(&se_sess->sess_kref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800237
238 return se_sess;
239}
240EXPORT_SYMBOL(transport_init_session);
241
242/*
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700243 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800244 */
245void __transport_register_session(
246 struct se_portal_group *se_tpg,
247 struct se_node_acl *se_nacl,
248 struct se_session *se_sess,
249 void *fabric_sess_ptr)
250{
251 unsigned char buf[PR_REG_ISID_LEN];
252
253 se_sess->se_tpg = se_tpg;
254 se_sess->fabric_sess_ptr = fabric_sess_ptr;
255 /*
256 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
257 *
258 * Only set for struct se_session's that will actually be moving I/O.
259 * eg: *NOT* discovery sessions.
260 */
261 if (se_nacl) {
262 /*
263 * If the fabric module supports an ISID based TransportID,
264 * save this value in binary from the fabric I_T Nexus now.
265 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000266 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800267 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +0000268 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800269 &buf[0], PR_REG_ISID_LEN);
270 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
271 }
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800272 kref_get(&se_nacl->acl_kref);
273
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800274 spin_lock_irq(&se_nacl->nacl_sess_lock);
275 /*
276 * The se_nacl->nacl_sess pointer will be set to the
277 * last active I_T Nexus for each struct se_node_acl.
278 */
279 se_nacl->nacl_sess = se_sess;
280
281 list_add_tail(&se_sess->sess_acl_list,
282 &se_nacl->acl_sess_list);
283 spin_unlock_irq(&se_nacl->nacl_sess_lock);
284 }
285 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
286
Andy Grover6708bb22011-06-08 10:36:43 -0700287 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000288 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800289}
290EXPORT_SYMBOL(__transport_register_session);
291
292void transport_register_session(
293 struct se_portal_group *se_tpg,
294 struct se_node_acl *se_nacl,
295 struct se_session *se_sess,
296 void *fabric_sess_ptr)
297{
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700298 unsigned long flags;
299
300 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800301 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700302 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303}
304EXPORT_SYMBOL(transport_register_session);
305
Joern Engel41492682012-05-18 13:57:19 -0700306void target_release_session(struct kref *kref)
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800307{
308 struct se_session *se_sess = container_of(kref,
309 struct se_session, sess_kref);
310 struct se_portal_group *se_tpg = se_sess->se_tpg;
311
312 se_tpg->se_tpg_tfo->close_session(se_sess);
313}
314
315void target_get_session(struct se_session *se_sess)
316{
317 kref_get(&se_sess->sess_kref);
318}
319EXPORT_SYMBOL(target_get_session);
320
Jörn Engel33933a02012-05-11 10:35:08 -0400321void target_put_session(struct se_session *se_sess)
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800322{
Joern Engel41492682012-05-18 13:57:19 -0700323 struct se_portal_group *tpg = se_sess->se_tpg;
324
325 if (tpg->se_tpg_tfo->put_session != NULL) {
326 tpg->se_tpg_tfo->put_session(se_sess);
327 return;
328 }
Jörn Engel33933a02012-05-11 10:35:08 -0400329 kref_put(&se_sess->sess_kref, target_release_session);
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800330}
331EXPORT_SYMBOL(target_put_session);
332
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800333static void target_complete_nacl(struct kref *kref)
334{
335 struct se_node_acl *nacl = container_of(kref,
336 struct se_node_acl, acl_kref);
337
338 complete(&nacl->acl_free_comp);
339}
340
341void target_put_nacl(struct se_node_acl *nacl)
342{
343 kref_put(&nacl->acl_kref, target_complete_nacl);
344}
345
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800346void transport_deregister_session_configfs(struct se_session *se_sess)
347{
348 struct se_node_acl *se_nacl;
Roland Dreier23388862011-06-22 01:02:21 -0700349 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800350 /*
351 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
352 */
353 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700354 if (se_nacl) {
Roland Dreier23388862011-06-22 01:02:21 -0700355 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800356 if (se_nacl->acl_stop == 0)
357 list_del(&se_sess->sess_acl_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800358 /*
359 * If the session list is empty, then clear the pointer.
360 * Otherwise, set the struct se_session pointer from the tail
361 * element of the per struct se_node_acl active session list.
362 */
363 if (list_empty(&se_nacl->acl_sess_list))
364 se_nacl->nacl_sess = NULL;
365 else {
366 se_nacl->nacl_sess = container_of(
367 se_nacl->acl_sess_list.prev,
368 struct se_session, sess_acl_list);
369 }
Roland Dreier23388862011-06-22 01:02:21 -0700370 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800371 }
372}
373EXPORT_SYMBOL(transport_deregister_session_configfs);
374
375void transport_free_session(struct se_session *se_sess)
376{
377 kmem_cache_free(se_sess_cache, se_sess);
378}
379EXPORT_SYMBOL(transport_free_session);
380
381void transport_deregister_session(struct se_session *se_sess)
382{
383 struct se_portal_group *se_tpg = se_sess->se_tpg;
Nicholas Bellinger01468342012-03-10 14:32:52 -0800384 struct target_core_fabric_ops *se_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800385 struct se_node_acl *se_nacl;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700386 unsigned long flags;
Nicholas Bellinger01468342012-03-10 14:32:52 -0800387 bool comp_nacl = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800388
Andy Grover6708bb22011-06-08 10:36:43 -0700389 if (!se_tpg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800390 transport_free_session(se_sess);
391 return;
392 }
Nicholas Bellinger01468342012-03-10 14:32:52 -0800393 se_tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800394
Roland Dreiere63a8e12011-08-12 16:01:02 -0700395 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800396 list_del(&se_sess->sess_list);
397 se_sess->se_tpg = NULL;
398 se_sess->fabric_sess_ptr = NULL;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700399 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800400
401 /*
402 * Determine if we need to do extra work for this initiator node's
403 * struct se_node_acl if it had been previously dynamically generated.
404 */
405 se_nacl = se_sess->se_node_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406
Nicholas Bellinger01468342012-03-10 14:32:52 -0800407 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
408 if (se_nacl && se_nacl->dynamic_node_acl) {
409 if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
410 list_del(&se_nacl->acl_list);
411 se_tpg->num_node_acls--;
412 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
413 core_tpg_wait_for_nacl_pr_ref(se_nacl);
414 core_free_device_list_for_node(se_nacl, se_tpg);
415 se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl);
416
417 comp_nacl = false;
418 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800419 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800420 }
Nicholas Bellinger01468342012-03-10 14:32:52 -0800421 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800422
Andy Grover6708bb22011-06-08 10:36:43 -0700423 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000424 se_tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellinger01468342012-03-10 14:32:52 -0800425 /*
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800426 * If last kref is dropping now for an explict NodeACL, awake sleeping
427 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
428 * removal context.
Nicholas Bellinger01468342012-03-10 14:32:52 -0800429 */
430 if (se_nacl && comp_nacl == true)
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800431 target_put_nacl(se_nacl);
Nicholas Bellinger01468342012-03-10 14:32:52 -0800432
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800433 transport_free_session(se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800434}
435EXPORT_SYMBOL(transport_deregister_session);
436
437/*
Andy Grovera1d8b492011-05-02 17:12:10 -0700438 * Called with cmd->t_state_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800439 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400440static void target_remove_from_state_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800441{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400442 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800443 unsigned long flags;
444
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400445 if (!dev)
446 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800447
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400448 if (cmd->transport_state & CMD_T_BUSY)
449 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800450
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400451 spin_lock_irqsave(&dev->execute_task_lock, flags);
452 if (cmd->state_active) {
453 list_del(&cmd->state_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400454 cmd->state_active = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800455 }
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400456 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800457}
458
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400459static int transport_cmd_check_stop(struct se_cmd *cmd, bool remove_from_lists)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800460{
461 unsigned long flags;
462
Andy Grovera1d8b492011-05-02 17:12:10 -0700463 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800464 /*
465 * Determine if IOCTL context caller in requesting the stopping of this
466 * command for LUN shutdown purposes.
467 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500468 if (cmd->transport_state & CMD_T_LUN_STOP) {
469 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
470 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800471
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500472 cmd->transport_state &= ~CMD_T_ACTIVE;
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400473 if (remove_from_lists)
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400474 target_remove_from_state_list(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700475 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800476
Andy Grovera1d8b492011-05-02 17:12:10 -0700477 complete(&cmd->transport_lun_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800478 return 1;
479 }
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400480
481 if (remove_from_lists) {
482 target_remove_from_state_list(cmd);
483
484 /*
485 * Clear struct se_cmd->se_lun before the handoff to FE.
486 */
487 cmd->se_lun = NULL;
488 }
489
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800490 /*
491 * Determine if frontend context caller is requesting the stopping of
Andy Grovere3d6f902011-07-19 08:55:10 +0000492 * this command for frontend exceptions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800493 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500494 if (cmd->transport_state & CMD_T_STOP) {
495 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
496 __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000497 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800498
Andy Grovera1d8b492011-05-02 17:12:10 -0700499 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500
Andy Grovera1d8b492011-05-02 17:12:10 -0700501 complete(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800502 return 1;
503 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800504
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400505 cmd->transport_state &= ~CMD_T_ACTIVE;
506 if (remove_from_lists) {
507 /*
508 * Some fabric modules like tcm_loop can release
509 * their internally allocated I/O reference now and
510 * struct se_cmd now.
511 *
512 * Fabric modules are expected to return '1' here if the
513 * se_cmd being passed is released at this point,
514 * or zero if not being released.
515 */
516 if (cmd->se_tfo->check_stop_free != NULL) {
517 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
518 return cmd->se_tfo->check_stop_free(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800519 }
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400520 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800521
Andy Grovera1d8b492011-05-02 17:12:10 -0700522 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800523 return 0;
524}
525
526static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
527{
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400528 return transport_cmd_check_stop(cmd, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800529}
530
531static void transport_lun_remove_cmd(struct se_cmd *cmd)
532{
Andy Grovere3d6f902011-07-19 08:55:10 +0000533 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800534 unsigned long flags;
535
536 if (!lun)
537 return;
538
Andy Grovera1d8b492011-05-02 17:12:10 -0700539 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500540 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
541 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400542 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800543 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700544 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800545
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800546 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -0500547 if (!list_empty(&cmd->se_lun_node))
548 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800549 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
550}
551
552void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
553{
Andy Groverc8e31f22012-01-19 13:39:17 -0800554 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellinger8dc52b52011-10-09 02:02:51 -0700555 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800556
557 if (transport_cmd_check_stop_to_fabric(cmd))
558 return;
Christoph Hellwigaf877292012-07-08 15:58:49 -0400559 if (remove)
Christoph Hellwige6a25732011-09-13 23:08:50 +0200560 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800561}
562
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400563static void target_complete_failure_work(struct work_struct *work)
564{
565 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
566
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700567 transport_generic_request_failure(cmd);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400568}
569
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200570/*
571 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
572 */
Paolo Bonzini27a27092012-09-05 17:09:14 +0200573static void transport_get_sense_data(struct se_cmd *cmd)
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200574{
575 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
576 struct se_device *dev = cmd->se_dev;
577 unsigned long flags;
578 u32 offset = 0;
579
580 WARN_ON(!cmd->se_lun);
581
582 if (!dev)
Paolo Bonzini27a27092012-09-05 17:09:14 +0200583 return;
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200584
585 spin_lock_irqsave(&cmd->t_state_lock, flags);
586 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
587 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Paolo Bonzini27a27092012-09-05 17:09:14 +0200588 return;
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200589 }
590
591 sense_buffer = dev->transport->get_sense_buffer(cmd);
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200592 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
593
594 offset = cmd->se_tfo->set_fabric_sense_len(cmd, TRANSPORT_SENSE_BUFFER);
595
596 memcpy(&buffer[offset], sense_buffer, TRANSPORT_SENSE_BUFFER);
597
598 /* Automatically padded */
599 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
600
601 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x and sense\n",
602 dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200603}
604
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400605void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800606{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400607 struct se_device *dev = cmd->se_dev;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400608 int success = scsi_status == GOOD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800609 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800610
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400611 cmd->scsi_status = scsi_status;
612
613
Andy Grovera1d8b492011-05-02 17:12:10 -0700614 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400615 cmd->transport_state &= ~CMD_T_BUSY;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800616
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800617 if (dev && dev->transport->transport_complete) {
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400618 if (dev->transport->transport_complete(cmd,
619 cmd->t_data_sg) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800620 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800621 success = 1;
622 }
623 }
624
625 /*
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400626 * See if we are waiting to complete for an exception condition.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800627 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400628 if (cmd->transport_state & CMD_T_REQUEST_STOP) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700629 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400630 complete(&cmd->task_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800631 return;
632 }
Christoph Hellwig22350072011-11-02 05:06:35 -0700633
634 if (!success)
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500635 cmd->transport_state |= CMD_T_FAILED;
Christoph Hellwig22350072011-11-02 05:06:35 -0700636
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800637 /*
638 * Check for case where an explict ABORT_TASK has been received
639 * and transport_wait_for_tasks() will be waiting for completion..
640 */
641 if (cmd->transport_state & CMD_T_ABORTED &&
642 cmd->transport_state & CMD_T_STOP) {
643 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
644 complete(&cmd->t_transport_stop_comp);
645 return;
646 } else if (cmd->transport_state & CMD_T_FAILED) {
Christoph Hellwig41e16e92011-11-23 06:54:15 -0500647 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400648 INIT_WORK(&cmd->work, target_complete_failure_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800649 } else {
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400650 INIT_WORK(&cmd->work, target_complete_ok_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800651 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400652
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400653 cmd->t_state = TRANSPORT_COMPLETE;
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800654 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
Andy Grovera1d8b492011-05-02 17:12:10 -0700655 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800656
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400657 queue_work(target_completion_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800658}
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400659EXPORT_SYMBOL(target_complete_cmd);
660
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400661static void target_add_to_state_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800662{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400663 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800664 unsigned long flags;
665
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800666 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400667 if (!cmd->state_active) {
668 list_add_tail(&cmd->state_list, &dev->state_list);
669 cmd->state_active = true;
670 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800671 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800672}
673
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700674/*
Nicholas Bellingerf147abb2011-10-25 23:57:41 -0700675 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700676 */
Christoph Hellwig7a6f0a12012-07-08 15:58:47 -0400677static void transport_write_pending_qf(struct se_cmd *cmd);
678static void transport_complete_qf(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700679
680static void target_qf_do_work(struct work_struct *work)
681{
682 struct se_device *dev = container_of(work, struct se_device,
683 qf_work_queue);
Roland Dreierbcac3642011-08-27 21:33:16 -0700684 LIST_HEAD(qf_cmd_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700685 struct se_cmd *cmd, *cmd_tmp;
686
687 spin_lock_irq(&dev->qf_cmd_lock);
Roland Dreierbcac3642011-08-27 21:33:16 -0700688 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
689 spin_unlock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700690
Roland Dreierbcac3642011-08-27 21:33:16 -0700691 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700692 list_del(&cmd->se_qf_node);
693 atomic_dec(&dev->dev_qf_count);
694 smp_mb__after_atomic_dec();
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700695
Andy Grover6708bb22011-06-08 10:36:43 -0700696 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700697 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -0400698 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700699 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
700 : "UNKNOWN");
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400701
Christoph Hellwig7a6f0a12012-07-08 15:58:47 -0400702 if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP)
703 transport_write_pending_qf(cmd);
704 else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK)
705 transport_complete_qf(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700706 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700707}
708
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800709unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
710{
711 switch (cmd->data_direction) {
712 case DMA_NONE:
713 return "NONE";
714 case DMA_FROM_DEVICE:
715 return "READ";
716 case DMA_TO_DEVICE:
717 return "WRITE";
718 case DMA_BIDIRECTIONAL:
719 return "BIDI";
720 default:
721 break;
722 }
723
724 return "UNKNOWN";
725}
726
727void transport_dump_dev_state(
728 struct se_device *dev,
729 char *b,
730 int *bl)
731{
732 *bl += sprintf(b + *bl, "Status: ");
733 switch (dev->dev_status) {
734 case TRANSPORT_DEVICE_ACTIVATED:
735 *bl += sprintf(b + *bl, "ACTIVATED");
736 break;
737 case TRANSPORT_DEVICE_DEACTIVATED:
738 *bl += sprintf(b + *bl, "DEACTIVATED");
739 break;
740 case TRANSPORT_DEVICE_SHUTDOWN:
741 *bl += sprintf(b + *bl, "SHUTDOWN");
742 break;
743 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
744 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
745 *bl += sprintf(b + *bl, "OFFLINE");
746 break;
747 default:
748 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
749 break;
750 }
751
Christoph Hellwig5f41a312012-05-20 14:34:44 -0400752 *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth);
Nicholas Bellinger11e764b2012-05-09 12:42:09 -0700753 *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n",
754 dev->se_sub_dev->se_dev_attrib.block_size,
755 dev->se_sub_dev->se_dev_attrib.hw_max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800756 *bl += sprintf(b + *bl, " ");
757}
758
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800759void transport_dump_vpd_proto_id(
760 struct t10_vpd *vpd,
761 unsigned char *p_buf,
762 int p_buf_len)
763{
764 unsigned char buf[VPD_TMP_BUF_SIZE];
765 int len;
766
767 memset(buf, 0, VPD_TMP_BUF_SIZE);
768 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
769
770 switch (vpd->protocol_identifier) {
771 case 0x00:
772 sprintf(buf+len, "Fibre Channel\n");
773 break;
774 case 0x10:
775 sprintf(buf+len, "Parallel SCSI\n");
776 break;
777 case 0x20:
778 sprintf(buf+len, "SSA\n");
779 break;
780 case 0x30:
781 sprintf(buf+len, "IEEE 1394\n");
782 break;
783 case 0x40:
784 sprintf(buf+len, "SCSI Remote Direct Memory Access"
785 " Protocol\n");
786 break;
787 case 0x50:
788 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
789 break;
790 case 0x60:
791 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
792 break;
793 case 0x70:
794 sprintf(buf+len, "Automation/Drive Interface Transport"
795 " Protocol\n");
796 break;
797 case 0x80:
798 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
799 break;
800 default:
801 sprintf(buf+len, "Unknown 0x%02x\n",
802 vpd->protocol_identifier);
803 break;
804 }
805
806 if (p_buf)
807 strncpy(p_buf, buf, p_buf_len);
808 else
Andy Grover6708bb22011-06-08 10:36:43 -0700809 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800810}
811
812void
813transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
814{
815 /*
816 * Check if the Protocol Identifier Valid (PIV) bit is set..
817 *
818 * from spc3r23.pdf section 7.5.1
819 */
820 if (page_83[1] & 0x80) {
821 vpd->protocol_identifier = (page_83[0] & 0xf0);
822 vpd->protocol_identifier_set = 1;
823 transport_dump_vpd_proto_id(vpd, NULL, 0);
824 }
825}
826EXPORT_SYMBOL(transport_set_vpd_proto_id);
827
828int transport_dump_vpd_assoc(
829 struct t10_vpd *vpd,
830 unsigned char *p_buf,
831 int p_buf_len)
832{
833 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +0000834 int ret = 0;
835 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800836
837 memset(buf, 0, VPD_TMP_BUF_SIZE);
838 len = sprintf(buf, "T10 VPD Identifier Association: ");
839
840 switch (vpd->association) {
841 case 0x00:
842 sprintf(buf+len, "addressed logical unit\n");
843 break;
844 case 0x10:
845 sprintf(buf+len, "target port\n");
846 break;
847 case 0x20:
848 sprintf(buf+len, "SCSI target device\n");
849 break;
850 default:
851 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
Andy Grovere3d6f902011-07-19 08:55:10 +0000852 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800853 break;
854 }
855
856 if (p_buf)
857 strncpy(p_buf, buf, p_buf_len);
858 else
Andy Grover6708bb22011-06-08 10:36:43 -0700859 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800860
861 return ret;
862}
863
864int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
865{
866 /*
867 * The VPD identification association..
868 *
869 * from spc3r23.pdf Section 7.6.3.1 Table 297
870 */
871 vpd->association = (page_83[1] & 0x30);
872 return transport_dump_vpd_assoc(vpd, NULL, 0);
873}
874EXPORT_SYMBOL(transport_set_vpd_assoc);
875
876int transport_dump_vpd_ident_type(
877 struct t10_vpd *vpd,
878 unsigned char *p_buf,
879 int p_buf_len)
880{
881 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +0000882 int ret = 0;
883 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800884
885 memset(buf, 0, VPD_TMP_BUF_SIZE);
886 len = sprintf(buf, "T10 VPD Identifier Type: ");
887
888 switch (vpd->device_identifier_type) {
889 case 0x00:
890 sprintf(buf+len, "Vendor specific\n");
891 break;
892 case 0x01:
893 sprintf(buf+len, "T10 Vendor ID based\n");
894 break;
895 case 0x02:
896 sprintf(buf+len, "EUI-64 based\n");
897 break;
898 case 0x03:
899 sprintf(buf+len, "NAA\n");
900 break;
901 case 0x04:
902 sprintf(buf+len, "Relative target port identifier\n");
903 break;
904 case 0x08:
905 sprintf(buf+len, "SCSI name string\n");
906 break;
907 default:
908 sprintf(buf+len, "Unsupported: 0x%02x\n",
909 vpd->device_identifier_type);
Andy Grovere3d6f902011-07-19 08:55:10 +0000910 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800911 break;
912 }
913
Andy Grovere3d6f902011-07-19 08:55:10 +0000914 if (p_buf) {
915 if (p_buf_len < strlen(buf)+1)
916 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800917 strncpy(p_buf, buf, p_buf_len);
Andy Grovere3d6f902011-07-19 08:55:10 +0000918 } else {
Andy Grover6708bb22011-06-08 10:36:43 -0700919 pr_debug("%s", buf);
Andy Grovere3d6f902011-07-19 08:55:10 +0000920 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800921
922 return ret;
923}
924
925int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
926{
927 /*
928 * The VPD identifier type..
929 *
930 * from spc3r23.pdf Section 7.6.3.1 Table 298
931 */
932 vpd->device_identifier_type = (page_83[1] & 0x0f);
933 return transport_dump_vpd_ident_type(vpd, NULL, 0);
934}
935EXPORT_SYMBOL(transport_set_vpd_ident_type);
936
937int transport_dump_vpd_ident(
938 struct t10_vpd *vpd,
939 unsigned char *p_buf,
940 int p_buf_len)
941{
942 unsigned char buf[VPD_TMP_BUF_SIZE];
943 int ret = 0;
944
945 memset(buf, 0, VPD_TMP_BUF_SIZE);
946
947 switch (vpd->device_identifier_code_set) {
948 case 0x01: /* Binary */
949 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
950 &vpd->device_identifier[0]);
951 break;
952 case 0x02: /* ASCII */
953 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
954 &vpd->device_identifier[0]);
955 break;
956 case 0x03: /* UTF-8 */
957 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
958 &vpd->device_identifier[0]);
959 break;
960 default:
961 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
962 " 0x%02x", vpd->device_identifier_code_set);
Andy Grovere3d6f902011-07-19 08:55:10 +0000963 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800964 break;
965 }
966
967 if (p_buf)
968 strncpy(p_buf, buf, p_buf_len);
969 else
Andy Grover6708bb22011-06-08 10:36:43 -0700970 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800971
972 return ret;
973}
974
975int
976transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
977{
978 static const char hex_str[] = "0123456789abcdef";
979 int j = 0, i = 4; /* offset to start of the identifer */
980
981 /*
982 * The VPD Code Set (encoding)
983 *
984 * from spc3r23.pdf Section 7.6.3.1 Table 296
985 */
986 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
987 switch (vpd->device_identifier_code_set) {
988 case 0x01: /* Binary */
989 vpd->device_identifier[j++] =
990 hex_str[vpd->device_identifier_type];
991 while (i < (4 + page_83[3])) {
992 vpd->device_identifier[j++] =
993 hex_str[(page_83[i] & 0xf0) >> 4];
994 vpd->device_identifier[j++] =
995 hex_str[page_83[i] & 0x0f];
996 i++;
997 }
998 break;
999 case 0x02: /* ASCII */
1000 case 0x03: /* UTF-8 */
1001 while (i < (4 + page_83[3]))
1002 vpd->device_identifier[j++] = page_83[i++];
1003 break;
1004 default:
1005 break;
1006 }
1007
1008 return transport_dump_vpd_ident(vpd, NULL, 0);
1009}
1010EXPORT_SYMBOL(transport_set_vpd_ident);
1011
1012static void core_setup_task_attr_emulation(struct se_device *dev)
1013{
1014 /*
1015 * If this device is from Target_Core_Mod/pSCSI, disable the
1016 * SAM Task Attribute emulation.
1017 *
1018 * This is currently not available in upsream Linux/SCSI Target
1019 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1020 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001021 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001022 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1023 return;
1024 }
1025
1026 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
Andy Grover6708bb22011-06-08 10:36:43 -07001027 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001028 " device\n", dev->transport->name,
1029 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001030}
1031
1032static void scsi_dump_inquiry(struct se_device *dev)
1033{
Andy Grovere3d6f902011-07-19 08:55:10 +00001034 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001035 char buf[17];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001036 int i, device_type;
1037 /*
1038 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1039 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001040 for (i = 0; i < 8; i++)
1041 if (wwn->vendor[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001042 buf[i] = wwn->vendor[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001043 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001044 buf[i] = ' ';
1045 buf[i] = '\0';
1046 pr_debug(" Vendor: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001047
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001048 for (i = 0; i < 16; i++)
1049 if (wwn->model[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001050 buf[i] = wwn->model[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001051 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001052 buf[i] = ' ';
1053 buf[i] = '\0';
1054 pr_debug(" Model: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001055
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001056 for (i = 0; i < 4; i++)
1057 if (wwn->revision[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001058 buf[i] = wwn->revision[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001059 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001060 buf[i] = ' ';
1061 buf[i] = '\0';
1062 pr_debug(" Revision: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001063
Andy Grovere3d6f902011-07-19 08:55:10 +00001064 device_type = dev->transport->get_device_type(dev);
Andy Grover6708bb22011-06-08 10:36:43 -07001065 pr_debug(" Type: %s ", scsi_device_type(device_type));
1066 pr_debug(" ANSI SCSI revision: %02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001067 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001068}
1069
1070struct se_device *transport_add_device_to_core_hba(
1071 struct se_hba *hba,
1072 struct se_subsystem_api *transport,
1073 struct se_subsystem_dev *se_dev,
1074 u32 device_flags,
1075 void *transport_dev,
1076 struct se_dev_limits *dev_limits,
1077 const char *inquiry_prod,
1078 const char *inquiry_rev)
1079{
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001080 int force_pt;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001081 struct se_device *dev;
1082
1083 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001084 if (!dev) {
1085 pr_err("Unable to allocate memory for se_dev_t\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001086 return NULL;
1087 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001088
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001089 dev->dev_flags = device_flags;
1090 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
Andy Grover5951146d2011-07-19 10:26:37 +00001091 dev->dev_ptr = transport_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001092 dev->se_hba = hba;
1093 dev->se_sub_dev = se_dev;
1094 dev->transport = transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001095 INIT_LIST_HEAD(&dev->dev_list);
1096 INIT_LIST_HEAD(&dev->dev_sep_list);
1097 INIT_LIST_HEAD(&dev->dev_tmr_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001098 INIT_LIST_HEAD(&dev->delayed_cmd_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001099 INIT_LIST_HEAD(&dev->state_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001100 INIT_LIST_HEAD(&dev->qf_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001101 spin_lock_init(&dev->execute_task_lock);
1102 spin_lock_init(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001103 spin_lock_init(&dev->dev_reservation_lock);
1104 spin_lock_init(&dev->dev_status_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001105 spin_lock_init(&dev->se_port_lock);
1106 spin_lock_init(&dev->se_tmr_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001107 spin_lock_init(&dev->qf_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001108 atomic_set(&dev->dev_ordered_id, 0);
1109
1110 se_dev_set_default_attribs(dev, dev_limits);
1111
1112 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1113 dev->creation_time = get_jiffies_64();
1114 spin_lock_init(&dev->stats_lock);
1115
1116 spin_lock(&hba->device_lock);
1117 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1118 hba->dev_count++;
1119 spin_unlock(&hba->device_lock);
1120 /*
1121 * Setup the SAM Task Attribute emulation for struct se_device
1122 */
1123 core_setup_task_attr_emulation(dev);
1124 /*
1125 * Force PR and ALUA passthrough emulation with internal object use.
1126 */
1127 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1128 /*
1129 * Setup the Reservations infrastructure for struct se_device
1130 */
1131 core_setup_reservations(dev, force_pt);
1132 /*
1133 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1134 */
1135 if (core_setup_alua(dev, force_pt) < 0)
Dan Carpenter29628462012-07-20 10:07:34 +03001136 goto err_dev_list;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001137
1138 /*
1139 * Startup the struct se_device processing thread
1140 */
Christoph Hellwigaf877292012-07-08 15:58:49 -04001141 dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
1142 dev->transport->name);
1143 if (!dev->tmr_wq) {
1144 pr_err("Unable to create tmr workqueue for %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001145 dev->transport->name);
Dan Carpenter29628462012-07-20 10:07:34 +03001146 goto err_dev_list;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001147 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001148 /*
1149 * Setup work_queue for QUEUE_FULL
1150 */
1151 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001152 /*
1153 * Preload the initial INQUIRY const values if we are doing
1154 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1155 * passthrough because this is being provided by the backend LLD.
1156 * This is required so that transport_get_inquiry() copies these
1157 * originals once back into DEV_T10_WWN(dev) for the virtual device
1158 * setup.
1159 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001160 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
Roland Dreierf22c1192011-05-02 22:15:37 -07001161 if (!inquiry_prod || !inquiry_rev) {
Andy Grover6708bb22011-06-08 10:36:43 -07001162 pr_err("All non TCM/pSCSI plugins require"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001163 " INQUIRY consts\n");
Dan Carpenter29628462012-07-20 10:07:34 +03001164 goto err_wq;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001165 }
1166
Andy Grovere3d6f902011-07-19 08:55:10 +00001167 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1168 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1169 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001170 }
1171 scsi_dump_inquiry(dev);
1172
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001173 return dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001174
Dan Carpenter29628462012-07-20 10:07:34 +03001175err_wq:
1176 destroy_workqueue(dev->tmr_wq);
1177err_dev_list:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001178 spin_lock(&hba->device_lock);
1179 list_del(&dev->dev_list);
1180 hba->dev_count--;
1181 spin_unlock(&hba->device_lock);
1182
1183 se_release_vpd_for_dev(dev);
1184
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001185 kfree(dev);
1186
1187 return NULL;
1188}
1189EXPORT_SYMBOL(transport_add_device_to_core_hba);
1190
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001191int target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
Christoph Hellwig9b3b8042012-05-20 11:59:12 -04001192{
1193 struct se_device *dev = cmd->se_dev;
1194
1195 if (cmd->unknown_data_length) {
1196 cmd->data_length = size;
1197 } else if (size != cmd->data_length) {
1198 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
1199 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
1200 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
1201 cmd->data_length, size, cmd->t_task_cdb[0]);
1202
Christoph Hellwig9b3b8042012-05-20 11:59:12 -04001203 if (cmd->data_direction == DMA_TO_DEVICE) {
1204 pr_err("Rejecting underflow/overflow"
1205 " WRITE data\n");
1206 goto out_invalid_cdb_field;
1207 }
1208 /*
1209 * Reject READ_* or WRITE_* with overflow/underflow for
1210 * type SCF_SCSI_DATA_CDB.
1211 */
1212 if (dev->se_sub_dev->se_dev_attrib.block_size != 512) {
1213 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
1214 " CDB on non 512-byte sector setup subsystem"
1215 " plugin: %s\n", dev->transport->name);
1216 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
1217 goto out_invalid_cdb_field;
1218 }
Nicholas Bellinger4c054ba2012-08-16 15:33:10 -07001219 /*
1220 * For the overflow case keep the existing fabric provided
1221 * ->data_length. Otherwise for the underflow case, reset
1222 * ->data_length to the smaller SCSI expected data transfer
1223 * length.
1224 */
Christoph Hellwig9b3b8042012-05-20 11:59:12 -04001225 if (size > cmd->data_length) {
1226 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
1227 cmd->residual_count = (size - cmd->data_length);
1228 } else {
1229 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
1230 cmd->residual_count = (cmd->data_length - size);
Nicholas Bellinger4c054ba2012-08-16 15:33:10 -07001231 cmd->data_length = size;
Christoph Hellwig9b3b8042012-05-20 11:59:12 -04001232 }
Christoph Hellwig9b3b8042012-05-20 11:59:12 -04001233 }
1234
1235 return 0;
1236
1237out_invalid_cdb_field:
1238 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1239 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1240 return -EINVAL;
1241}
1242
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001243/*
1244 * Used by fabric modules containing a local struct se_cmd within their
1245 * fabric dependent per I/O descriptor.
1246 */
1247void transport_init_se_cmd(
1248 struct se_cmd *cmd,
1249 struct target_core_fabric_ops *tfo,
1250 struct se_session *se_sess,
1251 u32 data_length,
1252 int data_direction,
1253 int task_attr,
1254 unsigned char *sense_buffer)
1255{
Andy Grover5951146d2011-07-19 10:26:37 +00001256 INIT_LIST_HEAD(&cmd->se_lun_node);
1257 INIT_LIST_HEAD(&cmd->se_delayed_node);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001258 INIT_LIST_HEAD(&cmd->se_qf_node);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001259 INIT_LIST_HEAD(&cmd->se_cmd_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001260 INIT_LIST_HEAD(&cmd->state_list);
Andy Grovera1d8b492011-05-02 17:12:10 -07001261 init_completion(&cmd->transport_lun_fe_stop_comp);
1262 init_completion(&cmd->transport_lun_stop_comp);
1263 init_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001264 init_completion(&cmd->cmd_wait_comp);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001265 init_completion(&cmd->task_stop_comp);
Andy Grovera1d8b492011-05-02 17:12:10 -07001266 spin_lock_init(&cmd->t_state_lock);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001267 cmd->transport_state = CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001268
1269 cmd->se_tfo = tfo;
1270 cmd->se_sess = se_sess;
1271 cmd->data_length = data_length;
1272 cmd->data_direction = data_direction;
1273 cmd->sam_task_attr = task_attr;
1274 cmd->sense_buffer = sense_buffer;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001275
1276 cmd->state_active = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001277}
1278EXPORT_SYMBOL(transport_init_se_cmd);
1279
1280static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1281{
1282 /*
1283 * Check if SAM Task Attribute emulation is enabled for this
1284 * struct se_device storage object
1285 */
Andy Grover5951146d2011-07-19 10:26:37 +00001286 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001287 return 0;
1288
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001289 if (cmd->sam_task_attr == MSG_ACA_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001290 pr_debug("SAM Task Attribute ACA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001291 " emulation is not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001292 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001293 }
1294 /*
1295 * Used to determine when ORDERED commands should go from
1296 * Dormant to Active status.
1297 */
Andy Grover5951146d2011-07-19 10:26:37 +00001298 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001299 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001300 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001301 cmd->se_ordered_id, cmd->sam_task_attr,
Andy Grover6708bb22011-06-08 10:36:43 -07001302 cmd->se_dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001303 return 0;
1304}
1305
Andy Grovera12f41f2012-04-03 15:51:20 -07001306/* target_setup_cmd_from_cdb():
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001307 *
1308 * Called from fabric RX Thread.
1309 */
Andy Grovera12f41f2012-04-03 15:51:20 -07001310int target_setup_cmd_from_cdb(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001311 struct se_cmd *cmd,
1312 unsigned char *cdb)
1313{
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001314 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
1315 u32 pr_reg_type = 0;
1316 u8 alua_ascq = 0;
1317 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001318 int ret;
1319
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001320 /*
1321 * Ensure that the received CDB is less than the max (252 + 8) bytes
1322 * for VARIABLE_LENGTH_CMD
1323 */
1324 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001325 pr_err("Received SCSI CDB with command_size: %d that"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001326 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1327 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001328 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1329 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grovere3d6f902011-07-19 08:55:10 +00001330 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001331 }
1332 /*
1333 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1334 * allocate the additional extended CDB buffer now.. Otherwise
1335 * setup the pointer from __t_task_cdb to t_task_cdb.
1336 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001337 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1338 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001339 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001340 if (!cmd->t_task_cdb) {
1341 pr_err("Unable to allocate cmd->t_task_cdb"
Andy Grovera1d8b492011-05-02 17:12:10 -07001342 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001343 scsi_command_size(cdb),
Andy Grovera1d8b492011-05-02 17:12:10 -07001344 (unsigned long)sizeof(cmd->__t_task_cdb));
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001345 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1346 cmd->scsi_sense_reason =
1347 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grovere3d6f902011-07-19 08:55:10 +00001348 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001349 }
1350 } else
Andy Grovera1d8b492011-05-02 17:12:10 -07001351 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001352 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07001353 * Copy the original CDB into cmd->
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001354 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001355 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001356
1357 /*
1358 * Check for an existing UNIT ATTENTION condition
1359 */
1360 if (core_scsi3_ua_check(cmd, cdb) < 0) {
1361 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1362 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
1363 return -EINVAL;
1364 }
1365
1366 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
1367 if (ret != 0) {
1368 /*
1369 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
1370 * The ALUA additional sense code qualifier (ASCQ) is determined
1371 * by the ALUA primary or secondary access state..
1372 */
1373 if (ret > 0) {
1374 pr_debug("[%s]: ALUA TG Port not available, "
1375 "SenseKey: NOT_READY, ASC/ASCQ: "
1376 "0x04/0x%02x\n",
1377 cmd->se_tfo->get_fabric_name(), alua_ascq);
1378
1379 transport_set_sense_codes(cmd, 0x04, alua_ascq);
1380 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1381 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
1382 return -EINVAL;
1383 }
1384 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1385 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1386 return -EINVAL;
1387 }
1388
1389 /*
1390 * Check status for SPC-3 Persistent Reservations
1391 */
1392 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type)) {
1393 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
1394 cmd, cdb, pr_reg_type) != 0) {
1395 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1396 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
1397 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1398 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
1399 return -EBUSY;
1400 }
1401 /*
1402 * This means the CDB is allowed for the SCSI Initiator port
1403 * when said port is *NOT* holding the legacy SPC-2 or
1404 * SPC-3 Persistent Reservation.
1405 */
1406 }
1407
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001408 ret = cmd->se_dev->transport->parse_cdb(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001409 if (ret < 0)
1410 return ret;
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001411
1412 spin_lock_irqsave(&cmd->t_state_lock, flags);
1413 cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1414 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1415
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001416 /*
1417 * Check for SAM Task Attribute Emulation
1418 */
1419 if (transport_check_alloc_task_attr(cmd) < 0) {
1420 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1421 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00001422 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001423 }
1424 spin_lock(&cmd->se_lun->lun_sep_lock);
1425 if (cmd->se_lun->lun_sep)
1426 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1427 spin_unlock(&cmd->se_lun->lun_sep_lock);
1428 return 0;
1429}
Andy Grovera12f41f2012-04-03 15:51:20 -07001430EXPORT_SYMBOL(target_setup_cmd_from_cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001431
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001432/*
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001433 * Used by fabric module frontends to queue tasks directly.
1434 * Many only be used from process context only
1435 */
1436int transport_handle_cdb_direct(
1437 struct se_cmd *cmd)
1438{
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001439 int ret;
1440
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001441 if (!cmd->se_lun) {
1442 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001443 pr_err("cmd->se_lun is NULL\n");
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001444 return -EINVAL;
1445 }
1446 if (in_interrupt()) {
1447 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001448 pr_err("transport_generic_handle_cdb cannot be called"
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001449 " from interrupt context\n");
1450 return -EINVAL;
1451 }
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001452 /*
Christoph Hellwigaf877292012-07-08 15:58:49 -04001453 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that
1454 * outstanding descriptors are handled correctly during shutdown via
1455 * transport_wait_for_tasks()
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001456 *
1457 * Also, we don't take cmd->t_state_lock here as we only expect
1458 * this to be called for initial descriptor submission.
1459 */
1460 cmd->t_state = TRANSPORT_NEW_CMD;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001461 cmd->transport_state |= CMD_T_ACTIVE;
1462
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001463 /*
1464 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1465 * so follow TRANSPORT_NEW_CMD processing thread context usage
1466 * and call transport_generic_request_failure() if necessary..
1467 */
1468 ret = transport_generic_new_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001469 if (ret < 0)
1470 transport_generic_request_failure(cmd);
1471
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001472 return 0;
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001473}
1474EXPORT_SYMBOL(transport_handle_cdb_direct);
1475
Nicholas Bellingera6360782011-11-18 20:36:22 -08001476/**
1477 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1478 *
1479 * @se_cmd: command descriptor to submit
1480 * @se_sess: associated se_sess for endpoint
1481 * @cdb: pointer to SCSI CDB
1482 * @sense: pointer to SCSI sense buffer
1483 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1484 * @data_length: fabric expected data transfer length
1485 * @task_addr: SAM task attribute
1486 * @data_dir: DMA data direction
1487 * @flags: flags for command submission from target_sc_flags_tables
1488 *
Roland Dreierd6dfc862012-07-16 11:04:39 -07001489 * Returns non zero to signal active I/O shutdown failure. All other
1490 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1491 * but still return zero here.
1492 *
Nicholas Bellingera6360782011-11-18 20:36:22 -08001493 * This may only be called from process context, and also currently
1494 * assumes internal allocation of fabric payload buffer by target-core.
1495 **/
Roland Dreierd6dfc862012-07-16 11:04:39 -07001496int target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
Nicholas Bellingera6360782011-11-18 20:36:22 -08001497 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1498 u32 data_length, int task_attr, int data_dir, int flags)
1499{
1500 struct se_portal_group *se_tpg;
1501 int rc;
1502
1503 se_tpg = se_sess->se_tpg;
1504 BUG_ON(!se_tpg);
1505 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1506 BUG_ON(in_interrupt());
1507 /*
1508 * Initialize se_cmd for target operation. From this point
1509 * exceptions are handled by sending exception status via
1510 * target_core_fabric_ops->queue_status() callback
1511 */
1512 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1513 data_length, data_dir, task_attr, sense);
Sebastian Andrzej Siewiorb0d79942012-01-10 14:16:59 +01001514 if (flags & TARGET_SCF_UNKNOWN_SIZE)
1515 se_cmd->unknown_data_length = 1;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001516 /*
1517 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1518 * se_sess->sess_cmd_list. A second kref_get here is necessary
1519 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1520 * kref_put() to happen during fabric packet acknowledgement.
1521 */
Roland Dreierbc187ea2012-07-16 11:04:40 -07001522 rc = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1523 if (rc)
Roland Dreierd6dfc862012-07-16 11:04:39 -07001524 return rc;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001525 /*
1526 * Signal bidirectional data payloads to target-core
1527 */
1528 if (flags & TARGET_SCF_BIDI_OP)
1529 se_cmd->se_cmd_flags |= SCF_BIDI;
1530 /*
1531 * Locate se_lun pointer and attach it to struct se_cmd
1532 */
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001533 if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1534 transport_send_check_condition_and_sense(se_cmd,
1535 se_cmd->scsi_sense_reason, 0);
1536 target_put_sess_cmd(se_sess, se_cmd);
Roland Dreierd6dfc862012-07-16 11:04:39 -07001537 return 0;
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001538 }
Christoph Hellwigd6e01752012-05-20 11:59:14 -04001539
Andy Grovera12f41f2012-04-03 15:51:20 -07001540 rc = target_setup_cmd_from_cdb(se_cmd, cdb);
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001541 if (rc != 0) {
1542 transport_generic_request_failure(se_cmd);
Roland Dreierd6dfc862012-07-16 11:04:39 -07001543 return 0;
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001544 }
Andy Grover11e319e2012-04-03 15:51:28 -07001545
1546 /*
1547 * Check if we need to delay processing because of ALUA
1548 * Active/NonOptimized primary access state..
1549 */
1550 core_alua_check_nonop_delay(se_cmd);
1551
Nicholas Bellingera6360782011-11-18 20:36:22 -08001552 transport_handle_cdb_direct(se_cmd);
Roland Dreierd6dfc862012-07-16 11:04:39 -07001553 return 0;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001554}
1555EXPORT_SYMBOL(target_submit_cmd);
1556
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001557static void target_complete_tmr_failure(struct work_struct *work)
1558{
1559 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
1560
1561 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1562 se_cmd->se_tfo->queue_tm_rsp(se_cmd);
1563 transport_generic_free_cmd(se_cmd, 0);
1564}
1565
Andy Groverea98d7f2012-01-19 13:39:21 -08001566/**
1567 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1568 * for TMR CDBs
1569 *
1570 * @se_cmd: command descriptor to submit
1571 * @se_sess: associated se_sess for endpoint
1572 * @sense: pointer to SCSI sense buffer
1573 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1574 * @fabric_context: fabric context for TMR req
1575 * @tm_type: Type of TM request
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001576 * @gfp: gfp type for caller
1577 * @tag: referenced task tag for TMR_ABORT_TASK
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001578 * @flags: submit cmd flags
Andy Groverea98d7f2012-01-19 13:39:21 -08001579 *
1580 * Callable from all contexts.
1581 **/
1582
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001583int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
Andy Groverea98d7f2012-01-19 13:39:21 -08001584 unsigned char *sense, u32 unpacked_lun,
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001585 void *fabric_tmr_ptr, unsigned char tm_type,
1586 gfp_t gfp, unsigned int tag, int flags)
Andy Groverea98d7f2012-01-19 13:39:21 -08001587{
1588 struct se_portal_group *se_tpg;
1589 int ret;
1590
1591 se_tpg = se_sess->se_tpg;
1592 BUG_ON(!se_tpg);
1593
1594 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1595 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001596 /*
1597 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1598 * allocation failure.
1599 */
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001600 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001601 if (ret < 0)
1602 return -ENOMEM;
Andy Groverea98d7f2012-01-19 13:39:21 -08001603
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001604 if (tm_type == TMR_ABORT_TASK)
1605 se_cmd->se_tmr_req->ref_task_tag = tag;
1606
Andy Groverea98d7f2012-01-19 13:39:21 -08001607 /* See target_submit_cmd for commentary */
Roland Dreierbc187ea2012-07-16 11:04:40 -07001608 ret = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1609 if (ret) {
1610 core_tmr_release_req(se_cmd->se_tmr_req);
1611 return ret;
1612 }
Andy Groverea98d7f2012-01-19 13:39:21 -08001613
Andy Groverea98d7f2012-01-19 13:39:21 -08001614 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1615 if (ret) {
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001616 /*
1617 * For callback during failure handling, push this work off
1618 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1619 */
1620 INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
1621 schedule_work(&se_cmd->work);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001622 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001623 }
1624 transport_generic_handle_tmr(se_cmd);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001625 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001626}
1627EXPORT_SYMBOL(target_submit_tmr);
1628
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001629/*
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001630 * If the cmd is active, request it to be stopped and sleep until it
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001631 * has completed.
1632 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001633bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001634{
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001635 bool was_active = false;
1636
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001637 if (cmd->transport_state & CMD_T_BUSY) {
1638 cmd->transport_state |= CMD_T_REQUEST_STOP;
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001639 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1640
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001641 pr_debug("cmd %p waiting to complete\n", cmd);
1642 wait_for_completion(&cmd->task_stop_comp);
1643 pr_debug("cmd %p stopped successfully\n", cmd);
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001644
1645 spin_lock_irqsave(&cmd->t_state_lock, *flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001646 cmd->transport_state &= ~CMD_T_REQUEST_STOP;
1647 cmd->transport_state &= ~CMD_T_BUSY;
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001648 was_active = true;
1649 }
1650
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001651 return was_active;
1652}
1653
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001654/*
1655 * Handle SAM-esque emulation for generic transport request failures.
1656 */
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001657void transport_generic_request_failure(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001658{
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001659 int ret = 0;
1660
Andy Grover6708bb22011-06-08 10:36:43 -07001661 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001662 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -07001663 cmd->t_task_cdb[0]);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001664 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001665 cmd->se_tfo->get_cmd_state(cmd),
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001666 cmd->t_state, cmd->scsi_sense_reason);
Christoph Hellwigd43d6ae2012-04-24 00:25:08 -04001667 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001668 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1669 (cmd->transport_state & CMD_T_STOP) != 0,
1670 (cmd->transport_state & CMD_T_SENT) != 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001671
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001672 /*
1673 * For SAM Task Attribute emulation for failed struct se_cmd
1674 */
1675 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1676 transport_complete_task_attr(cmd);
1677
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001678 switch (cmd->scsi_sense_reason) {
1679 case TCM_NON_EXISTENT_LUN:
1680 case TCM_UNSUPPORTED_SCSI_OPCODE:
1681 case TCM_INVALID_CDB_FIELD:
1682 case TCM_INVALID_PARAMETER_LIST:
1683 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1684 case TCM_UNKNOWN_MODE_PAGE:
1685 case TCM_WRITE_PROTECTED:
Roland Dreiere2397c72012-07-16 15:34:21 -07001686 case TCM_ADDRESS_OUT_OF_RANGE:
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001687 case TCM_CHECK_CONDITION_ABORT_CMD:
1688 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1689 case TCM_CHECK_CONDITION_NOT_READY:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001690 break;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001691 case TCM_RESERVATION_CONFLICT:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001692 /*
1693 * No SENSE Data payload for this case, set SCSI Status
1694 * and queue the response to $FABRIC_MOD.
1695 *
1696 * Uses linux/include/scsi/scsi.h SAM status codes defs
1697 */
1698 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1699 /*
1700 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1701 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1702 * CONFLICT STATUS.
1703 *
1704 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1705 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001706 if (cmd->se_sess &&
1707 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1708 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001709 cmd->orig_fe_lun, 0x2C,
1710 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1711
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001712 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001713 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001714 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001715 goto check_stop;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001716 default:
Andy Grover6708bb22011-06-08 10:36:43 -07001717 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001718 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001719 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1720 break;
1721 }
Christoph Hellwigf3146432012-07-08 15:58:48 -04001722
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001723 ret = transport_send_check_condition_and_sense(cmd,
1724 cmd->scsi_sense_reason, 0);
1725 if (ret == -EAGAIN || ret == -ENOMEM)
1726 goto queue_full;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001727
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001728check_stop:
1729 transport_lun_remove_cmd(cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07001730 if (!transport_cmd_check_stop_to_fabric(cmd))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001731 ;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001732 return;
1733
1734queue_full:
Christoph Hellwige057f532011-10-17 13:56:41 -04001735 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1736 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001737}
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001738EXPORT_SYMBOL(transport_generic_request_failure);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001739
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001740static void __target_execute_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001741{
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -04001742 int error = 0;
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001743
1744 spin_lock_irq(&cmd->t_state_lock);
1745 cmd->transport_state |= (CMD_T_BUSY|CMD_T_SENT);
1746 spin_unlock_irq(&cmd->t_state_lock);
1747
1748 if (cmd->execute_cmd)
1749 error = cmd->execute_cmd(cmd);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001750
1751 if (error) {
1752 spin_lock_irq(&cmd->t_state_lock);
1753 cmd->transport_state &= ~(CMD_T_BUSY|CMD_T_SENT);
1754 spin_unlock_irq(&cmd->t_state_lock);
1755
1756 transport_generic_request_failure(cmd);
1757 }
1758}
1759
Christoph Hellwig70baf0a2012-07-08 15:58:39 -04001760void target_execute_cmd(struct se_cmd *cmd)
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001761{
1762 struct se_device *dev = cmd->se_dev;
1763
Christoph Hellwigf7113a42012-07-08 15:58:38 -04001764 /*
Christoph Hellwigd59a02b2012-07-08 15:58:40 -04001765 * If the received CDB has aleady been aborted stop processing it here.
1766 */
1767 if (transport_check_aborted_status(cmd, 1))
1768 return;
1769
1770 /*
Christoph Hellwigf7113a42012-07-08 15:58:38 -04001771 * Determine if IOCTL context caller in requesting the stopping of this
1772 * command for LUN shutdown purposes.
1773 */
1774 spin_lock_irq(&cmd->t_state_lock);
1775 if (cmd->transport_state & CMD_T_LUN_STOP) {
1776 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
1777 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
1778
1779 cmd->transport_state &= ~CMD_T_ACTIVE;
1780 spin_unlock_irq(&cmd->t_state_lock);
1781 complete(&cmd->transport_lun_stop_comp);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001782 return;
Christoph Hellwigf7113a42012-07-08 15:58:38 -04001783 }
1784 /*
1785 * Determine if frontend context caller is requesting the stopping of
1786 * this command for frontend exceptions.
1787 */
1788 if (cmd->transport_state & CMD_T_STOP) {
1789 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
1790 __func__, __LINE__,
1791 cmd->se_tfo->get_task_tag(cmd));
1792
1793 spin_unlock_irq(&cmd->t_state_lock);
1794 complete(&cmd->t_transport_stop_comp);
1795 return;
1796 }
1797
1798 cmd->t_state = TRANSPORT_PROCESSING;
1799 spin_unlock_irq(&cmd->t_state_lock);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001800
1801 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1802 goto execute;
1803
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001804 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001805 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001806 * to allow the passed struct se_cmd list of tasks to the front of the list.
1807 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001808 switch (cmd->sam_task_attr) {
1809 case MSG_HEAD_TAG:
1810 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, "
1811 "se_ordered_id: %u\n",
1812 cmd->t_task_cdb[0], cmd->se_ordered_id);
1813 goto execute;
1814 case MSG_ORDERED_TAG:
1815 atomic_inc(&dev->dev_ordered_sync);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001816 smp_mb__after_atomic_inc();
1817
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001818 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, "
1819 " se_ordered_id: %u\n",
1820 cmd->t_task_cdb[0], cmd->se_ordered_id);
1821
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001822 /*
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001823 * Execute an ORDERED command if no other older commands
1824 * exist that need to be completed first.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001825 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001826 if (!atomic_read(&dev->simple_cmds))
1827 goto execute;
1828 break;
1829 default:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001830 /*
1831 * For SIMPLE and UNTAGGED Task Attribute commands
1832 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001833 atomic_inc(&dev->simple_cmds);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001834 smp_mb__after_atomic_inc();
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001835 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001836 }
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001837
1838 if (atomic_read(&dev->dev_ordered_sync) != 0) {
1839 spin_lock(&dev->delayed_cmd_lock);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001840 list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list);
1841 spin_unlock(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001842
Andy Grover6708bb22011-06-08 10:36:43 -07001843 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001844 " delayed CMD list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07001845 cmd->t_task_cdb[0], cmd->sam_task_attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001846 cmd->se_ordered_id);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001847 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001848 }
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001849
1850execute:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001851 /*
1852 * Otherwise, no ORDERED task attributes exist..
1853 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001854 __target_execute_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001855}
Christoph Hellwig70baf0a2012-07-08 15:58:39 -04001856EXPORT_SYMBOL(target_execute_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001857
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001858/*
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001859 * Process all commands up to the last received ORDERED task attribute which
1860 * requires another blocking boundary
1861 */
1862static void target_restart_delayed_cmds(struct se_device *dev)
1863{
1864 for (;;) {
1865 struct se_cmd *cmd;
1866
1867 spin_lock(&dev->delayed_cmd_lock);
1868 if (list_empty(&dev->delayed_cmd_list)) {
1869 spin_unlock(&dev->delayed_cmd_lock);
1870 break;
1871 }
1872
1873 cmd = list_entry(dev->delayed_cmd_list.next,
1874 struct se_cmd, se_delayed_node);
1875 list_del(&cmd->se_delayed_node);
1876 spin_unlock(&dev->delayed_cmd_lock);
1877
1878 __target_execute_cmd(cmd);
1879
1880 if (cmd->sam_task_attr == MSG_ORDERED_TAG)
1881 break;
1882 }
1883}
1884
1885/*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04001886 * Called from I/O completion to determine which dormant/delayed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001887 * and ordered cmds need to have their tasks added to the execution queue.
1888 */
1889static void transport_complete_task_attr(struct se_cmd *cmd)
1890{
Andy Grover5951146d2011-07-19 10:26:37 +00001891 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001892
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001893 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001894 atomic_dec(&dev->simple_cmds);
1895 smp_mb__after_atomic_dec();
1896 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07001897 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001898 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
1899 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001900 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001901 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07001902 pr_debug("Incremented dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001903 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
1904 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001905 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001906 atomic_dec(&dev->dev_ordered_sync);
1907 smp_mb__after_atomic_dec();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001908
1909 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07001910 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001911 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
1912 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001913
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001914 target_restart_delayed_cmds(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001915}
1916
Christoph Hellwige057f532011-10-17 13:56:41 -04001917static void transport_complete_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001918{
1919 int ret = 0;
1920
Christoph Hellwige057f532011-10-17 13:56:41 -04001921 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1922 transport_complete_task_attr(cmd);
1923
1924 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
1925 ret = cmd->se_tfo->queue_status(cmd);
1926 if (ret)
1927 goto out;
1928 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001929
1930 switch (cmd->data_direction) {
1931 case DMA_FROM_DEVICE:
1932 ret = cmd->se_tfo->queue_data_in(cmd);
1933 break;
1934 case DMA_TO_DEVICE:
Andy Groverec98f782011-07-20 19:28:46 +00001935 if (cmd->t_bidi_data_sg) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001936 ret = cmd->se_tfo->queue_data_in(cmd);
1937 if (ret < 0)
Christoph Hellwige057f532011-10-17 13:56:41 -04001938 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001939 }
1940 /* Fall through for DMA_TO_DEVICE */
1941 case DMA_NONE:
1942 ret = cmd->se_tfo->queue_status(cmd);
1943 break;
1944 default:
1945 break;
1946 }
1947
Christoph Hellwige057f532011-10-17 13:56:41 -04001948out:
1949 if (ret < 0) {
1950 transport_handle_queue_full(cmd, cmd->se_dev);
1951 return;
1952 }
1953 transport_lun_remove_cmd(cmd);
1954 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001955}
1956
1957static void transport_handle_queue_full(
1958 struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -04001959 struct se_device *dev)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001960{
1961 spin_lock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001962 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
1963 atomic_inc(&dev->dev_qf_count);
1964 smp_mb__after_atomic_inc();
1965 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
1966
1967 schedule_work(&cmd->se_dev->qf_work_queue);
1968}
1969
Christoph Hellwig35e0e752011-10-17 13:56:53 -04001970static void target_complete_ok_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001971{
Christoph Hellwig35e0e752011-10-17 13:56:53 -04001972 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Paolo Bonzini27a27092012-09-05 17:09:14 +02001973 int ret;
Christoph Hellwig35e0e752011-10-17 13:56:53 -04001974
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001975 /*
1976 * Check if we need to move delayed/dormant tasks from cmds on the
1977 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
1978 * Attribute.
1979 */
Andy Grover5951146d2011-07-19 10:26:37 +00001980 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001981 transport_complete_task_attr(cmd);
1982 /*
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001983 * Check to schedule QUEUE_FULL work, or execute an existing
1984 * cmd->transport_qf_callback()
1985 */
1986 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
1987 schedule_work(&cmd->se_dev->qf_work_queue);
1988
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001989 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001990 * Check if we need to retrieve a sense buffer from
1991 * the struct se_cmd in question.
1992 */
1993 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
Paolo Bonzini27a27092012-09-05 17:09:14 +02001994 WARN_ON(!cmd->scsi_status);
1995 transport_get_sense_data(cmd);
1996 ret = transport_send_check_condition_and_sense(
1997 cmd, 0, 1);
1998 if (ret == -EAGAIN || ret == -ENOMEM)
1999 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002000
Paolo Bonzini27a27092012-09-05 17:09:14 +02002001 transport_lun_remove_cmd(cmd);
2002 transport_cmd_check_stop_to_fabric(cmd);
2003 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002004 }
2005 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002006 * Check for a callback, used by amongst other things
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002007 * XDWRITE_READ_10 emulation.
2008 */
2009 if (cmd->transport_complete_callback)
2010 cmd->transport_complete_callback(cmd);
2011
2012 switch (cmd->data_direction) {
2013 case DMA_FROM_DEVICE:
2014 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00002015 if (cmd->se_lun->lun_sep) {
2016 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002017 cmd->data_length;
2018 }
2019 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002020
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002021 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002022 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002023 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002024 break;
2025 case DMA_TO_DEVICE:
2026 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00002027 if (cmd->se_lun->lun_sep) {
2028 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002029 cmd->data_length;
2030 }
2031 spin_unlock(&cmd->se_lun->lun_sep_lock);
2032 /*
2033 * Check if we need to send READ payload for BIDI-COMMAND
2034 */
Andy Groverec98f782011-07-20 19:28:46 +00002035 if (cmd->t_bidi_data_sg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002036 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00002037 if (cmd->se_lun->lun_sep) {
2038 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002039 cmd->data_length;
2040 }
2041 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002042 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002043 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002044 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002045 break;
2046 }
2047 /* Fall through for DMA_TO_DEVICE */
2048 case DMA_NONE:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002049 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002050 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002051 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002052 break;
2053 default:
2054 break;
2055 }
2056
2057 transport_lun_remove_cmd(cmd);
2058 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002059 return;
2060
2061queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07002062 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002063 " data_direction: %d\n", cmd, cmd->data_direction);
Christoph Hellwige057f532011-10-17 13:56:41 -04002064 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
2065 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002066}
2067
Andy Grover6708bb22011-06-08 10:36:43 -07002068static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002069{
Andy Groverec98f782011-07-20 19:28:46 +00002070 struct scatterlist *sg;
Andy Groverec98f782011-07-20 19:28:46 +00002071 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002072
Andy Grover6708bb22011-06-08 10:36:43 -07002073 for_each_sg(sgl, sg, nents, count)
2074 __free_page(sg_page(sg));
2075
2076 kfree(sgl);
2077}
2078
2079static inline void transport_free_pages(struct se_cmd *cmd)
2080{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002081 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
Andy Grover6708bb22011-06-08 10:36:43 -07002082 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002083
Andy Grover6708bb22011-06-08 10:36:43 -07002084 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00002085 cmd->t_data_sg = NULL;
2086 cmd->t_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002087
Andy Grover6708bb22011-06-08 10:36:43 -07002088 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00002089 cmd->t_bidi_data_sg = NULL;
2090 cmd->t_bidi_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002091}
2092
Christoph Hellwigd3df7822011-09-13 23:08:32 +02002093/**
Christoph Hellwige26d99a2011-11-14 12:30:30 -05002094 * transport_release_cmd - free a command
2095 * @cmd: command to free
2096 *
2097 * This routine unconditionally frees a command, and reference counting
2098 * or list removal must be done in the caller.
2099 */
2100static void transport_release_cmd(struct se_cmd *cmd)
2101{
2102 BUG_ON(!cmd->se_tfo);
2103
Andy Groverc8e31f22012-01-19 13:39:17 -08002104 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Christoph Hellwige26d99a2011-11-14 12:30:30 -05002105 core_tmr_release_req(cmd->se_tmr_req);
2106 if (cmd->t_task_cdb != cmd->__t_task_cdb)
2107 kfree(cmd->t_task_cdb);
2108 /*
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002109 * If this cmd has been setup with target_get_sess_cmd(), drop
2110 * the kref and call ->release_cmd() in kref callback.
Christoph Hellwige26d99a2011-11-14 12:30:30 -05002111 */
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002112 if (cmd->check_release != 0) {
2113 target_put_sess_cmd(cmd->se_sess, cmd);
2114 return;
2115 }
Christoph Hellwige26d99a2011-11-14 12:30:30 -05002116 cmd->se_tfo->release_cmd(cmd);
2117}
2118
2119/**
Christoph Hellwigd3df7822011-09-13 23:08:32 +02002120 * transport_put_cmd - release a reference to a command
2121 * @cmd: command to release
2122 *
2123 * This routine releases our reference to the command and frees it if possible.
2124 */
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002125static void transport_put_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002126{
2127 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002128
Andy Grovera1d8b492011-05-02 17:12:10 -07002129 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02002130 if (atomic_read(&cmd->t_fe_count)) {
2131 if (!atomic_dec_and_test(&cmd->t_fe_count))
2132 goto out_busy;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002133 }
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02002134
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002135 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
2136 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002137 target_remove_from_state_list(cmd);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02002138 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002139 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002140
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002141 transport_free_pages(cmd);
Christoph Hellwig31afc392011-09-13 23:08:11 +02002142 transport_release_cmd(cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002143 return;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02002144out_busy:
2145 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002146}
2147
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002148/*
Andy Groverec98f782011-07-20 19:28:46 +00002149 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
2150 * allocating in the core.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002151 * @cmd: Associated se_cmd descriptor
2152 * @mem: SGL style memory for TCM WRITE / READ
2153 * @sg_mem_num: Number of SGL elements
2154 * @mem_bidi_in: SGL style memory for TCM BIDI READ
2155 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
2156 *
2157 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
2158 * of parameters.
2159 */
2160int transport_generic_map_mem_to_cmd(
2161 struct se_cmd *cmd,
Andy Grover5951146d2011-07-19 10:26:37 +00002162 struct scatterlist *sgl,
2163 u32 sgl_count,
2164 struct scatterlist *sgl_bidi,
2165 u32 sgl_bidi_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002166{
Andy Grover5951146d2011-07-19 10:26:37 +00002167 if (!sgl || !sgl_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002168 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002169
Christoph Hellwig64f1db32012-05-20 11:59:11 -04002170 /*
2171 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
2172 * scatterlists already have been set to follow what the fabric
2173 * passes for the original expected data transfer length.
2174 */
2175 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2176 pr_warn("Rejecting SCSI DATA overflow for fabric using"
2177 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
2178 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2179 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2180 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002181 }
2182
Christoph Hellwig64f1db32012-05-20 11:59:11 -04002183 cmd->t_data_sg = sgl;
2184 cmd->t_data_nents = sgl_count;
2185
2186 if (sgl_bidi && sgl_bidi_count) {
2187 cmd->t_bidi_data_sg = sgl_bidi;
2188 cmd->t_bidi_data_nents = sgl_bidi_count;
2189 }
2190 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002191 return 0;
2192}
2193EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
2194
Andy Grover49493142012-01-16 16:57:08 -08002195void *transport_kmap_data_sg(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002196{
Andy Groverec98f782011-07-20 19:28:46 +00002197 struct scatterlist *sg = cmd->t_data_sg;
Andy Grover49493142012-01-16 16:57:08 -08002198 struct page **pages;
2199 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002200
Andy Groverec98f782011-07-20 19:28:46 +00002201 BUG_ON(!sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002202 /*
Andy Groverec98f782011-07-20 19:28:46 +00002203 * We need to take into account a possible offset here for fabrics like
2204 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
2205 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
Andy Grover05d1c7c2011-07-20 19:13:28 +00002206 */
Andy Grover49493142012-01-16 16:57:08 -08002207 if (!cmd->t_data_nents)
2208 return NULL;
2209 else if (cmd->t_data_nents == 1)
2210 return kmap(sg_page(sg)) + sg->offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002211
Andy Grover49493142012-01-16 16:57:08 -08002212 /* >1 page. use vmap */
2213 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
2214 if (!pages)
2215 return NULL;
2216
2217 /* convert sg[] to pages[] */
2218 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
2219 pages[i] = sg_page(sg);
2220 }
2221
2222 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
2223 kfree(pages);
2224 if (!cmd->t_data_vmap)
2225 return NULL;
2226
2227 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002228}
Andy Grover49493142012-01-16 16:57:08 -08002229EXPORT_SYMBOL(transport_kmap_data_sg);
2230
2231void transport_kunmap_data_sg(struct se_cmd *cmd)
2232{
Andy Grovera1edf9c2012-02-09 12:18:06 -08002233 if (!cmd->t_data_nents) {
Andy Grover49493142012-01-16 16:57:08 -08002234 return;
Andy Grovera1edf9c2012-02-09 12:18:06 -08002235 } else if (cmd->t_data_nents == 1) {
Andy Grover49493142012-01-16 16:57:08 -08002236 kunmap(sg_page(cmd->t_data_sg));
Andy Grovera1edf9c2012-02-09 12:18:06 -08002237 return;
2238 }
Andy Grover49493142012-01-16 16:57:08 -08002239
2240 vunmap(cmd->t_data_vmap);
2241 cmd->t_data_vmap = NULL;
2242}
2243EXPORT_SYMBOL(transport_kunmap_data_sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002244
2245static int
2246transport_generic_get_mem(struct se_cmd *cmd)
2247{
Andy Groverec98f782011-07-20 19:28:46 +00002248 u32 length = cmd->data_length;
2249 unsigned int nents;
2250 struct page *page;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08002251 gfp_t zero_flag;
Andy Groverec98f782011-07-20 19:28:46 +00002252 int i = 0;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002253
Andy Groverec98f782011-07-20 19:28:46 +00002254 nents = DIV_ROUND_UP(length, PAGE_SIZE);
2255 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
2256 if (!cmd->t_data_sg)
Andy Grovere3d6f902011-07-19 08:55:10 +00002257 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002258
Andy Groverec98f782011-07-20 19:28:46 +00002259 cmd->t_data_nents = nents;
2260 sg_init_table(cmd->t_data_sg, nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002261
Christoph Hellwig64f1db32012-05-20 11:59:11 -04002262 zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_CDB ? 0 : __GFP_ZERO;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08002263
Andy Groverec98f782011-07-20 19:28:46 +00002264 while (length) {
2265 u32 page_len = min_t(u32, length, PAGE_SIZE);
roland@purestorage.com9db9da32012-01-04 15:59:58 -08002266 page = alloc_page(GFP_KERNEL | zero_flag);
Andy Groverec98f782011-07-20 19:28:46 +00002267 if (!page)
2268 goto out;
2269
2270 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
2271 length -= page_len;
2272 i++;
2273 }
2274 return 0;
2275
2276out:
Yi Zoud0e27c82012-08-14 16:06:43 -07002277 while (i > 0) {
Andy Groverec98f782011-07-20 19:28:46 +00002278 i--;
Yi Zoud0e27c82012-08-14 16:06:43 -07002279 __free_page(sg_page(&cmd->t_data_sg[i]));
Andy Groverec98f782011-07-20 19:28:46 +00002280 }
2281 kfree(cmd->t_data_sg);
2282 cmd->t_data_sg = NULL;
2283 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002284}
2285
Andy Grovera1d8b492011-05-02 17:12:10 -07002286/*
Andy Groverb16a35b2012-04-03 15:51:21 -07002287 * Allocate any required resources to execute the command. For writes we
2288 * might not have the payload yet, so notify the fabric via a call to
2289 * ->write_pending instead. Otherwise place it on the execution queue.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002290 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002291int transport_generic_new_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002292{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002293 int ret = 0;
2294
2295 /*
2296 * Determine is the TCM fabric module has already allocated physical
2297 * memory, and is directly calling transport_generic_map_mem_to_cmd()
Andy Groverec98f782011-07-20 19:28:46 +00002298 * beforehand.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002299 */
Andy Groverec98f782011-07-20 19:28:46 +00002300 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
2301 cmd->data_length) {
Andy Grover05d1c7c2011-07-20 19:13:28 +00002302 ret = transport_generic_get_mem(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002303 if (ret < 0)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002304 goto out_fail;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002305 }
Nicholas Bellinger74f4cf22012-08-16 17:06:41 -07002306 /*
2307 * If this command doesn't have any payload and we don't have to call
2308 * into the fabric for data transfers, go ahead and complete it right
2309 * away.
2310 */
2311 if (!cmd->data_length) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002312 spin_lock_irq(&cmd->t_state_lock);
Roland Dreier410f6702011-11-22 13:51:32 -08002313 cmd->t_state = TRANSPORT_COMPLETE;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002314 cmd->transport_state |= CMD_T_ACTIVE;
2315 spin_unlock_irq(&cmd->t_state_lock);
Nicholas Bellinger91ec1d32012-01-13 12:01:34 -08002316
2317 if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
2318 u8 ua_asc = 0, ua_ascq = 0;
2319
2320 core_scsi3_ua_clear_for_request_sense(cmd,
2321 &ua_asc, &ua_ascq);
2322 }
2323
Roland Dreier410f6702011-11-22 13:51:32 -08002324 INIT_WORK(&cmd->work, target_complete_ok_work);
2325 queue_work(target_completion_wq, &cmd->work);
2326 return 0;
2327 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04002328
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04002329 atomic_inc(&cmd->t_fe_count);
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04002330
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002331 /*
Christoph Hellwigc3196f02012-07-08 15:58:41 -04002332 * If this command is not a write we can execute it right here,
2333 * for write buffers we need to notify the fabric driver first
2334 * and let it call back once the write buffers are ready.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002335 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04002336 target_add_to_state_list(cmd);
Christoph Hellwigc3196f02012-07-08 15:58:41 -04002337 if (cmd->data_direction != DMA_TO_DEVICE) {
2338 target_execute_cmd(cmd);
2339 return 0;
2340 }
2341
2342 spin_lock_irq(&cmd->t_state_lock);
2343 cmd->t_state = TRANSPORT_WRITE_PENDING;
2344 spin_unlock_irq(&cmd->t_state_lock);
2345
2346 transport_cmd_check_stop(cmd, false);
2347
2348 ret = cmd->se_tfo->write_pending(cmd);
2349 if (ret == -EAGAIN || ret == -ENOMEM)
2350 goto queue_full;
2351
2352 if (ret < 0)
2353 return ret;
2354 return 1;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04002355
2356out_fail:
2357 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2358 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2359 return -EINVAL;
Christoph Hellwigc3196f02012-07-08 15:58:41 -04002360queue_full:
2361 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
2362 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
2363 transport_handle_queue_full(cmd, cmd->se_dev);
2364 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002365}
Andy Grovera1d8b492011-05-02 17:12:10 -07002366EXPORT_SYMBOL(transport_generic_new_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002367
Christoph Hellwige057f532011-10-17 13:56:41 -04002368static void transport_write_pending_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002369{
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002370 int ret;
2371
2372 ret = cmd->se_tfo->write_pending(cmd);
2373 if (ret == -EAGAIN || ret == -ENOMEM) {
Christoph Hellwige057f532011-10-17 13:56:41 -04002374 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
2375 cmd);
2376 transport_handle_queue_full(cmd, cmd->se_dev);
2377 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002378}
2379
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002380void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002381{
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002382 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
Andy Groverc8e31f22012-01-19 13:39:17 -08002383 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002384 transport_wait_for_tasks(cmd);
2385
Christoph Hellwig35462972011-05-31 23:56:57 -04002386 transport_release_cmd(cmd);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002387 } else {
2388 if (wait_for_tasks)
2389 transport_wait_for_tasks(cmd);
2390
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002391 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
2392
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +02002393 if (cmd->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002394 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002395
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002396 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002397 }
2398}
2399EXPORT_SYMBOL(transport_generic_free_cmd);
2400
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002401/* target_get_sess_cmd - Add command to active ->sess_cmd_list
2402 * @se_sess: session to reference
2403 * @se_cmd: command descriptor to add
Nicholas Bellingera6360782011-11-18 20:36:22 -08002404 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002405 */
Roland Dreierbc187ea2012-07-16 11:04:40 -07002406static int target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
2407 bool ack_kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002408{
2409 unsigned long flags;
Roland Dreierbc187ea2012-07-16 11:04:40 -07002410 int ret = 0;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002411
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002412 kref_init(&se_cmd->cmd_kref);
Nicholas Bellingera6360782011-11-18 20:36:22 -08002413 /*
2414 * Add a second kref if the fabric caller is expecting to handle
2415 * fabric acknowledgement that requires two target_put_sess_cmd()
2416 * invocations before se_cmd descriptor release.
2417 */
Nicholas Bellinger86715562012-02-13 01:07:22 -08002418 if (ack_kref == true) {
Nicholas Bellingera6360782011-11-18 20:36:22 -08002419 kref_get(&se_cmd->cmd_kref);
Nicholas Bellinger86715562012-02-13 01:07:22 -08002420 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
2421 }
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002422
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002423 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
Roland Dreierbc187ea2012-07-16 11:04:40 -07002424 if (se_sess->sess_tearing_down) {
2425 ret = -ESHUTDOWN;
2426 goto out;
2427 }
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002428 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
2429 se_cmd->check_release = 1;
Roland Dreierbc187ea2012-07-16 11:04:40 -07002430
2431out:
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002432 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Roland Dreierbc187ea2012-07-16 11:04:40 -07002433 return ret;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002434}
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002435
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002436static void target_release_cmd_kref(struct kref *kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002437{
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002438 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
2439 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002440 unsigned long flags;
2441
2442 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2443 if (list_empty(&se_cmd->se_cmd_list)) {
2444 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Nicholas Bellingerffc32d52012-02-13 02:35:01 -08002445 se_cmd->se_tfo->release_cmd(se_cmd);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002446 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002447 }
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002448 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
2449 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2450 complete(&se_cmd->cmd_wait_comp);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002451 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002452 }
2453 list_del(&se_cmd->se_cmd_list);
2454 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2455
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002456 se_cmd->se_tfo->release_cmd(se_cmd);
2457}
2458
2459/* target_put_sess_cmd - Check for active I/O shutdown via kref_put
2460 * @se_sess: session to reference
2461 * @se_cmd: command descriptor to drop
2462 */
2463int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
2464{
2465 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002466}
2467EXPORT_SYMBOL(target_put_sess_cmd);
2468
Roland Dreier1c7b13f2012-07-16 11:04:42 -07002469/* target_sess_cmd_list_set_waiting - Flag all commands in
2470 * sess_cmd_list to complete cmd_wait_comp. Set
2471 * sess_tearing_down so no more commands are queued.
2472 * @se_sess: session to flag
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002473 */
Roland Dreier1c7b13f2012-07-16 11:04:42 -07002474void target_sess_cmd_list_set_waiting(struct se_session *se_sess)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002475{
2476 struct se_cmd *se_cmd;
2477 unsigned long flags;
2478
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002479 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
Roland Dreier1c7b13f2012-07-16 11:04:42 -07002480
2481 WARN_ON(se_sess->sess_tearing_down);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002482 se_sess->sess_tearing_down = 1;
2483
Roland Dreier1c7b13f2012-07-16 11:04:42 -07002484 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002485 se_cmd->cmd_wait_set = 1;
2486
2487 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2488}
Roland Dreier1c7b13f2012-07-16 11:04:42 -07002489EXPORT_SYMBOL(target_sess_cmd_list_set_waiting);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002490
2491/* target_wait_for_sess_cmds - Wait for outstanding descriptors
2492 * @se_sess: session to wait for active I/O
2493 * @wait_for_tasks: Make extra transport_wait_for_tasks call
2494 */
2495void target_wait_for_sess_cmds(
2496 struct se_session *se_sess,
2497 int wait_for_tasks)
2498{
2499 struct se_cmd *se_cmd, *tmp_cmd;
2500 bool rc = false;
2501
2502 list_for_each_entry_safe(se_cmd, tmp_cmd,
Roland Dreier1c7b13f2012-07-16 11:04:42 -07002503 &se_sess->sess_cmd_list, se_cmd_list) {
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002504 list_del(&se_cmd->se_cmd_list);
2505
2506 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
2507 " %d\n", se_cmd, se_cmd->t_state,
2508 se_cmd->se_tfo->get_cmd_state(se_cmd));
2509
2510 if (wait_for_tasks) {
2511 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
2512 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2513 se_cmd->se_tfo->get_cmd_state(se_cmd));
2514
2515 rc = transport_wait_for_tasks(se_cmd);
2516
2517 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
2518 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2519 se_cmd->se_tfo->get_cmd_state(se_cmd));
2520 }
2521
2522 if (!rc) {
2523 wait_for_completion(&se_cmd->cmd_wait_comp);
2524 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
2525 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2526 se_cmd->se_tfo->get_cmd_state(se_cmd));
2527 }
2528
2529 se_cmd->se_tfo->release_cmd(se_cmd);
2530 }
2531}
2532EXPORT_SYMBOL(target_wait_for_sess_cmds);
2533
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002534/* transport_lun_wait_for_tasks():
2535 *
2536 * Called from ConfigFS context to stop the passed struct se_cmd to allow
2537 * an struct se_lun to be successfully shutdown.
2538 */
2539static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
2540{
2541 unsigned long flags;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002542 int ret = 0;
2543
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002544 /*
2545 * If the frontend has already requested this struct se_cmd to
2546 * be stopped, we can safely ignore this struct se_cmd.
2547 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002548 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002549 if (cmd->transport_state & CMD_T_STOP) {
2550 cmd->transport_state &= ~CMD_T_LUN_STOP;
2551
2552 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
2553 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07002554 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwigf7113a42012-07-08 15:58:38 -04002555 transport_cmd_check_stop(cmd, false);
Andy Grovere3d6f902011-07-19 08:55:10 +00002556 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002557 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002558 cmd->transport_state |= CMD_T_LUN_FE_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07002559 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002560
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002561 // XXX: audit task_flags checks.
2562 spin_lock_irqsave(&cmd->t_state_lock, flags);
2563 if ((cmd->transport_state & CMD_T_BUSY) &&
2564 (cmd->transport_state & CMD_T_SENT)) {
2565 if (!target_stop_cmd(cmd, &flags))
2566 ret++;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002567 }
Christoph Hellwig5f41a312012-05-20 14:34:44 -04002568 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002569
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002570 pr_debug("ConfigFS: cmd: %p stop tasks ret:"
2571 " %d\n", cmd, ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002572 if (!ret) {
Andy Grover6708bb22011-06-08 10:36:43 -07002573 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002574 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07002575 wait_for_completion(&cmd->transport_lun_stop_comp);
Andy Grover6708bb22011-06-08 10:36:43 -07002576 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002577 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002578 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002579
2580 return 0;
2581}
2582
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002583static void __transport_clear_lun_from_sessions(struct se_lun *lun)
2584{
2585 struct se_cmd *cmd = NULL;
2586 unsigned long lun_flags, cmd_flags;
2587 /*
2588 * Do exception processing and return CHECK_CONDITION status to the
2589 * Initiator Port.
2590 */
2591 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
Andy Grover5951146d2011-07-19 10:26:37 +00002592 while (!list_empty(&lun->lun_cmd_list)) {
2593 cmd = list_first_entry(&lun->lun_cmd_list,
2594 struct se_cmd, se_lun_node);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -05002595 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002596
Andy Grovera1d8b492011-05-02 17:12:10 -07002597 spin_lock(&cmd->t_state_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07002598 pr_debug("SE_LUN[%d] - Setting cmd->transport"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002599 "_lun_stop for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002600 cmd->se_lun->unpacked_lun,
2601 cmd->se_tfo->get_task_tag(cmd));
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002602 cmd->transport_state |= CMD_T_LUN_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07002603 spin_unlock(&cmd->t_state_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002604
2605 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
2606
Andy Grover6708bb22011-06-08 10:36:43 -07002607 if (!cmd->se_lun) {
2608 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002609 cmd->se_tfo->get_task_tag(cmd),
2610 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002611 BUG();
2612 }
2613 /*
2614 * If the Storage engine still owns the iscsi_cmd_t, determine
2615 * and/or stop its context.
2616 */
Andy Grover6708bb22011-06-08 10:36:43 -07002617 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
Andy Grovere3d6f902011-07-19 08:55:10 +00002618 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
2619 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002620
Andy Grovere3d6f902011-07-19 08:55:10 +00002621 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002622 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2623 continue;
2624 }
2625
Andy Grover6708bb22011-06-08 10:36:43 -07002626 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002627 "_wait_for_tasks(): SUCCESS\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002628 cmd->se_lun->unpacked_lun,
2629 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002630
Andy Grovera1d8b492011-05-02 17:12:10 -07002631 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002632 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002633 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002634 goto check_cond;
2635 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002636 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002637 target_remove_from_state_list(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002638 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002639
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002640 /*
2641 * The Storage engine stopped this struct se_cmd before it was
2642 * send to the fabric frontend for delivery back to the
2643 * Initiator Node. Return this SCSI CDB back with an
2644 * CHECK_CONDITION status.
2645 */
2646check_cond:
2647 transport_send_check_condition_and_sense(cmd,
2648 TCM_NON_EXISTENT_LUN, 0);
2649 /*
2650 * If the fabric frontend is waiting for this iscsi_cmd_t to
2651 * be released, notify the waiting thread now that LU has
2652 * finished accessing it.
2653 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002654 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002655 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07002656 pr_debug("SE_LUN[%d] - Detected FE stop for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002657 " struct se_cmd: %p ITT: 0x%08x\n",
2658 lun->unpacked_lun,
Andy Grovere3d6f902011-07-19 08:55:10 +00002659 cmd, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002660
Andy Grovera1d8b492011-05-02 17:12:10 -07002661 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002662 cmd_flags);
Christoph Hellwigf7113a42012-07-08 15:58:38 -04002663 transport_cmd_check_stop(cmd, false);
Andy Grovera1d8b492011-05-02 17:12:10 -07002664 complete(&cmd->transport_lun_fe_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002665 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2666 continue;
2667 }
Andy Grover6708bb22011-06-08 10:36:43 -07002668 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002669 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002670
Andy Grovera1d8b492011-05-02 17:12:10 -07002671 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002672 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2673 }
2674 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
2675}
2676
2677static int transport_clear_lun_thread(void *p)
2678{
Jörn Engel8359cf42011-11-24 02:05:51 +01002679 struct se_lun *lun = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002680
2681 __transport_clear_lun_from_sessions(lun);
2682 complete(&lun->lun_shutdown_comp);
2683
2684 return 0;
2685}
2686
2687int transport_clear_lun_from_sessions(struct se_lun *lun)
2688{
2689 struct task_struct *kt;
2690
Andy Grover5951146d2011-07-19 10:26:37 +00002691 kt = kthread_run(transport_clear_lun_thread, lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002692 "tcm_cl_%u", lun->unpacked_lun);
2693 if (IS_ERR(kt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002694 pr_err("Unable to start clear_lun thread\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00002695 return PTR_ERR(kt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002696 }
2697 wait_for_completion(&lun->lun_shutdown_comp);
2698
2699 return 0;
2700}
2701
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002702/**
2703 * transport_wait_for_tasks - wait for completion to occur
2704 * @cmd: command to wait
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002705 *
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002706 * Called from frontend fabric context to wait for storage engine
2707 * to pause and/or release frontend generated struct se_cmd.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002708 */
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002709bool transport_wait_for_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002710{
2711 unsigned long flags;
2712
Andy Grovera1d8b492011-05-02 17:12:10 -07002713 spin_lock_irqsave(&cmd->t_state_lock, flags);
Andy Groverc8e31f22012-01-19 13:39:17 -08002714 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
2715 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002716 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002717 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002718 }
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04002719
Andy Groverc8e31f22012-01-19 13:39:17 -08002720 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
2721 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002722 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002723 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002724 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002725 /*
2726 * If we are already stopped due to an external event (ie: LUN shutdown)
2727 * sleep until the connection can have the passed struct se_cmd back.
Andy Grovera1d8b492011-05-02 17:12:10 -07002728 * The cmd->transport_lun_stopped_sem will be upped by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002729 * transport_clear_lun_from_sessions() once the ConfigFS context caller
2730 * has completed its operation on the struct se_cmd.
2731 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002732 if (cmd->transport_state & CMD_T_LUN_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07002733 pr_debug("wait_for_tasks: Stopping"
Andy Grovere3d6f902011-07-19 08:55:10 +00002734 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002735 "_stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002736 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002737 /*
2738 * There is a special case for WRITES where a FE exception +
2739 * LUN shutdown means ConfigFS context is still sleeping on
2740 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
2741 * We go ahead and up transport_lun_stop_comp just to be sure
2742 * here.
2743 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002744 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2745 complete(&cmd->transport_lun_stop_comp);
2746 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
2747 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002748
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002749 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002750 /*
2751 * At this point, the frontend who was the originator of this
2752 * struct se_cmd, now owns the structure and can be released through
2753 * normal means below.
2754 */
Andy Grover6708bb22011-06-08 10:36:43 -07002755 pr_debug("wait_for_tasks: Stopped"
Andy Grovere3d6f902011-07-19 08:55:10 +00002756 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002757 "stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002758 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002759
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002760 cmd->transport_state &= ~CMD_T_LUN_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002761 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002762
Nicholas Bellinger3d289342012-02-13 02:38:14 -08002763 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002764 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002765 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002766 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002767
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002768 cmd->transport_state |= CMD_T_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002769
Andy Grover6708bb22011-06-08 10:36:43 -07002770 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002771 " i_state: %d, t_state: %d, CMD_T_STOP\n",
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04002772 cmd, cmd->se_tfo->get_task_tag(cmd),
2773 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002774
Andy Grovera1d8b492011-05-02 17:12:10 -07002775 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002776
Andy Grovera1d8b492011-05-02 17:12:10 -07002777 wait_for_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002778
Andy Grovera1d8b492011-05-02 17:12:10 -07002779 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002780 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002781
Andy Grover6708bb22011-06-08 10:36:43 -07002782 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
Andy Grovera1d8b492011-05-02 17:12:10 -07002783 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002784 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002785
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002786 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002787
2788 return true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002789}
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002790EXPORT_SYMBOL(transport_wait_for_tasks);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002791
2792static int transport_get_sense_codes(
2793 struct se_cmd *cmd,
2794 u8 *asc,
2795 u8 *ascq)
2796{
2797 *asc = cmd->scsi_asc;
2798 *ascq = cmd->scsi_ascq;
2799
2800 return 0;
2801}
2802
2803static int transport_set_sense_codes(
2804 struct se_cmd *cmd,
2805 u8 asc,
2806 u8 ascq)
2807{
2808 cmd->scsi_asc = asc;
2809 cmd->scsi_ascq = ascq;
2810
2811 return 0;
2812}
2813
2814int transport_send_check_condition_and_sense(
2815 struct se_cmd *cmd,
2816 u8 reason,
2817 int from_transport)
2818{
2819 unsigned char *buffer = cmd->sense_buffer;
2820 unsigned long flags;
2821 int offset;
2822 u8 asc = 0, ascq = 0;
2823
Andy Grovera1d8b492011-05-02 17:12:10 -07002824 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002825 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002826 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002827 return 0;
2828 }
2829 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
Andy Grovera1d8b492011-05-02 17:12:10 -07002830 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002831
2832 if (!reason && from_transport)
2833 goto after_reason;
2834
2835 if (!from_transport)
2836 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
2837 /*
2838 * Data Segment and SenseLength of the fabric response PDU.
2839 *
2840 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
2841 * from include/scsi/scsi_cmnd.h
2842 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002843 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002844 TRANSPORT_SENSE_BUFFER);
2845 /*
2846 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
2847 * SENSE KEY values from include/scsi/scsi.h
2848 */
2849 switch (reason) {
2850 case TCM_NON_EXISTENT_LUN:
Nicholas Bellingereb39d342011-07-26 16:59:00 -07002851 /* CURRENT ERROR */
2852 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002853 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingereb39d342011-07-26 16:59:00 -07002854 /* ILLEGAL REQUEST */
2855 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2856 /* LOGICAL UNIT NOT SUPPORTED */
2857 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
2858 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002859 case TCM_UNSUPPORTED_SCSI_OPCODE:
2860 case TCM_SECTOR_COUNT_TOO_MANY:
2861 /* CURRENT ERROR */
2862 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002863 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002864 /* ILLEGAL REQUEST */
2865 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2866 /* INVALID COMMAND OPERATION CODE */
2867 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
2868 break;
2869 case TCM_UNKNOWN_MODE_PAGE:
2870 /* CURRENT ERROR */
2871 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002872 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002873 /* ILLEGAL REQUEST */
2874 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2875 /* INVALID FIELD IN CDB */
2876 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
2877 break;
2878 case TCM_CHECK_CONDITION_ABORT_CMD:
2879 /* CURRENT ERROR */
2880 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002881 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002882 /* ABORTED COMMAND */
2883 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2884 /* BUS DEVICE RESET FUNCTION OCCURRED */
2885 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
2886 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
2887 break;
2888 case TCM_INCORRECT_AMOUNT_OF_DATA:
2889 /* CURRENT ERROR */
2890 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002891 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002892 /* ABORTED COMMAND */
2893 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2894 /* WRITE ERROR */
2895 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
2896 /* NOT ENOUGH UNSOLICITED DATA */
2897 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
2898 break;
2899 case TCM_INVALID_CDB_FIELD:
2900 /* CURRENT ERROR */
2901 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002902 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08002903 /* ILLEGAL REQUEST */
2904 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002905 /* INVALID FIELD IN CDB */
2906 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
2907 break;
2908 case TCM_INVALID_PARAMETER_LIST:
2909 /* CURRENT ERROR */
2910 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002911 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08002912 /* ILLEGAL REQUEST */
2913 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002914 /* INVALID FIELD IN PARAMETER LIST */
2915 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
2916 break;
2917 case TCM_UNEXPECTED_UNSOLICITED_DATA:
2918 /* CURRENT ERROR */
2919 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002920 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002921 /* ABORTED COMMAND */
2922 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2923 /* WRITE ERROR */
2924 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
2925 /* UNEXPECTED_UNSOLICITED_DATA */
2926 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
2927 break;
2928 case TCM_SERVICE_CRC_ERROR:
2929 /* CURRENT ERROR */
2930 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002931 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002932 /* ABORTED COMMAND */
2933 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2934 /* PROTOCOL SERVICE CRC ERROR */
2935 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
2936 /* N/A */
2937 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
2938 break;
2939 case TCM_SNACK_REJECTED:
2940 /* CURRENT ERROR */
2941 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002942 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002943 /* ABORTED COMMAND */
2944 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2945 /* READ ERROR */
2946 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
2947 /* FAILED RETRANSMISSION REQUEST */
2948 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
2949 break;
2950 case TCM_WRITE_PROTECTED:
2951 /* CURRENT ERROR */
2952 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002953 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002954 /* DATA PROTECT */
2955 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
2956 /* WRITE PROTECTED */
2957 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
2958 break;
Roland Dreiere2397c72012-07-16 15:34:21 -07002959 case TCM_ADDRESS_OUT_OF_RANGE:
2960 /* CURRENT ERROR */
2961 buffer[offset] = 0x70;
2962 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
2963 /* ILLEGAL REQUEST */
2964 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2965 /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
2966 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x21;
2967 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002968 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
2969 /* CURRENT ERROR */
2970 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002971 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002972 /* UNIT ATTENTION */
2973 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
2974 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
2975 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
2976 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
2977 break;
2978 case TCM_CHECK_CONDITION_NOT_READY:
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 /* Not Ready */
2983 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
2984 transport_get_sense_codes(cmd, &asc, &ascq);
2985 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
2986 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
2987 break;
2988 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
2989 default:
2990 /* CURRENT ERROR */
2991 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08002992 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002993 /* ILLEGAL REQUEST */
2994 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2995 /* LOGICAL UNIT COMMUNICATION FAILURE */
2996 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
2997 break;
2998 }
2999 /*
3000 * This code uses linux/include/scsi/scsi.h SAM status codes!
3001 */
3002 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
3003 /*
3004 * Automatically padded, this value is encoded in the fabric's
3005 * data_length response PDU containing the SCSI defined sense data.
3006 */
3007 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
3008
3009after_reason:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003010 return cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003011}
3012EXPORT_SYMBOL(transport_send_check_condition_and_sense);
3013
3014int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
3015{
3016 int ret = 0;
3017
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003018 if (cmd->transport_state & CMD_T_ABORTED) {
Andy Grover6708bb22011-06-08 10:36:43 -07003019 if (!send_status ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003020 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
3021 return 1;
Andy Grover8b1e1242012-04-03 15:51:12 -07003022
Andy Grover6708bb22011-06-08 10:36:43 -07003023 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003024 " status for CDB: 0x%02x ITT: 0x%08x\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07003025 cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00003026 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07003027
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003028 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
Andy Grovere3d6f902011-07-19 08:55:10 +00003029 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003030 ret = 1;
3031 }
3032 return ret;
3033}
3034EXPORT_SYMBOL(transport_check_aborted_status);
3035
3036void transport_send_task_abort(struct se_cmd *cmd)
3037{
Nicholas Bellingerc252f002011-09-29 14:22:13 -07003038 unsigned long flags;
3039
3040 spin_lock_irqsave(&cmd->t_state_lock, flags);
3041 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
3042 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3043 return;
3044 }
3045 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3046
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003047 /*
3048 * If there are still expected incoming fabric WRITEs, we wait
3049 * until until they have completed before sending a TASK_ABORTED
3050 * response. This response with TASK_ABORTED status will be
3051 * queued back to fabric module by transport_check_aborted_status().
3052 */
3053 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grovere3d6f902011-07-19 08:55:10 +00003054 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003055 cmd->transport_state |= CMD_T_ABORTED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003056 smp_mb__after_atomic_inc();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003057 }
3058 }
3059 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
Andy Grover8b1e1242012-04-03 15:51:12 -07003060
Andy Grover6708bb22011-06-08 10:36:43 -07003061 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
Andy Grovera1d8b492011-05-02 17:12:10 -07003062 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00003063 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07003064
Andy Grovere3d6f902011-07-19 08:55:10 +00003065 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003066}
3067
Christoph Hellwigaf877292012-07-08 15:58:49 -04003068static void target_tmr_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003069{
Christoph Hellwigaf877292012-07-08 15:58:49 -04003070 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Andy Grover5951146d2011-07-19 10:26:37 +00003071 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003072 struct se_tmr_req *tmr = cmd->se_tmr_req;
3073 int ret;
3074
3075 switch (tmr->function) {
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003076 case TMR_ABORT_TASK:
Nicholas Bellinger3d289342012-02-13 02:38:14 -08003077 core_tmr_abort_task(dev, tmr, cmd->se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003078 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003079 case TMR_ABORT_TASK_SET:
3080 case TMR_CLEAR_ACA:
3081 case TMR_CLEAR_TASK_SET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003082 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
3083 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003084 case TMR_LUN_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003085 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
3086 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
3087 TMR_FUNCTION_REJECTED;
3088 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003089 case TMR_TARGET_WARM_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003090 tmr->response = TMR_FUNCTION_REJECTED;
3091 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07003092 case TMR_TARGET_COLD_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003093 tmr->response = TMR_FUNCTION_REJECTED;
3094 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003095 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003096 pr_err("Uknown TMR function: 0x%02x.\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003097 tmr->function);
3098 tmr->response = TMR_FUNCTION_REJECTED;
3099 break;
3100 }
3101
3102 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
Andy Grovere3d6f902011-07-19 08:55:10 +00003103 cmd->se_tfo->queue_tm_rsp(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003104
Christoph Hellwigb7b8bef2011-10-17 13:56:44 -04003105 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003106}
3107
Christoph Hellwigaf877292012-07-08 15:58:49 -04003108int transport_generic_handle_tmr(
3109 struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003110{
Christoph Hellwigaf877292012-07-08 15:58:49 -04003111 INIT_WORK(&cmd->work, target_tmr_work);
3112 queue_work(cmd->se_dev->tmr_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003113 return 0;
3114}
Christoph Hellwigaf877292012-07-08 15:58:49 -04003115EXPORT_SYMBOL(transport_generic_handle_tmr);