blob: 19804b6fdbaa11a5b4b5a9b0f98280e16a55b4a6 [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>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080040#include <asm/unaligned.h>
41#include <net/sock.h>
42#include <net/tcp.h>
43#include <scsi/scsi.h>
44#include <scsi/scsi_cmnd.h>
Nicholas Bellingere66ecd52011-05-19 20:19:14 -070045#include <scsi/scsi_tcq.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080046
47#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050048#include <target/target_core_backend.h>
49#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080050#include <target/target_core_configfs.h>
51
Christoph Hellwige26d99a2011-11-14 12:30:30 -050052#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080053#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080054#include "target_core_pr.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080055#include "target_core_ua.h"
56
Andy Grovere3d6f902011-07-19 08:55:10 +000057static int sub_api_initialized;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080058
Christoph Hellwig35e0e752011-10-17 13:56:53 -040059static struct workqueue_struct *target_completion_wq;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080060static struct kmem_cache *se_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080061struct kmem_cache *se_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080062struct kmem_cache *t10_pr_reg_cache;
63struct kmem_cache *t10_alua_lu_gp_cache;
64struct kmem_cache *t10_alua_lu_gp_mem_cache;
65struct kmem_cache *t10_alua_tg_pt_gp_cache;
66struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
67
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080068static int transport_generic_write_pending(struct se_cmd *);
Andy Grover5951146d2011-07-19 10:26:37 +000069static int transport_processing_thread(void *param);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -080070static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080071static void transport_complete_task_attr(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -070072static void transport_handle_queue_full(struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -040073 struct se_device *dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080074static void transport_free_dev_tasks(struct se_cmd *cmd);
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 Bellingerc66ac9d2010-12-17 11:11:26 -0800248
249 return se_sess;
250}
251EXPORT_SYMBOL(transport_init_session);
252
253/*
254 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
255 */
256void __transport_register_session(
257 struct se_portal_group *se_tpg,
258 struct se_node_acl *se_nacl,
259 struct se_session *se_sess,
260 void *fabric_sess_ptr)
261{
262 unsigned char buf[PR_REG_ISID_LEN];
263
264 se_sess->se_tpg = se_tpg;
265 se_sess->fabric_sess_ptr = fabric_sess_ptr;
266 /*
267 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
268 *
269 * Only set for struct se_session's that will actually be moving I/O.
270 * eg: *NOT* discovery sessions.
271 */
272 if (se_nacl) {
273 /*
274 * If the fabric module supports an ISID based TransportID,
275 * save this value in binary from the fabric I_T Nexus now.
276 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000277 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800278 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +0000279 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800280 &buf[0], PR_REG_ISID_LEN);
281 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
282 }
283 spin_lock_irq(&se_nacl->nacl_sess_lock);
284 /*
285 * The se_nacl->nacl_sess pointer will be set to the
286 * last active I_T Nexus for each struct se_node_acl.
287 */
288 se_nacl->nacl_sess = se_sess;
289
290 list_add_tail(&se_sess->sess_acl_list,
291 &se_nacl->acl_sess_list);
292 spin_unlock_irq(&se_nacl->nacl_sess_lock);
293 }
294 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
295
Andy Grover6708bb22011-06-08 10:36:43 -0700296 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000297 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800298}
299EXPORT_SYMBOL(__transport_register_session);
300
301void transport_register_session(
302 struct se_portal_group *se_tpg,
303 struct se_node_acl *se_nacl,
304 struct se_session *se_sess,
305 void *fabric_sess_ptr)
306{
307 spin_lock_bh(&se_tpg->session_lock);
308 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
309 spin_unlock_bh(&se_tpg->session_lock);
310}
311EXPORT_SYMBOL(transport_register_session);
312
313void transport_deregister_session_configfs(struct se_session *se_sess)
314{
315 struct se_node_acl *se_nacl;
Roland Dreier23388862011-06-22 01:02:21 -0700316 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800317 /*
318 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
319 */
320 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700321 if (se_nacl) {
Roland Dreier23388862011-06-22 01:02:21 -0700322 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800323 list_del(&se_sess->sess_acl_list);
324 /*
325 * If the session list is empty, then clear the pointer.
326 * Otherwise, set the struct se_session pointer from the tail
327 * element of the per struct se_node_acl active session list.
328 */
329 if (list_empty(&se_nacl->acl_sess_list))
330 se_nacl->nacl_sess = NULL;
331 else {
332 se_nacl->nacl_sess = container_of(
333 se_nacl->acl_sess_list.prev,
334 struct se_session, sess_acl_list);
335 }
Roland Dreier23388862011-06-22 01:02:21 -0700336 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800337 }
338}
339EXPORT_SYMBOL(transport_deregister_session_configfs);
340
341void transport_free_session(struct se_session *se_sess)
342{
343 kmem_cache_free(se_sess_cache, se_sess);
344}
345EXPORT_SYMBOL(transport_free_session);
346
347void transport_deregister_session(struct se_session *se_sess)
348{
349 struct se_portal_group *se_tpg = se_sess->se_tpg;
350 struct se_node_acl *se_nacl;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700351 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800352
Andy Grover6708bb22011-06-08 10:36:43 -0700353 if (!se_tpg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800354 transport_free_session(se_sess);
355 return;
356 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800357
Roland Dreiere63a8e12011-08-12 16:01:02 -0700358 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800359 list_del(&se_sess->sess_list);
360 se_sess->se_tpg = NULL;
361 se_sess->fabric_sess_ptr = NULL;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700362 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800363
364 /*
365 * Determine if we need to do extra work for this initiator node's
366 * struct se_node_acl if it had been previously dynamically generated.
367 */
368 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700369 if (se_nacl) {
Roland Dreiere63a8e12011-08-12 16:01:02 -0700370 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800371 if (se_nacl->dynamic_node_acl) {
Andy Grover6708bb22011-06-08 10:36:43 -0700372 if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
373 se_tpg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800374 list_del(&se_nacl->acl_list);
375 se_tpg->num_node_acls--;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700376 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800377
378 core_tpg_wait_for_nacl_pr_ref(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800379 core_free_device_list_for_node(se_nacl, se_tpg);
Andy Grovere3d6f902011-07-19 08:55:10 +0000380 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800381 se_nacl);
Roland Dreiere63a8e12011-08-12 16:01:02 -0700382 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800383 }
384 }
Roland Dreiere63a8e12011-08-12 16:01:02 -0700385 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800386 }
387
388 transport_free_session(se_sess);
389
Andy Grover6708bb22011-06-08 10:36:43 -0700390 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000391 se_tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800392}
393EXPORT_SYMBOL(transport_deregister_session);
394
395/*
Andy Grovera1d8b492011-05-02 17:12:10 -0700396 * Called with cmd->t_state_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800397 */
398static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
399{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400400 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800401 struct se_task *task;
402 unsigned long flags;
403
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400404 if (!dev)
405 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400407 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400408 if (task->task_flags & TF_ACTIVE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800409 continue;
410
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800411 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500412 if (task->t_state_active) {
413 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
414 cmd->se_tfo->get_task_tag(cmd), dev, task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800415
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500416 list_del(&task->t_state_list);
417 atomic_dec(&cmd->t_task_cdbs_ex_left);
418 task->t_state_active = false;
419 }
420 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800421 }
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500422
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800423}
424
425/* transport_cmd_check_stop():
426 *
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500427 * 'transport_off = 1' determines if CMD_T_ACTIVE should be cleared.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800428 * 'transport_off = 2' determines if task_dev_state should be removed.
429 *
430 * A non-zero u8 t_state sets cmd->t_state.
431 * Returns 1 when command is stopped, else 0.
432 */
433static int transport_cmd_check_stop(
434 struct se_cmd *cmd,
435 int transport_off,
436 u8 t_state)
437{
438 unsigned long flags;
439
Andy Grovera1d8b492011-05-02 17:12:10 -0700440 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800441 /*
442 * Determine if IOCTL context caller in requesting the stopping of this
443 * command for LUN shutdown purposes.
444 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500445 if (cmd->transport_state & CMD_T_LUN_STOP) {
446 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
447 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800448
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500449 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800450 if (transport_off == 2)
451 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700452 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800453
Andy Grovera1d8b492011-05-02 17:12:10 -0700454 complete(&cmd->transport_lun_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800455 return 1;
456 }
457 /*
458 * Determine if frontend context caller is requesting the stopping of
Andy Grovere3d6f902011-07-19 08:55:10 +0000459 * this command for frontend exceptions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800460 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500461 if (cmd->transport_state & CMD_T_STOP) {
462 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
463 __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000464 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800465
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800466 if (transport_off == 2)
467 transport_all_task_dev_remove_state(cmd);
468
469 /*
470 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
471 * to FE.
472 */
473 if (transport_off == 2)
474 cmd->se_lun = NULL;
Andy Grovera1d8b492011-05-02 17:12:10 -0700475 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800476
Andy Grovera1d8b492011-05-02 17:12:10 -0700477 complete(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800478 return 1;
479 }
480 if (transport_off) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500481 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800482 if (transport_off == 2) {
483 transport_all_task_dev_remove_state(cmd);
484 /*
485 * Clear struct se_cmd->se_lun before the transport_off == 2
486 * handoff to fabric module.
487 */
488 cmd->se_lun = NULL;
489 /*
490 * Some fabric modules like tcm_loop can release
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300491 * their internally allocated I/O reference now and
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800492 * struct se_cmd now.
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700493 *
494 * Fabric modules are expected to return '1' here if the
495 * se_cmd being passed is released at this point,
496 * or zero if not being released.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800497 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000498 if (cmd->se_tfo->check_stop_free != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800499 spin_unlock_irqrestore(
Andy Grovera1d8b492011-05-02 17:12:10 -0700500 &cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800501
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700502 return cmd->se_tfo->check_stop_free(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800503 }
504 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700505 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800506
507 return 0;
508 } else if (t_state)
509 cmd->t_state = t_state;
Andy Grovera1d8b492011-05-02 17:12:10 -0700510 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800511
512 return 0;
513}
514
515static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
516{
517 return transport_cmd_check_stop(cmd, 2, 0);
518}
519
520static void transport_lun_remove_cmd(struct se_cmd *cmd)
521{
Andy Grovere3d6f902011-07-19 08:55:10 +0000522 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800523 unsigned long flags;
524
525 if (!lun)
526 return;
527
Andy Grovera1d8b492011-05-02 17:12:10 -0700528 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500529 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
530 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
531 transport_all_task_dev_remove_state(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800532 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700533 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800534
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800535 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -0500536 if (!list_empty(&cmd->se_lun_node))
537 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800538 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
539}
540
541void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
542{
Andy Groverc8e31f22012-01-19 13:39:17 -0800543 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellinger8dc52b52011-10-09 02:02:51 -0700544 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800545
546 if (transport_cmd_check_stop_to_fabric(cmd))
547 return;
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700548 if (remove) {
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400549 transport_remove_cmd_from_queue(cmd);
Christoph Hellwige6a25732011-09-13 23:08:50 +0200550 transport_put_cmd(cmd);
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700551 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800552}
553
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400554static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
555 bool at_head)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800556{
557 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +0000558 struct se_queue_obj *qobj = &dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800559 unsigned long flags;
560
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800561 if (t_state) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700562 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800563 cmd->t_state = t_state;
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500564 cmd->transport_state |= CMD_T_ACTIVE;
Andy Grovera1d8b492011-05-02 17:12:10 -0700565 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800566 }
567
568 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Roland Dreier79a7fef2011-09-28 22:12:07 -0700569
570 /* If the cmd is already on the list, remove it before we add it */
571 if (!list_empty(&cmd->se_queue_node))
572 list_del(&cmd->se_queue_node);
573 else
574 atomic_inc(&qobj->queue_cnt);
575
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400576 if (at_head)
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700577 list_add(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400578 else
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700579 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500580 cmd->transport_state |= CMD_T_QUEUED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800581 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
582
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800583 wake_up_interruptible(&qobj->thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800584}
585
Andy Grover5951146d2011-07-19 10:26:37 +0000586static struct se_cmd *
587transport_get_cmd_from_queue(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588{
Andy Grover5951146d2011-07-19 10:26:37 +0000589 struct se_cmd *cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800590 unsigned long flags;
591
592 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
593 if (list_empty(&qobj->qobj_list)) {
594 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
595 return NULL;
596 }
Andy Grover5951146d2011-07-19 10:26:37 +0000597 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800598
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500599 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700600 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800601 atomic_dec(&qobj->queue_cnt);
602 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
603
Andy Grover5951146d2011-07-19 10:26:37 +0000604 return cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800605}
606
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400607static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800608{
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400609 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800610 unsigned long flags;
611
612 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500613 if (!(cmd->transport_state & CMD_T_QUEUED)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800614 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
615 return;
616 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500617 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700618 atomic_dec(&qobj->queue_cnt);
619 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800620 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800621}
622
623/*
624 * Completion function used by TCM subsystem plugins (such as FILEIO)
625 * for queueing up response from struct se_subsystem_api->do_task()
626 */
627void transport_complete_sync_cache(struct se_cmd *cmd, int good)
628{
Andy Grovera1d8b492011-05-02 17:12:10 -0700629 struct se_task *task = list_entry(cmd->t_task_list.next,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800630 struct se_task, t_list);
631
632 if (good) {
633 cmd->scsi_status = SAM_STAT_GOOD;
634 task->task_scsi_status = GOOD;
635 } else {
636 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700637 task->task_se_cmd->scsi_sense_reason =
638 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
639
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800640 }
641
642 transport_complete_task(task, good);
643}
644EXPORT_SYMBOL(transport_complete_sync_cache);
645
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400646static void target_complete_failure_work(struct work_struct *work)
647{
648 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
649
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700650 transport_generic_request_failure(cmd);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400651}
652
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800653/* transport_complete_task():
654 *
655 * Called from interrupt and non interrupt context depending
656 * on the transport plugin.
657 */
658void transport_complete_task(struct se_task *task, int success)
659{
Andy Grovere3d6f902011-07-19 08:55:10 +0000660 struct se_cmd *cmd = task->task_se_cmd;
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400661 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800662 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800663
Andy Grovera1d8b492011-05-02 17:12:10 -0700664 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400665 task->task_flags &= ~TF_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800666
667 /*
668 * See if any sense data exists, if so set the TASK_SENSE flag.
669 * Also check for any other post completion work that needs to be
670 * done by the plugins.
671 */
672 if (dev && dev->transport->transport_complete) {
673 if (dev->transport->transport_complete(task) != 0) {
674 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
Christoph Hellwigef804a82011-11-23 06:53:58 -0500675 task->task_flags |= TF_HAS_SENSE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800676 success = 1;
677 }
678 }
679
680 /*
681 * See if we are waiting for outstanding struct se_task
682 * to complete for an exception condition
683 */
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400684 if (task->task_flags & TF_REQUEST_STOP) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700685 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800686 complete(&task->task_stop_comp);
687 return;
688 }
Christoph Hellwig22350072011-11-02 05:06:35 -0700689
690 if (!success)
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500691 cmd->transport_state |= CMD_T_FAILED;
Christoph Hellwig22350072011-11-02 05:06:35 -0700692
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800693 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800694 * Decrement the outstanding t_task_cdbs_left count. The last
695 * struct se_task from struct se_cmd will complete itself into the
696 * device queue depending upon int success.
697 */
Andy Grover6708bb22011-06-08 10:36:43 -0700698 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700699 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800700 return;
701 }
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800702 /*
703 * Check for case where an explict ABORT_TASK has been received
704 * and transport_wait_for_tasks() will be waiting for completion..
705 */
706 if (cmd->transport_state & CMD_T_ABORTED &&
707 cmd->transport_state & CMD_T_STOP) {
708 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
709 complete(&cmd->t_transport_stop_comp);
710 return;
711 } else if (cmd->transport_state & CMD_T_FAILED) {
Christoph Hellwig41e16e92011-11-23 06:54:15 -0500712 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400713 INIT_WORK(&cmd->work, target_complete_failure_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800714 } else {
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400715 INIT_WORK(&cmd->work, target_complete_ok_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800716 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400717
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400718 cmd->t_state = TRANSPORT_COMPLETE;
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800719 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
Andy Grovera1d8b492011-05-02 17:12:10 -0700720 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800721
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400722 queue_work(target_completion_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800723}
724EXPORT_SYMBOL(transport_complete_task);
725
726/*
727 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
728 * struct se_task list are ready to be added to the active execution list
729 * struct se_device
730
731 * Called with se_dev_t->execute_task_lock called.
732 */
733static inline int transport_add_task_check_sam_attr(
734 struct se_task *task,
735 struct se_task *task_prev,
736 struct se_device *dev)
737{
738 /*
739 * No SAM Task attribute emulation enabled, add to tail of
740 * execution queue
741 */
742 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
743 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
744 return 0;
745 }
746 /*
747 * HEAD_OF_QUEUE attribute for received CDB, which means
748 * the first task that is associated with a struct se_cmd goes to
749 * head of the struct se_device->execute_task_list, and task_prev
750 * after that for each subsequent task
751 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -0700752 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800753 list_add(&task->t_execute_list,
754 (task_prev != NULL) ?
755 &task_prev->t_execute_list :
756 &dev->execute_task_list);
757
Andy Grover6708bb22011-06-08 10:36:43 -0700758 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800759 " in execution queue\n",
Andy Grover6708bb22011-06-08 10:36:43 -0700760 task->task_se_cmd->t_task_cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800761 return 1;
762 }
763 /*
764 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
765 * transitioned from Dermant -> Active state, and are added to the end
766 * of the struct se_device->execute_task_list
767 */
768 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
769 return 0;
770}
771
772/* __transport_add_task_to_execute_queue():
773 *
774 * Called with se_dev_t->execute_task_lock called.
775 */
776static void __transport_add_task_to_execute_queue(
777 struct se_task *task,
778 struct se_task *task_prev,
779 struct se_device *dev)
780{
781 int head_of_queue;
782
783 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
784 atomic_inc(&dev->execute_tasks);
785
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500786 if (task->t_state_active)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800787 return;
788 /*
789 * Determine if this task needs to go to HEAD_OF_QUEUE for the
790 * state list as well. Running with SAM Task Attribute emulation
791 * will always return head_of_queue == 0 here
792 */
793 if (head_of_queue)
794 list_add(&task->t_state_list, (task_prev) ?
795 &task_prev->t_state_list :
796 &dev->state_task_list);
797 else
798 list_add_tail(&task->t_state_list, &dev->state_task_list);
799
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500800 task->t_state_active = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800801
Andy Grover6708bb22011-06-08 10:36:43 -0700802 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000803 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800804 task, dev);
805}
806
807static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
808{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400809 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800810 struct se_task *task;
811 unsigned long flags;
812
Andy Grovera1d8b492011-05-02 17:12:10 -0700813 spin_lock_irqsave(&cmd->t_state_lock, flags);
814 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800815 spin_lock(&dev->execute_task_lock);
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500816 if (!task->t_state_active) {
817 list_add_tail(&task->t_state_list,
818 &dev->state_task_list);
819 task->t_state_active = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800820
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500821 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
822 task->task_se_cmd->se_tfo->get_task_tag(
823 task->task_se_cmd), task, dev);
824 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800825 spin_unlock(&dev->execute_task_lock);
826 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700827 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800828}
829
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800830static void __transport_add_tasks_from_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800831{
Andy Grover5951146d2011-07-19 10:26:37 +0000832 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800833 struct se_task *task, *task_prev = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800834
Andy Grovera1d8b492011-05-02 17:12:10 -0700835 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Christoph Hellwig04629b72011-10-12 11:07:04 -0400836 if (!list_empty(&task->t_execute_list))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800837 continue;
838 /*
839 * __transport_add_task_to_execute_queue() handles the
840 * SAM Task Attribute emulation if enabled
841 */
842 __transport_add_task_to_execute_queue(task, task_prev, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800843 task_prev = task;
844 }
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800845}
846
847static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
848{
849 unsigned long flags;
850 struct se_device *dev = cmd->se_dev;
851
852 spin_lock_irqsave(&dev->execute_task_lock, flags);
853 __transport_add_tasks_from_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800854 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800855}
856
Christoph Hellwig04629b72011-10-12 11:07:04 -0400857void __transport_remove_task_from_execute_queue(struct se_task *task,
858 struct se_device *dev)
859{
860 list_del_init(&task->t_execute_list);
861 atomic_dec(&dev->execute_tasks);
862}
863
Christoph Hellwige26d99a2011-11-14 12:30:30 -0500864static void transport_remove_task_from_execute_queue(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800865 struct se_task *task,
866 struct se_device *dev)
867{
868 unsigned long flags;
869
Christoph Hellwig04629b72011-10-12 11:07:04 -0400870 if (WARN_ON(list_empty(&task->t_execute_list)))
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700871 return;
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700872
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800873 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwig04629b72011-10-12 11:07:04 -0400874 __transport_remove_task_from_execute_queue(task, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800875 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
876}
877
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700878/*
Nicholas Bellingerf147abb2011-10-25 23:57:41 -0700879 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700880 */
881
882static void target_qf_do_work(struct work_struct *work)
883{
884 struct se_device *dev = container_of(work, struct se_device,
885 qf_work_queue);
Roland Dreierbcac3642011-08-27 21:33:16 -0700886 LIST_HEAD(qf_cmd_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700887 struct se_cmd *cmd, *cmd_tmp;
888
889 spin_lock_irq(&dev->qf_cmd_lock);
Roland Dreierbcac3642011-08-27 21:33:16 -0700890 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
891 spin_unlock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700892
Roland Dreierbcac3642011-08-27 21:33:16 -0700893 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700894 list_del(&cmd->se_qf_node);
895 atomic_dec(&dev->dev_qf_count);
896 smp_mb__after_atomic_dec();
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700897
Andy Grover6708bb22011-06-08 10:36:43 -0700898 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700899 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -0400900 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700901 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
902 : "UNKNOWN");
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400903
904 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700905 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700906}
907
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800908unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
909{
910 switch (cmd->data_direction) {
911 case DMA_NONE:
912 return "NONE";
913 case DMA_FROM_DEVICE:
914 return "READ";
915 case DMA_TO_DEVICE:
916 return "WRITE";
917 case DMA_BIDIRECTIONAL:
918 return "BIDI";
919 default:
920 break;
921 }
922
923 return "UNKNOWN";
924}
925
926void transport_dump_dev_state(
927 struct se_device *dev,
928 char *b,
929 int *bl)
930{
931 *bl += sprintf(b + *bl, "Status: ");
932 switch (dev->dev_status) {
933 case TRANSPORT_DEVICE_ACTIVATED:
934 *bl += sprintf(b + *bl, "ACTIVATED");
935 break;
936 case TRANSPORT_DEVICE_DEACTIVATED:
937 *bl += sprintf(b + *bl, "DEACTIVATED");
938 break;
939 case TRANSPORT_DEVICE_SHUTDOWN:
940 *bl += sprintf(b + *bl, "SHUTDOWN");
941 break;
942 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
943 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
944 *bl += sprintf(b + *bl, "OFFLINE");
945 break;
946 default:
947 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
948 break;
949 }
950
Nicholas Bellinger65586d52011-11-30 01:25:21 -0800951 *bl += sprintf(b + *bl, " Execute/Max Queue Depth: %d/%d",
952 atomic_read(&dev->execute_tasks), dev->queue_depth);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800953 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000954 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 -0800955 *bl += sprintf(b + *bl, " ");
956}
957
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800958void transport_dump_vpd_proto_id(
959 struct t10_vpd *vpd,
960 unsigned char *p_buf,
961 int p_buf_len)
962{
963 unsigned char buf[VPD_TMP_BUF_SIZE];
964 int len;
965
966 memset(buf, 0, VPD_TMP_BUF_SIZE);
967 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
968
969 switch (vpd->protocol_identifier) {
970 case 0x00:
971 sprintf(buf+len, "Fibre Channel\n");
972 break;
973 case 0x10:
974 sprintf(buf+len, "Parallel SCSI\n");
975 break;
976 case 0x20:
977 sprintf(buf+len, "SSA\n");
978 break;
979 case 0x30:
980 sprintf(buf+len, "IEEE 1394\n");
981 break;
982 case 0x40:
983 sprintf(buf+len, "SCSI Remote Direct Memory Access"
984 " Protocol\n");
985 break;
986 case 0x50:
987 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
988 break;
989 case 0x60:
990 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
991 break;
992 case 0x70:
993 sprintf(buf+len, "Automation/Drive Interface Transport"
994 " Protocol\n");
995 break;
996 case 0x80:
997 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
998 break;
999 default:
1000 sprintf(buf+len, "Unknown 0x%02x\n",
1001 vpd->protocol_identifier);
1002 break;
1003 }
1004
1005 if (p_buf)
1006 strncpy(p_buf, buf, p_buf_len);
1007 else
Andy Grover6708bb22011-06-08 10:36:43 -07001008 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001009}
1010
1011void
1012transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1013{
1014 /*
1015 * Check if the Protocol Identifier Valid (PIV) bit is set..
1016 *
1017 * from spc3r23.pdf section 7.5.1
1018 */
1019 if (page_83[1] & 0x80) {
1020 vpd->protocol_identifier = (page_83[0] & 0xf0);
1021 vpd->protocol_identifier_set = 1;
1022 transport_dump_vpd_proto_id(vpd, NULL, 0);
1023 }
1024}
1025EXPORT_SYMBOL(transport_set_vpd_proto_id);
1026
1027int transport_dump_vpd_assoc(
1028 struct t10_vpd *vpd,
1029 unsigned char *p_buf,
1030 int p_buf_len)
1031{
1032 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001033 int ret = 0;
1034 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001035
1036 memset(buf, 0, VPD_TMP_BUF_SIZE);
1037 len = sprintf(buf, "T10 VPD Identifier Association: ");
1038
1039 switch (vpd->association) {
1040 case 0x00:
1041 sprintf(buf+len, "addressed logical unit\n");
1042 break;
1043 case 0x10:
1044 sprintf(buf+len, "target port\n");
1045 break;
1046 case 0x20:
1047 sprintf(buf+len, "SCSI target device\n");
1048 break;
1049 default:
1050 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
Andy Grovere3d6f902011-07-19 08:55:10 +00001051 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001052 break;
1053 }
1054
1055 if (p_buf)
1056 strncpy(p_buf, buf, p_buf_len);
1057 else
Andy Grover6708bb22011-06-08 10:36:43 -07001058 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001059
1060 return ret;
1061}
1062
1063int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1064{
1065 /*
1066 * The VPD identification association..
1067 *
1068 * from spc3r23.pdf Section 7.6.3.1 Table 297
1069 */
1070 vpd->association = (page_83[1] & 0x30);
1071 return transport_dump_vpd_assoc(vpd, NULL, 0);
1072}
1073EXPORT_SYMBOL(transport_set_vpd_assoc);
1074
1075int transport_dump_vpd_ident_type(
1076 struct t10_vpd *vpd,
1077 unsigned char *p_buf,
1078 int p_buf_len)
1079{
1080 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001081 int ret = 0;
1082 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001083
1084 memset(buf, 0, VPD_TMP_BUF_SIZE);
1085 len = sprintf(buf, "T10 VPD Identifier Type: ");
1086
1087 switch (vpd->device_identifier_type) {
1088 case 0x00:
1089 sprintf(buf+len, "Vendor specific\n");
1090 break;
1091 case 0x01:
1092 sprintf(buf+len, "T10 Vendor ID based\n");
1093 break;
1094 case 0x02:
1095 sprintf(buf+len, "EUI-64 based\n");
1096 break;
1097 case 0x03:
1098 sprintf(buf+len, "NAA\n");
1099 break;
1100 case 0x04:
1101 sprintf(buf+len, "Relative target port identifier\n");
1102 break;
1103 case 0x08:
1104 sprintf(buf+len, "SCSI name string\n");
1105 break;
1106 default:
1107 sprintf(buf+len, "Unsupported: 0x%02x\n",
1108 vpd->device_identifier_type);
Andy Grovere3d6f902011-07-19 08:55:10 +00001109 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001110 break;
1111 }
1112
Andy Grovere3d6f902011-07-19 08:55:10 +00001113 if (p_buf) {
1114 if (p_buf_len < strlen(buf)+1)
1115 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001116 strncpy(p_buf, buf, p_buf_len);
Andy Grovere3d6f902011-07-19 08:55:10 +00001117 } else {
Andy Grover6708bb22011-06-08 10:36:43 -07001118 pr_debug("%s", buf);
Andy Grovere3d6f902011-07-19 08:55:10 +00001119 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001120
1121 return ret;
1122}
1123
1124int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1125{
1126 /*
1127 * The VPD identifier type..
1128 *
1129 * from spc3r23.pdf Section 7.6.3.1 Table 298
1130 */
1131 vpd->device_identifier_type = (page_83[1] & 0x0f);
1132 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1133}
1134EXPORT_SYMBOL(transport_set_vpd_ident_type);
1135
1136int transport_dump_vpd_ident(
1137 struct t10_vpd *vpd,
1138 unsigned char *p_buf,
1139 int p_buf_len)
1140{
1141 unsigned char buf[VPD_TMP_BUF_SIZE];
1142 int ret = 0;
1143
1144 memset(buf, 0, VPD_TMP_BUF_SIZE);
1145
1146 switch (vpd->device_identifier_code_set) {
1147 case 0x01: /* Binary */
1148 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1149 &vpd->device_identifier[0]);
1150 break;
1151 case 0x02: /* ASCII */
1152 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1153 &vpd->device_identifier[0]);
1154 break;
1155 case 0x03: /* UTF-8 */
1156 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1157 &vpd->device_identifier[0]);
1158 break;
1159 default:
1160 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1161 " 0x%02x", vpd->device_identifier_code_set);
Andy Grovere3d6f902011-07-19 08:55:10 +00001162 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001163 break;
1164 }
1165
1166 if (p_buf)
1167 strncpy(p_buf, buf, p_buf_len);
1168 else
Andy Grover6708bb22011-06-08 10:36:43 -07001169 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001170
1171 return ret;
1172}
1173
1174int
1175transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1176{
1177 static const char hex_str[] = "0123456789abcdef";
1178 int j = 0, i = 4; /* offset to start of the identifer */
1179
1180 /*
1181 * The VPD Code Set (encoding)
1182 *
1183 * from spc3r23.pdf Section 7.6.3.1 Table 296
1184 */
1185 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1186 switch (vpd->device_identifier_code_set) {
1187 case 0x01: /* Binary */
1188 vpd->device_identifier[j++] =
1189 hex_str[vpd->device_identifier_type];
1190 while (i < (4 + page_83[3])) {
1191 vpd->device_identifier[j++] =
1192 hex_str[(page_83[i] & 0xf0) >> 4];
1193 vpd->device_identifier[j++] =
1194 hex_str[page_83[i] & 0x0f];
1195 i++;
1196 }
1197 break;
1198 case 0x02: /* ASCII */
1199 case 0x03: /* UTF-8 */
1200 while (i < (4 + page_83[3]))
1201 vpd->device_identifier[j++] = page_83[i++];
1202 break;
1203 default:
1204 break;
1205 }
1206
1207 return transport_dump_vpd_ident(vpd, NULL, 0);
1208}
1209EXPORT_SYMBOL(transport_set_vpd_ident);
1210
1211static void core_setup_task_attr_emulation(struct se_device *dev)
1212{
1213 /*
1214 * If this device is from Target_Core_Mod/pSCSI, disable the
1215 * SAM Task Attribute emulation.
1216 *
1217 * This is currently not available in upsream Linux/SCSI Target
1218 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1219 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001220 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001221 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1222 return;
1223 }
1224
1225 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
Andy Grover6708bb22011-06-08 10:36:43 -07001226 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001227 " device\n", dev->transport->name,
1228 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001229}
1230
1231static void scsi_dump_inquiry(struct se_device *dev)
1232{
Andy Grovere3d6f902011-07-19 08:55:10 +00001233 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001234 char buf[17];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001235 int i, device_type;
1236 /*
1237 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1238 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001239 for (i = 0; i < 8; i++)
1240 if (wwn->vendor[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001241 buf[i] = wwn->vendor[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001242 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001243 buf[i] = ' ';
1244 buf[i] = '\0';
1245 pr_debug(" Vendor: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001246
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001247 for (i = 0; i < 16; i++)
1248 if (wwn->model[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001249 buf[i] = wwn->model[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001250 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001251 buf[i] = ' ';
1252 buf[i] = '\0';
1253 pr_debug(" Model: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001254
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001255 for (i = 0; i < 4; i++)
1256 if (wwn->revision[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001257 buf[i] = wwn->revision[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001258 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001259 buf[i] = ' ';
1260 buf[i] = '\0';
1261 pr_debug(" Revision: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001262
Andy Grovere3d6f902011-07-19 08:55:10 +00001263 device_type = dev->transport->get_device_type(dev);
Andy Grover6708bb22011-06-08 10:36:43 -07001264 pr_debug(" Type: %s ", scsi_device_type(device_type));
1265 pr_debug(" ANSI SCSI revision: %02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001266 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001267}
1268
1269struct se_device *transport_add_device_to_core_hba(
1270 struct se_hba *hba,
1271 struct se_subsystem_api *transport,
1272 struct se_subsystem_dev *se_dev,
1273 u32 device_flags,
1274 void *transport_dev,
1275 struct se_dev_limits *dev_limits,
1276 const char *inquiry_prod,
1277 const char *inquiry_rev)
1278{
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001279 int force_pt;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001280 struct se_device *dev;
1281
1282 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001283 if (!dev) {
1284 pr_err("Unable to allocate memory for se_dev_t\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001285 return NULL;
1286 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001287
Andy Grovere3d6f902011-07-19 08:55:10 +00001288 transport_init_queue_obj(&dev->dev_queue_obj);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001289 dev->dev_flags = device_flags;
1290 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
Andy Grover5951146d2011-07-19 10:26:37 +00001291 dev->dev_ptr = transport_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001292 dev->se_hba = hba;
1293 dev->se_sub_dev = se_dev;
1294 dev->transport = transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001295 INIT_LIST_HEAD(&dev->dev_list);
1296 INIT_LIST_HEAD(&dev->dev_sep_list);
1297 INIT_LIST_HEAD(&dev->dev_tmr_list);
1298 INIT_LIST_HEAD(&dev->execute_task_list);
1299 INIT_LIST_HEAD(&dev->delayed_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001300 INIT_LIST_HEAD(&dev->state_task_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001301 INIT_LIST_HEAD(&dev->qf_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001302 spin_lock_init(&dev->execute_task_lock);
1303 spin_lock_init(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001304 spin_lock_init(&dev->dev_reservation_lock);
1305 spin_lock_init(&dev->dev_status_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001306 spin_lock_init(&dev->se_port_lock);
1307 spin_lock_init(&dev->se_tmr_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001308 spin_lock_init(&dev->qf_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001309 atomic_set(&dev->dev_ordered_id, 0);
1310
1311 se_dev_set_default_attribs(dev, dev_limits);
1312
1313 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1314 dev->creation_time = get_jiffies_64();
1315 spin_lock_init(&dev->stats_lock);
1316
1317 spin_lock(&hba->device_lock);
1318 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1319 hba->dev_count++;
1320 spin_unlock(&hba->device_lock);
1321 /*
1322 * Setup the SAM Task Attribute emulation for struct se_device
1323 */
1324 core_setup_task_attr_emulation(dev);
1325 /*
1326 * Force PR and ALUA passthrough emulation with internal object use.
1327 */
1328 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1329 /*
1330 * Setup the Reservations infrastructure for struct se_device
1331 */
1332 core_setup_reservations(dev, force_pt);
1333 /*
1334 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1335 */
1336 if (core_setup_alua(dev, force_pt) < 0)
1337 goto out;
1338
1339 /*
1340 * Startup the struct se_device processing thread
1341 */
1342 dev->process_thread = kthread_run(transport_processing_thread, dev,
Andy Grovere3d6f902011-07-19 08:55:10 +00001343 "LIO_%s", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001344 if (IS_ERR(dev->process_thread)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001345 pr_err("Unable to create kthread: LIO_%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001346 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001347 goto out;
1348 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001349 /*
1350 * Setup work_queue for QUEUE_FULL
1351 */
1352 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001353 /*
1354 * Preload the initial INQUIRY const values if we are doing
1355 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1356 * passthrough because this is being provided by the backend LLD.
1357 * This is required so that transport_get_inquiry() copies these
1358 * originals once back into DEV_T10_WWN(dev) for the virtual device
1359 * setup.
1360 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001361 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
Roland Dreierf22c1192011-05-02 22:15:37 -07001362 if (!inquiry_prod || !inquiry_rev) {
Andy Grover6708bb22011-06-08 10:36:43 -07001363 pr_err("All non TCM/pSCSI plugins require"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001364 " INQUIRY consts\n");
1365 goto out;
1366 }
1367
Andy Grovere3d6f902011-07-19 08:55:10 +00001368 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1369 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1370 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001371 }
1372 scsi_dump_inquiry(dev);
1373
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001374 return dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001375out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001376 kthread_stop(dev->process_thread);
1377
1378 spin_lock(&hba->device_lock);
1379 list_del(&dev->dev_list);
1380 hba->dev_count--;
1381 spin_unlock(&hba->device_lock);
1382
1383 se_release_vpd_for_dev(dev);
1384
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001385 kfree(dev);
1386
1387 return NULL;
1388}
1389EXPORT_SYMBOL(transport_add_device_to_core_hba);
1390
1391/* transport_generic_prepare_cdb():
1392 *
1393 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1394 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1395 * The point of this is since we are mapping iSCSI LUNs to
1396 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1397 * devices and HBAs for a loop.
1398 */
1399static inline void transport_generic_prepare_cdb(
1400 unsigned char *cdb)
1401{
1402 switch (cdb[0]) {
1403 case READ_10: /* SBC - RDProtect */
1404 case READ_12: /* SBC - RDProtect */
1405 case READ_16: /* SBC - RDProtect */
1406 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1407 case VERIFY: /* SBC - VRProtect */
1408 case VERIFY_16: /* SBC - VRProtect */
1409 case WRITE_VERIFY: /* SBC - VRProtect */
1410 case WRITE_VERIFY_12: /* SBC - VRProtect */
1411 break;
1412 default:
1413 cdb[1] &= 0x1f; /* clear logical unit number */
1414 break;
1415 }
1416}
1417
1418static struct se_task *
1419transport_generic_get_task(struct se_cmd *cmd,
1420 enum dma_data_direction data_direction)
1421{
1422 struct se_task *task;
Andy Grover5951146d2011-07-19 10:26:37 +00001423 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001424
Andy Grover6708bb22011-06-08 10:36:43 -07001425 task = dev->transport->alloc_task(cmd->t_task_cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001426 if (!task) {
Andy Grover6708bb22011-06-08 10:36:43 -07001427 pr_err("Unable to allocate struct se_task\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001428 return NULL;
1429 }
1430
1431 INIT_LIST_HEAD(&task->t_list);
1432 INIT_LIST_HEAD(&task->t_execute_list);
1433 INIT_LIST_HEAD(&task->t_state_list);
1434 init_completion(&task->task_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001435 task->task_se_cmd = cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001436 task->task_data_direction = data_direction;
1437
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001438 return task;
1439}
1440
1441static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1442
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001443/*
1444 * Used by fabric modules containing a local struct se_cmd within their
1445 * fabric dependent per I/O descriptor.
1446 */
1447void transport_init_se_cmd(
1448 struct se_cmd *cmd,
1449 struct target_core_fabric_ops *tfo,
1450 struct se_session *se_sess,
1451 u32 data_length,
1452 int data_direction,
1453 int task_attr,
1454 unsigned char *sense_buffer)
1455{
Andy Grover5951146d2011-07-19 10:26:37 +00001456 INIT_LIST_HEAD(&cmd->se_lun_node);
1457 INIT_LIST_HEAD(&cmd->se_delayed_node);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001458 INIT_LIST_HEAD(&cmd->se_qf_node);
Roland Dreier79a7fef2011-09-28 22:12:07 -07001459 INIT_LIST_HEAD(&cmd->se_queue_node);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001460 INIT_LIST_HEAD(&cmd->se_cmd_list);
Andy Grovera1d8b492011-05-02 17:12:10 -07001461 INIT_LIST_HEAD(&cmd->t_task_list);
1462 init_completion(&cmd->transport_lun_fe_stop_comp);
1463 init_completion(&cmd->transport_lun_stop_comp);
1464 init_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001465 init_completion(&cmd->cmd_wait_comp);
Andy Grovera1d8b492011-05-02 17:12:10 -07001466 spin_lock_init(&cmd->t_state_lock);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001467 cmd->transport_state = CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001468
1469 cmd->se_tfo = tfo;
1470 cmd->se_sess = se_sess;
1471 cmd->data_length = data_length;
1472 cmd->data_direction = data_direction;
1473 cmd->sam_task_attr = task_attr;
1474 cmd->sense_buffer = sense_buffer;
1475}
1476EXPORT_SYMBOL(transport_init_se_cmd);
1477
1478static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1479{
1480 /*
1481 * Check if SAM Task Attribute emulation is enabled for this
1482 * struct se_device storage object
1483 */
Andy Grover5951146d2011-07-19 10:26:37 +00001484 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001485 return 0;
1486
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001487 if (cmd->sam_task_attr == MSG_ACA_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001488 pr_debug("SAM Task Attribute ACA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001489 " emulation is not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001490 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001491 }
1492 /*
1493 * Used to determine when ORDERED commands should go from
1494 * Dormant to Active status.
1495 */
Andy Grover5951146d2011-07-19 10:26:37 +00001496 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001497 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001498 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001499 cmd->se_ordered_id, cmd->sam_task_attr,
Andy Grover6708bb22011-06-08 10:36:43 -07001500 cmd->se_dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001501 return 0;
1502}
1503
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001504/* transport_generic_allocate_tasks():
1505 *
1506 * Called from fabric RX Thread.
1507 */
1508int transport_generic_allocate_tasks(
1509 struct se_cmd *cmd,
1510 unsigned char *cdb)
1511{
1512 int ret;
1513
1514 transport_generic_prepare_cdb(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001515 /*
1516 * Ensure that the received CDB is less than the max (252 + 8) bytes
1517 * for VARIABLE_LENGTH_CMD
1518 */
1519 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001520 pr_err("Received SCSI CDB with command_size: %d that"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001521 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1522 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001523 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1524 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grovere3d6f902011-07-19 08:55:10 +00001525 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001526 }
1527 /*
1528 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1529 * allocate the additional extended CDB buffer now.. Otherwise
1530 * setup the pointer from __t_task_cdb to t_task_cdb.
1531 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001532 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1533 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001534 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001535 if (!cmd->t_task_cdb) {
1536 pr_err("Unable to allocate cmd->t_task_cdb"
Andy Grovera1d8b492011-05-02 17:12:10 -07001537 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001538 scsi_command_size(cdb),
Andy Grovera1d8b492011-05-02 17:12:10 -07001539 (unsigned long)sizeof(cmd->__t_task_cdb));
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001540 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1541 cmd->scsi_sense_reason =
1542 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grovere3d6f902011-07-19 08:55:10 +00001543 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001544 }
1545 } else
Andy Grovera1d8b492011-05-02 17:12:10 -07001546 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001547 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07001548 * Copy the original CDB into cmd->
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001549 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001550 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001551 /*
1552 * Setup the received CDB based on SCSI defined opcodes and
1553 * perform unit attention, persistent reservations and ALUA
Andy Grovera1d8b492011-05-02 17:12:10 -07001554 * checks for virtual device backends. The cmd->t_task_cdb
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001555 * pointer is expected to be setup before we reach this point.
1556 */
1557 ret = transport_generic_cmd_sequencer(cmd, cdb);
1558 if (ret < 0)
1559 return ret;
1560 /*
1561 * Check for SAM Task Attribute Emulation
1562 */
1563 if (transport_check_alloc_task_attr(cmd) < 0) {
1564 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1565 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00001566 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001567 }
1568 spin_lock(&cmd->se_lun->lun_sep_lock);
1569 if (cmd->se_lun->lun_sep)
1570 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1571 spin_unlock(&cmd->se_lun->lun_sep_lock);
1572 return 0;
1573}
1574EXPORT_SYMBOL(transport_generic_allocate_tasks);
1575
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001576/*
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001577 * Used by fabric module frontends to queue tasks directly.
1578 * Many only be used from process context only
1579 */
1580int transport_handle_cdb_direct(
1581 struct se_cmd *cmd)
1582{
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001583 int ret;
1584
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001585 if (!cmd->se_lun) {
1586 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001587 pr_err("cmd->se_lun is NULL\n");
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001588 return -EINVAL;
1589 }
1590 if (in_interrupt()) {
1591 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001592 pr_err("transport_generic_handle_cdb cannot be called"
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001593 " from interrupt context\n");
1594 return -EINVAL;
1595 }
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001596 /*
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001597 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001598 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1599 * in existing usage to ensure that outstanding descriptors are handled
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07001600 * correctly during shutdown via transport_wait_for_tasks()
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001601 *
1602 * Also, we don't take cmd->t_state_lock here as we only expect
1603 * this to be called for initial descriptor submission.
1604 */
1605 cmd->t_state = TRANSPORT_NEW_CMD;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001606 cmd->transport_state |= CMD_T_ACTIVE;
1607
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001608 /*
1609 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1610 * so follow TRANSPORT_NEW_CMD processing thread context usage
1611 * and call transport_generic_request_failure() if necessary..
1612 */
1613 ret = transport_generic_new_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001614 if (ret < 0)
1615 transport_generic_request_failure(cmd);
1616
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001617 return 0;
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001618}
1619EXPORT_SYMBOL(transport_handle_cdb_direct);
1620
Nicholas Bellingera6360782011-11-18 20:36:22 -08001621/**
1622 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1623 *
1624 * @se_cmd: command descriptor to submit
1625 * @se_sess: associated se_sess for endpoint
1626 * @cdb: pointer to SCSI CDB
1627 * @sense: pointer to SCSI sense buffer
1628 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1629 * @data_length: fabric expected data transfer length
1630 * @task_addr: SAM task attribute
1631 * @data_dir: DMA data direction
1632 * @flags: flags for command submission from target_sc_flags_tables
1633 *
1634 * This may only be called from process context, and also currently
1635 * assumes internal allocation of fabric payload buffer by target-core.
1636 **/
Andy Grover1edcdb42012-01-19 13:39:23 -08001637void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
Nicholas Bellingera6360782011-11-18 20:36:22 -08001638 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1639 u32 data_length, int task_attr, int data_dir, int flags)
1640{
1641 struct se_portal_group *se_tpg;
1642 int rc;
1643
1644 se_tpg = se_sess->se_tpg;
1645 BUG_ON(!se_tpg);
1646 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1647 BUG_ON(in_interrupt());
1648 /*
1649 * Initialize se_cmd for target operation. From this point
1650 * exceptions are handled by sending exception status via
1651 * target_core_fabric_ops->queue_status() callback
1652 */
1653 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1654 data_length, data_dir, task_attr, sense);
1655 /*
1656 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1657 * se_sess->sess_cmd_list. A second kref_get here is necessary
1658 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1659 * kref_put() to happen during fabric packet acknowledgement.
1660 */
1661 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1662 /*
1663 * Signal bidirectional data payloads to target-core
1664 */
1665 if (flags & TARGET_SCF_BIDI_OP)
1666 se_cmd->se_cmd_flags |= SCF_BIDI;
1667 /*
1668 * Locate se_lun pointer and attach it to struct se_cmd
1669 */
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001670 if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1671 transport_send_check_condition_and_sense(se_cmd,
1672 se_cmd->scsi_sense_reason, 0);
1673 target_put_sess_cmd(se_sess, se_cmd);
1674 return;
1675 }
Nicholas Bellingera6360782011-11-18 20:36:22 -08001676 /*
1677 * Sanitize CDBs via transport_generic_cmd_sequencer() and
1678 * allocate the necessary tasks to complete the received CDB+data
1679 */
1680 rc = transport_generic_allocate_tasks(se_cmd, cdb);
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001681 if (rc != 0) {
1682 transport_generic_request_failure(se_cmd);
1683 return;
1684 }
Nicholas Bellingera6360782011-11-18 20:36:22 -08001685 /*
1686 * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
1687 * for immediate execution of READs, otherwise wait for
1688 * transport_generic_handle_data() to be called for WRITEs
1689 * when fabric has filled the incoming buffer.
1690 */
1691 transport_handle_cdb_direct(se_cmd);
Andy Grover1edcdb42012-01-19 13:39:23 -08001692 return;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001693}
1694EXPORT_SYMBOL(target_submit_cmd);
1695
Andy Groverea98d7f2012-01-19 13:39:21 -08001696/**
1697 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1698 * for TMR CDBs
1699 *
1700 * @se_cmd: command descriptor to submit
1701 * @se_sess: associated se_sess for endpoint
1702 * @sense: pointer to SCSI sense buffer
1703 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1704 * @fabric_context: fabric context for TMR req
1705 * @tm_type: Type of TM request
1706 *
1707 * Callable from all contexts.
1708 **/
1709
1710void target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
1711 unsigned char *sense, u32 unpacked_lun,
1712 void *fabric_tmr_ptr, unsigned char tm_type, int flags)
1713{
1714 struct se_portal_group *se_tpg;
1715 int ret;
1716
1717 se_tpg = se_sess->se_tpg;
1718 BUG_ON(!se_tpg);
1719
1720 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1721 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
1722
1723 /* See target_submit_cmd for commentary */
1724 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1725
1726 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, GFP_KERNEL);
1727 if (ret < 0) {
1728 dump_stack();
1729 /* FIXME XXX */
1730 return;
1731 }
1732
1733 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1734 if (ret) {
1735 transport_send_check_condition_and_sense(se_cmd,
1736 se_cmd->scsi_sense_reason, 0);
1737 transport_generic_free_cmd(se_cmd, 0);
1738 return;
1739 }
1740 transport_generic_handle_tmr(se_cmd);
1741}
1742EXPORT_SYMBOL(target_submit_tmr);
1743
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001744/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001745 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1746 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1747 * complete setup in TCM process context w/ TFO->new_cmd_map().
1748 */
1749int transport_generic_handle_cdb_map(
1750 struct se_cmd *cmd)
1751{
Andy Grovere3d6f902011-07-19 08:55:10 +00001752 if (!cmd->se_lun) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001753 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001754 pr_err("cmd->se_lun is NULL\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001755 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001756 }
1757
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001758 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001759 return 0;
1760}
1761EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1762
1763/* transport_generic_handle_data():
1764 *
1765 *
1766 */
1767int transport_generic_handle_data(
1768 struct se_cmd *cmd)
1769{
1770 /*
1771 * For the software fabric case, then we assume the nexus is being
1772 * failed/shutdown when signals are pending from the kthread context
1773 * caller, so we return a failure. For the HW target mode case running
1774 * in interrupt code, the signal_pending() check is skipped.
1775 */
1776 if (!in_interrupt() && signal_pending(current))
Andy Grovere3d6f902011-07-19 08:55:10 +00001777 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001778 /*
1779 * If the received CDB has aleady been ABORTED by the generic
1780 * target engine, we now call transport_check_aborted_status()
1781 * to queue any delated TASK_ABORTED status for the received CDB to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001782 * fabric module as we are expecting no further incoming DATA OUT
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001783 * sequences at this point.
1784 */
1785 if (transport_check_aborted_status(cmd, 1) != 0)
1786 return 0;
1787
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001788 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001789 return 0;
1790}
1791EXPORT_SYMBOL(transport_generic_handle_data);
1792
1793/* transport_generic_handle_tmr():
1794 *
1795 *
1796 */
1797int transport_generic_handle_tmr(
1798 struct se_cmd *cmd)
1799{
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001800 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001801 return 0;
1802}
1803EXPORT_SYMBOL(transport_generic_handle_tmr);
1804
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001805/*
1806 * If the task is active, request it to be stopped and sleep until it
1807 * has completed.
1808 */
1809bool target_stop_task(struct se_task *task, unsigned long *flags)
1810{
1811 struct se_cmd *cmd = task->task_se_cmd;
1812 bool was_active = false;
1813
1814 if (task->task_flags & TF_ACTIVE) {
1815 task->task_flags |= TF_REQUEST_STOP;
1816 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1817
1818 pr_debug("Task %p waiting to complete\n", task);
1819 wait_for_completion(&task->task_stop_comp);
1820 pr_debug("Task %p stopped successfully\n", task);
1821
1822 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1823 atomic_dec(&cmd->t_task_cdbs_left);
1824 task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
1825 was_active = true;
1826 }
1827
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001828 return was_active;
1829}
1830
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001831static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1832{
1833 struct se_task *task, *task_tmp;
1834 unsigned long flags;
1835 int ret = 0;
1836
Andy Grover6708bb22011-06-08 10:36:43 -07001837 pr_debug("ITT[0x%08x] - Stopping tasks\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001838 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001839
1840 /*
1841 * No tasks remain in the execution queue
1842 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001843 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001844 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07001845 &cmd->t_task_list, t_list) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04001846 pr_debug("Processing task %p\n", task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001847 /*
1848 * If the struct se_task has not been sent and is not active,
1849 * remove the struct se_task from the execution queue.
1850 */
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04001851 if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
Andy Grovera1d8b492011-05-02 17:12:10 -07001852 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001853 flags);
1854 transport_remove_task_from_execute_queue(task,
Christoph Hellwig42bf8292011-10-12 11:07:00 -04001855 cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001856
Christoph Hellwig04629b72011-10-12 11:07:04 -04001857 pr_debug("Task %p removed from execute queue\n", task);
Andy Grovera1d8b492011-05-02 17:12:10 -07001858 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001859 continue;
1860 }
1861
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001862 if (!target_stop_task(task, &flags)) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04001863 pr_debug("Task %p - did nothing\n", task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001864 ret++;
1865 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001866 }
Andy Grovera1d8b492011-05-02 17:12:10 -07001867 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001868
1869 return ret;
1870}
1871
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001872/*
1873 * Handle SAM-esque emulation for generic transport request failures.
1874 */
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001875void transport_generic_request_failure(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001876{
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001877 int ret = 0;
1878
Andy Grover6708bb22011-06-08 10:36:43 -07001879 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001880 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -07001881 cmd->t_task_cdb[0]);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001882 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001883 cmd->se_tfo->get_cmd_state(cmd),
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001884 cmd->t_state, cmd->scsi_sense_reason);
Andy Grover6708bb22011-06-08 10:36:43 -07001885 pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001886 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001887 " CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
1888 cmd->t_task_list_num,
Andy Grovera1d8b492011-05-02 17:12:10 -07001889 atomic_read(&cmd->t_task_cdbs_left),
1890 atomic_read(&cmd->t_task_cdbs_sent),
1891 atomic_read(&cmd->t_task_cdbs_ex_left),
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001892 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1893 (cmd->transport_state & CMD_T_STOP) != 0,
1894 (cmd->transport_state & CMD_T_SENT) != 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001895
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001896 /*
1897 * For SAM Task Attribute emulation for failed struct se_cmd
1898 */
1899 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1900 transport_complete_task_attr(cmd);
1901
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001902 switch (cmd->scsi_sense_reason) {
1903 case TCM_NON_EXISTENT_LUN:
1904 case TCM_UNSUPPORTED_SCSI_OPCODE:
1905 case TCM_INVALID_CDB_FIELD:
1906 case TCM_INVALID_PARAMETER_LIST:
1907 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1908 case TCM_UNKNOWN_MODE_PAGE:
1909 case TCM_WRITE_PROTECTED:
1910 case TCM_CHECK_CONDITION_ABORT_CMD:
1911 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1912 case TCM_CHECK_CONDITION_NOT_READY:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001913 break;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001914 case TCM_RESERVATION_CONFLICT:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001915 /*
1916 * No SENSE Data payload for this case, set SCSI Status
1917 * and queue the response to $FABRIC_MOD.
1918 *
1919 * Uses linux/include/scsi/scsi.h SAM status codes defs
1920 */
1921 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1922 /*
1923 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1924 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1925 * CONFLICT STATUS.
1926 *
1927 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1928 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001929 if (cmd->se_sess &&
1930 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1931 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001932 cmd->orig_fe_lun, 0x2C,
1933 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1934
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001935 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001936 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001937 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001938 goto check_stop;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001939 default:
Andy Grover6708bb22011-06-08 10:36:43 -07001940 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001941 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001942 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1943 break;
1944 }
Nicholas Bellinger16ab8e62011-08-08 19:03:38 -07001945 /*
1946 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1947 * make the call to transport_send_check_condition_and_sense()
1948 * directly. Otherwise expect the fabric to make the call to
1949 * transport_send_check_condition_and_sense() after handling
1950 * possible unsoliticied write data payloads.
1951 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001952 ret = transport_send_check_condition_and_sense(cmd,
1953 cmd->scsi_sense_reason, 0);
1954 if (ret == -EAGAIN || ret == -ENOMEM)
1955 goto queue_full;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001956
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001957check_stop:
1958 transport_lun_remove_cmd(cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07001959 if (!transport_cmd_check_stop_to_fabric(cmd))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001960 ;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001961 return;
1962
1963queue_full:
Christoph Hellwige057f532011-10-17 13:56:41 -04001964 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1965 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001966}
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001967EXPORT_SYMBOL(transport_generic_request_failure);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001968
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001969static inline u32 transport_lba_21(unsigned char *cdb)
1970{
1971 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1972}
1973
1974static inline u32 transport_lba_32(unsigned char *cdb)
1975{
1976 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1977}
1978
1979static inline unsigned long long transport_lba_64(unsigned char *cdb)
1980{
1981 unsigned int __v1, __v2;
1982
1983 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1984 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1985
1986 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1987}
1988
1989/*
1990 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
1991 */
1992static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
1993{
1994 unsigned int __v1, __v2;
1995
1996 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
1997 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
1998
1999 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2000}
2001
2002static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
2003{
2004 unsigned long flags;
2005
Andy Grovera1d8b492011-05-02 17:12:10 -07002006 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002007 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
Andy Grovera1d8b492011-05-02 17:12:10 -07002008 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002009}
2010
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002011/*
2012 * Called from Fabric Module context from transport_execute_tasks()
2013 *
2014 * The return of this function determins if the tasks from struct se_cmd
2015 * get added to the execution queue in transport_execute_tasks(),
2016 * or are added to the delayed or ordered lists here.
2017 */
2018static inline int transport_execute_task_attr(struct se_cmd *cmd)
2019{
Andy Grover5951146d2011-07-19 10:26:37 +00002020 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002021 return 1;
2022 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002023 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002024 * to allow the passed struct se_cmd list of tasks to the front of the list.
2025 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002026 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07002027 pr_debug("Added HEAD_OF_QUEUE for CDB:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002028 " 0x%02x, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07002029 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002030 cmd->se_ordered_id);
2031 return 1;
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002032 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Andy Grover5951146d2011-07-19 10:26:37 +00002033 atomic_inc(&cmd->se_dev->dev_ordered_sync);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002034 smp_mb__after_atomic_inc();
2035
Andy Grover6708bb22011-06-08 10:36:43 -07002036 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002037 " list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07002038 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002039 cmd->se_ordered_id);
2040 /*
2041 * Add ORDERED command to tail of execution queue if
2042 * no other older commands exist that need to be
2043 * completed first.
2044 */
Andy Grover6708bb22011-06-08 10:36:43 -07002045 if (!atomic_read(&cmd->se_dev->simple_cmds))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002046 return 1;
2047 } else {
2048 /*
2049 * For SIMPLE and UNTAGGED Task Attribute commands
2050 */
Andy Grover5951146d2011-07-19 10:26:37 +00002051 atomic_inc(&cmd->se_dev->simple_cmds);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002052 smp_mb__after_atomic_inc();
2053 }
2054 /*
2055 * Otherwise if one or more outstanding ORDERED task attribute exist,
2056 * add the dormant task(s) built for the passed struct se_cmd to the
2057 * execution queue and become in Active state for this struct se_device.
2058 */
Andy Grover5951146d2011-07-19 10:26:37 +00002059 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002060 /*
2061 * Otherwise, add cmd w/ tasks to delayed cmd queue that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002062 * will be drained upon completion of HEAD_OF_QUEUE task.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002063 */
Andy Grover5951146d2011-07-19 10:26:37 +00002064 spin_lock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002065 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
Andy Grover5951146d2011-07-19 10:26:37 +00002066 list_add_tail(&cmd->se_delayed_node,
2067 &cmd->se_dev->delayed_cmd_list);
2068 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002069
Andy Grover6708bb22011-06-08 10:36:43 -07002070 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002071 " delayed CMD list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07002072 cmd->t_task_cdb[0], cmd->sam_task_attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002073 cmd->se_ordered_id);
2074 /*
2075 * Return zero to let transport_execute_tasks() know
2076 * not to add the delayed tasks to the execution list.
2077 */
2078 return 0;
2079 }
2080 /*
2081 * Otherwise, no ORDERED task attributes exist..
2082 */
2083 return 1;
2084}
2085
2086/*
2087 * Called from fabric module context in transport_generic_new_cmd() and
2088 * transport_generic_process_write()
2089 */
2090static int transport_execute_tasks(struct se_cmd *cmd)
2091{
2092 int add_tasks;
Nicholas Bellinger40be67f2011-11-30 00:41:20 -08002093 struct se_device *se_dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002094 /*
2095 * Call transport_cmd_check_stop() to see if a fabric exception
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002096 * has occurred that prevents execution.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002097 */
Andy Grover6708bb22011-06-08 10:36:43 -07002098 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002099 /*
2100 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2101 * attribute for the tasks of the received struct se_cmd CDB
2102 */
2103 add_tasks = transport_execute_task_attr(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00002104 if (!add_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002105 goto execute_tasks;
2106 /*
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002107 * __transport_execute_tasks() -> __transport_add_tasks_from_cmd()
2108 * adds associated se_tasks while holding dev->execute_task_lock
2109 * before I/O dispath to avoid a double spinlock access.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002110 */
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002111 __transport_execute_tasks(se_dev, cmd);
2112 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002113 }
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002114
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002115execute_tasks:
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002116 __transport_execute_tasks(se_dev, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002117 return 0;
2118}
2119
2120/*
2121 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2122 * from struct se_device->execute_task_list and
2123 *
2124 * Called from transport_processing_thread()
2125 */
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002126static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002127{
2128 int error;
2129 struct se_cmd *cmd = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00002130 struct se_task *task = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002131 unsigned long flags;
2132
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002133check_depth:
Andy Grovere3d6f902011-07-19 08:55:10 +00002134 spin_lock_irq(&dev->execute_task_lock);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002135 if (new_cmd != NULL)
2136 __transport_add_tasks_from_cmd(new_cmd);
2137
Andy Grovere3d6f902011-07-19 08:55:10 +00002138 if (list_empty(&dev->execute_task_list)) {
2139 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002140 return 0;
2141 }
Andy Grovere3d6f902011-07-19 08:55:10 +00002142 task = list_first_entry(&dev->execute_task_list,
2143 struct se_task, t_execute_list);
Christoph Hellwig04629b72011-10-12 11:07:04 -04002144 __transport_remove_task_from_execute_queue(task, dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00002145 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002146
Andy Grovere3d6f902011-07-19 08:55:10 +00002147 cmd = task->task_se_cmd;
Andy Grovera1d8b492011-05-02 17:12:10 -07002148 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04002149 task->task_flags |= (TF_ACTIVE | TF_SENT);
Andy Grovera1d8b492011-05-02 17:12:10 -07002150 atomic_inc(&cmd->t_task_cdbs_sent);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002151
Andy Grovera1d8b492011-05-02 17:12:10 -07002152 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2153 cmd->t_task_list_num)
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002154 cmd->transport_state |= CMD_T_SENT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002155
Andy Grovera1d8b492011-05-02 17:12:10 -07002156 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002157
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002158 if (cmd->execute_task)
2159 error = cmd->execute_task(task);
2160 else
2161 error = dev->transport->do_task(task);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002162 if (error != 0) {
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002163 spin_lock_irqsave(&cmd->t_state_lock, flags);
2164 task->task_flags &= ~TF_ACTIVE;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002165 cmd->transport_state &= ~CMD_T_SENT;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002166 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002167
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002168 transport_stop_tasks_for_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002169 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002170 }
2171
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002172 new_cmd = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002173 goto check_depth;
2174
2175 return 0;
2176}
2177
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002178static inline u32 transport_get_sectors_6(
2179 unsigned char *cdb,
2180 struct se_cmd *cmd,
2181 int *ret)
2182{
Andy Grover5951146d2011-07-19 10:26:37 +00002183 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002184
2185 /*
2186 * Assume TYPE_DISK for non struct se_device objects.
2187 * Use 8-bit sector value.
2188 */
2189 if (!dev)
2190 goto type_disk;
2191
2192 /*
2193 * Use 24-bit allocation length for TYPE_TAPE.
2194 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002195 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002196 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2197
2198 /*
2199 * Everything else assume TYPE_DISK Sector CDB location.
Roland Dreier9b5cd7f2011-11-22 13:51:33 -08002200 * Use 8-bit sector value. SBC-3 says:
2201 *
2202 * A TRANSFER LENGTH field set to zero specifies that 256
2203 * logical blocks shall be written. Any other value
2204 * specifies the number of logical blocks that shall be
2205 * written.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002206 */
2207type_disk:
Roland Dreier9b5cd7f2011-11-22 13:51:33 -08002208 return cdb[4] ? : 256;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002209}
2210
2211static inline u32 transport_get_sectors_10(
2212 unsigned char *cdb,
2213 struct se_cmd *cmd,
2214 int *ret)
2215{
Andy Grover5951146d2011-07-19 10:26:37 +00002216 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002217
2218 /*
2219 * Assume TYPE_DISK for non struct se_device objects.
2220 * Use 16-bit sector value.
2221 */
2222 if (!dev)
2223 goto type_disk;
2224
2225 /*
2226 * XXX_10 is not defined in SSC, throw an exception
2227 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002228 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2229 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002230 return 0;
2231 }
2232
2233 /*
2234 * Everything else assume TYPE_DISK Sector CDB location.
2235 * Use 16-bit sector value.
2236 */
2237type_disk:
2238 return (u32)(cdb[7] << 8) + cdb[8];
2239}
2240
2241static inline u32 transport_get_sectors_12(
2242 unsigned char *cdb,
2243 struct se_cmd *cmd,
2244 int *ret)
2245{
Andy Grover5951146d2011-07-19 10:26:37 +00002246 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002247
2248 /*
2249 * Assume TYPE_DISK for non struct se_device objects.
2250 * Use 32-bit sector value.
2251 */
2252 if (!dev)
2253 goto type_disk;
2254
2255 /*
2256 * XXX_12 is not defined in SSC, throw an exception
2257 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002258 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2259 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002260 return 0;
2261 }
2262
2263 /*
2264 * Everything else assume TYPE_DISK Sector CDB location.
2265 * Use 32-bit sector value.
2266 */
2267type_disk:
2268 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2269}
2270
2271static inline u32 transport_get_sectors_16(
2272 unsigned char *cdb,
2273 struct se_cmd *cmd,
2274 int *ret)
2275{
Andy Grover5951146d2011-07-19 10:26:37 +00002276 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002277
2278 /*
2279 * Assume TYPE_DISK for non struct se_device objects.
2280 * Use 32-bit sector value.
2281 */
2282 if (!dev)
2283 goto type_disk;
2284
2285 /*
2286 * Use 24-bit allocation length for TYPE_TAPE.
2287 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002288 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002289 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2290
2291type_disk:
2292 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2293 (cdb[12] << 8) + cdb[13];
2294}
2295
2296/*
2297 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2298 */
2299static inline u32 transport_get_sectors_32(
2300 unsigned char *cdb,
2301 struct se_cmd *cmd,
2302 int *ret)
2303{
2304 /*
2305 * Assume TYPE_DISK for non struct se_device objects.
2306 * Use 32-bit sector value.
2307 */
2308 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2309 (cdb[30] << 8) + cdb[31];
2310
2311}
2312
2313static inline u32 transport_get_size(
2314 u32 sectors,
2315 unsigned char *cdb,
2316 struct se_cmd *cmd)
2317{
Andy Grover5951146d2011-07-19 10:26:37 +00002318 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002319
Andy Grovere3d6f902011-07-19 08:55:10 +00002320 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002321 if (cdb[1] & 1) { /* sectors */
Andy Grovere3d6f902011-07-19 08:55:10 +00002322 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002323 } else /* bytes */
2324 return sectors;
2325 }
2326#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07002327 pr_debug("Returning block_size: %u, sectors: %u == %u for"
Andy Grovere3d6f902011-07-19 08:55:10 +00002328 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2329 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2330 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002331#endif
Andy Grovere3d6f902011-07-19 08:55:10 +00002332 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002333}
2334
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002335static void transport_xor_callback(struct se_cmd *cmd)
2336{
2337 unsigned char *buf, *addr;
Andy Groverec98f782011-07-20 19:28:46 +00002338 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002339 unsigned int offset;
2340 int i;
Andy Groverec98f782011-07-20 19:28:46 +00002341 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002342 /*
2343 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2344 *
2345 * 1) read the specified logical block(s);
2346 * 2) transfer logical blocks from the data-out buffer;
2347 * 3) XOR the logical blocks transferred from the data-out buffer with
2348 * the logical blocks read, storing the resulting XOR data in a buffer;
2349 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2350 * blocks transferred from the data-out buffer; and
2351 * 5) transfer the resulting XOR data to the data-in buffer.
2352 */
2353 buf = kmalloc(cmd->data_length, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002354 if (!buf) {
2355 pr_err("Unable to allocate xor_callback buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002356 return;
2357 }
2358 /*
Andy Groverec98f782011-07-20 19:28:46 +00002359 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002360 * into the locally allocated *buf
2361 */
Andy Groverec98f782011-07-20 19:28:46 +00002362 sg_copy_to_buffer(cmd->t_data_sg,
2363 cmd->t_data_nents,
2364 buf,
2365 cmd->data_length);
2366
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002367 /*
2368 * Now perform the XOR against the BIDI read memory located at
Andy Grovera1d8b492011-05-02 17:12:10 -07002369 * cmd->t_mem_bidi_list
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002370 */
2371
2372 offset = 0;
Andy Groverec98f782011-07-20 19:28:46 +00002373 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2374 addr = kmap_atomic(sg_page(sg), KM_USER0);
2375 if (!addr)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002376 goto out;
2377
Andy Groverec98f782011-07-20 19:28:46 +00002378 for (i = 0; i < sg->length; i++)
2379 *(addr + sg->offset + i) ^= *(buf + offset + i);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002380
Andy Groverec98f782011-07-20 19:28:46 +00002381 offset += sg->length;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002382 kunmap_atomic(addr, KM_USER0);
2383 }
Andy Groverec98f782011-07-20 19:28:46 +00002384
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002385out:
2386 kfree(buf);
2387}
2388
2389/*
2390 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2391 */
2392static int transport_get_sense_data(struct se_cmd *cmd)
2393{
2394 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002395 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002396 struct se_task *task = NULL, *task_tmp;
2397 unsigned long flags;
2398 u32 offset = 0;
2399
Andy Grovere3d6f902011-07-19 08:55:10 +00002400 WARN_ON(!cmd->se_lun);
2401
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002402 if (!dev)
2403 return 0;
2404
Andy Grovera1d8b492011-05-02 17:12:10 -07002405 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002406 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002407 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002408 return 0;
2409 }
2410
2411 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07002412 &cmd->t_task_list, t_list) {
Christoph Hellwigef804a82011-11-23 06:53:58 -05002413 if (!(task->task_flags & TF_HAS_SENSE))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002414 continue;
2415
Andy Grovere3d6f902011-07-19 08:55:10 +00002416 if (!dev->transport->get_sense_buffer) {
Andy Grover6708bb22011-06-08 10:36:43 -07002417 pr_err("dev->transport->get_sense_buffer"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002418 " is NULL\n");
2419 continue;
2420 }
2421
Andy Grovere3d6f902011-07-19 08:55:10 +00002422 sense_buffer = dev->transport->get_sense_buffer(task);
Andy Grover6708bb22011-06-08 10:36:43 -07002423 if (!sense_buffer) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04002424 pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002425 " sense buffer for task with sense\n",
Christoph Hellwig04629b72011-10-12 11:07:04 -04002426 cmd->se_tfo->get_task_tag(cmd), task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002427 continue;
2428 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002429 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002430
Andy Grovere3d6f902011-07-19 08:55:10 +00002431 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002432 TRANSPORT_SENSE_BUFFER);
2433
Andy Grover5951146d2011-07-19 10:26:37 +00002434 memcpy(&buffer[offset], sense_buffer,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002435 TRANSPORT_SENSE_BUFFER);
2436 cmd->scsi_status = task->task_scsi_status;
2437 /* Automatically padded */
2438 cmd->scsi_sense_length =
2439 (TRANSPORT_SENSE_BUFFER + offset);
2440
Andy Grover6708bb22011-06-08 10:36:43 -07002441 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002442 " and sense\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002443 dev->se_hba->hba_id, dev->transport->name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002444 cmd->scsi_status);
2445 return 0;
2446 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002447 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002448
2449 return -1;
2450}
2451
Andy Groverec98f782011-07-20 19:28:46 +00002452static inline long long transport_dev_end_lba(struct se_device *dev)
2453{
2454 return dev->transport->get_blocks(dev) + 1;
2455}
2456
2457static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2458{
2459 struct se_device *dev = cmd->se_dev;
2460 u32 sectors;
2461
2462 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2463 return 0;
2464
2465 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2466
Andy Grover6708bb22011-06-08 10:36:43 -07002467 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2468 pr_err("LBA: %llu Sectors: %u exceeds"
Andy Groverec98f782011-07-20 19:28:46 +00002469 " transport_dev_end_lba(): %llu\n",
2470 cmd->t_task_lba, sectors,
2471 transport_dev_end_lba(dev));
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002472 return -EINVAL;
Andy Groverec98f782011-07-20 19:28:46 +00002473 }
2474
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002475 return 0;
Andy Groverec98f782011-07-20 19:28:46 +00002476}
2477
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002478static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2479{
2480 /*
2481 * Determine if the received WRITE_SAME is used to for direct
2482 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2483 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2484 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2485 */
2486 int passthrough = (dev->transport->transport_type ==
2487 TRANSPORT_PLUGIN_PHBA_PDEV);
2488
2489 if (!passthrough) {
2490 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2491 pr_err("WRITE_SAME PBDATA and LBDATA"
2492 " bits not supported for Block Discard"
2493 " Emulation\n");
2494 return -ENOSYS;
2495 }
2496 /*
2497 * Currently for the emulated case we only accept
2498 * tpws with the UNMAP=1 bit set.
2499 */
2500 if (!(flags[0] & 0x08)) {
2501 pr_err("WRITE_SAME w/o UNMAP bit not"
2502 " supported for Block Discard Emulation\n");
2503 return -ENOSYS;
2504 }
2505 }
2506
2507 return 0;
2508}
2509
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002510/* transport_generic_cmd_sequencer():
2511 *
2512 * Generic Command Sequencer that should work for most DAS transport
2513 * drivers.
2514 *
2515 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2516 * RX Thread.
2517 *
2518 * FIXME: Need to support other SCSI OPCODES where as well.
2519 */
2520static int transport_generic_cmd_sequencer(
2521 struct se_cmd *cmd,
2522 unsigned char *cdb)
2523{
Andy Grover5951146d2011-07-19 10:26:37 +00002524 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002525 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2526 int ret = 0, sector_ret = 0, passthrough;
2527 u32 sectors = 0, size = 0, pr_reg_type = 0;
2528 u16 service_action;
2529 u8 alua_ascq = 0;
2530 /*
2531 * Check for an existing UNIT ATTENTION condition
2532 */
2533 if (core_scsi3_ua_check(cmd, cdb) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002534 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2535 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
Andy Grover5951146d2011-07-19 10:26:37 +00002536 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002537 }
2538 /*
2539 * Check status of Asymmetric Logical Unit Assignment port
2540 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002541 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002542 if (ret != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002543 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002544 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002545 * The ALUA additional sense code qualifier (ASCQ) is determined
2546 * by the ALUA primary or secondary access state..
2547 */
2548 if (ret > 0) {
2549#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07002550 pr_debug("[%s]: ALUA TG Port not available,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002551 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002552 cmd->se_tfo->get_fabric_name(), alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002553#endif
2554 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2555 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2556 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
Andy Grover5951146d2011-07-19 10:26:37 +00002557 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002558 }
2559 goto out_invalid_cdb_field;
2560 }
2561 /*
2562 * Check status for SPC-3 Persistent Reservations
2563 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002564 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2565 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002566 cmd, cdb, pr_reg_type) != 0) {
2567 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2568 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2569 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2570 return -EBUSY;
2571 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002572 /*
2573 * This means the CDB is allowed for the SCSI Initiator port
2574 * when said port is *NOT* holding the legacy SPC-2 or
2575 * SPC-3 Persistent Reservation.
2576 */
2577 }
2578
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002579 /*
2580 * If we operate in passthrough mode we skip most CDB emulation and
2581 * instead hand the commands down to the physical SCSI device.
2582 */
2583 passthrough =
2584 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
2585
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002586 switch (cdb[0]) {
2587 case READ_6:
2588 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2589 if (sector_ret)
2590 goto out_unsupported_cdb;
2591 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002592 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002593 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2594 break;
2595 case READ_10:
2596 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2597 if (sector_ret)
2598 goto out_unsupported_cdb;
2599 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002600 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002601 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2602 break;
2603 case READ_12:
2604 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2605 if (sector_ret)
2606 goto out_unsupported_cdb;
2607 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002608 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002609 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2610 break;
2611 case READ_16:
2612 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2613 if (sector_ret)
2614 goto out_unsupported_cdb;
2615 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002616 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002617 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2618 break;
2619 case WRITE_6:
2620 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2621 if (sector_ret)
2622 goto out_unsupported_cdb;
2623 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002624 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002625 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2626 break;
2627 case WRITE_10:
2628 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2629 if (sector_ret)
2630 goto out_unsupported_cdb;
2631 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002632 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002633 if (cdb[1] & 0x8)
2634 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002635 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2636 break;
2637 case WRITE_12:
2638 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2639 if (sector_ret)
2640 goto out_unsupported_cdb;
2641 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002642 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002643 if (cdb[1] & 0x8)
2644 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002645 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2646 break;
2647 case WRITE_16:
2648 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2649 if (sector_ret)
2650 goto out_unsupported_cdb;
2651 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002652 cmd->t_task_lba = transport_lba_64(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002653 if (cdb[1] & 0x8)
2654 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002655 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2656 break;
2657 case XDWRITEREAD_10:
2658 if ((cmd->data_direction != DMA_TO_DEVICE) ||
Christoph Hellwig33c3faf2011-11-14 11:36:30 -05002659 !(cmd->se_cmd_flags & SCF_BIDI))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002660 goto out_invalid_cdb_field;
2661 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2662 if (sector_ret)
2663 goto out_unsupported_cdb;
2664 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002665 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002666 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002667
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002668 /*
2669 * Do now allow BIDI commands for passthrough mode.
2670 */
2671 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002672 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002673
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002674 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002675 * Setup BIDI XOR callback to be run after I/O completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002676 */
2677 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002678 if (cdb[1] & 0x8)
2679 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002680 break;
2681 case VARIABLE_LENGTH_CMD:
2682 service_action = get_unaligned_be16(&cdb[8]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002683 switch (service_action) {
2684 case XDWRITEREAD_32:
2685 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2686 if (sector_ret)
2687 goto out_unsupported_cdb;
2688 size = transport_get_size(sectors, cdb, cmd);
2689 /*
2690 * Use WRITE_32 and READ_32 opcodes for the emulated
2691 * XDWRITE_READ_32 logic.
2692 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002693 cmd->t_task_lba = transport_lba_64_ext(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002694 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2695
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002696 /*
2697 * Do now allow BIDI commands for passthrough mode.
2698 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002699 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002700 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002701
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002702 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002703 * Setup BIDI XOR callback to be run during after I/O
2704 * completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002705 */
2706 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002707 if (cdb[1] & 0x8)
2708 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002709 break;
2710 case WRITE_SAME_32:
2711 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2712 if (sector_ret)
2713 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002714
Andy Grover6708bb22011-06-08 10:36:43 -07002715 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002716 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07002717 else {
2718 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2719 " supported\n");
2720 goto out_invalid_cdb_field;
2721 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002722
Andy Grovera1d8b492011-05-02 17:12:10 -07002723 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002724 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2725
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002726 if (target_check_write_same_discard(&cdb[10], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08002727 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002728 if (!passthrough)
2729 cmd->execute_task = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002730 break;
2731 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002732 pr_err("VARIABLE_LENGTH_CMD service action"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002733 " 0x%04x not supported\n", service_action);
2734 goto out_unsupported_cdb;
2735 }
2736 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002737 case MAINTENANCE_IN:
Andy Grovere3d6f902011-07-19 08:55:10 +00002738 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002739 /* MAINTENANCE_IN from SCC-2 */
2740 /*
2741 * Check for emulated MI_REPORT_TARGET_PGS.
2742 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002743 if (cdb[1] == MI_REPORT_TARGET_PGS &&
2744 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2745 cmd->execute_task =
2746 target_emulate_report_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002747 }
2748 size = (cdb[6] << 24) | (cdb[7] << 16) |
2749 (cdb[8] << 8) | cdb[9];
2750 } else {
2751 /* GPCMD_SEND_KEY from multi media commands */
2752 size = (cdb[8] << 8) + cdb[9];
2753 }
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 case MODE_SELECT:
2757 size = cdb[4];
2758 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2759 break;
2760 case MODE_SELECT_10:
2761 size = (cdb[7] << 8) + cdb[8];
2762 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2763 break;
2764 case MODE_SENSE:
2765 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002766 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002767 if (!passthrough)
2768 cmd->execute_task = target_emulate_modesense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002769 break;
2770 case MODE_SENSE_10:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002771 size = (cdb[7] << 8) + cdb[8];
2772 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2773 if (!passthrough)
2774 cmd->execute_task = target_emulate_modesense;
2775 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002776 case GPCMD_READ_BUFFER_CAPACITY:
2777 case GPCMD_SEND_OPC:
2778 case LOG_SELECT:
2779 case LOG_SENSE:
2780 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002781 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002782 break;
2783 case READ_BLOCK_LIMITS:
2784 size = READ_BLOCK_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002785 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002786 break;
2787 case GPCMD_GET_CONFIGURATION:
2788 case GPCMD_READ_FORMAT_CAPACITIES:
2789 case GPCMD_READ_DISC_INFO:
2790 case GPCMD_READ_TRACK_RZONE_INFO:
2791 size = (cdb[7] << 8) + cdb[8];
2792 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2793 break;
2794 case PERSISTENT_RESERVE_IN:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002795 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002796 cmd->execute_task = target_scsi3_emulate_pr_in;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002797 size = (cdb[7] << 8) + cdb[8];
2798 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2799 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002800 case PERSISTENT_RESERVE_OUT:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002801 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002802 cmd->execute_task = target_scsi3_emulate_pr_out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002803 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002804 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002805 break;
2806 case GPCMD_MECHANISM_STATUS:
2807 case GPCMD_READ_DVD_STRUCTURE:
2808 size = (cdb[8] << 8) + cdb[9];
2809 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2810 break;
2811 case READ_POSITION:
2812 size = READ_POSITION_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002813 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002814 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002815 case MAINTENANCE_OUT:
Andy Grovere3d6f902011-07-19 08:55:10 +00002816 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002817 /* MAINTENANCE_OUT from SCC-2
2818 *
2819 * Check for emulated MO_SET_TARGET_PGS.
2820 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002821 if (cdb[1] == MO_SET_TARGET_PGS &&
2822 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2823 cmd->execute_task =
2824 target_emulate_set_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002825 }
2826
2827 size = (cdb[6] << 24) | (cdb[7] << 16) |
2828 (cdb[8] << 8) | cdb[9];
2829 } else {
2830 /* GPCMD_REPORT_KEY from multi media commands */
2831 size = (cdb[8] << 8) + cdb[9];
2832 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002833 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002834 break;
2835 case INQUIRY:
2836 size = (cdb[3] << 8) + cdb[4];
2837 /*
2838 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2839 * See spc4r17 section 5.3
2840 */
Andy Grover5951146d2011-07-19 10:26:37 +00002841 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002842 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002843 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002844 if (!passthrough)
2845 cmd->execute_task = target_emulate_inquiry;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002846 break;
2847 case READ_BUFFER:
2848 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002849 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002850 break;
2851 case READ_CAPACITY:
2852 size = READ_CAP_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002853 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002854 if (!passthrough)
2855 cmd->execute_task = target_emulate_readcapacity;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002856 break;
2857 case READ_MEDIA_SERIAL_NUMBER:
2858 case SECURITY_PROTOCOL_IN:
2859 case SECURITY_PROTOCOL_OUT:
2860 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002861 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002862 break;
2863 case SERVICE_ACTION_IN:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002864 switch (cmd->t_task_cdb[1] & 0x1f) {
2865 case SAI_READ_CAPACITY_16:
2866 if (!passthrough)
2867 cmd->execute_task =
2868 target_emulate_readcapacity_16;
2869 break;
2870 default:
2871 if (passthrough)
2872 break;
2873
2874 pr_err("Unsupported SA: 0x%02x\n",
2875 cmd->t_task_cdb[1] & 0x1f);
2876 goto out_unsupported_cdb;
2877 }
2878 /*FALLTHROUGH*/
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002879 case ACCESS_CONTROL_IN:
2880 case ACCESS_CONTROL_OUT:
2881 case EXTENDED_COPY:
2882 case READ_ATTRIBUTE:
2883 case RECEIVE_COPY_RESULTS:
2884 case WRITE_ATTRIBUTE:
2885 size = (cdb[10] << 24) | (cdb[11] << 16) |
2886 (cdb[12] << 8) | cdb[13];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002887 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002888 break;
2889 case RECEIVE_DIAGNOSTIC:
2890 case SEND_DIAGNOSTIC:
2891 size = (cdb[3] << 8) | cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002892 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002893 break;
2894/* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2895#if 0
2896 case GPCMD_READ_CD:
2897 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2898 size = (2336 * sectors);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002899 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002900 break;
2901#endif
2902 case READ_TOC:
2903 size = cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002904 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002905 break;
2906 case REQUEST_SENSE:
2907 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002908 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002909 if (!passthrough)
2910 cmd->execute_task = target_emulate_request_sense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002911 break;
2912 case READ_ELEMENT_STATUS:
2913 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002914 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002915 break;
2916 case WRITE_BUFFER:
2917 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002918 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002919 break;
2920 case RESERVE:
2921 case RESERVE_10:
2922 /*
2923 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2924 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2925 */
2926 if (cdb[0] == RESERVE_10)
2927 size = (cdb[7] << 8) | cdb[8];
2928 else
2929 size = cmd->data_length;
2930
2931 /*
2932 * Setup the legacy emulated handler for SPC-2 and
2933 * >= SPC-3 compatible reservation handling (CRH=1)
2934 * Otherwise, we assume the underlying SCSI logic is
2935 * is running in SPC_PASSTHROUGH, and wants reservations
2936 * emulation disabled.
2937 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002938 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2939 cmd->execute_task = target_scsi2_reservation_reserve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002940 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2941 break;
2942 case RELEASE:
2943 case RELEASE_10:
2944 /*
2945 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2946 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2947 */
2948 if (cdb[0] == RELEASE_10)
2949 size = (cdb[7] << 8) | cdb[8];
2950 else
2951 size = cmd->data_length;
2952
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002953 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2954 cmd->execute_task = target_scsi2_reservation_release;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002955 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2956 break;
2957 case SYNCHRONIZE_CACHE:
Andy Grover8e94b8d2012-01-16 16:57:07 -08002958 case SYNCHRONIZE_CACHE_16:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002959 /*
2960 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2961 */
2962 if (cdb[0] == SYNCHRONIZE_CACHE) {
2963 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002964 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002965 } else {
2966 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002967 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002968 }
2969 if (sector_ret)
2970 goto out_unsupported_cdb;
2971
2972 size = transport_get_size(sectors, cdb, cmd);
2973 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2974
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002975 if (passthrough)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002976 break;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002977
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002978 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002979 * Check to ensure that LBA + Range does not exceed past end of
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002980 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002981 */
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002982 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
2983 if (transport_cmd_get_valid_sectors(cmd) < 0)
2984 goto out_invalid_cdb_field;
2985 }
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002986 cmd->execute_task = target_emulate_synchronize_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002987 break;
2988 case UNMAP:
2989 size = get_unaligned_be16(&cdb[7]);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002990 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002991 if (!passthrough)
2992 cmd->execute_task = target_emulate_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002993 break;
2994 case WRITE_SAME_16:
2995 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2996 if (sector_ret)
2997 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002998
Andy Grover6708bb22011-06-08 10:36:43 -07002999 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07003000 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07003001 else {
3002 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3003 goto out_invalid_cdb_field;
3004 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07003005
Nicholas Bellinger5db07532011-07-27 22:18:52 -07003006 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003007 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003008
3009 if (target_check_write_same_discard(&cdb[1], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08003010 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003011 if (!passthrough)
3012 cmd->execute_task = target_emulate_write_same;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003013 break;
3014 case WRITE_SAME:
3015 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3016 if (sector_ret)
3017 goto out_unsupported_cdb;
3018
3019 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07003020 size = transport_get_size(1, cdb, cmd);
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003021 else {
3022 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3023 goto out_invalid_cdb_field;
3024 }
3025
3026 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
3027 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3028 /*
3029 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
3030 * of byte 1 bit 3 UNMAP instead of original reserved field
3031 */
3032 if (target_check_write_same_discard(&cdb[1], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08003033 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003034 if (!passthrough)
3035 cmd->execute_task = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003036 break;
3037 case ALLOW_MEDIUM_REMOVAL:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003038 case ERASE:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003039 case REZERO_UNIT:
3040 case SEEK_10:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003041 case SPACE:
3042 case START_STOP:
3043 case TEST_UNIT_READY:
3044 case VERIFY:
3045 case WRITE_FILEMARKS:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003046 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3047 if (!passthrough)
3048 cmd->execute_task = target_emulate_noop;
3049 break;
3050 case GPCMD_CLOSE_TRACK:
3051 case INITIALIZE_ELEMENT_STATUS:
3052 case GPCMD_LOAD_UNLOAD:
3053 case GPCMD_SET_SPEED:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003054 case MOVE_MEDIUM:
3055 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3056 break;
3057 case REPORT_LUNS:
Christoph Hellwige76a35d2011-11-03 17:50:42 -04003058 cmd->execute_task = target_report_luns;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003059 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3060 /*
3061 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3062 * See spc4r17 section 5.3
3063 */
Andy Grover5951146d2011-07-19 10:26:37 +00003064 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003065 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003066 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003067 break;
3068 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003069 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003070 " 0x%02x, sending CHECK_CONDITION.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003071 cmd->se_tfo->get_fabric_name(), cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003072 goto out_unsupported_cdb;
3073 }
3074
3075 if (size != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003076 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003077 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
Andy Grovere3d6f902011-07-19 08:55:10 +00003078 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003079 cmd->data_length, size, cdb[0]);
3080
3081 cmd->cmd_spdtl = size;
3082
3083 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grover6708bb22011-06-08 10:36:43 -07003084 pr_err("Rejecting underflow/overflow"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003085 " WRITE data\n");
3086 goto out_invalid_cdb_field;
3087 }
3088 /*
3089 * Reject READ_* or WRITE_* with overflow/underflow for
3090 * type SCF_SCSI_DATA_SG_IO_CDB.
3091 */
Andy Grover6708bb22011-06-08 10:36:43 -07003092 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3093 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003094 " CDB on non 512-byte sector setup subsystem"
Andy Grovere3d6f902011-07-19 08:55:10 +00003095 " plugin: %s\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003096 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3097 goto out_invalid_cdb_field;
3098 }
3099
3100 if (size > cmd->data_length) {
3101 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3102 cmd->residual_count = (size - cmd->data_length);
3103 } else {
3104 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3105 cmd->residual_count = (cmd->data_length - size);
3106 }
3107 cmd->data_length = size;
3108 }
3109
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003110 /* reject any command that we don't have a handler for */
3111 if (!(passthrough || cmd->execute_task ||
3112 (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
3113 goto out_unsupported_cdb;
3114
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003115 transport_set_supported_SAM_opcode(cmd);
3116 return ret;
3117
3118out_unsupported_cdb:
3119 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3120 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover5951146d2011-07-19 10:26:37 +00003121 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003122out_invalid_cdb_field:
3123 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3124 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00003125 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003126}
3127
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003128/*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003129 * Called from I/O completion to determine which dormant/delayed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003130 * and ordered cmds need to have their tasks added to the execution queue.
3131 */
3132static void transport_complete_task_attr(struct se_cmd *cmd)
3133{
Andy Grover5951146d2011-07-19 10:26:37 +00003134 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003135 struct se_cmd *cmd_p, *cmd_tmp;
3136 int new_active_tasks = 0;
3137
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003138 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003139 atomic_dec(&dev->simple_cmds);
3140 smp_mb__after_atomic_dec();
3141 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003142 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003143 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3144 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003145 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003146 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003147 pr_debug("Incremented dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003148 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3149 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003150 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003151 atomic_dec(&dev->dev_ordered_sync);
3152 smp_mb__after_atomic_dec();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003153
3154 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003155 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003156 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3157 }
3158 /*
3159 * Process all commands up to the last received
3160 * ORDERED task attribute which requires another blocking
3161 * boundary
3162 */
3163 spin_lock(&dev->delayed_cmd_lock);
3164 list_for_each_entry_safe(cmd_p, cmd_tmp,
Andy Grover5951146d2011-07-19 10:26:37 +00003165 &dev->delayed_cmd_list, se_delayed_node) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003166
Andy Grover5951146d2011-07-19 10:26:37 +00003167 list_del(&cmd_p->se_delayed_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003168 spin_unlock(&dev->delayed_cmd_lock);
3169
Andy Grover6708bb22011-06-08 10:36:43 -07003170 pr_debug("Calling add_tasks() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003171 " cmd_p: 0x%02x Task Attr: 0x%02x"
3172 " Dormant -> Active, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07003173 cmd_p->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003174 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3175
3176 transport_add_tasks_from_cmd(cmd_p);
3177 new_active_tasks++;
3178
3179 spin_lock(&dev->delayed_cmd_lock);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003180 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003181 break;
3182 }
3183 spin_unlock(&dev->delayed_cmd_lock);
3184 /*
3185 * If new tasks have become active, wake up the transport thread
3186 * to do the processing of the Active tasks.
3187 */
3188 if (new_active_tasks != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003189 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003190}
3191
Christoph Hellwige057f532011-10-17 13:56:41 -04003192static void transport_complete_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003193{
3194 int ret = 0;
3195
Christoph Hellwige057f532011-10-17 13:56:41 -04003196 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3197 transport_complete_task_attr(cmd);
3198
3199 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3200 ret = cmd->se_tfo->queue_status(cmd);
3201 if (ret)
3202 goto out;
3203 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003204
3205 switch (cmd->data_direction) {
3206 case DMA_FROM_DEVICE:
3207 ret = cmd->se_tfo->queue_data_in(cmd);
3208 break;
3209 case DMA_TO_DEVICE:
Andy Groverec98f782011-07-20 19:28:46 +00003210 if (cmd->t_bidi_data_sg) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003211 ret = cmd->se_tfo->queue_data_in(cmd);
3212 if (ret < 0)
Christoph Hellwige057f532011-10-17 13:56:41 -04003213 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003214 }
3215 /* Fall through for DMA_TO_DEVICE */
3216 case DMA_NONE:
3217 ret = cmd->se_tfo->queue_status(cmd);
3218 break;
3219 default:
3220 break;
3221 }
3222
Christoph Hellwige057f532011-10-17 13:56:41 -04003223out:
3224 if (ret < 0) {
3225 transport_handle_queue_full(cmd, cmd->se_dev);
3226 return;
3227 }
3228 transport_lun_remove_cmd(cmd);
3229 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003230}
3231
3232static void transport_handle_queue_full(
3233 struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -04003234 struct se_device *dev)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003235{
3236 spin_lock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003237 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3238 atomic_inc(&dev->dev_qf_count);
3239 smp_mb__after_atomic_inc();
3240 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3241
3242 schedule_work(&cmd->se_dev->qf_work_queue);
3243}
3244
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003245static void target_complete_ok_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003246{
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003247 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003248 int reason = 0, ret;
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003249
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003250 /*
3251 * Check if we need to move delayed/dormant tasks from cmds on the
3252 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3253 * Attribute.
3254 */
Andy Grover5951146d2011-07-19 10:26:37 +00003255 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003256 transport_complete_task_attr(cmd);
3257 /*
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003258 * Check to schedule QUEUE_FULL work, or execute an existing
3259 * cmd->transport_qf_callback()
3260 */
3261 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3262 schedule_work(&cmd->se_dev->qf_work_queue);
3263
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003264 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003265 * Check if we need to retrieve a sense buffer from
3266 * the struct se_cmd in question.
3267 */
3268 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3269 if (transport_get_sense_data(cmd) < 0)
3270 reason = TCM_NON_EXISTENT_LUN;
3271
3272 /*
3273 * Only set when an struct se_task->task_scsi_status returned
3274 * a non GOOD status.
3275 */
3276 if (cmd->scsi_status) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003277 ret = transport_send_check_condition_and_sense(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003278 cmd, reason, 1);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003279 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003280 goto queue_full;
3281
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003282 transport_lun_remove_cmd(cmd);
3283 transport_cmd_check_stop_to_fabric(cmd);
3284 return;
3285 }
3286 }
3287 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003288 * Check for a callback, used by amongst other things
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003289 * XDWRITE_READ_10 emulation.
3290 */
3291 if (cmd->transport_complete_callback)
3292 cmd->transport_complete_callback(cmd);
3293
3294 switch (cmd->data_direction) {
3295 case DMA_FROM_DEVICE:
3296 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003297 if (cmd->se_lun->lun_sep) {
3298 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003299 cmd->data_length;
3300 }
3301 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003302
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003303 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003304 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003305 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003306 break;
3307 case DMA_TO_DEVICE:
3308 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003309 if (cmd->se_lun->lun_sep) {
3310 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003311 cmd->data_length;
3312 }
3313 spin_unlock(&cmd->se_lun->lun_sep_lock);
3314 /*
3315 * Check if we need to send READ payload for BIDI-COMMAND
3316 */
Andy Groverec98f782011-07-20 19:28:46 +00003317 if (cmd->t_bidi_data_sg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003318 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003319 if (cmd->se_lun->lun_sep) {
3320 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003321 cmd->data_length;
3322 }
3323 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003324 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003325 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003326 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003327 break;
3328 }
3329 /* Fall through for DMA_TO_DEVICE */
3330 case DMA_NONE:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003331 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003332 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003333 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003334 break;
3335 default:
3336 break;
3337 }
3338
3339 transport_lun_remove_cmd(cmd);
3340 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003341 return;
3342
3343queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003344 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003345 " data_direction: %d\n", cmd, cmd->data_direction);
Christoph Hellwige057f532011-10-17 13:56:41 -04003346 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3347 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003348}
3349
3350static void transport_free_dev_tasks(struct se_cmd *cmd)
3351{
3352 struct se_task *task, *task_tmp;
3353 unsigned long flags;
Christoph Hellwig0c2cfe52011-10-17 13:56:45 -04003354 LIST_HEAD(dispose_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003355
Andy Grovera1d8b492011-05-02 17:12:10 -07003356 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003357 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07003358 &cmd->t_task_list, t_list) {
Christoph Hellwig0c2cfe52011-10-17 13:56:45 -04003359 if (!(task->task_flags & TF_ACTIVE))
3360 list_move_tail(&task->t_list, &dispose_list);
3361 }
3362 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3363
3364 while (!list_empty(&dispose_list)) {
3365 task = list_first_entry(&dispose_list, struct se_task, t_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003366
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003367 if (task->task_sg != cmd->t_data_sg &&
3368 task->task_sg != cmd->t_bidi_data_sg)
3369 kfree(task->task_sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003370
3371 list_del(&task->t_list);
3372
Christoph Hellwig42bf8292011-10-12 11:07:00 -04003373 cmd->se_dev->transport->free_task(task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003374 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003375}
3376
Andy Grover6708bb22011-06-08 10:36:43 -07003377static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003378{
Andy Groverec98f782011-07-20 19:28:46 +00003379 struct scatterlist *sg;
Andy Groverec98f782011-07-20 19:28:46 +00003380 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003381
Andy Grover6708bb22011-06-08 10:36:43 -07003382 for_each_sg(sgl, sg, nents, count)
3383 __free_page(sg_page(sg));
3384
3385 kfree(sgl);
3386}
3387
3388static inline void transport_free_pages(struct se_cmd *cmd)
3389{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003390 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
Andy Grover6708bb22011-06-08 10:36:43 -07003391 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003392
Andy Grover6708bb22011-06-08 10:36:43 -07003393 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003394 cmd->t_data_sg = NULL;
3395 cmd->t_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003396
Andy Grover6708bb22011-06-08 10:36:43 -07003397 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003398 cmd->t_bidi_data_sg = NULL;
3399 cmd->t_bidi_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003400}
3401
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003402/**
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003403 * transport_release_cmd - free a command
3404 * @cmd: command to free
3405 *
3406 * This routine unconditionally frees a command, and reference counting
3407 * or list removal must be done in the caller.
3408 */
3409static void transport_release_cmd(struct se_cmd *cmd)
3410{
3411 BUG_ON(!cmd->se_tfo);
3412
Andy Groverc8e31f22012-01-19 13:39:17 -08003413 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003414 core_tmr_release_req(cmd->se_tmr_req);
3415 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3416 kfree(cmd->t_task_cdb);
3417 /*
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003418 * If this cmd has been setup with target_get_sess_cmd(), drop
3419 * the kref and call ->release_cmd() in kref callback.
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003420 */
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003421 if (cmd->check_release != 0) {
3422 target_put_sess_cmd(cmd->se_sess, cmd);
3423 return;
3424 }
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003425 cmd->se_tfo->release_cmd(cmd);
3426}
3427
3428/**
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003429 * transport_put_cmd - release a reference to a command
3430 * @cmd: command to release
3431 *
3432 * This routine releases our reference to the command and frees it if possible.
3433 */
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003434static void transport_put_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003435{
3436 unsigned long flags;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003437 int free_tasks = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003438
Andy Grovera1d8b492011-05-02 17:12:10 -07003439 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003440 if (atomic_read(&cmd->t_fe_count)) {
3441 if (!atomic_dec_and_test(&cmd->t_fe_count))
3442 goto out_busy;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003443 }
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003444
3445 if (atomic_read(&cmd->t_se_count)) {
3446 if (!atomic_dec_and_test(&cmd->t_se_count))
3447 goto out_busy;
3448 }
3449
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003450 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
3451 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003452 transport_all_task_dev_remove_state(cmd);
3453 free_tasks = 1;
3454 }
Andy Grovera1d8b492011-05-02 17:12:10 -07003455 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003456
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003457 if (free_tasks != 0)
3458 transport_free_dev_tasks(cmd);
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003459
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003460 transport_free_pages(cmd);
Christoph Hellwig31afc392011-09-13 23:08:11 +02003461 transport_release_cmd(cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003462 return;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003463out_busy:
3464 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003465}
3466
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003467/*
Andy Groverec98f782011-07-20 19:28:46 +00003468 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3469 * allocating in the core.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003470 * @cmd: Associated se_cmd descriptor
3471 * @mem: SGL style memory for TCM WRITE / READ
3472 * @sg_mem_num: Number of SGL elements
3473 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3474 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3475 *
3476 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3477 * of parameters.
3478 */
3479int transport_generic_map_mem_to_cmd(
3480 struct se_cmd *cmd,
Andy Grover5951146d2011-07-19 10:26:37 +00003481 struct scatterlist *sgl,
3482 u32 sgl_count,
3483 struct scatterlist *sgl_bidi,
3484 u32 sgl_bidi_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003485{
Andy Grover5951146d2011-07-19 10:26:37 +00003486 if (!sgl || !sgl_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003487 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003488
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003489 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3490 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
Nicholas Bellingerfef58a62011-11-15 22:13:24 -08003491 /*
3492 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
3493 * scatterlists already have been set to follow what the fabric
3494 * passes for the original expected data transfer length.
3495 */
3496 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
3497 pr_warn("Rejecting SCSI DATA overflow for fabric using"
3498 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
3499 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3500 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3501 return -EINVAL;
3502 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003503
Andy Groverec98f782011-07-20 19:28:46 +00003504 cmd->t_data_sg = sgl;
3505 cmd->t_data_nents = sgl_count;
3506
Andy Grover5951146d2011-07-19 10:26:37 +00003507 if (sgl_bidi && sgl_bidi_count) {
Andy Groverec98f782011-07-20 19:28:46 +00003508 cmd->t_bidi_data_sg = sgl_bidi;
3509 cmd->t_bidi_data_nents = sgl_bidi_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003510 }
3511 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003512 }
3513
3514 return 0;
3515}
3516EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3517
Andy Grover49493142012-01-16 16:57:08 -08003518void *transport_kmap_data_sg(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003519{
Andy Groverec98f782011-07-20 19:28:46 +00003520 struct scatterlist *sg = cmd->t_data_sg;
Andy Grover49493142012-01-16 16:57:08 -08003521 struct page **pages;
3522 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003523
Andy Groverec98f782011-07-20 19:28:46 +00003524 BUG_ON(!sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003525 /*
Andy Groverec98f782011-07-20 19:28:46 +00003526 * We need to take into account a possible offset here for fabrics like
3527 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3528 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
Andy Grover05d1c7c2011-07-20 19:13:28 +00003529 */
Andy Grover49493142012-01-16 16:57:08 -08003530 if (!cmd->t_data_nents)
3531 return NULL;
3532 else if (cmd->t_data_nents == 1)
3533 return kmap(sg_page(sg)) + sg->offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003534
Andy Grover49493142012-01-16 16:57:08 -08003535 /* >1 page. use vmap */
3536 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
3537 if (!pages)
3538 return NULL;
3539
3540 /* convert sg[] to pages[] */
3541 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
3542 pages[i] = sg_page(sg);
3543 }
3544
3545 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
3546 kfree(pages);
3547 if (!cmd->t_data_vmap)
3548 return NULL;
3549
3550 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003551}
Andy Grover49493142012-01-16 16:57:08 -08003552EXPORT_SYMBOL(transport_kmap_data_sg);
3553
3554void transport_kunmap_data_sg(struct se_cmd *cmd)
3555{
Andy Grovera1edf9c2012-02-09 12:18:06 -08003556 if (!cmd->t_data_nents) {
Andy Grover49493142012-01-16 16:57:08 -08003557 return;
Andy Grovera1edf9c2012-02-09 12:18:06 -08003558 } else if (cmd->t_data_nents == 1) {
Andy Grover49493142012-01-16 16:57:08 -08003559 kunmap(sg_page(cmd->t_data_sg));
Andy Grovera1edf9c2012-02-09 12:18:06 -08003560 return;
3561 }
Andy Grover49493142012-01-16 16:57:08 -08003562
3563 vunmap(cmd->t_data_vmap);
3564 cmd->t_data_vmap = NULL;
3565}
3566EXPORT_SYMBOL(transport_kunmap_data_sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003567
3568static int
3569transport_generic_get_mem(struct se_cmd *cmd)
3570{
Andy Groverec98f782011-07-20 19:28:46 +00003571 u32 length = cmd->data_length;
3572 unsigned int nents;
3573 struct page *page;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003574 gfp_t zero_flag;
Andy Groverec98f782011-07-20 19:28:46 +00003575 int i = 0;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003576
Andy Groverec98f782011-07-20 19:28:46 +00003577 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3578 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3579 if (!cmd->t_data_sg)
Andy Grovere3d6f902011-07-19 08:55:10 +00003580 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003581
Andy Groverec98f782011-07-20 19:28:46 +00003582 cmd->t_data_nents = nents;
3583 sg_init_table(cmd->t_data_sg, nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003584
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003585 zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
3586
Andy Groverec98f782011-07-20 19:28:46 +00003587 while (length) {
3588 u32 page_len = min_t(u32, length, PAGE_SIZE);
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003589 page = alloc_page(GFP_KERNEL | zero_flag);
Andy Groverec98f782011-07-20 19:28:46 +00003590 if (!page)
3591 goto out;
3592
3593 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3594 length -= page_len;
3595 i++;
3596 }
3597 return 0;
3598
3599out:
3600 while (i >= 0) {
3601 __free_page(sg_page(&cmd->t_data_sg[i]));
3602 i--;
3603 }
3604 kfree(cmd->t_data_sg);
3605 cmd->t_data_sg = NULL;
3606 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003607}
3608
Andy Grovera1d8b492011-05-02 17:12:10 -07003609/* Reduce sectors if they are too long for the device */
3610static inline sector_t transport_limit_task_sectors(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003611 struct se_device *dev,
3612 unsigned long long lba,
Andy Grovera1d8b492011-05-02 17:12:10 -07003613 sector_t sectors)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003614{
Andy Grovera1d8b492011-05-02 17:12:10 -07003615 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003616
Andy Grovera1d8b492011-05-02 17:12:10 -07003617 if (dev->transport->get_device_type(dev) == TYPE_DISK)
3618 if ((lba + sectors) > transport_dev_end_lba(dev))
3619 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003620
Andy Grovera1d8b492011-05-02 17:12:10 -07003621 return sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003622}
3623
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003624
3625/*
3626 * This function can be used by HW target mode drivers to create a linked
3627 * scatterlist from all contiguously allocated struct se_task->task_sg[].
3628 * This is intended to be called during the completion path by TCM Core
3629 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
3630 */
3631void transport_do_task_sg_chain(struct se_cmd *cmd)
3632{
Andy Groverec98f782011-07-20 19:28:46 +00003633 struct scatterlist *sg_first = NULL;
3634 struct scatterlist *sg_prev = NULL;
3635 int sg_prev_nents = 0;
3636 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003637 struct se_task *task;
Andy Groverec98f782011-07-20 19:28:46 +00003638 u32 chained_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003639 int i;
3640
Andy Groverec98f782011-07-20 19:28:46 +00003641 BUG_ON(!cmd->se_tfo->task_sg_chaining);
3642
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003643 /*
3644 * Walk the struct se_task list and setup scatterlist chains
Andy Grovera1d8b492011-05-02 17:12:10 -07003645 * for each contiguously allocated struct se_task->task_sg[].
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003646 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003647 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Andy Groverec98f782011-07-20 19:28:46 +00003648 if (!task->task_sg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003649 continue;
3650
Andy Groverec98f782011-07-20 19:28:46 +00003651 if (!sg_first) {
3652 sg_first = task->task_sg;
Andy Grover6708bb22011-06-08 10:36:43 -07003653 chained_nents = task->task_sg_nents;
Nicholas Bellinger97868c82011-05-19 20:19:09 -07003654 } else {
Andy Groverec98f782011-07-20 19:28:46 +00003655 sg_chain(sg_prev, sg_prev_nents, task->task_sg);
Andy Grover6708bb22011-06-08 10:36:43 -07003656 chained_nents += task->task_sg_nents;
Nicholas Bellinger97868c82011-05-19 20:19:09 -07003657 }
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003658 /*
3659 * For the padded tasks, use the extra SGL vector allocated
3660 * in transport_allocate_data_tasks() for the sg_prev_nents
Christoph Hellwig04629b72011-10-12 11:07:04 -04003661 * offset into sg_chain() above.
3662 *
3663 * We do not need the padding for the last task (or a single
3664 * task), but in that case we will never use the sg_prev_nents
3665 * value below which would be incorrect.
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003666 */
Christoph Hellwig04629b72011-10-12 11:07:04 -04003667 sg_prev_nents = (task->task_sg_nents + 1);
Andy Groverec98f782011-07-20 19:28:46 +00003668 sg_prev = task->task_sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003669 }
3670 /*
3671 * Setup the starting pointer and total t_tasks_sg_linked_no including
3672 * padding SGs for linking and to mark the end.
3673 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003674 cmd->t_tasks_sg_chained = sg_first;
Andy Groverec98f782011-07-20 19:28:46 +00003675 cmd->t_tasks_sg_chained_no = chained_nents;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003676
Andy Grover6708bb22011-06-08 10:36:43 -07003677 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
Andy Grovera1d8b492011-05-02 17:12:10 -07003678 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
3679 cmd->t_tasks_sg_chained_no);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003680
Andy Grovera1d8b492011-05-02 17:12:10 -07003681 for_each_sg(cmd->t_tasks_sg_chained, sg,
3682 cmd->t_tasks_sg_chained_no, i) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003683
Andy Grover6708bb22011-06-08 10:36:43 -07003684 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
Andy Grover5951146d2011-07-19 10:26:37 +00003685 i, sg, sg_page(sg), sg->length, sg->offset);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003686 if (sg_is_chain(sg))
Andy Grover6708bb22011-06-08 10:36:43 -07003687 pr_debug("SG: %p sg_is_chain=1\n", sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003688 if (sg_is_last(sg))
Andy Grover6708bb22011-06-08 10:36:43 -07003689 pr_debug("SG: %p sg_is_last=1\n", sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003690 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003691}
3692EXPORT_SYMBOL(transport_do_task_sg_chain);
3693
Andy Grovera1d8b492011-05-02 17:12:10 -07003694/*
3695 * Break up cmd into chunks transport can handle
3696 */
Christoph Hellwig38b400672011-10-18 06:57:02 -04003697static int
3698transport_allocate_data_tasks(struct se_cmd *cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003699 enum dma_data_direction data_direction,
Christoph Hellwig38b400672011-10-18 06:57:02 -04003700 struct scatterlist *cmd_sg, unsigned int sgl_nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003701{
Andy Grover5951146d2011-07-19 10:26:37 +00003702 struct se_device *dev = cmd->se_dev;
Christoph Hellwiga3eedc22011-09-25 14:56:43 -04003703 int task_count, i;
Christoph Hellwig38b400672011-10-18 06:57:02 -04003704 unsigned long long lba;
3705 sector_t sectors, dev_max_sectors;
3706 u32 sector_size;
3707
3708 if (transport_cmd_get_valid_sectors(cmd) < 0)
3709 return -EINVAL;
3710
3711 dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
3712 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003713
Andy Groverec98f782011-07-20 19:28:46 +00003714 WARN_ON(cmd->data_length % sector_size);
Christoph Hellwig38b400672011-10-18 06:57:02 -04003715
3716 lba = cmd->t_task_lba;
Andy Groverec98f782011-07-20 19:28:46 +00003717 sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
Nicholas Bellinger277c5f22011-07-26 00:38:40 -07003718 task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003719
3720 /*
3721 * If we need just a single task reuse the SG list in the command
3722 * and avoid a lot of work.
3723 */
3724 if (task_count == 1) {
3725 struct se_task *task;
3726 unsigned long flags;
3727
3728 task = transport_generic_get_task(cmd, data_direction);
3729 if (!task)
3730 return -ENOMEM;
3731
3732 task->task_sg = cmd_sg;
3733 task->task_sg_nents = sgl_nents;
3734
3735 task->task_lba = lba;
3736 task->task_sectors = sectors;
3737 task->task_size = task->task_sectors * sector_size;
3738
3739 spin_lock_irqsave(&cmd->t_state_lock, flags);
3740 list_add_tail(&task->t_list, &cmd->t_task_list);
3741 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3742
3743 return task_count;
3744 }
3745
Andy Groverec98f782011-07-20 19:28:46 +00003746 for (i = 0; i < task_count; i++) {
Christoph Hellwig38b400672011-10-18 06:57:02 -04003747 struct se_task *task;
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003748 unsigned int task_size, task_sg_nents_padded;
Christoph Hellwig38b400672011-10-18 06:57:02 -04003749 struct scatterlist *sg;
3750 unsigned long flags;
Andy Groverec98f782011-07-20 19:28:46 +00003751 int count;
Andy Grovera1d8b492011-05-02 17:12:10 -07003752
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003753 task = transport_generic_get_task(cmd, data_direction);
Andy Grovera1d8b492011-05-02 17:12:10 -07003754 if (!task)
Andy Groverec98f782011-07-20 19:28:46 +00003755 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003756
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003757 task->task_lba = lba;
Andy Groverec98f782011-07-20 19:28:46 +00003758 task->task_sectors = min(sectors, dev_max_sectors);
3759 task->task_size = task->task_sectors * sector_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003760
Nicholas Bellinger525a48a2011-08-13 02:11:38 -07003761 /*
3762 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
3763 * in order to calculate the number per task SGL entries
3764 */
3765 task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003766 /*
Andy Groverec98f782011-07-20 19:28:46 +00003767 * Check if the fabric module driver is requesting that all
3768 * struct se_task->task_sg[] be chained together.. If so,
3769 * then allocate an extra padding SG entry for linking and
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003770 * marking the end of the chained SGL for every task except
3771 * the last one for (task_count > 1) operation, or skipping
3772 * the extra padding for the (task_count == 1) case.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003773 */
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003774 if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
3775 task_sg_nents_padded = (task->task_sg_nents + 1);
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003776 } else
3777 task_sg_nents_padded = task->task_sg_nents;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003778
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003779 task->task_sg = kmalloc(sizeof(struct scatterlist) *
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003780 task_sg_nents_padded, GFP_KERNEL);
Andy Groverec98f782011-07-20 19:28:46 +00003781 if (!task->task_sg) {
3782 cmd->se_dev->transport->free_task(task);
3783 return -ENOMEM;
3784 }
3785
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003786 sg_init_table(task->task_sg, task_sg_nents_padded);
Andy Groverec98f782011-07-20 19:28:46 +00003787
3788 task_size = task->task_size;
3789
3790 /* Build new sgl, only up to task_size */
Andy Grover6708bb22011-06-08 10:36:43 -07003791 for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
Andy Groverec98f782011-07-20 19:28:46 +00003792 if (cmd_sg->length > task_size)
3793 break;
3794
3795 *sg = *cmd_sg;
3796 task_size -= cmd_sg->length;
3797 cmd_sg = sg_next(cmd_sg);
3798 }
3799
3800 lba += task->task_sectors;
3801 sectors -= task->task_sectors;
3802
3803 spin_lock_irqsave(&cmd->t_state_lock, flags);
3804 list_add_tail(&task->t_list, &cmd->t_task_list);
3805 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003806 }
3807
Andy Groverec98f782011-07-20 19:28:46 +00003808 return task_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003809}
3810
3811static int
Andy Groverec98f782011-07-20 19:28:46 +00003812transport_allocate_control_task(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003813{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003814 struct se_task *task;
Andy Groverec98f782011-07-20 19:28:46 +00003815 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003816
Nicholas Bellinger91ec1d32012-01-13 12:01:34 -08003817 /* Workaround for handling zero-length control CDBs */
3818 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3819 !cmd->data_length)
3820 return 0;
3821
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003822 task = transport_generic_get_task(cmd, cmd->data_direction);
3823 if (!task)
Andy Groverec98f782011-07-20 19:28:46 +00003824 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003825
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003826 task->task_sg = cmd->t_data_sg;
Andy Groverec98f782011-07-20 19:28:46 +00003827 task->task_size = cmd->data_length;
Andy Grover6708bb22011-06-08 10:36:43 -07003828 task->task_sg_nents = cmd->t_data_nents;
Andy Groverec98f782011-07-20 19:28:46 +00003829
3830 spin_lock_irqsave(&cmd->t_state_lock, flags);
3831 list_add_tail(&task->t_list, &cmd->t_task_list);
3832 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003833
Andy Grover6708bb22011-06-08 10:36:43 -07003834 /* Success! Return number of tasks allocated */
Christoph Hellwiga3eedc22011-09-25 14:56:43 -04003835 return 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003836}
3837
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003838/*
3839 * Allocate any required ressources to execute the command, and either place
3840 * it on the execution queue if possible. For writes we might not have the
3841 * payload yet, thus notify the fabric via a call to ->write_pending instead.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003842 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003843int transport_generic_new_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003844{
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003845 struct se_device *dev = cmd->se_dev;
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003846 int task_cdbs, task_cdbs_bidi = 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003847 int set_counts = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003848 int ret = 0;
3849
3850 /*
3851 * Determine is the TCM fabric module has already allocated physical
3852 * memory, and is directly calling transport_generic_map_mem_to_cmd()
Andy Groverec98f782011-07-20 19:28:46 +00003853 * beforehand.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003854 */
Andy Groverec98f782011-07-20 19:28:46 +00003855 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3856 cmd->data_length) {
Andy Grover05d1c7c2011-07-20 19:13:28 +00003857 ret = transport_generic_get_mem(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003858 if (ret < 0)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003859 goto out_fail;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003860 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003861
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003862 /*
Christoph Hellwig38b400672011-10-18 06:57:02 -04003863 * For BIDI command set up the read tasks first.
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003864 */
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003865 if (cmd->t_bidi_data_sg &&
Christoph Hellwig38b400672011-10-18 06:57:02 -04003866 dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
3867 BUG_ON(!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB));
3868
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003869 task_cdbs_bidi = transport_allocate_data_tasks(cmd,
3870 DMA_FROM_DEVICE, cmd->t_bidi_data_sg,
3871 cmd->t_bidi_data_nents);
3872 if (task_cdbs_bidi <= 0)
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003873 goto out_fail;
3874
3875 atomic_inc(&cmd->t_fe_count);
3876 atomic_inc(&cmd->t_se_count);
3877 set_counts = 0;
3878 }
Christoph Hellwig38b400672011-10-18 06:57:02 -04003879
3880 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3881 task_cdbs = transport_allocate_data_tasks(cmd,
3882 cmd->data_direction, cmd->t_data_sg,
3883 cmd->t_data_nents);
3884 } else {
3885 task_cdbs = transport_allocate_control_task(cmd);
3886 }
3887
Roland Dreier410f6702011-11-22 13:51:32 -08003888 if (task_cdbs < 0)
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003889 goto out_fail;
Roland Dreier410f6702011-11-22 13:51:32 -08003890 else if (!task_cdbs && (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003891 spin_lock_irq(&cmd->t_state_lock);
Roland Dreier410f6702011-11-22 13:51:32 -08003892 cmd->t_state = TRANSPORT_COMPLETE;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003893 cmd->transport_state |= CMD_T_ACTIVE;
3894 spin_unlock_irq(&cmd->t_state_lock);
Nicholas Bellinger91ec1d32012-01-13 12:01:34 -08003895
3896 if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
3897 u8 ua_asc = 0, ua_ascq = 0;
3898
3899 core_scsi3_ua_clear_for_request_sense(cmd,
3900 &ua_asc, &ua_ascq);
3901 }
3902
Roland Dreier410f6702011-11-22 13:51:32 -08003903 INIT_WORK(&cmd->work, target_complete_ok_work);
3904 queue_work(target_completion_wq, &cmd->work);
3905 return 0;
3906 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003907
3908 if (set_counts) {
3909 atomic_inc(&cmd->t_fe_count);
3910 atomic_inc(&cmd->t_se_count);
3911 }
3912
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003913 cmd->t_task_list_num = (task_cdbs + task_cdbs_bidi);
3914 atomic_set(&cmd->t_task_cdbs_left, cmd->t_task_list_num);
3915 atomic_set(&cmd->t_task_cdbs_ex_left, cmd->t_task_list_num);
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003916
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003917 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07003918 * For WRITEs, let the fabric know its buffer is ready..
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003919 * This WRITE struct se_cmd (and all of its associated struct se_task's)
3920 * will be added to the struct se_device execution queue after its WRITE
3921 * data has arrived. (ie: It gets handled by the transport processing
3922 * thread a second time)
3923 */
3924 if (cmd->data_direction == DMA_TO_DEVICE) {
3925 transport_add_tasks_to_state_queue(cmd);
3926 return transport_generic_write_pending(cmd);
3927 }
3928 /*
3929 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
3930 * to the execution queue.
3931 */
3932 transport_execute_tasks(cmd);
3933 return 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003934
3935out_fail:
3936 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3937 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3938 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003939}
Andy Grovera1d8b492011-05-02 17:12:10 -07003940EXPORT_SYMBOL(transport_generic_new_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003941
3942/* transport_generic_process_write():
3943 *
3944 *
3945 */
3946void transport_generic_process_write(struct se_cmd *cmd)
3947{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003948 transport_execute_tasks(cmd);
3949}
3950EXPORT_SYMBOL(transport_generic_process_write);
3951
Christoph Hellwige057f532011-10-17 13:56:41 -04003952static void transport_write_pending_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003953{
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003954 int ret;
3955
3956 ret = cmd->se_tfo->write_pending(cmd);
3957 if (ret == -EAGAIN || ret == -ENOMEM) {
Christoph Hellwige057f532011-10-17 13:56:41 -04003958 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3959 cmd);
3960 transport_handle_queue_full(cmd, cmd->se_dev);
3961 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003962}
3963
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003964static int transport_generic_write_pending(struct se_cmd *cmd)
3965{
3966 unsigned long flags;
3967 int ret;
3968
Andy Grovera1d8b492011-05-02 17:12:10 -07003969 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003970 cmd->t_state = TRANSPORT_WRITE_PENDING;
Andy Grovera1d8b492011-05-02 17:12:10 -07003971 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003972
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003973 /*
3974 * Clear the se_cmd for WRITE_PENDING status in order to set
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003975 * CMD_T_ACTIVE so that transport_generic_handle_data can be called
3976 * from HW target mode interrupt code. This is safe to be called
3977 * with transport_off=1 before the cmd->se_tfo->write_pending
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003978 * because the se_cmd->se_lun pointer is not being cleared.
3979 */
3980 transport_cmd_check_stop(cmd, 1, 0);
3981
3982 /*
3983 * Call the fabric write_pending function here to let the
3984 * frontend know that WRITE buffers are ready.
3985 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003986 ret = cmd->se_tfo->write_pending(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003987 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003988 goto queue_full;
3989 else if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003990 return ret;
3991
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003992 return 1;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003993
3994queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003995 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003996 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
Christoph Hellwige057f532011-10-17 13:56:41 -04003997 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003998 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003999}
4000
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07004001void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004002{
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004003 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
Andy Groverc8e31f22012-01-19 13:39:17 -08004004 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004005 transport_wait_for_tasks(cmd);
4006
Christoph Hellwig35462972011-05-31 23:56:57 -04004007 transport_release_cmd(cmd);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004008 } else {
4009 if (wait_for_tasks)
4010 transport_wait_for_tasks(cmd);
4011
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004012 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
4013
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +02004014 if (cmd->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004015 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004016
Nicholas Bellingerf4366772011-05-19 20:19:11 -07004017 transport_free_dev_tasks(cmd);
4018
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07004019 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004020 }
4021}
4022EXPORT_SYMBOL(transport_generic_free_cmd);
4023
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004024/* target_get_sess_cmd - Add command to active ->sess_cmd_list
4025 * @se_sess: session to reference
4026 * @se_cmd: command descriptor to add
Nicholas Bellingera6360782011-11-18 20:36:22 -08004027 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004028 */
Nicholas Bellingera6360782011-11-18 20:36:22 -08004029void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
4030 bool ack_kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004031{
4032 unsigned long flags;
4033
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004034 kref_init(&se_cmd->cmd_kref);
Nicholas Bellingera6360782011-11-18 20:36:22 -08004035 /*
4036 * Add a second kref if the fabric caller is expecting to handle
4037 * fabric acknowledgement that requires two target_put_sess_cmd()
4038 * invocations before se_cmd descriptor release.
4039 */
Nicholas Bellinger86715562012-02-13 01:07:22 -08004040 if (ack_kref == true) {
Nicholas Bellingera6360782011-11-18 20:36:22 -08004041 kref_get(&se_cmd->cmd_kref);
Nicholas Bellinger86715562012-02-13 01:07:22 -08004042 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
4043 }
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004044
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004045 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4046 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
4047 se_cmd->check_release = 1;
4048 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4049}
4050EXPORT_SYMBOL(target_get_sess_cmd);
4051
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004052static void target_release_cmd_kref(struct kref *kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004053{
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004054 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
4055 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004056 unsigned long flags;
4057
4058 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4059 if (list_empty(&se_cmd->se_cmd_list)) {
4060 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Nicholas Bellingerffc32d52012-02-13 02:35:01 -08004061 se_cmd->se_tfo->release_cmd(se_cmd);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004062 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004063 }
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004064 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
4065 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4066 complete(&se_cmd->cmd_wait_comp);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004067 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004068 }
4069 list_del(&se_cmd->se_cmd_list);
4070 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4071
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004072 se_cmd->se_tfo->release_cmd(se_cmd);
4073}
4074
4075/* target_put_sess_cmd - Check for active I/O shutdown via kref_put
4076 * @se_sess: session to reference
4077 * @se_cmd: command descriptor to drop
4078 */
4079int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
4080{
4081 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004082}
4083EXPORT_SYMBOL(target_put_sess_cmd);
4084
4085/* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
4086 * @se_sess: session to split
4087 */
4088void target_splice_sess_cmd_list(struct se_session *se_sess)
4089{
4090 struct se_cmd *se_cmd;
4091 unsigned long flags;
4092
4093 WARN_ON(!list_empty(&se_sess->sess_wait_list));
4094 INIT_LIST_HEAD(&se_sess->sess_wait_list);
4095
4096 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4097 se_sess->sess_tearing_down = 1;
4098
4099 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
4100
4101 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
4102 se_cmd->cmd_wait_set = 1;
4103
4104 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4105}
4106EXPORT_SYMBOL(target_splice_sess_cmd_list);
4107
4108/* target_wait_for_sess_cmds - Wait for outstanding descriptors
4109 * @se_sess: session to wait for active I/O
4110 * @wait_for_tasks: Make extra transport_wait_for_tasks call
4111 */
4112void target_wait_for_sess_cmds(
4113 struct se_session *se_sess,
4114 int wait_for_tasks)
4115{
4116 struct se_cmd *se_cmd, *tmp_cmd;
4117 bool rc = false;
4118
4119 list_for_each_entry_safe(se_cmd, tmp_cmd,
4120 &se_sess->sess_wait_list, se_cmd_list) {
4121 list_del(&se_cmd->se_cmd_list);
4122
4123 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
4124 " %d\n", se_cmd, se_cmd->t_state,
4125 se_cmd->se_tfo->get_cmd_state(se_cmd));
4126
4127 if (wait_for_tasks) {
4128 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
4129 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4130 se_cmd->se_tfo->get_cmd_state(se_cmd));
4131
4132 rc = transport_wait_for_tasks(se_cmd);
4133
4134 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
4135 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4136 se_cmd->se_tfo->get_cmd_state(se_cmd));
4137 }
4138
4139 if (!rc) {
4140 wait_for_completion(&se_cmd->cmd_wait_comp);
4141 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
4142 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4143 se_cmd->se_tfo->get_cmd_state(se_cmd));
4144 }
4145
4146 se_cmd->se_tfo->release_cmd(se_cmd);
4147 }
4148}
4149EXPORT_SYMBOL(target_wait_for_sess_cmds);
4150
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004151/* transport_lun_wait_for_tasks():
4152 *
4153 * Called from ConfigFS context to stop the passed struct se_cmd to allow
4154 * an struct se_lun to be successfully shutdown.
4155 */
4156static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
4157{
4158 unsigned long flags;
4159 int ret;
4160 /*
4161 * If the frontend has already requested this struct se_cmd to
4162 * be stopped, we can safely ignore this struct se_cmd.
4163 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004164 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004165 if (cmd->transport_state & CMD_T_STOP) {
4166 cmd->transport_state &= ~CMD_T_LUN_STOP;
4167
4168 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
4169 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07004170 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004171 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovere3d6f902011-07-19 08:55:10 +00004172 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004173 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004174 cmd->transport_state |= CMD_T_LUN_FE_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07004175 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004176
Andy Grover5951146d2011-07-19 10:26:37 +00004177 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004178
4179 ret = transport_stop_tasks_for_cmd(cmd);
4180
Andy Grover6708bb22011-06-08 10:36:43 -07004181 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4182 " %d\n", cmd, cmd->t_task_list_num, ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004183 if (!ret) {
Andy Grover6708bb22011-06-08 10:36:43 -07004184 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004185 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07004186 wait_for_completion(&cmd->transport_lun_stop_comp);
Andy Grover6708bb22011-06-08 10:36:43 -07004187 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004188 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004189 }
Christoph Hellwig3df8d402011-10-17 13:56:43 -04004190 transport_remove_cmd_from_queue(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004191
4192 return 0;
4193}
4194
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004195static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4196{
4197 struct se_cmd *cmd = NULL;
4198 unsigned long lun_flags, cmd_flags;
4199 /*
4200 * Do exception processing and return CHECK_CONDITION status to the
4201 * Initiator Port.
4202 */
4203 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
Andy Grover5951146d2011-07-19 10:26:37 +00004204 while (!list_empty(&lun->lun_cmd_list)) {
4205 cmd = list_first_entry(&lun->lun_cmd_list,
4206 struct se_cmd, se_lun_node);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -05004207 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004208
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004209 /*
4210 * This will notify iscsi_target_transport.c:
4211 * transport_cmd_check_stop() that a LUN shutdown is in
4212 * progress for the iscsi_cmd_t.
4213 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004214 spin_lock(&cmd->t_state_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07004215 pr_debug("SE_LUN[%d] - Setting cmd->transport"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004216 "_lun_stop for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004217 cmd->se_lun->unpacked_lun,
4218 cmd->se_tfo->get_task_tag(cmd));
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004219 cmd->transport_state |= CMD_T_LUN_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07004220 spin_unlock(&cmd->t_state_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004221
4222 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4223
Andy Grover6708bb22011-06-08 10:36:43 -07004224 if (!cmd->se_lun) {
4225 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004226 cmd->se_tfo->get_task_tag(cmd),
4227 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004228 BUG();
4229 }
4230 /*
4231 * If the Storage engine still owns the iscsi_cmd_t, determine
4232 * and/or stop its context.
4233 */
Andy Grover6708bb22011-06-08 10:36:43 -07004234 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
Andy Grovere3d6f902011-07-19 08:55:10 +00004235 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4236 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004237
Andy Grovere3d6f902011-07-19 08:55:10 +00004238 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004239 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4240 continue;
4241 }
4242
Andy Grover6708bb22011-06-08 10:36:43 -07004243 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004244 "_wait_for_tasks(): SUCCESS\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004245 cmd->se_lun->unpacked_lun,
4246 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004247
Andy Grovera1d8b492011-05-02 17:12:10 -07004248 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004249 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004250 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004251 goto check_cond;
4252 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004253 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004254 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07004255 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004256
4257 transport_free_dev_tasks(cmd);
4258 /*
4259 * The Storage engine stopped this struct se_cmd before it was
4260 * send to the fabric frontend for delivery back to the
4261 * Initiator Node. Return this SCSI CDB back with an
4262 * CHECK_CONDITION status.
4263 */
4264check_cond:
4265 transport_send_check_condition_and_sense(cmd,
4266 TCM_NON_EXISTENT_LUN, 0);
4267 /*
4268 * If the fabric frontend is waiting for this iscsi_cmd_t to
4269 * be released, notify the waiting thread now that LU has
4270 * finished accessing it.
4271 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004272 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004273 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07004274 pr_debug("SE_LUN[%d] - Detected FE stop for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004275 " struct se_cmd: %p ITT: 0x%08x\n",
4276 lun->unpacked_lun,
Andy Grovere3d6f902011-07-19 08:55:10 +00004277 cmd, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004278
Andy Grovera1d8b492011-05-02 17:12:10 -07004279 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004280 cmd_flags);
4281 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovera1d8b492011-05-02 17:12:10 -07004282 complete(&cmd->transport_lun_fe_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004283 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4284 continue;
4285 }
Andy Grover6708bb22011-06-08 10:36:43 -07004286 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004287 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004288
Andy Grovera1d8b492011-05-02 17:12:10 -07004289 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004290 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4291 }
4292 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4293}
4294
4295static int transport_clear_lun_thread(void *p)
4296{
Jörn Engel8359cf42011-11-24 02:05:51 +01004297 struct se_lun *lun = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004298
4299 __transport_clear_lun_from_sessions(lun);
4300 complete(&lun->lun_shutdown_comp);
4301
4302 return 0;
4303}
4304
4305int transport_clear_lun_from_sessions(struct se_lun *lun)
4306{
4307 struct task_struct *kt;
4308
Andy Grover5951146d2011-07-19 10:26:37 +00004309 kt = kthread_run(transport_clear_lun_thread, lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004310 "tcm_cl_%u", lun->unpacked_lun);
4311 if (IS_ERR(kt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07004312 pr_err("Unable to start clear_lun thread\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00004313 return PTR_ERR(kt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004314 }
4315 wait_for_completion(&lun->lun_shutdown_comp);
4316
4317 return 0;
4318}
4319
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004320/**
4321 * transport_wait_for_tasks - wait for completion to occur
4322 * @cmd: command to wait
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004323 *
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004324 * Called from frontend fabric context to wait for storage engine
4325 * to pause and/or release frontend generated struct se_cmd.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004326 */
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004327bool transport_wait_for_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004328{
4329 unsigned long flags;
4330
Andy Grovera1d8b492011-05-02 17:12:10 -07004331 spin_lock_irqsave(&cmd->t_state_lock, flags);
Andy Groverc8e31f22012-01-19 13:39:17 -08004332 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
4333 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004334 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004335 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004336 }
4337 /*
4338 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
4339 * has been set in transport_set_supported_SAM_opcode().
4340 */
Andy Groverc8e31f22012-01-19 13:39:17 -08004341 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
4342 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004343 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004344 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004345 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004346 /*
4347 * If we are already stopped due to an external event (ie: LUN shutdown)
4348 * sleep until the connection can have the passed struct se_cmd back.
Andy Grovera1d8b492011-05-02 17:12:10 -07004349 * The cmd->transport_lun_stopped_sem will be upped by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004350 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4351 * has completed its operation on the struct se_cmd.
4352 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004353 if (cmd->transport_state & CMD_T_LUN_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07004354 pr_debug("wait_for_tasks: Stopping"
Andy Grovere3d6f902011-07-19 08:55:10 +00004355 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004356 "_stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004357 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004358 /*
4359 * There is a special case for WRITES where a FE exception +
4360 * LUN shutdown means ConfigFS context is still sleeping on
4361 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4362 * We go ahead and up transport_lun_stop_comp just to be sure
4363 * here.
4364 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004365 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4366 complete(&cmd->transport_lun_stop_comp);
4367 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4368 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004369
4370 transport_all_task_dev_remove_state(cmd);
4371 /*
4372 * At this point, the frontend who was the originator of this
4373 * struct se_cmd, now owns the structure and can be released through
4374 * normal means below.
4375 */
Andy Grover6708bb22011-06-08 10:36:43 -07004376 pr_debug("wait_for_tasks: Stopped"
Andy Grovere3d6f902011-07-19 08:55:10 +00004377 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004378 "stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004379 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004380
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004381 cmd->transport_state &= ~CMD_T_LUN_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004382 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004383
Nicholas Bellinger3d289342012-02-13 02:38:14 -08004384 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004385 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004386 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004387 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004388
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004389 cmd->transport_state |= CMD_T_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004390
Andy Grover6708bb22011-06-08 10:36:43 -07004391 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004392 " i_state: %d, t_state: %d, CMD_T_STOP\n",
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004393 cmd, cmd->se_tfo->get_task_tag(cmd),
4394 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004395
Andy Grovera1d8b492011-05-02 17:12:10 -07004396 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004397
Andy Grover5951146d2011-07-19 10:26:37 +00004398 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004399
Andy Grovera1d8b492011-05-02 17:12:10 -07004400 wait_for_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004401
Andy Grovera1d8b492011-05-02 17:12:10 -07004402 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004403 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004404
Andy Grover6708bb22011-06-08 10:36:43 -07004405 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
Andy Grovera1d8b492011-05-02 17:12:10 -07004406 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004407 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004408
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004409 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004410
4411 return true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004412}
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004413EXPORT_SYMBOL(transport_wait_for_tasks);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004414
4415static int transport_get_sense_codes(
4416 struct se_cmd *cmd,
4417 u8 *asc,
4418 u8 *ascq)
4419{
4420 *asc = cmd->scsi_asc;
4421 *ascq = cmd->scsi_ascq;
4422
4423 return 0;
4424}
4425
4426static int transport_set_sense_codes(
4427 struct se_cmd *cmd,
4428 u8 asc,
4429 u8 ascq)
4430{
4431 cmd->scsi_asc = asc;
4432 cmd->scsi_ascq = ascq;
4433
4434 return 0;
4435}
4436
4437int transport_send_check_condition_and_sense(
4438 struct se_cmd *cmd,
4439 u8 reason,
4440 int from_transport)
4441{
4442 unsigned char *buffer = cmd->sense_buffer;
4443 unsigned long flags;
4444 int offset;
4445 u8 asc = 0, ascq = 0;
4446
Andy Grovera1d8b492011-05-02 17:12:10 -07004447 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004448 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004449 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004450 return 0;
4451 }
4452 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
Andy Grovera1d8b492011-05-02 17:12:10 -07004453 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004454
4455 if (!reason && from_transport)
4456 goto after_reason;
4457
4458 if (!from_transport)
4459 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4460 /*
4461 * Data Segment and SenseLength of the fabric response PDU.
4462 *
4463 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4464 * from include/scsi/scsi_cmnd.h
4465 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004466 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004467 TRANSPORT_SENSE_BUFFER);
4468 /*
4469 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4470 * SENSE KEY values from include/scsi/scsi.h
4471 */
4472 switch (reason) {
4473 case TCM_NON_EXISTENT_LUN:
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004474 /* CURRENT ERROR */
4475 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004476 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004477 /* ILLEGAL REQUEST */
4478 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4479 /* LOGICAL UNIT NOT SUPPORTED */
4480 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4481 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004482 case TCM_UNSUPPORTED_SCSI_OPCODE:
4483 case TCM_SECTOR_COUNT_TOO_MANY:
4484 /* CURRENT ERROR */
4485 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004486 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004487 /* ILLEGAL REQUEST */
4488 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4489 /* INVALID COMMAND OPERATION CODE */
4490 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4491 break;
4492 case TCM_UNKNOWN_MODE_PAGE:
4493 /* CURRENT ERROR */
4494 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004495 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004496 /* ILLEGAL REQUEST */
4497 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4498 /* INVALID FIELD IN CDB */
4499 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4500 break;
4501 case TCM_CHECK_CONDITION_ABORT_CMD:
4502 /* CURRENT ERROR */
4503 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004504 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004505 /* ABORTED COMMAND */
4506 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4507 /* BUS DEVICE RESET FUNCTION OCCURRED */
4508 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4509 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4510 break;
4511 case TCM_INCORRECT_AMOUNT_OF_DATA:
4512 /* CURRENT ERROR */
4513 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004514 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004515 /* ABORTED COMMAND */
4516 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4517 /* WRITE ERROR */
4518 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4519 /* NOT ENOUGH UNSOLICITED DATA */
4520 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4521 break;
4522 case TCM_INVALID_CDB_FIELD:
4523 /* CURRENT ERROR */
4524 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004525 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08004526 /* ILLEGAL REQUEST */
4527 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004528 /* INVALID FIELD IN CDB */
4529 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4530 break;
4531 case TCM_INVALID_PARAMETER_LIST:
4532 /* CURRENT ERROR */
4533 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004534 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08004535 /* ILLEGAL REQUEST */
4536 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004537 /* INVALID FIELD IN PARAMETER LIST */
4538 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4539 break;
4540 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4541 /* CURRENT ERROR */
4542 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004543 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004544 /* ABORTED COMMAND */
4545 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4546 /* WRITE ERROR */
4547 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4548 /* UNEXPECTED_UNSOLICITED_DATA */
4549 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4550 break;
4551 case TCM_SERVICE_CRC_ERROR:
4552 /* CURRENT ERROR */
4553 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004554 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004555 /* ABORTED COMMAND */
4556 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4557 /* PROTOCOL SERVICE CRC ERROR */
4558 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4559 /* N/A */
4560 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4561 break;
4562 case TCM_SNACK_REJECTED:
4563 /* CURRENT ERROR */
4564 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004565 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004566 /* ABORTED COMMAND */
4567 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4568 /* READ ERROR */
4569 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4570 /* FAILED RETRANSMISSION REQUEST */
4571 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4572 break;
4573 case TCM_WRITE_PROTECTED:
4574 /* CURRENT ERROR */
4575 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004576 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004577 /* DATA PROTECT */
4578 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4579 /* WRITE PROTECTED */
4580 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4581 break;
4582 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4583 /* CURRENT ERROR */
4584 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004585 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004586 /* UNIT ATTENTION */
4587 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4588 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4589 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4590 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4591 break;
4592 case TCM_CHECK_CONDITION_NOT_READY:
4593 /* CURRENT ERROR */
4594 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004595 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004596 /* Not Ready */
4597 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4598 transport_get_sense_codes(cmd, &asc, &ascq);
4599 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4600 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4601 break;
4602 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4603 default:
4604 /* CURRENT ERROR */
4605 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004606 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004607 /* ILLEGAL REQUEST */
4608 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4609 /* LOGICAL UNIT COMMUNICATION FAILURE */
4610 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4611 break;
4612 }
4613 /*
4614 * This code uses linux/include/scsi/scsi.h SAM status codes!
4615 */
4616 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4617 /*
4618 * Automatically padded, this value is encoded in the fabric's
4619 * data_length response PDU containing the SCSI defined sense data.
4620 */
4621 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4622
4623after_reason:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004624 return cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004625}
4626EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4627
4628int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4629{
4630 int ret = 0;
4631
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004632 if (cmd->transport_state & CMD_T_ABORTED) {
Andy Grover6708bb22011-06-08 10:36:43 -07004633 if (!send_status ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004634 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4635 return 1;
4636#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07004637 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004638 " status for CDB: 0x%02x ITT: 0x%08x\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07004639 cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004640 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004641#endif
4642 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
Andy Grovere3d6f902011-07-19 08:55:10 +00004643 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004644 ret = 1;
4645 }
4646 return ret;
4647}
4648EXPORT_SYMBOL(transport_check_aborted_status);
4649
4650void transport_send_task_abort(struct se_cmd *cmd)
4651{
Nicholas Bellingerc252f002011-09-29 14:22:13 -07004652 unsigned long flags;
4653
4654 spin_lock_irqsave(&cmd->t_state_lock, flags);
4655 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4656 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4657 return;
4658 }
4659 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4660
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004661 /*
4662 * If there are still expected incoming fabric WRITEs, we wait
4663 * until until they have completed before sending a TASK_ABORTED
4664 * response. This response with TASK_ABORTED status will be
4665 * queued back to fabric module by transport_check_aborted_status().
4666 */
4667 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004668 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004669 cmd->transport_state |= CMD_T_ABORTED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004670 smp_mb__after_atomic_inc();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004671 }
4672 }
4673 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4674#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07004675 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
Andy Grovera1d8b492011-05-02 17:12:10 -07004676 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004677 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004678#endif
Andy Grovere3d6f902011-07-19 08:55:10 +00004679 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004680}
4681
Christoph Hellwige26d99a2011-11-14 12:30:30 -05004682static int transport_generic_do_tmr(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004683{
Andy Grover5951146d2011-07-19 10:26:37 +00004684 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004685 struct se_tmr_req *tmr = cmd->se_tmr_req;
4686 int ret;
4687
4688 switch (tmr->function) {
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004689 case TMR_ABORT_TASK:
Nicholas Bellinger3d289342012-02-13 02:38:14 -08004690 core_tmr_abort_task(dev, tmr, cmd->se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004691 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004692 case TMR_ABORT_TASK_SET:
4693 case TMR_CLEAR_ACA:
4694 case TMR_CLEAR_TASK_SET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004695 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4696 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004697 case TMR_LUN_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004698 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4699 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4700 TMR_FUNCTION_REJECTED;
4701 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004702 case TMR_TARGET_WARM_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004703 tmr->response = TMR_FUNCTION_REJECTED;
4704 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004705 case TMR_TARGET_COLD_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004706 tmr->response = TMR_FUNCTION_REJECTED;
4707 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004708 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004709 pr_err("Uknown TMR function: 0x%02x.\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004710 tmr->function);
4711 tmr->response = TMR_FUNCTION_REJECTED;
4712 break;
4713 }
4714
4715 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
Andy Grovere3d6f902011-07-19 08:55:10 +00004716 cmd->se_tfo->queue_tm_rsp(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004717
Christoph Hellwigb7b8bef2011-10-17 13:56:44 -04004718 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004719 return 0;
4720}
4721
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004722/* transport_processing_thread():
4723 *
4724 *
4725 */
4726static int transport_processing_thread(void *param)
4727{
Andy Grover5951146d2011-07-19 10:26:37 +00004728 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004729 struct se_cmd *cmd;
Jörn Engel8359cf42011-11-24 02:05:51 +01004730 struct se_device *dev = param;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004731
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004732 while (!kthread_should_stop()) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004733 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4734 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004735 kthread_should_stop());
4736 if (ret < 0)
4737 goto out;
4738
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004739get_cmd:
Andy Grover5951146d2011-07-19 10:26:37 +00004740 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4741 if (!cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004742 continue;
4743
Andy Grover5951146d2011-07-19 10:26:37 +00004744 switch (cmd->t_state) {
Christoph Hellwig680b73c2011-09-12 21:51:14 +02004745 case TRANSPORT_NEW_CMD:
4746 BUG();
4747 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004748 case TRANSPORT_NEW_CMD_MAP:
Andy Grover6708bb22011-06-08 10:36:43 -07004749 if (!cmd->se_tfo->new_cmd_map) {
4750 pr_err("cmd->se_tfo->new_cmd_map is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004751 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4752 BUG();
4753 }
Andy Grovere3d6f902011-07-19 08:55:10 +00004754 ret = cmd->se_tfo->new_cmd_map(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004755 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004756 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004757 break;
4758 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004759 ret = transport_generic_new_cmd(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07004760 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004761 transport_generic_request_failure(cmd);
4762 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004763 }
4764 break;
4765 case TRANSPORT_PROCESS_WRITE:
4766 transport_generic_process_write(cmd);
4767 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004768 case TRANSPORT_PROCESS_TMR:
4769 transport_generic_do_tmr(cmd);
4770 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004771 case TRANSPORT_COMPLETE_QF_WP:
Christoph Hellwige057f532011-10-17 13:56:41 -04004772 transport_write_pending_qf(cmd);
4773 break;
4774 case TRANSPORT_COMPLETE_QF_OK:
4775 transport_complete_qf(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004776 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004777 default:
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004778 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4779 "i_state: %d on SE LUN: %u\n",
4780 cmd->t_state,
Andy Grovere3d6f902011-07-19 08:55:10 +00004781 cmd->se_tfo->get_task_tag(cmd),
4782 cmd->se_tfo->get_cmd_state(cmd),
4783 cmd->se_lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004784 BUG();
4785 }
4786
4787 goto get_cmd;
4788 }
4789
4790out:
Nicholas Bellingerce8762f2011-10-09 02:19:01 -07004791 WARN_ON(!list_empty(&dev->state_task_list));
4792 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004793 dev->process_thread = NULL;
4794 return 0;
4795}