blob: 28df7b8ea9c418e54df1eced223691dd5e642b4f [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_transport.c
3 *
4 * This file contains the Generic Target Engine Core.
5 *
6 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
7 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
8 * Copyright (c) 2007-2010 Rising Tide Systems
9 * Copyright (c) 2008-2010 Linux-iSCSI.org
10 *
11 * Nicholas A. Bellinger <nab@kernel.org>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 *
27 ******************************************************************************/
28
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080029#include <linux/net.h>
30#include <linux/delay.h>
31#include <linux/string.h>
32#include <linux/timer.h>
33#include <linux/slab.h>
34#include <linux/blkdev.h>
35#include <linux/spinlock.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080036#include <linux/kthread.h>
37#include <linux/in.h>
38#include <linux/cdrom.h>
Paul Gortmaker827509e2011-08-30 14:20:44 -040039#include <linux/module.h>
Roland Dreier015487b2012-02-13 16:18:17 -080040#include <linux/ratelimit.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080041#include <asm/unaligned.h>
42#include <net/sock.h>
43#include <net/tcp.h>
44#include <scsi/scsi.h>
45#include <scsi/scsi_cmnd.h>
Nicholas Bellingere66ecd52011-05-19 20:19:14 -070046#include <scsi/scsi_tcq.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080047
48#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050049#include <target/target_core_backend.h>
50#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080051#include <target/target_core_configfs.h>
52
Christoph Hellwige26d99a2011-11-14 12:30:30 -050053#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080054#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080055#include "target_core_pr.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080056#include "target_core_ua.h"
57
Andy Grovere3d6f902011-07-19 08:55:10 +000058static int sub_api_initialized;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080059
Christoph Hellwig35e0e752011-10-17 13:56:53 -040060static struct workqueue_struct *target_completion_wq;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080061static struct kmem_cache *se_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080062struct kmem_cache *se_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080063struct kmem_cache *t10_pr_reg_cache;
64struct kmem_cache *t10_alua_lu_gp_cache;
65struct kmem_cache *t10_alua_lu_gp_mem_cache;
66struct kmem_cache *t10_alua_tg_pt_gp_cache;
67struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
68
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080069static int transport_generic_write_pending(struct se_cmd *);
Andy Grover5951146d2011-07-19 10:26:37 +000070static int transport_processing_thread(void *param);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -080071static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080072static void transport_complete_task_attr(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -070073static void transport_handle_queue_full(struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -040074 struct se_device *dev);
Andy Grover05d1c7c2011-07-20 19:13:28 +000075static int transport_generic_get_mem(struct se_cmd *cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -070076static void transport_put_cmd(struct se_cmd *cmd);
Christoph Hellwig3df8d402011-10-17 13:56:43 -040077static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
Christoph Hellwig35e0e752011-10-17 13:56:53 -040079static void target_complete_ok_work(struct work_struct *work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080080
Andy Grovere3d6f902011-07-19 08:55:10 +000081int init_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080082{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083 se_sess_cache = kmem_cache_create("se_sess_cache",
84 sizeof(struct se_session), __alignof__(struct se_session),
85 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070086 if (!se_sess_cache) {
87 pr_err("kmem_cache_create() for struct se_session"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080088 " failed\n");
Andy Groverc8e31f22012-01-19 13:39:17 -080089 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080090 }
91 se_ua_cache = kmem_cache_create("se_ua_cache",
92 sizeof(struct se_ua), __alignof__(struct se_ua),
93 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070094 if (!se_ua_cache) {
95 pr_err("kmem_cache_create() for struct se_ua failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -040096 goto out_free_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080097 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080098 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
99 sizeof(struct t10_pr_registration),
100 __alignof__(struct t10_pr_registration), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700101 if (!t10_pr_reg_cache) {
102 pr_err("kmem_cache_create() for struct t10_pr_registration"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800103 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400104 goto out_free_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800105 }
106 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
107 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
108 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700109 if (!t10_alua_lu_gp_cache) {
110 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800111 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400112 goto out_free_pr_reg_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800113 }
114 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
115 sizeof(struct t10_alua_lu_gp_member),
116 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700117 if (!t10_alua_lu_gp_mem_cache) {
118 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800119 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400120 goto out_free_lu_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800121 }
122 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
123 sizeof(struct t10_alua_tg_pt_gp),
124 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700125 if (!t10_alua_tg_pt_gp_cache) {
126 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800127 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400128 goto out_free_lu_gp_mem_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800129 }
130 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
131 "t10_alua_tg_pt_gp_mem_cache",
132 sizeof(struct t10_alua_tg_pt_gp_member),
133 __alignof__(struct t10_alua_tg_pt_gp_member),
134 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700135 if (!t10_alua_tg_pt_gp_mem_cache) {
136 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800137 "mem_t failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400138 goto out_free_tg_pt_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800139 }
140
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400141 target_completion_wq = alloc_workqueue("target_completion",
142 WQ_MEM_RECLAIM, 0);
143 if (!target_completion_wq)
144 goto out_free_tg_pt_gp_mem_cache;
145
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800146 return 0;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400147
148out_free_tg_pt_gp_mem_cache:
149 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
150out_free_tg_pt_gp_cache:
151 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
152out_free_lu_gp_mem_cache:
153 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
154out_free_lu_gp_cache:
155 kmem_cache_destroy(t10_alua_lu_gp_cache);
156out_free_pr_reg_cache:
157 kmem_cache_destroy(t10_pr_reg_cache);
158out_free_ua_cache:
159 kmem_cache_destroy(se_ua_cache);
160out_free_sess_cache:
161 kmem_cache_destroy(se_sess_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800162out:
Andy Grovere3d6f902011-07-19 08:55:10 +0000163 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800164}
165
Andy Grovere3d6f902011-07-19 08:55:10 +0000166void release_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800167{
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400168 destroy_workqueue(target_completion_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800169 kmem_cache_destroy(se_sess_cache);
170 kmem_cache_destroy(se_ua_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800171 kmem_cache_destroy(t10_pr_reg_cache);
172 kmem_cache_destroy(t10_alua_lu_gp_cache);
173 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
174 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
175 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800176}
177
Andy Grovere3d6f902011-07-19 08:55:10 +0000178/* This code ensures unique mib indexes are handed out. */
179static DEFINE_SPINLOCK(scsi_mib_index_lock);
180static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800181
182/*
183 * Allocate a new row index for the entry type specified
184 */
185u32 scsi_get_new_index(scsi_index_t type)
186{
187 u32 new_index;
188
Andy Grovere3d6f902011-07-19 08:55:10 +0000189 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800190
Andy Grovere3d6f902011-07-19 08:55:10 +0000191 spin_lock(&scsi_mib_index_lock);
192 new_index = ++scsi_mib_index[type];
193 spin_unlock(&scsi_mib_index_lock);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800194
195 return new_index;
196}
197
Christoph Hellwige26d99a2011-11-14 12:30:30 -0500198static void transport_init_queue_obj(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800199{
200 atomic_set(&qobj->queue_cnt, 0);
201 INIT_LIST_HEAD(&qobj->qobj_list);
202 init_waitqueue_head(&qobj->thread_wq);
203 spin_lock_init(&qobj->cmd_queue_lock);
204}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800205
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700206void transport_subsystem_check_init(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800207{
208 int ret;
209
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700210 if (sub_api_initialized)
211 return;
212
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800213 ret = request_module("target_core_iblock");
214 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700215 pr_err("Unable to load target_core_iblock\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800216
217 ret = request_module("target_core_file");
218 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700219 pr_err("Unable to load target_core_file\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800220
221 ret = request_module("target_core_pscsi");
222 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700223 pr_err("Unable to load target_core_pscsi\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800224
225 ret = request_module("target_core_stgt");
226 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700227 pr_err("Unable to load target_core_stgt\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800228
Andy Grovere3d6f902011-07-19 08:55:10 +0000229 sub_api_initialized = 1;
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700230 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800231}
232
233struct se_session *transport_init_session(void)
234{
235 struct se_session *se_sess;
236
237 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700238 if (!se_sess) {
239 pr_err("Unable to allocate struct se_session from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800240 " se_sess_cache\n");
241 return ERR_PTR(-ENOMEM);
242 }
243 INIT_LIST_HEAD(&se_sess->sess_list);
244 INIT_LIST_HEAD(&se_sess->sess_acl_list);
Nicholas Bellingera17f0912011-11-02 21:52:08 -0700245 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
246 INIT_LIST_HEAD(&se_sess->sess_wait_list);
247 spin_lock_init(&se_sess->sess_cmd_lock);
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800248 kref_init(&se_sess->sess_kref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800249
250 return se_sess;
251}
252EXPORT_SYMBOL(transport_init_session);
253
254/*
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700255 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800256 */
257void __transport_register_session(
258 struct se_portal_group *se_tpg,
259 struct se_node_acl *se_nacl,
260 struct se_session *se_sess,
261 void *fabric_sess_ptr)
262{
263 unsigned char buf[PR_REG_ISID_LEN];
264
265 se_sess->se_tpg = se_tpg;
266 se_sess->fabric_sess_ptr = fabric_sess_ptr;
267 /*
268 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
269 *
270 * Only set for struct se_session's that will actually be moving I/O.
271 * eg: *NOT* discovery sessions.
272 */
273 if (se_nacl) {
274 /*
275 * If the fabric module supports an ISID based TransportID,
276 * save this value in binary from the fabric I_T Nexus now.
277 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000278 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800279 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +0000280 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800281 &buf[0], PR_REG_ISID_LEN);
282 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
283 }
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800284 kref_get(&se_nacl->acl_kref);
285
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800286 spin_lock_irq(&se_nacl->nacl_sess_lock);
287 /*
288 * The se_nacl->nacl_sess pointer will be set to the
289 * last active I_T Nexus for each struct se_node_acl.
290 */
291 se_nacl->nacl_sess = se_sess;
292
293 list_add_tail(&se_sess->sess_acl_list,
294 &se_nacl->acl_sess_list);
295 spin_unlock_irq(&se_nacl->nacl_sess_lock);
296 }
297 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
298
Andy Grover6708bb22011-06-08 10:36:43 -0700299 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000300 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800301}
302EXPORT_SYMBOL(__transport_register_session);
303
304void transport_register_session(
305 struct se_portal_group *se_tpg,
306 struct se_node_acl *se_nacl,
307 struct se_session *se_sess,
308 void *fabric_sess_ptr)
309{
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700310 unsigned long flags;
311
312 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800313 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700314 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800315}
316EXPORT_SYMBOL(transport_register_session);
317
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800318static void target_release_session(struct kref *kref)
319{
320 struct se_session *se_sess = container_of(kref,
321 struct se_session, sess_kref);
322 struct se_portal_group *se_tpg = se_sess->se_tpg;
323
324 se_tpg->se_tpg_tfo->close_session(se_sess);
325}
326
327void target_get_session(struct se_session *se_sess)
328{
329 kref_get(&se_sess->sess_kref);
330}
331EXPORT_SYMBOL(target_get_session);
332
333int target_put_session(struct se_session *se_sess)
334{
335 return kref_put(&se_sess->sess_kref, target_release_session);
336}
337EXPORT_SYMBOL(target_put_session);
338
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800339static void target_complete_nacl(struct kref *kref)
340{
341 struct se_node_acl *nacl = container_of(kref,
342 struct se_node_acl, acl_kref);
343
344 complete(&nacl->acl_free_comp);
345}
346
347void target_put_nacl(struct se_node_acl *nacl)
348{
349 kref_put(&nacl->acl_kref, target_complete_nacl);
350}
351
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800352void transport_deregister_session_configfs(struct se_session *se_sess)
353{
354 struct se_node_acl *se_nacl;
Roland Dreier23388862011-06-22 01:02:21 -0700355 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800356 /*
357 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
358 */
359 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700360 if (se_nacl) {
Roland Dreier23388862011-06-22 01:02:21 -0700361 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800362 if (se_nacl->acl_stop == 0)
363 list_del(&se_sess->sess_acl_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800364 /*
365 * If the session list is empty, then clear the pointer.
366 * Otherwise, set the struct se_session pointer from the tail
367 * element of the per struct se_node_acl active session list.
368 */
369 if (list_empty(&se_nacl->acl_sess_list))
370 se_nacl->nacl_sess = NULL;
371 else {
372 se_nacl->nacl_sess = container_of(
373 se_nacl->acl_sess_list.prev,
374 struct se_session, sess_acl_list);
375 }
Roland Dreier23388862011-06-22 01:02:21 -0700376 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800377 }
378}
379EXPORT_SYMBOL(transport_deregister_session_configfs);
380
381void transport_free_session(struct se_session *se_sess)
382{
383 kmem_cache_free(se_sess_cache, se_sess);
384}
385EXPORT_SYMBOL(transport_free_session);
386
387void transport_deregister_session(struct se_session *se_sess)
388{
389 struct se_portal_group *se_tpg = se_sess->se_tpg;
Nicholas Bellinger01468342012-03-10 14:32:52 -0800390 struct target_core_fabric_ops *se_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800391 struct se_node_acl *se_nacl;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700392 unsigned long flags;
Nicholas Bellinger01468342012-03-10 14:32:52 -0800393 bool comp_nacl = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800394
Andy Grover6708bb22011-06-08 10:36:43 -0700395 if (!se_tpg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800396 transport_free_session(se_sess);
397 return;
398 }
Nicholas Bellinger01468342012-03-10 14:32:52 -0800399 se_tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800400
Roland Dreiere63a8e12011-08-12 16:01:02 -0700401 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800402 list_del(&se_sess->sess_list);
403 se_sess->se_tpg = NULL;
404 se_sess->fabric_sess_ptr = NULL;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700405 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406
407 /*
408 * Determine if we need to do extra work for this initiator node's
409 * struct se_node_acl if it had been previously dynamically generated.
410 */
411 se_nacl = se_sess->se_node_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800412
Nicholas Bellinger01468342012-03-10 14:32:52 -0800413 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
414 if (se_nacl && se_nacl->dynamic_node_acl) {
415 if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
416 list_del(&se_nacl->acl_list);
417 se_tpg->num_node_acls--;
418 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
419 core_tpg_wait_for_nacl_pr_ref(se_nacl);
420 core_free_device_list_for_node(se_nacl, se_tpg);
421 se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl);
422
423 comp_nacl = false;
424 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800425 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800426 }
Nicholas Bellinger01468342012-03-10 14:32:52 -0800427 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800428
Andy Grover6708bb22011-06-08 10:36:43 -0700429 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000430 se_tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellinger01468342012-03-10 14:32:52 -0800431 /*
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800432 * If last kref is dropping now for an explict NodeACL, awake sleeping
433 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
434 * removal context.
Nicholas Bellinger01468342012-03-10 14:32:52 -0800435 */
436 if (se_nacl && comp_nacl == true)
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800437 target_put_nacl(se_nacl);
Nicholas Bellinger01468342012-03-10 14:32:52 -0800438
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800439 transport_free_session(se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800440}
441EXPORT_SYMBOL(transport_deregister_session);
442
443/*
Andy Grovera1d8b492011-05-02 17:12:10 -0700444 * Called with cmd->t_state_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800445 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400446static void target_remove_from_state_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800447{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400448 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800449 unsigned long flags;
450
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400451 if (!dev)
452 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800453
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400454 if (cmd->transport_state & CMD_T_BUSY)
455 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800456
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400457 spin_lock_irqsave(&dev->execute_task_lock, flags);
458 if (cmd->state_active) {
459 list_del(&cmd->state_list);
460 atomic_dec(&cmd->t_task_cdbs_ex_left);
461 cmd->state_active = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800462 }
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400463 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800464}
465
466/* transport_cmd_check_stop():
467 *
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500468 * 'transport_off = 1' determines if CMD_T_ACTIVE should be cleared.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800469 * 'transport_off = 2' determines if task_dev_state should be removed.
470 *
471 * A non-zero u8 t_state sets cmd->t_state.
472 * Returns 1 when command is stopped, else 0.
473 */
474static int transport_cmd_check_stop(
475 struct se_cmd *cmd,
476 int transport_off,
477 u8 t_state)
478{
479 unsigned long flags;
480
Andy Grovera1d8b492011-05-02 17:12:10 -0700481 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800482 /*
483 * Determine if IOCTL context caller in requesting the stopping of this
484 * command for LUN shutdown purposes.
485 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500486 if (cmd->transport_state & CMD_T_LUN_STOP) {
487 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
488 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800489
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500490 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800491 if (transport_off == 2)
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400492 target_remove_from_state_list(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700493 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800494
Andy Grovera1d8b492011-05-02 17:12:10 -0700495 complete(&cmd->transport_lun_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800496 return 1;
497 }
498 /*
499 * Determine if frontend context caller is requesting the stopping of
Andy Grovere3d6f902011-07-19 08:55:10 +0000500 * this command for frontend exceptions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800501 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500502 if (cmd->transport_state & CMD_T_STOP) {
503 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
504 __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000505 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800506
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800507 if (transport_off == 2)
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400508 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800509
510 /*
511 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
512 * to FE.
513 */
514 if (transport_off == 2)
515 cmd->se_lun = NULL;
Andy Grovera1d8b492011-05-02 17:12:10 -0700516 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800517
Andy Grovera1d8b492011-05-02 17:12:10 -0700518 complete(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800519 return 1;
520 }
521 if (transport_off) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500522 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800523 if (transport_off == 2) {
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400524 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800525 /*
526 * Clear struct se_cmd->se_lun before the transport_off == 2
527 * handoff to fabric module.
528 */
529 cmd->se_lun = NULL;
530 /*
531 * Some fabric modules like tcm_loop can release
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300532 * their internally allocated I/O reference now and
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800533 * struct se_cmd now.
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700534 *
535 * Fabric modules are expected to return '1' here if the
536 * se_cmd being passed is released at this point,
537 * or zero if not being released.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800538 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000539 if (cmd->se_tfo->check_stop_free != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800540 spin_unlock_irqrestore(
Andy Grovera1d8b492011-05-02 17:12:10 -0700541 &cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800542
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700543 return cmd->se_tfo->check_stop_free(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800544 }
545 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700546 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800547
548 return 0;
549 } else if (t_state)
550 cmd->t_state = t_state;
Andy Grovera1d8b492011-05-02 17:12:10 -0700551 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800552
553 return 0;
554}
555
556static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
557{
558 return transport_cmd_check_stop(cmd, 2, 0);
559}
560
561static void transport_lun_remove_cmd(struct se_cmd *cmd)
562{
Andy Grovere3d6f902011-07-19 08:55:10 +0000563 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800564 unsigned long flags;
565
566 if (!lun)
567 return;
568
Andy Grovera1d8b492011-05-02 17:12:10 -0700569 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500570 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
571 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400572 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800573 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700574 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800575
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800576 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -0500577 if (!list_empty(&cmd->se_lun_node))
578 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800579 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
580}
581
582void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
583{
Andy Groverc8e31f22012-01-19 13:39:17 -0800584 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellinger8dc52b52011-10-09 02:02:51 -0700585 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800586
587 if (transport_cmd_check_stop_to_fabric(cmd))
588 return;
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700589 if (remove) {
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400590 transport_remove_cmd_from_queue(cmd);
Christoph Hellwige6a25732011-09-13 23:08:50 +0200591 transport_put_cmd(cmd);
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700592 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800593}
594
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400595static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
596 bool at_head)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800597{
598 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +0000599 struct se_queue_obj *qobj = &dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800600 unsigned long flags;
601
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800602 if (t_state) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700603 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800604 cmd->t_state = t_state;
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500605 cmd->transport_state |= CMD_T_ACTIVE;
Andy Grovera1d8b492011-05-02 17:12:10 -0700606 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800607 }
608
609 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Roland Dreier79a7fef2011-09-28 22:12:07 -0700610
611 /* If the cmd is already on the list, remove it before we add it */
612 if (!list_empty(&cmd->se_queue_node))
613 list_del(&cmd->se_queue_node);
614 else
615 atomic_inc(&qobj->queue_cnt);
616
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400617 if (at_head)
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700618 list_add(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400619 else
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700620 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500621 cmd->transport_state |= CMD_T_QUEUED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800622 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
623
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800624 wake_up_interruptible(&qobj->thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800625}
626
Andy Grover5951146d2011-07-19 10:26:37 +0000627static struct se_cmd *
628transport_get_cmd_from_queue(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800629{
Andy Grover5951146d2011-07-19 10:26:37 +0000630 struct se_cmd *cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800631 unsigned long flags;
632
633 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
634 if (list_empty(&qobj->qobj_list)) {
635 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
636 return NULL;
637 }
Andy Grover5951146d2011-07-19 10:26:37 +0000638 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800639
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500640 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700641 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800642 atomic_dec(&qobj->queue_cnt);
643 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
644
Andy Grover5951146d2011-07-19 10:26:37 +0000645 return cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800646}
647
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400648static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800649{
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400650 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800651 unsigned long flags;
652
653 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500654 if (!(cmd->transport_state & CMD_T_QUEUED)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800655 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
656 return;
657 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500658 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700659 atomic_dec(&qobj->queue_cnt);
660 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800661 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800662}
663
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400664static void target_complete_failure_work(struct work_struct *work)
665{
666 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
667
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700668 transport_generic_request_failure(cmd);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400669}
670
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400671void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800672{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400673 struct se_device *dev = cmd->se_dev;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400674 int success = scsi_status == GOOD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800675 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800676
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400677 cmd->scsi_status = scsi_status;
678
679
Andy Grovera1d8b492011-05-02 17:12:10 -0700680 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400681 cmd->transport_state &= ~CMD_T_BUSY;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800682
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800683 if (dev && dev->transport->transport_complete) {
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400684 if (dev->transport->transport_complete(cmd,
685 cmd->t_data_sg) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800686 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800687 success = 1;
688 }
689 }
690
691 /*
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400692 * See if we are waiting to complete for an exception condition.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800693 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400694 if (cmd->transport_state & CMD_T_REQUEST_STOP) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700695 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400696 complete(&cmd->task_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800697 return;
698 }
Christoph Hellwig22350072011-11-02 05:06:35 -0700699
700 if (!success)
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500701 cmd->transport_state |= CMD_T_FAILED;
Christoph Hellwig22350072011-11-02 05:06:35 -0700702
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800703 /*
704 * Check for case where an explict ABORT_TASK has been received
705 * and transport_wait_for_tasks() will be waiting for completion..
706 */
707 if (cmd->transport_state & CMD_T_ABORTED &&
708 cmd->transport_state & CMD_T_STOP) {
709 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
710 complete(&cmd->t_transport_stop_comp);
711 return;
712 } else if (cmd->transport_state & CMD_T_FAILED) {
Christoph Hellwig41e16e92011-11-23 06:54:15 -0500713 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400714 INIT_WORK(&cmd->work, target_complete_failure_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800715 } else {
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400716 INIT_WORK(&cmd->work, target_complete_ok_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800717 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400718
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400719 cmd->t_state = TRANSPORT_COMPLETE;
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800720 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
Andy Grovera1d8b492011-05-02 17:12:10 -0700721 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800722
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400723 queue_work(target_completion_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800724}
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400725EXPORT_SYMBOL(target_complete_cmd);
726
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400727static void target_add_to_state_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800728{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400729 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800730 unsigned long flags;
731
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800732 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400733 if (!cmd->state_active) {
734 list_add_tail(&cmd->state_list, &dev->state_list);
735 cmd->state_active = true;
736 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800737 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800738}
739
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400740static void __target_add_to_execute_list(struct se_cmd *cmd)
Christoph Hellwig04629b72011-10-12 11:07:04 -0400741{
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400742 struct se_device *dev = cmd->se_dev;
743 bool head_of_queue = false;
744
745 if (!list_empty(&cmd->execute_list))
746 return;
747
748 if (dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED &&
749 cmd->sam_task_attr == MSG_HEAD_TAG)
750 head_of_queue = true;
751
752 if (head_of_queue)
753 list_add(&cmd->execute_list, &dev->execute_list);
754 else
755 list_add_tail(&cmd->execute_list, &dev->execute_list);
756
757 atomic_inc(&dev->execute_tasks);
758
759 if (cmd->state_active)
760 return;
761
762 if (head_of_queue)
763 list_add(&cmd->state_list, &dev->state_list);
764 else
765 list_add_tail(&cmd->state_list, &dev->state_list);
766
767 cmd->state_active = true;
Christoph Hellwig04629b72011-10-12 11:07:04 -0400768}
769
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400770static void target_add_to_execute_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800771{
772 unsigned long flags;
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400773 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800774
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400775 spin_lock_irqsave(&dev->execute_task_lock, flags);
776 __target_add_to_execute_list(cmd);
777 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
778}
779
780void __target_remove_from_execute_list(struct se_cmd *cmd)
781{
782 list_del_init(&cmd->execute_list);
783 atomic_dec(&cmd->se_dev->execute_tasks);
784}
785
786static void target_remove_from_execute_list(struct se_cmd *cmd)
787{
788 struct se_device *dev = cmd->se_dev;
789 unsigned long flags;
790
791 if (WARN_ON(list_empty(&cmd->execute_list)))
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700792 return;
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700793
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800794 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400795 __target_remove_from_execute_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800796 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
797}
798
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700799/*
Nicholas Bellingerf147abb2011-10-25 23:57:41 -0700800 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700801 */
802
803static void target_qf_do_work(struct work_struct *work)
804{
805 struct se_device *dev = container_of(work, struct se_device,
806 qf_work_queue);
Roland Dreierbcac3642011-08-27 21:33:16 -0700807 LIST_HEAD(qf_cmd_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700808 struct se_cmd *cmd, *cmd_tmp;
809
810 spin_lock_irq(&dev->qf_cmd_lock);
Roland Dreierbcac3642011-08-27 21:33:16 -0700811 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
812 spin_unlock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700813
Roland Dreierbcac3642011-08-27 21:33:16 -0700814 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700815 list_del(&cmd->se_qf_node);
816 atomic_dec(&dev->dev_qf_count);
817 smp_mb__after_atomic_dec();
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700818
Andy Grover6708bb22011-06-08 10:36:43 -0700819 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700820 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -0400821 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700822 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
823 : "UNKNOWN");
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400824
825 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700826 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700827}
828
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800829unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
830{
831 switch (cmd->data_direction) {
832 case DMA_NONE:
833 return "NONE";
834 case DMA_FROM_DEVICE:
835 return "READ";
836 case DMA_TO_DEVICE:
837 return "WRITE";
838 case DMA_BIDIRECTIONAL:
839 return "BIDI";
840 default:
841 break;
842 }
843
844 return "UNKNOWN";
845}
846
847void transport_dump_dev_state(
848 struct se_device *dev,
849 char *b,
850 int *bl)
851{
852 *bl += sprintf(b + *bl, "Status: ");
853 switch (dev->dev_status) {
854 case TRANSPORT_DEVICE_ACTIVATED:
855 *bl += sprintf(b + *bl, "ACTIVATED");
856 break;
857 case TRANSPORT_DEVICE_DEACTIVATED:
858 *bl += sprintf(b + *bl, "DEACTIVATED");
859 break;
860 case TRANSPORT_DEVICE_SHUTDOWN:
861 *bl += sprintf(b + *bl, "SHUTDOWN");
862 break;
863 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
864 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
865 *bl += sprintf(b + *bl, "OFFLINE");
866 break;
867 default:
868 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
869 break;
870 }
871
Nicholas Bellinger65586d52011-11-30 01:25:21 -0800872 *bl += sprintf(b + *bl, " Execute/Max Queue Depth: %d/%d",
873 atomic_read(&dev->execute_tasks), dev->queue_depth);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800874 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000875 dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800876 *bl += sprintf(b + *bl, " ");
877}
878
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800879void transport_dump_vpd_proto_id(
880 struct t10_vpd *vpd,
881 unsigned char *p_buf,
882 int p_buf_len)
883{
884 unsigned char buf[VPD_TMP_BUF_SIZE];
885 int len;
886
887 memset(buf, 0, VPD_TMP_BUF_SIZE);
888 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
889
890 switch (vpd->protocol_identifier) {
891 case 0x00:
892 sprintf(buf+len, "Fibre Channel\n");
893 break;
894 case 0x10:
895 sprintf(buf+len, "Parallel SCSI\n");
896 break;
897 case 0x20:
898 sprintf(buf+len, "SSA\n");
899 break;
900 case 0x30:
901 sprintf(buf+len, "IEEE 1394\n");
902 break;
903 case 0x40:
904 sprintf(buf+len, "SCSI Remote Direct Memory Access"
905 " Protocol\n");
906 break;
907 case 0x50:
908 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
909 break;
910 case 0x60:
911 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
912 break;
913 case 0x70:
914 sprintf(buf+len, "Automation/Drive Interface Transport"
915 " Protocol\n");
916 break;
917 case 0x80:
918 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
919 break;
920 default:
921 sprintf(buf+len, "Unknown 0x%02x\n",
922 vpd->protocol_identifier);
923 break;
924 }
925
926 if (p_buf)
927 strncpy(p_buf, buf, p_buf_len);
928 else
Andy Grover6708bb22011-06-08 10:36:43 -0700929 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800930}
931
932void
933transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
934{
935 /*
936 * Check if the Protocol Identifier Valid (PIV) bit is set..
937 *
938 * from spc3r23.pdf section 7.5.1
939 */
940 if (page_83[1] & 0x80) {
941 vpd->protocol_identifier = (page_83[0] & 0xf0);
942 vpd->protocol_identifier_set = 1;
943 transport_dump_vpd_proto_id(vpd, NULL, 0);
944 }
945}
946EXPORT_SYMBOL(transport_set_vpd_proto_id);
947
948int transport_dump_vpd_assoc(
949 struct t10_vpd *vpd,
950 unsigned char *p_buf,
951 int p_buf_len)
952{
953 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +0000954 int ret = 0;
955 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800956
957 memset(buf, 0, VPD_TMP_BUF_SIZE);
958 len = sprintf(buf, "T10 VPD Identifier Association: ");
959
960 switch (vpd->association) {
961 case 0x00:
962 sprintf(buf+len, "addressed logical unit\n");
963 break;
964 case 0x10:
965 sprintf(buf+len, "target port\n");
966 break;
967 case 0x20:
968 sprintf(buf+len, "SCSI target device\n");
969 break;
970 default:
971 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
Andy Grovere3d6f902011-07-19 08:55:10 +0000972 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800973 break;
974 }
975
976 if (p_buf)
977 strncpy(p_buf, buf, p_buf_len);
978 else
Andy Grover6708bb22011-06-08 10:36:43 -0700979 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800980
981 return ret;
982}
983
984int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
985{
986 /*
987 * The VPD identification association..
988 *
989 * from spc3r23.pdf Section 7.6.3.1 Table 297
990 */
991 vpd->association = (page_83[1] & 0x30);
992 return transport_dump_vpd_assoc(vpd, NULL, 0);
993}
994EXPORT_SYMBOL(transport_set_vpd_assoc);
995
996int transport_dump_vpd_ident_type(
997 struct t10_vpd *vpd,
998 unsigned char *p_buf,
999 int p_buf_len)
1000{
1001 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001002 int ret = 0;
1003 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001004
1005 memset(buf, 0, VPD_TMP_BUF_SIZE);
1006 len = sprintf(buf, "T10 VPD Identifier Type: ");
1007
1008 switch (vpd->device_identifier_type) {
1009 case 0x00:
1010 sprintf(buf+len, "Vendor specific\n");
1011 break;
1012 case 0x01:
1013 sprintf(buf+len, "T10 Vendor ID based\n");
1014 break;
1015 case 0x02:
1016 sprintf(buf+len, "EUI-64 based\n");
1017 break;
1018 case 0x03:
1019 sprintf(buf+len, "NAA\n");
1020 break;
1021 case 0x04:
1022 sprintf(buf+len, "Relative target port identifier\n");
1023 break;
1024 case 0x08:
1025 sprintf(buf+len, "SCSI name string\n");
1026 break;
1027 default:
1028 sprintf(buf+len, "Unsupported: 0x%02x\n",
1029 vpd->device_identifier_type);
Andy Grovere3d6f902011-07-19 08:55:10 +00001030 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001031 break;
1032 }
1033
Andy Grovere3d6f902011-07-19 08:55:10 +00001034 if (p_buf) {
1035 if (p_buf_len < strlen(buf)+1)
1036 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001037 strncpy(p_buf, buf, p_buf_len);
Andy Grovere3d6f902011-07-19 08:55:10 +00001038 } else {
Andy Grover6708bb22011-06-08 10:36:43 -07001039 pr_debug("%s", buf);
Andy Grovere3d6f902011-07-19 08:55:10 +00001040 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001041
1042 return ret;
1043}
1044
1045int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1046{
1047 /*
1048 * The VPD identifier type..
1049 *
1050 * from spc3r23.pdf Section 7.6.3.1 Table 298
1051 */
1052 vpd->device_identifier_type = (page_83[1] & 0x0f);
1053 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1054}
1055EXPORT_SYMBOL(transport_set_vpd_ident_type);
1056
1057int transport_dump_vpd_ident(
1058 struct t10_vpd *vpd,
1059 unsigned char *p_buf,
1060 int p_buf_len)
1061{
1062 unsigned char buf[VPD_TMP_BUF_SIZE];
1063 int ret = 0;
1064
1065 memset(buf, 0, VPD_TMP_BUF_SIZE);
1066
1067 switch (vpd->device_identifier_code_set) {
1068 case 0x01: /* Binary */
1069 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1070 &vpd->device_identifier[0]);
1071 break;
1072 case 0x02: /* ASCII */
1073 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1074 &vpd->device_identifier[0]);
1075 break;
1076 case 0x03: /* UTF-8 */
1077 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1078 &vpd->device_identifier[0]);
1079 break;
1080 default:
1081 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1082 " 0x%02x", vpd->device_identifier_code_set);
Andy Grovere3d6f902011-07-19 08:55:10 +00001083 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001084 break;
1085 }
1086
1087 if (p_buf)
1088 strncpy(p_buf, buf, p_buf_len);
1089 else
Andy Grover6708bb22011-06-08 10:36:43 -07001090 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001091
1092 return ret;
1093}
1094
1095int
1096transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1097{
1098 static const char hex_str[] = "0123456789abcdef";
1099 int j = 0, i = 4; /* offset to start of the identifer */
1100
1101 /*
1102 * The VPD Code Set (encoding)
1103 *
1104 * from spc3r23.pdf Section 7.6.3.1 Table 296
1105 */
1106 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1107 switch (vpd->device_identifier_code_set) {
1108 case 0x01: /* Binary */
1109 vpd->device_identifier[j++] =
1110 hex_str[vpd->device_identifier_type];
1111 while (i < (4 + page_83[3])) {
1112 vpd->device_identifier[j++] =
1113 hex_str[(page_83[i] & 0xf0) >> 4];
1114 vpd->device_identifier[j++] =
1115 hex_str[page_83[i] & 0x0f];
1116 i++;
1117 }
1118 break;
1119 case 0x02: /* ASCII */
1120 case 0x03: /* UTF-8 */
1121 while (i < (4 + page_83[3]))
1122 vpd->device_identifier[j++] = page_83[i++];
1123 break;
1124 default:
1125 break;
1126 }
1127
1128 return transport_dump_vpd_ident(vpd, NULL, 0);
1129}
1130EXPORT_SYMBOL(transport_set_vpd_ident);
1131
1132static void core_setup_task_attr_emulation(struct se_device *dev)
1133{
1134 /*
1135 * If this device is from Target_Core_Mod/pSCSI, disable the
1136 * SAM Task Attribute emulation.
1137 *
1138 * This is currently not available in upsream Linux/SCSI Target
1139 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1140 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001141 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001142 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1143 return;
1144 }
1145
1146 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
Andy Grover6708bb22011-06-08 10:36:43 -07001147 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001148 " device\n", dev->transport->name,
1149 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001150}
1151
1152static void scsi_dump_inquiry(struct se_device *dev)
1153{
Andy Grovere3d6f902011-07-19 08:55:10 +00001154 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001155 char buf[17];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001156 int i, device_type;
1157 /*
1158 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1159 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001160 for (i = 0; i < 8; i++)
1161 if (wwn->vendor[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001162 buf[i] = wwn->vendor[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001163 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001164 buf[i] = ' ';
1165 buf[i] = '\0';
1166 pr_debug(" Vendor: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001167
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001168 for (i = 0; i < 16; i++)
1169 if (wwn->model[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001170 buf[i] = wwn->model[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001171 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001172 buf[i] = ' ';
1173 buf[i] = '\0';
1174 pr_debug(" Model: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001175
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001176 for (i = 0; i < 4; i++)
1177 if (wwn->revision[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001178 buf[i] = wwn->revision[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001179 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001180 buf[i] = ' ';
1181 buf[i] = '\0';
1182 pr_debug(" Revision: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001183
Andy Grovere3d6f902011-07-19 08:55:10 +00001184 device_type = dev->transport->get_device_type(dev);
Andy Grover6708bb22011-06-08 10:36:43 -07001185 pr_debug(" Type: %s ", scsi_device_type(device_type));
1186 pr_debug(" ANSI SCSI revision: %02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001187 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001188}
1189
1190struct se_device *transport_add_device_to_core_hba(
1191 struct se_hba *hba,
1192 struct se_subsystem_api *transport,
1193 struct se_subsystem_dev *se_dev,
1194 u32 device_flags,
1195 void *transport_dev,
1196 struct se_dev_limits *dev_limits,
1197 const char *inquiry_prod,
1198 const char *inquiry_rev)
1199{
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001200 int force_pt;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001201 struct se_device *dev;
1202
1203 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001204 if (!dev) {
1205 pr_err("Unable to allocate memory for se_dev_t\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001206 return NULL;
1207 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001208
Andy Grovere3d6f902011-07-19 08:55:10 +00001209 transport_init_queue_obj(&dev->dev_queue_obj);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001210 dev->dev_flags = device_flags;
1211 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
Andy Grover5951146d2011-07-19 10:26:37 +00001212 dev->dev_ptr = transport_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001213 dev->se_hba = hba;
1214 dev->se_sub_dev = se_dev;
1215 dev->transport = transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001216 INIT_LIST_HEAD(&dev->dev_list);
1217 INIT_LIST_HEAD(&dev->dev_sep_list);
1218 INIT_LIST_HEAD(&dev->dev_tmr_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001219 INIT_LIST_HEAD(&dev->execute_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001220 INIT_LIST_HEAD(&dev->delayed_cmd_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001221 INIT_LIST_HEAD(&dev->state_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001222 INIT_LIST_HEAD(&dev->qf_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001223 spin_lock_init(&dev->execute_task_lock);
1224 spin_lock_init(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001225 spin_lock_init(&dev->dev_reservation_lock);
1226 spin_lock_init(&dev->dev_status_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001227 spin_lock_init(&dev->se_port_lock);
1228 spin_lock_init(&dev->se_tmr_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001229 spin_lock_init(&dev->qf_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001230 atomic_set(&dev->dev_ordered_id, 0);
1231
1232 se_dev_set_default_attribs(dev, dev_limits);
1233
1234 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1235 dev->creation_time = get_jiffies_64();
1236 spin_lock_init(&dev->stats_lock);
1237
1238 spin_lock(&hba->device_lock);
1239 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1240 hba->dev_count++;
1241 spin_unlock(&hba->device_lock);
1242 /*
1243 * Setup the SAM Task Attribute emulation for struct se_device
1244 */
1245 core_setup_task_attr_emulation(dev);
1246 /*
1247 * Force PR and ALUA passthrough emulation with internal object use.
1248 */
1249 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1250 /*
1251 * Setup the Reservations infrastructure for struct se_device
1252 */
1253 core_setup_reservations(dev, force_pt);
1254 /*
1255 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1256 */
1257 if (core_setup_alua(dev, force_pt) < 0)
1258 goto out;
1259
1260 /*
1261 * Startup the struct se_device processing thread
1262 */
1263 dev->process_thread = kthread_run(transport_processing_thread, dev,
Andy Grovere3d6f902011-07-19 08:55:10 +00001264 "LIO_%s", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001265 if (IS_ERR(dev->process_thread)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001266 pr_err("Unable to create kthread: LIO_%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001267 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001268 goto out;
1269 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001270 /*
1271 * Setup work_queue for QUEUE_FULL
1272 */
1273 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001274 /*
1275 * Preload the initial INQUIRY const values if we are doing
1276 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1277 * passthrough because this is being provided by the backend LLD.
1278 * This is required so that transport_get_inquiry() copies these
1279 * originals once back into DEV_T10_WWN(dev) for the virtual device
1280 * setup.
1281 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001282 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
Roland Dreierf22c1192011-05-02 22:15:37 -07001283 if (!inquiry_prod || !inquiry_rev) {
Andy Grover6708bb22011-06-08 10:36:43 -07001284 pr_err("All non TCM/pSCSI plugins require"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001285 " INQUIRY consts\n");
1286 goto out;
1287 }
1288
Andy Grovere3d6f902011-07-19 08:55:10 +00001289 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1290 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1291 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001292 }
1293 scsi_dump_inquiry(dev);
1294
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001295 return dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001296out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001297 kthread_stop(dev->process_thread);
1298
1299 spin_lock(&hba->device_lock);
1300 list_del(&dev->dev_list);
1301 hba->dev_count--;
1302 spin_unlock(&hba->device_lock);
1303
1304 se_release_vpd_for_dev(dev);
1305
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001306 kfree(dev);
1307
1308 return NULL;
1309}
1310EXPORT_SYMBOL(transport_add_device_to_core_hba);
1311
1312/* transport_generic_prepare_cdb():
1313 *
1314 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1315 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1316 * The point of this is since we are mapping iSCSI LUNs to
1317 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1318 * devices and HBAs for a loop.
1319 */
1320static inline void transport_generic_prepare_cdb(
1321 unsigned char *cdb)
1322{
1323 switch (cdb[0]) {
1324 case READ_10: /* SBC - RDProtect */
1325 case READ_12: /* SBC - RDProtect */
1326 case READ_16: /* SBC - RDProtect */
1327 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1328 case VERIFY: /* SBC - VRProtect */
1329 case VERIFY_16: /* SBC - VRProtect */
1330 case WRITE_VERIFY: /* SBC - VRProtect */
1331 case WRITE_VERIFY_12: /* SBC - VRProtect */
1332 break;
1333 default:
1334 cdb[1] &= 0x1f; /* clear logical unit number */
1335 break;
1336 }
1337}
1338
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001339static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1340
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001341/*
1342 * Used by fabric modules containing a local struct se_cmd within their
1343 * fabric dependent per I/O descriptor.
1344 */
1345void transport_init_se_cmd(
1346 struct se_cmd *cmd,
1347 struct target_core_fabric_ops *tfo,
1348 struct se_session *se_sess,
1349 u32 data_length,
1350 int data_direction,
1351 int task_attr,
1352 unsigned char *sense_buffer)
1353{
Andy Grover5951146d2011-07-19 10:26:37 +00001354 INIT_LIST_HEAD(&cmd->se_lun_node);
1355 INIT_LIST_HEAD(&cmd->se_delayed_node);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001356 INIT_LIST_HEAD(&cmd->se_qf_node);
Roland Dreier79a7fef2011-09-28 22:12:07 -07001357 INIT_LIST_HEAD(&cmd->se_queue_node);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001358 INIT_LIST_HEAD(&cmd->se_cmd_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001359 INIT_LIST_HEAD(&cmd->execute_list);
1360 INIT_LIST_HEAD(&cmd->state_list);
Andy Grovera1d8b492011-05-02 17:12:10 -07001361 init_completion(&cmd->transport_lun_fe_stop_comp);
1362 init_completion(&cmd->transport_lun_stop_comp);
1363 init_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001364 init_completion(&cmd->cmd_wait_comp);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001365 init_completion(&cmd->task_stop_comp);
Andy Grovera1d8b492011-05-02 17:12:10 -07001366 spin_lock_init(&cmd->t_state_lock);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001367 cmd->transport_state = CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001368
1369 cmd->se_tfo = tfo;
1370 cmd->se_sess = se_sess;
1371 cmd->data_length = data_length;
1372 cmd->data_direction = data_direction;
1373 cmd->sam_task_attr = task_attr;
1374 cmd->sense_buffer = sense_buffer;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001375
1376 cmd->state_active = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001377}
1378EXPORT_SYMBOL(transport_init_se_cmd);
1379
1380static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1381{
1382 /*
1383 * Check if SAM Task Attribute emulation is enabled for this
1384 * struct se_device storage object
1385 */
Andy Grover5951146d2011-07-19 10:26:37 +00001386 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001387 return 0;
1388
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001389 if (cmd->sam_task_attr == MSG_ACA_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001390 pr_debug("SAM Task Attribute ACA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001391 " emulation is not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001392 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001393 }
1394 /*
1395 * Used to determine when ORDERED commands should go from
1396 * Dormant to Active status.
1397 */
Andy Grover5951146d2011-07-19 10:26:37 +00001398 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001399 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001400 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001401 cmd->se_ordered_id, cmd->sam_task_attr,
Andy Grover6708bb22011-06-08 10:36:43 -07001402 cmd->se_dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001403 return 0;
1404}
1405
Andy Grovera12f41f2012-04-03 15:51:20 -07001406/* target_setup_cmd_from_cdb():
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001407 *
1408 * Called from fabric RX Thread.
1409 */
Andy Grovera12f41f2012-04-03 15:51:20 -07001410int target_setup_cmd_from_cdb(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001411 struct se_cmd *cmd,
1412 unsigned char *cdb)
1413{
1414 int ret;
1415
1416 transport_generic_prepare_cdb(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001417 /*
1418 * Ensure that the received CDB is less than the max (252 + 8) bytes
1419 * for VARIABLE_LENGTH_CMD
1420 */
1421 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001422 pr_err("Received SCSI CDB with command_size: %d that"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001423 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1424 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001425 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1426 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grovere3d6f902011-07-19 08:55:10 +00001427 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001428 }
1429 /*
1430 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1431 * allocate the additional extended CDB buffer now.. Otherwise
1432 * setup the pointer from __t_task_cdb to t_task_cdb.
1433 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001434 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1435 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001436 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001437 if (!cmd->t_task_cdb) {
1438 pr_err("Unable to allocate cmd->t_task_cdb"
Andy Grovera1d8b492011-05-02 17:12:10 -07001439 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001440 scsi_command_size(cdb),
Andy Grovera1d8b492011-05-02 17:12:10 -07001441 (unsigned long)sizeof(cmd->__t_task_cdb));
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001442 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1443 cmd->scsi_sense_reason =
1444 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grovere3d6f902011-07-19 08:55:10 +00001445 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001446 }
1447 } else
Andy Grovera1d8b492011-05-02 17:12:10 -07001448 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001449 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07001450 * Copy the original CDB into cmd->
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001451 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001452 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001453 /*
1454 * Setup the received CDB based on SCSI defined opcodes and
1455 * perform unit attention, persistent reservations and ALUA
Andy Grovera1d8b492011-05-02 17:12:10 -07001456 * checks for virtual device backends. The cmd->t_task_cdb
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001457 * pointer is expected to be setup before we reach this point.
1458 */
1459 ret = transport_generic_cmd_sequencer(cmd, cdb);
1460 if (ret < 0)
1461 return ret;
1462 /*
1463 * Check for SAM Task Attribute Emulation
1464 */
1465 if (transport_check_alloc_task_attr(cmd) < 0) {
1466 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1467 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00001468 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001469 }
1470 spin_lock(&cmd->se_lun->lun_sep_lock);
1471 if (cmd->se_lun->lun_sep)
1472 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1473 spin_unlock(&cmd->se_lun->lun_sep_lock);
1474 return 0;
1475}
Andy Grovera12f41f2012-04-03 15:51:20 -07001476EXPORT_SYMBOL(target_setup_cmd_from_cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001477
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001478/*
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001479 * Used by fabric module frontends to queue tasks directly.
1480 * Many only be used from process context only
1481 */
1482int transport_handle_cdb_direct(
1483 struct se_cmd *cmd)
1484{
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001485 int ret;
1486
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001487 if (!cmd->se_lun) {
1488 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001489 pr_err("cmd->se_lun is NULL\n");
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001490 return -EINVAL;
1491 }
1492 if (in_interrupt()) {
1493 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001494 pr_err("transport_generic_handle_cdb cannot be called"
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001495 " from interrupt context\n");
1496 return -EINVAL;
1497 }
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001498 /*
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001499 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001500 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1501 * in existing usage to ensure that outstanding descriptors are handled
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07001502 * correctly during shutdown via transport_wait_for_tasks()
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001503 *
1504 * Also, we don't take cmd->t_state_lock here as we only expect
1505 * this to be called for initial descriptor submission.
1506 */
1507 cmd->t_state = TRANSPORT_NEW_CMD;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001508 cmd->transport_state |= CMD_T_ACTIVE;
1509
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001510 /*
1511 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1512 * so follow TRANSPORT_NEW_CMD processing thread context usage
1513 * and call transport_generic_request_failure() if necessary..
1514 */
1515 ret = transport_generic_new_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001516 if (ret < 0)
1517 transport_generic_request_failure(cmd);
1518
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001519 return 0;
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001520}
1521EXPORT_SYMBOL(transport_handle_cdb_direct);
1522
Nicholas Bellingera6360782011-11-18 20:36:22 -08001523/**
1524 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1525 *
1526 * @se_cmd: command descriptor to submit
1527 * @se_sess: associated se_sess for endpoint
1528 * @cdb: pointer to SCSI CDB
1529 * @sense: pointer to SCSI sense buffer
1530 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1531 * @data_length: fabric expected data transfer length
1532 * @task_addr: SAM task attribute
1533 * @data_dir: DMA data direction
1534 * @flags: flags for command submission from target_sc_flags_tables
1535 *
1536 * This may only be called from process context, and also currently
1537 * assumes internal allocation of fabric payload buffer by target-core.
1538 **/
Andy Grover1edcdb42012-01-19 13:39:23 -08001539void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
Nicholas Bellingera6360782011-11-18 20:36:22 -08001540 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1541 u32 data_length, int task_attr, int data_dir, int flags)
1542{
1543 struct se_portal_group *se_tpg;
1544 int rc;
1545
1546 se_tpg = se_sess->se_tpg;
1547 BUG_ON(!se_tpg);
1548 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1549 BUG_ON(in_interrupt());
1550 /*
1551 * Initialize se_cmd for target operation. From this point
1552 * exceptions are handled by sending exception status via
1553 * target_core_fabric_ops->queue_status() callback
1554 */
1555 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1556 data_length, data_dir, task_attr, sense);
Sebastian Andrzej Siewiorb0d79942012-01-10 14:16:59 +01001557 if (flags & TARGET_SCF_UNKNOWN_SIZE)
1558 se_cmd->unknown_data_length = 1;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001559 /*
1560 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1561 * se_sess->sess_cmd_list. A second kref_get here is necessary
1562 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1563 * kref_put() to happen during fabric packet acknowledgement.
1564 */
1565 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1566 /*
1567 * Signal bidirectional data payloads to target-core
1568 */
1569 if (flags & TARGET_SCF_BIDI_OP)
1570 se_cmd->se_cmd_flags |= SCF_BIDI;
1571 /*
1572 * Locate se_lun pointer and attach it to struct se_cmd
1573 */
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001574 if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1575 transport_send_check_condition_and_sense(se_cmd,
1576 se_cmd->scsi_sense_reason, 0);
1577 target_put_sess_cmd(se_sess, se_cmd);
1578 return;
1579 }
Nicholas Bellingera6360782011-11-18 20:36:22 -08001580 /*
1581 * Sanitize CDBs via transport_generic_cmd_sequencer() and
1582 * allocate the necessary tasks to complete the received CDB+data
1583 */
Andy Grovera12f41f2012-04-03 15:51:20 -07001584 rc = target_setup_cmd_from_cdb(se_cmd, cdb);
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001585 if (rc != 0) {
1586 transport_generic_request_failure(se_cmd);
1587 return;
1588 }
Andy Grover11e319e2012-04-03 15:51:28 -07001589
1590 /*
1591 * Check if we need to delay processing because of ALUA
1592 * Active/NonOptimized primary access state..
1593 */
1594 core_alua_check_nonop_delay(se_cmd);
1595
Nicholas Bellingera6360782011-11-18 20:36:22 -08001596 /*
1597 * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
1598 * for immediate execution of READs, otherwise wait for
1599 * transport_generic_handle_data() to be called for WRITEs
1600 * when fabric has filled the incoming buffer.
1601 */
1602 transport_handle_cdb_direct(se_cmd);
Andy Grover1edcdb42012-01-19 13:39:23 -08001603 return;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001604}
1605EXPORT_SYMBOL(target_submit_cmd);
1606
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001607static void target_complete_tmr_failure(struct work_struct *work)
1608{
1609 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
1610
1611 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1612 se_cmd->se_tfo->queue_tm_rsp(se_cmd);
1613 transport_generic_free_cmd(se_cmd, 0);
1614}
1615
Andy Groverea98d7f2012-01-19 13:39:21 -08001616/**
1617 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1618 * for TMR CDBs
1619 *
1620 * @se_cmd: command descriptor to submit
1621 * @se_sess: associated se_sess for endpoint
1622 * @sense: pointer to SCSI sense buffer
1623 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1624 * @fabric_context: fabric context for TMR req
1625 * @tm_type: Type of TM request
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001626 * @gfp: gfp type for caller
1627 * @tag: referenced task tag for TMR_ABORT_TASK
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001628 * @flags: submit cmd flags
Andy Groverea98d7f2012-01-19 13:39:21 -08001629 *
1630 * Callable from all contexts.
1631 **/
1632
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001633int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
Andy Groverea98d7f2012-01-19 13:39:21 -08001634 unsigned char *sense, u32 unpacked_lun,
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001635 void *fabric_tmr_ptr, unsigned char tm_type,
1636 gfp_t gfp, unsigned int tag, int flags)
Andy Groverea98d7f2012-01-19 13:39:21 -08001637{
1638 struct se_portal_group *se_tpg;
1639 int ret;
1640
1641 se_tpg = se_sess->se_tpg;
1642 BUG_ON(!se_tpg);
1643
1644 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1645 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001646 /*
1647 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1648 * allocation failure.
1649 */
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001650 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001651 if (ret < 0)
1652 return -ENOMEM;
Andy Groverea98d7f2012-01-19 13:39:21 -08001653
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001654 if (tm_type == TMR_ABORT_TASK)
1655 se_cmd->se_tmr_req->ref_task_tag = tag;
1656
Andy Groverea98d7f2012-01-19 13:39:21 -08001657 /* See target_submit_cmd for commentary */
1658 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1659
Andy Groverea98d7f2012-01-19 13:39:21 -08001660 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1661 if (ret) {
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001662 /*
1663 * For callback during failure handling, push this work off
1664 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1665 */
1666 INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
1667 schedule_work(&se_cmd->work);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001668 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001669 }
1670 transport_generic_handle_tmr(se_cmd);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001671 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001672}
1673EXPORT_SYMBOL(target_submit_tmr);
1674
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001675/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001676 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1677 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1678 * complete setup in TCM process context w/ TFO->new_cmd_map().
1679 */
1680int transport_generic_handle_cdb_map(
1681 struct se_cmd *cmd)
1682{
Andy Grovere3d6f902011-07-19 08:55:10 +00001683 if (!cmd->se_lun) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001684 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001685 pr_err("cmd->se_lun is NULL\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001686 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001687 }
1688
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001689 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001690 return 0;
1691}
1692EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1693
1694/* transport_generic_handle_data():
1695 *
1696 *
1697 */
1698int transport_generic_handle_data(
1699 struct se_cmd *cmd)
1700{
1701 /*
1702 * For the software fabric case, then we assume the nexus is being
1703 * failed/shutdown when signals are pending from the kthread context
1704 * caller, so we return a failure. For the HW target mode case running
1705 * in interrupt code, the signal_pending() check is skipped.
1706 */
1707 if (!in_interrupt() && signal_pending(current))
Andy Grovere3d6f902011-07-19 08:55:10 +00001708 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001709 /*
1710 * If the received CDB has aleady been ABORTED by the generic
1711 * target engine, we now call transport_check_aborted_status()
1712 * to queue any delated TASK_ABORTED status for the received CDB to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001713 * fabric module as we are expecting no further incoming DATA OUT
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001714 * sequences at this point.
1715 */
1716 if (transport_check_aborted_status(cmd, 1) != 0)
1717 return 0;
1718
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001719 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001720 return 0;
1721}
1722EXPORT_SYMBOL(transport_generic_handle_data);
1723
1724/* transport_generic_handle_tmr():
1725 *
1726 *
1727 */
1728int transport_generic_handle_tmr(
1729 struct se_cmd *cmd)
1730{
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001731 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001732 return 0;
1733}
1734EXPORT_SYMBOL(transport_generic_handle_tmr);
1735
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001736/*
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001737 * If the cmd is active, request it to be stopped and sleep until it
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001738 * has completed.
1739 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001740bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001741{
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001742 bool was_active = false;
1743
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001744 if (cmd->transport_state & CMD_T_BUSY) {
1745 cmd->transport_state |= CMD_T_REQUEST_STOP;
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001746 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1747
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001748 pr_debug("cmd %p waiting to complete\n", cmd);
1749 wait_for_completion(&cmd->task_stop_comp);
1750 pr_debug("cmd %p stopped successfully\n", cmd);
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001751
1752 spin_lock_irqsave(&cmd->t_state_lock, *flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001753 cmd->transport_state &= ~CMD_T_REQUEST_STOP;
1754 cmd->transport_state &= ~CMD_T_BUSY;
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001755 was_active = true;
1756 }
1757
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001758 return was_active;
1759}
1760
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001761/*
1762 * Handle SAM-esque emulation for generic transport request failures.
1763 */
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001764void transport_generic_request_failure(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001765{
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001766 int ret = 0;
1767
Andy Grover6708bb22011-06-08 10:36:43 -07001768 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001769 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -07001770 cmd->t_task_cdb[0]);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001771 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001772 cmd->se_tfo->get_cmd_state(cmd),
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001773 cmd->t_state, cmd->scsi_sense_reason);
Christoph Hellwig226b6fa2012-04-24 00:25:07 -04001774 pr_debug("-----[ t_task_cdbs_ex_left: %d --"
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001775 " CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07001776 atomic_read(&cmd->t_task_cdbs_ex_left),
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001777 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1778 (cmd->transport_state & CMD_T_STOP) != 0,
1779 (cmd->transport_state & CMD_T_SENT) != 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001780
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001781 /*
1782 * For SAM Task Attribute emulation for failed struct se_cmd
1783 */
1784 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1785 transport_complete_task_attr(cmd);
1786
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001787 switch (cmd->scsi_sense_reason) {
1788 case TCM_NON_EXISTENT_LUN:
1789 case TCM_UNSUPPORTED_SCSI_OPCODE:
1790 case TCM_INVALID_CDB_FIELD:
1791 case TCM_INVALID_PARAMETER_LIST:
1792 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1793 case TCM_UNKNOWN_MODE_PAGE:
1794 case TCM_WRITE_PROTECTED:
1795 case TCM_CHECK_CONDITION_ABORT_CMD:
1796 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1797 case TCM_CHECK_CONDITION_NOT_READY:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001798 break;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001799 case TCM_RESERVATION_CONFLICT:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001800 /*
1801 * No SENSE Data payload for this case, set SCSI Status
1802 * and queue the response to $FABRIC_MOD.
1803 *
1804 * Uses linux/include/scsi/scsi.h SAM status codes defs
1805 */
1806 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1807 /*
1808 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1809 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1810 * CONFLICT STATUS.
1811 *
1812 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1813 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001814 if (cmd->se_sess &&
1815 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1816 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001817 cmd->orig_fe_lun, 0x2C,
1818 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1819
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001820 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001821 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001822 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001823 goto check_stop;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001824 default:
Andy Grover6708bb22011-06-08 10:36:43 -07001825 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001826 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001827 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1828 break;
1829 }
Nicholas Bellinger16ab8e62011-08-08 19:03:38 -07001830 /*
1831 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1832 * make the call to transport_send_check_condition_and_sense()
1833 * directly. Otherwise expect the fabric to make the call to
1834 * transport_send_check_condition_and_sense() after handling
1835 * possible unsoliticied write data payloads.
1836 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001837 ret = transport_send_check_condition_and_sense(cmd,
1838 cmd->scsi_sense_reason, 0);
1839 if (ret == -EAGAIN || ret == -ENOMEM)
1840 goto queue_full;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001841
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001842check_stop:
1843 transport_lun_remove_cmd(cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07001844 if (!transport_cmd_check_stop_to_fabric(cmd))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001845 ;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001846 return;
1847
1848queue_full:
Christoph Hellwige057f532011-10-17 13:56:41 -04001849 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1850 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001851}
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001852EXPORT_SYMBOL(transport_generic_request_failure);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001853
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001854static inline u32 transport_lba_21(unsigned char *cdb)
1855{
1856 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1857}
1858
1859static inline u32 transport_lba_32(unsigned char *cdb)
1860{
1861 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1862}
1863
1864static inline unsigned long long transport_lba_64(unsigned char *cdb)
1865{
1866 unsigned int __v1, __v2;
1867
1868 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1869 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1870
1871 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1872}
1873
1874/*
1875 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
1876 */
1877static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
1878{
1879 unsigned int __v1, __v2;
1880
1881 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
1882 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
1883
1884 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1885}
1886
1887static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
1888{
1889 unsigned long flags;
1890
Andy Grovera1d8b492011-05-02 17:12:10 -07001891 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001892 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
Andy Grovera1d8b492011-05-02 17:12:10 -07001893 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001894}
1895
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001896/*
1897 * Called from Fabric Module context from transport_execute_tasks()
1898 *
1899 * The return of this function determins if the tasks from struct se_cmd
1900 * get added to the execution queue in transport_execute_tasks(),
1901 * or are added to the delayed or ordered lists here.
1902 */
1903static inline int transport_execute_task_attr(struct se_cmd *cmd)
1904{
Andy Grover5951146d2011-07-19 10:26:37 +00001905 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001906 return 1;
1907 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001908 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001909 * to allow the passed struct se_cmd list of tasks to the front of the list.
1910 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001911 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001912 pr_debug("Added HEAD_OF_QUEUE for CDB:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001913 " 0x%02x, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07001914 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001915 cmd->se_ordered_id);
1916 return 1;
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001917 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Andy Grover5951146d2011-07-19 10:26:37 +00001918 atomic_inc(&cmd->se_dev->dev_ordered_sync);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001919 smp_mb__after_atomic_inc();
1920
Andy Grover6708bb22011-06-08 10:36:43 -07001921 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001922 " list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07001923 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001924 cmd->se_ordered_id);
1925 /*
1926 * Add ORDERED command to tail of execution queue if
1927 * no other older commands exist that need to be
1928 * completed first.
1929 */
Andy Grover6708bb22011-06-08 10:36:43 -07001930 if (!atomic_read(&cmd->se_dev->simple_cmds))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001931 return 1;
1932 } else {
1933 /*
1934 * For SIMPLE and UNTAGGED Task Attribute commands
1935 */
Andy Grover5951146d2011-07-19 10:26:37 +00001936 atomic_inc(&cmd->se_dev->simple_cmds);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001937 smp_mb__after_atomic_inc();
1938 }
1939 /*
1940 * Otherwise if one or more outstanding ORDERED task attribute exist,
1941 * add the dormant task(s) built for the passed struct se_cmd to the
1942 * execution queue and become in Active state for this struct se_device.
1943 */
Andy Grover5951146d2011-07-19 10:26:37 +00001944 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001945 /*
1946 * Otherwise, add cmd w/ tasks to delayed cmd queue that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001947 * will be drained upon completion of HEAD_OF_QUEUE task.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001948 */
Andy Grover5951146d2011-07-19 10:26:37 +00001949 spin_lock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001950 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
Andy Grover5951146d2011-07-19 10:26:37 +00001951 list_add_tail(&cmd->se_delayed_node,
1952 &cmd->se_dev->delayed_cmd_list);
1953 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001954
Andy Grover6708bb22011-06-08 10:36:43 -07001955 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001956 " delayed CMD list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07001957 cmd->t_task_cdb[0], cmd->sam_task_attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001958 cmd->se_ordered_id);
1959 /*
1960 * Return zero to let transport_execute_tasks() know
1961 * not to add the delayed tasks to the execution list.
1962 */
1963 return 0;
1964 }
1965 /*
1966 * Otherwise, no ORDERED task attributes exist..
1967 */
1968 return 1;
1969}
1970
1971/*
1972 * Called from fabric module context in transport_generic_new_cmd() and
1973 * transport_generic_process_write()
1974 */
1975static int transport_execute_tasks(struct se_cmd *cmd)
1976{
1977 int add_tasks;
Nicholas Bellinger40be67f2011-11-30 00:41:20 -08001978 struct se_device *se_dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001979 /*
1980 * Call transport_cmd_check_stop() to see if a fabric exception
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001981 * has occurred that prevents execution.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001982 */
Andy Grover6708bb22011-06-08 10:36:43 -07001983 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001984 /*
1985 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
1986 * attribute for the tasks of the received struct se_cmd CDB
1987 */
1988 add_tasks = transport_execute_task_attr(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00001989 if (!add_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001990 goto execute_tasks;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001991
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08001992 __transport_execute_tasks(se_dev, cmd);
1993 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001994 }
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08001995
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001996execute_tasks:
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08001997 __transport_execute_tasks(se_dev, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001998 return 0;
1999}
2000
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002001static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002002{
2003 int error;
2004 struct se_cmd *cmd = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002005 unsigned long flags;
2006
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002007check_depth:
Andy Grovere3d6f902011-07-19 08:55:10 +00002008 spin_lock_irq(&dev->execute_task_lock);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002009 if (new_cmd != NULL)
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002010 __target_add_to_execute_list(new_cmd);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002011
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002012 if (list_empty(&dev->execute_list)) {
Andy Grovere3d6f902011-07-19 08:55:10 +00002013 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002014 return 0;
2015 }
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002016 cmd = list_first_entry(&dev->execute_list, struct se_cmd, execute_list);
2017 __target_remove_from_execute_list(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00002018 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002019
Andy Grovera1d8b492011-05-02 17:12:10 -07002020 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002021 cmd->transport_state |= CMD_T_BUSY;
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002022 cmd->transport_state |= CMD_T_SENT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002023
Andy Grovera1d8b492011-05-02 17:12:10 -07002024 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002025
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002026 if (cmd->execute_cmd)
2027 error = cmd->execute_cmd(cmd);
Christoph Hellwig5787cac2012-04-24 00:25:06 -04002028 else {
2029 error = dev->transport->execute_cmd(cmd, cmd->t_data_sg,
2030 cmd->t_data_nents, cmd->data_direction);
2031 }
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002032
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002033 if (error != 0) {
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002034 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002035 cmd->transport_state &= ~CMD_T_BUSY;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002036 cmd->transport_state &= ~CMD_T_SENT;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002037 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002038
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002039 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002040 }
2041
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002042 new_cmd = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002043 goto check_depth;
2044
2045 return 0;
2046}
2047
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002048static inline u32 transport_get_sectors_6(
2049 unsigned char *cdb,
2050 struct se_cmd *cmd,
2051 int *ret)
2052{
Andy Grover5951146d2011-07-19 10:26:37 +00002053 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002054
2055 /*
2056 * Assume TYPE_DISK for non struct se_device objects.
2057 * Use 8-bit sector value.
2058 */
2059 if (!dev)
2060 goto type_disk;
2061
2062 /*
2063 * Use 24-bit allocation length for TYPE_TAPE.
2064 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002065 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002066 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2067
2068 /*
2069 * Everything else assume TYPE_DISK Sector CDB location.
Roland Dreier9b5cd7f2011-11-22 13:51:33 -08002070 * Use 8-bit sector value. SBC-3 says:
2071 *
2072 * A TRANSFER LENGTH field set to zero specifies that 256
2073 * logical blocks shall be written. Any other value
2074 * specifies the number of logical blocks that shall be
2075 * written.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002076 */
2077type_disk:
Roland Dreier9b5cd7f2011-11-22 13:51:33 -08002078 return cdb[4] ? : 256;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002079}
2080
2081static inline u32 transport_get_sectors_10(
2082 unsigned char *cdb,
2083 struct se_cmd *cmd,
2084 int *ret)
2085{
Andy Grover5951146d2011-07-19 10:26:37 +00002086 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002087
2088 /*
2089 * Assume TYPE_DISK for non struct se_device objects.
2090 * Use 16-bit sector value.
2091 */
2092 if (!dev)
2093 goto type_disk;
2094
2095 /*
2096 * XXX_10 is not defined in SSC, throw an exception
2097 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002098 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2099 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002100 return 0;
2101 }
2102
2103 /*
2104 * Everything else assume TYPE_DISK Sector CDB location.
2105 * Use 16-bit sector value.
2106 */
2107type_disk:
2108 return (u32)(cdb[7] << 8) + cdb[8];
2109}
2110
2111static inline u32 transport_get_sectors_12(
2112 unsigned char *cdb,
2113 struct se_cmd *cmd,
2114 int *ret)
2115{
Andy Grover5951146d2011-07-19 10:26:37 +00002116 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002117
2118 /*
2119 * Assume TYPE_DISK for non struct se_device objects.
2120 * Use 32-bit sector value.
2121 */
2122 if (!dev)
2123 goto type_disk;
2124
2125 /*
2126 * XXX_12 is not defined in SSC, throw an exception
2127 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002128 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2129 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002130 return 0;
2131 }
2132
2133 /*
2134 * Everything else assume TYPE_DISK Sector CDB location.
2135 * Use 32-bit sector value.
2136 */
2137type_disk:
2138 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2139}
2140
2141static inline u32 transport_get_sectors_16(
2142 unsigned char *cdb,
2143 struct se_cmd *cmd,
2144 int *ret)
2145{
Andy Grover5951146d2011-07-19 10:26:37 +00002146 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002147
2148 /*
2149 * Assume TYPE_DISK for non struct se_device objects.
2150 * Use 32-bit sector value.
2151 */
2152 if (!dev)
2153 goto type_disk;
2154
2155 /*
2156 * Use 24-bit allocation length for TYPE_TAPE.
2157 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002158 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002159 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2160
2161type_disk:
2162 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2163 (cdb[12] << 8) + cdb[13];
2164}
2165
2166/*
2167 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2168 */
2169static inline u32 transport_get_sectors_32(
2170 unsigned char *cdb,
2171 struct se_cmd *cmd,
2172 int *ret)
2173{
2174 /*
2175 * Assume TYPE_DISK for non struct se_device objects.
2176 * Use 32-bit sector value.
2177 */
2178 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2179 (cdb[30] << 8) + cdb[31];
2180
2181}
2182
2183static inline u32 transport_get_size(
2184 u32 sectors,
2185 unsigned char *cdb,
2186 struct se_cmd *cmd)
2187{
Andy Grover5951146d2011-07-19 10:26:37 +00002188 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002189
Andy Grovere3d6f902011-07-19 08:55:10 +00002190 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002191 if (cdb[1] & 1) { /* sectors */
Andy Grovere3d6f902011-07-19 08:55:10 +00002192 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002193 } else /* bytes */
2194 return sectors;
2195 }
Andy Grover8b1e1242012-04-03 15:51:12 -07002196
Andy Grover6708bb22011-06-08 10:36:43 -07002197 pr_debug("Returning block_size: %u, sectors: %u == %u for"
Andy Grover8b1e1242012-04-03 15:51:12 -07002198 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size,
2199 sectors, dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2200 dev->transport->name);
2201
Andy Grovere3d6f902011-07-19 08:55:10 +00002202 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002203}
2204
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002205static void transport_xor_callback(struct se_cmd *cmd)
2206{
2207 unsigned char *buf, *addr;
Andy Groverec98f782011-07-20 19:28:46 +00002208 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002209 unsigned int offset;
2210 int i;
Andy Groverec98f782011-07-20 19:28:46 +00002211 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002212 /*
2213 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2214 *
2215 * 1) read the specified logical block(s);
2216 * 2) transfer logical blocks from the data-out buffer;
2217 * 3) XOR the logical blocks transferred from the data-out buffer with
2218 * the logical blocks read, storing the resulting XOR data in a buffer;
2219 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2220 * blocks transferred from the data-out buffer; and
2221 * 5) transfer the resulting XOR data to the data-in buffer.
2222 */
2223 buf = kmalloc(cmd->data_length, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002224 if (!buf) {
2225 pr_err("Unable to allocate xor_callback buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002226 return;
2227 }
2228 /*
Andy Groverec98f782011-07-20 19:28:46 +00002229 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002230 * into the locally allocated *buf
2231 */
Andy Groverec98f782011-07-20 19:28:46 +00002232 sg_copy_to_buffer(cmd->t_data_sg,
2233 cmd->t_data_nents,
2234 buf,
2235 cmd->data_length);
2236
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002237 /*
2238 * Now perform the XOR against the BIDI read memory located at
Andy Grovera1d8b492011-05-02 17:12:10 -07002239 * cmd->t_mem_bidi_list
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002240 */
2241
2242 offset = 0;
Andy Groverec98f782011-07-20 19:28:46 +00002243 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
Cong Wangca747d62011-11-25 23:14:25 +08002244 addr = kmap_atomic(sg_page(sg));
Andy Groverec98f782011-07-20 19:28:46 +00002245 if (!addr)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002246 goto out;
2247
Andy Groverec98f782011-07-20 19:28:46 +00002248 for (i = 0; i < sg->length; i++)
2249 *(addr + sg->offset + i) ^= *(buf + offset + i);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002250
Andy Groverec98f782011-07-20 19:28:46 +00002251 offset += sg->length;
Cong Wangca747d62011-11-25 23:14:25 +08002252 kunmap_atomic(addr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002253 }
Andy Groverec98f782011-07-20 19:28:46 +00002254
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002255out:
2256 kfree(buf);
2257}
2258
2259/*
2260 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2261 */
2262static int transport_get_sense_data(struct se_cmd *cmd)
2263{
2264 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002265 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002266 unsigned long flags;
2267 u32 offset = 0;
2268
Andy Grovere3d6f902011-07-19 08:55:10 +00002269 WARN_ON(!cmd->se_lun);
2270
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002271 if (!dev)
2272 return 0;
2273
Andy Grovera1d8b492011-05-02 17:12:10 -07002274 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002275 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002276 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002277 return 0;
2278 }
2279
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002280 if (!(cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2281 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002282
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002283 if (!dev->transport->get_sense_buffer) {
2284 pr_err("dev->transport->get_sense_buffer is NULL\n");
2285 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002286 }
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002287
Christoph Hellwig5787cac2012-04-24 00:25:06 -04002288 sense_buffer = dev->transport->get_sense_buffer(cmd);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002289 if (!sense_buffer) {
Christoph Hellwig5787cac2012-04-24 00:25:06 -04002290 pr_err("ITT 0x%08x cmd %p: Unable to locate"
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002291 " sense buffer for task with sense\n",
Christoph Hellwig5787cac2012-04-24 00:25:06 -04002292 cmd->se_tfo->get_task_tag(cmd), cmd);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002293 goto out;
2294 }
Christoph Hellwig5787cac2012-04-24 00:25:06 -04002295
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002296 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2297
2298 offset = cmd->se_tfo->set_fabric_sense_len(cmd, TRANSPORT_SENSE_BUFFER);
2299
2300 memcpy(&buffer[offset], sense_buffer, TRANSPORT_SENSE_BUFFER);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002301
2302 /* Automatically padded */
2303 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
2304
2305 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x and sense\n",
2306 dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
2307 return 0;
2308
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002309out:
Andy Grovera1d8b492011-05-02 17:12:10 -07002310 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002311 return -1;
2312}
2313
Andy Groverec98f782011-07-20 19:28:46 +00002314static inline long long transport_dev_end_lba(struct se_device *dev)
2315{
2316 return dev->transport->get_blocks(dev) + 1;
2317}
2318
2319static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2320{
2321 struct se_device *dev = cmd->se_dev;
2322 u32 sectors;
2323
2324 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2325 return 0;
2326
2327 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2328
Andy Grover6708bb22011-06-08 10:36:43 -07002329 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2330 pr_err("LBA: %llu Sectors: %u exceeds"
Andy Groverec98f782011-07-20 19:28:46 +00002331 " transport_dev_end_lba(): %llu\n",
2332 cmd->t_task_lba, sectors,
2333 transport_dev_end_lba(dev));
Nicholas Bellinger7abbe7f2011-08-10 18:41:14 -07002334 return -EINVAL;
Andy Groverec98f782011-07-20 19:28:46 +00002335 }
2336
Nicholas Bellinger7abbe7f2011-08-10 18:41:14 -07002337 return 0;
Andy Groverec98f782011-07-20 19:28:46 +00002338}
2339
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002340static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2341{
2342 /*
2343 * Determine if the received WRITE_SAME is used to for direct
2344 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2345 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2346 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2347 */
2348 int passthrough = (dev->transport->transport_type ==
2349 TRANSPORT_PLUGIN_PHBA_PDEV);
2350
2351 if (!passthrough) {
2352 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2353 pr_err("WRITE_SAME PBDATA and LBDATA"
2354 " bits not supported for Block Discard"
2355 " Emulation\n");
2356 return -ENOSYS;
2357 }
2358 /*
2359 * Currently for the emulated case we only accept
2360 * tpws with the UNMAP=1 bit set.
2361 */
2362 if (!(flags[0] & 0x08)) {
2363 pr_err("WRITE_SAME w/o UNMAP bit not"
2364 " supported for Block Discard Emulation\n");
2365 return -ENOSYS;
2366 }
2367 }
2368
2369 return 0;
2370}
2371
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002372/* transport_generic_cmd_sequencer():
2373 *
2374 * Generic Command Sequencer that should work for most DAS transport
2375 * drivers.
2376 *
Andy Grovera12f41f2012-04-03 15:51:20 -07002377 * Called from target_setup_cmd_from_cdb() in the $FABRIC_MOD
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002378 * RX Thread.
2379 *
2380 * FIXME: Need to support other SCSI OPCODES where as well.
2381 */
2382static int transport_generic_cmd_sequencer(
2383 struct se_cmd *cmd,
2384 unsigned char *cdb)
2385{
Andy Grover5951146d2011-07-19 10:26:37 +00002386 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002387 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2388 int ret = 0, sector_ret = 0, passthrough;
2389 u32 sectors = 0, size = 0, pr_reg_type = 0;
2390 u16 service_action;
2391 u8 alua_ascq = 0;
2392 /*
2393 * Check for an existing UNIT ATTENTION condition
2394 */
2395 if (core_scsi3_ua_check(cmd, cdb) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002396 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2397 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
Andy Grover5951146d2011-07-19 10:26:37 +00002398 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002399 }
2400 /*
2401 * Check status of Asymmetric Logical Unit Assignment port
2402 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002403 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002404 if (ret != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002405 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002406 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002407 * The ALUA additional sense code qualifier (ASCQ) is determined
2408 * by the ALUA primary or secondary access state..
2409 */
2410 if (ret > 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002411 pr_debug("[%s]: ALUA TG Port not available,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002412 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002413 cmd->se_tfo->get_fabric_name(), alua_ascq);
Andy Grover8b1e1242012-04-03 15:51:12 -07002414
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002415 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2416 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2417 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
Andy Grover5951146d2011-07-19 10:26:37 +00002418 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002419 }
2420 goto out_invalid_cdb_field;
2421 }
2422 /*
2423 * Check status for SPC-3 Persistent Reservations
2424 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002425 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2426 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002427 cmd, cdb, pr_reg_type) != 0) {
2428 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2429 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -07002430 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002431 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2432 return -EBUSY;
2433 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002434 /*
2435 * This means the CDB is allowed for the SCSI Initiator port
2436 * when said port is *NOT* holding the legacy SPC-2 or
2437 * SPC-3 Persistent Reservation.
2438 */
2439 }
2440
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002441 /*
2442 * If we operate in passthrough mode we skip most CDB emulation and
2443 * instead hand the commands down to the physical SCSI device.
2444 */
2445 passthrough =
2446 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
2447
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002448 switch (cdb[0]) {
2449 case READ_6:
2450 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2451 if (sector_ret)
2452 goto out_unsupported_cdb;
2453 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002454 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002455 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2456 break;
2457 case READ_10:
2458 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2459 if (sector_ret)
2460 goto out_unsupported_cdb;
2461 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002462 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002463 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2464 break;
2465 case READ_12:
2466 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2467 if (sector_ret)
2468 goto out_unsupported_cdb;
2469 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002470 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002471 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2472 break;
2473 case READ_16:
2474 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2475 if (sector_ret)
2476 goto out_unsupported_cdb;
2477 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002478 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002479 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2480 break;
2481 case WRITE_6:
2482 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2483 if (sector_ret)
2484 goto out_unsupported_cdb;
2485 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002486 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002487 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2488 break;
2489 case WRITE_10:
2490 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2491 if (sector_ret)
2492 goto out_unsupported_cdb;
2493 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002494 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002495 if (cdb[1] & 0x8)
2496 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002497 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2498 break;
2499 case WRITE_12:
2500 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2501 if (sector_ret)
2502 goto out_unsupported_cdb;
2503 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002504 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002505 if (cdb[1] & 0x8)
2506 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002507 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2508 break;
2509 case WRITE_16:
2510 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2511 if (sector_ret)
2512 goto out_unsupported_cdb;
2513 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002514 cmd->t_task_lba = transport_lba_64(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002515 if (cdb[1] & 0x8)
2516 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002517 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2518 break;
2519 case XDWRITEREAD_10:
2520 if ((cmd->data_direction != DMA_TO_DEVICE) ||
Christoph Hellwig33c3faf2011-11-14 11:36:30 -05002521 !(cmd->se_cmd_flags & SCF_BIDI))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002522 goto out_invalid_cdb_field;
2523 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2524 if (sector_ret)
2525 goto out_unsupported_cdb;
2526 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002527 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002528 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002529
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002530 /*
2531 * Do now allow BIDI commands for passthrough mode.
2532 */
2533 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002534 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002535
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002536 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002537 * Setup BIDI XOR callback to be run after I/O completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002538 */
2539 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002540 if (cdb[1] & 0x8)
2541 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002542 break;
2543 case VARIABLE_LENGTH_CMD:
2544 service_action = get_unaligned_be16(&cdb[8]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002545 switch (service_action) {
2546 case XDWRITEREAD_32:
2547 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2548 if (sector_ret)
2549 goto out_unsupported_cdb;
2550 size = transport_get_size(sectors, cdb, cmd);
2551 /*
2552 * Use WRITE_32 and READ_32 opcodes for the emulated
2553 * XDWRITE_READ_32 logic.
2554 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002555 cmd->t_task_lba = transport_lba_64_ext(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002556 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2557
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002558 /*
2559 * Do now allow BIDI commands for passthrough mode.
2560 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002561 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002562 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002563
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002564 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002565 * Setup BIDI XOR callback to be run during after I/O
2566 * completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002567 */
2568 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002569 if (cdb[1] & 0x8)
2570 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002571 break;
2572 case WRITE_SAME_32:
2573 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2574 if (sector_ret)
2575 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002576
Andy Grover6708bb22011-06-08 10:36:43 -07002577 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002578 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07002579 else {
2580 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2581 " supported\n");
2582 goto out_invalid_cdb_field;
2583 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002584
Andy Grovera1d8b492011-05-02 17:12:10 -07002585 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002586 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2587
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002588 if (target_check_write_same_discard(&cdb[10], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08002589 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002590 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002591 cmd->execute_cmd = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002592 break;
2593 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002594 pr_err("VARIABLE_LENGTH_CMD service action"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002595 " 0x%04x not supported\n", service_action);
2596 goto out_unsupported_cdb;
2597 }
2598 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002599 case MAINTENANCE_IN:
Andy Grovere3d6f902011-07-19 08:55:10 +00002600 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002601 /* MAINTENANCE_IN from SCC-2 */
2602 /*
2603 * Check for emulated MI_REPORT_TARGET_PGS.
2604 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002605 if (cdb[1] == MI_REPORT_TARGET_PGS &&
2606 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002607 cmd->execute_cmd =
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002608 target_emulate_report_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002609 }
2610 size = (cdb[6] << 24) | (cdb[7] << 16) |
2611 (cdb[8] << 8) | cdb[9];
2612 } else {
2613 /* GPCMD_SEND_KEY from multi media commands */
2614 size = (cdb[8] << 8) + cdb[9];
2615 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002616 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002617 break;
2618 case MODE_SELECT:
2619 size = cdb[4];
2620 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2621 break;
2622 case MODE_SELECT_10:
2623 size = (cdb[7] << 8) + cdb[8];
2624 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2625 break;
2626 case MODE_SENSE:
2627 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002628 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002629 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002630 cmd->execute_cmd = target_emulate_modesense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002631 break;
2632 case MODE_SENSE_10:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002633 size = (cdb[7] << 8) + cdb[8];
2634 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2635 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002636 cmd->execute_cmd = target_emulate_modesense;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002637 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002638 case GPCMD_READ_BUFFER_CAPACITY:
2639 case GPCMD_SEND_OPC:
2640 case LOG_SELECT:
2641 case LOG_SENSE:
2642 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002643 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002644 break;
2645 case READ_BLOCK_LIMITS:
2646 size = READ_BLOCK_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002647 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002648 break;
2649 case GPCMD_GET_CONFIGURATION:
2650 case GPCMD_READ_FORMAT_CAPACITIES:
2651 case GPCMD_READ_DISC_INFO:
2652 case GPCMD_READ_TRACK_RZONE_INFO:
2653 size = (cdb[7] << 8) + cdb[8];
2654 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2655 break;
2656 case PERSISTENT_RESERVE_IN:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002657 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002658 cmd->execute_cmd = target_scsi3_emulate_pr_in;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002659 size = (cdb[7] << 8) + cdb[8];
2660 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2661 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002662 case PERSISTENT_RESERVE_OUT:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002663 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002664 cmd->execute_cmd = target_scsi3_emulate_pr_out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002665 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002666 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002667 break;
2668 case GPCMD_MECHANISM_STATUS:
2669 case GPCMD_READ_DVD_STRUCTURE:
2670 size = (cdb[8] << 8) + cdb[9];
2671 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2672 break;
2673 case READ_POSITION:
2674 size = READ_POSITION_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002675 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002676 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002677 case MAINTENANCE_OUT:
Andy Grovere3d6f902011-07-19 08:55:10 +00002678 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002679 /* MAINTENANCE_OUT from SCC-2
2680 *
2681 * Check for emulated MO_SET_TARGET_PGS.
2682 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002683 if (cdb[1] == MO_SET_TARGET_PGS &&
2684 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002685 cmd->execute_cmd =
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002686 target_emulate_set_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002687 }
2688
2689 size = (cdb[6] << 24) | (cdb[7] << 16) |
2690 (cdb[8] << 8) | cdb[9];
2691 } else {
2692 /* GPCMD_REPORT_KEY from multi media commands */
2693 size = (cdb[8] << 8) + cdb[9];
2694 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002695 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002696 break;
2697 case INQUIRY:
2698 size = (cdb[3] << 8) + cdb[4];
2699 /*
2700 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2701 * See spc4r17 section 5.3
2702 */
Andy Grover5951146d2011-07-19 10:26:37 +00002703 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002704 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002705 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002706 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002707 cmd->execute_cmd = target_emulate_inquiry;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002708 break;
2709 case READ_BUFFER:
2710 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002711 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002712 break;
2713 case READ_CAPACITY:
2714 size = READ_CAP_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002715 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002716 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002717 cmd->execute_cmd = target_emulate_readcapacity;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002718 break;
2719 case READ_MEDIA_SERIAL_NUMBER:
2720 case SECURITY_PROTOCOL_IN:
2721 case SECURITY_PROTOCOL_OUT:
2722 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002723 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002724 break;
2725 case SERVICE_ACTION_IN:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002726 switch (cmd->t_task_cdb[1] & 0x1f) {
2727 case SAI_READ_CAPACITY_16:
2728 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002729 cmd->execute_cmd =
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002730 target_emulate_readcapacity_16;
2731 break;
2732 default:
2733 if (passthrough)
2734 break;
2735
2736 pr_err("Unsupported SA: 0x%02x\n",
2737 cmd->t_task_cdb[1] & 0x1f);
Roland Dreierb168fe82012-03-14 10:40:43 -07002738 goto out_invalid_cdb_field;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002739 }
2740 /*FALLTHROUGH*/
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002741 case ACCESS_CONTROL_IN:
2742 case ACCESS_CONTROL_OUT:
2743 case EXTENDED_COPY:
2744 case READ_ATTRIBUTE:
2745 case RECEIVE_COPY_RESULTS:
2746 case WRITE_ATTRIBUTE:
2747 size = (cdb[10] << 24) | (cdb[11] << 16) |
2748 (cdb[12] << 8) | cdb[13];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002749 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002750 break;
2751 case RECEIVE_DIAGNOSTIC:
2752 case SEND_DIAGNOSTIC:
2753 size = (cdb[3] << 8) | cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002754 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002755 break;
2756/* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2757#if 0
2758 case GPCMD_READ_CD:
2759 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2760 size = (2336 * sectors);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002761 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002762 break;
2763#endif
2764 case READ_TOC:
2765 size = cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002766 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002767 break;
2768 case REQUEST_SENSE:
2769 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002770 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002771 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002772 cmd->execute_cmd = target_emulate_request_sense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002773 break;
2774 case READ_ELEMENT_STATUS:
2775 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002776 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002777 break;
2778 case WRITE_BUFFER:
2779 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002780 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002781 break;
2782 case RESERVE:
2783 case RESERVE_10:
2784 /*
2785 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2786 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2787 */
2788 if (cdb[0] == RESERVE_10)
2789 size = (cdb[7] << 8) | cdb[8];
2790 else
2791 size = cmd->data_length;
2792
2793 /*
2794 * Setup the legacy emulated handler for SPC-2 and
2795 * >= SPC-3 compatible reservation handling (CRH=1)
2796 * Otherwise, we assume the underlying SCSI logic is
2797 * is running in SPC_PASSTHROUGH, and wants reservations
2798 * emulation disabled.
2799 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002800 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002801 cmd->execute_cmd = target_scsi2_reservation_reserve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002802 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2803 break;
2804 case RELEASE:
2805 case RELEASE_10:
2806 /*
2807 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2808 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2809 */
2810 if (cdb[0] == RELEASE_10)
2811 size = (cdb[7] << 8) | cdb[8];
2812 else
2813 size = cmd->data_length;
2814
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002815 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002816 cmd->execute_cmd = target_scsi2_reservation_release;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002817 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2818 break;
2819 case SYNCHRONIZE_CACHE:
Andy Grover8e94b8d2012-01-16 16:57:07 -08002820 case SYNCHRONIZE_CACHE_16:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002821 /*
2822 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2823 */
2824 if (cdb[0] == SYNCHRONIZE_CACHE) {
2825 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002826 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002827 } else {
2828 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002829 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002830 }
2831 if (sector_ret)
2832 goto out_unsupported_cdb;
2833
2834 size = transport_get_size(sectors, cdb, cmd);
2835 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2836
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002837 if (passthrough)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002838 break;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002839
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002840 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002841 * Check to ensure that LBA + Range does not exceed past end of
Nicholas Bellinger7abbe7f2011-08-10 18:41:14 -07002842 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002843 */
Nicholas Bellinger7abbe7f2011-08-10 18:41:14 -07002844 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
2845 if (transport_cmd_get_valid_sectors(cmd) < 0)
2846 goto out_invalid_cdb_field;
2847 }
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002848 cmd->execute_cmd = target_emulate_synchronize_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002849 break;
2850 case UNMAP:
2851 size = get_unaligned_be16(&cdb[7]);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002852 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002853 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002854 cmd->execute_cmd = target_emulate_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002855 break;
2856 case WRITE_SAME_16:
2857 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2858 if (sector_ret)
2859 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002860
Andy Grover6708bb22011-06-08 10:36:43 -07002861 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002862 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07002863 else {
2864 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2865 goto out_invalid_cdb_field;
2866 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002867
Nicholas Bellinger5db07532011-07-27 22:18:52 -07002868 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002869 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002870
2871 if (target_check_write_same_discard(&cdb[1], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08002872 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002873 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002874 cmd->execute_cmd = target_emulate_write_same;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002875 break;
2876 case WRITE_SAME:
2877 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2878 if (sector_ret)
2879 goto out_unsupported_cdb;
2880
2881 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002882 size = transport_get_size(1, cdb, cmd);
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002883 else {
2884 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2885 goto out_invalid_cdb_field;
2886 }
2887
2888 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
2889 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2890 /*
2891 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
2892 * of byte 1 bit 3 UNMAP instead of original reserved field
2893 */
2894 if (target_check_write_same_discard(&cdb[1], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08002895 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002896 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002897 cmd->execute_cmd = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002898 break;
2899 case ALLOW_MEDIUM_REMOVAL:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002900 case ERASE:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002901 case REZERO_UNIT:
2902 case SEEK_10:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002903 case SPACE:
2904 case START_STOP:
2905 case TEST_UNIT_READY:
2906 case VERIFY:
2907 case WRITE_FILEMARKS:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002908 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2909 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002910 cmd->execute_cmd = target_emulate_noop;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002911 break;
2912 case GPCMD_CLOSE_TRACK:
2913 case INITIALIZE_ELEMENT_STATUS:
2914 case GPCMD_LOAD_UNLOAD:
2915 case GPCMD_SET_SPEED:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002916 case MOVE_MEDIUM:
2917 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2918 break;
2919 case REPORT_LUNS:
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002920 cmd->execute_cmd = target_report_luns;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002921 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2922 /*
2923 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
2924 * See spc4r17 section 5.3
2925 */
Andy Grover5951146d2011-07-19 10:26:37 +00002926 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002927 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002928 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002929 break;
Stefan Hajnoczied0b2142012-04-30 16:35:13 +01002930 case GET_EVENT_STATUS_NOTIFICATION:
2931 size = (cdb[7] << 8) | cdb[8];
2932 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2933 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002934 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002935 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002936 " 0x%02x, sending CHECK_CONDITION.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002937 cmd->se_tfo->get_fabric_name(), cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002938 goto out_unsupported_cdb;
2939 }
2940
Sebastian Andrzej Siewiorb0d79942012-01-10 14:16:59 +01002941 if (cmd->unknown_data_length)
2942 cmd->data_length = size;
2943
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002944 if (size != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07002945 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002946 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
Andy Grovere3d6f902011-07-19 08:55:10 +00002947 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002948 cmd->data_length, size, cdb[0]);
2949
2950 cmd->cmd_spdtl = size;
2951
2952 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002953 pr_err("Rejecting underflow/overflow"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002954 " WRITE data\n");
2955 goto out_invalid_cdb_field;
2956 }
2957 /*
2958 * Reject READ_* or WRITE_* with overflow/underflow for
2959 * type SCF_SCSI_DATA_SG_IO_CDB.
2960 */
Andy Grover6708bb22011-06-08 10:36:43 -07002961 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
2962 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002963 " CDB on non 512-byte sector setup subsystem"
Andy Grovere3d6f902011-07-19 08:55:10 +00002964 " plugin: %s\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002965 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
2966 goto out_invalid_cdb_field;
2967 }
2968
2969 if (size > cmd->data_length) {
2970 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
2971 cmd->residual_count = (size - cmd->data_length);
2972 } else {
2973 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
2974 cmd->residual_count = (cmd->data_length - size);
2975 }
2976 cmd->data_length = size;
2977 }
2978
Roland Dreier015487b2012-02-13 16:18:17 -08002979 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB &&
Christoph Hellwigbebe2fd2012-03-26 17:53:17 -04002980 (sectors > dev->se_sub_dev->se_dev_attrib.fabric_max_sectors ||
2981 sectors > dev->se_sub_dev->se_dev_attrib.max_sectors)) {
Roland Dreier015487b2012-02-13 16:18:17 -08002982 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too big sectors %u\n",
2983 cdb[0], sectors);
2984 goto out_invalid_cdb_field;
2985 }
2986
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002987 /* reject any command that we don't have a handler for */
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002988 if (!(passthrough || cmd->execute_cmd ||
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002989 (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
2990 goto out_unsupported_cdb;
2991
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002992 transport_set_supported_SAM_opcode(cmd);
2993 return ret;
2994
2995out_unsupported_cdb:
2996 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2997 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover5951146d2011-07-19 10:26:37 +00002998 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002999out_invalid_cdb_field:
3000 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3001 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00003002 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003003}
3004
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003005/*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003006 * Called from I/O completion to determine which dormant/delayed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003007 * and ordered cmds need to have their tasks added to the execution queue.
3008 */
3009static void transport_complete_task_attr(struct se_cmd *cmd)
3010{
Andy Grover5951146d2011-07-19 10:26:37 +00003011 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003012 struct se_cmd *cmd_p, *cmd_tmp;
3013 int new_active_tasks = 0;
3014
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003015 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003016 atomic_dec(&dev->simple_cmds);
3017 smp_mb__after_atomic_dec();
3018 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003019 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003020 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3021 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003022 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003023 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003024 pr_debug("Incremented dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003025 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3026 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003027 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003028 atomic_dec(&dev->dev_ordered_sync);
3029 smp_mb__after_atomic_dec();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003030
3031 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003032 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003033 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3034 }
3035 /*
3036 * Process all commands up to the last received
3037 * ORDERED task attribute which requires another blocking
3038 * boundary
3039 */
3040 spin_lock(&dev->delayed_cmd_lock);
3041 list_for_each_entry_safe(cmd_p, cmd_tmp,
Andy Grover5951146d2011-07-19 10:26:37 +00003042 &dev->delayed_cmd_list, se_delayed_node) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003043
Andy Grover5951146d2011-07-19 10:26:37 +00003044 list_del(&cmd_p->se_delayed_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003045 spin_unlock(&dev->delayed_cmd_lock);
3046
Andy Grover6708bb22011-06-08 10:36:43 -07003047 pr_debug("Calling add_tasks() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003048 " cmd_p: 0x%02x Task Attr: 0x%02x"
3049 " Dormant -> Active, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07003050 cmd_p->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003051 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3052
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003053 target_add_to_execute_list(cmd_p);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003054 new_active_tasks++;
3055
3056 spin_lock(&dev->delayed_cmd_lock);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003057 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003058 break;
3059 }
3060 spin_unlock(&dev->delayed_cmd_lock);
3061 /*
3062 * If new tasks have become active, wake up the transport thread
3063 * to do the processing of the Active tasks.
3064 */
3065 if (new_active_tasks != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003066 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003067}
3068
Christoph Hellwige057f532011-10-17 13:56:41 -04003069static void transport_complete_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003070{
3071 int ret = 0;
3072
Christoph Hellwige057f532011-10-17 13:56:41 -04003073 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3074 transport_complete_task_attr(cmd);
3075
3076 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3077 ret = cmd->se_tfo->queue_status(cmd);
3078 if (ret)
3079 goto out;
3080 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003081
3082 switch (cmd->data_direction) {
3083 case DMA_FROM_DEVICE:
3084 ret = cmd->se_tfo->queue_data_in(cmd);
3085 break;
3086 case DMA_TO_DEVICE:
Andy Groverec98f782011-07-20 19:28:46 +00003087 if (cmd->t_bidi_data_sg) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003088 ret = cmd->se_tfo->queue_data_in(cmd);
3089 if (ret < 0)
Christoph Hellwige057f532011-10-17 13:56:41 -04003090 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003091 }
3092 /* Fall through for DMA_TO_DEVICE */
3093 case DMA_NONE:
3094 ret = cmd->se_tfo->queue_status(cmd);
3095 break;
3096 default:
3097 break;
3098 }
3099
Christoph Hellwige057f532011-10-17 13:56:41 -04003100out:
3101 if (ret < 0) {
3102 transport_handle_queue_full(cmd, cmd->se_dev);
3103 return;
3104 }
3105 transport_lun_remove_cmd(cmd);
3106 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003107}
3108
3109static void transport_handle_queue_full(
3110 struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -04003111 struct se_device *dev)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003112{
3113 spin_lock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003114 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3115 atomic_inc(&dev->dev_qf_count);
3116 smp_mb__after_atomic_inc();
3117 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3118
3119 schedule_work(&cmd->se_dev->qf_work_queue);
3120}
3121
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003122static void target_complete_ok_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003123{
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003124 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003125 int reason = 0, ret;
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003126
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003127 /*
3128 * Check if we need to move delayed/dormant tasks from cmds on the
3129 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3130 * Attribute.
3131 */
Andy Grover5951146d2011-07-19 10:26:37 +00003132 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003133 transport_complete_task_attr(cmd);
3134 /*
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003135 * Check to schedule QUEUE_FULL work, or execute an existing
3136 * cmd->transport_qf_callback()
3137 */
3138 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3139 schedule_work(&cmd->se_dev->qf_work_queue);
3140
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003141 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003142 * Check if we need to retrieve a sense buffer from
3143 * the struct se_cmd in question.
3144 */
3145 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3146 if (transport_get_sense_data(cmd) < 0)
3147 reason = TCM_NON_EXISTENT_LUN;
3148
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003149 if (cmd->scsi_status) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003150 ret = transport_send_check_condition_and_sense(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003151 cmd, reason, 1);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003152 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003153 goto queue_full;
3154
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003155 transport_lun_remove_cmd(cmd);
3156 transport_cmd_check_stop_to_fabric(cmd);
3157 return;
3158 }
3159 }
3160 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003161 * Check for a callback, used by amongst other things
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003162 * XDWRITE_READ_10 emulation.
3163 */
3164 if (cmd->transport_complete_callback)
3165 cmd->transport_complete_callback(cmd);
3166
3167 switch (cmd->data_direction) {
3168 case DMA_FROM_DEVICE:
3169 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003170 if (cmd->se_lun->lun_sep) {
3171 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003172 cmd->data_length;
3173 }
3174 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003175
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003176 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003177 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003178 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003179 break;
3180 case DMA_TO_DEVICE:
3181 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003182 if (cmd->se_lun->lun_sep) {
3183 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003184 cmd->data_length;
3185 }
3186 spin_unlock(&cmd->se_lun->lun_sep_lock);
3187 /*
3188 * Check if we need to send READ payload for BIDI-COMMAND
3189 */
Andy Groverec98f782011-07-20 19:28:46 +00003190 if (cmd->t_bidi_data_sg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003191 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003192 if (cmd->se_lun->lun_sep) {
3193 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003194 cmd->data_length;
3195 }
3196 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003197 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003198 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003199 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003200 break;
3201 }
3202 /* Fall through for DMA_TO_DEVICE */
3203 case DMA_NONE:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003204 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003205 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003206 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003207 break;
3208 default:
3209 break;
3210 }
3211
3212 transport_lun_remove_cmd(cmd);
3213 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003214 return;
3215
3216queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003217 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003218 " data_direction: %d\n", cmd, cmd->data_direction);
Christoph Hellwige057f532011-10-17 13:56:41 -04003219 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3220 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003221}
3222
Andy Grover6708bb22011-06-08 10:36:43 -07003223static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003224{
Andy Groverec98f782011-07-20 19:28:46 +00003225 struct scatterlist *sg;
Andy Groverec98f782011-07-20 19:28:46 +00003226 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003227
Andy Grover6708bb22011-06-08 10:36:43 -07003228 for_each_sg(sgl, sg, nents, count)
3229 __free_page(sg_page(sg));
3230
3231 kfree(sgl);
3232}
3233
3234static inline void transport_free_pages(struct se_cmd *cmd)
3235{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003236 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
Andy Grover6708bb22011-06-08 10:36:43 -07003237 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003238
Andy Grover6708bb22011-06-08 10:36:43 -07003239 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003240 cmd->t_data_sg = NULL;
3241 cmd->t_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003242
Andy Grover6708bb22011-06-08 10:36:43 -07003243 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003244 cmd->t_bidi_data_sg = NULL;
3245 cmd->t_bidi_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003246}
3247
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003248/**
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003249 * transport_release_cmd - free a command
3250 * @cmd: command to free
3251 *
3252 * This routine unconditionally frees a command, and reference counting
3253 * or list removal must be done in the caller.
3254 */
3255static void transport_release_cmd(struct se_cmd *cmd)
3256{
3257 BUG_ON(!cmd->se_tfo);
3258
Andy Groverc8e31f22012-01-19 13:39:17 -08003259 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003260 core_tmr_release_req(cmd->se_tmr_req);
3261 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3262 kfree(cmd->t_task_cdb);
3263 /*
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003264 * If this cmd has been setup with target_get_sess_cmd(), drop
3265 * the kref and call ->release_cmd() in kref callback.
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003266 */
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003267 if (cmd->check_release != 0) {
3268 target_put_sess_cmd(cmd->se_sess, cmd);
3269 return;
3270 }
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003271 cmd->se_tfo->release_cmd(cmd);
3272}
3273
3274/**
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003275 * transport_put_cmd - release a reference to a command
3276 * @cmd: command to release
3277 *
3278 * This routine releases our reference to the command and frees it if possible.
3279 */
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003280static void transport_put_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003281{
3282 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003283
Andy Grovera1d8b492011-05-02 17:12:10 -07003284 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003285 if (atomic_read(&cmd->t_fe_count)) {
3286 if (!atomic_dec_and_test(&cmd->t_fe_count))
3287 goto out_busy;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003288 }
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003289
3290 if (atomic_read(&cmd->t_se_count)) {
3291 if (!atomic_dec_and_test(&cmd->t_se_count))
3292 goto out_busy;
3293 }
3294
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003295 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
3296 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003297 target_remove_from_state_list(cmd);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003298 }
Andy Grovera1d8b492011-05-02 17:12:10 -07003299 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003300
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003301 transport_free_pages(cmd);
Christoph Hellwig31afc392011-09-13 23:08:11 +02003302 transport_release_cmd(cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003303 return;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003304out_busy:
3305 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003306}
3307
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003308/*
Andy Groverec98f782011-07-20 19:28:46 +00003309 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3310 * allocating in the core.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003311 * @cmd: Associated se_cmd descriptor
3312 * @mem: SGL style memory for TCM WRITE / READ
3313 * @sg_mem_num: Number of SGL elements
3314 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3315 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3316 *
3317 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3318 * of parameters.
3319 */
3320int transport_generic_map_mem_to_cmd(
3321 struct se_cmd *cmd,
Andy Grover5951146d2011-07-19 10:26:37 +00003322 struct scatterlist *sgl,
3323 u32 sgl_count,
3324 struct scatterlist *sgl_bidi,
3325 u32 sgl_bidi_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003326{
Andy Grover5951146d2011-07-19 10:26:37 +00003327 if (!sgl || !sgl_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003328 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003329
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003330 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3331 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
Nicholas Bellingerfef58a62011-11-15 22:13:24 -08003332 /*
3333 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
3334 * scatterlists already have been set to follow what the fabric
3335 * passes for the original expected data transfer length.
3336 */
3337 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
3338 pr_warn("Rejecting SCSI DATA overflow for fabric using"
3339 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
3340 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3341 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3342 return -EINVAL;
3343 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003344
Andy Groverec98f782011-07-20 19:28:46 +00003345 cmd->t_data_sg = sgl;
3346 cmd->t_data_nents = sgl_count;
3347
Andy Grover5951146d2011-07-19 10:26:37 +00003348 if (sgl_bidi && sgl_bidi_count) {
Andy Groverec98f782011-07-20 19:28:46 +00003349 cmd->t_bidi_data_sg = sgl_bidi;
3350 cmd->t_bidi_data_nents = sgl_bidi_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003351 }
3352 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003353 }
3354
3355 return 0;
3356}
3357EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3358
Andy Grover49493142012-01-16 16:57:08 -08003359void *transport_kmap_data_sg(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003360{
Andy Groverec98f782011-07-20 19:28:46 +00003361 struct scatterlist *sg = cmd->t_data_sg;
Andy Grover49493142012-01-16 16:57:08 -08003362 struct page **pages;
3363 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003364
Andy Groverec98f782011-07-20 19:28:46 +00003365 BUG_ON(!sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003366 /*
Andy Groverec98f782011-07-20 19:28:46 +00003367 * We need to take into account a possible offset here for fabrics like
3368 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3369 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
Andy Grover05d1c7c2011-07-20 19:13:28 +00003370 */
Andy Grover49493142012-01-16 16:57:08 -08003371 if (!cmd->t_data_nents)
3372 return NULL;
3373 else if (cmd->t_data_nents == 1)
3374 return kmap(sg_page(sg)) + sg->offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003375
Andy Grover49493142012-01-16 16:57:08 -08003376 /* >1 page. use vmap */
3377 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
3378 if (!pages)
3379 return NULL;
3380
3381 /* convert sg[] to pages[] */
3382 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
3383 pages[i] = sg_page(sg);
3384 }
3385
3386 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
3387 kfree(pages);
3388 if (!cmd->t_data_vmap)
3389 return NULL;
3390
3391 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003392}
Andy Grover49493142012-01-16 16:57:08 -08003393EXPORT_SYMBOL(transport_kmap_data_sg);
3394
3395void transport_kunmap_data_sg(struct se_cmd *cmd)
3396{
Andy Grovera1edf9c2012-02-09 12:18:06 -08003397 if (!cmd->t_data_nents) {
Andy Grover49493142012-01-16 16:57:08 -08003398 return;
Andy Grovera1edf9c2012-02-09 12:18:06 -08003399 } else if (cmd->t_data_nents == 1) {
Andy Grover49493142012-01-16 16:57:08 -08003400 kunmap(sg_page(cmd->t_data_sg));
Andy Grovera1edf9c2012-02-09 12:18:06 -08003401 return;
3402 }
Andy Grover49493142012-01-16 16:57:08 -08003403
3404 vunmap(cmd->t_data_vmap);
3405 cmd->t_data_vmap = NULL;
3406}
3407EXPORT_SYMBOL(transport_kunmap_data_sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003408
3409static int
3410transport_generic_get_mem(struct se_cmd *cmd)
3411{
Andy Groverec98f782011-07-20 19:28:46 +00003412 u32 length = cmd->data_length;
3413 unsigned int nents;
3414 struct page *page;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003415 gfp_t zero_flag;
Andy Groverec98f782011-07-20 19:28:46 +00003416 int i = 0;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003417
Andy Groverec98f782011-07-20 19:28:46 +00003418 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3419 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3420 if (!cmd->t_data_sg)
Andy Grovere3d6f902011-07-19 08:55:10 +00003421 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003422
Andy Groverec98f782011-07-20 19:28:46 +00003423 cmd->t_data_nents = nents;
3424 sg_init_table(cmd->t_data_sg, nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003425
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003426 zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
3427
Andy Groverec98f782011-07-20 19:28:46 +00003428 while (length) {
3429 u32 page_len = min_t(u32, length, PAGE_SIZE);
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003430 page = alloc_page(GFP_KERNEL | zero_flag);
Andy Groverec98f782011-07-20 19:28:46 +00003431 if (!page)
3432 goto out;
3433
3434 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3435 length -= page_len;
3436 i++;
3437 }
3438 return 0;
3439
3440out:
3441 while (i >= 0) {
3442 __free_page(sg_page(&cmd->t_data_sg[i]));
3443 i--;
3444 }
3445 kfree(cmd->t_data_sg);
3446 cmd->t_data_sg = NULL;
3447 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003448}
3449
Andy Grovera1d8b492011-05-02 17:12:10 -07003450/*
Andy Groverb16a35b2012-04-03 15:51:21 -07003451 * Allocate any required resources to execute the command. For writes we
3452 * might not have the payload yet, so notify the fabric via a call to
3453 * ->write_pending instead. Otherwise place it on the execution queue.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003454 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003455int transport_generic_new_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003456{
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003457 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003458 int ret = 0;
3459
3460 /*
3461 * Determine is the TCM fabric module has already allocated physical
3462 * memory, and is directly calling transport_generic_map_mem_to_cmd()
Andy Groverec98f782011-07-20 19:28:46 +00003463 * beforehand.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003464 */
Andy Groverec98f782011-07-20 19:28:46 +00003465 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3466 cmd->data_length) {
Andy Grover05d1c7c2011-07-20 19:13:28 +00003467 ret = transport_generic_get_mem(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003468 if (ret < 0)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003469 goto out_fail;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003470 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003471
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04003472 /* Workaround for handling zero-length control CDBs */
3473 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3474 !cmd->data_length) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003475 spin_lock_irq(&cmd->t_state_lock);
Roland Dreier410f6702011-11-22 13:51:32 -08003476 cmd->t_state = TRANSPORT_COMPLETE;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003477 cmd->transport_state |= CMD_T_ACTIVE;
3478 spin_unlock_irq(&cmd->t_state_lock);
Nicholas Bellinger91ec1d32012-01-13 12:01:34 -08003479
3480 if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
3481 u8 ua_asc = 0, ua_ascq = 0;
3482
3483 core_scsi3_ua_clear_for_request_sense(cmd,
3484 &ua_asc, &ua_ascq);
3485 }
3486
Roland Dreier410f6702011-11-22 13:51:32 -08003487 INIT_WORK(&cmd->work, target_complete_ok_work);
3488 queue_work(target_completion_wq, &cmd->work);
3489 return 0;
3490 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003491
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04003492 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3493 struct se_dev_attrib *attr = &dev->se_sub_dev->se_dev_attrib;
3494
3495 if (transport_cmd_get_valid_sectors(cmd) < 0)
3496 return -EINVAL;
3497
3498 BUG_ON(cmd->data_length % attr->block_size);
3499 BUG_ON(DIV_ROUND_UP(cmd->data_length, attr->block_size) >
3500 attr->max_sectors);
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003501 }
3502
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04003503 atomic_inc(&cmd->t_fe_count);
3504 atomic_inc(&cmd->t_se_count);
3505
Christoph Hellwig785fdf72012-04-24 00:25:04 -04003506 atomic_set(&cmd->t_task_cdbs_ex_left, 1);
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003507
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003508 /*
Christoph Hellwig5787cac2012-04-24 00:25:06 -04003509 * For WRITEs, let the fabric know its buffer is ready.
3510 *
3511 * The command will be added to the execution queue after its write
3512 * data has arrived.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003513 */
3514 if (cmd->data_direction == DMA_TO_DEVICE) {
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003515 target_add_to_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003516 return transport_generic_write_pending(cmd);
3517 }
3518 /*
Christoph Hellwig5787cac2012-04-24 00:25:06 -04003519 * Everything else but a WRITE, add the command to the execution queue.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003520 */
3521 transport_execute_tasks(cmd);
3522 return 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003523
3524out_fail:
3525 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3526 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3527 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003528}
Andy Grovera1d8b492011-05-02 17:12:10 -07003529EXPORT_SYMBOL(transport_generic_new_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003530
3531/* transport_generic_process_write():
3532 *
3533 *
3534 */
3535void transport_generic_process_write(struct se_cmd *cmd)
3536{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003537 transport_execute_tasks(cmd);
3538}
3539EXPORT_SYMBOL(transport_generic_process_write);
3540
Christoph Hellwige057f532011-10-17 13:56:41 -04003541static void transport_write_pending_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003542{
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003543 int ret;
3544
3545 ret = cmd->se_tfo->write_pending(cmd);
3546 if (ret == -EAGAIN || ret == -ENOMEM) {
Christoph Hellwige057f532011-10-17 13:56:41 -04003547 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3548 cmd);
3549 transport_handle_queue_full(cmd, cmd->se_dev);
3550 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003551}
3552
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003553static int transport_generic_write_pending(struct se_cmd *cmd)
3554{
3555 unsigned long flags;
3556 int ret;
3557
Andy Grovera1d8b492011-05-02 17:12:10 -07003558 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003559 cmd->t_state = TRANSPORT_WRITE_PENDING;
Andy Grovera1d8b492011-05-02 17:12:10 -07003560 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003561
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003562 /*
3563 * Clear the se_cmd for WRITE_PENDING status in order to set
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003564 * CMD_T_ACTIVE so that transport_generic_handle_data can be called
3565 * from HW target mode interrupt code. This is safe to be called
3566 * with transport_off=1 before the cmd->se_tfo->write_pending
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003567 * because the se_cmd->se_lun pointer is not being cleared.
3568 */
3569 transport_cmd_check_stop(cmd, 1, 0);
3570
3571 /*
3572 * Call the fabric write_pending function here to let the
3573 * frontend know that WRITE buffers are ready.
3574 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003575 ret = cmd->se_tfo->write_pending(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003576 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003577 goto queue_full;
3578 else if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003579 return ret;
3580
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003581 return 1;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003582
3583queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003584 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003585 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
Christoph Hellwige057f532011-10-17 13:56:41 -04003586 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003587 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003588}
3589
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003590void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003591{
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003592 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
Andy Groverc8e31f22012-01-19 13:39:17 -08003593 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003594 transport_wait_for_tasks(cmd);
3595
Christoph Hellwig35462972011-05-31 23:56:57 -04003596 transport_release_cmd(cmd);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003597 } else {
3598 if (wait_for_tasks)
3599 transport_wait_for_tasks(cmd);
3600
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003601 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
3602
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +02003603 if (cmd->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003604 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003605
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003606 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003607 }
3608}
3609EXPORT_SYMBOL(transport_generic_free_cmd);
3610
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003611/* target_get_sess_cmd - Add command to active ->sess_cmd_list
3612 * @se_sess: session to reference
3613 * @se_cmd: command descriptor to add
Nicholas Bellingera6360782011-11-18 20:36:22 -08003614 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003615 */
Nicholas Bellingera6360782011-11-18 20:36:22 -08003616void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
3617 bool ack_kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003618{
3619 unsigned long flags;
3620
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003621 kref_init(&se_cmd->cmd_kref);
Nicholas Bellingera6360782011-11-18 20:36:22 -08003622 /*
3623 * Add a second kref if the fabric caller is expecting to handle
3624 * fabric acknowledgement that requires two target_put_sess_cmd()
3625 * invocations before se_cmd descriptor release.
3626 */
Nicholas Bellinger86715562012-02-13 01:07:22 -08003627 if (ack_kref == true) {
Nicholas Bellingera6360782011-11-18 20:36:22 -08003628 kref_get(&se_cmd->cmd_kref);
Nicholas Bellinger86715562012-02-13 01:07:22 -08003629 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
3630 }
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003631
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003632 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3633 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
3634 se_cmd->check_release = 1;
3635 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3636}
3637EXPORT_SYMBOL(target_get_sess_cmd);
3638
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003639static void target_release_cmd_kref(struct kref *kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003640{
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003641 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
3642 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003643 unsigned long flags;
3644
3645 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3646 if (list_empty(&se_cmd->se_cmd_list)) {
3647 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Nicholas Bellingerffc32d52012-02-13 02:35:01 -08003648 se_cmd->se_tfo->release_cmd(se_cmd);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003649 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003650 }
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003651 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
3652 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3653 complete(&se_cmd->cmd_wait_comp);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003654 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003655 }
3656 list_del(&se_cmd->se_cmd_list);
3657 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3658
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003659 se_cmd->se_tfo->release_cmd(se_cmd);
3660}
3661
3662/* target_put_sess_cmd - Check for active I/O shutdown via kref_put
3663 * @se_sess: session to reference
3664 * @se_cmd: command descriptor to drop
3665 */
3666int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
3667{
3668 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003669}
3670EXPORT_SYMBOL(target_put_sess_cmd);
3671
3672/* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
3673 * @se_sess: session to split
3674 */
3675void target_splice_sess_cmd_list(struct se_session *se_sess)
3676{
3677 struct se_cmd *se_cmd;
3678 unsigned long flags;
3679
3680 WARN_ON(!list_empty(&se_sess->sess_wait_list));
3681 INIT_LIST_HEAD(&se_sess->sess_wait_list);
3682
3683 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3684 se_sess->sess_tearing_down = 1;
3685
3686 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
3687
3688 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
3689 se_cmd->cmd_wait_set = 1;
3690
3691 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3692}
3693EXPORT_SYMBOL(target_splice_sess_cmd_list);
3694
3695/* target_wait_for_sess_cmds - Wait for outstanding descriptors
3696 * @se_sess: session to wait for active I/O
3697 * @wait_for_tasks: Make extra transport_wait_for_tasks call
3698 */
3699void target_wait_for_sess_cmds(
3700 struct se_session *se_sess,
3701 int wait_for_tasks)
3702{
3703 struct se_cmd *se_cmd, *tmp_cmd;
3704 bool rc = false;
3705
3706 list_for_each_entry_safe(se_cmd, tmp_cmd,
3707 &se_sess->sess_wait_list, se_cmd_list) {
3708 list_del(&se_cmd->se_cmd_list);
3709
3710 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
3711 " %d\n", se_cmd, se_cmd->t_state,
3712 se_cmd->se_tfo->get_cmd_state(se_cmd));
3713
3714 if (wait_for_tasks) {
3715 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
3716 " fabric state: %d\n", se_cmd, se_cmd->t_state,
3717 se_cmd->se_tfo->get_cmd_state(se_cmd));
3718
3719 rc = transport_wait_for_tasks(se_cmd);
3720
3721 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
3722 " fabric state: %d\n", se_cmd, se_cmd->t_state,
3723 se_cmd->se_tfo->get_cmd_state(se_cmd));
3724 }
3725
3726 if (!rc) {
3727 wait_for_completion(&se_cmd->cmd_wait_comp);
3728 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
3729 " fabric state: %d\n", se_cmd, se_cmd->t_state,
3730 se_cmd->se_tfo->get_cmd_state(se_cmd));
3731 }
3732
3733 se_cmd->se_tfo->release_cmd(se_cmd);
3734 }
3735}
3736EXPORT_SYMBOL(target_wait_for_sess_cmds);
3737
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003738/* transport_lun_wait_for_tasks():
3739 *
3740 * Called from ConfigFS context to stop the passed struct se_cmd to allow
3741 * an struct se_lun to be successfully shutdown.
3742 */
3743static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
3744{
3745 unsigned long flags;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003746 int ret = 0;
3747
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003748 /*
3749 * If the frontend has already requested this struct se_cmd to
3750 * be stopped, we can safely ignore this struct se_cmd.
3751 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003752 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003753 if (cmd->transport_state & CMD_T_STOP) {
3754 cmd->transport_state &= ~CMD_T_LUN_STOP;
3755
3756 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
3757 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07003758 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003759 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovere3d6f902011-07-19 08:55:10 +00003760 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003761 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003762 cmd->transport_state |= CMD_T_LUN_FE_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07003763 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003764
Andy Grover5951146d2011-07-19 10:26:37 +00003765 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003766
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003767 // XXX: audit task_flags checks.
3768 spin_lock_irqsave(&cmd->t_state_lock, flags);
3769 if ((cmd->transport_state & CMD_T_BUSY) &&
3770 (cmd->transport_state & CMD_T_SENT)) {
3771 if (!target_stop_cmd(cmd, &flags))
3772 ret++;
3773 spin_lock_irqsave(&cmd->t_state_lock, flags);
3774 } else {
3775 spin_unlock_irqrestore(&cmd->t_state_lock,
3776 flags);
3777 target_remove_from_execute_list(cmd);
3778 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003779
Christoph Hellwig785fdf72012-04-24 00:25:04 -04003780 pr_debug("ConfigFS: cmd: %p stop tasks ret:"
3781 " %d\n", cmd, ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003782 if (!ret) {
Andy Grover6708bb22011-06-08 10:36:43 -07003783 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003784 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07003785 wait_for_completion(&cmd->transport_lun_stop_comp);
Andy Grover6708bb22011-06-08 10:36:43 -07003786 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003787 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003788 }
Christoph Hellwig3df8d402011-10-17 13:56:43 -04003789 transport_remove_cmd_from_queue(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003790
3791 return 0;
3792}
3793
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003794static void __transport_clear_lun_from_sessions(struct se_lun *lun)
3795{
3796 struct se_cmd *cmd = NULL;
3797 unsigned long lun_flags, cmd_flags;
3798 /*
3799 * Do exception processing and return CHECK_CONDITION status to the
3800 * Initiator Port.
3801 */
3802 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
Andy Grover5951146d2011-07-19 10:26:37 +00003803 while (!list_empty(&lun->lun_cmd_list)) {
3804 cmd = list_first_entry(&lun->lun_cmd_list,
3805 struct se_cmd, se_lun_node);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -05003806 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003807
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003808 /*
3809 * This will notify iscsi_target_transport.c:
3810 * transport_cmd_check_stop() that a LUN shutdown is in
3811 * progress for the iscsi_cmd_t.
3812 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003813 spin_lock(&cmd->t_state_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07003814 pr_debug("SE_LUN[%d] - Setting cmd->transport"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003815 "_lun_stop for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003816 cmd->se_lun->unpacked_lun,
3817 cmd->se_tfo->get_task_tag(cmd));
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003818 cmd->transport_state |= CMD_T_LUN_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07003819 spin_unlock(&cmd->t_state_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003820
3821 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
3822
Andy Grover6708bb22011-06-08 10:36:43 -07003823 if (!cmd->se_lun) {
3824 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003825 cmd->se_tfo->get_task_tag(cmd),
3826 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003827 BUG();
3828 }
3829 /*
3830 * If the Storage engine still owns the iscsi_cmd_t, determine
3831 * and/or stop its context.
3832 */
Andy Grover6708bb22011-06-08 10:36:43 -07003833 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
Andy Grovere3d6f902011-07-19 08:55:10 +00003834 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
3835 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003836
Andy Grovere3d6f902011-07-19 08:55:10 +00003837 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003838 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
3839 continue;
3840 }
3841
Andy Grover6708bb22011-06-08 10:36:43 -07003842 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003843 "_wait_for_tasks(): SUCCESS\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003844 cmd->se_lun->unpacked_lun,
3845 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003846
Andy Grovera1d8b492011-05-02 17:12:10 -07003847 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003848 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
Andy Grovera1d8b492011-05-02 17:12:10 -07003849 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003850 goto check_cond;
3851 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003852 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003853 target_remove_from_state_list(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07003854 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003855
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003856 /*
3857 * The Storage engine stopped this struct se_cmd before it was
3858 * send to the fabric frontend for delivery back to the
3859 * Initiator Node. Return this SCSI CDB back with an
3860 * CHECK_CONDITION status.
3861 */
3862check_cond:
3863 transport_send_check_condition_and_sense(cmd,
3864 TCM_NON_EXISTENT_LUN, 0);
3865 /*
3866 * If the fabric frontend is waiting for this iscsi_cmd_t to
3867 * be released, notify the waiting thread now that LU has
3868 * finished accessing it.
3869 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003870 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003871 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07003872 pr_debug("SE_LUN[%d] - Detected FE stop for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003873 " struct se_cmd: %p ITT: 0x%08x\n",
3874 lun->unpacked_lun,
Andy Grovere3d6f902011-07-19 08:55:10 +00003875 cmd, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003876
Andy Grovera1d8b492011-05-02 17:12:10 -07003877 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003878 cmd_flags);
3879 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovera1d8b492011-05-02 17:12:10 -07003880 complete(&cmd->transport_lun_fe_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003881 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
3882 continue;
3883 }
Andy Grover6708bb22011-06-08 10:36:43 -07003884 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003885 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003886
Andy Grovera1d8b492011-05-02 17:12:10 -07003887 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003888 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
3889 }
3890 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
3891}
3892
3893static int transport_clear_lun_thread(void *p)
3894{
Jörn Engel8359cf42011-11-24 02:05:51 +01003895 struct se_lun *lun = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003896
3897 __transport_clear_lun_from_sessions(lun);
3898 complete(&lun->lun_shutdown_comp);
3899
3900 return 0;
3901}
3902
3903int transport_clear_lun_from_sessions(struct se_lun *lun)
3904{
3905 struct task_struct *kt;
3906
Andy Grover5951146d2011-07-19 10:26:37 +00003907 kt = kthread_run(transport_clear_lun_thread, lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003908 "tcm_cl_%u", lun->unpacked_lun);
3909 if (IS_ERR(kt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003910 pr_err("Unable to start clear_lun thread\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00003911 return PTR_ERR(kt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003912 }
3913 wait_for_completion(&lun->lun_shutdown_comp);
3914
3915 return 0;
3916}
3917
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003918/**
3919 * transport_wait_for_tasks - wait for completion to occur
3920 * @cmd: command to wait
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003921 *
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003922 * Called from frontend fabric context to wait for storage engine
3923 * to pause and/or release frontend generated struct se_cmd.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003924 */
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003925bool transport_wait_for_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003926{
3927 unsigned long flags;
3928
Andy Grovera1d8b492011-05-02 17:12:10 -07003929 spin_lock_irqsave(&cmd->t_state_lock, flags);
Andy Groverc8e31f22012-01-19 13:39:17 -08003930 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
3931 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003932 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003933 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003934 }
3935 /*
3936 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
3937 * has been set in transport_set_supported_SAM_opcode().
3938 */
Andy Groverc8e31f22012-01-19 13:39:17 -08003939 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
3940 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003941 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003942 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003943 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003944 /*
3945 * If we are already stopped due to an external event (ie: LUN shutdown)
3946 * sleep until the connection can have the passed struct se_cmd back.
Andy Grovera1d8b492011-05-02 17:12:10 -07003947 * The cmd->transport_lun_stopped_sem will be upped by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003948 * transport_clear_lun_from_sessions() once the ConfigFS context caller
3949 * has completed its operation on the struct se_cmd.
3950 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003951 if (cmd->transport_state & CMD_T_LUN_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07003952 pr_debug("wait_for_tasks: Stopping"
Andy Grovere3d6f902011-07-19 08:55:10 +00003953 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003954 "_stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003955 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003956 /*
3957 * There is a special case for WRITES where a FE exception +
3958 * LUN shutdown means ConfigFS context is still sleeping on
3959 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
3960 * We go ahead and up transport_lun_stop_comp just to be sure
3961 * here.
3962 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003963 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3964 complete(&cmd->transport_lun_stop_comp);
3965 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
3966 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003967
Christoph Hellwigcf572a92012-04-24 00:25:05 -04003968 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003969 /*
3970 * At this point, the frontend who was the originator of this
3971 * struct se_cmd, now owns the structure and can be released through
3972 * normal means below.
3973 */
Andy Grover6708bb22011-06-08 10:36:43 -07003974 pr_debug("wait_for_tasks: Stopped"
Andy Grovere3d6f902011-07-19 08:55:10 +00003975 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003976 "stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003977 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003978
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003979 cmd->transport_state &= ~CMD_T_LUN_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003980 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003981
Nicholas Bellinger3d289342012-02-13 02:38:14 -08003982 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003983 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003984 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003985 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003986
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003987 cmd->transport_state |= CMD_T_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003988
Andy Grover6708bb22011-06-08 10:36:43 -07003989 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003990 " i_state: %d, t_state: %d, CMD_T_STOP\n",
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04003991 cmd, cmd->se_tfo->get_task_tag(cmd),
3992 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003993
Andy Grovera1d8b492011-05-02 17:12:10 -07003994 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003995
Andy Grover5951146d2011-07-19 10:26:37 +00003996 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003997
Andy Grovera1d8b492011-05-02 17:12:10 -07003998 wait_for_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003999
Andy Grovera1d8b492011-05-02 17:12:10 -07004000 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004001 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004002
Andy Grover6708bb22011-06-08 10:36:43 -07004003 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
Andy Grovera1d8b492011-05-02 17:12:10 -07004004 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004005 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004006
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004007 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004008
4009 return true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004010}
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004011EXPORT_SYMBOL(transport_wait_for_tasks);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004012
4013static int transport_get_sense_codes(
4014 struct se_cmd *cmd,
4015 u8 *asc,
4016 u8 *ascq)
4017{
4018 *asc = cmd->scsi_asc;
4019 *ascq = cmd->scsi_ascq;
4020
4021 return 0;
4022}
4023
4024static int transport_set_sense_codes(
4025 struct se_cmd *cmd,
4026 u8 asc,
4027 u8 ascq)
4028{
4029 cmd->scsi_asc = asc;
4030 cmd->scsi_ascq = ascq;
4031
4032 return 0;
4033}
4034
4035int transport_send_check_condition_and_sense(
4036 struct se_cmd *cmd,
4037 u8 reason,
4038 int from_transport)
4039{
4040 unsigned char *buffer = cmd->sense_buffer;
4041 unsigned long flags;
4042 int offset;
4043 u8 asc = 0, ascq = 0;
4044
Andy Grovera1d8b492011-05-02 17:12:10 -07004045 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004046 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004047 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004048 return 0;
4049 }
4050 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
Andy Grovera1d8b492011-05-02 17:12:10 -07004051 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004052
4053 if (!reason && from_transport)
4054 goto after_reason;
4055
4056 if (!from_transport)
4057 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4058 /*
4059 * Data Segment and SenseLength of the fabric response PDU.
4060 *
4061 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4062 * from include/scsi/scsi_cmnd.h
4063 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004064 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004065 TRANSPORT_SENSE_BUFFER);
4066 /*
4067 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4068 * SENSE KEY values from include/scsi/scsi.h
4069 */
4070 switch (reason) {
4071 case TCM_NON_EXISTENT_LUN:
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004072 /* CURRENT ERROR */
4073 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004074 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004075 /* ILLEGAL REQUEST */
4076 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4077 /* LOGICAL UNIT NOT SUPPORTED */
4078 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4079 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004080 case TCM_UNSUPPORTED_SCSI_OPCODE:
4081 case TCM_SECTOR_COUNT_TOO_MANY:
4082 /* CURRENT ERROR */
4083 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004084 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004085 /* ILLEGAL REQUEST */
4086 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4087 /* INVALID COMMAND OPERATION CODE */
4088 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4089 break;
4090 case TCM_UNKNOWN_MODE_PAGE:
4091 /* CURRENT ERROR */
4092 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004093 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004094 /* ILLEGAL REQUEST */
4095 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4096 /* INVALID FIELD IN CDB */
4097 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4098 break;
4099 case TCM_CHECK_CONDITION_ABORT_CMD:
4100 /* CURRENT ERROR */
4101 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004102 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004103 /* ABORTED COMMAND */
4104 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4105 /* BUS DEVICE RESET FUNCTION OCCURRED */
4106 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4107 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4108 break;
4109 case TCM_INCORRECT_AMOUNT_OF_DATA:
4110 /* CURRENT ERROR */
4111 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004112 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004113 /* ABORTED COMMAND */
4114 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4115 /* WRITE ERROR */
4116 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4117 /* NOT ENOUGH UNSOLICITED DATA */
4118 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4119 break;
4120 case TCM_INVALID_CDB_FIELD:
4121 /* CURRENT ERROR */
4122 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004123 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08004124 /* ILLEGAL REQUEST */
4125 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004126 /* INVALID FIELD IN CDB */
4127 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4128 break;
4129 case TCM_INVALID_PARAMETER_LIST:
4130 /* CURRENT ERROR */
4131 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004132 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08004133 /* ILLEGAL REQUEST */
4134 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004135 /* INVALID FIELD IN PARAMETER LIST */
4136 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4137 break;
4138 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4139 /* CURRENT ERROR */
4140 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004141 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004142 /* ABORTED COMMAND */
4143 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4144 /* WRITE ERROR */
4145 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4146 /* UNEXPECTED_UNSOLICITED_DATA */
4147 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4148 break;
4149 case TCM_SERVICE_CRC_ERROR:
4150 /* CURRENT ERROR */
4151 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004152 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004153 /* ABORTED COMMAND */
4154 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4155 /* PROTOCOL SERVICE CRC ERROR */
4156 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4157 /* N/A */
4158 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4159 break;
4160 case TCM_SNACK_REJECTED:
4161 /* CURRENT ERROR */
4162 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004163 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004164 /* ABORTED COMMAND */
4165 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4166 /* READ ERROR */
4167 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4168 /* FAILED RETRANSMISSION REQUEST */
4169 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4170 break;
4171 case TCM_WRITE_PROTECTED:
4172 /* CURRENT ERROR */
4173 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004174 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004175 /* DATA PROTECT */
4176 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4177 /* WRITE PROTECTED */
4178 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4179 break;
4180 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4181 /* CURRENT ERROR */
4182 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004183 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004184 /* UNIT ATTENTION */
4185 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4186 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4187 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4188 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4189 break;
4190 case TCM_CHECK_CONDITION_NOT_READY:
4191 /* CURRENT ERROR */
4192 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004193 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004194 /* Not Ready */
4195 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4196 transport_get_sense_codes(cmd, &asc, &ascq);
4197 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4198 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4199 break;
4200 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4201 default:
4202 /* CURRENT ERROR */
4203 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004204 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004205 /* ILLEGAL REQUEST */
4206 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4207 /* LOGICAL UNIT COMMUNICATION FAILURE */
4208 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4209 break;
4210 }
4211 /*
4212 * This code uses linux/include/scsi/scsi.h SAM status codes!
4213 */
4214 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4215 /*
4216 * Automatically padded, this value is encoded in the fabric's
4217 * data_length response PDU containing the SCSI defined sense data.
4218 */
4219 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4220
4221after_reason:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004222 return cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004223}
4224EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4225
4226int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4227{
4228 int ret = 0;
4229
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004230 if (cmd->transport_state & CMD_T_ABORTED) {
Andy Grover6708bb22011-06-08 10:36:43 -07004231 if (!send_status ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004232 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4233 return 1;
Andy Grover8b1e1242012-04-03 15:51:12 -07004234
Andy Grover6708bb22011-06-08 10:36:43 -07004235 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004236 " status for CDB: 0x%02x ITT: 0x%08x\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07004237 cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004238 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07004239
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004240 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
Andy Grovere3d6f902011-07-19 08:55:10 +00004241 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004242 ret = 1;
4243 }
4244 return ret;
4245}
4246EXPORT_SYMBOL(transport_check_aborted_status);
4247
4248void transport_send_task_abort(struct se_cmd *cmd)
4249{
Nicholas Bellingerc252f002011-09-29 14:22:13 -07004250 unsigned long flags;
4251
4252 spin_lock_irqsave(&cmd->t_state_lock, flags);
4253 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4254 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4255 return;
4256 }
4257 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4258
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004259 /*
4260 * If there are still expected incoming fabric WRITEs, we wait
4261 * until until they have completed before sending a TASK_ABORTED
4262 * response. This response with TASK_ABORTED status will be
4263 * queued back to fabric module by transport_check_aborted_status().
4264 */
4265 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004266 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004267 cmd->transport_state |= CMD_T_ABORTED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004268 smp_mb__after_atomic_inc();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004269 }
4270 }
4271 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
Andy Grover8b1e1242012-04-03 15:51:12 -07004272
Andy Grover6708bb22011-06-08 10:36:43 -07004273 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
Andy Grovera1d8b492011-05-02 17:12:10 -07004274 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004275 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07004276
Andy Grovere3d6f902011-07-19 08:55:10 +00004277 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004278}
4279
Christoph Hellwige26d99a2011-11-14 12:30:30 -05004280static int transport_generic_do_tmr(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004281{
Andy Grover5951146d2011-07-19 10:26:37 +00004282 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004283 struct se_tmr_req *tmr = cmd->se_tmr_req;
4284 int ret;
4285
4286 switch (tmr->function) {
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004287 case TMR_ABORT_TASK:
Nicholas Bellinger3d289342012-02-13 02:38:14 -08004288 core_tmr_abort_task(dev, tmr, cmd->se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004289 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004290 case TMR_ABORT_TASK_SET:
4291 case TMR_CLEAR_ACA:
4292 case TMR_CLEAR_TASK_SET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004293 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4294 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004295 case TMR_LUN_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004296 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4297 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4298 TMR_FUNCTION_REJECTED;
4299 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004300 case TMR_TARGET_WARM_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004301 tmr->response = TMR_FUNCTION_REJECTED;
4302 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004303 case TMR_TARGET_COLD_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004304 tmr->response = TMR_FUNCTION_REJECTED;
4305 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004306 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004307 pr_err("Uknown TMR function: 0x%02x.\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004308 tmr->function);
4309 tmr->response = TMR_FUNCTION_REJECTED;
4310 break;
4311 }
4312
4313 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
Andy Grovere3d6f902011-07-19 08:55:10 +00004314 cmd->se_tfo->queue_tm_rsp(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004315
Christoph Hellwigb7b8bef2011-10-17 13:56:44 -04004316 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004317 return 0;
4318}
4319
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004320/* transport_processing_thread():
4321 *
4322 *
4323 */
4324static int transport_processing_thread(void *param)
4325{
Andy Grover5951146d2011-07-19 10:26:37 +00004326 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004327 struct se_cmd *cmd;
Jörn Engel8359cf42011-11-24 02:05:51 +01004328 struct se_device *dev = param;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004329
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004330 while (!kthread_should_stop()) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004331 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4332 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004333 kthread_should_stop());
4334 if (ret < 0)
4335 goto out;
4336
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004337get_cmd:
Andy Grover5951146d2011-07-19 10:26:37 +00004338 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4339 if (!cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004340 continue;
4341
Andy Grover5951146d2011-07-19 10:26:37 +00004342 switch (cmd->t_state) {
Christoph Hellwig680b73c2011-09-12 21:51:14 +02004343 case TRANSPORT_NEW_CMD:
4344 BUG();
4345 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004346 case TRANSPORT_NEW_CMD_MAP:
Andy Grover6708bb22011-06-08 10:36:43 -07004347 if (!cmd->se_tfo->new_cmd_map) {
4348 pr_err("cmd->se_tfo->new_cmd_map is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004349 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4350 BUG();
4351 }
Andy Grovere3d6f902011-07-19 08:55:10 +00004352 ret = cmd->se_tfo->new_cmd_map(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004353 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004354 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004355 break;
4356 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004357 ret = transport_generic_new_cmd(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07004358 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004359 transport_generic_request_failure(cmd);
4360 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004361 }
4362 break;
4363 case TRANSPORT_PROCESS_WRITE:
4364 transport_generic_process_write(cmd);
4365 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004366 case TRANSPORT_PROCESS_TMR:
4367 transport_generic_do_tmr(cmd);
4368 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004369 case TRANSPORT_COMPLETE_QF_WP:
Christoph Hellwige057f532011-10-17 13:56:41 -04004370 transport_write_pending_qf(cmd);
4371 break;
4372 case TRANSPORT_COMPLETE_QF_OK:
4373 transport_complete_qf(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004374 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004375 default:
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004376 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4377 "i_state: %d on SE LUN: %u\n",
4378 cmd->t_state,
Andy Grovere3d6f902011-07-19 08:55:10 +00004379 cmd->se_tfo->get_task_tag(cmd),
4380 cmd->se_tfo->get_cmd_state(cmd),
4381 cmd->se_lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004382 BUG();
4383 }
4384
4385 goto get_cmd;
4386 }
4387
4388out:
Christoph Hellwigcf572a92012-04-24 00:25:05 -04004389 WARN_ON(!list_empty(&dev->state_list));
Nicholas Bellingerce8762f2011-10-09 02:19:01 -07004390 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004391 dev->process_thread = NULL;
4392 return 0;
4393}